diff --git a/new/test.c b/new/test.c new file mode 100644 index 0000000..cd35a5b --- /dev/null +++ b/new/test.c @@ -0,0 +1,32 @@ +// 6 april 2015 +#include "ui.h" +#include + +// #qo pkg-config: gtk+-3.0 + +int onClosing(uiWindow *w, void *data) +{ + printf("in closing!\n"); + uiQuit(); + return 1; +} + +int main(void) +{ + uiInitError *err; + uiWindow *w; + + err = uiInit(NULL); + if (err != NULL) { + fprintf(stderr, "error initializing ui: %s\n", uiInitErrorMessage(err)); + uiInitErrorFree(err); + return 1; + } + + w = uiNewWindow("Hello", 320, 240); + uiWindowOnClosing(w, onClosing, NULL); + uiWindowShow(w); + + uiMain(); + return 0; +} diff --git a/new/ui.h b/new/ui.h new file mode 100644 index 0000000..11f3615 --- /dev/null +++ b/new/ui.h @@ -0,0 +1,27 @@ +// 6 april 2015 + +#ifndef __UI_UI_H__ +#define __UI_UI_H__ + +#include + +typedef struct uiInitError uiInitError; +typedef struct uiInitOptions uiInitOptions; + +uiInitError *uiInit(uiInitOptions *); +const char *uiInitErrorMessage(uiInitError *); +void uiInitErrorFree(uiInitError *); + +void uiMain(void); +void uiQuit(void); + +typedef struct uiWindow uiWindow; +uiWindow *uiNewWindow(char *, int, int); +void uiWindowDestroy(uiWindow *); +uintptr_t uiWindowHandle(uiWindow *); +// TODO titles +void uiWindowShow(uiWindow *); +void uiWindowHide(uiWindow *); +void uiWindowOnClosing(uiWindow *, int (*)(uiWindow *, void *), void *); + +#endif diff --git a/new/ui_unix.h b/new/ui_unix.h new file mode 100644 index 0000000..82bcf7c --- /dev/null +++ b/new/ui_unix.h @@ -0,0 +1,13 @@ +// 6 april 2015 + +#ifndef __UI_UI_UNIX_H__ +#define __UI_UI_UNIX_H__ + +#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_32 +#define GLIB_VERSION_MAX_ALLOWED GLIB_VERSION_2_32 +#define GDK_VERSION_MIN_REQUIRED GDK_VERSION_3_4 +#define GDK_VERSION_MAX_ALLOWED GDK_VERSION_3_4 +#include +#include "ui.h" + +#endif diff --git a/new/uiinit_unix.c b/new/uiinit_unix.c new file mode 100644 index 0000000..bd2463d --- /dev/null +++ b/new/uiinit_unix.c @@ -0,0 +1,27 @@ +// 6 april 2015 +#include "ui_unix.h" + +struct uiInitError { + GError *err; +}; + +uiInitError *uiInit(uiInitOptions *o) +{ + uiInitError *e; + + e = g_new0(uiInitError, 1); + if (gtk_init_with_args(NULL, NULL, NULL, NULL, NULL, &(e->err)) == FALSE) + return e; + uiInitErrorFree(e); + return NULL; +} + +const char *uiInitErrorMessage(uiInitError *e) +{ + return e->err->message; +} + +void uiInitErrorFree(uiInitError *e) +{ + g_free(e); +} diff --git a/new/uimain_unix.c b/new/uimain_unix.c new file mode 100644 index 0000000..988519a --- /dev/null +++ b/new/uimain_unix.c @@ -0,0 +1,12 @@ +// 6 april 2015 +#include "ui_unix.h" + +void uiMain(void) +{ + gtk_main(); +} + +void uiQuit(void) +{ + gtk_main_quit(); +} diff --git a/new/window_unix.c b/new/window_unix.c new file mode 100644 index 0000000..5ace5a5 --- /dev/null +++ b/new/window_unix.c @@ -0,0 +1,59 @@ +// 6 april 2015 +#include "ui_unix.h" + +struct uiWindow { + GtkWidget *widget; + int (*onClosing)(uiWindow *, void *); + void *onClosingData; +}; + +uiWindow *uiNewWindow(char *title, int width, int height) +{ + uiWindow *w; + + w = g_new0(uiWindow, 1); + w->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_window_set_title(GTK_WINDOW(w->widget), title); + gtk_window_resize(GTK_WINDOW(w->widget), width, height); + return w; +} + +void uiWindowDestroy(uiWindow *w) +{ + gtk_widget_destroy(w->widget); + g_free(w); +} + +uintptr_t uiWindowHandle(uiWindow *w) +{ + return (uintptr_t) (w->widget); +} + +// TODO titles + +void uiWindowShow(uiWindow *w) +{ + gtk_widget_show_all(w->widget); +} + +void uiWindowHide(uiWindow *w) +{ + gtk_widget_hide(w->widget); +} + +static gboolean onClosing(GtkWidget *win, GdkEvent *e, gpointer data) +{ + uiWindow *w = (uiWindow *) data; + + // return exact values just in case + if ((*(w->onClosing))(w, w->onClosingData)) + return FALSE; + return TRUE; +} + +void uiWindowOnClosing(uiWindow *w, int (*f)(uiWindow *, void *), void *data) +{ + w->onClosing = f; + w->onClosingData = data; + g_signal_connect(w->widget, "delete-event", G_CALLBACK(onClosing), w); +}