Skip to content

Commit ddd724a

Browse files
committed
Merge pull request #1 from LRFLEW/dialogs
Some Objective-C Code Cleanup
2 parents 88e67bb + 8b63080 commit ddd724a

File tree

1 file changed

+30
-39
lines changed

1 file changed

+30
-39
lines changed

src/platform/osx.m

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ void platform_show_messagebox(char *message)
7979
{
8080
@autoreleasepool
8181
{
82-
NSAlert *alert = [[NSAlert alloc] init];
82+
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
8383
[alert addButtonWithTitle:@"OK"];
84-
[alert setMessageText:[NSString stringWithUTF8String:message]];
85-
[alert setAlertStyle:NSWarningAlertStyle];
84+
alert.messageText = [NSString stringWithUTF8String:message];
85+
alert.alertStyle = NSWarningAlertStyle;
8686
[alert runModal];
87-
[alert release];
8887
}
8988
}
9089

@@ -99,8 +98,8 @@ void platform_show_messagebox(char *message)
9998
utf8 *url = NULL;
10099
if ([panel runModal] == NSFileHandlingPanelOKButton)
101100
{
102-
NSString *selectedPath = [[[panel URLs] firstObject] path];
103-
const char *path = [selectedPath UTF8String];
101+
NSString *selectedPath = panel.URL.path;
102+
const char *path = selectedPath.UTF8String;
104103
url = (utf8*)malloc(strlen(path) + 1);
105104
strcpy(url,path);
106105
}
@@ -112,48 +111,40 @@ int platform_open_common_file_dialog(int type, utf8 *title, utf8 *filename, utf8
112111
{
113112
@autoreleasepool
114113
{
115-
NSArray *extensions = [
116-
[
117-
[NSString stringWithUTF8String:filterPattern]
118-
stringByReplacingOccurrencesOfString:@"*." withString:@""
119-
]
120-
componentsSeparatedByString:@";"
121-
];
114+
NSString *fillPatternNS = [NSString stringWithUTF8String:filterPattern];
115+
fillPatternNS = [fillPatternNS stringByReplacingOccurrencesOfString:@"*." withString:@""];
116+
NSArray *extensions = [fillPatternNS componentsSeparatedByString:@";"];
117+
122118
NSString *filePath = [NSString stringWithUTF8String:filename];
123-
NSString *directory = [filePath stringByDeletingLastPathComponent];
124-
NSString *basename = [filePath lastPathComponent];
119+
NSString *directory = filePath.stringByDeletingLastPathComponent;
120+
NSString *basename = filePath.lastPathComponent;
121+
122+
NSSavePanel *panel;
125123
if (type == 0)
126124
{
127-
NSSavePanel *panel = [NSSavePanel savePanel];
128-
panel.title = [NSString stringWithUTF8String:title];
129-
panel.nameFieldStringValue = [NSString stringWithFormat:@"%@.%@",basename,[extensions firstObject]];
130-
panel.allowedFileTypes = extensions;
131-
panel.directoryURL = [NSURL fileURLWithPath:directory];
132-
if ([panel runModal] == NSFileHandlingPanelCancelButton){
133-
return 0;
134-
} else {
135-
strcpy(filename,[[[panel URL] path] UTF8String]);
136-
return 1;
137-
}
125+
panel = [NSSavePanel savePanel];
126+
panel.nameFieldStringValue = [NSString stringWithFormat:@"%@.%@", basename, extensions.firstObject];
138127
}
139128
else if (type == 1)
140129
{
141-
NSOpenPanel *panel = [NSOpenPanel openPanel];
142-
panel.title = [NSString stringWithUTF8String:title];
143-
panel.allowedFileTypes = extensions;
144-
panel.canChooseDirectories = false;
145-
panel.canChooseFiles = true;
146-
panel.allowsMultipleSelection = false;
147-
panel.directoryURL = [NSURL fileURLWithPath:filePath];
148-
if ([panel runModal] == NSFileHandlingPanelCancelButton){
149-
return 0;
150-
} else {
151-
strcpy(filename,[[[[panel URLs] firstObject] path] UTF8String]);
152-
return 1;
153-
}
130+
NSOpenPanel *open = [NSOpenPanel openPanel];
131+
open.canChooseDirectories = false;
132+
open.canChooseFiles = true;
133+
open.allowsMultipleSelection = false;
134+
panel = open;
154135
} else {
155136
return 0;
156137
}
138+
139+
panel.title = [NSString stringWithUTF8String:title];
140+
panel.allowedFileTypes = extensions;
141+
panel.directoryURL = [NSURL fileURLWithPath:directory];
142+
if ([panel runModal] == NSFileHandlingPanelCancelButton){
143+
return 0;
144+
} else {
145+
strcpy(filename, panel.URL.path.UTF8String);
146+
return 1;
147+
}
157148
}
158149
}
159150

0 commit comments

Comments
 (0)