User Module: XPlatform Messages and dialogs Modalish

Share your advanced PureBasic knowledge/code with the community.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

User Module: XPlatform Messages and dialogs Modalish

Post by collectordave »

Searched the forum for a way to have modal messages and dialogs cross-platform. Picked up lots of tips etc and cobbled together the following. Tested on windows and MAC. Not Linux, I do not have a Linux machine as yet.

This is a module to hold all application global constants, variables and procedures with any cross platform code embedded here. At this point I have only programmed a message box with icons and OK\Cancel Yes\No etc with return values. All with no API calls.

More procedures can be added to the module which I will do from time to time.

First the Module: App.pbi

Code: Select all

UsePNGImageDecoder() 

DeclareModule App
  
  ;All OS's
  
  ;Constants for Flags
  #OkOnly = 1
  #OkCancel = 2
  #YesNo = 4
  #YesNoCancel = 8
  #InformationIcon = 16
  #WarningIcon = 32
  #StopIcon = 64
  
  ;Constants for return values
  #MsgOk = 1
  #MsgYes = 2
  #MsgNo = 3
  #MsgCancel = 4
  
  DataSection
  Info: 
  IncludeBinary "information.png"  ;Add your own image
  Warn:
  IncludeBinary "warning.png"      ;Add your own image
  Stop:
  IncludeBinary "stop.png"         ;Add your own image
  EndDataSection

  Declare.i Message(Title.s,Msg.s,Flags.i)
  
  ;OS Specific details
  CompilerSelect #PB_Compiler_OS
    
    CompilerCase   #PB_OS_MacOS
        
      #DefaultFolder = "/Volumes" ;For explorertreegadget etc
      
    CompilerCase   #PB_OS_Linux 
    
    CompilerCase   #PB_OS_Windows

      #DefaultFolder = "C:\"      ;For explorertreegadget etc
    
  CompilerEndSelect
  
EndDeclareModule

