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