~elementary-apps/pantheon-files/trunk

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
/***
    TopMenu.cs

    Authors:
       mathijshenquet <mathijs.henquet@gmail.com>
       ammonkey <am.monkeyd@gmail.com>

    Copyright (c) 2010 mathijshenquet

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
***/

namespace Marlin.View.Chrome
{
    public class TopMenu : Gtk.HeaderBar {
        public ViewSwitcher? view_switcher;
        public LocationBar? location_bar;
        public Chrome.ButtonWithMenu button_forward;
        public Chrome.ButtonWithMenu button_back;

        public bool locked_focus {get; private set; default = false;}

        public bool working {
            set {
                location_bar.sensitive = !value;
            }
        }

        public bool can_go_back {
           set {
                button_back.sensitive = value;
            }
        }

        public bool can_go_forward {
           set {
                button_forward.sensitive = value;
            }
        }

        public signal void forward (int steps);
        public signal void back (int steps);  /* TODO combine using negative step */


        public signal void focus_location_request (GLib.File? location);
        public signal void path_change_request (string path, Marlin.OpenFlag flag);
        public signal void escape ();
        public signal void reload_request ();

        public TopMenu (ViewSwitcher switcher) {
            button_back = new Marlin.View.Chrome.ButtonWithMenu.from_icon_name ("go-previous-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
            button_forward = new Marlin.View.Chrome.ButtonWithMenu.from_icon_name ("go-next-symbolic", Gtk.IconSize.LARGE_TOOLBAR);
            button_back.tooltip_text = _("Previous");
            button_back.show_all ();
            pack_start (button_back);

            button_forward.tooltip_text = _("Next");
            button_forward.show_all ();
            pack_start (button_forward);


            button_forward.slow_press.connect (() => {
                forward (1);
            });

            button_back.slow_press.connect (() => {
                back (1);
            });

            view_switcher = switcher;
            view_switcher.margin_end = 20;
            view_switcher.show_all ();
            pack_start (view_switcher);

            location_bar = new LocationBar ();
            connect_location_bar_signals ();
            location_bar.show_all ();
            pack_start (location_bar);

            show ();
        }

        private void connect_location_bar_signals () {
            location_bar.reload_request.connect (() => {
                reload_request ();
            });
            location_bar.focus_file_request.connect ((file) => {
                focus_location_request (file);
            });
            location_bar.focus_in_event.connect ((event) => {
                locked_focus = true;
                return focus_in_event (event);
            });
            location_bar.focus_out_event.connect ((event) => {
                locked_focus = false;
                return focus_out_event (event);
            });
            location_bar.path_change_request.connect ((path, flag) => {
                path_change_request (path, flag);
            });
            location_bar.escape.connect (() => {escape ();});
        }

        public bool enter_search_mode (bool local_only, bool begins_with_only) {
            return location_bar.enter_search_mode (local_only, begins_with_only);
        }

        public bool enter_navigate_mode () {
            return location_bar.enter_navigate_mode ();
        }

        public void set_back_menu (Gee.List<string> path_list) {
            /* Clear the back menu and re-add the correct entries. */
            var back_menu = new Gtk.Menu ();
            var n = 1;
            foreach (string path in path_list) {
                int cn = n++; /* No i'm not mad, thats just how closures work in vala (and other langs).
                               * You see if I would just use back(n) the reference to n would be passed
                               * in the closure, resulting in a value of n which would always be n=1. So
                               * by introducting a new variable I can bypass this anoyance.
                               */
                var item = new Gtk.MenuItem.with_label (PF.FileUtils.sanitize_path (path));
                item.activate.connect (() => {
                    back(cn);
                });
                back_menu.insert (item, -1);
            }

            back_menu.show_all ();
            button_back.menu = back_menu;
        }

        public void set_forward_menu (Gee.List<string> path_list) {
            /* Same for the forward menu */
            var forward_menu = new Gtk.Menu ();
            var n = 1;
            foreach (string path in path_list) {
                int cn = n++; /* For explanation look up */
                var item = new Gtk.MenuItem.with_label (PF.FileUtils.sanitize_path (path));
                item.activate.connect (() => {
                    forward (cn);
                });
                forward_menu.insert (item, -1);
            }

            forward_menu.show_all ();
            button_forward.menu = forward_menu;
        }

        public void update_location_bar (string new_path, bool with_animation = true) {
            location_bar.with_animation = with_animation;
            location_bar.set_display_path (new_path);
            location_bar.with_animation = true;
        }

        public void cancel () {
            location_bar.cancel ();
        }
    }
}