} //CanConvertFrom // Performs the conversion from string to a HtmlFontProperty (only) 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 // Performs the conversion from HtmlFontProperty to a string (only) 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 // compares two Html Fonts for equality // equality opertors not defined (Design Time issue with override of Equals) 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
// Performs the conversion from string to a HtmlFontProperty (only) 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); } }
// based on a font name being null the font can be assumed to be null // default constructor will give a null object public static bool IsNull(HtmlFontProperty font) { return (font.Name == null || font.Name.Trim() == string.Empty); }
// based on a font name being null the font can be assumed to be null // default constructor will give a null object public static bool IsNotNull(HtmlFontProperty font) { return (!HtmlFontProperty.IsNull(font)); }
// compares two Html Fonts for equality // equality opertors not defined (Design Time issue with override of Equals) public static bool IsNotEqual(HtmlFontProperty font1, HtmlFontProperty font2) { return (!HtmlFontProperty.IsEqual(font1, font2)); }
// compares two Html Fonts for equality // equality opertors not defined (Design Time issue with override of Equals) 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; }
} //IsNull // based on a font name being null the font can be assumed to be null // default constructor will give a null object public static bool IsNotNull(HtmlFontProperty font) { return(!HtmlFontProperty.IsNull(font)); } //IsNull
} //IsNotEqual // based on a font name being null the font can be assumed to be null // default constructor will give a null object public static bool IsNull(HtmlFontProperty font) { return(font.Name == null || font.Name.Trim() == string.Empty); } //IsNull
} //IsEquals // compares two Html Fonts for equality // equality opertors not defined (Design Time issue with override of Equals) public static bool IsNotEqual(HtmlFontProperty font1, HtmlFontProperty font2) { return(!HtmlFontProperty.IsEqual(font1, font2)); } //IsNotEqual
public HtmlEditorControl() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // define the context menu for format commands DefineFormatBlockMenu(); // define the default values // browser constants and commands EMPTY_PARAMETER = System.Reflection.Missing.Value; // default values used to reset values _defaultBackColor = Color.White; _defaultForeColor = Color.Black; _defaultFont = new HtmlFontProperty(this.Font); // set browser default values to hide IE items this.editorWebBrowser.AddressBar = false; this.editorWebBrowser.MenuBar = false; this.editorWebBrowser.StatusBar = false; // obtain the underlying web browser and set options // SHDocVw.WebBrowser webBrowser = (SHDocVw.WebBrowser)this.editorWebBrowser.GetOcx(); // define the default values of the properties _readOnly = false; _toolbarVisible = false; _enableVisualStyles = false; _toolbarDock = DockStyle.Bottom; _bodyText = DEFAULT_HTML_TEXT; _bodyHtml = DEFAULT_HTML_TEXT; _bodyBackColor = _defaultBackColor; _bodyForeColor = _defaultForeColor; _bodyFont = _defaultFont; _scrollBars = DisplayScrollBarOption.Auto; _imageDirectory = string.Empty; _htmlDirectory = string.Empty; _navigateWindow = NavigateActionOption.Default; _baseHref = string.Empty; _autoWordWrap = true; stylesheet = null; script = null; // define context menu state this.menuDocumentToolbar.Checked = true; this.menuDocumentScrollbar.Checked = true; this.menuDocumentWordwrap.Checked = true; // load the blank Html page to load the MsHtml object model BrowserCodeNavigate(BLANK_HTML_PAGE); // after load ensure document marked as editable this.ReadOnly = _readOnly; }
// once an html docuemnt has been loaded define the internal values private void DefineBodyAttributes() { // define the body colors based on the new body html if (body.bgColor == null) { _bodyBackColor = _defaultBackColor; } else { _bodyBackColor = ColorTranslator.FromHtml((string)body.bgColor); } if (body.text == null) { _bodyForeColor = _defaultForeColor; } else { _bodyForeColor = 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 HtmlStyle bodyStyle = body.style; if (bodyStyle != null) { string fontName = _bodyFont.Name; HtmlFontSize fontSize = _bodyFont.Size; bool fontBold = _bodyFont.Bold; bool fontItalic = _bodyFont.Italic; bool fontUnderline = _bodyFont.Underline; // define the font name if defined in the style if (bodyStyle.fontFamily != null) fontName = bodyStyle.fontFamily; if (bodyStyle.fontSize != null) fontSize = HtmlFontConversion.StyleSizeToHtml(bodyStyle.fontSize.ToString()); if (bodyStyle.fontWeight != null) fontBold = HtmlFontConversion.IsStyleBold(bodyStyle.fontWeight); if (bodyStyle.fontStyle != null) fontItalic = HtmlFontConversion.IsStyleItalic(bodyStyle.fontStyle); fontUnderline = bodyStyle.textDecorationUnderline; // define the new font object and set the property _bodyFont = new HtmlFontProperty(fontName, fontSize, fontBold, fontItalic, fontUnderline); this.BodyFont = _bodyFont; } // define the content based on the current value this.ReadOnly = _readOnly; this.ScrollBars = _scrollBars; this.AutoWordWrap = _autoWordWrap; }
// using the exec command define font properties for the selected text public void FormatFontAttributes(HtmlFontProperty font) { // obtain the selected range object HtmlTextRange range = GetTextRange(); if (range != null && HtmlFontProperty.IsNotNull(font)) { // Use the FONT object to set the properties // FontName, FontSize, Bold, Italic, Underline, Strikeout ExecuteCommandRange(range, HTML_COMMAND_FONT_NAME, font.Name); // set the font size provide set to a value if (font.Size != HtmlFontSize.Default) ExecuteCommandRange(range, HTML_COMMAND_FONT_SIZE, (int)font.Size); // determine the BOLD property and correct as neccessary object currentBold = QueryCommandRange(range, HTML_COMMAND_BOLD); bool fontBold = (currentBold is System.Boolean)? fontBold = (bool)currentBold : false; if (font.Bold != fontBold) ExecuteCommandRange(HTML_COMMAND_BOLD, null); // determine the UNDERLINE property and correct as neccessary object currentUnderline = QueryCommandRange(range, HTML_COMMAND_UNDERLINE); bool fontUnderline = (currentUnderline is System.Boolean) ? fontUnderline = (bool)currentUnderline : false; if (font.Underline != fontUnderline) ExecuteCommandRange(HTML_COMMAND_UNDERLINE, null); // determine the ITALIC property and correct as neccessary object currentItalic = QueryCommandRange(range, HTML_COMMAND_ITALIC); bool fontItalic = (currentItalic is System.Boolean) ? fontItalic = (bool)currentItalic : false; if (font.Italic != fontItalic) ExecuteCommandRange(HTML_COMMAND_ITALIC, null); // determine the STRIKEOUT property and correct as neccessary object currentStrikeout = QueryCommandRange(range, HTML_COMMAND_STRIKE_THROUGH); bool fontStrikeout = (currentStrikeout is System.Boolean) ? fontStrikeout = (bool)currentStrikeout : false; if (font.Strikeout != fontStrikeout) ExecuteCommandRange(HTML_COMMAND_STRIKE_THROUGH, null); // determine the SUPERSCRIPT property and correct as neccessary object currentSuperscript = QueryCommandRange(range, HTML_COMMAND_SUPERSCRIPT); bool fontSuperscript = (currentSuperscript is System.Boolean) ? fontSuperscript = (bool)currentSuperscript : false; if (font.Superscript != fontSuperscript) ExecuteCommandRange(HTML_COMMAND_SUPERSCRIPT, null); // determine the SUBSCRIPT property and correct as neccessary object currentSubscript = QueryCommandRange(range, HTML_COMMAND_SUBSCRIPT); bool fontSubscript = (currentSubscript is System.Boolean) ? fontSubscript = (bool)currentSubscript : false; if (font.Subscript != fontSubscript) ExecuteCommandRange(HTML_COMMAND_SUBSCRIPT, null); } else { // do not have text selected or a valid font class throw new HtmlEditorException("Invalid Text selection made or Invalid Font selection.", "FormatFont"); } }