~voluntatefaber/beat-box/bug952329

« back to all changes in this revision

Viewing changes to ToolButtonWithMenu.vala

  • Committer: Scott Ringwelski
  • Date: 2011-02-10 21:30:53 UTC
  • Revision ID: sgringwe@mtu.edu-20110210213053-d3c7mnexeref3cwj
sexy icons, sexy waf

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
using Gtk;
2
 
 
3
 
public abstract class ToolButtonWithMenu : ToggleToolButton
4
 
{
5
 
    protected Menu menu;
6
 
    private PositionType _menu_orientation;
7
 
    protected PositionType menu_orientation{
8
 
        set{
9
 
            if(value == PositionType.TOP || value == PositionType.BOTTOM){
10
 
                value = PositionType.LEFT;
11
 
            }
12
 
            
13
 
            _menu_orientation = value;
14
 
        }
15
 
        get{
16
 
            return _menu_orientation;
17
 
        }
18
 
    }
19
 
 
20
 
    public ToolButtonWithMenu (Image image, string label, Menu _menu, PositionType menu_orientation = PositionType.LEFT)
21
 
    {
22
 
        this.menu_orientation = menu_orientation;
23
 
    
24
 
        icon_widget = image;
25
 
        label_widget = new Label (label);
26
 
        ((Label) label_widget).use_underline = true;
27
 
        can_focus = true;
28
 
        set_tooltip_text ("Menu");
29
 
        menu = _menu;
30
 
        menu.attach_to_widget (this, null);
31
 
        menu.deactivate.connect(() => {
32
 
            active = false;
33
 
        });
34
 
 
35
 
        mnemonic_activate.connect(on_mnemonic_activate);
36
 
        menu.deactivate.connect(popdown_menu);
37
 
    }
38
 
 
39
 
    private bool on_mnemonic_activate (bool group_cycling)
40
 
    {
41
 
        // ToggleButton always grabs focus away from the editor,
42
 
        // so reimplement Widget's version, which only grabs the
43
 
        // focus if we are group cycling.
44
 
        if (!group_cycling) {
45
 
            activate ();
46
 
        } else if (can_focus) {
47
 
            grab_focus ();
48
 
        }
49
 
 
50
 
        return true;
51
 
    }
52
 
 
53
 
    protected new void popup_menu(Gdk.EventButton? ev)
54
 
    {
55
 
        try {
56
 
            menu.popup (null,
57
 
                        null,
58
 
                        get_menu_position,
59
 
                        (ev == null) ? 0 : ev.button,
60
 
                        (ev == null) ? get_current_event_time() : ev.time);
61
 
        } finally {
62
 
            // Highlight the parent
63
 
            if (menu.attach_widget != null)
64
 
                menu.attach_widget.set_state(StateType.SELECTED);
65
 
        }
66
 
    }
67
 
 
68
 
    protected void popdown_menu ()
69
 
    {
70
 
        menu.popdown ();
71
 
 
72
 
        // Unhighlight the parent
73
 
        if (menu.attach_widget != null)
74
 
            menu.attach_widget.set_state(Gtk.StateType.NORMAL);
75
 
    }
76
 
    
77
 
    public override void show_all(){
78
 
        base.show_all();
79
 
        menu.show_all();
80
 
    }
81
 
 
82
 
    private void get_menu_position (Menu menu, out int x, out int y, out bool push_in)
83
 
    {
84
 
        if (menu.attach_widget == null ||
85
 
            menu.attach_widget.get_window() == null) {
86
 
            // Prevent null exception in weird cases
87
 
            x = 0;
88
 
            y = 0;
89
 
            push_in = true;
90
 
            return;
91
 
        }
92
 
 
93
 
        menu.attach_widget.get_window().get_origin (out x, out y);
94
 
        Allocation allocation;
95
 
        menu.attach_widget.get_allocation(out allocation);
96
 
 
97
 
 
98
 
        x += allocation.x;
99
 
        y += allocation.y;
100
 
 
101
 
        int width, height;
102
 
        menu.get_size_request(out width, out height);
103
 
 
104
 
        if (y + height >= menu.attach_widget.get_screen().get_height())
105
 
            y -= height;
106
 
        else
107
 
            y += allocation.height;
108
 
 
109
 
        push_in = true;
110
 
    }
111
 
}