Dialog + treegadget: change icons on the fly with double/simple clic

Share your advanced PureBasic knowledge/code with the community.
Mesa
Enthusiast
Enthusiast
Posts: 349
Joined: Fri Feb 24, 2012 10:19 am

Dialog + treegadget: change icons on the fly with double/simple clic

Post by Mesa »

No api so it should be multiplatform...

Code: Select all


Runtime Enumeration Windows
	#win
EndEnumeration

Runtime Enumeration Gadgets
	#tree
EndEnumeration

Enumeration 
	#Closed
	#Open
	#sheet
EndEnumeration

Declare tree_OnEvent()
Declare tree_OnChange()
Declare tree_OnLeftclic()
Declare tree_OnLeftDoubleClic()

Structure StrL
	Index.s
	IconID.i
	Level.i
EndStructure

Global NewList Liste.StrL()
Global dc

CreateImage(#Closed, 16,16,24,#Red)
CreateImage(#Open, 16,16,24,#Green)
CreateImage(#sheet, 16,16,24,#Yellow)

AddElement(Liste()):Liste()\Index = "Sheet 1":Liste()\IconID =  ImageID(#sheet):Liste()\Level = 0 	
AddElement(Liste()):Liste()\Index = "Sheet 2":Liste()\IconID =  ImageID(#sheet):Liste()\Level =0
AddElement(Liste()):Liste()\Index = "Book 1" :Liste()\IconID =  ImageID(#Closed):Liste()\Level =0
AddElement(Liste()):Liste()\Index = "Sheet 3":Liste()\IconID =  ImageID(#sheet):Liste()\Level =1
AddElement(Liste()):Liste()\Index = "Sheet 4":Liste()\IconID =  ImageID(#sheet):Liste()\Level =1
AddElement(Liste()):Liste()\Index = "Book 2" :Liste()\IconID =  ImageID(#Closed):Liste()\Level =0
AddElement(Liste()):Liste()\Index = "Sheet 5":Liste()\IconID =  ImageID(#sheet):Liste()\Level = 1
AddElement(Liste()):Liste()\Index = "Sheet 6":Liste()\IconID =  ImageID(#sheet):Liste()\Level = 1
AddElement(Liste()):Liste()\Index = "Sheet 7":Liste()\IconID =  ImageID(#sheet):Liste()\Level = 1
AddElement(Liste()):Liste()\Index = "Sheet 8":Liste()\IconID =  ImageID(#sheet):Liste()\Level = 0
AddElement(Liste()):Liste()\Index = "Sheet 9":Liste()\IconID =  ImageID(#sheet):Liste()\Level = 0

Procedure.s GetXMLString()
	Protected XML$
	
	XML$ + "<?xml version='1.0' encoding='UTF-16'?>"
	XML$ + ""
	XML$ + "<dialogs><!--Created by Dialog Design0R V1.84 => get it from: https://hex0rs.coderbu.de/en/sdm_downloads/dialogdesign0r/-->"
	XML$ + "  <window flags='#PB_Window_SystemMenu | #PB_Window_ScreenCentered' name='window' xpos='0' ypos='0' id='#win'>"
	XML$ + "    <tree width='200' height='300' id='#tree' onevent='tree_OnEvent()' onchange='tree_Onchange()' onleftclick='tree_OnLeftclic()' onleftdoubleclick='tree_OnLeftDoubleClic()'/> "
	XML$ + "  </window>"
	XML$ + "</dialogs><!--DDesign0R Definition: PureBasic|1|1|1|_|example_with_declares|1-->"
	
	ProcedureReturn XML$
EndProcedure

;Test it
CompilerIf #PB_Compiler_IsMainFile
	
	
	Runtime Procedure tree_OnEvent()
		
		For i =0 To CountGadgetItems(#tree)-1
			If GetGadgetItemData(#tree, i) = 1
				If GetGadgetItemState(#tree,i) & #PB_Tree_Expanded
					SetGadgetItemImage(#tree, i, ImageID(#Open))
				ElseIf GetGadgetItemState(#tree,i) & #PB_Tree_Collapsed
					SetGadgetItemImage(#tree, i, ImageID(#Closed))
				EndIf
			EndIf
		Next i
		
		If dc=1
			i =GetGadgetState(#tree)
			If GetGadgetItemData(#tree, i) = 1
				If GetGadgetItemState(#tree,i) & #PB_Tree_Collapsed
					SetGadgetItemImage(#tree, i, ImageID(#Open))
				ElseIf GetGadgetItemState(#tree,i) & #PB_Tree_Expanded
					SetGadgetItemImage(#tree, i, ImageID(#Closed))
				EndIf				
			EndIf
			dc=0
		EndIf
		
	EndProcedure
	
	Runtime Procedure tree_Onchange()
		
	EndProcedure
	
	Runtime Procedure tree_OnLeftclic()
		
	EndProcedure
	
	Runtime Procedure tree_OnLeftDoubleClic()
		
		dc=1
		
	EndProcedure
	
	
	Procedure FillTree()
		
		ForEach Liste()
			AddGadgetItem(#tree, -1, Liste()\Index, Liste()\IconID, Liste()\Level)
		Next
		
		SetGadgetItemData(#tree, 2, 1)
		SetGadgetItemData(#tree, 5, 1)
		
	EndProcedure
	
	
	
	a$ = GetXMLString()
	If ParseXML(0, a$) And XMLStatus(0) = #PB_XML_Success
		
		CreateDialog(i)
		
		R = OpenXMLDialog(i, 0, "window")
		
		If R
			FillTree()
			While WaitWindowEvent() <> #PB_Event_CloseWindow : Wend
		Else
			Debug DialogError(i)
		EndIf
		
	Else
		Debug XMLStatus(0)
		Debug XMLError(0)
	EndIf
	
	
CompilerEndIf

;(c) HeX0R 2022



Mesa
User avatar
idle
Always Here
Always Here
Posts: 5081
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Dialog + treegadget: change icons on the fly with double/simple clic

Post by idle »

nice thanks
Post Reply