~do-win/do/test-paths

« back to all changes in this revision

Viewing changes to Do.Interface.Windows/src/Do.Interface/Do.Interface.Widgets/Frame.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
/* Frame.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
using Gtk;
 
22
using Gdk;
 
23
 
 
24
namespace Do.Interface.Widgets
 
25
{
 
26
        public class Frame : Bin
 
27
        {
 
28
                protected Rectangle childAlloc;
 
29
                protected double radius;
 
30
 
 
31
                protected bool drawFrame;
 
32
                protected Color frameColor;
 
33
                protected double frameAlpha;
 
34
                protected bool drawGradient;
 
35
 
 
36
                protected bool fill;
 
37
                protected Color fillColor;
 
38
                protected double fillAlpha;
 
39
                
 
40
                protected Cairo.Context cairo;
 
41
                protected int height, width;
 
42
                protected int x, y;
 
43
 
 
44
                public Frame () : base ()
 
45
                {
 
46
                        fill = false;
 
47
                        fillAlpha = 1.0;
 
48
                        drawFrame = false;
 
49
                        frameAlpha = 1.0;
 
50
                        radius = 12.0;
 
51
                        fillColor = frameColor = new Color (0, 0, 0);
 
52
                }
 
53
 
 
54
                public double Radius
 
55
                {
 
56
                        get { return radius; }
 
57
                        set {
 
58
                                radius = value;
 
59
                                if (IsDrawable) QueueDraw ();
 
60
                        }
 
61
                }
 
62
 
 
63
                public bool DrawFrame
 
64
                {
 
65
                        get { return drawFrame; }
 
66
                        set {
 
67
                                drawFrame = value;
 
68
                                if (IsDrawable) QueueDraw ();
 
69
                        }
 
70
                }
 
71
 
 
72
                public Color FrameColor
 
73
                {
 
74
                        get { return frameColor; }
 
75
                        set {
 
76
                                frameColor = new Color ((byte)value.Red, (byte)value.Green, (byte)value.Blue);
 
77
                                if (IsDrawable) QueueDraw ();
 
78
                        }
 
79
                }
 
80
 
 
81
                public double FrameAlpha
 
82
                {
 
83
                        get { return frameAlpha; }
 
84
                        set {
 
85
                                frameAlpha = value;
 
86
                                if (IsDrawable) QueueDraw ();
 
87
                        }
 
88
                }
 
89
 
 
90
                public bool DrawFill
 
91
                {
 
92
                        get { return fill; }
 
93
                        set {
 
94
                                fill = value;
 
95
                                if (IsDrawable) QueueDraw ();
 
96
                        }
 
97
                }
 
98
 
 
99
                public Color FillColor
 
100
                {
 
101
                        get { return fillColor; }
 
102
                        set {
 
103
                                fillColor = new Color ((byte)value.Red, (byte)value.Green, (byte)value.Blue);
 
104
                                if (IsDrawable) QueueDraw ();
 
105
                        }
 
106
                }
 
107
 
 
108
                public double FillAlpha
 
109
                {
 
110
                        get { return fillAlpha; }
 
111
                        set {
 
112
                                fillAlpha = value;
 
113
                                if (IsDrawable) QueueDraw ();
 
114
                        }
 
115
                }
 
116
 
 
117
                protected virtual void GetFrame (Cairo.Context cairo)
 
118
                {
 
119
                        if (radius == 0)
 
120
                        {
 
121
                                cairo.MoveTo (x, y);
 
122
                                cairo.Rectangle (x, y, width, height);
 
123
                        } else {
 
124
                                cairo.MoveTo (x+radius, y);
 
125
                                cairo.Arc (x+width-radius, y+radius, radius, (Math.PI*1.5), (Math.PI*2));
 
126
                                cairo.Arc (x+width-radius, y+height-radius, radius, 0, (Math.PI*0.5));
 
127
                                cairo.Arc (x+radius, y+height-radius, radius, (Math.PI*0.5), Math.PI);
 
128
                                cairo.Arc (x+radius, y+radius, radius, Math.PI, (Math.PI*1.5));
 
129
                        }
 
130
                }
 
131
 
 
132
                protected virtual void GetBorderFrame (Cairo.Context cairo)
 
133
                {
 
134
                        /* Override coordinates to align to the cairo grid */
 
135
                        double X, Y, Width, Height;
 
136
                        X = x + cairo.LineWidth/2.0;
 
137
                        Y = y + cairo.LineWidth/2.0;
 
138
                        Width = width - cairo.LineWidth;
 
139
                        Height = height - cairo.LineWidth;
 
140
        
 
141
                        if (radius == 0)
 
142
                        {
 
143
                                cairo.MoveTo (X, Y);
 
144
                                cairo.Rectangle (X, Y, Width, Height);
 
145
                        } else {
 
146
                                cairo.MoveTo (X+radius, Y);
 
147
                                cairo.Arc (X+Width-radius, Y+radius, radius, (Math.PI*1.5), (Math.PI*2));
 
148
                                cairo.Arc (X+Width-radius, Y+Height-radius, radius, 0, (Math.PI*0.5));
 
149
                                cairo.Arc (X+radius, Y+Height-radius, radius, (Math.PI*0.5), Math.PI);
 
150
                                cairo.Arc (X+radius, Y+radius, radius, Math.PI, (Math.PI*1.5));
 
151
                        }
 
152
                }
 
153
 
 
154
                protected virtual void Paint (Gdk.Rectangle area)
 
