~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Interface.Windows/src/Do.Interface/PixbufSurfaceCache.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
// PixbufSurfaceCache.cs
 
2
// 
 
3
// Copyright (C) 2008 GNOME Do
 
4
//
 
5
// This program is free software: you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by
 
7
// the Free Software Foundation, either version 3 of the License, or
 
8
// (at your option) any later version.
 
9
//
 
10
// This program is distributed in the hope that it will be useful,
 
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
// GNU General Public License for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License
 
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
//
 
18
 
 
19
using System;
 
20
using System.Linq;
 
21
using System.Collections.Generic;
 
22
 
 
23
using Cairo;
 
24
using Gdk;
 
25
using Gtk;
 
26
 
 
27
using Do.Platform;
 
28
using Do.Interface;
 
29
 
 
30
namespace Do.Interface
 
31
{
 
32
        
 
33
        
 
34
        public class PixbufSurfaceCache : IDisposable
 
35
        {
 
36
                Dictionary <string, Entry> surface_cache;
 
37
                int surface_width;
 
38
                int surface_height;
 
39
                
 
40
                public PixbufSurfaceCache(int count, int surface_width, int surface_height, Surface sourceSurface)
 
41
                {
 
42
                        this.surface_width = surface_width;
 
43
                        this.surface_height = surface_height;
 
44
                        surface_cache = new Dictionary<string, Entry> ();
 
45
                        Entry e;
 
46
                        for (int i=0; i<count; i++) {
 
47
                                e = new Entry (sourceSurface.CreateSimilar (sourceSurface.Content, surface_width, surface_height), "null"+i);
 
48
                                surface_cache.Add (e.ID, e);
 
49
                        }
 
50
                }
 
51
                
 
52
                public PixbufSurfaceCache(int count, int surface_width, int surface_height)
 
53
                {
 
54
                        this.surface_width = surface_width;
 
55
                        this.surface_height = surface_height;
 
56
                        surface_cache = new Dictionary<string, Entry> ();
 
57
                        Entry e;
 
58
                        for (int i=0; i<count; i++) {
 
59
                                e = new Entry (new ImageSurface (Format.Argb32, surface_width, surface_height), "null"+i);
 
60
                                surface_cache.Add (e.ID, e);
 
61
                        }
 
62
                }
 
63
                
 
64
                ~PixbufSurfaceCache ()
 
65
                {
 
66
                        foreach (Entry e in surface_cache.Values)
 
67
                                e.surface.Destroy ();
 
68
                }
 
69
                
 
70
                public Surface AddPixbufSurface (string id, string icon)
 
71
                {
 
72
                        Surface sr = EvictLRU ();
 
73
                        Context cr = new Context (sr);
 
74
 
 
75
                        cr.Operator = Operator.Source;
 
76
                        cr.Color = new Cairo.Color (0, 0, 0, 0);
 
77
                        cr.Paint ();
 
78
                        
 
79
                        DrawIconOnSurface (sr, icon);
 
80
                        
 
81
                        surface_cache.Add (id, new Entry (sr, id));
 
82
                        (cr as IDisposable).Dispose ();
 
83
                        return sr;
 
84
                }
 
85
                
 
86
                private void DrawIconOnSurface (Surface sr, string icon)
 
87
                {
 
88
                        Context cr = new Context (sr);
 
89
                        Gdk.Pixbuf pixbuf;
 
90
                        pixbuf = IconProvider.PixbufFromIconName (icon, surface_width);
 
91
                        if (pixbuf.Height != surface_width && pixbuf.Width != surface_width) {
 
92
                                double scale = (double)surface_width / Math.Max (pixbuf.Width, pixbuf.Height);
 
93
                                Gdk.Pixbuf temp = pixbuf.ScaleSimple ((int) (pixbuf.Width * scale), (int) (pixbuf.Height * scale), InterpType.Bilinear);
 
94
                                pixbuf.Dispose ();
 
95
                                pixbuf = temp;
 
96
                        }
 
97
                        
 
98
                        if (pixbuf == null) {
 
99
                                (cr as IDisposable).Dispose ();
 
100
                                return;
 
101
                        }
 
102
                        
 
103
                        Gdk.CairoHelper.SetSourcePixbuf (cr, 
 
104
                                                         pixbuf, 
 
105
                                                         (int) ((surface_width - pixbuf.Width)/2), 
 
106
                                                         (int) ((surface_height - pixbuf.Height)/2));
 
107
                        cr.Operator = Operator.Over;
 
108
                        cr.Paint ();
 
109
                        pixbuf.Dispose ();
 
110
                        (cr as IDisposable).Dispose ();
 
111
                }
 
112
                
 
113
                public Surface GetSurface (string  id)
 
114
                {
 
115
                        if (!surface_cache.ContainsKey (id))
 
116
                                return null;
 
117
                        surface_cache[id].time = DateTime.Now;
 
118
                        return surface_cache[id].surface;
 
119
                }
 
120
                
 
121
                public bool ContainsKey (string id)
 
122
                {
 
123
                        return surface_cache.ContainsKey (id);
 
124
                }
 
125
                
 
126
                private Surface EvictLRU ()
 
127
                {
 
128
                        Entry lru = surface_cache.Values.Min ();
 
129
                        surface_cache.Remove (lru.ID);
 
130
                        return lru.surface;
 
131
                }
 
132
 
 
133
                #region IDisposable implementation 
 
134
                
 
135
                public void Dispose ()
 
136
                {
 
137
                        foreach (Entry en in surface_cache.Values)
 
138
                                en.surface.Destroy ();
 
139
                }
 
140
                
 
141
                #endregion 
 
142
                
 
143
                
 
144
                class Entry : IComparable<Entry> {
 
145
                        public Surface surface;
 
146
                        public DateTime time;
 
147
                        public string ID;
 
148
                        
 
149
                        #region IComparable[PixbufSurfaceCache.Entry] implementation 
 
150
                        public int CompareTo (Entry other)
 
151
                        {
 
152
                                return time.CompareTo (other.time);
 
153
                        }
 
154
                        #endregion 
 
155
                        
 
156
                        public Entry (Surface s, string id)
 
157
                        {
 
158
                                ID = id;
 
159
                                surface = s;
 
160
                                time = DateTime.Now;
 
161
                        }
 
162
                }
 
163
        }
 
164
}