TreeViews events don't work.

BlitzMax Forums/BlitzMax Programming/TreeViews events don't work.

AntonyWells(Posted 2005) [#1]
I've created a tree view and it generates an event as expected when a node is either clicked or double clicked, but in both cases eventextra returns null rather than the node that was selected as the docs suggest.

What the @#!*?


Perturbatio(Posted 2005) [#2]
' createtreeview.bmx

Strict 

Local window:TGadget=CreateWindow("My Window",50,50,240,240)
Local treeview:TGadget=CreateTreeView(0,0,200,200,window)

SetGadgetLayout treeview,2,2,2,2

Local root:TGadget=TreeViewRoot(treeview)

Local help:TGadget=AddTreeViewNode("Help",root)
AddTreeViewNode "topic 1",help
AddTreeViewNode "topic 2",help
AddTreeViewNode "topic 3",help

Local projects:TGadget=AddTreeViewNode("Projects",root)
AddTreeViewNode "project 1",projects
AddTreeViewNode("project 2",projects)
AddTreeViewNode("project 3 is a big waste of time",projects)

While WaitEvent()
	Print CurrentEvent.ToString()
	Select EventID()
		Case EVENT_GADGETACTION, EVENT_GADGETSELECT
			Select EventSource()
				Case root
					ModifyTreeViewNode(TGadget(EventExtra()), "Selected")
			End Select
		Case EVENT_WINDOWCLOSE
			End
	End Select
Wend




AntonyWells(Posted 2005) [#3]
Still doesn't work. it generates the event but eventextra() always returns null.

Main

Strict 
Import "Map.bmx"
Import brl.bmploader
Import brl.jpgloader
SetGraphicsDriver GLMax2DDriver()
Global Main:TGadget = CreateWindow("Maptrix V0.1 (c)Dreamspaced 2005.",125,20,500,530,Null,WINDOW_TITLEBAR | WINDOW_MENU )
Global MapView:TGadget = CreateWindow("Map Tree",560,20,200,450,Null,WINDOW_TITLEBAR | WINDOW_STATUS )
Global Tools:TGadget = CreateWindow("Tools",20,105,100,450,Null,WINDOW_TITLEBAR | WINDOW_STATUS )
'Global MapWin:TGadget = CreateWindow("Current Map",125,105,450,400,Null,WINDOW_TITLEBAR | WINDOW_STATUS | WINDOW_MENU )
'--[ Maps ]

Global NewMap:TGadget = CreateButton("New Map",0,0,60,20,main)
Global LoadMap:TGadget = CreateButton("Load",60,0,60,20,main)
Global SaveMap:TGadget = CreateButton("Save",120,0,60,20,main)
Global SaveAsMap:TGadget = CreateButton("Save As",180,0,60,20,main)

Global MapCan:TGadget = CreateCanvas(0,40,ClientWidth(main),ClientHeight(main)-40,main,0,GRAPHICS_BACKBUFFER ) 

'--[ Tile Viewer ]

Global TileV:TGadget = CreateWindow("Tiles",120,350,500,200,main,WINDOW_TITLEBAR)
Global tilecan:Tgadget = CreateCanvas(100,0,300,200,tilev,0,graphics_backbuffer)
Global tiletree:TGadget = CreateTreeView(5,20,90,140,tilev)
CreateLabel("Tilesets.",0,0,80,20,tilev)
Global NewSet:TGadget = CreateButton("New Set.",405,20,70,20,tilev)
Global CloseSet:Tgadget = CreateButton("Delete",405,45,70,20,tilev)


Global TileSets:Map[255],NumSets
Global tilenode:Tgadget[255]
Global tileset:Map
For Local j=0 To 200
newTileSet()
Next
CreateTimer 60
SetGraphics CanvasGraphics( mapcan )
Local scrolllock
Local scrollx#,scrolly#,firstrun=True
Global EditMap:Map = map.create(0,0,0,0,0)
Local tileroot:Tgadget = TreeViewRoot(tiletree)
Function NewTileSet()
	tilesets[numsets] = map.create(8,128,1,64,64)
	tileset = tilesets[numsets]
	Local root:Tgadget = TreeViewRoot(tiletree)
	
	tilenode[numsets]=AddTreeViewNode("Tileset "+String(numsets+1),root)
	numsets:+1
	
End Function
'SelectedTreeViewNode

Repeat
	WaitEvent()
	Select EventID()
		Case EVENT_GADGETSELECT
			If EventSource()=tiletree
			'
			EndIf
			
		Case EVENT_GADGETACTION
			Select EventSource()
				Case NewMap
					NewMapWin()
				Case TileRoot
					Print EventData()
					Print EventExtra().tostring()
					For Local j=0 To numsets-1
						Local tn:tgadget = tgadget( EventExtra())
						If tn = tilenode[j]
						
						
							tileset = tilesets[j]
							Print "Set to node "+j
						End If
					Next
					Print "Done."
					
			End Select
			
		Case EVENT_WINDOWCLOSE
			Select EventSource()
				Case Main
					End			
			End Select
		Case EVENT_TIMERTICK
			SetGraphics CanvasGraphics( MapCan )
			Cls
			editmap.drawGrid()
			editmap.draw()
			Flip
			SetGraphics CanvasGraphics(Tilecan)
			Cls
			tileset.drawgrid()
			tileset.draw()
			Flip
		Case EVENT_MOUSEDOWN
			Select EventData()
				Case 2
					scrolllock=True
				Case 1
			End Select
		Case EVENT_MOUSEUP
			Select EventData()
				Case 2
					scrolllock=False
					firstrun=True
			End Select
		Case EVENT_MOUSEMOVE
			If scrolllock
			
				If firstrun
					scrollx=EventX()
					scrolly=EventY()
					firstrun=False
				End If
				
				Local xs# = EventX()-scrollx
				Local ys# = EventY()-scrolly
				scrollx=EventX()
				scrolly=EventY()
				Select EventSource()
					Case Mapcan
						editmap.scroll( -xs,-ys )
					Case tilecan
						SetGraphics CanvasGraphics(tilecan)
						tileset.scrollbound( -xs,-ys )
				End Select
				
			EndIf
		Case EVENT_MOUSEWHEEL
			'Print "First:"+tst.getscale()
			If EventSource() = mapcan
			editmap.setvscale( editmap.getvscale()+EventData()*0.03 )
			EndIf
			'Print tst.getscale()
			
	End Select
	
	
Forever

Function newMapwin()
	Local dg:TGadget = Desktop()
	Local dw = GadgetWidth(dg)
	Local dh = GadgetHeight(dg)
	JoyX
	Local nmwin:tgadget = CreateWindow("New Map",dw/2-110,dh/2-80,220,160,main,WINDOW_TITLEBAR)
	Local mwidth:TGadget=CreateTextField(10,10,40,20,nmwin)
	CreateLabel("Width",52,10,60,20,nmwin)
	Local mheight:TGadget = CreateTextField(105,10,40,20,nmwin)
	CreateLabel("Height",147,10,60,20,nmwin)
	Local mdepth:TGadget=CreateTextField(10,35,40,20,nmwin)
	CreateLabel("Depth",52,35,60,20,nmwin)
	Local twidth:Tgadget=CreateTextField(10,60,40,20,nmwin)
	CreateLabel("Tile Width",52,60,60,20,nmwin)
	Local theight:TGadget=CreateTextField(105,60,40,20,nmwin)
	CreateLabel("Tile Height",147,60,60,20,nmwin)
	Local ok:Tgadget = CreateButton("Create",10,90,80,20,nmwin)
	Local cancel:TGadget = CreateButton("Cancel",100,90,80,20,nmwin)
	SetGadgetText mwidth,"32"
	SetGadgetText mheight,"32"
	SetGadgetText mdepth,"4"
	SetGadgetText twidth,"64"
	SetGadgetText theight,"64"
	Repeat
		WaitEvent()
		Select EventID()
			Case EVENT_WINDOWCLOSE
				If EventSource()=nmwin
					FreeGadget nmwin
					Return 
				End If
			Case EVENT_GADGETACTION
				Select EventSource()
					Case cancel
						FreeGadget nmwin
						Return
					Case ok
						Local width,height,depth,widtht,heightt
						width =Int( TextFieldText(mwidth))
						height = Int(TextFieldText(mheight))
						depth = Int(TextFieldText(mdepth))
						widtht = Int(TextFieldText(twidth))
						heightt = Int(TextFieldText(Theight))
						If width<1 width=1
						If height<1 height=1
						If depth<1 depth=1
						If widtht<4 widtht=4
						If heightt<4 heightt=4
						editmap = map.create(width,height,depth,widtht,heightt)
						FreeGadget nmwin
						Return 	
				End Select
				
		End Select
		
	Forever
							
End Function



Map.bmx

Strict
Import brl.bmploader
Import brl.jpgloader
Type Tile
	Field frame:TImage[255],frames
	Field fps,mode,framenum
	Function Load:Tile(file:String)
		Local out:Tile = New tile
		out.frame[0] = LoadImage(file)
		If out.frame[0] = Null 
			Throw "Tile image failed to load."
		End If
		out.frames=1
		tile.tiles.addlast( out )
		Return out
	End Function
	Method Draw(x#,y#,w,h)
		DrawImageRect frame[framenum],x,y,w,h
	End Method
	Global tiles:Tlist
End Type
tile.tiles = CreateList()

Type Map
	Field map:Tile[,,]
	Field high:Int[,]
	Field width,height,depth
	Field twidth,theight
	Field ViewX#,ViewY#,ViewScale#
	Method New()
		viewscale=1
	End Method
	Function Create:Map(width,height,depth,tilewidth,tileheight)
	
		Local out:Map = New map
			out.width=width
			out.height=height
			out.depth=depth
			out.twidth=tilewidth
			out.theight=tileheight
			out.map = New tile[width,height,depth]
			out.high = New Int[width,height]
		Return out	
		
	End Function
	
	Method PasteTile(x,y,d,ti:tile)
		map[x,y,d]=ti
	End Method
	

	Method DrawGrid()
		
		SetColor 0,255,255
		SetLineWidth 4
		Local mw#,mh#
		mw=GraphicsWidth()/2.0
		mh=GraphicsHeight()/2.0
		For Local x=0 Until width
			Local dx# = x*twidth-viewx
			Local nx# = dx-mw
			dx = mw+nx*viewscale
		For Local y=0 Until height
			Local dy# = y*theight-viewy
			Local ny# = dy-mh
			dy = mh+ny*viewscale
			DrawLine dx,dy,dx+twidth*viewscale,dy
			DrawLine dx,dy,dx,dy+theight*viewscale
			DrawLine dx,dy+theight*viewscale,dx+twidth*viewscale,dy+theight*viewscale
			DrawLine dx+twidth*viewscale,dy,dx+twidth*viewscale,dy+theight*viewscale
		Next
		Next
		
	End Method
	
		
	Method Draw()
	
		SetScale viewscale,viewscale
		Local mw#,mh#
		mw=GraphicsWidth()/2.0
		mh=GraphicsHeight()/2.0
		Local gw,gh
		gw= GraphicsWidth()
		gh = GraphicsHeight()
		For Local x=0 Until width
			Local dx# = x*twidth-viewx
			Local nx# = dx-mw
			dx = mw+nx*viewscale
			If dx>0-twidth*viewscale And dx<gw+twidth*viewscale
		For Local y=0 Until height
			Local dy# = y*theight-viewy
			Local ny# = dy-mh
			dy = mh+ny*viewscale
			If dy>0-theight*Viewscale And dy<gh+theight*viewscale
			For Local d=0 Until depth
				If map[x,y,d]<>Null	map[x,y,d].draw(dx,dy,twidth,theight)
			Next
			
			If high[x,y]			
				DrawLine dx,dy,dx+twidth*viewscale,dy
				DrawLine dx,dy,dx,dy+theight*viewscale
				DrawLine dx,dy+theight*viewscale,dx+twidth*viewscale,dy+theight*viewscale
				DrawLine dx+twidth*viewscale,dy,dx+twidth*viewscale,dy+theight*viewscale
				high[x,y]=0
			EndIf
			EndIf
			
			
		Next
		EndIf
		Next
		SetScale 1,1
	End Method
	
	Method SetvScale(scale#)
		viewscale=scale
	End Method
	Method GetvScale#()	
		Return viewscale
	End Method
	Method scroll(mx#,my#)
		viewx=viewx+mx/viewscale
		viewy=viewy+my/viewscale
	End Method
	Method scrollbound(mx#,my#)
		viewx :+mx/viewscale
		viewy :+my/viewscale
		If viewx<0 viewx=0
		If viewy<0 viewy=0
		If viewx>width*Twidth-GraphicsWidth()
			viewx=width*Twidth-GraphicsWidth()
		EndIf
		If viewy>height*Theight-GraphicsHeight()
			viewy=height*Theight-GraphicsHeight()
		EndIf

	End Method
	
	

End Type





Compiled in the demo version of the gui.


Perturbatio(Posted 2005) [#4]
ahhh... I see. There have been several updates to the GUI since the demo was released.


AntonyWells(Posted 2005) [#5]
Plus your example crashes when I select a node because it's being returned as null.

Any chance of an updated demo that works?


Perturbatio(Posted 2005) [#6]
If it's any consolation, the code above compiles and runs fine for me.


AntonyWells(Posted 2005) [#7]
I'm singing in the rain here.

I take it your using the commercial version then? Sodding demo. sodding world. sodding sod. sod.


Perturbatio(Posted 2005) [#8]
yes, commercial version.