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