Page 1 of 1

[Windows] custom InputRequester

Posted: Thu Aug 08, 2002 10:31 am
by BackupUser
Code updated for 5.20+
Restored from previous forum. Originally posted by fweil.

Hello,

Some months ago when starting with PureBasic I discovered that Input regular function applied only to console mode and could not find a nice and easy way to have equivalent Input for window mode.

I looked requesters and different things but the results were not satisfying because of bad text / string gadget possibilities (I thought it was not good enough when beginning).

Now with more experience and a basic knowledge of PureBasic, and thanks to the forum numerous available users and exchanged tricks 'n' tips, I have a nice code for an InputRequester feature to insert in my programs.

Here you will find a piece of code giving to string input requesters, one single-lined and the other multi-lined.

This works fine and is easy to convert in include or DLL for those who prefer. For my own purpose I do not make it DLL because I consider either to have application based customization to do and that PureBasic compiler is fast enough to cut and paste it directly to application's source code.

This sample code is commented enough I believe and meaningful for window programming. It uses a really few API tricks but is WIN32API based.

Hope this example will help and give ideas / answers.

Rgrds

Code: Select all

;====================================================================
;
; InputRequesterS is a Requester function displaying a string box
; and returning the entered string.
;
; A title and a default value are passed, and return key or OK button
; allow to return from procedure.
;
; If Escape key is pushed or the requester is closed without return or OK
; validation , the returned value is the default.
;
Procedure.s InputRequesterS(lTitle.s, lDefault.s)
  
  ;
  ; Constants are set for possible integration
  ;
  #InputRequesterSWID = 99
  #InputRequesterSGID = 198
  
  lXSize.l = 200
  lYSize.l = 25
  ;
  ; GetSystemMetrics_ API function gives access to screen dimensions
  ;
  If OpenWindow(#InputRequesterSWID, (GetSystemMetrics_(#SM_CXSCREEN) - lXSize) / 2, (GetSystemMetrics_(#SM_CYSCREEN) - lYSize) / 2, lXSize + 40, lYSize,lTitle,#PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
    AddKeyboardShortcut(#InputRequesterSWID, #PB_Shortcut_Return, 10)
    AddKeyboardShortcut(#InputRequesterSWID, #PB_Shortcut_Escape, 99)
    ;If CreateGadgetList(WindowID())
      StringGadget(#InputRequesterSGID, 2, 2, lXSize - 4, lYSize - 5, lDefault)
      ButtonGadget(#InputRequesterSGID + 1, lXSize + 5, lYSize - 25, 30, 20, "OK")
    ;EndIf
    lResult.s = GetGadgetText(#InputRequesterSGID)
    lQuit.l = #False
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          lQuit = #True
        Case #PB_Event_Menu
          Select EventMenu()
            Case 10
              lResult = GetGadgetText(#InputRequesterSGID)
              lQuit = #True
            Case 99
              lQuit = #True
          EndSelect
        Case #PB_Event_Gadget
          Select EventGadget()
            Case (#InputRequesterSGID + 1)
              lResult = GetGadgetText(#InputRequesterSGID)
              lQuit = #True
          EndSelect
      EndSelect
    Until lQuit
    RemoveKeyboardShortcut(#InputRequesterSWID, #PB_Shortcut_All)
    CloseWindow(#InputRequesterSWID)
  EndIf
  ProcedureReturn lResult
EndProcedure

;
; InputRequesterSML is a multilined string input requester.
; It is like InputRequesterS but does not return when using the return key
; and allows mulitple lines handling.
;
; Also this requester gives the possibility of resizing to meet user's needs
;
Procedure.s InputRequesterSML(lTitle.s, lDefault.s)
  
  #InputRequesterSWID = 99
  #InputRequesterSGID = 198
  
  lXSize.l = 200
  lYSize.l = 80
  If OpenWindow(#InputRequesterSWID, (GetSystemMetrics_(#SM_CXSCREEN) - lXSize) / 2, (GetSystemMetrics_(#SM_CYSCREEN) - lYSize) / 2, lXSize + 40, lYSize,lTitle, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)
    AddKeyboardShortcut(#InputRequesterSWID, #PB_Shortcut_Escape, 99)
    ;If CreateGadgetList(WindowID())
      EditorGadget(#InputRequesterSGID, 2, 2, lXSize - 4, lYSize - 5)
      SetGadgetText(#InputRequesterSGID,lDefault)
      ButtonGadget(#InputRequesterSGID + 1, lXSize + 5, lYSize - 25, 30, 20, "OK")
    ;EndIf
    lResult.s = GetGadgetText(#InputRequesterSGID)
    lQuit.l = #False
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          lQuit = #True
        Case #PB_Event_Menu
          Select EventMenu()
            Case 99
              lQuit = #True
          EndSelect
        Case #PB_Event_Gadget
          Select EventGadget()
            Case (#InputRequesterSGID + 1)
              lResult = GetGadgetText(#InputRequesterSGID)
              lQuit = #True
          EndSelect
      EndSelect
      If WindowWidth(#InputRequesterSWID) > lXSize + 40 Or WindowHeight(#InputRequesterSWID) > lYSize
        lXSize = WindowWidth(#InputRequesterSWID) - 40
        lYSize = WindowHeight(#InputRequesterSWID)
        ResizeGadget(#InputRequesterSGID, 2, 2, lXSize - 4, lYSize - 5)
        ResizeGadget(#InputRequesterSGID + 1, lXSize + 5, lYSize - 25, 30, 20)
      EndIf
    Until lQuit
    RemoveKeyboardShortcut(#InputRequesterSWID, #PB_Shortcut_All)
    CloseWindow(#InputRequesterSWID)
  EndIf
  ProcedureReturn lResult
EndProcedure

;
; Main test code
;
sString.s

sString = InputRequesterS("Enter a text", "Default")
 Debug sString
 
 sString = InputRequesterSML("Enter a text", "Default")
Debug sString
End
;====================================================================
Francois Weil
14, rue Douer
F64100 Bayonne

Posted: Thu Aug 08, 2002 11:08 am
by BackupUser
Restored from previous forum. Originally posted by PB.

> I have a nice code for an InputRequester feature to insert in my programs.

Fred said he's adding an InputRequester to a future release, but thanks for
coding this one anyway! (This is another reason why Fred should have a section
on his web site that lists all current fixed bugs and new added features).


PB - Registered PureBasic Coder

Posted: Sat Aug 10, 2002 1:17 am
by BackupUser
Restored from previous forum. Originally posted by Art Sentinel.

Hi fweil,

Thank you for this code! This was most helpfull in teaching me something I was not quite sure of. :)

Take care.


- Art Sentinel
http://www.artsentinel.net

--------------

Top Ten Reasons Not To Procrastinate:


Coming Soon...

Posted: Sat Aug 10, 2002 5:11 pm
by BackupUser
Restored from previous forum. Originally posted by fweil.

You are sincerily welcome Art Sentinel.

I really love your posts, so it's a pleasure to have given something useful for you.

KRgrds

Francois Weil
14, rue Douer
F64100 Bayonne