MessageRequesterEx

Share your advanced PureBasic knowledge/code with the community.
Mesa
Enthusiast
Enthusiast
Posts: 345
Joined: Fri Feb 24, 2012 10:19 am

MessageRequesterEx

Post by Mesa »

A simple customizable requestermessager.

Code: Select all

;======================================================
;
; Message requester Ex: 1 to 3 buttons and icon or not
;
; Mesa 24-11-2020
;
; Should be multiplateform and x32 and x64, dpi not tested
;
; 1) Add icon in the DataSection
; 2) If you like, you can add font, font size, colors, choose align (line 47) and border line (48)
; 3) MessageRequesterEx(Title$, Message$, TextButton1$, TextButton2$ = "",TextButton3$ = "", Icon = -1, IconOnTheLeft = #True)
;     1 button mini, 3 buttons maxi (but you can add more buttons if you like, just code it ;)
;     If Icon = -1 => no icon else icon number 0, etc... will be displayed from the DataSection
;     IconOnTheLeft = #True => icon on the left otherwise on the right
;======================================================

EnableExplicit


Procedure MessageRequesterEx(Title$, Message$, TextButton1$, TextButton2$ = "",TextButton3$ = "", Icon = -1, IconOnTheLeft = #True)
  
  Protected MarginLeft, MarginRight, MarginUp, MarginDown, MarginBetweenButton, MarginBottom
  Protected RequesterX, RequesterY, RequesterW, RequesterH, RequesterFlags, RequesterID, RequestEvent
  Protected MessageX, MessageY
  Protected ButtonX, ButtonY, ButtonW, ButtonH
  Protected border, align, PadText, PadIcon, gw, gh, tmp
  Protected ContID, TextGadgetID, IconID, Button1ID, Button2ID, Button3ID, ImageGadgetID
  Protected IconeX, IconeY, *Icon_Start, *Icon_Size
  
  
  ; Choose your margins as you like
  ;==============================================================
  MarginLeft = 30
  MarginRight = MarginLeft
  MarginUp = 10
  MarginDown = 20
  MarginBetweenButton = 6
  MarginBottom = 10
  PadText = 3 ;Margins all around the message
  PadIcon = 10;Margins all around the icon
  
  ; Init Icon 
  ;==============================================================
  If Icon <> -1
    UsePNGImageDecoder()
    ;Use decoders here
    
    Select icon
        
      Case 0
        *Icon_Start = ?Icon0_Start
        *Icon_Size = ?Icon0_End - ?Icon0_Start
        
        ;case ...
        
    EndSelect
    
    iconID = CatchImage(#PB_Any, *Icon_Start, *Icon_Size);:ResizeImage(iconID,128,128)
    MarginLeft = ImageWidth(iconID) + PadIcon + PadIcon
    MarginRight = MarginLeft
    MarginUp = ImageHeight(iconID) + PadIcon + PadIcon
    
  EndIf 
  
  
  ; Open a window
  ;==============================================================
  RequesterX = 1
  RequesterY = 1
  RequesterW = 1
  RequesterH = 1
  RequesterFlags = #PB_Window_ScreenCentered|#PB_Window_Invisible
  
  RequesterID = OpenWindow(#PB_Any, RequesterX, RequesterY, RequesterW, RequesterY, Title$, RequesterFlags)
  
  
  If RequesterID
    
    StickyWindow(RequesterID, #True)
    
    ; Message
    ;==============================================================
    ;border : 0=Borderless - 1=Flat 2=Raised - 3=Single - 4=Double
    ;align  : 0=left - 1=center - 2=right
    border = 1  ; <==== BORDER
    align = 1   ; <==== ALIGN
    
    ContID=ContainerGadget(#PB_Any, 0, 0, 0, 0, border)
    TextGadgetID = TextGadget(#PB_Any, 0, 0, 0, 0, Space(1), align)
    CloseGadgetList()
    
    ; Set font here
    ;SetGadgetFont(TextGadgetID,FontID(.))
    
    SetGadgetText(TextGadgetID, Message$)
    gw = GadgetWidth(TextGadgetID,#PB_Gadget_RequiredSize)
    gh = GadgetHeight(TextGadgetID,#PB_Gadget_RequiredSize) 
    
    ; Set Colors here
    ;SetGadgetColor(ContID,#PB_Gadget_BackColor, bColor)
    ;SetGadgetColor(TextGadgetID,#PB_Gadget_BackColor, bColor)
    ;SetGadgetColor(TextGadgetID,#PB_Gadget_FrontColor, fColor)
    
    MessageX = MarginLeft
    MessageY = MarginUp
    
    ResizeGadget(ContID, MessageX, MessageY, gw+PadText*4, gh+PadText*2)
    If align = #PB_Text_Center
      ResizeGadget(TextGadgetID,PadText,PadText,gw+PadText*2,gh)
    ElseIf align = #PB_Text_Right
      ResizeGadget(TextGadgetID, PadText*2, PadText, gw, gh)
    Else
      ResizeGadget(TextGadgetID, PadText*2, PadText, gw+PadText*2, gh)
    EndIf  
    
    
    ; Buttons
    ;==============================================================
    ButtonX = MarginLeft
    ButtonY = MarginUp + GadgetHeight(TextGadgetID) + MarginDown
    
    Button1ID = ButtonGadget(#PB_Any, ButtonX, ButtonY, 1, 1,
                             TextButton1$)
    
    ; Set font here
    ;SetGadgetFont(Button1ID, FontID(.))
    ButtonW = GadgetWidth(Button1ID, #PB_Gadget_RequiredSize)
    ButtonH = GadgetHeight(Button1ID, #PB_Gadget_RequiredSize)
    ResizeGadget(Button1ID,  #PB_Ignore,  #PB_Ignore, ButtonW, ButtonH)
    
    RequesterW = ButtonX + ButtonW + MarginRight
    
    If TextButton2$
      Button2ID = ButtonGadget(#PB_Any, 1, 1, 1, 1, TextButton2$)
      ; Set font here
      ;SetGadgetFont(Button2ID, FontID(.))
      tmp = GadgetWidth(Button2ID, #PB_Gadget_RequiredSize)
      If tmp > ButtonW
        ButtonW = tmp
      EndIf
      tmp = GadgetHeight(Button2ID, #PB_Gadget_RequiredSize)
      If tmp > ButtonH
        ButtonH = tmp
      EndIf
    EndIf
    
    
    If TextButton3$
      Button3ID = ButtonGadget(#PB_Any, 1, 1, 1, 1, TextButton3$)
      ; Set font here
      ;SetGadgetFont(Button3ID, FontID(.))
      tmp = GadgetWidth(Button3ID, #PB_Gadget_RequiredSize)
      If tmp > ButtonW
        ButtonW = tmp
      EndIf
      tmp = GadgetHeight(Button3ID, #PB_Gadget_RequiredSize)
      If tmp > ButtonH
        ButtonH = tmp
      EndIf
    EndIf
    
    If Button2ID
      ButtonX + GadgetWidth(Button1ID) + MarginBetweenButton
      ResizeGadget(Button2ID,  ButtonX,  ButtonY, ButtonW, ButtonH)
    EndIf
    
    If Button3ID
      ButtonX + GadgetWidth(Button2ID) + MarginBetweenButton
      ResizeGadget(Button3ID,  ButtonX,  ButtonY, ButtonW, ButtonH)
    EndIf
    
    If Bool(Button2ID > 0 Or Button3ID > 0)
      ResizeGadget(Button1ID,  #PB_Ignore,  #PB_Ignore, ButtonW, ButtonH)
      RequesterW = ButtonX + ButtonW + MarginRight
    EndIf  
    
    
    ; Resize everybody
    ;==============================================================
    RequesterH = ButtonY + ButtonH + MarginBottom
    
    ResizeGadget(ContID, #PB_Ignore, #PB_Ignore, RequesterW - MarginLeft - MarginRight, #PB_Ignore)
    If align=#PB_Text_Center
      ResizeGadget(TextGadgetID,GadgetWidth(ContID)/2-gw/2-PadText, #PB_Ignore, #PB_Ignore, #PB_Ignore )
    ElseIf align=#PB_Text_Right
      ResizeGadget(TextGadgetID, GadgetWidth(ContID)-gw-PadText, #PB_Ignore, #PB_Ignore, #PB_Ignore )
    EndIf  
    
    ResizeWindow(RequesterID, #PB_Ignore,  #PB_Ignore, RequesterW, RequesterH)
    
    
    ;Show Icon 
    ;==============================================================
    If Icon <> -1
      
      
      If IconOnTheLeft = #True
        IconeX = MarginLeft/2 - ImageWidth(iconID) / 2
        IconeY = PadIcon
      Else
        IconeX = RequesterW - MarginRight / 2 - ImageWidth(iconID) / 2
        IconeY = PadIcon
      EndIf
      
      
      
      ImageGadgetID = ImageGadget(#PB_Any, 
                                  IconeX, 
                                  IconeY, 
                                  ImageWidth(iconID), 
                                  ImageHeight(iconID), 
                                  ImageID(iconID))
      
    EndIf 
    
    
    ;Show the requester
    ;==============================================================
    HideWindow(RequesterID, #False)
    
    
    ;Loop
    ;==============================================================
    Repeat
      RequestEvent = WaitWindowEvent()
      
      Select RequestEvent
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case Button1ID
              CloseWindow(RequesterID); or do what you want ...
              ProcedureReturn 1 
              
            Case Button2ID
              CloseWindow(RequesterID); or do what you want ...
              ProcedureReturn 2
              
            Case Button3ID
              CloseWindow(RequesterID); or do what you want ...
              ProcedureReturn 3
              
            Case ImageGadgetID
              Debug "The icon has been clicked"
          EndSelect
          
      EndSelect
    Until RequestEvent = #PB_Event_CloseWindow
  EndIf
  
  ;DataSection 
  ;==============================================================
  DataSection
    Icon0_Start:
    IncludeBinary #PB_Compiler_Home+"Examples\Sources\Data\world.png"
    Icon0_End:
  EndDataSection
  
EndProcedure



CompilerIf #PB_Compiler_IsMainFile
  ;- Examples 
  
  Define Message$ = "Line 1" + #LF$ + "Line 2" + #TAB$ + "1234" + #LF$ + "Line 3"
  
  Debug MessageRequesterEx("Icon on the right, border and  center alignment", Message$, "TextButton1$", "Cancel ?","?", 0, #False) ; icon number 0 on the right
  Debug MessageRequesterEx("Icon on the left", Message$, "TextButton1$", "Cancel ?","?", 0, #True)  ; icon number 0 on the left
  Debug MessageRequesterEx("No icon", Message$, "TextButton1$", "Cancel ?","?"); no icon 
  
CompilerEndIf 

M.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5342
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MessageRequesterEx

Post by Kwai chang caine »

It's right the native messagerequester is a little bit poor in option :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: MessageRequesterEx

Post by davido »

@Mesa,
Looks good. Works on my MacBook with a change to the line 255 below:

Code: Select all

IncludeBinary #PB_Compiler_Home+"Examples/Sources/Data/world.png"
With this change, I think, it should still work on Windows.

Thank you :D
DE AA EB
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: MessageRequesterEx

Post by mestnyi »

It's useless until Fred allows the loop inside the bind.
Or until they find a replacement using api for all os.
viewtopic.php?f=19&t=67807&p=561971#p561971

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  ;- Examples
  Global Event, Message$ = "Line 1" + #LF$ + "Line 2" + #TAB$ + "1234" + #LF$ + "Line 3"
  
  Procedure Event_click()
    Debug MessageRequesterEx("Icon on the right, border and  center alignment", Message$, "TextButton1$", "Cancel ?","?", 0, #False) ; icon number 0 on the right
    Debug MessageRequesterEx("Icon on the left", Message$, "TextButton1$", "Cancel ?","?", 0, #True)                                 ; icon number 0 on the left
    Debug MessageRequesterEx("No icon", Message$, "TextButton1$", "Cancel ?","?")                                                    ; no icon
  EndProcedure
  
  OpenWindow(0, 100, 100, 200, 200, "demo", #PB_Window_SizeGadget | #PB_Window_SystemMenu)
  ButtonGadget(0, 10, 10, 80, 30, "message")
  
  BindGadgetEvent(0, @Event_click())
  
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
CompilerEndIf
Post Reply