155
                {
 
156
                        if (!IsDrawable) {
 
157
                                return;
 
158
                        }
 
159
                        if (!drawFrame && !fill) {
 
160
                                /* Nothing to draw. */
 
161
                                return;
 
162
                        }
 
163
 
 
164
                        /* You shouldn't change the size of the drawing area
 
165
                         * to avoid glitches when switching panes, though
 
166
                         * you can enlarge the big frame.
 
167
                         * This workaround is enlarging only the frame which has
 
168
                         * radius == 0, so when the window is not composited.
 
169
                         * Pretty ugly, I should think on something better.
 
170
                         *
 
171
                         * int offset = radius == 0 ? 1 : 0;
 
172
                         *
 
173
                         * x = childAlloc.X - offset;
 
174
                         * y = childAlloc.Y - offset;
 
175
                         * width  = childAlloc.Width + offset * 2;
 
176
                         * height = childAlloc.Height + offset * 2;
 
177
                         */
 
178
 
 
179
                        x = childAlloc.X;
 
180
                        y = childAlloc.Y;
 
181
                        width  = childAlloc.Width;
 
182
                        height = childAlloc.Height;
 
183
 
 
184
                        if (this.radius < 0.0) {
 
185
                                radius = Math.Min (width, height);
 
186
                                radius = (radius / 100) * 10;
 
187
                        }
 
188
 
 
189
                        using (cairo = Gdk.CairoHelper.Create (GdkWindow)) {
 
190
                                cairo.Operator = Cairo.Operator.Over;
 
191
 
 
192
                                if (fill) {
 
193
                                        PaintFill ();
 
194
                                }
 
195
                                cairo.NewPath ();
 
196
 
 
197
                                if (drawFrame) {
 
198
                                        PaintBorder ();
 
199
                                }
 
200
                                
 
201
                                if (drawGradient)
 
202
                                        ((IDisposable)cairo.Target).Dispose ();
 
203
                        }
 
204
                }
 
205
                
 
206
                protected virtual Cairo.LinearGradient GetGradient ()
 
207
                {
 
208
                        return new Cairo.LinearGradient (x, y, x, y+height);
 
209
                }
 
210
                
 
211
                protected virtual void PaintFill ()
 
212
                {
 
213
                        double r, g, b;
 
214
                        
 
215
                        r = (double) fillColor.Red / ushort.MaxValue;
 
216
                        g = (double) fillColor.Green / ushort.MaxValue;
 
217
                        b = (double) fillColor.Blue / ushort.MaxValue;
 
218
                        
 
219
                        cairo.Save ();
 
220
                        GetFrame (cairo);
 
221
                        
 
222
                        if ( !drawGradient )
 
223
                                cairo.Color = new Cairo.Color (r, g, b, fillAlpha);
 
224
                        else
 
225
                                cairo.Pattern = GetGradient ();
 
226
                        
 
227
                        cairo.FillPreserve ();
 
228
                        cairo.Restore ();
 
229
                }
 
230
                
 
231
                protected virtual void PaintBorder ()
 
232
                {
 
233
                        double r, g, b;
 
234
                        
 
235
                        r = (double) frameColor.Red / ushort.MaxValue;
 
236
                        g = (double) frameColor.Green / ushort.MaxValue;
 
237
                        b = (double) frameColor.Blue / ushort.MaxValue;
 
238
                        
 
239
                        cairo.Save ();
 
240
                        cairo.LineWidth = 2;
 
241
                        GetBorderFrame (cairo);
 
242
 
 
243
                        cairo.Color = new Cairo.Color (r, g, b, frameAlpha);
 
244
                        cairo.Stroke ();
 
245
                        cairo.Restore ();
 
246
                }
 
247
 
 
248
                protected override bool OnExposeEvent (EventExpose evnt)
 
249
                {
 
250
                        if (IsDrawable) {
 
251
                                Paint (evnt.Area);
 
252
                                base.OnExposeEvent (evnt);
 
253
                        }
 
254
                        return false;
 
255
                }
 
256
 
 
257
                protected override void OnSizeRequested (ref Requisition requisition)
 
258
                {
 
259
                        Requisition cr;
 
260
 
 
261
                        requisition.Height = requisition.Width = 0;
 
262
                        if (Child != null && Child.Visible) {
 
263
                                cr = Child.SizeRequest ();
 
264
                                requisition.Width = Math.Max (requisition.Width, cr.Width);
 
265
                                requisition.Height += cr.Height;
 
266
                        }
 
267
                        requisition.Width += (int)(BorderWidth + 2);
 
268
                        requisition.Height += (int)(BorderWidth + 2);
 
269
                }
 
270
 
 
271
                protected override void OnSizeAllocated (Rectangle allocation)
 
272
                {
 
273
                        Rectangle new_alloc;
 
274
 
 
275
                        new_alloc.X = (int) BorderWidth + 1;
 
276
                        new_alloc.Width = Math.Max (1, allocation.Width - new_alloc.X * 2);
 
277
                        new_alloc.Y = (int) BorderWidth + 1;
 
278
                        new_alloc.Height = Math.Max (1, allocation.Height - new_alloc.Y * 2);
 
279
                        new_alloc.X += allocation.X;
 
280
                        new_alloc.Y += allocation.Y;
 
281
                        if (IsMapped && new_alloc != childAlloc) {
 
282
                                GdkWindow.InvalidateRect (new_alloc, false);
 
283
                        }
 
284
                        if (Child != null && Child.Visible) {
 
285
                                Child.SizeAllocate (new_alloc);
 
286
                        }
 
287
                        childAlloc = new_alloc;
 
288
                }
 
289
        }
 
290
}