Quit menu item

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Quit menu item

Post by Mag » Fri Sep 25, 2015 2:51 pm

What's the better way to handle it in an application that has to close open documents before to quit?

Once the "Quit" menu item is invoked it start the message shutdownRequest and from there it seems impossible to close open documents because you get into a loop (for example, closing the last stack pops back up the message shutdownRequest again).

If someone has already created the standalones that manage multiple documents, some tell me how he did it?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Quit menu item

Post by Mark » Sun Oct 04, 2015 9:59 pm

Hi,

This should give you some idea.

Code: Select all

on menuPick theItem
  switch theItem
    case "Quit"
      beep
      answer "Do you really want to quit?" with "Yes" or "No"
      if it is "Yes" then
        send "quitMyProject" to me in 0 millisecs
      end if
     break
  end switch
end menuPick

on shutDownRequest
  send "quitMyProject" to me in 0 millisecs
end shutDownRequest

on quitMyProject
  repeat for each line myStack in myOpenStacks
   if the "cThisIsADocument" of stack myStack is true then
      beep
      // you might want to check whether changes were made by the user, I didn´t include this
      answer "Do you want to save this stack before closing?" with "Yes" or "No" or "Cancel"
      if it is "Cancel" then
        exit quitMyProject
      else
        // handle saving your document here and then close
        // saving is not included
        close stack myStack
      end if
    end if
  end repeat
  // we have now checked, saved and closed all stacks
  // the user never clicked "Cancel" so we can quit
  lock messages // do not invoke shutDownRequest (again)!!!
  // you may have to close the tts engine, close databases, hide the backdrop, etc
  // otherwise your app may either crash or stay in memory
  // now close all remaining stacks except for the main stack
  repeat for each line myStack in the openStacks
    if myStack is not "The Name of the Mainstack" then
      close stack myStack
    end if
  end repeat
  quit // poof! 
end quitMyProject
Use the closeStackRequest message to ask the user to save a document without closing all documents and quitting the app.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Quit menu item

Post by Mag » Fri Oct 14, 2016 2:14 pm

Thank you so much Mark.

Where do I need to put this part of your sample code?

Code: Select all

on menuPick theItem
  switch theItem
    case "Quit"
      beep
      answer "Do you really want to quit?" with "Yes" or "No"
      if it is "Yes" then
        send "quitMyProject" to me in 0 millisecs
      end if
     break
  end switch
end menuPick
I have a group "MainMenubar" in the main application window, with three buttons containing the menu handlers, which I install with the statement:

Code: Select all

set the menubar of this stack to "MainMenubar"
One of this buttons if for File, one for Edit and one for Help, but I don't have one for the Application menu and don't know if it possible to have one due to the particular nature of that menu. :oops:

Klaus
Posts: 13828
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Quit menu item

Post by Klaus » Fri Oct 14, 2016 2:39 pm

Hi Mag,
Mag wrote:One of this buttons if for File, one for Edit and one for Help, but I don't have one for the Application menu
and don't know if it possible to have one due to the particular nature of that menu. :oops:
so you got everything you need! :D

To solve this mistery, refer to "Special menu items" in the "User Guide" on page 230.
A MUST READ for Mac developers!


Best

Klaus

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Quit menu item

Post by Mag » Fri Oct 14, 2016 4:12 pm

Thank you Klaus. :mrgreen:

Post Reply

Return to “Mac OS”