Merge pull request #268 from pcc/menudeactivate
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / FontDialog.cs
index 0206a0536815a6a0bc286c97fe4d579f28603e5c..5c1927d1acc20357eba9d95338f62ba1b736c62f 100644 (file)
@@ -86,8 +86,6 @@ namespace System.Windows.Forms
                
                private ColorComboBox colorComboBox;
                
-               private FontFamily[] fontFamilies;
-               
                private string currentFontName;
                
                private float currentSize;
@@ -169,6 +167,7 @@ namespace System.Windows.Forms
                #region Public Constructors
                public FontDialog( )
                {
+                       form = new DialogForm (this);
                        example_panel_text = char_sets [0];
                        
                        okButton = new Button( );
@@ -324,6 +323,7 @@ namespace System.Windows.Forms
                        examplePanel.BorderStyle = BorderStyle.Fixed3D;
                        
                        form.AcceptButton = okButton;
+                       form.CancelButton = cancelButton;
                        
                        form.Controls.Add( scriptComboBox );
                        form.Controls.Add( scriptLabel );
@@ -355,20 +355,6 @@ namespace System.Windows.Forms
                        
                        form.ResumeLayout( false );
                        
-                       fontFamilies = FontFamily.Families;
-                       
-                       fontListBox.BeginUpdate( );
-                       foreach ( FontFamily ff in fontFamilies )
-                       {
-                               if ( !fontHash.ContainsKey (ff.Name) ) {
-                                       fontListBox.Items.Add( ff.Name );
-                                       fontHash.Add( ff.Name, ff );
-                               }
-                       }
-                       fontListBox.EndUpdate( );
-                       
-                       CreateFontSizeListBoxItems ();
-                       
                        scriptComboBox.BeginUpdate ();
                        scriptComboBox.Items.AddRange (char_sets_names);
                        scriptComboBox.SelectedIndex = 0;
@@ -405,7 +391,7 @@ namespace System.Windows.Forms
                        fontstyleTextBox.MouseWheel += new MouseEventHandler (OnFontStyleTextBoxMouseWheel);
                        fontsizeTextBox.MouseWheel += new MouseEventHandler (OnFontSizeTextBoxMouseWheel);
                        
-                       Font = form.Font;
+                       PopulateFontList ();
                }
                #endregion      // Public Constructors
                
@@ -421,9 +407,12 @@ namespace System.Windows.Forms
                                        font = new Font(value, value.Style);
                                        
                                        currentFontStyle = font.Style;
-                                       currentSize = font.Size;
+                                       currentSize = font.SizeInPoints;
                                        currentFontName = font.Name;
                                        
+                                       strikethroughCheckBox.Checked = font.Strikeout;
+                                       underlinedCheckBox.Checked = font.Underline;
+                                       
                                        int index = fontListBox.FindString (currentFontName);
                                        
                                        if (index != -1) {
@@ -432,6 +421,9 @@ namespace System.Windows.Forms
                                                fontListBox.SelectedIndex = 0;
                                        }
                                        
+                                       UpdateFontSizeListBox ();
+                                       UpdateFontStyleListBox ();
+                                       
                                        fontListBox.TopIndex = fontListBox.SelectedIndex;
                                }
                        }
@@ -449,9 +441,7 @@ namespace System.Windows.Forms
                        }
                }
 
-#if NET_2_0
                [DefaultValue ("Color [Black]")]
