Пример #1
0
        public void SaveToXml(XmlDocument xmlDoc, XmlNode XMLNode, string sXMLEncoding, Selection Selection_Collection, string nspace)
        {
            NodesList list;
            XmlNode targetXmlNode = null;
            XmlNode selNode = null;
            bool ownSel = false;
            if ((Selection_Collection != null) && (this == Selection_Collection.parent))
            {
                ownSel = true;
                targetXmlNode = XMLNode;
            }
            
            if (type_ == null)
            {
                return;
            }

            if (!ownSel)
            {
                if (type_.type == ElementType.Entity)
                {
                    targetXmlNode = xmlDoc.CreateNode(XmlNodeType.EntityReference, xmlTagName, nspace);
                }
                else if ((type_.xmlTag != null) && (type_.xmlTag.Length > 0))
                {
                    {
                        targetXmlNode = xmlDoc.CreateNode(XmlNodeType.Element, type_.xmlTag, nspace);

                        if (((type_.type == ElementType.Ms) ||
                             (type_.type == ElementType.Mtext)) &&
                            (((targetXmlNode != null) && (literalText != null)) && (literalText.Length > 0)))
                        {
                            string s = literalText;
                            if (Selection_Collection != null)
                            {
                                if ((Selection_Collection.First != null) && (Selection_Collection.First == this))
                                {
                                    if ((Selection_Collection.Last != null) && (Selection_Collection.Last == this))
                                    {
                                        s = s.Substring(Selection_Collection.caret,
                                                            Selection_Collection.literalLength -
                                                            Selection_Collection.caret);
                                    }
                                    else
                                    {
                                        s = s.Substring(Selection_Collection.caret,
                                                            s.Length - Selection_Collection.caret);
                                    }
                                }
                                else if (((Selection_Collection.Last != null) && (Selection_Collection.Last == this)) &&
                                         (Selection_Collection.First != this))
                                {
                                    s = s.Substring(0, Selection_Collection.literalLength);
                                }
                            }
                            selNode = xmlDoc.CreateTextNode(s);
                            targetXmlNode.AppendChild(selNode);
                        }
                        else if (((type_.type != ElementType.Mglyph) && (targetXmlNode != null)) &&
                                 ((literalText != null) && (literalText.Length > 0)))
                        {
                            string s = literalText;
                            if (Selection_Collection != null)
                            {
                                if ((Selection_Collection.First != null) && (Selection_Collection.First == this))
                                {
                                    if ((Selection_Collection.Last != null) && (Selection_Collection.Last == this))
                                    {
                                        s =
                                            s.Substring(Selection_Collection.caret,
                                                            Selection_Collection.literalLength -
                                                            Selection_Collection.caret);
                                    }
                                    else
                                    {
                                        s =
                                            s.Substring(Selection_Collection.caret,
                                                            s.Length - Selection_Collection.caret);
                                    }
                                }
                                else if (((Selection_Collection.Last != null) && (Selection_Collection.Last == this)) &&
                                         (Selection_Collection.First != this))
                                {
                                    s = s.Substring(0, Selection_Collection.literalLength);
                                }
                            }
                            selNode = xmlDoc.CreateTextNode(s);
                            targetXmlNode.AppendChild(selNode);
                        }
                    }
                }
            }
            if (targetXmlNode == null)
            {
                return;
            }
            
            if (XMLNode == null)
            {
                string xml = "";
                if (sXMLEncoding == "UTF-16")
                {
                    xml = "<?xml version='1.0' encoding='UTF-16'?>";
                }
                else if (sXMLEncoding == "UTF-8")
                {
                    xml = "<?xml version='1.0' encoding='UTF-8'?>";
                }
                else if (sXMLEncoding.Length > 0)
                {
                    xml = "<?xml version='1.0' encoding='" + sXMLEncoding + "'?>";
                }
                else
                {
                    xml = "<?xml version='1.0'?>";
                }
                xml = xml + "<root/>";
                xmlDoc.LoadXml(xml);
                if ((type_.type == ElementType.Math) && displayStyle)
                {
                    XmlAttribute attribute = xmlDoc.CreateAttribute("", "display", "");
                    attribute.Value = "block";
                    targetXmlNode.Attributes.Append(attribute);
                }
                
                xmlDoc.ReplaceChild(targetXmlNode, xmlDoc.DocumentElement);
            }
            else if ((!ownSel && !false) && !false)
            {
                {
                    XMLNode.AppendChild(targetXmlNode);
                }
                if ((type_.type == ElementType.Math) && displayStyle)
                {
                    XmlAttribute attribute = xmlDoc.CreateAttribute("", "display", "");
                    attribute.Value = "block";
                    targetXmlNode.Attributes.Append(attribute);
                    XMLNode.AppendChild(targetXmlNode);
                }
            }
            if (((((Selection_Collection == null) || (this != Selection_Collection.parent)))) && ((!HasChildren())))
            {
                return;
            }
            if ((Selection_Collection != null) && (this == Selection_Collection.parent))
            {
                list = Selection_Collection.nodesList;
            }
            else
            {
                list = GetChildrenNodes();
            }
            
            list.Reset();
            Node next = list.Next();
            int level = 0;
            bool sameSel = false;
            bool isPrev = false;
            bool isNext = false;
            XmlNode curXmlNode = targetXmlNode;
            bool ok = true;
            while ((next != null) && ok)
            {
                sameSel = false;
                isPrev = false;
                isNext = false;

                if (next.type_.type != ElementType.Entity)
                {
                    if ((Selection_Collection != null) && (next == Selection_Collection.First))
                    {
                        if (next.IsSameStyleParent())
                        {
                            sameSel = true;
                        }
                    }
                    else if (next.prevSibling == null)
                    {
                        if (next.IsSameStyleParent())
                        {
                            sameSel = true;
                        }
                    }
                    else if (!next.IsSameStyle(next.prevSibling))
                    {
                        if (next.IsSameStyleParent() && next.prevSibling.IsSameStyleParent())
                        {
                            isNext = true;
                        }
                        else
                        {
                            if (next.prevSibling.IsSameStyleParent())
                            {
                                isPrev = true;
                            }
                            if (next.IsSameStyleParent())
                            {
                                sameSel = true;
                            }
                        }
                    }
                }
                if (isNext)
                {
                    if (next.prevSibling.IsSameStyleParent() && (level > 0))
                    {
                        curXmlNode = curXmlNode.ParentNode;
                        level--;
                    }
                    sameSel = true;
                }
                if (sameSel && next.IsSameStyleParent())
                {
                    Node snode;
                    if (next.style_ == null)
                    {
                        snode = new Node();
                        next.style_ = new StyleAttributes();
                    }
                    if ((next.parent_ != null) && (next.parent_.style_ != null))
                    {
                        snode = new Node(next.parent_.style_);
                    }
                    else
                    {
                        snode = new Node(new StyleAttributes());
                    }
                    XmlNode mstyleNode = null;
                    mstyleNode = xmlDoc.CreateNode(XmlNodeType.Element, "mstyle", nspace);
                    AttributeBuilder.CascadeStyles(next.parent_, snode, next.style_);
                    if (snode.attrs != null)
                    {
                        snode.attrs.Reset();
                        for (Attribute i = snode.attrs.Next(); i != null; i = snode.attrs.Next())
                        {
                            XmlAttribute attr = xmlDoc.CreateAttribute("", i.name, "");
                            attr.Value = i.val;
                            mstyleNode.Attributes.Append(attr);
                        }
                        snode.attrs.Reset();
                    }
                    curXmlNode.AppendChild(mstyleNode);
                    curXmlNode = mstyleNode;
                    level++;
                }
                if (isPrev && next.prevSibling.IsSameStyleParent())
                {
                    curXmlNode = curXmlNode.ParentNode;
                    level--;
                }

                next.SaveToXml(xmlDoc, curXmlNode, sXMLEncoding, Selection_Collection, nspace);
                next = list.Next();
                if (((Selection_Collection != null) && (Selection_Collection.Last != null)) &&
                    ((next == Selection_Collection.Last) && (Selection_Collection.literalLength == 0)))
                {
                    ok = false;
                }
            }
        }
