FontDialog.cs 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2006, Alexander Olk
  21. //
  22. // Authors:
  23. // Alexander Olk alex.olk@googlemail.com
  24. //
  25. //
  26. // NOT COMPLETE - work in progress
  27. // changed (simplified) by Alexander Tsyganenko
  28. using System.Collections;
  29. using System.ComponentModel;
  30. using System.Drawing;
  31. namespace System.Windows.Forms
  32. {
  33. public class FontDialog : CommonDialog
  34. {
  35. protected static readonly object EventApply = new object();
  36. private Font font;
  37. private Color color = Color.Black;
  38. private bool allowSimulations = true;
  39. private bool allowVectorFonts = true;
  40. private bool allowVerticalFonts = true;
  41. private bool allowScriptChange = true;
  42. private bool fixedPitchOnly = false;
  43. private int maxSize = 0;
  44. private int minSize = 0;
  45. private bool scriptsOnly = false;
  46. private bool showApply = false;
  47. private bool showColor = false;
  48. private bool showEffects = true;
  49. private bool showHelp = false;
  50. private bool fontMustExist = false;
  51. private Panel examplePanel;
  52. private Button okButton;
  53. private Button cancelButton;
  54. private TextBox fontTextBox;
  55. private TextBox fontstyleTextBox;
  56. private TextBox fontsizeTextBox;
  57. private ListBox fontListBox;
  58. private ListBox fontstyleListBox;
  59. private ListBox fontsizeListBox;
  60. private GroupBox effectsGroupBox;
  61. private CheckBox strikethroughCheckBox;
  62. private CheckBox underlinedCheckBox;
  63. private Label fontLabel;
  64. private Label fontstyleLabel;
  65. private Label sizeLabel;
  66. private GroupBox exampleGroupBox;
  67. private string currentFontName;
  68. private float currentSize;
  69. private FontFamily currentFamily;
  70. private System.Drawing.FontStyle currentFontStyle;
  71. private bool underlined = false;
  72. private bool strikethrough = false;
  73. private Hashtable fontHash = new Hashtable();
  74. private int[] a_sizes = {
  75. 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72
  76. };
  77. private string example_panel_text;
  78. private bool internal_change = false;
  79. #region Public Constructors
  80. public FontDialog()
  81. {
  82. form = new DialogForm(this);
  83. example_panel_text = "AaBbYyZz";
  84. okButton = new Button();
  85. cancelButton = new Button();
  86. fontTextBox = new TextBox();
  87. fontstyleTextBox = new TextBox();
  88. fontsizeTextBox = new TextBox();
  89. fontListBox = new ListBox();
  90. fontsizeListBox = new ListBox();
  91. fontstyleListBox = new ListBox();
  92. fontLabel = new Label();
  93. fontstyleLabel = new Label();
  94. sizeLabel = new Label();
  95. exampleGroupBox = new GroupBox();
  96. effectsGroupBox = new GroupBox();
  97. underlinedCheckBox = new CheckBox();
  98. strikethroughCheckBox = new CheckBox();
  99. examplePanel = new Panel();
  100. exampleGroupBox.SuspendLayout();
  101. effectsGroupBox.SuspendLayout();
  102. form.SuspendLayout();
  103. form.FormBorderStyle = FormBorderStyle.FixedDialog;
  104. form.MaximizeBox = false;
  105. // fontsizeListBox
  106. fontsizeListBox.Location = new System.Drawing.Point(284, 47);
  107. fontsizeListBox.Size = new System.Drawing.Size(52, 95);
  108. fontsizeListBox.TabIndex = 10;
  109. fontListBox.Sorted = true;
  110. // fontTextBox
  111. fontTextBox.Location = new System.Drawing.Point(16, 26);
  112. fontTextBox.Size = new System.Drawing.Size(140, 21);
  113. fontTextBox.TabIndex = 5;
  114. fontTextBox.Text = "";
  115. // fontstyleLabel
  116. fontstyleLabel.Location = new System.Drawing.Point(164, 10);
  117. fontstyleLabel.Size = new System.Drawing.Size(100, 16);
  118. fontstyleLabel.TabIndex = 1;
  119. fontstyleLabel.Text = "Font Style:";
  120. // typesizeTextBox
  121. fontsizeTextBox.Location = new System.Drawing.Point(284, 26);
  122. fontsizeTextBox.Size = new System.Drawing.Size(52, 21);
  123. fontsizeTextBox.TabIndex = 7;
  124. fontsizeTextBox.Text = "";
  125. fontsizeTextBox.MaxLength = 2;
  126. // fontListBox
  127. fontListBox.Location = new System.Drawing.Point(16, 47);
  128. fontListBox.Size = new System.Drawing.Size(140, 95);
  129. fontListBox.TabIndex = 8;
  130. fontListBox.Sorted = true;
  131. // exampleGroupBox
  132. exampleGroupBox.Controls.Add(examplePanel);
  133. exampleGroupBox.Location = new System.Drawing.Point(164, 158);
  134. exampleGroupBox.Size = new System.Drawing.Size(172, 70);
  135. exampleGroupBox.TabIndex = 12;
  136. exampleGroupBox.TabStop = false;
  137. exampleGroupBox.Text = "Example";
  138. // fontstyleListBox
  139. fontstyleListBox.Location = new System.Drawing.Point(164, 47);
  140. fontstyleListBox.Size = new System.Drawing.Size(112, 95);
  141. fontstyleListBox.TabIndex = 9;
  142. // schriftartLabel
  143. fontLabel.Location = new System.Drawing.Point(16, 10);
  144. fontLabel.Size = new System.Drawing.Size(88, 16);
  145. fontLabel.TabIndex = 0;
  146. fontLabel.Text = "Font:";
  147. // effectsGroupBox
  148. effectsGroupBox.Controls.Add(underlinedCheckBox);
  149. effectsGroupBox.Controls.Add(strikethroughCheckBox);
  150. effectsGroupBox.Location = new System.Drawing.Point(16, 158);
  151. effectsGroupBox.Size = new System.Drawing.Size(140, 70);
  152. effectsGroupBox.TabIndex = 11;
  153. effectsGroupBox.TabStop = false;
  154. effectsGroupBox.Text = "Effects";
  155. // strikethroughCheckBox
  156. strikethroughCheckBox.Location = new System.Drawing.Point(12, 20);
  157. strikethroughCheckBox.TabIndex = 0;
  158. strikethroughCheckBox.Text = "Strikethrough";
  159. // underlinedCheckBox
  160. underlinedCheckBox.Location = new System.Drawing.Point(12, 43);
  161. underlinedCheckBox.TabIndex = 1;
  162. underlinedCheckBox.Text = "Underlined";
  163. // sizeLabel
  164. sizeLabel.Location = new System.Drawing.Point(284, 10);
  165. sizeLabel.Size = new System.Drawing.Size(100, 16);
  166. sizeLabel.TabIndex = 2;
  167. sizeLabel.Text = "Size:";
  168. // okButton
  169. okButton.Location = new System.Drawing.Point(352, 26);
  170. okButton.Size = new System.Drawing.Size(70, 23);
  171. okButton.TabIndex = 3;
  172. okButton.Text = "OK";
  173. // cancelButton
  174. cancelButton.Location = new System.Drawing.Point(352, 52);
  175. cancelButton.Size = new System.Drawing.Size(70, 23);
  176. cancelButton.TabIndex = 4;
  177. cancelButton.Text = "Cancel";
  178. // fontstyleTextBox
  179. fontstyleTextBox.Location = new System.Drawing.Point(164, 26);
  180. fontstyleTextBox.Size = new System.Drawing.Size(112, 21);
  181. fontstyleTextBox.TabIndex = 6;
  182. fontstyleTextBox.Text = "";
  183. // examplePanel
  184. examplePanel.Location = new System.Drawing.Point(8, 20);
  185. examplePanel.TabIndex = 0;
  186. examplePanel.Size = new System.Drawing.Size(156, 40);
  187. form.AcceptButton = okButton;
  188. form.CancelButton = cancelButton;
  189. form.Controls.Add(exampleGroupBox);
  190. form.Controls.Add(effectsGroupBox);
  191. form.Controls.Add(fontsizeListBox);
  192. form.Controls.Add(fontstyleListBox);
  193. form.Controls.Add(fontListBox);
  194. form.Controls.Add(fontsizeTextBox);
  195. form.Controls.Add(fontstyleTextBox);
  196. form.Controls.Add(fontTextBox);
  197. form.Controls.Add(cancelButton);
  198. form.Controls.Add(okButton);
  199. form.Controls.Add(sizeLabel);
  200. form.Controls.Add(fontstyleLabel);
  201. form.Controls.Add(fontLabel);
  202. exampleGroupBox.ResumeLayout(false);
  203. effectsGroupBox.ResumeLayout(false);
  204. form.ClientSize = new System.Drawing.Size(434, 244);
  205. form.Text = "Font";
  206. form.ResumeLayout(false);
  207. cancelButton.Click += new EventHandler(OnClickCancelButton);
  208. okButton.Click += new EventHandler(OnClickOkButton);
  209. examplePanel.Paint += new PaintEventHandler(OnPaintExamplePanel);
  210. fontListBox.SelectedIndexChanged += new EventHandler(OnSelectedIndexChangedFontListBox);
  211. fontsizeListBox.SelectedIndexChanged += new EventHandler(OnSelectedIndexChangedSizeListBox);
  212. fontstyleListBox.SelectedIndexChanged += new EventHandler(OnSelectedIndexChangedFontStyleListBox);
  213. underlinedCheckBox.CheckedChanged += new EventHandler(OnCheckedChangedUnderlinedCheckBox);
  214. strikethroughCheckBox.CheckedChanged += new EventHandler(OnCheckedChangedStrikethroughCheckBox);
  215. fontTextBox.KeyPress += new KeyPressEventHandler(OnFontTextBoxKeyPress);
  216. fontstyleTextBox.KeyPress += new KeyPressEventHandler(OnFontStyleTextBoxKeyPress);
  217. fontsizeTextBox.KeyPress += new KeyPressEventHandler(OnFontSizeTextBoxKeyPress);
  218. fontTextBox.TextChanged += new EventHandler(OnFontTextBoxTextChanged);
  219. fontstyleTextBox.TextChanged += new EventHandler(OnFontStyleTextTextChanged);
  220. fontsizeTextBox.TextChanged += new EventHandler(OnFontSizeTextBoxTextChanged);
  221. PopulateFontList();
  222. }
  223. #endregion // Public Constructors
  224. #region Public Instance Properties
  225. public Font Font
  226. {
  227. get
  228. {
  229. return font;
  230. }
  231. set
  232. {
  233. if (value != null)
  234. {
  235. font = new Font(value, value.Style);
  236. currentFontStyle = font.Style;
  237. currentSize = font.SizeInPoints;
  238. currentFontName = font.Name;
  239. strikethroughCheckBox.Checked = font.Strikeout;
  240. underlinedCheckBox.Checked = font.Underline;
  241. int index = fontListBox.FindString(currentFontName);
  242. if (index != -1)
  243. {
  244. fontListBox.SelectedIndex = index;
  245. }
  246. else
  247. {
  248. fontListBox.SelectedIndex = 0;
  249. }
  250. UpdateFontSizeListBox();
  251. UpdateFontStyleListBox();
  252. fontListBox.TopIndex = fontListBox.SelectedIndex;
  253. }
  254. }
  255. }
  256. [DefaultValue(false)]
  257. public bool FontMustExist
  258. {
  259. get
  260. {
  261. return fontMustExist;
  262. }
  263. set
  264. {
  265. fontMustExist = value;
  266. }
  267. }
  268. [DefaultValue("Color [Black]")]
  269. public Color Color
  270. {
  271. set
  272. {
  273. color = value;
  274. examplePanel.Invalidate();
  275. }
  276. get
  277. {
  278. return color;
  279. }
  280. }
  281. [DefaultValue(true)]
  282. public bool AllowSimulations
  283. {
  284. set
  285. {
  286. allowSimulations = value;
  287. }
  288. get
  289. {
  290. return allowSimulations;
  291. }
  292. }
  293. [DefaultValue(true)]
  294. public bool AllowVectorFonts
  295. {
  296. set
  297. {
  298. allowVectorFonts = value;
  299. }
  300. get
  301. {
  302. return allowVectorFonts;
  303. }
  304. }
  305. [DefaultValue(true)]
  306. public bool AllowVerticalFonts
  307. {
  308. set
  309. {
  310. allowVerticalFonts = value;
  311. }
  312. get
  313. {
  314. return allowVerticalFonts;
  315. }
  316. }
  317. [DefaultValue(true)]
  318. public bool AllowScriptChange
  319. {
  320. set
  321. {
  322. allowScriptChange = value;
  323. }
  324. get
  325. {
  326. return allowScriptChange;
  327. }
  328. }
  329. [DefaultValue(false)]
  330. public bool FixedPitchOnly
  331. {
  332. set
  333. {
  334. if (fixedPitchOnly != value)
  335. {
  336. fixedPitchOnly = value;
  337. PopulateFontList();
  338. }
  339. }
  340. get
  341. {
  342. return fixedPitchOnly;
  343. }
  344. }
  345. [DefaultValue(0)]
  346. public int MaxSize
  347. {
  348. set
  349. {
  350. maxSize = value;
  351. if (maxSize < 0)
  352. maxSize = 0;
  353. if (maxSize < minSize)
  354. minSize = maxSize;
  355. CreateFontSizeListBoxItems();
  356. }
  357. get
  358. {
  359. return maxSize;
  360. }
  361. }
  362. [DefaultValue(0)]
  363. public int MinSize
  364. {
  365. set
  366. {
  367. minSize = value;
  368. if (minSize < 0)
  369. minSize = 0;
  370. if (minSize > maxSize)
  371. maxSize = minSize;
  372. CreateFontSizeListBoxItems();
  373. if (minSize > currentSize)
  374. if (font != null)
  375. {
  376. font.Dispose();
  377. currentSize = minSize;
  378. font = new Font(currentFamily, currentSize, currentFontStyle);
  379. UpdateExamplePanel();
  380. fontsizeTextBox.Text = currentSize.ToString();
  381. }
  382. }
  383. get
  384. {
  385. return minSize;
  386. }
  387. }
  388. [DefaultValue(false)]
  389. public bool ScriptsOnly
  390. {
  391. set
  392. {
  393. scriptsOnly = value;
  394. }
  395. get
  396. {
  397. return scriptsOnly;
  398. }
  399. }
  400. [DefaultValue(false)]
  401. public bool ShowApply
  402. {
  403. set
  404. {
  405. if (value != showApply)
  406. {
  407. showApply = value;
  408. }
  409. }
  410. get
  411. {
  412. return showApply;
  413. }
  414. }
  415. [DefaultValue(false)]
  416. public bool ShowColor
  417. {
  418. set
  419. {
  420. if (value != showColor)
  421. {
  422. showColor = value;
  423. }
  424. }
  425. get
  426. {
  427. return showColor;
  428. }
  429. }
  430. [DefaultValue(true)]
  431. public bool ShowEffects
  432. {
  433. set
  434. {
  435. if (value != showEffects)
  436. {
  437. showEffects = value;
  438. if (showEffects)
  439. effectsGroupBox.Show();
  440. else
  441. effectsGroupBox.Hide();
  442. form.Refresh();
  443. }
  444. }
  445. get
  446. {
  447. return showEffects;
  448. }
  449. }
  450. [DefaultValue(false)]
  451. public bool ShowHelp
  452. {
  453. set
  454. {
  455. if (value != showHelp)
  456. {
  457. showHelp = value;
  458. }
  459. }
  460. get
  461. {
  462. return showHelp;
  463. }
  464. }
  465. #endregion // Public Instance Properties
  466. #region Protected Instance Properties
  467. protected int Options
  468. {
  469. get { return 0; }
  470. }
  471. #endregion // Protected Instance Properties
  472. #region Public Instance Methods
  473. public override void Reset()
  474. {
  475. color = Color.Black;
  476. allowSimulations = true;
  477. allowVectorFonts = true;
  478. allowVerticalFonts = true;
  479. allowScriptChange = true;
  480. fixedPitchOnly = false;
  481. maxSize = 0;
  482. minSize = 0;
  483. CreateFontSizeListBoxItems();
  484. scriptsOnly = false;
  485. showApply = false;
  486. showColor = false;
  487. showEffects = true;
  488. effectsGroupBox.Show();
  489. showHelp = false;
  490. form.Refresh();
  491. }
  492. public override string ToString()
  493. {
  494. if (font == null)
  495. return base.ToString();
  496. return String.Concat(base.ToString(), ", Font: ", font.ToString());
  497. }
  498. #endregion // Public Instance Methods
  499. #region Protected Instance Methods
  500. protected override bool RunDialog()
  501. {
  502. return true;
  503. }
  504. #endregion // Protected Instance Methods
  505. void OnClickCancelButton(object sender, EventArgs e)
  506. {
  507. form.DialogResult = DialogResult.Cancel;
  508. }
  509. void OnClickOkButton(object sender, EventArgs e)
  510. {
  511. form.DialogResult = DialogResult.OK;
  512. }
  513. void OnPaintExamplePanel(object sender, PaintEventArgs e)
  514. {
  515. e.Graphics.FillRectangle(SystemBrushes.Control, 0, 0, 156, 40);
  516. SizeF fontSizeF = e.Graphics.MeasureString(example_panel_text, font);
  517. int text_width = (int)fontSizeF.Width;
  518. int text_height = (int)fontSizeF.Height;
  519. int x = (examplePanel.Width / 2) - (text_width / 2);
  520. if (x < 0) x = 0;
  521. int y = (examplePanel.Height / 2) - (text_height / 2);
  522. e.Graphics.DrawString(example_panel_text, font, SystemBrushes.ControlText, new System.Drawing.Point(x, y));
  523. }
  524. void OnSelectedIndexChangedFontListBox(object sender, EventArgs e)
  525. {
  526. if (fontListBox.SelectedIndex != -1)
  527. {
  528. currentFamily = FindByName(fontListBox.Items[fontListBox.SelectedIndex].ToString());
  529. fontTextBox.Text = currentFamily.Name;
  530. internal_change = true;
  531. UpdateFontStyleListBox();
  532. UpdateFontSizeListBox();
  533. UpdateExamplePanel();
  534. fontTextBox.Focus();
  535. internal_change = false;
  536. }
  537. }
  538. void OnSelectedIndexChangedSizeListBox(object sender, EventArgs e)
  539. {
  540. if (fontsizeListBox.SelectedIndex != -1)
  541. {
  542. currentSize = (float)System.Convert.ToDouble(fontsizeListBox.Items[fontsizeListBox.SelectedIndex]);
  543. }
  544. fontsizeTextBox.Text = currentSize.ToString();
  545. UpdateExamplePanel();
  546. if (!internal_change)
  547. fontsizeTextBox.Focus();
  548. }
  549. void OnSelectedIndexChangedFontStyleListBox(object sender, EventArgs e)
  550. {
  551. if (fontstyleListBox.SelectedIndex != -1)
  552. {
  553. switch (fontstyleListBox.SelectedIndex)
  554. {
  555. case 0:
  556. currentFontStyle = System.Drawing.FontStyle.Regular;
  557. break;
  558. case 1:
  559. currentFontStyle = System.Drawing.FontStyle.Bold;
  560. break;
  561. case 2:
  562. currentFontStyle = System.Drawing.FontStyle.Italic;
  563. break;
  564. case 3:
  565. currentFontStyle = System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic;
  566. break;
  567. default:
  568. currentFontStyle = System.Drawing.FontStyle.Regular;
  569. break;
  570. }
  571. if (underlined)
  572. currentFontStyle = currentFontStyle | System.Drawing.FontStyle.Underline;
  573. if (strikethrough)
  574. currentFontStyle = currentFontStyle | System.Drawing.FontStyle.Strikeout;
  575. fontstyleTextBox.Text = fontstyleListBox.Items[fontstyleListBox.SelectedIndex].ToString();
  576. if (!internal_change)
  577. {
  578. UpdateExamplePanel();
  579. fontstyleTextBox.Focus();
  580. }
  581. }
  582. }
  583. void OnCheckedChangedUnderlinedCheckBox(object sender, EventArgs e)
  584. {
  585. if (underlinedCheckBox.Checked)
  586. {
  587. currentFontStyle = currentFontStyle | System.Drawing.FontStyle.Underline;
  588. underlined = true;
  589. }
  590. else
  591. {
  592. currentFontStyle = currentFontStyle ^ System.Drawing.FontStyle.Underline;
  593. underlined = false;
  594. }
  595. UpdateExamplePanel();
  596. }
  597. void OnCheckedChangedStrikethroughCheckBox(object sender, EventArgs e)
  598. {
  599. if (strikethroughCheckBox.Checked)
  600. {
  601. currentFontStyle = currentFontStyle | System.Drawing.FontStyle.Strikeout;
  602. strikethrough = true;
  603. }
  604. else
  605. {
  606. currentFontStyle = currentFontStyle ^ System.Drawing.FontStyle.Strikeout;
  607. strikethrough = false;
  608. }
  609. UpdateExamplePanel();
  610. }
  611. bool internal_textbox_change = false;
  612. void OnFontTextBoxKeyPress(object sender, KeyPressEventArgs e)
  613. {
  614. internal_textbox_change = true;
  615. if (fontListBox.SelectedIndex > -1)
  616. fontListBox.SelectedIndex = -1;
  617. }
  618. void OnFontStyleTextBoxKeyPress(object sender, KeyPressEventArgs e)
  619. {
  620. internal_textbox_change = true;
  621. if (fontstyleListBox.SelectedIndex > -1)
  622. fontstyleListBox.SelectedIndex = -1;
  623. }
  624. void OnFontSizeTextBoxKeyPress(object sender, KeyPressEventArgs e)
  625. {
  626. if (Char.IsLetter(e.KeyChar) || Char.IsWhiteSpace(e.KeyChar) || Char.IsPunctuation(e.KeyChar) || e.KeyChar == ',')
  627. {
  628. e.Handled = true;
  629. return;
  630. }
  631. internal_textbox_change = true;
  632. }
  633. void OnFontTextBoxTextChanged(object sender, EventArgs e)
  634. {
  635. if (!internal_textbox_change)
  636. return;
  637. internal_textbox_change = false;
  638. string search = fontTextBox.Text;
  639. // Look for an exact match
  640. int found = fontListBox.FindStringExact(search);
  641. if (found != -1)
  642. {
  643. fontListBox.SelectedIndex = found;
  644. return;
  645. }
  646. // Look for a partial match
  647. found = fontListBox.FindString(search);
  648. if (found != -1)
  649. {
  650. fontListBox.TopIndex = found;
  651. return;
  652. }
  653. // No match, scroll to the top
  654. if (fontListBox.Items.Count > 0)
  655. fontListBox.TopIndex = 0;
  656. }
  657. void OnFontStyleTextTextChanged(object sender, EventArgs e)
  658. {
  659. if (!internal_textbox_change)
  660. return;
  661. internal_textbox_change = false;
  662. // Look for an exact match
  663. int found = fontstyleListBox.FindStringExact(fontstyleTextBox.Text);
  664. if (found != -1)
  665. fontstyleListBox.SelectedIndex = found;
  666. }
  667. void OnFontSizeTextBoxTextChanged(object sender, EventArgs e)
  668. {
  669. if (!internal_textbox_change)
  670. return;
  671. internal_textbox_change = false;
  672. if (fontsizeTextBox.Text.Length == 0)
  673. return;
  674. for (int i = 0; i < fontsizeListBox.Items.Count; i++)
  675. {
  676. string name = fontsizeListBox.Items[i] as string;
  677. if (name.StartsWith(fontsizeTextBox.Text))
  678. {
  679. if (name == fontsizeTextBox.Text)
  680. fontsizeListBox.SelectedIndex = i;
  681. else
  682. fontsizeListBox.TopIndex = i;
  683. break;
  684. }
  685. }
  686. currentSize = (float)System.Convert.ToDouble(fontsizeTextBox.Text);
  687. UpdateExamplePanel();
  688. }
  689. void UpdateExamplePanel()
  690. {
  691. if (font != null)
  692. font.Dispose();
  693. font = new Font(currentFamily, currentSize, currentFontStyle);
  694. examplePanel.Invalidate();
  695. }
  696. void UpdateFontSizeListBox()
  697. {
  698. int index = fontsizeListBox.FindString(((int)Math.Round((currentSize))).ToString());
  699. fontsizeListBox.SelectedIndex = index;
  700. }
  701. void UpdateFontStyleListBox()
  702. {
  703. // don't know if that works, IsStyleAvailable returns true for all styles under X
  704. fontstyleListBox.BeginUpdate();
  705. fontstyleListBox.Items.Clear();
  706. int index = -1;
  707. int to_select = 0;
  708. if (currentFamily.IsStyleAvailable(System.Drawing.FontStyle.Regular))
  709. {
  710. index = fontstyleListBox.Items.Add("Regular");
  711. if ((currentFontStyle & System.Drawing.FontStyle.Regular) == System.Drawing.FontStyle.Regular)
  712. to_select = index;
  713. }
  714. if (currentFamily.IsStyleAvailable(System.Drawing.FontStyle.Bold))
  715. {
  716. index = fontstyleListBox.Items.Add("Bold");
  717. if ((currentFontStyle & System.Drawing.FontStyle.Bold) == System.Drawing.FontStyle.Bold)
  718. to_select = index;
  719. }
  720. if (currentFamily.IsStyleAvailable(System.Drawing.FontStyle.Italic))
  721. {
  722. index = fontstyleListBox.Items.Add("Italic");
  723. if ((currentFontStyle & System.Drawing.FontStyle.Italic) == System.Drawing.FontStyle.Italic)
  724. to_select = index;
  725. }
  726. if (currentFamily.IsStyleAvailable(System.Drawing.FontStyle.Bold) && currentFamily.IsStyleAvailable(System.Drawing.FontStyle.Italic))
  727. {
  728. index = fontstyleListBox.Items.Add("Bold Italic");
  729. if ((currentFontStyle & (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)) == (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))
  730. to_select = index;
  731. }
  732. if (fontstyleListBox.Items.Count > 0)
  733. {
  734. fontstyleListBox.SelectedIndex = to_select;
  735. switch ((string)fontstyleListBox.SelectedItem)
  736. {
  737. case "Regular":
  738. currentFontStyle = System.Drawing.FontStyle.Regular;
  739. break;
  740. case "Bold":
  741. currentFontStyle = System.Drawing.FontStyle.Bold;
  742. break;
  743. case "Italic":
  744. currentFontStyle = System.Drawing.FontStyle.Italic;
  745. break;
  746. case "Bold Italic":
  747. currentFontStyle = System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic;
  748. break;
  749. }
  750. if (strikethroughCheckBox.Checked)
  751. currentFontStyle |= System.Drawing.FontStyle.Strikeout;
  752. if (underlinedCheckBox.Checked)
  753. currentFontStyle |= System.Drawing.FontStyle.Underline;
  754. }
  755. fontstyleListBox.EndUpdate();
  756. }
  757. FontFamily FindByName(string name)
  758. {
  759. return fontHash[name] as FontFamily;
  760. }
  761. void CreateFontSizeListBoxItems()
  762. {
  763. fontsizeListBox.BeginUpdate();
  764. fontsizeListBox.Items.Clear();
  765. if (minSize == 0 && maxSize == 0)
  766. {
  767. foreach (int i in a_sizes)
  768. fontsizeListBox.Items.Add(i.ToString());
  769. }
  770. else
  771. {
  772. foreach (int i in a_sizes)
  773. {
  774. if (i >= minSize && i <= maxSize)
  775. fontsizeListBox.Items.Add(i.ToString());
  776. }
  777. }
  778. fontsizeListBox.EndUpdate();
  779. }
  780. #region Private Methods
  781. private void PopulateFontList()
  782. {
  783. fontListBox.Items.Clear();
  784. fontHash.Clear();
  785. fontListBox.BeginUpdate();
  786. foreach (FontFamily ff in FontFamily.Families)
  787. {
  788. if (!fontHash.ContainsKey(ff.Name))
  789. {
  790. if (!fixedPitchOnly || (IsFontFamilyFixedPitch(ff)))
  791. {
  792. fontListBox.Items.Add(ff.Name);
  793. fontHash.Add(ff.Name, ff);
  794. }
  795. }
  796. }
  797. fontListBox.EndUpdate();
  798. CreateFontSizeListBoxItems();
  799. if (fixedPitchOnly)
  800. this.Font = new Font(FontFamily.GenericMonospace, 8.25f);
  801. else
  802. this.Font = form.Font;
  803. }
  804. private bool IsFontFamilyFixedPitch(FontFamily family)
  805. {
  806. System.Drawing.FontStyle fs;
  807. if (family.IsStyleAvailable(System.Drawing.FontStyle.Regular))
  808. fs = System.Drawing.FontStyle.Regular;
  809. else if (family.IsStyleAvailable(System.Drawing.FontStyle.Bold))
  810. fs = System.Drawing.FontStyle.Bold;
  811. else if (family.IsStyleAvailable(System.Drawing.FontStyle.Italic))
  812. fs = System.Drawing.FontStyle.Italic;
  813. else if (family.IsStyleAvailable(System.Drawing.FontStyle.Strikeout))
  814. fs = System.Drawing.FontStyle.Strikeout;
  815. else if (family.IsStyleAvailable(System.Drawing.FontStyle.Underline))
  816. fs = System.Drawing.FontStyle.Underline;
  817. else
  818. return false;
  819. Font f = new Font(family.Name, 10, fs);
  820. if (TextRenderer.MeasureText("i", f).Width == TextRenderer.MeasureText("w", f).Width)
  821. return true;
  822. return false;
  823. }
  824. #endregion
  825. }
  826. }