Beispiel #1
0
        public XDrawing GetDrawing(Canvas canvas)
        {
            if (m_Drawing != null && m_Drawing.Canvas != canvas)
            {
                StoreXmlCode();
                m_Drawing.Canvas.ClearValue(Canvas.WidthProperty);
                m_Drawing.Canvas.ClearValue(Canvas.HeightProperty);
                m_Drawing.Dispose();
                m_Drawing = null;
            }
            if (m_Drawing == null)
            {
                var bw = new Binding("DrawingSize.Width");
                bw.Source = this;
                canvas.SetBinding(Canvas.WidthProperty, bw);
                bw        = new Binding("DrawingSize.Height");
                bw.Source = this;
                canvas.SetBinding(Canvas.HeightProperty, bw);

                m_Drawing = new XDrawing(canvas);
                if (m_XmlCode != null)
                {
                    m_Drawing.Import(m_XmlCode);
                }
            }
            return(m_Drawing);
        }
Beispiel #2
0
        internal static XDrawingShape CreateFromXml(XDrawing drawing, Path path, XElement xGeometry)
        {
            var nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = ".";

            var tmpDoc = XDocument.Parse((xGeometry.Parent.FirstNode as XComment).Value);

            path.Data = null;
            var txt = new XDrawingText(drawing, path);

            var props = tmpDoc.Root.Attribute("TopLeft").Value.Split(',', ' ');

            txt.TopLeft = new Point(Double.Parse(props[0], nfi), Double.Parse(props[1], nfi));

            props           = tmpDoc.Root.Attribute("BottomRight").Value.Split(',', ' ');
            txt.BottomRight = new Point(Double.Parse(props[0], nfi), Double.Parse(props[1], nfi));

            txt.Text       = tmpDoc.Root.Attribute("Text").Value;
            txt.FontFamily = new FontFamily(tmpDoc.Root.Attribute("FontFamily").Value);
            txt.FontStyle  = tmpDoc.Root.Attribute("FontStyle").Value == "Italic" ? FontStyles.Italic : FontStyles.Normal;
            txt.FontWeight = tmpDoc.Root.Attribute("FontWeight").Value == "Bold" ? FontWeights.Bold : FontWeights.Normal;
            txt.TextSize   = Double.Parse(tmpDoc.Root.Attribute("TextSize").Value, nfi);
            txt.Alignment  = (TextAlignment)Enum.Parse(typeof(TextAlignment), tmpDoc.Root.Attribute("Alignment").Value);
            txt.Trimming   = (TextTrimming)Enum.Parse(typeof(TextTrimming), tmpDoc.Root.Attribute("Trimming").Value);

            return(txt);
        }
Beispiel #3
0
        public static HitTestInfo CreateNewByDrag(XDrawing drawing, Point startPt, out XDrawingModes newMode)
        {
            var path = new Path();

            path.Stroke          = new SolidColorBrush(Colors.Black);
            path.StrokeThickness = 1.0;

            var pathG  = new PathGeometry();
            var figure = new PathFigure();

            figure.StartPoint = startPt;
            var segment = new LineSegment(startPt, true);

            figure.Segments.Add(segment);
            pathG.Figures.Add(figure);
            path.Data = pathG;

            var          shape = new XDrawingPath(drawing, path);
            ControlPoint cp;

            shape.CreateControlPoints(true, out cp, null);
            newMode = XDrawingModes.Edit;
            var hti = new HitTestInfo();

            hti.Shape        = shape;
            hti.Offset       = new Vector(0.0, 0.0);
            hti.ControlPoint = cp;
            return(hti);
        }
