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 7a4b445
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 36 deletions.
42 changes: 21 additions & 21 deletions demos/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
add_executable(audio audio.c)
target_link_libraries(audio gral)

add_executable(clipboard clipboard.c)
target_link_libraries(clipboard gral)

add_executable(cursors cursors.c)
target_link_libraries(cursors gral)

add_executable(draw draw.c)
target_link_libraries(draw gral)

#add_executable(audio audio.c)
#target_link_libraries(audio gral)
#
#add_executable(clipboard clipboard.c)
#target_link_libraries(clipboard gral)
#
#add_executable(cursors cursors.c)
#target_link_libraries(cursors gral)
#
#add_executable(draw draw.c)
#target_link_libraries(draw gral)
#
add_executable(events events.c)
target_link_libraries(events gral)

add_executable(file file.c)
target_link_libraries(file gral)

add_executable(file_dialogs file_dialogs.c)
target_link_libraries(file_dialogs gral)

add_executable(text text.c)
target_link_libraries(text gral)
#
#add_executable(file file.c)
#target_link_libraries(file gral)
#
#add_executable(file_dialogs file_dialogs.c)
#target_link_libraries(file_dialogs gral)
#
#add_executable(text text.c)
#target_link_libraries(text gral)
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;
}
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
9 changes: 5 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 @@ -580,13 +584,10 @@ - (void)doCommandBySelector:(SEL)selector {
[window setContentView:view];
[view release];
[window makeKeyAndOrderFront:nil];
[window release];
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 7a4b445

Please sign in to comment.