1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /*
4 * GThumb
5 *
6 * Copyright (C) 2009 The Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23 #include <gtk/gtk.h>
24 #include "dlg-sort-order.h"
25 #include "gth-browser.h"
26 #include "gth-main.h"
27 #include "gtk-utils.h"
28
29
30 #define GET_WIDGET(name) _gtk_builder_get_widget (data->builder, (name))
31
32
33 typedef struct {
34 GthBrowser *browser;
35 GtkBuilder *builder;
36 GtkWidget *dialog;
37 GthFileDataSort *current_sort_type;
38 GList *sort_types;
39 } DialogData;
40
41
42 static void
destroy_cb(GtkWidget * widget,DialogData * data)43 destroy_cb (GtkWidget *widget,
44 DialogData *data)
45 {
46 gth_browser_set_dialog (data->browser, "sort-order", NULL);
47 g_list_free (data->sort_types);
48 g_object_unref (data->builder);
49 g_free (data);
50 }
51
52
53 static void
apply_sort_order(GtkWidget * widget,DialogData * data)54 apply_sort_order (GtkWidget *widget,
55 DialogData *data)
56 {
57 gth_browser_set_sort_order (data->browser,
58 data->current_sort_type,
59 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (GET_WIDGET ("inverse_checkbutton"))));
60 }
61
62
63 typedef struct {
64 DialogData *data;
65 GthFileDataSort *sort_type;
66 } ButtonData;
67
68
69 static void
button_data_free(ButtonData * button_data)70 button_data_free (ButtonData *button_data)
71 {
72 g_return_if_fail (button_data != NULL);
73 g_free (button_data);
74 }
75
76
77 static void
order_button_toggled_cb(GtkToggleButton * button,gpointer user_data)78 order_button_toggled_cb (GtkToggleButton *button,
79 gpointer user_data)
80 {
81 ButtonData *button_data = user_data;
82 DialogData *data = button_data->data;
83
84 if (! gtk_toggle_button_get_active (button))
85 return;
86
87 data->current_sort_type = button_data->sort_type;
88 apply_sort_order (NULL, data);
89 }
90
91
92 void
dlg_sort_order(GthBrowser * browser)93 dlg_sort_order (GthBrowser *browser)
94 {
95 DialogData *data;
96 GtkWidget *first_button;
97 GthFileData *file_data;
98 GList *scan;
99 gboolean sort_inverse;
100
101 if (gth_browser_get_dialog (browser, "sort-order") != NULL) {
102 gtk_window_present (GTK_WINDOW (gth_browser_get_dialog (browser, "sort-order")));
103 return;
104 }
105
106 data = g_new0 (DialogData, 1);
107 data->browser = browser;
108 data->builder = _gtk_builder_new_from_file ("sort-order.ui", NULL);
109
110 /* Get the widgets. */
111
112 data->dialog = g_object_new (GTK_TYPE_DIALOG,
113 "title", _("Sort By"),
114 "transient-for", GTK_WINDOW (browser),
115 "modal", FALSE,
116 "destroy-with-parent", FALSE,
117 "use-header-bar", _gtk_settings_get_dialogs_use_header (),
118 NULL);
119 gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (data->dialog))),
120 _gtk_builder_get_widget (data->builder, "dialog_content"));
121 gtk_dialog_add_buttons (GTK_DIALOG (data->dialog),
122 _GTK_LABEL_OK, GTK_RESPONSE_CLOSE,
123 NULL);
124
125 gth_browser_set_dialog (browser, "sort-order", data->dialog);
126 g_object_set_data (G_OBJECT (data->dialog), "dialog_data", data);
127
128 /* Set widgets data. */
129
130 file_data = gth_browser_get_location_data (data->browser);
131 if (file_data != NULL) {
132 data->current_sort_type = gth_main_get_sort_type (g_file_info_get_attribute_string (file_data->info, "sort::type"));
133 sort_inverse = g_file_info_get_attribute_boolean (file_data->info, "sort::inverse");
134 }
135 else
136 gth_browser_get_sort_order (data->browser, &data->current_sort_type, &sort_inverse);
137
138 first_button = NULL;
139 data->sort_types = gth_main_get_all_sort_types ();
140 for (scan = data->sort_types; scan; scan = scan->next) {
141 GthFileDataSort *sort_type = scan->data;
142 GtkWidget *button;
143 ButtonData *button_data;
144
145 button = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (first_button), _(sort_type->display_name));
146 if (scan == data->sort_types)
147 first_button = button;
148 gtk_widget_show (button);
149 gtk_box_pack_start (GTK_BOX (GET_WIDGET ("sort_order_box")), button, FALSE, FALSE, 0);
150 if (strcmp (sort_type->name, data->current_sort_type->name) == 0)
151 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
152
153 button_data = g_new0 (ButtonData, 1);
154 button_data->data = data;
155 button_data->sort_type = sort_type;
156
157 g_signal_connect_data (button,
158 "toggled",
159 G_CALLBACK (order_button_toggled_cb),
160 button_data,
161 (GClosureNotify) button_data_free,
162 0);
163 }
164
165 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (GET_WIDGET ("inverse_checkbutton")), sort_inverse);
166
167 /* Set the signals handlers. */
168
169 g_signal_connect (G_OBJECT (data->dialog),
170 "destroy",
171 G_CALLBACK (destroy_cb),
172 data);
173 g_signal_connect_swapped (gtk_dialog_get_widget_for_response (GTK_DIALOG (data->dialog), GTK_RESPONSE_CLOSE),
174 "clicked",
175 G_CALLBACK (gtk_widget_destroy),
176 G_OBJECT (data->dialog));
177 g_signal_connect (GET_WIDGET ("inverse_checkbutton"),
178 "toggled",
179 G_CALLBACK (apply_sort_order),
180 data);
181
182 /* run dialog. */
183
184 gtk_window_set_transient_for (GTK_WINDOW (data->dialog),
185 GTK_WINDOW (browser));
186 gtk_window_set_modal (GTK_WINDOW (data->dialog), FALSE);
187 gtk_widget_show (data->dialog);
188 }
189