Commit 62d8ad067c5d864b8aa8965900490a77c0280643

Authored by Perry Werneck
1 parent 8822ca30

Implementing toolbar.

src/include/pw3270/toolbar.h
@@ -58,6 +58,8 @@ @@ -58,6 +58,8 @@
58 58
59 GtkWidget * pw3270_toolbar_insert_lib3270_action(GtkWidget *toolbar, const LIB3270_ACTION *action, gint pos); 59 GtkWidget * pw3270_toolbar_insert_lib3270_action(GtkWidget *toolbar, const LIB3270_ACTION *action, gint pos);
60 GtkWidget * pw3270_toolbar_insert_action(GtkWidget *toolbar, GAction *action, gint pos); 60 GtkWidget * pw3270_toolbar_insert_action(GtkWidget *toolbar, GAction *action, gint pos);
  61 + GtkWidget * pw3270_toolbar_insert_action_by_name(GtkWidget *toolbar, const gchar *name, gint pos);
  62 +
61 63
62 G_END_DECLS 64 G_END_DECLS
63 65
src/objects/toolbar/actions.c
@@ -30,33 +30,164 @@ @@ -30,33 +30,164 @@
30 #include "private.h" 30 #include "private.h"
31 #include <pw3270/actions.h> 31 #include <pw3270/actions.h>
32 32
  33 + static GtkWidget * pw3270_tool_button_new(GAction *action) {
  34 +
  35 + const gchar * action_name = g_action_get_name(action);
  36 + debug("action=%s enabled=%s type=%s",action_name,g_action_get_enabled(action) ? "Yes" : "No", g_action_get_parameter_type(action));
  37 +
  38 + if(PW3270_IS_ACTION(action)) {
  39 +
  40 + // It's a pw3270 action, get attributes from it.
  41 +
  42 + const gchar * icon_name = pw3270_action_get_icon_name(action);
  43 + if(!icon_name) {
  44 + g_message("Action doesn't have an icon");
  45 + return NULL;
  46 + }
  47 +
  48 + debug("%s - %s",icon_name,pw3270_action_get_label(action));
  49 +
  50 + GtkToolItem * item = gtk_tool_button_new(
  51 + gtk_image_new_from_icon_name(icon_name,GTK_ICON_SIZE_LARGE_TOOLBAR),
  52 + pw3270_action_get_label(action)
  53 + );
  54 +
  55 + const gchar * tooltip = pw3270_action_get_tooltip(action);
  56 + if(tooltip)
  57 + gtk_widget_set_tooltip_markup(GTK_WIDGET(item),tooltip);
  58 +
  59 + return GTK_WIDGET(item);
  60 +
  61 + }
  62 +
  63 + //
  64 + // Check for my Actions.
  65 + //
  66 +
  67 + static const struct _actions {
  68 + const gchar * name;
  69 + const gchar * icon;
  70 + const gchar * label;
  71 + const gchar * tooltip;
  72 + } actions[] = {
  73 +
  74 + {
  75 + .name = "win.connect",
  76 + .icon = "gtk-connect",
  77 + .label = N_("Connect"),
  78 + .tooltip = N_("Connect to host")
  79 + },
  80 +
  81 + {
  82 + .name = "win.close",
  83 + .icon = "window-close",
  84 + .label = N_("Close"),
  85 + .tooltip = N_("Close window")
  86 + }
  87 +
  88 + };
  89 +
  90 + size_t ix;
  91 + for(ix = 0; ix < G_N_ELEMENTS(actions); ix++) {
  92 +
  93 + if(!g_ascii_strcasecmp(action_name,actions[ix].name)) {
  94 +
  95 + GtkToolItem * item = gtk_tool_button_new(
  96 + gtk_image_new_from_icon_name(actions[ix].icon,GTK_ICON_SIZE_LARGE_TOOLBAR),
  97 + gettext(actions[ix].label)
  98 + );
  99 +
  100 + if(actions[ix].tooltip)
  101 + gtk_widget_set_tooltip_markup(GTK_WIDGET(item),gettext(actions[ix].tooltip));
  102 +
  103 + return GTK_WIDGET(item);
  104 + }
  105 +
  106 + }
  107 +
  108 + g_warning("Action \"%s\" can't be inserted on toolbar",action_name);
  109 +
  110 + return NULL;
  111 +
  112 + }
  113 +
  114 + static void clicked(GtkToolButton G_GNUC_UNUSED(*toolbutton), GAction *action) {
  115 +
  116 + if(g_action_get_enabled(action)) {
  117 + g_action_activate(action,NULL);
  118 + }
  119 +#ifdef DEBUG
  120 + else {
  121 + debug("Action \"%s\" is disabled",g_action_get_name(action));
  122 + }
  123 +#endif // DEBUG
  124 +
  125 + }
  126 +
  127 + static void notify(GAction *action, GParamSpec *pspec, GtkWidget *item) {
  128 + if(!strcmp(pspec->name,"enabled"))
  129 + gtk_widget_set_sensitive(item,g_action_get_enabled(action));
  130 + }
  131 +
