GUI; treeview and controls

Discuss the future of the AutoHotkey language
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

GUI; treeview and controls

25 Aug 2019, 09:48

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.
Attachments
Image2.jpg
Image2.jpg (21.13 KiB) Viewed 2928 times
Image1.jpg
Image1.jpg (28.73 KiB) Viewed 2928 times
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: GUI; treeview and controls

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
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: GUI; treeview and controls

27 Aug 2019, 09:20

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()
}
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: GUI; treeview and controls

27 Aug 2019, 10:42

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()
}
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: GUI; treeview and controls

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.
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: GUI; treeview and controls

29 Aug 2019, 05:10

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?
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: GUI; treeview and controls

30 Aug 2019, 03:44

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.
User avatar
Maestr0
Posts: 136
Joined: 05 Dec 2013, 17:43

Re: GUI; treeview and controls

30 Aug 2019, 09:04

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!

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 18 guests