From 8aae05fefcb5d512896a2925452af657bddfbb9f Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 26 Jan 2023 01:38:51 +0100 Subject: [PATCH] Cleanup and added events --- include/ui.h | 22 ++++- src/ui/{actions.c => events.c} | 64 ++++++++++++- src/ui/window.c | 165 ++++++++++++--------------------- 3 files changed, 139 insertions(+), 112 deletions(-) rename src/ui/{actions.c => events.c} (84%) diff --git a/include/ui.h b/include/ui.h index d91abbb..531d72e 100644 --- a/include/ui.h +++ b/include/ui.h @@ -58,6 +58,10 @@ GemGraphClientApplication *gem_graph_client_application_new(const char *applicat G_END_DECLS +void ui_enable_action(const char *name); + +void ui_disable_action(const char *name); + // // Actions // @@ -120,10 +124,6 @@ static const GActionEntry app_actions[] = { { "toastclose", on_toast_close_action, NULL, NULL, NULL }, }; -void ui_enable_action(const char *name); - -void ui_disable_action(const char *name); - // // Actions responses // @@ -135,6 +135,19 @@ void on_openfile_response_complete(GObject *source_object, GAsyncResult *result, GemGraphClientWindow *self); +// +// General events +// +void on_axis_value_change(GtkAdjustment *adjustment, gpointer data); + +gboolean on_render(GtkGLArea * area, GdkGLContext * context); + +void on_realize(GtkWidget *widget); + +void on_unrealize(GtkWidget *widget); + +void on_close_window(GtkWidget *widget); + /* -------------------------------------------------------------------------- */ // @@ -146,4 +159,5 @@ void ui_set_stack(const char *mode); void ui_send_notification(const char *message); void ui_send_internal_notification(const char *message); void ui_close_internal_notification(void); +void ui_toggle_sidebar(); void ui_setup_glarea(); diff --git a/src/ui/actions.c b/src/ui/events.c similarity index 84% rename from src/ui/actions.c rename to src/ui/events.c index 5ef3441..bd0e358 100644 --- a/src/ui/actions.c +++ b/src/ui/events.c @@ -81,7 +81,7 @@ void on_togglesidebar_action(GSimpleAction *action, g_assert(GEM_GRAPH_CLIENT_IS_APPLICATION(self)); - ui_send_internal_notification("Not implemented !"); + ui_toggle_sidebar(); } void on_editmode_action(GSimpleAction *action, @@ -184,6 +184,68 @@ void on_toast_close_action(GSimpleAction *action, /* -------------------------------------------------------------------------- */ +/* + * General events + */ + +void on_axis_value_change(GtkAdjustment *adjustment, gpointer data) +{ + int axis = GPOINTER_TO_INT(data); + + g_assert(axis >= 0 && axis < N_AXIS); + + /* Update the rotation angle */ + rotation_angles[axis] = gtk_adjustment_get_value(adjustment); + + /* Update the contents of the GL drawing area */ + gtk_widget_queue_draw(gl_area); +} + +gboolean on_render(GtkGLArea * area, GdkGLContext * context) +{ + if(gtk_gl_area_get_error(area) != NULL) + return FALSE; + + graphics_draw(); + + return TRUE; +} + + +/* We need to set up our state when we realize the GtkGLArea widget */ +void on_realize(GtkWidget *widget) +{ + gtk_gl_area_make_current(GTK_GL_AREA(widget)); + + if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) + return; + + graphics_init(); +} + +/* We should tear down the state when unrealizing */ +void on_unrealize(GtkWidget *widget) +{ + gtk_gl_area_make_current(GTK_GL_AREA(widget)); + + if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) + return; + + graphics_shutdown(); +} + +void on_close_window(GtkWidget *widget) +{ + /* Reset the state */ + gl_area = NULL; + + rotation_angles[X_AXIS] = 0.0; + rotation_angles[Y_AXIS] = 0.0; + rotation_angles[Z_AXIS] = 0.0; +} + +/* -------------------------------------------------------------------------- */ + /* * Responses */ diff --git a/src/ui/window.c b/src/ui/window.c index 7bb3827..e874ac8 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -40,111 +40,6 @@ static GemGraphClientWindow *window; /* -------------------------------------------------------------------------- */ -static void on_axis_value_change(GtkAdjustment *adjustment, gpointer data); - -static inline GtkWidget *create_axis_slider(int axis) -{ - GtkWidget *box, *label, *slider; - GtkAdjustment *adj; - const char *text; - - box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); - - switch (axis) { - case X_AXIS: - text = "X"; - break; - - case Y_AXIS: - text = "Y"; - break; - - case Z_AXIS: - text = "Z"; - break; - - default: - g_assert_not_reached(); - } - - label = gtk_label_new(text); - gtk_box_append(GTK_BOX(box), label); - gtk_widget_show(label); - - adj = gtk_adjustment_new(0.0, 0.0, 360.0, 1.0, 12.0, 0.0); - g_signal_connect(adj, "value-changed", - G_CALLBACK(on_axis_value_change), - GINT_TO_POINTER(axis)); - slider = gtk_scale_new(GTK_ORIENTATION_HORIZONTAL, adj); - gtk_box_append(GTK_BOX(box), slider); - gtk_widget_set_hexpand(slider, TRUE); - gtk_widget_show(slider); - - gtk_widget_show(box); - - return box; -} - -/* -------------------------------------------------------------------------- */ - -static void on_axis_value_change(GtkAdjustment *adjustment, gpointer data) -{ - int axis = GPOINTER_TO_INT(data); - - g_assert(axis >= 0 && axis < N_AXIS); - - /* Update the rotation angle */ - rotation_angles[axis] = gtk_adjustment_get_value(adjustment); - - /* Update the contents of the GL drawing area */ - gtk_widget_queue_draw(gl_area); -} - -static gboolean on_render(GtkGLArea * area, GdkGLContext * context) -{ - if(gtk_gl_area_get_error(area) != NULL) - return FALSE; - - graphics_draw(); - - return TRUE; -} - - -/* We need to set up our state when we realize the GtkGLArea widget */ -static void on_realize(GtkWidget *widget) -{ - gtk_gl_area_make_current(GTK_GL_AREA(widget)); - - if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) - return; - - graphics_init(); -} - -/* We should tear down the state when unrealizing */ -static void on_unrealize(GtkWidget *widget) -{ - gtk_gl_area_make_current(GTK_GL_AREA(widget)); - - if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) - return; - - graphics_shutdown(); -} - -static void on_close_window(GtkWidget *widget) -{ - /* Reset the state */ - gl_area = NULL; - - rotation_angles[X_AXIS] = 0.0; - rotation_angles[Y_AXIS] = 0.0; - rotation_angles[Z_AXIS] = 0.0; -} - -/* -------------------------------------------------------------------------- */ - struct _GemGraphClientWindow { GtkApplicationWindow parent_instance; @@ -223,6 +118,10 @@ static void gem_graph_client_window_init(GemGraphClientWindow *self) window = self; + // Launch with sidebar off + ui_toggle_sidebar(); + + // Setup GLArea ui_setup_glarea(); } @@ -300,12 +199,53 @@ void ui_close_internal_notification(void) gtk_revealer_set_reveal_child(window->toast_revealer, false); } +static inline GtkWidget *create_axis_slider(int axis) +{ + GtkWidget *box, *label, *slider; + GtkAdjustment *adj; + const char *text; + + box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); + + switch (axis) { + case X_AXIS: + text = "X"; + break; + + case Y_AXIS: + text = "Y"; + break; + + case Z_AXIS: + text = "Z"; + break; + + default: + g_assert_not_reached(); + } + + label = gtk_label_new(text); + gtk_box_append(GTK_BOX(box), label); + gtk_widget_show(label); + + adj = gtk_adjustment_new(0.0, 0.0, 360.0, 1.0, 12.0, 0.0); + g_signal_connect(adj, "value-changed", + G_CALLBACK(on_axis_value_change), + GINT_TO_POINTER(axis)); + slider = gtk_scale_new(GTK_ORIENTATION_HORIZONTAL, adj); + gtk_box_append(GTK_BOX(box), slider); + gtk_widget_set_hexpand(slider, TRUE); + gtk_widget_show(slider); + + gtk_widget_show(box); + + return box; +} + void ui_setup_glarea() { int i, minor=4, major=0; GtkWidget * controls; - gint height; - gint width; gl_area = GTK_WIDGET(window->run_glarea); controls = GTK_WIDGET(window->run_controls); @@ -337,3 +277,14 @@ void ui_setup_glarea() gtk_gl_area_get_required_version( GTK_GL_AREA(gl_area), &minor, &major); } +void ui_toggle_sidebar(void) +{ + int position = gtk_paned_get_position(window->main_paned); + + if (position != 0) { + gtk_paned_set_position(window->main_paned, 0); + } else { + gtk_paned_set_position(window->main_paned, 300); + } +} +