Silence GTK deprecation warning in unused function
[geeqie.git] / src / layout-config.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
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 "layout-config.h"
23
24 #include <cstring>
25
26 #include <glib-object.h>
27
28 #include <config.h>
29
30 #include "compat.h"
31 #include "intl.h"
32 #include "ui-misc.h"
33
34 namespace
35 {
36
37 enum {
38         COLUMN_TEXT = 0,
39         COLUMN_KEY
40 };
41
42 struct LayoutStyle
43 {
44         LayoutLocation a, b, c;
45 };
46
47 struct LayoutConfig
48 {
49         GtkWidget *box;
50
51         GList *style_widgets;
52
53         GtkWidget *listview;
54
55         gint style;
56         gint a, b, c;
57 };
58
59 constexpr gint LAYOUT_STYLE_SIZE = 48;
60
61 // @todo Use std::array
62 constexpr LayoutStyle layout_config_styles[] = {
63         /* 1, 2, 3 */
64         { static_cast<LayoutLocation>(LAYOUT_LEFT | LAYOUT_TOP), static_cast<LayoutLocation>(LAYOUT_LEFT | LAYOUT_BOTTOM), LAYOUT_RIGHT },
65         { static_cast<LayoutLocation>(LAYOUT_LEFT | LAYOUT_TOP), static_cast<LayoutLocation>(LAYOUT_RIGHT | LAYOUT_TOP), LAYOUT_BOTTOM },
66         { LAYOUT_LEFT, static_cast<LayoutLocation>(LAYOUT_RIGHT | LAYOUT_TOP), static_cast<LayoutLocation>(LAYOUT_RIGHT | LAYOUT_BOTTOM) },
67         { LAYOUT_TOP, static_cast<LayoutLocation>(LAYOUT_LEFT | LAYOUT_BOTTOM), static_cast<LayoutLocation>(LAYOUT_RIGHT | LAYOUT_BOTTOM) }
68 };
69
70 constexpr gint layout_config_style_count = sizeof(layout_config_styles) / sizeof(LayoutStyle);
71
72 const gchar *layout_titles[] = { N_("Tools"), N_("Files"), N_("Image") };
73
74
75 void layout_config_destroy(GtkWidget *, gpointer data)
76 {
77         auto lc = static_cast<LayoutConfig *>(data);
78
79         g_list_free(lc->style_widgets);
80         g_free(lc);
81 }
82
83 void layout_config_set_order(LayoutLocation l, gint n,
84                                     LayoutLocation *a, LayoutLocation *b, LayoutLocation *c)
85 {
86         switch (n)
87                 {
88                 case 0:
89                         *a = l;
90                         break;
91                 case 1:
92                         *b = l;
93                         break;
94                 case 2: default:
95                         *c = l;
96                         break;
97                 }
98 }
99
100 void layout_config_from_data(gint style, gint oa, gint ob, gint oc,
101                                     LayoutLocation *la, LayoutLocation *lb, LayoutLocation *lc)
102 {
103         LayoutStyle ls;
104
105         style = CLAMP(style, 0, layout_config_style_count);
106
107         ls = layout_config_styles[style];
108
109         layout_config_set_order(ls.a, oa, la, lb, lc);
110         layout_config_set_order(ls.b, ob, la, lb, lc);
111         layout_config_set_order(ls.c, oc, la, lb, lc);
112 }
113
114 void layout_config_list_order_set(LayoutConfig *lc, gint src, gint dest)
115 {
116         GtkListStore *store;
117         GtkTreeIter iter;
118         gboolean valid;
119         gint n;
120
121         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(lc->listview)));
122
123         n = 0;
124         valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
125         while (valid)
126                 {
127                 if (n == dest)
128                         {
129                         gtk_list_store_set(store, &iter, COLUMN_TEXT, _(layout_titles[src]), COLUMN_KEY, src, -1);
130                         return;
131                         }
132                 n++;
133                 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
134                 }
135 }
136
137 gint layout_config_list_order_get(LayoutConfig *lc, gint n)
138 {
139         GtkTreeModel *store;
140         GtkTreeIter iter;
141         gboolean valid;
142         gint c = 0;
143
144         store = gtk_tree_view_get_model(GTK_TREE_VIEW(lc->listview));
145
146         valid = gtk_tree_model_get_iter_first(store, &iter);
147         while (valid)
148                 {
149                 if (c == n)
150                         {
151                         gint val;
152                         gtk_tree_model_get(store, &iter, COLUMN_KEY, &val, -1);
153                         return val;
154                         }
155                 c++;
156                 valid = gtk_tree_model_iter_next(store, &iter);
157                 }
158         return 0;
159 }
160
161 void layout_config_widget_click_cb(GtkWidget *widget, gpointer data)
162 {
163         LayoutConfig *lc;
164
165         lc = static_cast<LayoutConfig *>(g_object_get_data(G_OBJECT(widget), "layout_config"));
166
167         if (lc) lc->style = GPOINTER_TO_INT(data);
168 }
169
170 void layout_config_table_button(GtkWidget *table, LayoutLocation l, const gchar *text)
171 {
172         GtkWidget *button;
173
174         gint x1;
175         gint y1;
176         gint x2;
177         gint y2;
178
179         x1 = 0;
180         y1 = 0;
181         x2 = 2;
182         y2 = 2;
183
184         if (l & LAYOUT_LEFT) x2 = 1;
185         if (l & LAYOUT_RIGHT) x1 = 1;
186         if (l & LAYOUT_TOP) y2 = 1;
187         if (l & LAYOUT_BOTTOM) y1 = 1;
188
189         button = gtk_button_new_with_label(text);
190         gtk_widget_set_sensitive(button, FALSE);
191         gtk_widget_set_can_focus(button, FALSE);
192         gtk_grid_attach(GTK_GRID(table), button, x1, y1, x2 - x1, y2 - y1);
193         gtk_widget_show(button);
194 }
195
196 GtkWidget *layout_config_widget(GtkWidget *group, GtkWidget *box, gint style, LayoutConfig *lc)
197 {
198         GtkWidget *table;
199         LayoutStyle ls;
200
201         ls = layout_config_styles[style];
202
203         if (group)
204                 {
205 #if HAVE_GTK4
206                 group = gtk_toggle_button_new();
207                 gtk_toggle_button_set_group(button, group);
208 #else
209                 group = gtk_radio_button_new(gtk_radio_button_get_group(GTK_RADIO_BUTTON(group)));
210 #endif
211                 }
212         else
213                 {
214 #if HAVE_GTK4
215                 group = gtk_toggle_button_new();
216 #else
217                 group = gtk_radio_button_new(nullptr);
218 #endif
219                 }
220         g_object_set_data(G_OBJECT(group), "layout_config", lc);
221         g_signal_connect(G_OBJECT(group), "clicked",
222                          G_CALLBACK(layout_config_widget_click_cb), GINT_TO_POINTER(style));
223         gq_gtk_box_pack_start(GTK_BOX(box), group, FALSE, FALSE, 0);
224
225         table = gtk_grid_new();
226
227         layout_config_table_button(table, ls.a, "1");
228         layout_config_table_button(table, ls.b, "2");
229         layout_config_table_button(table, ls.c, "3");
230
231         gtk_widget_set_size_request(table, LAYOUT_STYLE_SIZE, LAYOUT_STYLE_SIZE);
232         gq_gtk_container_add(GTK_WIDGET(group), table);
233         gtk_widget_show(table);
234
235         gtk_widget_show(group);
236
237         return group;
238 }
239
240 void layout_config_number_cb(GtkTreeViewColumn *, GtkCellRenderer *cell,
241                     GtkTreeModel *store, GtkTreeIter *iter, gpointer)
242 {
243         GtkTreePath *tpath;
244         gint *indices;
245         gchar *buf;
246
247         tpath = gtk_tree_model_get_path(store, iter);
248         indices = gtk_tree_path_get_indices(tpath);
249         buf = g_strdup_printf("%d", indices[0] + 1);
250         gtk_tree_path_free(tpath);
251         g_object_set(G_OBJECT(cell), "text", buf, NULL);
252         g_free(buf);
253 }
254
255 gchar num_to_text_char(gint n)
256 {
257         switch (n)
258                 {
259                 case 1:
260                         return '2';
261                         break;
262                 case 2:
263                         return '3';
264                         break;
265                 }
266         return '1';
267 }
268
269 gchar *layout_config_order_to_text(gint a, gint b, gint c)
270 {
271         gchar *text;
272
273         text = g_strdup("   ");
274
275         text[0] = num_to_text_char(a);
276         text[1] = num_to_text_char(b);
277         text[2] = num_to_text_char(c);
278
279         return text;
280 }
281
282 gint text_char_to_num(const gchar *text, gint n)
283 {
284         if (text[n] == '3') return 2;
285         if (text[n] == '2') return 1;
286         return 0;
287 }
288
289 void layout_config_order_from_text(const gchar *text, gint *a, gint *b, gint *c)
290 {
291         if (!text || strlen(text) < 3)
292                 {
293                 *a = 0;
294                 *b = 1;
295                 *c = 2;
296                 }
297         else
298                 {
299                 *a = text_char_to_num(text, 0);
300                 *b = text_char_to_num(text, 1);
301                 *c = text_char_to_num(text, 2);
302                 }
303 }
304
305 } // namespace
306
307 void layout_config_parse(gint style, const gchar *order,
308                          LayoutLocation *a, LayoutLocation *b, LayoutLocation *c)
309 {
310         gint na;
311         gint nb;
312         gint nc;
313
314         layout_config_order_from_text(order, &na, &nb, &nc);
315         layout_config_from_data(style, na, nb, nc, a, b, c);
316 }
317
318 void layout_config_set(GtkWidget *widget, gint style, const gchar *order)
319 {
320         LayoutConfig *lc;
321         GtkWidget *button;
322         gint a;
323         gint b;
324         gint c;
325
326         lc = static_cast<LayoutConfig *>(g_object_get_data(G_OBJECT(widget), "layout_config"));
327
328         if (!lc) return;
329
330         style = CLAMP(style, 0, layout_config_style_count);
331         button = static_cast<GtkWidget *>(g_list_nth_data(lc->style_widgets, style));
332         if (!button) return;
333
334         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
335
336         layout_config_order_from_text(order, &a, &b, &c);
337
338         layout_config_list_order_set(lc, a, 0);
339         layout_config_list_order_set(lc, b, 1);
340         layout_config_list_order_set(lc, c, 2);
341 }
342
343 gchar *layout_config_get(GtkWidget *widget, gint *style)
344 {
345         LayoutConfig *lc;
346
347         lc = static_cast<LayoutConfig *>(g_object_get_data(G_OBJECT(widget), "layout_config"));
348
349         /* this should not happen */
350         if (!lc) return nullptr;
351
352         *style = lc->style;
353
354         lc->a = layout_config_list_order_get(lc, 0);
355         lc->b = layout_config_list_order_get(lc, 1);
356         lc->c = layout_config_list_order_get(lc, 2);
357
358         return layout_config_order_to_text(lc->a, lc->b, lc->c);
359 }
360
361 GtkWidget *layout_config_new()
362 {
363         LayoutConfig *lc;
364         GtkWidget *hbox;
365         GtkWidget *group = nullptr;
366         GtkWidget *scrolled;
367         GtkListStore *store;
368         GtkTreeViewColumn *column;
369         GtkCellRenderer *renderer;
370         gint i;
371
372         lc = g_new0(LayoutConfig, 1);
373
374         lc->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
375         g_object_set_data(G_OBJECT(lc->box), "layout_config", lc);
376
377         g_signal_connect(G_OBJECT(lc->box), "destroy",
378                          G_CALLBACK(layout_config_destroy), lc);
379
380         hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
381         gq_gtk_box_pack_start(GTK_BOX(lc->box), hbox, FALSE, FALSE, 0);
382         for (i = 0; i < layout_config_style_count; i++)
383                 {
384                 group = layout_config_widget(group, hbox, i, lc);
385                 lc->style_widgets = g_list_append(lc->style_widgets, group);
386                 }
387         gtk_widget_show(hbox);
388
389         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
390         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
391         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
392                                        GTK_POLICY_NEVER, GTK_POLICY_NEVER);
393         gq_gtk_box_pack_start(GTK_BOX(lc->box), scrolled, FALSE, FALSE, 0);
394         gtk_widget_show(scrolled);
395
396         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
397         lc->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
398         g_object_unref(store);
399
400         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(lc->listview), FALSE);
401         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(lc->listview), FALSE);
402         gtk_tree_view_set_reorderable(GTK_TREE_VIEW(lc->listview), TRUE);
403
404         column = gtk_tree_view_column_new();
405         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
406
407         renderer = gtk_cell_renderer_text_new();
408         gtk_tree_view_column_pack_start(column, renderer, FALSE);
409         gtk_tree_view_column_set_cell_data_func(column, renderer, layout_config_number_cb, lc, nullptr);
410
411         renderer = gtk_cell_renderer_text_new();
412         gtk_tree_view_column_pack_start(column, renderer, TRUE);
413         gtk_tree_view_column_add_attribute(column, renderer, "text", COLUMN_TEXT);
414
415         gtk_tree_view_append_column(GTK_TREE_VIEW(lc->listview), column);
416
417         for (i = 0; i < 3; i++)
418                 {
419                 GtkTreeIter iter;
420
421                 gtk_list_store_append(store, &iter);
422                 gtk_list_store_set(store, &iter, COLUMN_TEXT, _(layout_titles[i]), COLUMN_KEY, i, -1);
423                 }
424
425         gq_gtk_container_add(GTK_WIDGET(scrolled), lc->listview);
426         gtk_widget_show(lc->listview);
427
428         pref_label_new(lc->box, _("(drag to change order)"));
429
430         return lc->box;
431 }
432 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */