Page 1 of 1

Proper modal dialog boxes in PB

Posted: Sun May 04, 2003 8:42 pm
by spangly
Code updated For 5.20+

Well I hope someone finds this usefull, I figured out you don't need resources to use proper modal dialog boxes.

If something similar has been posted before I apologise in advance.

Code: Select all

; Proper Dialogs in Purebasic
; Hacked together by Spangly

Structure DLG_TEMPLATE
  style.l
  dwExtendedStyle.l
  cdit.w
  x.w
  y.w
  cx.w
  cy.w
  menu.w
  class.w
  title.l
EndStructure

dlg.DLG_TEMPLATE
dlg\style=#WS_POPUP | #WS_BORDER | #WS_SYSMENU | #DS_MODALFRAME | #WS_CAPTION | #DS_CENTER
dlg\cx=200
dlg\cy=100

Procedure DlgProc(hWnd, uMsg, wParam, lParam)
  Select uMsg
    Case #WM_INITDIALOG
      
      ButtonGadget(0,20,20,100,22,"OK")
      ButtonGadget(1,20,50,100,22,"Cancel")
      ButtonGadget(2,20,80,100,22,"Quit")
      SetWindowText_(hWnd,"Dialog Title")
    Case #WM_COMMAND
      EndDialog_(hWnd,wParam&$FFFF)
  EndSelect
  ProcedureReturn 0
EndProcedure

OpenWindow(0, 325, 185, 600, 330, "Proper Dialogs", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar )
Debug DialogBoxIndirectParam_(0,dlg,WindowID(0),@DlgProc(),0)

Repeat
  event.l=WaitWindowEvent()
  Select event
    Case #PB_Event_Gadget
      Debug EventGadget()
  EndSelect
Until event=#PB_Event_CloseWindow

End

; ExecutableFormat=Windows
; EnableAsm
; EnableXP
; EOF


Posted: Tue Oct 05, 2004 1:07 pm
by eddy
Hi,

Why the colorisation does not work when the text is in the panel ? :? :?

Cross-platform modal dialog boxes needed

Posted: Thu Jun 02, 2005 11:10 pm
by USCode
IMHO PB *desperately* needs CROSS-PLATFORM modal dialog box support. Writing applications with modal dialog boxes makes life SO much easier for programmers and users alike. To me, that is a critical feature that will turn off many potential PB adopters if it isnt available. VB, RB and Java ALL allow modal windows/dialog-boxes to be created.

Re: Proper modal dialog boxes in PB

Posted: Thu Jun 02, 2005 11:39 pm
by NoahPhense
Very nice.. I hope something like this will become native.

- np

Modals ASAP please

Posted: Fri Jun 03, 2005 6:28 pm
by USCode
Yeah, cross-platform modals are really really needed, especially for those that develop business database-type apps. Currently, for me at least, this is the only major hole in PB. Everything else I need pretty-much has workarounds (except for the lack of a native grid control as well). I've been writing GUI database-type business applications for 14yrs now (originally on OS/2!) and can't think of one that didnt need at least 1 modal dialog. But, like everything, Fred's time is limited. This should, however, be pretty high on the priority list IMHO.

Posted: Fri Jun 03, 2005 9:25 pm
by dell_jockey
I agree with that assertion...

Posted: Sat Jun 11, 2005 8:59 pm
by Nico
Under Window, It is possible to simulate a window PB as Dialog!

Posted: Sat Jun 11, 2005 10:21 pm
by GeoTrail
Kewl, was looking for something like this.
Perfect for an about dialog ;)

Posted: Sat Jun 11, 2005 11:36 pm
by Dare2
Thanks spangly. :)

Custom Requester

Posted: Sun Jul 17, 2005 11:06 pm
by 5pu+n!k
Hi,

based on spangly's code I made a requester style 3 button modal dialog.
The procedure takes the button text as argument. You can also specify which button is to be the default button.

regards,
Sputnik

Code: Select all

; 
; 3-Button Requester-Style Modal Dialog with arbitrary Button-Text and Default-Button
; by Sputnik 
; based on code by spangly 
;  
Declare Requester_3Btn(hWndParent.l,title$,msgtxt$,btn1$,btn2$,btn3$,btn_default.l)
Declare Requester_3Btn_Callback(hWnd, uMsg, wParam, lParam)

style.l = #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget
style   = style | #PB_Window_MaximizeGadget|#PB_Window_SizeGadget | #PB_Window_TitleBar
If OpenWindow(0, 0, 0, 660,450, style,"Modal Test") = 0 :End : EndIf

