HeaderContextMenu.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.ComponentModel;
  32. using System.Drawing;
  33. using System.Windows.Forms;
  34. using XPTable;
  35. using XPTable.Events;
  36. using XPTable.Win32;
  37. namespace XPTable.Models
  38. {
  39. /// <summary>
  40. /// A specialized ContextMenu for Column Headers
  41. /// </summary>
  42. [ToolboxItem(false)]
  43. public class HeaderContextMenu : ContextMenu
  44. {
  45. #region Class Data
  46. /// <summary>
  47. /// The ColumnModel that owns the menu
  48. /// </summary>
  49. private ColumnModel model;
  50. /// <summary>
  51. /// Specifies whether the menu is enabled
  52. /// </summary>
  53. private bool enabled;
  54. /// <summary>
  55. /// More columns menuitem
  56. /// </summary>
  57. private MenuItem moreMenuItem;
  58. /// <summary>
  59. /// Seperator menuitem
  60. /// </summary>
  61. private MenuItem separator;
  62. #endregion
  63. #region Constructor
  64. /// <summary>
  65. /// Initializes a new instance of the HeaderContextMenu class with
  66. /// no menu items specified
  67. /// </summary>
  68. public HeaderContextMenu() : base()
  69. {
  70. this.model = null;
  71. this.enabled = true;
  72. this.moreMenuItem = new MenuItem("More...", new EventHandler(moreMenuItem_Click));
  73. this.separator = new MenuItem("-");
  74. }
  75. #endregion
  76. #region Methods
  77. /// <summary>
  78. /// Displays the shortcut menu at the specified position
  79. /// </summary>
  80. /// <param name="control">A Control object that specifies the control
  81. /// with which this shortcut menu is associated</param>
  82. /// <param name="pos">A Point object that specifies the coordinates at
  83. /// which to display the menu. These coordinates are specified relative
  84. /// to the client coordinates of the control specified in the control
  85. /// parameter</param>
  86. public new void Show(Control control, Point pos)
  87. {
  88. if (control == null)
  89. {
  90. throw new ArgumentNullException("control", "control cannot be null");
  91. }
  92. if (!(control is Table))
  93. {
  94. throw new ArgumentException("control must be of type Table", "control");
  95. }
  96. if (((Table) control).ColumnModel == null)
  97. {
  98. throw new InvalidOperationException("The specified Table does not have an associated ColumnModel");
  99. }
  100. //
  101. this.model = ((Table) control).ColumnModel;
  102. //
  103. this.MenuItems.Clear();
  104. base.Show(control, pos);
  105. }
  106. /// <summary>
  107. ///
  108. /// </summary>
  109. internal bool Enabled
  110. {
  111. get
  112. {
  113. return this.enabled;
  114. }
  115. set
  116. {
  117. this.enabled = value;
  118. }
  119. }
  120. #endregion
  121. #region Events
  122. /// <summary>
  123. /// Raises the Popup event
  124. /// </summary>
  125. /// <param name="e">An EventArgs that contains the event data</param>
  126. protected override void OnPopup(EventArgs e)
  127. {
  128. if (this.model.Columns.Count > 0)
  129. {
  130. MenuItem item;
  131. for (int i=0; i<this.model.Columns.Count; i++)
  132. {
  133. if (i == 10)
  134. {
  135. this.MenuItems.Add(this.separator);
  136. this.MenuItems.Add(this.moreMenuItem);
  137. break;
  138. }
  139. item = new MenuItem(this.model.Columns[i].Text, new EventHandler(menuItem_Click));
  140. item.Checked = this.model.Columns[i].Visible;
  141. this.MenuItems.Add(item);
  142. }
  143. }
  144. base.OnPopup(e);
  145. }
  146. /// <summary>
  147. ///
  148. /// </summary>
  149. /// <param name="sender"></param>
  150. /// <param name="e"></param>
  151. private void menuItem_Click(object sender, EventArgs e)
  152. {
  153. MenuItem item = (MenuItem) sender;
  154. this.model.Columns[item.Index].Visible = !item.Checked;
  155. }
  156. /// <summary>
  157. ///
  158. /// </summary>
  159. /// <param name="sender"></param>
  160. /// <param name="e"></param>
  161. private void moreMenuItem_Click(object sender, EventArgs e)
  162. {
  163. ShowColumnsDialog scd = new ShowColumnsDialog();
  164. scd.AddColumns(this.model);
  165. scd.ShowDialog(this.SourceControl);
  166. }
  167. #endregion
  168. #region ShowColumnsDialog
  169. /// <summary>
  170. /// Summary description for ShowColumnsDialog.
  171. /// </summary>
  172. internal class ShowColumnsDialog : Form
  173. {
  174. #region Class Data
  175. /// <summary>
  176. /// Required designer variable.
  177. /// </summary>
  178. private System.ComponentModel.Container components = null;
  179. private ColumnModel model = null;
  180. private Label label1;
  181. private Button upButton;
  182. private Button downButton;
  183. private Button showButton;
  184. private Button hideButton;
  185. private Label label2;
  186. private TextBox widthTextBox;
  187. private CheckBox autoSizeCheckBox;
  188. private GroupBox groupBox1;
  189. private Button okButton;
  190. private Button cancelButton;
  191. private Table columnTable;
  192. #endregion
  193. #region Constructor
  194. /// <summary>
  195. ///
  196. /// </summary>
  197. public ShowColumnsDialog()
  198. {
  199. this.label1 = new Label();
  200. this.columnTable = new Table();
  201. this.upButton = new Button();
  202. this.downButton = new Button();
  203. this.showButton = new Button();
  204. this.hideButton = new Button();
  205. this.label2 = new Label();
  206. this.widthTextBox = new TextBox();
  207. this.autoSizeCheckBox = new CheckBox();
  208. this.groupBox1 = new GroupBox();
  209. this.okButton = new Button();
  210. this.cancelButton = new Button();
  211. this.SuspendLayout();
  212. //
  213. // label1
  214. //
  215. this.label1.Location = new Point(8, 12);
  216. this.label1.Name = "label1";
  217. this.label1.Size = new Size(324, 28);
  218. this.label1.TabIndex = 0;
  219. //this.label1.Text = "Select the columns you want to appear in this view. Click Move Up and Move Down " +
  220. // "to arrange the columns.";
  221. this.label1.Text = "Select the columns you want to appear in this view.";
  222. //
  223. // columnListBox
  224. //
  225. this.columnTable.HeaderStyle = ColumnHeaderStyle.None;
  226. this.columnTable.Location = new Point(12, 52);
  227. this.columnTable.Name = "columnListBox";
  228. this.columnTable.Size = new Size(231, 240);
  229. this.columnTable.TabIndex = 1;
  230. this.columnTable.ColumnModel = new ColumnModel();
  231. this.columnTable.ColumnModel.Columns.Add(new CheckBoxColumn("Columns", 227));
  232. this.columnTable.TableModel = new TableModel();
  233. this.columnTable.TableModel.RowHeight += 3;
  234. //
  235. // upButton
  236. //
  237. this.upButton.FlatStyle = FlatStyle.System;
  238. this.upButton.Location = new Point(253, 52);
  239. this.upButton.Name = "upButton";
  240. this.upButton.TabIndex = 2;
  241. this.upButton.Text = "Move &Up";
  242. this.upButton.Visible = false;
  243. //this.upButton.Click += new EventHandler(this.upButton_Click);
  244. //
  245. // downButton
  246. //
  247. this.downButton.FlatStyle = FlatStyle.System;
  248. this.downButton.Location = new Point(253, 81);
  249. this.downButton.Name = "downButton";
  250. this.downButton.TabIndex = 3;
  251. this.downButton.Text = "Move &Down";
  252. this.downButton.Visible = false;
  253. //this.downButton.Click += new EventHandler(this.downButton_Click);
  254. //
  255. // showButton
  256. //
  257. this.showButton.FlatStyle = FlatStyle.System;
  258. //this.showButton.Location = new Point(253, 114);
  259. this.showButton.Location = new Point(253, 52);
  260. this.showButton.Name = "showButton";
  261. this.showButton.TabIndex = 4;
  262. this.showButton.Text = "&Show";
  263. this.showButton.Click += new EventHandler(this.showButton_Click);
  264. //
  265. // hideButton
  266. //
  267. this.hideButton.FlatStyle = FlatStyle.System;
  268. //this.hideButton.Location = new Point(253, 145);
  269. this.hideButton.Location = new Point(253, 81);
  270. this.hideButton.Name = "hideButton";
  271. this.hideButton.TabIndex = 5;
  272. this.hideButton.Text = "&Hide";
  273. this.hideButton.Click += new EventHandler(this.hideButton_Click);
  274. //
  275. // label2
  276. //
  277. this.label2.Location = new Point(12, 300);
  278. this.label2.Name = "label2";
  279. this.label2.Size = new Size(192, 21);
  280. this.label2.TabIndex = 6;
  281. this.label2.Text = "&Width of selected column (in pixels):";
  282. this.label2.TextAlign = ContentAlignment.MiddleLeft;
  283. //
  284. // textBox1
  285. //
  286. this.widthTextBox.Location = new Point(207, 300);
  287. this.widthTextBox.MaxLength = 4;
  288. this.widthTextBox.Name = "textBox1";
  289. this.widthTextBox.Size = new Size(36, 21);
  290. this.widthTextBox.TabIndex = 7;
  291. this.widthTextBox.Text = "0";
  292. this.widthTextBox.TextAlign = HorizontalAlignment.Right;
  293. this.widthTextBox.KeyPress += new KeyPressEventHandler(widthTextBox_KeyPress);
  294. //
  295. // autoSizeCheckBox
  296. //
  297. this.autoSizeCheckBox.Location = new Point(12, 330);
  298. this.autoSizeCheckBox.Name = "autoSizeCheckBox";
  299. this.autoSizeCheckBox.Size = new Size(228, 16);
  300. this.autoSizeCheckBox.TabIndex = 8;
  301. this.autoSizeCheckBox.Text = "&Automatically size all columns";
  302. this.autoSizeCheckBox.Visible = false;
  303. //
  304. // groupBox1
  305. //
  306. this.groupBox1.Location = new Point(8, 352);
  307. this.groupBox1.Name = "groupBox1";
  308. this.groupBox1.Size = new Size(322, 8);
  309. this.groupBox1.TabIndex = 9;
  310. this.groupBox1.TabStop = false;
  311. //
  312. // okButton
  313. //
  314. this.okButton.FlatStyle = FlatStyle.System;
  315. this.okButton.Location = new Point(168, 372);
  316. this.okButton.Name = "okButton";
  317. this.okButton.TabIndex = 10;
  318. this.okButton.Text = "OK";
  319. this.okButton.Click += new EventHandler(okButton_Click);
  320. //
  321. // cancelButton
  322. //
  323. this.cancelButton.DialogResult = DialogResult.Cancel;
  324. this.cancelButton.FlatStyle = FlatStyle.System;
  325. this.cancelButton.Location = new Point(253, 372);
  326. this.cancelButton.Name = "cancelButton";
  327. this.cancelButton.TabIndex = 11;
  328. this.cancelButton.Text = "Cancel";
  329. //
  330. // ShowColumnsDialog
  331. //
  332. this.AcceptButton = this.okButton;
  333. this.AutoScaleBaseSize = new Size(5, 14);
  334. this.CancelButton = this.cancelButton;
  335. this.ClientSize = new Size(339, 408);
  336. this.Controls.Add(this.cancelButton);
  337. this.Controls.Add(this.okButton);
  338. this.Controls.Add(this.groupBox1);
  339. this.Controls.Add(this.autoSizeCheckBox);
  340. this.Controls.Add(this.widthTextBox);
  341. this.Controls.Add(this.label2);
  342. this.Controls.Add(this.hideButton);
  343. this.Controls.Add(this.showButton);
  344. this.Controls.Add(this.downButton);
  345. this.Controls.Add(this.upButton);
  346. this.Controls.Add(this.columnTable);
  347. this.Controls.Add(this.label1);
  348. this.Font = new Font("Tahoma", 8.25F);
  349. this.FormBorderStyle = FormBorderStyle.FixedDialog;
  350. this.MaximizeBox = false;
  351. this.MinimizeBox = false;
  352. this.Name = "ShowColumnsDialog";
  353. this.ShowInTaskbar = false;
  354. this.StartPosition = FormStartPosition.CenterParent;
  355. this.Text = "Choose Columns";
  356. this.ResumeLayout(false);
  357. }
  358. #endregion
  359. #region Methods
  360. /// <summary>
  361. /// Clean up any resources being used.
  362. /// </summary>
  363. protected override void Dispose(bool disposing)
  364. {
  365. if (disposing)
  366. {
  367. if (components != null)
  368. {
  369. components.Dispose();
  370. }
  371. }
  372. base.Dispose(disposing);
  373. }
  374. /// <summary>
  375. ///
  376. /// </summary>
  377. /// <param name="model"></param>
  378. /// <returns></returns>
  379. public void AddColumns(ColumnModel model)
  380. {
  381. this.model = model;
  382. CellStyle cellStyle = new CellStyle();
  383. cellStyle.Padding = new CellPadding(6, 0, 0, 0);
  384. this.columnTable.BeginUpdate();
  385. for (int i=0; i<model.Columns.Count; i++)
  386. {
  387. Row row = new Row();
  388. Cell cell = new Cell(model.Columns[i].Text, model.Columns[i].Visible);
  389. cell.Tag = model.Columns[i].Width;
  390. cell.CellStyle = cellStyle;
  391. row.Cells.Add(cell);
  392. this.columnTable.TableModel.Rows.Add(row);
  393. }
  394. this.columnTable.SelectionChanged += new XPTable.Events.SelectionEventHandler(columnTable_SelectionChanged);
  395. this.columnTable.CellCheckChanged += new XPTable.Events.CellCheckBoxEventHandler(columnTable_CellCheckChanged);
  396. if (this.columnTable.VScroll)
  397. {
  398. this.columnTable.ColumnModel.Columns[0].Width -= SystemInformation.VerticalScrollBarWidth;
  399. }
  400. if (this.columnTable.TableModel.Rows.Count > 0)
  401. {
  402. this.columnTable.TableModel.Selections.SelectCell(0, 0);
  403. this.showButton.Enabled = !this.model.Columns[0].Visible;
  404. this.hideButton.Enabled = this.model.Columns[0].Visible;
  405. this.widthTextBox.Text = this.model.Columns[0].Width.ToString();
  406. }
  407. this.columnTable.EndUpdate();
  408. }
  409. #endregion
  410. #region Events
  411. /*/// <summary>
  412. ///
  413. /// </summary>
  414. /// <param name="sender"></param>
  415. /// <param name="e"></param>
  416. private void upButton_Click(object sender, System.EventArgs e)
  417. {
  418. }
  419. /// <summary>
  420. ///
  421. /// </summary>
  422. /// <param name="sender"></param>
  423. /// <param name="e"></param>
  424. private void downButton_Click(object sender, System.EventArgs e)
  425. {
  426. }*/
  427. /// <summary>
  428. ///
  429. /// </summary>
  430. /// <param name="sender"></param>
  431. /// <param name="e"></param>
  432. private void showButton_Click(object sender, System.EventArgs e)
  433. {
  434. int[] indicies = this.columnTable.SelectedIndicies;
  435. if (indicies.Length > 0)
  436. {
  437. this.columnTable.TableModel[indicies[0], 0].Checked = true;
  438. this.hideButton.Focus();
  439. }
  440. }
  441. /// <summary>
  442. ///
  443. /// </summary>
  444. /// <param name="sender"></param>
  445. /// <param name="e"></param>
  446. private void hideButton_Click(object sender, System.EventArgs e)
  447. {
  448. int[] indicies = this.columnTable.SelectedIndicies;
  449. if (indicies.Length > 0)
  450. {
  451. this.columnTable.TableModel[indicies[0], 0].Checked = false;
  452. this.showButton.Focus();
  453. }
  454. }
  455. /// <summary>
  456. ///
  457. /// </summary>
  458. /// <param name="sender"></param>
  459. /// <param name="e"></param>
  460. private void okButton_Click(object sender, EventArgs e)
  461. {
  462. int[] indicies = this.columnTable.SelectedIndicies;
  463. if (indicies.Length > 0)
  464. {
  465. if (this.widthTextBox.Text.Length == 0)
  466. {
  467. this.columnTable.TableModel[indicies[0], 0].Tag = Column.MinimumWidth;
  468. }
  469. else
  470. {
  471. int width = Convert.ToInt32(this.widthTextBox.Text);
  472. if (width < Column.MinimumWidth)
  473. {
  474. this.columnTable.TableModel[indicies[0], 0].Tag = Column.MinimumWidth;
  475. }
  476. else
  477. {
  478. this.columnTable.TableModel[indicies[0], 0].Tag = width;
  479. }
  480. }
  481. }
  482. for (int i=0; i<this.columnTable.TableModel.Rows.Count; i++)
  483. {
  484. this.model.Columns[i].Visible = this.columnTable.TableModel[i, 0].Checked;
  485. this.model.Columns[i].Width = (int) this.columnTable.TableModel[i, 0].Tag;
  486. }
  487. this.Close();
  488. }
  489. /// <summary>
  490. ///
  491. /// </summary>
  492. /// <param name="sender"></param>
  493. /// <param name="e"></param>
  494. private void columnTable_SelectionChanged(object sender, SelectionEventArgs e)
  495. {
  496. if (e.OldSelectedIndicies.Length > 0)
  497. {
  498. if (this.widthTextBox.Text.Length == 0)
  499. {
  500. this.columnTable.TableModel[e.OldSelectedIndicies[0], 0].Tag = Column.MinimumWidth;
  501. }
  502. else
  503. {
  504. int width = Convert.ToInt32(this.widthTextBox.Text);
  505. if (width < Column.MinimumWidth)
  506. {
  507. this.columnTable.TableModel[e.OldSelectedIndicies[0], 0].Tag = Column.MinimumWidth;
  508. }
  509. else
  510. {
  511. this.columnTable.TableModel[e.OldSelectedIndicies[0], 0].Tag = width;
  512. }
  513. }
  514. }
  515. if (e.NewSelectedIndicies.Length > 0)
  516. {
  517. this.showButton.Enabled = !this.columnTable.TableModel[e.NewSelectedIndicies[0], 0].Checked;
  518. this.hideButton.Enabled = this.columnTable.TableModel[e.NewSelectedIndicies[0], 0].Checked;
  519. this.widthTextBox.Text = this.columnTable.TableModel[e.NewSelectedIndicies[0], 0].Tag.ToString();
  520. }
  521. else
  522. {
  523. this.showButton.Enabled = false;
  524. this.hideButton.Enabled = false;
  525. this.widthTextBox.Text = "0";
  526. }
  527. }
  528. /// <summary>
  529. ///
  530. /// </summary>
  531. /// <param name="sender"></param>
  532. /// <param name="e"></param>
  533. private void columnTable_CellCheckChanged(object sender, CellCheckBoxEventArgs e)
  534. {
  535. this.showButton.Enabled = !e.Cell.Checked;
  536. this.hideButton.Enabled = e.Cell.Checked;
  537. }
  538. /// <summary>
  539. ///
  540. /// </summary>
  541. /// <param name="sender"></param>
  542. /// <param name="e"></param>
  543. private void widthTextBox_KeyPress(object sender, KeyPressEventArgs e)
  544. {
  545. if (!char.IsDigit(e.KeyChar) && e.KeyChar != AsciiChars.Backspace && e.KeyChar != AsciiChars.Delete)
  546. {
  547. if ((Control.ModifierKeys & (Keys.Alt | Keys.Control)) == Keys.None)
  548. {
  549. e.Handled = true;
  550. NativeMethods.MessageBeep(0 /*MB_OK*/);
  551. }
  552. }
  553. }
  554. #endregion
  555. }
  556. #endregion
  557. }
  558. }