# HG changeset patch # User Olaf Wintermann # Date 1396783297 -7200 # Node ID 794a5c91c47943a587282f1bf6fbcffdd9011a7b # Parent 77b09bb52ca050247e9b0598e3118ce76e12ebff added open/save dialogs diff -r 77b09bb52ca0 -r 794a5c91c479 application/main.c --- a/application/main.c Sat Apr 05 18:41:30 2014 +0200 +++ b/application/main.c Sun Apr 06 13:21:37 2014 +0200 @@ -30,6 +30,8 @@ #include #include +#include +#include typedef struct TestDocument { UiInteger check1; @@ -55,7 +57,21 @@ //printf("check1: %s\n", event->intval ? "true" : "false"); //printf("check1: %s\n", ui_getint(event->obj, "check1") ? "true" : "false"); TestDocument *doc = event->document; + TestWindowData *wd = event->window; printf("check1: %s\n", ui_getval(doc->check1) ? "true" : "false"); + + char *path = ui_openfiledialog(event->obj); + if(path) { + UcxBuffer *buf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND); + FILE *file = fopen(path, "r"); + if(file) { + ucx_stream_hcopy(file, buf, fread, ucx_buffer_write); + ucx_buffer_putc(buf, '\0'); + ui_setval(wd->text, buf->space); + fclose(file); + } + ucx_buffer_free(buf); + } } void action_save(UiEvent *event, void *data) { diff -r 77b09bb52ca0 -r 794a5c91c479 ui/cocoa/window.m --- a/ui/cocoa/window.m Sat Apr 05 18:41:30 2014 +0200 +++ b/ui/cocoa/window.m Sun Apr 06 13:21:37 2014 +0200 @@ -27,6 +27,8 @@ */ #include +#include +#include #import "window.h" #import "menu.h" @@ -190,3 +192,26 @@ return obj; } + +char* ui_openfiledialog(UiObject *obj) { + NSOpenPanel* op = [NSOpenPanel openPanel]; + if ([op runModal] == NSOKButton) { + NSArray *urls = [op URLs]; + NSURL *url = [urls objectAtIndex:0]; + + const char *str = [[url path] UTF8String]; + return (char*)strdup(str); + } + return NULL; +} + +char* ui_savefiledialog(UiObject *obj) { + NSSavePanel* sp = [NSSavePanel savePanel]; + if ([sp runModal] == NSOKButton) { + NSURL *url = [sp URL]; + + const char *str = [[url path] UTF8String]; + return (char*)strdup(str); + } + return NULL; +} diff -r 77b09bb52ca0 -r 794a5c91c479 ui/gtk/window.c --- a/ui/gtk/window.c Sat Apr 05 18:41:30 2014 +0200 +++ b/ui/gtk/window.c Sun Apr 06 13:21:37 2014 +0200 @@ -115,3 +115,44 @@ return obj; } +static char* ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action) { + char *button; + char *title; + + if(action == GTK_FILE_CHOOSER_ACTION_OPEN) { + button = GTK_STOCK_OPEN; + title = "Datei öffnen..."; + } else { + button = GTK_STOCK_SAVE; + title = "Datei speichern..."; + } + + GtkWidget *dialog = gtk_file_chooser_dialog_new( + title, + GTK_WINDOW(obj->widget), + action, + GTK_STOCK_CANCEL, + GTK_RESPONSE_CANCEL, + button, + GTK_RESPONSE_ACCEPT, + NULL); + if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { + char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + gtk_widget_destroy(dialog); + char *copy = strdup(file); + g_free(file); + return copy; + } else { + gtk_widget_destroy(dialog); + return NULL; + } +} + +char* ui_openfiledialog(UiObject *obj) { + return ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_OPEN); +} + +char* ui_savefiledialog(UiObject *obj) { + return ui_gtkfilechooser(obj, GTK_FILE_CHOOSER_ACTION_SAVE); +} + diff -r 77b09bb52ca0 -r 794a5c91c479 ui/motif/toolkit.c --- a/ui/motif/toolkit.c Sat Apr 05 18:41:30 2014 +0200 +++ b/ui/motif/toolkit.c Sun Apr 06 13:21:37 2014 +0200 @@ -88,6 +88,14 @@ uic_store_app_properties(); } +void ui_secondary_event_loop(int *loop) { + while(*loop && !XtAppGetExitFlag(app)) { + XEvent event; + XtAppNextEvent(app, &event); + XtDispatchEvent(&event); + } +} + void ui_show(UiObject *obj) { uic_check_group_widgets(obj->ctx); XtRealizeWidget(obj->widget); diff -r 77b09bb52ca0 -r 794a5c91c479 ui/motif/toolkit.h --- a/ui/motif/toolkit.h Sat Apr 05 18:41:30 2014 +0200 +++ b/ui/motif/toolkit.h Sun Apr 06 13:21:37 2014 +0200 @@ -49,6 +49,8 @@ void ui_set_active_window(Widget w); Widget ui_get_active_window(); +void ui_secondary_event_loop(int *loop); + #ifdef __cplusplus } #endif diff -r 77b09bb52ca0 -r 794a5c91c479 ui/motif/window.c --- a/ui/motif/window.c Sat Apr 05 18:41:30 2014 +0200 +++ b/ui/motif/window.c Sun Apr 06 13:21:37 2014 +0200 @@ -127,3 +127,50 @@ nwindows++; return obj; } + +typedef struct FileDialogData { + int running; + char *file; +} FileDialogData; + +static void filedialog_select( + Widget widget, + FileDialogData *data, + XmFileSelectionBoxCallbackStruct *selection) +{ + char *path = NULL; + XmStringGetLtoR(selection->value, XmSTRING_DEFAULT_CHARSET, &path); + data->running = 0; + data->file = strdup(path); + XtFree(path); + XtUnmanageChild(widget); +} + +static void filedialog_cancel( + Widget widget, + FileDialogData *data, + XmFileSelectionBoxCallbackStruct *selection) + +{ + data->running = 0; + XtUnmanageChild(widget); +} + +char* ui_openfiledialog(UiObject *obj) { + Widget dialog = XmCreateFileSelectionDialog(obj->widget, "openfiledialog", NULL, 0); + XtManageChild(dialog); + + FileDialogData data; + data.running = 1; + data.file = NULL; + + XtAddCallback(dialog, XmNokCallback, (XtCallbackProc)filedialog_select, &data); + XtAddCallback(dialog, XmNcancelCallback, (XtCallbackProc)filedialog_cancel, &data); + + ui_secondary_event_loop(&data.running); + return data.file; +} + +char* ui_savefiledialog(UiObject *obj) { + return ui_openfiledialog(obj); +} diff -r 77b09bb52ca0 -r 794a5c91c479 ui/ui/window.h --- a/ui/ui/window.h Sat Apr 05 18:41:30 2014 +0200 +++ b/ui/ui/window.h Sun Apr 06 13:21:37 2014 +0200 @@ -36,12 +36,8 @@ #endif UiObject* ui_window(char *title, void *window_data); - -void ui_window_addint(UiObject *obj, char *name); -void ui_window_regint(UiObject *obj, char *name, UiInteger *i); -void ui_window_setint(UiObject *obj, char *name, int val); -int ui_window_getint(UiObject *obj, char *name); - +char* ui_openfiledialog(UiObject *obj); +char* ui_savefiledialog(UiObject *obj); #ifdef __cplusplus }