} //CanConvertFrom /// <summary> /// Performs the conversion from string to a HtmlFontProperty (only) /// </summary> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { // define a new font property string fontString = (string)value; HtmlFontProperty font = new HtmlFontProperty(string.Empty);; try { // parse the contents of the given string using a regex string fontName = string.Empty; string fontSize = string.Empty; Regex expression = new Regex(FONT_PARSE_EXPRESSION, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.ExplicitCapture); Match match = expression.Match(fontString); // see if a match was found if (match.Success) { // extract the content type elements fontName = match.Result(FONT_PARSE_NAME); fontSize = match.Result(FONT_PARSE_SIZE); // set the fontname TextInfo text = Thread.CurrentThread.CurrentCulture.TextInfo; font.Name = text.ToTitleCase(fontName); // determine size from given string using Small if blank if (fontSize == string.Empty) { fontSize = "Small"; } font.Size = (HtmlFontSize)Enum.Parse(typeof(HtmlFontSize), fontSize, true); } } catch (Exception) { // do nothing but ensure font is a null font font.Name = string.Empty; } if (HtmlFontProperty.IsNull(font)) { // error performing the string conversion so throw exception given possible format string error = string.Format(@"Cannot convert '{0}' to Type HtmlFontProperty. Format: 'FontName, HtmlSize', Font Size values: {1}", fontString, string.Join(", ", Enum.GetNames(typeof(HtmlFontSize)))); throw new ArgumentException(error); } else { // return the font return(font); } } else { return(base.ConvertFrom(context, culture, value)); } } //ConvertFrom
} //CanConvertTo /// <summary> /// Performs the conversion from HtmlFontProperty to a string (only) /// </summary> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { // ensure working with the intented type HtmlFontProperty if (value is HtmlFontProperty) { HtmlFontProperty font = (HtmlFontProperty)value; if (destinationType == typeof(string)) { return(font.ToString()); } if (destinationType == typeof(InstanceDescriptor)) { // define array to hold the properties and values Object[] properties = new Object[8]; Type[] types = new Type[8]; // Name property properties[0] = font.Name; types[0] = typeof(string); // Size property properties[1] = font.Size; types[1] = typeof(HtmlFontSize); // Bold property properties[2] = font.Bold; types[2] = typeof(bool); // Italic property properties[3] = font.Italic; types[3] = typeof(bool); // Underline property properties[4] = font.Underline; types[4] = typeof(bool); // Strikeout property properties[5] = font.Strikeout; types[5] = typeof(bool); // Subscript property properties[6] = font.Subscript; types[6] = typeof(bool); // Superscript property properties[7] = font.Superscript; types[7] = typeof(bool); // create the instance constructor to return ConstructorInfo ci = typeof(HtmlFontProperty).GetConstructor(types); return(new InstanceDescriptor(ci, properties)); } } // have something other than InstanceDescriptor or sting return(base.ConvertTo(context, culture, value, destinationType)); } //ConvertTo
} //ToString /// <summary> /// Compares two Html Fonts for equality /// Equality opertors not defined (Design Time issue with override of Equals) /// </summary> public static bool IsEqual(HtmlFontProperty font1, HtmlFontProperty font2) { // assume not equal bool equals = false; // perform the comparsion if (HtmlFontProperty.IsNotNull(font1) && HtmlFontProperty.IsNotNull(font2)) { if (font1.Name == font2.Name && font1.Size == font2.Size && font1.Bold == font2.Bold && font1.Italic == font2.Italic && font1.Underline == font2.Underline && font1.Strikeout == font2.Strikeout && font1.Subscript == font2.Subscript && font1.Superscript == font2.Superscript) { equals = true; } } // return the calculated value return(equals); } //IsEquals
} //IsNull /// <summary> /// Based on a font name being null the font can be assumed to be null /// Default constructor will give a null object /// </summary> public static bool IsNotNull(HtmlFontProperty font) { return(!HtmlFontProperty.IsNull(font)); } //IsNull
} //IsNotEqual /// <summary> /// Based on a font name being null the font can be assumed to be null /// Default constructor will give a null object /// </summary> public static bool IsNull(HtmlFontProperty font) { return(font.Name == null || font.Name.Trim() == string.Empty); } //IsNull
} //IsEquals /// <summary> /// Compares two Html Fonts for equality /// Equality opertors not defined (Design Time issue with override of Equals) /// </summary> public static bool IsNotEqual(HtmlFontProperty font1, HtmlFontProperty font2) { return(!HtmlFontProperty.IsEqual(font1, font2)); } //IsNotEqual
} //CanConvertFrom /// <summary> /// Performs the conversion from string to a HtmlFontProperty (only) /// </summary> public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { // define a new font property string fontString = (string)value; HtmlFontProperty font = new HtmlFontProperty(string.Empty);; try { // parse the contents of the given string using a regex string fontName = string.Empty; string fontSize = string.Empty; Regex expression = new Regex(FONT_PARSE_EXPRESSION, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.ExplicitCapture); Match match = expression.Match(fontString); // see if a match was found if (match.Success) { // extract the content type elements fontName = match.Result(FONT_PARSE_NAME); fontSize = match.Result(FONT_PARSE_SIZE); // set the fontname TextInfo text = Thread.CurrentThread.CurrentCulture.TextInfo; font.Name = text.ToTitleCase(fontName); // determine size from given string using Small if blank if (fontSize == string.Empty) fontSize = "Small"; font.Size = (HtmlFontSize)Enum.Parse(typeof(HtmlFontSize), fontSize, true); } } catch (Exception) { // do nothing but ensure font is a null font font.Name = string.Empty; } if (HtmlFontProperty.IsNull(font)) { // error performing the string conversion so throw exception given possible format string error = string.Format(@"Cannot convert '{0}' to Type HtmlFontProperty. Format: 'FontName, HtmlSize', Font Size values: {1}", fontString, string.Join(", ", Enum.GetNames(typeof(HtmlFontSize)))); throw new ArgumentException(error); } else { // return the font return font; } } else { return base.ConvertFrom(context, culture, value); } } //ConvertFrom
} //IsNotEqual /// <summary> /// Based on a font name being null the font can be assumed to be null /// Default constructor will give a null object /// </summary> public static bool IsNull(HtmlFontProperty font) { return (font.Name == null || font.Name.Trim() == string.Empty); } //IsNull
} //IsNull /// <summary> /// Based on a font name being null the font can be assumed to be null /// Default constructor will give a null object /// </summary> public static bool IsNotNull(HtmlFontProperty font) { return (!HtmlFontProperty.IsNull(font)); } //IsNull
} //IsEquals /// <summary> /// Compares two Html Fonts for equality /// Equality opertors not defined (Design Time issue with override of Equals) /// </summary> public static bool IsNotEqual(HtmlFontProperty font1, HtmlFontProperty font2) { return (!HtmlFontProperty.IsEqual(font1, font2)); } //IsNotEqual
} //ToString /// <summary> /// Compares two Html Fonts for equality /// Equality opertors not defined (Design Time issue with override of Equals) /// </summary> public static bool IsEqual(HtmlFontProperty font1, HtmlFontProperty font2) { // assume not equal bool equals = false; // perform the comparsion if (HtmlFontProperty.IsNotNull(font1) && HtmlFontProperty.IsNotNull(font2)) { if (font1.Name == font2.Name && font1.Size == font2.Size && font1.Bold == font2.Bold && font1.Italic == font2.Italic && font1.Underline == font2.Underline && font1.Strikeout == font2.Strikeout && font1.Subscript == font2.Subscript && font1.Superscript == font2.Superscript) { equals = true; } } // return the calculated value return equals; } //IsEquals
} //ShouldSerializeBodyFont public void ResetBodyFont() { this.BodyFont = _defaultFont; } //ResetBodyFont
} //BodyHtml /// <summary> /// Defines all the body attributes once a document has been loaded /// </summary> private void DefineBodyAttributes() { // define the body colors based on the new body html _bodyBackColor = GeneralUtil.IsNull(body.bgColor) ? _defaultBodyBackColor : ColorTranslator.FromHtml((string)body.bgColor); _bodyForeColor = GeneralUtil.IsNull(body.text) ? _defaultBodyForeColor : ColorTranslator.FromHtml((string)body.text); // define the font object based on current font of new document // deafult used unless a style on the body modifies the value mshtmlStyle bodyStyle = body.style; if (!bodyStyle.IsNull()) { string fontName = _bodyFont.Name; HtmlFontSize fontSize = _bodyFont.Size; bool fontBold = _bodyFont.Bold; bool fontItalic = _bodyFont.Italic; // define the font name if defined in the style if (!bodyStyle.fontFamily.IsNull()) fontName = bodyStyle.fontFamily; if (!GeneralUtil.IsNull(bodyStyle.fontSize)) fontSize = HtmlFontConversion.StyleSizeToHtml(bodyStyle.fontSize.ToString()); if (!bodyStyle.fontWeight.IsNull()) fontBold = HtmlFontConversion.IsStyleBold(bodyStyle.fontWeight); if (!bodyStyle.fontStyle.IsNull()) fontItalic = HtmlFontConversion.IsStyleItalic(bodyStyle.fontStyle); bool fontUnderline = bodyStyle.textDecorationUnderline; // define the new font object and set the property _bodyFont = new HtmlFontProperty(fontName, fontSize, fontBold, fontItalic, fontUnderline); BodyFont = _bodyFont; } // define the content based on the current value ReadOnly = _readOnly; ScrollBars = _scrollBars; AutoWordWrap = _autoWordWrap; } //DefineBodyAttributes
public HtmlEditor() { InitializeComponent(); // define the default values // browser constants and commands EMPTY_PARAMETER = System.Reflection.Missing.Value; // default values used to reset values _defaultBodyBackColor = Color.White; _defaultBodyForeColor = Color.Black; _defaultFont = new HtmlFontProperty(Font); _navigateWindow = NavigateActionOption.Default; _scrollBars = DisplayScrollBarOption.Auto; _autoWordWrap = true; _toolbarVisible = true; _statusbarVisible = true; _wbVisible = true; _bodyText = DEFAULT_HTML_TEXT; _bodyHtml = DEFAULT_HTML_TEXT; _bodyBackColor = _defaultBodyBackColor; _bodyForeColor = _defaultBodyForeColor; _bodyFont = _defaultFont; // load the blank Html page to load the MsHtml object model BrowserCodeNavigate(BLANK_HTML_PAGE); // after load ensure document marked as editable ReadOnly = _readOnly; ScrollBars = _scrollBars; SetupComboFontSize(); SynchFont(string.Empty); }