Пример #2
0
        //
        private bool SaveToXml (XmlDocument doc, Selection sel)
        {
            bool r = false;
            XmlNode root = null;
            string decl = "<?xml version='1.0' encoding='UTF-8'?>";
            try
            {
                if (sel.nodesList.Count <= 0)
                {
                    return r;
                }
                bool ok = false;


                decl = decl + "<math xmlns='http://www.w3.org/1998/Math/MathML'/>";
                doc.LoadXml(decl);
                root = doc.DocumentElement;
                ok = true;


                if (!ok)
                {
                    return r;
                }
                try
                {
                    sel.parent.SaveToXml(doc, root, "UTF-8", sel);
                    return true;
                }
                catch (Exception)
                {
                    return r;
                }
            }
            catch
            {
                r = false;
            }
            return r;
        }
Пример #3
0
 public void SaveToXml(XmlDocument xmlDoc, XmlNode XMLNode, string sXMLEncoding, Selection Selection_Collection)
 {
     SaveToXml(xmlDoc, XMLNode, sXMLEncoding, Selection_Collection, namespaceURI);
 }
Пример #4
0
 //
 private Node TearSelection (Selection collection, ref int updatedMark)
 {
     Node newRow = null;
     Node c = null;
     updatedMark = 0;
     bool samePar = false;
     try
     {
         if (((((collection != null) && (collection.nodesList != null)) &&
               ((collection.nodesList.Count > 0) && (collection.First != null))) &&
              (((collection.Last != null) && (collection.First.parent_ != null)) &&
               ((collection.Last.parent_ != null) && (collection.parent == collection.First.parent_)))) &&
             (collection.parent == collection.Last.parent_))
         {
             samePar = true;
         }
         if (samePar)
         {
             bool ok = false;
             if (collection.First == collection.Last)
             {
                 if (collection.First.IsAtom())
                 {
                     if ((collection.caret > 0) || (collection.literalLength < collection.Last.LiteralLength))
                     {
                         c = collection.First;
                         updatedMark = collection.caret;
                     }
                     else
                     {
                         ok = true;
                     }
                 }
                 ok = true;
             }
             else if (collection.First.IsAtom())
             {
                 if (collection.caret > 0)
                 {
                     c = collection.First;
                     updatedMark = collection.caret;
                 }
                 else
                 {
                     if (collection.First.prevSibling != null)
                     {
                         c = collection.First.prevSibling;
                         updatedMark = collection.First.prevSibling.LiteralLength;
                     }
                     ok = true;
                 }
             }
             else
             {
                 ok = true;
             }
             if (((c == null) && ok) &&
                 ((collection.First != collection.Last) && (collection.literalLength < collection.Last.LiteralLength)))
             {
                 c = collection.Last;
                 updatedMark = 0;
             }
             if (c == null)
             {
                 if (collection.Last.nextSibling != null)
                 {
                     c = collection.Last.nextSibling;
                     updatedMark = 0;
                 }
                 else if (collection.First.prevSibling != null)
                 {
                     c = collection.First.prevSibling;
                     updatedMark = collection.First.prevSibling.LiteralLength;
                 }
                 else
                 {
                     c = collection.First.parent_;
                     updatedMark = 0;
                 }
             }
             Node cur = null;
             collection.nodesList.Reset ();
             for (cur = collection.nodesList.Next (); cur != null; cur = collection.nodesList.Next ())
             {
                 if (((cur == collection.First) || ((cur == collection.Last) && (collection.literalLength > 0))) &&
                     (cur.IsAtom()))
                 {
                     if ((cur.literalText != null) && (cur.literalText.Length > 0))
                     {
                         int mark = 0;
                         int len = 2;
                         try
                         {
                             if (collection.nodesList.Count == 1)
                             {
                                 mark = collection.caret;
                                 len = collection.literalLength;
                             }
                             else if (cur == collection.First)
                             {
                                 mark = collection.caret;
                                 len = cur.LiteralLength;
                             }
                             else if (cur == collection.Last)
                             {
                                 mark = 0;
                                 len = collection.literalLength;
                             }
                             if ((mark == 0) && (len == cur.LiteralLength))
                             {
                                 this.Tear (cur);
                             }
                             else if ((mark == 0) && (len < cur.LiteralLength))
                             {
                                 cur.literalText = cur.literalText.Substring (len, cur.LiteralLength - len);
                                 cur.LiteralStart = 0;
                             }
                             else if ((mark > 0) && (len == cur.LiteralLength))
                             {
                                 cur.literalText = cur.literalText.Substring (0, mark);
                             }
                             else
                             {
                                 cur.literalText = cur.literalText.Substring (0, mark) +
                                                    cur.literalText.Substring (len, cur.LiteralLength - len);
                             }
                         }
                         catch
                         {
                         }
                     }
                 }
                 else
                 {
                     try
                     {
                         if ((cur != collection.Last) || ((cur == collection.Last) && (collection.literalLength == cur.LiteralLength)))
                         {
                             this.Tear (cur, true, true, ref newRow);
                         }
                     }
                     catch
                     {
                     }
                 }
             }
             collection.nodesList.Reset ();
         }
     }
     catch
     {
     }
     try
     {
         if ((((newRow != null) && !newRow.tagDeleted) && ((newRow.parent_ != null) && (newRow.type_ != null))) &&
             ((newRow.type_.type == ElementType.Mrow) && (this.selectedNode == newRow)))
         {
             c = newRow;
             updatedMark = 0;
             return c;
         }
         if ((c != null) && !c.isVisible)
         {
             bool valid = false;
             Node n = null;
             n = c;
             while ((!valid && (n.prevSibling != null)) && !n.prevSibling.tagDeleted)
             {
                 if (n.prevSibling.isVisible && !n.prevSibling.tagDeleted)
                 {
                     valid = true;
                 }
                 n = n.prevSibling;
             }
             if (!valid)
             {
                 for (n = c;
                      (!valid && (n.nextSibling != null)) && !n.nextSibling.tagDeleted;
                      n = n.nextSibling)
                 {
                     if (n.nextSibling.isVisible && !n.nextSibling.tagDeleted)
                     {
                         valid = true;
                     }
                 }
             }
             if ((!valid && (c.parent_ != null)) &&
                 (c.parent_.isVisible && !c.parent_.tagDeleted))
             {
                 n = c.parent_;
             }
             if ((valid && (n != null)) && (n.isVisible && !n.tagDeleted))
             {
                 c = n;
                 updatedMark = 0;
             }
             return c;
         }
         if ((c == null) ||
             ((c.type_.type != ElementType.Mtd) &&
              ((c.type_.type != ElementType.Math) || (c.parent_ != null))))
         {
             return c;
         }
         if (c.type_.type == ElementType.Mtd)
         {
             if (c.firstChild != null)
             {
                 c = c.firstChild;
                 updatedMark = 0;
             }
             return c;
         }
         if (((c.type_.type != ElementType.Math) || (c.parent_ != null)) ||
             (c.firstChild == null))
         {
             return c;
         }
         if ((c.firstChild.type_.type == ElementType.Mtable) && (c.firstChild.Class == "nugentoplevel"))
         {
             if ((((c.firstChild.firstChild != null) &&
                   (c.firstChild.firstChild.type_.type == ElementType.Mtr)) &&
                  ((c.firstChild.firstChild.firstChild != null) &&
                   (c.firstChild.firstChild.firstChild.type_.type == ElementType.Mtd))) &&
                 (c.firstChild.firstChild.firstChild.firstChild != null))
             {
                 c = c.firstChild.firstChild.firstChild.firstChild;
                 updatedMark = 0;
             }
             return c;
         }
         c = c.firstChild;
         updatedMark = 0;
     }
     catch
     {
     }
     return c;
 }