result = Requester_3Btn(WindowID(),"Window Title","Message Text","Button 1","Button 2","Button 3",2)
MessageRequester("Information","You have chosen button "+Str(result),#PB_MessageRequester_Ok)
result = Requester_3Btn(WindowID(),"File already exists","How do you want to proceed ?","Replace","Append","Cancel",3)
MessageRequester("Information","You have chosen button "+Str(result),#PB_MessageRequester_Ok)

Repeat
  EventID = WaitWindowEvent()
Until EventID = #PB_Event_CloseWindow
End

;-
Procedure.l Requester_3Btn(hWndParent.l,title$,msgtxt$,btn1$,btn2$,btn3$,btn_default.l)
; Parameters : hWndParent.l        Handle of parent window 
;              title$,msgtxt$      Titlebar Text, Message Text
;              btn1$,btn2$,btn3$   Text for Buttons
;              btn_default.l       Default Button  1,2, or 3    
; returns    : 1,2 or 3 for selected Button
;              0 for cancel
   
  Structure DLG_INFO
    titletxt.s 
    msgtxt.s
    btn1txt.s 
    btn2txt.s 
    btn3txt.s
    hnd_btn1.l
    hnd_btn2.l
    hnd_btn3.l
    dflt_btn.l
  EndStructure
  
  di.DLG_INFO
  di\titletxt = title$ 
  di\msgtxt   = msgtxt$
  di\btn1txt  = btn1$ 
  di\btn2txt  = btn2$
  di\btn3txt  = btn3$
  di\dflt_btn = btn_default
  
  Structure DLG_TEMPLATE 
    style.l 
    dwExtendedStyle.l 
    cdit.w 
    x.w 
    y.w 
    cx.w          ; size of client-area in DialogUnits 
    cy.w          ; 
    menu.w 
    class.w 
    title.l 
  EndStructure 
  
  dt.DLG_TEMPLATE 
  dt\style = #WS_POPUP | #WS_BORDER | #WS_SYSMENU | #DS_MODALFRAME | #WS_CAPTION | #DS_CENTER 

  ; desired dialog-box size is 323 x 100 pixel (will produce 322 x 99)
  dlgX = 323
  dlgY = 100  
  
  ; converting pixels to dialogunits  
  ; dialogunitX = (pixelX * 4) / baseunitX  
  ; dialogunitY = (pixelY * 8) / baseunitY  
  baseunit    = GetDialogBaseUnits_()
  baseunitX   = PeekW(@baseunit)
  baseunitY   = PeekW(@baseunit+2)

  titleheight = GetSystemMetrics_(#SM_CYCAPTION)
  frameX      = GetSystemMetrics_(#SM_CXFIXEDFRAME)
  frameY      = GetSystemMetrics_(#SM_CYFIXEDFRAME)
  
  dt\cx = ((dlgX - (2 * frameX)) * 4) / baseunitX   
  dt\cy = ((dlgY - titleheight - (2 * frameY)) * 8) / baseunitY
  
  ; calling the dialog-box
  Hnd_selected_btn = DialogBoxIndirectParam_(0,@dt,hWndParent,@Requester_3Btn_Callback(),@di)

  ; translating button-handle to a number
  selected = 0
  Select Hnd_selected_btn
    Case di\hnd_btn1
      selected = 1
    Case di\hnd_btn2
      selected = 2
    Case di\hnd_btn3
      selected = 3
  EndSelect

  ProcedureReturn selected
EndProcedure

;- 
Procedure Requester_3Btn_Callback(hWnd, uMsg, wParam, lParam) ;DlgProc 

  Select uMsg
    
    Case #WM_INITDIALOG
      ; is sent when the dialog box is created but before it is displayed 
      ; wParam : handle of dialog box 
      ; lParam : address of additional initialization information 
      ;          (=pointer To my DLG_INFO Struct)
      
      *p.DLG_INFO = lParam
      
      CreateGadgetList(hWnd)
      TextGadget(#PB_Any, 11, 11, 296, 15,*p\msgtxt)
      ; uses handles to identify buttons
      *p\hnd_btn1 = GadgetID(ButtonGadget(#PB_Any, 11, 41, 95, 23,*p\btn1txt))
      *p\hnd_btn2 = GadgetID(ButtonGadget(#PB_Any, 111, 41, 95, 23,*p\btn2txt))
      *p\hnd_btn3 = GadgetID(ButtonGadget(#PB_Any, 212, 41, 95, 23,*p\btn3txt))
      SetWindowText_(hWnd,*p\titletxt)
      
      ; setting default button and keyboard-focus 
      rc = 0
      Select *p\dflt_btn
        Case 2
          SendMessage_(*p\hnd_btn2,#BM_SETSTYLE,#BS_DEFPUSHBUTTON,#True)
          SetFocus_(*p\hnd_btn2)
        Case 3
          SendMessage_(*p\hnd_btn3,#BM_SETSTYLE,#BS_DEFPUSHBUTTON,#True)
          SetFocus_(*p\hnd_btn3)
        Default
          rc = 1
      EndSelect
      
      ProcedureReturn rc                    ; rc = 1 : default; keyboard-focus on first button
                                            ; rc = 0 : you have set keyboard-focus and default-button yourself
    Case #WM_COMMAND
      ; is sent when a button is selected
      ; lParam : handle of control  
    
      EndDialog_(hWnd,lParam)               ; returns handle of selected button and ends dialog
                                            ; this is the returned value of the DialogBoxIndirectParam call
    Case #WM_SYSCOMMAND
      ; is sent when a window-control or menu item is selected
      ; wParam : type of system command requested
      ;        : the four low-order bits are used internally by Windows
      ;        : they have to be masked with $FFF0

      If wParam&$FFF0 = #SC_CLOSE           ; returns zero if close-button or ESC was pressed
        EndDialog_(hWnd,0)
      EndIf

  EndSelect 
  ProcedureReturn 0                         ; returns zero for messages that are not handled here
                                            ; WM_COMMAND,WM_SYSCOMMAND should also return zero
EndProcedure