Skip to content

Commit

Permalink
replace gral_window_delete with a destroy signal
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Sep 19, 2024
1 parent d7f8924 commit 50b738b
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 20 deletions.
6 changes: 5 additions & 1 deletion demos/clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ struct demo {
struct gral_window *window;
};

static void destroy(void *user_data) {

}

static int close(void *user_data) {
return 1;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion demos/cursors.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ struct demo {
int cursor;
};

static void destroy(void *user_data) {

}

static int close(void *user_data) {
return 1;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion demos/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ struct demo {
struct gral_window *window;
};

static void destroy(void *user_data) {

}

static int close(void *user_data) {
return 1;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion demos/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion demos/file_dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ struct demo {
struct gral_window *window;
};

static void destroy(void *user_data) {

}

static int close(void *user_data) {
return 1;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion demos/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ struct demo {
float descent;
};

static void destroy(void *user_data) {

}

static int close(void *user_data) {
return 1;
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion gral.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions gral_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions gral_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSTextInputClient> {
Expand Down Expand Up @@ -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]];
}
Expand Down
5 changes: 1 addition & 4 deletions gral_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 50b738b

Please sign in to comment.