Commit 156167267cea558e47354d830e8c4fd61768bed4
1 parent
b63f2e52
Exists in
master
and in
3 other branches
Refactoring action list engine.
Showing
3 changed files
with
105 additions
and
101 deletions
Show diff stats
src/include/pw3270/actions.h
... | ... | @@ -91,16 +91,14 @@ |
91 | 91 | typedef GSList Pw3270ActionList; |
92 | 92 | |
93 | 93 | typedef enum _pw3270ActionViewFlag { |
94 | - PW3270_ACTION_VIEW_FLAG_FIXED = 0, ///< @brief No special options. | |
95 | - PW3270_ACTION_VIEW_FLAG_ALLOW_ADD = 1, ///< @brief Allow add to target widget. | |
96 | - PW3270_ACTION_VIEW_ALLOW_REMOVE = 2, ///< @brief Allow remove from source widget. | |
97 | - PW3270_ACTION_VIEW_ALLOW_MOVE = 3 ///< @brief Allow move from source to target. | |
94 | + PW3270_ACTION_VIEW_FLAG_FIXED = 0, ///< @brief Don't move to other views. | |
95 | + PW3270_ACTION_VIEW_FLAG_ALLOW_ADD = 1, ///< @brief Allow add to target view. | |
96 | + PW3270_ACTION_VIEW_ALLOW_REMOVE = 2, ///< @brief Allow remove from source view. | |
97 | + PW3270_ACTION_VIEW_ALLOW_MOVE = 3 ///< @brief Allow move from one view to another. | |
98 | 98 | } PW3270ActionViewFlag; |
99 | 99 | |
100 | 100 | |
101 | 101 | GtkWidget * pw3270_action_view_new(); |
102 | - Pw3270ActionList * pw3270_action_list_new(GtkApplication *application); | |
103 | - void pw3270_action_list_free(Pw3270ActionList *action_list); | |
104 | 102 | void pw3270_action_view_set_actions(GtkWidget *view, Pw3270ActionList *list); |
105 | 103 | void pw3270_action_view_order_by_label(GtkWidget *view); |
106 | 104 | void pw3270_action_view_move_selected(GtkWidget *from, GtkWidget *to); |
... | ... | @@ -108,7 +106,10 @@ |
108 | 106 | gchar * pw3270_action_view_get_action_names(GtkWidget *widget); |
109 | 107 | GtkWidget * pw3270_action_view_extract_button_new(GtkWidget *widget, const gchar *icon_name); |
110 | 108 | |
109 | + Pw3270ActionList * pw3270_action_list_new(GtkApplication *application); | |
110 | + Pw3270ActionList * pw3270_action_list_append(Pw3270ActionList *action_list, const gchar *label, GdkPixbuf *pixbuf, const gchar *action_name, const PW3270ActionViewFlag flags); | |
111 | 111 | Pw3270ActionList * pw3270_action_list_move_action(Pw3270ActionList *action_list, const gchar *action_name, GtkWidget *view); |
112 | + void pw3270_action_list_free(Pw3270ActionList *action_list); | |
112 | 113 | |
113 | 114 | // |
114 | 115 | // Tools | ... | ... |
src/objects/actions/view.c
... | ... | @@ -45,9 +45,10 @@ |
45 | 45 | }; |
46 | 46 | |
47 | 47 | struct ListElement { |
48 | - GAction * action; | |
48 | +// GAction * action; | |
49 | 49 | GdkPixbuf * pixbuf; |
50 | - gchar name[1]; | |
50 | + gchar * action_name; | |
51 | + gchar * action_label; | |
51 | 52 | }; |
52 | 53 | |
53 | 54 | static void list_element_free(struct ListElement *element); |
... | ... | @@ -138,59 +139,18 @@ |
138 | 139 | |
139 | 140 | static void pw3270_action_view_append_element(GtkListStore * store, struct ListElement * element) { |
140 | 141 | |
141 | - size_t ix; | |
142 | - | |
143 | - struct Properties { | |
144 | - const gchar * name; | |
145 | - GType g_type; | |
146 | - GValue value; | |
147 | - } properties[] = { | |
148 | - { | |
149 | - .name = "label", | |
150 | - .g_type = G_TYPE_STRING, | |
151 | - .value = G_VALUE_INIT | |
152 | - } | |
153 | - }; | |
154 | - | |
155 | - for(ix = 0; ix < G_N_ELEMENTS(properties); ix++) { | |
156 | - | |
157 | - g_value_init(&properties[ix].value, properties[ix].g_type); | |
158 | - g_object_get_property(G_OBJECT(element->action), properties[ix].name, &properties[ix].value); | |
159 | - | |
160 | - } | |
161 | - | |
162 | - // Remove "_" | |
163 | - g_autofree gchar * label = g_strdup(g_value_get_string(&properties[0].value)); | |
164 | - | |
165 | - if(label) { | |
166 | - | |
167 | - gchar *from, *to; | |
168 | - | |
169 | - for(from=to=label;*from;from++) { | |
170 | - if(*from != '_') { | |
171 | - *(to++) = *from; | |
172 | - } | |
173 | - } | |
174 | - *to = 0; | |
175 | - | |
176 | - } | |
177 | - | |
178 | 142 | GtkTreeIter iter; |
179 | 143 | gtk_list_store_append(store, &iter); |
180 | 144 | gtk_list_store_set( |
181 | 145 | store, |
182 | 146 | &iter, |
183 | 147 | COLUMN_PIXBUF, element->pixbuf, |
184 | - COLUMN_LABEL, (label ? label : g_action_get_name(element->action)), | |
185 | - COLUMN_ACTION_NAME, element->name, | |
148 | + COLUMN_LABEL, element->action_label, | |
149 | + COLUMN_ACTION_NAME, element->action_name, | |
186 | 150 | COLUMN_FLAGS, (gint) PW3270_ACTION_VIEW_ALLOW_MOVE, |
187 | 151 | -1 |
188 | 152 | ); |
189 | 153 | |
190 | - for(ix = 0; ix < G_N_ELEMENTS(properties); ix++) { | |
191 | - g_value_unset(&properties[ix].value); | |
192 | - } | |
193 | - | |
194 | 154 | } |
195 | 155 | |
196 | 156 | Pw3270ActionList * pw3270_action_list_move_action(Pw3270ActionList *action_list, const gchar *action_name, GtkWidget *view) { |
... | ... | @@ -201,8 +161,7 @@ |
201 | 161 | while(item) { |
202 | 162 | |
203 | 163 | struct ListElement * element = (struct ListElement *) item->data; |
204 | - | |
205 | - if(!g_ascii_strcasecmp(action_name,element->name)) { | |
164 | + if(!g_ascii_strcasecmp(action_name,element->action_name)) { | |
206 | 165 | |
207 | 166 | pw3270_action_view_append_element(store, element); |
208 | 167 | list_element_free(element); |
... | ... | @@ -230,36 +189,38 @@ |
230 | 189 | |
231 | 190 | } |
232 | 191 | |
233 | - static GSList * append_action(GSList * list, const gchar *type, GAction *action) { | |
192 | + static GSList * append_action(GSList * list, const gchar *prefix, GAction *action) { | |
234 | 193 | |
235 | 194 | if(!action) |
236 | 195 | return list; |
237 | 196 | |
238 | - if(!g_object_class_find_property(G_OBJECT_GET_CLASS(action),"label")) { | |
239 | - debug("Action \"%s\": Doesn't have a label",g_action_get_name(action)); | |
240 | - return list; | |
241 | - } | |
242 | - | |
243 | 197 | GdkPixbuf * pixbuf = g_action_get_pixbuf(action, GTK_ICON_SIZE_MENU, GTK_ICON_LOOKUP_GENERIC_FALLBACK); |
244 | 198 | if(!pixbuf) { |
245 | 199 | debug("Action \"%s\": Doesn't have a pixbuf",g_action_get_name(action)); |
246 | 200 | return list; |
247 | 201 | } |
248 | 202 | |
249 | - const gchar *name = g_action_get_name(action); | |
250 | - | |
251 | - struct ListElement * element = (struct ListElement *) g_malloc0(sizeof(struct ListElement) + strlen(type) + strlen(name)); | |
203 | + g_autofree gchar *action_name = g_strconcat(prefix,".",g_action_get_name(action),NULL); | |
252 | 204 | |
253 | - strcpy(element->name,type); | |
254 | - strcat(element->name,name); | |
205 | + GValue value = G_VALUE_INIT; | |
206 | + g_value_init(&value, G_TYPE_STRING); | |
207 | + g_object_get_property(G_OBJECT(action),"label",&value); | |
255 | 208 | |
256 | - element->action = action; | |
209 | + const gchar * label = g_value_get_string(&value); | |
210 | + if(!(label && *label)) | |
211 | + label = g_action_get_name(action); | |
257 | 212 | |
258 | - element->pixbuf = pixbuf; | |
259 | - g_object_ref_sink(G_OBJECT(element->pixbuf)); | |
213 | + list = pw3270_action_list_append( | |
214 | + list, | |
215 | + label, | |
216 | + pixbuf, | |
217 | + action_name, | |
218 | + PW3270_ACTION_VIEW_ALLOW_MOVE | |
219 | + ); | |
260 | 220 | |
261 | - return g_slist_prepend(list,element); | |
221 | + g_value_unset(&value); | |
262 | 222 | |
223 | + return list; | |
263 | 224 | } |
264 | 225 | |
265 | 226 | Pw3270ActionList * pw3270_action_list_new(GtkApplication *application) { |
... | ... | @@ -273,7 +234,7 @@ |
273 | 234 | |
274 | 235 | action_names = g_action_group_list_actions(G_ACTION_GROUP(application)); |
275 | 236 | for(ix = 0; action_names[ix];ix++) { |
276 | - list = append_action(list,"app.",g_action_map_lookup_action(G_ACTION_MAP(application),action_names[ix])); | |
237 | + list = append_action(list,"app",g_action_map_lookup_action(G_ACTION_MAP(application),action_names[ix])); | |
277 | 238 | } |
278 | 239 | g_strfreev(action_names); |
279 | 240 | |
... | ... | @@ -285,7 +246,7 @@ |
285 | 246 | // Get window actions. |
286 | 247 | action_names = g_action_group_list_actions(G_ACTION_GROUP(window)); |
287 | 248 | for(ix = 0; action_names[ix];ix++) { |
288 | - list = append_action(list,"win.",g_action_map_lookup_action(G_ACTION_MAP(window),action_names[ix])); | |
249 | + list = append_action(list,"win",g_action_map_lookup_action(G_ACTION_MAP(window),action_names[ix])); | |
289 | 250 | } |
290 | 251 | g_strfreev(action_names); |
291 | 252 | |
... | ... | @@ -294,6 +255,32 @@ |
294 | 255 | return (Pw3270ActionList *) list; |
295 | 256 | } |
296 | 257 | |
258 | + Pw3270ActionList * pw3270_action_list_append(Pw3270ActionList *action_list, const gchar *label, GdkPixbuf *pixbuf, const gchar *action_name, const PW3270ActionViewFlag flags) { | |
259 | + | |
260 | + struct ListElement * element = (struct ListElement *) | |
261 | + g_malloc0( | |
262 | + sizeof(struct ListElement) | |
263 | + + strlen(action_name) | |
264 | + + strlen(label) | |
265 | + + 3 | |
266 | + ); | |
267 | + | |
268 | + if(pixbuf) { | |
269 | + element->pixbuf = pixbuf; | |
270 | + g_object_ref_sink(G_OBJECT(element->pixbuf)); | |
271 | + } | |
272 | + | |
273 | + element->action_name = (char *) (element+1); | |
274 | + strcpy(element->action_name,action_name); | |
275 | + | |
276 | + element->action_label = element->action_name + strlen(element->action_name) + 1; | |
277 | + strcpy(element->action_label,label); | |
278 | + | |
279 | + return (Pw3270ActionList *) g_slist_prepend((GSList *) action_list, element); | |
280 | + | |
281 | + | |
282 | + }; | |
283 | + | |
297 | 284 | void list_element_free(struct ListElement *element) { |
298 | 285 | |
299 | 286 | if(element->pixbuf) { | ... | ... |
src/objects/window/header-settings.c
... | ... | @@ -236,6 +236,52 @@ |
236 | 236 | // Populate views |
237 | 237 | Pw3270ActionList * action_list = pw3270_action_list_new(GTK_APPLICATION(g_application_get_default())); |
238 | 238 | |
239 | + // Add standard menus | |
240 | + { | |
241 | + static const struct menu { | |
242 | + const gchar * action_name; | |
243 | + const gchar * label; | |
244 | + const gchar * icon_name; | |
245 | + } menus[] = { | |
246 | + { | |
247 | + .action_name = "menu.open-menu", | |
248 | + .label = N_("Application menu"), | |
249 | + .icon_name = "open-menu-symbolic" | |
250 | + } | |
251 | + }; | |
252 | + | |
253 | + size_t ix; | |
254 | + | |
255 | + for(ix = 0; ix < G_N_ELEMENTS(menus); ix++) { | |
256 | + | |
257 | + GError *error = NULL; | |
258 | + | |
259 | + GdkPixbuf * pixbuf = gtk_icon_theme_load_icon( | |
260 | + gtk_icon_theme_get_default(), | |
261 | + menus[ix].icon_name, | |
262 | + GTK_ICON_SIZE_MENU, | |
263 | + GTK_ICON_LOOKUP_GENERIC_FALLBACK, | |
264 | + &error | |
265 | + ); | |
266 | + | |
267 | + if(error) { | |
268 | + g_warning(error->message); | |
269 | + g_error_free(error); | |
270 | + error = NULL; | |
271 | + } | |
272 | + | |
273 | + action_list = pw3270_action_list_append( | |
274 | + action_list, | |
275 | + gettext(menus[ix].label), | |
276 | + pixbuf, | |
277 | + menus[ix].action_name, | |
278 | + PW3270_ACTION_VIEW_ALLOW_MOVE | |
279 | + ); | |
280 | + } | |
281 | + | |
282 | + } | |
283 | + | |
284 | + // Load settings | |
239 | 285 | g_autofree gchar * action_names = g_settings_get_string(settings,"header-action-names"); |
240 | 286 | gchar **views = g_strsplit(action_names,":",-1); |
241 | 287 | |
... | ... | @@ -247,37 +293,7 @@ |
247 | 293 | gchar ** actions = g_strsplit(views[view],",",-1); |
248 | 294 | |
249 | 295 | for(action = 0; actions[action];action++) { |
250 | - | |
251 | - if(g_str_has_prefix(actions[action],"menu.")) { | |
252 | - | |
253 | - GError *error = NULL; | |
254 | - g_autofree gchar * icon_name = g_strconcat(actions[action]+5,"-symbolic",NULL); | |
255 | - | |
256 | - GdkPixbuf * pixbuf = gtk_icon_theme_load_icon( | |
257 | - gtk_icon_theme_get_default(), | |
258 | - icon_name, | |
259 | - GTK_ICON_SIZE_MENU, | |
260 | - GTK_ICON_LOOKUP_GENERIC_FALLBACK, | |
261 | - &error | |
262 | - ); | |
263 | - | |
264 | - if(error) { | |
265 | - g_warning(error->message); | |
266 | - g_error_free(error); | |
267 | - error = NULL; | |
268 | - } | |
269 | - | |
270 | - pw3270_action_view_append( | |
271 | - page->views[view], // Widget | |
272 | - _( "System Menu" ), // label | |
273 | - pixbuf, // Icon | |
274 | - actions[action], // Action name | |
275 | - PW3270_ACTION_VIEW_FLAG_FIXED // Fixed item | |
276 | - ); | |
277 | - | |
278 | - } else { | |
279 | - action_list = pw3270_action_list_move_action(action_list,actions[action],page->views[view]); | |
280 | - } | |
296 | + action_list = pw3270_action_list_move_action(action_list,actions[action],page->views[view]); | |
281 | 297 | } |
282 | 298 | |
283 | 299 | g_strfreev(actions); | ... | ... |