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.
23 #include "bar-rating.h"
33 * Rating values as specified by: \n
34 * Adobe XMP Basic namespace \n
44 GtkWidget *radio_button_first;
46 GtkCheckButton *rating_buttons[7];
49 static void bar_pane_rating_update(PaneRatingData *prd)
53 rating = metadata_read_int(prd->fd, RATING_KEY, 0) + 1;
55 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(prd->rating_buttons[rating]), TRUE);
58 static void bar_pane_rating_set_fd(GtkWidget *pane, FileData *fd)
62 prd = static_cast<PaneRatingData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
65 file_data_unref(prd->fd);
66 prd->fd = file_data_ref(fd);
68 bar_pane_rating_update(prd);
71 static void bar_pane_rating_write_config(GtkWidget *pane, GString *outstr, gint indent)
75 prd = static_cast<PaneRatingData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
79 WRITE_STRING("<pane_rating ");
80 write_char_option(outstr, indent, "id", prd->pane.id);
81 write_char_option(outstr, indent, "title", gtk_label_get_text(GTK_LABEL(prd->pane.title)));
82 WRITE_BOOL(prd->pane, expanded);
86 static void bar_pane_rating_notify_cb(FileData *fd, NotifyType type, gpointer data)
88 auto prd = static_cast<PaneRatingData *>(data);
90 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE | NOTIFY_HISTMAP | NOTIFY_PIXBUF)) && fd == prd->fd)
92 DEBUG_1("Notify pane_rating: %s %04x", fd->path, type);
93 bar_pane_rating_update(prd);
97 static void bar_pane_rating_destroy(GtkWidget *, gpointer data)
99 auto prd = static_cast<PaneRatingData *>(data);
101 file_data_unregister_notify_func(bar_pane_rating_notify_cb, prd);
102 file_data_unref(prd->fd);
103 g_free(prd->pane.id);
107 static void bar_pane_rating_selected_cb(GtkCheckButton *checkbutton, gpointer data)
109 auto prd = static_cast<PaneRatingData *>(data);
113 const gchar *rating_label;
115 rating_label = gtk_check_button_get_label(checkbutton);
117 if (g_strcmp0(rating_label, "Rejected") == 0)
119 rating = g_strdup("-1");
121 else if (g_strcmp0(rating_label, "Unrated") == 0)
123 rating = g_strdup("0");
127 rating = g_strdup(rating_label);
130 metadata_write_string(prd->fd, RATING_KEY, rating);
136 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton)))
141 if (prd->rating_buttons[i] == checkbutton)
143 rating = g_strdup_printf("%d", i - 1 );
144 metadata_write_string(prd->fd, RATING_KEY, rating);
155 static GtkWidget *bar_pane_rating_new(const gchar *id, const gchar *title, gboolean expanded)
158 GtkWidget *radio_rejected;
159 GtkWidget *radio_unrated;
160 GtkWidget *radio_rating;
166 prd = g_new0(PaneRatingData, 1);
168 prd->pane.pane_set_fd = bar_pane_rating_set_fd;
169 prd->pane.pane_write_config = bar_pane_rating_write_config;
170 prd->pane.title = bar_pane_expander_title(title);
171 prd->pane.id = g_strdup(id);
172 prd->pane.type = PANE_RATING;
174 prd->pane.expanded = expanded;
176 prd->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
178 g_object_set_data(G_OBJECT(prd->widget), "pane_data", prd);
179 g_signal_connect(G_OBJECT(prd->widget), "destroy", G_CALLBACK(bar_pane_rating_destroy), prd);
181 row_1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
182 gq_gtk_box_pack_start(GTK_BOX(prd->widget), row_1, FALSE, FALSE, 0);
185 radio_rejected = gtk_check_button_new_with_label(_("Rejected"));
187 radio_rejected = gtk_radio_button_new_with_label(nullptr, _("Rejected"));
189 gq_gtk_box_pack_start(GTK_BOX(row_1), radio_rejected, FALSE, FALSE, 0);
190 g_signal_connect(radio_rejected, "released", G_CALLBACK(bar_pane_rating_selected_cb), prd);
191 prd->rating_buttons[0] = GTK_CHECK_BUTTON(radio_rejected);
194 radio_unrated = gtk_check_button_new_with_label(_("Unrated"));
195 gtk_check_button_set_group(GTK_CHECK_BUTTON(radio_unrated), GTK_CHECK_BUTTON(radio_rejected));
197 radio_unrated = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_rejected), _("Unrated"));
199 gq_gtk_box_pack_start(GTK_BOX(row_1), radio_unrated, FALSE, FALSE, 0);
200 g_signal_connect(radio_unrated, "released", G_CALLBACK(bar_pane_rating_selected_cb), prd);
201 prd->rating_buttons[1] = GTK_CHECK_BUTTON(radio_unrated);
203 row_2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
204 gq_gtk_box_pack_start(GTK_BOX(prd->widget), row_2, FALSE, FALSE, 0);
209 i_str = g_strdup_printf("%d", i - 1);
212 radio_rating = gtk_check_button_new_with_label(i_str);
213 gtk_check_button_set_group(GTK_CHECK_BUTTON(radio_rating), GTK_CHECK_BUTTON(radio_rejected));
215 radio_rating = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio_rejected), i_str);
217 g_signal_connect(radio_rating, "released", G_CALLBACK(bar_pane_rating_selected_cb), prd);
219 gq_gtk_box_pack_start(GTK_BOX(row_2), radio_rating, FALSE, FALSE, 1);
220 prd->rating_buttons[i ] = GTK_CHECK_BUTTON(radio_rating);
226 prd->radio_button_first = radio_rating;
228 gtk_widget_show(prd->widget);
230 file_data_register_notify_func(bar_pane_rating_notify_cb, prd, NOTIFY_PRIORITY_LOW);
235 GtkWidget *bar_pane_rating_new_from_config(const gchar **attribute_names, const gchar **attribute_values)
237 gchar *title = nullptr;
238 gchar *id = g_strdup("rating");
239 gboolean expanded = TRUE;
242 while (*attribute_names)
244 const gchar *option = *attribute_names++;
245 const gchar *value = *attribute_values++;
247 if (READ_CHAR_FULL("id", id)) continue;
248 if (READ_CHAR_FULL("title", title)) continue;
249 if (READ_BOOL_FULL("expanded", expanded)) continue;
251 log_printf("unknown attribute %s = %s\n", option, value);
254 bar_pane_translate_title(PANE_RATING, id, &title);
255 ret = bar_pane_rating_new(id, title, expanded);
262 void bar_pane_rating_update_from_config(GtkWidget *pane, const gchar **attribute_names, const gchar **attribute_values)
265 gchar *title = nullptr;
267 prd = static_cast<PaneRatingData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
270 while (*attribute_names)
272 const gchar *option = *attribute_names++;
273 const gchar *value = *attribute_values++;
275 if (READ_CHAR_FULL("title", title)) continue;
276 if (READ_CHAR_FULL("id", prd->pane.id)) continue;
277 if (READ_BOOL_FULL("expanded", prd->pane.expanded)) continue;
279 log_printf("unknown attribute %s = %s\n", option, value);
282 bar_update_expander(pane);
283 bar_pane_rating_update(prd);
285 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */