InputRequester with Flag for selecting the default text

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

InputRequester with Flag for selecting the default text

Post by infratec »

Hi,

I used the InputRequester() for a search function in an EditorGadget().
I used the last search string as default value.
But when someone simply start to enter a new search text, it was appended to the old text.
To avoid this I needed that the default text is 'selected'. Then the behaviour is more 'normal'.

Code: Select all

CompilerIf Not Defined(PB_InputRequester_SelectText, #PB_Constant)
  #PB_InputRequester_SelectText = 2
CompilerEndIf

; if you need crossplatform selection look here:
; https://www.purebasic.fr/english/viewtopic.php?f=13&t=51950&start=3

Procedure.s OwnInputRequester(Titel$, Message$, DefaultString$, Flags.i=0)
 
  Protected Win.i, StringG.i, StringGFlags.i, ButtonG.i, Text$, Exit.i, Event.i, ActiveWindow.i
 
  ActiveWindow = GetActiveWindow()
  If IsWindow(ActiveWindow)
    Win = OpenWindow(#PB_Any, 0, 0, 300, 110, Titel$, #PB_Window_SystemMenu|#PB_Window_WindowCentered, WindowID(ActiveWindow))
  Else
    Win = OpenWindow(#PB_Any, 0, 0, 300, 110, Titel$, #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  EndIf
  If Win
   
    If Flags & #PB_InputRequester_Password
      StringGFlags = #PB_String_Password
    EndIf
   
    TextGadget(#PB_Any, 10, 10, 280, 20, Message$)
   
    StringG = StringGadget(#PB_Any, 10, 35, 280, 20, DefaultString$)
    If Flags & #PB_InputRequester_SelectText
      SendMessage_(GadgetID(StringG), #EM_SETSEL, 0, -1)
    EndIf
   
    ButtonG = ButtonGadget(#PB_Any, 110, 70, 80, 25, "Ok")
   
    SetActiveGadget(StringG)
   
    AddKeyboardShortcut(Win, #PB_Shortcut_Return, 1)
    AddKeyboardShortcut(Win, #PB_Shortcut_Escape, 2)
   
    Repeat
      Event = WaitWindowEvent()
     
      If EventWindow() = Win
        Select Event
          Case #PB_Event_Menu
            Select EventMenu()
              Case 1 : PostEvent(#PB_Event_Gadget, Win, ButtonG)
              Case 2 : Exit = #True
            EndSelect
           
          Case #PB_Event_Gadget
            Select EventGadget()
              Case ButtonG
                Text$ = GetGadgetText(StringG)
                Exit = #True
            EndSelect
           
          Case #PB_Event_CloseWindow
            Exit = #True

        EndSelect
      EndIf
     
    Until Exit
   
    CloseWindow(Win)
   
  EndIf
 
  ProcedureReturn Text$
 
EndProcedure



Search$ = OwnInputRequester("Search", "Enter your search string:", Search$, #PB_InputRequester_SelectText)

MessageRequester("Info", "Now the second search")

Search$ = OwnInputRequester("Search", "Enter your search string:", Search$, #PB_InputRequester_SelectText)
Last edited by infratec on Mon May 27, 2019 10:25 am, edited 3 times in total.
User avatar
ar-s
Enthusiast
Enthusiast
Posts: 340
Joined: Sat Oct 06, 2007 11:20 pm
Location: France

Re: InputRequester with Flag for selecting the default text

Post by ar-s »

I'm no sure to understand what you need. Maybe something like that ?

Code: Select all

CompilerIf Not Defined(PB_InputRequester_SelectText, #PB_Constant)
  #PB_InputRequester_SelectText = 2
CompilerEndIf

; if you need crossplatform selection look here:
; https://www.purebasic.fr/english/viewtopic.php?f=13&t=51950&start=3

Procedure.s OwnInputRequester(Titel$, Message$, DefaultString$, Flags.i=0)
 
  Protected Win.i, StringG.i, StringGFlags.i, ButtonG.i, Text$, Exit.i, Event.i, ActiveWindow.i
 
  ActiveWindow = GetActiveWindow()
  If IsWindow(ActiveWindow)
    Win = OpenWindow(#PB_Any, 0, 0, 300, 120, Titel$, #PB_Window_SystemMenu|#PB_Window_WindowCentered, WindowID(ActiveWindow))
  Else
    Win = OpenWindow(#PB_Any, 0, 0, 300, 120, Titel$, #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  EndIf
  If Win
   
    If Flags & #PB_InputRequester_Password
      StringGFlags = #PB_String_Password
    EndIf
   
    TextGadget(#PB_Any, 10, 10, 280, 20, Message$)
   
    StringG = StringGadget(#PB_Any, 10, 40, 280, 20, DefaultString$)
    
    
;     
;     If Flags & #PB_InputRequester_SelectText
;       SendMessage_(GadgetID(StringG), #EM_SETSEL, 0, -1)
;     EndIf
   
    ButtonG = ButtonGadget(#PB_Any, 25, 80, 240, 30, "Write txt then Push me (" +Str(3-Count) + " try To test)")
   
    SetActiveGadget(StringG)
    SetActiveGadget(ButtonG)
    
    Repeat
      Event = WaitWindowEvent()
     
      If EventWindow() = Win
        Select Event
          Case #PB_Event_Gadget
            Select EventGadget()
              Case StringG
                Select EventType()
                  Case #PB_EventType_Focus 
                    If GetGadgetText(StringG) = OldM.s
                      SetGadgetText(StringG,"")
                    EndIf
                  Case #PB_EventType_LostFocus
                    OldM.s = GetGadgetText(StringG)
                EndSelect
                
               
                
                
              Case ButtonG
                OldM.s = GetGadgetText(StringG)
                Count + 1
                
                SetGadgetText( ButtonG, "Write txt then Push me (" +Str(3-Count) + " try To test)")
                If COunt = 3
                  Exit = #True
                EndIf
              
            EndSelect
           
          Case #PB_Event_CloseWindow
            Exit = #True
        EndSelect
      EndIf
     
    Until Exit
   
    CloseWindow(Win)
   
  EndIf
 
  ProcedureReturn Text$
 
EndProcedure

Debug OwnInputRequester("Search", "Enter your search string:", "test", #PB_InputRequester_SelectText)
~Ar-S~
My Image Hoster for PB users
My webSite (french) with PB apps : LDVMULTIMEDIA
PB - 3.x / 5.7x / 6 - W11 x64 - Ryzen 7 3700x / #Rpi4

Code: Select all

r3p347 : 7ry : un71l d0n3 = 1
Mindphazer
Enthusiast
Enthusiast
Posts: 340
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: InputRequester with Flag for selecting the default text

Post by Mindphazer »

I don't think he needs anything...
He's just proposing a solution for an inputrequester that behaves like he wants to : an option for the text in the requester to be already selected
MacBook Pro 14" M1 Pro - 16 Gb - MacOS 14 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: InputRequester with Flag for selecting the default text

Post by skywalk »

Optionally this is fine, but definitely an example of "you cannot please all the people all the time".
I hate any dialog box that is preselected. :evil:
Many of my additional searches require 1 or 2 characters to change.
So, each attempt to do a 2nd or 3rd search requires I mouse click to unselect, then navigate to the character position that needs to change. Very annoying when you have a very long string to navigate.

stepping off soapbox...
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: InputRequester with Flag for selecting the default text

Post by infratec »

Hey, we are here in Tricks 'n' Tips not in Coding Questions :wink:

I wrote this InputRequester() with an optional flag for selecting the text.

So I can now use the InputRequester() for searching in a text.
It has now the same behaviour like ctrl+f in FireFox (and in other editors).
The first input is stored an if you open the dialog the second time, the old search text is selected.
If you want you can start with a new search text, or press end and modify the text.

I made also a feature request for this flag.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: InputRequester with Flag for selecting the default text

Post by infratec »

I added above a keyboard shortcut for return (like in the original InputRequester)
And I modified the example, so that you have more an impression why I did this.

Btw. you should enter a few letters in the first requester :wink:
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: InputRequester with Flag for selecting the default text

Post by infratec »

Added shortcut for escape key.
Post Reply