1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
/*-
* Copyright (c) 2015-2016 elementary LLC (http://launchpad.net/elementary)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authored by: Adam Bieńkowski <donadigos159@gmail.com>
*/
/*** The Gtk.FileChooserWidget widget names and paths can be found in "gtkfilechooserwidget.ui"
* in the Gtk+3 source code package. Changes to that file could break this code.
***/
public class CustomFileChooserDialog : Object {
private static Gtk.FileChooserDialog chooser_dialog;
private static Gtk.Widget rootwidget;
private static Gtk.Box container_box;
private static Gtk.Button? gtk_folder_button = null;
/* Response to get parent of the bottom box */
private const int BUTTON_RESPONSE = -6;
/* Paths to widgets */
private const string[] GTK_PATHBAR_PATH = { "widget", "browse_widgets_box", "browse_files_box", "browse_header_revealer" };
private const string[] GTK_FILTERCHOOSER_PATH = { "extra_and_filters", "filter_combo_hbox" };
private const string[] GTK_TREEVIEW_PATH = { "browse_files_stack", "browse_files_swin", "browse_files_tree_view" };
private const string PLACES_SIDEBAR_PATH = "places_sidebar";
private GLib.Queue<string> previous_paths;
private GLib.Queue<string> next_paths;
private bool filters_available = false;
private string current_path = null;
private bool is_previous = false;
private bool is_button_next = false;
private bool is_single_click = true;
private bool can_activate = true;
public CustomFileChooserDialog (Gtk.FileChooserDialog dialog) {
previous_paths = new GLib.Queue<string> ();
next_paths = new GLib.Queue<string> ();
/* The "chooser_dialog" variable is the main dialog */
chooser_dialog = dialog;
chooser_dialog.can_focus = true;
chooser_dialog.deletable = false;
var settings = new Settings ("org.pantheon.files.preferences");
is_single_click = settings.get_boolean ("single-click");
assign_container_box ();
remove_gtk_widgets ();
setup_filter_box ();
var header_bar = new Gtk.HeaderBar ();
var button_back = new Gtk.Button.from_icon_name ("go-previous-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
button_back.tooltip_text = _("Previous");
button_back.sensitive = false;
var button_forward = new Gtk.Button.from_icon_name ("go-next-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
button_forward.tooltip_text = _("Next");
button_forward.sensitive = false;
var location_bar = new Marlin.View.Chrome.BasicLocationBar ();
location_bar.set_display_path (chooser_dialog.get_current_folder_uri ());
location_bar.hexpand = true;
header_bar.pack_start (button_back);
header_bar.pack_start (button_forward);
header_bar.pack_start (location_bar);
if ((gtk_folder_button != null) && (chooser_dialog.get_action () != Gtk.FileChooserAction.OPEN)) {
gtk_folder_button.image = new Gtk.Image.from_icon_name ("folder-new", Gtk.IconSize.LARGE_TOOLBAR);
((Gtk.Container) gtk_folder_button.get_parent ()).remove (gtk_folder_button);
header_bar.pack_end (gtk_folder_button);
}
chooser_dialog.set_titlebar (header_bar);
chooser_dialog.show_all ();
button_back.clicked.connect (() => {
is_previous = true;
chooser_dialog.set_current_folder_uri (previous_paths.pop_head ());
});
button_forward.clicked.connect (() => {
is_button_next = true;
chooser_dialog.set_current_folder_uri (next_paths.pop_head ());
});
chooser_dialog.current_folder_changed.connect (() => {
var previous_path = current_path;
current_path = chooser_dialog.get_current_folder_uri ();
if (previous_path == null || previous_path == current_path) {
location_bar.set_display_path (current_path);
return;
}
if (is_previous) {
next_paths.push_head (previous_path);
is_previous = false;
} else {
previous_paths.push_head (previous_path);
if (!is_button_next) {
next_paths.clear ();
} else {
is_button_next = false;
}
}
button_back.sensitive = !previous_paths.is_empty ();
button_forward.sensitive = !next_paths.is_empty ();
location_bar.set_display_path (current_path);
});
location_bar.path_change_request.connect ((uri) => {
chooser_dialog.set_current_folder (uri);
});
}
/*
* Playing with the native Gtk dialog.
*/
/* Remove GTK's native path bar and FileFilter chooser by widgets names */
private void remove_gtk_widgets () {
chooser_dialog.get_children ().foreach ((root) => {
(root as Gtk.Container).get_children ().foreach ((w0) => {
if (w0.get_name () == GTK_PATHBAR_PATH[0]) {
/* Add top separator between headerbar and filechooser when is not Save action */
var chooserwidget = w0 as Gtk.Container;
chooserwidget.vexpand = true;
(root as Gtk.Container).remove (w0);
var root_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
root_box.add (new Gtk.Separator (Gtk.Orientation.HORIZONTAL));
root_box.add (chooserwidget);
if (chooser_dialog.get_extra_widget () == null) {
root_box.add (new Gtk.Separator (Gtk.Orientation.HORIZONTAL));
}
(root as Gtk.Container).add (root_box);
rootwidget = chooserwidget;
rootwidget = w0;
rootwidget.can_focus = true;
transform_rootwidget_container (rootwidget, w0);
}
});
});
}
private void transform_rootwidget_container (Gtk.Widget rootwidget, Gtk.Widget w0) {
(rootwidget as Gtk.Container).get_children ().foreach ((w1) => {
if (w1.name == "GtkBox" && w1.get_name () != GTK_PATHBAR_PATH[1]) {
w1.ref ();
(rootwidget as Gtk.Container).remove (w1);
(w1 as Gtk.Container).get_children ().foreach ((grid) => {
grid.ref ();
grid.margin = 0;
grid.valign = Gtk.Align.CENTER;
((Gtk.Container) grid).border_width = 0;
(w1 as Gtk.Container).remove (grid);
container_box.pack_start (grid);
((Gtk.ButtonBox) container_box).set_child_secondary (grid, true);
grid.unref ();
});
w1.unref ();
container_box.show_all ();
} else if (w1.get_name () == GTK_PATHBAR_PATH[1]) {
transform_w1_container (w1);
} else {
if (w1.get_name () == GTK_FILTERCHOOSER_PATH[0]) {
/* Remove extra_and_filters if there is no extra widget */
if (chooser_dialog.get_extra_widget () == null) {
(w0 as Gtk.Container).remove (w1);
} else {
(w1 as Gtk.Container).get_children ().foreach ((w5) => {
if (w5.get_name () == GTK_FILTERCHOOSER_PATH[1])
(w1 as Gtk.Container).remove (w5);
});
}
}
}
});
}
private void transform_w1_container (Gtk.Widget w1) {
(w1 as Gtk.Container).get_children ().foreach ((paned) => {
(paned as Gtk.Container).get_children ().foreach ((w2) => {
if (w2 is Gtk.PlacesSidebar) {
(w2 as Gtk.PlacesSidebar).show_desktop = false;
(w2 as Gtk.PlacesSidebar).show_enter_location = false;
} else {
transform_w2_container (w2);
}
});
});
}
private void transform_w2_container (Gtk.Widget w2) {
(w2 as Gtk.Container).get_children ().foreach ((w3) => {
if (w3.get_name () == GTK_PATHBAR_PATH[3]) {
(w3 as Gtk.Container).get_children ().foreach ((w4) => {
(w4 as Gtk.Container).get_children ().foreach ((w5) => {
(w5 as Gtk.Container).get_children ().foreach ((w6) => {
if (w6 is Gtk.Box) {
(w6 as Gtk.Container).get_children ().foreach ((w7) => {
if (w7 is Gtk.Button) {
/* Register the button so we can use it's signal */
gtk_folder_button = w7 as Gtk.Button;
}
});
}
});
});
});
(w2 as Gtk.Container).remove (w3);
} else if (w3.get_name () == "list_and_preview_box") { /* file browser list and preview box */
var tv = find_tree_view (w3);
if (tv != null) {
/* set its click behaviour the same as pantheon-files setting */
tv.set_activate_on_single_click (is_single_click);
if (is_single_click) {
/* We need to modify native behaviour to only activate on folders */
tv.add_events (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK);
tv.button_press_event.connect (on_tv_button_press_event);
tv.button_release_event.connect (on_tv_button_release_event);
}
}
}
});
}
private Gtk.TreeView? find_tree_view (Gtk.Widget browser_box) {
/* Locate the TreeView */
Gtk.TreeView? tv = null;
((Gtk.Container)browser_box).get_children ().foreach ((w) => {
if (w.get_name () == GTK_TREEVIEW_PATH[0]) {
((Gtk.Container)w).get_children ().foreach ((w) => {
if (w.name == "GtkBox") {
((Gtk.Container)w).get_children ().foreach ((w) => {
if (w.get_name () == GTK_TREEVIEW_PATH[1]) {
((Gtk.Container)w).get_children ().foreach ((w) => {
if (w.get_name () == GTK_TREEVIEW_PATH[2]) {
tv = (Gtk.TreeView)w;
}
});
}
});
}
});
}
});
return tv;
}
private void assign_container_box () {
container_box = chooser_dialog.get_action_area () as Gtk.Box;
container_box.valign = Gtk.Align.CENTER;
container_box.get_children ().foreach ((child) => {
child.valign = Gtk.Align.CENTER;
});
}
private void setup_filter_box () {
var filters = chooser_dialog.list_filters ();
var current_filter_name = chooser_dialog.get_filter ().get_filter_name ();
if (filters.length () > 0) {
filters_available = true;
var combo_box = new Gtk.ComboBoxText ();
combo_box.changed.connect (() => {
chooser_dialog.list_filters ().foreach ((filter) => {
if (filter.get_filter_name () == combo_box.get_active_text ())
chooser_dialog.set_filter (filter);
});
});
var index = 0;
filters.foreach ((filter) => {
var name = filter.get_filter_name ();
combo_box.append_text (name);
if (name == current_filter_name) {
combo_box.active = index;
}
index++;
});
var grid = new Gtk.Grid ();
grid.valign = Gtk.Align.CENTER;
grid.add (combo_box);
container_box.pack_end (grid);
((Gtk.ButtonBox) container_box).set_child_secondary (grid, true);
}
}
private bool on_tv_button_press_event (Gtk.Widget w, Gdk.EventButton event) {
can_activate = false;
if (event.type == Gdk.EventType.@2BUTTON_PRESS) {
can_activate = true;
return false;
}
if (w == null) {
return false;
}
Gtk.TreeView tv = ((Gtk.TreeView)(w));
Gtk.TreePath? path = null;
int cell_x, cell_y;
tv.get_path_at_pos ((int)(event.x), (int)(event.y), out path, null, out cell_x, out cell_y);
if (path != null) {
var model = tv.get_model ();
Gtk.TreeIter? iter = null;
if (model.get_iter (out iter, path)) {
bool is_folder;
model.@get (iter, 5, out is_folder);
if (is_folder) {
can_activate = true;
}
}
}
return false;
}
private bool on_tv_button_release_event (Gdk.EventButton event) {
return !can_activate;
}
}
|