33 GtkWidget * pw3270_toolbar_insert_action(GtkWidget *toolbar, GAction *action, gint pos) { 132 GtkWidget * pw3270_toolbar_insert_action(GtkWidget *toolbar, GAction *action, gint pos) {
34 133
35 - debug("toolbar=%p action=%p",toolbar,action); 134 + g_return_val_if_fail(PW3270_IS_TOOLBAR(toolbar),NULL);
36 135
37 - g_return_val_if_fail(PW3270_IS_ACTION(action) && PW3270_IS_TOOLBAR(toolbar),NULL); 136 + GtkWidget * item = pw3270_tool_button_new(action);
38 137
39 - const gchar * icon_name = pw3270_action_get_icon_name(action);  
40 - if(!icon_name) {  
41 - g_message("Action doesn't have an icon"); 138 + if(!item)
42 return NULL; 139 return NULL;
  140 +
  141 + gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item),TRUE);
  142 + gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(item), pos);
  143 + gtk_widget_show_all(GTK_WIDGET(item));
  144 +
  145 + g_signal_connect(G_OBJECT(item),"clicked",G_CALLBACK(clicked),action);
  146 + g_signal_connect(G_OBJECT(action),"notify",G_CALLBACK(notify),item);
  147 +
  148 + return item;
  149 +
  150 + }
  151 +
  152 + GtkWidget * pw3270_toolbar_insert_action_by_name(GtkWidget *toolbar, const gchar *name, gint pos) {
  153 +
  154 + g_return_val_if_fail(PW3270_IS_TOOLBAR(toolbar),NULL);
  155 +
  156 + if(!g_ascii_strcasecmp(name,"")) {
  157 +
  158 + GtkToolItem * item = gtk_separator_tool_item_new();
  159 +
  160 + gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(item),FALSE);
  161 + gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, pos);
  162 + gtk_widget_show_all(GTK_WIDGET(item));
  163 +
  164 + return GTK_WIDGET(item);
  165 +
  166 + } else if(!g_ascii_strcasecmp(name,"separator")) {
  167 +
  168 + GtkToolItem * item = gtk_separator_tool_item_new();
  169 +
  170 + gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(item),TRUE);
  171 + gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, pos);
  172 + gtk_widget_show_all(GTK_WIDGET(item));
  173 +
  174 + return GTK_WIDGET(item);
  175 +
