~ubuntu-branches/ubuntu/lucid/docky/lucid-proposed

« back to all changes in this revision

Viewing changes to Docky.Items/Docky.Items/IconDockItem.cs

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-02-17 15:10:07 UTC
  • Revision ID: james.westby@ubuntu.com-20100217151007-msxpd0lsj300ndde
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2009 Jason Smith, Robert Dyer, Chris Szikszoy
 
3
// 
 
4
//  This program is free software: you can redistribute it and/or modify
 
5
//  it under the terms of the GNU General Public License as published by
 
6
//  the Free Software Foundation, either version 3 of the License, or
 
7
//  (at your option) any later version.
 
8
// 
 
9
//  This program is distributed in the hope that it will be useful,
 
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
//  GNU General Public License for more details.
 
13
// 
 
14
//  You should have received a copy of the GNU General Public License
 
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
// 
 
17
 
 
18
using System;
 
19
using System.Linq;
 
20
using System.Text;
 
21
using System.Collections.Generic;
 
22
using System.Collections.ObjectModel;
 
23
using System.Text.RegularExpressions;
 
24
 
 
25
using Gdk;
 
26
using Gtk;
 
27
using Cairo;
 
28
using Mono.Unix;
 
29
 
 
30
using Docky.Menus;
 
31
using Docky.Services;
 
32
using Docky.CairoHelper;
 
33
 
 
34
namespace Docky.Items
 
35
{
 
36
        public abstract class IconDockItem : AbstractDockItem
 
37
        {
 
38
                public event EventHandler IconUpdated;
 
39
                
 
40
                string remote_icon;
 
41
                string icon;
 
42
                public string Icon {
 
43
                        get { return string.IsNullOrEmpty (remote_icon) ? icon : remote_icon; }
 
44
                        protected set {
 
45
                                // if we set this, clear the forced pixbuf
 
46
                                if (forced_pixbuf != null)
 
47
                                        forced_pixbuf = null;
 
48
                                if (icon == value)
 
49
                                        return;
 
50
                                icon = value;
 
51
                                
 
52
                                using (Gtk.IconInfo info = Gtk.IconTheme.Default.LookupIcon (icon, 48, Gtk.IconLookupFlags.ForceSvg)) {
 
53
                                        if (info != null && info.Filename != null && info.Filename.EndsWith (".svg")) {
 
54
                                                icon = info.Filename;
 
55
                                                ScalableRendering = true;
 
56
                                        } else {
 
57
                                                ScalableRendering = false;
 
58
                                        }
 
59
                                }
 
60
                                
 
61
                                OnIconUpdated ();
 
62
                                QueueRedraw ();
 
63
                        }
 
64
                }
 
65
                
 
66
                Pixbuf forced_pixbuf;
 
67
                protected Pixbuf ForcePixbuf {
 
68
                        get { return forced_pixbuf; }
 
69
                        set {
 
70
                                if (forced_pixbuf == value)
 
71
                                        return;
 
72
                                forced_pixbuf = value;
 
73
                                QueueRedraw ();
 
74
                        }
 
75
                }
 
76
                
 
77
                List<IconEmblem> Emblems;
 
78
                
 
79
                protected void SetIconFromGIcon (GLib.Icon gIcon)
 
80
                {
 
81
                        Icon = DockServices.Drawing.IconFromGIcon (gIcon);
 
82
                }
 
83
                
 
84
                protected void SetIconFromPixbuf (Pixbuf pbuf)
 
85
                {
 
86
                        forced_pixbuf = pbuf;
 
87
                }
 
88
                
 
89
                public IconDockItem ()
 
90
                {
 
91
                        Emblems = new List<IconEmblem> ();
 
92
                        Icon = "";
 
93
                }
 
94
                
 
95
                public void AddEmblem (IconEmblem emblem)
 
96
                {
 
97
                        // remove current emblems at this position
 
98
                        if (Emblems.Any (e => e.Position == emblem.Position))
 
99
                                Emblems.RemoveAll (e => e.Position == emblem.Position);
 
100
                        // add the new emblem
 
101
                        Emblems.Add (emblem);
 
102
                        QueueRedraw ();
 
103
                        emblem.Changed += delegate {
 
104
                                QueueRedraw ();
 
105
                        };
 
106
                }
 
107
                
 
108
                public void RemoveEmblem (IconEmblem emblem)
 
109
                {
 
110
                        if (Emblems.Contains (emblem)) {
 
111
                                Emblems.Remove (emblem);
 
112
                                QueueRedraw ();
 
113
                        }
 
114
                }
 
115
                
 
116
                public void SetRemoteIcon (string icon)
 
117
                {
 
118
                        remote_icon = icon;
 
119
                        
 
120
                        OnIconUpdated ();
 
121
                        QueueRedraw ();
 
122
                }
 
123
                
 
124
                protected override void PaintIconSurface (DockySurface surface)
 
125
                {                       
 
126
                        Gdk.Pixbuf pbuf;
 
127
                        
 
128
                        if (forced_pixbuf == null)
 
129
                                pbuf = DockServices.Drawing.LoadIcon (Icon, surface.Width, surface.Height);
 
130
                        else
 
131
                                pbuf = DockServices.Drawing.ARScale (surface.Width, surface.Height, forced_pixbuf.Copy ());
 
132
                        
 
133
                        pbuf = ProcessPixbuf (pbuf);
 
134
 
 
135
                        Gdk.CairoHelper.SetSourcePixbuf (surface.Context, 
 
136
                                                         pbuf, 
 
137
                                                         (surface.Width - pbuf.Width) / 2, 
 
138
                                                         (surface.Height - pbuf.Height) / 2);
 
139
                        surface.Context.Paint ();
 
140
                        
 
141
                        // draw the emblems
 
142
                        foreach (IconEmblem emblem in Emblems) {
 
143
                                Pixbuf p = emblem.GetPixbuf (surface.Width, surface.Height);
 
144
                                int x, y;
 
145
                                switch (emblem.Position) {
 
146
                                case 1:
 
147
                                        x = surface.Width - p.Width;
 
148
                                        y = 0;
 
149
                                        break;
 
150
                                case 2:
 
151
                                        x = surface.Width - p.Width;
 
152
                                        y = surface.Height - p.Height;
 
153
                                        break;
 
154
                                case 3:
 
155
                                        x = 0;
 
156
                                        y = surface.Height - p.Height;
 
157
                                        break;
 
158
                                default:
 
159
                                        x = y = 0;
 
160
                                        break;
 
161
                                }
 
162
                                Gdk.CairoHelper.SetSourcePixbuf (surface.Context, p, x, y);
 
163
                                surface.Context.Paint ();
 
164
                                p.Dispose ();
 
165
                        }
 
166
 
 
167
                        pbuf.Dispose ();
 
168
                        
 
169
                        try {
 
170
                                PostProcessIconSurface (surface);
 
171
                        } catch (Exception e) {
 
172
                                Log<IconDockItem>.Error (e.Message);
 
173
                                Log<IconDockItem>.Debug (e.StackTrace);
 
174
                        }
 
175
                }
 
176
                
 
177
                protected virtual Gdk.Pixbuf ProcessPixbuf (Gdk.Pixbuf pbuf)
 
178
                {
 
179
                        return pbuf;
 
180
                }
 
181
                
 
182
                protected virtual void PostProcessIconSurface (DockySurface surface)
 
183
                {
 
184
                }
 
185
                
 
186
                protected void OnIconUpdated ()
 
187
                {
 
188
                        if (IconUpdated != null)
 
189
                                IconUpdated (this, EventArgs.Empty);
 
190
                }
 
191
        }
 
192
}