summaryrefslogtreecommitdiff
path: root/src/gui/cellRendererIcon.vala
blob: 959a0b734c4e3297ec4efd7b3a3b116a96c0e1ee (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
/* 
Copyright (c) 2011 by Simon Schneegans

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 GnomePie {

/////////////////////////////////////////////////////////////////////////    
/// A cellrenderer which displays an Icon. When clicked onto, a window
/// opens for selecting another icon. This needs to be a subclass of
/// Gtk.CellRendererText because Gtk.CellRendererPixbuf can't receive
/// click events. Internally it stores a Gtk.CellRendererPixbuf
/// which renders and stuff.
/////////////////////////////////////////////////////////////////////////

public class CellRendererIcon : Gtk.CellRendererText {
    
    /////////////////////////////////////////////////////////////////////
    /// This signal is emitted when the user selects another icon.
    /////////////////////////////////////////////////////////////////////
    
    public signal void on_select(string path, string icon);
    
    /////////////////////////////////////////////////////////////////////
    /// The IconSelectWindow which is shown on click.
    /////////////////////////////////////////////////////////////////////

    private IconSelectWindow select_window = null;
    
    /////////////////////////////////////////////////////////////////////
    /// The internal Renderer used for drawing.
    /////////////////////////////////////////////////////////////////////
    
    private Gtk.CellRendererPixbuf renderer = null;
    
    /////////////////////////////////////////////////////////////////////
    /// A helper variable, needed to emit the current path.
    /////////////////////////////////////////////////////////////////////
    
    private string current_path = "";
    
    public string icon_name { get; set; }

    /////////////////////////////////////////////////////////////////////
    /// Forward some parts of the CellRendererPixbuf's interface.
    /////////////////////////////////////////////////////////////////////
    
    public bool follow_state {
        get { return renderer.follow_state; }
        set { renderer.follow_state = value; }
    }
    
    public bool icon_sensitive {
        get { return renderer.sensitive; }
        set { renderer.sensitive = value; }
    }
    
    public Gdk.Pixbuf pixbuf {
        owned get { return renderer.pixbuf; }
        set { renderer.pixbuf = value; }
    }

    /////////////////////////////////////////////////////////////////////
    /// C'tor, creates a new CellRendererIcon.
    /////////////////////////////////////////////////////////////////////
    
    public CellRendererIcon() {
        this.select_window = new IconSelectWindow();  
        this.renderer = new Gtk.CellRendererPixbuf();
    
        this.select_window.on_select.connect((icon) => {
            this.on_select(current_path, icon);
        });
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Forward some parts of the CellRendererPixbuf's interface.
    /////////////////////////////////////////////////////////////////////
    
    public override void get_size (Gtk.Widget widget, Gdk.Rectangle? cell_area,
                               out int x_offset, out int y_offset,
                               out int width, out int height) {

        this.renderer.get_size(widget, cell_area, out x_offset, out y_offset, out width, out height);
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Forward some parts of the CellRendererPixbuf's interface.
    /////////////////////////////////////////////////////////////////////
    
    public override void render (Gdk.Window window, Gtk.Widget widget,
                             Gdk.Rectangle bg_area,
                             Gdk.Rectangle cell_area,
                             Gdk.Rectangle expose_area,
                             Gtk.CellRendererState flags) {
                             
        this.renderer.render(window, widget, bg_area, cell_area, expose_area, flags);
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Open the IconSelectWindow on click.
    /////////////////////////////////////////////////////////////////////
    
    public override unowned Gtk.CellEditable start_editing(
        Gdk.Event event, Gtk.Widget widget, string path, Gdk.Rectangle bg_area, 
        Gdk.Rectangle cell_area, Gtk.CellRendererState flags) {
        
        this.select_window.set_transient_for((Gtk.Window)widget.get_toplevel());
        this.select_window.set_modal(true);
        
        this.current_path = path;
        this.select_window.show();
        this.select_window.active_icon = this.icon_name;
            
        return this.renderer.start_editing(event, widget, path, bg_area, cell_area, flags);
    }
}

}