43 } 176 }
44 177
45 - debug("%s - %s",icon_name,pw3270_action_get_label(action)); 178 + GtkWidget * window = gtk_widget_get_toplevel(toolbar);
46 179
47 - GtkToolItem * item = gtk_tool_button_new(  
48 - gtk_image_new_from_icon_name(icon_name,GTK_ICON_SIZE_LARGE_TOOLBAR),  
49 - pw3270_action_get_label(action)  
50 - ); 180 + if(window) {
51 181
52 - gtk_tool_button_set_use_underline(GTK_TOOL_BUTTON(item),TRUE); 182 + GAction *action = g_action_map_lookup_action(G_ACTION_MAP(window), name);
53 183
54 - gtk_toolbar_insert(GTK_TOOLBAR(toolbar), item, pos);  
55 - gtk_widget_show_all(GTK_WIDGET(item)); 184 + if(action)
  185 + return pw3270_toolbar_insert_action(toolbar, action, pos);
56 186
57 - const gchar * tooltip = pw3270_action_get_tooltip(action);  
58 - if(tooltip)  
59 - gtk_widget_set_tooltip_markup(GTK_WIDGET(item),tooltip); 187 + g_warning("Can't find action \"%s\"",name);
60 188
61 - return GTK_WIDGET(item); 189 + }
  190 +
  191 + return NULL;
62 } 192 }
  193 +
src/objects/window/window.c
@@ -68,44 +68,28 @@ @@ -68,44 +68,28 @@
68 // 68 //
69 pw3270_window_add_actions(GTK_WIDGET(widget)); 69 pw3270_window_add_actions(GTK_WIDGET(widget));
70 70
71 - {  
72 - static const gchar *actions[] = {  
73 - "win.select_all",  
74 - "win.copy",  
75 - "win.paste",  
76 - "win.reconnect",  
77 - "win.disconnect",  
78 - "win.print",  
79 - "app.quit"  
80 - };  
81 -  
82 - size_t ix;  
83 -  
84 - for(ix = 0; ix < G_N_ELEMENTS(actions); ix++) {  
85 - pw3270_toolbar_insert_action(GTK_WIDGET(widget->toolbar), g_action_map_lookup_action(G_ACTION_MAP(widget), actions[ix]), -1);  
86 - }  
87 -  
88 - }  
89 -  
90 -  
91 -  
92 // 71 //
93 // Setup Window actions. 72 // Setup Window actions.
94 // 73 //
95 static GActionEntry actions[] = { 74 static GActionEntry actions[] = {
96 75
97 { 76 {
98 - .name = "open", 77 + .name = "win.open",
99 .activate = pw3270_application_generic_activated, 78 .activate = pw3270_application_generic_activated,
100 }, 79 },
101 80
102 { 81 {
103 - .name = "close", 82 + .name = "win.close",
104 .activate = pw3270_application_generic_activated, 83 .activate = pw3270_application_generic_activated,
105 }, 84 },
106 85
107 { 86 {
108 - .name = "preferences", 87 + .name = "win.connect",
  88 + .activate = pw3270_application_generic_activated,
  89 + },
  90 +
  91 + {
  92 + .name = "win.preferences",
109 .activate = pw3270_application_generic_activated, 93 .activate = pw3270_application_generic_activated,
110 }, 94 },
111 95
@@ -118,6 +102,31 @@ @@ -118,6 +102,31 @@
118 widget 102 widget
119 ); 103 );
120 104
  105 + //
  106 + // Setup toolbar
  107 + //
  108 + {
  109 + static const gchar *actions[] = {
  110 + "win.select_all",
  111 + "win.copy",
  112 + "win.paste",
  113 + "separator",
  114 + "win.connect",
  115 + "win.disconnect",
  116 + "separator",
  117 + "win.print",
  118 + "win.close"
  119 + };
  120 +
  121 + size_t ix;
  122 +
  123 + for(ix = 0; ix < G_N_ELEMENTS(actions); ix++) {
  124 + pw3270_toolbar_insert_action_by_name(GTK_WIDGET(widget->toolbar),actions[ix],-1);
  125 + }
  126 +
  127 + }
  128 +
  129 +
121 } 130 }
122 131
123 GtkWidget * pw3270_application_window_new(GtkApplication * application) { 132 GtkWidget * pw3270_application_window_new(GtkApplication * application) {