Repository navigation
Expand file tree
/
Copy pathSaveFileDialog.m
More file actions
63 lines (53 loc) · 2.14 KB
/
Copy pathSaveFileDialog.m
File metadata and controls
63 lines (53 loc) · 2.14 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:@"Save..."];
[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:@"SaveFileDialog example"];
[[self contentView] addSubview:button];
[[self contentView] addSubview:label];
[self setIsVisible:YES];
return self;
}
- (IBAction) OnButtonClick:(id)sender {
NSSavePanel* saveFileDialog = [[[NSSavePanel alloc] init] autorelease];
[saveFileDialog setCanCreateDirectories:YES];
[saveFileDialog setAllowedFileTypes:[NSArray arrayWithObjects:@"txt", @"md", nil]];
[saveFileDialog setDirectoryURL:[NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES) firstObject]]];
[saveFileDialog setNameFieldStringValue:@"MyFile.txt"];
NSModalResponse response = [saveFileDialog runModal];
if (response == NSModalResponseOK)
[label setStringValue:[NSString stringWithFormat:@"File = %@", [[saveFileDialog URL] 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];
}