-
Notifications
You must be signed in to change notification settings - Fork 3
/
save-file.c
76 lines (55 loc) · 2.56 KB
/
save-file.c
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
65
66
67
68
69
70
71
72
73
74
75
76
/*
Based on: https://github.com/gammasoft71/Examples_Cocoa/blob/master/src/CommonDialogs/SaveFileDialog/README.md
*/
#define GL_SILENCE_DEPRECATION
#define SILICON_IMPLEMENTATION
#include <silicon.h>
NSButton* button;
NSTextField* label;
NSApplication* NSApp;
void OnButtonClick(void* sender) {
siArray(const char*) value = si_array_init((char*[]){"txt", "md"}, sizeof(*value), 2);
NSSavePanel* saveFileDialog = NSInit(NSAlloc(SI_NS_CLASSES[NS_SAVE_PANEL_CODE]));
NSSavePanel_setCanCreateDirectories(saveFileDialog, true);
NSSavePanel_setAllowedFileTypes(saveFileDialog, value);
siArray(const char*) directories = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, true);
NSURL* url = NSURL_fileURLWithPath(directories[0]);
NSSavePanel_setDirectoryURL(saveFileDialog, url);
NSSavePanel_setNameFieldStringValue(saveFileDialog, "MyFile.txt");
NSModalResponse response = NSSavePanel_runModal(saveFileDialog);
if (response == NSModalResponseOK)
NSTextField_setStringValue(label, NSURL_path(NSSavePanel_URL(saveFileDialog)));
si_array_free(value);
si_array_free(directories);
release(saveFileDialog);
}
bool windowShouldClose(void* sender) {
NSApplication_terminate(NSApp, sender);
return true;
}
int main(int argc, char* argv[]) {
si_func_to_SEL(SI_DEFAULT, OnButtonClick);
si_func_to_SEL(SI_DEFAULT, windowShouldClose);
NSApp = NSApplication_sharedApplication();
NSApplication_setActivationPolicy(NSApp, NSApplicationActivationPolicyRegular);
NSWindow* window = NSWindow_init(NSMakeRect(100, 100, 300, 300), NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable, NSBackingStoreBuffered, false);
NSView* view = NSWindow_contentView(window);
button = NSButton_initWithFrame(NSMakeRect(10, 265, 100, 32));
NSButton_setTitle(button, "Save...");
NSButton_setBezelStyle(button, NSBezelStyleRounded);
NSButton_setTarget(button, (id)window);
NSButton_setAction(button, selector(OnButtonClick));
NSButton_setAutoresizingMask(button, NSViewMaxXMargin | NSViewMinYMargin);
label = NSTextField_initWithFrame(NSMakeRect(10, 235, 280, 20));
NSTextField_setStringValue(label, "File =");
NSTextField_setBezeled(label, false);
NSTextField_setDrawsBackground(label, false);
NSTextField_setEditable(label, false);
NSTextField_setSelectable(label, false);
NSWindow_setTitle(window, "SaveFileDialog example");
NSView_addSubview(view, (void*)button);
NSView_addSubview(view, (void*)label);
NSWindow_setIsVisible(window, true);
NSWindow_makeMainWindow(window);
NSApplication_run(NSApp);
}