Repository navigation
Expand file tree
/
Copy pathFolderBrowserDialog.m
More file actions
63 lines (53 loc) · 2.16 KB
/
Copy pathFolderBrowserDialog.m
File metadata and controls
63 lines (53 loc) · 2.16 KB
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
#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:@"Folder..."];
[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:@"Path ="];
[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:@"FolderBrowserDialog example"];
[[self contentView] addSubview:button];
[[self contentView] addSubview:label];
[self setIsVisible:YES];
return self;
}
- (IBAction) OnButtonClick:(id)sender {
NSOpenPanel* folderBrowserDialog = [[[NSOpenPanel alloc] init] autorelease];
[folderBrowserDialog setCanChooseFiles:NO];
[folderBrowserDialog setCanChooseDirectories:YES];
[folderBrowserDialog setAllowsMultipleSelection:NO];
[folderBrowserDialog setDirectoryURL:[NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) firstObject]]];
NSModalResponse response = [folderBrowserDialog runModal];
if (response == NSModalResponseOK)
[label setStringValue:[NSString stringWithFormat:@"Path = %@", [(NSURL*)[[folderBrowserDialog 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];
}