146 |
149 |
147 UiObject* ui_window(const char *title, void *window_data) { |
150 UiObject* ui_window(const char *title, void *window_data) { |
148 return create_window(title, window_data, FALSE); |
151 return create_window(title, window_data, FALSE); |
149 } |
152 } |
150 |
153 |
151 UiObject* ui_simplewindow(const char *title, void *window_data) { |
154 UiObject* ui_simple_window(const char *title, void *window_data) { |
152 return create_window(title, window_data, TRUE); |
155 return create_window(title, window_data, TRUE); |
153 } |
156 } |
154 |
157 |
155 static char* ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action) { |
158 void ui_window_size(UiObject *obj, int width, int height) { |
|
159 gtk_window_set_default_size( |
|
160 GTK_WINDOW(obj->widget), |
|
161 width, |
|
162 height); |
|
163 } |
|
164 |
|
165 static void ui_dialog_response (GtkDialog* self, gint response_id, gpointer user_data) { |
|
166 UiEventData *data = user_data; |
|
167 UiEvent evt; |
|
168 evt.obj = data->obj; |
|
169 evt.document = evt.obj->ctx->document; |
|
170 evt.window = evt.obj->window; |
|
171 evt.eventdata = NULL; |
|
172 evt.intval = 0; |
|
173 |
|
174 if(data->customdata) { |
|
175 GtkWidget *entry = data->customdata; |
|
176 evt.eventdata = (void*)gtk_entry_get_text(GTK_ENTRY(entry)); |
|
177 |
|
178 } |
|
179 |
|
180 if(response_id == 1 || response_id == 2) { |
|
181 evt.intval = response_id; |
|
182 } |
|
183 |
|
184 |
|
185 if(data->callback) { |
|
186 data->callback(&evt, data->userdata); |
|
187 } |
|
188 |
|
189 gtk_widget_destroy(GTK_WIDGET(self)); |
|
190 } |
|
191 |
|
192 void ui_dialog_create(UiObject *parent, UiDialogArgs args) { |
|
193 GtkDialog *dialog = GTK_DIALOG(gtk_dialog_new()); |
|
194 GtkWidget *dialog_w = GTK_WIDGET(dialog); |
|
195 if(args.title) { |
|
196 gtk_window_set_title(GTK_WINDOW(dialog), args.title); |
|
197 } |
|
198 if(args.button1_label) { |
|
199 gtk_dialog_add_button(dialog, args.button1_label, 1); |
|
200 } |
|
201 if(args.button2_label) { |
|
202 gtk_dialog_add_button(dialog, args.button2_label, 2); |
|
203 } |
|
204 if(args.closebutton_label) { |
|
205 gtk_dialog_add_button(dialog, args.closebutton_label, 0); |
|
206 } |
|
207 |
|
208 GtkWidget *content_area = gtk_dialog_get_content_area(dialog); |
|
209 if(args.content) { |
|
210 GtkWidget *label = gtk_label_new(args.content); |
|
211 gtk_container_add(GTK_CONTAINER(content_area), label); |
|
212 } |
|
213 |
|
214 GtkWidget *textfield = NULL; |
|
215 if(args.input) { |
|
216 textfield = gtk_entry_new(); |
|
217 gtk_container_add(GTK_CONTAINER(content_area), textfield); |
|
218 } |
|
219 |
|
220 UiEventData *event = malloc(sizeof(UiEventData)); |
|
221 event->obj = parent; |
|
222 event->callback = args.result; |
|
223 event->userdata = args.resultdata; |
|
224 event->value = 0; |
|
225 event->customdata = textfield; |
|
226 |
|
227 g_signal_connect(dialog_w, |
|
228 "response", |
|
229 G_CALLBACK(ui_dialog_response), |
|
230 event); |
|
231 |
|
232 gtk_widget_show_all(GTK_WIDGET(dialog_w)); |
|
233 } |
|
234 |
|
235 static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, ui_callback file_selected_callback, void *cbdata) { |
156 char *button; |
236 char *button; |
157 char *title; |
237 char *title; |
158 |
238 |
159 if(action == GTK_FILE_CHOOSER_ACTION_OPEN) { |
239 GtkWidget *dialog; |
160 button = GTK_STOCK_OPEN; |
240 if((mode & UI_FILEDIALOG_SELECT_FOLDER) == UI_FILEDIALOG_SELECT_FOLDER) { |
161 title = "Datei öffnen..."; |
241 dialog = gtk_file_chooser_dialog_new ( |
|
242 "Open Folder", |
|
243 GTK_WINDOW(obj->widget), |
|
244 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, |
|
245 "Cancel", |
|
246 GTK_RESPONSE_CANCEL, |
|
247 "Select Folder", |
|
248 GTK_RESPONSE_ACCEPT, |
|
249 NULL); |
|
250 } else if(action == GTK_FILE_CHOOSER_ACTION_OPEN) { |
|
251 dialog = gtk_file_chooser_dialog_new ( |
|
252 "Select Folder", |
|
253 GTK_WINDOW(obj->widget), |
|
254 action, |
|
255 "Cancel", |
|
256 GTK_RESPONSE_CANCEL, |
|
257 "Open File", |
|
258 GTK_RESPONSE_ACCEPT, |
|
259 NULL); |
162 } else { |
260 } else { |
163 button = GTK_STOCK_SAVE; |
261 dialog = gtk_file_chooser_dialog_new ( |
164 title = "Datei speichern..."; |
262 "Save File", |
165 } |
263 GTK_WINDOW(obj->widget), |
166 |
264 action, |
167 GtkWidget *dialog = gtk_file_chooser_dialog_new( |
265 "Cancel", |
168 title, |
266 GTK_RESPONSE_CANCEL, |
169 GTK_WINDOW(obj->widget), |
267 "Save File", |
170 action, |
268 GTK_RESPONSE_ACCEPT, |
171 GTK_STOCK_CANCEL, |
269 NULL); |
172 GTK_RESPONSE_CANCEL, |
270 } |
173 button, |
271 |
174 GTK_RESPONSE_ACCEPT, |
272 if((mode & UI_FILEDIALOG_SELECT_MULTI) == UI_FILEDIALOG_SELECT_MULTI) { |
175 NULL); |
273 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE); |
176 if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { |
274 } |
177 char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); |
275 |
178 gtk_widget_destroy(dialog); |
276 UiEvent evt; |
179 char *copy = strdup(file); |
277 evt.obj = obj; |
180 g_free(file); |
278 evt.document = evt.obj->ctx->document; |
181 return copy; |
279 evt.window = evt.obj->window; |
182 } else { |
280 evt.intval = 0; |
183 gtk_widget_destroy(dialog); |
281 |
184 return NULL; |
282 UiFileList flist; |
185 } |
283 flist.files = NULL; |
186 } |
284 flist.nfiles = 0; |
187 |
285 evt.eventdata = &flist; |
188 |
286 |
|
287 int result = gtk_dialog_run(GTK_DIALOG (dialog)); |
|
288 GSList *selection = NULL; |
|
289 if(result == GTK_RESPONSE_ACCEPT) { |
|
290 selection = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); |
|
291 flist.nfiles = g_slist_length(selection); |
|
292 flist.files = calloc(flist.nfiles, sizeof(char*)); |
|
293 int i = 0; |
|
294 while(selection) { |
|
295 flist.files[i] = selection->data; |
|
296 selection = selection->next; |
|
297 i++; |
|
298 } |
|
299 } |
|
300 |
|
301 if(file_selected_callback) { |
|
302 file_selected_callback(&evt, cbdata); |
|
303 } |
|
304 |
|
305 for(int i=0;i<flist.nfiles;i++) { |
|
306 g_free(flist.files[i]); |
|
307 } |
|
308 free(flist.files); |
|
309 g_slist_free(selection); |
|
310 |
|
311 gtk_widget_destroy(dialog); |
|
312 } |
|
313 |
|
314 void ui_openfiledialog(UiObject *obj, unsigned int mode, ui_callback file_selected_callback, void *cbdata) { |
|
315 ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN, mode, file_selected_callback, cbdata); |
|
316 } |
|
317 |
|
318 void ui_savefiledialog(UiObject *obj, const char *name, ui_callback file_selected_callback, void *cbdata) { |
|
319 ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_SAVE, 0, file_selected_callback, cbdata); |
|
320 } |
|
321 |