-------------------------------------------------------------------------
-- AproposGUI.ms
-- Create's a GUI for the apropos function.
-- By Simon Feltman [discreet]  simon.feltman@autodesk.com
-------------------------------------------------------------------------
global g_aproposWidth = 340
global g_aproposHeight = 400

-- change this to the location of aproposGui.ms
global g_aproposGuiPath = scriptsPath + "startup\\AproposGUI.ms"

Rollout aproposRollout "AproposGUI"
(
	EditText ed_apropos text:"*" width:(g_aproposWidth-98) height:20 align:#center offset:[-32,0]
	Button  btn_apropos "Apropos" width:56 height:17 \
		pos:[ed_apropos.pos.x+(g_aproposWidth-98),ed_apropos.pos.y]

	EditText ed_find width:(g_aproposWidth-98) height:20 align:#center offset:[-32,0]
	Button  btn_find "Find Next" width:56 height:17 \
		pos:[ed_find.pos.x+(g_aproposWidth-98),ed_find.pos.y]

	ListBox  lb_results width:(g_aproposWidth-40) height:((g_aproposHeight-150)/13) align:#center
	EditText ed_selected width:(g_aproposWidth-38) height:20 align:#center offset:[-2,0]

	-------------------------------------------------------------------------
	function StripBackslashes str =
	(
		if str == undefined then str = ""
		while str.count > 0 do
		(
			if str[str.count] == "\\" then
			(
				if str.count == 1 then str = ""
				else str = SubString str 1 (str.count-1)
			)
			else exit
		)
		return str
	)

	-------------------------------------------------------------------------
	function DoApropos =
	(
		local resultsList = #()
		local stream = ("" as StringStream)

		Apropos ed_apropos.text to:stream
		Seek stream 0

		while(not eof stream) do
			Append resultsList (ReadLine stream)

		Sort resultsList
		lb_results.items = resultsList

		if resultsList.count > 0 then
		(
			lb_results.selection = 1
			ed_selected.text = lb_results.selected
		)
		else ed_selected.text = ""
	)

	-------------------------------------------------------------------------
	function FindNext =
	(
		local start = 1
		if lb_results.selection > 0 and lb_results.selection <= lb_results.items.count then
			start = lb_results.selection + 1

		for i = start to lb_results.items.count do
		(
			if (FindString (lb_results.items[i]) ed_find.text) != undefined then
			(
				lb_results.selection = i
				ed_selected.text = lb_results.selected
				exit
			)

		)
	)

	-------------------------------------------------------------------------
	on ed_apropos entered str do DoApropos()
	on btn_apropos pressed do DoApropos()

	on ed_find entered str do FindNext()
	on btn_find pressed do FindNext()

	on lb_results selected sel do
		ed_selected.text = lb_results.selected

	on lb_results doubleclicked sel do
	(
		stream = ("" as StringStream)
		str = lb_results.selected
		end = FindString str " "
		if end != undefined then str = SubString lb_results.selected 1 (end-1)
		Format "ShowSource %" str to:stream
		Execute (stream as String)
	)
)

-------------------------------------------------------------------------
function AproposGUI width:g_aproposWidth height:g_aproposHeight =
(
	g_aproposWidth = width
	g_aproposHeight = height

	-- This is kind of a hack so you can have multiple instances of the same rollouts.
	-- Only problem with this is you have to have the script in your scripts path.
	try (
		FileIn (g_aproposGuiPath)
	)
	catch (
		format "warning: unable to re-load AproposGUI.ms, cannot use multiple instances.\n"
	)

	aproposFloater = (NewRolloutFloater "AproposGUI" width height)
	AddRollout aproposRollout aproposFloater
)

-------------------------------------------------------------------------
MacroScript AproposGUI
	category:"Tools"
	tooltip:"AproposGUI"
	buttontext:"AproposGUI"
(
	AproposGUI()
)