public bool TestTipePage()
        {
            PageItem = RenderItem;
            // XDocument xDox = null;
            bool errorXml = false;

            if (RenderItem.TextEditor.Text.Length > 0)
            {
                try
                {
                    xDox = XDocument.Parse(RenderItem.TextEditor.Text);
                }
                catch (XmlException ex)
                {
                    errorXml = true;

                    NewPlace(RenderItem, ex.LineNumber, ex.LinePosition);
                    Error = ex.ToString();
                    MessageBox.Show(Error);
                }
            }

            else
            {
                errorXml = true;
            }

            return(errorXml);
        }
        public Page Transform(EditableFile Item)
        {
            RenderItem = Item;


            bool errorXml = TestTipePage();

            if (errorXml == false)
            {
                RenderPage = GetCurentPage();

                BrowserSet();
                TableSet();
            }
            else
            {
                TableViewModel.Table.Clear();
                TableViewModel.Name = "";

                BrowserViewModel.Address = "about:blank";
            }
            //RenderPage.Error = Error;
            //RenderPage.Warnings = Warning;

            return(RenderPage);
        }
        public int LoadFilePreview(string file, ObservableCollection <EditableFile> Items)
        {
            int index = getFileIndex(file, Items);

            if (Items.Count > 0)
            {
                for (int i = 0; i < Items.Count; i++)
                {
                    if (Items[i].IsPreview == true)
                    {
                        Items.RemoveAt(i);
                        break;
                    }
                }
            }

            if (index < 0)
            {
                EditableFile item = new EditableFile();
                item           = CreateItem(file);
                item.IsPreview = true;
                Items.Insert(Items.Count, item);

                return(Items.Count - 1);
            }
            else
            {
                return(index);
            }
        }
        public bool?Save(EditableFile SelectedItem)
        {
            bool?            res     = null;
            string           Message = "Сохранить изменения в файле" + Environment.NewLine + SelectedItem.Header;//+ Environment.NewLine + Path.GetFileName(SelectedTab.Path)
            MessageBoxResult result  = MessageBox.Show(Message, "Сохранить", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                try
                {
                    File.WriteAllText(SelectedItem.Path, SelectedItem.TextEditor.Text);
                    res = true;
                }
                catch
                {
                    res = SaveAs(SelectedItem);
                }

                SelectedItem.IsChangedText = false;
            }

            if (result == MessageBoxResult.No)
            {
                res = false;
            }

            return(res);
        }
        public bool ClosingItem(ObservableCollection <EditableFile> Items, EditableFile Tab)
        {
            bool res = true;

            if (Tab.IsChangedText == true)
            {
                if (Save(Tab) != null)
                {
                    if (Tab.Untitled == UntitledIndex - 1)
                    {
                        UntitledIndex--;
                    }

                    Items.Remove(Tab);
                }
                else
                {
                    res = false;
                }
            }
            else
            {
                if (Tab.Untitled == UntitledIndex - 1)
                {
                    UntitledIndex--;
                }

                Items.Remove(Tab);
            }
            return(res);
        }
        public EditableFile OpenFile(ObservableCollection <EditableFile> Items)
        {
            EditableFile newItem = null;

            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Multiselect = true;
                openFileDialog.Filter      = ".xml | *.xml";

                if (openFileDialog.ShowDialog() == true)
                {
                    foreach (string path in openFileDialog.FileNames)
                    {
                        try
                        {
                            newItem = LoadFile(path, Items);
                            Transform(Items[Items.Count - 1]);
                        }
                        catch
                        {
                            MessageBox.Show("Не удается открыть " + path);
                        }
                    }
                    // Transform();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            return(newItem);
        }
        public void SetText(EditableFile SelectedItem, string param)
        {
            MyAvalonEditor TextEditor = SelectedItem.TextEditor;

            string[] tagStr = (param).Split('_');
            if (tagStr.Length == 3)
            {
                if (tagStr[1].Equals("cursor"))
                {
                    TextEditor.SelectionLength = 0;
                    TextEditor.Document.Insert(TextEditor.CaretOffset, tagStr[0] + tagStr[2]);

                    TextEditor.SelectionStart = TextEditor.SelectionStart - tagStr[2].Length;
                    TextEditor.Focus();
                }
                if (tagStr[1].Equals("cursorSelect"))
                {
                    TextEditor.Document.Insert(TextEditor.SelectionStart, tagStr[0]);
                    TextEditor.Document.Insert(TextEditor.SelectionStart + TextEditor.SelectionLength, tagStr[2]);

                    if (TextEditor.SelectionLength == 0)
                    {
                        TextEditor.SelectionStart = TextEditor.SelectionStart - tagStr[2].Length;
                    }
                    TextEditor.Focus();
                }
            }

            /*if (tagStr.Length == 2)
             * {
             *  if (tagStr[1].Equals("cursor"))
             *  {
             *      TextEditor.Document.Insert(TextEditor.CaretOffset, tagStr[0]);
             *      //TextEditor.SelectionLength = 0;
             *      TextEditor.Focus();
             *
             *      /* SelectedItem.FastTextBox.SelectedText = tagStr[0];
             *       SelectedItem.FastTextBox.Focus();
             *  }
             *  if (tagStr[0].Equals("cursor"))
             *  {
             *      TextEditor.Document.Insert(TextEditor.CaretOffset, tagStr[0]);
             *
             *      TextEditor.CaretOffset -= tagStr[0].Length;
             *      TextEditor.Focus();
             *
             *      /*SelectedItem.FastTextBox.SelectedText = tagStr[0];
             *      SelectedItem.FastTextBox.SelectionStart = SelectedItem.FastTextBox.SelectionStart - tagStr[0].Length;
             *      SelectedItem.FastTextBox.Focus();
             *  }
             * }*/

            if (tagStr.Length == 1)
            {
                TextEditor.Document.Insert(TextEditor.SelectionStart, tagStr[0]);
                TextEditor.SelectionLength = 0;
                TextEditor.Focus();
            }
        }
 public void UpdateFile(EditableFile SelectedItem)
 {
     try
     {
         SelectedItem.TextEditor.Text = File.ReadAllText(SelectedItem.Path);
         SelectedItem.IsChangedText   = false;
         //Transform(SelectedItem);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
        public EditableFile CreateItem(string path)
        {
            EditableFile item = new EditableFile
            {
                Path   = path,
                Header = Path.GetFileName(path),
                Text   = File.ReadAllText(path),

                TextEditor = new MyAvalonEditor
                {
                    Text = File.ReadAllText(path),
                },
                IsChangedText = false,
            };

            return(item);
        }
        public EditableFile CreateNewItem()
        {
            EditableFile item = new EditableFile
            {
                Header = "Без названия-" + UntitledIndex + ".xml",
                Path   = "",

                TextEditor = new MyAvalonEditor
                {
                    Text = ""
                },
                Untitled      = UntitledIndex,
                IsChangedText = false
            };

            UntitledIndex++;
            return(item);
        }
        public EditableFile LoadFile(string path, ObservableCollection <EditableFile> Items)
        {
            int          index   = getFileIndex(path, Items);
            EditableFile newItem = new EditableFile();

            if (index < 0)
            {
                newItem = CreateItem(path);
                Items.Insert(Items.Count, newItem);
                //SelectedItemIndex = Items.Count-1;
            }
            else
            {
                newItem = Items.ElementAt(index);
            }

            return(newItem);
        }
        public bool?SaveAs(EditableFile SelectedItem)
        {
            bool?res;

            SaveFileDialog saveFileDialog = new SaveFileDialog();

            {
                saveFileDialog.Filter   = ".xml | *.xml";
                saveFileDialog.FileName = SelectedItem.Header;
                res = saveFileDialog.ShowDialog();

                if (res == true)
                {
                    SelectedItem.Path          = saveFileDialog.FileName;
                    SelectedItem.Header        = Path.GetFileName(SelectedItem.Path);
                    SelectedItem.IsChangedText = false;
                    File.WriteAllText(SelectedItem.Path, SelectedItem.TextEditor.Text);
                    res = true;
                }
            }

            return(res);
        }
        /// <summary>
        /// Редактирование текста
        /// </summary>
        #region text

        public void NewPlace(EditableFile SelectedItem, int Line, int Col)
        {
            SelectedItem.TextEditor.ScrollTo(Line, Col);
            SelectedItem.TextEditor.TextArea.Caret.Position = new TextViewPosition(Line, Col);
            SelectedItem.TextEditor.Focus();
        }