Page 1 of 1

GUI; treeview and controls

Posted: 25 Aug 2019, 09:48
by Maestr0
Are there plans to adjust the GUI in such a way that a treeview can easily be bound to GUI controls?
By that I mean that selecting a treeview on the left of a GUI will hide all controls not bound by that item, and show all controls that are bound by that item.

If there already is a good way to do this, please let me know.

Re: GUI; treeview and controls

Posted: 25 Aug 2019, 14:05
by tmplinshi
An invisible Tab control would do the trick.

I haven't used AHK V2, the following is v1 code.

Code: Select all

global g_tabIndex := {}

Gui, Add, TreeView, w200 gMyTree vMyTree
	P1 := TVAdd("First parent")
	P1C1 := TVAdd("Parent 1's first child", P1)
Gui, Add, Tab, x+0 w0 h0 vHiddenTab, a|b
	Gui, Tab, 1
	Gui, Add, Text, xp+20 yp+30, Page 1
	Gui, Tab, 2
	Gui, Add, Text, xp+20 yp+30, Page 2
Gui, Show
return

MyTree:
	idx := g_tabIndex[ TV_GetSelection() ]
	GuiControl, Choose, HiddenTab, |%idx%
	GuiControl, Focus, MyTree
Return

TVAdd(p*) {
	id := TV_Add(p*)
	g_tabIndex[id] := g_tabIndex.Count() + 1
	return id
}

GuiClose:
ExitApp

Re: GUI; treeview and controls

Posted: 27 Aug 2019, 09:20
by Maestr0
tmplinshi wrote:
25 Aug 2019, 14:05
An invisible Tab control would do the trick.

I haven't used AHK V2, the following is v1 code.

Code: Select all

global g_tabIndex := {}

Gui, Add, TreeView, w200 gMyTree vMyTree
	P1 := TVAdd("First parent")
	P1C1 := TVAdd("Parent 1's first child", P1)
Gui, Add, Tab, x+0 w0 h0 vHiddenTab, a|b
	Gui, Tab, 1
	Gui, Add, Text, xp+20 yp+30, Page 1
	Gui, Tab, 2
	Gui, Add, Text, xp+20 yp+30, Page 2
Gui, Show
return

MyTree:
	idx := g_tabIndex[ TV_GetSelection() ]
	GuiControl, Choose, HiddenTab, |%idx%
	GuiControl, Focus, MyTree
Return

TVAdd(p*) {
	id := TV_Add(p*)
	g_tabIndex[id] := g_tabIndex.Count() + 1
	return id
}

GuiClose:
ExitApp
Thanks, good idea!
I've not gotten it to work exactly (the controls in v2 have to be inside the tab, it seems), but this is what I came up with thusfar (Tab works, Tab3 doesn't). Feedback welcome!

Code: Select all

#persistent

gui()
return

gui() {
	g_tabIndex := Map()

	GUI_Settings := GuiCreate("+LastFound",A_ScriptName " : Settings")
	TV := GUI_Settings.Add("TreeView","y5 r25 w150 section")
		P1 := TVAdd(TV,"General",, "expand select")
		P1C1 := TVAdd(TV,"View", P1)
		P2 := TVAdd(TV,"Settings")
	Tab := GUI_Settings.Add("Tab", "x+0 w0 h0 vHiddenTab", "General|View|Settings")
		Tab.UseTab(1)
		GUI_Settings.Add("Text", "x170 yp+30 w200", "Page 1")
		Tab.UseTab(2)
		GUI_Settings.Add("Text", "xp+20 yp+30 w200", "Page 2")
		Tab.UseTab(3)
		GUI_Settings.Add("Text", "xp+20 yp+30 w200", "Page 3")
	GUI_Settings.Show()

	TV.OnEvent('ItemSelect', Func('TV_click').Bind(GUI_Settings,TV))

	TV_click(gui, this, *) {
		idx := g_tabIndex[ this.GetSelection() ]
		gui["HiddenTab"].Choose(idx)
		this.Focus()
		return
	}

	TVAdd(TV,p*) {
		id := TV.Add(p*)
		g_tabIndex[id] := g_tabIndex.Count + 1
		return id
	}
}

GuiClose() {
	ExitApp()
}

Re: GUI; treeview and controls

Posted: 27 Aug 2019, 10:42
by Maestr0
oh, and here's a version (Tab2 works as well) that will work with a103 as well as a104:

Code: Select all

#persistent

gui()
return

