summaryrefslogtreecommitdiff
path: root/midori/midori-window.vala
blob: 2bd9f760d81c247f8b853401f09404c5f6d7f2fa (plain)
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
/*
 Copyright (C) 2015 Christian Dywan <christian@twotoasts.de>

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

 See the file COPYING for the full license text.
*/

namespace Midori {
    public class Window : Gtk.Window {
        Gtk.Widget? _toolbar = null;
        public Gtk.Widget? toolbar { get {
            if (_toolbar == null) {
#if HAVE_GTK3
                if (strcmp (Environment.get_variable ("GTK_CSD"), "1") == 0) {
                    var toolbar = new Gtk.HeaderBar ();
                    toolbar.show_close_button = true;
                    toolbar.show ();
                    toolbar.get_style_context ().add_class ("midori-titlebar");
                    _toolbar = toolbar;
                    return _toolbar;
                }
#endif
                var toolbar = new Gtk.Toolbar ();
                toolbar.show_arrow = true;
#if HAVE_GTK3
                toolbar.get_style_context ().add_class ("primary-toolbar");
#endif
                toolbar.popup_context_menu.connect ((x, y, button) => {
                    return button == 3 && context_menu (toolbar); });
                _toolbar = toolbar;
            }
            return _toolbar;
        } }

        public string actions { get; set; default = ""; }
        string extra_actions { get; set; default = ""; }
        List<Gtk.ActionGroup> action_groups;
        public signal bool context_menu (Gtk.Widget widget, Gtk.Action? action=null);
        Gtk.Box? box = null;
        List<Gtk.Widget> toolbars;

        Gtk.Widget? _contents = null;
        public Gtk.Widget? contents { get {
            return _contents;
        } set {
            if (_contents != null)
                box.remove (_contents);
            _contents = value;
            _contents.show ();
            if (box != null)
                box.pack_end (_contents, true, true, 0);
        } }

        public void add_action_group (Gtk.ActionGroup action_group) {
            action_groups.append (action_group);
        }

        public bool show_menubar { get; set; default = false; }

        [CCode (type = "GtkWidget*")]
        public Window () {
        }

        public Gtk.ToolItem? get_tool_item (string name) {
            /* Name is the empty string if actions has ,, or trailing , */
            if (name == "")
                return null;
            /* Shown in the notebook, no need to include in the toolbar */
            if (name == "TabNew")
                return null;
            foreach (unowned Gtk.ActionGroup action_group in action_groups) {
                var action = action_group.get_action (name);
                if (action != null) {
                    return create_tool_item (action);
                }
            }
            warning ("Action %s not known to Window", name);
            return null;
        }

        Gtk.ToolItem create_tool_item (Gtk.Action action) {
            var toolitem = (Gtk.ToolItem)action.create_tool_item ();
            /* Show label if button has no icon of any kind */
            if (action.icon_name == null && action.stock_id == null && action.gicon == null)
                toolitem.is_important = true;
            toolitem.get_child ().button_press_event.connect ((event) => {
                return event.button == 3 && context_menu (toolitem, action); });
            if (action.name == "CompactMenu")
            {
                toolitem.visible = !show_menubar;
                bind_property ("show-menubar", toolitem, "visible",
                    BindingFlags.DEFAULT | BindingFlags.INVERT_BOOLEAN);
            }
            return toolitem;
        }

        /**
         * Adds an action to the (browser) window.
         * Typically it will be displayed in the primary toolbar or headerbar.
         *
         * If @action is a ContextAction a menu will be displayed.
         *
         * Since: 0.6.0
         **/
        public void add_action (Gtk.Action action) {
            action_groups.nth_data (0).add_action (action);
            extra_actions += "," + action.name;
            update_toolbar ();
        }

        /**
         * Remove an action from the (browser) window.
         *
         * Since: 0.6.0
         **/
        public void remove_action (Gtk.Action action) {
            action_groups.nth_data (0).remove_action (action);
            extra_actions = extra_actions.replace ("," + action.name, "");
            update_toolbar ();
        }

