From 381abf24ff115c8e25610a8e538f295d75e9ff49 Mon Sep 17 00:00:00 2001 From: mathieu _alkama_ m Date: Sun, 13 Dec 2015 22:28:46 +0100 Subject: [PATCH] Fix: Just a touch of modernisation - Fixing OSX Alerts to not use deprecated API - Fixing missing release for refcounted objects - Use a more conventional filename for the app delegate - Fixing some variable types to avoid conversions --- ogl_editor/src/Commands.c | 3 +- ogl_editor/src/Editor.c | 4 +- ogl_editor/src/RemoteConnection.c | 2 +- ogl_editor/src/loadsave.c | 4 +- ogl_editor/src/macosx/FileDialog.m | 15 +++-- ogl_editor/src/macosx/RocketAppDelegate.h | 13 ++++ ogl_editor/src/macosx/RocketAppDelegate.m | 103 ++++++++++++++++++++++++++++++ ogl_editor/src/macosx/RocketView.m | 26 ++++---- ogl_editor/src/macosx/delegate.h | 13 ---- ogl_editor/src/macosx/delegate.m | 92 -------------------------- ogl_editor/src/macosx/main.m | 5 +- 11 files changed, 149 insertions(+), 131 deletions(-) create mode 100644 ogl_editor/src/macosx/RocketAppDelegate.h create mode 100644 ogl_editor/src/macosx/RocketAppDelegate.m delete mode 100644 ogl_editor/src/macosx/delegate.h delete mode 100644 ogl_editor/src/macosx/delegate.m diff --git a/ogl_editor/src/Commands.c b/ogl_editor/src/Commands.c index 6ccb438..5c43b9c 100644 --- a/ogl_editor/src/Commands.c +++ b/ogl_editor/src/Commands.c @@ -582,7 +582,8 @@ static void toggleMute(void* userData) else { struct track_key defKey; - int i, keysSize = sizeof(struct track_key) * data->syncTrack->num_keys; + int i; + size_t keysSize = sizeof(struct track_key) * data->syncTrack->num_keys; float currentValue = (float)sync_get_val(data->syncTrack, data->row); data->track->disabled = true; diff --git a/ogl_editor/src/Editor.c b/ogl_editor/src/Editor.c index e965610..d9dd285 100644 --- a/ogl_editor/src/Editor.c +++ b/ogl_editor/src/Editor.c @@ -182,7 +182,7 @@ static inline int getActiveTrack() static inline int getTrackCount() { - return s_editorData.trackData.num_syncTracks; + return (int)s_editorData.trackData.num_syncTracks; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1631,7 +1631,7 @@ static void onPrevNextKey(bool prevKey, enum Selection selection) if (idx < 0) row = track->keys[0].row; - else if (idx > (int)track->num_keys - 2) + else if (idx > track->num_keys - 2) row = track->keys[track->num_keys - 1].row; else row = track->keys[idx + 1].row; diff --git a/ogl_editor/src/RemoteConnection.c b/ogl_editor/src/RemoteConnection.c index cd9c2d4..f2226b5 100644 --- a/ogl_editor/src/RemoteConnection.c +++ b/ogl_editor/src/RemoteConnection.c @@ -493,7 +493,7 @@ void RemoteConnection_sendKeyFrames(const char* name, struct sync_track* track) if (!RemoteConnection_connected() || track_id == -1) return; - for (i = 0; i < (int)track->num_keys; ++i) + for (i = 0; i < track->num_keys; ++i) sendSetKeyCommandIndex((uint32_t)track_id, &track->keys[i]); } diff --git a/ogl_editor/src/loadsave.c b/ogl_editor/src/loadsave.c index ab79a33..240da73 100644 --- a/ogl_editor/src/loadsave.c +++ b/ogl_editor/src/loadsave.c @@ -338,7 +338,7 @@ static void saveTrackData(mxml_node_t* track, struct track_key* keys, int count) for (i = 0; i < count; ++i) { mxml_node_t* key = mxmlNewElement(track, "key"); - setElementInt(key, "row", "%d", (int)keys[i].row); + setElementInt(key, "row", "%d", keys[i].row); setElementFloat(key, "value", keys[i].value); setElementInt(key, "interpolation", "%d", (int)keys[i].type); } @@ -428,7 +428,7 @@ int LoadSave_saveRocketXML(const text_t* path, TrackData* trackData) } else { - saveTrackData(track, t->keys, (int)t->num_keys); + saveTrackData(track, t->keys, t->num_keys); } } diff --git a/ogl_editor/src/macosx/FileDialog.m b/ogl_editor/src/macosx/FileDialog.m index f6b1f8f..6c7ade4 100644 --- a/ogl_editor/src/macosx/FileDialog.m +++ b/ogl_editor/src/macosx/FileDialog.m @@ -10,9 +10,9 @@ int Dialog_open(char* dest) NSOpenPanel* open = [NSOpenPanel openPanel]; [open setAllowsMultipleSelection:NO]; - int result = [open runModal]; + NSInteger result = [open runModal]; - if (result != NSOKButton) + if (result != NSModalResponseOK) return false; // Grab the first file @@ -35,9 +35,9 @@ int Dialog_save(char* dest) NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSSavePanel* open = [NSSavePanel savePanel]; - int result = [open runModal]; + NSInteger result = [open runModal]; - if (result != NSOKButton) + if (result != NSModalResponseOK) return false; // Grab the first file @@ -149,7 +149,12 @@ void Dialog_showError(const text_t* text) NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; NSString* message = [[[NSString alloc] initWithUTF8String:text] autorelease];// convert - NSRunAlertPanel(@"Error", message, @"Ok", @"", @""); + NSAlert *alert = [[NSAlert alloc] init]; + [alert addButtonWithTitle:@"OK"]; + [alert setMessageText:message]; + [alert setAlertStyle:NSCriticalAlertStyle]; + [alert runModal]; + [alert release]; [pool drain]; } diff --git a/ogl_editor/src/macosx/RocketAppDelegate.h b/ogl_editor/src/macosx/RocketAppDelegate.h new file mode 100644 index 0000000..9a120f0 --- /dev/null +++ b/ogl_editor/src/macosx/RocketAppDelegate.h @@ -0,0 +1,13 @@ +#import + +@interface RocketAppDelegate : NSObject { + NSWindow *window; + NSButton *button; +} + +- (IBAction) buttonClicked:(id)sender; + +@property (assign) IBOutlet NSWindow *window; +@property (assign) IBOutlet NSButton *button; + +@end diff --git a/ogl_editor/src/macosx/RocketAppDelegate.m b/ogl_editor/src/macosx/RocketAppDelegate.m new file mode 100644 index 0000000..f861b26 --- /dev/null +++ b/ogl_editor/src/macosx/RocketAppDelegate.m @@ -0,0 +1,103 @@ +#import "RocketAppDelegate.h" +#include "../Editor.h" +#include "../RemoteConnection.h" +#include "rlog.h" + +void Window_populateRecentList(char** files); +void Window_buildMenu(); + +@implementation RocketAppDelegate + +@synthesize window; +@synthesize button; + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender +{ + NSUInteger exitcode = NSTerminateNow; + + if (!Editor_needsSave()) + return exitcode; + + NSAlert *alert = [[NSAlert alloc] init]; + [alert addButtonWithTitle:@"Yes"]; + [alert addButtonWithTitle:@"No"]; + [alert addButtonWithTitle:@"Cancel"]; + [alert setMessageText:@"Save before exit?"]; + [alert setInformativeText:@"Do you want save the work?"]; + [alert setAlertStyle:NSWarningAlertStyle]; + + NSModalResponse ret = [alert runModal]; + + if (ret == NSAlertFirstButtonReturn) + { + if (!Editor_saveBeforeExit()) + exitcode = NSTerminateCancel; + } + + if (ret == NSAlertThirdButtonReturn) + exitcode = NSTerminateCancel; + + [alert release]; + + return exitcode; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +- (void)applicationDidFinishLaunching:(NSNotification *)aNotification +{ + char** recent_list = Editor_getRecentFiles(); + + NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults]; + + for (int i = 0; i < 4; ++i) + recent_list[i][0] = 0; + + if (prefs) + { + NSArray* stringArray = [prefs objectForKey:@"recentFiles"]; + int recentIndex = 0; + + for (int i = 0; i < 4; ++i) + { + NSString* name = [stringArray objectAtIndex:i]; + const char* filename = [name cStringUsingEncoding:NSUTF8StringEncoding]; + if (filename && filename[0] != 0) + strcpy(recent_list[recentIndex++], filename); + } + } + + Window_buildMenu(); + Window_populateRecentList(recent_list); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +- (IBAction) buttonClicked:(id)sender +{ + Editor_menuEvent((int)((NSButton*)sender).tag); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +- (void)applicationWillTerminate:(NSNotification *)aNotification +{ + 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(); + [stringArray release]; +} + +@end diff --git a/ogl_editor/src/macosx/RocketView.m b/ogl_editor/src/macosx/RocketView.m index 5486b38..16c98a6 100644 --- a/ogl_editor/src/macosx/RocketView.m +++ b/ogl_editor/src/macosx/RocketView.m @@ -7,8 +7,8 @@ #include #include -NSOpenGLContext* g_context = 0; -NSWindow* g_window = 0; +NSOpenGLContext* g_context = nil; +NSWindow* g_window = nil; void Window_setTitle(const char* title); @@ -52,13 +52,14 @@ void Window_setTitle(const char* title); if (self == nil) return nil; - NSOpenGLPixelFormatAttribute attributes[4]; - - attributes[0] = NSOpenGLPFADoubleBuffer; - attributes[1] = 0; + NSOpenGLPixelFormatAttribute attributes[] = { + NSOpenGLPFADoubleBuffer, + 0 + }; NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; oglContext = [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil]; + [format release]; [oglContext makeCurrentContext]; g_context = oglContext; @@ -185,6 +186,7 @@ static int getModifierFlags(int flags) owner:self userInfo:nil]; [self addTrackingArea:trackingArea]; + [trackingArea release]; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -296,7 +298,7 @@ CFStringRef createStringForKey(CGKeyCode keyCode) /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -static int s_characterToKeyCode[] = +static CGKeyCode s_characterToKeyCode[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x27, // ''' @@ -383,11 +385,11 @@ NSString* convertKeyCodeToString(int key) case EMGUI_KEY_BACKSPACE : return [NSString stringWithFormat:@"%C",(uint16_t)0x232b]; case EMGUI_KEY_TAB : return [NSString stringWithFormat:@"%C",(uint16_t)0x21e4]; case EMGUI_KEY_PAGE_UP : return [NSString stringWithFormat:@"%C",(uint16_t)0x21de]; - case EMGUI_KEY_PAGE_DOWN : return [NSString stringWithFormat:@"%C",(uint16_t)0x21df]; + case EMGUI_KEY_PAGE_DOWN : return [NSString stringWithFormat:@"%C",(uint16_t)0x21df]; } } - return 0; + return nil; } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -407,8 +409,8 @@ void buildSubMenu(NSMenu* menu, MenuDescriptor menuDesc[]) } else if (desc->id == EDITOR_MENU_SUB_MENU) { - MyMenuItem* newItem = [[MyMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:name action:NULL keyEquivalent:@""]; - NSMenu* newMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:name]; + MyMenuItem* newItem = [[MyMenuItem alloc] initWithTitle:name action:NULL keyEquivalent:@""]; + NSMenu* newMenu = [[NSMenu alloc] initWithTitle:name]; [newItem setSubmenu:newMenu]; [newMenu release]; [menu addItem:newItem]; @@ -416,7 +418,7 @@ void buildSubMenu(NSMenu* menu, MenuDescriptor menuDesc[]) } else { - int mask = 0; + NSUInteger mask = 0; MyMenuItem* newItem = [[MyMenuItem alloc] initWithTitle:name action:@selector(onMenuPress:) keyEquivalent:@""]; [newItem setTag:desc->id]; diff --git a/ogl_editor/src/macosx/delegate.h b/ogl_editor/src/macosx/delegate.h deleted file mode 100644 index 9a120f0..0000000 --- a/ogl_editor/src/macosx/delegate.h +++ /dev/null @@ -1,13 +0,0 @@ -#import - -@interface RocketAppDelegate : NSObject { - NSWindow *window; - NSButton *button; -} - -- (IBAction) buttonClicked:(id)sender; - -@property (assign) IBOutlet NSWindow *window; -@property (assign) IBOutlet NSButton *button; - -@end diff --git a/ogl_editor/src/macosx/delegate.m b/ogl_editor/src/macosx/delegate.m deleted file mode 100644 index f984153..0000000 --- a/ogl_editor/src/macosx/delegate.m +++ /dev/null @@ -1,92 +0,0 @@ -#import "delegate.h" -#include "../Editor.h" -#include "../RemoteConnection.h" -#include "rlog.h" - -void Window_populateRecentList(char** files); -void Window_buildMenu(); - -@implementation RocketAppDelegate - -@synthesize window; -@synthesize button; - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender -{ - if (!Editor_needsSave()) - return NSTerminateNow; - - 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 -{ - char** recent_list = Editor_getRecentFiles(); - - NSUserDefaults* prefs = [NSUserDefaults standardUserDefaults]; - - for (int i = 0; i < 4; ++i) - recent_list[i][0] = 0; - - if (prefs) - { - NSArray* stringArray = [prefs objectForKey:@"recentFiles"]; - int recentIndex = 0; - - for (int i = 0; i < 4; ++i) - { - NSString* name = [stringArray objectAtIndex:i]; - const char* filename = [name cStringUsingEncoding:NSUTF8StringEncoding]; - if (filename && filename[0] != 0) - strcpy(recent_list[recentIndex++], filename); - } - } - - Window_buildMenu(); - Window_populateRecentList(recent_list); -} - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -- (IBAction) buttonClicked:(id)sender -{ - Editor_menuEvent((int)((NSButton*)sender).tag); -} - -/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - -- (void)applicationWillTerminate:(NSNotification *)aNotification -{ - 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(); -} - -@end diff --git a/ogl_editor/src/macosx/main.m b/ogl_editor/src/macosx/main.m index bc3b4aa..41c4b04 100644 --- a/ogl_editor/src/macosx/main.m +++ b/ogl_editor/src/macosx/main.m @@ -1,7 +1,6 @@ -#import #import -int main(int argc, char *argv[]) +int main(int argc, const char * argv[]) { - return NSApplicationMain(argc, (const char **) argv); + return NSApplicationMain(argc, argv); }