Code: Select all
;*************************************************
; PROJECT DATA
;*************************************************
; Project name: Module mbox
; Project works on: Windows 7
;*************************************************
; Features
; - You can write text in the buttons
; - Don't need to define a result variable in main program
; - Project code and comments are all written in english
;*************************************************
DeclareModule mbox
; creating independent OS text for buttons
Global mbox_button_ok_text$="OK"
Global mbox_button_yes_text$="Yes"
Global mbox_button_no_text$="No"
Global mbox_button_cancel_text$="Cancel"
Global mbox_result ; stores return value
; contants to represent flags (must be declared here for you to use it outside module)
#mbox_noflags=0
#mbox_ok=1
#mbox_yesno=2
#mbox_yesnocancel=3
#mbox_info=11
#mbox_warning=22
#mbox_error=33
; constants for result of buttons press (must be declared here for you to use it outside module)
#mbox_ok=1
#mbox_yes=2
#mbox_no=3
#mbox_cancel=4
Declare mbox_messagebox(boxtitle$,boxtext$,flags)
EndDeclareModule
Module mbox
; so what do we need ... hum ... a window, a text message, an image and some buttons ... let's enumerate this idea
Enumeration mbox #PB_Compiler_EnumerationValue
#mbox_window
#mbox_textgadget_message
#mbox_imagegadget
#mbox_button_ok
#mbox_button_cancel
#mbox_button_yes
#mbox_button_no
#mbox_image_info
#mbox_image_warning
#mbox_image_error
#mbox_font
EndEnumeration
; $boxtitle - put the box title
; $boxtitle - put the box text
; flags - put the flags
; procedure will return something to variable mbox_result when you press buttons
Procedure mbox_messagebox(boxtitle$,boxtext$,flags)
Protected quit
Protected backcolor=RGB(64,62,73)
Protected forecolor=RGB(255,255,255)
Protected fieldforecolor=RGB(0,0,0)
Protected fieldbackcolor=RGB(181,230,29)
;need to create a dir called images
Protected pathmoduleimages.s="images/"
;Protected pathmoduleimages.s=GetPathPart(ProgramFilename())+"modules/mbox/images/"
UsePNGImageDecoder()
;get the 32x32 pixels images down there and give them this names
LoadImage(#mbox_image_info,pathmoduleimages+"messagebox_info.png")
LoadImage(#mbox_image_warning,pathmoduleimages+"messagebox_warning.png")
LoadImage(#mbox_image_error,pathmoduleimages+"messagebox_error.png")
; let's make a window
;
If OpenWindow(#mbox_window, 0, 0, 300, 150, boxtitle$, #PB_Window_ScreenCentered)
SetWindowColor(#mbox_window, BackColor)
StickyWindow(#mbox_window, #True) ; stay on top
If LoadFont(#mbox_font, "Consolas", 10)
SetGadgetFont(#PB_Default, FontID(#mbox_font))
EndIf
TextGadget(#mbox_textgadget_message,10,50,280,40,boxtext$)
SetGadgetColor(#mbox_textgadget_message, #PB_Gadget_FrontColor, fieldforecolor)
SetGadgetColor(#mbox_textgadget_message, #PB_Gadget_BackColor, fieldbackcolor)
Select flags
Case #mbox_ok
ButtonGadget(#mbox_button_ok, 10, 110, 70, 30, "OK")
Case #mbox_ok+#mbox_info
ButtonGadget(#mbox_button_ok, 10, 110, 70, 30, mbox_button_ok_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_info))
Case #mbox_ok+#mbox_warning
ButtonGadget(#mbox_button_ok, 10, 110, 70, 30, mbox_button_ok_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_warning))
Case #mbox_ok+#mbox_error
ButtonGadget(#mbox_button_ok, 10, 110, 70, 30, mbox_button_ok_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_error))
Case #mbox_yesno
ButtonGadget(#mbox_button_yes, 10, 110, 70, 30, mbox_button_yes_text$)
ButtonGadget(#mbox_button_no, 85, 110, 70, 30, mbox_button_no_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_info))
Case #mbox_yesno+#mbox_info
ButtonGadget(#mbox_button_yes, 10, 110, 70, 30, mbox_button_yes_text$)
ButtonGadget(#mbox_button_no, 85, 110, 70, 30, mbox_button_no_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_info))
Case #mbox_yesno+#mbox_warning
ButtonGadget(#mbox_button_yes, 10, 110, 70, 30, mbox_button_yes_text$)
ButtonGadget(#mbox_button_no, 85, 110, 70, 30, mbox_button_no_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_warning))
Case #mbox_yesno+#mbox_error
ButtonGadget(#mbox_button_yes, 10, 110, 70, 30, mbox_button_yes_text$)
ButtonGadget(#mbox_button_no, 85, 110, 70, 30, mbox_button_no_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_error))
Case #mbox_yesnocancel
ButtonGadget(#mbox_button_yes, 10, 110, 70, 30, mbox_button_yes_text$)
ButtonGadget(#mbox_button_no, 85, 110, 70, 30, mbox_button_no_text$)
ButtonGadget(#mbox_button_cancel, 160, 110, 70, 30, mbox_button_cancel_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_info))
Case #mbox_yesnocancel+#mbox_info
ButtonGadget(#mbox_button_yes, 10, 110, 70, 30, mbox_button_yes_text$)
ButtonGadget(#mbox_button_no, 85, 110, 70, 30, mbox_button_no_text$)
ButtonGadget(#mbox_button_cancel, 160, 110, 70, 30, mbox_button_cancel_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_info))
Case #mbox_yesnocancel+#mbox_warning
ButtonGadget(#mbox_button_yes, 10, 110, 70, 30, mbox_button_yes_text$)
ButtonGadget(#mbox_button_no, 85, 110, 70, 30, mbox_button_no_text$)
ButtonGadget(#mbox_button_cancel, 160, 110, 70, 30, mbox_button_cancel_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_warning))
Case #mbox_yesnocancel+#mbox_error
ButtonGadget(#mbox_button_yes, 10, 110, 70, 30, mbox_button_yes_text$)
ButtonGadget(#mbox_button_no, 85, 110, 70, 30, mbox_button_no_text$)
ButtonGadget(#mbox_button_cancel, 160, 110, 70, 30, mbox_button_cancel_text$)
ImageGadget(#mbox_imagegadget, 10, 10, 32, 32, ImageID(#mbox_image_error))
EndSelect
result=0
quit=0
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case #mbox_button_ok
mbox_result=#mbox_ok
quit=1
Case #mbox_button_no
mbox_result=#mbox_no
quit=1
Case #mbox_button_yes
mbox_result=#mbox_yes
quit=1
Case #mbox_button_cancel
mbox_result=#mbox_cancel
quit=1
EndSelect
EndSelect
Until quit=1
CloseWindow(#mbox_window)
EndIf
;ProcedureReturn result
EndProcedure
EndModule
appWin = OpenWindow(#PB_Any, 100, 100, 400, 200, "Test Message Box",
#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
button = ButtonGadget(#PB_Any, 150, 60, 100, 30, "Show Message")
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_CloseWindow
appQuit = 1
Case #PB_Event_Gadget
If EventGadget() = button
DisableWindow(appWin, #True)
UseModule mbox
;with the UseModule keyboard the module prefix can be omitted
mbox_button_ok_text$="Ok"
mbox_button_yes_text$="Yes"
mbox_button_no_text$="No"
mbox_button_cancel_text$="Cancel"
mbox_messagebox("Area 51 - Warning","Alien life form is contained in dark chamber. Go to night vision mode ?",mbox::#mbox_yesno+mbox::#mbox_warning)
Select mbox_result
Case #mbox_ok
Debug "OK!"
Case #mbox_yes
Debug "Yes!"
Case #mbox_no
Debug "No!"
Case #mbox_cancel
Debug "Cancel!"
EndSelect
DisableWindow(appWin, #False)
SetActiveWindow(appWin)
EndIf
EndSelect
Until appQuit
is called, the module prefix can be safely omitted.