GTK4: GtkRadioButton
authorColin Clark <colin.clark@cclark.uk>
Tue, 26 Sep 2023 15:18:36 +0000 (16:18 +0100)
committerColin Clark <colin.clark@cclark.uk>
Tue, 26 Sep 2023 15:18:36 +0000 (16:18 +0100)
GTK4 migration

- GtkRadioButton Replaced by GTkToggleButton

src/bar-rating.cc
src/layout-config.cc
src/ui-misc.cc

index 49c5667..54f9252 100644 (file)
@@ -43,18 +43,16 @@ struct PaneRatingData
        GtkWidget *widget;
        GtkWidget *radio_button_first;
        FileData *fd;
+       GtkCheckButton *rating_buttons[7];
 };
 
 static void bar_pane_rating_update(PaneRatingData *prd)
 {
        guint64 rating;
-       GSList *list;
 
-       rating = metadata_read_int(prd->fd, RATING_KEY, 0);
+       rating = metadata_read_int(prd->fd, RATING_KEY, 0) + 1;
 
-       list = gtk_radio_button_get_group(GTK_RADIO_BUTTON(prd->radio_button_first));
-
-       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(g_slist_nth_data(list, 5 - rating)), TRUE);
+       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prd->rating_buttons[rating]), TRUE);
 }
 
 static void bar_pane_rating_set_fd(GtkWidget *pane, FileData *fd)
@@ -106,32 +104,52 @@ static void bar_pane_rating_destroy(GtkWidget *, gpointer data)
        g_free(prd);
 }
 
-static void bar_pane_rating_selected_cb(GtkToggleButton *togglebutton, gpointer data)
+static void bar_pane_rating_selected_cb(GtkCheckButton *checkbutton, gpointer data)
 {
        auto prd = static_cast<PaneRatingData *>(data);
-       GSList *list;
-       gint i;
        gchar *rating;
 
-       if (gtk_toggle_button_get_active(togglebutton))
+#ifdef HAVE_GTK4
+       const gchar *rating_label;
+
+       rating_label = gtk_check_button_get_label(checkbutton);
+
+       if (g_strcmp0(rating_label, "Rejected") == 0)
                {
-               list = gtk_radio_button_get_group(GTK_RADIO_BUTTON(togglebutton));
-               i = 0;
+               rating = g_strdup("-1");
+               }
+       else if (g_strcmp0(rating_label, "Unrated") == 0)
+               {
+               rating = g_strdup("0");
+               }
+       else
+               {
+               rating = g_strdup(rating_label);
+               }
 
-               while (list)
+       metadata_write_string(prd->fd, RATING_KEY, rating);
+
+       g_free(rating);
+#else
+       gint i;
+
+       if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton)))
+               {
+               i = 0;
+               while (i < 7)
                        {
-                       if (list->data == togglebutton)
+                       if (prd->rating_buttons[i] == checkbutton)
                                {
-                               rating = g_strdup_printf("%d", 5 - i);
+                               rating = g_strdup_printf("%d",   i - 1 );
                                metadata_write_string(prd->fd, RATING_KEY, rating);
                                g_free(rating);
                                break;
                                }
 
                        i++;
-                       list = list->next;
                        }
                }
+#endif
 }
 
 static GtkWidget *bar_pane_rating_new(const gchar *id, const gchar *title, gboolean expanded)
@@ -163,26 +181,43 @@ static GtkWidget *bar_pane_rating_new(const gchar *id, const gchar *title, gbool
        row_1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
        gq_gtk_box_pack_start(GTK_BOX(prd->widget), row_1, FALSE, FALSE, 0);
 
+#ifdef HAVE_GTK4
+       radio_rejected = gtk_check_button_new_with_label(_("Rejected"));
+#else
        radio_rejected = gtk_radio_button_new_with_label(nullptr, _("Rejected"));
+#endif
        gq_gtk_box_pack_start(GTK_BOX(row_1), radio_rejected, FALSE, FALSE, 0);
        g_signal_connect(radio_rejected, "released", G_CALLBACK(bar_pane_rating_selected_cb), prd);
+       prd->rating_buttons[0] = GTK_CHECK_BUTTON(radio_rejected);
 
+#ifdef HAVE_GTK4
+       radio_unrated = gtk_check_button_new_with_label(_("Unrated"));
+       gtk_check_button_set_group(GTK_CHECK_BUTTON(radio_unrated), GTK_CHECK_BUTTON(radio_rejected));
+#else
        radio_unrated = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_rejected), _("Unrated"));
