1 // System.Windows.Forms.ToolBar.cs
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 // Ravindra (rkumar@novell.com)
24 // Mike Kestner <mkestner@novell.com>
29 // Copyright (C) 2004-2006 Novell, Inc. (http://www.novell.com)
35 using System.Collections;
36 using System.ComponentModel;
37 using System.ComponentModel.Design;
39 using System.Drawing.Imaging;
40 using System.Runtime.InteropServices;
42 namespace System.Windows.Forms
44 [DefaultEvent ("ButtonClick")]
45 [DefaultProperty ("Buttons")]
46 [Designer ("System.Windows.Forms.Design.ToolBarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
47 public class ToolBar : Control
49 #region Instance Variables
50 bool size_specified = false;
51 ToolBarButton current_button;
52 #endregion Instance Variables
56 [EditorBrowsable (EditorBrowsableState.Never)]
57 public new event EventHandler BackColorChanged {
58 add { base.BackColorChanged += value; }
59 remove { base.BackColorChanged -= value; }
63 [EditorBrowsable (EditorBrowsableState.Never)]
64 public new event EventHandler BackgroundImageChanged {
65 add { base.BackgroundImageChanged += value; }
66 remove { base.BackgroundImageChanged -= value; }
69 public event ToolBarButtonClickEventHandler ButtonClick;
70 public event ToolBarButtonClickEventHandler ButtonDropDown;
73 [EditorBrowsable (EditorBrowsableState.Never)]
74 public new event EventHandler ForeColorChanged {
75 add { base.ForeColorChanged += value; }
76 remove { base.ForeColorChanged -= value; }
80 [EditorBrowsable (EditorBrowsableState.Never)]
81 public new event EventHandler ImeModeChanged {
82 add { base.ImeModeChanged += value; }
83 remove { base.ImeModeChanged -= value; }
87 [EditorBrowsable (EditorBrowsableState.Never)]
88 public new event PaintEventHandler Paint {
89 add { base.Paint += value; }
90 remove { base.Paint -= value; }
94 [EditorBrowsable (EditorBrowsableState.Never)]
95 public new event EventHandler RightToLeftChanged {
96 add { base.RightToLeftChanged += value; }
97 remove { base.RightToLeftChanged -= value; }
101 [EditorBrowsable (EditorBrowsableState.Never)]
102 public new event EventHandler TextChanged {
103 add { base.TextChanged += value; }
104 remove { base.TextChanged -= value; }
111 background_color = ThemeEngine.Current.DefaultControlBackColor;
112 foreground_color = ThemeEngine.Current.DefaultControlForeColor;
113 buttons = new ToolBarButtonCollection (this);
114 dock_style = DockStyle.Top;
116 GotFocus += new EventHandler (FocusChanged);
117 LostFocus += new EventHandler (FocusChanged);
118 MouseDown += new MouseEventHandler (ToolBar_MouseDown);
119 MouseLeave += new EventHandler (ToolBar_MouseLeave);
120 MouseMove += new MouseEventHandler (ToolBar_MouseMove);
121 MouseUp += new MouseEventHandler (ToolBar_MouseUp);
123 SetStyle (ControlStyles.UserPaint, false);
124 SetStyle (ControlStyles.FixedHeight, true);
126 #endregion Constructor
128 #region protected Properties
129 protected override CreateParams CreateParams
131 get { return base.CreateParams; }
134 protected override ImeMode DefaultImeMode {
135 get { return ImeMode.Disable; }
138 protected override Size DefaultSize {
139 get { return ThemeEngine.Current.ToolBarDefaultSize; }
143 ToolBarAppearance appearance = ToolBarAppearance.Normal;
145 #region Public Properties
146 [DefaultValue (ToolBarAppearance.Normal)]
148 public ToolBarAppearance Appearance {
149 get { return appearance; }
151 if (value == appearance)
159 bool autosize = true;
161 [DefaultValue (true)]
163 public bool AutoSize {
164 get { return autosize; }
166 if (value == autosize)
175 [EditorBrowsable (EditorBrowsableState.Never)]
176 public override Color BackColor {
177 get { return background_color; }
179 if (value == background_color)
182 background_color = value;
183 OnBackColorChanged (EventArgs.Empty);
189 [EditorBrowsable (EditorBrowsableState.Never)]
190 public override Image BackgroundImage {
191 get { return background_image; }
193 if (value == background_image)
196 background_image = value;
197 OnBackgroundImageChanged (EventArgs.Empty);
202 [DefaultValue (BorderStyle.None)]
203 [DispIdAttribute (-504)]
204 public BorderStyle BorderStyle {
205 get { return InternalBorderStyle; }
206 set { InternalBorderStyle = value; }
209 ToolBarButtonCollection buttons;
211 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
213 [MergableProperty (false)]
214 public ToolBarButtonCollection Buttons {
215 get { return buttons; }
221 [RefreshProperties (RefreshProperties.All)]
222 public Size ButtonSize {
224 if (button_size.IsEmpty) {
225 if (buttons.Count == 0)
226 return new Size (24, 22);
227 Size result = CalcButtonSize ();
229 return new Size (24, 22);
236 size_specified = value != Size.Empty;
237 if (button_size == value)
247 [DefaultValue (true)]
248 public bool Divider {
249 get { return divider; }
251 if (value == divider)
259 [DefaultValue (DockStyle.Top)]
261 public override DockStyle Dock {
262 get { return base.Dock; }
263 set { base.Dock = value; }
266 bool drop_down_arrows = false;
268 [DefaultValue (false)]
270 public bool DropDownArrows {
271 get { return drop_down_arrows; }
273 if (value == drop_down_arrows)
276 drop_down_arrows = value;
282 [EditorBrowsable (EditorBrowsableState.Never)]
283 public override Color ForeColor {
284 get { return foreground_color; }
286 if (value == foreground_color)
289 foreground_color = value;
290 OnForeColorChanged (EventArgs.Empty);
295 ImageList image_list;
297 [DefaultValue (null)]
298 public ImageList ImageList {
299 get { return image_list; }
301 if (image_list == value)
309 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
310 [EditorBrowsable (EditorBrowsableState.Advanced)]
311 public Size ImageSize {
313 if (ImageList == null)
316 return ImageList.ImageSize;
323 [EditorBrowsable (EditorBrowsableState.Never)]
324 public new ImeMode ImeMode {
325 get { return ime_mode; }
327 if (value == ime_mode)
331 OnImeModeChanged (EventArgs.Empty);
336 [EditorBrowsable (EditorBrowsableState.Never)]
337 public override RightToLeft RightToLeft {
338 get { return base.RightToLeft; }
340 if (value == base.RightToLeft)
343 base.RightToLeft = value;
344 OnRightToLeftChanged (EventArgs.Empty);
348 bool show_tooltips = false;
350 [DefaultValue (false)]
352 public bool ShowToolTips {
353 get { return show_tooltips; }
354 set { show_tooltips = value; }
357 [DefaultValue (false)]
358 public new bool TabStop {
359 get { return base.TabStop; }
360 set { base.TabStop = value; }
365 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
366 [EditorBrowsable (EditorBrowsableState.Never)]
367 public override string Text {
375 OnTextChanged (EventArgs.Empty);
379 ToolBarTextAlign text_alignment = ToolBarTextAlign.Underneath;
381 [DefaultValue (ToolBarTextAlign.Underneath)]
383 public ToolBarTextAlign TextAlign {
384 get { return text_alignment; }
386 if (value == text_alignment)
389 text_alignment = value;
394 bool wrappable = true;
396 [DefaultValue (true)]
398 public bool Wrappable {
399 get { return wrappable; }
401 if (value == wrappable)
408 #endregion Public Properties
410 #region Public Methods
411 public override string ToString ()
413 int count = this.Buttons.Count;
416 return string.Format ("System.Windows.Forms.ToolBar, Button.Count: 0");
418 return string.Format ("System.Windows.Forms.ToolBar, Button.Count: {0}, Buttons[0]: {1}",
419 count, this.Buttons [0].ToString ());
421 #endregion Public Methods
423 #region Protected Methods
424 protected override void CreateHandle ()
426 base.CreateHandle ();
429 protected override void Dispose (bool disposing)
434 base.Dispose (disposing);
437 protected virtual void OnButtonClick (ToolBarButtonClickEventArgs e)
439 if (e.Button.Style == ToolBarButtonStyle.ToggleButton) {
440 if (! e.Button.Pushed)
441 e.Button.Pushed = true;
443 e.Button.Pushed = false;
445 e.Button.pressed = false;
447 Invalidate (e.Button.Rectangle);
449 if (ButtonClick != null)
450 ButtonClick (this, e);
453 protected virtual void OnButtonDropDown (ToolBarButtonClickEventArgs e)
455 if (ButtonDropDown != null)
456 ButtonDropDown (this, e);
458 if (e.Button.DropDownMenu == null)
461 Point loc = new Point (e.Button.Rectangle.X + 1, e.Button.Rectangle.Bottom + 1);
462 ((ContextMenu) e.Button.DropDownMenu).Show (this, loc);
464 e.Button.dd_pressed = false;
465 Invalidate (e.Button.Rectangle);
468 protected override void OnFontChanged (EventArgs e)
470 base.OnFontChanged (e);
474 protected override void OnHandleCreated (EventArgs e)
476 base.OnHandleCreated (e);
479 protected override void OnResize (EventArgs e)
483 if (Width <= 0 || Height <= 0 || !Visible)
489 bool height_specified = false;
490 int requested_height = -1;
492 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
494 if ((specified & BoundsSpecified.Height) != 0) {
495 requested_height = height;
496 height_specified = true;
498 base.SetBoundsCore (x, y, width, height, specified);
501 protected override void WndProc (ref Message m)
503 base.WndProc (ref m);
506 internal override bool InternalPreProcessMessage (ref Message msg)
508 if (msg.Msg == (int)Msg.WM_KEYDOWN) {
509 Keys key_data = (Keys)msg.WParam.ToInt32();
510 if (HandleKeyDown (key_data))
513 return base.InternalPreProcessMessage (ref msg);
516 #endregion Protected Methods
518 #region Private Methods
519 private void FocusChanged (object sender, EventArgs args)
521 if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
524 ToolBarButton prelit = null;
525 foreach (ToolBarButton b in Buttons)
531 if (Focused && prelit == null)
532 foreach (ToolBarButton btn in Buttons) {
538 else if (prelit != null)
539 prelit.Hilight = false;
542 private bool HandleKeyDown (Keys key_data)
544 if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
550 HighlightButton (-1);
561 void HighlightButton (int offset)
563 ArrayList enabled = new ArrayList ();
566 ToolBarButton curr_button = null;
567 foreach (ToolBarButton btn in Buttons) {
579 int next = (start + offset) % count;
586 if (curr_button != null)
587 curr_button.Hilight = false;
588 (enabled [next] as ToolBarButton).Hilight = true;
591 private void ToolBar_MouseDown (object sender, MouseEventArgs me)
596 Point loc = new Point (me.X, me.Y);
598 // draw the pushed button
599 foreach (ToolBarButton button in buttons) {
600 if (button.Enabled && button.Rectangle.Contains (loc)) {
601 // Mark the DropDown rect as pressed.
602 // We don't redraw the dropdown rect.
603 if (button.Style == ToolBarButtonStyle.DropDownButton) {
604 Rectangle rect = button.Rectangle;
605 rect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
606 rect.X = button.Rectangle.Right - rect.Width;
607 if (rect.Contains (loc)) {
608 if (button.DropDownMenu != null) {
609 button.dd_pressed = true;
615 button.pressed = true;
616 button.inside = true;
617 Invalidate (button.Rectangle);
623 private void ToolBar_MouseUp (object sender, MouseEventArgs me)
628 Point loc = new Point (me.X, me.Y);
630 // draw the normal button
631 // Make a copy in case the list is modified during enumeration
632 ArrayList buttons = new ArrayList (this.buttons);
633 foreach (ToolBarButton button in buttons) {
634 if (button.Enabled && button.Rectangle.Contains (loc)) {
635 if (button.Style == ToolBarButtonStyle.DropDownButton) {
636 Rectangle ddRect = button.Rectangle;
637 ddRect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
638 ddRect.X = button.Rectangle.Right - ddRect.Width;
639 if (ddRect.Contains (loc)) {
640 if (button.dd_pressed)
641 OnButtonDropDown (new ToolBarButtonClickEventArgs (button));
645 // Fire a ButtonClick
647 OnButtonClick (new ToolBarButtonClickEventArgs (button));
648 } else if (button.pressed) {
649 button.pressed = false;
650 Invalidate (button.Rectangle);
655 private void ToolBar_MouseLeave (object sender, EventArgs e)
657 if (!Enabled || appearance != ToolBarAppearance.Flat || current_button == null)
660 if (current_button.Hilight) {
661 current_button.Hilight = false;
662 Invalidate (current_button.Rectangle);
664 current_button = null;
667 private void ToolBar_MouseMove (object sender, MouseEventArgs me)
672 Point loc = new Point (me.X, me.Y);
675 // If the button was pressed and we leave, release the
676 // button press and vice versa
677 foreach (ToolBarButton button in buttons) {
678 if (button.pressed &&
679 (button.inside != button.Rectangle.Contains (loc))) {
680 button.inside = button.Rectangle.Contains (loc);
681 button.Hilight = false;
682 Invalidate (button.Rectangle);
687 // following is only for flat style toolbar
688 else if (appearance == ToolBarAppearance.Flat) {
689 if (current_button != null && current_button.Rectangle.Contains (loc)) {
690 if (current_button.Hilight || current_button.Pushed)
692 current_button.Hilight = true;
693 Invalidate (current_button.Rectangle);
696 foreach (ToolBarButton button in buttons) {
697 if (button.Rectangle.Contains (loc) && button.Enabled) {
698 current_button = button;
699 if (current_button.Hilight || current_button.Pushed)
701 current_button.Hilight = true;
702 Invalidate (current_button.Rectangle);
704 else if (button.Hilight) {
705 button.Hilight = false;
706 Invalidate (button.Rectangle);
713 internal override void OnPaintInternal (PaintEventArgs pevent)
715 ThemeEngine.Current.DrawToolBar (pevent.Graphics, pevent.ClipRectangle, this);
718 internal void Redraw (bool recalculate)
726 internal bool SizeSpecified {
727 get { return size_specified; }
730 const int text_padding = 3;
732 private Size CalcButtonSize ()
734 if (Buttons.Count == 0)
737 string longest_text = Buttons [0].Text;
738 for (int i = 1; i < Buttons.Count; i++) {
739 if (Buttons[i].Text.Length > longest_text.Length)
740 longest_text = Buttons[i].Text;
743 Size size = Size.Empty;
744 if (longest_text != null && longest_text.Length > 0) {
745 SizeF sz = DeviceContext.MeasureString (longest_text, Font);
746 if (sz != SizeF.Empty)
747 size = new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding, (int) Math.Ceiling (sz.Height));
750 Size img_size = ImageList == null ? new Size (16, 16) : ImageSize;
752 Theme theme = ThemeEngine.Current;
753 int imgWidth = img_size.Width + 2 * theme.ToolBarImageGripWidth;
754 int imgHeight = img_size.Height + 2 * theme.ToolBarImageGripWidth;
756 if (text_alignment == ToolBarTextAlign.Right) {
757 size.Width = imgWidth + size.Width;
758 size.Height = (size.Height > imgHeight) ? size.Height : imgHeight;
760 size.Height = imgHeight + size.Height;
761 size.Width = (size.Width > imgWidth) ? size.Width : imgWidth;
764 size.Width += theme.ToolBarImageGripWidth;
765 size.Height += theme.ToolBarImageGripWidth;
769 // Flat toolbars disregard specified sizes. Normal toolbars grow the
770 // button size to be at least large enough to show the image.
771 Size AdjustedButtonSize {
773 Size size = ButtonSize;
774 if (size_specified) {
775 if (Appearance == ToolBarAppearance.Flat)
776 size = CalcButtonSize ();
778 int grip = ThemeEngine.Current.ToolBarImageGripWidth;
779 if (size.Width < ImageSize.Width + 2 * grip )
780 size.Width = ImageSize.Width + 2 * grip;
781 if (size.Height < ImageSize.Height + 2 * grip)
782 size.Height = ImageSize.Height + 2 * grip;
791 Theme theme = ThemeEngine.Current;
792 int x = theme.ToolBarGripWidth;
793 int y = theme.ToolBarGripWidth;
795 Size button_size = AdjustedButtonSize;
797 int ht = button_size.Height + theme.ToolBarGripWidth;
799 if (Wrappable && Parent != null) {
800 int separator_index = -1;
802 for (int i = 0; i < buttons.Count; i++) {
803 ToolBarButton button = buttons [i];
809 button.Layout (button_size);
813 bool is_separator = button.Style == ToolBarButtonStyle.Separator;
815 if (x + button.Rectangle.Width < Width || is_separator) {
816 button.Location = new Point (x, y);
817 x += button.Rectangle.Width;
820 } else if (separator_index > 0) {
822 separator_index = -1;
823 x = theme.ToolBarGripWidth;
826 x = theme.ToolBarGripWidth;
828 button.Location = new Point (x, y);
829 x += button.Rectangle.Width;
837 else if (!height_specified)
838 Height = DefaultSize.Height;
839 foreach (ToolBarButton button in buttons) {
841 button.Layout (button_size);
844 button.Location = new Point (x, y);
845 x += button.Rectangle.Width;
849 #endregion Private Methods
852 public class ToolBarButtonCollection : IList, ICollection, IEnumerable
854 #region instance variables
855 private ArrayList list;
856 private ToolBar owner;
860 public ToolBarButtonCollection (ToolBar owner)
863 list = new ArrayList ();
870 get { return list.Count; }
873 public bool IsReadOnly {
874 get { return list.IsReadOnly; }
877 public virtual ToolBarButton this [int index] {
878 get { return (ToolBarButton) list [index]; }
880 value.SetParent (owner);
881 list [index] = value;
886 bool ICollection.IsSynchronized {
887 get { return list.IsSynchronized; }
890 object ICollection.SyncRoot {
891 get { return list.SyncRoot; }
894 bool IList.IsFixedSize {
895 get { return list.IsFixedSize; }
898 object IList.this [int index] {
899 get { return this [index]; }
901 if (! (value is ToolBarButton))
902 throw new ArgumentException("Not of type ToolBarButton", "value");
903 this [index] = (ToolBarButton) value;
909 public int Add (string text)
911 ToolBarButton button = new ToolBarButton (text);
912 return this.Add (button);
915 public int Add (ToolBarButton button)
918 button.SetParent (owner);
919 result = list.Add (button);
924 public void AddRange (ToolBarButton [] buttons)
926 foreach (ToolBarButton button in buttons)
933 owner.Redraw (false);
936 public bool Contains (ToolBarButton button)
938 return list.Contains (button);
941 public IEnumerator GetEnumerator ()
943 return list.GetEnumerator ();
946 void ICollection.CopyTo (Array dest, int index)
948 list.CopyTo (dest, index);
951 int IList.Add (object button)
953 if (! (button is ToolBarButton)) {
954 throw new ArgumentException("Not of type ToolBarButton", "button");
957 return this.Add ((ToolBarButton) button);
960 bool IList.Contains (object button)
962 if (! (button is ToolBarButton)) {
963 throw new ArgumentException("Not of type ToolBarButton", "button");
966 return this.Contains ((ToolBarButton) button);
969 int IList.IndexOf (object button)
971 if (! (button is ToolBarButton)) {
972 throw new ArgumentException("Not of type ToolBarButton", "button");
975 return this.IndexOf ((ToolBarButton) button);
978 void IList.Insert (int index, object button)
980 if (! (button is ToolBarButton)) {
981 throw new ArgumentException("Not of type ToolBarButton", "button");
984 this.Insert (index, (ToolBarButton) button);
987 void IList.Remove (object button)
989 if (! (button is ToolBarButton)) {
990 throw new ArgumentException("Not of type ToolBarButton", "button");
993 this.Remove ((ToolBarButton) button);
996 public int IndexOf (ToolBarButton button)
998 return list.IndexOf (button);
1001 public void Insert (int index, ToolBarButton button)
1003 list.Insert (index, button);
1004 owner.Redraw (true);
1007 public void Remove (ToolBarButton button)
1009 list.Remove (button);
1010 owner.Redraw (true);
1013 public void RemoveAt (int index)
1015 list.RemoveAt (index);
1016 owner.Redraw (true);