-#endif
                public Color Color
                {
                        set {
@@ -516,7 +506,10 @@ namespace System.Windows.Forms
                public bool FixedPitchOnly
                {
                        set {
-                               fixedPitchOnly = value;
+                               if (fixedPitchOnly != value) {
+                                       fixedPitchOnly = value;
+                                       PopulateFontList ();
+                               }
                        }
                        
                        get {
@@ -727,7 +720,7 @@ namespace System.Windows.Forms
                        return base.HookProc (hWnd, msg, wparam, lparam);
                }
 
-               protected override bool RunDialog( IntPtr hwndOwner )
+               protected override bool RunDialog( IntPtr hWndOwner )
                {
                        form.Refresh();
                        
@@ -899,70 +892,40 @@ namespace System.Windows.Forms
                
                void OnFontTextBoxKeyDown (object sender, KeyEventArgs e)
                {
-                       if (e.KeyCode == Keys.Up) {
-                               int sel_index = fontListBox.SelectedIndex;
-                               
-                               sel_index--;
-                               
-                               if (sel_index < 0)
-                                       sel_index = 0;
-                               
-                               fontListBox.SelectedIndex = sel_index;
-                       } else if (e.KeyCode == Keys.Down) {
-                               int sel_index = fontListBox.SelectedIndex;
-                               
-                               sel_index++;
-                               
-                               if (sel_index > fontListBox.Items.Count - 1)
-                                       sel_index = fontListBox.Items.Count - 1;
-                               
-                               fontListBox.SelectedIndex = sel_index;
+                       // Forward these keys on to the font style listbox
+                       switch (e.KeyCode) {
+                               case Keys.Up:
+                               case Keys.Down:
+                               case Keys.PageDown:
+                               case Keys.PageUp:
+                                       fontListBox.HandleKeyDown (e.KeyCode);
+                                       break;
                        }
                }
                
                void OnFontStyleTextBoxKeyDown (object sender, KeyEventArgs e)
                {
-                       if (e.KeyCode == Keys.Up) {
-                               int sel_index = fontstyleListBox.SelectedIndex;
-                               
-                               sel_index--;
-                               
-                               if (sel_index < 0)
-                                       sel_index = 0;
-                               
-                               fontstyleListBox.SelectedIndex = sel_index;
-                       } else if (e.KeyCode == Keys.Down) {
-                               int sel_index = fontstyleListBox.SelectedIndex;
-                               
-                               sel_index++;
-                               
-                               if (sel_index > fontstyleListBox.Items.Count - 1)
-                                       sel_index = fontstyleListBox.Items.Count - 1;
-                               
-                               fontstyleListBox.SelectedIndex = sel_index;
+                       // Forward these keys on to the font style listbox
+                       switch (e.KeyCode) {
+                               case Keys.Up:
+                               case Keys.Down:
+                               case Keys.PageDown:
+                               case Keys.PageUp:
+                                       fontstyleListBox.HandleKeyDown (e.KeyCode);
+                                       break;
                        }
                }
                
                void OnFontSizeTextBoxKeyDown (object sender, KeyEventArgs e)
                {
-                       if (e.KeyCode == Keys.Up) {
-                               int sel_index = fontsizeListBox.SelectedIndex;
-                               
-                               sel_index--;
-                               
-                               if (sel_index < 0)
-                                       sel_index = 0;
-                               
-                               fontsizeListBox.SelectedIndex = sel_index;
-                       } else if (e.KeyCode == Keys.Down) {
-                               int sel_index = fontsizeListBox.SelectedIndex;
-                               
-                               sel_index++;
-                               
-                               if (sel_index > fontsizeListBox.Items.Count - 1)
-                                       sel_index = fontsizeListBox.Items.Count - 1;
-                               
-                               fontsizeListBox.SelectedIndex = sel_index;
+                       // Forward these keys on to the font size listbox
+                       switch (e.KeyCode) {
+                               case Keys.Up:
+                               case Keys.Down:
+                               case Keys.PageDown:
+                               case Keys.PageUp:
+                                       fontsizeListBox.HandleKeyDown (e.KeyCode);
+                                       break;
                        }
                }
                
@@ -999,18 +962,27 @@ namespace System.Windows.Forms
                        
                        internal_textbox_change = false;
                        
-                       for (int i = 0; i < fontListBox.Items.Count; i++) {
-                               string name = fontListBox.Items [i] as string;
-                               
-                               if (name.StartsWith(fontTextBox.Text)) {
-                                       if (name == fontTextBox.Text)
-                                               fontListBox.SelectedIndex = i;
-                                       else
-                                               fontListBox.TopIndex = i;
-                                       
-                                       break;
-                               }
+                       string search = fontTextBox.Text;
+                       
+                       // Look for an exact match
+                       int found = fontListBox.FindStringExact (search);
+                       
+                       if (found != ListBox.NoMatches) {
+                               fontListBox.SelectedIndex = found;
+                               return;
+                       }
+                       
+                       // Look for a partial match
+                       found = fontListBox.FindString (search);
+                       
+                       if (found != ListBox.NoMatches) {
+                               fontListBox.TopIndex = found;
+                               return;
                        }
+
+                       // No match, scroll to the top
+                       if (fontListBox.Items.Count > 0)
+                               fontListBox.TopIndex = 0;
                }
                
                void OnFontStyleTextTextChanged (object sender, EventArgs e)
@@ -1019,17 +991,12 @@ namespace System.Windows.Forms
                                return;
                        
                        internal_textbox_change = false;
-                       
-                       for (int i = 0; i < fontstyleListBox.Items.Count; i++) {
-                               string name = fontstyleListBox.Items [i] as string;
-                               
-                               if (name.StartsWith(fontstyleTextBox.Text)) {
-                                       if (name == fontstyleTextBox.Text)
-                                               fontstyleListBox.SelectedIndex = i;
-                                       
-                                       break;
-                               }
-                       }
+
+                       // Look for an exact match
+                       int found = fontstyleListBox.FindStringExact (fontstyleTextBox.Text);
+
+                       if (found != ListBox.NoMatches)
+                               fontstyleListBox.SelectedIndex = found;
                }
                
                void OnFontSizeTextBoxTextChanged (object sender, EventArgs e)
@@ -1079,7 +1046,7 @@ namespace System.Windows.Forms
                
                void UpdateFontSizeListBox ()
                {
-                       int index = fontsizeListBox.FindString(currentSize.ToString());
+                       int index = fontsizeListBox.FindString(((int)Math.Round ((currentSize))).ToString());
                        
                        if (index != -1)
                                fontsizeListBox.SelectedIndex = index;
@@ -1130,8 +1097,29 @@ namespace System.Windows.Forms
                                        to_select = index;
                        }
                        
-                       if (fontstyleListBox.Items.Count > 0)
+                       if (fontstyleListBox.Items.Count > 0) {
                                fontstyleListBox.SelectedIndex = to_select;
+
+                               switch ((string)fontstyleListBox.SelectedItem) {
+                                       case "Regular":
+                                               currentFontStyle = FontStyle.Regular;
+                                               break;
+                                       case "Bold":
+                                               currentFontStyle = FontStyle.Bold;
+                                               break;
+                                       case "Italic":
+                                               currentFontStyle = FontStyle.Italic;
+                                               break;
+                                       case "Bold Italic":
+                                               currentFontStyle = FontStyle.Bold | FontStyle.Italic;
+                                               break;
+                               }
+                               
+                               if (strikethroughCheckBox.Checked)
+                                       currentFontStyle |= FontStyle.Strikeout;
+                               if (underlinedCheckBox.Checked)
+                                       currentFontStyle |= FontStyle.Underline;
+                       }
                        
                        fontstyleListBox.EndUpdate( );
                }
@@ -1145,7 +1133,7 @@ namespace System.Windows.Forms
                {
                        fontsizeListBox.BeginUpdate ();
                        
-                       fontsizeListBox.Items. Clear();
+                       fontsizeListBox.Items.Clear();
                        
                        if (minSize == 0 && maxSize == 0)
                        {
@@ -1160,6 +1148,58 @@ namespace System.Windows.Forms
                        
                        fontsizeListBox.EndUpdate ();
                }
+
+               #region Private Methods
+               private void PopulateFontList ()
+               {
+                       fontListBox.Items.Clear ();
+                       fontHash.Clear ();
+
+                       fontListBox.BeginUpdate ();
+
+                       foreach (FontFamily ff in FontFamily.Families) {
+                               if (!fontHash.ContainsKey (ff.Name)) {
+                                       if (!fixedPitchOnly || (IsFontFamilyFixedPitch (ff))) {
+                                               fontListBox.Items.Add (ff.Name);
+                                               fontHash.Add (ff.Name, ff);
+                                       }
+                               }
+                       }
+                       
+                       fontListBox.EndUpdate ();
+                       CreateFontSizeListBoxItems ();
+
+                       if (fixedPitchOnly)
+                               this.Font = new Font (FontFamily.GenericMonospace, 8.25f);
+                       else
+                               this.Font = form.Font;  
+               }
+               
+               private bool IsFontFamilyFixedPitch (FontFamily family)
+               {
+                       FontStyle fs;
+                       
+                       if (family.IsStyleAvailable (FontStyle.Regular))
+                               fs = FontStyle.Regular;
+                       else if (family.IsStyleAvailable (FontStyle.Bold))
+                               fs = FontStyle.Bold;
+                       else if (family.IsStyleAvailable (FontStyle.Italic))
+                               fs = FontStyle.Italic;
+                       else if (family.IsStyleAvailable (FontStyle.Strikeout))
+                               fs = FontStyle.Strikeout;
+                       else if (family.IsStyleAvailable (FontStyle.Underline))
+                               fs = FontStyle.Underline;
+                       else
+                               return false;
+
+                       Font f = new Font (family.Name, 10, fs);
+
+                       if (TextRenderer.MeasureString ("i", f).Width == TextRenderer.MeasureString ("w", f).Width)
+                               return true;
+                               
+                       return false;
+               }
+               #endregion
                
                internal class ColorComboBox : ComboBox
                {
@@ -1195,6 +1235,11 @@ namespace System.Windows.Forms
                                                return name;
                                        }
                                }
+
+                               public override string ToString()
+                               {
+                                       return this.Name;
+                               }
                        }
                        
                        private Color selectedColor;
@@ -1208,26 +1253,27 @@ namespace System.Windows.Forms
                                DropDownStyle = ComboBoxStyle.DropDownList;
                                DrawMode = DrawMode.OwnerDrawFixed;
                                
-                               Items.AddRangenew object[] {
-                                                      new ColorComboBoxItem( Color.Black, "Black" ),
-                                                      new ColorComboBoxItem( Color.DarkRed, "Dark-Red" ),
-                                                      new ColorComboBoxItem( Color.Green, "Green" ),
-                                                      new ColorComboBoxItem( Color.Olive, "Olive-Green" ), // color not correct
-                                                      new ColorComboBoxItem( Color.Aquamarine, "Aquamarine" ), // color not correct
-                                                      new ColorComboBoxItem( Color.Crimson, "Crimson" ),
-                                                      new ColorComboBoxItem( Color.Cyan, "Cyan" ),
-                                                      new ColorComboBoxItem( Color.Gray, "Gray" ),
-                                                      new ColorComboBoxItem( Color.Silver, "Silver" ),
-                                                      new ColorComboBoxItem( Color.Red, "Red" ),
-                                                      new ColorComboBoxItem( Color.YellowGreen, "Yellow-Green" ),
-                                                      new ColorComboBoxItem( Color.Yellow, "Yellow" ),
-                                                      new ColorComboBoxItem( Color.Blue, "Blue" ),
-                                                      new ColorComboBoxItem( Color.Purple, "Purple" ),
-                                                      new ColorComboBoxItem( Color.Aquamarine, "Aquamarine" ),
-                                                      new ColorComboBoxItem( Color.White, "White" ) }
-                                              );
+                               Items.AddRange (new object[] {
+                                       new ColorComboBoxItem (Color.Black, "Black"),
+                                       new ColorComboBoxItem (Color.Maroon, "Maroon"),
+                                       new ColorComboBoxItem (Color.Green, "Green"),
+                                       new ColorComboBoxItem (Color.Olive, "Olive"),
+                                       new ColorComboBoxItem (Color.Navy, "Navy"),
+                                       new ColorComboBoxItem (Color.Purple, "Purple"),
+                                       new ColorComboBoxItem (Color.Teal, "Teal"),
+                                       new ColorComboBoxItem (Color.Gray, "Gray"),
+                                       new ColorComboBoxItem (Color.Silver, "Silver"),
+                                       new ColorComboBoxItem (Color.Red, "Red"),
+                                       new ColorComboBoxItem (Color.Lime, "Lime"),
+                                       new ColorComboBoxItem (Color.Yellow, "Yellow"),
+                                       new ColorComboBoxItem (Color.Blue, "Blue"),
+                                       new ColorComboBoxItem (Color.Fuchsia, "Fuchsia"),
+                                       new ColorComboBoxItem (Color.Aqua, "Aqua"),
+                                       new ColorComboBoxItem (Color.White, "White") }
+                               );
                                
                                SelectedIndex = 0;
+                               MaxDropDownItems = 16;
                        }
                        
                        protected override void OnDrawItem( DrawItemEventArgs e )