~and471/+junk/panel

« back to all changes in this revision

Viewing changes to src/wnck/windowlist.vala

  • Committer: Andrew
  • Date: 2011-10-17 08:35:18 UTC
  • Revision ID: at.higginson@gmail.com-20111017083518-4v8vbjtml35dmown
reorganiseĀ files
debuggingĀ support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright 2010 Andrew Higginson
2
 
 *
3
 
 * This program is free software; you can redistribute it and/or modify
4
 
 * it under the terms of the GNU General Public License as published by
5
 
 * the Free Software Foundation; either version 2 of the License, or
6
 
 * (at your option) any later version.
7
 
*/
8
 
 
9
 
public class WindowList : Gtk.HBox {
10
 
    
11
 
    private Gtk.Builder builder;
12
 
    private Wnck.Screen screen;
13
 
    private Gee.ArrayList<Wnck.Window> windows = new Gee.ArrayList<Wnck.Window>();
14
 
 
15
 
    public WindowList() {
16
 
        this.homogeneous = true;
17
 
        this.screen = Wnck.Screen.get_default();
18
 
        this.screen.window_opened.connect(this.on_screen_window_opened);
19
 
        this.screen.window_closed.connect(this.on_screen_window_closed);
20
 
        this.screen.active_window_changed.connect(this.on_screen_active_window_changed);
21
 
    }
22
 
 
23
 
    /* Private Functions */
24
 
    
25
 
    private WindowButton? get_button_for_window(Wnck.Window window) {
26
 
        foreach (var widget in this.get_children()) {
27
 
            var button = widget as WindowButton;
28
 
            if (button.window == window) {
29
 
                return button;
30
 
            }
31
 
        }
32
 
        return null;
33
 
    }
34
 
    
35
 
    /* Callbacks */
36
 
 
37
 
    private void on_screen_window_opened(Wnck.Window window) {
38
 
        this.windows.add(window);
39
 
        
40
 
        if (window.is_skip_tasklist()) {
41
 
            return;
42
 
        }
43
 
        
44
 
        var button = new WindowButton(window);
45
 
        button.window = window;
46
 
        button.click_event.connect(this.on_window_button_click_event);
47
 
        
48
 
        this.pack_start(button, true, true);
49
 
        button.show();
50
 
    }
51
 
    
52
 
    private void on_screen_window_closed(Wnck.Window window) {
53
 
        this.windows.remove(window);
54
 
        
55
 
        var button = this.get_button_for_window(window);
56
 
        if (button != null) {
57
 
            this.remove(button);
58
 
            button.click_event.disconnect(this.on_window_button_click_event);
59
 
            button.destroy();
60
 
        }
61
 
    }
62
 
    
63
 
    private void on_window_button_click_event(WindowButton button, Gdk.EventButton event) {
64
 
        if (Gdk.ModifierType.BUTTON1_MASK in event.state) {
65
 
            button.window.activate(event.time);
66
 
        } else if (Gdk.ModifierType.BUTTON3_MASK in event.state) {
67
 
            var menu = new WindowMenu(button);
68
 
            menu.popup(null, null, null, event.button, event.time);
69
 
            menu.ref();
70
 
        }
71
 
    }
72
 
    
73
 
    private void on_screen_active_window_changed(Wnck.Window? previous_active_window) {
74
 
        if (previous_active_window != null) {
75
 
            var button_previous_active = this.get_button_for_window(previous_active_window);
76
 
            if (button_previous_active != null) {
77
 
                button_previous_active.set_active(false);
78
 
            }
79
 
        }
80
 
    
81
 
        var active_window = this.screen.get_active_window();
82
 
        if (active_window != null) {
83
 
            var button = this.get_button_for_window(active_window);
84
 
            if (button != null) {
85
 
                button.set_active(true);
86
 
            }
87
 
        }
88
 
    }
89
 
 
90
 
    /* Public Functions */
91
 
 
92
 
}
93
 
 
94
 
public class WindowButton : Gtk.ToggleButton {
95
 
    
96
 
    public Wnck.Window window;
97
 
    public QuickLists.QuickList quicklist;
98
 
    private new bool pressed = false;
99
 
    
100
 
    public new signal void click_event(Gdk.EventButton event);
101
 
    
102
 
    public WindowButton(Wnck.Window window) {
103
 
        this.window = window;
104
 
        this.button_press_event.connect(this.on_button_press_event);
105
 
        this.button_release_event.connect(this.on_button_release_event);
106
 
        
107
 
        var hbox = new Gtk.HBox(false, 6);
108
 
        var label = new Gtk.Label(window.get_name());
109
 
        var pixbuf = window.get_mini_icon();
110
 
        var image = new Gtk.Image.from_pixbuf(pixbuf);
111
 
        
112
 
        label.set_ellipsize(Pango.EllipsizeMode.END);
113
 
        label.set_alignment(0.0f, 0.5f);
114
 
        
115
 
        hbox.pack_start(image, false, false);
116
 
        hbox.pack_start(label, true, true);
117
 
        this.add(hbox);
118
 
        
119
 
        hbox.show_all();
120
 
        
121
 
        this.quicklist = new QuickLists.QuickList.from_window(window);
122
 
    }
123
 
    
124
 
    private bool on_button_press_event(Gdk.EventButton event) {
125
 
        this.pressed = true;
126
 
        return false;
127
 
    }
128
 
    
129
 
    private bool on_button_release_event(Gdk.EventButton event) {
130
 
        if (this.pressed) {
131
 
            this.click_event(event);
132
 
        }
133
 
        this.pressed = false;
134
 
        return false;
135
 
    }
136
 
}
137
 
 
138
 
public class WindowMenu : Gtk.Menu {
139
 
 
140
 
    private QuickLists.QuickList quicklist;
141
 
    private Wnck.Window window;
142
 
 
143
 
    public WindowMenu(WindowButton button) {
144
 
        this.quicklist = button.quicklist;
145
 
        this.window = button.window;
146
 
        this.create_quicklist_action_items();
147
 
        this.create_end_items();
148
 
        this.show_all();
149
 
    }
150
 
    
151
 
    private void create_quicklist_action_items() {
152
 
        foreach (QuickLists.Action action in quicklist.get_actions()) {
153
 
            var name = action.name;
154
 
            var menuitem = new Gtk.MenuItem.with_label(name);
155
 
            menuitem.activate.connect(this.on_quicklist_action_menuitem_activate);
156
 
            this.add(menuitem);
157
 
        }
158
 
    }
159
 
    
160
 
    private void create_end_items() {
161
 
        var menuitem_separator = new Gtk.SeparatorMenuItem();
162
 
        var menuitem_close = new Gtk.MenuItem.with_label(_("Close"));
163
 
        
164
 
        menuitem_close.activate.connect(this.on_menuitem_close_activate);
165
 
        
166
 
        this.add(menuitem_separator);
167
 
        this.add(menuitem_close);
168
 
    }
169
 
    
170
 
    private void on_quicklist_action_menuitem_activate(Gtk.MenuItem menuitem) {
171
 
        var action = this.quicklist.get_action_from_name(menuitem.get_label());
172
 
        Process.spawn_async(null, action.exec.split(" "), null, SpawnFlags.SEARCH_PATH, null, null);
173
 
    }
174
 
    
175
 
    private void on_menuitem_close_activate() {
176
 
        this.window.close(Gtk.get_current_event_time());
177
 
    }
178
 
}