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) 2004 Novell, Inc. (http://www.novell.com)
23 // Alexander Olk xenomorph2@onlinehome.de
27 // NOT COMPLETE - work in progress
30 // - select values for font/style/size via the TextBoxes
33 using System.ComponentModel;
35 using System.Text.RegularExpressions;
37 namespace System.Windows.Forms
39 [DefaultProperty( "Font" )]
40 [DefaultEvent("Apply")]
41 public class FontDialog : CommonDialog
43 private FontDialogPanel fontDialogPanel;
46 private Color color = Color.Black;
47 private bool allowSimulations = true;
48 private bool allowVectorFonts = true;
49 private bool allowVerticalFonts = true;
50 private bool allowScriptChange = true;
51 private bool fixedPitchOnly = false;
52 private int maxSize = 0;
53 private int minSize = 0;
54 private bool scriptsOnly = false;
55 private bool showApply = false;
56 private bool showColor = false;
57 private bool showEffects = true;
58 private bool showHelp = false;
60 private bool fontMustExist = false;
62 #region Public Constructors
65 form.ClientSize = new Size( 430, 318 );
67 form.Size = new Size( 430, 318 );
71 fontDialogPanel = new FontDialogPanel( this );
73 #endregion // Public Constructors
75 #region Public Instance Properties
88 public bool FontMustExist
95 fontMustExist = value;
111 public bool AllowSimulations
114 allowSimulations = value;
118 return allowSimulations;
123 public bool AllowVectorFonts
126 allowVectorFonts = value;
130 return allowVectorFonts;
135 public bool AllowVerticalFonts
138 allowVerticalFonts = value;
142 return allowVerticalFonts;
147 public bool AllowScriptChange
150 allowScriptChange = value;
154 return allowScriptChange;
158 [DefaultValue(false)]
159 public bool FixedPitchOnly
162 fixedPitchOnly = value;
166 return fixedPitchOnly;
194 [DefaultValue(false)]
195 public bool ScriptsOnly
206 [DefaultValue(false)]
207 public bool ShowApply
218 [DefaultValue(false)]
219 public bool ShowColor
231 public bool ShowEffects
242 [DefaultValue(false)]
254 #endregion // Public Instance Properties
256 #region Protected Instance Properties
257 #endregion // Protected Instance Properties
259 #region Public Instance Methods
261 public override void Reset( )
264 allowSimulations = true;
265 allowVectorFonts = true;
266 allowVerticalFonts = true;
267 allowScriptChange = true;
268 fixedPitchOnly = false;
277 #endregion // Public Instance Methods
279 #region Protected Instance Methods
281 protected override bool RunDialog( IntPtr hwndOwner )
283 form.Controls.Add( fontDialogPanel );
287 #endregion // Protected Instance Methods
290 internal class FontDialogPanel : Panel
292 private Panel examplePanel;
294 private Button okButton;
295 private Button cancelButton;
296 private Button applyButton;
297 private Button helpButton;
299 private TextBox fontTextBox;
300 private TextBox fontstyleTextBox;
301 private TextBox sizeTextBox;
303 private ListBox fontListBox;
304 private ListBox fontstyleListBox;
305 private ListBox sizeListBox;
307 private GroupBox effectsGroupBox;
308 private CheckBox strikethroughCheckBox;
309 private CheckBox underlinedCheckBox;
310 private ComboBox scriptComboBox;
312 private Label fontLabel;
313 private Label fontstyleLabel;
314 private Label sizeLabel;
315 private Label scriptLabel;
317 private GroupBox exampleGroupBox;
319 private ColorComboBox colorComboBox;
321 private FontFamily[] fontFamilies;
323 private string currentFontName;
325 private Font currentFont;
327 private int currentSize;
329 private FontFamily currentFamily;
331 private Color currentColor;
333 private FontStyle currentFontStyle;
335 private FontDialog fontDialog;
337 private System.Collections.ArrayList fontStyleArray = new System.Collections.ArrayList();
339 private System.Collections.Hashtable fontHash = new System.Collections.Hashtable();
341 public FontDialogPanel( FontDialog fontDialog )
343 this.fontDialog = fontDialog;
345 okButton = new Button( );
346 cancelButton = new Button( );
347 applyButton = new Button( );
348 helpButton = new Button( );
350 fontTextBox = new TextBox( );
351 fontstyleTextBox = new TextBox( );
352 sizeTextBox = new TextBox( );
354 fontListBox = new ListBox( );
355 sizeListBox = new ListBox( );
357 fontLabel = new Label( );
358 fontstyleLabel = new Label( );
359 sizeLabel = new Label( );
360 scriptLabel = new Label( );
362 exampleGroupBox = new GroupBox( );
363 fontstyleListBox = new ListBox( );
365 effectsGroupBox = new GroupBox( );
366 underlinedCheckBox = new CheckBox( );
367 strikethroughCheckBox = new CheckBox( );
368 scriptComboBox = new ComboBox( );
370 examplePanel = new Panel( );
372 colorComboBox = new ColorComboBox( this );
374 exampleGroupBox.SuspendLayout( );
375 effectsGroupBox.SuspendLayout( );
379 sizeListBox.Location = new Point( 284, 47 );
380 sizeListBox.Size = new Size( 52, 95 );
381 sizeListBox.TabIndex = 10;
383 fontTextBox.Location = new Point( 16, 26 );
384 fontTextBox.Size = new Size( 140, 21 );
385 fontTextBox.TabIndex = 5;
386 fontTextBox.Text = "";
388 fontstyleLabel.Location = new Point( 164, 10 );
389 fontstyleLabel.Size = new Size( 100, 16 );
390 fontstyleLabel.TabIndex = 1;
391 fontstyleLabel.Text = "Font Style:";
393 sizeTextBox.Location = new Point( 284, 26 );
394 sizeTextBox.Size = new Size( 52, 21 );
395 sizeTextBox.TabIndex = 7;
396 sizeTextBox.Text = "";
398 fontListBox.Location = new Point( 16, 47 );
399 fontListBox.Size = new Size( 140, 95 );
400 fontListBox.TabIndex = 8;
401 fontListBox.Sorted = true;
403 exampleGroupBox.Controls.Add( examplePanel );
404 exampleGroupBox.FlatStyle = FlatStyle.System;
405 exampleGroupBox.Location = new Point( 164, 158 );
406 exampleGroupBox.Size = new Size( 172, 70 );
407 exampleGroupBox.TabIndex = 12;
408 exampleGroupBox.TabStop = false;
409 exampleGroupBox.Text = "Example";
411 fontstyleListBox.Location = new Point( 164, 47 );
412 fontstyleListBox.Size = new Size( 112, 95 );
413 fontstyleListBox.TabIndex = 9;
415 fontLabel.Location = new Point( 16, 10 );
416 fontLabel.Size = new Size( 88, 16 );
417 fontLabel.TabIndex = 0;
418 fontLabel.Text = "Font:";
420 effectsGroupBox.Controls.Add( underlinedCheckBox );
421 effectsGroupBox.Controls.Add( strikethroughCheckBox );
422 effectsGroupBox.Controls.Add( colorComboBox );
423 effectsGroupBox.FlatStyle = FlatStyle.System;
424 effectsGroupBox.Location = new Point( 16, 158 );
425 effectsGroupBox.Size = new Size( 140, 116 );
426 effectsGroupBox.TabIndex = 11;
427 effectsGroupBox.TabStop = false;
428 effectsGroupBox.Text = "Effects";
429 // strikethroughCheckBox
430 strikethroughCheckBox.FlatStyle = FlatStyle.System;
431 strikethroughCheckBox.Location = new Point( 8, 16 );
432 strikethroughCheckBox.TabIndex = 0;
433 strikethroughCheckBox.Text = "Strikethrough";
435 colorComboBox.Location = new Point( 8, 70 );
436 colorComboBox.Size = new Size( 130, 21 );
438 sizeLabel.Location = new Point( 284, 10 );
439 sizeLabel.Size = new Size( 100, 16 );
440 sizeLabel.TabIndex = 2;
441 sizeLabel.Text = "Size:";
443 scriptComboBox.Location = new Point( 164, 253 );
444 scriptComboBox.Size = new Size( 172, 21 );
445 scriptComboBox.TabIndex = 14;
446 scriptComboBox.Text = "-/-";
448 okButton.FlatStyle = FlatStyle.System;
449 okButton.Location = new Point( 352, 26 );
450 okButton.Size = new Size( 70, 23 );
451 okButton.TabIndex = 3;
452 okButton.Text = "OK";
454 cancelButton.FlatStyle = FlatStyle.System;
455 cancelButton.Location = new Point( 352, 52 );
456 cancelButton.Size = new Size( 70, 23 );
457 cancelButton.TabIndex = 4;
458 cancelButton.Text = "Cancel";
460 applyButton.FlatStyle = FlatStyle.System;
461 applyButton.Location = new Point( 352, 78 );
462 applyButton.Size = new Size( 70, 23 );
463 applyButton.TabIndex = 5;
464 applyButton.Text = "Apply";
466 helpButton.FlatStyle = FlatStyle.System;
467 helpButton.Location = new Point( 352, 104 );
468 helpButton.Size = new Size( 70, 23 );
469 helpButton.TabIndex = 6;
470 helpButton.Text = "Help";
471 // underlinedCheckBox
472 underlinedCheckBox.FlatStyle = FlatStyle.System;
473 underlinedCheckBox.Location = new Point( 8, 36 );
474 underlinedCheckBox.TabIndex = 1;
475 underlinedCheckBox.Text = "Underlined";
477 fontstyleTextBox.Location = new Point( 164, 26 );
478 fontstyleTextBox.Size = new Size( 112, 21 );
479 fontstyleTextBox.TabIndex = 6;
480 fontstyleTextBox.Text = "";
482 scriptLabel.Location = new Point( 164, 236 );
483 scriptLabel.Size = new Size( 100, 16 );
484 scriptLabel.TabIndex = 13;
485 scriptLabel.Text = "Script:";
487 examplePanel.Location = new Point( 8, 20 );
488 examplePanel.TabIndex = 0;
489 examplePanel.BorderStyle = BorderStyle.Fixed3D;
490 examplePanel.Size = new Size( 156, 40 );
492 ClientSize = new Size( 430, 318 );
494 Controls.Add( scriptComboBox );
495 Controls.Add( scriptLabel );
496 Controls.Add( exampleGroupBox );
497 Controls.Add( effectsGroupBox );
498 Controls.Add( sizeListBox );
499 Controls.Add( fontstyleListBox );
500 Controls.Add( fontListBox );
501 Controls.Add( sizeTextBox );
502 Controls.Add( fontstyleTextBox );
503 Controls.Add( fontTextBox );
504 Controls.Add( cancelButton );
505 Controls.Add( okButton );
506 Controls.Add( sizeLabel );
507 Controls.Add( fontstyleLabel );
508 Controls.Add( fontLabel );
509 Controls.Add( applyButton );
510 Controls.Add( helpButton );
512 exampleGroupBox.ResumeLayout( false );
513 effectsGroupBox.ResumeLayout( false );
515 ResumeLayout( false );
517 fontFamilies = FontFamily.Families;
519 fontListBox.BeginUpdate( );
520 foreach ( FontFamily ff in fontFamilies )
522 fontListBox.Items.Add( ff.Name );
523 fontHash.Add( ff.Name, ff );
525 fontListBox.EndUpdate( );
527 fontListBox.SelectedIndex = 0;
529 // TODO: If Font is provided via FontDialog.Font property set correct font in FontListBox
530 currentFontName = fontListBox.Items[ 0 ].ToString( );
531 fontTextBox.Text = currentFontName;
536 currentFamily = FindByName( currentFontName );
538 currentFontStyle = FontStyle.Regular;
540 currentFont = new Font( currentFamily, currentSize, currentFontStyle );
542 currentColor = fontDialog.Color;
544 UpdateFontStyleListBox( );
546 fontstyleTextBox.Text = "Regular";
548 fontstyleListBox.SelectedIndex = 0 ;
550 sizeTextBox.Text = currentSize.ToString( );
552 sizeListBox.Items.AddRange( new object[] {
570 sizeListBox.SelectedIndex = 4;
572 if ( !fontDialog.ShowApply )
574 if ( !fontDialog.ShowHelp )
576 if ( !fontDialog.ShowEffects )
577 effectsGroupBox.Hide( );
578 if ( !fontDialog.ShowColor )
579 colorComboBox.Hide( );
581 cancelButton.Click += new EventHandler( OnClickCancelButton );
582 okButton.Click += new EventHandler( OnClickOkButton );
583 examplePanel.Paint += new PaintEventHandler( OnPaintExamplePanel );
584 fontListBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedFontListBox );
585 sizeListBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedSizeListBox );
586 fontstyleListBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedFontStyleListBox );
587 underlinedCheckBox.CheckedChanged += new EventHandler( OnCheckedChangedUnderlinedCheckBox );
588 strikethroughCheckBox.CheckedChanged += new EventHandler( OnCheckedChangedStrikethroughCheckBox );
591 public Color CurrentColor
594 currentColor = value;
595 examplePanel.Invalidate( );
603 private void UpdateFontStyleListBox( )
605 // don't know if that works, IsStyleAvailable returns true for all styles under X
607 fontStyleArray.Clear( );
609 fontstyleListBox.BeginUpdate( );
611 fontstyleListBox.Items.Clear( );
613 if ( currentFamily.IsStyleAvailable( FontStyle.Regular ) )
615 fontstyleListBox.Items.Add( "Regular" );
616 fontStyleArray.Add( 0 );
619 if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) )
621 fontstyleListBox.Items.Add( "Bold" );
622 fontStyleArray.Add( 1 );
625 if ( currentFamily.IsStyleAvailable( FontStyle.Italic ) )
627 fontstyleListBox.Items.Add( "Italic" );
628 fontStyleArray.Add( 2 );
631 if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) && currentFamily.IsStyleAvailable( FontStyle.Italic ) )
633 fontstyleListBox.Items.Add( "Bold Italic" );
634 fontStyleArray.Add( 3 );
637 fontstyleListBox.EndUpdate( );
640 private FontFamily FindByName( string name )
642 return fontHash[ name ] as FontFamily;
645 void OnClickCancelButton( object sender, EventArgs e )
647 fontDialog.form.Controls.Remove( this );
648 fontDialog.form.DialogResult = DialogResult.Cancel;
651 void OnClickOkButton( object sender, EventArgs e )
653 fontDialog.form.Controls.Remove( this );
654 fontDialog.Font = currentFont;
655 fontDialog.Color = currentColor;
656 fontDialog.form.DialogResult = DialogResult.OK;
659 void OnPaintExamplePanel( object sender, PaintEventArgs e )
661 SolidBrush brush = ThemeEngine.Current.ResPool.GetSolidBrush( currentColor );
663 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( SystemColors.Control ), 0, 0, 156, 40 );
665 string text = "AaBbYyZz";
667 SizeF fontSizeF = e.Graphics.MeasureString( text, currentFont );
669 int text_width = (int)fontSizeF.Width;
670 int text_height = (int)fontSizeF.Height;
672 int x = ( examplePanel.Width / 2 ) - ( text_width / 2 );
675 int y = ( examplePanel.Height / 2 ) - ( text_height / 2 );
677 e.Graphics.DrawString( text, currentFont, brush, new Point( x, y ) );
680 void OnSelectedIndexChangedFontListBox( object sender, EventArgs e )
682 if ( fontListBox.SelectedIndex != -1 )
684 currentFamily = FindByName( fontListBox.Items[ fontListBox.SelectedIndex ].ToString( ) );
686 fontTextBox.Text = currentFamily.Name;
688 UpdateFontStyleListBox( );
690 UpdateExamplePanel( );
694 void OnSelectedIndexChangedSizeListBox( object sender, EventArgs e )
696 if ( sizeListBox.SelectedIndex != -1 )
698 currentSize = System.Convert.ToInt32( sizeListBox.Items[ sizeListBox.SelectedIndex ] );
700 sizeTextBox.Text = currentSize.ToString( );
702 UpdateExamplePanel( );
706 void OnSelectedIndexChangedFontStyleListBox( object sender, EventArgs e )
708 if ( fontstyleListBox.SelectedIndex != -1 )
710 switch ( (int)fontStyleArray[ fontstyleListBox.SelectedIndex ] )
713 currentFontStyle = FontStyle.Regular;
716 currentFontStyle = FontStyle.Bold;
719 currentFontStyle = FontStyle.Italic;
722 currentFontStyle = FontStyle.Bold | FontStyle.Italic;
725 currentFontStyle = FontStyle.Regular;
729 fontstyleTextBox.Text = fontstyleListBox.Items[ fontstyleListBox.SelectedIndex ].ToString( );
731 UpdateExamplePanel( );
735 void OnCheckedChangedUnderlinedCheckBox( object sender, EventArgs e )
737 if ( underlinedCheckBox.Checked )
738 currentFontStyle = currentFontStyle | FontStyle.Underline;
740 currentFontStyle = currentFontStyle ^ FontStyle.Underline;
742 UpdateExamplePanel( );
745 void OnCheckedChangedStrikethroughCheckBox( object sender, EventArgs e )
747 if ( strikethroughCheckBox.Checked )
748 currentFontStyle = currentFontStyle | FontStyle.Strikeout;
750 currentFontStyle = currentFontStyle ^ FontStyle.Strikeout;
752 UpdateExamplePanel( );
755 private void UpdateExamplePanel( )
757 currentFont = new Font( currentFamily, currentSize, currentFontStyle );
759 examplePanel.Invalidate( );
760 examplePanel.Update( );
763 internal class ColorComboBox : ComboBox
765 internal class ColorComboBoxItem
770 public ColorComboBoxItem( Color color, string name )
799 private Color selectedColor;
801 private FontDialogPanel fontDialogPanel;
803 // FIXME: TextBox backcolor shouldn't be the same as the selected item in the ListBox/ListCtrl
805 public ColorComboBox( FontDialogPanel fontDialogPanel )
807 this.fontDialogPanel = fontDialogPanel;
809 DropDownStyle = ComboBoxStyle.DropDownList;
810 DrawMode = DrawMode.OwnerDrawFixed;
812 Items.AddRange( new object[] {
813 new ColorComboBoxItem( Color.Black, "Black" ),
814 new ColorComboBoxItem( Color.DarkRed, "Dark-Red" ),
815 new ColorComboBoxItem( Color.Green, "Green" ),
816 new ColorComboBoxItem( Color.Olive, "Olive-Green" ), // color not correct
817 new ColorComboBoxItem( Color.Aquamarine, "Aquamarine" ), // color not correct
818 new ColorComboBoxItem( Color.Crimson, "Crimson" ),
819 new ColorComboBoxItem( Color.Cyan, "Cyan" ),
820 new ColorComboBoxItem( Color.Gray, "Gray" ),
821 new ColorComboBoxItem( Color.Silver, "Silver" ),
822 new ColorComboBoxItem( Color.Red, "Red" ),
823 new ColorComboBoxItem( Color.YellowGreen, "Yellow-Green" ),
824 new ColorComboBoxItem( Color.Yellow, "Yellow" ),
825 new ColorComboBoxItem( Color.Blue, "Blue" ),
826 new ColorComboBoxItem( Color.Purple, "Purple" ),
827 new ColorComboBoxItem( Color.Aquamarine, "Aquamarine" ),
828 new ColorComboBoxItem( Color.White, "White" ) }
834 protected override void OnDrawItem( DrawItemEventArgs e )
839 ColorComboBoxItem ccbi = Items[ e.Index ] as ColorComboBoxItem;
841 Rectangle r = e.Bounds;
844 if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected )
846 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( Color.Blue ), e.Bounds ); // bot blue
847 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( ccbi.Color ), e.Bounds.X + 3, e.Bounds.Y + 3, e.Bounds.X + 16, e.Bounds.Y + e.Bounds.Height - 2 );
848 e.Graphics.DrawRectangle( ThemeEngine.Current.ResPool.GetPen( Color.Black ), e.Bounds.X + 2, e. Bounds.Y + 2, e.Bounds.X + 17, e.Bounds.Y + e.Bounds.Height - 1 );
849 e.Graphics.DrawString( ccbi.Name, this.Font, ThemeEngine.Current.ResPool.GetSolidBrush( Color.White ), r );
853 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( Color.White ), e.Bounds );
854 e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( ccbi.Color ), e.Bounds.X + 3, e.Bounds.Y + 3, e.Bounds.X + 16, e.Bounds.Y + e.Bounds.Height - 2 );
855 e.Graphics.DrawRectangle( ThemeEngine.Current.ResPool.GetPen( Color.Black ), e.Bounds.X + 2, e. Bounds.Y + 2, e.Bounds.X + 17, e.Bounds.Y + e.Bounds.Height - 1 );
856 e.Graphics.DrawString( ccbi.Name, this.Font, ThemeEngine.Current.ResPool.GetSolidBrush( Color.Black ), r );
860 protected override void OnSelectedIndexChanged( EventArgs e )
862 ColorComboBoxItem ccbi = Items[ SelectedIndex ] as ColorComboBoxItem;
863 selectedColor = ccbi.Color;
865 fontDialogPanel.CurrentColor = selectedColor;