From 62d8ad067c5d864b8aa8965900490a77c0280643 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Fri, 25 Oct 2019 13:20:36 -0300 Subject: [PATCH] Implementing toolbar. --- src/include/pw3270/toolbar.h | 2 ++ src/objects/toolbar/actions.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- src/objects/window/window.c | 57 +++++++++++++++++++++++++++++++++------------------------ 3 files changed, 183 insertions(+), 41 deletions(-) diff --git a/src/include/pw3270/toolbar.h b/src/include/pw3270/toolbar.h index 0d8e546..0ba85e0 100644 --- a/src/include/pw3270/toolbar.h +++ b/src/include/pw3270/toolbar.h @@ -58,6 +58,8 @@ GtkWidget * pw3270_toolbar_insert_lib3270_action(GtkWidget *toolbar, const LIB3270_ACTION *action, gint pos); GtkWidget * pw3270_toolbar_insert_action(GtkWidget *toolbar, GAction *action, gint pos); + GtkWidget * pw3270_toolbar_insert_action_by_name(GtkWidget *toolbar, const gchar *name, gint pos); + G_END_DECLS diff --git a/src/objects/toolbar/actions.c b/src/objects/toolbar/actions.c index 0d7bb2c..18c45d2 100644 --- a/src/objects/toolbar/actions.c +++ b/src/objects/toolbar/actions.c @@ -30,33 +30,164 @@ #include "private.h" #include + static GtkWidget * pw3270_tool_button_new(GAction *action) { + + const gchar * action_name = g_action_get_name(action); + debug("action=%s enabled=%s type=%s",action_name,g_action_get_enabled(action) ? "Yes" : "No", g_action_get_parameter_type(action)); + + if(PW3270_IS_ACTION(action)) { + + // It's a pw3270 action, get attributes from it. + + const gchar * icon_name = pw3270_action_get_icon_name(action); + if(!icon_name) { + g_message("Action doesn't have an icon"); + return NULL; + } + + debug("%s - %s",icon_name,pw3270_action_get_label(action)); + + GtkToolItem * item = gtk_tool_button_new( + gtk_image_new_from_icon_name(icon_name,GTK_ICON_SIZE_LARGE_TOOLBAR), + pw3270_action_get_label(action) + ); + + const gchar * tooltip = pw3270_action_get_tooltip(action); + if(tooltip) + gtk_widget_set_tooltip_markup(GTK_WIDGET(item),tooltip); + + return GTK_WIDGET(item); + + } + + // + // Check for my Actions. + // + + static const struct _actions { + const gchar * name; + const gchar * icon; + const gchar * label; + const gchar * tooltip; + } actions[] = { + + { + .name = "win.connect", + .icon = "gtk-connect", + .label = N_("Connect"), + .tooltip = N_("Connect to host") + }, + + { + .name = "win.close", + .icon = "window-close", + .label = N_("Close"), + .tooltip = N_("Close window") + } + + }; + + size_t ix; + for(ix = 0; ix < G_N_ELEMENTS(actions); ix++) { + + if(!g_ascii_strcasecmp(action_name,actions[ix].name)) { + + GtkToolItem * item = gtk_tool_button_new( + gtk_image_new_from_icon_name(actions[ix].icon,GTK_ICON_SIZE_LARGE_TOOLBAR), + gettext(actions[ix].label) + ); + + if(actions[ix].tooltip) + gtk_widget_set_tooltip_markup(GTK_WIDGET(item),gettext(actions[ix].tooltip)); + + return GTK_WIDGET(item); + } + + } + + g_warning("Action \"%s\" can't be inserted on toolbar",action_name); + + return NULL; + + } + + static void clicked(GtkToolButton G_GNUC_UNUSED(*toolbutton), GAction *action) { + + if(g_action_get_enabled(action)) { + g_action_activate(action,NULL); + } +#ifdef DEBUG + else { + debug("Action \"%s\" is disabled",g_action_get_name(action)); + } +#endif // DEBUG + + } + + static void notify(GAction *action, GParamSpec *pspec, GtkWidget *item) { + if(!strcmp(pspec->name,"enabled")) + gtk_widget_set_sensitive(item,g_action_get_enabled(action)); + } + GtkWidget * pw3270_toolbar_insert_action(GtkWidget *toolbar, GAction *action, gint pos) { - debug("toolbar=%p action=%p",toolbar,action); + g_return_val_if_fail(PW3270_IS_TOOLBAR(toolbar),NULL); - g_return_val_if_fail(PW3270_IS_ACTION(action) && PW3270_IS_TOOLBAR(toolbar),NULL); + GtkWidget * item = pw3270_tool_button_new(action); - const gchar * icon_name = pw3270_action_get_icon_name(action); - if(!icon_name) { - g_message("Action doesn't have an icon"); + if(!item) return NULL; + + gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item),TRUE); + gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(item), pos); + gtk_widget_show_all(GTK_WIDGET(item)); + + g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(clicked),action); + g_signal_connect(G_OBJECT(action),"notify",G_CALLBACK(notify),item); + + return item; + + } + + GtkWidget * pw3270_toolbar_insert_action_by_name(GtkWidget *toolbar, const gchar *name, gint pos) { + + g_return_val_if_fail(PW3270_IS_TOOLBAR(toolbar),NULL); + + if(!g_ascii_strcasecmp(name,"")) { + + GtkToolItem * item = gtk_separator_tool_item_new(); + + gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(item),FALSE); + gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, pos); + gtk_widget_show_all(GTK_WIDGET(item)); + + return GTK_WIDGET(item); + + } else if(!g_ascii_strcasecmp(name,"separator")) { + + GtkToolItem * item = gtk_separator_tool_item_new(); + + gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(item),TRUE); + gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, pos); + gtk_widget_show_all(GTK_WIDGET(item)); + + return GTK_WIDGET(item); + } - debug("%s - %s",icon_name,pw3270_action_get_label(action)); + GtkWidget * window = gtk_widget_get_toplevel(toolbar); - GtkToolItem * item = gtk_tool_button_new( - gtk_image_new_from_icon_name(icon_name,GTK_ICON_SIZE_LARGE_TOOLBAR), - pw3270_action_get_label(action) - ); + if(window) { - gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item),TRUE); + GAction *action = g_action_map_lookup_action(G_ACTION_MAP(window), name); - gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, pos); - gtk_widget_show_all(GTK_WIDGET(item)); + if(action) + return pw3270_toolbar_insert_action(toolbar, action, pos); - const gchar * tooltip = pw3270_action_get_tooltip(action); - if(tooltip) - gtk_widget_set_tooltip_markup(GTK_WIDGET(item),tooltip); + g_warning("Can't find action \"%s\"",name); - return GTK_WIDGET(item); + } + + return NULL; } + diff --git a/src/objects/window/window.c b/src/objects/window/window.c index b769f39..247fae9 100644 --- a/src/objects/window/window.c +++ b/src/objects/window/window.c @@ -68,44 +68,28 @@ // pw3270_window_add_actions(GTK_WIDGET(widget)); - { - static const gchar *actions[] = { - "win.select_all", - "win.copy", - "win.paste", - "win.reconnect", - "win.disconnect", - "win.print", - "app.quit" - }; - - size_t ix; - - for(ix = 0; ix < G_N_ELEMENTS(actions); ix++) { - pw3270_toolbar_insert_action(GTK_WIDGET(widget->toolbar), g_action_map_lookup_action(G_ACTION_MAP(widget), actions[ix]), -1); - } - - } - - - // // Setup Window actions. // static GActionEntry actions[] = { { - .name = "open", + .name = "win.open", .activate = pw3270_application_generic_activated, }, { - .name = "close", + .name = "win.close", .activate = pw3270_application_generic_activated, }, { - .name = "preferences", + .name = "win.connect", + .activate = pw3270_application_generic_activated, + }, + + { + .name = "win.preferences", .activate = pw3270_application_generic_activated, }, @@ -118,6 +102,31 @@ widget ); + // + // Setup toolbar + // + { + static const gchar *actions[] = { + "win.select_all", + "win.copy", + "win.paste", + "separator", + "win.connect", + "win.disconnect", + "separator", + "win.print", + "win.close" + }; + + size_t ix; + + for(ix = 0; ix < G_N_ELEMENTS(actions); ix++) { + pw3270_toolbar_insert_action_by_name(GTK_WIDGET(widget->toolbar),actions[ix],-1); + } + + } + + } GtkWidget * pw3270_application_window_new(GtkApplication * application) { -- libgit2 0.21.2