Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/Silverlight/CS/BarsDemo/Controls/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/Silverlight/CS/BarsDemo/Controls/DemoRichTextBox.cs

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Documents;
using System.Collections.Generic;
using System.Windows.Printing;
using DevExpress.Utils.Internal;

namespace DemoUtils {
    public class DemoRichControl : System.Windows.Controls.RichTextBox {
        public DemoRichControl() {
        }
        public bool AcceptsTab { get { return true; } set { } }
        public bool CanUndo { get { return false; } }
        public bool CanRedo { get { return false; } }
        public bool TextIsBold {
            get { return IsTextBold(); }
            set { ToggleTextFormatBold(value); }
        }
        public bool TextIsItalic {
            get { return IsTextItalic(); }
            set { ToggleTextFormatItalic(value); }
        }
        public bool TextIsUnderline {
            get { return IsTextUnderline(); }
            set { ToggleTextFormatUnderline(value); }
        }
        public string Text {
            get { return Selection.Text; }
            set { Selection.Text = value; }
        }
        public object TextFontFamily {
            get {
                object value = Selection.GetPropertyValue(Run.FontFamilyProperty);
                return (value == DependencyProperty.UnsetValue) ? null : value;
            }
            set {
                if(value == null || value == TextFontFamily) return;
                try {
                    if(value is string)
                        Selection.ApplyPropertyValue(Run.FontFamilyProperty, new FontFamily(value as string));
                    else
                        Selection.ApplyPropertyValue(Run.FontFamilyProperty, value);
                } catch { }
            }
        }
        public object TextFontSize {
            get {
                object value = Selection.GetPropertyValue(Run.FontSizeProperty);
                if(value == DependencyProperty.UnsetValue)
                    return null;
                return value;
            }
            set {
                if(value == null || value.Equals(TextFontSize))
                    return;

                Selection.ApplyPropertyValue(Run.FontSizeProperty, Convert.ToDouble(value));
            }
        }
        public Color TextColor {
            set {
                if(value == TextColor)
                    return;
                Selection.ApplyPropertyValue(Run.ForegroundProperty, new SolidColorBrush(value));
            }
            get {
                SolidColorBrush brush = Selection.GetPropertyValue(Run.ForegroundProperty) as SolidColorBrush;
                if(brush == null)
                    return Colors.Black;
                return brush.Color;
            }
        }
        public void SetTextColor(Color value) {
            Selection.ApplyPropertyValue(Run.ForegroundProperty, new SolidColorBrush(value));
        }
        public Color TextBackgroundColor { get; set; }
        public void SetTextBackgroundColor(Color value) { }
        public TextAlignment GetTextAlignment() {
            object value = Selection.GetPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty);
            if(value == DependencyProperty.UnsetValue) return TextAlignment.Left;
            if((TextAlignment)value == TextAlignment.Center) return TextAlignment.Center;
            else if((TextAlignment)value == TextAlignment.Right) return TextAlignment.Right;
            else return TextAlignment.Left;
        }
        public void ToggleTextAlignmentLeft() {
            Selection.ApplyPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty, TextAlignment.Left);
        }
        public void ToggleTextAlignmentCenter() {
            Selection.ApplyPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty, TextAlignment.Center);
        }
        public void ToggleTextAlignmentRight() {
            Selection.ApplyPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty, TextAlignment.Right);
        }
        public void ToggleTextAlignmentJustify() {
            Selection.ApplyPropertyValue(System.Windows.Documents.Paragraph.TextAlignmentProperty, TextAlignment.Justify);
        }
        public void Clear() {
            Blocks.Clear();
        }
        public void Print() {
            PrintDocument p = new PrintDocument();
            p.PrintPage += new EventHandler<PrintPageEventArgs>(PrintPage);
            p.Print(string.Empty);
        }

        public void Undo() { }
        public void Redo() { }
        public void Cut() {
            Clipboard.SetText(Selection.Text);
            Selection.Text = string.Empty;
        }
        public void Copy() {
            Clipboard.SetText(Selection.Text);
        }
        public void Paste() {
            Selection.Text = Clipboard.GetText();
        }

        protected virtual void PrintPage(object sender, PrintPageEventArgs e) {
            ((PrintPageEventArgs)e).PageVisual = this;
        }
        public bool IsEmpty {
            get {
            foreach(Block b in Blocks) {
                    if(!(b is Paragraph))
                        return false;

                    foreach(object o in ((Paragraph)b).Inlines) {
                        if(!(o is Run))
                            return false;
                        Run r = o as Run;
                        if(!string.IsNullOrEmpty(r.Text))
                            return false;
                    }
                }
            return true;
            }
        }
        public bool IsSelectionEmpty {
            get {
                return Selection.Text.Length == 0;
            }
        }
        protected bool IsTextBold() {
            object value = Selection.GetPropertyValue(TextElement.FontWeightProperty);
            return (value == DependencyProperty.UnsetValue) ? false : ((FontWeight)value == FontWeights.Bold);
        }
        protected bool IsTextItalic() {
            object value = Selection.GetPropertyValue(Run.FontStyleProperty);
            return (value == DependencyProperty.UnsetValue) ? false : (((System.Windows.FontStyle)value) == FontStyles.Italic);
        }
        protected bool IsTextUnderline() {
            object value = Selection.GetPropertyValue(Inline.TextDecorationsProperty);
            return (value == DependencyProperty.UnsetValue) ? false : value != null && System.Windows.TextDecorations.Underline.Equals(value);
        }
        protected void ToggleTextFormatBold(bool bold) {
            if(bold == IsTextBold())
                return;
            if (!bold)
                Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Normal);
            else
                Selection.ApplyPropertyValue(Run.FontWeightProperty, FontWeights.Bold);
        }
        protected void ToggleTextFormatItalic(bool italic) {
            if(italic == IsTextItalic())
                return;
            if (!italic)
                Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Normal);
            else
                Selection.ApplyPropertyValue(Run.FontStyleProperty, FontStyles.Italic);
        }
        protected void ToggleTextFormatUnderline(bool underline) {
            if(underline == IsTextUnderline())
                return;
            if (!underline)
                Selection.ApplyPropertyValue(Run.TextDecorationsProperty, null);
            else
                Selection.ApplyPropertyValue(Run.TextDecorationsProperty, System.Windows.TextDecorations.Underline);
        }
        public T GetUIElementUnderSelection<T>(BlockCollection blocks) where T : class {
            foreach(Block block in blocks) {
                Paragraph ph = block as Paragraph;
                if(ph != null) {
                    foreach(object obj in ph.Inlines) {
                        if(obj is Run)
                            continue;
                        InlineUIContainer cont = obj as InlineUIContainer;
                        if(cont != null && cont.ContentStart.CompareTo(Selection.Start) > 0 && cont.ContentStart.CompareTo(Selection.End) < 0) {
                            if(cont.Child is T)
                                return cont.Child as T;
                        }
                    }
                }
            }
            return null;

        }
        public TextMarkerStyle ListMarkerStyle {
            get {
                return TextMarkerStyle.None;
            }
            set {
            }
        }
        public T GetUIElementUnderSelection<T>() where T : class {
            BlockCollection col = Blocks;
            if(Selection.Start.GetNextInsertionPosition(LogicalDirection.Forward) == null ||
                Selection.Start.GetNextInsertionPosition(LogicalDirection.Forward).CompareTo(Selection.End) != 0)
                return null;
            return GetUIElementUnderSelection<T>(col);
        }
    }
    public enum TextMarkerStyle { None = 0, Disc = 1, Circle = 2, Square = 3, Box = 4, LowerRoman = 5, UpperRoman = 6, LowerLatin = 7, UpperLatin = 8, Decimal = 9 }
}