From cb47ecdb1dec50bd45e305a8cead2d9947105848 Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Sat, 29 Dec 2012 16:35:11 +0100 Subject: [PATCH] Implemented "Recent Files" on Mac OS X Also added shortcut for each recent file (CMD + 1-4 for each entry in the list) Closes #18 --- ogl_editor/data/macosx/appnib.xib | 142 ++++++++++++++++++++++++------------- ogl_editor/data/macosx/info.plist | 2 +- ogl_editor/src/Editor.c | 89 ++++++++++++++++++++--- ogl_editor/src/Editor.h | 3 + ogl_editor/src/macosx/RocketView.m | 38 ++++++++++ ogl_editor/src/macosx/delegate.m | 32 ++++++++- 6 files changed, 242 insertions(+), 64 deletions(-) diff --git a/ogl_editor/data/macosx/appnib.xib b/ogl_editor/data/macosx/appnib.xib index a9cfc87..1c04376 100644 --- a/ogl_editor/data/macosx/appnib.xib +++ b/ogl_editor/data/macosx/appnib.xib @@ -125,7 +125,30 @@ 1 - + + + Recent Files + + 2147483647 + + + submenuAction: + + Recent Files + + YES + + + Item + + 2147483647 + + + + + + + Open Recent @@ -134,12 +157,12 @@ submenuAction: - + Open Recent YES - - + + Clear Menu 1048576 @@ -149,6 +172,8 @@ _NSRecentDocumentsMenu + YES + YES @@ -310,6 +335,7 @@ {800, 600} + _NS:9 RocketView @@ -369,22 +395,6 @@ - arrangeInFront: - - - - 39 - - - - clearRecentDocuments: - - - - 127 - - - performZoom: @@ -525,10 +535,11 @@ - + + @@ -538,15 +549,6 @@ - 124 - - - YES - - - - - 79 @@ -557,20 +559,6 @@ - 125 - - - YES - - - - - - 126 - - - - 57 @@ -704,6 +692,52 @@ + + 548 + + + YES + + + + + + 549 + + + YES + + + + + + 550 + + + + + 562 + + + YES + + + + + + 563 + + + YES + + + + + + 564 + + + @@ -713,9 +747,6 @@ -1.IBPluginDependency -2.IBPluginDependency -3.IBPluginDependency - 124.IBPluginDependency - 125.IBPluginDependency - 126.IBPluginDependency 136.IBPluginDependency 149.IBPluginDependency 19.IBPluginDependency @@ -734,7 +765,13 @@ 494.IBPluginDependency 5.IBPluginDependency 537.IBPluginDependency + 548.IBPluginDependency + 549.IBPluginDependency + 550.IBPluginDependency 56.IBPluginDependency + 562.IBPluginDependency + 563.IBPluginDependency + 564.IBPluginDependency 57.IBPluginDependency 58.IBPluginDependency 72.IBPluginDependency @@ -761,9 +798,6 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin {{928, 459}, {480, 360}} com.apple.InterfaceBuilder.CocoaPlugin @@ -787,6 +821,12 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin @@ -801,7 +841,7 @@ - 547 + 567 0 diff --git a/ogl_editor/data/macosx/info.plist b/ogl_editor/data/macosx/info.plist index ff1d020..3641a92 100644 --- a/ogl_editor/data/macosx/info.plist +++ b/ogl_editor/data/macosx/info.plist @@ -9,7 +9,7 @@ CFBundleIconFile icon CFBundleIdentifier - com.yourcompany.someid + com.tbl.rocketeditor CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/ogl_editor/src/Editor.c b/ogl_editor/src/Editor.c index 3ef398e..8f718d8 100644 --- a/ogl_editor/src/Editor.c +++ b/ogl_editor/src/Editor.c @@ -17,6 +17,7 @@ #include "../../sync/data.h" extern void Window_setTitle(const char* title); +extern void Window_populateRecentList(const char** files); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -45,10 +46,61 @@ typedef struct EditorData int copyCount; } EditorData; -static char s_currentFile[2048]; static EditorData s_editorData; static CopyData s_copyData; static bool reset_tracks = true; +static char s_filenames[5][2048]; + +static char* s_recentFiles[] = +{ + s_filenames[0], + s_filenames[1], + s_filenames[2], + s_filenames[3], + s_filenames[4], +}; + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +char** Editor_getRecentFiles() +{ + return (char**)s_recentFiles; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +const char* getMostRecentFile() +{ + return s_recentFiles[0]; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void setMostRecentFile(const char* filename) +{ + int i; + + // move down all files + for (i = 3; i >= 0; --i) + strcpy(s_recentFiles[i+1], s_recentFiles[i]); + + strcpy(s_recentFiles[0], filename); + + // check if the string was already present and remove it if that is the case by compacting the array + + for (i = 1; i < 5; ++i) + { + if (!strcmp(s_recentFiles[i], filename)) + { + for (; i < 4; ++i) + strcpy(s_recentFiles[i], s_recentFiles[i + 1]); + + break; + } + } + + Window_populateRecentList((const char**)s_recentFiles); +} /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -565,6 +617,7 @@ bool Editor_keyDown(int key, int keyCode, int modifiers) row = t->keys[idx + 1].row; viewInfo->rowPos = row; + viewInfo->selectStartRow = viewInfo->selectStopRow = row; } break; @@ -826,14 +879,9 @@ bool Editor_keyDown(int key, int keyCode, int modifiers) case 14 : bias_value = 1.0f; break; case 15 : bias_value = 10.f; break; case 17 : bias_value = 100.0f; break; - case 16 : bias_value = 1000.0f; break; } biasSelection(bias_value, selectLeft, selectRight, selectTop, selectBottom); - - Editor_update(); - - return true; } // do edit here and biasing here @@ -1089,12 +1137,30 @@ static void setWindowTitle(const char* path) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -static void onOpen() +void Editor_loadRecentFile(int id) { - if (LoadSave_loadRocketXMLDialog(s_currentFile, getTrackData())) + char path[2048]; + strcpy(path, s_recentFiles[id]); // must be unique buffer when doing set mostRecent + + if (LoadSave_loadRocketXML(path, getTrackData())) { Editor_update(); - setWindowTitle(s_currentFile); + setWindowTitle(path); + setMostRecentFile(path); + } +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +static void onOpen() +{ + char currentFile[2048]; + + if (LoadSave_loadRocketXMLDialog(currentFile, getTrackData())) + { + Editor_update(); + setWindowTitle(currentFile); + setMostRecentFile(currentFile); } } @@ -1102,7 +1168,7 @@ static void onOpen() static void onSave() { - LoadSave_saveRocketXML(s_currentFile, getTrackData()); + LoadSave_saveRocketXML(getMostRecentFile(), getTrackData()); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1110,9 +1176,11 @@ static void onSave() static void onSaveDialog() { char path[2048]; + if (!LoadSave_saveRocketXMLDialog(path, getTrackData())) return; + setMostRecentFile(path); setWindowTitle(path); } @@ -1120,7 +1188,6 @@ static void onSaveDialog() void Editor_menuEvent(int menuItem) { - printf("%d\n", menuItem); switch (menuItem) { case EDITOR_MENU_OPEN : onOpen(); break; diff --git a/ogl_editor/src/Editor.h b/ogl_editor/src/Editor.h index e7e4ea1..6a2e06f 100644 --- a/ogl_editor/src/Editor.h +++ b/ogl_editor/src/Editor.h @@ -13,6 +13,9 @@ void Editor_setWindowSize(int x, int y); void Editor_menuEvent(int menuItem); void Editor_scroll(float deltaX, float deltaY, int flags); void Editor_updateTrackScroll(); +void Editor_loadRecentFile(int file); + +char** Editor_getRecentFiles(); enum { diff --git a/ogl_editor/src/macosx/RocketView.m b/ogl_editor/src/macosx/RocketView.m index ae7eb26..28416e5 100644 --- a/ogl_editor/src/macosx/RocketView.m +++ b/ogl_editor/src/macosx/RocketView.m @@ -213,6 +213,43 @@ static int getModifierFlags(int flags) return YES; } +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +- (void)onRecentFile:(id)sender +{ + NSString* string = [sender representedObject]; + Editor_loadRecentFile([string intValue]); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void Window_populateRecentList(const char** files) +{ + NSMenu* fileMenu = [[[NSApp mainMenu] itemWithTitle:@"File"] submenu]; + NSMenu* recentItems = [[fileMenu itemWithTitle:@"Recent Files"] submenu]; + + [recentItems removeAllItems]; + + for (int i = 0; i < 4; ++i) + { + const char* filename = files[i]; + + if (!strcmp(filename, "")) + continue; + + NSString* name = [NSString stringWithUTF8String: filename]; + + NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:name action:@selector(onRecentFile:) keyEquivalent:@""]; + [newItem setRepresentedObject:[NSString stringWithFormat:@"%d",i]]; + [newItem setKeyEquivalentModifierMask: NSCommandKeyMask]; + [newItem setKeyEquivalent:[NSString stringWithFormat:@"%d",i + 1]]; + + [recentItems addItem:newItem]; + + [newItem release]; + } +} + @end ////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -229,3 +266,4 @@ void Window_setTitle(const char* title) [g_window setTitle:[NSString stringWithUTF8String:title]]; } + diff --git a/ogl_editor/src/macosx/delegate.m b/ogl_editor/src/macosx/delegate.m index 667c4a7..dc298e2 100644 --- a/ogl_editor/src/macosx/delegate.m +++ b/ogl_editor/src/macosx/delegate.m @@ -3,6 +3,8 @@ #include "../RemoteConnection.h" #include "rlog.h" +void Window_populateRecentList(char** files); + @implementation MinimalAppAppDelegate @synthesize window; @@ -12,6 +14,24 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { + char** recent_list = Editor_getRecentFiles(); + + NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults]; + + if (prefs) + { + NSArray* stringArray = [prefs objectForKey:@"recentFiles"]; + + for (int i = 0; i < 4; ++i) + { + NSString* name = [stringArray objectAtIndex:i]; + const char* filename = [name cStringUsingEncoding:NSASCIIStringEncoding]; + if (filename) + strcpy(recent_list[i], filename); + } + } + + Window_populateRecentList(recent_list); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -25,7 +45,17 @@ - (void)applicationWillTerminate:(NSNotification *)aNotification { - rlog(R_INFO, "Dealloc\n"); + int i; + NSMutableArray* stringArray; + char** recent_list = Editor_getRecentFiles(); + stringArray = [[NSMutableArray alloc] init]; + + for (i = 0; i < 4; ++i) + [stringArray addObject:[NSString stringWithUTF8String: recent_list[i]]]; + + [[NSUserDefaults standardUserDefaults] setObject:stringArray forKey:@"recentFiles"]; + [[NSUserDefaults standardUserDefaults] synchronize]; + Editor_destroy(); RemoteConnection_close(); }