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
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
Regards
CD