Beispiel #4
0
        internal static XDrawingShape LoadFromPath(XDrawing drawing, XElement xPath)
        {
            var p = new
            {
                SimpleFillBrush      = (string)xPath.Attributes("Fill").FirstOrDefault(),
                ComplexFillBrushNode = (from fb in xPath.Elements("Path.Fill")
                                        select fb.Elements().FirstOrDefault()).FirstOrDefault(),
                Thickness              = (string)xPath.Attributes("StrokeThickness").FirstOrDefault(),
                DashCap                = (string)xPath.Attributes("StrokeDashCap").FirstOrDefault(),
                StartLineCap           = (string)xPath.Attributes("StrokeStartLineCap").FirstOrDefault(),
                EndLineCap             = (string)xPath.Attributes("StrokeEndLineCap").FirstOrDefault(),
                LineJoin               = (string)xPath.Attributes("StrokeLineJoin").FirstOrDefault(),
                MiterLimit             = (string)xPath.Attributes("StrokeMiterLimit").FirstOrDefault(),
                SimpleStrokeBrush      = (string)xPath.Attributes("Stroke").FirstOrDefault(),
                ComplexStrokeBrushNode = (from sb in xPath.Elements("Path.Stroke")
                                          select sb.Elements().FirstOrDefault()).FirstOrDefault(),
                DashArray    = (string)xPath.Attributes("StrokeDashArray").FirstOrDefault(),
                DashOffset   = (string)xPath.Attributes("StrokeDashOffset").FirstOrDefault(),
                GeometryNode = (from g in xPath.Elements("Path.Data")
                                select g.Elements().FirstOrDefault()).FirstOrDefault()
            };

            return(CreateShape(drawing,
                               CreateBrush(p.SimpleFillBrush, p.ComplexFillBrushNode),
                               p.Thickness,
                               p.DashCap,
                               p.StartLineCap,
                               p.EndLineCap,
                               p.LineJoin,
                               p.MiterLimit,
                               CreateBrush(p.SimpleStrokeBrush, p.ComplexStrokeBrushNode),
                               p.DashArray,
                               p.DashOffset,
                               p.GeometryNode));
        }
Beispiel #5
0
        public XDrawingShape(XDrawing drawing, Path path)
        {
            Drawing = drawing;
            Path    = path;

            SetValue(StrokeDashProperty, DashArrayToString(Path.StrokeDashArray));
        }
Beispiel #6
0
        public XDrawingText(XDrawing drawing, Path path)
            : base(drawing, path)
        {
            m_MoveControlPoint = new ControlPoint(Drawing, this, this, XDrawingText.TopLeftProperty, 0, false, false);
            Path.Tag           = this;

            CreateGeom();
        }
Beispiel #7
0
 public void CloseDrawing()
 {
     if (m_Drawing != null)
     {
         StoreXmlCode();
         m_Drawing.Dispose();
         m_Drawing = null;
     }
 }
Beispiel #8
0
 public XDrawingLine(XDrawing drawing, Path path)
     : base(drawing, path)
 {
     if (!(Path.Data is LineGeometry))
     {
         throw new ArgumentException("path.Data must be of type LineGeometry");
     }
     Path.Tag = this;
 }
Beispiel #9
0
 public XDrawingEllipse(XDrawing drawing, Path path)
     : base(drawing, path)
 {
     if (!(Path.Data is EllipseGeometry))
     {
         throw new ArgumentException("path.Data must be of type EllipseGeometry");
     }
     m_MoveControlPoint = new ControlPoint(Drawing, this, EllipseGeom, EllipseGeometry.CenterProperty, 0, false, false);
     Path.Tag           = this;
 }
Beispiel #10
0
        internal static XDrawingShape CreateFromXml(XDrawing drawing, Path path, XElement xGeometry)
        {
            var nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = ".";

            var lGeom = new LineGeometry(
                Point.Parse(xGeometry.Attribute("StartPoint").Value),
                Point.Parse(xGeometry.Attribute("EndPoint").Value));

            path.Data = lGeom;
            return(new XDrawingLine(drawing, path));
        }
