~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/ClassDiagram/ClassCanvas/Src/ClassCanvas.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.ComponentModel;
 
7
 
 
8
using System.Drawing;
 
9
using System.Drawing.Drawing2D;
 
10
using System.Windows.Forms;
 
11
 
 
12
using System.Xml;
 
13
using System.Xml.XPath;
 
14
 
 
15
using ICSharpCode.SharpDevelop;
 
16
using ICSharpCode.SharpDevelop.Dom;
 
17
using ICSharpCode.SharpDevelop.Project;
 
18
 
 
19
using Tools.Diagrams;
 
20
 
 
21
namespace ClassDiagram
 
22
{
 
23
        public partial class ClassCanvas
 
24
        {
 
25
                private class CanvasItemData : IDisposable
 
26
                {
 
27
                        public CanvasItemData (CanvasItem item,
 
28
                                               EventHandler<SizeGripEventArgs> SizeGripMouseEntered,
 
29
                                               EventHandler<SizeGripEventArgs> SizeGripMouseLeft)
 
30
                        {
 
31
                                this.item = item;
 
32
                                
 
33
                                focusDecorator = new FocusDecorator(item);
 
34
                                sizeGripDecorator = new SizeGripDecorator(item);
 
35
                                
 
36
                                sizeGripDecorator.SizeGripMouseEnter += SizeGripMouseEntered;
 
37
                                sizeGripDecorator.SizeGripMouseLeave += SizeGripMouseLeft;
 
38
                                
 
39
                                item.AddDecorator(focusDecorator);
 
40
                                item.AddDecorator(sizeGripDecorator);
 
41
                        }
 
42
                        
 
43
                        CanvasItem item;
 
44
                        
 
45
                        public CanvasItem Item
 
46
                        {
 
47
                                get { return item; }
 
48
                        }
 
49
                        
 
50
                        public bool Focused
 
51
                        {
 
52
                                get { return focusDecorator.Active; }
 
53
                                set
 
54
                                {
 
55
                                        focusDecorator.Active = value;
 
56
                                        sizeGripDecorator.Active = value;
 
57
                                }
 
58
                        }
 
59
                        
 
60
                        bool justGainedFocus;
 
61
                        public bool JustGainedFocus
 
62
                        {
 
63
                                get { return justGainedFocus; }
 
64
                                set { justGainedFocus = value; }
 
65
                        }
 
66
                        
 
67
                        FocusDecorator focusDecorator;
 
68
                        SizeGripDecorator sizeGripDecorator;
 
69
                        
 
70
                        public void Dispose()
 
71
                        {
 
72
                                item.RemoveDecorator(focusDecorator);
 
73
                                item.RemoveDecorator(sizeGripDecorator);
 
74
                        }
 
75
                }
 
76
                
 
77
                LinkedListNode<CanvasItemData> dragItemNode;
 
78
                LinkedListNode<CanvasItemData> hoverItemNode;
 
79
                LinkedList<CanvasItemData> itemsList = new LinkedList<CanvasItemData>();
 
80
                Dictionary<CanvasItem, CanvasItemData> itemsData = new Dictionary<CanvasItem, CanvasItemData>();
 
81
                Dictionary<IClass, CanvasItemData> classesToData = new Dictionary<IClass, CanvasItemData>();
 
82
                                
 
83
                DiagramRouter diagramRouter = new DiagramRouter();
 
84
                
 
85
                public event EventHandler ZoomChanged = delegate { };
 
86
                float zoom = 1.0f;
 
87
                bool ctrlDown;
 
88
                bool holdRedraw;
 
89
                bool redrawNeeded;
 
90
                
 
91
                PointF lastMouseClickPosition;
 
92
                
 
93
                public ClassCanvas()
 
94
                {
 
95
                        //
 
96
                        // The InitializeComponent() call is required for Windows Forms designer support.
 
97
                        //
 
98
                        InitializeComponent();
 
99
                }
 
100
                
 
101
                #region Diagram Activities
 
102
                
 
103
                public float Zoom
 
104
                {
 
105
                        get { return zoom; }
 
106
                        set
 
107
                        {
 
108
                                zoom = value;
 
109
                                pictureBox1.Invalidate();
 
110
                                LayoutChanged (this, EventArgs.Empty);
 
111
                                ZoomChanged(this, EventArgs.Empty);
 
112
                        }
 
113
                }
 
114
                
 
115
                public void CollapseAll ()
 
116
                {
 
117
                        foreach (CanvasItemData item in itemsList)
 
118
                        {
 
119
                                ClassCanvasItem classitem = item.Item as ClassCanvasItem;
 
120
                                if (classitem != null)
 
121
                                        classitem.Collapsed = true;
 
122
                        }
 
123
                        LayoutChanged (this, EventArgs.Empty);
 
124
                }
 
125
                
 
126
                public void ExpandAll ()
 
127
                {
 
128
                        foreach (CanvasItemData item in itemsList)
 
129
                        {
 
130
                                ClassCanvasItem classitem = item.Item as ClassCanvasItem;
 
131
                                if (classitem != null)
 
132
                                        classitem.Collapsed = false;
 
133
                        }
 
134
                        LayoutChanged (this, EventArgs.Empty);
 
135
                }
 
136
                
 
137
                public void MatchAllWidths ()
 
138
                {
 
139
                        foreach (CanvasItemData item in itemsList)
 
140
                        {
 
141
                                ClassCanvasItem classitem = item.Item as ClassCanvasItem;
 
142
                                if (classitem != null)
 
143
                                        classitem.Width = classitem.GetAbsoluteContentWidth();
 
144
                        }
 
145
                        LayoutChanged (this, EventArgs.Empty);
 
146
                }
 
147
                
 
148
                public void ShrinkAllWidths ()
 
149
                {
 
150
                        foreach (CanvasItemData item in itemsList)
 
151
                        {
 
152
                                ClassCanvasItem classitem = item.Item as ClassCanvasItem;
 
153
                                if (classitem != null)
 
154
                                        classitem.Width = 0;
 
155
                        }
 
156
                        LayoutChanged (this, EventArgs.Empty);
 
157
                }
 
158
                
 
159
                #endregion
 
160
                
 
161
                public SizeF GetDiagramLogicalSize ()
 
162
                {
 
163
                        float w=1, h=1;
 
164
                        foreach (CanvasItemData item in itemsList)
 
165
                        {
 
166
                                w = Math.Max(w, item.Item.X + item.Item.ActualWidth + item.Item.Border);
 
167
                                h = Math.Max(h, item.Item.Y + item.Item.ActualHeight + item.Item.Border);
 
168
                        }
 
169
                        return new SizeF(w + 50, h + 50);
 
170
                }
 
171
                
 
172
                public Size GetDiagramPixelSize ()
 
173
                {
 
174
                        float zoom = Math.Max(this.zoom, 0.1f);
 
175
                        SizeF size = GetDiagramLogicalSize();
 
176
                        return new Size((int)(size.Width * zoom), (int)(size.Height * zoom));
 
177
                }
 
178
                
 
179
                public void SetRecommendedGraphicsAttributes (Graphics graphics)
 
180
                {
 
181
                        if (graphics == null) return;
 
182
                        graphics.CompositingQuality = CompositingQuality.HighSpeed;
 
183
                        //graphics.SmoothingMode = SmoothingMode.AntiAlias;
 
184
                        graphics.SmoothingMode = SmoothingMode.HighQuality;
 
185
                        graphics.PageUnit = GraphicsUnit.Pixel;
 
186
                        graphics.PixelOffsetMode = PixelOffsetMode.Half;
 
187
                        graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
 
188
                }
 
189
                
 
190
                public void DrawToGraphics(Graphics graphics)
 
191
                {
 
192
                        foreach (CanvasItemData item in itemsList)
 
193
                                item.Item.DrawToGraphics(graphics);
 
194
 
 
195
                        DrawRoutes(graphics);
 
196
                }
 
197
                
 
198
                private void PictureBox1Paint (object sender, PaintEventArgs e)
 
199
                {
 
200
//                      System.Diagnostics.Debug.WriteLine("ClassCanvas.PictureBox1Paint");
 
201
                        Size bbox = GetDiagramPixelSize();
 
202
                        
 
203
                        pictureBox1.Width = Math.Min(10000, bbox.Width + 100);
 
204
                        pictureBox1.Height = Math.Min(10000, bbox.Height + 100);
 
205
 
 
206
                        e.Graphics.PageScale = zoom;
 
207
                        SetRecommendedGraphicsAttributes(e.Graphics);
 
208
                        DrawToGraphics(e.Graphics);
 
209
                }
 
210
                
 
211
                private void DrawRoutes (Graphics g)
 
212
                {
 
213
                        foreach (Route route in diagramRouter.Routes)
 
214
                        {
 
215
                                route.Recalc(itemsList as IEnumerable<IRectangle>);
 
216
                                PointF origin = route.GetStartPoint();
 
217
                                RouteSegment[] segments = route.RouteSegments;
 
218
                                foreach (RouteSegment rs in segments)
 
219
                                {
 
220
                                        PointF dest = rs.CreateDestinationPoint(origin);
 
221
                                        g.DrawLine(Pens.Black, origin, dest);
 
222
                                        origin = dest;
 
223
                                }
 
224
                                
 
225
                                if (route.EndShape != null)
 
226
                                        ((RouteShape)route.EndShape).Draw(g, route, true);
 
227
 
 
228
                                if (route.StartShape != null)
 
229
                                        ((RouteShape)route.StartShape).Draw(g, route, false);
 
230
                        }
 
231
                }
 
232
                
 
233
                private LinkedListNode<CanvasItemData> FindCanvasItemNode (PointF pos)
 
234
                {
 
235
                        LinkedListNode<CanvasItemData> itemNode = itemsList.Last;
 
236
                        while (itemNode != null)
 
237
                        {
 
238
                                if (itemNode.Value.Item.HitTest(pos))
 
239
                                {
 
240
                                        return itemNode;
 
241
                                }
 
242
                                itemNode = itemNode.Previous;
 
243
                        }
 
244
                        return null;
 
245
                }
 
246
                
 
247
                #region Diagram Items Drag and Selection
 
248
                
 
249
                private void PictureBox1MouseClick (object sender, MouseEventArgs e)
 
250
                {
 
251
                        PointF pos = new PointF(e.X / zoom, e.Y / zoom);
 
252
                        lastMouseClickPosition = pos;
 
253
                        LinkedListNode<CanvasItemData> itemNode = FindCanvasItemNode(pos);
 
254
                        if (itemNode != null)
 
255
                        {
 
256
                                itemNode.Value.Item.HandleMouseClick(pos);
 
257
                                if (itemNode.Value.Focused)
 
258
                                {
 
259
                                        if (itemNode.Value.JustGainedFocus)
 
260
                                        {
 
261
                                                itemNode.Value.JustGainedFocus = false;
 
262
                                        }
 
263
                                        else if (itemNode.Value.Item.StartEditing())
 
264
                                        {
 
265
                                                Control ec = itemNode.Value.Item.GetEditingControl();
 
266
                                                if (ec != null)
 
267
                                                {
 
268
                                                        //TODO - refactor this damn thing... why couldn't they make the "Scale" scale the font as well?
 
269
                                                        ec.Scale(new SizeF(zoom, zoom));
 
270
                                                        Font ecf = ec.Font;
 
271
                                                        ec.Font = new Font(ecf.FontFamily,
 
272
                                                                           ecf.Size * zoom,
 
273
                                                                           ecf.Style, ec.Font.Unit,
 
274
                                                                           ecf.GdiCharSet, ec.Font.GdiVerticalFont);
 
275
                                                        ec.Hide();
 
276
                                                        ec.VisibleChanged += delegate { if (!ec.Visible) ec.Font = ecf; };
 
277
                                                        panel1.Controls.Add(ec);
 
278
                                                        ec.Top -= panel1.VerticalScroll.Value;
 
279
                                                        ec.Left -= panel1.HorizontalScroll.Value;
 
280
                                                        ec.Show();
 
281
                                                        panel1.Controls.SetChildIndex(ec, 0);
 
282
                                                        this.ActiveControl = ec;
 
283
                                                        ec.Focus();
 
284
                                                }
 
285
                                        }
 
286
                                }
 
287
                        }
 
288
                }
 
289
                
 
290
                private void PictureBox1MouseDown (object sender, MouseEventArgs e)
 
291
                {
 
292
                        HoldRedraw = true;
 
293
                        PointF pos = new PointF(e.X / zoom, e.Y / zoom);
 
294
                        LinkedListNode<CanvasItemData> itemNode = FindCanvasItemNode(pos);
 
295
                        dragItemNode = itemNode;
 
296
                        
 
297
                        if (!ctrlDown)
 
298
                        {
 
299
                                foreach (CanvasItemData item in itemsList)
 
300
                                {
 
301
                                        item.Item.StopEditing();
 
302
                                        if (itemNode == null || item != itemNode.Value)
 
303
                                                item.Focused = false;
 
304
                                }
 
305
                        }
 
306
                        
 
307
                        if (itemNode != null)
 
308
                        {
 
309
                                if (!itemNode.Value.Focused)
 
310
                                {
 
311
                                        itemNode.Value.JustGainedFocus = true;
 
312
                                        itemNode.Value.Focused = true;
 
313
                                        itemsList.Remove(itemNode);
 
314
                                        itemsList.AddLast(itemNode);
 
315
                                        CanvasItemSelected (this, new CanvasItemEventArgs (itemNode.Value.Item));
 
316
                                }
 
317
                                itemNode.Value.Item.HandleMouseDown(pos);
 
318
                        }
 
319
                        HoldRedraw = false;
 
320
                }
 
321
                
 
322
                private void PictureBox1MouseMove (object sender, MouseEventArgs e)
 
323
                {
 
324
                        HoldRedraw = true;
 
325
                        PointF pos = new PointF(e.X / zoom, e.Y / zoom);
 
326
                        if (dragItemNode != null)
 
327
                                dragItemNode.Value.Item.HandleMouseMove(pos);
 
328
                        else
 
329
                        {
 
330
                                LinkedListNode<CanvasItemData> itemNode = FindCanvasItemNode(pos);
 
331
                                if (hoverItemNode != itemNode)
 
332
                                {
 
333
                                        if (hoverItemNode != null && hoverItemNode.Value != null)
 
334
                                                hoverItemNode.Value.Item.HandleMouseLeave();
 
335
                                        hoverItemNode = itemNode;
 
336
                                }
 
337
                                
 
338
                                if (itemNode != null)
 
339
                                        itemNode.Value.Item.HandleMouseMove(pos);
 
340
                        }
 
341
                        HoldRedraw = false;
 
342
                }
 
343
                
 
344
                private void PictureBox1MouseUp (object sender, MouseEventArgs e)
 
345
                {
 
346
                        PointF pos = new PointF(e.X / zoom, e.Y / zoom);
 
347
                        
 
348
                        if (dragItemNode != null)
 
349
                                dragItemNode.Value.Item.HandleMouseUp(pos);
 
350
                        dragItemNode = null;
 
351
 
 
352
                        LinkedListNode<CanvasItemData> itemNode = FindCanvasItemNode(pos);
 
353
                        if (itemNode != null)
 
354
                                itemNode.Value.Item.HandleMouseUp(pos);
 
355
                }
 
356
                
 
357
                #endregion
 
358
                
 
359
                private bool HoldRedraw
 
360
                {
 
361
                        get { return holdRedraw; }
 
362
                        set
 
363
                        {
 
364
                                holdRedraw = value;
 
365
                                if (!value && redrawNeeded)
 
366
                                {
 
367
                                        redrawNeeded = false;
 
368
                                        HandleRedraw (this, EventArgs.Empty);
 
369
                                }
 
370
                        }
 
371
                }
 
372
 
 
373
                private void HandleItemLayoutChange (object sender, EventArgs args)
 
374
                {
 
375
                        LayoutChanged (this, args);
 
376
                        if (HoldRedraw)
 
377
                                redrawNeeded = true;
 
378
                        else
 
379
                                HandleRedraw (sender, args);
 
380
                }
 
381
 
 
382
                private void HandleRedraw (object sender, EventArgs args)
 
383
                {
 
384
                        if (HoldRedraw)
 
385
                        {
 
386
                                redrawNeeded = true;
 
387
                                return;
 
388
                        }
 
389
//                      System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
 
390
//                      System.Diagnostics.Debug.WriteLine(st.ToString());
 
391
                        this.Invalidate(true);
 
392
                }
 
393
                
 
394
                private void HandleItemPositionChange (object sender, ValueChangingEventArgs<PointF> args)
 
395
                {
 
396
                        PointF pos = new PointF(args.Value.X, args.Value.Y);
 
397
                        
 
398
                        pos.X = Math.Max ((float) Math.Round(pos.X / 10.0f) * 10.0f, 50.0f);
 
399
                        pos.Y = Math.Max ((float) Math.Round(pos.Y / 10.0f) * 10.0f, 50.0f);
 
400
                        
 
401
                        args.Cancel = (pos.X == args.Value.X) && (pos.Y == args.Value.Y);
 
402
                        args.Value = pos;
 
403
                }
 
404
                
 
405
                private void HandleItemSizeChange (object sender, ValueChangingEventArgs<SizeF> args)
 
406
                {
 
407
                        SizeF size = new SizeF(args.Value);
 
408
                        
 
409
                        size.Width = (float) Math.Round(size.Width / 10.0f) * 10.0f;
 
410
                        size.Height = (float) Math.Round(size.Height / 10.0f) * 10.0f;
 
411
                        
 
412
                        args.Cancel = (size.Width == args.Value.Width) && (size.Height == args.Value.Height);
 
413
                        args.Value = size;
 
414
                }
 
415
                                
 
416
                private void SizeGripMouseEntered (object sender, SizeGripEventArgs e)
 
417
                {
 
418
                        if ((e.GripPosition & SizeGripPositions.EastWest) != SizeGripPositions.None)
 
419
                        {
 
420
                                pictureBox1.Cursor = Cursors.SizeWE;
 
421
                        }
 
422
                        else if ((e.GripPosition & SizeGripPositions.NorthSouth) != SizeGripPositions.None)
 
423
                        {
 
424
                                pictureBox1.Cursor = Cursors.SizeNS;
 
425
                        }
 
426
                }
 
427
 
 
428
                private void SizeGripMouseLeft (object sender, SizeGripEventArgs e)
 
429
                {
 
430
                        pictureBox1.Cursor = Cursors.Default;
 
431
                }
 
432
                
 
433
                public void AddCanvasItem (CanvasItem item)
 
434
                {
 
435
                        diagramRouter.AddItem(item);
 
436
                        CanvasItemData itemData = new CanvasItemData(item, SizeGripMouseEntered, SizeGripMouseLeft);
 
437
                        itemsData[item] = itemData;
 
438
 
 
439
                        ClassCanvasItem classItem = item as ClassCanvasItem;
 
440
                        if (classItem != null)
 
441
                        {
 
442
                                classesToData.Add(classItem.RepresentedClassType, itemData);
 
443
                                foreach (CanvasItemData ci in itemsList)
 
444
                                {
 
445
                                        ClassCanvasItem cci = ci.Item as ClassCanvasItem;
 
446
                                        if (cci != null)
 
447
                                        {
 
448
                                                Route r = null;
 
449
                                                if (cci.RepresentedClassType == classItem.RepresentedClassType.BaseClass)
 
450
                                                        r = diagramRouter.AddRoute(item, cci);
 
451
                                                else if (classItem.RepresentedClassType == cci.RepresentedClassType.BaseClass)
 
452
                                                        r = diagramRouter.AddRoute(cci, classItem);
 
453
                                                
 
454
                                                if (r != null)
 
455
                                                        r.EndShape = new RouteInheritanceShape();
 
456
                                        }
 
457
                                }
 
458
                        }
 
459
 
 
460
                        itemsList.AddLast(itemData);
 
461
                        item.RedrawNeeded += HandleRedraw;
 
462
                        item.LayoutChanged += HandleItemLayoutChange;
 
463
                        item.PositionChanging += HandleItemPositionChange;
 
464
                        item.SizeChanging += HandleItemSizeChange;
 
465
                }
 
466
                
 
467
                public void RemoveCanvasItem (CanvasItem item)
 
468
                {
 
469
                        itemsList.Remove(itemsData[item]);
 
470
                        Stack<Route> routesToRemove = new Stack<Route>();
 
471
                        foreach (Route r in diagramRouter.Routes)
 
472
                        {
 
473
                                if (r.From == item || r.To == item)
 
474
                                        routesToRemove.Push(r);
 
475
                        }
 
476
                        
 
477
                        foreach (Route r in routesToRemove)
 
478
                                diagramRouter.RemoveRoute(r);
 
479
 
 
480
                        diagramRouter.RemoveItem (item);
 
481
 
 
482
                        ClassCanvasItem classItem = item as ClassCanvasItem;
 
483
                        if (classItem != null)
 
484
                        {
 
485
                                classesToData.Remove (classItem.RepresentedClassType);
 
486
                        }
 
487
                        
 
488
                        LayoutChanged(this, EventArgs.Empty);
 
489
                }
 
490
                
 
491
                public void ClearCanvas()
 
492
                {
 
493
                        itemsList.Clear();
 
494
                        classesToData.Clear();
 
495
                        itemsList.Clear();
 
496
                        dragItemNode = null;
 
497
                        hoverItemNode = null;
 
498
                        diagramRouter.Clear();
 
499
                }
 
500
                
 
501
                /// <summary>
 
502
                /// Retruns a copy of the the canvas items list as an array.
 
503
                /// </summary>
 
504
                public CanvasItem[] GetCanvasItems()
 
505
                {
 
506
                        CanvasItem[] items = new CanvasItem[itemsList.Count];
 
507
                        int i = 0;
 
508
                        foreach (CanvasItemData item in itemsList)
 
509
                                items[i++] = item.Item;
 
510
                        return items;
 
511
                }
 
512
                
 
513
                public bool Contains (IClass ct)
 
514
                {
 
515
                        return classesToData.ContainsKey(ct);
 
516
                        /*
 
517
                        foreach (CanvasItemData ci in itemsList)
 
518
                        {
 
519
                                ClassCanvasItem cci = ci.Item as ClassCanvasItem;
 
520
                                if (cci != null)
 
521
                                        if (cci.RepresentedClassType.Equals(ct)) return true;
 
522
                        }*/
 
523
                        
 
524
                        //return false;
 
525
                }
 
526
                
 
527
                public void AutoArrange ()
 
528
                {
 
529
                        diagramRouter.RecalcPositions();
 
530
                }
 
531
                
 
532
                public static ClassCanvasItem CreateItemFromType (IClass ct)
 
533
                {
 
534
                        if (ct == null) return null;
 
535
                        ClassCanvasItem item = null;
 
536
                        if (ct.ClassType == ClassType.Interface)
 
537
                                item = new InterfaceCanvasItem(ct);
 
538
                        else if (ct.ClassType == ClassType.Enum)
 
539
                                item = new EnumCanvasItem(ct);
 
540
                        else if (ct.ClassType == ClassType.Struct)
 
541
                                item = new StructCanvasItem(ct);
 
542
                        else if (ct.ClassType == ClassType.Delegate)
 
543
                                item = new DelegateCanvasItem(ct);
 
544
                        else
 
545
                                item = new ClassCanvasItem(ct);
 
546
                        item.Initialize();
 
547
                        return item;
 
548
                }
 
549
                
 
550
                #region File Save/Load
 
551
                
 
552
                [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1059:MembersShouldNotExposeCertainConcreteTypes", MessageId = "System.Xml.XmlNode")]
 
553
                public XmlDocument WriteToXml ()
 
554
                {
 
555
                        XmlDocument doc = new XmlDocument();
 
556
                        doc.LoadXml("<ClassDiagram/>");
 
557
                        
 
558
                        XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", "yes");
 
559
                        doc.InsertBefore(decl, doc.FirstChild);
 
560
                        
 
561
                        XmlAttribute zoom = doc.CreateAttribute("Zoom"); // Non-Standard attribute
 
562
                        zoom.Value = Zoom.ToString(System.Globalization.CultureInfo.InvariantCulture);
 
563
                        doc.DocumentElement.Attributes.Append(zoom);
 
564
                        
 
565
                        #region unsupported attributes - added for compatability
 
566
                        // FIXME - Attribute not yet supported
 
567
                        XmlAttribute majorVersion = doc.CreateAttribute("MajorVersion");
 
568
                        majorVersion.Value = "1";
 
569
                        doc.DocumentElement.Attributes.Append(majorVersion);
 
570
                        
 
571
                        // FIXME - Attribute not yet supported
 
572
                        XmlAttribute minorVersion = doc.CreateAttribute("MinorVersion");
 
573
                        minorVersion.Value = "1";
 
574
                        doc.DocumentElement.Attributes.Append(minorVersion);
 
575
 
 
576
                        // FIXME - Attribute not yet supported
 
577
                        XmlAttribute membersFormat = doc.CreateAttribute("MembersFormat");
 
578
                        membersFormat.Value = "FullSignature";
 
579
                        doc.DocumentElement.Attributes.Append(membersFormat);
 
580
                        
 
581
                        // FIXME - Element not yet supported
 
582
                        XmlAttribute fontName = doc.CreateAttribute("Name");
 
583
                        fontName.Value = "Tahoma";
 
584
 
 
585
                        XmlAttribute fontSize = doc.CreateAttribute("Size");
 
586
                        fontSize.Value = "8.25";
 
587
                        
 
588
                        XmlElement fontElement = doc.CreateElement("Font");
 
589
                        fontElement.Attributes.Append(fontName);
 
590
                        fontElement.Attributes.Append(fontSize);
 
591
                        #endregion
 
592
                        
 
593
                        foreach (CanvasItemData item in itemsList)
 
594
                        {
 
595
                                item.Item.WriteToXml(doc);
 
596
                        }
 
597
                        return doc;
 
598
                }
 
599
                
 
600
                public void LoadFromXml (IXPathNavigable doc, IProjectContent pc)
 
601
                {
 
602
                        if (pc == null) return;
 
603
                        if (doc == null) return;
 
604
                        ClearCanvas();
 
605
                        
 
606
                        XPathNavigator nav = doc.CreateNavigator();
 
607
                        XPathNodeIterator ni = nav.Select(@"/ClassDiagram/Class | /ClassDiagram/Struct | /ClassDiagram/Enum | /ClassDiagram/Interface | /ClassDiagram/Delegate");
 
608
                        while (ni.MoveNext())
 
609
                        {
 
610
                                string typeName = ni.Current.GetAttribute("Name", "");
 
611
                                IClass ct = pc.GetClass(typeName, 0);
 
612
                                ClassCanvasItem canvasitem = ClassCanvas.CreateItemFromType(ct);
 
613
                                if (canvasitem != null)
 
614
                                {
 
615
                                        canvasitem.LoadFromXml (ni.Current);
 
616
                                        AddCanvasItem(canvasitem);
 
617
                                }
 
618
                        }
 
619
                        ni = nav.Select(@"/ClassDiagram/Comment");
 
620
                        while (ni.MoveNext())
 
621
                        {
 
622
                                NoteCanvasItem note = new NoteCanvasItem();
 
623
                                note.LoadFromXml(ni.Current);
 
624
                                AddCanvasItem(note);
 
625
                        }
 
626
                }
 
627
                
 
628
                #endregion
 
629
                
 
630
                public event EventHandler LayoutChanged = delegate {};
 
631
                public event EventHandler<CanvasItemEventArgs> CanvasItemSelected = delegate {};
 
632
                
 
633
                public Bitmap GetAsBitmap ()
 
634
                {
 
635
                        Size bbox = GetDiagramPixelSize();
 
636
                        Bitmap bitmap = new Bitmap(bbox.Width, bbox.Height);
 
637
                        Graphics g = Graphics.FromImage(bitmap);
 
638
                        g.PageScale = zoom;
 
639
                        SetRecommendedGraphicsAttributes(g);
 
640
                        DrawToGraphics(g);
 
641
                        return bitmap;
 
642
                }
 
643
                
 
644
                public void SaveToImage (string filename)
 
645
                {
 
646
                        GetAsBitmap().Save(filename);
 
647
                }
 
648
                
 
649
                public PointF LastMouseClickPosition
 
650
                {
 
651
                        get { return lastMouseClickPosition; }
 
652
                }
 
653
                
 
654
                #region Drag/Drop from Class Browser Handling
 
655
                
 
656
                private void ClassCanvasDragOver(object sender, DragEventArgs e)
 
657
                {
 
658
                        e.Effect = DragDropEffects.Link;
 
659
                }
 
660
                
 
661
                private void ClassCanvasDragDrop(object sender, DragEventArgs e)
 
662
                {
 
663
                        
 
664
                }
 
665
                
 
666
                #endregion
 
667
                
 
668
                void ClassCanvasKeyDown(object sender, KeyEventArgs e)
 
669
                {
 
670
                        if (e.KeyCode == Keys.Control)
 
671
                                ctrlDown = true;
 
672
                }
 
673
                
 
674
                void ClassCanvasKeyUp(object sender, KeyEventArgs e)
 
675
                {
 
676
                        if (e.KeyCode == Keys.Control)
 
677
                                ctrlDown = false;
 
678
                }
 
679
        }
 
680
 
 
681
        public class CanvasItemEventArgs : EventArgs
 
682
        {
 
683
                public CanvasItemEventArgs (CanvasItem canvasItem)
 
684
                {
 
685
                        this.canvasItem = canvasItem;
 
686
                }
 
687
                
 
688
                private CanvasItem canvasItem;
 
689
                
 
690
                public CanvasItem CanvasItem
 
691
                {
 
692
                        get { return canvasItem; }
 
693
                }
 
694
        }
 
695
        
 
696
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
 
697
        public enum TestEnum {Dog, Cat, Fish};
 
698
}