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