From 156167267cea558e47354d830e8c4fd61768bed4 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Thu, 17 Sep 2020 13:43:56 -0300 Subject: [PATCH] Refactoring action list engine. --- src/include/pw3270/actions.h | 13 +++++++------ src/objects/actions/view.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------- src/objects/window/header-settings.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++------------------------------- 3 files changed, 105 insertions(+), 101 deletions(-) diff --git a/src/include/pw3270/actions.h b/src/include/pw3270/actions.h index f949678..0c29b0e 100644 --- a/src/include/pw3270/actions.h +++ b/src/include/pw3270/actions.h @@ -91,16 +91,14 @@ typedef GSList Pw3270ActionList; typedef enum _pw3270ActionViewFlag { - PW3270_ACTION_VIEW_FLAG_FIXED = 0, ///< @brief No special options. - PW3270_ACTION_VIEW_FLAG_ALLOW_ADD = 1, ///< @brief Allow add to target widget. - PW3270_ACTION_VIEW_ALLOW_REMOVE = 2, ///< @brief Allow remove from source widget. - PW3270_ACTION_VIEW_ALLOW_MOVE = 3 ///< @brief Allow move from source to target. + PW3270_ACTION_VIEW_FLAG_FIXED = 0, ///< @brief Don't move to other views. + PW3270_ACTION_VIEW_FLAG_ALLOW_ADD = 1, ///< @brief Allow add to target view. + PW3270_ACTION_VIEW_ALLOW_REMOVE = 2, ///< @brief Allow remove from source view. + PW3270_ACTION_VIEW_ALLOW_MOVE = 3 ///< @brief Allow move from one view to another. } PW3270ActionViewFlag; GtkWidget * pw3270_action_view_new(); - Pw3270ActionList * pw3270_action_list_new(GtkApplication *application); - void pw3270_action_list_free(Pw3270ActionList *action_list); void pw3270_action_view_set_actions(GtkWidget *view, Pw3270ActionList *list); void pw3270_action_view_order_by_label(GtkWidget *view); void pw3270_action_view_move_selected(GtkWidget *from, GtkWidget *to); @@ -108,7 +106,10 @@ gchar * pw3270_action_view_get_action_names(GtkWidget *widget); GtkWidget * pw3270_action_view_extract_button_new(GtkWidget *widget, const gchar *icon_name); + Pw3270ActionList * pw3270_action_list_new(GtkApplication *application); + Pw3270ActionList * pw3270_action_list_append(Pw3270ActionList *action_list, const gchar *label, GdkPixbuf *pixbuf, const gchar *action_name, const PW3270ActionViewFlag flags); Pw3270ActionList * pw3270_action_list_move_action(Pw3270ActionList *action_list, const gchar *action_name, GtkWidget *view); + void pw3270_action_list_free(Pw3270ActionList *action_list); // // Tools diff --git a/src/objects/actions/view.c b/src/objects/actions/view.c index 404d036..f079531 100644 --- a/src/objects/actions/view.c +++ b/src/objects/actions/view.c @@ -45,9 +45,10 @@ }; struct ListElement { - GAction * action; +// GAction * action; GdkPixbuf * pixbuf; - gchar name[1]; + gchar * action_name; + gchar * action_label; }; static void list_element_free(struct ListElement *element); @@ -138,59 +139,18 @@ static void pw3270_action_view_append_element(GtkListStore * store, struct ListElement * element) { - size_t ix; - - struct Properties { - const gchar * name; - GType g_type; - GValue value; - } properties[] = { - { - .name = "label", - .g_type = G_TYPE_STRING, - .value = G_VALUE_INIT - } - }; - - for(ix = 0; ix < G_N_ELEMENTS(properties); ix++) { - - g_value_init(&properties[ix].value, properties[ix].g_type); - g_object_get_property(G_OBJECT(element->action), properties[ix].name, &properties[ix].value); - - } - - // Remove "_" - g_autofree gchar * label = g_strdup(g_value_get_string(&properties[0].value)); - - if(label) { - - gchar *from, *to; - - for(from=to=label;*from;from++) { - if(*from != '_') { - *(to++) = *from; - } - } - *to = 0; - - } - GtkTreeIter iter; gtk_list_store_append(store, &iter); gtk_list_store_set( store, &iter, COLUMN_PIXBUF, element->pixbuf, - COLUMN_LABEL, (label ? label : g_action_get_name(element->action)), - COLUMN_ACTION_NAME, element->name, + COLUMN_LABEL, element->action_label, + COLUMN_ACTION_NAME, element->action_name, COLUMN_FLAGS, (gint) PW3270_ACTION_VIEW_ALLOW_MOVE, -1 ); - for(ix = 0; ix < G_N_ELEMENTS(properties); ix++) { - g_value_unset(&properties[ix].value); - } - } Pw3270ActionList * pw3270_action_list_move_action(Pw3270ActionList *action_list, const gchar *action_name, GtkWidget *view) { @@ -201,8 +161,7 @@ while(item) { struct ListElement * element = (struct ListElement *) item->data; - - if(!g_ascii_strcasecmp(action_name,element->name)) { + if(!g_ascii_strcasecmp(action_name,element->action_name)) { pw3270_action_view_append_element(store, element); list_element_free(element); @@ -230,36 +189,38 @@ } - static GSList * append_action(GSList * list, const gchar *type, GAction *action) { + static GSList * append_action(GSList * list, const gchar *prefix, GAction *action) { if(!action) return list; - if(!g_object_class_find_property(G_OBJECT_GET_CLASS(action),"label")) { - debug("Action \"%s\": Doesn't have a label",g_action_get_name(action)); - return list; - } - GdkPixbuf * pixbuf = g_action_get_pixbuf(action, GTK_ICON_SIZE_MENU, GTK_ICON_LOOKUP_GENERIC_FALLBACK); if(!pixbuf) { debug("Action \"%s\": Doesn't have a pixbuf",g_action_get_name(action)); return list; } - const gchar *name = g_action_get_name(action); - - struct ListElement * element = (struct ListElement *) g_malloc0(sizeof(struct ListElement) + strlen(type) + strlen(name)); + g_autofree gchar *action_name = g_strconcat(prefix,".",g_action_get_name(action),NULL); - strcpy(element->name,type); - strcat(element->name,name); + GValue value = G_VALUE_INIT; + g_value_init(&value, G_TYPE_STRING); + g_object_get_property(G_OBJECT(action),"label",&value); - element->action = action; + const gchar * label = g_value_get_string(&value); + if(!(label && *label)) + label = g_action_get_name(action); - element->pixbuf = pixbuf; - g_object_ref_sink(G_OBJECT(element->pixbuf)); + list = pw3270_action_list_append( + list, + label, + pixbuf, + action_name, + PW3270_ACTION_VIEW_ALLOW_MOVE + ); - return g_slist_prepend(list,element); + g_value_unset(&value); + return list; } Pw3270ActionList * pw3270_action_list_new(GtkApplication *application) { @@ -273,7 +234,7 @@ action_names = g_action_group_list_actions(G_ACTION_GROUP(application)); for(ix = 0; action_names[ix];ix++) { - list = append_action(list,"app.",g_action_map_lookup_action(G_ACTION_MAP(application),action_names[ix])); + list = append_action(list,"app",g_action_map_lookup_action(G_ACTION_MAP(application),action_names[ix])); } g_strfreev(action_names); @@ -285,7 +246,7 @@ // Get window actions. action_names = g_action_group_list_actions(G_ACTION_GROUP(window)); for(ix = 0; action_names[ix];ix++) { - list = append_action(list,"win.",g_action_map_lookup_action(G_ACTION_MAP(window),action_names[ix])); + list = append_action(list,"win",g_action_map_lookup_action(G_ACTION_MAP(window),action_names[ix])); } g_strfreev(action_names); @@ -294,6 +255,32 @@ return (Pw3270ActionList *) list; } + Pw3270ActionList * pw3270_action_list_append(Pw3270ActionList *action_list, const gchar *label, GdkPixbuf *pixbuf, const gchar *action_name, const PW3270ActionViewFlag flags) { + + struct ListElement * element = (struct ListElement *) + g_malloc0( + sizeof(struct ListElement) + + strlen(action_name) + + strlen(label) + + 3 + ); + + if(pixbuf) { + element->pixbuf = pixbuf; + g_object_ref_sink(G_OBJECT(element->pixbuf)); + } + + element->action_name = (char *) (element+1); + strcpy(element->action_name,action_name); + + element->action_label = element->action_name + strlen(element->action_name) + 1; + strcpy(element->action_label,label); + + return (Pw3270ActionList *) g_slist_prepend((GSList *) action_list, element); + + + }; + void list_element_free(struct ListElement *element) { if(element->pixbuf) { diff --git a/src/objects/window/header-settings.c b/src/objects/window/header-settings.c index bebbe8b..5824036 100644 --- a/src/objects/window/header-settings.c +++ b/src/objects/window/header-settings.c @@ -236,6 +236,52 @@ // Populate views Pw3270ActionList * action_list = pw3270_action_list_new(GTK_APPLICATION(g_application_get_default())); + // Add standard menus + { + static const struct menu { + const gchar * action_name; + const gchar * label; + const gchar * icon_name; + } menus[] = { + { + .action_name = "menu.open-menu", + .label = N_("Application menu"), + .icon_name = "open-menu-symbolic" + } + }; + + size_t ix; + + for(ix = 0; ix < G_N_ELEMENTS(menus); ix++) { + + GError *error = NULL; + + GdkPixbuf * pixbuf = gtk_icon_theme_load_icon( + gtk_icon_theme_get_default(), + menus[ix].icon_name, + GTK_ICON_SIZE_MENU, + GTK_ICON_LOOKUP_GENERIC_FALLBACK, + &error + ); + + if(error) { + g_warning(error->message); + g_error_free(error); + error = NULL; + } + + action_list = pw3270_action_list_append( + action_list, + gettext(menus[ix].label), + pixbuf, + menus[ix].action_name, + PW3270_ACTION_VIEW_ALLOW_MOVE + ); + } + + } + + // Load settings g_autofree gchar * action_names = g_settings_get_string(settings,"header-action-names"); gchar **views = g_strsplit(action_names,":",-1); @@ -247,37 +293,7 @@ gchar ** actions = g_strsplit(views[view],",",-1); for(action = 0; actions[action];action++) { - - if(g_str_has_prefix(actions[action],"menu.")) { - - GError *error = NULL; - g_autofree gchar * icon_name = g_strconcat(actions[action]+5,"-symbolic",NULL); - - GdkPixbuf * pixbuf = gtk_icon_theme_load_icon( - gtk_icon_theme_get_default(), - icon_name, - GTK_ICON_SIZE_MENU, - GTK_ICON_LOOKUP_GENERIC_FALLBACK, - &error - ); - - if(error) { - g_warning(error->message); - g_error_free(error); - error = NULL; - } - - pw3270_action_view_append( - page->views[view], // Widget - _( "System Menu" ), // label - pixbuf, // Icon - actions[action], // Action name - PW3270_ACTION_VIEW_FLAG_FIXED // Fixed item - ); - - } else { - action_list = pw3270_action_list_move_action(action_list,actions[action],page->views[view]); - } + action_list = pw3270_action_list_move_action(action_list,actions[action],page->views[view]); } g_strfreev(actions); -- libgit2 0.21.2