gui() {
	if ( A_AHKVersion >= "2.0-a104-3e7a969d" )
		g_tabIndex := Map()
	else
		g_tabIndex := {}

	GUI_Settings := GuiCreate("+LastFound",A_ScriptName " : Settings")
	TV := GUI_Settings.Add("TreeView","y5 r25 w150 section")
		P1 := TVAdd(TV,"General",, "expand select")
		P1C1 := TVAdd(TV,"View", P1)
		P2 := TVAdd(TV,"Settings")
	Tab := GUI_Settings.Add("Tab2", "x+0 w0 h0 vHiddenTab", "General|View|Settings")
		Tab.UseTab(1)
		GUI_Settings.Add("Text", "x170 yp+30 w200", "Page 1")
		Tab.UseTab(2)
		GUI_Settings.Add("Text", "xp+20 yp+30 w200", "Page 2")
		Tab.UseTab(3)
		GUI_Settings.Add("Text", "xp+20 yp+30 w200", "Page 3")
	GUI_Settings.Show()

	TV.OnEvent('ItemSelect', Func('TV_click').Bind(GUI_Settings,TV))
	return

	TV_click(gui, this, *) {
		idx := g_tabIndex[ this.GetSelection() ]
		gui["HiddenTab"].Choose(idx)
		this.Focus()
		return
	}

	TVAdd(TV,p*) {
		id := TV.Add(p*)
		if ( A_AHKVersion >= "2.0-a104-3e7a969d" )
			g_tabIndex[id] := g_tabIndex.Count + 1
		else
			g_tabIndex[id] := g_tabIndex.Count() + 1
		return id
	}
}

GuiClose() {
	ExitApp()
}

Re: GUI; treeview and controls

Posted: 29 Aug 2019, 04:10
by lexikos
This is outside the purview of the TreeView control (and it's already easy to do).


Note: A script which contains Map() won't load on a103 if that function is not defined. However, Map.new() is permitted. You could check for the presence or type of Map instead of checking the version number.

Re: GUI; treeview and controls

Posted: 29 Aug 2019, 05:10
by Maestr0
lexikos wrote:
29 Aug 2019, 04:10
This is outside the purview of the TreeView control (and it's already easy to do).


Note: A script which contains Map() won't load on a103 if that function is not defined. However, Map.new() is permitted. You could check for the presence or type of Map instead of checking the version number.
Thank you for your reply, as always it's very useful to me.

Easy to do if you know how, yes :) An example on how to do it might be a useful addition to the help files.

As for your note... could you give an example?

Something else: instead of setting an onevent for each guictrl object, is there a way to set a default onevent?

Re: GUI; treeview and controls

Posted: 30 Aug 2019, 03:44
by lexikos

Code: Select all

mymap := Map ? Map.new() : {}
This does have the drawback of triggering a warning if #Warn UseUnset is enabled. You can use A_AhkVersion as before and just replace Map() with Map.new() to permit the script to load on v2.0-a103. Alternatively,

Code: Select all

static mapf := IsFunc("Map") ? Func("Map") : Func("Object")
mymap := %mapf%()

Even if you don't know that you can utilize the Tab control in that way, it is simple to hide and show controls.

Default OnEvent: No, that doesn't even make sense. Some controls have multiple event types. All controls allow multiple handlers for one event. An event handler which is installed by default would still be called after registering other event handlers. You can already achieve that same behaviour by enumerating the controls with for and calling OnEvent.

Re: GUI; treeview and controls

Posted: 30 Aug 2019, 09:04
by Maestr0
Again, much appreciated!
As for the default onevent, I meant for each type, not for all, I guess I should have specified.
I'd used the enumeration method already, thanks for that, guess that's what I'll keep using:

Code: Select all

	GuiSetOnEvent(GuiObj,*) {	; this function sets the onevent for each of the GUI items
		clickcontrols := "Text, Pic, CheckBox, Radio, ListView, TreeView, Link, StatusBar" ; Button can also do an onEvent, but set that one separately
		changecontrols := "Edit"
		For Hwnd, GuiCtrlObj in GuiObj
		{
			if InStr(clickcontrols, GuiCtrlObj.Type)	; because OnEvent click only works on certain controls, this prevents an error. The controls that allow for OnEvent are Text, Pic, Button, CheckBox, Radio, ListView, TreeView, Link, StatusBar.
				GuiCtrlObj.OnEvent("Click","GUI_Event")
			else if InStr(changecontrols, GuiCtrlObj.Type)	; because OnEvent click only works on certain controls, this prevents an error. The controls that allow for OnEvent are Text, Pic, Button, CheckBox, Radio, ListView, TreeView, Link, StatusBar.
				GuiCtrlObj.OnEvent("Change","GUI_Event")
		}
	}
Thank you for everything you're doing for this community, Lexikos!