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);
450 if (ButtonClick != null)
451 ButtonClick (this, e);
454 protected virtual void OnButtonDropDown (ToolBarButtonClickEventArgs e)
456 if (ButtonDropDown != null)
457 ButtonDropDown (this, e);
459 if (e.Button.DropDownMenu == null)
462 Point loc = new Point (e.Button.Rectangle.X + 1, e.Button.Rectangle.Bottom + 1);
463 ((ContextMenu) e.Button.DropDownMenu).Show (this, loc);
465 e.Button.dd_pressed = false;
466 Invalidate (e.Button.Rectangle);
469 protected override void OnFontChanged (EventArgs e)
471 base.OnFontChanged (e);
475 protected override void OnHandleCreated (EventArgs e)
477 base.OnHandleCreated (e);
480 protected override void OnResize (EventArgs e)
484 if (Width <= 0 || Height <= 0 || !Visible)
490 bool height_specified = false;
491 int requested_height = -1;
493 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
495 if ((specified & BoundsSpecified.Height) != 0) {
496 requested_height = height;
497 height_specified = true;
499 base.SetBoundsCore (x, y, width, height, specified);
502 protected override void WndProc (ref Message m)
504 base.WndProc (ref m);
507 internal override bool InternalPreProcessMessage (ref Message msg)
509 if (msg.Msg == (int)Msg.WM_KEYDOWN) {
510 Keys key_data = (Keys)msg.WParam.ToInt32();
511 if (HandleKeyDown (key_data))
514 return base.InternalPreProcessMessage (ref msg);
517 #endregion Protected Methods
519 #region Private Methods
520 private void FocusChanged (object sender, EventArgs args)
522 if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
525 ToolBarButton prelit = null;
526 foreach (ToolBarButton b in Buttons)
532 if (Focused && prelit == null)
533 foreach (ToolBarButton btn in Buttons) {
539 else if (prelit != null)
540 prelit.Hilight = false;
543 private bool HandleKeyDown (Keys key_data)
545 if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
551 HighlightButton (-1);
562 void HighlightButton (int offset)
564 ArrayList enabled = new ArrayList ();
567 ToolBarButton curr_button = null;
568 foreach (ToolBarButton btn in Buttons) {
580 int next = (start + offset) % count;
587 if (curr_button != null)
588 curr_button.Hilight = false;
589 (enabled [next] as ToolBarButton).Hilight = true;
592 private void ToolBar_MouseDown (object sender, MouseEventArgs me)
597 Point loc = new Point (me.X, me.Y);
599 // draw the pushed button
600 foreach (ToolBarButton button in buttons) {
601 if (button.Enabled && button.Rectangle.Contains (loc)) {
602 // Mark the DropDown rect as pressed.
603 // We don't redraw the dropdown rect.
604 if (button.Style == ToolBarButtonStyle.DropDownButton) {
605 Rectangle rect = button.Rectangle;
606 rect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
607 rect.X = button.Rectangle.Right - rect.Width;
608 if (rect.Contains (loc)) {
609 if (button.DropDownMenu != null) {
610 button.dd_pressed = true;
616 button.pressed = true;
617 button.inside = true;
618 Invalidate (button.Rectangle);
624 private void ToolBar_MouseUp (object sender, MouseEventArgs me)
629 Point loc = new Point (me.X, me.Y);
631 // draw the normal button
632 // Make a copy in case the list is modified during enumeration
633 ArrayList buttons = new ArrayList (this.buttons);
634 foreach (ToolBarButton button in buttons) {
635 if (button.Enabled && button.Rectangle.Contains (loc)) {
636 if (button.Style == ToolBarButtonStyle.DropDownButton) {
637 Rectangle ddRect = button.Rectangle;
638 ddRect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
639 ddRect.X = button.Rectangle.Right - ddRect.Width;
640 if (ddRect.Contains (loc)) {
641 if (button.dd_pressed)
642 OnButtonDropDown (new ToolBarButtonClickEventArgs (button));
646 // Fire a ButtonClick
648 OnButtonClick (new ToolBarButtonClickEventArgs (button));
649 } else if (button.pressed) {
650 button.pressed = false;
651 Invalidate (button.Rectangle);
656 private void ToolBar_MouseLeave (object sender, EventArgs e)
658 if (!Enabled || appearance != ToolBarAppearance.Flat || current_button == null)
661 if (current_button.Hilight) {
662 current_button.Hilight = false;
663 Invalidate (current_button.Rectangle);
666 current_button = null;
669 private void ToolBar_MouseMove (object sender, MouseEventArgs me)
674 Point loc = new Point (me.X, me.Y);
677 // If the button was pressed and we leave, release the
678 // button press and vice versa
679 foreach (ToolBarButton button in buttons) {
680 if (button.pressed &&
681 (button.inside != button.Rectangle.Contains (loc))) {
682 button.inside = button.Rectangle.Contains (loc);
683 button.Hilight = false;
684 Invalidate (button.Rectangle);
690 // following is only for flat style toolbar
691 else if (appearance == ToolBarAppearance.Flat) {
692 if (current_button != null && current_button.Rectangle.Contains (loc)) {
693 if (current_button.Hilight || current_button.Pushed)
695 current_button.Hilight = true;
696 Invalidate (current_button.Rectangle);
700 foreach (ToolBarButton button in buttons) {
701 if (button.Rectangle.Contains (loc) && button.Enabled) {
702 current_button = button;
703 if (current_button.Hilight || current_button.Pushed)
705 current_button.Hilight = true;
706 Invalidate (current_button.Rectangle);
709 else if (button.Hilight) {
710 button.Hilight = false;
711 Invalidate (button.Rectangle);
719 internal override void OnPaintInternal (PaintEventArgs pevent)
721 ThemeEngine.Current.DrawToolBar (pevent.Graphics, pevent.ClipRectangle, this);
724 internal void Redraw (bool recalculate)
732 internal bool SizeSpecified {
733 get { return size_specified; }
736 const int text_padding = 3;
738 private Size CalcButtonSize ()
740 if (Buttons.Count == 0)
743 string longest_text = Buttons [0].Text;
744 for (int i = 1; i < Buttons.Count; i++) {
745 if (Buttons[i].Text.Length > longest_text.Length)
746 longest_text = Buttons[i].Text;
749 Size size = Size.Empty;
750 if (longest_text != null && longest_text.Length > 0) {
751 SizeF sz = DeviceContext.MeasureString (longest_text, Font);
752 if (sz != SizeF.Empty)
753 size = new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding, (int) Math.Ceiling (sz.Height));
756 Size img_size = ImageList == null ? new Size (16, 16) : ImageSize;
758 Theme theme = ThemeEngine.Current;
759 int imgWidth = img_size.Width + 2 * theme.ToolBarImageGripWidth;
760 int imgHeight = img_size.Height + 2 * theme.ToolBarImageGripWidth;
762 if (text_alignment == ToolBarTextAlign.Right) {
763 size.Width = imgWidth + size.Width;
764 size.Height = (size.Height > imgHeight) ? size.Height : imgHeight;
766 size.Height = imgHeight + size.Height;
767 size.Width = (size.Width > imgWidth) ? size.Width : imgWidth;
770 size.Width += theme.ToolBarImageGripWidth;
771 size.Height += theme.ToolBarImageGripWidth;
775 // Flat toolbars disregard specified sizes. Normal toolbars grow the
776 // button size to be at least large enough to show the image.
777 Size AdjustedButtonSize {
779 Size size = ButtonSize;
780 if (size_specified) {
781 if (Appearance == ToolBarAppearance.Flat)
782 size = CalcButtonSize ();
784 int grip = ThemeEngine.Current.ToolBarImageGripWidth;
785 if (size.Width < ImageSize.Width + 2 * grip )
786 size.Width = ImageSize.Width + 2 * grip;
787 if (size.Height < ImageSize.Height + 2 * grip)
788 size.Height = ImageSize.Height + 2 * grip;
797 Theme theme = ThemeEngine.Current;
798 int x = theme.ToolBarGripWidth;
799 int y = theme.ToolBarGripWidth;
801 Size button_size = AdjustedButtonSize;
803 int ht = button_size.Height + theme.ToolBarGripWidth;
805 if (Wrappable && Parent != null) {
806 int separator_index = -1;
808 for (int i = 0; i < buttons.Count; i++) {
809 ToolBarButton button = buttons [i];
815 button.Layout (button_size);
819 bool is_separator = button.Style == ToolBarButtonStyle.Separator;
821 if (x + button.Rectangle.Width < Width || is_separator) {
822 button.Location = new Point (x, y);
823 x += button.Rectangle.Width;
826 } else if (separator_index > 0) {
828 separator_index = -1;
829 x = theme.ToolBarGripWidth;
832 x = theme.ToolBarGripWidth;
834 button.Location = new Point (x, y);
835 x += button.Rectangle.Width;
843 else if (!height_specified)
844 Height = DefaultSize.Height;
845 foreach (ToolBarButton button in buttons) {
847 button.Layout (button_size);
850 button.Location = new Point (x, y);
851 x += button.Rectangle.Width;
855 #endregion Private Methods
858 public class ToolBarButtonCollection : IList, ICollection, IEnumerable
860 #region instance variables
861 private ArrayList list;
862 private ToolBar owner;
866 public ToolBarButtonCollection (ToolBar owner)
869 list = new ArrayList ();
876 get { return list.Count; }
879 public bool IsReadOnly {
880 get { return list.IsReadOnly; }
883 public virtual ToolBarButton this [int index] {
884 get { return (ToolBarButton) list [index]; }
886 value.SetParent (owner);
887 list [index] = value;
892 bool ICollection.IsSynchronized {
893 get { return list.IsSynchronized; }
896 object ICollection.SyncRoot {
897 get { return list.SyncRoot; }
900 bool IList.IsFixedSize {
901 get { return list.IsFixedSize; }
904 object IList.this [int index] {
905 get { return this [index]; }
907 if (! (value is ToolBarButton))
908 throw new ArgumentException("Not of type ToolBarButton", "value");
909 this [index] = (ToolBarButton) value;
915 public int Add (string text)
917 ToolBarButton button = new ToolBarButton (text);
918 return this.Add (button);
921 public int Add (ToolBarButton button)
924 button.SetParent (owner);
925 result = list.Add (button);
930 public void AddRange (ToolBarButton [] buttons)
932 foreach (ToolBarButton button in buttons)
939 owner.Redraw (false);
942 public bool Contains (ToolBarButton button)
944 return list.Contains (button);
947 public IEnumerator GetEnumerator ()
949 return list.GetEnumerator ();
952 void ICollection.CopyTo (Array dest, int index)
954 list.CopyTo (dest, index);
957 int IList.Add (object button)
959 if (! (button is ToolBarButton)) {
960 throw new ArgumentException("Not of type ToolBarButton", "button");
963 return this.Add ((ToolBarButton) button);
966 bool IList.Contains (object button)
968 if (! (button is ToolBarButton)) {
969 throw new ArgumentException("Not of type ToolBarButton", "button");
972 return this.Contains ((ToolBarButton) button);
975 int IList.IndexOf (object button)
977 if (! (button is ToolBarButton)) {
978 throw new ArgumentException("Not of type ToolBarButton", "button");
981 return this.IndexOf ((ToolBarButton) button);
984 void IList.Insert (int index, object button)
986 if (! (button is ToolBarButton)) {
987 throw new ArgumentException("Not of type ToolBarButton", "button");
990 this.Insert (index, (ToolBarButton) button);
993 void IList.Remove (object button)
995 if (! (button is ToolBarButton)) {
996 throw new ArgumentException("Not of type ToolBarButton", "button");
999 this.Remove ((ToolBarButton) button);
1002 public int IndexOf (ToolBarButton button)
1004 return list.IndexOf (button);
1007 public void Insert (int index, ToolBarButton button)
1009 list.Insert (index, button);
1010 owner.Redraw (true);
1013 public void Remove (ToolBarButton button)
1015 list.Remove (button);
1016 owner.Redraw (true);
1019 public void RemoveAt (int index)
1021 list.RemoveAt (index);
1022 owner.Redraw (true);