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