~jaapz-b/switchboard/fix-1088451

« back to all changes in this revision

Viewing changes to Switchboard/AppMenu.vala

  • Committer: Avi Romanoff
  • Date: 2011-06-23 05:26:15 UTC
  • Revision ID: avi@elementaryos.org-20110623052615-vd2nmnbcitrjrnml
Make consts all caps, move source into it's own directory, utilize constants more, bump version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace ElementaryWidgets {
 
2
 
 
3
    using Gtk;
 
4
 
 
5
    public abstract class ToolButtonWithMenu : ToggleToolButton
 
6
    {
 
7
        protected Menu menu;
 
8
        private PositionType _menu_orientation;
 
9
        protected PositionType menu_orientation{
 
10
            set{
 
11
                if(value == PositionType.TOP || value == PositionType.BOTTOM){
 
12
                    value = PositionType.LEFT;
 
13
                }
 
14
                
 
15
                _menu_orientation = value;
 
16
            }
 
17
            get{
 
18
                return _menu_orientation;
 
19
            }
 
20
        }
 
21
 
 
22
        public ToolButtonWithMenu (Image image, string label, Menu _menu, PositionType menu_orientation = PositionType.LEFT)
 
23
        {
 
24
            this.menu_orientation = menu_orientation;
 
25
        
 
26
            icon_widget = image;
 
27
            label_widget = new Label (label);
 
28
            ((Label) label_widget).use_underline = true;
 
29
            can_focus = true;
 
30
            set_tooltip_text ("Menu");
 
31
            menu = _menu;
 
32
            menu.attach_to_widget (this, null);
 
33
            menu.deactivate.connect(() => {
 
34
                active = false;
 
35
            });
 
36
 
 
37
            mnemonic_activate.connect(on_mnemonic_activate);
 
38
            menu.deactivate.connect(popdown_menu);
 
39
            clicked.connect(on_clicked);
 
40
        }
 
41
 
 
42
        private bool on_mnemonic_activate (bool group_cycling)
 
43
        {
 
44
            // ToggleButton always grabs focus away from the editor,
 
45
            // so reimplement Widget's version, which only grabs the
 
46
            // focus if we are group cycling.
 
47
            if (!group_cycling) {
 
48
                activate ();
 
49
            } else if (can_focus) {
 
50
                grab_focus ();
 
51
            }
 
52
 
 
53
            return true;
 
54
        }
 
55
 
 
56
        protected new void popup_menu(Gdk.EventButton? ev)
 
57
        {
 
58
            try {
 
59
                menu.popup (null,
 
60
                            null,
 
61
                            get_menu_position,
 
62
                            (ev == null) ? 0 : ev.button,
 
63
                            (ev == null) ? get_current_event_time() : ev.time);
 
64
            } finally {
 
65
                // Highlight the parent
 
66
                if (menu.attach_widget != null)
 
67
                    menu.attach_widget.set_state(StateType.SELECTED);
 
68
            }
 
69
        }
 
70
 
 
71
        protected void popdown_menu ()
 
72
        {
 
73
            menu.popdown ();
 
74
 
 
75
            // Unhighlight the parent
 
76
            if (menu.attach_widget != null)
 
77
                menu.attach_widget.set_state(Gtk.StateType.NORMAL);
 
78
        }
 
79
        
 
80
        public override void show_all(){
 
81
            base.show_all();
 
82
            menu.show_all();
 
83
        }
 
84
 
 
85
        private void on_clicked ()
 
86
        {
 
87
            menu.select_first (true);
 
88
            popup_menu (null);
 
89
        }
 
90
 
 
91
        private void get_menu_position (Menu menu, out int x, out int y, out bool push_in)
 
92
        {
 
93
            if (menu.attach_widget == null ||
 
94
                menu.attach_widget.get_window() == null) {
 
95
                // Prevent null exception in weird cases
 
96
                x = 0;
 
97
                y = 0;
 
98
                push_in = true;
 
99
                return;
 
100
            }
 
101
 
 
102
            menu.attach_widget.get_window().get_origin (out x, out y);
 
103
            Allocation allocation;
 
104
            menu.attach_widget.get_allocation(out allocation);
 
105
 
 
106
 
 
107
            x += allocation.x;
 
108
            y += allocation.y;
 
109
 
 
110
            int width, height;
 
111
            menu.get_size_request(out width, out height);
 
112
 
 
113
            if (y + height >= menu.attach_widget.get_screen().get_height())
 
114
                y -= height;
 
115
            else
 
116
                y += allocation.height;
 
117
 
 
118
            push_in = true;
 
119
        }
 
120
    }
 
121
 
 
122
    public class AppMenu : ToolButtonWithMenu
 
123
    {
 
124
        
 
125
        string APP_NAME;
 
126
        string LAUNCHPAD_NAME;
 
127
        string WEBSITE;
 
128
        string VERSION;
 
129
        string ICON_NAME;
 
130
        string COPYRIGHT;
 
131
        string[] AUTHORS;
 
132
        Window WINDOW;
 
133
 
 
134
        public AppMenu (Window window, Menu menu, 
 
135
                        string app_name, 
 
136
                        string launchpad_name,
 
137
                        string website,
 
138
                        string version,
 
139
                        string copyright,
 
140
                        string[] authors,
 
141
                        string icon_name)
 
142
        {
 
143
            Image image = new Image.from_stock(Stock.PROPERTIES, IconSize.MENU);
 
144
            MenuItem help_item = new MenuItem.with_label ("Get Help Online...");
 
145
            MenuItem translate_item = new MenuItem.with_label ("Translate This Application...");
 
146
            MenuItem report_item = new MenuItem.with_label ("Report a Problem...");
 
147
            MenuItem about_item = new MenuItem.with_label ("About");
 
148
            menu.append (help_item);
 
149
            menu.append (translate_item);
 
150
            menu.append (report_item);
 
151
            menu.append(new SeparatorMenuItem());
 
152
            menu.append (about_item);
 
153
            base(image, "Menu", menu);
 
154
            APP_NAME = app_name;
 
155
            LAUNCHPAD_NAME = launchpad_name;
 
156
            WINDOW = window;
 
157
            WEBSITE = website;
 
158
            VERSION = version;
 
159
            AUTHORS = authors;
 
160
            COPYRIGHT = copyright;
 
161
            ICON_NAME = icon_name;
 
162
            help_item.activate.connect(() => launch_launchpad("answers"));
 
163
            translate_item.activate.connect(() => launch_launchpad("translations"));
 
164
            report_item.activate.connect(() => launch_launchpad("bugs"));
 
165
            about_item.activate.connect(about_dialog);
 
166
        }
 
167
 
 
168
        private void launch_launchpad (string service) {
 
169
            try {
 
170
                GLib.Process.spawn_async ("/usr/bin/", 
 
171
                    {"x-www-browser", 
 
172
                    "https://"+service+".launchpad.net/"+LAUNCHPAD_NAME}, 
 
173
                    null, GLib.SpawnFlags.STDERR_TO_DEV_NULL, null, null);
 
174
            } catch { }
 
175
        }
 
176
 
 
177
        private void about_dialog () {
 
178
            Gtk.show_about_dialog (WINDOW,
 
179
                "program-name", APP_NAME,
 
180
                "version", VERSION,
 
181
                "website", WEBSITE,
 
182
                "copyright", COPYRIGHT,
 
183
                "authors", AUTHORS,
 
184
                "logo-icon-name", ICON_NAME,
 
185
                null);
 
186
        }
 
187
    }
 
188
}