~and471/+junk/do-with-docky

« back to all changes in this revision

Viewing changes to Do.Interface.Linux.Docky/src/Docky.Interface/Docky.Interface.Painters/AbstractIntegratedPainter.cs

  • Committer: rugby471 at gmail
  • Date: 2010-10-15 16:08:38 UTC
  • Revision ID: rugby471@gmail.com-20101015160838-z9m3utbf7bxzb5ty
reverted to before docky removal

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2009 GNOME Do
 
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.Collections.Generic;
 
20
using System.Linq;
 
21
 
 
22
using Gdk;
 
23
using Cairo;
 
24
 
 
25
using Do.Interface;
 
26
 
 
27
using Docky.Core;
 
28
using Docky.Interface;
 
29
using Docky.Utilities;
 
30
 
 
31
namespace Docky.Interface.Painters
 
32
{
 
33
        
 
34
        
 
35
        public abstract class AbstractIntegratedPainter : IDockPainter
 
36
        {
 
37
                protected int BorderSize { get { return 10; } }
 
38
                int buffer_height = 0;
 
39
                
 
40
                Surface icon_surface, buffer;
 
41
                AbstractDockItem dock_item;
 
42
                
 
43
                protected abstract int PainterWidth { get; }
 
44
                
 
45
                protected virtual bool NeedsRepaint {
 
46
                        get { return false; }
 
47
                }
 
48
                
 
49
                protected abstract void PaintArea (Cairo.Context context, Gdk.Rectangle paintableArea);
 
50
                
 
51
                protected virtual bool ReceiveClick (Gdk.Rectangle paintArea, Gdk.Point cursor)
 
52
                {
 
53
                        return true;
 
54
                }
 
55
                
 
56
                public virtual void Scrolled (Gdk.ScrollDirection direction)
 
57
                {
 
58
                }
 
59
                
 
60
                #region IDockPainter implementation 
 
61
                
 
62
                public event EventHandler HideRequested;
 
63
                
 
64
                public event EventHandler<PaintNeededArgs> PaintNeeded;
 
65
                
 
66
                public event EventHandler ShowRequested;
 
67
                
 
68
                public void Clicked (Gdk.Rectangle dockArea, Gdk.Point cursor)
 
69
                {
 
70
                        Gdk.Rectangle paintArea = new Gdk.Rectangle (0, 0, Width, dockArea.Height);
 
71
                        Gdk.Point paintAreaCursor = new Gdk.Point (cursor.X - dockArea.Left - DockPreferences.FullIconSize - BorderSize,
 
72
                                                                   cursor.Y - dockArea.Top);
 
73
 
 
74
                        if (!paintArea.Contains (paintAreaCursor) || ReceiveClick (paintArea, paintAreaCursor))
 
75
                                OnHideRequested ();
 
76
                }
 
77
                
 
78
                public virtual bool DoubleBuffer {
 
79
                        get { return false; }
 
80
                }
 
81
                
 
82
                public virtual bool Interruptable {
 
83
                        get { return true; }
 
84
                }
 
85
                
 
86
                public int Width {
 
87
                        get {
 
88
                                return PainterWidth + DockPreferences.FullIconSize + 2 * BorderSize;
 
89
                        }
 
90
                }
 
91
                
 
92
                public AbstractIntegratedPainter (AbstractDockItem dockItem)
 
93
                {
 
94
                        dock_item = dockItem;
 
95
                        DockPreferences.IconSizeChanged +=HandleIconSizeChanged; 
 
96
                        dockItem.UpdateNeeded +=HandleUpdateNeeded; 
 
97
                }
 
98
 
 
99
                void HandleUpdateNeeded(object sender, UpdateRequestArgs args)
 
100
                {
 
101
                        if (icon_surface != null)
 
102
                                icon_surface.Destroy ();
 
103
                        icon_surface = null;
 
104
                        
 
105
                        OnPaintNeeded (new PaintNeededArgs ());
 
106
                }
 
107
 
 
108
                void HandleIconSizeChanged()
 
109
                {
 
110
                        if (icon_surface != null)
 
111
                                icon_surface.Destroy ();
 
112
                        icon_surface = null;
 
113
                }
 
114
                
 
115
                public virtual void Interrupt ()
 
116
                {
 
117
                        OnHideRequested ();
 
118
                }
 
119
                
 
120
                public void Paint (Cairo.Context cr, Gdk.Rectangle dockArea, Gdk.Point cursor)
 
121
                {
 
122
                        if (buffer == null || buffer_height != dockArea.Height || NeedsRepaint) {
 
123
                                if (buffer != null)
 
124
                                        buffer.Destroy ();
 
125
                                
 
126
                                buffer = cr.Target.CreateSimilar (cr.Target.Content, PainterWidth, dockArea.Height);
 
127
                                using (Cairo.Context context = new Cairo.Context (buffer)) {
 
128
                                        PaintArea (context, new Gdk.Rectangle (0, 0, PainterWidth, dockArea.Height));
 
129
                                }
 
130
                                buffer_height = dockArea.Height;
 
131
                        }
 
132
                        
 
133
                        PaintIcon (cr, dockArea);
 
134
                        
 
135
                        cr.Rectangle (dockArea.X, dockArea.Y, dockArea.Width, dockArea.Height);
 
136
                        cr.Clip ();
 
137
                        
 
138
                        int x = dockArea.X + DockPreferences.FullIconSize + BorderSize;
 
139
                        buffer.Show (cr, x, dockArea.Y);
 
140
                        cr.ResetClip ();
 
141
                }
 
142
                
 
143
                #endregion 
 
144
                
 
145
                void PaintIcon (Cairo.Context cr, Gdk.Rectangle dockArea)
 
146
                {
 
147
                        if (icon_surface == null)
 
148
                                icon_surface = CreateIconSurface (cr.Target);
 
149
                        
 
150
                        switch (DockPreferences.Orientation) {
 
151
                        case DockOrientation.Top:
 
152
                                icon_surface.Show (cr, dockArea.X + BorderSize, dockArea.Y + 5);
 
153
                                break;
 
154
                        case DockOrientation.Bottom:
 
155
                                icon_surface.Show (cr, dockArea.X + BorderSize, dockArea.Y + dockArea.Height - 5 - DockPreferences.FullIconSize);
 
156
                                break;
 
157
                        }
 
158
                }
 
159
                
 
160
                protected virtual Surface CreateIconSurface (Surface similar)
 
161
                {
 
162
                        Surface surface = similar.CreateSimilar (similar.Content,
 
163
                                                                   DockPreferences.FullIconSize,
 
164
                                                                   DockPreferences.FullIconSize);
 
165
                        
 
166
                        int actual_size;
 
167
                        Surface icon_surface = dock_item.GetIconSurface (similar, DockPreferences.FullIconSize, out actual_size);
 
168
                        
 
169
                        using (Context context = new Context (surface)) {
 
170
                                int offset = (DockPreferences.FullIconSize - actual_size) / 2;
 
171
                                icon_surface.Show (context, offset, offset);
 
172
                        }
 
173
                        
 
174
                        return surface;
 
175
                }
 
176
                
 
177
                protected void OnPaintNeeded (PaintNeededArgs args)
 
178
                {
 
179
                        if (PaintNeeded != null)
 
180
                                PaintNeeded (this, args);
 
181
                }
 
182
                
 
183
                protected void OnShowRequested ()
 
184
                {
 
185
                        if (ShowRequested != null)
 
186
                                ShowRequested (this, EventArgs.Empty);
 
187
                }
 
188
                
 
189
                protected void OnHideRequested ()
 
190
                {
 
191
                        if (HideRequested != null)
 
192
                                HideRequested (this, EventArgs.Empty);
 
193
                }
 
194
                
 
195
                #region IDisposable implementation 
 
196
                
 
197
                public virtual void Dispose ()
 
198
                {
 
199
                }
 
200
                
 
201
                #endregion 
 
202
        }
 
203
}