+#endif
        gq_gtk_box_pack_start(GTK_BOX(row_1), radio_unrated, FALSE, FALSE, 0);
        g_signal_connect(radio_unrated, "released", G_CALLBACK(bar_pane_rating_selected_cb), prd);
+       prd->rating_buttons[1] = GTK_CHECK_BUTTON(radio_unrated);
 
        row_2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
        gq_gtk_box_pack_start(GTK_BOX(prd->widget), row_2, FALSE, FALSE, 0);
 
-       i = 1;
-       while (i <= 5)
+       i = 2;
+       while (i <= 6)
                {
-               i_str = g_strdup_printf("%d", i);
+               i_str = g_strdup_printf("%d", i - 1);
 
+#ifdef HAVE_GTK4
+               radio_rating = gtk_check_button_new_with_label(i_str);
+               gtk_check_button_set_group(GTK_CHECK_BUTTON(radio_rating), GTK_CHECK_BUTTON(radio_rejected));
+#else
                radio_rating = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_rejected), i_str);
+#endif
                g_signal_connect(radio_rating, "released", G_CALLBACK(bar_pane_rating_selected_cb), prd);
 
                gq_gtk_box_pack_start(GTK_BOX(row_2), radio_rating, FALSE, FALSE, 1);
+               prd->rating_buttons[i ] = GTK_CHECK_BUTTON(radio_rating);
 
                g_free(i_str);
                i++;
index 932a5f3..620ca7a 100644 (file)
@@ -242,11 +242,20 @@ static GtkWidget *layout_config_widget(GtkWidget *group, GtkWidget *box, gint st
 
        if (group)
                {
+#ifdef HAVE_GTK4
+               group = gtk_toggle_button_new();
+               gtk_toggle_button_set_group(button, group);
+#else
                group = gtk_radio_button_new(gtk_radio_button_get_group(GTK_RADIO_BUTTON(group)));
+#endif
                }
        else
                {
+#ifdef HAVE_GTK4
+               group = gtk_toggle_button_new();
+#else
                group = gtk_radio_button_new(nullptr);
+#endif
                }
        g_object_set_data(G_OBJECT(group), "layout_config", lc);
        g_signal_connect(G_OBJECT(group), "clicked",
index 6536cd4..64982ba 100644 (file)
@@ -354,11 +354,19 @@ static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *si
                                            GCallback func, gpointer data)
 {
        GtkWidget *button;
+#ifdef HAVE_GTK4
+       GtkToggleButton *group;;
+#else
        GSList *group;
+#endif
 
        if (sibling)
                {
+#ifdef HAVE_GTK4
+               group = sibling;
+#else
                group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(sibling));
+#endif
                }
        else
                {
@@ -367,11 +375,21 @@ static GtkWidget *real_pref_radiobutton_new(GtkWidget *parent_box, GtkWidget *si
 
        if (mnemonic_text)
                {
+#ifdef HAVE_GTK4
+               button = gtk_toggle_button_new_with_mnemonic(text);
+               gtk_toggle_button_set_group(button, group);
+#else
                button = gtk_radio_button_new_with_mnemonic(group, text);
+#endif
                }
        else
                {
+#ifdef HAVE_GTK4
+               button = gtk_toggle_button_new_with_label(text);
+               gtk_toggle_button_set_group(button, group);
+#else
                button = gtk_radio_button_new_with_label(group, text);
+#endif
                }
 
        if (active) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), active);