PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/Managed.Windows.Forms/System.Windows.Forms/ToolBar.cs

https://bitbucket.org/danipen/mono
C# | 1634 lines | 1288 code | 291 blank | 55 comment | 328 complexity | 588e63a36fbef863d0008fbccf520242 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // System.Windows.Forms.ToolBar.cs
  2. //
  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:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  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.
  21. //
  22. // Author:
  23. // Ravindra (rkumar@novell.com)
  24. // Mike Kestner <mkestner@novell.com>
  25. // Everaldo Canuto <ecanuto@novell.com>
  26. //
  27. // Copyright (C) 2004-2006 Novell, Inc. (http://www.novell.com)
  28. //
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.ComponentModel.Design;
  32. using System.Drawing;
  33. using System.Drawing.Text;
  34. using System.Drawing.Imaging;
  35. using System.Runtime.InteropServices;
  36. namespace System.Windows.Forms
  37. {
  38. [ComVisible (true)]
  39. [ClassInterface (ClassInterfaceType.AutoDispatch)]
  40. [DefaultEvent ("ButtonClick")]
  41. [DefaultProperty ("Buttons")]
  42. [Designer ("System.Windows.Forms.Design.ToolBarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  43. public class ToolBar : Control
  44. {
  45. #region Instance Variables
  46. private bool size_specified = false;
  47. private ToolBarItem current_item;
  48. internal ToolBarItem[] items;
  49. internal Size default_size;
  50. #endregion Instance Variables
  51. #region Events
  52. static object ButtonClickEvent = new object ();
  53. static object ButtonDropDownEvent = new object ();
  54. [Browsable (true)]
  55. [EditorBrowsable (EditorBrowsableState.Always)]
  56. public new event EventHandler AutoSizeChanged {
  57. add { base.AutoSizeChanged += value; }
  58. remove { base.AutoSizeChanged -= value; }
  59. }
  60. [Browsable (false)]
  61. [EditorBrowsable (EditorBrowsableState.Never)]
  62. public new event EventHandler BackColorChanged {
  63. add { base.BackColorChanged += value; }
  64. remove { base.BackColorChanged -= value; }
  65. }
  66. [Browsable (false)]
  67. [EditorBrowsable (EditorBrowsableState.Never)]
  68. public new event EventHandler BackgroundImageChanged {
  69. add { base.BackgroundImageChanged += value; }
  70. remove { base.BackgroundImageChanged -= value; }
  71. }
  72. [Browsable (false)]
  73. [EditorBrowsable (EditorBrowsableState.Never)]
  74. public new event EventHandler BackgroundImageLayoutChanged {
  75. add { base.BackgroundImageLayoutChanged += value; }
  76. remove { base.BackgroundImageLayoutChanged -= value; }
  77. }
  78. public event ToolBarButtonClickEventHandler ButtonClick {
  79. add { Events.AddHandler (ButtonClickEvent, value); }
  80. remove {Events.RemoveHandler (ButtonClickEvent, value); }
  81. }
  82. public event ToolBarButtonClickEventHandler ButtonDropDown {
  83. add { Events.AddHandler (ButtonDropDownEvent, value); }
  84. remove {Events.RemoveHandler (ButtonDropDownEvent, value); }
  85. }
  86. [Browsable (false)]
  87. [EditorBrowsable (EditorBrowsableState.Never)]
  88. public new event EventHandler ForeColorChanged {
  89. add { base.ForeColorChanged += value; }
  90. remove { base.ForeColorChanged -= value; }
  91. }
  92. [Browsable (false)]
  93. [EditorBrowsable (EditorBrowsableState.Never)]
  94. public new event EventHandler ImeModeChanged {
  95. add { base.ImeModeChanged += value; }
  96. remove { base.ImeModeChanged -= value; }
  97. }
  98. [Browsable (false)]
  99. [EditorBrowsable (EditorBrowsableState.Never)]
  100. public new event PaintEventHandler Paint {
  101. add { base.Paint += value; }
  102. remove { base.Paint -= value; }
  103. }
  104. [Browsable (false)]
  105. [EditorBrowsable (EditorBrowsableState.Never)]
  106. public new event EventHandler RightToLeftChanged {
  107. add { base.RightToLeftChanged += value; }
  108. remove { base.RightToLeftChanged -= value; }
  109. }
  110. [Browsable (false)]
  111. [EditorBrowsable (EditorBrowsableState.Never)]
  112. public new event EventHandler TextChanged {
  113. add { base.TextChanged += value; }
  114. remove { base.TextChanged -= value; }
  115. }
  116. #endregion Events
  117. #region Constructor
  118. public ToolBar ()
  119. {
  120. background_color = ThemeEngine.Current.DefaultControlBackColor;
  121. foreground_color = ThemeEngine.Current.DefaultControlForeColor;
  122. buttons = new ToolBarButtonCollection (this);
  123. Dock = DockStyle.Top;
  124. GotFocus += new EventHandler (FocusChanged);
  125. LostFocus += new EventHandler (FocusChanged);
  126. MouseDown += new MouseEventHandler (ToolBar_MouseDown);
  127. MouseHover += new EventHandler (ToolBar_MouseHover);
  128. MouseLeave += new EventHandler (ToolBar_MouseLeave);
  129. MouseMove += new MouseEventHandler (ToolBar_MouseMove);
  130. MouseUp += new MouseEventHandler (ToolBar_MouseUp);
  131. BackgroundImageChanged += new EventHandler (ToolBar_BackgroundImageChanged);
  132. TabStop = false;
  133. SetStyle (ControlStyles.UserPaint, false);
  134. SetStyle (ControlStyles.FixedHeight, true);
  135. SetStyle (ControlStyles.FixedWidth, false);
  136. }
  137. #endregion Constructor
  138. #region protected Properties
  139. protected override CreateParams CreateParams {
  140. get {
  141. CreateParams create_params = base.CreateParams;
  142. if (appearance == ToolBarAppearance.Flat) {
  143. create_params.Style |= (int) ToolBarStyles.TBSTYLE_FLAT;
  144. }
  145. return create_params;
  146. }
  147. }
  148. protected override ImeMode DefaultImeMode {
  149. get { return ImeMode.Disable; }
  150. }
  151. protected override Size DefaultSize {
  152. get { return ThemeEngine.Current.ToolBarDefaultSize; }
  153. }
  154. [EditorBrowsable (EditorBrowsableState.Never)]
  155. protected override bool DoubleBuffered {
  156. get { return base.DoubleBuffered; }
  157. set { base.DoubleBuffered = value; }
  158. }
  159. #endregion
  160. ToolBarAppearance appearance = ToolBarAppearance.Normal;
  161. #region Public Properties
  162. [DefaultValue (ToolBarAppearance.Normal)]
  163. [Localizable (true)]
  164. public ToolBarAppearance Appearance {
  165. get { return appearance; }
  166. set {
  167. if (value == appearance)
  168. return;
  169. appearance = value;
  170. Redraw (true);
  171. }
  172. }
  173. bool autosize = true;
  174. [Browsable (true)]
  175. [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
  176. [EditorBrowsable (EditorBrowsableState.Always)]
  177. [DefaultValue (true)]
  178. [Localizable (true)]
  179. public override bool AutoSize {
  180. get { return autosize; }
  181. set {
  182. if (value == autosize)
  183. return;
  184. autosize = value;
  185. if (IsHandleCreated)
  186. Redraw (true);
  187. }
  188. }
  189. [Browsable (false)]
  190. [EditorBrowsable (EditorBrowsableState.Never)]
  191. public override Color BackColor {
  192. get { return background_color; }
  193. set {
  194. if (value == background_color)
  195. return;
  196. background_color = value;
  197. OnBackColorChanged (EventArgs.Empty);
  198. Redraw (false);
  199. }
  200. }
  201. [Browsable (false)]
  202. [EditorBrowsable (EditorBrowsableState.Never)]
  203. public override Image BackgroundImage {
  204. get { return base.BackgroundImage; }
  205. set { base.BackgroundImage = value; }
  206. }
  207. [Browsable (false)]
  208. [EditorBrowsable (EditorBrowsableState.Never)]
  209. public override ImageLayout BackgroundImageLayout {
  210. get { return base.BackgroundImageLayout; }
  211. set { base.BackgroundImageLayout = value; }
  212. }
  213. [DefaultValue (BorderStyle.None)]
  214. [DispIdAttribute (-504)]
  215. public BorderStyle BorderStyle {
  216. get { return InternalBorderStyle; }
  217. set { InternalBorderStyle = value; }
  218. }
  219. ToolBarButtonCollection buttons;
  220. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  221. [Localizable (true)]
  222. [MergableProperty (false)]
  223. public ToolBarButtonCollection Buttons {
  224. get { return buttons; }
  225. }
  226. Size button_size;
  227. [Localizable (true)]
  228. [RefreshProperties (RefreshProperties.All)]
  229. public Size ButtonSize {
  230. get {
  231. if (!button_size.IsEmpty)
  232. return button_size;
  233. if (buttons.Count == 0)
  234. return new Size (39, 36);
  235. Size result = CalcButtonSize ();
  236. if (result.IsEmpty)
  237. return new Size (24, 22);
  238. else
  239. return result;
  240. }
  241. set {
  242. size_specified = value != Size.Empty;
  243. if (button_size == value)
  244. return;
  245. button_size = value;
  246. Redraw (true);
  247. }
  248. }
  249. bool divider = true;
  250. [DefaultValue (true)]
  251. public bool Divider {
  252. get { return divider; }
  253. set {
  254. if (value == divider)
  255. return;
  256. divider = value;
  257. Redraw (false);
  258. }
  259. }
  260. [DefaultValue (DockStyle.Top)]
  261. [Localizable (true)]
  262. public override DockStyle Dock {
  263. get { return base.Dock; }
  264. set {
  265. if (base.Dock == value) {
  266. // Call base anyways so layout_type gets set correctly
  267. if (value != DockStyle.None)
  268. base.Dock = value;
  269. return;
  270. }
  271. if (Vertical) {
  272. SetStyle (ControlStyles.FixedWidth, AutoSize);
  273. SetStyle (ControlStyles.FixedHeight, false);
  274. } else {
  275. SetStyle (ControlStyles.FixedHeight, AutoSize);
  276. SetStyle (ControlStyles.FixedWidth, false);
  277. }
  278. LayoutToolBar ();
  279. base.Dock = value;
  280. }
  281. }
  282. bool drop_down_arrows = true;
  283. [DefaultValue (false)]
  284. [Localizable (true)]
  285. public bool DropDownArrows {
  286. get { return drop_down_arrows; }
  287. set {
  288. if (value == drop_down_arrows)
  289. return;
  290. drop_down_arrows = value;
  291. Redraw (true);
  292. }
  293. }
  294. [Browsable (false)]
  295. [EditorBrowsable (EditorBrowsableState.Never)]
  296. public override Color ForeColor {
  297. get { return foreground_color; }
  298. set {
  299. if (value == foreground_color)
  300. return;
  301. foreground_color = value;
  302. OnForeColorChanged (EventArgs.Empty);
  303. Redraw (false);
  304. }
  305. }
  306. ImageList image_list;
  307. [DefaultValue (null)]
  308. public ImageList ImageList {
  309. get { return image_list; }
  310. set {
  311. if (image_list == value)
  312. return;
  313. image_list = value;
  314. Redraw (true);
  315. }
  316. }
  317. [Browsable (false)]
  318. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  319. [EditorBrowsable (EditorBrowsableState.Advanced)]
  320. public Size ImageSize {
  321. get {
  322. if (ImageList == null)
  323. return Size.Empty;
  324. return ImageList.ImageSize;
  325. }
  326. }
  327. // XXX this should probably go away and it should call
  328. // into Control.ImeMode instead.
  329. ImeMode ime_mode = ImeMode.Disable;
  330. [Browsable (false)]
  331. [EditorBrowsable (EditorBrowsableState.Never)]
  332. public new ImeMode ImeMode {
  333. get { return ime_mode; }
  334. set {
  335. if (value == ime_mode)
  336. return;
  337. ime_mode = value;
  338. OnImeModeChanged (EventArgs.Empty);
  339. }
  340. }
  341. [Browsable (false)]
  342. [EditorBrowsable (EditorBrowsableState.Never)]
  343. public override RightToLeft RightToLeft {
  344. get { return base.RightToLeft; }
  345. set {
  346. if (value == base.RightToLeft)
  347. return;
  348. base.RightToLeft = value;
  349. OnRightToLeftChanged (EventArgs.Empty);
  350. }
  351. }
  352. // Default value is "false" but after make a test in .NET we get "true" result as default.
  353. bool show_tooltips = true;
  354. [DefaultValue (false)]
  355. [Localizable (true)]
  356. public bool ShowToolTips {
  357. get { return show_tooltips; }
  358. set { show_tooltips = value; }
  359. }
  360. [DefaultValue (false)]
  361. public new bool TabStop {
  362. get { return base.TabStop; }
  363. set { base.TabStop = value; }
  364. }
  365. [Bindable (false)]
  366. [Browsable (false)]
  367. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  368. [EditorBrowsable (EditorBrowsableState.Never)]
  369. public override string Text {
  370. get { return base.Text; }
  371. set {
  372. if (value == base.Text)
  373. return;
  374. base.Text = value;
  375. Redraw (true);
  376. }
  377. }
  378. ToolBarTextAlign text_alignment = ToolBarTextAlign.Underneath;
  379. [DefaultValue (ToolBarTextAlign.Underneath)]
  380. [Localizable (true)]
  381. public ToolBarTextAlign TextAlign {
  382. get { return text_alignment; }
  383. set {
  384. if (value == text_alignment)
  385. return;
  386. text_alignment = value;
  387. Redraw (true);
  388. }
  389. }
  390. bool wrappable = true;
  391. [DefaultValue (true)]
  392. [Localizable (true)]
  393. public bool Wrappable {
  394. get { return wrappable; }
  395. set {
  396. if (value == wrappable)
  397. return;
  398. wrappable = value;
  399. Redraw (true);
  400. }
  401. }
  402. #endregion Public Properties
  403. #region Public Methods
  404. public override string ToString ()
  405. {
  406. int count = this.Buttons.Count;
  407. if (count == 0)
  408. return string.Format ("System.Windows.Forms.ToolBar, Buttons.Count: 0");
  409. else
  410. return string.Format ("System.Windows.Forms.ToolBar, Buttons.Count: {0}, Buttons[0]: {1}",
  411. count, this.Buttons [0].ToString ());
  412. }
  413. #endregion Public Methods
  414. #region Protected Methods
  415. protected override void CreateHandle ()
  416. {
  417. base.CreateHandle ();
  418. default_size = CalcButtonSize ();
  419. // In win32 the recalculate size only happens for not flat style
  420. if (appearance != ToolBarAppearance.Flat)
  421. Redraw (true);
  422. }
  423. protected override void Dispose (bool disposing)
  424. {
  425. if (disposing)
  426. ImageList = null;
  427. base.Dispose (disposing);
  428. }
  429. private ToolBarButton button_for_focus = null;
  430. internal void UIAPerformClick (ToolBarButton button)
  431. {
  432. ToolBarItem previous_item = current_item;
  433. current_item = null;
  434. foreach (ToolBarItem item in items)
  435. if (item.Button == button) {
  436. current_item = item;
  437. break;
  438. }
  439. try {
  440. if (current_item == null)
  441. throw new ArgumentException ("button", "The button specified is not part of this toolbar");
  442. PerformButtonClick (new ToolBarButtonClickEventArgs (button));
  443. } finally {
  444. current_item = previous_item;
  445. }
  446. }
  447. void PerformButtonClick (ToolBarButtonClickEventArgs e)
  448. {
  449. // Only change pushed for ToogleButton
  450. if (e.Button.Style == ToolBarButtonStyle.ToggleButton) {
  451. if (! e.Button.Pushed)
  452. e.Button.Pushed = true;
  453. else
  454. e.Button.Pushed = false;
  455. }
  456. current_item.Pressed = false;
  457. current_item.Invalidate ();
  458. button_for_focus = current_item.Button;
  459. button_for_focus.UIAHasFocus = true;
  460. OnButtonClick (e);
  461. }
  462. protected virtual void OnButtonClick (ToolBarButtonClickEventArgs e)
  463. {
  464. ToolBarButtonClickEventHandler eh = (ToolBarButtonClickEventHandler)(Events [ButtonClickEvent]);
  465. if (eh != null)
  466. eh (this, e);
  467. }
  468. protected virtual void OnButtonDropDown (ToolBarButtonClickEventArgs e)
  469. {
  470. ToolBarButtonClickEventHandler eh = (ToolBarButtonClickEventHandler)(Events [ButtonDropDownEvent]);
  471. if (eh != null)
  472. eh (this, e);
  473. if (e.Button.DropDownMenu == null)
  474. return;
  475. ShowDropDownMenu (current_item);
  476. }
  477. internal void ShowDropDownMenu (ToolBarItem item)
  478. {
  479. Point loc = new Point (item.Rectangle.X + 1, item.Rectangle.Bottom + 1);
  480. ((ContextMenu) item.Button.DropDownMenu).Show (this, loc);
  481. item.DDPressed = false;
  482. item.Hilight = false;
  483. item.Invalidate ();
  484. }
  485. protected override void OnFontChanged (EventArgs e)
  486. {
  487. base.OnFontChanged (e);
  488. Redraw (true);
  489. }
  490. protected override void OnHandleCreated (EventArgs e)
  491. {
  492. base.OnHandleCreated (e);
  493. }
  494. protected override void OnResize (EventArgs e)
  495. {
  496. base.OnResize (e);
  497. LayoutToolBar ();
  498. }
  499. protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
  500. {
  501. specified &= ~BoundsSpecified.Height;
  502. base.ScaleControl (factor, specified);
  503. }
  504. [EditorBrowsable (EditorBrowsableState.Never)]
  505. protected override void ScaleCore (float dx, float dy)
  506. {
  507. dy = 1.0f;
  508. base.ScaleCore (dx, dy);
  509. }
  510. private int requested_size = -1;
  511. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  512. {
  513. if (Vertical) {
  514. if (!AutoSize && (requested_size != width) && ((specified & BoundsSpecified.Width) != BoundsSpecified.None))
  515. requested_size = width;
  516. } else {
  517. if (!AutoSize && (requested_size != height) && ((specified & BoundsSpecified.Height) != BoundsSpecified.None))
  518. requested_size = height;
  519. }
  520. base.SetBoundsCore (x, y, width, height, specified);
  521. }
  522. protected override void WndProc (ref Message m)
  523. {
  524. base.WndProc (ref m);
  525. }
  526. internal override bool InternalPreProcessMessage (ref Message msg)
  527. {
  528. if (msg.Msg == (int)Msg.WM_KEYDOWN) {
  529. Keys key_data = (Keys)msg.WParam.ToInt32();
  530. if (HandleKeyDown (ref msg, key_data))
  531. return true;
  532. }
  533. return base.InternalPreProcessMessage (ref msg);
  534. }
  535. #endregion Protected Methods
  536. #region Private Methods
  537. internal int CurrentItem {
  538. get {
  539. return Array.IndexOf (items, current_item);
  540. }
  541. set {
  542. if (current_item != null)
  543. current_item.Hilight = false;
  544. current_item = value == -1 ? null : items [value];
  545. if (current_item != null)
  546. current_item.Hilight = true;
  547. }
  548. }
  549. private void FocusChanged (object sender, EventArgs args)
  550. {
  551. if (!Focused && button_for_focus != null)
  552. button_for_focus.UIAHasFocus = false;
  553. button_for_focus = null;
  554. if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
  555. return;
  556. ToolBarItem prelit = null;
  557. foreach (ToolBarItem item in items) {
  558. if (item.Hilight) {
  559. prelit = item;
  560. break;
  561. }
  562. }
  563. if (Focused && prelit == null) {
  564. foreach (ToolBarItem item in items) {
  565. if (item.Button.Enabled) {
  566. item.Hilight = true;
  567. break;
  568. }
  569. }
  570. } else if (prelit != null) {
  571. prelit.Hilight = false;
  572. }
  573. }
  574. private bool HandleKeyDown (ref Message msg, Keys key_data)
  575. {
  576. if (Appearance != ToolBarAppearance.Flat || Buttons.Count == 0)
  577. return false;
  578. // Handle the key as needed if the current item is a dropdownbutton.
  579. if (HandleKeyOnDropDown (ref msg, key_data))
  580. return true;
  581. switch (key_data) {
  582. case Keys.Left:
  583. case Keys.Up:
  584. HighlightButton (-1);
  585. return true;
  586. case Keys.Right:
  587. case Keys.Down:
  588. HighlightButton (1);
  589. return true;
  590. case Keys.Enter:
  591. case Keys.Space:
  592. if (current_item != null) {
  593. OnButtonClick (new ToolBarButtonClickEventArgs (current_item.Button));
  594. return true;
  595. }
  596. break;
  597. }
  598. return false;
  599. }
  600. bool HandleKeyOnDropDown (ref Message msg, Keys key_data)
  601. {
  602. if (current_item == null || current_item.Button.Style != ToolBarButtonStyle.DropDownButton ||
  603. current_item.Button.DropDownMenu == null)
  604. return false;
  605. Menu dropdown_menu = current_item.Button.DropDownMenu;
  606. if (dropdown_menu.Tracker.active) {
  607. dropdown_menu.ProcessCmdKey (ref msg, key_data);
  608. return true; // always true if the menu is active
  609. }
  610. if (key_data == Keys.Up || key_data == Keys.Down) {
  611. current_item.DDPressed = true;
  612. current_item.Invalidate ();
  613. OnButtonDropDown (new ToolBarButtonClickEventArgs (current_item.Button));
  614. return true;
  615. }
  616. return false;
  617. }
  618. void HighlightButton (int offset)
  619. {
  620. ArrayList enabled = new ArrayList ();
  621. int count = 0;
  622. int start = -1;
  623. ToolBarItem curr_item = null;
  624. foreach (ToolBarItem item in items) {
  625. if (item.Hilight) {
  626. start = count;
  627. curr_item = item;
  628. }
  629. if (item.Button.Enabled) {
  630. enabled.Add (item);
  631. count++;
  632. }
  633. }
  634. int next = (start + offset) % count;
  635. if (next < 0)
  636. next = count - 1;
  637. if (next == start)
  638. return;
  639. if (curr_item != null)
  640. curr_item.Hilight = false;
  641. current_item = enabled [next] as ToolBarItem;
  642. current_item.Hilight = true;
  643. }
  644. private void ToolBar_BackgroundImageChanged (object sender, EventArgs args)
  645. {
  646. Redraw (false, true);
  647. }
  648. private void ToolBar_MouseDown (object sender, MouseEventArgs me)
  649. {
  650. if ((!Enabled) || ((me.Button & MouseButtons.Left) == 0))
  651. return;
  652. Point loc = new Point (me.X, me.Y);
  653. if (ItemAtPoint (loc) == null)
  654. return;
  655. // Hide tooltip when left mouse button
  656. if ((tip_window != null) && (tip_window.Visible) && ((me.Button & MouseButtons.Left) == MouseButtons.Left)) {
  657. TipDownTimer.Stop ();
  658. tip_window.Hide (this);
  659. }
  660. // draw the pushed button
  661. foreach (ToolBarItem item in items) {
  662. if (item.Button.Enabled && item.Rectangle.Contains (loc)) {
  663. // Mark the DropDown rect as pressed.
  664. // We don't redraw the dropdown rect.
  665. if (item.Button.Style == ToolBarButtonStyle.DropDownButton) {
  666. Rectangle rect = item.Rectangle;
  667. if (DropDownArrows) {
  668. rect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
  669. rect.X = item.Rectangle.Right - rect.Width;
  670. }
  671. if (rect.Contains (loc)) {
  672. if (item.Button.DropDownMenu != null) {
  673. item.DDPressed = true;
  674. Invalidate (rect);
  675. }
  676. break;
  677. }
  678. }
  679. item.Pressed = true;
  680. item.Inside = true;
  681. item.Invalidate ();
  682. break;
  683. }
  684. }
  685. }
  686. private void ToolBar_MouseUp (object sender, MouseEventArgs me)
  687. {
  688. if ((!Enabled) || ((me.Button & MouseButtons.Left) == 0))
  689. return;
  690. Point loc = new Point (me.X, me.Y);
  691. // draw the normal button
  692. // Make a copy in case the list is modified during enumeration
  693. ArrayList items = new ArrayList (this.items);
  694. foreach (ToolBarItem item in items) {
  695. if (item.Button.Enabled && item.Rectangle.Contains (loc)) {
  696. if (item.Button.Style == ToolBarButtonStyle.DropDownButton) {
  697. Rectangle ddRect = item.Rectangle;
  698. ddRect.Width = ThemeEngine.Current.ToolBarDropDownWidth;
  699. ddRect.X = item.Rectangle.Right - ddRect.Width;
  700. if (ddRect.Contains (loc)) {
  701. current_item = item;
  702. if (item.DDPressed)
  703. OnButtonDropDown (new ToolBarButtonClickEventArgs (item.Button));
  704. continue;
  705. }
  706. }
  707. // Fire a ButtonClick
  708. current_item = item;
  709. if ((item.Pressed) && ((me.Button & MouseButtons.Left) == MouseButtons.Left))
  710. PerformButtonClick (new ToolBarButtonClickEventArgs (item.Button));
  711. } else if (item.Pressed) {
  712. item.Pressed = false;
  713. item.Invalidate ();
  714. }
  715. }
  716. }
  717. private ToolBarItem ItemAtPoint (Point pt)
  718. {
  719. foreach (ToolBarItem item in items)
  720. if (item.Rectangle.Contains (pt))
  721. return item;
  722. return null;
  723. }
  724. ToolTip tip_window = null;
  725. Timer tipdown_timer = null;
  726. private void PopDownTip (object o, EventArgs args)
  727. {
  728. tip_window.Hide (this);
  729. }
  730. private Timer TipDownTimer {
  731. get {
  732. if (tipdown_timer == null) {
  733. tipdown_timer = new Timer ();
  734. tipdown_timer.Enabled = false;
  735. tipdown_timer.Interval = 5000;
  736. tipdown_timer.Tick += new EventHandler (PopDownTip);
  737. }
  738. return tipdown_timer;
  739. }
  740. }
  741. private void ToolBar_MouseHover (object sender, EventArgs e)
  742. {
  743. if (Capture)
  744. return;
  745. if (tip_window == null)
  746. tip_window = new ToolTip ();
  747. ToolBarItem item = ItemAtPoint (PointToClient (Control.MousePosition));
  748. current_item = item;
  749. if (item == null || item.Button.ToolTipText.Length == 0)
  750. return;
  751. tip_window.Present (this, item.Button.ToolTipText);
  752. TipDownTimer.Start ();
  753. }
  754. private void ToolBar_MouseLeave (object sender, EventArgs e)
  755. {
  756. if (tipdown_timer != null)
  757. tipdown_timer.Dispose ();
  758. tipdown_timer = null;
  759. if (tip_window != null)
  760. tip_window.Dispose ();
  761. tip_window = null;
  762. if (!Enabled || current_item == null)
  763. return;
  764. current_item.Hilight = false;
  765. current_item = null;
  766. }
  767. private void ToolBar_MouseMove (object sender, MouseEventArgs me)
  768. {
  769. if (!Enabled)
  770. return;
  771. if (tip_window != null && tip_window.Visible) {
  772. TipDownTimer.Stop ();
  773. TipDownTimer.Start ();
  774. }
  775. Point loc = new Point (me.X, me.Y);
  776. if (Capture) {
  777. // If the button was pressed and we leave, release the
  778. // button press and vice versa
  779. foreach (ToolBarItem item in items) {
  780. if (item.Pressed &&
  781. (item.Inside != item.Rectangle.Contains (loc))) {
  782. item.Inside = item.Rectangle.Contains (loc);
  783. item.Hilight = false;
  784. break;
  785. }
  786. }
  787. return;
  788. }
  789. if (current_item != null && current_item.Rectangle.Contains (loc)) {
  790. if (ThemeEngine.Current.ToolBarHasHotElementStyles (this)) {
  791. if (current_item.Hilight || (!ThemeEngine.Current.ToolBarHasHotCheckedElementStyles && current_item.Button.Pushed) || !current_item.Button.Enabled)
  792. return;
  793. current_item.Hilight = true;
  794. }
  795. } else {
  796. if (tip_window != null) {
  797. if (tip_window.Visible) {
  798. tip_window.Hide (this);
  799. TipDownTimer.Stop ();
  800. }
  801. current_item = ItemAtPoint (loc);
  802. if (current_item != null && current_item.Button.ToolTipText.Length > 0) {
  803. tip_window.Present (this, current_item.Button.ToolTipText);
  804. TipDownTimer.Start ();
  805. }
  806. }
  807. if (ThemeEngine.Current.ToolBarHasHotElementStyles (this)) {
  808. foreach (ToolBarItem item in items) {
  809. if (item.Rectangle.Contains (loc) && item.Button.Enabled) {
  810. current_item = item;
  811. if (current_item.Hilight || (!ThemeEngine.Current.ToolBarHasHotCheckedElementStyles && current_item.Button.Pushed))
  812. continue;
  813. current_item.Hilight = true;
  814. }
  815. else if (item.Hilight) {
  816. item.Hilight = false;
  817. }
  818. }
  819. }
  820. }
  821. }
  822. internal override void OnPaintInternal (PaintEventArgs pevent)
  823. {
  824. if (GetStyle (ControlStyles.UserPaint))
  825. return;
  826. ThemeEngine.Current.DrawToolBar (pevent.Graphics, pevent.ClipRectangle, this);
  827. // Toolbars do not raise OnPaint unless UserPaint is set
  828. pevent.Handled = true;
  829. }
  830. internal void Redraw (bool recalculate)
  831. {
  832. Redraw (recalculate, true);
  833. }
  834. internal void Redraw (bool recalculate, bool force)
  835. {
  836. bool invalidate = true;
  837. if (recalculate)
  838. invalidate = LayoutToolBar ();
  839. if (force || invalidate)
  840. Invalidate ();
  841. }
  842. internal bool SizeSpecified {
  843. get { return size_specified; }
  844. }
  845. internal bool Vertical {
  846. get { return (Dock == DockStyle.Left) || (Dock == DockStyle.Right); }
  847. }
  848. internal const int text_padding = 3;
  849. private Size CalcButtonSize ()
  850. {
  851. if (Buttons.Count == 0)
  852. return Size.Empty;
  853. string longest_text = Buttons [0].Text;
  854. for (int i = 1; i < Buttons.Count; i++) {
  855. if (Buttons[i].Text.Length > longest_text.Length)
  856. longest_text = Buttons[i].Text;
  857. }
  858. Size size = Size.Empty;
  859. if (longest_text != null && longest_text.Length > 0) {
  860. SizeF sz = TextRenderer.MeasureString (longest_text, Font);
  861. if (sz != SizeF.Empty)
  862. size = new Size ((int) Math.Ceiling (sz.Width) + 2 * text_padding, (int) Math.Ceiling (sz.Height));
  863. }
  864. Size img_size = ImageList == null ? new Size (16, 16) : ImageSize;
  865. Theme theme = ThemeEngine.Current;
  866. int imgWidth = img_size.Width + 2 * theme.ToolBarImageGripWidth;
  867. int imgHeight = img_size.Height + 2 * theme.ToolBarImageGripWidth;
  868. if (text_alignment == ToolBarTextAlign.Right) {
  869. size.Width = imgWidth + size.Width;
  870. size.Height = (size.Height > imgHeight) ? size.Height : imgHeight;
  871. } else {
  872. size.Height = imgHeight + size.Height;
  873. size.Width = (size.Width > imgWidth) ? size.Width : imgWidth;
  874. }
  875. size.Width += theme.ToolBarImageGripWidth;
  876. size.Height += theme.ToolBarImageGripWidth;
  877. return size;
  878. }
  879. // Flat toolbars disregard specified sizes. Normal toolbars grow the
  880. // button size to be at least large enough to show the image.
  881. private Size AdjustedButtonSize {
  882. get {
  883. Size size;
  884. if (default_size.IsEmpty || Appearance == ToolBarAppearance.Normal)
  885. size = ButtonSize;
  886. else
  887. size = default_size;
  888. if (size_specified) {
  889. if (Appearance == ToolBarAppearance.Flat)
  890. size = CalcButtonSize ();
  891. else {
  892. int grip = ThemeEngine.Current.ToolBarImageGripWidth;
  893. if (size.Width < ImageSize.Width + 2 * grip )
  894. size.Width = ImageSize.Width + 2 * grip;
  895. if (size.Height < ImageSize.Height + 2 * grip)
  896. size.Height = ImageSize.Height + 2 * grip;
  897. }
  898. }
  899. return size;
  900. }
  901. }
  902. private bool LayoutToolBar ()
  903. {
  904. bool changed = false;
  905. Theme theme = ThemeEngine.Current;
  906. int x = theme.ToolBarGripWidth;
  907. int y = theme.ToolBarGripWidth;
  908. Size adjusted_size = AdjustedButtonSize;
  909. int calculated_size = (Vertical ? adjusted_size.Width : adjusted_size.Height) + theme.ToolBarGripWidth;
  910. int separator_index = -1;
  911. items = new ToolBarItem [buttons.Count];
  912. for (int i = 0; i < buttons.Count; i++) {
  913. ToolBarButton button = buttons [i];
  914. ToolBarItem item = new ToolBarItem (button);
  915. items [i] = item;
  916. if (!button.Visible)
  917. continue;
  918. if (size_specified && (button.Style != ToolBarButtonStyle.Separator))
  919. changed = item.Layout (adjusted_size);
  920. else
  921. changed = item.Layout (Vertical, calculated_size);
  922. bool is_separator = button.Style == ToolBarButtonStyle.Separator;
  923. if (Vertical) {
  924. if (y + item.Rectangle.Height < Height || is_separator || !Wrappable) {
  925. if (item.Location.X != x || item.Location.Y != y)
  926. changed = true;
  927. item.Location = new Point (x, y);
  928. y += item.Rectangle.Height;
  929. if (is_separator)
  930. separator_index = i;
  931. } else if (separator_index > 0) {
  932. i = separator_index;
  933. separator_index = -1;
  934. y = theme.ToolBarGripWidth;
  935. x += calculated_size;
  936. } else {
  937. y = theme.ToolBarGripWidth;
  938. x += calculated_size;
  939. if (item.Location.X != x || item.Location.Y != y)
  940. changed = true;
  941. item.Location = new Point (x, y);
  942. y += item.Rectangle.Height;
  943. }
  944. } else {
  945. if (x + item.Rectangle.Width < Width || is_separator || !Wrappable) {
  946. if (item.Location.X != x || item.Location.Y != y)
  947. changed = true;
  948. item.Location = new Point (x, y);
  949. x += item.Rectangle.Width;
  950. if (is_separator)
  951. separator_index = i;
  952. } else if (separator_index > 0) {
  953. i = separator_index;
  954. separator_index = -1;
  955. x = theme.ToolBarGripWidth;
  956. y += calculated_size;
  957. } else {
  958. x = theme.ToolBarGripWidth;
  959. y += calculated_size;
  960. if (item.Location.X != x || item.Location.Y != y)
  961. changed = true;
  962. item.Location = new Point (x, y);
  963. x += item.Rectangle.Width;
  964. }
  965. }
  966. }
  967. if (Parent == null)
  968. return changed;
  969. if (Wrappable)
  970. calculated_size += Vertical ? x : y;
  971. if (IsHandleCreated) {
  972. if (Vertical)
  973. Width = calculated_size;
  974. else
  975. Height = calculated_size;
  976. }
  977. return changed;
  978. }
  979. #endregion Private Methods
  980. #region subclass
  981. public class ToolBarButtonCollection : IList, ICollection, IEnumerable
  982. {
  983. #region instance variables
  984. private ArrayList list; // ToolBarButton list
  985. private ToolBar owner; // ToolBar associated to Collection
  986. private bool redraw; // Flag if needs to redraw after add/remove operations
  987. #endregion
  988. #region UIA Framework Events
  989. static object UIACollectionChangedEvent = new object ();
  990. internal event CollectionChangeEventHandler UIACollectionChanged {
  991. add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
  992. remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
  993. }
  994. internal void OnUIACollectionChanged (CollectionChangeEventArgs e)
  995. {
  996. CollectionChangeEventHandler eh
  997. = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
  998. if (eh != null)
  999. eh (owner, e);
  1000. }
  1001. #endregion
  1002. #region constructors
  1003. public ToolBarButtonCollection (ToolBar owner)
  1004. {
  1005. this.list = new ArrayList ();
  1006. this.owner = owner;
  1007. this.redraw = true;
  1008. }
  1009. #endregion
  1010. #region properties
  1011. [Browsable (false)]
  1012. public int Count {
  1013. get { return list.Count; }
  1014. }
  1015. public bool IsReadOnly {
  1016. get { return list.IsReadOnly; }
  1017. }
  1018. public virtual ToolBarButton this [int index] {
  1019. get { return (ToolBarButton) list [index]; }
  1020. set {
  1021. // UIA Framework Event: Button Removed
  1022. OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
  1023. value.SetParent (owner);
  1024. list [index] = value;
  1025. owner.Redraw (true);
  1026. // UIA Framework Event: Button Added
  1027. OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
  1028. }
  1029. }
  1030. public virtual ToolBarButton this[string key] {
  1031. get {
  1032. if (string.IsNullOrEmpty (key))
  1033. return null;
  1034. foreach (ToolBarButton b in list)
  1035. if (string.Compare (b.Name, key, true) == 0)
  1036. return b;
  1037. return null;
  1038. }
  1039. }
  1040. bool ICollection.IsSynchronized {
  1041. get { return list.IsSynchronized; }
  1042. }
  1043. object ICollection.SyncRoot {
  1044. get { return list.SyncRoot; }
  1045. }
  1046. bool IList.IsFixedSize {
  1047. get { return list.IsFixedSize; }
  1048. }
  1049. object IList.this [int index] {
  1050. get { return this [index]; }
  1051. set {
  1052. if (! (value is ToolBarButton))
  1053. throw new ArgumentException("Not of type ToolBarButton", "value");
  1054. this [index] = (ToolBarButton) value;
  1055. }
  1056. }
  1057. #endregion
  1058. #region methods
  1059. public int Add (string text)
  1060. {
  1061. ToolBarButton button = new ToolBarButton (text);
  1062. return this.Add (button);
  1063. }
  1064. public int Add (ToolBarButton button)
  1065. {
  1066. int result;
  1067. button.SetParent (owner);
  1068. result = list.Add (button);
  1069. if (redraw)
  1070. owner.Redraw (true);
  1071. // UIA Framework Event: Button Added
  1072. OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, result));
  1073. return result;
  1074. }
  1075. public void AddRange (ToolBarButton [] buttons)
  1076. {
  1077. try {
  1078. redraw = false;
  1079. foreach (ToolBarButton button in buttons)
  1080. Add (button);
  1081. }
  1082. finally {
  1083. redraw = true;
  1084. owner.Redraw (true);
  1085. }
  1086. }
  1087. public void Clear ()
  1088. {
  1089. list.Clear ();
  1090. owner.Redraw (false);
  1091. // UIA Framework Event: Button Cleared
  1092. OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, -1));
  1093. }
  1094. public bool Contains (ToolBarButton button)
  1095. {
  1096. return list.Contains (button);
  1097. }
  1098. public virtual bool ContainsKey (string key)
  1099. {
  1100. return !(this[key] == null);
  1101. }
  1102. public IEnumerator GetEnumerator ()
  1103. {
  1104. return list.GetEnumerator ();
  1105. }
  1106. void ICollection.CopyTo (Array dest, int index)
  1107. {
  1108. list.CopyTo (dest, index);
  1109. }
  1110. int IList.Add (object button)
  1111. {
  1112. if (! (button is ToolBarButton)) {
  1113. throw new ArgumentException("Not of type ToolBarButton", "button");
  1114. }
  1115. return this.Add ((ToolBarButton) button);
  1116. }
  1117. bool IList.Contains (object button)
  1118. {
  1119. if (! (button is ToolBarButton)) {
  1120. throw new ArgumentException("Not of type ToolBarButton", "button");
  1121. }
  1122. return this.Contains ((ToolBarButton) button);
  1123. }
  1124. int IList.IndexOf (object button)
  1125. {
  1126. if (! (button is ToolBarButton)) {
  1127. throw new ArgumentException("Not of type ToolBarButton", "button");
  1128. }
  1129. return this.IndexOf ((ToolBarButton) button);
  1130. }
  1131. void IList.Insert (int index, object button)
  1132. {
  1133. if (! (button is ToolBarButton)) {
  1134. throw new ArgumentException("Not of type ToolBarButton", "button");
  1135. }
  1136. this.Insert (index, (ToolBarButton) button);
  1137. }
  1138. void IList.Remove (object button)
  1139. {
  1140. if (! (button is ToolBarButton)) {
  1141. throw new ArgumentException("Not of type ToolBarButton", "button");
  1142. }
  1143. this.Remove ((ToolBarButton) button);
  1144. }
  1145. public int IndexOf (ToolBarButton button)
  1146. {
  1147. return list.IndexOf (button);
  1148. }
  1149. public virtual int IndexOfKey (string key)
  1150. {
  1151. return IndexOf (this[key]);
  1152. }
  1153. public void Insert (int index, ToolBarButton button)
  1154. {
  1155. list.Insert (index, button);
  1156. owner.Redraw (true);
  1157. // UIA Framework Event: Button Added
  1158. OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Add, index));
  1159. }
  1160. public void Remove (ToolBarButton button)
  1161. {
  1162. list.Remove (button);
  1163. owner.Redraw (true);
  1164. }
  1165. public void RemoveAt (int index)
  1166. {
  1167. list.RemoveAt (index);
  1168. owner.Redraw (true);
  1169. // UIA Framework Event: Button Removed
  1170. OnUIACollectionChanged (new CollectionChangeEventArgs (CollectionChangeAction.Remove, index));
  1171. }
  1172. public virtual void RemoveByKey (string key)
  1173. {
  1174. Remove (this[key]);
  1175. }
  1176. #endregion methods
  1177. }
  1178. #endregion subclass
  1179. }
  1180. // Because same button can be added to toolbar multiple times, we need to maintain
  1181. // a list of button information for each positions.
  1182. internal class ToolBarItem : Component
  1183. {
  1184. #region Instance variables
  1185. private ToolBar toolbar; // Parent toolbar
  1186. private ToolBarButton button; // Associated toolBar button
  1187. private Rectangle bounds; // Toolbar button bounds
  1188. private Rectangle image_rect; // Image button bounds
  1189. private Rectangle text_rect; // Text button bounds
  1190. private bool dd_pressed = false; // to check for a mouse down on dropdown rect
  1191. private bool inside = false; // to handle the mouse move event with mouse pressed
  1192. private bool hilight = false; // to hilight buttons in flat style
  1193. private bool pressed = false; // this is to check for mouse down on a button
  1194. #endregion
  1195. #region Constructors
  1196. public ToolBarItem (ToolBarButton button)
  1197. {
  1198. this.toolbar = button.Parent;
  1199. this.button = button;
  1200. }
  1201. #endregion Constructors
  1202. #region Properties
  1203. public ToolBarButton Button {
  1204. get { return this.button; }
  1205. }
  1206. public Rectangle Rectangle {
  1207. get {
  1208. if (!button.Visible || toolbar == null)
  1209. return Rectangle.Empty;
  1210. if (button.Style == ToolBarButtonStyle.DropDownButton && toolbar.DropDownArrows) {
  1211. Rectangle result = bounds;
  1212. result.Width += ThemeEngine.Current.ToolBarDropDownWidth;
  1213. return result;
  1214. }
  1215. return bounds;
  1216. }
  1217. set { this.bounds = value; }
  1218. }
  1219. public Point Location {
  1220. get { return bounds.Location; }
  1221. set { bounds.Location = value; }
  1222. }
  1223. public Rectangle ImageRectangle {
  1224. get {
  1225. Rectangle result = image_rect;
  1226. result.X += bounds.X;
  1227. result.Y += bounds.Y;
  1228. return result;
  1229. }
  1230. }
  1231. public Rectangle TextRectangle {
  1232. get {
  1233. Rectangle result = text_rect;
  1234. result.X += bounds.X;
  1235. result.Y += bounds.Y;
  1236. return result;
  1237. }
  1238. }
  1239. private Size TextSize {
  1240. get {
  1241. StringFormat text_format = new StringFormat ();
  1242. text_format.HotkeyPrefix = HotkeyPrefix.Hide;
  1243. SizeF sz = TextRenderer.MeasureString (button.Text, toolbar.Font, SizeF.Empty, text_format);
  1244. if (sz == SizeF.Empty)
  1245. return Size.Empty;
  1246. return new Size ((int) Math.Ceiling (sz.Width) + 2 * ToolBar.text_padding, (int) Math.Ceiling (sz.Height));
  1247. }
  1248. }
  1249. public bool Pressed {
  1250. get { return (pressed && inside); }
  1251. set { pressed = value; }
  1252. }
  1253. public bool DDPressed {
  1254. get { return dd_pressed; }
  1255. set { dd_pressed = value; }
  1256. }
  1257. public bool Inside {
  1258. get { return inside; }
  1259. set { inside = value; }
  1260. }
  1261. public bool Hilight {
  1262. get { return hilight; }
  1263. set {
  1264. if (hilight == value)
  1265. return;
  1266. hilight = value;
  1267. Invalidate ();
  1268. }
  1269. }
  1270. #endregion Properties
  1271. #region Methods
  1272. public Size CalculateSize ()
  1273. {
  1274. Theme theme = ThemeEngine.Current;
  1275. int ht = toolbar.ButtonSize.Height + 2 * theme.ToolBarGripWidth;
  1276. if (button.Style == ToolBarButtonStyle.Separator)
  1277. return new Size (theme.ToolBarSeparatorWidth, ht);
  1278. Size size;
  1279. if (TextSize.IsEmpty && (button.Image == null))
  1280. size = toolbar.default_size;
  1281. else
  1282. size = TextSize;
  1283. Size image_size = (toolbar.ImageSize == Size.Empty) ? new Size (16, 16) : toolbar.ImageSize;
  1284. int image_width = image_size.Width + 2 * theme.ToolBarImageGripWidth;
  1285. int image_height = image_size.Height + 2 * theme.ToolBarImageGripWidth;
  1286. if (toolbar.TextAlign == ToolBarTextAlign.Right) {
  1287. size.Width = image_width + size.Width;
  1288. size.Height = (size.Height > image_height) ? size.Height : image_height;
  1289. } else {
  1290. size.Height = image_height + size.Height;
  1291. size.Width = (size.Width > image_width) ? size.Width : image_width;
  1292. }
  1293. size.Width += theme.ToolBarGripWidth;
  1294. size.Height += theme.ToolBarGripWidth;
  1295. return size;
  1296. }
  1297. public bool Layout (bool vertical, int calculated_size)
  1298. {
  1299. if (toolbar == null || !button.Visible)
  1300. return false;
  1301. Size psize = toolbar.ButtonSize;
  1302. Size size = psize;
  1303. if ((!toolbar.SizeSpecified) || (button.Style == ToolBarButtonStyle.Separator)) {
  1304. size = CalculateSize ();
  1305. if (size.Width == 0 || size.Height == 0)
  1306. size = psize;
  1307. if (vertical)
  1308. size.Width = calculated_size;
  1309. else
  1310. size.Height = calculated_size;
  1311. }
  1312. return Layout (size);
  1313. }
  1314. public bool Layout (Size size)
  1315. {
  1316. if (toolbar == null || !button.Visible)
  1317. return false;
  1318. bounds.Size = size;
  1319. Size image_size = (toolbar.ImageSize == Size.Empty) ? new Size (16, 16) : toolbar.ImageSize;
  1320. int grip = ThemeEngine.Current.ToolBarImageGripWidth;
  1321. Rectangle new_image_rect, new_text_rect;
  1322. if (toolbar.TextAlign == ToolBarTextAlign.Underneath) {
  1323. new_image_rect = new Rectangle ((bounds.Size.Width - image_size.Width) / 2 - grip, 0, image_size.Width + 2 + grip, image_size.Height + 2 * grip);
  1324. new_text_rect = new Rectangle (0, new_image_rect.Height, bounds.Size.Width, bounds.Size.Height - new_image_rect.Height - 2 * grip);
  1325. } else {
  1326. new_image_rect = new Rectangle (0, 0, image_size.Width + 2 * grip, image_size.Height + 2 * grip);
  1327. new_text_rect = new Rectangle (new_image_rect.Width, 0, bounds.Size.Width - new_image_rect.Width, bounds.Size.Height - 2 * grip);
  1328. }
  1329. bool changed = false;
  1330. if (new_image_rect != image_rect || new_text_rect != text_rect)
  1331. changed = true;
  1332. image_rect = new_image_rect;
  1333. text_rect = new_text_rect;
  1334. return changed;
  1335. }
  1336. public void Invalidate ()
  1337. {
  1338. if (toolbar != null)
  1339. toolbar.Invalidate (Rectangle);
  1340. }
  1341. #endregion Methods
  1342. }
  1343. }