ui/cocoa/window.m

changeset 724
ba7a2f7675b9
parent 570
a2df724b4cb9
child 750
7b627710c155
equal deleted inserted replaced
723:e513086eadbb 724:ba7a2f7675b9
72 UiObject* ui_simple_window(const char *title, void *window_data) { 72 UiObject* ui_simple_window(const char *title, void *window_data) {
73 UiObject *obj = create_window(title, TRUE); 73 UiObject *obj = create_window(title, TRUE);
74 obj->window = window_data; 74 obj->window = window_data;
75 return obj; 75 return obj;
76 } 76 }
77
78
79 /* --------------------------------- File Dialogs --------------------------------- */
80
81 void ui_openfiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {
82 NSOpenPanel *openPanel = [NSOpenPanel openPanel];
83 if((mode & UI_FILEDIALOG_SELECT_MULTI) == UI_FILEDIALOG_SELECT_MULTI) {
84 openPanel.allowsMultipleSelection = YES;
85 }
86 if((mode & UI_FILEDIALOG_SELECT_FOLDER) == UI_FILEDIALOG_SELECT_FOLDER) {
87 openPanel.canChooseFiles = NO;
88 openPanel.canChooseDirectories = YES;
89 }
90
91 NSWindow *window = (__bridge NSWindow*)obj->wobj;
92 [openPanel beginSheetModalForWindow:window completionHandler:^(NSModalResponse result) {
93 UiEvent event;
94 event.obj = obj;
95 event.window = obj->window;
96 event.document = obj->ctx->document;
97 event.intval = 0;
98 event.set = 0;
99
100 UiFileList flist = { NULL, 0 };
101 event.eventdata = &flist;
102 event.eventdatatype = UI_EVENT_DATA_FILE_LIST;
103
104 if(result == NSModalResponseOK) {
105 NSArray<NSURL *> *urls = [openPanel URLs];
106 flist.files = calloc(urls.count, sizeof(char*));
107 for(NSURL *url in urls) {
108 if([url isFileURL]) {
109 flist.files[flist.nfiles++] = strdup(url.path.UTF8String);
110 }
111 }
112 }
113
114 if(file_selected_callback) {
115 file_selected_callback(&event, cbdata);
116 }
117 ui_filelist_free(flist);
118 }];
119 }
120
121 void ui_savefiledialog(UiObject *obj, const char *name, ui_callback file_selected_callback, void *cbdata) {
122 NSSavePanel *savePanel = [NSSavePanel savePanel];
123 if(name) {
124 NSString *nameStr = [[NSString alloc] initWithUTF8String:name];
125 [savePanel setNameFieldStringValue: nameStr];
126 }
127
128 NSWindow *window = (__bridge NSWindow*)obj->wobj;
129 [savePanel beginSheetModalForWindow:window completionHandler:^(NSModalResponse result) {
130 UiEvent event;
131 event.obj = obj;
132 event.window = obj->window;
133 event.document = obj->ctx->document;
134 event.intval = 0;
135 event.set = 0;
136
137 UiFileList flist = { NULL, 0 };
138 event.eventdata = &flist;
139 event.eventdatatype = UI_EVENT_DATA_FILE_LIST;
140
141 if(result == NSModalResponseOK) {
142 NSURL *url = [savePanel URL];
143 if([url isFileURL]) {
144 NSString *path = url.path;
145 flist.files = malloc(sizeof(char*));
146 flist.files[0] = strdup(path.UTF8String);
147 flist.nfiles = 1;
148 }
149 file_selected_callback(NULL, NULL);
150 }
151 if(file_selected_callback) {
152 file_selected_callback(&event, cbdata);
153 }
154 ui_filelist_free(flist);
155 }];
156 }

mercurial