Webview & ""

Just starting out? Need help? Post your questions and find answers here.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Webview & ""

Post 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!
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 487
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Webview & ""

Post by Mindphazer »

The compiler explains : "not cannot be used with strings"
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 487
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Webview & ""

Post 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
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Webview & ""

Post 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
Last edited by Piero on Fri Apr 19, 2024 11:07 am, edited 1 time in total.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Webview & ""

Post 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?
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 487
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Webview & ""

Post 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:
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Webview & ""

Post 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
...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Webview & ""

Post 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
Last edited by Piero on Fri Apr 19, 2024 1:59 pm, edited 1 time in total.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Simple Help PB/Forum

Post 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
Last edited by Piero on Fri Apr 19, 2024 4:47 pm, edited 1 time in total.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 487
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Webview & ""

Post 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
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Webview & ""

Post 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!
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Webview & ""

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 487
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Webview & ""

Post 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:
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Webview & ""

Post 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()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Webview & ""

Post 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
Post Reply