MessageRequester_FW

Share your advanced PureBasic knowledge/code with the community.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

MessageRequester_FW

Post by Saki »

To test the new Font_Wizzard_BF module I have extended a small MessageRequester with it.

It has become very good, so I make it available here.
The code is easily extendable.
Everyone is free to do as he likes.

Only a few lines of code are enough to adapt the code for it.
The requester can now display any text.
The text is automatically fitted into the given buttons and the text window.

This little code shows how it is done,
it's a matter of a few minutes, nothing more.

Note that in the requester code below the used Gadgets are released when the requester is closed.

Just insert the Font_Wizzard_BF module at the top :
viewtopic.php?f=12&t=76338

Images, backgrounds, frames, ColorButtons can be easily added with FontWizzard_BF.

Have fun with it.

Image

DPI aware - all OS.
The requester can be resized at will, everything adapts automatically.

Code: Select all

Procedure MessageRequester_FW(title_line$="",
                              text$="",
                              flags.a=#PB_MessageRequester_Yes,
                              text_button_yes$="Yes",
                              text_button_no$="No",
                              text_button_cancel$="Cancel",
                              text_width=470,
                              text_height=165,
                              buttons_width=150,
                              buttons_height=35)
  
  ; (c) Saki - Free for using and enhancing
  ; The module Font_Wizzard_BF is needed, you found here :
  ; https://www.purebasic.fr/english/viewtopic.php?f=12&t=76338
  
  Protected text_flag=#PB_Text_Center|#PB_Text_Border ; You can change
  Protected font_ID
  
  flags << 5 : flags >> 5
  
  If text_width<120 : text_width=120 : EndIf
  If text_height<20 : text_height=20 : EndIf
  
  text_width+(1-text_width%2)
  Protected window_ID, button_yes_ID, button_no_ID, button_cancel_ID, text_ID, result, window_event
  
  If flags=#PB_MessageRequester_Yes
    If buttons_width>text_width-10 : buttons_width=text_width-10 : EndIf
  ElseIf flags=#PB_MessageRequester_YesNo
    If buttons_width*2+40>text_width-10 : buttons_width=text_width/2-20 : EndIf
  ElseIf flags=#PB_MessageRequester_YesNoCancel
    If buttons_width*3+60>text_width-10 : buttons_width=text_width/3-20 : EndIf
  EndIf
  
  window_ID=OpenWindow(#PB_Any, 0, 0, text_width+20, buttons_height+30+text_height, " "+title_line$, #PB_Window_Tool|#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_Invisible)
  If IsWindow(window_ID)
    StickyWindow(window_ID, #True)
    text_ID=CanvasGadget(#PB_Any, 10, 10, text_width, text_height)
    If StartDrawing(CanvasOutput(text_ID))
      DrawingMode(#PB_2DDrawing_Outlined)
      Box(0, 0, (text_width)*DesktopResolutionX(), text_height*DesktopResolutionY(), #Black)
      StopDrawing()
    EndIf
    
    button_yes_ID=ButtonGadget(#PB_Any, -buttons_width, WindowHeight(window_ID)-buttons_height-10, buttons_width, buttons_height, " "+text_button_yes$, #PB_Button_MultiLine)
    button_no_ID=ButtonGadget(#PB_Any, -buttons_width, WindowHeight(window_ID)-buttons_height-10, buttons_width, buttons_height, " "+text_button_no$, #PB_Button_MultiLine)
    button_cancel_ID=ButtonGadget(#PB_Any, -buttons_width, WindowHeight(window_ID)-buttons_height-10, buttons_width, buttons_height, " "+text_button_cancel$, #PB_Button_MultiLine)
    
    If Not IsGadget(text_ID) Or Not IsGadget(button_yes_ID) Or Not IsGadget(button_no_ID) Or Not IsGadget(button_cancel_ID)
      CloseWindow(window_ID)
      ProcedureReturn 0
    EndIf
    
    If flags=#PB_MessageRequester_Yes : HideGadget(button_no_ID, 1) : HideGadget(button_cancel_ID, 1)
      ResizeGadget(button_yes_ID, WindowWidth(window_ID)/2-buttons_width/2, #PB_Ignore, #PB_Ignore, #PB_Ignore)
    ElseIf flags=#PB_MessageRequester_YesNo : HideGadget(button_cancel_ID, 1)
      ResizeGadget(button_yes_ID, WindowWidth(window_ID)/2-buttons_width-10, #PB_Ignore, #PB_Ignore, #PB_Ignore)
      ResizeGadget(button_no_ID, WindowWidth(window_ID)/2+10, #PB_Ignore, #PB_Ignore, #PB_Ignore)
    ElseIf flags=#PB_MessageRequester_YesNoCancel
      ResizeGadget(button_yes_ID, WindowWidth(window_ID)/2-buttons_width*1.5-20, #PB_Ignore, #PB_Ignore, #PB_Ignore)
      ResizeGadget(button_no_ID, WindowWidth(window_ID)/2-buttons_width/2, #PB_Ignore, #PB_Ignore, #PB_Ignore)
      ResizeGadget(button_cancel_ID, WindowWidth(window_ID)/2+buttons_width/2+20, #PB_Ignore, #PB_Ignore, #PB_Ignore)
    EndIf
    
    HideWindow(window_ID, 0)
    
    Protected padding_x=10, padding_y
    
    ; Add a frame to all outputs - Padding is automatically adjusted - Alpha channel sensitive - Sample $8000FFFF
    AddFrame_FW(2, #Black)
    
    PresetMaxFontsize_FW(15) ; Sets the largest font size - You must deactivate again
    
    SetImageBasedGadgetText_FW(text_ID,
                               "Tahoma",
                               #PB_Font_Bold,
                               #Blue,  ; Text Color
                               0,      ; Text adjustment - 0=center - 1=left - 2=right
                               -1,     ; Background color ; Ignore = -1
                               text$,  ; Text
                               20,     ; Padding x
                               20,     ; Padding y
                               1.0)    ; Resize Factor
    
    AddFrame_FW() ; Deactivate again
    PresetMaxFontsize_FW() ; Deactivate again
    
    padding_y=GadgetHeight(button_yes_ID)/6/(GetTextLinesAmount_BF(text_button_yes$))
    SetGadgetText_FW(button_yes_ID,
                     "Tahoma",
                     #PB_Font_Bold,
                     text_button_yes$,
                     padding_x, ; Padding x
                     padding_y, ; Padding y
                     1.0)       ; Resize Factor
    
    padding_y=GadgetHeight(button_no_ID)/6/GetTextLinesAmount_BF(text_button_no$)
    SetGadgetText_FW(button_no_ID,
                     "Tahoma",
                     #PB_Font_Bold,
                     text_button_no$,
                     padding_x, ; Padding x
                     padding_y, ; Padding y
                     1.0)       ; Resize Factor
    
    padding_y=GadgetHeight(button_cancel_ID)/6/GetTextLinesAmount_BF(text_button_cancel$)
    SetGadgetText_FW(button_cancel_ID,
                     "Tahoma",
                     #PB_Font_Bold,
                     text_button_cancel$,
                     padding_x, ; Padding x
                     padding_y, ; Padding y
                     1.0)       ; Resize Factor
    
    Repeat
      window_event=WaitWindowEvent()
      If window_event=#PB_Event_Gadget
        Select EventGadget()
          Case button_yes_ID
            result=#PB_MessageRequester_Yes
          Case button_no_ID
            result=#PB_MessageRequester_No
          Case button_cancel_ID
            result=#PB_MessageRequester_Cancel
        EndSelect
      EndIf
      If window_event=#PB_Event_CloseWindow
        End
      EndIf
    Until result
    FreeGadget_FW(text_ID)
    FreeGadget_FW(button_yes_ID)
    FreeGadget_FW(button_no_ID)
    FreeGadget_FW(button_cancel_ID)
    CloseWindow(window_ID)
    ProcedureReturn result
  EndIf
EndProcedure

Define title_line$="MessageRequester_FW"
Define text$="Hello World"+#LF$+#LF$+
             "This is MessageRequester_FW"+#LF$+#LF$+
             "Have Fun"
Define flags.a=#PB_MessageRequester_YesNoCancel
Define text_button_yes$="Yes"+#LF$+"it's OK"     
Define text_button_no$="NoNo"
Define text_button_cancel$="Or not"
Define text_width=450
Define text_height=165
Define buttons_width=150
Define buttons_height=32

MessageRequester_FW("MessageRequester_FW",
                    text$,
                    flags.a,
                    text_button_yes$,
                    text_button_no$,
                    text_button_cancel$,
                    text_width,
                    text_height,
                    buttons_width,
                    buttons_height)
Last edited by Saki on Wed Dec 16, 2020 11:21 am, edited 9 times in total.
地球上の平和
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: MessageRequester_FW

Post by Kwai chang caine »

Works great :wink:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply