Page 1 of 1

Modal Messages and dialogs

Posted: Sun Mar 27, 2016 6:18 am
by collectordave
Working towards converting some programmes to PB I found I had to show user messages and some dialogs to the user either to quit or to add more detail. At times these need to be Modal i.e. halt main programme execution until I get a response and at others, especially where window timers are involved the main programme execution must not stop.

I came up with the code below. It allows me to have modal or non modal dialogs and messages throughout my programme.

It is in two parts the first is the main form which should be compiled. This calls the dialog include file. So copy both to the same folder and open in PB then compile the main form to see what I mean.

frmMain.pb

Code: Select all

DeclareModule MainForm
  
  Global frmMain.i
  
  Declare Open()
  Declare EventHandler(Event)
  
EndDeclareModule

IncludeFile "dlgtest.pbi"

Module MainForm
  
  Global Button_0.i,String_0.i
  
Procedure Open()

frmMain = OpenWindow(#PB_Any, 0, 0, 360, 150, "", #PB_Window_SystemMenu)
Button_0 = ButtonGadget(#PB_Any, 20, 90, 90, 30, "Dialog")
String_0 = StringGadget(#PB_Any, 20, 10, 170, 20, "")
AddWindowTimer(frmMain, 0, 1000)

EndProcedure

Procedure EventHandler(Event)
  
  Static Time.i
  
  Select event
        
      Case #PB_Event_Timer 
        time = time + 1
        SetGadgetText(String_0,Str(Time))
        
      Case #PB_Event_CloseWindow
        
       End

     Case #PB_Event_Gadget
       
       Select EventGadget()
           
         Case Button_0
           
          ;The message requester is fully modal
          ;This can be seen as the main form counter stops
          MessageRequester("Test","test Message")
          ;This dialog is as modal as you wish
          Dialog::open(frmMain)
          
      EndSelect
      
  EndSelect
  
EndProcedure

EndModule
dlgTest.pbi

Code: Select all

DeclareModule Dialog
  
  Declare Open(Parent.i)
  
  
EndDeclareModule

Module Dialog

Procedure Open(ctlWnd.i)
  
  ;ctlWnd is a handle to the window that controls this dialog
  
  dlgWindow = OpenWindow(#PB_Any, 0, 0, 360, 150, "My Dialog - Not Modal",  #PB_Window_TitleBar | #PB_Window_Tool)
  Button_0 = ButtonGadget(#PB_Any, 120, 60, 90, 30, "Close")
  StickyWindow(dlgWindow,#True)
  
  Repeat
    ;When opened all application events are routed here
    Event = WaitWindowEvent()
    
    ;To have this dialog fully modal comment out the next three program lines
    ;When commented out the main form counter stops
    If EventWindow() = ctlWnd
      MainForm::EventHandler(Event)
    EndIf

    Select event

      Case #PB_Event_Gadget
        Select EventGadget()
          Case Button_0
            CloseWindow(dlgWindow)
            Break
        EndSelect
    EndSelect 
    
  ForEver
  
EndProcedure

EndModule

A different approach to laying out a programme so can anyone see any pitfalls?

I have tested this on windows and MAC. Works fine.