        void update_toolbar () {
            var container = (Gtk.Container)_toolbar;
            foreach (unowned Gtk.Widget toolitem in container.get_children ())
                container.remove (toolitem);

            /* Always include app menu (visible depending on menubar) and extensions. */
            string all_actions;
            if ("CompactMenu" in actions)
                all_actions = actions.replace ("CompactMenu", extra_actions + ",CompactMenu");
            else
                all_actions = actions + "," + extra_actions + ",CompactMenu";
            string[] names = all_actions.split (",");

#if HAVE_GTK3
            var headerbar = _toolbar as Gtk.HeaderBar;
            if (headerbar != null) {
                var tail = new List<Gtk.ToolItem> ();
                foreach (unowned string name in names) {
                    var toolitem = get_tool_item (name);
                    if (toolitem == null)
                        continue;
                    var widget = toolitem.get_child ();
                    if (widget is Gtk.Alignment)
                        widget = ((Gtk.Bin)widget).get_child ();
                    if (name == "Location") {
                        widget.set ("margin-top", 1, "margin-bottom", 1);
                        ((Gtk.Entry)widget).width_chars = 36;
                        headerbar.custom_title = toolitem;
                        headerbar.custom_title.set (
                            "margin-start", 25, "margin-end", 25,
                            "margin-top", 5, "margin-bottom", 5);
                    } else if (name == "Search") {
                        ((Gtk.Entry)widget).width_chars = 12;
                        tail.append (toolitem);
                    } else if (all_actions.index_of (name) > all_actions.index_of ("Location"))
                        tail.append (toolitem);
                    else
                        headerbar.pack_start (toolitem);
                }

                /* Pack end appends, so we need to pack in reverse order */
                tail.reverse ();
                foreach (unowned Gtk.ToolItem toolitem in tail)
                    headerbar.pack_end (toolitem);

                set_titlebar (headerbar);
                return;
            }
#endif

            var toolbar = (Gtk.Toolbar)_toolbar;
            string? previous = null;
            Gtk.ToolItem? toolitem_previous = null;
            foreach (unowned string name in names) {
                var toolitem = get_tool_item (name);
                if (toolitem == null)
                    continue;
                if ((name == "Location" || name == "Search")
                 && (previous == "Location" || previous == "Search")) {
                    toolitem_previous.ref ();
                    toolbar.remove (toolitem_previous);
                    var paned = new Midori.PanedAction ();
                    paned.set_child1 (toolitem_previous, previous, previous != "Search", true);
                    paned.set_child2 (toolitem, name, name != "Search", true);
                    /* Midori.Settings.search-width on Midori.Browser.settings */
                    Midori.Settings? settings = null;
                    get ("settings", ref settings);
                    var sizeable = name == "Search" ? toolitem : toolitem_previous;
                    sizeable.size_allocate.connect ((allocation) => {
                        settings.set ("search-width", allocation.width);
                    });
                    var requester = previous == "Search" ? toolitem_previous : toolitem;
                    requester.set_size_request (settings.search_width, -1);
                    toolitem = (Gtk.ToolItem)paned.create_tool_item ();
                    previous = null;
                    toolitem_previous.unref ();
                    toolitem_previous = null;
                } else {
                    previous = name;
                    toolitem_previous = toolitem;
                }
                toolbar.insert (toolitem, -1);
            }
        }

        public void add_toolbar (Gtk.Widget toolbar) {
            var _toolbar = toolbar as Gtk.Toolbar;
            if (_toolbar != null) {
#if HAVE_GTK3
                get_style_context ().add_class ("secondary-toolbar");
#endif
                _toolbar.popup_context_menu.connect ((x, y, button) => {
                    return button == 3 && context_menu (toolbar);
                });
            }
            if (box == null)
                toolbars.append (toolbar);
            else
                box.pack_start (toolbar, false, false);
        }

        construct {
            box = new Gtk.VBox (false, 0);
            box.show ();
            add (box);
            foreach (unowned Gtk.Widget toolbar in toolbars) {
                if (toolbar is Gtk.MenuBar)
                    box.pack_start (toolbar, false, false);
            }
            if (toolbar is Gtk.Toolbar)
                box.pack_start (toolbar, false, false);
            foreach (unowned Gtk.Widget toolbar in toolbars) {
                if (!(toolbar is Gtk.MenuBar))
                    box.pack_start (toolbar, false, false);
            }
            if (_contents != null)
                box.pack_end (_contents, true, true, 0);
            if (actions != "")
                update_toolbar ();
            notify["actions"].connect ((pspec) => { update_toolbar (); });
        }
    }
}