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)]
223 get { return autosize; }
225 if (value == autosize)
236 [EditorBrowsable (EditorBrowsableState.Never)]
237 public override Color BackColor {
238 get { return background_color; }
240 if (value == background_color)
243 background_color = value;
244 OnBackColorChanged (EventArgs.Empty);
250 [EditorBrowsable (EditorBrowsableState.Never)]
251 public override Image BackgroundImage {
252 get { return base.BackgroundImage; }
253 set { base.BackgroundImage = value; }
258 [EditorBrowsable (EditorBrowsableState.Never)]
259 public override ImageLayout BackgroundImageLayout {
260 get { return base.BackgroundImageLayout; }
261 set { base.BackgroundImageLayout = value; }
265 [DefaultValue (BorderStyle.None)]
266 [DispIdAttribute (-504)]
267 public BorderStyle BorderStyle {
268 get { return InternalBorderStyle; }
269 set { InternalBorderStyle = value; }
272 ToolBarButtonCollection buttons;
274 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
276 [MergableProperty (false)]
277 public ToolBarButtonCollection Buttons {
278 get { return buttons; }
284 [RefreshProperties (RefreshProperties.All)]
285 public Size ButtonSize {
287 if (!button_size.IsEmpty)
290 if (buttons.Count == 0)
291 return new Size (39, 36);
293 Size result = CalcButtonSize ();
295 return new Size (24, 22);
300 size_specified = value != Size.Empty;
301 if (button_size == value)
311 [DefaultValue (true)]
312 public bool Divider {
313 get { return divider; }
315 if (value == divider)
323 [DefaultValue (DockStyle.Top)]
325 public override DockStyle Dock {
326 get { return base.Dock; }
328 if (base.Dock == value) {
329 // Call base anyways so layout_type gets set correctly
330 if (value != DockStyle.None)
336 SetStyle (ControlStyles.FixedWidth, AutoSize);
337 SetStyle (ControlStyles.FixedHeight, false);
339 SetStyle (ControlStyles.FixedHeight, AutoSize);
340 SetStyle (ControlStyles.FixedWidth, false);
349 bool drop_down_arrows = true;
351 [DefaultValue (false)]
353 public bool DropDownArrows {
354 get { return drop_down_arrows; }
356 if (value == drop_down_arrows)
359 drop_down_arrows = value;
365 [EditorBrowsable (EditorBrowsableState.Never)]
366 public override Color ForeColor {
367 get { return foreground_color; }
369 if (value == foreground_color)
372 foreground_color = value;
373 OnForeColorChanged (EventArgs.Empty);
378 ImageList image_list;
380 [DefaultValue (null)]
381 public ImageList ImageList {
382 get { return image_list; }
384 if (image_list == value)
392 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
393 [EditorBrowsable (EditorBrowsableState.Advanced)]
394 public Size ImageSize {
396 if (ImageList == null)
399 return ImageList.ImageSize;
403 // XXX this should probably go away and it should call
404 // into Control.ImeMode instead.
405 ImeMode ime_mode = ImeMode.Disable;
408 [EditorBrowsable (EditorBrowsableState.Never)]
409 public new ImeMode ImeMode {
410 get { return ime_mode; }
412 if (value == ime_mode)
416 OnImeModeChanged (EventArgs.Empty);
421 [EditorBrowsable (EditorBrowsableState.Never)]
422 public override RightToLeft RightToLeft {
423 get { return base.RightToLeft; }
425 if (value == base.RightToLeft)
428 base.RightToLeft = value;
429 OnRightToLeftChanged (EventArgs.Empty);
433 // Default value is "false" but after make a test in .NET we get "true" result as default.
434 bool show_tooltips = true;
436 [DefaultValue (false)]
438 public bool ShowToolTips {
439 get { return show_tooltips; }
440 set { show_tooltips = value; }
443 [DefaultValue (false)]
444 public new bool TabStop {
445 get { return base.TabStop; }
446 set { base.TabStop = value; }
451 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
452 [EditorBrowsable (EditorBrowsableState.Never)]
453 public override string Text {
454 get { return base.Text; }
456 if (value == base.Text)
464 ToolBarTextAlign text_alignment = ToolBarTextAlign.Underneath;
466 [DefaultValue (ToolBarTextAlign.Underneath)]
468 public ToolBarTextAlign TextAlign {
469 get { return text_alignment; }
471 if (value == text_alignment)
474 text_alignment = value;
479 bool wrappable = true;
481 [DefaultValue (true)]
483 public bool Wrappable {
484 get { return wrappable; }
486 if (value == wrappable)
493 #endregion Public Properties
495 #region Public Methods
496 public override string ToString ()
498 int count = this.Buttons.Count;
501 return string.Format ("System.Windows.Forms.ToolBar, Buttons.Count: 0");
503 return string.Format ("System.Windows.Forms.ToolBar, Buttons.Count: {0}, Buttons[0]: {1}",
504 count, this.Buttons [0].ToString ());
506 #endregion Public Methods
508 #region Protected Methods
509 protected override void CreateHandle ()
511 base.CreateHandle ();
512 default_size = CalcButtonSize ();
514 // In win32 the recalculate size only happens for not flat style
515 if (appearance != ToolBarAppearance.Flat)
519 protected override void Dispose (bool disposing)
524 base.Dispose (disposing);
528 private ToolBarButton button_for_focus = null;
530 internal void UIAPerformClick (ToolBarButton button)
532 ToolBarItem previous_item = current_item;
535 foreach (ToolBarItem item in items)
536 if (item.Button == button) {
542 if (current_item == null)
543 throw new ArgumentException ("button", "The button specified is not part of this toolbar");
544 OnButtonClick (new ToolBarButtonClickEventArgs (button));
546 current_item = previous_item;
551 protected virtual void OnButtonClick (ToolBarButtonClickEventArgs e)
553 if (e.Button.Style == ToolBarButtonStyle.ToggleButton) {
554 if (! e.Button.Pushed)
555 e.Button.Pushed = true;
557 e.Button.Pushed = false;
560 current_item.Pressed = false;
561 current_item.Invalidate ();
564 button_for_focus = current_item.Button;
565 button_for_focus.UIAHasFocus = true;
568 ToolBarButtonClickEventHandler eh = (ToolBarButtonClickEventHandler)(Events [ButtonClickEvent]);
573 protected virtual void OnButtonDropDown (ToolBarButtonClickEventArgs e)
575 ToolBarButtonClickEventHandler eh = (ToolBarButtonClickEventHandler)(Events [ButtonDropDownEvent]);
579 if (e.Button.DropDownMenu == null)
582 ToolBarItem item = current_item;
584 Point loc = new Point (item.Rectangle.X + 1, item.Rectangle.Bottom + 1);
585 ((ContextMenu) e.Button.DropDownMenu).Show (this, loc);
587 item.DDPressed = false;
588 item.Hilight = false;
592 protected override void OnFontChanged (EventArgs e)
594 base.OnFontChanged (e);
598 protected override void OnHandleCreated (EventArgs e)
600 base.OnHandleCreated (e);
603 protected override void OnResize (EventArgs e)
610 protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
612 specified &= ~BoundsSpecified.Height;
614 base.ScaleControl (factor, specified);
617 [EditorBrowsable (EditorBrowsableState.Never)]
618 protected override void ScaleCore (float dx, float dy)
622 base.ScaleCore (dx, dy);
626 private int requested_size = -1;
628 protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
631 if (!AutoSize && (requested_size != width) && ((specified & BoundsSpecified.Width) != BoundsSpecified.None))
632 requested_size = width;
634 if (!AutoSize && (requested_size != height) && ((specified & BoundsSpecified.Height) != BoundsSpecified.None))
635 requested_size = height;
638 base.SetBoundsCore (x, y, width, height, specified);
641 protected override void WndProc (ref Message m)
643 base.WndProc (ref m);
646 internal override bool InternalPreProcessMessage (ref Message msg)
648 if (msg.Msg == (int)Msg.WM_KEYDOWN) {
649 Keys key_data = (Keys)msg.WParam.ToInt32();
650 if (HandleKeyDown (key_data))
653 return base.InternalPreProcessMessage (ref msg);
656 #endregion Protected Methods
658 #region Private Methods
659 private void FocusChanged (object sender, EventArgs args)
662 if (!Focused && button_for_focus != null)
663 button_for_focus.UIAHasFocus = false;
664 button_for_focus = null;
667 if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
670 ToolBarItem prelit = null;
671 foreach (ToolBarItem item in items) {
678 if (Focused && prelit == null) {
679 foreach (ToolBarItem item in items) {
680 if (item.Button.Enabled) {
685 } else if (prelit != null) {
686 prelit.Hilight = false;
690 private bool HandleKeyDown (Keys key_data)
692 if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
698 HighlightButton (-1);
709 void HighlightButton (int offset)
711 ArrayList enabled = new ArrayList ();
714 ToolBarItem curr_item = null;
715 foreach (ToolBarItem item in items) {
721 if (item.Button.Enabled) {
727 int next = (start + offset) % count;
734 if (curr_item != null)
735 curr_item.Hilight = false;
736 (enabled [next] as ToolBarItem).Hilight = true;
739 private void ToolBar_BackgroundImageChanged (object sender, EventArgs args)
741 Redraw (false, true);
744 private void ToolBar_MouseDown (object sender, MouseEventArgs me)
746 if ((!Enabled) || ((me.Button & MouseButtons.Left) == 0))
749 Point loc = new Point (me.X, me.Y);
751 if (ItemAtPoint (loc) == null)
754 // Hide tooltip when left mouse button
755 if ((tip_window != null) && (tip_window.Visible) && ((me.Button & MouseButtons.Left) == MouseButtons.Left)) {
756 TipDownTimer.Stop ();
757 tip_window.Hide (this);
760 // draw the pushed button
761 foreach (ToolBarItem item in items) {
762 if (item.Button.Enabled && item.Rectangle.Contains (loc)) {
763 // Mark the DropDown rect as pressed.
764 // We don't redraw the dropdown rect.
765 if (item.Button.Style == ToolBarButtonStyle.DropDownButton) {
766 Rectangle rect = item.Rectangle;
767 if (DropDownArrows) {
768 rect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
769 rect.X = item.Rectangle.Right - rect.Width;
772 if (rect.Contains (loc)) {
773 if (item.Button.DropDownMenu != null) {
774 item.DDPressed = true;
788 private void ToolBar_MouseUp (object sender, MouseEventArgs me)
790 if ((!Enabled) || ((me.Button & MouseButtons.Left) == 0))
793 Point loc = new Point (me.X, me.Y);
795 // draw the normal button
796 // Make a copy in case the list is modified during enumeration
797 ArrayList items = new ArrayList (this.items);
798 foreach (ToolBarItem item in items) {
799 if (item.Button.Enabled && item.Rectangle.Contains (loc)) {
800 if (item.Button.Style == ToolBarButtonStyle.DropDownButton) {
801 Rectangle ddRect = item.Rectangle;
802 ddRect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
803 ddRect.X = item.Rectangle.Right - ddRect.Width;
804 if (ddRect.Contains (loc)) {
807 OnButtonDropDown (new ToolBarButtonClickEventArgs (item.Button));
811 // Fire a ButtonClick
813 if ((item.Pressed) && ((me.Button & MouseButtons.Left) == MouseButtons.Left))
814 OnButtonClick (new ToolBarButtonClickEventArgs (item.Button));
815 } else if (item.Pressed) {
816 item.Pressed = false;
822 private ToolBarItem ItemAtPoint (Point pt)
824 foreach (ToolBarItem item in items)
825 if (item.Rectangle.Contains (pt))
831 ToolTip tip_window = null;
832 Timer tipdown_timer = null;
834 private void PopDownTip (object o, EventArgs args)
836 tip_window.Hide (this);
839 private Timer TipDownTimer {
841 if (tipdown_timer == null) {
842 tipdown_timer = new Timer ();
843 tipdown_timer.Enabled = false;
844 tipdown_timer.Interval = 5000;
845 tipdown_timer.Tick += new EventHandler (PopDownTip);
847 return tipdown_timer;
851 private void ToolBar_MouseHover (object sender, EventArgs e)
856 if (tip_window == null)
857 tip_window = new ToolTip ();
859 ToolBarItem item = ItemAtPoint (PointToClient (Control.MousePosition));
862 if (item == null || item.Button.ToolTipText.Length == 0)
865 tip_window.Present (this, item.Button.ToolTipText);
866 TipDownTimer.Start ();
869 private void ToolBar_MouseLeave (object sender, EventArgs e)
871 if (tipdown_timer != null)
872 tipdown_timer.Dispose ();
873 tipdown_timer = null;
874 if (tip_window != null)
875 tip_window.Dispose ();
878 if (!Enabled || current_item == null)
881 current_item.Hilight = false;
885 private void ToolBar_MouseMove (object sender, MouseEventArgs me)
890 if (tip_window != null && tip_window.Visible) {
891 TipDownTimer.Stop ();
892 TipDownTimer.Start ();
895 Point loc = new Point (me.X, me.Y);
898 // If the button was pressed and we leave, release the
899 // button press and vice versa
900 foreach (ToolBarItem item in items) {
902 (item.Inside != item.Rectangle.Contains (loc))) {
903 item.Inside = item.Rectangle.Contains (loc);
904 item.Hilight = false;
911 if (current_item != null && current_item.Rectangle.Contains (loc)) {
912 if (ThemeEngine.Current.ToolBarHasHotElementStyles (this)) {
913 if (current_item.Hilight || (!ThemeEngine.Current.ToolBarHasHotCheckedElementStyles && current_item.Button.Pushed) || !current_item.Button.Enabled)
915 current_item.Hilight = true;
918 if (tip_window != null) {
919 if (tip_window.Visible) {
920 tip_window.Hide (this);
921 TipDownTimer.Stop ();
923 current_item = ItemAtPoint (loc);
924 if (current_item != null && current_item.Button.ToolTipText.Length > 0) {
925 tip_window.Present (this, current_item.Button.ToolTipText);
926 TipDownTimer.Start ();
930 if (ThemeEngine.Current.ToolBarHasHotElementStyles (this)) {
931 foreach (ToolBarItem item in items) {
932 if (item.Rectangle.Contains (loc) && item.Button.Enabled) {
934 if (current_item.Hilight || (!ThemeEngine.Current.ToolBarHasHotCheckedElementStyles && current_item.Button.Pushed))
936 current_item.Hilight = true;
938 else if (item.Hilight) {
939 item.Hilight = false;
946 internal override void OnPaintInternal (PaintEventArgs pevent)
948 if (GetStyle (ControlStyles.UserPaint))
951 ThemeEngine.Current.DrawToolBar (pevent.Graphics, pevent.ClipRectangle, this);
953 // Toolbars do not raise OnPaint unless UserPaint is set
954 pevent.Handled = true;
957 internal void Redraw (bool recalculate)
959 Redraw (recalculate, true);
962 internal void Redraw (bool recalculate, bool force)
964 bool invalidate = true;
967 invalidate = LayoutToolBar ();
969 if (force || invalidate)
973 internal bool SizeSpecified {
974 get { return size_specified; }
977 internal bool Vertical {
978 get { return (Dock == DockStyle.Left) || (Dock == DockStyle.Right); }
981 internal const int text_padding = 3;
983 private Size CalcButtonSize ()
985 if (Buttons.Count == 0)
988 string longest_text = Buttons [0].Text;
989 for (int i = 1; i < Buttons.Count; i++) {
990 if (Buttons[i].Text.Length > longest_text.Length)
991 longest_text = Buttons[i].Text;
994 Size size = Size.Empty;
995 if (longest_text != null && longest_text.Length > 0) {
996 SizeF sz = TextRenderer.MeasureString (longest_text, Font);
997 if (sz != SizeF.Empty)
998 size = new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding, (int) Math.Ceiling (sz.Height));
1001 Size img_size = ImageList == null ? new Size (16, 16) : ImageSize;
1003 Theme theme = ThemeEngine.Current;
1004 int imgWidth = img_size.Width + 2 * theme.ToolBarImageGripWidth;
1005 int imgHeight = img_size.Height + 2 * theme.ToolBarImageGripWidth;
1007 if (text_alignment == ToolBarTextAlign.Right) {
1008 size.Width = imgWidth + size.Width;
1009 size.Height = (size.Height > imgHeight) ? size.Height : imgHeight;
1011 size.Height = imgHeight + size.Height;
1012 size.Width = (size.Width > imgWidth) ? size.Width : imgWidth;
1015 size.Width += theme.ToolBarImageGripWidth;
1016 size.Height += theme.ToolBarImageGripWidth;
1020 // Flat toolbars disregard specified sizes. Normal toolbars grow the
1021 // button size to be at least large enough to show the image.
1022 private Size AdjustedButtonSize {
1026 if (default_size.IsEmpty || Appearance == ToolBarAppearance.Normal)
1029 size = default_size;
1031 if (size_specified) {
1032 if (Appearance == ToolBarAppearance.Flat)
1033 size = CalcButtonSize ();
1035 int grip = ThemeEngine.Current.ToolBarImageGripWidth;
1036 if (size.Width < ImageSize.Width + 2 * grip )
1037 size.Width = ImageSize.Width + 2 * grip;
1038 if (size.Height < ImageSize.Height + 2 * grip)
1039 size.Height = ImageSize.Height + 2 * grip;
1046 private bool LayoutToolBar ()
1048 bool changed = false;
1049 Theme theme = ThemeEngine.Current;
1050 int x = theme.ToolBarGripWidth;
1051 int y = theme.ToolBarGripWidth;
1053 Size adjusted_size = AdjustedButtonSize;
1055 int calculated_size = (Vertical ? adjusted_size.Width : adjusted_size.Height) + theme.ToolBarGripWidth;
1057 int separator_index = -1;
1059 items = new ToolBarItem [buttons.Count];
1061 for (int i = 0; i < buttons.Count; i++) {
1062 ToolBarButton button = buttons [i];
1064 ToolBarItem item = new ToolBarItem (button);
1067 if (!button.Visible)
1070 if (size_specified && (button.Style != ToolBarButtonStyle.Separator))
1071 changed = item.Layout (adjusted_size);
1073 changed = item.Layout (Vertical, calculated_size);
1075 bool is_separator = button.Style == ToolBarButtonStyle.Separator;
1078 if (y + item.Rectangle.Height < Height || is_separator || !Wrappable) {
1079 if (item.Location.X != x || item.Location.Y != y)
1081 item.Location = new Point (x, y);
1082 y += item.Rectangle.Height;
1084 separator_index = i;
1085 } else if (separator_index > 0) {
1086 i = separator_index;
1087 separator_index = -1;
1088 y = theme.ToolBarGripWidth;
1089 x += calculated_size;
1091 y = theme.ToolBarGripWidth;
1092 x += calculated_size;
1093 if (item.Location.X != x || item.Location.Y != y)
1095 item.Location = new Point (x, y);
1096 y += item.Rectangle.Height;
1099 if (x + item.Rectangle.Width < Width || is_separator || !Wrappable) {
1100 if (item.Location.X != x || item.Location.Y != y)
1102 item.Location = new Point (x, y);
1103 x += item.Rectangle.Width;
1105 separator_index = i;
1106 } else if (separator_index > 0) {
1107 i = separator_index;
1108 separator_index = -1;
1109 x = theme.ToolBarGripWidth;
1110 y += calculated_size;
1112 x = theme.ToolBarGripWidth;
1113 y += calculated_size;
1114 if (item.Location.X != x || item.Location.Y != y)
1116 item.Location = new Point (x, y);
1117 x += item.Rectangle.Width;
1126 calculated_size += Vertical ? x : y;
1128 if (IsHandleCreated) {
1130 Width = calculated_size;
1132 Height = calculated_size;
1137 #endregion Private Methods
1140 public class ToolBarButtonCollection : IList, ICollection, IEnumerable
1142 #region instance variables
1143 private ArrayList list; // ToolBarButton list
1144 private ToolBar owner; // ToolBar associated to Collection
1145 private bool redraw; // Flag if needs to redraw after add/remove operations
1148 #region UIA Framework Events
1150 static object UIACollectionChangedEvent = new object ();
1152 internal event CollectionChangeEventHandler UIACollectionChanged {
1153 add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
1154 remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
1157 internal void OnUIACollectionChanged (CollectionChangeEventArgs e)
1159 CollectionChangeEventHandler eh
1160 = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
1167 #region constructors
1168 public ToolBarButtonCollection (ToolBar owner)
1170 this.list = new ArrayList ();
1179 get { return list.Count; }
1182 public bool IsReadOnly {
1183 get { return list.IsReadOnly; }
1186 public virtual ToolBarButton this [int index] {
1187 get { return (ToolBarButton) list [index]; }
1190 // UIA Framework Event: Button Removed
1191 OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
1194 value.SetParent (owner);
1195 list [index] = value;
1196 owner.Redraw (true);
1200 // UIA Framework Event: Button Added
1201 OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
1207 public virtual ToolBarButton this[string key] {
1209 if (string.IsNullOrEmpty (key))
1212 foreach (ToolBarButton b in list)
1213 if (string.Compare (b.Name, key, true) == 0)
1221 bool ICollection.IsSynchronized {
1222 get { return list.IsSynchronized; }
1225 object ICollection.SyncRoot {
1226 get { return list.SyncRoot; }
1229 bool IList.IsFixedSize {
1230 get { return list.IsFixedSize; }
1233 object IList.this [int index] {
1234 get { return this [index]; }
1236 if (! (value is ToolBarButton))
1237 throw new ArgumentException("Not of type ToolBarButton", "value");
1238 this [index] = (ToolBarButton) value;
1244 public int Add (string text)
1246 ToolBarButton button = new ToolBarButton (text);
1247 return this.Add (button);
1250 public int Add (ToolBarButton button)
1253 button.SetParent (owner);
1254 result = list.Add (button);
1256 owner.Redraw (true);
1259 // UIA Framework Event: Button Added
1260 OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, result));
1266 public void AddRange (ToolBarButton [] buttons)
1270 foreach (ToolBarButton button in buttons)
1275 owner.Redraw (true);
1279 public void Clear ()
1282 owner.Redraw (false);
1285 // UIA Framework Event: Button Cleared
1286 OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, -1));
1290 public bool Contains (ToolBarButton button)
1292 return list.Contains (button);
1296 public virtual bool ContainsKey (string key)
1298 return !(this[key] == null);
1302 public IEnumerator GetEnumerator ()
1304 return list.GetEnumerator ();
1307 void ICollection.CopyTo (Array dest, int index)
1309 list.CopyTo (dest, index);
1312 int IList.Add (object button)
1314 if (! (button is ToolBarButton)) {
1315 throw new ArgumentException("Not of type ToolBarButton", "button");
1318 return this.Add ((ToolBarButton) button);
1321 bool IList.Contains (object button)
1323 if (! (button is ToolBarButton)) {
1324 throw new ArgumentException("Not of type ToolBarButton", "button");
1327 return this.Contains ((ToolBarButton) button);
1330 int IList.IndexOf (object button)
1332 if (! (button is ToolBarButton)) {
1333 throw new ArgumentException("Not of type ToolBarButton", "button");
1336 return this.IndexOf ((ToolBarButton) button);
1339 void IList.Insert (int index, object button)
1341 if (! (button is ToolBarButton)) {
1342 throw new ArgumentException("Not of type ToolBarButton", "button");
1345 this.Insert (index, (ToolBarButton) button);
1348 void IList.Remove (object button)
1350 if (! (button is ToolBarButton)) {
1351 throw new ArgumentException("Not of type ToolBarButton", "button");
1354 this.Remove ((ToolBarButton) button);
1357 public int IndexOf (ToolBarButton button)
1359 return list.IndexOf (button);
1363 public virtual int IndexOfKey (string key)
1365 return IndexOf (this[key]);
1369 public void Insert (int index, ToolBarButton button)
1371 list.Insert (index, button);
1372 owner.Redraw (true);
1375 // UIA Framework Event: Button Added
1376 OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
1380 public void Remove (ToolBarButton button)
1382 list.Remove (button);
1383 owner.Redraw (true);
1386 public void RemoveAt (int index)
1388 list.RemoveAt (index);
1389 owner.Redraw (true);
1392 // UIA Framework Event: Button Removed
1393 OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
1398 public virtual void RemoveByKey (string key)
1410 // Because same button can be added to toolbar multiple times, we need to maintain
1411 // a list of button information for each positions.
1412 internal class ToolBarItem : Component
1414 #region Instance variables
1416 private ToolBar toolbar; // Parent toolbar
1417 private ToolBarButton button; // Associated toolBar button
1418 private Rectangle bounds; // Toolbar button bounds
1419 private Rectangle image_rect; // Image button bounds
1420 private Rectangle text_rect; // Text button bounds
1422 private bool dd_pressed = false; // to check for a mouse down on dropdown rect
1423 private bool inside = false; // to handle the mouse move event with mouse pressed
1424 private bool hilight = false; // to hilight buttons in flat style
1425 private bool pressed = false; // this is to check for mouse down on a button
1429 #region Constructors
1431 public ToolBarItem (ToolBarButton button)
1433 this.toolbar = button.Parent;
1434 this.button = button;
1437 #endregion Constructors
1441 public ToolBarButton Button {
1442 get { return this.button; }
1445 public Rectangle Rectangle {
1447 if (!button.Visible || toolbar == null)
1448 return Rectangle.Empty;
1450 if (button.Style == ToolBarButtonStyle.DropDownButton && toolbar.DropDownArrows) {
1451 Rectangle result = bounds;
1452 result.Width += ThemeEngine.Current.ToolBarDropDownWidth;
1458 set { this.bounds = value; }
1461 public Point Location {
1462 get { return bounds.Location; }
1463 set { bounds.Location = value; }
1466 public Rectangle ImageRectangle {
1468 Rectangle result = image_rect;
1469 result.X += bounds.X;
1470 result.Y += bounds.Y;
1475 public Rectangle TextRectangle {
1477 Rectangle result = text_rect;
1478 result.X += bounds.X;
1479 result.Y += bounds.Y;
1484 private Size TextSize {
1486 StringFormat text_format = new StringFormat ();
1487 text_format.HotkeyPrefix = HotkeyPrefix.Hide;
1489 SizeF sz = TextRenderer.MeasureString (button.Text, toolbar.Font, SizeF.Empty, text_format);
1490 if (sz == SizeF.Empty)
1492 return new Size ((int) Math.Ceiling (sz.Width) + 2 * ToolBar.text_padding, (int) Math.Ceiling (sz.Height));
1496 public bool Pressed {
1497 get { return (pressed && inside); }
1498 set { pressed = value; }
1501 public bool DDPressed {
1502 get { return dd_pressed; }
1503 set { dd_pressed = value; }
1506 public bool Inside {
1507 get { return inside; }
1508 set { inside = value; }
1511 public bool Hilight {
1512 get { return hilight; }
1514 if (hilight == value)
1522 #endregion Properties
1526 public Size CalculateSize ()
1528 Theme theme = ThemeEngine.Current;
1530 int ht = toolbar.ButtonSize.Height + 2 * theme.ToolBarGripWidth;
1532 if (button.Style == ToolBarButtonStyle.Separator)
1533 return new Size (theme.ToolBarSeparatorWidth, ht);
1536 if (TextSize.IsEmpty && (button.Image == null))
1537 size = toolbar.default_size;
1541 Size image_size = (toolbar.ImageSize == Size.Empty) ? new Size (16, 16) : toolbar.ImageSize;
1543 int image_width = image_size.Width + 2 * theme.ToolBarImageGripWidth;
1544 int image_height = image_size.Height + 2 * theme.ToolBarImageGripWidth;
1546 if (toolbar.TextAlign == ToolBarTextAlign.Right) {
1547 size.Width = image_width + size.Width;
1548 size.Height = (size.Height > image_height) ? size.Height : image_height;
1550 size.Height = image_height + size.Height;
1551 size.Width = (size.Width > image_width) ? size.Width : image_width;
1554 size.Width += theme.ToolBarGripWidth;
1555 size.Height += theme.ToolBarGripWidth;
1560 public bool Layout (bool vertical, int calculated_size)
1562 if (toolbar == null || !button.Visible)
1565 Size psize = toolbar.ButtonSize;
1567 if ((!toolbar.SizeSpecified) || (button.Style == ToolBarButtonStyle.Separator)) {
1568 size = CalculateSize ();
1570 if (size.Width == 0 || size.Height == 0)
1574 size.Width = calculated_size;
1576 size.Height = calculated_size;
1578 return Layout (size);
1581 public bool Layout (Size size)
1583 if (toolbar == null || !button.Visible)
1588 Size image_size = (toolbar.ImageSize == Size.Empty) ? new Size (16, 16) : toolbar.ImageSize;
1589 int grip = ThemeEngine.Current.ToolBarImageGripWidth;
1591 Rectangle new_image_rect, new_text_rect;
1593 if (toolbar.TextAlign == ToolBarTextAlign.Underneath) {
1594 new_image_rect = new Rectangle ((bounds.Size.Width - image_size.Width) / 2 - grip, 0, image_size.Width + 2 + grip, image_size.Height + 2 * grip);
1595 new_text_rect = new Rectangle (0, new_image_rect.Height, bounds.Size.Width, bounds.Size.Height - new_image_rect.Height - 2 * grip);
1597 new_image_rect = new Rectangle (0, 0, image_size.Width + 2 * grip, image_size.Height + 2 * grip);
1598 new_text_rect = new Rectangle (new_image_rect.Width, 0, bounds.Size.Width - new_image_rect.Width, bounds.Size.Height - 2 * grip);
1601 bool changed = false;
1603 if (new_image_rect != image_rect || new_text_rect != text_rect)
1606 image_rect = new_image_rect;
1607 text_rect = new_text_rect;
1612 public void Invalidate ()
1614 if (toolbar != null)
1615 toolbar.Invalidate (Rectangle);