1 using System;
2 using System.Drawing ;
3 using System.Text ;
4 using System.Xml ;
5 using System.Diagnostics ;
6
7 using XmlBlaster ;
8
9 namespace xmlrpc
10 {
11
12 abstract class MessageDrawing
13 {
14 static readonly string EOL = Environment.NewLine ;
15 public static readonly string message_topic = "demo.csharp.drawing" ;
16
17 public enum DrawFormType
18 {
19 Line = 1 ,
20 Rectangle ,
21 Ellipse ,
22 Triangle
23 }
24 protected DrawFormType drawForm ;
25 public DrawFormType DrawForm
26 {
27 get { return drawForm ; }
28 }
29
30 protected int penRgb ;
31 protected bool filled ;
32 protected int fillRgb ;
33
34 string GetXmlString()
35 {
36 StringBuilder sb = new StringBuilder(255);
37
38 sb.Append( "<drawingMessage>\n" );
39
40 sb.Append( "<pen color=\""+ this.penRgb.ToString() +"\" />\n" );
41 if( filled )
42 {
43 sb.Append( "<fill color=\""+ this.fillRgb.ToString() +"\" />\n" );
44 }
45
46 AddDataXmlString( sb );
47
48 sb.Append( "</drawingMessage>" );
49
50 return sb.ToString();
51 }
52
53
54 protected abstract void AddDataXmlString( StringBuilder sb );
55 public abstract void Draw( Graphics graphics );
56
57 public static MessageDrawing CreateFromXml( string xml )
58 {
59 string me="MessageDrawing.CreateFromXml()";
60
61 //Debug.WriteLine( "MessageDrawing.CreateFromXml()" );
62 Debug.WriteLine( me+" : " + xml );
63
64 MessageDrawing msg = null ;
65
66 XmlDocument xmlDoc = new XmlDocument();
67 xmlDoc.LoadXml( xml );
68 XmlNode root = xmlDoc.DocumentElement ;
69
70 bool filled = false ;
71 Color fillColor = Color.Black, penColor = Color.Black ;
72
73 XmlNodeList nodes ;
74 XmlNode node ;
75
76 node = root.SelectSingleNode("fill");
77 if( node != null )
78 {
79 try
80 {
81 filled = true ;
82 int rgb = Convert.ToInt32( node.Attributes[ "color" ].Value );
83 fillColor = Color.FromArgb( rgb );
84 }
85 catch( Exception ex )
86 {
87 throw new Exception( "Malformed MessageDrawing : fill element has error." );
88 }
89 }
90 node = root.SelectSingleNode("pen");
91 if( node != null )
92 {
93 try
94 {
95 int rgb = Convert.ToInt32( node.Attributes[ "color" ].Value );
96 penColor = Color.FromArgb( rgb );
97 }
98 catch( Exception ex )
99 {
100 throw new Exception( "Malformed MessageDrawing : fill element has error." );
101 }
102 }
103
104 XmlNode data_node ;
105 string type = null ;
106
107 try
108 {
109 //node = root.SelectSingleNode("descendant::book[author/last-name='Austen']");
110 data_node = root.SelectSingleNode("data");
111 type = data_node.Attributes[ "type" ].Value ;
112 }
113 catch( Exception ex )
114 {
115 throw new Exception( "Malformed MessageDrawing : missing data element or type attribute." );
116 }
117
118 switch( type )
119 {
120 case "line":
121 Debug.WriteLine( me+" : create a MessageDrawingLine" );
122
123 try
124 {
125 nodes = data_node.SelectNodes("point");
126 Point p1, p2 ;
127 p1 = new Point( Convert.ToInt32(nodes[0].Attributes["x"].Value), Convert.ToInt32( nodes[0].Attributes["y"].Value) );
128 p2 = new Point( Convert.ToInt32(nodes[1].Attributes["x"].Value), Convert.ToInt32( nodes[1].Attributes["y"].Value) );
129
130 msg = new MessageDrawingLine( penColor, p1, p2 );
131 }
132 catch( Exception ex )
133 {
134 throw new Exception( "Malformed MessageDrawingLine : "+ex.Message +EOL +ex.StackTrace );
135 }
136 break;
137
138 case "rectangle":
139 try
140 {
141 node = data_node.SelectSingleNode("point");
142 Point point = new Point( Convert.ToInt32(node.Attributes["x"].Value), Convert.ToInt32( node.Attributes["y"].Value) );
143 node = data_node.SelectSingleNode("size");
144 Size size = new Size( Convert.ToInt32(node.Attributes["w"].Value), Convert.ToInt32( node.Attributes["h"].Value) );
145
146 if( filled )
147 {
148 msg = new MessageDrawingRectangle( filled, fillColor, point, size );
149 }
150 else
151 {
152 msg = new MessageDrawingRectangle( filled, penColor, point, size );
153 }
154 }
155 catch( Exception ex )
156 {
157 throw new Exception( "Malformed MessageDrawingRectangle : "+ex.Message +EOL +ex.StackTrace );
158 }
159 break;
160
161 case "ellipse":
162 try
163 {
164 node = data_node.SelectSingleNode("point");
165 Point point = new Point( Convert.ToInt32(node.Attributes["x"].Value), Convert.ToInt32( node.Attributes["y"].Value) );
166 node = data_node.SelectSingleNode("size");
167 Size size = new Size( Convert.ToInt32(node.Attributes["w"].Value), Convert.ToInt32( node.Attributes["h"].Value) );
168
169 if( filled )
170 {
171 msg = new MessageDrawingEllipse( filled, fillColor, point, size );
172 }
173 else
174 {
175 msg = new MessageDrawingEllipse( filled, penColor, point, size );
176 }
177 }
178 catch( Exception ex )
179 {
180 throw new Exception( "Malformed MessageDrawingEllipse : "+ex.Message +EOL +ex.StackTrace );
181 }
182 break;
183
184 case "triangle":
185 try
186 {
187 nodes = data_node.SelectNodes("point");
188 Point p1, p2, p3 ;
189 p1 = new Point( Convert.ToInt32(nodes[0].Attributes["x"].Value), Convert.ToInt32( nodes[0].Attributes["y"].Value) );
190 p2 = new Point( Convert.ToInt32(nodes[1].Attributes["x"].Value), Convert.ToInt32( nodes[1].Attributes["y"].Value) );
191 p3 = new Point( Convert.ToInt32(nodes[2].Attributes["x"].Value), Convert.ToInt32( nodes[2].Attributes["y"].Value) );
192
193 if( filled )
194 {
195 msg = new MessageDrawingTriangle( filled, fillColor, p1, p2, p3 );
196 }
197 else
198 {
199 msg = new MessageDrawingTriangle( filled, penColor, p1, p2, p3 );
200 }
201 }
202 catch( Exception ex )
203 {
204 throw new Exception( "Malformed MessageDrawingTriangle : "+ex.Message+EOL +ex.StackTrace );
205 }
206 break;
207 }
208
209 if( msg == null )
210 {
211 throw new Exception( "Malformed MessageDrawing : Unknow drawing type" );
212 }
213
214 return msg ;
215 }
216
217 public void Send( Form1 form, XmlBlasterClient xmlBlaster )
218 {
219 try
220 {
221 xmlBlaster.Publish( MessageDrawing.message_topic, this.GetXmlString() );
222 }
223 catch( XmlBlasterException ex )
224 {
225 form.printError( ex.Message );
226 //MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
227 }
228 }
229
230 }
231 class MessageDrawingLine : MessageDrawing
232 {
233 Point p1, p2 ;
234 public MessageDrawingLine( Color color, Point p1, Point p2 )
235 {
236 this.drawForm = DrawFormType.Line ;
237
238 this.filled = false ;
239 this.penRgb = color.ToArgb() ;
240 this.p1 = p1 ;
241 this.p2 = p2 ;
242 }
243 protected override void AddDataXmlString( StringBuilder sb )
244 {
245 sb.Append( "<data type=\"line\">\n" );
246 sb.Append( " <point x=\""+this.p1.X+"\" y=\""+this.p1.Y+"\" />\n" );
247 sb.Append( " <point x=\""+this.p2.X+"\" y=\""+this.p2.Y+"\" />\n" );
248 sb.Append( "</data>\n" );
249 }
250 public override void Draw( Graphics graphics )
251 {
252 Debug.WriteLine( "MessageDrawingLine.Draw()" );
253 graphics.DrawLine( new Pen(Color.FromArgb(this.penRgb)),this.p2,this.p1);
254 }
255 }
256 class MessageDrawingRectangle : MessageDrawing
257 {
258 Point orig ;
259 Size size ;
260 public MessageDrawingRectangle( bool filled, Color color, Point point, Size size )
261 {
262 drawForm = DrawFormType.Rectangle ;
263
264 this.penRgb = color.ToArgb() ;
265 this.filled = filled ;
266 this.fillRgb = this.penRgb ;
267 this.orig = point ;
268 this.size = size ;
269 }
270 protected override void AddDataXmlString( StringBuilder sb )
271 {
272 sb.Append( "<data type=\"rectangle\">\n" );
273 sb.Append( " <point x=\""+this.orig.X+"\" y=\""+this.orig.Y+"\" />\n" );
274 sb.Append( " <size w=\""+this.size.Width+"\" h=\""+this.size.Height+"\" />\n" );
275 sb.Append( "</data>\n" );
276 }
277 public override void Draw( Graphics graphics )
278 {
279 if(this.filled)
280 {
281 graphics.FillRectangle( new SolidBrush(Color.FromArgb(this.fillRgb)),orig.X,orig.Y,size.Width,size.Height);
282 }
283 else
284 {
285 graphics.DrawRectangle( new Pen(Color.FromArgb(this.penRgb)),orig.X,orig.Y,size.Width,size.Height);
286 }
287
288 }
289 }
290 class MessageDrawingEllipse : MessageDrawing
291 {
292 Point orig ;
293 Size size ;
294 public MessageDrawingEllipse( bool filled, Color color, Point point, Size size )
295 {
296 drawForm = DrawFormType.Ellipse ;
297
298 this.penRgb = color.ToArgb() ;
299 this.filled = filled ;
300 this.fillRgb = this.penRgb ;
301 this.orig = point ;
302 this.size = size ;
303 }
304 protected override void AddDataXmlString( StringBuilder sb )
305 {
306 sb.Append( "<data type=\"ellipse\">\n" );
307 sb.Append( " <point x=\""+this.orig.X+"\" y=\""+this.orig.Y+"\" />\n" );
308 sb.Append( " <size w=\""+this.size.Width+"\" h=\""+this.size.Height+"\" />\n" );
309 sb.Append( "</data>\n" );
310 }
311 public override void Draw( Graphics graphics )
312 {
313 if(this.filled)
314 graphics.FillEllipse(new SolidBrush(Color.FromArgb(this.fillRgb)),orig.X,orig.Y,size.Width,size.Height);
315 else
316 graphics.DrawEllipse(new Pen(Color.FromArgb(this.penRgb)),orig.X,orig.Y,size.Width,size.Height);
317 }
318 }
319 class MessageDrawingTriangle : MessageDrawing
320 {
321 Point p1, p2, p3 ;
322 public MessageDrawingTriangle( bool filled, Color color, Point p1, Point p2, Point p3 )
323 {
324 drawForm = DrawFormType.Triangle ;
325
326 this.penRgb = color.ToArgb() ;
327 this.filled = filled ;
328 this.fillRgb = this.penRgb ;
329 this.p1 = p1 ;
330 this.p2 = p2 ;
331 this.p3 = p3 ;
332 }
333 protected override void AddDataXmlString( StringBuilder sb )
334 {
335 sb.Append( "<data type=\"triangle\">\n" );
336 sb.Append( " <point x=\""+this.p1.X+"\" y=\""+this.p1.Y+"\" />\n" );
337 sb.Append( " <point x=\""+this.p2.X+"\" y=\""+this.p2.Y+"\" />\n" );
338 sb.Append( " <point x=\""+this.p3.X+"\" y=\""+this.p3.Y+"\" />\n" );
339 sb.Append( "</data>\n" );
340 }
341 public override void Draw( Graphics graphics )
342 {
343 if(this.filled)
344 graphics.FillPolygon(new SolidBrush(Color.FromArgb(this.fillRgb)),new Point[]{p3,p2,p1});
345 else
346 graphics.DrawPolygon(new Pen(Color.FromArgb(this.penRgb)),new Point[]{p3,p2,p1});
347 }
348 }
349
350 }
syntax highlighted by Code2HTML, v. 0.9.1