Beispiel #11
0
 public XDrawingRectangle(XDrawing drawing, Path path)
     : base(drawing, path)
 {
     if (!(Path.Data is RectangleGeometry))
     {
         throw new ArgumentException("path.Data must be of type RectangleGeometry");
     }
     m_MoveControlPoint = new ControlPoint(Drawing, this, RectGeom, RectangleGeometry.RectProperty, -1, false, false);
     Path.Tag           = this;
     Width  = RectGeom.Rect.Width;
     Height = RectGeom.Rect.Height;
     DependencyPropertyDescriptor.FromProperty(RectangleGeometry.RectProperty, typeof(RectangleGeometry)).AddValueChanged(Path.Data, RectChanged);
 }
Beispiel #12
0
        internal static XDrawingShape CreateFromXml(XDrawing drawing, Path path, XElement xGeometry)
        {
            var nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = ".";

            var eGeom = new EllipseGeometry(
                Point.Parse(xGeometry.Attribute("Center").Value),
                Double.Parse(xGeometry.Attribute("RadiusX").Value, nfi),
                Double.Parse(xGeometry.Attribute("RadiusY").Value, nfi));

            path.Data = eGeom;
            return(new XDrawingEllipse(drawing, path));
        }
Beispiel #13
0
        public static HitTestInfo CreateNewByDrag(XDrawing drawing, Point startPt, out XDrawingModes newMode)
        {
            var path = new Path();

            path.Stroke          = new SolidColorBrush(Colors.Black);
            path.StrokeThickness = 1.0;
            path.Data            = new EllipseGeometry(startPt, 0.0, 5.0);
            var          shape = new XDrawingEllipse(drawing, path);
            ControlPoint cp;

            shape.CreateControlPoints(false, out cp);
            //newMode = XDrawingModes.Select;
            newMode = XDrawingModes.Edit;
            var hti = new HitTestInfo();

            hti.Shape        = shape;
            hti.Offset       = new Vector(0.0, 0.0);
            hti.ControlPoint = cp;
            return(hti);
        }
Beispiel #14
0
        internal static XDrawingShape CreateFromXml(XDrawing drawing, Path path, XElement xGeometry)
        {
            var nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = ".";

            var rGeom = new RectangleGeometry(
                Rect.Parse(xGeometry.Attribute("Rect").Value));

            if (xGeometry.Attribute("RadiusX") != null)
            {
                rGeom.RadiusX = Double.Parse(xGeometry.Attribute("RadiusX").Value, nfi);
            }
            if (xGeometry.Attribute("RadiusY") != null)
            {
                rGeom.RadiusY = Double.Parse(xGeometry.Attribute("RadiusY").Value, nfi);
            }

            path.Data = rGeom;
            return(new XDrawingRectangle(drawing, path));
        }
Beispiel #15
0
 public ControlPoint(XDrawing drawing, XDrawingShape shape,
                     DependencyObject obj, DependencyProperty prop,
                     int dings, bool visible, bool isSelectable)
 {
     ConnectedTo    = null;
     Drawing        = drawing;
     Shape          = shape;
     m_Object       = obj;
     m_Prop         = prop;
     m_Dings        = dings;
     m_IsSelectable = isSelectable;
     if (m_Prop != null)
     {
         DependencyPropertyDescriptor.FromProperty(m_Prop, m_Object.GetType()).AddValueChanged(m_Object, PropertyChanged);
     }
     if (visible)
     {
         m_CPGeom = new EllipseGeometry(GetPoint(), 5.0 / Drawing.ZoomFactor, 5.0 / Drawing.ZoomFactor);
         Drawing.ControlPointGroup.Children.Add(m_CPGeom);
     }
 }
