AnotherInputRequester

Share your advanced PureBasic knowledge/code with the community.
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

AnotherInputRequester

Post by MrMat »

Code updated For 5.20+

There's already a million of these, and now there's another one :) This is for Windows with PB 4 Beta 6:

Code: Select all

Procedure.s AnotherInputRequester(title.s, text.s, defaulttext.s, parentwin = -1, canceltext.s = "", ok.s = "OK", cancel.s = "Cancel")
  If parentwin = -1
    inputno = OpenWindow(#PB_Any, 0, 0, 400, 100, title, #PB_Window_Invisible | #PB_Window_ScreenCentered)
  Else
    inputno = OpenWindow(#PB_Any, 0, 0, 400, 100, title, #PB_Window_Invisible | #PB_Window_WindowCentered, WindowID(parentwin))
    DisableWindow(parentwin, 1)
  EndIf
  
  
  text.s = ReplaceString(text, #CRLF$, Chr(10))
  textno = TextGadget(#PB_Any, 8, 8, 200, 20, text)
  
  lines = CountString(text, Chr(10)) + 1
  
  hdc = GetDC_(GadgetID(textno))
  font = SendMessage_(GadgetID(textno), #WM_GETFONT, 0, 0)
  oldfont = SelectObject_(hdc, font)
  GetTextMetrics_(hDC, @lptm.TEXTMETRIC)
  textwidth = 0
  For loopy = 1 To lines
    textline.s = StringField(text, loopy, Chr(10))
    GetTextExtentPoint32_(hdc, @textline, Len(textline), size.SIZE)
    If size\cx > textwidth
      textwidth = size\cx
    EndIf
  Next
  SelectObject_(hdc, oldfont)
  ReleaseDC_(GadgetID(textno), hdc)
  
  If textwidth < 200
    textwidth = 200
  EndIf
  windowwidth = textwidth + 16
  textheight = lines * lptm\tmHeight
  
  stringno = StringGadget(#PB_Any, 8, 16 + textheight, textwidth, 20, defaulttext)
  buttonwidth = 60
  
  If cancel = ""
    okno = ButtonGadget(#PB_Any, (windowwidth - buttonwidth) / 2, 44 + textheight, 60, 24, ok)
    cancelno = -1
  Else
    okno = ButtonGadget(#PB_Any, (windowwidth - buttonwidth * 2) / 3, 44 + textheight, 60, 24, ok)
    cancelno = ButtonGadget(#PB_Any, 2 * (windowwidth - buttonwidth * 2) / 3 + buttonwidth, 44 + textheight, 60, 24, cancel)
  EndIf
  
  ResizeGadget(textno, #PB_Ignore, #PB_Ignore, textwidth, textheight)
  windowheight = 76 + textheight
  
  wx = WindowX(inputno)
  wy = WindowY(inputno)
  ResizeWindow(inputno, wx - (windowwidth - 400) / 2, wy - (windowheight - 100) / 2, windowwidth, windowheight)
  SetActiveGadget(stringno)
  SendMessage_(GadgetID(stringno), #EM_SETSEL, Len(defaulttext), Len(defaulttext))
  
  StickyWindow(inputno, 1)
  HideWindow(inputno, 0)
  
  quit = 0
  Repeat
    event = WaitWindowEvent()
    eventgad = EventGadget()
    If event = #PB_Event_Gadget
      If eventgad = okno
        quit = 1
      ElseIf eventgad = cancelno
        quit = 2
      EndIf
    ElseIf event = #PB_Event_CloseWindow
      quit = 2
    ElseIf event = #WM_KEYDOWN
      If EventwParam() = #VK_RETURN
        quit = 1
      ElseIf EventwParam() = #VK_ESCAPE
        quit = 2
      EndIf
    EndIf
  Until quit <> 0
  
  If quit = 1
    actualtext.s = GetGadgetText(stringno)
  ElseIf quit = 2
    actualtext = canceltext
  EndIf
  
  CloseWindow(inputno)
  
  If parentwin <> -1
    DisableWindow(parentwin, 0)
  EndIf
  ProcedureReturn actualtext
EndProcedure

OpenWindow(0, 0, 0, 300, 200, "Background window", #PB_Window_ScreenCentered)
result.s = AnotherInputRequester("AnotherInputRequester", "Hello there." + #CRLF$ + "How are you today?", "Pretty good thanks", 0, "Not going to tell you")
MessageRequester("AnotherInputRequester result", result)
I needed an input requester that would display multi-line text with two buttons so made this one. There's not much else to it, the example above shows most features. #CRLF$ in the text will produce a newline and the gadgets below should be positioned correctly so that it looks nice. Pressing return selects OK, escape selects Cancel. If parentwin is -1 then there is no parent window and it won't be modal. The button text can be specified if two more parameters are given. Passing "" for the cancel text will reduce it to one button.
Mat