Code: Select all
; inputDialog.pbi
; PureBasic 6.20 and possibly also before
; WINDOWS ONLY
EnableExplicit
#inpDialogEscMenu = 32767 ; ESC key menu event number
#inpDialogRetMenu = 32768 ; RETURN key menu event number
; Alternative inputDialog
; appTitle.s : dialog title
; message.s : dialog message
; width.i : dialog width (px), defaults to 400
; defaultText.s : the default to put into the text field
; cancelResult.s : what to return if dialog is cancelled (no NULL in PB), default to "-1". This allows "" as valid result.
Procedure.s inputDialog(appTitle.s, message.s, width.i = 400, defaultText.s = "", cancelResult.s = "-1")
Protected inputText.s = ""
Protected result.i = 0
Protected NWndParent.i, hParent.i, wflag.i
NWndParent.i = GetActiveWindow()
If NWndParent.i <> -1
hParent.i = WindowID(NWndParent.i)
DisableWindow(NWndParent.i, 1) ; <- disable parent window (because of our own event loop)
wflag.i = #PB_Window_Tool | #PB_Window_WindowCentered
Else
hParent = 0
wflag.i = #PB_Window_Tool | #PB_Window_ScreenCentered
EndIf
; helpers
Protected margin.i = 12 ; generic gadget margin
Protected gadH.i = 24 ; usual gadget height (text, buttons)
Protected buttonW.i = 70 ; button width
; calc the size of the Textgadget!
Protected rc.RECT
Protected hdc.i = GetDC_(0)
Protected hFont.i = GetGadgetFont(#PB_Default)
SelectObject_(hdc.i, hFont.i)
rc\right = DesktopScaledX(width.i - (margin * 2))
DrawText_(hdc.i, message.s, Len(message.s), @rc, #DT_CALCRECT|#DT_WORDBREAK)
ReleaseDC_(0, hdc.i)
Protected txtHeight.i = DesktopUnscaledY(rc\bottom - rc\top) ; <- TextGadget height!
; calc final window height
Protected WndH.i = margin + txtHeight.i + margin + gadH + margin + gadH + margin.i
Protected inputWin.i = OpenWindow(#PB_Any,
0, 0,
width.i, WndH.i,
" " + appTitle.s,
wflag.i, hParent.i)
If inputWin.i = 0
Debug "Failed to open dialog window!"
ProcedureReturn cancelResult.s
EndIf
Protected labelGadget.i = TextGadget(#PB_Any,
margin,
margin,
width.i - margin * 2,
txtHeight.i,
message.s)
Protected inputGadget.i = StringGadget(#PB_Any,
margin,
margin + txtHeight.i + margin,
width.i - margin * 2,
gadH.i,
defaultText.s)
Protected okButton.i = ButtonGadget(#PB_Any,
width.i - margin.i - buttonW.i - margin.i - buttonW.i,
margin.i + txtHeight.i + margin.i + gadH.i + margin.i,
buttonW.i,
gadH.i,
"OK")
Protected cancelButton.i = ButtonGadget(#PB_Any,
width.i - margin.i - buttonW.i,
margin.i + txtHeight.i + margin.i + gadH.i + margin.i,
buttonW.i,
gadH.i,
"Cancel")
SetActiveGadget(inputGadget.i)
AddKeyboardShortcut(inputWin, #PB_Shortcut_Escape, #inpDialogEscMenu)
AddKeyboardShortcut(inputWin, #PB_Shortcut_Return, #inpDialogRetMenu)
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadget()
Case okButton
inputText.s = GetGadgetText(inputGadget.i)
result.i = 1
Break
Case cancelButton
inputText.s = ""
result.i = 0
Break
EndSelect
Case #PB_Event_CloseWindow
inputText.s = ""
result.i = 0
Break
Case #PB_Event_Menu
Select EventMenu()
Case #inpDialogEscMenu
inputText.s = ""
result.i = 0
Break
Case #inpDialogRetMenu
inputText.s = GetGadgetText(inputGadget.i)
result.i = 1
Break
EndSelect
EndSelect
ForEver
RemoveKeyboardShortcut(inputWin.i, #PB_Shortcut_Escape)
RemoveKeyboardShortcut(inputWin.i, #PB_Shortcut_Return)
CloseWindow(inputWin.i)
If NWndParent.i <> -1
DisableWindow(NWndParent.i, 0) ; enable parent window again, now we use the main event loop
EndIf
If result.i = 1
ProcedureReturn inputText.s
EndIf
ProcedureReturn cancelResult.s
EndProcedure
CompilerIf #PB_Compiler_IsMainFile = #True
; test if started from include file
Define proxy.s = inputDialog("Testing inputDialog 1",
"Enter proxy server in the following format:"+#CR$+#CR$+
"proxyServer:Port optionalUsername optionalPassword"+#CR$+#CR$+
"Supported protocols:"+#CR$+
"- http:// - HTTP proxy (Default)"+#CR$+
"- socks4:// - SOCKS4 proxy"+#CR$+
"- socks4a:// - SOCKS4 proxy with domain name support rather than IP address"+#CR$+
"- socks5:// - SOCKS5 proxy"+#CR$+
"- socks5h:// - SOCKS5 proxy and ask the proxy to do the hostname resolving",
500)
Debug "inputDialog result: [" + proxy.s + "]"
proxy.s = inputDialog("Testing inputDialog 2",
"Enter proxy server:")
Debug "inputDialog result: [" + proxy.s + "]"
CompilerEndIf