Started doing the C rewrite. Defined the basic initialization and main loop and window API and implemented them on GTK+.

This commit is contained in:
Pietro Gagliardi 2015-04-06 12:20:51 -04:00
parent 64d0100d7a
commit fc19f776c4
6 changed files with 170 additions and 0 deletions

32
test.c Normal file
View File

@ -0,0 +1,32 @@
// 6 april 2015
#include "ui.h"
#include <stdio.h>
// #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;
}

27
ui.h Normal file
View File

@ -0,0 +1,27 @@
// 6 april 2015
#ifndef __UI_UI_H__
#define __UI_UI_H__
#include <stdint.h>
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

13
ui_unix.h Normal file
View File

@ -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 <gtk/gtk.h>
#include "ui.h"
#endif

27
uiinit_unix.c Normal file
View File

@ -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);
}

12
uimain_unix.c Normal file
View File

@ -0,0 +1,12 @@
// 6 april 2015
#include "ui_unix.h"
void uiMain(void)
{
gtk_main();
}
void uiQuit(void)
{
gtk_main_quit();
}

59
window_unix.c Normal file
View File

@ -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);
}