Пример #5
0
 //
 private StyleAttributes GetSelectionStyle (StyleAttributes styleAttributes, Selection selectionCollection)
 {
     try
     {
         if (((selectionCollection == null) || (selectionCollection.nodesList.Count <= 0)))
         {
             return styleAttributes;
         }
         int count = selectionCollection.nodesList.Count;
         Node n = selectionCollection.nodesList.Next ();
         if (n.style_ != null)
         {
             n.style_.CopyTo (styleAttributes);
             return styleAttributes;
         }
         styleAttributes = new StyleAttributes ();
     }
     catch
     {
     }
     return styleAttributes;
 }
Пример #6
0
 //
 private void ApplyStyleToSelection (StyleAttributes styleAttributes, Selection selectionCollection)
 {
     try
     {
         if (((selectionCollection == null) || (selectionCollection.nodesList.Count <= 0)))
         {
             return;
         }
         int count = selectionCollection.nodesList.Count;
         selectionCollection.nodesList.Reset ();
         for (int i = 0; i < count; i++)
         {
             Node n = selectionCollection.nodesList.Next ();
             if (((i <= 0) || (n != selectionCollection.Last)) || (selectionCollection.literalLength != 0))
             {
                 styleAttributes.hasBackground = true;
                 styleAttributes.hasColor = true;
                 n.SetStyle (styleAttributes);
             }
         }
         selectionCollection.nodesList.Reset ();
     }
     catch
     {
     }
 }
