Include GitHub action to check builds
[geeqie.git] / src / bar.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: Vladimir Nadvornik
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "main.h"
23 #include "bar.h"
24
25 #include "bar-histogram.h"
26 #include "filedata.h"
27 #include "metadata.h"
28 #include "rcfile.h"
29 #include "ui-menu.h"
30 #include "ui-misc.h"
31
32 typedef struct _KnownPanes KnownPanes;
33 struct _KnownPanes
34 {
35         PaneType type;
36         const gchar *id;
37         const gchar *title;
38         const gchar *config;
39 };
40
41 static const gchar default_config_histogram[] =
42 "<gq>"
43 "    <layout id = '_current_'>"
44 "        <bar>"
45 "            <pane_histogram id = 'histogram' expanded = 'true' histogram_channel = '4' histogram_mode = '0' />"
46 "        </bar>"
47 "    </layout>"
48 "</gq>";
49
50 static const gchar default_config_title[] =
51 "<gq>"
52 "    <layout id = '_current_'>"
53 "        <bar>"
54 "            <pane_comment id = 'title' expanded = 'true' key = 'Xmp.dc.title' height = '40' />"
55 "        </bar>"
56 "    </layout>"
57 "</gq>";
58
59 static const gchar default_config_headline[] =
60 "<gq>"
61 "    <layout id = '_current_'>"
62 "        <bar>"
63 "            <pane_comment id = 'headline' expanded = 'true' key = 'Xmp.photoshop.Headline'  height = '40' />"
64 "        </bar>"
65 "    </layout>"
66 "</gq>";
67
68 static const gchar default_config_keywords[] =
69 "<gq>"
70 "    <layout id = '_current_'>"
71 "        <bar>"
72 "            <pane_keywords id = 'keywords' expanded = 'true' key = '" KEYWORD_KEY "' />"
73 "        </bar>"
74 "    </layout>"
75 "</gq>";
76
77 static const gchar default_config_comment[] =
78 "<gq>"
79 "    <layout id = '_current_'>"
80 "        <bar>"
81 "            <pane_comment id = 'comment' expanded = 'true' key = '" COMMENT_KEY "' height = '150' />"
82 "        </bar>"
83 "    </layout>"
84 "</gq>";
85 static const gchar default_config_rating[] =
86 "<gq>"
87 "    <layout id = '_current_'>"
88 "        <bar>"
89 "            <pane_rating id = 'rating' expanded = 'true' />"
90 "        </bar>"
91 "    </layout>"
92 "</gq>";
93
94 static const gchar default_config_exif[] =
95 "<gq>"
96 "    <layout id = '_current_'>"
97 "        <bar>"
98 "            <pane_exif id = 'exif' expanded = 'true' >"
99 "                <entry key = 'formatted.Camera' if_set = 'true' editable = 'false' />"
100 "                <entry key = 'formatted.DateTime' if_set = 'true' editable = 'false' />"
101 "                <entry key = 'formatted.localtime' if_set = 'true' editable = 'false' />"
102 "                <entry key = 'formatted.ShutterSpeed' if_set = 'true' editable = 'false' />"
103 "                <entry key = 'formatted.Aperture' if_set = 'true' editable = 'false' />"
104 "                <entry key = 'formatted.ExposureBias' if_set = 'true' editable = 'false' />"
105 "                <entry key = 'formatted.ISOSpeedRating' if_set = 'true' editable = 'false' />"
106 "                <entry key = 'formatted.FocalLength' if_set = 'true' editable = 'false' />"
107 "                <entry key = 'formatted.FocalLength35mmFilm' if_set = 'true' editable = 'false' />"
108 "                <entry key = 'formatted.Flash' if_set = 'true' editable = 'false' />"
109 "                <entry key = 'Exif.Photo.ExposureProgram' if_set = 'true' editable = 'false' />"
110 "                <entry key = 'Exif.Photo.MeteringMode' if_set = 'true' editable = 'false' />"
111 "                <entry key = 'Exif.Photo.LightSource' if_set = 'true' editable = 'false' />"
112 "                <entry key = 'formatted.ColorProfile' if_set = 'true' editable = 'false' />"
113 "                <entry key = 'formatted.SubjectDistance' if_set = 'true' editable = 'false' />"
114 "                <entry key = 'formatted.Resolution' if_set = 'true' editable = 'false' />"
115 "                <entry key = '" ORIENTATION_KEY "' if_set = 'true' editable = 'false' />"
116 "                <entry key = 'formatted.star_rating' if_set = 'true' editable = 'false' />"
117 "            </pane_exif>"
118 "        </bar>"
119 "    </layout>"
120 "</gq>";
121
122 static const gchar default_config_file_info[] =
123 "<gq>"
124 "    <layout id = '_current_'>"
125 "        <bar>"
126 "            <pane_exif id = 'file_info' expanded = 'true' >"
127 "                <entry key = 'file.mode' if_set = 'false' editable = 'false' />"
128 "                <entry key = 'file.date' if_set = 'false' editable = 'false' />"
129 "                <entry key = 'file.size' if_set = 'false' editable = 'false' />"
130 "                <entry key = 'file.owner' if_set = 'false' editable = 'false' />"
131 "                <entry key = 'file.group' if_set = 'false' editable = 'false' />"
132 "                <entry key = 'file.class' if_set = 'false' editable = 'false' />"
133 "                <entry key = 'file.link' if_set = 'false' editable = 'false' />"
134 "            </pane_exif>"
135 "        </bar>"
136 "    </layout>"
137 "</gq>";
138
139 static const gchar default_config_location[] =
140 "<gq>"
141 "    <layout id = '_current_'>"
142 "        <bar>"
143 "            <pane_exif id = 'location' expanded = 'true' >"
144 "                <entry key = 'formatted.GPSPosition' if_set = 'true' editable = 'false' />"
145 "                <entry key = 'formatted.GPSAltitude' if_set = 'true' editable = 'false' />"
146 "                <entry key = 'formatted.timezone' if_set = 'true' editable = 'false' />"
147 "                <entry key = 'Xmp.photoshop.Country' if_set = 'false' editable = 'true' />"
148 "                <entry key = 'Xmp.iptc.CountryCode' if_set = 'false' editable = 'true' />"
149 "                <entry key = 'Xmp.photoshop.State' if_set = 'false' editable = 'true' />"
150 "                <entry key = 'Xmp.photoshop.City' if_set = 'false' editable = 'true' />"
151 "                <entry key = 'Xmp.iptc.Location' if_set = 'false' editable = 'true' />"
152 "            </pane_exif>"
153 "        </bar>"
154 "    </layout>"
155 "</gq>";
156
157 static const gchar default_config_copyright[] =
158 "<gq>"
159 "    <layout id = '_current_'>"
160 "        <bar>"
161 "            <pane_exif id = 'copyright' expanded = 'true' >"
162 "                <entry key = 'Xmp.dc.creator' if_set = 'true' editable = 'false' />"
163 "                <entry key = 'Xmp.dc.contributor' if_set = 'true' editable = 'false' />"
164 "                <entry key = 'Xmp.dc.rights' if_set = 'false' editable = 'false' />"
165 "            </pane_exif>"
166 "        </bar>"
167 "    </layout>"
168 "</gq>";
169
170 #ifdef HAVE_LIBCHAMPLAIN
171 #ifdef HAVE_LIBCHAMPLAIN_GTK
172 static const gchar default_config_gps[] =
173 "<gq>"
174 "    <layout id = '_current_'>"
175 "        <bar>"
176 "            <pane_gps id = 'gps' expanded = 'true'"
177 "                      map-id = 'osm-mapnik'"
178 "                      zoom-level = '8'"
179 "                      latitude = '50116666'"
180 "                      longitude = '8683333' />"
181 "        </bar>"
182 "    </layout>"
183 "</gq>";
184 #endif
185 #endif
186
187 static const KnownPanes known_panes[] = {
188 /* default sidebar */
189         {PANE_HISTOGRAM,        "histogram",    N_("Histogram"),        default_config_histogram},
190         {PANE_COMMENT,          "title",        N_("Title"),            default_config_title},
191         {PANE_KEYWORDS,         "keywords",     N_("Keywords"),         default_config_keywords},
192         {PANE_COMMENT,          "comment",      N_("Comment"),          default_config_comment},
193         {PANE_RATING,           "rating",       N_("Star Rating"),      default_config_rating},
194         {PANE_COMMENT,          "headline",     N_("Headline"),         default_config_headline},
195         {PANE_EXIF,             "exif",         N_("Exif"),             default_config_exif},
196 /* other pre-configured panes */
197         {PANE_EXIF,             "file_info",    N_("File info"),        default_config_file_info},
198         {PANE_EXIF,             "location",     N_("Location and GPS"), default_config_location},
199         {PANE_EXIF,             "copyright",    N_("Copyright"),        default_config_copyright},
200 #ifdef HAVE_LIBCHAMPLAIN
201 #ifdef HAVE_LIBCHAMPLAIN_GTK
202         {PANE_GPS,              "gps",  N_("GPS Map"),  default_config_gps},
203 #endif
204 #endif
205         {PANE_UNDEF,            NULL,           NULL,                   NULL}
206 };
207
208 typedef struct _BarData BarData;
209 struct _BarData
210 {
211         GtkWidget *widget;
212         GtkWidget *vbox;
213         FileData *fd;
214         GtkWidget *label_file_name;
215         GtkWidget *add_button;
216
217         LayoutWindow *lw;
218         gint width;
219 };
220
221 static void bar_expander_move(GtkWidget *UNUSED(widget), gpointer data, gboolean up, gboolean single_step)
222 {
223         GtkWidget *expander = static_cast<GtkWidget *>(data);
224         GtkWidget *box;
225         gint pos;
226
227         if (!expander) return;
228         box = gtk_widget_get_ancestor(expander, GTK_TYPE_BOX);
229         if (!box) return;
230
231         gtk_container_child_get(GTK_CONTAINER(box), expander, "position", &pos, NULL);
232
233         if (single_step)
234                 {
235                 pos = up ? (pos - 1) : (pos + 1);
236                 if (pos < 0) pos = 0;
237                 }
238         else
239                 {
240                 pos = up ? 0 : -1;
241                 }
242
243         gtk_box_reorder_child(GTK_BOX(box), expander, pos);
244 }
245
246
247 static void bar_expander_move_up_cb(GtkWidget *widget, gpointer data)
248 {
249         bar_expander_move(widget, data, TRUE, TRUE);
250 }
251
252 static void bar_expander_move_down_cb(GtkWidget *widget, gpointer data)
253 {
254         bar_expander_move(widget, data, FALSE, TRUE);
255 }
256
257 static void bar_expander_move_top_cb(GtkWidget *widget, gpointer data)
258 {
259         bar_expander_move(widget, data, TRUE, FALSE);
260 }
261
262 static void bar_expander_move_bottom_cb(GtkWidget *widget, gpointer data)
263 {
264         bar_expander_move(widget, data, FALSE, FALSE);
265 }
266
267 static void height_spin_changed_cb(GtkSpinButton *spin, gpointer data)
268 {
269
270         gtk_widget_set_size_request(GTK_WIDGET(data), -1, gtk_spin_button_get_value_as_int(spin));
271 }
272
273 static void height_spin_key_press_cb(GtkEventControllerKey *UNUSED(controller), gint keyval, guint UNUSED(keycode), GdkModifierType UNUSED(state), gpointer data)
274 {
275         if ((keyval == GDK_KEY_Return || keyval == GDK_KEY_Escape))
276                 {
277                 gtk_widget_destroy(GTK_WIDGET(data));
278                 }
279 }
280
281 static void bar_expander_height_cb(GtkWidget *UNUSED(widget), gpointer data)
282 {
283         GtkWidget *expander = static_cast<GtkWidget *>(data);
284         GtkWidget *spin;
285         GtkWidget *window;
286         GtkWidget *data_box;
287         GList *list;
288         gint x, y;
289         gint w, h;
290         GdkDisplay *display;
291         GdkSeat *seat;
292         GdkDevice *device;
293         GtkEventController *controller;
294
295         display = gdk_display_get_default();
296         seat = gdk_display_get_default_seat(display);
297         device = gdk_seat_get_pointer(seat);
298         gdk_device_get_position(device, NULL, &x, &y);
299
300         list = gtk_container_get_children(GTK_CONTAINER(expander));
301         data_box = static_cast<GtkWidget *>(list->data);
302
303         window = gtk_window_new(GTK_WINDOW_POPUP);
304
305         gtk_window_set_modal(GTK_WINDOW(window), TRUE);
306         gtk_window_set_keep_above(GTK_WINDOW(window), TRUE);
307         gtk_window_set_default_size(GTK_WINDOW(window), 50, 30); //** @FIXME set these values in a more sensible way */
308
309         gtk_window_move(GTK_WINDOW(window), x, y);
310         gtk_widget_show(window);
311
312         gtk_widget_get_size_request(GTK_WIDGET(data_box), &w, &h);
313
314         spin = gtk_spin_button_new_with_range(1, 1000, 1);
315         g_signal_connect(G_OBJECT(spin), "value-changed", G_CALLBACK(height_spin_changed_cb), data_box);
316         controller = gtk_event_controller_key_new(spin);
317         g_signal_connect(controller, "key-pressed", G_CALLBACK(height_spin_key_press_cb), window);
318
319         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), h);
320         gtk_container_add(GTK_CONTAINER(window), spin);
321         gtk_widget_show(spin);
322         gtk_widget_grab_focus(GTK_WIDGET(spin));
323
324         g_list_free(list);
325 }
326
327 static void bar_expander_delete_cb(GtkWidget *UNUSED(widget), gpointer data)
328 {
329         GtkWidget *expander = static_cast<GtkWidget *>(data);
330         gtk_widget_destroy(expander);
331 }
332
333 static void bar_expander_add_cb(GtkWidget *widget, gpointer UNUSED(data))
334 {
335         //GtkWidget *bar = static_cast<//GtkWidget *>(data);
336         const KnownPanes *pane = known_panes;
337         const gchar *id = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "pane_add_id"));
338         const gchar *config;
339
340         if (!id) return;
341
342         while (pane->id)
343                 {
344                 if (strcmp(pane->id, id) == 0) break;
345                 pane++;
346                 }
347         if (!pane->id) return;
348
349         config = bar_pane_get_default_config(id);
350         if (config) load_config_from_buf(config, strlen(config), FALSE);
351
352 }
353
354
355 static void bar_menu_popup(GtkWidget *widget)
356 {
357         GtkWidget *menu;
358         GtkWidget *bar;
359         GtkWidget *expander;
360         BarData *bd;
361         gboolean display_height_option = FALSE;
362         gchar const *label;
363
364         label = gtk_expander_get_label(GTK_EXPANDER(widget));
365         display_height_option = (g_strcmp0(label, "Comment") == 0) ||
366                                                         (g_strcmp0(label, "Rating") == 0) ||
367                                                         (g_strcmp0(label, "Title") == 0) ||
368                                                         (g_strcmp0(label, "Headline") == 0) ||
369                                                         (g_strcmp0(label, "Keywords") == 0) ||
370                                                         (g_strcmp0(label, "GPS Map") == 0);
371
372         bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(widget), "bar_data"));
373         if (bd)
374                 {
375                 expander = NULL;
376                 bar = widget;
377                 }
378         else
379                 {
380                 expander = widget;
381                 bar = gtk_widget_get_parent(widget);
382                 while (bar && !g_object_get_data(G_OBJECT(bar), "bar_data"))
383                         bar = gtk_widget_get_parent(bar);
384                 if (!bar) return;
385                 }
386
387         menu = popup_menu_short_lived();
388
389         if (expander)
390                 {
391                 menu_item_add_stock(menu, _("Move to _top"), GTK_STOCK_GOTO_TOP, G_CALLBACK(bar_expander_move_top_cb), expander);
392                 menu_item_add_stock(menu, _("Move _up"), GTK_STOCK_GO_UP, G_CALLBACK(bar_expander_move_up_cb), expander);
393                 menu_item_add_stock(menu, _("Move _down"), GTK_STOCK_GO_DOWN, G_CALLBACK(bar_expander_move_down_cb), expander);
394                 menu_item_add_stock(menu, _("Move to _bottom"), GTK_STOCK_GOTO_BOTTOM, G_CALLBACK(bar_expander_move_bottom_cb), expander);
395                 menu_item_add_divider(menu);
396
397                 if (gtk_expander_get_expanded(GTK_EXPANDER(expander)) && display_height_option)
398                         {
399                         menu_item_add_stock(menu, _("Height..."), GTK_STOCK_PREFERENCES, G_CALLBACK(bar_expander_height_cb), expander);
400                         menu_item_add_divider(menu);
401                         }
402
403                 menu_item_add_stock(menu, _("Remove"), GTK_STOCK_DELETE, G_CALLBACK(bar_expander_delete_cb), expander);
404                 menu_item_add_divider(menu);
405                 }
406
407         gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
408 }
409
410 static void bar_menu_add_popup(GtkWidget *widget)
411 {
412         GtkWidget *menu;
413         GtkWidget *bar;
414         const KnownPanes *pane = known_panes;
415
416         bar = widget;
417
418         menu = popup_menu_short_lived();
419
420         while (pane->id)
421                 {
422                 GtkWidget *item;
423                 item = menu_item_add_stock(menu, _(pane->title), GTK_STOCK_ADD, G_CALLBACK(bar_expander_add_cb), bar);
424                 g_object_set_data(G_OBJECT(item), "pane_add_id", const_cast<gchar *>(pane->id));
425                 pane++;
426                 }
427
428         gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
429 }
430
431
432 static gboolean bar_menu_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer UNUSED(data))
433 {
434         if (bevent->button == MOUSE_BUTTON_RIGHT)
435                 {
436                 bar_menu_popup(widget);
437                 return TRUE;
438                 }
439         return FALSE;
440 }
441
442 static void bar_expander_cb(GObject *object, GParamSpec *UNUSED(param_spec), gpointer UNUSED(data))
443 {
444         GtkExpander *expander;
445         GtkWidget *child;
446
447         expander = GTK_EXPANDER(object);
448         child = gtk_bin_get_child(GTK_BIN(expander));
449
450         if (gtk_expander_get_expanded(expander))
451                 {
452                 gtk_widget_show_all(child);
453                 }
454         else
455                 {
456                 gtk_widget_hide(child);
457                 }
458 }
459
460 static gboolean bar_menu_add_cb(GtkWidget *widget, GdkEventButton *UNUSED(bevent), gpointer UNUSED(data))
461 {
462         bar_menu_add_popup(widget);
463         return TRUE;
464 }
465
466
467 static void bar_pane_set_fd_cb(GtkWidget *expander, gpointer data)
468 {
469         GtkWidget *widget = gtk_bin_get_child(GTK_BIN(expander));
470         PaneData *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
471         if (!pd) return;
472         if (pd->pane_set_fd) pd->pane_set_fd(widget, static_cast<FileData *>(data));
473 }
474
475 void bar_set_fd(GtkWidget *bar, FileData *fd)
476 {
477         BarData *bd;
478         bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
479         if (!bd) return;
480
481         file_data_unref(bd->fd);
482         bd->fd = file_data_ref(fd);
483
484         gtk_container_foreach(GTK_CONTAINER(bd->vbox), bar_pane_set_fd_cb, fd);
485
486         gtk_label_set_text(GTK_LABEL(bd->label_file_name), (bd->fd) ? bd->fd->name : "");
487
488 }
489
490 static void bar_pane_notify_selection_cb(GtkWidget *expander, gpointer data)
491 {
492         GtkWidget *widget = gtk_bin_get_child(GTK_BIN(expander));
493         PaneData *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
494         if (!pd) return;
495         if (pd->pane_notify_selection) pd->pane_notify_selection(widget, GPOINTER_TO_INT(data));
496 }
497
498 void bar_notify_selection(GtkWidget *bar, gint count)
499 {
500         BarData *bd;
501         bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
502         if (!bd) return;
503
504         gtk_container_foreach(GTK_CONTAINER(bd->vbox), bar_pane_notify_selection_cb, GINT_TO_POINTER(count));
505 }
506
507 gboolean bar_event(GtkWidget *bar, GdkEvent *event)
508 {
509         BarData *bd;
510         GList *list, *work;
511         gboolean ret = FALSE;
512
513         bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
514         if (!bd) return FALSE;
515
516         list = gtk_container_get_children(GTK_CONTAINER(bd->vbox));
517
518         work = list;
519         while (work)
520                 {
521                 GtkWidget *widget = gtk_bin_get_child(GTK_BIN(work->data));
522                 PaneData *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
523                 if (!pd) continue;
524
525                 if (pd->pane_event && pd->pane_event(widget, event))
526                         {
527                         ret = TRUE;
528                         break;
529                         }
530                 work = work->next;
531                 }
532         g_list_free(list);
533         return ret;
534 }
535
536 GtkWidget *bar_find_pane_by_id(GtkWidget *bar, PaneType type, const gchar *id)
537 {
538         BarData *bd;
539         GList *list, *work;
540         GtkWidget *ret = NULL;
541
542         if (!id || !id[0]) return NULL;
543
544         bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
545         if (!bd) return NULL;
546
547         list = gtk_container_get_children(GTK_CONTAINER(bd->vbox));
548
549         work = list;
550         while (work)
551                 {
552                 GtkWidget *widget = gtk_bin_get_child(GTK_BIN(work->data));
553                 PaneData *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
554                 if (!pd) continue;
555
556                 if (type == pd->type && strcmp(id, pd->id) == 0)
557                         {
558                         ret = widget;
559                         break;
560                         }
561                 work = work->next;
562                 }
563         g_list_free(list);
564         return ret;
565 }
566
567 void bar_clear(GtkWidget *bar)
568 {
569         BarData *bd;
570         GList *list, *work;
571
572         bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
573         if (!bd) return;
574
575         list = gtk_container_get_children(GTK_CONTAINER(bd->vbox));
576
577         work = list;
578         while (work)
579                 {
580                 GtkWidget *widget = static_cast<GtkWidget *>(work->data);
581                 gtk_widget_destroy(widget);
582                 work = work->next;
583                 }
584         g_list_free(list);
585 }
586
587 void bar_write_config(GtkWidget *bar, GString *outstr, gint indent)
588 {
589         BarData *bd;
590         GList *list, *work;
591
592         if (!bar) return;
593
594         bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
595         if (!bd) return;
596
597         WRITE_NL(); WRITE_STRING("<bar ");
598         write_bool_option(outstr, indent, "enabled", gtk_widget_get_visible(bar));
599         write_uint_option(outstr, indent, "width", bd->width);
600         WRITE_STRING(">");
601
602         indent++;
603         WRITE_NL(); WRITE_STRING("<clear/>");
604
605         list = gtk_container_get_children(GTK_CONTAINER(bd->vbox));
606         work = list;
607         while (work)
608                 {
609                 GtkWidget *expander = static_cast<GtkWidget *>(work->data);
610                 GtkWidget *widget = gtk_bin_get_child(GTK_BIN(expander));
611                 PaneData *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(widget), "pane_data"));
612                 if (!pd) continue;
613
614                 pd->expanded = gtk_expander_get_expanded(GTK_EXPANDER(expander));
615
616                 if (pd->pane_write_config)
617                         pd->pane_write_config(widget, outstr, indent);
618
619                 work = work->next;
620                 }
621         g_list_free(list);
622         indent--;
623         WRITE_NL(); WRITE_STRING("</bar>");
624 }
625
626 void bar_update_expander(GtkWidget *pane)
627 {
628         PaneData *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
629         GtkWidget *expander;
630
631         if (!pd) return;
632
633         expander = gtk_widget_get_parent(pane);
634
635         gtk_expander_set_expanded(GTK_EXPANDER(expander), pd->expanded);
636 }
637
638 void bar_add(GtkWidget *bar, GtkWidget *pane)
639 {
640         GtkWidget *expander;
641         BarData *bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
642         PaneData *pd = static_cast<PaneData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
643
644         if (!bd) return;
645
646         pd->lw = bd->lw;
647         pd->bar = bar;
648
649         expander = gtk_expander_new(NULL);
650         DEBUG_NAME(expander);
651         if (pd && pd->title)
652                 {
653                 gtk_expander_set_label_widget(GTK_EXPANDER(expander), pd->title);
654                 gtk_widget_show(pd->title);
655                 }
656
657         gtk_box_pack_start(GTK_BOX(bd->vbox), expander, FALSE, TRUE, 0);
658
659         g_signal_connect(expander, "button_release_event", G_CALLBACK(bar_menu_cb), bd);
660         g_signal_connect(expander, "notify::expanded", G_CALLBACK(bar_expander_cb), pd);
661
662         gtk_container_add(GTK_CONTAINER(expander), pane);
663
664         gtk_expander_set_expanded(GTK_EXPANDER(expander), pd->expanded);
665
666         gtk_widget_show(expander);
667
668         if (bd->fd && pd && pd->pane_set_fd) pd->pane_set_fd(pane, bd->fd);
669
670 }
671
672 void bar_populate_default(GtkWidget *UNUSED(bar))
673 {
674         const gchar *populate_id[] = {"histogram", "title", "keywords", "comment", "rating", "exif", NULL};
675         const gchar **id = populate_id;
676
677         while (*id)
678                 {
679                 const gchar *config = bar_pane_get_default_config(*id);
680                 if (config) load_config_from_buf(config, strlen(config), FALSE);
681                 id++;
682                 }
683 }
684
685 static void bar_size_allocate(GtkWidget *UNUSED(widget), GtkAllocation *UNUSED(allocation), gpointer data)
686 {
687         BarData *bd = static_cast<BarData *>(data);
688
689         bd->width = gtk_paned_get_position(GTK_PANED(bd->lw->utility_paned));
690 }
691
692 //gint bar_get_width(GtkWidget *bar)
693 //{
694         //BarData *bd;
695
696         //bd = g_object_get_data(G_OBJECT(bar), "bar_data");
697         //if (!bd) return 0;
698
699         //return bd->width;
700 //}
701
702 void bar_close(GtkWidget *bar)
703 {
704         BarData *bd;
705
706         bd = static_cast<BarData *>(g_object_get_data(G_OBJECT(bar), "bar_data"));
707         if (!bd) return;
708
709         gtk_widget_destroy(bd->widget);
710 }
711
712 static void bar_destroy(GtkWidget *UNUSED(widget), gpointer data)
713 {
714         BarData *bd = static_cast<BarData *>(data);
715
716         file_data_unref(bd->fd);
717         g_free(bd);
718 }
719
720 #ifdef HAVE_LIBCHAMPLAIN_GTK
721 /**
722    @FIXME this is an ugly hack that works around this bug:
723    https://bugzilla.gnome.org/show_bug.cgi?id=590692
724    http://bugzilla.openedhand.com/show_bug.cgi?id=1751
725    it should be removed as soon as a better solution exists
726 */
727
728 static void bar_unrealize_clutter_fix_cb(GtkWidget *widget, gpointer UNUSED(data))
729 {
730         GtkWidget *child = gtk_bin_get_child(GTK_BIN(widget));
731         if (child) gtk_widget_unrealize(child);
732 }
733 #endif
734
735 GtkWidget *bar_new(LayoutWindow *lw)
736 {
737         BarData *bd;
738         GtkWidget *box;
739         GtkWidget *scrolled;
740         GtkWidget *tbar;
741         GtkWidget *add_box;
742
743         bd = g_new0(BarData, 1);
744
745         bd->lw = lw;
746
747         bd->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
748         DEBUG_NAME(bd->widget);
749         g_object_set_data(G_OBJECT(bd->widget), "bar_data", bd);
750         g_signal_connect(G_OBJECT(bd->widget), "destroy",
751                          G_CALLBACK(bar_destroy), bd);
752
753         g_signal_connect(G_OBJECT(bd->widget), "size-allocate",
754                          G_CALLBACK(bar_size_allocate), bd);
755
756         g_signal_connect(G_OBJECT(bd->widget), "button_release_event", G_CALLBACK(bar_menu_cb), bd);
757
758         bd->width = SIDEBAR_DEFAULT_WIDTH;
759
760         box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
761         DEBUG_NAME(box);
762
763         bd->label_file_name = gtk_label_new("");
764         gtk_label_set_ellipsize(GTK_LABEL(bd->label_file_name), PANGO_ELLIPSIZE_END);
765         gtk_label_set_selectable(GTK_LABEL(bd->label_file_name), TRUE);
766         gtk_label_set_xalign(GTK_LABEL(bd->label_file_name), 0.5);
767         gtk_label_set_yalign(GTK_LABEL(bd->label_file_name), 0.5);
768
769         gtk_box_pack_start(GTK_BOX(box), bd->label_file_name, TRUE, TRUE, 0);
770         gtk_widget_show(bd->label_file_name);
771
772         gtk_box_pack_start(GTK_BOX(bd->widget), box, FALSE, FALSE, 0);
773         gtk_widget_show(box);
774
775         scrolled = gtk_scrolled_window_new(NULL, NULL);
776         DEBUG_NAME(scrolled);
777         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
778                 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
779         gtk_box_pack_start(GTK_BOX(bd->widget), scrolled, TRUE, TRUE, 0);
780         gtk_widget_show(scrolled);
781
782
783         bd->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
784         gtk_container_add(GTK_CONTAINER(scrolled), bd->vbox);
785         gtk_viewport_set_shadow_type(GTK_VIEWPORT(gtk_bin_get_child(GTK_BIN(scrolled))), GTK_SHADOW_NONE);
786
787         add_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
788         DEBUG_NAME(add_box);
789         gtk_box_pack_end(GTK_BOX(bd->widget), add_box, FALSE, FALSE, 0);
790         tbar = pref_toolbar_new(add_box, GTK_TOOLBAR_ICONS);
791         bd->add_button = pref_toolbar_button(tbar, GTK_STOCK_ADD, NULL, FALSE,
792                                              _("Add Pane"),
793                                              G_CALLBACK(bar_menu_add_cb), bd);
794         gtk_widget_show(add_box);
795
796 #ifdef HAVE_LIBCHAMPLAIN_GTK
797         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN(scrolled))), "unrealize", G_CALLBACK(bar_unrealize_clutter_fix_cb), NULL);
798 #endif
799
800         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_NONE);
801         gtk_widget_show(bd->vbox);
802         return bd->widget;
803 }
804
805
806 GtkWidget *bar_update_from_config(GtkWidget *bar, const gchar **attribute_names, const gchar **attribute_values, LayoutWindow *lw, gboolean startup)
807 {
808         gboolean enabled = TRUE;
809         gint width = SIDEBAR_DEFAULT_WIDTH;
810
811         while (*attribute_names)
812                 {
813                 const gchar *option = *attribute_names++;
814                 const gchar *value = *attribute_values++;
815
816                 if (READ_BOOL_FULL("enabled", enabled)) continue;
817                 if (READ_INT_FULL("width", width)) continue;
818
819
820                 log_printf("unknown attribute %s = %s\n", option, value);
821                 }
822
823         if (startup)
824                 {
825                 gtk_paned_set_position(GTK_PANED(lw->utility_paned), width);
826                 }
827
828         if (enabled)
829                 {
830                 gtk_widget_show(bar);
831                 }
832         else
833                 {
834                 gtk_widget_hide(bar);
835                 }
836         return bar;
837 }
838
839 GtkWidget *bar_new_from_config(LayoutWindow *lw, const gchar **attribute_names, const gchar **attribute_values)
840 {
841         GtkWidget *bar = bar_new(lw);
842         return bar_update_from_config(bar, attribute_names, attribute_values, lw, TRUE);
843 }
844
845 GtkWidget *bar_pane_expander_title(const gchar *title)
846 {
847         GtkWidget *widget = gtk_label_new(title);
848
849         pref_label_bold(widget, TRUE, FALSE);
850         //gtk_label_set_ellipsize(GTK_LABEL(widget), PANGO_ELLIPSIZE_END);
851         /** @FIXME do not work */
852
853         return widget;
854 }
855
856 gboolean bar_pane_translate_title(PaneType type, const gchar *id, gchar **title)
857 {
858         const KnownPanes *pane = known_panes;
859
860         if (!title) return FALSE;
861         while (pane->id)
862                 {
863                 if (pane->type == type && strcmp(pane->id, id) == 0) break;
864                 pane++;
865                 }
866         if (!pane->id) return FALSE;
867
868         if (*title && **title && strcmp(pane->title, *title) != 0) return FALSE;
869
870         g_free(*title);
871         *title = g_strdup(_(pane->title));
872         return TRUE;
873 }
874
875 const gchar *bar_pane_get_default_config(const gchar *id)
876 {
877         const KnownPanes *pane = known_panes;
878
879         while (pane->id)
880                 {
881                 if (strcmp(pane->id, id) == 0) break;
882                 pane++;
883                 }
884         if (!pane->id) return NULL;
885         return pane->config;
886 }
887
888 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */