Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.ObjectModel
Imports System.Windows
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Documents
Imports System.Collections.Generic
Imports System.Windows.Printing
Imports DevExpress.Utils.Internal
Namespace DemoUtils
Public Class DemoRichControl
Inherits System.Windows.Controls.RichTextBox
Public Sub New()
End Sub
Public Overloads Property AcceptsTab() As Boolean
Get
Return True
End Get
Set(ByVal value As Boolean)
End Set
End Property
Public ReadOnly Overloads Property CanUndo() As Boolean
Get
Return False
End Get
End Property
Public ReadOnly Overloads Property CanRedo() As Boolean
Get
Return False
End Get
End Property
Public Property TextIsBold() As Boolean
Get
Return IsTextBold()
End Get
Set(ByVal value As Boolean)
ToggleTextFormatBold(value)
End Set
End Property
Public Property TextIsItalic() As Boolean
Get
Return IsTextItalic()
End Get
Set(ByVal value As Boolean)
ToggleTextFormatItalic(value)
End Set
End Property
Public Property TextIsUnderline() As Boolean
Get
Return IsTextUnderline()
End Get
Set(ByVal value As Boolean)
ToggleTextFormatUnderline(value)
End Set
End Property
Public Property Text() As String
Get
Return Selection.Text
End Get
Set(ByVal value As String)
Selection.Text = value
End Set
End Property
Public Property TextFontFamily() As Object
Get
Dim value As Object = Selection.GetPropertyValue(Run.FontFamilyProperty)
Return If((value Is DependencyProperty.UnsetValue), Nothing, value)
End Get
Set(ByVal value As Object)
If value Is Nothing OrElse value Is TextFontFamily Then
Return
End If
Try
If TypeOf value Is String Then
Selection.ApplyPropertyValue(Run.FontFamilyProperty, New FontFamily(TryCast(value, String)))
Else
Selection.ApplyPropertyValue(Run.FontFamilyProperty, value)
End If
Catch
End Try
End Set
End Property
Public Property TextFontSize() As Object
Get
Dim value As Object = Selection.GetPropertyValue(Run.FontSizeProperty)
If value Is DependencyProperty.UnsetValue Then
Return Nothing
End If
Return value
End Get
Set(ByVal value As Object)
If value Is Nothing OrElse value.Equals(TextFontSize) Then
Return
End If
Selection.ApplyPropertyValue(Run.FontSizeProperty, Convert.ToDouble(value))
End Set
End Property
Public Property TextColor() As Color
Set(ByVal value As Color)
If value = TextColor Then
Return
End If
Selection.ApplyPropertyValue(Run.ForegroundProperty, New SolidColorBrush(value))
End Set
Get
Dim brush As SolidColorBrush = TryCast(Selection.GetPropertyValue(Run.ForegroundProperty), SolidColorBrush)
If brush Is Nothing Then
Return Colors.Black
End If
Return brush.Color
End Get
End Property
Public Sub SetTextColor(ByVal value As Color)
Selection.ApplyPropertyValue(Run.ForegroundProperty, New SolidColorBrush(value))
End Sub
Private privateTextBackgroundColor As Color
Public Property TextBackgroundColor() As Color
Get
Return privateTextBackgroundColor
End Get
Set(ByVal value As Color)
privateTextBackgroundColor = value
End Set
End Property
Public Sub SetTextBackgroundColor(ByVal value As Color)
End Sub
Public Function GetTextAlignment() As TextAlignment
Dim value As Object = Selection.GetPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty)
If value Is DependencyProperty.UnsetValue Then
Return TextAlignment.Left
End If
If CType(value, TextAlignment) = TextAlignment.Center Then
Return TextAlignment.Center
ElseIf CType(value, TextAlignment) = TextAlignment.Right Then
Return TextAlignment.Right
Else
Return TextAlignment.Left
End If
End Function
Public Sub ToggleTextAlignmentLeft()
Selection.ApplyPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty, TextAlignment.Left)
End Sub
Public Sub ToggleTextAlignmentCenter()
Selection.ApplyPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty, TextAlignment.Center)
End Sub
Public Sub ToggleTextAlignmentRight()
Selection.ApplyPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty, TextAlignment.Right)
End Sub
Public Sub ToggleTextAlignmentJustify()
Selection.ApplyPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty, TextAlignment.Justify)
End Sub
Public Sub Clear()
Blocks.Clear()
End Sub
Public Sub Print()
Dim p As New PrintDocument()
AddHandler p.PrintPage, AddressOf PrintPage
p.Print(String.Empty)
End Sub
Public Overloads Sub Undo()
End Sub
Public Overloads Sub Redo()
End Sub
Public Overloads Sub Cut()
Clipboard.SetText(Selection.Text)
Selection.Text = String.Empty
End Sub
Public Overloads Sub Copy()
Clipboard.SetText(Selection.Text)
End Sub
Public Overloads Sub Paste()
Selection.Text = Clipboard.GetText()
End Sub
Protected Overridable Sub PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
CType(e, PrintPageEventArgs).PageVisual = Me
End Sub
Public ReadOnly Property IsEmpty() As Boolean
Get
For Each b As Block In Blocks
If Not(TypeOf b Is Paragraph) Then
Return False
End If
For Each o As Object In (CType(b, Paragraph)).Inlines
If Not(TypeOf o Is Run) Then
Return False
End If
Dim r As Run = TryCast(o, Run)
If (Not String.IsNullOrEmpty(r.Text)) Then
Return False
End If
Next o
Next b
Return True
End Get
End Property
Public ReadOnly Property IsSelectionEmpty() As Boolean
Get
Return Selection.Text.Length = 0
End Get
End Property
Protected Function IsTextBold() As Boolean
Dim value As Object = Selection.GetPropertyValue(TextElement.FontWeightProperty)
Return If((value Is DependencyProperty.UnsetValue), False, (CType(value, FontWeight) = FontWeights.Bold))
End Function
Protected Function IsTextItalic() As Boolean
Dim value As Object = Selection.GetPropertyValue(Run.FontStyleProperty)
Return If((value Is DependencyProperty.UnsetValue), False, ((CType(value, System.Windows.FontStyle)) = FontStyles.Italic))
End Function
Protected Function IsTextUnderline() As Boolean
Dim value As Object = Selection.GetPropertyValue(Inline.TextDecorationsProperty)
Return If((value Is DependencyProperty.UnsetValue), False, value IsNot Nothing AndAlso System.Windows.TextDecorations.Underline.Equals(value))
End Function
Protected Sub ToggleTextFormatBold(ByVal bold As Boolean)
If bold = IsTextBold() Then
Return
End If
If (Not bold) Then
Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal)
Else
Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold)
End If
End Sub
Protected Sub ToggleTextFormatItalic(ByVal italic As Boolean)
If italic = IsTextItalic() Then
Return
End If
If (Not italic) Then
Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal)
Else
Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic)
End If
End Sub
Protected Sub ToggleTextFormatUnderline(ByVal underline As Boolean)
If underline = IsTextUnderline() Then
Return
End If
If (Not underline) Then
Selection.ApplyPropertyValue(Run.TextDecorationsProperty, Nothing)
Else
Selection.ApplyPropertyValue(Run.TextDecorationsProperty, System.Windows.TextDecorations.Underline)
End If
End Sub
Public Function GetUIElementUnderSelection(Of T As Class)(ByVal blocks As BlockCollection) As T
For Each block As Block In blocks
Dim ph As Paragraph = TryCast(block, Paragraph)
If ph IsNot Nothing Then
For Each obj As Object In ph.Inlines
If TypeOf obj Is Run Then
Continue For
End If
Dim cont As InlineUIContainer = TryCast(obj, InlineUIContainer)
If cont IsNot Nothing AndAlso cont.ContentStart.CompareTo(Selection.Start) > 0 AndAlso cont.ContentStart.CompareTo(Selection.End) < 0 Then
If TypeOf cont.Child Is T Then
Return TryCast(cont.Child, T)
End If
End If
Next obj
End If
Next block
Return Nothing
End Function
Public Property ListMarkerStyle() As TextMarkerStyle
Get
Return TextMarkerStyle.None
End Get
Set(ByVal value As TextMarkerStyle)
End Set
End Property
Public Function GetUIElementUnderSelection(Of T As Class)() As T
Dim col As BlockCollection = Blocks
If Selection.Start.GetNextInsertionPosition(LogicalDirection.Forward) Is Nothing OrElse Selection.Start.GetNextInsertionPosition(LogicalDirection.Forward).CompareTo(Selection.End) <> 0 Then
Return Nothing
End If
Return GetUIElementUnderSelection(Of T)(col)
End Function
End Class
Public Enum TextMarkerStyle
None = 0
Disc = 1
Circle = 2
Square = 3
Box = 4
LowerRoman = 5
UpperRoman = 6
LowerLatin = 7
UpperLatin = 8
[Decimal] = 9
End Enum
End Namespace