~elementary-apps/pantheon-files/trunk

« back to all changes in this revision

Viewing changes to filechooser-module/FileChooserDialog.vala

Merge trunk to r2319

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*-
2
 
 * Copyright (c) 2015 elementary LLC (http://launchpad.net/elementary)
 
2
 * Copyright (c) 2015-2016 elementary LLC (http://launchpad.net/elementary)
3
3
 *
4
4
 * This library is free software; you can redistribute it and/or
5
5
 * modify it under the terms of the GNU Library General Public
19
19
 * Authored by: Adam Bieńkowski <donadigos159@gmail.com>
20
20
 */
21
21
 
 
22
/*** The Gtk.FileChooserWidget widget names and paths can be found in "gtkfilechooserwidget.ui"
 
23
 *   in the Gtk+3 source code package.  Changes to that file could break this code.
 
24
***/
22
25
public class CustomFileChooserDialog : Object {
23
26
    private static Gtk.FileChooserDialog chooser_dialog;
24
27
    private static Gtk.Widget rootwidget;
32
35
    /* Paths to widgets */
33
36
    private const string[] GTK_PATHBAR_PATH = { "widget", "browse_widgets_box", "browse_files_box", "browse_header_revealer" };
34
37
    private const string[] GTK_FILTERCHOOSER_PATH = { "extra_and_filters", "filter_combo_hbox" };
 
38
    private const string[] GTK_TREEVIEW_PATH = { "browse_files_stack", "browse_files_swin", "browse_files_tree_view" };
35
39
    private const string PLACES_SIDEBAR_PATH = "places_sidebar";
36
40
 
37
41
    private GLib.Queue<string> previous_paths;
42
46
    private string current_path = null;
43
47
    private bool is_previous = false;
44
48
    private bool is_button_next = false;
 
49
    private bool is_single_click = true;
 
50
    private bool can_activate = true;
45
51
 
46
52
    public CustomFileChooserDialog (Gtk.FileChooserDialog dialog) {
47
53
        previous_paths = new GLib.Queue<string> ();
51
57
        chooser_dialog.can_focus = true;
52
58
        chooser_dialog.deletable = false;
53
59
 
 
60
        var settings = new Settings ("org.pantheon.files.preferences");
 
61
        is_single_click = settings.get_boolean ("single-click");
 
62
 
54
63
        assign_container_box ();
55
64
        remove_gtk_widgets ();
56
65
        setup_filter_box ();
131
140
            (root as Gtk.Container).get_children ().foreach ((w0) => {
132
141
                if (w0.get_name () == GTK_PATHBAR_PATH[0]) {
133
142
                    /* Add top separator between headerbar and filechooser when is not Save action */
134
 
                        var chooserwidget = w0 as Gtk.Container;
135
 
                        chooserwidget.vexpand = true;
136
 
 
137
 
                        (root as Gtk.Container).remove (w0);
138
 
                        var root_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
 
143
                    var chooserwidget = w0 as Gtk.Container;
 
144
                    chooserwidget.vexpand = true;
 
145
 
 
146
                    (root as Gtk.Container).remove (w0);
 
147
                    var root_box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
 
148
                    root_box.add (new Gtk.Separator (Gtk.Orientation.HORIZONTAL));
 
149
                    root_box.add (chooserwidget); 
 
150
 
 
151
                    if (chooser_dialog.get_extra_widget () == null) {
139
152
                        root_box.add (new Gtk.Separator (Gtk.Orientation.HORIZONTAL));
140
 
                        root_box.add (chooserwidget); 
141
 
 
142
 
                        if (chooser_dialog.get_extra_widget () == null) {
143
 
                            root_box.add (new Gtk.Separator (Gtk.Orientation.HORIZONTAL));
144
 
                        }
145
 
 
146
 
                        (root as Gtk.Container).add (root_box);
147
 
                        rootwidget = chooserwidget;
148
 
                        rootwidget = w0;
149
 
                        rootwidget.can_focus = true;
150
 
                        transform_rootwidget_container (rootwidget, w0);
 
153
                    }
 
154
 
 
155
                    (root as Gtk.Container).add (root_box);
 
156
                    rootwidget = chooserwidget;
 
157
                    rootwidget = w0;
 
158
                    rootwidget.can_focus = true;
 
159
                    transform_rootwidget_container (rootwidget, w0);
151
160
                }
152
161
            });
153
162
        });
222
231
                });