Пример #7
0
 //
 public Selection CaptureSelection (Node from, int fromCaret, Node to, int toCaret, bool needSwap)
 {
     Selection r = new Selection ();
     r.swap = needSwap;
     r.parent = from.parent_;
     
     Node cur = from;
     bool done = false;
     while ((cur != null) && !done)
     {
         if (((cur == from) || (cur == to)) &&
             (((cur.IsAtom() && (cur.parent_ != null)) && cur.isVisible)))
         {
             if ((cur.literalText != null) && (cur.literalText.Length > 0))
             {
                 int curCaret = 0;
                 int len = 0;
                 if ((cur == from) && (cur == to))
                 {
                     curCaret = fromCaret;
                     len = toCaret;
                     if (len < 0)
                     {
                         len = cur.LiteralLength;
                     }
                     if (curCaret > len)
                     {
                         int t = len;
                         len = curCaret;
                         curCaret = t;
                     }
                     if (len > curCaret)
                     {
                         r.caret = curCaret;
                         r.literalLength = len;
                         r.Add (cur);
                     }
                 }
                 else if (cur == from)
                 {
                     curCaret = fromCaret;
                     if (curCaret < 0)
                     {
                         curCaret = 0;
                     }
                     r.caret = curCaret;
                     r.Add (cur);
                 }
                 else if (cur == to)
                 {
                     len = toCaret;
                     if (len < 0)
                     {
                         len = cur.LiteralLength;
                     }
                     r.literalLength = len;
                     r.Add (cur);
                 }
             }
         }
         else if ((cur == from) && (cur == to))
         {
             int curCaret = 0;
             int len = 0;
             curCaret = fromCaret;
             len = toCaret;
             if (len < 0)
             {
                 len = cur.LiteralLength;
             }
             if (curCaret > len)
             {
                 int t = len;
                 len = curCaret;
                 curCaret = t;
             }
             else
             {
             }
             if (len > curCaret)
             {
                 r.caret = curCaret;
                 r.literalLength = len;
                 r.Add (cur);
             }
         }
         else
         {
             if (cur != to)
             {
                 r.Add (cur);
             }
             if (cur == to)
             {
                 if (toCaret == cur.LiteralLength)
                 {
                     r.literalLength = cur.LiteralLength;
                     r.Add (cur);
                     if (cur == from)
                     {
                         r.caret = 0;
                     }
                 }
                 else if (cur == from)
                 {
                     r.literalLength = from.LiteralLength;
                     r.caret = 0;
                 }
                 else if (cur.prevSibling != null)
                 {
                     r.literalLength = cur.prevSibling.LiteralLength;
                 }
             }
         }
         if (cur == to)
         {
             done = true;
             continue;
         }
         cur = cur.nextSibling;
     }
     return r;
 }
