Simple listboxes

BlitzMax Forums/BlitzMax Beginners Area/Simple listboxes

Hi there

I'd thought I'd share with you a bit of code that can be used to create list boxes, which I did when I should have been working this morning!


Strict

Global C_ScreenWidth# = 640, C_ScreenHeight# = 480
Global C_ScreenMidX# =C_ScreenWidth/2
Global C_ScreenMidY# =C_ScreenHeight/2

Global hertz# = 32
Global counter: Int

Global TitleWidth#,TitleHeight#
Graphics C_ScreenWidth, C_ScreenHeight ,0,hertz

Type listbox
	
	Global control_width:Int = 200
	Global control_active_area_width:Int = 18
	Global control_active_area_height:Int = 13

	Field x:Int
	Field y:Int
	Field w:Int
	Field h:Int
	Field list:TList = CreateList()
	Field listItem:Int
	
	Method Arse(h:Int)
	
	End Method
	
	Method hitControl(mX:Int, mY:Int)
	
		If mX >= (lb.x + (lb.w - control_active_area_width)) And mX =< (lb.x + lb.w)
			
		If MouseHit(1)
		
			If mY > (lb.y ) And mY < (lb.y + 12)
			
				lb.listitem:+1
				
				If lb.listItem >= CountList(lb.list)
				
					lb.listItem = 0
				
				End If
			
			End If

			If mY > (lb.Y + 12) And mY < (lb.y + lb.h)
			
				lb.listitem:-1
				
				If lb.listItem < 0
										
					lb.listItem = CountList(lb.list) - 1
				
				End If
			
			End If
			
		End If
		
	End If
	
	End Method
	
	Method New()	'constructor
		
		h = 20	
		w = control_width
		
	End Method
	
	Method drawControl()
	
		SetColor 255,255,255	'outer rect
		DrawRect lb.x, lb.y, lb.w, lb.h
		
		SetColor 0,0,0	'inner rect
		DrawRect lb.x + 1, lb.y + 1, lb.w - 22, lb.h - 2
		
		SetColor 0,0,0	'draw control rectangle
		DrawRect lb.x + (lb.w - control_active_area_width - 1), lb.y + 1, lb.w - (lb.w - (control_active_area_width - 1)) , lb.h - 2

		SetColor 255,255,255	'split it
		DrawRect lb.x + (lb.w - control_active_area_width - 1), lb.y + (lb.h / 2) , lb.w - (lb.w - (control_active_area_width - 1)), 1
	
		SetColor 255,255,255	'draw text in box
		DrawText "+", lb.x + (lb.w - control_active_area_height - 2), lb.y - 2 ' draw spin up		
		DrawText "-", lb.x + (lb.w - control_active_area_height - 2), lb.y + (control_active_area_height / 1.8)  ' draw spin down
		DrawText String(lb.list.valueatindex (lb.listitem)), lb.x + 2, lb.y + 2	'draw selected text
	
	End Method
	
End Type

Global c:TList = CreateList()

Global lb:listbox = New listbox




lb:listbox = New listbox
lb.x = 50
lb.y = 50
lb.list.addlast "floor1"
lb.list.addlast "floor2"
lb.list.addlast "floor3"

c.addlast lb

lb:listbox = New listbox
lb.x = 250
lb.y = 250
lb.list.addlast "room1"
lb.list.addlast "room2"
lb.list.addlast "room3"

c.addlast lb

While Not KeyHit(key_escape)

	Cls
	
		ListBoxes

	Flip
  	FlushMem()
  
Wend

Function ListBoxes()

	For lb = EachIn c
	
		lb.drawControl
		lb.hitControl (MouseX(), MouseY())
	
	Next

End Function



It's probably hopelessly inneficient and badly coded, but it works for me :)

Here is some code to create simple code boxes :)
{code}{/code}

replace {} with []

<codebox></codebox> for long sections of code, while we're being politely unuseful =] replacing <> with [] of course

Changes made as requested. Howsabout being a bit more polite fellas, the word "please" doesn't cost anything. :)