[planner: 22/61] project-properties: Port phase selector to GtkComboBoxText




commit 1d10d167f963d0315ac3044601a4f543597169f2
Author: Mart Raudsepp <leio gentoo org>
Date:   Sun Mar 8 20:11:23 2020 +0200

    project-properties: Port phase selector to GtkComboBoxText
    
    GtkOptionMenu (used before) has been deprecated for most of gtk2 times
    and does not exist in gtk3.

 data/ui/project-properties.ui    |   6 +-
 src/planner-project-properties.c | 128 ++++++++++++++-------------------------
 2 files changed, 48 insertions(+), 86 deletions(-)
---
diff --git a/data/ui/project-properties.ui b/data/ui/project-properties.ui
index 94399957..3a7135d8 100644
--- a/data/ui/project-properties.ui
+++ b/data/ui/project-properties.ui
@@ -330,7 +330,7 @@
                     <property name="yalign">0.5</property>
                     <property name="xpad">0</property>
                     <property name="ypad">0</property>
-                    <property name="mnemonic_widget">optionmenu_phase</property>
+                    <property name="mnemonic_widget">comboboxtext_phase</property>
                     <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
                     <property name="width_chars">-1</property>
                     <property name="single_line_mode">False</property>
@@ -346,10 +346,10 @@
                   </packing>
                 </child>
                 <child>
-                  <object class="GtkOptionMenu" id="optionmenu_phase">
+                  <object class="GtkComboBoxText" id="comboboxtext_phase">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
-                    <property name="history">-1</property>
+                    <property name="entry-text-column">0</property>
                   </object>
                   <packing>
                     <property name="left_attach">1</property>
