PDA

View Full Version : Callback Buttons and popups



Lionheart008
13-01-2009, 14:38
hi all:)
welcome to the new year with more powerful thinbasic releases :D

a) have made a little exercise some minutes ago with callback buttons and console... the code isn't just perfect, I know, but how can I make "different popup window" (lionhearts test-window-popup1, test-window-popup2 and so on...) while I am clicking the button 1 - 5... ???

b) clicking the lionheart-test button shows no popup window... why???

perhaps somebody can help. would be nice...

bye, have all a nice day, I am back again... have had a short break cause of nightly work:)

ciao und servus, Lionheart (Frank)

Petr Schreiber
13-01-2009, 15:20
Hi,

ThinBasic callback dialogs can filter kind of event (CBMSG aka callback message), to which control it is related (CBCTL aka callback control), which control specific message arrived (CBCTLMSG aka callback control message). This is explained in Journal #2 (http://community.thinbasic.com/index.php?topic=2310.msg17554#msg17554), if you need.

Why Lionheart_Box button does not work?
You, in your original code, test value of CBCTLMSG, and compare it both to %BN_CLICKED ( correct!, it is message to control ) and to control id ( %ID_Button_06, control itself ), which is not good.

I will not give you corrected source as I am in my evil-educator mode :D, but will give you some advices.

When you need to have multiple dialogs in program, create one function where you create it ( DIALOG NEW ), call it always DialogDefinition_<nameHere> for better clarity.

For each of those dialogs, create callback, call it DialogCallback_<nameHere>, again for better clarity.

So to create 5 different dialogs, write 5 functions with creation code, custom title instead of writing DIALOG NEW ... somewhere in 3rd level of SELECT CASE :)

With this code organization, it will be much easier to keep your code easy to read.

Here is template for you:


' -- My controls for <nameHere> dialog
BEGIN CONST
%btnClose = %WM_USER + 500 ' -- Safe start offset
END CONST

' -- My dialog definition
FUNCTION DialogDefinition_<nameHere>( parentDialog AS DWORD )

LOCAL hDlg AS DWORD

DIALOG New parentDialog, "<nameHere>",-1,-1, 160, 120, _
%WS_<nameHere> Or %WS_VISIBLE Or _
%WS_CLIPCHILDREN Or %WS_CAPTION OR _
%WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg

' -- Place controls here
CONTROL ADD BUTTON, hDlg, %btnClose, "Click to close", 95, 100, 60, 14

DIALOG SHOW MODAL hDlg, CALL DialogCallback_<nameHere>

END FUNCTION

' -- My dialog callback
CALLBACK FUNCTION DialogCallback_<nameHere>()

' -- Test for messages
SELECT CASE CBMSG

CASE %WM_INITDIALOG
' -- Put code to be executed after dialog creation here

CASE %WM_COMMAND
' -- You can handle controls here

SELECT CASE CBCTL

CASE %btnClose
IF CBCTLMSG = %BN_CLICKED THEN
DIALOG END CBHNDL
END IF

END SELECT


CASE %WM_CLOSE
' -- Put code to be executed before dialog end here

END SELECT

END FUNCTION


To "spawn" a dialog, you will call just DialogDefinition_<nameHere>( hereHandleToParentDialogOrZero )

ErosOlmi
13-01-2009, 15:33
:twisted: :twisted:
Frank,

the problem is that script is a little confused.

If a button control has its own callback message, when a button is fired, more than one message is sent both for command and notification. If you do not check the kind of message, risk is to handle twice or more times the same message so you are doing more than once the same thing. See BUTTON callback example in help at http://www.thinbasic.com/public/products/thinBasic/help/html/control_add_button.htm
You have to check both CBMSG and CBCTLMSG

'...
If CBMSG = %WM_COMMAND Then
'---Check control specific message
Select Case CBCTLMSG
Case %BN_CLICKED
'...

If you want to keep it simple, just handle %WM_COMMAND in dialog callback and you will be sure to handle only when the button is clicked.

IMPORTANT: remove %BS_NOTIFY in BUTTON style otherwise button callback (or dialog callback in case button has no callback) will receive notification messages.


_________________________________________________
A callback is a callback in all cases but do not use control callbacks for dialogs callbacks, NEVER.
Even if in principle they can be used, in practice you will get a lot of unwanted messages.

Here s imple revision of your script:


'------------------------------------------------------------------------------
' Program to show dialogs and buttons callbacks
'------------------------------------------------------------------------------
'- exploring there might be more by lionheart ;), 3./12.jan. 2009

USES "UI", "TBGL"
USES "console" '---Console is used to easily output events info

DIM Counter AS LONG '---Counter to count output lines

'---Constant declarations

BEGIN CONST
%ID_Button_01 = %IDOK
%ID_Button_02
%ID_Button_03
%ID_Button_04
%ID_Button_05
%ID_Button_06
%dat3
%tim3
END CONST


'------------------------------------------------------------------------------
' Program start point
'------------------------------------------------------------------------------

