ErosOlmi
23-08-2013, 23:23
Something you will get into next thinBasic beta (1.9.8.0)
DIALOGS can be samed using Name clause during DIALOG NEW ...
If named option is used, you can use dialog name to create events dialog callbacks function that will be automatically called when such an event is fired
automatic dialog callbacks must be named with the dialog name + underscore + event
so far I've created the following events: *_OnInit, *_OnSize, *_OnMove, *_OnTimer, *_OnCommand, *_OnCallback (can be used to create automatic callback to dialogs)
During this development I discovered it would be very useful to have variables defined with a DIALOG scope, that is variable accessible from any dialog callback of the same dialog. This in order to avoid global variables and keep things all together. I'm thinking about it and how to implement.
Suggestions/ideas are welcome.
Ciao
Eros
Example:
uses "UI"
Uses "console"
Begin ControlID
%IDC_TIMER
%IDC_LABEL
%IDC_Start
%IDC_Stop
End ControlID
%TIMER_DELAY = 10 ' Timer delay (in milliseconds, not very accurate below about 100)
Global TimerValue As Double '---Will be used to calculate timer values
'------------------------------------------------------------------------------
' Main function
'------------------------------------------------------------------------------
FUNCTION TBMain() as long
LOCAL hDlg AS DWORD
Dialog New Name "TimerDialog", 0 , "Timer Example using CallBacks functions", -1, -1, 240, 40, %WS_OVERLAPPEDWINDOW To hDlg
CONTROL ADD BUTTON, hDlg, %IDC_Start, "Start" , 10, 10, 50, 20
CONTROL ADD LABEL , hDlg, %IDC_LABEL, "" , 90, 10, 55, 20, %WS_CHILD OR %WS_VISIBLE OR %WS_BORDER OR %SS_CENTERIMAGE OR %SS_CENTER
CONTROL ADD BUTTON, hDlg, %IDC_Stop , "Stop" , 180, 10, 50, 20
Dialog Show Modal hDlg 'Call MainDialog_OnCallBack
END FUNCTION
'------------------------------------------------------------------------------
' OnCallBack
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnCallBack() As Long
PrintL Timer, Function_Name, CBMSG
End Function
'------------------------------------------------------------------------------
' OnCommand
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnCommand() As Long
Select Case CBCTL
'---Check which control as fired the event
Case %IDC_Start
'---If start, than enable/disable relevant buttons and activate the timer
Control Disable CBHNDL, %IDC_Start
Control Enable CBHNDL, %IDC_Stop
TimerValue = 0 '---Set the time counter to zero
Dialog Set Timer CBHNDL, %IDC_TIMER, %TIMER_DELAY
Case %IDC_Stop
'---If sstop, than enable/disable relevant buttons and destroy the timer
Control Disable CBHNDL, %IDC_Stop
Control Enable CBHNDL, %IDC_Start
Dialog Kill Timer CBHNDL, %IDC_TIMER
End Select
End Function
'------------------------------------------------------------------------------
' OnInit
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnInit() As Long
Control Disable CBHNDL, %IDC_Stop
End Function
'------------------------------------------------------------------------------
' OnTimer
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnTimer() As Long
'---This event is fired by any timer created on the dialog. We have to check which one
'---CBWPARAM contains the ID of the timer that fired %WM_TIMER event
'---More than one timer can be active sumultaneously
Select Case CBWPARAM
Case %IDC_TIMER
'---Increment and set the test value...
TimerValue += 0.01
Control Set Text CBHNDL, %IDC_LABEL, Format$(TimerValue, "#0.00")
End Select
End Function
'------------------------------------------------------------------------------
' OnSize
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnSize() As Long
PrintL Timer, "Sizing", CBWPARAM, CBLPARAM
End Function
'------------------------------------------------------------------------------
' OnMove
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnMove() As Long
PrintL Timer, "Moving", CBWPARAM, CBLPARAM
End Function
PS: this post fired OOP discussion (that I really appreaciated) in this post: http://www.thinbasic.com/community/showthread.php?t=12194
I just wanted to point out that here I'm not discussing OOP but just another approach to Dialog Events CallBacks handling, at least for the more often used events.
DIALOGS can be samed using Name clause during DIALOG NEW ...
If named option is used, you can use dialog name to create events dialog callbacks function that will be automatically called when such an event is fired
automatic dialog callbacks must be named with the dialog name + underscore + event
so far I've created the following events: *_OnInit, *_OnSize, *_OnMove, *_OnTimer, *_OnCommand, *_OnCallback (can be used to create automatic callback to dialogs)
During this development I discovered it would be very useful to have variables defined with a DIALOG scope, that is variable accessible from any dialog callback of the same dialog. This in order to avoid global variables and keep things all together. I'm thinking about it and how to implement.
Suggestions/ideas are welcome.
Ciao
Eros
Example:
uses "UI"
Uses "console"
Begin ControlID
%IDC_TIMER
%IDC_LABEL
%IDC_Start
%IDC_Stop
End ControlID
%TIMER_DELAY = 10 ' Timer delay (in milliseconds, not very accurate below about 100)
Global TimerValue As Double '---Will be used to calculate timer values
'------------------------------------------------------------------------------
' Main function
'------------------------------------------------------------------------------
FUNCTION TBMain() as long
LOCAL hDlg AS DWORD
Dialog New Name "TimerDialog", 0 , "Timer Example using CallBacks functions", -1, -1, 240, 40, %WS_OVERLAPPEDWINDOW To hDlg
CONTROL ADD BUTTON, hDlg, %IDC_Start, "Start" , 10, 10, 50, 20
CONTROL ADD LABEL , hDlg, %IDC_LABEL, "" , 90, 10, 55, 20, %WS_CHILD OR %WS_VISIBLE OR %WS_BORDER OR %SS_CENTERIMAGE OR %SS_CENTER
CONTROL ADD BUTTON, hDlg, %IDC_Stop , "Stop" , 180, 10, 50, 20
Dialog Show Modal hDlg 'Call MainDialog_OnCallBack
END FUNCTION
'------------------------------------------------------------------------------
' OnCallBack
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnCallBack() As Long
PrintL Timer, Function_Name, CBMSG
End Function
'------------------------------------------------------------------------------
' OnCommand
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnCommand() As Long
Select Case CBCTL
'---Check which control as fired the event
Case %IDC_Start
'---If start, than enable/disable relevant buttons and activate the timer
Control Disable CBHNDL, %IDC_Start
Control Enable CBHNDL, %IDC_Stop
TimerValue = 0 '---Set the time counter to zero
Dialog Set Timer CBHNDL, %IDC_TIMER, %TIMER_DELAY
Case %IDC_Stop
'---If sstop, than enable/disable relevant buttons and destroy the timer
Control Disable CBHNDL, %IDC_Stop
Control Enable CBHNDL, %IDC_Start
Dialog Kill Timer CBHNDL, %IDC_TIMER
End Select
End Function
'------------------------------------------------------------------------------
' OnInit
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnInit() As Long
Control Disable CBHNDL, %IDC_Stop
End Function
'------------------------------------------------------------------------------
' OnTimer
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnTimer() As Long
'---This event is fired by any timer created on the dialog. We have to check which one
'---CBWPARAM contains the ID of the timer that fired %WM_TIMER event
'---More than one timer can be active sumultaneously
Select Case CBWPARAM
Case %IDC_TIMER
'---Increment and set the test value...
TimerValue += 0.01
Control Set Text CBHNDL, %IDC_LABEL, Format$(TimerValue, "#0.00")
End Select
End Function
'------------------------------------------------------------------------------
' OnSize
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnSize() As Long
PrintL Timer, "Sizing", CBWPARAM, CBLPARAM
End Function
'------------------------------------------------------------------------------
' OnMove
'------------------------------------------------------------------------------
CallBack Function TimerDialog_OnMove() As Long
PrintL Timer, "Moving", CBWPARAM, CBLPARAM
End Function
PS: this post fired OOP discussion (that I really appreaciated) in this post: http://www.thinbasic.com/community/showthread.php?t=12194
I just wanted to point out that here I'm not discussing OOP but just another approach to Dialog Events CallBacks handling, at least for the more often used events.