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