2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2022 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "bar-rating.h"
24 #include <glib-object.h>
40 * Rating values as specified by: \n
41 * Adobe XMP Basic namespace \n
51 GtkWidget *radio_button_first;
53 GtkCheckButton *rating_buttons[7];
56 static void bar_pane_rating_update(PaneRatingData *prd)
60 rating = metadata_read_int(prd->fd, RATING_KEY, 0) + 1;
62 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prd->rating_buttons[rating]), TRUE);
65 static void bar_pane_rating_set_fd(GtkWidget *pane, FileData *fd)
69 prd = static_cast<PaneRatingData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
72 file_data_unref(prd->fd);
73 prd->fd = file_data_ref(fd);
75 bar_pane_rating_update(prd);
78 static void bar_pane_rating_write_config(GtkWidget *pane, GString *outstr, gint indent)
82 prd = static_cast<PaneRatingData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
86 WRITE_STRING("<pane_rating ");
87 write_char_option(outstr, indent, "id", prd->pane.id);
88 write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(prd->pane.title)));
89 WRITE_BOOL(prd->pane, expanded);
93 static void bar_pane_rating_notify_cb(FileData *fd, NotifyType type, gpointer data)
95 auto prd = static_cast<PaneRatingData *>(data);
97 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == prd->fd)
99 DEBUG_1("Notify pane_rating: %s %04x", fd->path, type);
100 bar_pane_rating_update(prd);
104 static void bar_pane_rating_destroy(GtkWidget *, gpointer data)
106 auto prd = static_cast<PaneRatingData *>(data);
108 file_data_unregister_notify_func(bar_pane_rating_notify_cb, prd);
109 file_data_unref(prd->fd);
110 g_free(prd->pane.id);
114 static void bar_pane_rating_selected_cb(GtkCheckButton *checkbutton, gpointer data)
116 auto prd = static_cast<PaneRatingData *>(data);
120 const gchar *rating_label;
122 rating_label = gtk_check_button_get_label(checkbutton);
124 if (g_strcmp0(rating_label, "Rejected") == 0)
126 rating = g_strdup("-1");
128 else if (g_strcmp0(rating_label, "Unrated") == 0)
130 rating = g_strdup("0");
134 rating = g_strdup(rating_label);
137 metadata_write_string(prd->fd, RATING_KEY, rating);
143 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton)))
148 if (prd->rating_buttons[i] == checkbutton)
150 rating = g_strdup_printf("%d", i - 1 );
151 metadata_write_string(prd->fd, RATING_KEY, rating);
162 static GtkWidget *bar_pane_rating_new(const gchar *id, const gchar *title, gboolean expanded)
165 GtkWidget *radio_rejected;
166 GtkWidget *radio_unrated;
167 GtkWidget *radio_rating;
173 prd = g_new0(PaneRatingData, 1);
175 prd->pane.pane_set_fd = bar_pane_rating_set_fd;
176 prd->pane.pane_write_config = bar_pane_rating_write_config;
177 prd->pane.title = bar_pane_expander_title(title);
178 prd->pane.id = g_strdup(id);
179 prd->pane.type = PANE_RATING;
181 prd->pane.expanded = expanded;
183 prd->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
185 g_object_set_data(G_OBJECT(prd->widget), "pane_data", prd);
186 g_signal_connect(G_OBJECT(prd->widget), "destroy", G_CALLBACK(bar_pane_rating_destroy), prd);
188 row_1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
189 gq_gtk_box_pack_start(GTK_BOX(prd->widget), row_1, FALSE, FALSE, 0);
192 radio_rejected = gtk_check_button_new_with_label(_("Rejected"));
194 radio_rejected = gtk_radio_button_new_with_label(nullptr, _("Rejected"));
196 gq_gtk_box_pack_start(GTK_BOX(row_1), radio_rejected, FALSE, FALSE, 0);
197 g_signal_connect(radio_rejected, "released", G_CALLBACK(bar_pane_rating_selected_cb), prd);
198 prd->rating_buttons[0] = GTK_CHECK_BUTTON(radio_rejected);
201 radio_unrated = gtk_check_button_new_with_label(_("Unrated"));
202 gtk_check_button_set_group(GTK_CHECK_BUTTON(radio_unrated), GTK_CHECK_BUTTON(radio_rejected));
204 radio_unrated = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_rejected), _("Unrated"));
206 gq_gtk_box_pack_start(GTK_BOX(row_1), radio_unrated, FALSE, FALSE, 0);
207 g_signal_connect(radio_unrated, "released", G_CALLBACK(bar_pane_rating_selected_cb), prd);
208 prd->rating_buttons[1] = GTK_CHECK_BUTTON(radio_unrated);
210 row_2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
211 gq_gtk_box_pack_start(GTK_BOX(prd->widget), row_2, FALSE, FALSE, 0);
216 i_str = g_strdup_printf("%d", i - 1);
219 radio_rating = gtk_check_button_new_with_label(i_str);
220 gtk_check_button_set_group(GTK_CHECK_BUTTON(radio_rating), GTK_CHECK_BUTTON(radio_rejected));
222 radio_rating = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_rejected), i_str);
224 g_signal_connect(radio_rating, "released", G_CALLBACK(bar_pane_rating_selected_cb), prd);
226 gq_gtk_box_pack_start(GTK_BOX(row_2), radio_rating, FALSE, FALSE, 1);
227 prd->rating_buttons[i ] = GTK_CHECK_BUTTON(radio_rating);
233 prd->radio_button_first = radio_rating;
235 gtk_widget_show(prd->widget);
237 file_data_register_notify_func(bar_pane_rating_notify_cb, prd, NOTIFY_PRIORITY_LOW);
242 GtkWidget *bar_pane_rating_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
244 gchar *title = nullptr;
245 gchar *id = g_strdup("rating");
246 gboolean expanded = TRUE;
249 while (*attribute_names)
251 const gchar *option = *attribute_names++;
252 const gchar *value = *attribute_values++;
254 if (READ_CHAR_FULL("id", id)) continue;
255 if (READ_CHAR_FULL("title", title)) continue;
256 if (READ_BOOL_FULL("expanded", expanded)) continue;
258 log_printf("unknown attribute %s = %s\n", option, value);
261 bar_pane_translate_title(PANE_RATING, id, &title);
262 ret = bar_pane_rating_new(id, title, expanded);
269 void bar_pane_rating_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
272 gchar *title = nullptr;
274 prd = static_cast<PaneRatingData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
277 while (*attribute_names)
279 const gchar *option = *attribute_names++;
280 const gchar *value = *attribute_values++;
282 if (READ_CHAR_FULL("title", title)) continue;
283 if (READ_CHAR_FULL("id", prd->pane.id)) continue;
284 if (READ_BOOL_FULL("expanded", prd->pane.expanded)) continue;
286 log_printf("unknown attribute %s = %s\n", option, value);
289 bar_update_expander(pane);
290 bar_pane_rating_update(prd);
292 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */