diff --git a/include/GToolkit.h b/include/GToolkit.h index 5677a1a..22ed917 100644 --- a/include/GToolkit.h +++ b/include/GToolkit.h @@ -31,6 +31,7 @@ #ifndef GTOOLKIT_GTOOLKIT_H # define GTOOLKIT_GTOOLKIT_H +# include "GToolkit/GWidget.h" # include "GToolkit/GWindow.h" diff --git a/include/GToolkit/GWidget.h b/include/GToolkit/GWidget.h index 64f7784..c3e34dc 100644 --- a/include/GToolkit/GWidget.h +++ b/include/GToolkit/GWidget.h @@ -35,5 +35,10 @@ /* GWidget */ /* types */ typedef struct _GWidget GWidget; +# define GWIDGET(gwidget) (GWidget *)(gwidget) + + +/* functions */ +void gwidget_show(GWidget * gwidget); #endif /* !GTOOLKIT_GWIDGET_H */ diff --git a/include/GToolkit/GWindow.h b/include/GToolkit/GWindow.h index 09f26db..c0c026c 100644 --- a/include/GToolkit/GWindow.h +++ b/include/GToolkit/GWindow.h @@ -55,6 +55,5 @@ void gwindow_set_title(GWindow * gwindow, char const * title); /* useful */ void gwindow_resize(GWindow * gwindow, int width, int height); -void gwindow_show(GWindow * gwindow); #endif /* !GTOOLKIT_GWINDOW_H */ diff --git a/src/common.h b/src/common.h index dddb093..b59de03 100644 --- a/src/common.h +++ b/src/common.h @@ -34,8 +34,19 @@ # include # include # include +# include +/* types */ +typedef enum _GWidgetHandler +{ + GWIDGET_HANDLER_SHOW = 0 +} GWidgetHandler; +# define GWIDGET_HANDLER_LAST GWIDGET_HANDLER_SHOW +# define GWIDGET_HANDLER_COUNT (GWIDGET_HANDLER_LAST + 1) + + +/* functions */ /* GToolkit */ /* accessors */ Display * gtoolkit_get_display(void); @@ -47,6 +58,15 @@ void gtoolkit_register_window(GWindow * gwindow); void gtoolkit_deregister_window(GWindow * gwindow); +/* GWidget */ +GWidget * gwidget_new(void); +void gwidget_delete(GWidget * gwidget); + +/* useful */ +void gwidget_set_handler(GWidget * gwidget, GWidgetHandler handler, ...); +void gwidget_set_self(GWidget * gwidget, void * self); + + /* GWindow */ /* accessors */ Window gwindow_get_window(GWindow * gwindow); diff --git a/src/gwidget.h b/src/gwidget.c similarity index 53% rename from src/gwidget.h rename to src/gwidget.c index d79a783..2e26a0f 100644 --- a/src/gwidget.h +++ b/src/gwidget.c @@ -1,5 +1,5 @@ /* $Id$ */ -/* Copyright (c) 2006-2020 Pierre Pronchery */ +/* Copyright (c) 2020 Pierre Pronchery */ /* This file is part of DeforaOS Graphics GToolkit */ /* All rights reserved. * @@ -28,13 +28,81 @@ -#ifndef GTOOLKIT_GWIDGET_H -# define GTOOLKIT_GWIDGET_H +#include +#include +#include "common.h" /* GWidget */ -/* public */ +/* private */ /* types */ -GWidget * widget; +typedef void (*GWidgetHandlerSelf)(void * self); -#endif /* !GTOOLKIT_GWIDGET_H */ +struct _GWidget +{ + GWidget * gwidget; + + /* GWidget */ + void * self; + + /* handlers */ + GWidgetHandlerSelf handler_show; +}; + + +/* public */ +/* functions */ +/* gwidget_new */ +GWidget * gwidget_new(void) +{ + GWidget * gwidget; + + if((gwidget = malloc(sizeof(*gwidget))) == NULL) + return NULL; + gwidget->gwidget = gwidget; + gwidget->self = NULL; + gwidget->handler_show = NULL; + return gwidget; +} + + +/* gwidget_delete */ +void gwidget_delete(GWidget * gwidget) +{ + free(gwidget); +} + + +/* accessors */ +/* gwidget_set_handler */ +void gwidget_set_handler(GWidget * gwidget, GWidgetHandler handler, ...) +{ + va_list ap; + + va_start(ap, handler); + switch(handler) + { + case GWIDGET_HANDLER_SHOW: + gwidget->handler_show = va_arg(ap, GWidgetHandlerSelf); + break; + } + va_end(ap); +} + + +/* gwidget_set_self */ +void gwidget_set_self(GWidget * gwidget, void * self) +{ + gwidget->self = self; +} + + +/* useful */ +/* gwidget_show */ +void gwidget_show(GWidget * gwidget) +{ + if(gwidget->gwidget != gwidget) + gwidget = gwidget->gwidget; + if(gwidget->handler_show != NULL) + gwidget->handler_show(gwidget->self); +} diff --git a/src/gwindow.c b/src/gwindow.c index 0f98dd7..0f00fd5 100644 --- a/src/gwindow.c +++ b/src/gwindow.c @@ -32,6 +32,7 @@ #include #include "GToolkit/GWidget.h" #include "GToolkit/GWindow.h" +#include "GToolkit.h" #include "common.h" @@ -40,7 +41,7 @@ /* types */ struct _GWindow { -#include "gwidget.h" + GWidget * gwidget; /* GWindow */ char * title; @@ -56,6 +57,10 @@ struct _GWindow }; +/* prototypes */ +static void _gwindow_show(GWindow * gwindow); + + /* public */ /* functions */ /* gwindow_new */ @@ -69,6 +74,14 @@ GWindow * gwindow_new(void) if((gwindow = malloc(sizeof(*gwindow))) == NULL) return NULL; /* FIXME report */ + if((gwindow->gwidget = gwidget_new()) == NULL) + { + free(gwindow); + return NULL; + } + gwidget_set_self(gwindow->gwidget, gwindow); + gwidget_set_handler(gwindow->gwidget, GWIDGET_HANDLER_SHOW, + _gwindow_show); gwindow->title = NULL; gwindow->decorated = true; gwindow->fullscreen = false; @@ -216,8 +229,10 @@ void gwindow_resize(GWindow * gwindow, int width, int height) } +/* private */ +/* functions */ /* gwindow_show */ -void gwindow_show(GWindow * gwindow) +static void _gwindow_show(GWindow * gwindow) /* FIXME accept flags (focus...) */ { Display * display; diff --git a/src/project.conf b/src/project.conf index 50acaec..76f4b26 100644 --- a/src/project.conf +++ b/src/project.conf @@ -2,7 +2,7 @@ targets=libGToolkit cppflags_force=-I ../include cflags_force=-W cflags=-Wall -fPIC -g -O2 -pedantic -dist=Makefile,common.h,gwidget.h +dist=Makefile,common.h #targets [libGToolkit] @@ -10,11 +10,14 @@ type=library cflags=`pkg-config --cflags gl` ldflags=`pkg-config --libs gl` install=$(PREFIX)/lib -sources=gtoolkit.c,gwindow.c +sources=gtoolkit.c,gwidget.c,gwindow.c #sources [gtoolkit.c] depends=common.h +[gwidget.c] +depends=common.h + [gwindow.c] depends=common.h diff --git a/tools/test.c b/tools/test.c index 15926a6..f872833 100644 --- a/tools/test.c +++ b/tools/test.c @@ -40,7 +40,7 @@ int main(void) return 2; window = gwindow_new(); gwindow_set_title(window, "Test window"); - gwindow_show(window); + gwidget_show(GWIDGET(window)); gtoolkit_main(); return 0; }