Пример #8
0
        //
        private void DrawHighlightSelection (Selection SelectionCollection, PaintEventArgs e)
        {
            try
            {
                Node multi = builder_.MultiSelectNode ();
                Node first = SelectionCollection.First;
                Node last = SelectionCollection.Last;
                SelectionCollection.nodesList.Reset ();
                if (multi != null)
                {
                    SelectionCollection.nodesList.Reset();
                    for (Node par = SelectionCollection.nodesList.Next();
                         par != null;
                         par = SelectionCollection.nodesList.Next())
                    {
                        if ((par.IsAtom() && (par == first)) && (par == last))
                        {
                            int markX = 0;
                            int markEnd = 0;
                            markX = builder_.WidthToMark(par, SelectionCollection.caret);
                            markEnd = builder_.WidthToMark(par, SelectionCollection.literalLength);

                            if (markX > markEnd)
                            {
                                int t = markX;
                                markX = markEnd;
                                markEnd = t;
                            }
                            e.Graphics.FillRectangle(selectionBrush_,
                                                     ((par.box.X + markX) + lMargin) - OffsetX,
                                                     (par.box.Y + tMargin) - OffsetY, markEnd - markX,
                                                     par.box.Height);
                        }
                        else if (par.IsAtom() && (par == first))
                        {
                            int markX = 0;
                            markX = builder_.WidthToMark(par, SelectionCollection.caret);
                            e.Graphics.FillRectangle(selectionBrush_,
                                                     ((par.box.X + markX) + lMargin) - OffsetX,
                                                     (par.box.Y + tMargin) - OffsetY,
                                                     par.box.Width - markX, par.box.Height);
                        }
                        else if (par.IsAtom() && (par == last))
                        {
                            int markEnd = 0;
                            markEnd = builder_.WidthToMark(par, SelectionCollection.literalLength);
                            if (par == multi)
                            {
                            }
                            e.Graphics.FillRectangle(selectionBrush_,
                                                     (par.box.X + lMargin) - OffsetX,
                                                     (par.box.Y + tMargin) - OffsetY, markEnd,
                                                     par.box.Height);
                        }
                        else
                        {
                            bool draw = false;
                            if (SelectionCollection.nodesList.Count == 1)
                            {
                                if ((SelectionCollection.caret == 0) &&
                                    (SelectionCollection.literalLength == par.LiteralLength))
                                {
                                    draw = true;
                                }
                            }
                            else if (par == first)
                            {
                                if (SelectionCollection.caret == 0)
                                {
                                    draw = true;
                                }
                            }
                            else if (par == last)
                            {
                                if (SelectionCollection.literalLength == par.LiteralLength)
                                {
                                    draw = true;
                                }
                            }
                            else
                            {
                                draw = true;
                            }
                            if (draw)
                            {
                                e.Graphics.FillRectangle(selectionBrush_,
                                                         (par.box.X + lMargin) - OffsetX,
                                                         (par.box.Y + tMargin) - OffsetY,
                                                         par.box.Width, par.box.Height);
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }