~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Interface.Windows/src/Do.Interface/Do.Interface.Widgets/IconBox.cs

  • Committer: Hardeep S
  • Date: 2009-06-23 03:53:12 UTC
  • Revision ID: ootz0rz@gmail.com-20090623035312-it8tb5wkha6nf31p
Added Do.Interface.Windows, and a bunch of cleanup with old MonoDevelop files elsewhere

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* IconBox.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this source distribution.
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
using System;
 
21
 
 
22
using Gtk;
 
23
using Gdk;
 
24
 
 
25
using Do.Universe;
 
26
using Do.Platform;
 
27
using Do.Interface;
 
28
 
 
29
namespace Do.Interface.Widgets
 
30
{
 
31
        public class IconBox : Frame
 
32
        {
 
33
                const string CaptionFormat = "{0}";
 
34
                const string HighlightFormat = "<span weight=\"bold\" underline=\"single\">{0}</span>";
 
35
 
 
36
                protected bool focused, textOverlay;
 
37
 
 
38
                protected string caption, icon_name, highlight;
 
39
                protected Pixbuf empty_pixbuf;
 
40
                protected int icon_size;
 
41
                
 
42
                protected VBox vbox;
 
43
                protected Gtk.Image image;
 
44
                protected Label label;
 
45
                protected Gdk.Pixbuf overlay_pixbuf;
 
46
 
 
47
                protected float focused_fill_transparency = 0.4f;
 
48
                protected float unfocused_fill_transparency = 0.1f;
 
49
                protected float focused_frame_transparency = 0.3f;
 
50
                protected float unfocused_frame_transparency = 0.075f;
 
51
 
 
52
                public IconBox (int icon_size) : base ()
 
53
                {
 
54
                        this.icon_size = icon_size;
 
55
                        overlay_pixbuf = IconProvider.PixbufFromIconName ("gnome-mime-text", icon_size);
 
56
                        Build ();
 
57
                }
 
58
                
 
59
                protected virtual void Build ()
 
60
                {
 
61
                        Alignment label_align;
 
62
 
 
63
                        caption = "";
 
64
                        highlight = "";
 
65
                        
 
66
                        vbox = new VBox (false, 4);
 
67
                        vbox.BorderWidth = 6;
 
68
                        Add (vbox);
 
69
                        
 
70
                        vbox.Show ();
 
71
 
 
72
                        empty_pixbuf = new Pixbuf (Colorspace.Rgb, true, 8, icon_size, icon_size);
 
73
                        empty_pixbuf.Fill (uint.MinValue);
 
74
 
 
75
                        image = new Gtk.Image ();
 
76
                        vbox.PackStart (image, false, false, 0);
 
77
                        image.Show ();
 
78
 
 
79
                        label = new Label ();
 
80
                        label.Ellipsize = Pango.EllipsizeMode.End;
 
81
                        label.ModifyFg (StateType.Normal, Style.White);
 
82
                        label_align = new Alignment (1.0F, 0.0F, 0, 0);
 
83
                        label_align.SetPadding (0, 2, 2, 2);
 
84
                        label_align.Add (label);
 
85
                        vbox.PackStart (label_align, false, false, 0);
 
86
                        label.Show ();
 
87
                        label_align.Show ();
 
88
 
 
89
                        image.SetSizeRequest (icon_size, icon_size);
 
90
                        label.SetSizeRequest (icon_size / 4 * 5, -1);
 
91
                        // SetSizeRequest (icon_size * 2, icon_size * 2);
 
92
 
 
93
                        DrawFrame = DrawFill = true;
 
94
                        FrameColor = FillColor = new Color (byte.MaxValue, byte.MaxValue, byte.MaxValue);
 
95
 
 
96
                        Realized += OnRealized;
 
97
                        UpdateFocus ();
 
98
                }
 
99
 
 
100
                public virtual void Clear ()
 
101
                {
 
102
                        Pixbuf = null;
 
103
                        highlight = "";
 
104
                        Caption = "";
 
105
                        icon_name = "";
 
106
                        TextOverlay = false;
 
107
                }
 
108
 
 
109
                protected virtual void OnRealized (object o, EventArgs args)
 
110
                {
 
111
                        UpdateFocus ();
 
112
                }
 
113
 
 
114
                public bool IsFocused
 
115
                {
 
116
                        get { return focused; }
 
117
                        set {
 
118
                                if (focused == value) return;
 
119
                                focused = value;
 
120
                                UpdateFocus ();
 
121
                        }
 
122
                }
 
123
                
 
124
                public virtual bool TextOverlay
 
125
                {
 
126
                        get { return textOverlay; }
 
127
                        set {
 
128
                                if (textOverlay == value)
 
129
                                        return;
 
130
                                
 
131
                                textOverlay = value;
 
132
                                if (value) {
 
133
                                        FillAlpha = focused_fill_transparency;
 
134
                                        FrameAlpha = focused_frame_transparency;
 
135
                                        FillColor = FrameColor = new Color (0x00, 0x00, 0x00);
 
136
                                        image.Hide ();
 
137
                                        label.Ellipsize = Pango.EllipsizeMode.None;
 
138
                                        label.LineWrapMode = Pango.WrapMode.WordChar;
 
139
                                        label.LineWrap = true;
 
140
                                        highlight = "";
 
141
                                } else {
 
142
                                        FillColor = FrameColor = new Color (0xff, 0xff, 0xff);
 
143
                                        image.Show ();
 
144
                                        label.Wrap = false;
 
145
                                        label.Ellipsize = Pango.EllipsizeMode.End;
 
146
                                }
 
147
                        }
 
148
                }
 
149
 
 
150
                public string Caption
 
151
                {
 
152
                        get { return caption; }
 
153
                        set {
 
154
                                caption = GLib.Markup.EscapeText (value ?? "");
 
155
                                caption = caption.Replace ("\n", " ");
 
156
                                UpdateLabel ();
 
157
                        }
 
158
                }
 
159
                
 
160
                private void UpdateLabel ()
 
161
                {
 
162
                                int lines = label.Layout.LineCount;
 
163
                                label.Markup = string.Format (CaptionFormat, 
 
164
                                                              Util.FormatCommonSubstrings (caption, highlight, HighlightFormat));
 
165
                                if (lines != label.Layout.LineCount && LinesChanged != null)
 
166
                                        LinesChanged (label.Layout.LineCount, new EventArgs ());
 
167
                }
 
168
 
 
169
                public string Icon
 
170
                {
 
171
                        get {
 
172
                                return icon_name;
 
173
                        }
 
174
                        set {
 
175
                                if (value == null || textOverlay) return;
 
176
                                icon_name = value;
 
177
                                using (Gdk.Pixbuf pix = IconProvider.PixbufFromIconName (value, icon_size)) {
 
178
                                        Pixbuf = pix;
 
179
                                }
 
180
                        }
 
181
                }
 
182
 
 
183
                public Pixbuf Pixbuf
 
184
                {
 
185
                        set {
 
186
                                image.Pixbuf = value ?? empty_pixbuf;
 
187
                        }
 
188
                }
 
189
 
 
190
                public Do.Universe.Item DisplayObject
 
191
                {
 
192
                        set {
 
193
                                string name, icon;
 
194
 
 
195
                                icon = null;
 
196
                                name = null;
 
197
                                if (value != null) {
 
198
                                        icon = value.Icon;
 
199
                                        name = value.Name;
 
200
                                        
 
201
                                        if (name == Caption && icon == this.Icon)
 
202
                                                return;
 
203
                                }
 
204
                                
 
205
                                Icon = icon;
 
206
                                Caption = name;
 
207
                        }
 
208
                }
 
209
 
 
210
                public string Highlight
 
211
                {
 
212
                        set {
 
213
                                highlight = value ?? "";
 
214
                                UpdateLabel ();
 
215
                        }
 
216
                }
 
217
 
 
218
                protected virtual void UpdateFocus ()
 
219
                {
 
220
                        FillAlpha = focused ? focused_fill_transparency : unfocused_fill_transparency;
 
221
                        FrameAlpha = focused ? focused_frame_transparency : unfocused_frame_transparency;
 
222
                }
 
223
                
 
224
                protected override void PaintFill ()
 
225
                {
 
226
                        if (!textOverlay) {
 
227
                                base.PaintFill ();
 
228
                                return;
 
229
                        }
 
230
                        // Gtk doesn't allow stacking elements, so we can't use a standard GTK interface here
 
231
                        // To work around this we will instead draw our own icon and gtk will be none the wiser.
 
232
                        // We are very clever.
 
233
                        
 
234
                        double r, g, b;
 
235
                        
 
236
                        r = (double) fillColor.Red / ushort.MaxValue;
 
237
                        g = (double) fillColor.Green / ushort.MaxValue;
 
238
                        b = (double) fillColor.Blue / ushort.MaxValue;
 
239
                        
 
240
                        cairo.Save ();
 
241
                        GetFrame (cairo);
 
242
                        
 
243
                        Gdk.CairoHelper.SetSourcePixbuf (cairo, 
 
244
                                                         overlay_pixbuf, 
 
245
                                                         (int) (width / 2) - (int) (overlay_pixbuf.Width / 2) + x, 
 
246
                                                         (int) (height / 2) - (int) (overlay_pixbuf.Height / 2) + y);
 
247
                        cairo.PaintWithAlpha (fillAlpha);
 
248
                        
 
249
                        cairo.Color = new Cairo.Color (r, g, b, fillAlpha);
 
250
                        cairo.FillPreserve ();
 
251
                        
 
252
                        cairo.Restore ();
 
253
                }
 
254
 
 
255
                public event EventHandler LinesChanged;
 
256
        }
 
257
}