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