Beispiel #16
0
        public static HitTestInfo CreateNewByDrag(XDrawing drawing, Point startPt, out XDrawingModes newMode)
        {
            var path = new Path();

            path.Fill            = new SolidColorBrush(Colors.Black);
            path.StrokeThickness = 1.0;
            var shape = new XDrawingText(drawing, path);

            shape.TopLeft     = startPt;
            shape.BottomRight = startPt;
            ControlPoint cp;

            shape.CreateControlPoints(false, out cp);
            //newMode = XDrawingModes.Select;
            newMode = XDrawingModes.Edit;
            var hti = new HitTestInfo();

            hti.Shape        = shape;
            hti.Offset       = new Vector(0.0, 0.0);
            hti.ControlPoint = cp;
            return(hti);
        }
Beispiel #17
0
        internal static XDrawingShape CreateFromXml(XDrawing drawing, Path path, XElement xGeometry)
        {
            var nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = ".";

            var pathG = new PathGeometry();

            var figuresStr = (string)xGeometry.Attributes("Figures").FirstOrDefault();

            if (figuresStr != null)
            {
                LoadFromGeomMiniLang(pathG, figuresStr);
            }
            else
            {
                LoadPathFigures(pathG, xGeometry);
            }

            path.Data = pathG;
            return(new XDrawingPath(drawing, path));
        }
Beispiel #18
0
        private static XDrawingShape CreateShape(XDrawing drawing, Brush fillBrush, string penThickness, string penDashCap,
                                                 string penStartLineCap, string penEndLineCap, string penLineJoin, string penMiterLimit,
                                                 Brush penBrush, string penDashArray, string penDashOffset, XElement xGeometry)
        {
            var nfi = new NumberFormatInfo();

            nfi.NumberDecimalSeparator = ".";

            var path = new Path();

            path.Fill = fillBrush;
            if (penThickness != null)
            {
                path.StrokeThickness = Double.Parse(penThickness, nfi);
            }
            if (penDashCap != null)
            {
                path.StrokeDashCap = (PenLineCap)Enum.Parse(typeof(PenLineCap), penDashCap);
            }
            if (penStartLineCap != null)
            {
                path.StrokeStartLineCap = (PenLineCap)Enum.Parse(typeof(PenLineCap), penStartLineCap);
            }
            if (penEndLineCap != null)
            {
                path.StrokeEndLineCap = (PenLineCap)Enum.Parse(typeof(PenLineCap), penEndLineCap);
            }
            if (penLineJoin != null)
            {
                path.StrokeLineJoin = (PenLineJoin)Enum.Parse(typeof(PenLineJoin), penLineJoin);
            }
            if (penMiterLimit != null)
            {
                path.StrokeMiterLimit = Double.Parse(penMiterLimit, nfi);
            }
            path.Stroke = penBrush;
            if (penDashArray != null)
            {
                XDrawingShape.StringToDashArray(penDashArray, path.StrokeDashArray);
            }
            if (penDashOffset != null)
            {
                path.StrokeDashOffset = Double.Parse(penDashOffset, nfi);
            }

            var types = typeof(XDrawingShape).Assembly.GetTypes();

            foreach (var type in types)
            {
                if (type.IsSubclassOf(typeof(XDrawingShape)))
                {
                    var attributes = type.GetCustomAttributes(typeof(XDrawGeometryAttribute), false) as XDrawGeometryAttribute[];
                    var geomName   = xGeometry.Name.LocalName;

                    // check if we have a TextGeometry, which is no "real" XAML node type
                    if (String.CompareOrdinal(geomName, "GeometryGroup") == 0)
                    {
                        var c = xGeometry.Parent.FirstNode as XComment;

                        if (c != null && c.Value.StartsWith("<TextGeometry"))
                        {
                            geomName = "TextGeometry";
                        }
                    }

                    if (attributes.Length >= 1 && String.CompareOrdinal(geomName, attributes[0].GeometryName) == 0)
                    {
                        var mi = type.GetMethod(
                            "CreateFromXml",
                            BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic,
                            null,
                            new Type[] { typeof(XDrawing), typeof(Path), typeof(XElement) },
                            null);
                        if (mi == null)
                        {
                            throw new Exception("shape type does not support static method CreateFromXml");
                        }
                        return(mi.Invoke(null, new object[] { drawing, path, xGeometry }) as XDrawingShape);
                    }
                }
            }

            throw new Exception("Unknown geometry type: " + xGeometry.Name);
        }
