Implemented dialog that asks if the user wants to save before quit (Mac OS X only for now)

Closes #40
This commit is contained in:
Daniel Collin 2012-12-30 21:10:26 +01:00
parent 49af1595f0
commit 4a4ca8e682
4 changed files with 92 additions and 9 deletions

View File

@ -51,6 +51,7 @@ static EditorData s_editorData;
static CopyData s_copyData; static CopyData s_copyData;
static bool reset_tracks = true; static bool reset_tracks = true;
static char s_filenames[5][2048]; static char s_filenames[5][2048];
static char* s_loadedFilename = 0;
static char* s_recentFiles[] = static char* s_recentFiles[] =
{ {
@ -86,6 +87,7 @@ void setMostRecentFile(const char* filename)
strcpy(s_recentFiles[i+1], s_recentFiles[i]); strcpy(s_recentFiles[i+1], s_recentFiles[i]);
strcpy(s_recentFiles[0], filename); strcpy(s_recentFiles[0], filename);
s_loadedFilename = s_recentFiles[0];
// check if the string was already present and remove it if that is the case by compacting the array // check if the string was already present and remove it if that is the case by compacting the array
@ -1226,7 +1228,6 @@ static void onFinishedLoad(const char* path)
Editor_update(); Editor_update();
setWindowTitle(path); setWindowTitle(path);
setMostRecentFile(path); setMostRecentFile(path);
//Commands_init(getTracks(), getTrackData());
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -1252,22 +1253,41 @@ static void onOpen()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void onSave() static bool onSaveDialog()
{ {
LoadSave_saveRocketXML(getMostRecentFile(), getTrackData()); char path[2048];
int ret;
if (!(ret = LoadSave_saveRocketXMLDialog(path, getTrackData())))
return false;
setMostRecentFile(path);
setWindowTitle(path);
return true;
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void onSaveDialog() static void onSave()
{ {
char path[2048]; if (!s_loadedFilename)
onSaveDialog();
else
LoadSave_saveRocketXML(getMostRecentFile(), getTrackData());
}
if (!LoadSave_saveRocketXMLDialog(path, getTrackData())) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
return;
setMostRecentFile(path); bool Editor_saveBeforeExit()
setWindowTitle(path); {
if (s_loadedFilename)
{
onSave();
return true;
}
return onSaveDialog();
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -14,6 +14,7 @@ void Editor_menuEvent(int menuItem);
void Editor_scroll(float deltaX, float deltaY, int flags); void Editor_scroll(float deltaX, float deltaY, int flags);
void Editor_updateTrackScroll(); void Editor_updateTrackScroll();
void Editor_loadRecentFile(int file); void Editor_loadRecentFile(int file);
bool Editor_saveBeforeExit();
char** Editor_getRecentFiles(); char** Editor_getRecentFiles();

View File

@ -98,6 +98,48 @@ static int getModifierFlags(int flags)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)windowShouldClose:(NSNotification *)aNotification
{
NSAlert *alert = [NSAlert alertWithMessageText:@"\nDo you really want exit?"
defaultButton:@"Yes" alternateButton:NO otherButton:@"No"
informativeTextWithFormat:@""];
int result = [alert runModal];
if(result == NSOKButton)
{
[NSApp terminate:self];
}
else
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
//return NSTerminateCancel;
}
}
- (void)windowWillClose:(NSNotification *)aNotification
{
NSAlert *alert = [NSAlert alertWithMessageText:@"\nDo you really want exit?"
defaultButton:@"Yes" alternateButton:NO otherButton:@"No"
informativeTextWithFormat:@""];
int result = [alert runModal];
if(result == NSOKButton)
{
[NSApp terminate:self];
}
else
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
//return NSTerminateCancel;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)keyDown:(NSEvent *)theEvent - (void)keyDown:(NSEvent *)theEvent
{ {
NSString* key = [theEvent charactersIgnoringModifiers]; NSString* key = [theEvent charactersIgnoringModifiers];

View File

@ -12,6 +12,26 @@ void Window_populateRecentList(char** files);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
int ret = NSRunAlertPanel(@"Save before exit?", @"Do you want save the work?", @"Yes", @"Cancel", @"No");
if (ret == NSAlertDefaultReturn)
{
if (!Editor_saveBeforeExit())
return NSTerminateCancel;
return NSTerminateNow;
}
if (ret == NSAlertAlternateReturn)
return NSTerminateCancel;
return NSTerminateNow;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{ {
char** recent_list = Editor_getRecentFiles(); char** recent_list = Editor_getRecentFiles();