1
// PixbufSurfaceCache.cs
3
// Copyright (C) 2008 GNOME Do
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.
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.
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/>.
21
using System.Collections.Generic;
30
namespace Do.Interface
34
public class PixbufSurfaceCache : IDisposable
36
Dictionary <string, Entry> surface_cache;
40
public PixbufSurfaceCache(int count, int surface_width, int surface_height, Surface sourceSurface)
42
this.surface_width = surface_width;
43
this.surface_height = surface_height;
44
surface_cache = new Dictionary<string, Entry> ();
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);
52
public PixbufSurfaceCache(int count, int surface_width, int surface_height)
54
this.surface_width = surface_width;
55
this.surface_height = surface_height;
56
surface_cache = new Dictionary<string, Entry> ();
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);
64
~PixbufSurfaceCache ()
66
foreach (Entry e in surface_cache.Values)
70
public Surface AddPixbufSurface (string id, string icon)
72
Surface sr = EvictLRU ();
73
Context cr = new Context (sr);
75
cr.Operator = Operator.Source;
76
cr.Color = new Cairo.Color (0, 0, 0, 0);
79
DrawIconOnSurface (sr, icon);
81
surface_cache.Add (id, new Entry (sr, id));
82
(cr as IDisposable).Dispose ();
86
private void DrawIconOnSurface (Surface sr, string icon)
88
Context cr = new Context (sr);
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);
99
(cr as IDisposable).Dispose ();
103
Gdk.CairoHelper.SetSourcePixbuf (cr,
105
(int) ((surface_width - pixbuf.Width)/2),
106
(int) ((surface_height - pixbuf.Height)/2));
107
cr.Operator = Operator.Over;
110
(cr as IDisposable).Dispose ();
113
public Surface GetSurface (string id)
115
if (!surface_cache.ContainsKey (id))
117
surface_cache[id].time = DateTime.Now;
118
return surface_cache[id].surface;
121
public bool ContainsKey (string id)
123
return surface_cache.ContainsKey (id);
126
private Surface EvictLRU ()
128
Entry lru = surface_cache.Values.Min ();
129
surface_cache.Remove (lru.ID);
133
#region IDisposable implementation
135
public void Dispose ()
137
foreach (Entry en in surface_cache.Values)
138
en.surface.Destroy ();
144
class Entry : IComparable<Entry> {
145
public Surface surface;
146
public DateTime time;
149
#region IComparable[PixbufSurfaceCache.Entry] implementation
150
public int CompareTo (Entry other)
152
return time.CompareTo (other.time);
156
public Entry (Surface s, string id)