private void AddBlock(Block block, TextSelection selection) { if (selection == null || selection.End == null) AddBlockToEnd(block); else _flowDocument.Blocks.InsertAfter(selection.End.GetInsertionPosition(LogicalDirection.Forward).Paragraph, block); }
public FontFamilyHandler(swd.TextSelection range, sw.Controls.RichTextBox control) { Control = range.GetPropertyValue(swd.TextElement.FontFamilyProperty) as swm.FontFamily ?? swd.TextElement.GetFontFamily(control); var familyMapName = Control.FamilyNames.Select(r => r.Value).FirstOrDefault(); Name = familyMapName ?? Control.Source; }
//----------------------------------------------------- // // Constructors // //----------------------------------------------------- #region Constructors /// <summary> /// Initialize the TextEditor /// </summary> /// <param name="textContainer"> /// TextContainer representing a content to edit. /// </param> /// <param name="uiScope"> /// FrameworkElement on which all events for the user interaction will be /// processed. /// </param> /// <param name="isUndoEnabled"> /// If true the TextEditor will enable undo support /// </param> internal TextEditor(ITextContainer textContainer, FrameworkElement uiScope, bool isUndoEnabled) { // Validate parameters Invariant.Assert(uiScope != null); // Set non-zero property defaults. _acceptsRichContent = true; // Attach the editor instance to the scope _textContainer = textContainer; _uiScope = uiScope; // Enable undo manager for this uiScope if (isUndoEnabled && _textContainer is TextContainer) { ((TextContainer)_textContainer).EnableUndo(_uiScope); } // Create TextSelection and link it to text container _selection = new TextSelection(this); textContainer.TextSelection = _selection; // Create DragDropProcess // _dragDropProcess = new TextEditorDragDrop._DragDropProcess(this); // By default we use IBeam cursor _cursor = Cursors.IBeam; // Add InputLanguageChanged event handler TextEditorTyping._AddInputLanguageChangedEventHandler(this); // Listen to both TextContainer.EndChanging and TextContainer.Changed events TextContainer.Changed += new TextContainerChangedEventHandler(OnTextContainerChanged); // Add IsEnabled event handler for cleaning the caret element when uiScope is disabled _uiScope.IsEnabledChanged += new DependencyPropertyChangedEventHandler(OnIsEnabledChanged); // Attach this instance of text editor to its uiScope _uiScope.SetValue(TextEditor.InstanceProperty, this); // The IsSpellerEnabled property might have been set before this // TextEditor was instantiated -- check if we need to rev // up speller support. if ((bool)_uiScope.GetValue(SpellCheck.IsEnabledProperty)) { SetSpellCheckEnabled(true); SetCustomDictionaries(true); } // If no IME/TextServices are installed, we have no native reasources // to clean up at Finalizer. if (!TextServicesLoader.ServicesInstalled) { GC.SuppressFinalize(this); } }
public FontTypefaceHandler(swd.TextSelection range, sw.Controls.RichTextBox control) { var family = range.GetPropertyValue(swd.TextElement.FontFamilyProperty) as swm.FontFamily ?? swd.TextElement.GetFontFamily(control); var style = range.GetPropertyValue(swd.TextElement.FontStyleProperty) as sw.FontStyle? ?? swd.TextElement.GetFontStyle(control); var weight = range.GetPropertyValue(swd.TextElement.FontWeightProperty) as sw.FontWeight? ?? swd.TextElement.GetFontWeight(control); var stretch = range.GetPropertyValue(swd.TextElement.FontStretchProperty) as sw.FontStretch? ?? swd.TextElement.GetFontStretch(control); Control = new swm.Typeface(family, style, weight, stretch); }
public FontHandler(swd.TextSelection range, sw.FrameworkElement control) { var wpfFamily = range.GetPropertyValue(swd.TextElement.FontFamilyProperty) as swm.FontFamily ?? swd.TextElement.GetFontFamily(control); this.Family = new FontFamily(new FontFamilyHandler(wpfFamily)); this.Size = PixelsToPoints(range.GetPropertyValue(swd.TextElement.FontSizeProperty) as double? ?? swd.TextElement.GetFontSize(control), control); this.WpfFontStyle = range.GetPropertyValue(swd.TextElement.FontStyleProperty) as sw.FontStyle? ?? swd.TextElement.GetFontStyle(control); this.WpfFontWeight = range.GetPropertyValue(swd.TextElement.FontWeightProperty) as sw.FontWeight? ?? swd.TextElement.GetFontWeight(control); var decorations = range.GetPropertyValue(swd.Inline.TextDecorationsProperty) as sw.TextDecorationCollection; if (decorations != null) { this.WpfTextDecorations = new sw.TextDecorationCollection(decorations); } }
public static void GenerateClipBoardData(DataObjectCopyingEventArgs e, TextSelection selection) { string str; using (MemoryStream stream = new MemoryStream()) { TextRange range = new TextRange(selection.Start, selection.End); range.ClearAllProperties(); range.Save(stream, DataFormats.Xaml, true); stream.Flush(); stream.Position = 0L; using (StreamReader reader = new StreamReader(stream)) { str = reader.ReadToEnd(); } } if (!string.IsNullOrEmpty(str)) { string str2 = ReplaceControls.ReplaceGUIWithClipboardControl(str, selection.Start, selection.End); if (!string.IsNullOrEmpty(str2) && (str2 != str)) { e.DataObject.SetData(BamaDataFormat, str2); } } }
private static void DecreaseIndentation(TextSelection thisSelection, ListItem parentListItem, ListItem immediateListItem) { if (immediateListItem != null) { TextRangeEditLists.UnindentListItems(thisSelection); } else if (parentListItem != null) { TextRangeEditLists.ConvertParagraphsToListItems(thisSelection, TextMarkerStyle.Disc); TextRangeEditLists.UnindentListItems(thisSelection); } else { if (thisSelection.IsEmpty) { // When selection is empty, handle indentation based on current TextIndent property of the paragraph. Block paragraphOrBlockUIContainer = thisSelection.Start.ParagraphOrBlockUIContainer; if (paragraphOrBlockUIContainer is BlockUIContainer) { // Decrement BlockUIContainer's leading margin. TextRangeEdit.IncrementParagraphLeadingMargin(thisSelection, /*increment:*/20, PropertyValueAction.DecreaseByAbsoluteValue); } else { // Create implicit paragraph if at a potential paragraph position, such as empty FlowDocument, TableCell. CreateImplicitParagraphIfNeededAndUpdateSelection(thisSelection); Paragraph paragraph = thisSelection.Start.Paragraph; Invariant.Assert(paragraph != null, "EnsureInsertionPosition must guarantee a position in text content"); // When selection is empty, handle indentation based on current TextIndent property of the paragraph. if (paragraph.TextIndent > 20) { // Reset text indent to 20. TextRangeEdit.SetParagraphProperty(thisSelection.Start, thisSelection.End, Paragraph.TextIndentProperty, 20.0, PropertyValueAction.SetValue); } else if (paragraph.TextIndent > 0) { // Reset text indent to 0. TextRangeEdit.SetParagraphProperty(thisSelection.Start, thisSelection.End, Paragraph.TextIndentProperty, 0.0, PropertyValueAction.SetValue); } else { // Decrement paragraph leading margin. TextRangeEdit.IncrementParagraphLeadingMargin(thisSelection, /*increment:*/20, PropertyValueAction.DecreaseByAbsoluteValue); } } } else { // For non-empty selection, always decrement paragraph margin. TextRangeEdit.IncrementParagraphLeadingMargin(thisSelection, /*increment:*/20, PropertyValueAction.DecreaseByAbsoluteValue); } } }
private static void ToggleNumbering(TextSelection thisSelection, ListItem parentListItem, ListItem immediateListItem, List list) { if (immediateListItem != null && HasNumericMarker(list)) { if (list.Parent is ListItem) { TextRangeEditLists.UnindentListItems(thisSelection); TextRangeEditLists.ConvertListItemsToParagraphs(thisSelection); } else { TextRangeEditLists.UnindentListItems(thisSelection); } } else if (immediateListItem != null) { list.MarkerStyle = TextMarkerStyle.Decimal; } else if (parentListItem != null) { TextRangeEditLists.ConvertParagraphsToListItems(thisSelection, TextMarkerStyle.Decimal); TextRangeEditLists.IndentListItems(thisSelection); } else { TextRangeEditLists.ConvertParagraphsToListItems(thisSelection, TextMarkerStyle.Decimal); } }
protected override void OnSelectionChanged(RoutedEventArgs e) { base.OnSelectionChanged(e); BindableTextSelection = Selection; }
// Returns a new movingPosition at the first insertion position of the next Table row. // Will return null when end-of-doc is encountered. // This method is only called when the selection's anchor position is not // within a Table. internal static TextPointer GetNextRowStartMovingPosition(TextSelection selection, LogicalDirection direction) { // We only support table selection in TextContainers. Invariant.Assert(((ITextSelection)selection).Start is TextPointer); // This method is called when the selection anchor is outside the scope of // a Table and the selection moving position is within the scope of a Table. Invariant.Assert(!selection.IsTableCellRange); TableCell cell = GetTableCellFromPosition(selection.MovingPosition); Invariant.Assert(cell != null); TableRow row = cell.Row; return (direction == LogicalDirection.Forward) ? row.ContentEnd.GetNextInsertionPosition(LogicalDirection.Forward) : row.ContentStart.GetNextInsertionPosition(LogicalDirection.Backward); }
// Returns true iff selection.MovingPosition is within the scope of a TableCell // and selection.AnchorPosition is not scoped by the same TableCell. internal static bool MovingPositionCrossesCellBoundary(TextSelection selection) { // We only support table selection in TextContainers. Invariant.Assert(((ITextSelection)selection).Start is TextPointer); TableCell cell = GetTableCellFromPosition(selection.MovingPosition); return (cell == null) ? false : !cell.Contains(selection.AnchorPosition); }
// Returns a new movingPosition at the next Table row end. // Will return null when end-of-doc is encountered. // This method is only called when the selection's anchor position is not // within a Table. internal static TextPointer GetNextRowEndMovingPosition(TextSelection selection, LogicalDirection direction) { // This method is called when the selection anchor is outside the scope of // a Table and the selection moving position is within the scope of a Table. Invariant.Assert(!selection.IsTableCellRange); Invariant.Assert(TextPointerBase.IsAtRowEnd(selection.MovingPosition)); TableRow row = (TableRow)selection.MovingPosition.Parent; return (direction == LogicalDirection.Forward) ? row.ContentEnd.GetNextInsertionPosition(LogicalDirection.Forward) : row.ContentStart.GetNextInsertionPosition(LogicalDirection.Backward); }
public void AddPlot(TextSelection currentSelection) { AddBlock(new PlotBlock(), currentSelection); }
public void AddFormula(TextSelection selection) { AddBlock(new FormulaBlock {Formula = "f(x)"}, selection); }
public void AddImage(TextSelection currentSelection, ImageSource source) { AddBlock(new ImageBlock { Source = source }, currentSelection); }
//listo bool swEstáEnNegrita(TextSelection texto) { if ((FontWeight)texto.GetPropertyValue(Run.FontWeightProperty) == FontWeights.Bold) return true; else return false; }
public void AddOrderedList(TextSelection selection) { AddBlock(new List {ListItems = {new ListItem()}, MarkerStyle = TextMarkerStyle.Decimal}, selection); }
/// A handler for an event reporting that the drag over during drag-and-drop operation. internal void TargetOnDragOver(DragEventArgs e) { if (!AllowDragDrop(e)) { return; } // Ok, there's data to move or copy here. if ((e.AllowedEffects & DragDropEffects.Move) != 0) { e.Effects = DragDropEffects.Move; } bool ctrlKeyDown = ((int)(e.KeyStates & DragDropKeyStates.ControlKey) != 0); if (ctrlKeyDown) { e.Effects |= DragDropEffects.Copy; } // Show the caret on the drag over target position. if (_caretDragDrop != null) { // Update the layout to get the corrected text position. Otherwise, we can get the // incorrected text position. if (!_textEditor.TextView.Validate(e.GetPosition(_textEditor.TextView.RenderScope))) { return; } // Find the scroller from the render scope FrameworkElement scroller = _textEditor._Scroller; // Automatically scroll the dropable content(line or page up/down) if scroller is available if (scroller != null) { // Get the ScrollInfo to scroll a line or page up/down IScrollInfo scrollInfo = scroller as IScrollInfo; if (scrollInfo == null && scroller is ScrollViewer) { scrollInfo = ((ScrollViewer)scroller).ScrollInfo; } Invariant.Assert(scrollInfo != null); // Takes care of scrolling mechanism when vertical scrollbar is available, it creates a virtual // block within the viewport where if you position your mouse during drag leads to scrolling,here // it is of 16pixels and within first 8pixels it does scrolling by line and for next it scrolls by page. Point pointScroller = e.GetPosition((IInputElement)scroller); double pageHeight = (double)_textEditor.UiScope.GetValue(TextEditor.PageHeightProperty); double slowAreaHeight = ScrollViewer._scrollLineDelta; if (pointScroller.Y < slowAreaHeight) { // Drag position is on the scroll area that we need to scroll up if (pointScroller.Y > slowAreaHeight / 2) { // scroll a line up scrollInfo.LineUp(); } else { // scroll a page up scrollInfo.PageUp(); } } else if (pointScroller.Y > (pageHeight - slowAreaHeight)) { // Drag position is on the scroll area that we need to scroll down if (pointScroller.Y < (pageHeight - slowAreaHeight / 2)) { // scroll a line down scrollInfo.LineDown(); } else { // scroll a page down scrollInfo.PageDown(); } } } // Get the current text position from the dropable mouse point. _textEditor.TextView.RenderScope.UpdateLayout(); // REVIEW:benwest:6/27/2006: This should use TextView.Validate, and check the return value instead of using IsValid below. if (_textEditor.TextView.IsValid) { ITextPointer dragPosition = GetDropPosition(_textEditor.TextView.RenderScope as Visual, e.GetPosition(_textEditor.TextView.RenderScope)); if (dragPosition != null) { // Get the caret position to show the dropable point. Rect caretRectangle = this.TextView.GetRectangleFromTextPosition(dragPosition); // NOTE: We DO NOT use GetCurrentValue because springload formatting should NOT be involved for drop caret. object fontStylePropertyValue = dragPosition.GetValue(TextElement.FontStyleProperty); bool italic = (_textEditor.AcceptsRichContent && fontStylePropertyValue != DependencyProperty.UnsetValue && (FontStyle)fontStylePropertyValue == FontStyles.Italic); Brush caretBrush = TextSelection.GetCaretBrush(_textEditor); // Show the caret on the dropable position. _caretDragDrop.Update(/*visible:*/ true, caretRectangle, caretBrush, 0.5, italic, CaretScrollMethod.None, /*wordWrappingPosition*/ double.NaN); } } } }
private void GetTextProperties(TextSelection ts) { var size = ts.GetPropertyValue(Inline.FontSizeProperty); if (size != DependencyProperty.UnsetValue) { fontsize.Text = size.ToString(); } var font = ts.GetPropertyValue(Inline.FontFamilyProperty); if (font != DependencyProperty.UnsetValue) { Font.SelectedIndex = Font.Items.IndexOf(font); } var bold = ts.GetPropertyValue(Inline.FontWeightProperty); if (bold != DependencyProperty.UnsetValue) { if ((FontWeight)bold == FontWeights.Bold) { btnBold.IsChecked = true; } else { btnBold.IsChecked = false; } } var td = ts.GetPropertyValue(Inline.TextDecorationsProperty); if (td != DependencyProperty.UnsetValue && td is TextDecorationCollection) { foreach (TextDecoration tdec in (TextDecorationCollection)td) { if (tdec == TextDecorations.Underline[0]) btnUnderLine.IsChecked = true; else btnUnderLine.IsChecked = false ; } } else btnUnderLine.IsChecked = false; var italic = ts.GetPropertyValue(Inline.FontStyleProperty); if (italic != DependencyProperty.UnsetValue) { if ((FontStyle)italic == FontStyles.Italic) { btnItalic.IsChecked = true; } else { btnItalic.IsChecked = false; } } var colorValue = ts.GetPropertyValue(Inline.ForegroundProperty); if (colorValue != DependencyProperty.UnsetValue) { SolidColorBrush b = (SolidColorBrush)colorValue; color.SelectedColor = b.Color; } }
private static void CreateImplicitParagraphIfNeededAndUpdateSelection(TextSelection thisSelection) { // Create implicit paragraph if we are at a potential paragraph position, such as empty FlowDocument, TableCell. TextPointer position = thisSelection.Start; if (TextPointerBase.IsAtPotentialParagraphPosition(position)) { position = TextRangeEditTables.EnsureInsertionPosition(position); thisSelection.Select(position, position); } }
// Finds new movingPosition for the selection when it is in TableCellRange state. // Returns null if there is no next insertion position in the requested direction. internal static TextPointer GetNextTableCellRangeInsertionPosition(TextSelection selection, LogicalDirection direction) { Invariant.Assert(selection.IsTableCellRange, "TextSelection call this method only if selection is in TableCellRange state"); TextPointer movingPosition = selection.MovingPosition; // Table range could disappear if some content change happened after last range building; // so try building again TableCell anchorCell; TableCell movingCell; if (TextRangeEditTables.IsTableCellRange(selection.AnchorPosition, (TextPointer)movingPosition, /*includeCellAtMovingPosition:*/false, out anchorCell, out movingCell)) { // anchorCell is a corner cell of a table where selection has been started. // movingCell is a diagonally oppoosite cell from which we need to find next movingPosition. // Note that movingCell is a cell *included* into selection (not the "next position after the last selected cell"). Invariant.Assert(anchorCell != null && movingCell != null, "anchorCell != null && movingCell != null"); Invariant.Assert(anchorCell.Row.RowGroup == movingCell.Row.RowGroup, "anchorCell.Row.RowGroup == movingCell.Row.RowGroup"); if (direction == LogicalDirection.Backward && movingCell == anchorCell) { // This is a case when selection returns back to acnhor cell from the next cell movingPosition = anchorCell.ContentEnd.GetInsertionPosition(); } else if (direction == LogicalDirection.Forward && (movingCell.Row == anchorCell.Row && movingCell.Index + 1 == anchorCell.Index || anchorCell.Index == 0 && movingCell.Index == movingCell.Row.Cells.Count - 1 && movingCell.Row.Index + 1 == anchorCell.Row.Index)) { // This is a case when selection returns back to acnhor cell from the previous cell movingPosition = anchorCell.ContentStart.GetInsertionPosition(); } else { // Find out what should be new movingCell after selection extension in requested direction TableRow row = movingCell.Row; TableCellCollection cells = row.Cells; TableRowCollection rows = row.RowGroup.Rows; if (direction == LogicalDirection.Forward) { if (movingCell.Index + 1 < cells.Count) { // There is at least one cell in this direction; take it as a movingCell movingCell = cells[movingCell.Index + 1]; } else { // Select first cell in the first following nonempty row int rowIndex = row.Index + 1; // Skip empty rows while (rowIndex < rows.Count && rows[rowIndex].Cells.Count == 0) { rowIndex++; } if (rowIndex < rows.Count) { movingCell = rows[rowIndex].Cells[0]; } else { movingCell = null; } } } else // extending in LogicalDirection.Backward { if (movingCell.Index > 0) { movingCell = cells[movingCell.Index - 1]; } else { // Select the last cell in the first preceding nonempty row int rowIndex = row.Index - 1; // Skip empty rows while (rowIndex >= 0 && rows[rowIndex].Cells.Count == 0) { rowIndex--; } if (rowIndex >= 0) { movingCell = rows[rowIndex].Cells[rows[rowIndex].Cells.Count - 1]; } else { movingCell = null; } } } // Calculate movingPosition that would represent this movingCell if (movingCell != null) { if (movingCell.ColumnIndex >= anchorCell.ColumnIndex) { movingPosition = movingCell.ContentEnd.GetInsertionPosition().GetNextInsertionPosition(LogicalDirection.Forward); } else { movingPosition = movingCell.ContentStart.GetInsertionPosition(); } } else { // We have reached a table boundary if (direction == LogicalDirection.Forward) { movingPosition = anchorCell.Table.ContentEnd; } else { movingPosition = anchorCell.Table.ContentStart; } movingPosition = movingPosition.GetNextInsertionPosition(direction); } } } return movingPosition; }
public void AddUnorderedList(TextSelection selection) { AddBlock(new List {ListItems = {new ListItem()}}, selection); }
public List<TableCell> GetSelectedCells(TextSelection selection, out Table t) { t = TryFindParent<Table>(rtb.Selection.Start.Parent as DependencyObject); List<TableCell> selectedCells = new List<TableCell>(); if (t == null) return selectedCells; minc = 0; maxc = int.MaxValue; minr = 0; maxr = int.MaxValue; TableCell find = GetTableCell(rtb.Selection.Start); if (find != null) { TableRow findr = (find.Parent as TableRow); minc = findr.Cells.IndexOf(find); minr = (findr.Parent as TableRowGroup).Rows.IndexOf(findr); } find = GetTableCell(rtb.Selection.End); if (find != null) { maxc = (find.Parent as TableRow).Cells.IndexOf(find); int ssc = rtb.Selection.End.CompareTo(find.ContentStart); if (ssc == 1 && maxc > 0) { maxc = maxc - 1; } } foreach (TableRowGroup rg in t.RowGroups) { foreach (TableRow tr in rg.Rows) { for (int i = 0; i < tr.Cells.Count; i++) { TableCell tc = tr.Cells[i]; if (i >= minc && i <= maxc) { int ss = rtb.Selection.Start.CompareTo(tc.ContentStart); int se = rtb.Selection.Start.CompareTo(tc.ContentEnd); int es = rtb.Selection.End.CompareTo(tc.ContentStart); int ee = rtb.Selection.End.CompareTo(tc.ContentEnd); if ( (ss >= 0 && se <= 0) || (es > 0 && ee < 0) || (ss < 0 && ee > 0) ) { selectedCells.Add(tc); } } } } } return selectedCells; }
public void AddDrawing(TextSelection currentSelection) { AddBlock(new DrawerBlock(), currentSelection); }