Beispiel #19
0
        internal static XDrawingShape LoadFromGeometryDrawing(XDrawing drawing, XElement xGD)
        {
            var gd = new
            {
                SimpleFillBrush      = (string)xGD.Attributes("Brush").FirstOrDefault(),
                ComplexFillBrushNode = (from b in xGD.Elements("GeometryDrawing.Brush")
                                        select b.Elements().FirstOrDefault()).FirstOrDefault(),
                Pen = (from gdp in xGD.Elements("GeometryDrawing.Pen")
                       select(from p in gdp.Elements("Pen")
                              select new
                {
                    Thickness = (string)p.Attributes("Thickness").FirstOrDefault(),
                    DashCap = (string)p.Attributes("DashCap").FirstOrDefault(),
                    StartLineCap = (string)p.Attributes("StartLineCap").FirstOrDefault(),
                    EndLineCap = (string)p.Attributes("EndLineCap").FirstOrDefault(),
                    LineJoin = (string)p.Attributes("LineJoin").FirstOrDefault(),
                    MiterLimit = (string)p.Attributes("MiterLimit").FirstOrDefault(),
                    SimpleBrush = (string)p.Attributes("Brush").FirstOrDefault(),
                    ComplexBrushNode = (from b in p.Elements("Pen.Brush")
                                        select b.Elements().FirstOrDefault()).FirstOrDefault(),
                    DashStyle = (from pds in p.Elements("Pen.DashStyle")
                                 select(from ds in pds.Elements("DashStyle")
                                        select new
                    {
                        Dashes = (string)ds.Attributes("Dashes").FirstOrDefault(),
                        Offset = (string)ds.Attributes("Offset").FirstOrDefault(),
                    }).FirstOrDefault()).FirstOrDefault()
                }).FirstOrDefault()).FirstOrDefault(),
                GeometryNode = (from g in xGD.Elements("GeometryDrawing.Geometry")
                                select g.Elements().FirstOrDefault()).FirstOrDefault()
            };

            if (gd.Pen != null)
            {
                if (gd.Pen.DashStyle != null)
                {
                    return(CreateShape(drawing,
                                       CreateBrush(gd.SimpleFillBrush, gd.ComplexFillBrushNode),
                                       gd.Pen.Thickness,
                                       gd.Pen.DashCap,
                                       gd.Pen.StartLineCap,
                                       gd.Pen.EndLineCap,
                                       gd.Pen.LineJoin,
                                       gd.Pen.MiterLimit,
                                       CreateBrush(gd.Pen.SimpleBrush, gd.Pen.ComplexBrushNode),
                                       gd.Pen.DashStyle.Dashes,
                                       gd.Pen.DashStyle.Offset,
                                       gd.GeometryNode));
                }
                else
                {
                    return(CreateShape(drawing,
                                       CreateBrush(gd.SimpleFillBrush, gd.ComplexFillBrushNode),
                                       gd.Pen.Thickness,
                                       gd.Pen.DashCap,
                                       gd.Pen.StartLineCap,
                                       gd.Pen.EndLineCap,
                                       gd.Pen.LineJoin,
                                       gd.Pen.MiterLimit,
                                       CreateBrush(gd.Pen.SimpleBrush, gd.Pen.ComplexBrushNode),
                                       null, null,
                                       gd.GeometryNode));
                }
            }
            else
            {
                return(CreateShape(drawing,
                                   CreateBrush(gd.SimpleFillBrush, gd.ComplexFillBrushNode),
                                   null, null, null, null,
                                   null, null, null, null, null,
                                   gd.GeometryNode));
            }
        }