123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Drawing.Text;
- using System.Windows.Forms;
- namespace WinHtmlEditor
- {
- public class ToolStripFontComboBox : ToolStripComboBox
- {
- #region Private Member Declarations
- private readonly Dictionary<string, Font> _fontCache;
- private int _itemHeight;
- private int _previewFontSize;
- private StringFormat _stringFormat;
- #endregion Private Member Declarations
- #region Public Constructors
- public ToolStripFontComboBox()
- {
- _fontCache = new Dictionary<string, Font>();
- DrawMode = DrawMode.OwnerDrawVariable;
- if (!ComboBox.IsNull())
- {
- ComboBox.DrawItem += OnDrawItem;
- ComboBox.MeasureItem += OnMeasureItem;
- }
- Sorted = true;
- PreviewFontSize = 12;
- CalculateLayout();
- CreateStringFormat();
- }
- #endregion Public Constructors
- #region Events
- public event EventHandler PreviewFontSizeChanged;
- #endregion Events
- #region Protected Overridden Methods
- protected override void Dispose(bool disposing)
- {
- ClearFontCache();
- if (!_stringFormat.IsNull())
- _stringFormat.Dispose();
- base.Dispose(disposing);
- }
- protected void OnDrawItem(object sender, DrawItemEventArgs e)
- {
- if (e.Index <= -1 || e.Index >= Items.Count) return;
- e.DrawBackground();
- if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
- e.DrawFocusRectangle();
- using (var textBrush = new SolidBrush(e.ForeColor))
- {
- string fontFamilyName = Items[e.Index].ToString();
- e.Graphics.DrawString(fontFamilyName, GetFont(fontFamilyName), textBrush, e.Bounds, _stringFormat);
- }
- }
- protected override void OnClick(EventArgs e)
- {
- base.OnClick(e);
- _internalCall = false;
- }
- protected override void OnSelectedIndexChanged(EventArgs e)
- {
- base.OnSelectedIndexChanged(e);
- _internalCall = false;
- }
- protected override void OnFontChanged(EventArgs e)
- {
- base.OnFontChanged(e);
- CalculateLayout();
- }
- protected override void OnGotFocus(EventArgs e)
- {
- LoadFontFamilies();
- base.OnGotFocus(e);
- }
- protected void OnMeasureItem(object sender, MeasureItemEventArgs e)
- {
- if (e.Index > -1 && e.Index < Items.Count)
- {
- e.ItemHeight = _itemHeight;
- }
- }
- protected override void OnRightToLeftChanged(EventArgs e)
- {
- base.OnRightToLeftChanged(e);
- CreateStringFormat();
- }
- protected override void OnTextChanged(EventArgs e)
- {
- base.OnTextChanged(e);
- if (Items.Count == 0)
- {
- LoadFontFamilies();
- int selectedIndex = FindStringExact(Text);
- if (selectedIndex != -1)
- SelectedIndex = selectedIndex;
- }
- }
- #endregion Protected Overridden Methods
- #region Public Methods
- public void LoadFontFamilies()
- {
- if (Items.Count == 0)
- {
- Cursor.Current = Cursors.WaitCursor;
- foreach (FontFamily fontFamily in FontFamily.Families)
- Items.Add(fontFamily.Name);
- Cursor.Current = Cursors.Default;
- }
- }
- #endregion Public Methods
- #region Public Properties
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
- public DrawMode DrawMode
- {
- get
- {
- if (!ComboBox.IsNull()) return ComboBox.DrawMode;
- return DrawMode.Normal;
- }
- set { if (!ComboBox.IsNull()) ComboBox.DrawMode = value; }
- }
- [Category("Appearance"), DefaultValue(12)]
- public int PreviewFontSize
- {
- get { return _previewFontSize; }
- set
- {
- _previewFontSize = value;
- OnPreviewFontSizeChanged(EventArgs.Empty);
- }
- }
- [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
- public new bool Sorted
- {
- get { return base.Sorted; }
- set { base.Sorted = value; }
- }
- private bool _internalCall;
- public bool InternalCall
- {
- get { return _internalCall; }
- set { _internalCall = value; }
- }
- private int _count;
- private bool _gotit;
- public string SelectedFontNameItem
- {
- get
- {
- if (SelectedIndex > -1)
- return ((Font)SelectedItem).Name;
- return string.Empty;
- }
- set
- {
- _gotit = false;
- _count = 0;
- foreach (object item in Items)
- {
- if (((Font)item).Name == value)
- {
- _gotit = true;
- break;
- }
- _count++;
- }
- if (_gotit)
- {
- _internalCall = true;
- SelectedIndex = _count;
- }
- }
- }
- public Font SelectedFontItem
- {
- get
- {
- if (SelectedIndex > -1)
- return (Font)SelectedItem;
- return null;
- }
- set
- {
- _gotit = false;
- _count = 0;
- foreach (object item in Items)
- {
- if (((Font)item).Name == value.Name)
- {
- _gotit = true;
- break;
- }
- _count++;
- }
- if (_gotit)
- {
- _internalCall = true;
- SelectedIndex = _count;
- }
- }
- }
- #endregion Public Properties
- #region Private Methods
- private void CalculateLayout()
- {
- ClearFontCache();
- using (var font = new Font(Font.FontFamily, PreviewFontSize))
- {
- Size textSize = TextRenderer.MeasureText("yY", font);
- _itemHeight = textSize.Height + 2;
- }
- }
- private bool IsUsingRTL(Control control)
- {
- bool result;
- if (control.RightToLeft == RightToLeft.Yes)
- result = true;
- else if (control.RightToLeft == RightToLeft.Inherit && !control.Parent.IsNull())
- result = IsUsingRTL(control.Parent);
- else
- result = false;
- return result;
- }
- #endregion Private Methods
- #region Protected Methods
- private void ClearFontCache()
- {
- if (!_fontCache.IsNull())
- {
- foreach (string key in _fontCache.Keys)
- _fontCache[key].Dispose();
- _fontCache.Clear();
- }
- }
- private void CreateStringFormat()
- {
- if (!_stringFormat.IsNull())
- _stringFormat.Dispose();
- _stringFormat = new StringFormat(StringFormatFlags.NoWrap)
- {
- Trimming = StringTrimming.EllipsisCharacter,
- HotkeyPrefix = HotkeyPrefix.None,
- Alignment = StringAlignment.Near,
- LineAlignment = StringAlignment.Center
- };
- if (IsUsingRTL(ComboBox))
- _stringFormat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
- }
- private Font GetFont(string fontFamilyName)
- {
- lock (_fontCache)
- {
- if (!_fontCache.ContainsKey(fontFamilyName))
- {
- Font font = (((GetFont(fontFamilyName, FontStyle.Regular) ??
- GetFont(fontFamilyName, FontStyle.Bold)) ??
- GetFont(fontFamilyName, FontStyle.Italic)) ??
- GetFont(fontFamilyName, FontStyle.Bold | FontStyle.Italic)) ??
- (Font)Font.Clone();
- _fontCache.Add(fontFamilyName, font);
- }
- }
- return _fontCache[fontFamilyName];
- }
- private Font GetFont(string fontFamilyName, FontStyle fontStyle)
- {
- Font font;
- try
- {
- font = new Font(fontFamilyName, PreviewFontSize, fontStyle);
- }
- catch
- {
- font = null;
- }
- return font;
- }
- private void OnPreviewFontSizeChanged(EventArgs e)
- {
- if (!PreviewFontSizeChanged.IsNull())
- PreviewFontSizeChanged(this, e);
- CalculateLayout();
- }
- #endregion Protected Methods
- }
- }
|