diff --git a/src/planner-project-properties.c b/src/planner-project-properties.c
index 9cd48071..d82cffd2 100644
--- a/src/planner-project-properties.c
+++ b/src/planner-project-properties.c
@@ -48,7 +48,7 @@ typedef struct {
        GtkWidget     *manager_entry;
        GtkWidget     *start_entry;
        GtkWidget     *start_button;
-       GtkWidget     *phase_option_menu;
+       GtkWidget     *phase_combo_box_text;
        GtkWidget     *calendar_label;
 
        GtkWidget     *calendar;
@@ -132,13 +132,11 @@ static void     mpp_project_calendar_notify_cb        (MrpProject          *proj
 static void     mpp_project_calendar_notify_cb        (MrpProject          *project,
                                                       GParamSpec          *pspec,
                                                       GtkWidget           *dialog);
-static void     mpp_phase_option_menu_changed_cb      (GtkOptionMenu       *option_menu,
+static void     mpp_phase_combo_box_text_changed_cb   (GtkComboBoxText     *combo_box_text,
                                                       GtkWidget           *dialog);
 static void     mpp_project_phases_notify_cb          (MrpProject          *project,
                                                       GParamSpec          *pspec,
                                                       GtkWidget           *dialog);
-static void     mpp_phase_set_from_widget             (GtkWidget           *dialog);
-
 static void     mpp_project_phase_notify_cb           (MrpProject          *project,
                                                       GParamSpec          *pspec,
                                                       GtkWidget           *dialog);
@@ -800,10 +798,15 @@ mpp_project_calendar_notify_cb (MrpProject *project,
 }
 
 static void
-mpp_phase_option_menu_changed_cb (GtkOptionMenu *option_menu,
-                                 GtkWidget     *dialog)
+mpp_phase_combo_box_text_changed_cb (GtkComboBoxText *combo_box_text,
+                                    GtkWidget       *dialog)
 {
-       mpp_phase_set_from_widget (dialog);
+       property_cmd_edit (DIALOG_GET_DATA (dialog),
+                          _("Edit Project Phase"), PROP_STRING, "phase",
+                          gtk_combo_box_get_active (GTK_COMBO_BOX (combo_box_text))
+                          ? gtk_combo_box_text_get_active_text (combo_box_text)
+                          : NULL, /* First item is always "None", meaning no phase is selected */
+                          0, NULL);
 }
 
 static void
@@ -813,35 +816,17 @@ mpp_project_phases_notify_cb (MrpProject  *project,
 {
        DialogData *data = DIALOG_GET_DATA (dialog);
 
-       g_signal_handlers_block_by_func (data->phase_option_menu,
-                                        mpp_phase_option_menu_changed_cb,
+       g_signal_handlers_block_by_func (data->phase_combo_box_text,
+                                        mpp_phase_combo_box_text_changed_cb,
                                         dialog);
 
        mpp_setup_phases (data);
 
-       g_signal_handlers_unblock_by_func (data->phase_option_menu,
-                                          mpp_phase_option_menu_changed_cb,
+       g_signal_handlers_unblock_by_func (data->phase_combo_box_text,
+                                          mpp_phase_combo_box_text_changed_cb,
                                           dialog);
 }
 
-static void
-mpp_phase_set_from_widget (GtkWidget *dialog)
-{
-       DialogData  *data;
-       GtkWidget    *menu;
-       GtkWidget    *item;
-       const gchar *phase;
-
-       data = DIALOG_GET_DATA (dialog);
-
-               menu = gtk_option_menu_get_menu (GTK_OPTION_MENU (data->phase_option_menu));
-       item = gtk_menu_get_active (GTK_MENU (menu));
-
-       phase = g_object_get_data (G_OBJECT (item), "data");
-
-       property_cmd_edit (data, _("Edit Project Phase"), PROP_STRING, "phase", phase, 0, NULL);
-}
-
 static void
 mpp_project_phase_notify_cb (MrpProject  *project,
                             GParamSpec  *pspec,
@@ -860,81 +845,58 @@ mpp_project_phase_notify_cb (MrpProject  *project,
 static void
 mpp_setup_phases (DialogData *data)
 {
-       GtkOptionMenu *option_menu;
-       GList         *phases, *l;
-       GtkWidget     *menu;
-       GtkWidget     *menu_item;
-       gchar         *phase;
+       GtkComboBoxText *combo_text;
+       GList           *phases, *l;
+       gchar           *phase;
 
        phases = NULL;
 
-       g_object_get (data->project, "phases", &phases, NULL);
-
-       option_menu = GTK_OPTION_MENU (data->phase_option_menu);
-       menu = gtk_option_menu_get_menu (option_menu);
-
-       if (menu) {
-               gtk_widget_destroy (menu);
-       }
+       g_object_get (data->project,
+                     "phases", &phases,
+                     "phase", &phase,
+                     NULL);
 
-       menu = gtk_menu_new ();
+       combo_text = GTK_COMBO_BOX_TEXT (data->phase_combo_box_text);
+       /* TODO-GTK3: Use gtk_combo_box_text_remove_all */
+       gtk_list_store_clear (GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combo_text))));
 
-       menu_item = gtk_menu_item_new_with_label (_("None"));
-       gtk_widget_show (menu_item);
-       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
+       gtk_combo_box_text_append_text (combo_text, _("None"));
 
        for (l = phases; l; l = l->next) {
-               menu_item = gtk_menu_item_new_with_label (l->data);
-               gtk_widget_show (menu_item);
-               gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
-
-               g_object_set_data_full (G_OBJECT (menu_item),
-                                       "data",
-                                       g_strdup (l->data),
-                                       g_free);
+               gtk_combo_box_text_append_text (combo_text, (l->data));
        }
 
-       gtk_widget_show (menu);
-       gtk_option_menu_set_menu (option_menu, menu);
-
-       mrp_string_list_free (phases);
-
-       g_object_get (data->project, "phase", &phase, NULL);
        mpp_set_phase (data, phase);
+
        g_free (phase);
+       mrp_string_list_free (phases);
 }
 
 static void
 mpp_set_phase (DialogData  *data,
               const gchar *phase)
 {
-       GtkOptionMenu *option_menu;
-       GtkWidget     *menu;
-       GtkWidget     *item;
-       GList         *children, *l;
-       gint           i;
-       gchar         *name;
-
-       option_menu = GTK_OPTION_MENU (data->phase_option_menu);
-
        if (phase) {
-               menu = gtk_option_menu_get_menu (option_menu);
-
-               children = GTK_MENU_SHELL (menu)->children;
-               for (i = 0, l = children; l; i++, l = l->next) {
-                       item = l->data;
-
-                       name = g_object_get_data (G_OBJECT (item), "data");
-
-                       if (name && !strcmp (phase, name)) {
-                               gtk_option_menu_set_history (option_menu, i);
+               GtkComboBox  *combo;
+               GtkTreeModel *model;
+               GtkTreeIter   iter;
+               gchar        *name;
+
+               combo = GTK_COMBO_BOX (data->phase_combo_box_text);
+               model = gtk_combo_box_get_model (combo);
+               gtk_tree_model_get_iter_first (model, &iter);
+
+               while (gtk_tree_model_iter_next (model, &iter)) { /* Skips the first "None" item */
+                       gtk_tree_model_get (model, &iter, 0, &name, -1);
+                       if (!strcmp (phase, name)) {
+                               gtk_combo_box_set_active_iter (combo, &iter);
                                return;
                        }
                }
        }
 
        /* If we didn't match, set "None". */
-       gtk_option_menu_set_history (option_menu, 0);
+       gtk_combo_box_set_active (GTK_COMBO_BOX (data->phase_combo_box_text), 0);
 }
 
 static void
@@ -1576,7 +1538,7 @@ planner_project_properties_new (PlannerWindow *window)
        hbox = GTK_WIDGET (gtk_builder_get_object (builder, "hbox_start"));
        data->start_entry = GTK_WIDGET (gtk_builder_get_object (builder, "entry_start"));
        data->start_button = planner_popup_button_new (_("Date..."));
-       data->phase_option_menu = GTK_WIDGET (gtk_builder_get_object (builder, "optionmenu_phase"));
+       data->phase_combo_box_text = GTK_WIDGET (gtk_builder_get_object (builder, "comboboxtext_phase"));
        data->calendar_label = GTK_WIDGET (gtk_builder_get_object (builder, "label_calendar"));
        data->properties_tree = GTK_TREE_VIEW (gtk_builder_get_object (builder, "properties_tree"));
 
@@ -1640,9 +1602,9 @@ planner_project_properties_new (PlannerWindow *window)
        /* Project phase. */
        mpp_setup_phases (data);
 
-       g_signal_connect (data->phase_option_menu,
+       g_signal_connect (data->phase_combo_box_text,
                          "changed",
-                         G_CALLBACK (mpp_phase_option_menu_changed_cb),
+                         G_CALLBACK (mpp_phase_combo_box_text_changed_cb),
                          dialog);
        g_signal_connect (data->add_property_button,
                          "clicked",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]