Module App
      
  Procedure.i Message(Title.s,Msg.s,Flags.i)
  
    Define This_Window.i,btn1,btn2,btn3,txtMsg,RetVal,IconImage,Testimg
  
    This_Window = OpenWindow(#PB_Any, 0, 0, 300, 130, Title,  #PB_Window_Tool | #PB_Window_ScreenCentered)
    
    ;Add the buttons
    If Flags & #YesNoCancel > 0
      btn1 = ButtonGadget(#PB_Any, 30, 90, 80, 30, "Yes")
      btn2 = ButtonGadget(#PB_Any, 120, 90, 80, 30, "No")
      btn3 = ButtonGadget(#PB_Any, 210, 90, 80, 30, "Cancel")
    ElseIf   Flags & #OkCancel > 0
      btn2 = ButtonGadget(#PB_Any, 120, 90, 80, 30, "Ok")
      btn3 = ButtonGadget(#PB_Any, 210, 90, 80, 30, "Cancel")
    ElseIf   Flags & #YesNo > 0
      btn2 = ButtonGadget(#PB_Any, 120, 90, 80, 30, "Yes")
      btn3 = ButtonGadget(#PB_Any, 210, 90, 80, 30, "No")
    Else
      btn3 = ButtonGadget(#PB_Any, 210, 90, 80, 30, "Ok")
    EndIf
    
    ;Add the message image
    IconImage = ImageGadget(#PB_Any, 20, 20, 50, 50, 0)
    If Flags & #InformationIcon > 0
      CatchImage(0, ?Info)
      If IsImage(0)
        SetGadgetState(IconImage,ImageID(0)) 
      EndIf
    EndIf
    If Flags & #WarningIcon > 0
      CatchImage(Testimg, ?Warn)
      SetGadgetState(IconImage,ImageID(0))    
    EndIf  
    If Flags & #StopIcon > 0
      CatchImage(0, ?Stop)
      SetGadgetState(IconImage,ImageID(0))   
    EndIf
  
    ;Add the main message text
    txtMsg = TextGadget(#PB_Any, 70, 10, 220, 70, Msg)
    
    ;Make sure it stays on top
    StickyWindow(This_Window,#True)
    
    ;Start of message handling loop
    ;Not to be mixed up with the main application loop
    ;Allways include code to close this window!
    Repeat
      Event = WaitWindowEvent()
      
      Select EventWindow()
        
        Case This_Window ;Messages for this window only all others discarded

          Select Event
           
            Case #PB_Event_Gadget
            
              Select EventGadget()
                  
                ;Each button has a different meaning depending on
                ;the typr of message box
                  
                Case btn1
                  If Flags&#YesNoCancel > 0
                    RetVal = #MsgYes
                  EndIf 
                  CloseWindow(This_Window)
                  
                Case btn2
                  If Flags&#YesNoCancel > 0 
                    RetVal = #MsgNo
                  ElseIf Flags&#YesNo > 0
                    RetVal = #MsgYes  
                  ElseIf Flags&#OkCancel > 0
                    RetVal = #MsgOk                   
                  EndIf
                  CloseWindow(This_Window)
                  
                Case btn3
                  If Flags&#YesNoCancel > 0 Or Flags&#OkCancel > 0
                    RetVal = #MsgCancel
                  ElseIf Flags&#YesNo > 0
                    RetVal = #MsgNo 
                  Else
                    RetVal = #MsgOk
                  EndIf
                  CloseWindow(This_Window)
                
              EndSelect ;Eventgadget
            
          EndSelect ;Event
  
    EndSelect ;Eventwindow
    
  Until Not IsWindow(This_Window)
  
  ProcedureReturn RetVal
  
EndProcedure
  
EndModule

Next a throwaway test programme: frmMain.pb

Code: Select all

UsePNGImageDecoder() 

IncludeFile "App.pbi"
 
Window_0 = OpenWindow(#PB_Any, 0, 0, 400, 100, "Test", #PB_Window_SystemMenu)
Button_0 = ButtonGadget(#PB_Any, 170, 40, 100, 30, "Message")

Repeat
  Event = WaitWindowEvent()
   
    Select event
        
      Case #PB_Event_CloseWindow
        Break
        
      Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0
          Select App::Message("Stop Message","The app has crashed or some other major failure",App::#OkOnly|App::#StopIcon)
            Case App::#MsgOk
              Debug "OK Pressed"                
          EndSelect
          Select App::Message("Warning Message","Incorrect value entered for Title",App::#YesNoCancel|App::#WarningIcon)
            Case App::#MsgYes
              Debug "Yes Pressed"
            Case App::#MsgNo
              Debug "No Pressed"
            Case App::#MsgCancel
              Debug "Cancel Pressed"
          EndSelect
          Select App::Message("Information Message","File Saved Successfully",App::#OkCancel|App::#InformationIcon)
            Case App::#MsgCancel
              Debug "Cancel Pressed"
            Case App::#MsgOk
              Debug "OK Pressed"
          EndSelect
      EndSelect
  EndSelect

ForEver

Hope all find a use for it

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: User Module: XPlatform Messages and dialogs Modalish

Post by Keya »

nice! :) btw here's a related thread in which several others including myself have posted similar, probably youve already come across it in your searches but just linking for reference :)
http://purebasic.fr/english/viewtopic.php?f=12&t=50114 :)
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: User Module: XPlatform Messages and dialogs Modalish

Post by collectordave »

Hi Keya
probably youve already come across it in your searches but just linking for reference :)
viewtopic.php?f=12&t=50114 :)
The post that got me started looking at crossplatform stuff after just getting a mac! :D

Many many others viewed as well.

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: User Module: XPlatform Messages and dialogs Modalish

Post by collectordave »

Adding more stuff to the module so started a thread which I will update with anymore additions here http://www.purebasic.fr/english/viewtop ... 12&t=65132

Hope this helps someone!

Regards

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply