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>
25 // Everaldo Canuto <ecanuto@novell.com>
27 // Copyright (C) 2004-2006 Novell, Inc. (http://www.novell.com)
30 using System.Collections;
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
34 using System.Drawing.Text;
35 using System.Drawing.Imaging;
36 using System.Runtime.InteropServices;
38 namespace System.Windows.Forms
42 [ClassInterface (ClassInterfaceType.AutoDispatch)]
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 private bool size_specified = false;
51 private ToolBarItem current_item;
52 internal ToolBarItem[] items;
53 internal Size default_size;
54 #endregion Instance Variables
57 static object ButtonClickEvent = new object ();
58 static object ButtonDropDownEvent = new object ();
62 [EditorBrowsable (EditorBrowsableState.Always)]
63 public new event EventHandler AutoSizeChanged {
64 add { base.AutoSizeChanged += value; }
65 remove { base.AutoSizeChanged -= value; }
70 [EditorBrowsable (EditorBrowsableState.Never)]
71 public new event EventHandler BackColorChanged {
72 add { base.BackColorChanged += value; }
73 remove { base.BackColorChanged -= value; }
77 [EditorBrowsable (EditorBrowsableState.Never)]
78 public new event EventHandler BackgroundImageChanged {
79 add { base.BackgroundImageChanged += value; }
80 remove { base.BackgroundImageChanged -= value; }
85 [EditorBrowsable (EditorBrowsableState.Never)]
86 public new event EventHandler BackgroundImageLayoutChanged {
87 add { base.BackgroundImageLayoutChanged += value; }
88 remove { base.BackgroundImageLayoutChanged -= value; }
92 public event ToolBarButtonClickEventHandler ButtonClick {
93 add { Events.AddHandler (ButtonClickEvent, value); }
94 remove {Events.RemoveHandler (ButtonClickEvent, value); }
97 public event ToolBarButtonClickEventHandler ButtonDropDown {
98 add { Events.AddHandler (ButtonDropDownEvent, value); }
99 remove {Events.RemoveHandler (ButtonDropDownEvent, value); }
103 [EditorBrowsable (EditorBrowsableState.Never)]
104 public new event EventHandler ForeColorChanged {
105 add { base.ForeColorChanged += value; }
106 remove { base.ForeColorChanged -= value; }
110 [EditorBrowsable (EditorBrowsableState.Never)]
111 public new event EventHandler ImeModeChanged {
112 add { base.ImeModeChanged += value; }
113 remove { base.ImeModeChanged -= value; }
117 [EditorBrowsable (EditorBrowsableState.Never)]
118 public new event PaintEventHandler Paint {
119 add { base.Paint += value; }
120 remove { base.Paint -= value; }
124 [EditorBrowsable (EditorBrowsableState.Never)]
125 public new event EventHandler RightToLeftChanged {
126 add { base.RightToLeftChanged += value; }
127 remove { base.RightToLeftChanged -= value; }
131 [EditorBrowsable (EditorBrowsableState.Never)]
132 public new event EventHandler TextChanged {
133 add { base.TextChanged += value; }
134 remove { base.TextChanged -= value; }
141 background_color = ThemeEngine.Current.DefaultControlBackColor;
142 foreground_color = ThemeEngine.Current.DefaultControlForeColor;
143 buttons = new ToolBarButtonCollection (this);
144 Dock = DockStyle.Top;
146 GotFocus += new EventHandler (FocusChanged);
147 LostFocus += new EventHandler (FocusChanged);
148 MouseDown += new MouseEventHandler (ToolBar_MouseDown);
149 MouseHover += new EventHandler (ToolBar_MouseHover);
150 MouseLeave += new EventHandler (ToolBar_MouseLeave);
151 MouseMove += new MouseEventHandler (ToolBar_MouseMove);
152 MouseUp += new MouseEventHandler (ToolBar_MouseUp);
153 BackgroundImageChanged += new EventHandler (ToolBar_BackgroundImageChanged);
157 SetStyle (ControlStyles.UserPaint, false);
158 SetStyle (ControlStyles.FixedHeight, true);
159 SetStyle (ControlStyles.FixedWidth, false);
161 #endregion Constructor
163 #region protected Properties
164 protected override CreateParams CreateParams {
166 CreateParams create_params = base.CreateParams;
168 if (appearance == ToolBarAppearance.Flat) {
169 create_params.Style |= (int) ToolBarStyles.TBSTYLE_FLAT;
172 return create_params;
176 protected override ImeMode DefaultImeMode {
177 get { return ImeMode.Disable; }
180 protected override Size DefaultSize {
181 get { return ThemeEngine.Current.ToolBarDefaultSize; }
185 [EditorBrowsable (EditorBrowsableState.Never)]
186 protected override bool DoubleBuffered {
187 get { return base.DoubleBuffered; }
188 set { base.DoubleBuffered = value; }
193 ToolBarAppearance appearance = ToolBarAppearance.Normal;
195 #region Public Properties
196 [DefaultValue (ToolBarAppearance.Normal)]
198 public ToolBarAppearance Appearance {
199 get { return appearance; }
201 if (value == appearance)
209 bool autosize = true;
213 [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
214 [EditorBrowsable (EditorBrowsableState.Always)]
216 [DefaultValue (true)]
225 get { return autosize; }
227 if (value == autosize)
238 [EditorBrowsable (EditorBrowsableState.Never)]
239 public override Color BackColor {
240 get { return background_color; }
242 if (value == background_color)
245 background_color = value;
246 OnBackColorChanged (EventArgs.Empty);
252 [EditorBrowsable (EditorBrowsableState.Never)]
253 public override Image BackgroundImage {
254 get { return base.BackgroundImage; }
255 set { base.BackgroundImage = value; }
260 [EditorBrowsable (EditorBrowsableState.Never)]
261 public override ImageLayout BackgroundImageLayout {
262 get { return base.BackgroundImageLayout; }
263 set { base.BackgroundImageLayout = value; }
267 [DefaultValue (BorderStyle.None)]
268 [DispIdAttribute (-504)]
269 public BorderStyle BorderStyle {
270 get { return InternalBorderStyle; }
271 set { InternalBorderStyle = value; }
274 ToolBarButtonCollection buttons;
276 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
278 [MergableProperty (false)]
279 public ToolBarButtonCollection Buttons {
280 get { return buttons; }
286 [RefreshProperties (RefreshProperties.All)]
287 public Size ButtonSize {
289 if (!button_size.IsEmpty)
292 if (buttons.Count == 0)
293 return new Size (39, 36);
295 Size result = CalcButtonSize ();
297 return new Size (24, 22);
302 size_specified = value != Size.Empty;
303 if (button_size == value)
313 [DefaultValue (true)]
314 public bool Divider {
315 get { return divider; }
317 if (value == divider)
325 [DefaultValue (DockStyle.Top)]
327 public override DockStyle Dock {
328 get { return base.Dock; }
330 if (base.Dock == value) {
331 // Call base anyways so layout_type gets set correctly
332 if (value != DockStyle.None)
338 SetStyle (ControlStyles.FixedWidth, AutoSize);
339 SetStyle (ControlStyles.FixedHeight, false);
341 SetStyle (ControlStyles.FixedHeight, AutoSize);
342 SetStyle (ControlStyles.FixedWidth, false);
351 bool drop_down_arrows = true;
353 [DefaultValue (false)]
355 public bool DropDownArrows {
356 get { return drop_down_arrows; }
358 if (value == drop_down_arrows)
361 drop_down_arrows = value;
367 [EditorBrowsable (EditorBrowsableState.Never)]
368 public override Color ForeColor {
369 get { return foreground_color; }
371 if (value == foreground_color)
374 foreground_color = value;
375 OnForeColorChanged (EventArgs.Empty);
380 ImageList image_list;
382 [DefaultValue (null)]
383 public ImageList ImageList {
384 get { return image_list; }
386 if (image_list == value)
394 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
395 [EditorBrowsable (EditorBrowsableState.Advanced)]
396 public Size ImageSize {
398 if (ImageList == null)
401 return ImageList.ImageSize;
405 // XXX this should probably go away and it should call
406 // into Control.ImeMode instead.
407 new ImeMode ime_mode = ImeMode.Disable;
410 [EditorBrowsable (EditorBrowsableState.Never)]
411 public new ImeMode ImeMode {
412 get { return ime_mode; }
414 if (value == ime_mode)
418 OnImeModeChanged (EventArgs.Empty);
423 [EditorBrowsable (EditorBrowsableState.Never)]
424 public override RightToLeft RightToLeft {
425 get { return base.RightToLeft; }
427 if (value == base.RightToLeft)
430 base.RightToLeft = value;
431 OnRightToLeftChanged (EventArgs.Empty);
435 // Default value is "false" but after make a test in .NET we get "true" result as default.
436 bool show_tooltips = true;
438 [DefaultValue (false)]
440 public bool ShowToolTips {
441 get { return show_tooltips; }
442 set { show_tooltips = value; }
445 [DefaultValue (false)]
446 public new bool TabStop {
447 get { return base.TabStop; }
448 set { base.TabStop = value; }
453 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
454 [EditorBrowsable (EditorBrowsableState.Never)]
455 public override string Text {
456 get { return base.Text; }
458 if (value == base.Text)
466 ToolBarTextAlign text_alignment = ToolBarTextAlign.Underneath;
468 [DefaultValue (ToolBarTextAlign.Underneath)]
470 public ToolBarTextAlign TextAlign {
471 get { return text_alignment; }
473 if (value == text_alignment)
476 text_alignment = value;
481 bool wrappable = true;
483 [DefaultValue (true)]
485 public bool Wrappable {
486 get { return wrappable; }
488 if (value == wrappable)
495 #endregion Public Properties
497 #region Public Methods
498 public override string ToString ()
500 int count = this.Buttons.Count;
503 return string.Format ("System.Windows.Forms.ToolBar, Buttons.Count: 0");
505 return string.Format ("System.Windows.Forms.ToolBar, Buttons.Count: {0}, Buttons[0]: {1}",
506 count, this.Buttons [0].ToString ());
508 #endregion Public Methods
510 #region Protected Methods
511 protected override void CreateHandle ()
513 base.CreateHandle ();
514 default_size = CalcButtonSize ();
516 // In win32 the recalculate size only happens for not flat style
517 if (appearance != ToolBarAppearance.Flat)
521 protected override void Dispose (bool disposing)
526 base.Dispose (disposing);
529 protected virtual void OnButtonClick (ToolBarButtonClickEventArgs e)
531 if (e.Button.Style == ToolBarButtonStyle.ToggleButton) {
532 if (! e.Button.Pushed)
533 e.Button.Pushed = true;
535 e.Button.Pushed = false;
538 current_item.Pressed = false;
539 current_item.Invalidate ();
541 ToolBarButtonClickEventHandler eh = (ToolBarButtonClickEventHandler)(Events [ButtonClickEvent]);
546 protected virtual void OnButtonDropDown (ToolBarButtonClickEventArgs e)
548 ToolBarButtonClickEventHandler eh = (ToolBarButtonClickEventHandler)(Events [ButtonDropDownEvent]);
552 if (e.Button.DropDownMenu == null)
555 ToolBarItem item = current_item;
557 Point loc = new Point (item.Rectangle.X + 1, item.Rectangle.Bottom + 1);
558 ((ContextMenu) e.Button.DropDownMenu).Show (this, loc);
560 item.DDPressed = false;
561 item.Hilight = false;
565 protected override void OnFontChanged (EventArgs e)
567 base.OnFontChanged (e);
571 protected override void OnHandleCreated (EventArgs e)
573 base.OnHandleCreated (e);
576 protected override void OnResize (EventArgs e)
583 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
585 specified &= ~BoundsSpecified.Height;
587 base.ScaleControl (factor, specified);
590 [EditorBrowsable (EditorBrowsableState.Never)]
591 protected override void ScaleCore (float dx, float dy)
595 base.ScaleCore (dx, dy);
599 private int requested_size = -1;
601 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
604 if (!AutoSize && (requested_size != width) && ((specified & BoundsSpecified.Width) != BoundsSpecified.None))
605 requested_size = width;
607 if (!AutoSize && (requested_size != height) && ((specified & BoundsSpecified.Height) != BoundsSpecified.None))
608 requested_size = height;
611 base.SetBoundsCore (x, y, width, height, specified);
614 protected override void WndProc (ref Message m)
616 base.WndProc (ref m);
619 internal override bool InternalPreProcessMessage (ref Message msg)
621 if (msg.Msg == (int)Msg.WM_KEYDOWN) {
622 Keys key_data = (Keys)msg.WParam.ToInt32();
623 if (HandleKeyDown (key_data))
626 return base.InternalPreProcessMessage (ref msg);
629 #endregion Protected Methods
631 #region Private Methods
632 private void FocusChanged (object sender, EventArgs args)
634 if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
637 ToolBarItem prelit = null;
638 foreach (ToolBarItem item in items) {
645 if (Focused && prelit == null) {
646 foreach (ToolBarItem item in items) {
647 if (item.Button.Enabled) {
652 } else if (prelit != null) {
653 prelit.Hilight = false;
657 private bool HandleKeyDown (Keys key_data)
659 if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
665 HighlightButton (-1);
676 void HighlightButton (int offset)
678 ArrayList enabled = new ArrayList ();
681 ToolBarItem curr_item = null;
682 foreach (ToolBarItem item in items) {
688 if (item.Button.Enabled) {
694 int next = (start + offset) % count;
701 if (curr_item != null)
702 curr_item.Hilight = false;
703 (enabled [next] as ToolBarItem).Hilight = true;
706 private void ToolBar_BackgroundImageChanged (object sender, EventArgs args)
708 Redraw (false, true);
711 private void ToolBar_MouseDown (object sender, MouseEventArgs me)
713 if ((!Enabled) || ((me.Button & MouseButtons.Left) == 0))
716 Point loc = new Point (me.X, me.Y);
718 if (ItemAtPoint (loc) == null)
721 // Hide tooltip when left mouse button
722 if ((tip_window != null) && (tip_window.Visible) && ((me.Button & MouseButtons.Left) == MouseButtons.Left)) {
723 TipDownTimer.Stop ();
724 tip_window.Hide (this);
727 // draw the pushed button
728 foreach (ToolBarItem item in items) {
729 if (item.Button.Enabled && item.Rectangle.Contains (loc)) {
730 // Mark the DropDown rect as pressed.
731 // We don't redraw the dropdown rect.
732 if (item.Button.Style == ToolBarButtonStyle.DropDownButton) {
733 Rectangle rect = item.Rectangle;
734 if (DropDownArrows) {
735 rect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
736 rect.X = item.Rectangle.Right - rect.Width;
739 if (rect.Contains (loc)) {
740 if (item.Button.DropDownMenu != null) {
741 item.DDPressed = true;
755 private void ToolBar_MouseUp (object sender, MouseEventArgs me)
757 if ((!Enabled) || ((me.Button & MouseButtons.Left) == 0))
760 Point loc = new Point (me.X, me.Y);
762 // draw the normal button
763 // Make a copy in case the list is modified during enumeration
764 ArrayList items = new ArrayList (this.items);
765 foreach (ToolBarItem item in items) {
766 if (item.Button.Enabled && item.Rectangle.Contains (loc)) {
767 if (item.Button.Style == ToolBarButtonStyle.DropDownButton) {
768 Rectangle ddRect = item.Rectangle;
769 ddRect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
770 ddRect.X = item.Rectangle.Right - ddRect.Width;
771 if (ddRect.Contains (loc)) {
774 OnButtonDropDown (new ToolBarButtonClickEventArgs (item.Button));
778 // Fire a ButtonClick
780 if ((item.Pressed) && ((me.Button & MouseButtons.Left) == MouseButtons.Left))
781 OnButtonClick (new ToolBarButtonClickEventArgs (item.Button));
782 } else if (item.Pressed) {
783 item.Pressed = false;
789 private ToolBarItem ItemAtPoint (Point pt)
791 foreach (ToolBarItem item in items)
792 if (item.Rectangle.Contains (pt))
798 ToolTip tip_window = null;
799 Timer tipdown_timer = null;
801 private void PopDownTip (object o, EventArgs args)
803 tip_window.Hide (this);
806 private Timer TipDownTimer {
808 if (tipdown_timer == null) {
809 tipdown_timer = new Timer ();
810 tipdown_timer.Enabled = false;
811 tipdown_timer.Interval = 5000;
812 tipdown_timer.Tick += new EventHandler (PopDownTip);
814 return tipdown_timer;
818 private void ToolBar_MouseHover (object sender, EventArgs e)
823 if (tip_window == null)
824 tip_window = new ToolTip ();
826 ToolBarItem item = ItemAtPoint (PointToClient (Control.MousePosition));
829 if (item == null || item.Button.ToolTipText.Length == 0)
832 tip_window.Present (this, item.Button.ToolTipText);
833 TipDownTimer.Start ();
836 private void ToolBar_MouseLeave (object sender, EventArgs e)
838 if (tipdown_timer != null)
839 tipdown_timer.Dispose ();
840 tipdown_timer = null;
841 if (tip_window != null)
842 tip_window.Dispose ();
845 if (!Enabled || current_item == null)
848 current_item.Hilight = false;
852 private void ToolBar_MouseMove (object sender, MouseEventArgs me)
857 if (tip_window != null && tip_window.Visible) {
858 TipDownTimer.Stop ();
859 TipDownTimer.Start ();
862 Point loc = new Point (me.X, me.Y);
865 // If the button was pressed and we leave, release the
866 // button press and vice versa
867 foreach (ToolBarItem item in items) {
869 (item.Inside != item.Rectangle.Contains (loc))) {
870 item.Inside = item.Rectangle.Contains (loc);
871 item.Hilight = false;
878 if (current_item != null && current_item.Rectangle.Contains (loc)) {
879 if (appearance == ToolBarAppearance.Flat) {
880 if (current_item.Hilight || current_item.Button.Pushed || !current_item.Button.Enabled)
882 current_item.Hilight = true;
885 if (tip_window != null) {
886 if (tip_window.Visible) {
887 tip_window.Hide (this);
888 TipDownTimer.Stop ();
890 current_item = ItemAtPoint (loc);
891 if (current_item != null && current_item.Button.ToolTipText.Length > 0) {
892 tip_window.Present (this, current_item.Button.ToolTipText);
893 TipDownTimer.Start ();
897 if (appearance == ToolBarAppearance.Flat) {
898 foreach (ToolBarItem item in items) {
899 if (item.Rectangle.Contains (loc) && item.Button.Enabled) {
901 if (current_item.Hilight || current_item.Button.Pushed)
903 current_item.Hilight = true;
905 else if (item.Hilight) {
906 item.Hilight = false;
913 internal override void OnPaintInternal (PaintEventArgs pevent)
915 ThemeEngine.Current.DrawToolBar (pevent.Graphics, pevent.ClipRectangle, this);
918 internal void Redraw (bool recalculate)
920 Redraw (recalculate, true);
923 internal void Redraw (bool recalculate, bool force)
925 bool invalidate = true;
928 invalidate = LayoutToolBar ();
930 if (force || invalidate)
934 internal bool SizeSpecified {
935 get { return size_specified; }
938 private bool Vertical {
939 get { return (Dock == DockStyle.Left) || (Dock == DockStyle.Right); }
942 internal const int text_padding = 3;
944 private Size CalcButtonSize ()
946 if (Buttons.Count == 0)
949 string longest_text = Buttons [0].Text;
950 for (int i = 1; i < Buttons.Count; i++) {
951 if (Buttons[i].Text.Length > longest_text.Length)
952 longest_text = Buttons[i].Text;
955 Size size = Size.Empty;
956 if (longest_text != null && longest_text.Length > 0) {
957 SizeF sz = TextRenderer.MeasureString (longest_text, Font);
958 if (sz != SizeF.Empty)
959 size = new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding, (int) Math.Ceiling (sz.Height));
962 Size img_size = ImageList == null ? new Size (16, 16) : ImageSize;
964 Theme theme = ThemeEngine.Current;
965 int imgWidth = img_size.Width + 2 * theme.ToolBarImageGripWidth;
966 int imgHeight = img_size.Height + 2 * theme.ToolBarImageGripWidth;
968 if (text_alignment == ToolBarTextAlign.Right) {
969 size.Width = imgWidth + size.Width;
970 size.Height = (size.Height > imgHeight) ? size.Height : imgHeight;
972 size.Height = imgHeight + size.Height;
973 size.Width = (size.Width > imgWidth) ? size.Width : imgWidth;
976 size.Width += theme.ToolBarImageGripWidth;
977 size.Height += theme.ToolBarImageGripWidth;
981 // Flat toolbars disregard specified sizes. Normal toolbars grow the
982 // button size to be at least large enough to show the image.
983 private Size AdjustedButtonSize {
987 if (default_size.IsEmpty || Appearance == ToolBarAppearance.Normal)
992 if (size_specified) {
993 if (Appearance == ToolBarAppearance.Flat)
994 size = CalcButtonSize ();
996 int grip = ThemeEngine.Current.ToolBarImageGripWidth;
997 if (size.Width < ImageSize.Width + 2 * grip )
998 size.Width = ImageSize.Width + 2 * grip;
999 if (size.Height < ImageSize.Height + 2 * grip)
1000 size.Height = ImageSize.Height + 2 * grip;
1007 private bool LayoutToolBar ()
1009 bool changed = false;
1010 Theme theme = ThemeEngine.Current;
1011 int x = theme.ToolBarGripWidth;
1012 int y = theme.ToolBarGripWidth;
1014 Size adjusted_size = AdjustedButtonSize;
1016 int calculated_size = (Vertical ? adjusted_size.Width : adjusted_size.Height) + theme.ToolBarGripWidth;
1018 int separator_index = -1;
1020 items = new ToolBarItem [buttons.Count];
1022 for (int i = 0; i < buttons.Count; i++) {
1023 ToolBarButton button = buttons [i];
1025 ToolBarItem item = new ToolBarItem (button);
1028 if (!button.Visible)
1031 if (size_specified && (button.Style != ToolBarButtonStyle.Separator))
1032 changed = item.Layout (adjusted_size);
1034 changed = item.Layout (Vertical, calculated_size);
1036 bool is_separator = button.Style == ToolBarButtonStyle.Separator;
1039 if (y + item.Rectangle.Height < Height || is_separator || !Wrappable) {
1040 if (item.Location.X != x || item.Location.Y != y)
1042 item.Location = new Point (x, y);
1043 y += item.Rectangle.Height;
1045 separator_index = i;
1046 } else if (separator_index > 0) {
1047 i = separator_index;
1048 separator_index = -1;
1049 y = theme.ToolBarGripWidth;
1050 x += calculated_size;
1052 y = theme.ToolBarGripWidth;
1053 x += calculated_size;
1054 if (item.Location.X != x || item.Location.Y != y)
1056 item.Location = new Point (x, y);
1057 y += item.Rectangle.Height;
1060 if (x + item.Rectangle.Width < Width || is_separator || !Wrappable) {
1061 if (item.Location.X != x || item.Location.Y != y)
1063 item.Location = new Point (x, y);
1064 x += item.Rectangle.Width;
1066 separator_index = i;
1067 } else if (separator_index > 0) {
1068 i = separator_index;
1069 separator_index = -1;
1070 x = theme.ToolBarGripWidth;
1071 y += calculated_size;
1073 x = theme.ToolBarGripWidth;
1074 y += calculated_size;
1075 if (item.Location.X != x || item.Location.Y != y)
1077 item.Location = new Point (x, y);
1078 x += item.Rectangle.Width;
1087 calculated_size += Vertical ? x : y;
1089 if (IsHandleCreated) {
1091 Width = calculated_size;
1093 Height = calculated_size;
1098 #endregion Private Methods
1101 public class ToolBarButtonCollection : IList, ICollection, IEnumerable
1103 #region instance variables
1104 private ArrayList list; // ToolBarButton list
1105 private ToolBar owner; // ToolBar associated to Collection
1106 private bool redraw; // Flag if needs to redraw after add/remove operations
1109 #region constructors
1110 public ToolBarButtonCollection (ToolBar owner)
1112 this.list = new ArrayList ();
1121 get { return list.Count; }
1124 public bool IsReadOnly {
1125 get { return list.IsReadOnly; }
1128 public virtual ToolBarButton this [int index] {
1129 get { return (ToolBarButton) list [index]; }
1131 value.SetParent (owner);
1132 list [index] = value;
1133 owner.Redraw (true);
1138 public virtual ToolBarButton this[string key] {
1140 if (string.IsNullOrEmpty (key))
1143 foreach (ToolBarButton b in list)
1144 if (string.Compare (b.Name, key, true) == 0)
1152 bool ICollection.IsSynchronized {
1153 get { return list.IsSynchronized; }
1156 object ICollection.SyncRoot {
1157 get { return list.SyncRoot; }
1160 bool IList.IsFixedSize {
1161 get { return list.IsFixedSize; }
1164 object IList.this [int index] {
1165 get { return this [index]; }
1167 if (! (value is ToolBarButton))
1168 throw new ArgumentException("Not of type ToolBarButton", "value");
1169 this [index] = (ToolBarButton) value;
1175 public int Add (string text)
1177 ToolBarButton button = new ToolBarButton (text);
1178 return this.Add (button);
1181 public int Add (ToolBarButton button)
1184 button.SetParent (owner);
1185 result = list.Add (button);
1187 owner.Redraw (true);
1191 public void AddRange (ToolBarButton [] buttons)
1195 foreach (ToolBarButton button in buttons)
1200 owner.Redraw (true);
1204 public void Clear ()
1207 owner.Redraw (false);
1210 public bool Contains (ToolBarButton button)
1212 return list.Contains (button);
1216 public virtual bool ContainsKey (string key)
1218 return !(this[key] == null);
1222 public IEnumerator GetEnumerator ()
1224 return list.GetEnumerator ();
1227 void ICollection.CopyTo (Array dest, int index)
1229 list.CopyTo (dest, index);
1232 int IList.Add (object button)
1234 if (! (button is ToolBarButton)) {
1235 throw new ArgumentException("Not of type ToolBarButton", "button");
1238 return this.Add ((ToolBarButton) button);
1241 bool IList.Contains (object button)
1243 if (! (button is ToolBarButton)) {
1244 throw new ArgumentException("Not of type ToolBarButton", "button");
1247 return this.Contains ((ToolBarButton) button);
1250 int IList.IndexOf (object button)
1252 if (! (button is ToolBarButton)) {
1253 throw new ArgumentException("Not of type ToolBarButton", "button");
1256 return this.IndexOf ((ToolBarButton) button);
1259 void IList.Insert (int index, object button)
1261 if (! (button is ToolBarButton)) {
1262 throw new ArgumentException("Not of type ToolBarButton", "button");
1265 this.Insert (index, (ToolBarButton) button);
1268 void IList.Remove (object button)
1270 if (! (button is ToolBarButton)) {
1271 throw new ArgumentException("Not of type ToolBarButton", "button");
1274 this.Remove ((ToolBarButton) button);
1277 public int IndexOf (ToolBarButton button)
1279 return list.IndexOf (button);
1283 public virtual int IndexOfKey (string key)
1285 return IndexOf (this[key]);
1289 public void Insert (int index, ToolBarButton button)
1291 list.Insert (index, button);
1292 owner.Redraw (true);
1295 public void Remove (ToolBarButton button)
1297 list.Remove (button);
1298 owner.Redraw (true);
1301 public void RemoveAt (int index)
1303 list.RemoveAt (index);
1304 owner.Redraw (true);
1308 public virtual void RemoveByKey (string key)
1320 // Because same button can be added to toolbar multiple times, we need to maintain
1321 // a list of button information for each positions.
1322 internal class ToolBarItem : Component
1324 #region Instance variables
1326 private ToolBar toolbar; // Parent toolbar
1327 private ToolBarButton button; // Associated toolBar button
1328 private Rectangle bounds; // Toolbar button bounds
1329 private Rectangle image_rect; // Image button bounds
1330 private Rectangle text_rect; // Text button bounds
1332 private bool dd_pressed = false; // to check for a mouse down on dropdown rect
1333 private bool inside = false; // to handle the mouse move event with mouse pressed
1334 private bool hilight = false; // to hilight buttons in flat style
1335 private bool pressed = false; // this is to check for mouse down on a button
1339 #region Constructors
1341 public ToolBarItem (ToolBarButton button)
1343 this.toolbar = button.Parent;
1344 this.button = button;
1347 #endregion Constructors
1351 public ToolBarButton Button {
1352 get { return this.button; }
1355 public Rectangle Rectangle {
1357 if (!button.Visible || toolbar == null)
1358 return Rectangle.Empty;
1360 if (button.Style == ToolBarButtonStyle.DropDownButton && toolbar.DropDownArrows) {
1361 Rectangle result = bounds;
1362 result.Width += ThemeEngine.Current.ToolBarDropDownWidth;
1368 set { this.bounds = value; }
1371 public Point Location {
1372 get { return bounds.Location; }
1373 set { bounds.Location = value; }
1376 public Rectangle ImageRectangle {
1378 Rectangle result = image_rect;
1379 result.X += bounds.X;
1380 result.Y += bounds.Y;
1385 public Rectangle TextRectangle {
1387 Rectangle result = text_rect;
1388 result.X += bounds.X;
1389 result.Y += bounds.Y;
1394 private Size TextSize {
1396 StringFormat text_format = new StringFormat ();
1397 text_format.HotkeyPrefix = HotkeyPrefix.Hide;
1399 SizeF sz = TextRenderer.MeasureString (button.Text, toolbar.Font, SizeF.Empty, text_format);
1400 if (sz == SizeF.Empty)
1402 return new Size ((int) Math.Ceiling (sz.Width) + 2 * ToolBar.text_padding, (int) Math.Ceiling (sz.Height));
1406 public bool Pressed {
1407 get { return (pressed && inside); }
1408 set { pressed = value; }
1411 public bool DDPressed {
1412 get { return dd_pressed; }
1413 set { dd_pressed = value; }
1416 public bool Inside {
1417 get { return inside; }
1418 set { inside = value; }
1421 public bool Hilight {
1422 get { return hilight; }
1424 if (hilight == value)
1432 #endregion Properties
1436 public Size CalculateSize ()
1438 Theme theme = ThemeEngine.Current;
1440 int ht = toolbar.ButtonSize.Height + 2 * theme.ToolBarGripWidth;
1442 if (button.Style == ToolBarButtonStyle.Separator)
1443 return new Size (theme.ToolBarSeparatorWidth, ht);
1446 if (TextSize.IsEmpty && (button.Image == null))
1447 size = toolbar.default_size;
1451 Size image_size = (toolbar.ImageSize == Size.Empty) ? new Size (16, 16) : toolbar.ImageSize;
1453 int image_width = image_size.Width + 2 * theme.ToolBarImageGripWidth;
1454 int image_height = image_size.Height + 2 * theme.ToolBarImageGripWidth;
1456 if (toolbar.TextAlign == ToolBarTextAlign.Right) {
1457 size.Width = image_width + size.Width;
1458 size.Height = (size.Height > image_height) ? size.Height : image_height;
1460 size.Height = image_height + size.Height;
1461 size.Width = (size.Width > image_width) ? size.Width : image_width;
1464 size.Width += theme.ToolBarGripWidth;
1465 size.Height += theme.ToolBarGripWidth;
1470 public bool Layout (bool vertical, int calculated_size)
1472 if (toolbar == null || !button.Visible)
1475 Size psize = toolbar.ButtonSize;
1477 if ((!toolbar.SizeSpecified) || (button.Style == ToolBarButtonStyle.Separator)) {
1478 size = CalculateSize ();
1480 if (size.Width == 0 || size.Height == 0)
1484 size.Width = calculated_size;
1486 size.Height = calculated_size;
1488 return Layout (size);
1491 public bool Layout (Size size)
1493 if (toolbar == null || !button.Visible)
1498 Size image_size = (toolbar.ImageSize == Size.Empty) ? new Size (16, 16) : toolbar.ImageSize;
1499 int grip = ThemeEngine.Current.ToolBarImageGripWidth;
1501 Rectangle new_image_rect, new_text_rect;
1503 if (toolbar.TextAlign == ToolBarTextAlign.Underneath) {
1504 new_image_rect = new Rectangle ((bounds.Size.Width - image_size.Width) / 2 - grip, 0, image_size.Width + 2 + grip, image_size.Height + 2 * grip);
1505 new_text_rect = new Rectangle (0, new_image_rect.Height, bounds.Size.Width, bounds.Size.Height - new_image_rect.Height - 2 * grip);
1507 new_image_rect = new Rectangle (0, 0, image_size.Width + 2 * grip, image_size.Height + 2 * grip);
1508 new_text_rect = new Rectangle (new_image_rect.Width, 0, bounds.Size.Width - new_image_rect.Width, bounds.Size.Height - 2 * grip);
1511 bool changed = false;
1513 if (new_image_rect != image_rect || new_text_rect != text_rect)
1516 image_rect = new_image_rect;
1517 text_rect = new_text_rect;
1522 public void Invalidate ()
1524 if (toolbar != null)
1525 toolbar.Invalidate (Rectangle);