It "should be already there as #PB_Menu_Quit"… am I missing something?
Also, mostly out of curiosity:
Code: Select all
s$=""
if s$
debug "not empty"
else
debug "empty"
endif
if not s$ : debug "empty" : endif ; ??? doesn't compile?Code: Select all
s$=""
if s$
debug "not empty"
else
debug "empty"
endif
if not s$ : debug "empty" : endif ; ??? doesn't compile?

Something like thisPiero wrote: Fri Apr 19, 2024 9:54 am How do I "catch" a ⌘Q (quit) shortcut in the WebBrowser.pb example?
It "should be already there as #PB_Menu_Quit"… am I missing something?![]()
Code: Select all
Enumeration
#MainWindow
#Kbd_CmdQ
EndEnumeration
If OpenWindow(#MainWindow, 0, 0, 300, 200, "Test", #PB_Window_ScreenCentered)
AddKeyboardShortcut(#MainWindow, #PB_Shortcut_Command| #PB_Shortcut_Q, #Kbd_CmdQ)
If CreateMenu(#PB_Any, WindowID(#MainWindow))
MenuItem(#PB_Menu_About, "")
MenuItem(#PB_Menu_Preferences, "")
MenuItem(#PB_Menu_Quit, "")
EndIf
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
CloseWindow(#MainWindow)
End
Case #PB_Event_Menu
Select EventMenu()
Case #Kbd_CmdQ, #PB_Menu_Quit
CloseWindow(#MainWindow)
End
EndSelect
EndSelect
ForEver
EndIfCode: Select all
#helppath$="/Applications/PureBasic.app/Contents/Resources/help/purebasic/"
#ind=#helppath$+"reference/reference.html"
Procedure.s sh(search$)
Protected Output$, shell
shell = RunProgram("/bin/sh","","",
#PB_Program_Open|#PB_Program_Write|#PB_Program_Read)
If shell
WriteProgramStringN(shell,search$)
WriteProgramData(shell,#PB_Program_Eof,0)
While ProgramRunning(shell)
If AvailableProgramOutput(shell)
Output$ + ReadProgramString(shell)
EndIf
Wend
CloseProgram(shell)
EndIf
ProcedureReturn Output$
EndProcedure
wl$=ProgramParameter()
if wl$
s.s=#helppath$+sh("grep -i '"+wl$+"' "+#helppath$+~"index.xml | awk -F'\"' '{ print $(NF-1) }' | head -n 1")
EndIf
if s = #helppath$ or wl$ = ""
wl$=#ind
else
wl$ = URLEncoder(s)
EndIf
Enumeration
#Kbd_CmdQ
#Kbd_ret
EndEnumeration
Procedure ResizeWebWindow()
ResizeGadget(10, #PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0)-52)
ResizeGadget(4, #PB_Ignore, #PB_Ignore, WindowWidth(0)-250, #PB_Ignore)
ResizeGadget(5, WindowWidth(0)-60, #PB_Ignore, #PB_Ignore, #PB_Ignore)
; ResizeGadget(6, #PB_Ignore, #PB_Ignore, WindowWidth(0), #PB_Ignore)
EndProcedure
If OpenWindow(0, 100, 200, 800, 800, "PureBasic MiniBrowser v1.0", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)
CreateStatusBar(0, WindowID(0))
AddStatusBarField(#PB_Ignore)
StatusBarText(0, 0, "Welcome to the world's smallest Browser !", 0)
ButtonGadget(1, 0, 3, 60, 25, "Back")
ButtonGadget(2, 60, 3, 60, 25, "Next")
ButtonGadget(3, 120, 3, 60, 25, "Stop")
StringGadget(4, 185, 5, 0, 20, wl$)
ButtonGadget(5, 0, 3, 60, 25, "Go")
;FrameGadget(6, 0, 30, 0, 2, "", 2) ; Nice little separator
If WebGadget(10, 0, 31, 0, 0, wl$) = 0
MessageRequester("Error", "Webkit library not found", 0)
End ; Quit
EndIf
AddKeyboardShortcut(0, #PB_Shortcut_Return, #Kbd_ret)
AddKeyboardShortcut(0, #PB_Shortcut_Command| #PB_Shortcut_Q, #Kbd_CmdQ)
; Use bindevent() to have a realtime window resize
BindEvent(#PB_Event_SizeWindow, @ResizeWebWindow())
ResizeWebWindow() ; Adjust the gadget to the current window size
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
SetGadgetState(10, #PB_Web_Back)
Case 2
SetGadgetState(10, #PB_Web_Forward)
Case 3
SetGadgetState(10, #PB_Web_Stop)
Case 5
SetGadgetText(10, GetGadgetText(4))
EndSelect
Case #PB_Event_Menu
Select EventMenu()
Case #Kbd_CmdQ, #PB_Menu_Quit
CloseWindow(0)
End
case #Kbd_ret
SetGadgetText(10, #ind)
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
EndIfBut don't you think it's strange being that you can use if... ELSE?Mindphazer wrote: Fri Apr 19, 2024 10:09 am The compiler explains : "not cannot be used with strings"

Indeed... But Fred is probably the one who can answerPiero wrote: Fri Apr 19, 2024 11:04 amBut don't you think it's strange being that you can use if... ELSE?Mindphazer wrote: Fri Apr 19, 2024 10:09 am The compiler explains : "not cannot be used with strings"
Code: Select all
...
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Procedure MacOS_Quit()
PostEvent(#PB_Event_CloseWindow, 0, 0)
EndProcedure
CompilerEndIf
If OpenWindow(0, 100, 200, 500, 300, "PureBasic MiniBrowser v1.0", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)
CreateMenu(0, WindowID(0))
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
BindMenuEvent(0, #PB_Menu_Quit, @MacOS_Quit())
CompilerEndIf
...
Gedanken Anyway!
Code: Select all
Until event = #PB_Event_CloseWindow or EventMenu() = #PB_Menu_QuitCode: Select all
; IDE tool with argument: %WORD
Procedure simpleShell(ShellCommand$)
Protected shell
shell = RunProgram("/bin/sh","","", #PB_Program_Open|#PB_Program_Write)
If shell
WriteProgramStringN(shell,ShellCommand$)
WriteProgramData(shell,#PB_Program_Eof,0)
CloseProgram(shell)
EndIf
EndProcedure
simpleShell("/Applications/webview.app/Contents/MacOS/webview "+ ProgramParameter() +" > /dev/null 2>&1 &")Code: Select all
; /Applications/webview.app
#helppath$="/Applications/PureBasic.app/Contents/Resources/help/purebasic/"
#ind=#helppath$+"reference/reference.html"
#forum="https://www.purebasic.fr/english/search.php?keywords="
fi$="https://www.purebasic.fr/english/index.php"
Procedure.s sh(search$)
Protected Output$, shell
shell = RunProgram("/bin/sh","","",
#PB_Program_Open|#PB_Program_Write|#PB_Program_Read)
If shell
WriteProgramStringN(shell,search$)
WriteProgramData(shell,#PB_Program_Eof,0)
While ProgramRunning(shell)
If AvailableProgramOutput(shell)
Output$ + ReadProgramString(shell)
EndIf
Wend
CloseProgram(shell)
EndIf
ProcedureReturn Output$
EndProcedure
wl2$= ProgramParameter()
wl$=wl2$
if wl$
s.s=#helppath$+sh("grep -i '"+wl$+"' "+#helppath$+~"index.xml | awk -F'\"' '{ print $(NF-1) }' | head -n 1")
EndIf
if s = #helppath$ or wl$ = ""
wl$=#ind
else
wl$= URLEncoder(s)
fi$= URLEncoder(#forum+wl2$)
EndIf
Procedure ResizeWebWindow()
ResizeGadget(10, #PB_Ignore, #PB_Ignore, WindowWidth(0), WindowHeight(0)-52)
ResizeGadget(4, #PB_Ignore, #PB_Ignore, WindowWidth(0)-250, #PB_Ignore)
ResizeGadget(5, WindowWidth(0)-60, #PB_Ignore, #PB_Ignore, #PB_Ignore)
EndProcedure
If OpenWindow(0, 100, 200, 1024, 868, "PureBasic MiniBrowser v1.0", #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget)
CreateStatusBar(0, WindowID(0))
AddStatusBarField(#PB_Ignore)
StatusBarText(0, 0, "Welcome to the world's smallest Browser !", 0)
ButtonGadget(1, 0, 3, 60, 25, "Back")
ButtonGadget(2, 60, 3, 60, 25, "Next")
ButtonGadget(3, 120, 3, 60, 25, "Stop")
StringGadget(4, 185, 5, 0, 20, wl$)
ButtonGadget(5, 0, 3, 60, 25, "Forum")
If WebGadget(10, 0, 31, 0, 0, wl$) = 0
MessageRequester("Error", "Webkit library not found", 0)
End ; Quit
EndIf
; Use bindevent() to have a realtime window resize
BindEvent(#PB_Event_SizeWindow, @ResizeWebWindow())
ResizeWebWindow() ; Adjust the gadget to the current window size
; Enumeration
; #Kbd_ret
; EndEnumeration
; AddKeyboardShortcut(0, #PB_Shortcut_Return, #Kbd_ret)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
SetGadgetState(10, #PB_Web_Back)
Case 2
SetGadgetState(10, #PB_Web_Forward)
Case 3
SetGadgetState(10, #PB_Web_Stop)
Case 5
SetGadgetText(10, fi$)
EndSelect
SetGadgetText(4, GetGadgetText(10))
Case #PB_Event_Menu
Select EventMenu()
Case #PB_Menu_Quit
End
;Case #Kbd_ret
;debug "#Kbd_ret"
EndSelect
Case #PB_Event_CloseWindow
End
EndSelect
ForEver
EndIf
It works if you click on "Quit", but you wanted to catch the ⌘Q shortcutPiero wrote: Fri Apr 19, 2024 1:56 pm What confused me is that it's normally very easy:Code: Select all
Until event = #PB_Event_CloseWindow or EventMenu() = #PB_Menu_Quit
That is wrong. EventMenu() may only be queried if there is also an event #PB_Event_Menu.Mindphazer wrote: Fri Apr 19, 2024 2:12 pmIt works if you click on "Quit", but you wanted to catch the ⌘Q shortcutPiero wrote: Fri Apr 19, 2024 1:56 pm What confused me is that it's normally very easy:Code: Select all
Until event = #PB_Event_CloseWindow or EventMenu() = #PB_Menu_Quit

True. I answered a bit too fastmk-soft wrote: Fri Apr 19, 2024 3:05 pmThat is wrong. EventMenu() may only be queried if there is also an event #PB_Event_Menu.Mindphazer wrote: Fri Apr 19, 2024 2:12 pmIt works if you click on "Quit", but you wanted to catch the ⌘Q shortcutPiero wrote: Fri Apr 19, 2024 1:56 pm What confused me is that it's normally very easy:Code: Select all
Until event = #PB_Event_CloseWindow or EventMenu() = #PB_Menu_Quit
Code: Select all
;-TOP
Procedure UpdateWindow()
Protected dx, dy
dx = WindowWidth(0)
dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
; Resize Gadgets
EndProcedure
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Procedure DoEventMacOS()
Select EventMenu()
Case #PB_Menu_Quit
PostEvent(#PB_Event_CloseWindow, 0, 0)
Case #PB_Menu_About
;PostEvent(#PB_Event_Menu, 0, #MyMenuItem_About)
MessageRequester("About", "My Program v1.01", #PB_MessageRequester_Info)
Case #PB_Menu_Preferences
;PostEvent(#PB_Event_Menu, 0, #MyMenuItem_Preferences)
;
EndSelect
EndProcedure
CompilerEndIf
Procedure Main()
Protected dx, dy
#WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
; MenuBar
CreateMenu(0, WindowID(0))
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
MenuItem(#PB_Menu_About, "")
MenuItem(#PB_Menu_Preferences, "")
MenuItem(#PB_Menu_Quit, "")
BindMenuEvent(0, #PB_Menu_About, @DoEventMacOS())
BindMenuEvent(0, #PB_Menu_Preferences, @DoEventMacOS())
BindMenuEvent(0, #PB_Menu_Quit, @DoEventMacOS())
CompilerEndIf
;MenuTitle("File")
; StatusBar
CreateStatusBar(0, WindowID(0))
AddStatusBarField(#PB_Ignore)
; Gadgets
dx = WindowWidth(0)
dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
; Bind Events
BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
; Main Loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Select EventWindow()
Case 0
Break
EndSelect
Case #PB_Event_Menu
Select EventMenu()
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
EndSelect
EndSelect
ForEver
EndIf
EndProcedure : Main()
Code: Select all
;If OpenWindow...
Enumeration
#Kbd_ret
EndEnumeration
AddKeyboardShortcut(0, #PB_Shortcut_Return, #Kbd_ret)
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case 1
SetGadgetState(10, #PB_Web_Back)
Case 2
SetGadgetState(10, #PB_Web_Forward)
Case 3
SetGadgetState(10, #PB_Web_Stop)
Case 5
SetGadgetText(10, fi$)
EndSelect
SetGadgetText(4, GetGadgetText(10))
Case #PB_Event_Menu
Select EventMenu()
Case #PB_Menu_Quit
End
Case #Kbd_ret
debug "#Kbd_ret"
EndSelect
Case #PB_Event_CloseWindow
End
EndSelect
ForEver
EndIf