* ImageList.cs: When the image stream is set pull all the images
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / FontDialog.cs
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) 2004 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Alexander Olk   xenomorph2@onlinehome.de
24 //
25 //
26
27 // NOT COMPLETE - work in progress
28
29 // TODO:
30 // - correct drawing of the selected font in examplePanel
31 // - select values for font/style/size via the TextBoxes
32 // - etc
33
34 using System.ComponentModel;
35 using System.Drawing;
36
37 namespace System.Windows.Forms
38 {
39         [DefaultProperty( "Font" )]
40         public class FontDialog : CommonDialog
41         {
42                 private FontDialogPanel fontDialogPanel;
43                 
44                 private Font font;
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;
58                 
59                 private bool fontMustExist = false;
60                 
61                 #region Public Constructors
62                 public FontDialog( )
63                 {
64                         form.ClientSize = new Size( 430, 318 );
65                         
66                         form.Size = new Size( 430, 318 );
67                         
68                         form.Text = "Font";
69                         
70                         fontDialogPanel = new FontDialogPanel( this );
71                 }
72                 #endregion      // Public Constructors
73                 
74                 #region Public Instance Properties
75                 public Font Font
76                 {
77                         get {
78                                 return font;
79                         }
80                         
81                         set {
82                                 font = value;
83                         }
84                 }
85                 
86                 [DefaultValue(false)]
87                 public bool FontMustExist
88                 {
89                         get {
90                                 return fontMustExist;
91                         }
92                         
93                         set {
94                                 fontMustExist = value;
95                         }
96                 }
97                 
98                 [DefaultValue("Black")]
99                 public Color Color
100                 {
101                         set {
102                                 color = value;
103                         }
104                         
105                         get {
106                                 return color;
107                         }
108                 }
109                 
110                 [DefaultValue(true)]
111                 public bool AllowSimulations
112                 {
113                         set
114                         {
115                                 allowSimulations = value;
116                         }
117                         
118                         get
119                         {
120                                 return allowSimulations;
121                         }
122                 }
123                 
124                 [DefaultValue(true)]
125                 public bool AllowVectorFonts
126                 {
127                         set
128                         {
129                                 allowVectorFonts = value;
130                         }
131                         
132                         get
133                         {
134                                 return allowVectorFonts;
135                         }
136                 }
137                 
138                 [DefaultValue(true)]
139                 public bool AllowVerticalFonts
140                 {
141                         set
142                         {
143                                 allowVerticalFonts = value;
144                         }
145                         
146                         get
147                         {
148                                 return allowVerticalFonts;
149                         }
150                 }
151                 
152                 [DefaultValue(true)]
153                 public bool AllowScriptChange
154                 {
155                         set
156                         {
157                                 allowScriptChange = value;
158                         }
159                         
160                         get
161                         {
162                                 return allowScriptChange;
163                         }
164                 }
165                 
166                 [DefaultValue(false)]
167                 public bool FixedPitchOnly
168                 {
169                         set
170                         {
171                                 fixedPitchOnly = value;
172                         }
173                         
174                         get
175                         {
176                                 return fixedPitchOnly;
177                         }
178                 }
179                 
180                 [DefaultValue(0)]
181                 public int MaxSize
182                 {
183                         set
184                         {
185                                 maxSize = value;
186                         }
187                         
188                         get
189                         {
190                                 return maxSize;
191                         }
192                 }
193                 
194                 [DefaultValue(0)]
195                 public int MinSize
196                 {
197                         set
198                         {
199                                 minSize = value;
200                         }
201                         
202                         get
203                         {
204                                 return minSize;
205                         }
206                 }
207                 
208                 [DefaultValue(false)]
209                 public bool ScriptsOnly
210                 {
211                         set
212                         {
213                                 scriptsOnly = value;
214                         }
215                         
216                         get
217                         {
218                                 return scriptsOnly;
219                         }
220                 }
221                 
222                 [DefaultValue(false)]
223                 public bool ShowApply
224                 {
225                         set
226                         {
227                                 showApply = value;
228                         }
229                         
230                         get
231                         {
232                                 return showApply;
233                         }
234                 }
235                 
236                 [DefaultValue(false)]
237                 public bool ShowColor
238                 {
239                         set
240                         {
241                                 showColor = value;
242                         }
243                         
244                         get
245                         {
246                                 return showColor;
247                         }
248                 }
249                 
250                 [DefaultValue(true)]
251                 public bool ShowEffects
252                 {
253                         set
254                         {
255                                 showEffects = value;
256                         }
257                         
258                         get
259                         {
260                                 return showEffects;
261                         }
262                 }
263                 
264                 [DefaultValue(false)]
265                 public bool ShowHelp
266                 {
267                         set
268                         {
269                                 showHelp = value;
270                         }
271                         
272                         get
273                         {
274                                 return showHelp;
275                         }
276                 }
277                 
278                 #endregion      // Public Instance Properties
279                 
280                 #region Protected Instance Properties
281                 #endregion      // Protected Instance Properties
282                 
283                 #region Public Instance Methods
284                 [MonoTODO]
285                 public override void Reset( )
286                 {
287                         color = Color.Black;
288                         allowSimulations = true;
289                         allowVectorFonts = true;
290                         allowVerticalFonts = true;
291                         allowScriptChange = true;
292                         fixedPitchOnly = false;
293                         maxSize = 0;
294                         minSize = 0;
295                         scriptsOnly = false;
296                         showApply = false;
297                         showColor = false;
298                         showEffects = true;
299                         showHelp = false;
300                 }
301                 #endregion      // Public Instance Methods
302                 
303                 #region Protected Instance Methods
304                 [MonoTODO]
305                 protected override bool RunDialog( IntPtr hwndOwner )
306                 {
307                         form.Controls.Add( fontDialogPanel );
308                         
309                         return true;
310                 }
311                 #endregion      // Protected Instance Methods
312         }
313         
314         internal class FontDialogPanel : Panel
315         {
316                 private Panel examplePanel;
317                 
318                 private Button okButton;
319                 private Button cancelButton;
320                 private Button applyButton;
321                 private Button helpButton;
322                 
323                 private TextBox fontTextBox;
324                 private TextBox fontstyleTextBox;
325                 private TextBox sizeTextBox;
326                 
327                 private ListBox fontListBox;
328                 private ListBox fontstyleListBox;
329                 private ListBox sizeListBox;
330                 
331                 private GroupBox effectsGroupBox;
332                 private CheckBox strikethroughCheckBox;
333                 private CheckBox underlinedCheckBox;
334                 private ComboBox scriptComboBox;
335                 
336                 private Label fontLabel;
337                 private Label fontstyleLabel;
338                 private Label sizeLabel;
339                 private Label scriptLabel;
340                 
341                 private GroupBox exampleGroupBox;
342                 
343                 private ColorComboBox colorComboBox;
344                 
345                 private FontFamily[] fontFamilies;
346                 
347                 private string currentFontName;
348                 
349                 private Font currentFont;
350                 
351                 private int currentSize;
352                 
353                 private FontFamily currentFamily;
354                 
355                 private Color currentColor;
356                 
357                 private FontStyle currentFontStyle;
358                 
359                 private FontDialog fontDialog;
360                 
361                 private System.Collections.ArrayList fontStyleArray = new System.Collections.ArrayList();
362                 
363                 private System.Collections.Hashtable fontHash = new System.Collections.Hashtable();
364                 
365                 /// <summary>
366                 /// Constructor
367                 /// </summary>
368                 /// <param name="fontDialog">A  FontDialog</param>
369                 public FontDialogPanel( FontDialog fontDialog )
370                 {
371                         this.fontDialog = fontDialog;
372                         
373                         okButton = new Button( );
374                         cancelButton = new Button( );
375                         applyButton = new Button( );
376                         helpButton = new Button( );
377                         
378                         fontTextBox = new TextBox( );
379                         fontstyleTextBox = new TextBox( );
380                         sizeTextBox = new TextBox( );
381                         
382                         fontListBox = new ListBox( );
383                         sizeListBox = new ListBox( );
384                         
385                         fontLabel = new Label( );
386                         fontstyleLabel = new Label( );
387                         sizeLabel = new Label( );
388                         scriptLabel = new Label( );
389                         
390                         exampleGroupBox = new GroupBox( );
391                         fontstyleListBox = new ListBox( );
392                         
393                         effectsGroupBox = new GroupBox( );
394                         underlinedCheckBox = new CheckBox( );
395                         strikethroughCheckBox = new CheckBox( );
396                         scriptComboBox = new ComboBox( );
397                         
398                         examplePanel = new Panel( );
399                         
400                         colorComboBox = new ColorComboBox( this );
401                         
402                         exampleGroupBox.SuspendLayout( );
403                         effectsGroupBox.SuspendLayout( );
404                         SuspendLayout( );
405                         
406                         // typesizeListBox
407                         sizeListBox.Location = new Point( 284, 47 );
408                         sizeListBox.Size = new Size( 52, 95 );
409                         sizeListBox.TabIndex = 10;
410                         // fontTextBox
411                         fontTextBox.Location = new Point( 16, 26 );
412                         fontTextBox.Size = new Size( 140, 21 );
413                         fontTextBox.TabIndex = 5;
414                         fontTextBox.Text = "";
415                         // fontstyleLabel
416                         fontstyleLabel.Location = new Point( 164, 10 );
417                         fontstyleLabel.Size = new Size( 100, 16 );
418                         fontstyleLabel.TabIndex = 1;
419                         fontstyleLabel.Text = "Font Style:";
420                         // typesizeTextBox
421                         sizeTextBox.Location = new Point( 284, 26 );
422                         sizeTextBox.Size = new Size( 52, 21 );
423                         sizeTextBox.TabIndex = 7;
424                         sizeTextBox.Text = "";
425                         // schriftartListBox
426                         fontListBox.Location = new Point( 16, 47 );
427                         fontListBox.Size = new Size( 140, 95 );
428                         fontListBox.TabIndex = 8;
429                         fontListBox.Sorted = true;
430                         // exampleGroupBox
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";
438                         // fontstyleListBox
439                         fontstyleListBox.Location = new Point( 164, 47 );
440                         fontstyleListBox.Size = new Size( 112, 95 );
441                         fontstyleListBox.TabIndex = 9;
442                         // schriftartLabel
443                         fontLabel.Location = new Point( 16, 10 );
444                         fontLabel.Size = new Size( 88, 16 );
445                         fontLabel.TabIndex = 0;
446                         fontLabel.Text = "Font:";
447                         // effectsGroupBox
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";
462                         // colorComboBox
463                         colorComboBox.Location = new Point( 8, 70 );
464                         colorComboBox.Size = new Size( 130, 21 );
465                         // sizeLabel
466                         sizeLabel.Location = new Point( 284, 10 );
467                         sizeLabel.Size = new Size( 100, 16 );
468                         sizeLabel.TabIndex = 2;
469                         sizeLabel.Text = "Size:";
470                         // scriptComboBox
471                         scriptComboBox.Location = new Point( 164, 253 );
472                         scriptComboBox.Size = new Size( 172, 21 );
473                         scriptComboBox.TabIndex = 14;
474                         scriptComboBox.Text = "-/-";
475                         // okButton
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";
481                         // cancelButton
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";
487                         // applyButton
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";
493                         // helpButton
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";
504                         // fontstyleTextBox
505                         fontstyleTextBox.Location = new Point( 164, 26 );
506                         fontstyleTextBox.Size = new Size( 112, 21 );
507                         fontstyleTextBox.TabIndex = 6;
508                         fontstyleTextBox.Text = "";
509                         // scriptLabel
510                         scriptLabel.Location = new Point( 164, 236 );
511                         scriptLabel.Size = new Size( 100, 16 );
512                         scriptLabel.TabIndex = 13;
513                         scriptLabel.Text = "Script:";
514                         // examplePanel
515                         examplePanel.Location = new Point( 8, 20 );
516                         examplePanel.TabIndex = 0;
517                         examplePanel.BorderStyle = BorderStyle.Fixed3D;
518                         examplePanel.Size = new Size( 156, 40 );
519                         
520                         ClientSize = new Size( 430, 318 );
521                         
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 );
539                         
540                         exampleGroupBox.ResumeLayout( false );
541                         effectsGroupBox.ResumeLayout( false );
542                         
543                         ResumeLayout( false );
544                         
545                         fontFamilies = FontFamily.Families;
546                         
547                         fontListBox.BeginUpdate( );
548                         foreach ( FontFamily ff in fontFamilies )
549                         {
550                                 fontListBox.Items.Add( ff.Name );
551                                 fontHash.Add( ff.Name, ff );
552                         }
553                         fontListBox.EndUpdate( );
554                         
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;
558                         
559                         // default 12 ?!?
560                         currentSize = 12;
561                         
562                         currentFamily = FindByName( currentFontName );
563                         
564                         currentFontStyle = FontStyle.Regular;
565                         
566                         currentFont = new Font( currentFamily, currentSize, currentFontStyle );
567                         
568                         currentColor = fontDialog.Color;
569                         
570                         UpdateFontStyleListBox();
571                         
572                         fontstyleTextBox.Text = "Regular";
573                         
574                         sizeTextBox.Text = currentSize.ToString( );
575                         
576                         sizeListBox.Items.AddRange( new object[] {
577                                                                                    "8",
578                                                                                    "9",
579                                                                                    "10",
580                                                                                    "11",
581                                                                                    "12",
582                                                                                    "14",
583                                                                                    "16",
584                                                                                    "18",
585                                                                                    "20",
586                                                                                    "22",
587                                                                                    "24",
588                                                                                    "26",
589                                                                                    "28",
590                                                                                    "36",
591                                                                                    "48",
592                                                                                    "72" } );
593                         
594                         sizeListBox.SelectedIndex = 0;
595                         
596                         if ( !fontDialog.ShowApply )
597                                 applyButton.Hide( );
598                         if ( !fontDialog.ShowHelp )
599                                 helpButton.Hide( );
600                         if ( !fontDialog.ShowEffects )
601                                 effectsGroupBox.Hide();
602                         if ( !fontDialog.ShowColor )
603                                 colorComboBox.Hide();
604                         
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 );
613                 }
614                 
615                 public Color CurrentColor
616                 {
617                         set
618                         {
619                                 currentColor = value;
620                                 examplePanel.Invalidate();
621                         }
622                         
623                         get
624                         {
625                                 return currentColor;
626                         }
627                 }
628
629                 private void UpdateFontStyleListBox( )
630                 {
631                         // don't know if that works, IsStyleAvailable returns true for all styles under X
632                         
633                         fontStyleArray.Clear();
634                         
635                         fontstyleListBox.BeginUpdate( );
636                         
637                         fontstyleListBox.Items.Clear();
638                         
639                         if ( currentFamily.IsStyleAvailable( FontStyle.Regular ) )
640                         {
641                                 fontstyleListBox.Items.Add( "Regular" );
642                                 fontStyleArray.Add(0);
643                         }
644                         
645                         if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) )
646                         {
647                                 fontstyleListBox.Items.Add( "Bold" );
648                                 fontStyleArray.Add(1);
649                         }
650                         
651                         if ( currentFamily.IsStyleAvailable( FontStyle.Italic ) )
652                         {
653                                 fontstyleListBox.Items.Add( "Italic" );
654                                 fontStyleArray.Add(2);
655                         }
656                         
657                         if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) && currentFamily.IsStyleAvailable( FontStyle.Italic ) )
658                         {
659                                 fontstyleListBox.Items.Add( "Bold Italic" );
660                                 fontStyleArray.Add(3);
661                         }
662                         
663                         fontstyleListBox.EndUpdate( );
664                 }
665                 
666                 private FontFamily FindByName( string name )
667                 {
668                         return fontHash[ name ] as FontFamily;
669                 }
670                 
671                 void OnClickCancelButton( object sender, EventArgs e )
672                 {
673                         fontDialog.form.Controls.Remove( this );
674                         fontDialog.form.DialogResult = DialogResult.Cancel;
675                 }
676                 
677                 void OnClickOkButton( object sender, EventArgs e )
678                 {
679                         fontDialog.form.Controls.Remove( this );
680                         fontDialog.Font = currentFont;
681                         fontDialog.Color = currentColor;
682                         fontDialog.form.DialogResult = DialogResult.OK;
683                 }
684                 
685                 void OnPaintExamplePanel( object sender, PaintEventArgs e )
686                 {
687                         SolidBrush brush = new SolidBrush( currentColor );
688                         
689                         // FIXME: need to find a better algorithm to display the font
690                         
691                         int x = ( examplePanel.Width / 2 ) - ( currentSize * 4 );
692                         int y = ( examplePanel.Height / 2 ) - ( currentFont.Height / 2 );
693                         
694                         if ( x < 0 ) x = 0;
695                         if ( y < 0 ) y = 0;
696                         
697                         e.Graphics.DrawString( "AaBbYyZz", currentFont, brush, new Point( x, y ) );
698                         
699 //                      StringFormat strformat = new StringFormat();
700 //                      strformat.Alignment = StringAlignment.Center;
701 //                      strformat.Alignment = StringAlignment.Center;
702 //
703 //                      e.Graphics.DrawString ("AaBbYyZz", currentFont, brush, new Rectangle(0, 0, examplePanel.Width, examplePanel.Height), strformat);
704                 }
705                 
706                 void OnSelectedIndexChangedFontListBox( object sender, EventArgs e )
707                 {
708                         if ( fontListBox.SelectedIndex != -1 )
709                         {
710                                 currentFamily = FindByName( fontListBox.Items[ fontListBox.SelectedIndex ].ToString( ) );
711                                 
712                                 fontTextBox.Text = currentFamily.Name;
713                                 
714                                 UpdateFontStyleListBox();
715                                 
716                                 UpdateExamplePanel( );
717                         }
718                 }
719                 
720                 void OnSelectedIndexChangedSizeListBox( object sender, EventArgs e )
721                 {
722                         if ( sizeListBox.SelectedIndex != -1 )
723                         {
724                                 currentSize = System.Convert.ToInt32( sizeListBox.Items[ sizeListBox.SelectedIndex ] );
725                                 
726                                 sizeTextBox.Text = currentSize.ToString( );
727                                 
728                                 UpdateExamplePanel( );
729                         }
730                 }
731                 
732                 void OnSelectedIndexChangedFontStyleListBox( object sender, EventArgs e )
733                 {
734                         if ( fontstyleListBox.SelectedIndex != -1 )
735                         {
736                                 switch ( (int)fontStyleArray[ fontstyleListBox.SelectedIndex] )
737                                 {
738                                         case 0:
739                                                 currentFontStyle = FontStyle.Regular;
740                                                 break;
741                                         case 1:
742                                                 currentFontStyle = FontStyle.Bold;
743                                                 break;
744                                         case 2:
745                                                 currentFontStyle = FontStyle.Italic;
746                                                 break;
747                                         case 3:
748                                                 currentFontStyle = FontStyle.Bold | FontStyle.Italic;
749                                                 break;
750                                         default:
751                                                 currentFontStyle = FontStyle.Regular;
752                                                 break;
753                                 }
754                                 
755                                 fontstyleTextBox.Text = fontstyleListBox.Items[ fontstyleListBox.SelectedIndex ].ToString( );
756                                 
757                                 UpdateExamplePanel( );
758                         }
759                 }
760                 
761                 void OnCheckedChangedUnderlinedCheckBox( object sender, EventArgs e )
762                 {
763                         if ( underlinedCheckBox.Checked )
764                                 currentFontStyle = currentFontStyle | FontStyle.Underline;
765                         else
766                                 currentFontStyle = currentFontStyle ^ FontStyle.Underline;
767                         
768                         UpdateExamplePanel( );
769                 }
770                 
771                 void OnCheckedChangedStrikethroughCheckBox( object sender, EventArgs e )
772                 {
773                         if ( strikethroughCheckBox.Checked )
774                                 currentFontStyle = currentFontStyle | FontStyle.Strikeout;
775                         else
776                                 currentFontStyle = currentFontStyle ^ FontStyle.Strikeout;
777                         
778                         UpdateExamplePanel( );
779                 }
780                                 
781                 private void UpdateExamplePanel( )
782                 {
783                         currentFont = new Font( currentFamily, currentSize, currentFontStyle );
784                         
785                         examplePanel.Invalidate( );
786                         examplePanel.Update( );
787                 }
788                 
789                 internal class ColorComboBox : ComboBox
790                 {
791                         internal class ColorComboBoxItem
792                         {
793                                 private Color color;
794                                 private string name;
795                                 
796                                 public ColorComboBoxItem( Color color, string name )
797                                 {
798                                         this.color = color;
799                                         this.name = name;
800                                 }
801                                 
802                                 public Color Color
803                                 {
804                                         set {
805                                                 color = value;
806                                         }
807                                         
808                                         get {
809                                                 return color;
810                                         }
811                                 }
812                                 
813                                 public string Name
814                                 {
815                                         set {
816                                                 name = value;
817                                         }
818                                         
819                                         get {
820                                                 return name;
821                                         }
822                                 }
823                         }
824                         
825                         private Color selectedColor;
826                         
827                         private FontDialogPanel fontDialogPanel;
828                         
829                         // FIXME: TextBox backcolor shouldn't be the same as the selected item in the ListBox/ListCtrl
830                         
831                         public ColorComboBox( FontDialogPanel fontDialogPanel )
832                         {
833                                 this.fontDialogPanel = fontDialogPanel;
834                                 
835                                 DropDownStyle = ComboBoxStyle.DropDownList;
836                                 DrawMode = DrawMode.OwnerDrawFixed;
837                                 
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" ) }
855                                                            );
856                                 
857                                 SelectedIndex = 0;
858                         }
859                         
860                         protected override void OnDrawItem( DrawItemEventArgs e )
861                         {
862                                 if ( e.Index == -1 )
863                                         return;
864                                 
865                                 ColorComboBoxItem ccbi = Items[ e.Index ] as ColorComboBoxItem;
866                                 
867                                 Rectangle r = e.Bounds;
868                                 r.X = r.X + 24;
869                                 
870                                 if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected )
871                                 {
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 );
876                                 }
877                                 else
878                                 {
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 );
883                                 }
884                         }
885                         
886                         protected override void OnSelectedIndexChanged( EventArgs e )
887                         {
888                                 ColorComboBoxItem ccbi = Items[ SelectedIndex ] as ColorComboBoxItem;
889                                 selectedColor = ccbi.Color;
890                                 
891                                 fontDialogPanel.CurrentColor = selectedColor;
892                         }
893                 }
894         }
895 }
896
897