PageRenderTime 122ms CodeModel.GetById 0ms RepoModel.GetById 2ms app.codeStats 0ms

/Backup/FireballFX/Fireball.Windows.Forms/Windows/Forms/DiscoverControl.cs

http://gluar.googlecode.com/
C# | 713 lines | 535 code | 163 blank | 15 comment | 88 complexity | 959010d974acde1ccfcdc3aa279d4511 MD5 | raw file
  1. // Copyright (C) 2005 Sebastian Faltoni
  2. //
  3. // This library is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU Lesser General Public
  5. // License as published by the Free Software Foundation; either
  6. // version 2.1 of the License, or (at your option) any later version.
  7. //
  8. // This library is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // Lesser General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU Lesser General Public
  14. // License along with this library; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. using System;
  17. using System.Drawing;
  18. using System.Collections.Generic;
  19. using System.Text;
  20. using System.Windows.Forms;
  21. using System.Drawing.Text;
  22. using System.ComponentModel;
  23. namespace Fireball.Windows.Forms
  24. {
  25. [ToolboxBitmap(typeof(DiscoverControl), "DiscoverControl.bmp"), Designer(typeof(DiscoverControlDesigner)), DefaultEvent("DiscoverPaneChanged")]
  26. public class DiscoverControl : Panel
  27. {
  28. public event CancelEventHandler DiscoverPaneChanging;
  29. public event EventHandler DiscoverPaneChanged;
  30. public event EventHandler DiscoverShowOptions;
  31. public DiscoverPane SelectedPane
  32. {
  33. get { return _SelectedPane; }
  34. set
  35. {
  36. if (value != null && this.Controls.IndexOf(value) == -1)
  37. throw new Exception("You need to add to Controls collection before set it as current pane");
  38. if (_SelectedPane != null)
  39. _SelectedPane.Visible = false;
  40. _SelectedPane = value;
  41. if (_SelectedPane != null)
  42. _SelectedPane.Visible = true;
  43. }
  44. }
  45. public DiscoverPane GetPaneAt(int x, int y)
  46. {
  47. return GetListedPaneAt(new Point(x, y));
  48. }
  49. protected override void OnPaint(PaintEventArgs e)
  50. {
  51. bool rtlEnabled = this.RightToLeft == RightToLeft.Yes;
  52. _CurrentRender.DrawBackground(e.Graphics, this.ClientRectangle, this.BackColor);
  53. if (this.SelectedPane != null)
  54. {
  55. _CurrentRender.DrawHeader(e.Graphics, this.HeaderRect,
  56. this.SelectedPane.Text, _HeaderFont, null, rtlEnabled);
  57. }
  58. if (this.SwitchBarRect != Rectangle.Empty)
  59. {
  60. this._CurrentRender.DrawDiscoverContentPaneBackground(e.Graphics,
  61. this.SwitchBarRect, DrawItemState.None);
  62. }
  63. foreach (Control currentControl in base.Controls)
  64. {
  65. DiscoverPane currentPane = (DiscoverPane)currentControl;
  66. if ((currentPane.Listed && (currentPane.ButtonRect != Rectangle.Empty))
  67. && (currentPane.ButtonRect.X >= 0))
  68. {
  69. DrawItemState paneState = DrawItemState.None;
  70. if (this._SelectedPane == currentPane)
  71. {
  72. paneState |= DrawItemState.HotLight;
  73. }
  74. else if (_MouseOverPane == currentPane)
  75. {
  76. paneState |= DrawItemState.HotLight;
  77. }
  78. if (this._boo && (this._SelectedPane == currentPane))
  79. {
  80. paneState |= DrawItemState.Selected;
  81. }
  82. if (this.SelectedPane == currentPane)
  83. {
  84. paneState |= DrawItemState.Checked;
  85. }
  86. if (!currentPane.Enabled)
  87. {
  88. paneState |= DrawItemState.Disabled;
  89. }
  90. if (currentPane.ShowOnList)//BOOOO
  91. {
  92. this._CurrentRender.DrawDiscoverContentPaneBackground(e.Graphics, currentPane.ListButtonRect, paneState);
  93. this._CurrentRender.DrawDiscoverContentPane(e.Graphics, currentPane.ListButtonRect, paneState,
  94. currentPane, this.FooterFont, rtlEnabled);
  95. continue;
  96. }
  97. if (CanShowButton(currentPane))
  98. {
  99. this._CurrentRender.DrawDiscoverFooterPaneBackground(e.Graphics, currentPane.ButtonRect, paneState);
  100. this._CurrentRender.DrawDiscoverFooterPane(e.Graphics, currentPane.ButtonRect, paneState,
  101. currentPane, this.FooterFont);
  102. }
  103. }
  104. }
  105. if (_DrawGripper)
  106. {
  107. this._CurrentRender.DrawGripper(e.Graphics, this.GripperRect);
  108. }
  109. if (_DrawChewron)
  110. {
  111. this._CurrentRender.DrawChevron(e.Graphics, this.ChewronRect, this.ForeColor);
  112. }
  113. }
  114. private bool CanShowButton(DiscoverPane pane)
  115. {
  116. return this.SwitchBarRect.IntersectsWith(pane.ButtonRect);
  117. }
  118. private Rectangle HeaderRect
  119. {
  120. get
  121. {
  122. Rectangle rect;
  123. int h = this.HeaderFont.Height + 4;
  124. rect = new Rectangle(0, 0, this.ClientSize.Width - 1, h);
  125. return rect;
  126. }
  127. }
  128. public IDiscoverRenderer CurrentRender
  129. {
  130. get { return _CurrentRender; }
  131. set { _CurrentRender = value; }
  132. }
  133. public Font HeaderFont
  134. {
  135. get { return _HeaderFont; }
  136. set { _HeaderFont = value; }
  137. }
  138. internal int ListButtonHeight
  139. {
  140. get
  141. {
  142. return 26;
  143. }
  144. }
  145. internal Rectangle ChewronRect
  146. {
  147. get
  148. {
  149. if (!_DrawChewron)
  150. return Rectangle.Empty;
  151. Rectangle rect = new Rectangle(this.ClientSize.Width - 23,
  152. this.SwitchBarRect.Top, 22, this.SwitchBarRect.Height);
  153. return rect;
  154. }
  155. }
  156. private Rectangle GripperRect
  157. {
  158. get
  159. {
  160. int y = this.ClientSize.Height - 30 - 8;
  161. y -= this.ListButtonHeight * _ShowPanes;
  162. _GripperRect = new Rectangle(1, y,
  163. this.ClientSize.Width - 2, 8);
  164. return _GripperRect;
  165. }
  166. }
  167. internal Rectangle SwitchBarRect
  168. {
  169. get
  170. {
  171. return new Rectangle(1, this.ClientSize.Height - 30,
  172. this.ClientSize.Width - 2, 29);
  173. }
  174. }
  175. public Font FooterFont
  176. {
  177. get
  178. {
  179. return this.Font;
  180. }
  181. }
  182. private DiscoverPane GetListedPaneAt(Point loc)
  183. {
  184. if (this.ShowPanes == 0)
  185. return null;
  186. foreach (DiscoverPane pane in base.Controls)
  187. {
  188. if (pane.ShowOnList && pane.ListButtonRect.Contains(loc))
  189. {
  190. return pane;
  191. }
  192. }
  193. return null;
  194. }
  195. private DiscoverPane GetButtonPaneAt(Point loc)
  196. {
  197. foreach (DiscoverPane pane in base.Controls)
  198. {
  199. if (!pane.ShowOnList && pane.ButtonRect.Contains(loc))
  200. {
  201. return pane;
  202. }
  203. }
  204. return null;
  205. }
  206. private int _ShowPanes = 0;
  207. public int ShowPanes
  208. {
  209. get
  210. {
  211. return _ShowPanes;
  212. }
  213. set
  214. {
  215. _ShowPanes = value;
  216. if (_ShowPanes < 0)
  217. _ShowPanes = 0;
  218. if (_ShowPanes > this.ListedCount())
  219. {
  220. _ShowPanes = this.ListedCount();
  221. }
  222. this.PerformLayout();
  223. }
  224. }
  225. protected virtual void OnDiscoverPaneChanging(CancelEventArgs e)
  226. {
  227. if (DiscoverPaneChanging != null)
  228. DiscoverPaneChanging(this, e);
  229. }
  230. protected virtual void OnDiscoverPaneChanged(EventArgs e)
  231. {
  232. if (DiscoverPaneChanged != null)
  233. DiscoverPaneChanged(this, e);
  234. }
  235. protected override void OnLayout(LayoutEventArgs levent)
  236. {
  237. base.OnLayout(levent);
  238. if (this.IsHandleCreated)
  239. {
  240. int h = this.GripperRect.Top;
  241. h -= this.HeaderRect.Bottom;
  242. Rectangle rect = new Rectangle(1, this.HeaderRect.Bottom,
  243. this.ClientSize.Width - 2, h);
  244. int i = 0;
  245. int count = 0;
  246. foreach (Control current in base.Controls)
  247. {
  248. current.ClientSize = rect.Size;
  249. current.Location = rect.Location;
  250. DiscoverPane pane = (DiscoverPane)current;
  251. if (count < this.ShowPanes && this.ShowPanes > 0)
  252. {
  253. pane.ShowOnList = true;
  254. }
  255. else
  256. {
  257. pane.ShowOnList = false;
  258. }
  259. if (pane.ShowOnList)
  260. {
  261. int top = this.ListButtonHeight * (i + 1);
  262. top = this.SwitchBarRect.Top - top;
  263. Rectangle rc = new Rectangle(1, top,
  264. this.ClientSize.Width - 2, this.ListButtonHeight);
  265. pane.ListButtonRect = rc;
  266. i++;
  267. }
  268. count++;
  269. }
  270. this.Invalidate();
  271. }
  272. }
  273. private Point old_location;
  274. private bool grip_is_ondrag = false;
  275. private DiscoverPane _MouseOverPane = null;
  276. protected override void OnMouseDown(MouseEventArgs e)
  277. {
  278. base.OnMouseDown(e);
  279. if (this.GripperRect.Contains(e.Location))
  280. {
  281. grip_is_ondrag = true;
  282. old_location = e.Location;
  283. }
  284. }
  285. private void CalculateGripPosition()
  286. {
  287. }
  288. protected override void OnMouseMove(MouseEventArgs e)
  289. {
  290. base.OnMouseMove(e);
  291. if (grip_is_ondrag)
  292. {
  293. if (this.ShowPanes < 0)
  294. {
  295. this.ShowPanes = 0;
  296. return;
  297. }
  298. int m = this.GripperRect.Bottom - this.GripperRect.Top;
  299. m = m / 2;
  300. Rectangle rcUp = new Rectangle(1, this.GripperRect.Top - this.ListButtonHeight,
  301. this.ClientSize.Width - 2, m + this.ListButtonHeight);
  302. Rectangle rcDown = new Rectangle(1, this.GripperRect.Bottom + this.ListButtonHeight,
  303. this.ClientSize.Width - 2, m + this.ListButtonHeight);
  304. if (rcUp.Contains(e.Location)
  305. && (this.ShowPanes < ListedCount()))
  306. {
  307. this.ShowPanes++;
  308. }
  309. else if (rcDown.Contains(e.Location))
  310. {
  311. if (this.ShowPanes > 0)
  312. {
  313. this.ShowPanes--;
  314. }
  315. }
  316. old_location = new Point(e.Location.X, this.GripperRect.Top + m);
  317. this.PerformLayout();
  318. return;
  319. }
  320. if (this.GripperRect.Contains(e.Location))
  321. {
  322. this.Cursor = Cursors.SizeNS;
  323. return;
  324. }
  325. DiscoverPane listedPane = this.GetListedPaneAt(e.Location);
  326. if (listedPane != null)
  327. {
  328. if (listedPane.ListButtonRect.Contains(e.Location))
  329. {
  330. this.Cursor = Cursors.Hand;
  331. }
  332. else
  333. {
  334. this.Cursor = Cursors.Default;
  335. }
  336. }
  337. else if (this.ChewronRect.Contains(e.Location))
  338. {
  339. this.Cursor = Cursors.Hand;
  340. }
  341. else
  342. {
  343. this.Cursor = Cursors.Default;
  344. DiscoverPane buttonpane = this.GetButtonPaneAt(e.Location);
  345. if (buttonpane != null)
  346. {
  347. this.Cursor = Cursors.Hand;
  348. }
  349. _MouseOverPane = buttonpane;
  350. this.Invalidate(this.SwitchBarRect);
  351. }
  352. }
  353. private int ListedCount()
  354. {
  355. int count = 0;
  356. foreach (DiscoverPane pane in this.Controls)
  357. {
  358. if (pane.Listed)
  359. {
  360. count++;
  361. }
  362. }
  363. return count;
  364. }
  365. protected override void OnMouseUp(MouseEventArgs e)
  366. {
  367. base.OnMouseUp(e);
  368. if (grip_is_ondrag == true)
  369. this.Cursor = Cursors.Default;
  370. grip_is_ondrag = false;
  371. DiscoverPane listedPane = this.GetListedPaneAt(e.Location);
  372. if (listedPane != null)
  373. {
  374. CancelEventArgs evt = new CancelEventArgs();
  375. evt.Cancel = false;
  376. OnDiscoverPaneChanging(evt);
  377. if (!evt.Cancel)
  378. {
  379. this.SelectedPane = listedPane;
  380. OnDiscoverPaneChanged(new EventArgs());
  381. }
  382. return;
  383. }
  384. DiscoverPane buttonPane = this.GetButtonPaneAt(e.Location);
  385. if (buttonPane != null && buttonPane.Listed)
  386. {
  387. CancelEventArgs evt = new CancelEventArgs();
  388. evt.Cancel = false;
  389. OnDiscoverPaneChanging(evt);
  390. if (!evt.Cancel)
  391. {
  392. this.SelectedPane = buttonPane;
  393. OnDiscoverPaneChanged(new EventArgs());
  394. }
  395. }
  396. if (this.ChewronRect.Contains(e.Location))
  397. {
  398. _ContextMenu.Show(this, e.Location);
  399. }
  400. this.PerformLayout();
  401. }
  402. public ToolStripRenderer MenuItemsRenderer
  403. {
  404. get { return _ContextMenu.Renderer; }
  405. set { _ContextMenu.Renderer = value; }
  406. }
  407. protected override void OnControlAdded(ControlEventArgs e)
  408. {
  409. e.Control.Visible = false;
  410. base.OnControlAdded(e);
  411. }
  412. protected override void OnControlRemoved(ControlEventArgs e)
  413. {
  414. if (e.Control != null)
  415. {
  416. DiscoverPane pane = (DiscoverPane)e.Control;
  417. if (pane.Listed)
  418. {
  419. if (this.ShowPanes > 0)
  420. this.ShowPanes--;
  421. }
  422. }
  423. base.OnControlRemoved(e);
  424. this.PerformLayout();
  425. this.Invalidate();
  426. }
  427. public DiscoverControl()
  428. {
  429. this.SetStyle(ControlStyles.AllPaintingInWmPaint
  430. | ControlStyles.OptimizedDoubleBuffer
  431. | ControlStyles.ResizeRedraw
  432. | ControlStyles.UserPaint
  433. , true);
  434. _CurrentRender = new DiscoverProfessionalRender();
  435. _AddRemoveButtonsMenuItem = new DiscoverMenuItem(this);
  436. _ConfigureButtonsMenuItem = new DiscoverMenuItem(this);
  437. _FewerButtonsMenuItem = new DiscoverMenuItem(this);
  438. _MoreButtonsMenuItem = new DiscoverMenuItem(this);
  439. _PaneOptionsMenuItem = new DiscoverMenuItem(this);
  440. _FewerButtonsMenuItem.Click += new EventHandler(_FewerButtonsMenuItem_Click);
  441. _MoreButtonsMenuItem.Click += new EventHandler(_MoreButtonsMenuItem_Click);
  442. _ConfigureButtonsMenuItem.Click += new EventHandler(_ConfigureButtonsMenuItem_Click);
  443. _ConfigureButtonsMenuItem.Visible = false;
  444. _FewerButtonsMenuItem.Image = Properties.Resources.show_fever;
  445. _MoreButtonsMenuItem.Image = Properties.Resources.show_more;
  446. _ContextMenu = new ContextMenuStrip();
  447. _ContextMenu.Items.AddRange(
  448. new ToolStripItem[] {
  449. _MoreButtonsMenuItem,
  450. _FewerButtonsMenuItem,
  451. _PaneOptionsMenuItem,
  452. _ConfigureButtonsMenuItem,
  453. _AddRemoveButtonsMenuItem
  454. }
  455. );
  456. _ContextMenu.Opening += new System.ComponentModel.CancelEventHandler(_ContextMenu_Opening);
  457. }
  458. void _ConfigureButtonsMenuItem_Click(object sender, EventArgs e)
  459. {
  460. this.OnDiscoverShowOptions(new EventArgs());
  461. }
  462. protected virtual void OnDiscoverShowOptions(EventArgs e)
  463. {
  464. if (DiscoverShowOptions != null)
  465. DiscoverShowOptions(this, new EventArgs());
  466. }
  467. void _MoreButtonsMenuItem_Click(object sender, EventArgs e)
  468. {
  469. this.ShowPanes++;
  470. }
  471. void _FewerButtonsMenuItem_Click(object sender, EventArgs e)
  472. {
  473. this.ShowPanes--;
  474. }
  475. void _ContextMenu_Opening(object sender, System.ComponentModel.CancelEventArgs e)
  476. {
  477. _AddRemoveButtonsMenuItem.Text = DiscoverLanguage.AddRemoveButtons;
  478. _ConfigureButtonsMenuItem.Text = DiscoverLanguage.ConfigureButtons;
  479. _FewerButtonsMenuItem.Text = DiscoverLanguage.FewerButtons;
  480. _MoreButtonsMenuItem.Text = DiscoverLanguage.MoreButtons;
  481. _PaneOptionsMenuItem.Text = DiscoverLanguage.PaneOptions;
  482. if (this.ShowPanes == 0)
  483. {
  484. _FewerButtonsMenuItem.Enabled = false;
  485. }
  486. else
  487. {
  488. _FewerButtonsMenuItem.Enabled = true;
  489. }
  490. if (this.Controls.Count > 0 && this.ShowPanes < this.Controls.Count)
  491. {
  492. _MoreButtonsMenuItem.Enabled = true;
  493. }
  494. else
  495. {
  496. _MoreButtonsMenuItem.Enabled = false;
  497. }
  498. _AddRemoveButtonsMenuItem.DropDownItems.Clear();
  499. foreach (DiscoverPane pane in this.Controls)
  500. {
  501. ToolStripMenuItem menuPane = new ToolStripMenuItem(pane.Text,
  502. pane.SmallImage);
  503. menuPane.Tag = pane;
  504. menuPane.Checked = pane.Listed;
  505. menuPane.CheckOnClick = true;
  506. menuPane.CheckedChanged += new EventHandler(addRemoveButtonCheckChanged);
  507. _AddRemoveButtonsMenuItem.DropDownItems.Add(menuPane);
  508. }
  509. }
  510. private void addRemoveButtonCheckChanged(object sender, EventArgs e)
  511. {
  512. ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
  513. DiscoverPane pane = (DiscoverPane)menuItem.Tag;
  514. if (this.ShowPanes > 0 && pane.ShowOnList)
  515. this.ShowPanes--;
  516. pane.Listed = menuItem.Checked;
  517. this.PerformLayout();
  518. }
  519. private IDiscoverRenderer _CurrentRender = null;
  520. private DiscoverPane _SelectedPane = null;
  521. private Font _HeaderFont = new Font("Tahoma", 12f, FontStyle.Bold, GraphicsUnit.Point);
  522. private bool _boo = false;
  523. private bool _DrawChewron = true;
  524. private bool _DrawGripper = true;
  525. private Rectangle _GripperRect;
  526. private DiscoverMenuItem _AddRemoveButtonsMenuItem = null;
  527. private DiscoverMenuItem _ConfigureButtonsMenuItem = null;
  528. private DiscoverMenuItem _FewerButtonsMenuItem = null;
  529. private DiscoverMenuItem _MoreButtonsMenuItem = null;
  530. private DiscoverMenuItem _PaneOptionsMenuItem = null;
  531. private ContextMenuStrip _ContextMenu = null;
  532. internal class DiscoverMenuItem : ToolStripMenuItem
  533. {
  534. private DiscoverControl _DiscoverControl = null;
  535. public DiscoverMenuItem(DiscoverControl parentDiscover)
  536. : this(parentDiscover, string.Empty)
  537. {
  538. }
  539. public DiscoverMenuItem(DiscoverControl parentDiscover, string text)
  540. {
  541. this._DiscoverControl = parentDiscover;
  542. this.Text = text;
  543. }
  544. public DiscoverControl DiscoverControl
  545. {
  546. get { return _DiscoverControl; }
  547. }
  548. }
  549. }
  550. }