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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2006, Alexander Olk
23 // Alexander Olk alex.olk@googlemail.com
27 // NOT COMPLETE - work in progress
29 using System.ComponentModel;
31 using System.Text.RegularExpressions;
33 using System.Collections;
35 namespace System.Windows.Forms
37 [DefaultProperty( "Font" )]
38 [DefaultEvent("Apply")]
39 public class FontDialog : CommonDialog
41 protected static readonly object EventApply = new object ();
44 private Color color = Color.Black;
45 private bool allowSimulations = true;
46 private bool allowVectorFonts = true;
47 private bool allowVerticalFonts = true;
48 private bool allowScriptChange = true;
49 private bool fixedPitchOnly = false;
50 private int maxSize = 0;
51 private int minSize = 0;
52 private bool scriptsOnly = false;
53 private bool showApply = false;
54 private bool showColor = false;
55 private bool showEffects = true;
56 private bool showHelp = false;
58 private bool fontMustExist = false;
60 private Panel examplePanel;
62 private Button okButton;
63 private Button cancelButton;
64 private Button applyButton;
65 private Button helpButton;
67 private TextBox fontTextBox;
68 private TextBox fontstyleTextBox;
69 private TextBox fontsizeTextBox;
71 private MouseWheelListBox fontListBox;
72 private MouseWheelListBox fontstyleListBox;
73 private MouseWheelListBox fontsizeListBox;
75 private GroupBox effectsGroupBox;
76 private CheckBox strikethroughCheckBox;
77 private CheckBox underlinedCheckBox;
78 private ComboBox scriptComboBox;
80 private Label fontLabel;
81 private Label fontstyleLabel;
82 private Label sizeLabel;
83 private Label scriptLabel;
85 private GroupBox exampleGroupBox;
87 private ColorComboBox colorComboBox;
89 private string currentFontName;
91 private float currentSize;
93 private FontFamily currentFamily;
95 private FontStyle currentFontStyle;
97 private bool underlined = false;
98 private bool strikethrough = false;
100 private Hashtable fontHash = new Hashtable();
102 private int[] a_sizes = {
103 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72
106 // char set stuff is only here to make me happy :-)
107 private string [] char_sets_names = {
135 private string [] char_sets = {
138 "Aa" + (char)0x3042 + (char)0x3041 + (char)0x30a2 + (char)0x30a1 + (char)0x4e9c + (char)0x5b87,
139 (char)0xac00 + (char)0xb098 + (char)0xb2e4 + "AaBYyZz",
140 new String(new Char [] {(char)0x5fae, (char)0x8f6f, (char)0x4e2d, (char)0x6587, (char)0x8f6f, (char)0x4ef6}),
141 new String(new Char [] {(char)0x4e2d, (char)0x6587, (char)0x5b57, (char)0x578b, (char)0x7bc4, (char)0x4f8b}),
142 "AaBb" + (char)0x0391 + (char)0x03b1 + (char)0x0392 + (char)0x03b2,
143 "AaBb" + (char)0x011e + (char)0x011f + (char)0x015e + (char)0x015f,
144 "AaBb" + (char)0x05e0 + (char)0x05e1 + (char)0x05e9 + (char)0x05ea,
145 "AaBb" + (char)0x0627 + (char)0x0628 + (char)0x062c + (char)0x062f + (char)0x0647 + (char)0x0648 + (char)0x0632,
147 "AaBb" + (char)0x01a0 + (char)0x01a1 + (char)0x01af + (char)0x01b0,
148 "AaBb" + (char)0x0411 + (char)0x0431 + (char)0x0424 + (char)0x0444,
149 "AaBb" + (char)0xc1 + (char)0xe1 + (char)0xd4 + (char)0xf4,
150 "AaBb" + (char)0x0e2d + (char)0x0e31 + (char)0x0e01 + (char)0x0e29 + (char)0x0e23 + (char)0x0e44 + (char)0x0e17 +(char)0x0e22,
151 (char)0xac00 + (char)0xb098 + (char)0xb2e4 + "AaBYyZz",
153 "AaBb" + (char)0xf8 + (char)0xf1 + (char)0xfd,
163 private string example_panel_text;
165 private bool internal_change = false;
167 #region Public Constructors
170 form = new DialogForm (this);
171 example_panel_text = char_sets [0];
173 okButton = new Button( );
174 cancelButton = new Button( );
175 applyButton = new Button( );
176 helpButton = new Button( );
178 fontTextBox = new TextBox( );
179 fontstyleTextBox = new TextBox( );
180 fontsizeTextBox = new TextBox( );
182 fontListBox = new MouseWheelListBox ();
183 fontsizeListBox = new MouseWheelListBox ();
184 fontstyleListBox = new MouseWheelListBox ();
186 fontLabel = new Label( );
187 fontstyleLabel = new Label( );
188 sizeLabel = new Label( );
189 scriptLabel = new Label( );
191 exampleGroupBox = new GroupBox( );
193 effectsGroupBox = new GroupBox( );
194 underlinedCheckBox = new CheckBox( );
195 strikethroughCheckBox = new CheckBox( );
196 scriptComboBox = new ComboBox( );
198 examplePanel = new Panel( );
200 colorComboBox = new ColorComboBox( this );
202 exampleGroupBox.SuspendLayout( );
203 effectsGroupBox.SuspendLayout( );
204 form.SuspendLayout( );
206 form.FormBorderStyle = FormBorderStyle.FixedDialog;
207 form.MaximizeBox = false;
210 fontsizeListBox.Location = new Point( 284, 47 );
211 fontsizeListBox.Size = new Size( 52, 95 );
212 fontsizeListBox.TabIndex = 10;
213 fontListBox.Sorted = true;
215 fontTextBox.Location = new Point( 16, 26 );
216 fontTextBox.Size = new Size( 140, 21 );
217 fontTextBox.TabIndex = 5;
218 fontTextBox.Text = "";
220 fontstyleLabel.Location = new Point( 164, 10 );
221 fontstyleLabel.Size = new Size( 100, 16 );
222 fontstyleLabel.TabIndex = 1;
223 fontstyleLabel.Text = "Font Style:";
225 fontsizeTextBox.Location = new Point( 284, 26 );
226 fontsizeTextBox.Size = new Size( 52, 21 );
227 fontsizeTextBox.TabIndex = 7;
228 fontsizeTextBox.Text = "";
229 fontsizeTextBox.MaxLength = 2;
231 fontListBox.Location = new Point( 16, 47 );
232 fontListBox.Size = new Size( 140, 95 );
233 fontListBox.TabIndex = 8;
234 fontListBox.Sorted = true;
236 exampleGroupBox.Controls.Add( examplePanel );
237 exampleGroupBox.FlatStyle = FlatStyle.System;
238 exampleGroupBox.Location = new Point( 164, 158 );
239 exampleGroupBox.Size = new Size( 172, 70 );
240 exampleGroupBox.TabIndex = 12;
241 exampleGroupBox.TabStop = false;
242 exampleGroupBox.Text = "Example";
244 fontstyleListBox.Location = new Point( 164, 47 );
245 fontstyleListBox.Size = new Size( 112, 95 );
246 fontstyleListBox.TabIndex = 9;
248 fontLabel.Location = new Point( 16, 10 );
249 fontLabel.Size = new Size( 88, 16 );
250 fontLabel.TabIndex = 0;
251 fontLabel.Text = "Font:";
253 effectsGroupBox.Controls.Add( underlinedCheckBox );
254 effectsGroupBox.Controls.Add( strikethroughCheckBox );
255 effectsGroupBox.Controls.Add( colorComboBox );
256 effectsGroupBox.FlatStyle = FlatStyle.System;
257 effectsGroupBox.Location = new Point( 16, 158 );
258 effectsGroupBox.Size = new Size( 140, 116 );
259 effectsGroupBox.TabIndex = 11;
260 effectsGroupBox.TabStop = false;
261 effectsGroupBox.Text = "Effects";
262 // strikethroughCheckBox
263 strikethroughCheckBox.FlatStyle = FlatStyle.System;
264 strikethroughCheckBox.Location = new Point( 8, 16 );
265 strikethroughCheckBox.TabIndex = 0;
266 strikethroughCheckBox.Text = "Strikethrough";
268 colorComboBox.Location = new Point( 8, 70 );
269 colorComboBox.Size = new Size( 130, 21 );
271 sizeLabel.Location = new Point( 284, 10 );
272 sizeLabel.Size = new Size( 100, 16 );
273 sizeLabel.TabIndex = 2;
274 sizeLabel.Text = "Size:";
276 scriptComboBox.Location = new Point( 164, 253 );
277 scriptComboBox.Size = new Size( 172, 21 );
278 scriptComboBox.TabIndex = 14;
279 scriptComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
281 okButton.FlatStyle = FlatStyle.System;
282 okButton.Location = new Point( 352, 26 );
283 okButton.Size = new Size( 70, 23 );
284 okButton.TabIndex = 3;
285 okButton.Text = "OK";
287 cancelButton.FlatStyle = FlatStyle.System;
288 cancelButton.Location = new Point( 352, 52 );
289 cancelButton.Size = new Size( 70, 23 );
290 cancelButton.TabIndex = 4;
291 cancelButton.Text = "Cancel";
293 applyButton.FlatStyle = FlatStyle.System;
294 applyButton.Location = new Point( 352, 78 );
295 applyButton.Size = new Size( 70, 23 );
296 applyButton.TabIndex = 5;
297 applyButton.Text = "Apply";
299 helpButton.FlatStyle = FlatStyle.System;
300 helpButton.Location = new Point( 352, 104 );
301 helpButton.Size = new Size( 70, 23 );
302 helpButton.TabIndex = 6;
303 helpButton.Text = "Help";
304 // underlinedCheckBox
305 underlinedCheckBox.FlatStyle = FlatStyle.System;
306 underlinedCheckBox.Location = new Point( 8, 36 );
307 underlinedCheckBox.TabIndex = 1;
308 underlinedCheckBox.Text = "Underlined";
310 fontstyleTextBox.Location = new Point( 164, 26 );
311 fontstyleTextBox.Size = new Size( 112, 21 );
312 fontstyleTextBox.TabIndex = 6;
313 fontstyleTextBox.Text = "";
315 scriptLabel.Location = new Point( 164, 236 );
316 scriptLabel.Size = new Size( 100, 16 );
317 scriptLabel.TabIndex = 13;
318 scriptLabel.Text = "Script:";
320 examplePanel.Location = new Point( 8, 20 );
321 examplePanel.TabIndex = 0;
322 examplePanel.Size = new Size( 156, 40 );
323 examplePanel.BorderStyle = BorderStyle.Fixed3D;
325 form.AcceptButton = okButton;
326 form.CancelButton = cancelButton;
328 form.Controls.Add( scriptComboBox );
329 form.Controls.Add( scriptLabel );
330 form.Controls.Add( exampleGroupBox );
331 form.Controls.Add( effectsGroupBox );
332 form.Controls.Add( fontsizeListBox );
333 form.Controls.Add( fontstyleListBox );
334 form.Controls.Add( fontListBox );
335 form.Controls.Add( fontsizeTextBox );
336 form.Controls.Add( fontstyleTextBox );
337 form.Controls.Add( fontTextBox );
338 form.Controls.Add( cancelButton );
339 form.Controls.Add( okButton );
340 form.Controls.Add( sizeLabel );
341 form.Controls.Add( fontstyleLabel );
342 form.Controls.Add( fontLabel );
343 form.Controls.Add( applyButton );
344 form.Controls.Add( helpButton );
346 exampleGroupBox.ResumeLayout( false );
347 effectsGroupBox.ResumeLayout( false );
349 form.Size = new Size( 430, 318 );
351 form.FormBorderStyle = FormBorderStyle.FixedDialog;
352 form.MaximizeBox = false;
356 form.ResumeLayout( false );
358 scriptComboBox.BeginUpdate ();
359 scriptComboBox.Items.AddRange (char_sets_names);
360 scriptComboBox.SelectedIndex = 0;
361 scriptComboBox.EndUpdate ();
365 colorComboBox.Hide( );
367 cancelButton.Click += new EventHandler( OnClickCancelButton );
368 okButton.Click += new EventHandler( OnClickOkButton );
369 applyButton.Click += new EventHandler (OnApplyButton);
370 examplePanel.Paint += new PaintEventHandler( OnPaintExamplePanel );
371 fontListBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedFontListBox );
372 fontsizeListBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedSizeListBox );
373 fontstyleListBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedFontStyleListBox );
374 underlinedCheckBox.CheckedChanged += new EventHandler( OnCheckedChangedUnderlinedCheckBox );
375 strikethroughCheckBox.CheckedChanged += new EventHandler( OnCheckedChangedStrikethroughCheckBox );
376 scriptComboBox.SelectedIndexChanged += new EventHandler (OnSelectedIndexChangedScriptComboBox);
378 fontTextBox.KeyPress += new KeyPressEventHandler (OnFontTextBoxKeyPress);
379 fontstyleTextBox.KeyPress += new KeyPressEventHandler (OnFontStyleTextBoxKeyPress);
380 fontsizeTextBox.KeyPress += new KeyPressEventHandler (OnFontSizeTextBoxKeyPress);
382 fontTextBox.TextChanged += new EventHandler (OnFontTextBoxTextChanged);
383 fontstyleTextBox.TextChanged += new EventHandler (OnFontStyleTextTextChanged);
384 fontsizeTextBox.TextChanged += new EventHandler (OnFontSizeTextBoxTextChanged);
386 fontTextBox.KeyDown += new KeyEventHandler (OnFontTextBoxKeyDown);
387 fontstyleTextBox.KeyDown += new KeyEventHandler (OnFontStyleTextBoxKeyDown);
388 fontsizeTextBox.KeyDown += new KeyEventHandler (OnFontSizeTextBoxKeyDown);
390 fontTextBox.MouseWheel += new MouseEventHandler (OnFontTextBoxMouseWheel);
391 fontstyleTextBox.MouseWheel += new MouseEventHandler (OnFontStyleTextBoxMouseWheel);
392 fontsizeTextBox.MouseWheel += new MouseEventHandler (OnFontSizeTextBoxMouseWheel);
396 #endregion // Public Constructors
398 #region Public Instance Properties
407 font = new Font(value, value.Style);
409 currentFontStyle = font.Style;
410 currentSize = font.SizeInPoints;
411 currentFontName = font.Name;
413 strikethroughCheckBox.Checked = font.Strikeout;
414 underlinedCheckBox.Checked = font.Underline;
416 int index = fontListBox.FindString (currentFontName);
419 fontListBox.SelectedIndex = index;
421 fontListBox.SelectedIndex = 0;
424 UpdateFontSizeListBox ();
425 UpdateFontStyleListBox ();
427 fontListBox.TopIndex = fontListBox.SelectedIndex;
432 [DefaultValue(false)]
433 public bool FontMustExist
436 return fontMustExist;
440 fontMustExist = value;
444 [DefaultValue ("Color [Black]")]
449 examplePanel.Invalidate( );
458 public bool AllowSimulations
461 allowSimulations = value;
465 return allowSimulations;
470 public bool AllowVectorFonts
473 allowVectorFonts = value;
477 return allowVectorFonts;
482 public bool AllowVerticalFonts
485 allowVerticalFonts = value;
489 return allowVerticalFonts;
494 public bool AllowScriptChange
497 allowScriptChange = value;
501 return allowScriptChange;
505 [DefaultValue(false)]
506 public bool FixedPitchOnly
509 if (fixedPitchOnly != value) {
510 fixedPitchOnly = value;
516 return fixedPitchOnly;
529 if (maxSize < minSize)
532 CreateFontSizeListBoxItems ();
549 if (minSize > maxSize)
552 CreateFontSizeListBoxItems ();
554 if (minSize > currentSize)
558 currentSize = minSize;
560 font = new Font( currentFamily, currentSize, currentFontStyle );
562 UpdateExamplePanel ();
564 fontsizeTextBox.Text = currentSize.ToString ();
573 [DefaultValue(false)]
574 public bool ScriptsOnly
585 [DefaultValue(false)]
586 public bool ShowApply
589 if (value != showApply)
607 [DefaultValue(false)]
608 public bool ShowColor
611 if (value != showColor)
615 colorComboBox.Show ();
617 colorComboBox.Hide ();
629 public bool ShowEffects
632 if (value != showEffects)
636 effectsGroupBox.Show ();
638 effectsGroupBox.Hide ();
649 [DefaultValue(false)]
653 if (value != showHelp)
670 #endregion // Public Instance Properties
672 #region Protected Instance Properties
673 protected int Options {
676 #endregion // Protected Instance Properties
678 #region Public Instance Methods
679 public override void Reset( )
682 allowSimulations = true;
683 allowVectorFonts = true;
684 allowVerticalFonts = true;
685 allowScriptChange = true;
686 fixedPitchOnly = false;
690 CreateFontSizeListBoxItems ();
698 colorComboBox.Hide ();
701 effectsGroupBox.Show ();
709 public override string ToString ()
712 return base.ToString ();
713 return String.Concat (base.ToString (), ", Font: ", font.ToString ());
715 #endregion // Public Instance Methods
717 #region Protected Instance Methods
718 protected override IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
720 return base.HookProc (hWnd, msg, wparam, lparam);
723 protected override bool RunDialog( IntPtr hWndOwner )
730 internal void OnApplyButton (object sender, EventArgs e)
735 protected virtual void OnApply (EventArgs e)
737 EventHandler apply = (EventHandler) Events [EventApply];
741 #endregion // Protected Instance Methods
743 void OnClickCancelButton( object sender, EventArgs e )
745 form.DialogResult = DialogResult.Cancel;
748 void OnClickOkButton( object sender, EventArgs e )
750 form.DialogResult = DialogResult.OK;
753 void OnPaintExamplePanel( object sender, PaintEventArgs e )
755 SolidBrush brush = ThemeEngine.Current.ResPool.GetSolidBrush( color );
757 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( SystemColors.Control ), 0, 0, 156, 40 );
759 SizeF fontSizeF = e.Graphics.MeasureString( example_panel_text, font );
761 int text_width = (int)fontSizeF.Width;
762 int text_height = (int)fontSizeF.Height;
764 int x = ( examplePanel.Width / 2 ) - ( text_width / 2 );
767 int y = ( examplePanel.Height / 2 ) - ( text_height / 2 );
769 e.Graphics.DrawString( example_panel_text, font, brush, new Point( x, y ) );
772 void OnSelectedIndexChangedFontListBox( object sender, EventArgs e )
774 if ( fontListBox.SelectedIndex != -1 )
776 currentFamily = FindByName( fontListBox.Items[ fontListBox.SelectedIndex ].ToString( ) );
778 fontTextBox.Text = currentFamily.Name;
780 internal_change = true;
782 UpdateFontStyleListBox( );
784 UpdateFontSizeListBox ();
786 UpdateExamplePanel ();
788 form.Select(fontTextBox);
790 internal_change = false;
794 void OnSelectedIndexChangedSizeListBox( object sender, EventArgs e )
796 if ( fontsizeListBox.SelectedIndex != -1 )
798 currentSize = (float)System.Convert.ToDouble( fontsizeListBox.Items[ fontsizeListBox.SelectedIndex ] );
800 fontsizeTextBox.Text = currentSize.ToString( );
802 UpdateExamplePanel( );
804 if (!internal_change)
805 form.Select(fontsizeTextBox);
809 void OnSelectedIndexChangedFontStyleListBox( object sender, EventArgs e )
811 if ( fontstyleListBox.SelectedIndex != -1 )
813 switch ( fontstyleListBox.SelectedIndex )
816 currentFontStyle = FontStyle.Regular;
819 currentFontStyle = FontStyle.Bold;
822 currentFontStyle = FontStyle.Italic;
825 currentFontStyle = FontStyle.Bold | FontStyle.Italic;
828 currentFontStyle = FontStyle.Regular;
833 currentFontStyle = currentFontStyle | FontStyle.Underline;
836 currentFontStyle = currentFontStyle | FontStyle.Strikeout;
838 fontstyleTextBox.Text = fontstyleListBox.Items[ fontstyleListBox.SelectedIndex ].ToString( );
840 if (!internal_change) {
841 UpdateExamplePanel( );
843 form.Select(fontstyleTextBox);
848 void OnCheckedChangedUnderlinedCheckBox( object sender, EventArgs e )
850 if ( underlinedCheckBox.Checked ) {
851 currentFontStyle = currentFontStyle | FontStyle.Underline;
855 currentFontStyle = currentFontStyle ^ FontStyle.Underline;
859 UpdateExamplePanel( );
862 void OnCheckedChangedStrikethroughCheckBox( object sender, EventArgs e )
864 if ( strikethroughCheckBox.Checked ) {
865 currentFontStyle = currentFontStyle | FontStyle.Strikeout;
866 strikethrough = true;
869 currentFontStyle = currentFontStyle ^ FontStyle.Strikeout;
870 strikethrough = false;
873 UpdateExamplePanel( );
876 bool internal_textbox_change = false;
878 void OnFontTextBoxMouseWheel (object sender, MouseEventArgs e)
880 fontListBox.SendMouseWheelEvent (e);
883 void OnFontStyleTextBoxMouseWheel (object sender, MouseEventArgs e)
885 fontstyleListBox.SendMouseWheelEvent (e);
888 void OnFontSizeTextBoxMouseWheel (object sender, MouseEventArgs e)
890 fontsizeListBox.SendMouseWheelEvent (e);
893 void OnFontTextBoxKeyDown (object sender, KeyEventArgs e)
895 // Forward these keys on to the font style listbox
901 fontListBox.HandleKeyDown (e.KeyCode);
906 void OnFontStyleTextBoxKeyDown (object sender, KeyEventArgs e)
908 // Forward these keys on to the font style listbox
914 fontstyleListBox.HandleKeyDown (e.KeyCode);
919 void OnFontSizeTextBoxKeyDown (object sender, KeyEventArgs e)
921 // Forward these keys on to the font size listbox
927 fontsizeListBox.HandleKeyDown (e.KeyCode);
932 void OnFontTextBoxKeyPress (object sender, KeyPressEventArgs e)
934 internal_textbox_change = true;
936 if (fontListBox.SelectedIndex > -1)
937 fontListBox.SelectedIndex = -1;
940 void OnFontStyleTextBoxKeyPress (object sender, KeyPressEventArgs e)
942 internal_textbox_change = true;
944 if (fontstyleListBox.SelectedIndex > -1)
945 fontstyleListBox.SelectedIndex = -1;
948 void OnFontSizeTextBoxKeyPress (object sender, KeyPressEventArgs e)
950 if (Char.IsLetter (e.KeyChar) || Char.IsWhiteSpace (e.KeyChar) || Char.IsPunctuation (e.KeyChar) || e.KeyChar == ',') {
955 internal_textbox_change = true;
958 void OnFontTextBoxTextChanged (object sender, EventArgs e)
960 if (!internal_textbox_change)
963 internal_textbox_change = false;
965 string search = fontTextBox.Text;
967 // Look for an exact match
968 int found = fontListBox.FindStringExact (search);
970 if (found != ListBox.NoMatches) {
971 fontListBox.SelectedIndex = found;
975 // Look for a partial match
976 found = fontListBox.FindString (search);
978 if (found != ListBox.NoMatches) {
979 fontListBox.TopIndex = found;
983 // No match, scroll to the top
984 if (fontListBox.Items.Count > 0)
985 fontListBox.TopIndex = 0;
988 void OnFontStyleTextTextChanged (object sender, EventArgs e)
990 if (!internal_textbox_change)
993 internal_textbox_change = false;
995 // Look for an exact match
996 int found = fontstyleListBox.FindStringExact (fontstyleTextBox.Text);
998 if (found != ListBox.NoMatches)
999 fontstyleListBox.SelectedIndex = found;
1002 void OnFontSizeTextBoxTextChanged (object sender, EventArgs e)
1004 if (!internal_textbox_change)
1007 internal_textbox_change = false;
1009 if (fontsizeTextBox.Text.Length == 0)
1012 for (int i = 0; i < fontsizeListBox.Items.Count; i++) {
1013 string name = fontsizeListBox.Items [i] as string;
1015 if (name.StartsWith(fontsizeTextBox.Text)) {
1016 if (name == fontsizeTextBox.Text)
1017 fontsizeListBox.SelectedIndex = i;
1019 fontsizeListBox.TopIndex = i;
1026 void OnSelectedIndexChangedScriptComboBox (object sender, EventArgs e)
1028 string tmp_str = char_sets [scriptComboBox.SelectedIndex];
1030 if (tmp_str.Length > 0) {
1031 example_panel_text = tmp_str;
1033 UpdateExamplePanel ();
1037 void UpdateExamplePanel( )
1042 font = new Font( currentFamily, currentSize, currentFontStyle );
1044 examplePanel.Invalidate( );
1047 void UpdateFontSizeListBox ()
1049 int index = fontsizeListBox.FindString(((int)Math.Round ((currentSize))).ToString());
1052 fontsizeListBox.SelectedIndex = index;
1054 fontsizeListBox.SelectedIndex = 0;
1057 void UpdateFontStyleListBox( )
1059 // don't know if that works, IsStyleAvailable returns true for all styles under X
1061 fontstyleListBox.BeginUpdate( );
1063 fontstyleListBox.Items.Clear( );
1068 if ( currentFamily.IsStyleAvailable( FontStyle.Regular ) )
1070 index = fontstyleListBox.Items.Add( "Regular" );
1072 if ((currentFontStyle & FontStyle.Regular) == FontStyle.Regular)
1076 if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) )
1078 index = fontstyleListBox.Items.Add( "Bold" );
1080 if ((currentFontStyle & FontStyle.Bold) == FontStyle.Bold)
1084 if ( currentFamily.IsStyleAvailable( FontStyle.Italic ) )
1086 index = fontstyleListBox.Items.Add( "Italic" );
1088 if ((currentFontStyle & FontStyle.Italic) == FontStyle.Italic)
1092 if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) && currentFamily.IsStyleAvailable( FontStyle.Italic ) )
1094 index = fontstyleListBox.Items.Add( "Bold Italic" );
1096 if ((currentFontStyle & (FontStyle.Bold | FontStyle.Italic)) == (FontStyle.Bold | FontStyle.Italic))
1100 if (fontstyleListBox.Items.Count > 0) {
1101 fontstyleListBox.SelectedIndex = to_select;
1103 switch ((string)fontstyleListBox.SelectedItem) {
1105 currentFontStyle = FontStyle.Regular;
1108 currentFontStyle = FontStyle.Bold;
1111 currentFontStyle = FontStyle.Italic;
1114 currentFontStyle = FontStyle.Bold | FontStyle.Italic;
1118 if (strikethroughCheckBox.Checked)
1119 currentFontStyle |= FontStyle.Strikeout;
1120 if (underlinedCheckBox.Checked)
1121 currentFontStyle |= FontStyle.Underline;
1124 fontstyleListBox.EndUpdate( );
1127 FontFamily FindByName( string name )
1129 return fontHash[ name ] as FontFamily;
1132 void CreateFontSizeListBoxItems ()
1134 fontsizeListBox.BeginUpdate ();
1136 fontsizeListBox.Items.Clear();
1138 if (minSize == 0 && maxSize == 0)
1140 foreach (int i in a_sizes)
1141 fontsizeListBox.Items.Add (i.ToString());
1143 foreach (int i in a_sizes) {
1144 if (i >= minSize && i <= maxSize)
1145 fontsizeListBox.Items.Add (i.ToString());
1149 fontsizeListBox.EndUpdate ();
1152 #region Private Methods
1153 private void PopulateFontList ()
1155 fontListBox.Items.Clear ();
1158 fontListBox.BeginUpdate ();
1160 foreach (FontFamily ff in FontFamily.Families) {
1161 if (!fontHash.ContainsKey (ff.Name)) {
1162 if (!fixedPitchOnly || (IsFontFamilyFixedPitch (ff))) {
1163 fontListBox.Items.Add (ff.Name);
1164 fontHash.Add (ff.Name, ff);
1169 fontListBox.EndUpdate ();
1170 CreateFontSizeListBoxItems ();
1173 this.Font = new Font (FontFamily.GenericMonospace, 8.25f);
1175 this.Font = form.Font;
1178 private bool IsFontFamilyFixedPitch (FontFamily family)
1182 if (family.IsStyleAvailable (FontStyle.Regular))
1183 fs = FontStyle.Regular;
1184 else if (family.IsStyleAvailable (FontStyle.Bold))
1185 fs = FontStyle.Bold;
1186 else if (family.IsStyleAvailable (FontStyle.Italic))
1187 fs = FontStyle.Italic;
1188 else if (family.IsStyleAvailable (FontStyle.Strikeout))
1189 fs = FontStyle.Strikeout;
1190 else if (family.IsStyleAvailable (FontStyle.Underline))
1191 fs = FontStyle.Underline;
1195 Font f = new Font (family.Name, 10, fs);
1197 if (TextRenderer.MeasureString ("i", f).Width == TextRenderer.MeasureString ("w", f).Width)
1204 internal class ColorComboBox : ComboBox
1206 internal class ColorComboBoxItem
1208 private Color color;
1209 private string name;
1211 public ColorComboBoxItem( Color color, string name )
1239 public override string ToString()
1245 private Color selectedColor;
1247 private FontDialog fontDialog;
1249 public ColorComboBox( FontDialog fontDialog )
1251 this.fontDialog = fontDialog;
1253 DropDownStyle = ComboBoxStyle.DropDownList;
1254 DrawMode = DrawMode.OwnerDrawFixed;
1256 Items.AddRange (new object[] {
1257 new ColorComboBoxItem (Color.Black, "Black"),
1258 new ColorComboBoxItem (Color.Maroon, "Maroon"),
1259 new ColorComboBoxItem (Color.Green, "Green"),
1260 new ColorComboBoxItem (Color.Olive, "Olive"),
1261 new ColorComboBoxItem (Color.Navy, "Navy"),
1262 new ColorComboBoxItem (Color.Purple, "Purple"),
1263 new ColorComboBoxItem (Color.Teal, "Teal"),
1264 new ColorComboBoxItem (Color.Gray, "Gray"),
1265 new ColorComboBoxItem (Color.Silver, "Silver"),
1266 new ColorComboBoxItem (Color.Red, "Red"),
1267 new ColorComboBoxItem (Color.Lime, "Lime"),
1268 new ColorComboBoxItem (Color.Yellow, "Yellow"),
1269 new ColorComboBoxItem (Color.Blue, "Blue"),
1270 new ColorComboBoxItem (Color.Fuchsia, "Fuchsia"),
1271 new ColorComboBoxItem (Color.Aqua, "Aqua"),
1272 new ColorComboBoxItem (Color.White, "White") }
1276 MaxDropDownItems = 16;
1279 protected override void OnDrawItem( DrawItemEventArgs e )
1281 if ( e.Index == -1 )
1284 ColorComboBoxItem ccbi = Items[ e.Index ] as ColorComboBoxItem;
1286 Rectangle r = e.Bounds;
1289 if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected )
1291 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( Color.Blue ), e.Bounds ); // bot blue
1292 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( ccbi.Color ), e.Bounds.X + 3, e.Bounds.Y + 3, e.Bounds.X + 16, e.Bounds.Bottom - 3 );
1293 e.Graphics.DrawRectangle( ThemeEngine.Current.ResPool.GetPen( Color.Black ), e.Bounds.X + 2, e. Bounds.Y + 2, e.Bounds.X + 17, e.Bounds.Bottom - 3 );
1294 e.Graphics.DrawString( ccbi.Name, this.Font, ThemeEngine.Current.ResPool.GetSolidBrush( Color.White ), r );
1298 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( Color.White ), e.Bounds );
1299 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( ccbi.Color ), e.Bounds.X + 3, e.Bounds.Y + 3, e.Bounds.X + 16, e.Bounds.Bottom - 3 );
1300 e.Graphics.DrawRectangle( ThemeEngine.Current.ResPool.GetPen( Color.Black ), e.Bounds.X + 2, e. Bounds.Y + 2, e.Bounds.X + 17, e.Bounds.Bottom - 3 );
1301 e.Graphics.DrawString( ccbi.Name, this.Font, ThemeEngine.Current.ResPool.GetSolidBrush( Color.Black ), r );
1305 protected override void OnSelectedIndexChanged( EventArgs e )
1307 ColorComboBoxItem ccbi = Items[ SelectedIndex ] as ColorComboBoxItem;
1308 selectedColor = ccbi.Color;
1310 fontDialog.Color = selectedColor;
1314 public event EventHandler Apply {
1315 add { Events.AddHandler (EventApply, value); }
1316 remove { Events.RemoveHandler (EventApply, value); }
1320 internal class MouseWheelListBox : ListBox
1322 public void SendMouseWheelEvent(MouseEventArgs e)