223
232
 
224
233
                (w2 as Gtk.Container).remove (w3);
225
 
            }
226
 
        });
 
234
            } else if (w3.get_name () ==  "list_and_preview_box") { /* file browser list and preview box */
 
235
                var tv = find_tree_view (w3);
 
236
                if (tv != null) {
 
237
                    /* set its click behaviour the same as pantheon-files setting */
 
238
                    tv.set_activate_on_single_click (is_single_click);
 
239
                    if (is_single_click) {
 
240
                        /* We need to modify native behaviour to only activate on folders */
 
241
                        tv.add_events (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK);
 
242
                        tv.button_press_event.connect (on_tv_button_press_event);
 
243
                        tv.button_release_event.connect (on_tv_button_release_event);
 
244
                    }
 
245
                }
 
246
            }
 
247
        });
 
248
    }
 
249
 
 
250
    private Gtk.TreeView? find_tree_view (Gtk.Widget browser_box) {
 
251
        /* Locate the TreeView */
 
252
        Gtk.TreeView? tv = null;
 
253
        ((Gtk.Container)browser_box).get_children ().foreach ((w) => {
 
254
            if (w.get_name () == GTK_TREEVIEW_PATH[0]) {
 
255
                ((Gtk.Container)w).get_children ().foreach ((w) => {
 
256
                    if (w.name == "GtkBox") {
 
257
                        ((Gtk.Container)w).get_children ().foreach ((w) => {
 
258
                            if (w.get_name () == GTK_TREEVIEW_PATH[1]) {
 
259
                                ((Gtk.Container)w).get_children ().foreach ((w) => {
 
260
                                    if (w.get_name () == GTK_TREEVIEW_PATH[2]) {
 
261
                                        tv = (Gtk.TreeView)w;
 
262
                                    }
 
263
                                });
 
264
                            }
 
265
                        });
 
266
                    }
 
267
                });
 
268
            }
 
269
        });
 
270
 
 
271
        return tv;
227
272
    }
228
273
 
229
274
    private void assign_container_box () {
266
311
            ((Gtk.ButtonBox) container_box).set_child_secondary (grid, true);
267
312
        }
268
313
    }
 
314
 
 
315
    private bool on_tv_button_press_event (Gtk.Widget w, Gdk.EventButton event) {
 
316
        can_activate = false;
 
317
        if (event.type == Gdk.EventType.@2BUTTON_PRESS) {
 
318
            can_activate = true;
 
319
            return false;
 
320
        }
 
321
 
 
322
        if (w == null) {
 
323
            return false;
 
324
        }
 
325
 
 
326
        Gtk.TreeView tv = ((Gtk.TreeView)(w));
 
327
        Gtk.TreePath? path = null;
 
328
        int cell_x, cell_y;
 
329
 
 
330
        tv.get_path_at_pos ((int)(event.x), (int)(event.y), out path, null, out cell_x, out cell_y);
 
331
 
 
332
        if (path != null) {
 
333
            var model = tv.get_model ();
 
334
            Gtk.TreeIter? iter = null;
 
335
            if (model.get_iter (out iter, path)) {
 
336
                bool is_folder;
 
337
                model.@get (iter, 5, out is_folder);
 
338
                if (is_folder) {
 
339
                    can_activate = true;
 
340
                }
 
341
            }
 
342
        }
 
343
 
 
344
        return false;
 
345
    }
 
346
 
 
347
    private bool on_tv_button_release_event (Gdk.EventButton event) {
 
348
        return !can_activate;
 
349
    }
269
350
}