-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathOpenFileDialog.m
64 lines (54 loc) · 2.2 KB
/
OpenFileDialog.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <Cocoa/Cocoa.h>
@interface Window : NSWindow {
NSButton* button;
NSTextField* label;
}
- (IBAction) OnButtonClick:(id)sender;
- (void)changeColor:(id)sender;
- (BOOL)windowShouldClose:(id)sender;
@end
@implementation Window
- (instancetype)init {
button = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 265, 100, 32)] autorelease];
[button setTitle:@"Open..."];
[button setBezelStyle:NSBezelStyleRounded];
[button setTarget:self];
[button setAction:@selector(OnButtonClick:)];
[button setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];
label = [[[NSTextField alloc] initWithFrame:NSMakeRect(10, 235, 280, 20)] autorelease];
[label setStringValue:@"File ="];
[label setBezeled:NO];
[label setDrawsBackground:NO];
[label setEditable:NO];
[label setSelectable:NO];
[super initWithContentRect:NSMakeRect(100, 100, 300, 300) styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];
[self setTitle:@"OpenFileDialog example"];
[[self contentView] addSubview:button];
[[self contentView] addSubview:label];
[self setIsVisible:YES];
return self;
}
- (IBAction) OnButtonClick:(id)sender {
NSOpenPanel* openFileDialog = [[[NSOpenPanel alloc] init] autorelease];
[openFileDialog setCanChooseFiles:YES];
[openFileDialog setCanChooseDirectories:NO];
[openFileDialog setAllowsMultipleSelection:NO];
[openFileDialog setAllowedFileTypes:[NSArray arrayWithObjects:@"txt", @"md", nil]];
[openFileDialog setDirectoryURL:[NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) firstObject]]];
NSModalResponse response = [openFileDialog runModal];
if (response == NSModalResponseOK)
[label setStringValue:[NSString stringWithFormat:@"File = %@", [(NSURL*)[[openFileDialog URLs] objectAtIndex:0] path]]];
}
- (void)changeColor:(id)sender {
[self setBackgroundColor:[(NSColorPanel*)sender color]];
}
- (BOOL)windowShouldClose:(id)sender {
[NSApp terminate:sender];
return NO;
}
@end
int main(int argc, char* argv[]) {
[NSApplication sharedApplication];
[[[[Window alloc] init] autorelease] makeMainWindow];
[NSApp run];
}