Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change the state of the window close button in Cocoa?

In Cocoa Applications, you often see a little red dot in the window's close button when you have unsaved data; TextEdit is a good example of this. I've pored through the Cocoa documentation but I can't find a way to programmatically set this state. I'm sure there's some really easy way to do it, but obviously I'm missing something.

like image 393
conmulligan Avatar asked Sep 10 '09 18:09

conmulligan


3 Answers

To set it programmatically, you can use the -setDocumentEdited: method of NSWindow. If you are writing a Document-based app, NSDocumentManager should automatically detect when there are unsaved changes to the NSUndoManager associated with the current NSDocument.

like image 121
titaniumdecoy Avatar answered Oct 19 '22 01:10

titaniumdecoy


Depends on what kind of application you're building. If it's NSDocument based, use NSDocument's updateChangeCount: method. If you've just got an NSWindowController, use setDocumentEdited:. NSWindow has a setDocumentEdited: method if all you've got is the NSWindow.

like image 42
Alex Avatar answered Oct 19 '22 00:10

Alex


Also, in 10.6, the 'setDocumentEdited' marks the application as dirty, and so can't be fast-killed. If you don't call this, and set the flag in the Info.plist (see What's New in 10.6)

<key>NSSupportsSuddenTermination</key>
<string>YES</string>

That way, if your app is running (but isn't dirty) then Mac OS X can simply kill it, rather than invoking polite shutdown requests. If your document(s) are marked dirty (or the window is) then it'll go through the normal app shutdown process to shut it down.

like image 9
AlBlue Avatar answered Oct 19 '22 01:10

AlBlue