Page 1 of 2

Webview & ""

Posted: Fri Apr 19, 2024 9:54 am
by Piero
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? :oops:

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?
Thanks!

Re: Webview & ""

Posted: Fri Apr 19, 2024 10:09 am
by Mindphazer
The compiler explains : "not cannot be used with strings"

Re: Webview & ""

Posted: Fri Apr 19, 2024 10:32 am
by Mindphazer
Piero 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? :oops:
Something like this

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
EndIf

Re: Webview & ""

Posted: Fri Apr 19, 2024 10:44 am
by Piero
Works!
Thanks!

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

Re: Webview & ""

Posted: Fri Apr 19, 2024 11:04 am
by Piero
Mindphazer wrote: Fri Apr 19, 2024 10:09 am The compiler explains : "not cannot be used with strings"
But don't you think it's strange being that you can use if... ELSE?

Re: Webview & ""

Posted: Fri Apr 19, 2024 11:25 am
by Mindphazer
Piero wrote: Fri Apr 19, 2024 11:04 am
Mindphazer wrote: Fri Apr 19, 2024 10:09 am The compiler explains : "not cannot be used with strings"
But don't you think it's strange being that you can use if... ELSE?
Indeed... But Fred is probably the one who can answer :mrgreen:

Re: Webview & ""

Posted: Fri Apr 19, 2024 12:47 pm
by mk-soft
Not testet ...

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

Re: Webview & ""

Posted: Fri Apr 19, 2024 1:56 pm
by Piero
mk-soft wrote: Fri Apr 19, 2024 12:47 pmNot testet ...
Gedanken Anyway!
What confused me is that it's normally very easy:

Code: Select all

Until event = #PB_Event_CloseWindow or EventMenu() = #PB_Menu_Quit

Simple Help PB/Forum

Posted: Fri Apr 19, 2024 1:58 pm
by Piero
Finally I can use ⌘C on Help! ;)

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

Re: Webview & ""

Posted: Fri Apr 19, 2024 2:12 pm
by Mindphazer
Piero 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
It works if you click on "Quit", but you wanted to catch the ⌘Q shortcut

Re: Webview & ""

Posted: Fri Apr 19, 2024 2:24 pm
by Piero
Yep I think I messed up trying to catch quit, return and others in a test...
Anyway it was a good thing; I learned some stuff,
Thanks to all!

Re: Webview & ""

Posted: Fri Apr 19, 2024 3:05 pm
by mk-soft
Mindphazer wrote: Fri Apr 19, 2024 2:12 pm
Piero 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
It works if you click on "Quit", but you wanted to catch the ⌘Q shortcut
That is wrong. EventMenu() may only be queried if there is also an event #PB_Event_Menu.

Re: Webview & ""

Posted: Fri Apr 19, 2024 3:52 pm
by Mindphazer
mk-soft wrote: Fri Apr 19, 2024 3:05 pm
Mindphazer wrote: Fri Apr 19, 2024 2:12 pm
Piero 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
It works if you click on "Quit", but you wanted to catch the ⌘Q shortcut
That is wrong. EventMenu() may only be queried if there is also an event #PB_Event_Menu.
True. I answered a bit too fast :mrgreen:

Re: Webview & ""

Posted: Fri Apr 19, 2024 4:21 pm
by mk-soft
As I write programmes for Windows, Linux and macOS, I simply forward the menu event #PB_Menu_Quit as event #PB_Event_CloseWindow.
So I only need to react to this event.

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

Re: Webview & ""

Posted: Fri Apr 19, 2024 4:35 pm
by Piero
Interesting
I simplified this way being this a Mac test:

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