FUNCTION TBMain( ) AS LONG
LOCAL hDlg AS DWORD '---Used to store window handle of main dialog
'---Create a new dialog
DIALOG NEW PIXELS, 0, "Frankos TestButton Callbacks_1", - 1, - 1, 300, 200, _
%WS_DLGFRAME | _
%DS_CENTER | _
%WS_CAPTION | _
%WS_SYSMENU | _
%WS_OVERLAPPEDWINDOW, 0 TO hDlg
'---Set window minimum size
DIALOG SET MINSIZE hDlg, 300, 200
'---Show dialog in modal mode
'---cbDialog function is the callback function handling dialog events
'---Application control will pass to dialog callback till dialog will exists
DIALOG SHOW MODAL hDlg, CALL cbDialog
'---If execution comes here it means main dialog as been destroyed
PRINTL "---Application finished. Press a key to end---"
WAITKEY
END FUNCTION

'------------------------------------------------------------------------------
' Callback procedure for main window
'------------------------------------------------------------------------------

CALLBACK FUNCTION cbDialog( ) AS LONG
'printl CBHNDL, CBCTL, CBCTLMSG, CBLPARAM, CBWPARAM
SELECT CASE CBMSG
CASE %WM_CREATE
PRINTL TIME$, "Fired %WM_CREATE dialog message"
CASE %WM_INITDIALOG
PRINTL TIME$, "Fired %WM_INITDIALOG dialog message"
'---Add controls
CONTROL ADD BUTTON, CBHNDL, %ID_Button_01, "I'm the button 1", 100, 30, 100, 35, %WS_TABSTOP
CONTROL ADD BUTTON, CBHNDL, %ID_Button_02, "I'm the button 2", 100, 70, 100, 35, %WS_TABSTOP
CONTROL ADD BUTTON, CBHNDL, %ID_Button_03, "I'm the button 3", 100, 110, 100, 35, %WS_TABSTOP
CONTROL ADD BUTTON, CBHNDL, %ID_Button_04, "I'm the button 4", 100, 150, 100, 35, %WS_TABSTOP
CONTROL ADD BUTTON, CBHNDL, %ID_Button_05, "Thinbasic_Box", 4, 120, 90, 35, %WS_TABSTOP
CONTROL ADD BUTTON, CBHNDL, %ID_Button_06, "Lionhearts_Box", 205, 120, 90, 35, %WS_TABSTOP
CONTROL ADD "sysdatetimepick32", CBHNDL, %dat3, "31.12.2008", 4, 70, 80, 24, %ws_child OR %ws_visible
CONTROL ADD "sysdatetimepick32", CBHNDL, %tim3, "15:38:14", 210, 70, 80, 24, %ws_child OR %ws_visible OR %dts_timeformat
CASE %WM_COMMAND

PRINTL TIME$, "Fired %WM_COMMAND dialog message", CBCTL
select case CBCTL
CASE %ID_Button_01
PRINTL TIME$, "Button 1 clicked"
CASE %ID_Button_02
PRINTL TIME$, "Button 2 clicked"
CASE %ID_Button_03
PRINTL TIME$, "Button 3 clicked"
CASE %ID_Button_04
PRINTL TIME$, "Button 4 clicked"
CASE %ID_Button_05
PRINTL TIME$, "Button 5 clicked"
CASE %ID_Button_06
PRINTL TIME$, "Button 6 clicked"
end select
CASE %WM_SIZE '---The WM_SIZE message is sent to a window after its size has changed.
PRINTL TIME$, "Fired %WM_SIZE dialog message", CBWPARAM, LOWRD( CBLPARAM ), HIWRD( CBLPARAM )
CASE %WM_SIZING '---The WM_SIZING message is sent to a window that the user is resizing.
PRINTL TIME$, "Fired %WM_SIZING dialog message", CBWPARAM, LOWRD( CBLPARAM ), HIWRD( CBLPARAM )
'case %WM_MOVING
' local RC as RECT ptr
' RC = cblParam
' printl time$, "Fired %WM_MOVING dialog message", RC.nLeft, RC.nTop, RC.nRight, RC.nBottom
CASE %WM_MOVE
PRINTL TIME$, "Fired %WM_MOVE dialog message", LO( INTEGER, CBLPARAM ), HI( INTEGER, CBLPARAM )
CASE %WM_DESTROY
'---Do whatever needed just before dialog is destroyed.
PRINTL TIME$, "Fired %WM_DESTROY dialog message"
END SELECT
END FUNCTION


Now you can create different dialogs in response to button clicking but please add to each dialog a callback handling dialogs events and not share callbacks for dialogs and controls.

Ciao
Eros

Lionheart008
13-01-2009, 17:46
hi, ok, thanks ! :twisted: and :D for the useful informations, I needed this one and was eager to manage this little callback exercise...

I add the new version and have understood the problem! Learning by mistakes, I like it! :)

My mistake was to forget the callback function for the separately call back button so I have to specific it what this buttons should do..., many thanks petr and eros... was blind :shock:

my little helper: with a cup of coffee and a biscuit I can work better and see the clear water ;)

important for me was...

CALLBACK FUNCTION cbPopupWindow( ) AS LONG
IF CBMSG = %WM_COMMAND THEN
IF CBCTLMSG = %BN_CLICKED THEN

and


CONTROL ADD BUTTON, CBHNDL, %ID_Button_01, "I'm the button 1", 100, 30, 100, 35, %WS_TABSTOP | %BS_DEFAULT, CALL cbPopupWindow

... :)

best regards, callback Lionheart

ps: only button 1+3 are the same, I wanted it in this way
button 2, 4, thinbasicBox, lionheartsBox shows other popupwindows :)

ErosOlmi
13-01-2009, 18:07
Frank

I forgot to say that usually there is only one button that can have %BS_DEFAULT style because on a window only one button at a time can be the default one.

Ciao
Eros