* TabControl.cs: Show the tooltip depending on the value
[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) 2006, Alexander Olk
21 //
22 // Authors:
23 //      Alexander Olk   alex.olk@googlemail.com
24 //
25 //
26
27 // NOT COMPLETE - work in progress
28
29 using System.ComponentModel;
30 using System.Drawing;
31 using System.Text.RegularExpressions;
32 using System;
33 using System.Collections;
34
35 namespace System.Windows.Forms
36 {
37         [DefaultProperty( "Font" )]
38         [DefaultEvent("Apply")]
39         public class FontDialog : CommonDialog
40         {
41                 protected static readonly object EventApply = new object ();
42
43                 private Font font;
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;
57                 
58                 private bool fontMustExist = false;
59                 
60                 private Panel examplePanel;
61                 
62                 private Button okButton;
63                 private Button cancelButton;
64                 private Button applyButton;
65                 private Button helpButton;
66                 
67                 private TextBox fontTextBox;
68                 private TextBox fontstyleTextBox;
69                 private TextBox fontsizeTextBox;
70                 
71                 private MouseWheelListBox fontListBox;
72                 private MouseWheelListBox fontstyleListBox;
73                 private MouseWheelListBox fontsizeListBox;
74                 
75                 private GroupBox effectsGroupBox;
76                 private CheckBox strikethroughCheckBox;
77                 private CheckBox underlinedCheckBox;
78                 private ComboBox scriptComboBox;
79                 
80                 private Label fontLabel;
81                 private Label fontstyleLabel;
82                 private Label sizeLabel;
83                 private Label scriptLabel;
84                 
85                 private GroupBox exampleGroupBox;
86                 
87                 private ColorComboBox colorComboBox;
88                 
89                 private string currentFontName;
90                 
91                 private float currentSize;
92                 
93                 private FontFamily currentFamily;
94                 
95                 private FontStyle currentFontStyle;
96                 
97                 private bool underlined = false;
98                 private bool strikethrough = false;
99                 
100                 private Hashtable fontHash = new Hashtable();
101                 
102                 private int[] a_sizes = {
103                         6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72
104                 };
105                 
106                 // char set stuff is only here to make me happy :-)
107                 private string [] char_sets_names = {
108                         "Western",
109                         "Symbol",
110                         "Shift Jis",
111                         "Hangul",
112                         "GB2312",
113                         "BIG5",
114                         "Greek",
115                         "Turkish",
116                         "Hebrew",
117                         "Arabic",
118                         "Baltic",
119                         "Vietname",
120                         "Cyrillic",
121                         "East European",
122                         "Thai",
123                         "Johab",
124                         "Mac",
125                         "OEM",
126                         "VISCII",
127                         "TCVN",
128                         "KOI-8",
129                         "ISO-8859-3",
130                         "ISO-8859-4",
131                         "ISO-8859-10",
132                         "Celtic"
133                 };
134                 
135                 private string [] char_sets = {
136                         "AaBbYyZz",
137                         "Symbol",
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,
146                         "AaBbYyZz",
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",
152                         "AaBbYyZz",
153                         "AaBb" + (char)0xf8 + (char)0xf1 + (char)0xfd,
154                         "",
155                         "",
156                         "",
157                         "",
158                         "",
159                         "",
160                         ""
161                 };
162                 
163                 private string example_panel_text;
164                 
165                 private bool internal_change = false;
166                 
167                 #region Public Constructors
168                 public FontDialog( )
169                 {
170                         form = new DialogForm (this);
171                         example_panel_text = char_sets [0];
172                         
173                         okButton = new Button( );
174                         cancelButton = new Button( );
175                         applyButton = new Button( );
176                         helpButton = new Button( );
177                         
178                         fontTextBox = new TextBox( );
179                         fontstyleTextBox = new TextBox( );
180                         fontsizeTextBox = new TextBox( );
181                         
182                         fontListBox = new MouseWheelListBox ();
183                         fontsizeListBox = new MouseWheelListBox ();
184                         fontstyleListBox = new MouseWheelListBox ();
185                         
186                         fontLabel = new Label( );
187                         fontstyleLabel = new Label( );
188                         sizeLabel = new Label( );
189                         scriptLabel = new Label( );
190                         
191                         exampleGroupBox = new GroupBox( );
192                         
193                         effectsGroupBox = new GroupBox( );
194                         underlinedCheckBox = new CheckBox( );
195                         strikethroughCheckBox = new CheckBox( );
196                         scriptComboBox = new ComboBox( );
197                         
198                         examplePanel = new Panel( );
199                         
200                         colorComboBox = new ColorComboBox( this );
201                         
202                         exampleGroupBox.SuspendLayout( );
203                         effectsGroupBox.SuspendLayout( );
204                         form.SuspendLayout( );
205                         
206                         form.FormBorderStyle = FormBorderStyle.FixedDialog;
207                         form.MaximizeBox = false;
208                         
209                         // fontsizeListBox
210                         fontsizeListBox.Location = new Point( 284, 47 );
211                         fontsizeListBox.Size = new Size( 52, 95 );
212                         fontsizeListBox.TabIndex = 10;
213                         fontListBox.Sorted = true;
214                         // fontTextBox
215                         fontTextBox.Location = new Point( 16, 26 );
216                         fontTextBox.Size = new Size( 140, 21 );
217                         fontTextBox.TabIndex = 5;
218                         fontTextBox.Text = "";
219                         // fontstyleLabel
220                         fontstyleLabel.Location = new Point( 164, 10 );
221                         fontstyleLabel.Size = new Size( 100, 16 );
222                         fontstyleLabel.TabIndex = 1;
223                         fontstyleLabel.Text = "Font Style:";
224                         // typesizeTextBox
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;
230                         // schriftartListBox
231                         fontListBox.Location = new Point( 16, 47 );
232                         fontListBox.Size = new Size( 140, 95 );
233                         fontListBox.TabIndex = 8;
234                         fontListBox.Sorted = true;
235                         // exampleGroupBox
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";
243                         // fontstyleListBox
244                         fontstyleListBox.Location = new Point( 164, 47 );
245                         fontstyleListBox.Size = new Size( 112, 95 );
246                         fontstyleListBox.TabIndex = 9;
247                         // schriftartLabel
248                         fontLabel.Location = new Point( 16, 10 );
249                         fontLabel.Size = new Size( 88, 16 );
250                         fontLabel.TabIndex = 0;
251                         fontLabel.Text = "Font:";
252                         // effectsGroupBox
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";
267                         // colorComboBox
268                         colorComboBox.Location = new Point( 8, 70 );
269                         colorComboBox.Size = new Size( 130, 21 );
270                         // sizeLabel
271                         sizeLabel.Location = new Point( 284, 10 );
272                         sizeLabel.Size = new Size( 100, 16 );
273                         sizeLabel.TabIndex = 2;
274                         sizeLabel.Text = "Size:";
275                         // scriptComboBox
276                         scriptComboBox.Location = new Point( 164, 253 );
277                         scriptComboBox.Size = new Size( 172, 21 );
278                         scriptComboBox.TabIndex = 14;
279                         scriptComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
280                         // okButton
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";
286                         // cancelButton
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";
292                         // applyButton
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";
298                         // helpButton
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";
309                         // fontstyleTextBox
310                         fontstyleTextBox.Location = new Point( 164, 26 );
311                         fontstyleTextBox.Size = new Size( 112, 21 );
312                         fontstyleTextBox.TabIndex = 6;
313                         fontstyleTextBox.Text = "";
314                         // scriptLabel
315                         scriptLabel.Location = new Point( 164, 236 );
316                         scriptLabel.Size = new Size( 100, 16 );
317                         scriptLabel.TabIndex = 13;
318                         scriptLabel.Text = "Script:";
319                         // examplePanel
320                         examplePanel.Location = new Point( 8, 20 );
321                         examplePanel.TabIndex = 0;
322                         examplePanel.Size = new Size( 156, 40 );
323                         examplePanel.BorderStyle = BorderStyle.Fixed3D;
324                         
325                         form.AcceptButton = okButton;
326                         form.CancelButton = cancelButton;
327                         
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 );
345                         
346                         exampleGroupBox.ResumeLayout( false );
347                         effectsGroupBox.ResumeLayout( false );
348                         
349                         form.Size = new Size( 430, 318 );
350                         
351                         form.FormBorderStyle = FormBorderStyle.FixedDialog;
352                         form.MaximizeBox = false;
353                         
354                         form.Text = "Font";
355                         
356                         form.ResumeLayout( false );
357                         
358                         scriptComboBox.BeginUpdate ();
359                         scriptComboBox.Items.AddRange (char_sets_names);
360                         scriptComboBox.SelectedIndex = 0;
361                         scriptComboBox.EndUpdate ();
362                         
363                         applyButton.Hide( );
364                         helpButton.Hide( );
365                         colorComboBox.Hide( );
366                         
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);
377                         
378                         fontTextBox.KeyPress += new KeyPressEventHandler (OnFontTextBoxKeyPress);
379                         fontstyleTextBox.KeyPress += new KeyPressEventHandler (OnFontStyleTextBoxKeyPress);
380                         fontsizeTextBox.KeyPress += new KeyPressEventHandler (OnFontSizeTextBoxKeyPress);
381                         
382                         fontTextBox.TextChanged += new EventHandler (OnFontTextBoxTextChanged);
383                         fontstyleTextBox.TextChanged += new EventHandler (OnFontStyleTextTextChanged);
384                         fontsizeTextBox.TextChanged += new EventHandler (OnFontSizeTextBoxTextChanged);
385                         
386                         fontTextBox.KeyDown += new KeyEventHandler (OnFontTextBoxKeyDown);
387                         fontstyleTextBox.KeyDown += new KeyEventHandler (OnFontStyleTextBoxKeyDown);
388                         fontsizeTextBox.KeyDown += new KeyEventHandler (OnFontSizeTextBoxKeyDown);
389                         
390                         fontTextBox.MouseWheel += new MouseEventHandler (OnFontTextBoxMouseWheel);
391                         fontstyleTextBox.MouseWheel += new MouseEventHandler (OnFontStyleTextBoxMouseWheel);
392                         fontsizeTextBox.MouseWheel += new MouseEventHandler (OnFontSizeTextBoxMouseWheel);
393                         
394                         PopulateFontList ();
395                 }
396                 #endregion      // Public Constructors
397                 
398                 #region Public Instance Properties
399                 public Font Font
400                 {
401                         get {
402                                 return font;
403                         }
404                         
405                         set {
406                                 if (value != null) {
407                                         font = new Font(value, value.Style);
408                                         
409                                         currentFontStyle = font.Style;
410                                         currentSize = font.SizeInPoints;
411                                         currentFontName = font.Name;
412                                         
413                                         strikethroughCheckBox.Checked = font.Strikeout;
414                                         underlinedCheckBox.Checked = font.Underline;
415                                         
416                                         int index = fontListBox.FindString (currentFontName);
417                                         
418                                         if (index != -1) {
419                                                 fontListBox.SelectedIndex = index;
420                                         } else {
421                                                 fontListBox.SelectedIndex = 0;
422                                         }
423                                         
424                                         UpdateFontSizeListBox ();
425                                         UpdateFontStyleListBox ();
426                                         
427                                         fontListBox.TopIndex = fontListBox.SelectedIndex;
428                                 }
429                         }
430                 }
431                 
432                 [DefaultValue(false)]
433                 public bool FontMustExist
434                 {
435                         get {
436                                 return fontMustExist;
437                         }
438                         
439                         set {
440                                 fontMustExist = value;
441                         }
442                 }
443
444 #if NET_2_0
445                 [DefaultValue ("Color [Black]")]
446 #endif
447                 public Color Color
448                 {
449                         set {
450                                 color = value;
451                                 examplePanel.Invalidate( );
452                         }
453                         
454                         get {
455                                 return color;
456                         }
457                 }
458                 
459                 [DefaultValue(true)]
460                 public bool AllowSimulations
461                 {
462                         set {
463                                 allowSimulations = value;
464                         }
465                         
466                         get {
467                                 return allowSimulations;
468                         }
469                 }
470                 
471                 [DefaultValue(true)]
472                 public bool AllowVectorFonts
473                 {
474                         set {
475                                 allowVectorFonts = value;
476                         }
477                         
478                         get {
479                                 return allowVectorFonts;
480                         }
481                 }
482                 
483                 [DefaultValue(true)]
484                 public bool AllowVerticalFonts
485                 {
486                         set {
487                                 allowVerticalFonts = value;
488                         }
489                         
490                         get {
491                                 return allowVerticalFonts;
492                         }
493                 }
494                 
495                 [DefaultValue(true)]
496                 public bool AllowScriptChange
497                 {
498                         set {
499                                 allowScriptChange = value;
500                         }
501                         
502                         get {
503                                 return allowScriptChange;
504                         }
505                 }
506                 
507                 [DefaultValue(false)]
508                 public bool FixedPitchOnly
509                 {
510                         set {
511                                 if (fixedPitchOnly != value) {
512                                         fixedPitchOnly = value;
513                                         PopulateFontList ();
514                                 }
515                         }
516                         
517                         get {
518                                 return fixedPitchOnly;
519                         }
520                 }
521                 
522                 [DefaultValue(0)]
523                 public int MaxSize
524                 {
525                         set {
526                                 maxSize = value;
527                                 
528                                 if (maxSize < 0)
529                                         maxSize = 0;
530                                 
531                                 if (maxSize < minSize)
532                                         minSize = maxSize;
533                                 
534                                 CreateFontSizeListBoxItems ();
535                         }
536                         
537                         get {
538                                 return maxSize;
539                         }
540                 }
541                 
542                 [DefaultValue(0)]
543                 public int MinSize
544                 {
545                         set {
546                                 minSize = value;
547                                 
548                                 if (minSize < 0)
549                                         minSize = 0;
550                                 
551                                 if (minSize > maxSize)
552                                         maxSize = minSize;
553                                 
554                                 CreateFontSizeListBoxItems ();
555                                 
556                                 if (minSize > currentSize)
557                                         if (font != null) {
558                                                 font.Dispose();
559                                                 
560                                                 currentSize = minSize;
561                                                 
562                                                 font = new Font( currentFamily, currentSize, currentFontStyle );
563                                                 
564                                                 UpdateExamplePanel ();
565                                                 
566                                                 fontsizeTextBox.Text = currentSize.ToString ();
567                                         }
568                         }
569                         
570                         get {
571                                 return minSize;
572                         }
573                 }
574                 
575                 [DefaultValue(false)]
576                 public bool ScriptsOnly
577                 {
578                         set {
579                                 scriptsOnly = value;
580                         }
581                         
582                         get {
583                                 return scriptsOnly;
584                         }
585                 }
586                 
587                 [DefaultValue(false)]
588                 public bool ShowApply
589                 {
590                         set {
591                                 if (value != showApply)
592                                 {
593                                         showApply = value;
594                                         if (showApply)
595                                                 applyButton.Show ();
596                                         else
597                                                 applyButton.Hide ();
598                                         
599                                         form.Refresh();
600                                 }
601                                 
602                         }
603                         
604                         get {
605                                 return showApply;
606                         }
607                 }
608                 
609                 [DefaultValue(false)]
610                 public bool ShowColor
611                 {
612                         set {
613                                 if (value != showColor)
614                                 {
615                                         showColor = value;
616                                         if (showColor)
617                                                 colorComboBox.Show ();
618                                         else
619                                                 colorComboBox.Hide ();
620                                         
621                                         form.Refresh();
622                                 }
623                         }
624                         
625                         get {
626                                 return showColor;
627                         }
628                 }
629                 
630                 [DefaultValue(true)]
631                 public bool ShowEffects
632                 {
633                         set {
634                                 if (value != showEffects)
635                                 {
636                                         showEffects = value;
637                                         if (showEffects)
638                                                 effectsGroupBox.Show ();
639                                         else
640                                                 effectsGroupBox.Hide ();
641                                         
642                                         form.Refresh();
643                                 }
644                         }
645                         
646                         get {
647                                 return showEffects;
648                         }
649                 }
650                 
651                 [DefaultValue(false)]
652                 public bool ShowHelp
653                 {
654                         set {
655                                 if (value != showHelp)
656                                 {
657                                         showHelp = value;
658                                         if (showHelp)
659                                                 helpButton.Show ();
660                                         else
661                                                 helpButton.Hide ();
662                                         
663                                         form.Refresh();
664                                 }
665                         }
666                         
667                         get {
668                                 return showHelp;
669                         }
670                 }
671                 
672                 #endregion      // Public Instance Properties
673                 
674                 #region Protected Instance Properties
675                 protected int Options {
676                         get { return 0; }
677                 }
678                 #endregion      // Protected Instance Properties
679                 
680                 #region Public Instance Methods
681                 public override void Reset( )
682                 {
683                         color = Color.Black;
684                         allowSimulations = true;
685                         allowVectorFonts = true;
686                         allowVerticalFonts = true;
687                         allowScriptChange = true;
688                         fixedPitchOnly = false;
689                         
690                         maxSize = 0;
691                         minSize = 0;
692                         CreateFontSizeListBoxItems ();
693                         
694                         scriptsOnly = false;
695                         
696                         showApply = false;
697                         applyButton.Hide ();
698                         
699                         showColor = false;
700                         colorComboBox.Hide ();
701                         
702                         showEffects = true;
703                         effectsGroupBox.Show ();
704                         
705                         showHelp = false;
706                         helpButton.Hide ();
707                         
708                         form.Refresh ();
709                 }
710
711                 public override string ToString ()
712                 {
713                         if (font == null)
714                                 return base.ToString ();
715                         return String.Concat (base.ToString (), ", Font: ", font.ToString ());
716                 }
717                 #endregion      // Public Instance Methods
718                 
719                 #region Protected Instance Methods
720                 protected override IntPtr HookProc (IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
721                 {
722                         return base.HookProc (hWnd, msg, wparam, lparam);
723                 }
724
725                 protected override bool RunDialog( IntPtr hWndOwner )
726                 {
727                         form.Refresh();
728                         
729                         return true;
730                 }
731
732                 internal void OnApplyButton (object sender, EventArgs e)
733                 {
734                         OnApply (e);
735                 }
736
737                 protected virtual void OnApply (EventArgs e)
738                 {
739                         EventHandler apply = (EventHandler) Events [EventApply];
740                         if (apply != null)
741                                 apply (this, e);
742                 }
743                 #endregion      // Protected Instance Methods
744                 
745                 void OnClickCancelButton( object sender, EventArgs e )
746                 {
747                         form.DialogResult = DialogResult.Cancel;
748                 }
749                 
750                 void OnClickOkButton( object sender, EventArgs e )
751                 {
752                         form.DialogResult = DialogResult.OK;
753                 }
754                 
755                 void OnPaintExamplePanel( object sender, PaintEventArgs e )
756                 {
757                         SolidBrush brush = ThemeEngine.Current.ResPool.GetSolidBrush( color );
758                         
759                         e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( SystemColors.Control ), 0, 0, 156, 40 );
760                         
761                         SizeF fontSizeF = e.Graphics.MeasureString( example_panel_text, font );
762                         
763                         int text_width = (int)fontSizeF.Width;
764                         int text_height = (int)fontSizeF.Height;
765                         
766                         int x = ( examplePanel.Width / 2 ) - ( text_width / 2 );
767                         if ( x < 0 ) x = 0;
768                         
769                         int y = ( examplePanel.Height / 2 ) - ( text_height / 2 );
770                         
771                         e.Graphics.DrawString( example_panel_text, font, brush, new Point( x, y ) );
772                 }
773                 
774                 void OnSelectedIndexChangedFontListBox( object sender, EventArgs e )
775                 {
776                         if ( fontListBox.SelectedIndex != -1 )
777                         {
778                                 currentFamily = FindByName( fontListBox.Items[ fontListBox.SelectedIndex ].ToString( ) );
779                                 
780                                 fontTextBox.Text = currentFamily.Name;
781                                 
782                                 internal_change = true;
783                                 
784                                 UpdateFontStyleListBox( );
785                                 
786                                 UpdateFontSizeListBox ();
787                                 
788                                 UpdateExamplePanel ();
789                                 
790                                 form.Select(fontTextBox);
791                                 
792                                 internal_change = false;
793                         }
794                 }
795                 
796                 void OnSelectedIndexChangedSizeListBox( object sender, EventArgs e )
797                 {
798                         if ( fontsizeListBox.SelectedIndex != -1 )
799                         {
800                                 currentSize = (float)System.Convert.ToDouble( fontsizeListBox.Items[ fontsizeListBox.SelectedIndex ] );
801                                 
802                                 fontsizeTextBox.Text = currentSize.ToString( );
803                                 
804                                 UpdateExamplePanel( );
805                                 
806                                 if (!internal_change)
807                                         form.Select(fontsizeTextBox);
808                         }
809                 }
810                 
811                 void OnSelectedIndexChangedFontStyleListBox( object sender, EventArgs e )
812                 {
813                         if ( fontstyleListBox.SelectedIndex != -1 )
814                         {
815                                 switch ( fontstyleListBox.SelectedIndex )
816                                 {
817                                 case 0:
818                                         currentFontStyle = FontStyle.Regular;
819                                         break;
820                                 case 1:
821                                         currentFontStyle = FontStyle.Bold;
822                                         break;
823                                 case 2:
824                                         currentFontStyle = FontStyle.Italic;
825                                         break;
826                                 case 3:
827                                         currentFontStyle = FontStyle.Bold | FontStyle.Italic;
828                                         break;
829                                 default:
830                                         currentFontStyle = FontStyle.Regular;
831                                         break;
832                                 }
833                                 
834                                 if (underlined) 
835                                         currentFontStyle = currentFontStyle | FontStyle.Underline;
836                                 
837                                 if (strikethrough)
838                                         currentFontStyle = currentFontStyle | FontStyle.Strikeout;
839                                 
840                                 fontstyleTextBox.Text = fontstyleListBox.Items[ fontstyleListBox.SelectedIndex ].ToString( );
841                                 
842                                 if (!internal_change) {
843                                         UpdateExamplePanel( );
844                                         
845                                         form.Select(fontstyleTextBox);
846                                 }
847                         }
848                 }
849                 
850                 void OnCheckedChangedUnderlinedCheckBox( object sender, EventArgs e )
851                 {
852                         if ( underlinedCheckBox.Checked ) {
853                                 currentFontStyle = currentFontStyle | FontStyle.Underline;
854                                 underlined = true;
855                         }
856                         else {
857                                 currentFontStyle = currentFontStyle ^ FontStyle.Underline;
858                                 underlined = false;
859                         }
860                         
861                         UpdateExamplePanel( );
862                 }
863                 
864                 void OnCheckedChangedStrikethroughCheckBox( object sender, EventArgs e )
865                 {
866                         if ( strikethroughCheckBox.Checked ) {
867                                 currentFontStyle = currentFontStyle | FontStyle.Strikeout;
868                                 strikethrough = true;
869                         }
870                         else {
871                                 currentFontStyle = currentFontStyle ^ FontStyle.Strikeout;
872                                 strikethrough = false;
873                         }
874                         
875                         UpdateExamplePanel( );
876                 }
877                 
878                 bool internal_textbox_change = false;
879                 
880                 void OnFontTextBoxMouseWheel (object sender, MouseEventArgs e)
881                 {
882                         fontListBox.SendMouseWheelEvent (e);
883                 }
884                 
885                 void OnFontStyleTextBoxMouseWheel (object sender, MouseEventArgs e)
886                 {
887                         fontstyleListBox.SendMouseWheelEvent (e);
888                 }
889                 
890                 void OnFontSizeTextBoxMouseWheel (object sender, MouseEventArgs e)
891                 {
892                         fontsizeListBox.SendMouseWheelEvent (e);
893                 }
894                 
895                 void OnFontTextBoxKeyDown (object sender, KeyEventArgs e)
896                 {
897                         // Forward these keys on to the font style listbox
898                         switch (e.KeyCode) {
899                                 case Keys.Up:
900                                 case Keys.Down:
901                                 case Keys.PageDown:
902                                 case Keys.PageUp:
903                                         fontListBox.HandleKeyDown (e.KeyCode);
904                                         break;
905                         }
906                 }
907                 
908                 void OnFontStyleTextBoxKeyDown (object sender, KeyEventArgs e)
909                 {
910                         // Forward these keys on to the font style listbox
911                         switch (e.KeyCode) {
912                                 case Keys.Up:
913                                 case Keys.Down:
914                                 case Keys.PageDown:
915                                 case Keys.PageUp:
916                                         fontstyleListBox.HandleKeyDown (e.KeyCode);
917                                         break;
918                         }
919                 }
920                 
921                 void OnFontSizeTextBoxKeyDown (object sender, KeyEventArgs e)
922                 {
923                         // Forward these keys on to the font size listbox
924                         switch (e.KeyCode) {
925                                 case Keys.Up:
926                                 case Keys.Down:
927                                 case Keys.PageDown:
928                                 case Keys.PageUp:
929                                         fontsizeListBox.HandleKeyDown (e.KeyCode);
930                                         break;
931                         }
932                 }
933                 
934                 void OnFontTextBoxKeyPress (object sender, KeyPressEventArgs e)
935                 {
936                         internal_textbox_change = true;
937                         
938                         if (fontListBox.SelectedIndex > -1)
939                                 fontListBox.SelectedIndex = -1;
940                 }
941                 
942                 void OnFontStyleTextBoxKeyPress (object sender, KeyPressEventArgs e)
943                 {
944                         internal_textbox_change = true;
945                         
946                         if (fontstyleListBox.SelectedIndex > -1)
947                                 fontstyleListBox.SelectedIndex = -1;
948                 }
949                 
950                 void OnFontSizeTextBoxKeyPress (object sender, KeyPressEventArgs e)
951                 {
952                         if (Char.IsLetter (e.KeyChar) || Char.IsWhiteSpace (e.KeyChar) || Char.IsPunctuation (e.KeyChar) || e.KeyChar == ',') {
953                                 e.Handled = true;
954                                 return; 
955                         }
956                         
957                         internal_textbox_change = true;
958                 }
959                 
960                 void OnFontTextBoxTextChanged (object sender, EventArgs e)
961                 {
962                         if (!internal_textbox_change)
963                                 return;
964                         
965                         internal_textbox_change = false;
966                         
967                         string search = fontTextBox.Text;
968                         
969                         // Look for an exact match
970                         int found = fontListBox.FindStringExact (search);
971                         
972                         if (found != ListBox.NoMatches) {
973                                 fontListBox.SelectedIndex = found;
974                                 return;
975                         }
976                         
977                         // Look for a partial match
978                         found = fontListBox.FindString (search);
979                         
980                         if (found != ListBox.NoMatches) {
981                                 fontListBox.TopIndex = found;
982                                 return;
983                         }
984
985                         // No match, scroll to the top
986                         if (fontListBox.Items.Count > 0)
987                                 fontListBox.TopIndex = 0;
988                 }
989                 
990                 void OnFontStyleTextTextChanged (object sender, EventArgs e)
991                 {
992                         if (!internal_textbox_change)
993                                 return;
994                         
995                         internal_textbox_change = false;
996
997                         // Look for an exact match
998                         int found = fontstyleListBox.FindStringExact (fontstyleTextBox.Text);
999
1000                         if (found != ListBox.NoMatches)
1001                                 fontstyleListBox.SelectedIndex = found;
1002                 }
1003                 
1004                 void OnFontSizeTextBoxTextChanged (object sender, EventArgs e)
1005                 {
1006                         if (!internal_textbox_change)
1007                                 return;
1008                         
1009                         internal_textbox_change = false;
1010                         
1011                         if (fontsizeTextBox.Text.Length == 0)
1012                                 return;
1013                         
1014                         for (int i = 0; i < fontsizeListBox.Items.Count; i++) {
1015                                 string name = fontsizeListBox.Items [i] as string;
1016                                 
1017                                 if (name.StartsWith(fontsizeTextBox.Text)) {
1018                                         if (name == fontsizeTextBox.Text)
1019                                                 fontsizeListBox.SelectedIndex = i;
1020                                         else
1021                                                 fontsizeListBox.TopIndex = i;
1022                                         
1023                                         break;
1024                                 }
1025                         }
1026                 }
1027                 
1028                 void OnSelectedIndexChangedScriptComboBox (object sender, EventArgs e)
1029                 {
1030                         string tmp_str = char_sets [scriptComboBox.SelectedIndex];
1031                         
1032                         if (tmp_str.Length > 0) {
1033                                 example_panel_text = tmp_str;
1034                                 
1035                                 UpdateExamplePanel ();
1036                         }
1037                 }
1038                 
1039                 void UpdateExamplePanel( )
1040                 {
1041                         if (font != null)
1042                                 font.Dispose();
1043                         
1044                         font = new Font( currentFamily, currentSize, currentFontStyle );
1045                         
1046                         examplePanel.Invalidate( );
1047                 }
1048                 
1049                 void UpdateFontSizeListBox ()
1050                 {
1051                         int index = fontsizeListBox.FindString(((int)Math.Round ((currentSize))).ToString());
1052                         
1053                         if (index != -1)
1054                                 fontsizeListBox.SelectedIndex = index;
1055                         else 
1056                                 fontsizeListBox.SelectedIndex = 0;
1057                 }
1058                 
1059                 void UpdateFontStyleListBox( )
1060                 {
1061                         // don't know if that works, IsStyleAvailable returns true for all styles under X
1062                         
1063                         fontstyleListBox.BeginUpdate( );
1064                         
1065                         fontstyleListBox.Items.Clear( );
1066                         
1067                         int index = -1;
1068                         int to_select = 0;
1069                         
1070                         if ( currentFamily.IsStyleAvailable( FontStyle.Regular ) )
1071                         {
1072                                 index = fontstyleListBox.Items.Add( "Regular" );
1073                                 
1074                                 if ((currentFontStyle & FontStyle.Regular) == FontStyle.Regular)
1075                                         to_select = index;
1076                         }
1077                         
1078                         if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) )
1079                         {
1080                                 index = fontstyleListBox.Items.Add( "Bold" );
1081                                 
1082                                 if ((currentFontStyle & FontStyle.Bold) == FontStyle.Bold)
1083                                         to_select = index;
1084                         }
1085                         
1086                         if ( currentFamily.IsStyleAvailable( FontStyle.Italic ) )
1087                         {
1088                                 index = fontstyleListBox.Items.Add( "Italic" );
1089                                 
1090                                 if ((currentFontStyle & FontStyle.Italic) == FontStyle.Italic)
1091                                         to_select = index;
1092                         }
1093                         
1094                         if ( currentFamily.IsStyleAvailable( FontStyle.Bold ) && currentFamily.IsStyleAvailable( FontStyle.Italic ) )
1095                         {
1096                                 index = fontstyleListBox.Items.Add( "Bold Italic" );
1097                                 
1098                                 if ((currentFontStyle & (FontStyle.Bold | FontStyle.Italic)) == (FontStyle.Bold | FontStyle.Italic))
1099                                         to_select = index;
1100                         }
1101                         
1102                         if (fontstyleListBox.Items.Count > 0) {
1103                                 fontstyleListBox.SelectedIndex = to_select;
1104
1105                                 switch ((string)fontstyleListBox.SelectedItem) {
1106                                         case "Regular":
1107                                                 currentFontStyle = FontStyle.Regular;
1108                                                 break;
1109                                         case "Bold":
1110                                                 currentFontStyle = FontStyle.Bold;
1111                                                 break;
1112                                         case "Italic":
1113                                                 currentFontStyle = FontStyle.Italic;
1114                                                 break;
1115                                         case "Bold Italic":
1116                                                 currentFontStyle = FontStyle.Bold | FontStyle.Italic;
1117                                                 break;
1118                                 }
1119                                 
1120                                 if (strikethroughCheckBox.Checked)
1121                                         currentFontStyle |= FontStyle.Strikeout;
1122                                 if (underlinedCheckBox.Checked)
1123                                         currentFontStyle |= FontStyle.Underline;
1124                         }
1125                         
1126                         fontstyleListBox.EndUpdate( );
1127                 }
1128                 
1129                 FontFamily FindByName( string name )
1130                 {
1131                         return fontHash[ name ] as FontFamily;
1132                 }
1133                 
1134                 void CreateFontSizeListBoxItems ()
1135                 {
1136                         fontsizeListBox.BeginUpdate ();
1137                         
1138                         fontsizeListBox.Items.Clear();
1139                         
1140                         if (minSize == 0 && maxSize == 0)
1141                         {
1142                                 foreach (int i in a_sizes)
1143                                         fontsizeListBox.Items.Add (i.ToString());
1144                         } else {
1145                                 foreach (int i in a_sizes) {
1146                                         if (i >= minSize && i <= maxSize)
1147                                                 fontsizeListBox.Items.Add (i.ToString());
1148                                 }
1149                         }
1150                         
1151                         fontsizeListBox.EndUpdate ();
1152                 }
1153
1154                 #region Private Methods
1155                 private void PopulateFontList ()
1156                 {
1157                         fontListBox.Items.Clear ();
1158                         fontHash.Clear ();
1159
1160                         fontListBox.BeginUpdate ();
1161
1162                         foreach (FontFamily ff in FontFamily.Families) {
1163                                 if (!fontHash.ContainsKey (ff.Name)) {
1164                                         if (!fixedPitchOnly || (IsFontFamilyFixedPitch (ff))) {
1165                                                 fontListBox.Items.Add (ff.Name);
1166                                                 fontHash.Add (ff.Name, ff);
1167                                         }
1168                                 }
1169                         }
1170                         
1171                         fontListBox.EndUpdate ();
1172                         CreateFontSizeListBoxItems ();
1173
1174                         if (fixedPitchOnly)
1175                                 this.Font = new Font (FontFamily.GenericMonospace, 8.25f);
1176                         else
1177                                 this.Font = form.Font;  
1178                 }
1179                 
1180                 private bool IsFontFamilyFixedPitch (FontFamily family)
1181                 {
1182                         FontStyle fs;
1183                         
1184                         if (family.IsStyleAvailable (FontStyle.Regular))
1185                                 fs = FontStyle.Regular;
1186                         else if (family.IsStyleAvailable (FontStyle.Bold))
1187                                 fs = FontStyle.Bold;
1188                         else if (family.IsStyleAvailable (FontStyle.Italic))
1189                                 fs = FontStyle.Italic;
1190                         else if (family.IsStyleAvailable (FontStyle.Strikeout))
1191                                 fs = FontStyle.Strikeout;
1192                         else if (family.IsStyleAvailable (FontStyle.Underline))
1193                                 fs = FontStyle.Underline;
1194                         else
1195                                 return false;
1196
1197                         Font f = new Font (family.Name, 10, fs);
1198
1199                         if (TextRenderer.MeasureString ("i", f).Width == TextRenderer.MeasureString ("w", f).Width)
1200                                 return true;
1201                                 
1202                         return false;
1203                 }
1204                 #endregion
1205                 
1206                 internal class ColorComboBox : ComboBox
1207                 {
1208                         internal class ColorComboBoxItem
1209                         {
1210                                 private Color color;
1211                                 private string name;
1212                                 
1213                                 public ColorComboBoxItem( Color color, string name )
1214                                 {
1215                                         this.color = color;
1216                                         this.name = name;
1217                                 }
1218                                 
1219                                 public Color Color
1220                                 {
1221                                         set {
1222                                                 color = value;
1223                                         }
1224                                         
1225                                         get {
1226                                                 return color;
1227                                         }
1228                                 }
1229                                 
1230                                 public string Name
1231                                 {
1232                                         set {
1233                                                 name = value;
1234                                         }
1235                                         
1236                                         get {
1237                                                 return name;
1238                                         }
1239                                 }
1240
1241                                 public override string ToString()
1242                                 {
1243                                         return this.Name;
1244                                 }
1245                         }
1246                         
1247                         private Color selectedColor;
1248                         
1249                         private FontDialog fontDialog;
1250                         
1251                         public ColorComboBox( FontDialog fontDialog )
1252                         {
1253                                 this.fontDialog = fontDialog;
1254                                 
1255                                 DropDownStyle = ComboBoxStyle.DropDownList;
1256                                 DrawMode = DrawMode.OwnerDrawFixed;
1257                                 
1258                                 Items.AddRange (new object[] {
1259                                         new ColorComboBoxItem (Color.Black, "Black"),
1260                                         new ColorComboBoxItem (Color.Maroon, "Maroon"),
1261                                         new ColorComboBoxItem (Color.Green, "Green"),
1262                                         new ColorComboBoxItem (Color.Olive, "Olive"),
1263                                         new ColorComboBoxItem (Color.Navy, "Navy"),
1264                                         new ColorComboBoxItem (Color.Purple, "Purple"),
1265                                         new ColorComboBoxItem (Color.Teal, "Teal"),
1266                                         new ColorComboBoxItem (Color.Gray, "Gray"),
1267                                         new ColorComboBoxItem (Color.Silver, "Silver"),
1268                                         new ColorComboBoxItem (Color.Red, "Red"),
1269                                         new ColorComboBoxItem (Color.Lime, "Lime"),
1270                                         new ColorComboBoxItem (Color.Yellow, "Yellow"),
1271                                         new ColorComboBoxItem (Color.Blue, "Blue"),
1272                                         new ColorComboBoxItem (Color.Fuchsia, "Fuchsia"),
1273                                         new ColorComboBoxItem (Color.Aqua, "Aqua"),
1274                                         new ColorComboBoxItem (Color.White, "White") }
1275                                 );
1276                                 
1277                                 SelectedIndex = 0;
1278                                 MaxDropDownItems = 16;
1279                         }
1280                         
1281                         protected override void OnDrawItem( DrawItemEventArgs e )
1282                         {
1283                                 if ( e.Index == -1 )
1284                                         return;
1285                                 
1286                                 ColorComboBoxItem ccbi = Items[ e.Index ] as ColorComboBoxItem;
1287                                 
1288                                 Rectangle r = e.Bounds;
1289                                 r.X = r.X + 24;
1290                                 
1291                                 if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected )
1292                                 {
1293                                         e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( Color.Blue ), e.Bounds ); // bot blue
1294                                         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 );
1295                                         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 );
1296                                         e.Graphics.DrawString( ccbi.Name, this.Font, ThemeEngine.Current.ResPool.GetSolidBrush( Color.White ), r );
1297                                 }
1298                                 else
1299                                 {
1300                                         e.Graphics.FillRectangle( ThemeEngine.Current.ResPool.GetSolidBrush( Color.White ), e.Bounds );
1301                                         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 );
1302                                         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 );
1303                                         e.Graphics.DrawString( ccbi.Name, this.Font, ThemeEngine.Current.ResPool.GetSolidBrush( Color.Black ), r );
1304                                 }
1305                         }
1306                         
1307                         protected override void OnSelectedIndexChanged( EventArgs e )
1308                         {
1309                                 ColorComboBoxItem ccbi = Items[ SelectedIndex ] as ColorComboBoxItem;
1310                                 selectedColor = ccbi.Color;
1311                                 
1312                                 fontDialog.Color = selectedColor;
1313                         }
1314                 }
1315
1316                 public event EventHandler Apply {
1317                         add { Events.AddHandler (EventApply, value); }
1318                         remove { Events.RemoveHandler (EventApply, value); }
1319                 }
1320         }
1321                 
1322         internal class MouseWheelListBox : ListBox
1323         {
1324                 public void SendMouseWheelEvent(MouseEventArgs e)
1325                 {
1326                         OnMouseWheel (e);
1327                 }
1328         }
1329 }
1330