ToolBar.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. //
  2. // System.Windows.Forms.ToolBar.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Authors:
  24. // Ravindra ([email protected])
  25. //
  26. // TODO:
  27. // - Drawing dotted text when text is too big to draw on a button
  28. // - Hilighting a button in flat appearance, when mouse moves over
  29. // - RightToLeft
  30. // - DropDown ContextMenu
  31. // - Tooltip
  32. //
  33. // Copyright (C) Novell Inc., 2004
  34. //
  35. //
  36. // $Revision: 1.7 $
  37. // $Modtime: $
  38. // $Log: ToolBar.cs,v $
  39. // Revision 1.7 2004/08/22 01:20:14 ravindra
  40. // Correcting the formatting mess of VS.NET.
  41. //
  42. // Revision 1.6 2004/08/22 00:49:37 ravindra
  43. // Probably this completes the missing attributes in toolbar control.
  44. //
  45. // Revision 1.5 2004/08/22 00:03:20 ravindra
  46. // Fixed toolbar control signatures.
  47. //
  48. // Revision 1.4 2004/08/21 01:52:08 ravindra
  49. // Improvments in mouse event handling in the ToolBar control.
  50. //
  51. // Revision 1.3 2004/08/17 02:00:54 ravindra
  52. // Added attributes.
  53. //
  54. // Revision 1.2 2004/08/17 00:48:50 ravindra
  55. // Added attributes.
  56. //
  57. // Revision 1.1 2004/08/15 23:13:15 ravindra
  58. // First Implementation of ToolBar control.
  59. //
  60. //
  61. // NOT COMPLETE
  62. using System.Collections;
  63. using System.Drawing;
  64. using System.Drawing.Imaging;
  65. using System.ComponentModel;
  66. using System.ComponentModel.Design;
  67. using System.Runtime.InteropServices;
  68. namespace System.Windows.Forms
  69. {
  70. [DefaultEvent ("ButtonClick")]
  71. [DefaultProperty ("Buttons")]
  72. [Designer ("System.Windows.Forms.Design.ToolBarDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  73. public class ToolBar : Control
  74. {
  75. #region Instance Variables
  76. private ToolBarAppearance appearance;
  77. private bool autosize;
  78. private BorderStyle borderStyle;
  79. private ToolBarButtonCollection buttons;
  80. private Size buttonSize;
  81. private bool divider;
  82. private bool dropDownArrows;
  83. private ImageList imageList;
  84. private ImeMode imeMode;
  85. private bool showToolTips;
  86. private ToolBarTextAlign textAlignment;
  87. private bool wrappable; // flag to make the toolbar wrappable
  88. private bool recalculate; // flag to make sure that we calculate the toolbar before drawing
  89. private bool redraw; // flag to force redrawing the control
  90. private ToolBarButton currentButton; // the highlighted button
  91. #endregion Instance Variables
  92. #region Events
  93. [Browsable (false)]
  94. [EditorBrowsable (EditorBrowsableState.Never)]
  95. public new event EventHandler BackColorChanged;
  96. [Browsable (false)]
  97. [EditorBrowsable (EditorBrowsableState.Never)]
  98. public new event EventHandler BackgroundImageChanged;
  99. public event ToolBarButtonClickEventHandler ButtonClick;
  100. public event ToolBarButtonClickEventHandler ButtonDropDown;
  101. [Browsable (false)]
  102. [EditorBrowsable (EditorBrowsableState.Never)]
  103. public new event EventHandler ForeColorChanged;
  104. [Browsable (false)]
  105. [EditorBrowsable (EditorBrowsableState.Never)]
  106. public new event EventHandler ImeModeChanged;
  107. [Browsable (false)]
  108. [EditorBrowsable (EditorBrowsableState.Never)]
  109. public new event PaintEventHandler Paint;
  110. [Browsable (false)]
  111. [EditorBrowsable (EditorBrowsableState.Never)]
  112. public new event EventHandler RightToLeftChanged;
  113. [Browsable (false)]
  114. [EditorBrowsable (EditorBrowsableState.Never)]
  115. public new event EventHandler TextChanged;
  116. #endregion Events
  117. #region Constructor
  118. public ToolBar ()
  119. {
  120. appearance = ToolBarAppearance.Normal;
  121. autosize = true;
  122. background_color = ThemeEngine.Current.DefaultControlBackColor;
  123. borderStyle = BorderStyle.None;
  124. buttons = new ToolBarButtonCollection (this);
  125. buttonSize = Size.Empty;
  126. divider = true;
  127. dropDownArrows = false;
  128. foreground_color = ThemeEngine.Current.DefaultControlForeColor;
  129. showToolTips = false;
  130. textAlignment = ToolBarTextAlign.Underneath;
  131. wrappable = true;
  132. redraw = true;
  133. recalculate = true;
  134. // event handlers
  135. this.MouseDown += new MouseEventHandler (ToolBar_MouseDown);
  136. this.MouseLeave += new EventHandler (ToolBar_MouseLeave);
  137. this.MouseMove += new MouseEventHandler (ToolBar_MouseMove);
  138. this.MouseUp += new MouseEventHandler (ToolBar_MouseUp);
  139. base.Paint += new PaintEventHandler (ToolBar_Paint);
  140. }
  141. #endregion Constructor
  142. #region protected Properties
  143. protected override CreateParams CreateParams
  144. {
  145. get {
  146. CreateParams createParams = base.CreateParams;
  147. createParams.ClassName = XplatUI.DefaultClassName;
  148. createParams.Style = (int) WindowStyles.WS_CHILD | (int) WindowStyles.WS_VISIBLE |
  149. (int)WindowStyles.WS_CLIPCHILDREN | (int) WindowStyles.WS_CLIPSIBLINGS;
  150. return createParams;
  151. }
  152. }
  153. protected override ImeMode DefaultImeMode {
  154. get { return ImeMode.Disable; }
  155. }
  156. protected override Size DefaultSize {
  157. get { return new Size (100, 42); }
  158. }
  159. #endregion
  160. #region Public Properties
  161. [DefaultValue (ToolBarAppearance.Normal)]
  162. [Localizable (true)]
  163. public ToolBarAppearance Appearance {
  164. get { return appearance; }
  165. set {
  166. if (value == appearance)
  167. return;
  168. appearance = value;
  169. Redraw (false);
  170. }
  171. }
  172. [DefaultValue (true)]
  173. [Localizable (true)]
  174. public bool AutoSize {
  175. get { return autosize; }
  176. set {
  177. if (value == autosize)
  178. return;
  179. autosize = value;
  180. Redraw (true);
  181. }
  182. }
  183. [Browsable (false)]
  184. [EditorBrowsable (EditorBrowsableState.Never)]
  185. public override Color BackColor {
  186. get { return background_color; }
  187. set {
  188. if (value == background_color)
  189. return;
  190. background_color = value;
  191. if (BackColorChanged != null)
  192. BackColorChanged (this, new EventArgs ());
  193. Redraw (false);
  194. }
  195. }
  196. [Browsable (false)]
  197. [EditorBrowsable (EditorBrowsableState.Never)]
  198. public override Image BackgroundImage {
  199. get { return background_image; }
  200. set {
  201. if (value == background_image)
  202. return;
  203. background_image = value;
  204. if (BackgroundImageChanged != null)
  205. BackgroundImageChanged (this, new EventArgs ());
  206. Redraw (false);
  207. }
  208. }
  209. [DefaultValue (BorderStyle.None)]
  210. [DispIdAttribute (-504)]
  211. public BorderStyle BorderStyle {
  212. get { return borderStyle; }
  213. set {
  214. if (value == borderStyle)
  215. return;
  216. borderStyle = value;
  217. Redraw (false);
  218. }
  219. }
  220. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  221. [Localizable (true)]
  222. [MergableProperty (false)]
  223. public ToolBarButtonCollection Buttons {
  224. get { return buttons; }
  225. }
  226. [Localizable (true)]
  227. [RefreshProperties (RefreshProperties.All)]
  228. public Size ButtonSize {
  229. get {
  230. if (buttonSize.IsEmpty) {
  231. if (buttons.Count == 0)
  232. return new Size (39, 36);
  233. else
  234. return CalcButtonSize ();
  235. }
  236. return buttonSize;
  237. }
  238. set {
  239. if (buttonSize.Width == value.Width && buttonSize.Height == value.Height)
  240. return;
  241. if (value.Width > 0 && value.Height > 0) {
  242. buttonSize = value;
  243. Redraw (true);
  244. }
  245. }
  246. }
  247. [DefaultValue (true)]
  248. public bool Divider {
  249. get { return divider; }
  250. set { divider = value; }
  251. }
  252. [DefaultValue (DockStyle.Top)]
  253. [Localizable (true)]
  254. public override DockStyle Dock {
  255. get { return base.Dock; }
  256. set { base.Dock = value; }
  257. }
  258. [DefaultValue (false)]
  259. [Localizable (true)]
  260. public bool DropDownArrows {
  261. get { return dropDownArrows; }
  262. set {
  263. if (value == dropDownArrows)
  264. return;
  265. dropDownArrows = value;
  266. Redraw (true);
  267. }
  268. }
  269. [Browsable (false)]
  270. [EditorBrowsable (EditorBrowsableState.Never)]
  271. public override Color ForeColor {
  272. get { return foreground_color; }
  273. set {
  274. if (value == foreground_color)
  275. return;
  276. foreground_color = value;
  277. if (ForeColorChanged != null)
  278. ForeColorChanged (this, new EventArgs ());
  279. Redraw (false);
  280. }
  281. }
  282. [DefaultValue (null)]
  283. public ImageList ImageList {
  284. get { return imageList; }
  285. set { imageList = value; }
  286. }
  287. [Browsable (false)]
  288. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  289. [EditorBrowsable (EditorBrowsableState.Advanced)]
  290. public Size ImageSize {
  291. get {
  292. if (imageList == null)
  293. return Size.Empty;
  294. return imageList.ImageSize;
  295. }
  296. }
  297. [Browsable (false)]
  298. [EditorBrowsable (EditorBrowsableState.Never)]
  299. public new ImeMode ImeMode {
  300. get { return imeMode; }
  301. set {
  302. if (value == imeMode)
  303. return;
  304. imeMode = value;
  305. if (ImeModeChanged != null)
  306. ImeModeChanged (this, new EventArgs ());
  307. }
  308. }
  309. [Browsable (false)]
  310. [EditorBrowsable (EditorBrowsableState.Never)]
  311. public override RightToLeft RightToLeft {
  312. get { return base.RightToLeft; }
  313. set {
  314. if (value == base.RightToLeft)
  315. return;
  316. base.RightToLeft = value;
  317. if (RightToLeftChanged != null)
  318. RightToLeftChanged (this, new EventArgs ());
  319. }
  320. }
  321. [DefaultValue (false)]
  322. [Localizable (true)]
  323. public bool ShowToolTips {
  324. get { return showToolTips; }
  325. set { showToolTips = value; }
  326. }
  327. [DefaultValue (false)]
  328. public new bool TabStop {
  329. get { return base.TabStop; }
  330. set { base.TabStop = value; }
  331. }
  332. [Bindable (false)]
  333. [Browsable (false)]
  334. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  335. [EditorBrowsable (EditorBrowsableState.Never)]
  336. public override string Text {
  337. get { return text; }
  338. set {
  339. if (value == text)
  340. return;
  341. text = value;
  342. Redraw (true);
  343. if (TextChanged != null)
  344. TextChanged (this, new EventArgs ());
  345. }
  346. }
  347. [DefaultValue (ToolBarTextAlign.Underneath)]
  348. [Localizable (true)]
  349. public ToolBarTextAlign TextAlign {
  350. get { return textAlignment; }
  351. set {
  352. if (value == textAlignment)
  353. return;
  354. textAlignment = value;
  355. Redraw (true);
  356. }
  357. }
  358. [DefaultValue (true)]
  359. [Localizable (true)]
  360. public bool Wrappable {
  361. get { return wrappable; }
  362. set {
  363. if (value == wrappable)
  364. return;
  365. wrappable = value;
  366. Redraw (true);
  367. }
  368. }
  369. #endregion Public Properties
  370. #region Public Methods
  371. public override string ToString ()
  372. {
  373. int count = this.Buttons.Count;
  374. if (count == 0)
  375. return string.Format ("System.Windows.Forms.ToolBar, Button.Count: 0");
  376. else
  377. return string.Format ("System.Windows.Forms.ToolBar, Button.Count: {0}, Buttons[0]: {1}",
  378. count, this.Buttons [0].ToString ());
  379. }
  380. #endregion Public Methods
  381. #region Internal Methods
  382. internal Rectangle GetChildBounds (ToolBarButton button)
  383. {
  384. if (button.Style == ToolBarButtonStyle.Separator)
  385. return new Rectangle (button.Location.X, button.Location.Y,
  386. ThemeEngine.Current.ToolBarSeparatorWidth, this.ButtonSize.Height);
  387. SizeF sz = this.DeviceContext.MeasureString (button.Text, this.Font);
  388. Size size = new Size ((int) Math.Ceiling (sz.Width), (int) Math.Ceiling (sz.Height));
  389. if (imageList != null) {
  390. // adjustment for the image grip
  391. int imgWidth = this.ImageSize.Width + 2 * ThemeEngine.Current.ToolBarImageGripWidth;
  392. int imgHeight = this.ImageSize.Height + 2 * ThemeEngine.Current.ToolBarImageGripWidth;
  393. if (textAlignment == ToolBarTextAlign.Right) {
  394. size.Width = imgWidth + size.Width;
  395. size.Height = (size.Height > imgHeight) ? size.Height : imgHeight;
  396. }
  397. else {
  398. size.Height = imgHeight + size.Height;
  399. size.Width = (size.Width > imgWidth) ? size.Width : imgWidth;
  400. }
  401. }
  402. if (button.Style == ToolBarButtonStyle.DropDownButton && this.dropDownArrows)
  403. size.Width += ThemeEngine.Current.ToolBarDropDownWidth;
  404. return new Rectangle (button.Location, size);
  405. }
  406. #endregion Internal Methods
  407. #region Protected Methods
  408. protected override void CreateHandle ()
  409. {
  410. base.CreateHandle ();
  411. }
  412. protected override void Dispose (bool disposing)
  413. {
  414. if (disposing) {
  415. if (imageList != null)
  416. imageList.Dispose ();
  417. }
  418. base.Dispose (disposing);
  419. }
  420. protected virtual void OnButtonClick (ToolBarButtonClickEventArgs e)
  421. {
  422. if (e.Button.Style == ToolBarButtonStyle.ToggleButton) {
  423. if (! e.Button.Pushed)
  424. e.Button.Pushed = true;
  425. else
  426. e.Button.Pushed = false;
  427. }
  428. e.Button.Pressed = false;
  429. Redraw (false);
  430. Invalidate (e.Button.Rectangle);
  431. if (e.Button.Style == ToolBarButtonStyle.DropDownButton)
  432. this.OnButtonDropDown (e);
  433. if (ButtonClick != null)
  434. ButtonClick (this, e);
  435. else
  436. return;
  437. }
  438. protected virtual void OnButtonDropDown (ToolBarButtonClickEventArgs e)
  439. {
  440. // if (e.Button.DropDownMenu == null) return;
  441. // TODO: Display the dropdown menu
  442. if (ButtonDropDown != null)
  443. ButtonDropDown (this, e);
  444. else
  445. return;
  446. }
  447. protected override void OnFontChanged (EventArgs e)
  448. {
  449. base.OnFontChanged (e);
  450. Redraw (true);
  451. }
  452. protected override void OnHandleCreated (EventArgs e)
  453. {
  454. base.OnHandleCreated (e);
  455. Refresh ();
  456. }
  457. protected override void OnResize (EventArgs e)
  458. {
  459. base.OnResize (e);
  460. if (this.Width <= 0 || this.Height <= 0 || this.Visible == false)
  461. return;
  462. Redraw (true);
  463. }
  464. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  465. {
  466. base.SetBoundsCore (x, y, width, height, specified);
  467. }
  468. protected override void WndProc (ref Message m)
  469. {
  470. base.WndProc (ref m);
  471. }
  472. #endregion Protected Methods
  473. #region Private Methods
  474. private void ToolBar_MouseDown (object sender, MouseEventArgs me)
  475. {
  476. if (! this.Enabled) return;
  477. Point hit = new Point (me.X, me.Y);
  478. // draw the pushed button
  479. foreach (ToolBarButton button in buttons) {
  480. if (button.Rectangle.Contains (hit) && button.Enabled) {
  481. button.Pressed = true;
  482. Redraw (false);
  483. Invalidate (button.Rectangle);
  484. break;
  485. }
  486. }
  487. }
  488. private void ToolBar_MouseUp (object sender, MouseEventArgs me)
  489. {
  490. if (! this.Enabled) return;
  491. Point hit = new Point (me.X, me.Y);
  492. // draw the normal button
  493. foreach (ToolBarButton button in buttons) {
  494. if (button.Rectangle.Contains (hit) && button.Enabled) {
  495. if (button.Pressed)
  496. this.OnButtonClick (new ToolBarButtonClickEventArgs (button));
  497. else {
  498. button.Pressed = false;
  499. Redraw (false);
  500. Invalidate (button.Rectangle);
  501. }
  502. break;
  503. }
  504. }
  505. }
  506. private void ToolBar_MouseLeave (object sender, EventArgs e)
  507. {
  508. if (! this.Enabled || appearance != ToolBarAppearance.Flat) return;
  509. if (currentButton != null) {
  510. currentButton.Hilight = false;
  511. Redraw (false);
  512. Invalidate (currentButton.Rectangle);
  513. currentButton = null;
  514. }
  515. }
  516. private void ToolBar_MouseMove (object sender, MouseEventArgs me)
  517. {
  518. if (! this.Enabled || appearance != ToolBarAppearance.Flat) return;
  519. Point hit = new Point (me.X, me.Y);
  520. if (currentButton != null && currentButton.Rectangle.Contains (hit)) {
  521. if (currentButton.Hilight)
  522. return;
  523. currentButton.Hilight = true;
  524. Redraw (false);
  525. Invalidate (currentButton.Rectangle);
  526. }
  527. else {
  528. foreach (ToolBarButton button in buttons) {
  529. if (button.Rectangle.Contains (hit) && button.Enabled) {
  530. currentButton = button;
  531. if (currentButton.Hilight)
  532. break;
  533. currentButton.Hilight = true;
  534. Redraw (false);
  535. Invalidate (currentButton.Rectangle);
  536. }
  537. else if (button.Hilight) {
  538. button.Hilight = false;
  539. Redraw (false);
  540. Invalidate (button.Rectangle);
  541. }
  542. }
  543. }
  544. }
  545. private void ToolBar_Paint (object sender, PaintEventArgs pe)
  546. {
  547. if (this.Width <= 0 || this.Height <= 0 || this.Visible == false)
  548. return;
  549. Draw ();
  550. pe.Graphics.DrawImage (this.ImageBuffer, pe.ClipRectangle, pe.ClipRectangle, GraphicsUnit.Pixel);
  551. if (Paint != null)
  552. Paint (this, pe);
  553. }
  554. private void Redraw (bool recalculate)
  555. {
  556. redraw = true;
  557. this.recalculate = recalculate;
  558. }
  559. private void Draw ()
  560. {
  561. if (redraw) {
  562. StringFormat strFormat = new StringFormat ();
  563. if (textAlignment == ToolBarTextAlign.Underneath) {
  564. strFormat.LineAlignment = StringAlignment.Center;
  565. strFormat.Alignment = StringAlignment.Center;
  566. }
  567. else {
  568. strFormat.LineAlignment = StringAlignment.Center;
  569. strFormat.Alignment = StringAlignment.Near;
  570. }
  571. if (recalculate) {
  572. CalcToolBar ();
  573. CreateBuffers (this.Width, this.Height);
  574. }
  575. ThemeEngine.Current.DrawToolBar (this.DeviceContext, this, strFormat);
  576. }
  577. redraw = false;
  578. recalculate = false;
  579. }
  580. private Size CalcButtonSize ()
  581. {
  582. ToolBarButton button;
  583. String longestText = buttons [0].Text;
  584. for (int i = 1; i < buttons.Count; i++) {
  585. if (buttons[i].Text.Length > longestText.Length)
  586. longestText = buttons[i].Text;
  587. }
  588. SizeF sz = this.DeviceContext.MeasureString (longestText, this.Font);
  589. Size size = new Size ((int) Math.Ceiling (sz.Width), (int) Math.Ceiling (sz.Height));
  590. if (imageList != null) {
  591. // adjustment for the image grip
  592. int imgWidth = this.ImageSize.Width + 2 * ThemeEngine.Current.ToolBarImageGripWidth;
  593. int imgHeight = this.ImageSize.Height + 2 * ThemeEngine.Current.ToolBarImageGripWidth;
  594. if (textAlignment == ToolBarTextAlign.Right) {
  595. size.Width = imgWidth + size.Width;
  596. size.Height = (size.Height > imgHeight) ? size.Height : imgHeight;
  597. }
  598. else {
  599. size.Height = imgHeight + size.Height;
  600. size.Width = (size.Width > imgWidth) ? size.Width : imgWidth;
  601. }
  602. }
  603. return size;
  604. }
  605. /* Checks for the separators and sets the location of a button and its wrapper flag */
  606. private void CalcToolBar ()
  607. {
  608. int wd = this.Width; // the amount of space we have for rest of the buttons
  609. int ht = this.ButtonSize.Height; // all buttons are displayed with the same height
  610. Point loc = new Point (0, 0); // the location to place the next button
  611. // clear all the wrappers if toolbar is not wrappable
  612. if (! wrappable && ! autosize) {
  613. if (this.Height != this.DefaultSize.Height)
  614. this.Height = this.DefaultSize.Height;
  615. foreach (ToolBarButton button in buttons) {
  616. button.Location = loc;
  617. button.Wrapper = false;
  618. loc.X = loc.X + button.Rectangle.Width;
  619. }
  620. }
  621. else if (! wrappable) { // autosizeable
  622. if (ht != this.Height)
  623. this.Height = ht;
  624. foreach (ToolBarButton button in buttons) {
  625. button.Location = loc;
  626. button.Wrapper = false;
  627. loc.X = loc.X + button.Rectangle.Width;
  628. }
  629. }
  630. else { // wrappable
  631. bool seenSeparator = false;
  632. int separatorIndex = -1;
  633. ToolBarButton button;
  634. for (int i = 0; i < buttons.Count; i++) {
  635. button = buttons [i];
  636. if (button.Visible) {
  637. if (button.Style == ToolBarButtonStyle.Separator) {
  638. wd -= ThemeEngine.Current.ToolBarSeparatorWidth;
  639. if (wd > 0) {
  640. button.Wrapper = false; // clear the old flag in case it was set
  641. button.Location = loc;
  642. loc.X = loc.X + ThemeEngine.Current.ToolBarSeparatorWidth;
  643. }
  644. else {
  645. button.Wrapper = true;
  646. button.Location = loc;
  647. loc.X = 0;
  648. wd = this.Width;
  649. // we need space to draw horizontal separator
  650. loc.Y = loc.Y + ThemeEngine.Current.ToolBarSeparatorWidth + ht;
  651. }
  652. seenSeparator = true;
  653. separatorIndex = i;
  654. }
  655. else {
  656. Rectangle rect = button.Rectangle;
  657. wd -= rect.Width;
  658. if (wd > 0) {
  659. button.Wrapper = false;
  660. button.Location = loc;
  661. loc.X = loc.X + rect.Width;
  662. }
  663. else if (seenSeparator) {
  664. // wrap at the separator and reassign the locations
  665. i = separatorIndex; // for loop is going to increment it
  666. buttons [separatorIndex].Wrapper = true;
  667. seenSeparator = false;
  668. separatorIndex = -1;
  669. loc.X = 0;
  670. // we need space to draw horizontal separator
  671. loc.Y = loc.Y + ht + ThemeEngine.Current.ToolBarSeparatorWidth;
  672. wd = this.Width;
  673. continue;
  674. }
  675. else {
  676. button.Wrapper = true;
  677. wd = this.Width;
  678. loc.X = 0;
  679. loc.Y += ht;
  680. button.Location = loc;
  681. loc.X = loc.X + rect.Width;
  682. }
  683. }
  684. }
  685. else // don't consider invisible buttons
  686. continue;
  687. }
  688. /* adjust the control height, if we are autosizeable */
  689. if (autosize) // wrappable
  690. if (this.Height != (loc.Y + ht))
  691. this.Height = loc.Y + ht;
  692. }
  693. }
  694. private void DumpToolBar (string msg)
  695. {
  696. Console.WriteLine (msg);
  697. Console.WriteLine ("ToolBar: name: " + this.Text);
  698. Console.WriteLine ("ToolBar: wd, ht: " + this.Size);
  699. Console.WriteLine ("ToolBar: img size: " + this.ImageSize);
  700. Console.WriteLine ("ToolBar: button sz: " + this.buttonSize);
  701. Console.WriteLine ("ToolBar: textalignment: "+ this.TextAlign);
  702. Console.WriteLine ("ToolBar: appearance: "+ this.Appearance);
  703. Console.WriteLine ("ToolBar: wrappable: "+ this.Wrappable);
  704. Console.WriteLine ("ToolBar: buttons count: " + this.Buttons.Count);
  705. int i= 0;
  706. foreach (ToolBarButton b in buttons) {
  707. Console.WriteLine ("ToolBar: button [{0}]:",i++);
  708. b.Dump ();
  709. }
  710. }
  711. #endregion Private Methods
  712. #region subclass
  713. public class ToolBarButtonCollection : IList, ICollection, IEnumerable
  714. {
  715. #region instance variables
  716. private ArrayList buttonsList;
  717. private ToolBar owner;
  718. #endregion
  719. #region constructors
  720. public ToolBarButtonCollection (ToolBar owner)
  721. {
  722. this.owner = owner;
  723. this.buttonsList = new ArrayList ();
  724. }
  725. #endregion
  726. #region properties
  727. [Browsable (false)]
  728. public virtual int Count {
  729. get { return buttonsList.Count; }
  730. }
  731. public virtual bool IsReadOnly {
  732. get { return buttonsList.IsReadOnly; }
  733. }
  734. public virtual ToolBarButton this [int index] {
  735. get { return (ToolBarButton) buttonsList [index]; }
  736. set {
  737. value.SetParent (owner);
  738. buttonsList [index] = value;
  739. owner.Redraw (true);
  740. }
  741. }
  742. bool ICollection.IsSynchronized {
  743. get { return buttonsList.IsSynchronized; }
  744. }
  745. object ICollection.SyncRoot {
  746. get { return buttonsList.SyncRoot; }
  747. }
  748. bool IList.IsFixedSize {
  749. get { return buttonsList.IsFixedSize; }
  750. }
  751. object IList.this [int index] {
  752. get { return this [index]; }
  753. set {
  754. if (! (value is ToolBarButton))
  755. throw new ArgumentException("Not of type ToolBarButton", "value");
  756. this [index] = (ToolBarButton) value;
  757. }
  758. }
  759. #endregion
  760. #region methods
  761. public int Add (string text)
  762. {
  763. ToolBarButton button = new ToolBarButton (text);
  764. return this.Add (button);
  765. }
  766. public int Add (ToolBarButton button)
  767. {
  768. int result;
  769. button.SetParent (owner);
  770. result = buttonsList.Add (button);
  771. owner.Redraw (true);
  772. return result;
  773. }
  774. public void AddRange (ToolBarButton [] buttons)
  775. {
  776. foreach (ToolBarButton button in buttons)
  777. Add (button);
  778. }
  779. public virtual void Clear ()
  780. {
  781. buttonsList.Clear ();
  782. owner.Redraw (false);
  783. }
  784. public bool Contains (ToolBarButton button)
  785. {
  786. return buttonsList.Contains (button);
  787. }
  788. public virtual IEnumerator GetEnumerator ()
  789. {
  790. return buttonsList.GetEnumerator ();
  791. }
  792. void ICollection.CopyTo (Array dest, int index)
  793. {
  794. buttonsList.CopyTo (dest, index);
  795. }
  796. int IList.Add (object button)
  797. {
  798. if (! (button is ToolBarButton)) {
  799. throw new ArgumentException("Not of type ToolBarButton", "button");
  800. }
  801. return this.Add ((ToolBarButton) button);
  802. }
  803. bool IList.Contains (object button)
  804. {
  805. if (! (button is ToolBarButton)) {
  806. throw new ArgumentException("Not of type ToolBarButton", "button");
  807. }
  808. return this.Contains ((ToolBarButton) button);
  809. }
  810. int IList.IndexOf (object button)
  811. {
  812. if (! (button is ToolBarButton)) {
  813. throw new ArgumentException("Not of type ToolBarButton", "button");
  814. }
  815. return this.IndexOf ((ToolBarButton) button);
  816. }
  817. void IList.Insert (int index, object button)
  818. {
  819. if (! (button is ToolBarButton)) {
  820. throw new ArgumentException("Not of type ToolBarButton", "button");
  821. }
  822. this.Insert (index, (ToolBarButton) button);
  823. }
  824. void IList.Remove (object button)
  825. {
  826. if (! (button is ToolBarButton)) {
  827. throw new ArgumentException("Not of type ToolBarButton", "button");
  828. }
  829. this.Remove ((ToolBarButton) button);
  830. }
  831. public int IndexOf (ToolBarButton button)
  832. {
  833. return buttonsList.IndexOf (button);
  834. }
  835. public void Insert (int index, ToolBarButton button)
  836. {
  837. buttonsList.Insert (index, button);
  838. owner.Redraw (true);
  839. }
  840. public void Remove (ToolBarButton button)
  841. {
  842. buttonsList.Remove (button);
  843. owner.Redraw (true);
  844. }
  845. public virtual void RemoveAt (int index)
  846. {
  847. buttonsList.RemoveAt (index);
  848. owner.Redraw (true);
  849. }
  850. #endregion methods
  851. }
  852. #endregion subclass
  853. }
  854. }