4. Property ListsWe'll need dialog boxes. Potentially a lot.
Those who have already programmed dialog boxes with Blue know, it takes a lot of time to write.
So we need a way to speed up the process, this way is the property list.
Instead of creating and handling the gadgets individually, we use
predefined blocks of gadgets arranged sequentially:
How does it works:The property list is created easyly:
+ Code Snippet...
PropList = CreatePropList(0, 0, 320, 320, UIFont, UIBoldFont, AppWindow)
PropListCell_Edit = PropList_AddEdit(PropList, "Edit", "My text")
PropListCell_Label = PropList_AddLabel(PropList, "Label")
PropListCell_Boolean = PropList_AddBoolean(PropList, "Boolean", true)
PropListCell_Button = PropList_AddButton(PropList, "Button")
PropListCell_Vector = PropList_AddVector(PropList, "Vector", 1.0, 2.0, 3.0)
PropListCell_ComboBox = PropList_AddComboBox(PropList, "ComboBox", "Item 1;Item 2;Item 3;Item 4")
PropListCell_Color = PropList_AddColorRGB(PropList, "Color", 0x800000)
PropList_AdjustHeight(PropList)
ResizeGadget AppWindow, GadgetClientWidth(PropList), GadgetClientHeight(PropList), 1
...
Remember, the event queue is emptied at the beginning of the main loop and the event are stored in variables
+ Code SnippetRem - HGUI Event Queue -
For EventQueueEmpty = false To true
GetEvent
_EventSource = EventSource():_EventType = EventType()
_EventData = EventData(): _EventDataEx = EventDataEx()
If _EventType = WINDOW_CLOSE Then EventCloseWindow = _EventSource
If _EventType = KEYDOWN
If _EventData = 13 Then EventReturnKeyDownGadget = _EventSource
EndIf
If _EventType = LEFTBUTTON_DOWN Then EventMouseDownGadget = _EventSource
If _EventType = LEFTBUTTON_UP Then EventMouseUpGadget = _EventSource
If _EventType = LEFTBUTTON_DBLCLICK Then EventMouseDblcGadget = _EventSource
EventQueueEmpty = (_EventType = 0)
Next EventQueueEmpty
Interacting with the property list is just as easy.
The proplist is provided with a few events: "Validate", "Edit", "Click" and "Select".
then,
the PropList_GetEvents() function runs the internal function of all the cells to figure out if one has changed:
+ Code Snippet If EventReturnKeyDownGadget Then PropList_ValidateGadget(PropList, EventReturnKeyDownGadget)
If EventMouseDownGadget Then PropList_EditGadget(PropList, EventMouseDownGadget)
If EventMouseUpGadget Then PropList_ClickGadget(PropList, EventMouseUpGadget)
If EventMouseDblcGadget Then PropList_SelectGadget(PropList, EventMouseDblcGadget)
If PropList_GetEvents(PropList)
If PropListCell_IsChanged(PropListCell_Edit) Then ...
If PropListCell_IsChanged(PropListCell_Boolean) Then ...
If PropListCell_IsChanged(PropListCell_Button) Then ...
...
EndIf
Simple and quick!!
The demoIn this step, only the "Edit" and "Label" are available. But other cell types will be regularly added in the next step!
+ Code SnippetRem * PropertyListDemo *
#Constant false 0
#Constant true 1
Global AppRequestToClose
Global AppWindow
Global AppTitle$
Global AppIsActive
Global UIFont
Global UIBoldFont
Open Console
AppTitle$ = "PropertyListDemo"
Style = WINDOW_TITLEBAR+WINDOW_CLOSEBUTTON+WINDOW_MAXBUTTON+WINDOW_MINBUTTON
AppWindow = CreateWindow(15, 16, 320, 320, AppTitle$, Style, 0, 1, 0)
UIFont = CreateFont("Tahoma", 8, 0, 0, 0)
UIBoldFont = CreateFont("Tahoma", 8, 1, 0, 0)
PropList = CreatePropList(0, 0, 320, 320, UIFont, UIBoldFont, AppWindow)
PropListCell_Edit = PropList_AddEdit(PropList, "Edit", "My text")
PropListCell_Label = PropList_AddLabel(PropList, "Label")
PropListCell_Edit2 = PropList_AddEdit(PropList, "Edit2", "My text")
PropList_AdjustHeight(PropList)
ResizeGadget AppWindow, GadgetClientWidth(PropList), GadgetClientHeight(PropList), 1
Rem * Main Loop *
AppExit = false
Do
EventCloseWindow = 0
EventReturnKeyDownGadget = 0
EventMouseDownGadget = 0
EventMouseUpGadget = 0
Rem - HGUI Event Queue -
For EventQueueEmpty = false To true
GetEvent
_EventSource = EventSource():_EventType = EventType()
_EventData = EventData(): _EventDataEx = EventDataEx()
If _EventType = WINDOW_CLOSE Then EventCloseWindow = _EventSource
If _EventType = KEYDOWN
If _EventData = 13 Then EventReturnKeyDownGadget = _EventSource
EndIf
If _EventType = LEFTBUTTON_DOWN Then EventMouseDownGadget = _EventSource
If _EventType = LEFTBUTTON_UP Then EventMouseUpGadget = _EventSource
EventQueueEmpty = (_EventType = 0)
Next EventQueueEmpty
Rem - App Close Button -
If EventCloseWindow = AppWindow Then Exit
Rem - Property List -
If EventReturnKeyDownGadget Then PropList_ValidateGadget(PropList, EventReturnKeyDownGadget)
If EventMouseDownGadget Then PropList_EditGadget(PropList, EventMouseDownGadget)
If EventMouseUpGadget Then PropList_ClickGadget(PropList, EventMouseUpGadget)
If PropList_GetEvents(PropList)
If PropListCell_IsChanged(PropListCell_Edit) Then Print Console "Edit changed: "+PropListCell_GetText(PropListCell_Edit, 0), CRLF$()
If PropListCell_IsChanged(PropListCell_Edit2) Then Print Console "Edit2 changed: "+PropListCell_GetText(PropListCell_Edit2, 0), CRLF$()
EndIf
Rem - Loop End -
If AppRequestToClose Then Exit
Loop
AppEnd:
End
Rem > Use at Least one memblock command when using BlueGUI image features..
Make Memblock 1, 8
The Proplist functions:
+ Code SnippetRem * PropList.dba *
#Constant PROPLIST_FIRSTCELL 0
#Constant PROPLIST_LASTCELL 4
#Constant PROPLIST_FONT 8
#Constant PROPLIST_BOLDFONT 12
#Constant PROPLIST_LABELWIDTH 16
#Constant PROPLIST_CELLHEIGHT 20
#Constant PROPLIST_CELLPANEL 24
#Constant PROPLIST_VALIDATEGADGET 28
#Constant PROPLIST_EDITGADGET 32
#Constant PROPLIST_CLICKGADGET 36
#Constant PROPLIST_SELECTGADGET 40
#Constant SIZEOF_PROPLIST 44
#Constant PROPLISTCELL_NEXTCELL 0
#Constant PROPLISTCELL_FUNCTION 4
#Constant PROPLISTCELL_CHANGED 8
#Constant PROPLISTCELL_GADGETCOUNT 12
#Constant PROPLISTCELL_FIRSTGADGET 16
Function CreatePropList(X, Y, W, H, Font, BoldFont, Parent)
Local MainGadgetData As DWORD
NewPropList = CreatePanel(X, Y, W, H, Parent)
CW = GadgetClientWidth(NewPropList)
NewPanel = CreatePanel(2, 2, CW-4, 0, NewPropList)
MainGadgetData = Make Memory(SIZEOF_PROPLIST)
Poke Integer MainGadgetData+PROPLIST_FIRSTCELL, 0
Poke Integer MainGadgetData+PROPLIST_LASTCELL, 0
Poke Integer MainGadgetData+PROPLIST_FONT, Font
Poke Integer MainGadgetData+PROPLIST_BOLDFONT, BoldFont
Poke Integer MainGadgetData+PROPLIST_LABELWIDTH, 96
Poke Integer MainGadgetData+PROPLIST_CELLHEIGHT, 25
Poke Integer MainGadgetData+PROPLIST_CELLPANEL, NewPanel
Poke Integer MainGadgetData+PROPLIST_VALIDATEGADGET, 0
Poke Integer MainGadgetData+PROPLIST_EDITGADGET, 0
Poke Integer MainGadgetData+PROPLIST_CLICKGADGET, 0
SetGadgetData NewPropList, MainGadgetData
EndFunction NewPropList
Function DeletePropList(PropList)
Local MainGadgetData As DWORD
MainGadgetData = GetGadgetData(PropList)
Cell = Peek Integer(MainGadgetData+PROPLIST_FIRSTCELL)
If Cell
For i = 0 To 1
GadgetCount = Peek Integer(Cell+PROPLISTCELL_GADGETCOUNT)
Ptr = Cell+PROPLISTCELL_FIRSTGADGET
For j = 1 To GadgetCount
DeleteGadget Peek Integer(Cell+PROPLISTCELL_FIRSTGADGET-4+j*4)
Next j
Cell = Peek Integer(Cell+PROPLISTCELL_NEXTCELL)
i = (Cell = 0)
Next i
EndIf
DeleteGadget Peek Integer(MainGadgetData+PROPLIST_CELLPANEL)
DeleteGadget PropList
Delete Memory MainGadgetData
EndFunction
Function PropList_AdjustHeight(PropList)
Local MainGadgetData As DWORD
MainGadgetData = GetGadgetData(PropList)
CellPanel = Peek Integer(MainGadgetData+PROPLIST_CELLPANEL)
ResizeGadget PropList, GadgetWidth(PropList), GadgetHeight(CellPanel) + 4
EndFunction
Function PropList_ValidateGadget(PropList, Gadget)
Local MainGadgetData As DWORD
MainGadgetData = GetGadgetData(PropList)
Poke Integer MainGadgetData+PROPLIST_VALIDATEGADGET, Gadget
EndFunction
Function PropList_EditGadget(PropList, Gadget)
Local MainGadgetData As DWORD
MainGadgetData = GetGadgetData(PropList)
Poke Integer MainGadgetData+PROPLIST_EDITGADGET, Gadget
EndFunction
Function PropList_ClickGadget(PropList, Gadget)
Local MainGadgetData As DWORD
MainGadgetData = GetGadgetData(PropList)
Poke Integer MainGadgetData+PROPLIST_CLICKGADGET, Gadget
EndFunction
Function PropList_SelectGadget(PropList, Gadget)
Local MainGadgetData As DWORD
MainGadgetData = GetGadgetData(PropList)
Poke Integer MainGadgetData+PROPLIST_SELECTGADGET, Gadget
EndFunction
Function PropList_GetEvents(PropList)
Local MainGadgetData As DWORD
Local FnPtr As DWORD
MainGadgetData = GetGadgetData(PropList)
Cell = Peek Integer(MainGadgetData+PROPLIST_FIRSTCELL)
If Cell
Changed = 0
For i = 0 To 1
FnPtr = Peek Integer(Cell+PROPLISTCELL_FUNCTION)
If FnPtr Then Changed = Call Function Ptr(FnPtr, MainGadgetData, Cell) || Changed
Cell = Peek Integer(Cell+PROPLISTCELL_NEXTCELL)
i = (Cell = 0)
Next i
EndIf
Poke Integer MainGadgetData+PROPLIST_VALIDATEGADGET, 0
Poke Integer MainGadgetData+PROPLIST_EDITGADGET, 0
Poke Integer MainGadgetData+PROPLIST_CLICKGADGET, 0
EndFunction Changed
Function PropListCell_IsChanged(PropListCell)
Changed = Peek Integer(PropListCell+PROPLISTCELL_CHANGED)
Poke Integer PropListCell+PROPLISTCELL_CHANGED, 0
EndFunction Changed
Function PropListCell_GetGadget(PropListCell, GadgetIndex)
Gadget = Peek Integer(PropListCell+PROPLISTCELL_FIRSTGADGET+GadgetIndex*4)
EndFunction Gadget
Function PropListCell_GetValue(PropListCell, Field)
Gadget = Peek Integer(PropListCell+PROPLISTCELL_FIRSTGADGET+Field*4)
CellValue = GetGadgetData(Gadget)
EndFunction CellValue
Function PropListCell_GetText(PropListCell, Field)
Gadget = Peek Integer(PropListCell+PROPLISTCELL_FIRSTGADGET+Field*4)
CellText$ = GetGadgetText(Gadget)
EndFunction CellText$
Function PropListCell_SetText(PropListCell, Field, CellText$)
Gadget = Peek Integer(PropListCell+PROPLISTCELL_FIRSTGADGET+Field*4)
SetGadgetText Gadget, CellText$
EndFunction
Function _PropList_NewCell(MainGadgetData As DWORD, GadgetCount)
Local NewCell As DWORD
NewCellSize = PROPLISTCELL_FIRSTGADGET+GadgetCount*4
NewCell = Make Memory(NewCellSize)
Fill Memory NewCell, 0, NewCellSize
Poke Integer NewCell+PROPLISTCELL_GADGETCOUNT, GadgetCount
LastCell = Peek Integer(MainGadgetData+PROPLIST_LASTCELL)
If LastCell
Poke Integer LastCell+PROPLISTCELL_NEXTCELL, NewCell
Else
Poke Integer MainGadgetData+PROPLIST_FIRSTCELL, NewCell
EndIf
Poke Integer MainGadgetData+PROPLIST_LASTCELL, NewCell
EndFunction NewCell
The Cells functions, a "Create" and "Process" function by cell type:
+ Code SnippetRem * Cells functions *
Function PropList_AddLabel(PropList, Caption$)
Local NewCell As DWORD
MainGadgetData = GetGadgetData(PropList)
CellPanel = Peek Integer(MainGadgetData+PROPLIST_CELLPANEL)
CW = GadgetWidth(CellPanel)
LW = Peek Integer(MainGadgetData+PROPLIST_LABELWIDTH)
ClH = Peek Integer(MainGadgetData+PROPLIST_CELLHEIGHT)
Y = GadgetHeight(CellPanel): If Y Then Inc Y, 2
ResizeGadget CellPanel, CW, Y+ClH, 1
NewLabel = CreateLabel(0, Y+3, CW, ClH-3, Caption$, CellPanel)
Font = Peek Integer(MainGadgetData+PROPLIST_BOLDFONT): If Font Then ApplyFont NewLabel, Font
NewCell = _PropList_NewCell(MainGadgetData, 1)
Poke Integer NewCell+PROPLISTCELL_FUNCTION, Get Ptr To Function("_PropList_LabelFunction")
Poke Integer NewCell+PROPLISTCELL_FIRSTGADGET, NewLabel
EndFunction NewCell
Function _PropList_LabelFunction(MainGadgetData As DWORD, Cell As DWORD)
Label = Peek Integer(Cell+PROPLISTCELL_FIRSTGADGET)
If ActiveGadget() = Label Then Poke Integer MainGadgetData+PROPLIST_VALIDATEGADGET, -1
EndFunction 0
Function PropList_AddEdit(PropList, Caption$, EditText$)
Local MainGadgetData As DWORD
Local NewCell As DWORD
MainGadgetData = GetGadgetData(PropList)
CellPanel = Peek Integer(MainGadgetData+PROPLIST_CELLPANEL)
CW = GadgetWidth(CellPanel)
LW = Peek Integer(MainGadgetData+PROPLIST_LABELWIDTH)
ClH = Peek Integer(MainGadgetData+PROPLIST_CELLHEIGHT)
Y = GadgetHeight(CellPanel)
If Y Then Inc Y, 2
ResizeGadget CellPanel, GadgetWidth(CellPanel), Y+ClH
NewLabel = CreateLabel(0, Y+3, LW, ClH-3, Caption$, CellPanel)
Font = Peek Integer(MainGadgetData+PROPLIST_FONT): If Font Then ApplyFont NewLabel, Font
NewEdit = CreateEdit(LW, Y, CW-LW, ClH, 0, CellPanel)
SetGadgetText NewEdit, EditText$
Font = Peek Integer(MainGadgetData+PROPLIST_FONT): If Font Then ApplyFont NewEdit, Font
NewCell = _PropList_NewCell(MainGadgetData, 2)
Poke Integer NewCell+PROPLISTCELL_FUNCTION, Get Ptr To Function("_PropList_EditFunction")
Poke Integer NewCell+PROPLISTCELL_FIRSTGADGET, NewEdit
Poke Integer NewCell+PROPLISTCELL_FIRSTGADGET+4, NewLabel
EndFunction NewCell
Function _PropList_EditFunction(MainGadgetData As DWORD, Cell As DWORD)
Edit = Peek Integer(Cell+PROPLISTCELL_FIRSTGADGET)
Label = Peek Integer(Cell+PROPLISTCELL_FIRSTGADGET+4)
ValidatedGadget = Peek Integer(MainGadgetData+PROPLIST_VALIDATEGADGET)
If ActiveGadget() = Label Then ValidatedGadget = -1
NextValidatedGadget = ValidatedGadget
If ValidatedGadget = -1
ActivateGadget Edit
SetSelStart Edit, 0
SetSelLen Edit, Fast Len(GetGadgetText(Edit))
NextValidatedGadget = 0
EndIf
If ValidatedGadget = Edit
Changed = true
Poke Integer Cell+PROPLISTCELL_CHANGED, true
NextValidatedGadget = -1
EndIf
Poke Integer MainGadgetData+PROPLIST_VALIDATEGADGET, NextValidatedGadget
EndFunction Changed
As usual, dont forget the gui.dba included in the first step post!