2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
23 #include "layout-config.h"
36 LayoutLocation a, b, c;
52 static LayoutStyle layout_config_styles[] = {
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) }
60 static gint layout_config_style_count = sizeof(layout_config_styles) / sizeof(LayoutStyle);
62 static const gchar *layout_titles[] = { N_("Tools"), N_("Files"), N_("Image") };
65 static void layout_config_destroy(GtkWidget *, gpointer data)
67 auto lc = static_cast<LayoutConfig *>(data);
69 g_list_free(lc->style_widgets);
73 static void layout_config_set_order(LayoutLocation l, gint n,
74 LayoutLocation *a, LayoutLocation *b, LayoutLocation *c)
90 static void layout_config_from_data(gint style, gint oa, gint ob, gint oc,
91 LayoutLocation *la, LayoutLocation *lb, LayoutLocation *lc)
95 style = CLAMP(style, 0, layout_config_style_count);
97 ls = layout_config_styles[style];
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);
104 void layout_config_parse(gint style, const gchar *order,
105 LayoutLocation *a, LayoutLocation *b, LayoutLocation *c)
109 layout_config_order_from_text(order, &na, &nb, &nc);
110 layout_config_from_data(style, na, nb, nc, a, b, c);
113 static void layout_config_list_order_set(LayoutConfig *lc, gint src, gint dest)
120 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(lc->listview)));
123 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
128 gtk_list_store_set(store, &iter, COLUMN_TEXT, _(layout_titles[src]), COLUMN_KEY, src, -1);
132 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
136 static gint layout_config_list_order_get(LayoutConfig *lc, gint n)
143 store = gtk_tree_view_get_model(GTK_TREE_VIEW(lc->listview));
145 valid = gtk_tree_model_get_iter_first(store, &iter);
151 gtk_tree_model_get(store, &iter, COLUMN_KEY, &val, -1);
155 valid = gtk_tree_model_iter_next(store, &iter);
160 void layout_config_set(GtkWidget *widget, gint style, const gchar *order)
166 lc = static_cast<LayoutConfig *>(g_object_get_data(G_OBJECT(widget), "layout_config"));
170 style = CLAMP(style, 0, layout_config_style_count);
171 button = static_cast<GtkWidget *>(g_list_nth_data(lc->style_widgets, style));
174 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
176 layout_config_order_from_text(order, &a, &b, &c);
178 layout_config_list_order_set(lc, a, 0);
179 layout_config_list_order_set(lc, b, 1);
180 layout_config_list_order_set(lc, c, 2);
183 gchar *layout_config_get(GtkWidget *widget, gint *style)
187 lc = static_cast<LayoutConfig *>(g_object_get_data(G_OBJECT(widget), "layout_config"));
189 /* this should not happen */
190 if (!lc) return nullptr;
194 lc->a = layout_config_list_order_get(lc, 0);
195 lc->b = layout_config_list_order_get(lc, 1);
196 lc->c = layout_config_list_order_get(lc, 2);
198 return layout_config_order_to_text(lc->a, lc->b, lc->c);
201 static void layout_config_widget_click_cb(GtkWidget *widget, gpointer data)
205 lc = static_cast<LayoutConfig *>(g_object_get_data(G_OBJECT(widget), "layout_config"));
207 if (lc) lc->style = GPOINTER_TO_INT(data);
210 static void layout_config_table_button(GtkWidget *table, LayoutLocation l, const gchar *text)
222 if (l & LAYOUT_LEFT) x2 = 1;
223 if (l & LAYOUT_RIGHT) x1 = 1;
224 if (l & LAYOUT_TOP) y2 = 1;
225 if (l & LAYOUT_BOTTOM) y1 = 1;
227 button = gtk_button_new_with_label(text);
228 gtk_widget_set_sensitive(button, FALSE);
229 gtk_widget_set_can_focus(button, FALSE);
230 gtk_grid_attach(GTK_GRID(table), button, x1, y1, x2 - x1, y2 - y1);
231 gtk_widget_show(button);
234 #define LAYOUT_STYLE_SIZE 48
236 static GtkWidget *layout_config_widget(GtkWidget *group, GtkWidget *box, gint style, LayoutConfig *lc)
241 ls = layout_config_styles[style];
246 group = gtk_toggle_button_new();
247 gtk_toggle_button_set_group(button, group);
249 group = gtk_radio_button_new(gtk_radio_button_get_group(GTK_RADIO_BUTTON(group)));
255 group = gtk_toggle_button_new();
257 group = gtk_radio_button_new(nullptr);
260 g_object_set_data(G_OBJECT(group), "layout_config", lc);
261 g_signal_connect(G_OBJECT(group), "clicked",
262 G_CALLBACK(layout_config_widget_click_cb), GINT_TO_POINTER(style));
263 gq_gtk_box_pack_start(GTK_BOX(box), group, FALSE, FALSE, 0);
265 table = gtk_grid_new();
267 layout_config_table_button(table, ls.a, "1");
268 layout_config_table_button(table, ls.b, "2");
269 layout_config_table_button(table, ls.c, "3");
271 gtk_widget_set_size_request(table, LAYOUT_STYLE_SIZE, LAYOUT_STYLE_SIZE);
272 gq_gtk_container_add(GTK_WIDGET(group), table);
273 gtk_widget_show(table);
275 gtk_widget_show(group);
280 static void layout_config_number_cb(GtkTreeViewColumn *, GtkCellRenderer *cell,
281 GtkTreeModel *store, GtkTreeIter *iter, gpointer)
287 tpath = gtk_tree_model_get_path(store, iter);
288 indices = gtk_tree_path_get_indices(tpath);
289 buf = g_strdup_printf("%d", indices[0] + 1);
290 gtk_tree_path_free(tpath);
291 g_object_set(G_OBJECT(cell), "text", buf, NULL);
295 GtkWidget *layout_config_new()
299 GtkWidget *group = nullptr;
302 GtkTreeViewColumn *column;
303 GtkCellRenderer *renderer;
306 lc = g_new0(LayoutConfig, 1);
308 lc->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
309 g_object_set_data(G_OBJECT(lc->box), "layout_config", lc);
311 g_signal_connect(G_OBJECT(lc->box), "destroy",
312 G_CALLBACK(layout_config_destroy), lc);
314 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
315 gq_gtk_box_pack_start(GTK_BOX(lc->box), hbox, FALSE, FALSE, 0);
316 for (i = 0; i < layout_config_style_count; i++)
318 group = layout_config_widget(group, hbox, i, lc);
319 lc->style_widgets = g_list_append(lc->style_widgets, group);
321 gtk_widget_show(hbox);
323 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
324 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
325 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
326 GTK_POLICY_NEVER, GTK_POLICY_NEVER);
327 gq_gtk_box_pack_start(GTK_BOX(lc->box), scrolled, FALSE, FALSE, 0);
328 gtk_widget_show(scrolled);
330 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_INT);
331 lc->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
332 g_object_unref(store);
334 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(lc->listview), FALSE);
335 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(lc->listview), FALSE);
336 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(lc->listview), TRUE);
338 column = gtk_tree_view_column_new();
339 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
341 renderer = gtk_cell_renderer_text_new();
342 gtk_tree_view_column_pack_start(column, renderer, FALSE);
343 gtk_tree_view_column_set_cell_data_func(column, renderer, layout_config_number_cb, lc, nullptr);
345 renderer = gtk_cell_renderer_text_new();
346 gtk_tree_view_column_pack_start(column, renderer, TRUE);
347 gtk_tree_view_column_add_attribute(column, renderer, "text", COLUMN_TEXT);
349 gtk_tree_view_append_column(GTK_TREE_VIEW(lc->listview), column);
351 for (i = 0; i < 3; i++)
355 gtk_list_store_append(store, &iter);
356 gtk_list_store_set(store, &iter, COLUMN_TEXT, _(layout_titles[i]), COLUMN_KEY, i, -1);
359 gq_gtk_container_add(GTK_WIDGET(scrolled), lc->listview);
360 gtk_widget_show(lc->listview);
362 pref_label_new(lc->box, _("(drag to change order)"));
367 static gchar num_to_text_char(gint n)
381 gchar *layout_config_order_to_text(gint a, gint b, gint c)
385 text = g_strdup(" ");
387 text[0] = num_to_text_char(a);
388 text[1] = num_to_text_char(b);
389 text[2] = num_to_text_char(c);
394 static gint text_char_to_num(const gchar *text, gint n)
396 if (text[n] == '3') return 2;
397 if (text[n] == '2') return 1;
401 void layout_config_order_from_text(const gchar *text, gint *a, gint *b, gint *c)
403 if (!text || strlen(text) < 3)
411 *a = text_char_to_num(text, 0);
412 *b = text_char_to_num(text, 1);
413 *c = text_char_to_num(text, 2);
416 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */