// --- Вспомогательные методы --- /// <summary> /// Добавить новую вкладку с документом /// </summary> private void AddNewTab() { MenuItem newContextMenuItem = new MenuItem() { Header = "Закрыть", Command = ApplicationCommands.Close }; ContextMenu newContextMenu = new ContextMenu(); newContextMenu.Items.Add(newContextMenuItem); RichTextBox newRichTextBox = new RichTextBox(); newRichTextBox.SpellCheck.IsEnabled = true; // проверка орфографии newRichTextBox.Document.LineHeight = 0.5; // межстрочный интервал newRichTextBox.KeyUp += NewRichTextBox_KeyUp; newRichTextBox.AllowDrop = true; newRichTextBox.PreviewDragEnter += UserDocumentTab_PreviewDragEnter; newRichTextBox.ForceCursor = true; newRichTextBox.Cursor = new Cursor(@"D:\visual_studio\ООП\Lab9-10\OOPLab08\cursor.cur"); UserDocumentTab userDocumentTab = new UserDocumentTab() { Header = (string)FindResource("Key_NewFileName") + (Tabs.Items.Count + 1), Content = newRichTextBox, ContextMenu = newContextMenu, IsSelected = true }; Tabs.Items.Add(userDocumentTab); }
private void OpenRecentOpenedWindow(object sender, MouseButtonEventArgs e) { bool sameFileIsOpen = false; foreach (UserDocumentTab udt in Tabs.Items) { if (udt.Path == selectedItem) { sameFileIsOpen = true; break; } } if (!sameFileIsOpen) { AddNewTab(); UserDocumentTab currentUserDocumentTab = GetSelectedUserDocumentTab(); RichTextBox documentContent = (currentUserDocumentTab.Content as RichTextBox); using (FileStream fs = new FileStream(SelectedItem, FileMode.Open)) { new TextRange(documentContent.Document.ContentStart, documentContent.Document.ContentEnd).Load(fs, DataFormats.Rtf); currentUserDocumentTab.Header = "Opened Window"; currentUserDocumentTab.Path = SelectedItem; UpdateStatusBar(); } ChangeWindowTitle(); } }
/// <summary> /// Изменить заголовок главного окна в соответствии с открытым на данный момент файлом /// </summary> private void ChangeWindowTitle() { UserDocumentTab udt = GetSelectedUserDocumentTab(); if (udt != null) { this.Title = udt.Path ?? udt.Header as string; this.Title += " - " + mainWindowTitle; } }
/// <summary> /// Обновить количество символов и слов в статус-баре /// </summary> private void UpdateStatusBar() { UserDocumentTab userDocumentTab = GetSelectedUserDocumentTab(); if (userDocumentTab != null) { RichTextBox documentContent = userDocumentTab.Content as RichTextBox; string text = new TextRange(documentContent.Document.ContentStart, documentContent.Document.ContentEnd).Text; NumOfSymbols.Content = text.Count().ToString(); NumOfWords.Content = text.WordCount().ToString(); } }
private void SaveCommand_Executed(object sender, ExecutedRoutedEventArgs e) { UserDocumentTab currentUserDocumentTab = GetSelectedUserDocumentTab(); if (currentUserDocumentTab.Path == null) { SaveUserDocumentAs(); } else { RichTextBox documentContent = currentUserDocumentTab.Content as RichTextBox; using (FileStream fs = new FileStream(currentUserDocumentTab.Path, FileMode.Open)) { new TextRange(documentContent.Document.ContentStart, documentContent.Document.ContentEnd).Save(fs, DataFormats.Rtf); } } }
private void UserDocumentTab_PreviewDragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] docPath = (string[])e.Data.GetData(DataFormats.FileDrop); UserDocumentTab currentUserDocumentTab = GetSelectedUserDocumentTab(); RichTextBox documentContent = currentUserDocumentTab.Content as RichTextBox; using (FileStream fs = new FileStream(docPath[0], FileMode.Open)) { new TextRange(documentContent.Document.ContentStart, documentContent.Document.ContentEnd).Load(fs, DataFormats.Rtf); currentUserDocumentTab.Header = docPath[0].Split('\\').Last(); currentUserDocumentTab.Path = docPath[0]; UpdateStatusBar(); } } }
private void OpenCommand_Executed(object sender, ExecutedRoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "RTF-файлы (*.rtf)|*.rtf"; ofd.InitialDirectory = Directory.GetCurrentDirectory(); if (ofd.ShowDialog() == true) { bool sameFileIsOpen = false; foreach (UserDocumentTab udt in Tabs.Items) { if (udt.Path == ofd.FileName) { sameFileIsOpen = true; break; } } if (!sameFileIsOpen) { AddNewTab(); UserDocumentTab currentUserDocumentTab = GetSelectedUserDocumentTab(); RichTextBox documentContent = (currentUserDocumentTab.Content as RichTextBox); using (FileStream fs = new FileStream(ofd.FileName, FileMode.Open)) { new TextRange(documentContent.Document.ContentStart, documentContent.Document.ContentEnd).Load(fs, DataFormats.Rtf); currentUserDocumentTab.Header = ofd.SafeFileName; currentUserDocumentTab.Path = ofd.FileName; OpenedDocuments.Add(ofd.FileName); UpdateStatusBar(); } ChangeWindowTitle(); } } }
/// <summary> /// Открывает диалог для сохранения файла /// </summary> private void SaveUserDocumentAs() { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "RTF-файлы (*.rtf)|*.rtf"; sfd.InitialDirectory = Directory.GetCurrentDirectory(); if (sfd.ShowDialog() == true) { UserDocumentTab currentUserDocumentTab = GetSelectedUserDocumentTab(); RichTextBox documentContent = (currentUserDocumentTab.Content as RichTextBox); using (FileStream fs = new FileStream(sfd.FileName, FileMode.OpenOrCreate)) { new TextRange(documentContent.Document.ContentStart, documentContent.Document.ContentEnd).Save(fs, DataFormats.Rtf); currentUserDocumentTab.Header = sfd.SafeFileName; currentUserDocumentTab.Path = sfd.FileName; } ChangeWindowTitle(); } }