diff --git a/demos/clipboard.c b/demos/clipboard.c index 9321f0b..7d5a172 100644 --- a/demos/clipboard.c +++ b/demos/clipboard.c @@ -6,6 +6,10 @@ struct demo { struct gral_window *window; }; +static void destroy(void *user_data) { + +} + static int close(void *user_data) { return 1; } @@ -81,6 +85,7 @@ static void focus_leave(void *user_data) { static void create_window(void *user_data) { struct demo *demo = user_data; struct gral_window_interface interface = { + &destroy, &close, &draw, &resize, @@ -122,7 +127,6 @@ int main(int argc, char **argv) { struct gral_application_interface interface = {&start, &open_empty, &open_file, &quit}; demo.application = gral_application_create("com.github.eyelash.libgral.demos.clipboard", &interface, &demo); int result = gral_application_run(demo.application, argc, argv); - gral_window_delete(demo.window); gral_application_delete(demo.application); return result; } diff --git a/demos/cursors.c b/demos/cursors.c index dd1d302..0ca0b19 100644 --- a/demos/cursors.c +++ b/demos/cursors.c @@ -7,6 +7,10 @@ struct demo { int cursor; }; +static void destroy(void *user_data) { + +} + static int close(void *user_data) { return 1; } @@ -107,6 +111,7 @@ static void focus_leave(void *user_data) { static void create_window(void *user_data) { struct demo *demo = user_data; struct gral_window_interface interface = { + &destroy, &close, &draw, &resize, @@ -149,7 +154,6 @@ int main(int argc, char **argv) { struct gral_application_interface interface = {&start, &open_empty, &open_file, &quit}; demo.application = gral_application_create("com.github.eyelash.libgral.demos.cursors", &interface, &demo); int result = gral_application_run(demo.application, argc, argv); - gral_window_delete(demo.window); gral_application_delete(demo.application); return result; } diff --git a/demos/draw.c b/demos/draw.c index 4caa7d7..c6fcac6 100644 --- a/demos/draw.c +++ b/demos/draw.c @@ -9,6 +9,10 @@ struct demo { struct gral_window *window; }; +static void destroy(void *user_data) { + +} + static int close(void *user_data) { return 1; } @@ -149,6 +153,7 @@ static void focus_leave(void *user_data) { static void create_window(void *user_data) { struct demo *demo = user_data; struct gral_window_interface interface = { + &destroy, &close, &draw, &resize, @@ -191,7 +196,6 @@ int main(int argc, char **argv) { struct gral_application_interface interface = {&start, &open_empty, &open_file, &quit}; demo.application = gral_application_create("com.github.eyelash.libgral.demos.draw", &interface, &demo); int result = gral_application_run(demo.application, argc, argv); - gral_window_delete(demo.window); gral_application_delete(demo.application); return result; } diff --git a/demos/events.c b/demos/events.c index b48e4ad..7000605 100644 --- a/demos/events.c +++ b/demos/events.c @@ -9,6 +9,10 @@ struct demo { struct gral_midi *midi; }; +static void destroy(void *user_data) { + printf("destroy\n"); +} + static int close(void *user_data) { printf("close\n"); return 1; @@ -122,6 +126,7 @@ static void control_change(unsigned char controller, unsigned char value, void * static void create_window(void *user_data) { struct demo *demo = user_data; struct gral_window_interface interface = { + &destroy, &close, &draw, &resize, @@ -171,7 +176,6 @@ int main(int argc, char **argv) { struct gral_application_interface interface = {&start, &open_empty, &open_file, &quit}; demo.application = gral_application_create("com.github.eyelash.libgral.demos.events", &interface, &demo); int result = gral_application_run(demo.application, argc, argv); - gral_window_delete(demo.window); gral_application_delete(demo.application); return result; } diff --git a/demos/file_dialogs.c b/demos/file_dialogs.c index bc7ee5a..6c83050 100644 --- a/demos/file_dialogs.c +++ b/demos/file_dialogs.c @@ -7,6 +7,10 @@ struct demo { struct gral_window *window; }; +static void destroy(void *user_data) { + +} + static int close(void *user_data) { return 1; } @@ -91,6 +95,7 @@ static void focus_leave(void *user_data) { static void create_window(void *user_data) { struct demo *demo = user_data; struct gral_window_interface interface = { + &destroy, &close, &draw, &resize, @@ -133,7 +138,6 @@ int main(int argc, char **argv) { struct gral_application_interface interface = {&start, &open_empty, &open_file, &quit}; demo.application = gral_application_create("com.github.eyelash.libgral.demos.file_dialogs", &interface, &demo); int result = gral_application_run(demo.application, argc, argv); - gral_window_delete(demo.window); gral_application_delete(demo.application); return result; } diff --git a/demos/text.c b/demos/text.c index 0cab9f9..0315cae 100644 --- a/demos/text.c +++ b/demos/text.c @@ -9,6 +9,10 @@ struct demo { float descent; }; +static void destroy(void *user_data) { + +} + static int close(void *user_data) { return 1; } @@ -96,6 +100,7 @@ static void focus_leave(void *user_data) { static void create_window(void *user_data) { struct demo *demo = user_data; struct gral_window_interface interface = { + &destroy, &close, &draw, &resize, @@ -146,7 +151,6 @@ int main(int argc, char **argv) { demo.application = gral_application_create("com.github.eyelash.libgral.demos.text", &interface, &demo); int result = gral_application_run(demo.application, argc, argv); gral_text_delete(demo.text); - gral_window_delete(demo.window); gral_application_delete(demo.application); return result; } diff --git a/gral.h b/gral.h index a58dd67..a33c73c 100644 --- a/gral.h +++ b/gral.h @@ -78,6 +78,7 @@ struct gral_gradient_stop { struct gral_draw_context; struct gral_window; struct gral_window_interface { + void (*destroy)(void *user_data); int (*close)(void *user_data); void (*draw)(struct gral_draw_context *draw_context, int x, int y, int width, int height, void *user_data); void (*resize)(int width, int height, void *user_data); @@ -154,7 +155,6 @@ void gral_draw_context_draw_transformed(struct gral_draw_context *draw_context, ===========*/ struct gral_window *gral_window_create(struct gral_application *application, int width, int height, char const *title, struct gral_window_interface const *interface, void *user_data); -void gral_window_delete(struct gral_window *window); void gral_window_set_title(struct gral_window *window, char const *title); void gral_window_request_redraw(struct gral_window *window, int x, int y, int width, int height); void gral_window_set_minimum_size(struct gral_window *window, int minimum_width, int minimum_height); diff --git a/gral_linux.c b/gral_linux.c index 2f721b4..db02139 100644 --- a/gral_linux.c +++ b/gral_linux.c @@ -307,12 +307,19 @@ static gboolean gral_window_delete_event(GtkWidget *widget, GdkEventAny *event) GralWindow *window = GRAL_WINDOW(widget); return !window->interface.close(window->user_data); } +static void gral_window_finalize(GObject *object) { + GralWindow *window = GRAL_WINDOW(object); + window->interface.destroy(window->user_data); + G_OBJECT_CLASS(gral_window_parent_class)->finalize(object); +} static void gral_window_init(GralWindow *window) { } static void gral_window_class_init(GralWindowClass *class) { GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(class); widget_class->delete_event = gral_window_delete_event; + GObjectClass *object_class = G_OBJECT_CLASS(class); + object_class->finalize = gral_window_finalize; } #define GRAL_TYPE_AREA gral_area_get_type() @@ -496,7 +503,6 @@ static void gral_area_class_init(GralAreaClass *class) { struct gral_window *gral_window_create(struct gral_application *application, int width, int height, char const *title, struct gral_window_interface const *interface, void *user_data) { GralWindow *window = g_object_new(GRAL_TYPE_WINDOW, "application", application, NULL); - g_object_ref_sink(window); window->interface = *interface; window->user_data = user_data; window->is_cursor_hidden = FALSE; @@ -510,10 +516,6 @@ struct gral_window *gral_window_create(struct gral_application *application, int return (struct gral_window *)window; } -void gral_window_delete(struct gral_window *window) { - g_object_unref(window); -} - void gral_window_set_title(struct gral_window *window, char const *title) { gtk_window_set_title(GTK_WINDOW(window), title); } diff --git a/gral_macos.m b/gral_macos.m index d199864..fe4e0db 100644 --- a/gral_macos.m +++ b/gral_macos.m @@ -411,6 +411,10 @@ - (void)windowDidBecomeKey:(NSNotification *)notification { - (void)windowDidResignKey:(NSNotification *)notification { interface.focus_leave(user_data); } +- (void)dealloc { + interface.destroy(user_data); + [super dealloc]; +} @end @interface GralView: NSView { @@ -583,10 +587,6 @@ - (void)doCommandBySelector:(SEL)selector { return (struct gral_window *)window; } -void gral_window_delete(struct gral_window *window) { - [(GralWindow *)window release]; -} - void gral_window_set_title(struct gral_window *window, char const *title) { [(GralWindow *)window setTitle:[NSString stringWithUTF8String:title]]; } diff --git a/gral_windows.cpp b/gral_windows.cpp index d8fcc77..3ba1b9c 100644 --- a/gral_windows.cpp +++ b/gral_windows.cpp @@ -487,6 +487,7 @@ static LRESULT CALLBACK window_procedure(HWND hwnd, UINT uMsg, WPARAM wParam, LP } case WM_DESTROY: { + window_data->iface.destroy(window_data->user_data); if (window_data->target) { window_data->target->Release(); } @@ -930,10 +931,6 @@ gral_window *gral_window_create(gral_application *application, int width, int he return (gral_window *)hwnd; } -void gral_window_delete(gral_window *window) { - -} - void gral_window_set_title(gral_window *window, char const *title) { SetWindowText((HWND)window, utf8_to_utf16(title)); }