Respond to Quit from Application Menu

Mac OSX specific forum
russellm72
User
User
Posts: 12
Joined: Wed Jul 13, 2022 2:04 pm

Respond to Quit from Application Menu

Post by russellm72 »

Hello!

This is making me feel a bit dense and frustrated.

I can't figure out how to respond and quit when Quit is chosen from the Application menu.

My app so far is super simple. I'm just trying to wrap my head around using the Forms Designer and how to created gadgets, events, and respond to them.

My form code is like this:

Code: Select all

;
; This code is automatically generated by the FormDesigner.
; Manual modification is possible to adjust existing commands, but anything else will be dropped when the code is compiled.
; Event procedures needs to be put in another source file.
;

Global Window_0

Global Button_0, String_0

Declare sayHello(EventType)

Procedure OpenWindow_0(x = 0, y = 0, width = 600, height = 400)
  Window_0 = OpenWindow(#PB_Any, x, y, width, height, "Hello, World.", #PB_Window_SystemMenu)
  Button_0 = ButtonGadget(#PB_Any, 250, 350, 100, 25, "Click Me")
  String_0 = StringGadget(#PB_Any, 10, 140, 570, 25, "")
EndProcedure

Procedure Window_0_Events(event)
  Select event
    Case #PB_Event_CloseWindow
      ProcedureReturn #False

    Case #PB_Event_Menu
      Select EventMenu()
      EndSelect

    Case #PB_Event_Gadget
      Select EventGadget()
        Case Button_0
          sayHello(EventType())          
      EndSelect
  EndSelect
  ProcedureReturn #True
EndProcedure
And my .pb code is like this:

Code: Select all

XIncludeFile "Hello World.pbf"

OpenWindow_0()

Procedure sayHello(x)
  SetGadgetText(String_0, "Hello, World.")
EndProcedure

Repeat
    event = WaitWindowEvent()
    Window_0_Events(event)
Until event = #PB_Event_CloseWindow
I tried making the last line of my Repeat loop look like this:

Code: Select all

Until event = #PB_Event_CloseWindow Or event = #PB_Menu_Quit
That compiles and runs but, doesn't work.

I even tried making my Repeat loop like this:

Code: Select all

Repeat
    If event = #PB_Menu_Quite
       Break
    Else
       event = WaitWindowEvent()
       Window_0_Events(event)
    EndIf
Until event = #PB_Event_CloseWindow
That also, compiled and ran but, did not work.

Then, I tried making a menu in the Forms Designer and adding a menu item to it and setting the constant of the menu item to be #PB_Menu_Quit and that did not compile but threw an error telling me that #PB_Menu_Quit is already defined.

I clearly don't understand what I'm reading in the documentation and what the best way to make my Hello World respond to selecting Quit from the Application menu is.

Help would be appreciated. Thanks.
russellm72
User
User
Posts: 12
Joined: Wed Jul 13, 2022 2:04 pm

Re: Respond to Quit from Application Menu

Post by russellm72 »

I figured it out.

I made my event loop like this:

Code: Select all

Repeat
  event = WaitWindowEvent()
  If event = #PB_Event_Menu
    If EventMenu() = #PB_Menu_Quit
      Break
    EndIf
    
  Else
    Window_0_Events(event)
  EndIf
  
      
Until event = #PB_Event_CloseWindow
That compiles, runs, and the app quits when choosing Quit from the Application menu as expected.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Respond to Quit from Application Menu

Post by mk-soft »

This is how it works on all three OS ...
I have saved it as a template

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuPreferences
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainEdit
  #MainButtonOk
  #MainButtonCancel
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainEdit, 5, 5, dx - 10, dy - 45)
  ResizeGadget(#MainButtonok, 10, dy - 35, 120, 30)
  ResizeGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
      MenuItem(#PB_Menu_Preferences, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
      MenuBar()
      MenuItem(#MainMenuPreferences, "Preferences ...")
      MenuBar()
      ;MenuItem(...)
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    EditorGadget(#MainEdit, 5, 5, dx -10, dy - 45)
    ButtonGadget(#MainButtonok, 10, dy - 35, 120, 30, "Ok")
    ButtonGadget(#MainButtonCancel, dx - 130, dy - 35, 120, 30, "Abbruch")
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Event Loop
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                PostEvent(#PB_Event_Menu, #Main, #MainMenuPreferences)
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuAbout
            MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
            
          Case #MainMenuPreferences
            ;
            
          Case #MainMenuExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainEdit
              Select EventType()
                Case #PB_EventType_Change
                  ;
                  
              EndSelect
              
            Case #MainButtonOk
              ;
            Case #MainButtonCancel
              ;
              
          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
Wolfram
Enthusiast
Enthusiast
Posts: 567
Joined: Thu May 30, 2013 4:39 pm

Re: Respond to Quit from Application Menu

Post by Wolfram »

Why do you use PostEvent(#PB_Event_CloseWindow, #Main, #Null) instead of Break?
macOS Catalina 10.15.7
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Respond to Quit from Application Menu

Post by mk-soft »

Wolfram wrote: Sun Jan 22, 2023 4:01 pm Why do you use PostEvent(#PB_Event_CloseWindow, #Main, #Null) instead of Break?
So that I only have to change code at #PB_Event_CloseWindow if necessary and not everywhere where a break occurs.
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
Post Reply