~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.WPF/Xwt.WPFBackend/DrawingContext.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// DrawingContext.cs
 
3
//  
 
4
// Author:
 
5
//       Eric Maupin <ermau@xamarin.com>
 
6
//       Lytico (http://limada.sourceforge.net)
 
7
// 
 
8
// Copyright (c) 2012 Xamarin, Inc.
 
9
// 
 
10
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
11
// of this software and associated documentation files (the "Software"), to deal
 
12
// in the Software without restriction, including without limitation the rights
 
13
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
14
// copies of the Software, and to permit persons to whom the Software is
 
15
// furnished to do so, subject to the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be included in
 
18
// all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
21
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
22
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
23
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
24
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
25
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
26
// THE SOFTWARE.
 
27
using System;
 
28
using System.Collections.Generic;
 
29
using System.Drawing;
 
30
using System.Drawing.Drawing2D;
 
31
 
 
32
namespace Xwt.WPFBackend
 
33
{
 
34
        internal class DrawingContext:IDisposable
 
35
        {
 
36
                internal DrawingContext (Graphics graphics)
 
37
                {
 
38
                        if (graphics == null)
 
39
                                throw new ArgumentNullException ("graphics");
 
40
                        
 
41
                        graphics.SmoothingMode = SmoothingMode.None;
 
42
                        graphics.PixelOffsetMode = PixelOffsetMode.Half;
 
43
                        graphics.CompositingQuality = CompositingQuality.HighSpeed;
 
44
 
 
45
                        // necessary for correct text rendering with System.Drawing
 
46
                        //graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
 
47
                        // necessary for none-pixelated text drawing in images, revert to above line if it introduces a performance problem
 
48
                        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
 
49
 
 
50
                        Graphics = graphics;
 
51
                }
 
52
 
 
53
                internal DrawingContext (DrawingContext context)
 
54
                {
 
55
                        
 
56
                        context.CopyTo (this, false);
 
57
                        
 
58
                        CurrentX = context.CurrentX;
 
59
                        CurrentY = context.CurrentY;
 
60
                }
 
61
 
 
62
                internal readonly Graphics Graphics;
 
63
                Font font = null;
 
64
 
 
65
                internal Font Font {
 
66
                        get { return font ?? (font = new Font (FontFamily.GenericSansSerif, 12));}
 
67
                        set { font = value;}
 
68
                }
 
69
 
 
70
                Color color = Color.Black;
 
71
                float width = 1;
 
72
                Pen pen = null;
 
73
 
 
74
                internal Pen Pen {
 
75
                        get { return pen ?? (pen = new Pen (color, width));}
 
76
                        set { pen = value;}
 
77
                }
 
78
 
 
79
                Brush brush = null;
 
80
 
 
81
                internal Brush Brush {
 
82
                        get { return brush ?? (brush = new SolidBrush (color));}
 
83
                        set { brush = value;}
 
84
                }
 
85
 
 
86
                internal GraphicsState State;
 
87
                internal float CurrentX;
 
88
                internal float CurrentY;
 
89
                GraphicsPath path = null;
 
90
 
 
91
                internal GraphicsPath Path {
 
92
                        get { return path ?? (path = new GraphicsPath ());}
 
93
                        set { path = value;}
 
94
                }
 
95
 
 
96
                internal void SetColor (Color color)
 
97
                {
 
98
                        if (this.color == color)
 
99
                                return;
 
100
                        
 
101
                        if (pen != null)
 
102
                                pen.Color = color;
 
103
 
 
104
                        if (brush != null) {
 
105
                                SolidBrush solidBrush = brush as SolidBrush;
 
106
                                if (solidBrush == null) {
 
107
                                        brush.Dispose ();
 
108
                                        brush = null;
 
109
                                } else
 
110
                                        solidBrush.Color = color;
 
111
                        }
 
112
 
 
113
                        this.color = color;
 
114
                }
 
115
                
 
116
                internal void SetWidth (float width)
 
117
                {
 
118
                        if (this.width != width) {
 
119
                                if (pen != null) {
 
120
                                        pen.Width = width;
 
121
                                }
 
122
                        }
 
123
                        this.width = width;
 
124
                }
 
125
                
 
126
                internal void CopyTo (DrawingContext dc, bool toCurrent)
 
127
                {
 
128
                        if (toCurrent) 
 
129
                                dc.Graphics.Restore (this.State);
 
130
                        else 
 
131
                                dc.State = this.Graphics.Save ();
 
132
                        dc.Font = this.font;
 
133
                        dc.Brush = this.brush;
 
134
                        dc.Pen = this.pen;
 
135
                        dc.SetWidth (this.width);
 
136
                        dc.SetColor (this.color);
 
137
                        dc.CurrentX = this.CurrentX;
 
138
                        dc.CurrentY = this.CurrentY;
 
139
                        if (this.path != null && this.path.PointCount > 0)
 
140
                                dc.Path = (GraphicsPath) this.path.Clone ();
 
141
                }
 
142
                
 
143
                internal void Save ()
 
144
                {
 
145
                        if (this.contexts == null)
 
146
                                this.contexts = new Stack<DrawingContext> ();
 
147
 
 
148
                        this.contexts.Push (new DrawingContext (this));
 
149
                }
 
150
                
 
151
                internal void Restore ()
 
152
                {
 
153
                        if (this.contexts == null || this.contexts.Count == 0)
 
154
                                throw new InvalidOperationException ();
 
155
 
 
156
                        var dc = this.contexts.Pop ();
 
157
 
 
158
                        dc.CopyTo (this, true);
 
159
                        dc.Dispose (true);
 
160
 
 
161
                }
 
162
 
 
163
                private Stack<DrawingContext> contexts;
 
164
 
 
165
                public void Dispose (bool stacked)
 
166
                {
 
167
                        if (!stacked) {
 
168
                                if (font != null)
 
169
                                        font.Dispose ();
 
170
                                if (brush != null)
 
171
                                        brush.Dispose ();
 
172
                                if (pen != null)
 
173
                                        pen.Dispose ();
 
174
                        }
 
175
                        
 
176
                        if (path != null)
 
177
                                path.Dispose ();
 
178
                        
 
179
                        if (contexts != null)
 
180
                                while (contexts.Count!=0) {
 
181
                                        var c = contexts.Pop ();
 
182
                                        c.Dispose (true);
 
183
                                }
 
184
                        font = null;
 
185
                        brush = null;
 
186
                        pen = null;
 
187
                        path = null;
 
188
                }
 
189
                
 
190
                public void Dispose ()
 
191
                {
 
192
                        Dispose (false);
 
193
                }
 
194
        }
 
195
}
 
 
b'\\ No newline at end of file'