PureBasic Forum
http://forums.purebasic.com/english/

InputRequester with Flag for selecting the default text
http://forums.purebasic.com/english/viewtopic.php?f=12&t=72874
Page 1 of 1

Author:  infratec [ Fri May 24, 2019 8:30 am ]
Post subject:  InputRequester with Flag for selecting the default text

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:
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)

Author:  ar-s [ Fri May 24, 2019 3:29 pm ]
Post subject:  Re: InputRequester with Flag for selecting the default text

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

Code:
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)

Author:  Mindphazer [ Fri May 24, 2019 3:32 pm ]
Post subject:  Re: InputRequester with Flag for selecting the default text

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

Author:  skywalk [ Fri May 24, 2019 4:05 pm ]
Post subject:  Re: InputRequester with Flag for selecting the default text

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...

Author:  infratec [ Fri May 24, 2019 8:07 pm ]
Post subject:  Re: InputRequester with Flag for selecting the default text

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.

Author:  infratec [ Fri May 24, 2019 8:21 pm ]
Post subject:  Re: InputRequester with Flag for selecting the default text

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:

Author:  infratec [ Mon May 27, 2019 10:26 am ]
Post subject:  Re: InputRequester with Flag for selecting the default text

Added shortcut for escape key.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/