ToolStripFontComboBox.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Text;
  6. using System.Windows.Forms;
  7. namespace WinHtmlEditor
  8. {
  9. public class ToolStripFontComboBox : ToolStripComboBox
  10. {
  11. #region  Private Member Declarations
  12. private readonly Dictionary<string, Font> _fontCache;
  13. private int _itemHeight;
  14. private int _previewFontSize;
  15. private StringFormat _stringFormat;
  16. #endregion  Private Member Declarations
  17. #region  Public Constructors
  18. public ToolStripFontComboBox()
  19. {
  20. _fontCache = new Dictionary<string, Font>();
  21. DrawMode = DrawMode.OwnerDrawVariable;
  22. if (!ComboBox.IsNull())
  23. {
  24. ComboBox.DrawItem += OnDrawItem;
  25. ComboBox.MeasureItem += OnMeasureItem;
  26. }
  27. Sorted = true;
  28. PreviewFontSize = 12;
  29. CalculateLayout();
  30. CreateStringFormat();
  31. }
  32. #endregion  Public Constructors
  33. #region  Events
  34. public event EventHandler PreviewFontSizeChanged;
  35. #endregion  Events
  36. #region  Protected Overridden Methods
  37. protected override void Dispose(bool disposing)
  38. {
  39. ClearFontCache();
  40. if (!_stringFormat.IsNull())
  41. _stringFormat.Dispose();
  42. base.Dispose(disposing);
  43. }
  44. protected void OnDrawItem(object sender, DrawItemEventArgs e)
  45. {
  46. if (e.Index <= -1 || e.Index >= Items.Count) return;
  47. e.DrawBackground();
  48. if ((e.State & DrawItemState.Focus) == DrawItemState.Focus)
  49. e.DrawFocusRectangle();
  50. using (var textBrush = new SolidBrush(e.ForeColor))
  51. {
  52. string fontFamilyName = Items[e.Index].ToString();
  53. e.Graphics.DrawString(fontFamilyName, GetFont(fontFamilyName), textBrush, e.Bounds, _stringFormat);
  54. }
  55. }
  56. protected override void OnClick(EventArgs e)
  57. {
  58. base.OnClick(e);
  59. _internalCall = false;
  60. }
  61. protected override void OnSelectedIndexChanged(EventArgs e)
  62. {
  63. base.OnSelectedIndexChanged(e);
  64. _internalCall = false;
  65. }
  66. protected override void OnFontChanged(EventArgs e)
  67. {
  68. base.OnFontChanged(e);
  69. CalculateLayout();
  70. }
  71. protected override void OnGotFocus(EventArgs e)
  72. {
  73. LoadFontFamilies();
  74. base.OnGotFocus(e);
  75. }
  76. protected void OnMeasureItem(object sender, MeasureItemEventArgs e)
  77. {
  78. if (e.Index > -1 && e.Index < Items.Count)
  79. {
  80. e.ItemHeight = _itemHeight;
  81. }
  82. }
  83. protected override void OnRightToLeftChanged(EventArgs e)
  84. {
  85. base.OnRightToLeftChanged(e);
  86. CreateStringFormat();
  87. }
  88. protected override void OnTextChanged(EventArgs e)
  89. {
  90. base.OnTextChanged(e);
  91. if (Items.Count == 0)
  92. {
  93. LoadFontFamilies();
  94. int selectedIndex = FindStringExact(Text);
  95. if (selectedIndex != -1)
  96. SelectedIndex = selectedIndex;
  97. }
  98. }
  99. #endregion  Protected Overridden Methods
  100. #region  Public Methods
  101. public void LoadFontFamilies()
  102. {
  103. if (Items.Count == 0)
  104. {
  105. Cursor.Current = Cursors.WaitCursor;
  106. foreach (FontFamily fontFamily in FontFamily.Families)
  107. Items.Add(fontFamily.Name);
  108. Cursor.Current = Cursors.Default;
  109. }
  110. }
  111. #endregion  Public Methods
  112. #region  Public Properties
  113. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
  114. public DrawMode DrawMode
  115. {
  116. get
  117. {
  118. if (!ComboBox.IsNull()) return ComboBox.DrawMode;
  119. return DrawMode.Normal;
  120. }
  121. set { if (!ComboBox.IsNull()) ComboBox.DrawMode = value; }
  122. }
  123. [Category("Appearance"), DefaultValue(12)]
  124. public int PreviewFontSize
  125. {
  126. get { return _previewFontSize; }
  127. set
  128. {
  129. _previewFontSize = value;
  130. OnPreviewFontSizeChanged(EventArgs.Empty);
  131. }
  132. }
  133. [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never)]
  134. public new bool Sorted
  135. {
  136. get { return base.Sorted; }
  137. set { base.Sorted = value; }
  138. }
  139. private bool _internalCall;
  140. public bool InternalCall
  141. {
  142. get { return _internalCall; }
  143. set { _internalCall = value; }
  144. }
  145. private int _count;
  146. private bool _gotit;
  147. public string SelectedFontNameItem
  148. {
  149. get
  150. {
  151. if (SelectedIndex > -1)
  152. return ((Font)SelectedItem).Name;
  153. return string.Empty;
  154. }
  155. set
  156. {
  157. _gotit = false;
  158. _count = 0;
  159. foreach (object item in Items)
  160. {
  161. if (((Font)item).Name == value)
  162. {
  163. _gotit = true;
  164. break;
  165. }
  166. _count++;
  167. }
  168. if (_gotit)
  169. {
  170. _internalCall = true;
  171. SelectedIndex = _count;
  172. }
  173. }
  174. }
  175. public Font SelectedFontItem
  176. {
  177. get
  178. {
  179. if (SelectedIndex > -1)
  180. return (Font)SelectedItem;
  181. return null;
  182. }
  183. set
  184. {
  185. _gotit = false;
  186. _count = 0;
  187. foreach (object item in Items)
  188. {
  189. if (((Font)item).Name == value.Name)
  190. {
  191. _gotit = true;
  192. break;
  193. }
  194. _count++;
  195. }
  196. if (_gotit)
  197. {
  198. _internalCall = true;
  199. SelectedIndex = _count;
  200. }
  201. }
  202. }
  203. #endregion  Public Properties
  204. #region  Private Methods
  205. private void CalculateLayout()
  206. {
  207. ClearFontCache();
  208. using (var font = new Font(Font.FontFamily, PreviewFontSize))
  209. {
  210. Size textSize = TextRenderer.MeasureText("yY", font);
  211. _itemHeight = textSize.Height + 2;
  212. }
  213. }
  214. private bool IsUsingRTL(Control control)
  215. {
  216. bool result;
  217. if (control.RightToLeft == RightToLeft.Yes)
  218. result = true;
  219. else if (control.RightToLeft == RightToLeft.Inherit && !control.Parent.IsNull())
  220. result = IsUsingRTL(control.Parent);
  221. else
  222. result = false;
  223. return result;
  224. }
  225. #endregion  Private Methods
  226. #region  Protected Methods
  227. private void ClearFontCache()
  228. {
  229. if (!_fontCache.IsNull())
  230. {
  231. foreach (string key in _fontCache.Keys)
  232. _fontCache[key].Dispose();
  233. _fontCache.Clear();
  234. }
  235. }
  236. private void CreateStringFormat()
  237. {
  238. if (!_stringFormat.IsNull())
  239. _stringFormat.Dispose();
  240. _stringFormat = new StringFormat(StringFormatFlags.NoWrap)
  241. {
  242. Trimming = StringTrimming.EllipsisCharacter,
  243. HotkeyPrefix = HotkeyPrefix.None,
  244. Alignment = StringAlignment.Near,
  245. LineAlignment = StringAlignment.Center
  246. };
  247. if (IsUsingRTL(ComboBox))
  248. _stringFormat.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
  249. }
  250. private Font GetFont(string fontFamilyName)
  251. {
  252. lock (_fontCache)
  253. {
  254. if (!_fontCache.ContainsKey(fontFamilyName))
  255. {
  256. Font font = (((GetFont(fontFamilyName, FontStyle.Regular) ??
  257. GetFont(fontFamilyName, FontStyle.Bold)) ??
  258. GetFont(fontFamilyName, FontStyle.Italic)) ??
  259. GetFont(fontFamilyName, FontStyle.Bold | FontStyle.Italic)) ??
  260. (Font)Font.Clone();
  261. _fontCache.Add(fontFamilyName, font);
  262. }
  263. }
  264. return _fontCache[fontFamilyName];
  265. }
  266. private Font GetFont(string fontFamilyName, FontStyle fontStyle)
  267. {
  268. Font font;
  269. try
  270. {
  271. font = new Font(fontFamilyName, PreviewFontSize, fontStyle);
  272. }
  273. catch
  274. {
  275. font = null;
  276. }
  277. return font;
  278. }
  279. private void OnPreviewFontSizeChanged(EventArgs e)
  280. {
  281. if (!PreviewFontSizeChanged.IsNull())
  282. PreviewFontSizeChanged(this, e);
  283. CalculateLayout();
  284. }
  285. #endregion  Protected Methods
  286. }
  287. }