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 // - correct drawing of the selected font in examplePanel
31 // - select values for font/style/size via the TextBoxes
34 using System.ComponentModel;
37 namespace System.Windows.Forms
39 [DefaultProperty( "Font" )]
40 public class FontDialog : CommonDialog
42 private FontDialogPanel fontDialogPanel;
45 private Color color = Color.Black;
46 private bool allowSimulations = true;
47 private bool allowVectorFonts = true;
48 private bool allowVerticalFonts = true;
49 private bool allowScriptChange = true;
50 private bool fixedPitchOnly = false;
51 private int maxSize = 0;
52 private int minSize = 0;
53 private bool scriptsOnly = false;
54 private bool showApply = false;
55 private bool showColor = false;
56 private bool showEffects = true;
57 private bool showHelp = false;
59 private bool fontMustExist = false;
61 #region Public Constructors
64 form.ClientSize = new Size( 430, 318 );
66 form.Size = new Size( 430, 318 );
70 fontDialogPanel = new FontDialogPanel( this );
72 #endregion // Public Constructors
74 #region Public Instance Properties
87 public bool FontMustExist
94 fontMustExist = value;
98 [DefaultValue("Black")]
111 public bool AllowSimulations
115 allowSimulations = value;
120 return allowSimulations;
125 public bool AllowVectorFonts
129 allowVectorFonts = value;
134 return allowVectorFonts;
139 public bool AllowVerticalFonts
143 allowVerticalFonts = value;
148 return allowVerticalFonts;
153 public bool AllowScriptChange
157 allowScriptChange = value;
162 return allowScriptChange;
166 [DefaultValue(false)]
167 public bool FixedPitchOnly
171 fixedPitchOnly = value;
176 return fixedPitchOnly;
208 [DefaultValue(false)]
209 public bool ScriptsOnly
222 [DefaultValue(false)]
223 public bool ShowApply
236 [DefaultValue(false)]
237 public bool ShowColor
251 public bool ShowEffects
264 [DefaultValue(false)]
278 #endregion // Public Instance Properties
280 #region Protected Instance Properties
281 #endregion // Protected Instance Properties
283 #region Public Instance Methods
285 public override void Reset( )
288 allowSimulations = true;
289 allowVectorFonts = true;
290 allowVerticalFonts = true;
291 allowScriptChange = true;
292 fixedPitchOnly = false;
301 #endregion // Public Instance Methods
303 #region Protected Instance Methods
305 protected override bool RunDialog( IntPtr hwndOwner )
307 form.Controls.Add( fontDialogPanel );
311 #endregion // Protected Instance Methods
314 internal class FontDialogPanel : Panel
316 private Panel examplePanel;
318 private Button okButton;
319 private Button cancelButton;
320 private Button applyButton;
321 private Button helpButton;
323 private TextBox fontTextBox;
324 private TextBox fontstyleTextBox;
325 private TextBox sizeTextBox;
327 private ListBox fontListBox;
328 private ListBox fontstyleListBox;
329 private ListBox sizeListBox;
331 private GroupBox effectsGroupBox;
332 private CheckBox strikethroughCheckBox;
333 private CheckBox underlinedCheckBox;
334 private ComboBox scriptComboBox;
336 private Label fontLabel;
337 private Label fontstyleLabel;
338 private Label sizeLabel;
339 private Label scriptLabel;
341 private GroupBox exampleGroupBox;
343 private ColorComboBox colorComboBox;
345 private FontFamily[] fontFamilies;
347 private string currentFontName;
349 private Font currentFont;
351 private int currentSize;
353 private FontFamily currentFamily;
355 private Color currentColor;
357 private FontStyle currentFontStyle;
359 private FontDialog fontDialog;
361 private System.Collections.ArrayList fontStyleArray = new System.Collections.ArrayList();
363 private System.Collections.Hashtable fontHash = new System.Collections.Hashtable();
368 /// <param name="fontDialog">A FontDialog</param>
369 public FontDialogPanel( FontDialog fontDialog )
371 this.fontDialog = fontDialog;
373 okButton = new Button( );
374 cancelButton = new Button( );
375 applyButton = new Button( );
376 helpButton = new Button( );
378 fontTextBox = new TextBox( );
379 fontstyleTextBox = new TextBox( );
380 sizeTextBox = new TextBox( );
382 fontListBox = new ListBox( );
383 sizeListBox = new ListBox( );
385 fontLabel = new Label( );
386 fontstyleLabel = new Label( );
387 sizeLabel = new Label( );
388 scriptLabel = new Label( );
390 exampleGroupBox = new GroupBox( );
391 fontstyleListBox = new ListBox( );
393 effectsGroupBox = new GroupBox( );
394 underlinedCheckBox = new CheckBox( );
395 strikethroughCheckBox = new CheckBox( );
396 scriptComboBox = new ComboBox( );
398 examplePanel = new Panel( );
400 colorComboBox = new ColorComboBox( this );
402 exampleGroupBox.SuspendLayout( );
403 effectsGroupBox.SuspendLayout( );
407 sizeListBox.Location = new Point( 284, 47 );
408 sizeListBox.Size = new Size( 52, 95 );
409 sizeListBox.TabIndex = 10;
411 fontTextBox.Location = new Point( 16, 26 );
412 fontTextBox.Size = new Size( 140, 21 );
413 fontTextBox.TabIndex = 5;
414 fontTextBox.Text = "";
416 fontstyleLabel.Location = new Point( 164, 10 );
417 fontstyleLabel.Size = new Size( 100, 16 );
418 fontstyleLabel.TabIndex = 1;
419 fontstyleLabel.Text = "Font Style:";
421 sizeTextBox.Location = new Point( 284, 26 );
422 sizeTextBox.Size = new Size( 52, 21 );
423 sizeTextBox.TabIndex = 7;
424 sizeTextBox.Text = "";
426 fontListBox.Location = new Point( 16, 47 );
427 fontListBox.Size = new Size( 140, 95 );
428 fontListBox.TabIndex = 8;
429 fontListBox.Sorted = true;
431 exampleGroupBox.Controls.Add( examplePanel );
432 exampleGroupBox.FlatStyle = FlatStyle.System;
433 exampleGroupBox.Location = new Point( 164, 158 );
434 exampleGroupBox.Size = new Size( 172, 70 );
435 exampleGroupBox.TabIndex = 12;
436 exampleGroupBox.TabStop = false;
437 exampleGroupBox.Text = "Example";
439 fontstyleListBox.Location = new Point( 164, 47 );
440 fontstyleListBox.Size = new Size( 112, 95 );
441 fontstyleListBox.TabIndex = 9;
443 fontLabel.Location = new Point( 16, 10 );
444 fontLabel.Size = new Size( 88, 16 );
445 fontLabel.TabIndex = 0;
446 fontLabel.Text = "Font:";
448 effectsGroupBox.Controls.Add( underlinedCheckBox );
449 effectsGroupBox.Controls.Add( strikethroughCheckBox );
450 effectsGroupBox.Controls.Add( colorComboBox );
451 effectsGroupBox.FlatStyle = FlatStyle.System;
452 effectsGroupBox.Location = new Point( 16, 158 );
453 effectsGroupBox.Size = new Size( 140, 116 );
454 effectsGroupBox.TabIndex = 11;
455 effectsGroupBox.TabStop = false;
456 effectsGroupBox.Text = "Effects";
457 // strikethroughCheckBox
458 strikethroughCheckBox.FlatStyle = FlatStyle.System;
459 strikethroughCheckBox.Location = new Point( 8, 16 );
460 strikethroughCheckBox.TabIndex = 0;
461 strikethroughCheckBox.Text = "Strikethrough";
463 colorComboBox.Location = new Point( 8, 70 );
464 colorComboBox.Size = new Size( 130, 21 );
466 sizeLabel.Location = new Point( 284, 10 );
467 sizeLabel.Size = new Size( 100, 16 );
468 sizeLabel.TabIndex = 2;
469 sizeLabel.Text = "Size:";
471 scriptComboBox.Location = new Point( 164, 253 );
472 scriptComboBox.Size = new Size( 172, 21 );
473 scriptComboBox.TabIndex = 14;
474 scriptComboBox.Text = "-/-";
476 okButton.FlatStyle = FlatStyle.System;
477 okButton.Location = new Point( 352, 26 );
478 okButton.Size = new Size( 70, 23 );
479 okButton.TabIndex = 3;
480 okButton.Text = "OK";
482 cancelButton.FlatStyle = FlatStyle.System;
483 cancelButton.Location = new Point( 352, 52 );
484 cancelButton.Size = new Size( 70, 23 );
485 cancelButton.TabIndex = 4;
486 cancelButton.Text = "Cancel";
488 applyButton.FlatStyle = FlatStyle.System;
489 applyButton.Location = new Point( 352, 78 );
490 applyButton.Size = new Size( 70, 23 );
491 applyButton.TabIndex = 5;
492 applyButton.Text = "Apply";
494 helpButton.FlatStyle = FlatStyle.System;
495 helpButton.Location = new Point( 352, 104 );
496 helpButton.Size = new Size( 70, 23 );
497 helpButton.TabIndex = 6;
498 helpButton.Text = "Help";
499 // underlinedCheckBox
500 underlinedCheckBox.FlatStyle = FlatStyle.System;
501 underlinedCheckBox.Location = new Point( 8, 36 );
502 underlinedCheckBox.TabIndex = 1;
503 underlinedCheckBox.Text = "Underlined";
505 fontstyleTextBox.Location = new Point( 164, 26 );
506 fontstyleTextBox.Size = new Size( 112, 21 );
507 fontstyleTextBox.TabIndex = 6;
508 fontstyleTextBox.Text = "";
510 scriptLabel.Location = new Point( 164, 236 );
511 scriptLabel.Size = new Size( 100, 16 );
512 scriptLabel.TabIndex = 13;
513 scriptLabel.Text = "Script:";
515 examplePanel.Location = new Point( 8, 20 );
516 examplePanel.TabIndex = 0;
517 examplePanel.BorderStyle = BorderStyle.Fixed3D;
518 examplePanel.Size = new Size( 156, 40 );
520 ClientSize = new Size( 430, 318 );
522 Controls.Add( scriptComboBox );
523 Controls.Add( scriptLabel );
524 Controls.Add( exampleGroupBox );
525 Controls.Add( effectsGroupBox );
526 Controls.Add( sizeListBox );
527 Controls.Add( fontstyleListBox );
528 Controls.Add( fontListBox );
529 Controls.Add( sizeTextBox );
530 Controls.Add( fontstyleTextBox );
531 Controls.Add( fontTextBox );
532 Controls.Add( cancelButton );
533 Controls.Add( okButton );
534 Controls.Add( sizeLabel );
535 Controls.Add( fontstyleLabel );
536 Controls.Add( fontLabel );
537 Controls.Add( applyButton );
538 Controls.Add( helpButton );
540 exampleGroupBox.ResumeLayout( false );
541 effectsGroupBox.ResumeLayout( false );
543 ResumeLayout( false );
545 fontFamilies = FontFamily.Families;
547 fontListBox.BeginUpdate( );
548 foreach ( FontFamily ff in fontFamilies )
550 fontListBox.Items.Add( ff.Name );
551 fontHash.Add( ff.Name, ff );
553 fontListBox.EndUpdate( );
555 // TODO: If Font is provided via FontDialog.Font property set correct font in FontListBox
556 currentFontName = fontListBox.Items[ 0 ].ToString( );
557 fontTextBox.Text = currentFontName;
562 currentFamily = FindByName( currentFontName );
564 currentFontStyle = FontStyle.Regular;
566 currentFont = new Font( currentFamily, currentSize, currentFontStyle );
568 currentColor = fontDialog.Color;
570 UpdateFontStyleListBox();
572 fontstyleTextBox.Text = "Regular";
574 sizeTextBox.Text = currentSize.ToString( );
576 sizeListBox.Items.AddRange( new object[] {
594 sizeListBox.SelectedIndex = 0;
596 if ( !fontDialog.ShowApply )
598 if ( !fontDialog.ShowHelp )
600 if ( !fontDialog.ShowEffects )
601 effectsGroupBox.Hide();
602 if ( !fontDialog.ShowColor )
603 colorComboBox.Hide();
605 cancelButton.Click += new EventHandler( OnClickCancelButton );
606 okButton.Click += new EventHandler( OnClickOkButton );
607 examplePanel.Paint += new PaintEventHandler( OnPaintExamplePanel );
608 fontListBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedFontListBox );
609 sizeListBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedSizeListBox );
610 fontstyleListBox.SelectedIndexChanged += new EventHandler( OnSelectedIndexChangedFontStyleListBox );
611 underlinedCheckBox.CheckedChanged += new EventHandler( OnCheckedChangedUnderlinedCheckBox );
612 strikethroughCheckBox.CheckedChanged += new EventHandler( OnCheckedChangedStrikethroughCheckBox );
615 public Color CurrentColor
619 currentColor = value;
620 examplePanel.Invalidate();
629 private void UpdateFontStyleListBox( )
631 // don't know if that works, IsStyleAvailable returns true for all styles under X
633 fontStyleArray.Clear();
635 fontstyleListBox.BeginUpdate( );
637 fontstyleListBox.Items.Clear();
639 if ( currentFamily.IsStyleAvailable( FontStyle.Regular ) )
641 fontstyleListBox.Items.Add( "Regular" );
642 fontStyleArray.Add(0);
645 if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) )
647 fontstyleListBox.Items.Add( "Bold" );
648 fontStyleArray.Add(1);
651 if ( currentFamily.IsStyleAvailable( FontStyle.Italic ) )
653 fontstyleListBox.Items.Add( "Italic" );
654 fontStyleArray.Add(2);
657 if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) && currentFamily.IsStyleAvailable( FontStyle.Italic ) )
659 fontstyleListBox.Items.Add( "Bold Italic" );
660 fontStyleArray.Add(3);
663 fontstyleListBox.EndUpdate( );
666 private FontFamily FindByName( string name )
668 return fontHash[ name ] as FontFamily;
671 void OnClickCancelButton( object sender, EventArgs e )
673 fontDialog.form.Controls.Remove( this );
674 fontDialog.form.DialogResult = DialogResult.Cancel;
677 void OnClickOkButton( object sender, EventArgs e )
679 fontDialog.form.Controls.Remove( this );
680 fontDialog.Font = currentFont;
681 fontDialog.Color = currentColor;
682 fontDialog.form.DialogResult = DialogResult.OK;
685 void OnPaintExamplePanel( object sender, PaintEventArgs e )
687 SolidBrush brush = new SolidBrush( currentColor );
689 // FIXME: need to find a better algorithm to display the font
691 int x = ( examplePanel.Width / 2 ) - ( currentSize * 4 );
692 int y = ( examplePanel.Height / 2 ) - ( currentFont.Height / 2 );
697 e.Graphics.DrawString( "AaBbYyZz", currentFont, brush, new Point( x, y ) );
699 // StringFormat strformat = new StringFormat();
700 // strformat.Alignment = StringAlignment.Center;
701 // strformat.Alignment = StringAlignment.Center;
703 // e.Graphics.DrawString ("AaBbYyZz", currentFont, brush, new Rectangle(0, 0, examplePanel.Width, examplePanel.Height), strformat);
706 void OnSelectedIndexChangedFontListBox( object sender, EventArgs e )
708 if ( fontListBox.SelectedIndex != -1 )
710 currentFamily = FindByName( fontListBox.Items[ fontListBox.SelectedIndex ].ToString( ) );
712 fontTextBox.Text = currentFamily.Name;
714 UpdateFontStyleListBox();
716 UpdateExamplePanel( );
720 void OnSelectedIndexChangedSizeListBox( object sender, EventArgs e )
722 if ( sizeListBox.SelectedIndex != -1 )
724 currentSize = System.Convert.ToInt32( sizeListBox.Items[ sizeListBox.SelectedIndex ] );
726 sizeTextBox.Text = currentSize.ToString( );
728 UpdateExamplePanel( );
732 void OnSelectedIndexChangedFontStyleListBox( object sender, EventArgs e )
734 if ( fontstyleListBox.SelectedIndex != -1 )
736 switch ( (int)fontStyleArray[ fontstyleListBox.SelectedIndex] )
739 currentFontStyle = FontStyle.Regular;
742 currentFontStyle = FontStyle.Bold;
745 currentFontStyle = FontStyle.Italic;
748 currentFontStyle = FontStyle.Bold | FontStyle.Italic;
751 currentFontStyle = FontStyle.Regular;
755 fontstyleTextBox.Text = fontstyleListBox.Items[ fontstyleListBox.SelectedIndex ].ToString( );
757 UpdateExamplePanel( );
761 void OnCheckedChangedUnderlinedCheckBox( object sender, EventArgs e )
763 if ( underlinedCheckBox.Checked )
764 currentFontStyle = currentFontStyle | FontStyle.Underline;
766 currentFontStyle = currentFontStyle ^ FontStyle.Underline;
768 UpdateExamplePanel( );
771 void OnCheckedChangedStrikethroughCheckBox( object sender, EventArgs e )
773 if ( strikethroughCheckBox.Checked )
774 currentFontStyle = currentFontStyle | FontStyle.Strikeout;
776 currentFontStyle = currentFontStyle ^ FontStyle.Strikeout;
778 UpdateExamplePanel( );
781 private void UpdateExamplePanel( )
783 currentFont = new Font( currentFamily, currentSize, currentFontStyle );
785 examplePanel.Invalidate( );
786 examplePanel.Update( );
789 internal class ColorComboBox : ComboBox
791 internal class ColorComboBoxItem
796 public ColorComboBoxItem( Color color, string name )
825 private Color selectedColor;
827 private FontDialogPanel fontDialogPanel;
829 // FIXME: TextBox backcolor shouldn't be the same as the selected item in the ListBox/ListCtrl
831 public ColorComboBox( FontDialogPanel fontDialogPanel )
833 this.fontDialogPanel = fontDialogPanel;
835 DropDownStyle = ComboBoxStyle.DropDownList;
836 DrawMode = DrawMode.OwnerDrawFixed;
838 Items.AddRange( new object[] {
839 new ColorComboBoxItem( Color.Black, "Black" ),
840 new ColorComboBoxItem( Color.DarkRed, "Dark-Red" ),
841 new ColorComboBoxItem( Color.Green, "Green" ),
842 new ColorComboBoxItem( Color.Olive, "Olive-Green" ), // color not correct
843 new ColorComboBoxItem( Color.Aquamarine, "Aquamarine" ), // color not correct
844 new ColorComboBoxItem( Color.Crimson, "Crimson" ),
845 new ColorComboBoxItem( Color.Cyan, "Cyan" ),
846 new ColorComboBoxItem( Color.Gray, "Gray" ),
847 new ColorComboBoxItem( Color.Silver, "Silver" ),
848 new ColorComboBoxItem( Color.Red, "Red" ),
849 new ColorComboBoxItem( Color.YellowGreen, "Yellow-Green" ),
850 new ColorComboBoxItem( Color.Yellow, "Yellow" ),
851 new ColorComboBoxItem( Color.Blue, "Blue" ),
852 new ColorComboBoxItem( Color.Purple, "Purple" ),
853 new ColorComboBoxItem( Color.Aquamarine, "Aquamarine" ),
854 new ColorComboBoxItem( Color.White, "White" ) }
860 protected override void OnDrawItem( DrawItemEventArgs e )
865 ColorComboBoxItem ccbi = Items[ e.Index ] as ColorComboBoxItem;
867 Rectangle r = e.Bounds;
870 if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected )
872 e.Graphics.FillRectangle( new SolidBrush( Color.Blue ), e.Bounds ); // bot blue
873 e.Graphics.FillRectangle( new SolidBrush( ccbi.Color ), e.Bounds.X + 3, e.Bounds.Y + 3, e.Bounds.X + 16, e.Bounds.Y + e.Bounds.Height - 2 );
874 e.Graphics.DrawRectangle( new Pen( Color.Black ), e.Bounds.X + 2, e. Bounds.Y + 2, e.Bounds.X + 17, e.Bounds.Y + e.Bounds.Height - 1 );
875 e.Graphics.DrawString( ccbi.Name, this.Font, new SolidBrush( Color.White ), r );
879 e.Graphics.FillRectangle( new SolidBrush( Color.White ), e.Bounds );
880 e.Graphics.FillRectangle( new SolidBrush( ccbi.Color ), e.Bounds.X + 3, e.Bounds.Y + 3, e.Bounds.X + 16, e.Bounds.Y + e.Bounds.Height - 2 );
881 e.Graphics.DrawRectangle( new Pen( Color.Black ), e.Bounds.X + 2, e. Bounds.Y + 2, e.Bounds.X + 17, e.Bounds.Y + e.Bounds.Height - 1 );
882 e.Graphics.DrawString( ccbi.Name, this.Font, new SolidBrush( Color.Black ), r );
886 protected override void OnSelectedIndexChanged( EventArgs e )
888 ColorComboBoxItem ccbi = Items[ SelectedIndex ] as ColorComboBoxItem;
889 selectedColor = ccbi.Color;
891 fontDialogPanel.CurrentColor = selectedColor;