ComboBox.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. //
  26. // NOT COMPLETE
  27. using System;
  28. using System.Drawing;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Reflection;
  32. namespace System.Windows.Forms
  33. {
  34. public class ComboBox : ListControl
  35. {
  36. private DrawMode draw_mode;
  37. private ComboBoxStyle dropdown_style;
  38. private int dropdown_width;
  39. private int max_length;
  40. private int preferred_height;
  41. private int selected_index;
  42. private object selected_item;
  43. internal ObjectCollection items;
  44. private bool suspend_ctrlupdate;
  45. private int maxdrop_items;
  46. private bool integral_height;
  47. private bool sorted;
  48. internal ComboBoxInfo combobox_info;
  49. private readonly int def_button_width = 16;
  50. private bool clicked;
  51. private ListBoxPopUp listbox_popup;
  52. private StringFormat string_format;
  53. internal class ComboBoxInfo
  54. {
  55. internal int item_height; /* Item's height */
  56. internal Rectangle textarea_rect; /* Rectangle of the editable text area - decorations */
  57. internal Rectangle button_rect;
  58. internal bool show_button; /* Is the DropDown button shown? */
  59. internal Rectangle client_rect; /* Client Rectangle. Usually = ClientRectangle except when IntegralHeight has been applied*/
  60. internal ButtonState button_status; /* Drop button status */
  61. internal Size listbox_size;
  62. public ComboBoxInfo ()
  63. {
  64. button_status = ButtonState.Normal;
  65. show_button = false;
  66. item_height = 0;
  67. }
  68. }
  69. internal class ComboBoxItem
  70. {
  71. internal int Index;
  72. public ComboBoxItem (int index)
  73. {
  74. Index = index;
  75. }
  76. }
  77. public ComboBox ()
  78. {
  79. BackColor = ThemeEngine.Current.ColorWindow;
  80. draw_mode = DrawMode.Normal;
  81. selected_index = -1;
  82. selected_item = null;
  83. maxdrop_items = 8;
  84. combobox_info = new ComboBoxInfo ();
  85. combobox_info.item_height = FontHeight;
  86. suspend_ctrlupdate = false;
  87. clicked = false;
  88. items = new ObjectCollection (this);
  89. string_format = new StringFormat ();
  90. CBoxInfo.show_button = true;
  91. listbox_popup = null;
  92. /* Events */
  93. MouseDown += new MouseEventHandler (OnMouseDownCB);
  94. MouseUp += new MouseEventHandler (OnMouseUpCB);
  95. Console.WriteLine ("ComboBox is still an on going effort. Only ComboBoxStyle.DropDownList is implemented");
  96. }
  97. #region Events
  98. public new event EventHandler BackgroundImageChanged;
  99. public event DrawItemEventHandler DrawItem;
  100. public event EventHandler DropDown;
  101. public event EventHandler DropDownStyleChanged;
  102. public event MeasureItemEventHandler MeasureItem;
  103. public new event PaintEventHandler Paint;
  104. public event EventHandler SelectedIndexChanged;
  105. public event EventHandler SelectionChangeCommitted;
  106. #endregion Events
  107. #region Public Properties
  108. public override Color BackColor {
  109. get { return base.BackColor; }
  110. set {
  111. if (base.BackColor == value)
  112. return;
  113. base.BackColor = value;
  114. Refresh ();
  115. }
  116. }
  117. public override Image BackgroundImage {
  118. get { return base.BackgroundImage; }
  119. set {
  120. if (base.BackgroundImage == value)
  121. return;
  122. base.BackgroundImage = value;
  123. if (BackgroundImageChanged != null)
  124. BackgroundImageChanged (this, EventArgs.Empty);
  125. Refresh ();
  126. }
  127. }
  128. protected override CreateParams CreateParams {
  129. get { return base.CreateParams;}
  130. }
  131. protected override Size DefaultSize {
  132. get { return new Size (121, PreferredHeight); }
  133. }
  134. public DrawMode DrawMode {
  135. get { return draw_mode; }
  136. set {
  137. if (!Enum.IsDefined (typeof (DrawMode), value))
  138. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
  139. if (draw_mode == value)
  140. return;
  141. draw_mode = value;
  142. Refresh ();
  143. }
  144. }
  145. public ComboBoxStyle DropDownStyle {
  146. get { return dropdown_style; }
  147. set {
  148. if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
  149. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
  150. if (dropdown_style == value)
  151. return;
  152. dropdown_style = value;
  153. Refresh ();
  154. }
  155. }
  156. public int DropDownWidth {
  157. get { return dropdown_width; }
  158. set {
  159. if (dropdown_width == value)
  160. return;
  161. dropdown_width = value;
  162. Refresh ();
  163. }
  164. }
  165. public override bool Focused {
  166. get { return base.Focused; }
  167. }
  168. public override Color ForeColor {
  169. get { return base.ForeColor; }
  170. set {
  171. if (base.ForeColor == value)
  172. return;
  173. base.ForeColor = value;
  174. Refresh ();
  175. }
  176. }
  177. public bool IntegralHeight {
  178. get { return integral_height; }
  179. set {
  180. if (integral_height == value)
  181. return;
  182. integral_height = value;
  183. }
  184. }
  185. public virtual int ItemHeight {
  186. get { return combobox_info.item_height; }
  187. set {
  188. if (value < 0)
  189. throw new ArgumentOutOfRangeException ("The item height value is less than zero");
  190. combobox_info.item_height = value;
  191. Refresh ();
  192. }
  193. }
  194. public ComboBox.ObjectCollection Items {
  195. get { return items; }
  196. }
  197. public int MaxDropDownItems {
  198. get { return maxdrop_items; }
  199. set {
  200. if (maxdrop_items == value)
  201. return;
  202. maxdrop_items = value;
  203. }
  204. }
  205. public int MaxLength {
  206. get { return max_length; }
  207. set {
  208. if (max_length == value)
  209. return;
  210. max_length = value;
  211. }
  212. }
  213. public int PreferredHeight {
  214. get { return preferred_height; }
  215. }
  216. public override int SelectedIndex {
  217. get { return selected_index; }
  218. set {
  219. if (value < -2 || value >= Items.Count)
  220. throw new ArgumentOutOfRangeException ("Index of out range");
  221. if (selected_index == value)
  222. return;
  223. selected_index = value;
  224. OnSelectedIndexChanged (new EventArgs ());
  225. Refresh ();
  226. }
  227. }
  228. public object SelectedItem {
  229. get {
  230. if (selected_index !=-1 && Items.Count > 0)
  231. return Items[selected_index];
  232. else
  233. return null;
  234. }
  235. set {
  236. if (selected_item == value)
  237. return;
  238. int index = Items.IndexOf (value);
  239. if (index == -1)
  240. return;
  241. selected_index = index;
  242. OnSelectedItemChanged (new EventArgs ());
  243. Refresh ();
  244. }
  245. }
  246. public string SelectedText {
  247. get {throw new NotImplementedException ();}
  248. set {}
  249. }
  250. public int SelectionLength {
  251. get {throw new NotImplementedException ();}
  252. set {}
  253. }
  254. public int SelectionStart {
  255. get {throw new NotImplementedException (); }
  256. set {}
  257. }
  258. public bool Sorted {
  259. get { return sorted; }
  260. set {
  261. if (sorted == value)
  262. return;
  263. sorted = value;
  264. }
  265. }
  266. public override string Text {
  267. get { return ""; /*throw new NotImplementedException ();*/ }
  268. set {}
  269. }
  270. #endregion Public Properties
  271. #region Private Properties
  272. internal ComboBoxInfo CBoxInfo {
  273. get { return combobox_info; }
  274. }
  275. #endregion Private Properties
  276. #region Public Methods
  277. protected virtual void AddItemsCore (object[] value)
  278. {
  279. }
  280. public void BeginUpdate ()
  281. {
  282. suspend_ctrlupdate = true;
  283. }
  284. protected virtual void Dispose (bool disposing)
  285. {
  286. }
  287. public void EndUpdate ()
  288. {
  289. suspend_ctrlupdate = false;
  290. Refresh ();
  291. }
  292. public int FindString (string s)
  293. {
  294. return FindString (s, 0);
  295. }
  296. public int FindString (string s, int startIndex)
  297. {
  298. for (int i = startIndex; i < Items.Count; i++) {
  299. if ((Items[i].ToString ()).StartsWith (s))
  300. return i;
  301. }
  302. return -1;
  303. }
  304. public int FindStringExact (string s)
  305. {
  306. return FindStringExact (s, 0);
  307. }
  308. public int FindStringExact (string s, int startIndex)
  309. {
  310. for (int i = startIndex; i < Items.Count; i++) {
  311. if ((Items[i].ToString ()).Equals (s))
  312. return i;
  313. }
  314. return -1;
  315. }
  316. public int GetItemHeight (int index)
  317. {
  318. throw new NotImplementedException ();
  319. }
  320. protected override bool IsInputKey (Keys keyData)
  321. {
  322. return false;
  323. }
  324. protected override void OnBackColorChanged (EventArgs e)
  325. {
  326. base.OnBackColorChanged (e);
  327. }
  328. protected override void OnDataSourceChanged (EventArgs e)
  329. {
  330. base.OnDataSourceChanged (e);
  331. }
  332. protected override void OnDisplayMemberChanged (EventArgs e)
  333. {
  334. base.OnDisplayMemberChanged (e);
  335. }
  336. protected virtual void OnDrawItem (DrawItemEventArgs e)
  337. {
  338. if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable))
  339. DrawItem (this, e);
  340. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
  341. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  342. (ThemeEngine.Current.ColorHilight), e.Bounds);
  343. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  344. ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHilightText),
  345. e.Bounds, string_format);
  346. // It seems to be a bug in CPDrawFocusRectangle
  347. //ThemeEngine.Current.CPDrawFocusRectangle (e.Graphics, e.Bounds,
  348. // ThemeEngine.Current.ColorHilightText, BackColor);
  349. }
  350. else {
  351. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  352. (e.BackColor), e.Bounds);
  353. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  354. ThemeEngine.Current.ResPool.GetSolidBrush (e.ForeColor),
  355. e.Bounds, string_format);
  356. }
  357. }
  358. protected virtual void OnDropDownStyleChanged (EventArgs e)
  359. {
  360. }
  361. protected override void OnFontChanged (EventArgs e)
  362. {
  363. base.OnFontChanged (e);
  364. }
  365. protected override void OnForeColorChanged (EventArgs e)
  366. {
  367. base.OnForeColorChanged (e);
  368. }
  369. protected override void OnHandleCreated (EventArgs e)
  370. {
  371. base.OnHandleCreated (e);
  372. CalcTextArea ();
  373. }
  374. protected override void OnHandleDestroyed (EventArgs e)
  375. {
  376. base.OnHandleDestroyed (e);
  377. }
  378. protected override void OnKeyPress (KeyPressEventArgs e)
  379. {
  380. }
  381. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  382. {
  383. }
  384. protected override void OnParentBackColorChanged (EventArgs e)
  385. {
  386. base.OnParentBackColorChanged (e);
  387. }
  388. protected override void OnResize (EventArgs e)
  389. {
  390. base.OnResize (e);
  391. CalcTextArea ();
  392. }
  393. protected override void OnSelectedIndexChanged (EventArgs e)
  394. {
  395. base.OnSelectedIndexChanged (e);
  396. if (SelectedIndexChanged != null)
  397. SelectedIndexChanged (this, e);
  398. }
  399. protected virtual void OnSelectedItemChanged (EventArgs e)
  400. {
  401. }
  402. protected override void OnSelectedValueChanged (EventArgs e)
  403. {
  404. base.OnSelectedValueChanged (e);
  405. }
  406. protected virtual void OnSelectionChangeCommitted (EventArgs e)
  407. {
  408. }
  409. protected override void RefreshItem (int index)
  410. {
  411. }
  412. protected virtual void Select (int start, int lenght)
  413. {
  414. }
  415. public void SelectAll ()
  416. {
  417. }
  418. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  419. {
  420. base.SetBoundsCore (x, y, width, height, specified);
  421. }
  422. protected override void SetItemCore (int index, object value)
  423. {
  424. if (index < 0 || index >= Items.Count)
  425. return;
  426. Items[index] = value;
  427. }
  428. protected override void SetItemsCore (IList value)
  429. {
  430. }
  431. public override string ToString ()
  432. {
  433. throw new NotImplementedException ();
  434. }
  435. protected override void WndProc (ref Message m)
  436. {
  437. switch ((Msg) m.Msg) {
  438. case Msg.WM_PAINT: {
  439. PaintEventArgs paint_event;
  440. paint_event = XplatUI.PaintEventStart (Handle);
  441. OnPaintCB (paint_event);
  442. XplatUI.PaintEventEnd (Handle);
  443. return;
  444. }
  445. case Msg.WM_ERASEBKGND:
  446. m.Result = (IntPtr) 1;
  447. return;
  448. default:
  449. break;
  450. }
  451. base.WndProc (ref m);
  452. }
  453. #endregion Public Methods
  454. #region Private Methods
  455. internal void ButtonReleased ()
  456. {
  457. combobox_info.button_status = ButtonState.Normal;
  458. Invalidate (combobox_info.button_rect);
  459. }
  460. // Calcs the text area size
  461. internal void CalcTextArea ()
  462. {
  463. combobox_info.textarea_rect = ClientRectangle;
  464. combobox_info.textarea_rect.Y += ThemeEngine.Current.DrawComboBoxDecorationTop ();
  465. combobox_info.textarea_rect.X += ThemeEngine.Current.DrawComboBoxDecorationLeft ();
  466. combobox_info.textarea_rect.Height -= ThemeEngine.Current.DrawComboBoxDecorationBottom ();
  467. combobox_info.textarea_rect.Width -= ThemeEngine.Current.DrawComboBoxDecorationRight ();
  468. if (CBoxInfo.show_button) {
  469. combobox_info.textarea_rect.Width -= def_button_width;
  470. combobox_info.button_rect = new Rectangle (combobox_info.textarea_rect.X + combobox_info.textarea_rect.Width,
  471. combobox_info.textarea_rect.Y, def_button_width, combobox_info.textarea_rect.Height);
  472. }
  473. }
  474. private void CreateListBoxPopUp ()
  475. {
  476. listbox_popup = new ListBoxPopUp (this);
  477. listbox_popup.Location = PointToScreen (new Point (ClientRectangle.X, ClientRectangle.Y + ClientRectangle.Height));
  478. listbox_popup.Size = combobox_info.listbox_size;
  479. }
  480. internal void Draw (Rectangle clip)
  481. {
  482. // No edit control, we paint the edit are ourselfs
  483. if (dropdown_style == ComboBoxStyle.DropDownList) {
  484. if (selected_index != -1) {
  485. OnDrawItem (new DrawItemEventArgs (DeviceContext, Font, combobox_info.textarea_rect,
  486. selected_index, DrawItemState.Selected,
  487. ForeColor, BackColor));
  488. }
  489. else
  490. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
  491. ClientRectangle);
  492. }
  493. if (CBoxInfo.show_button) {
  494. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorButtonFace),
  495. combobox_info.button_rect);
  496. ThemeEngine.Current.CPDrawComboButton (DeviceContext,
  497. combobox_info.button_rect, combobox_info.button_status);
  498. }
  499. ThemeEngine.Current.DrawComboBoxDecorations (DeviceContext, this);
  500. }
  501. internal virtual void OnMouseDownCB (object sender, MouseEventArgs e)
  502. {
  503. /* Click On button*/
  504. if (clicked == false && combobox_info.button_rect.Contains (e.X, e.Y)) {
  505. clicked = true;
  506. if (combobox_info.button_status == ButtonState.Normal) {
  507. combobox_info.button_status = ButtonState.Pushed;
  508. }
  509. else {
  510. if (combobox_info.button_status == ButtonState.Pushed) {
  511. combobox_info.button_status = ButtonState.Normal;
  512. }
  513. }
  514. if (combobox_info.button_status == ButtonState.Pushed) {
  515. if (listbox_popup == null)
  516. CreateListBoxPopUp ();
  517. listbox_popup.ShowWindow ();
  518. }
  519. Invalidate (combobox_info.button_rect);
  520. }
  521. }
  522. internal virtual void OnMouseUpCB (object sender, MouseEventArgs e)
  523. {
  524. /* Click on button*/
  525. if (clicked == true && combobox_info.button_rect.Contains (e.X, e.Y)) {
  526. clicked = false;
  527. }
  528. }
  529. private void OnPaintCB (PaintEventArgs pevent)
  530. {
  531. if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
  532. return;
  533. /* Copies memory drawing buffer to screen*/
  534. Draw (ClientRectangle);
  535. pevent.Graphics.DrawImage (ImageBuffer, ClientRectangle, ClientRectangle, GraphicsUnit.Pixel);
  536. if (Paint != null)
  537. Paint (this, pevent);
  538. }
  539. #endregion Private Methods
  540. /*
  541. ComboBox.ObjectCollection
  542. */
  543. public class ObjectCollection : IList, ICollection, IEnumerable
  544. {
  545. private ComboBox owner;
  546. internal ArrayList object_items = new ArrayList ();
  547. internal ArrayList listbox_items = new ArrayList ();
  548. public ObjectCollection (ComboBox owner)
  549. {
  550. this.owner = owner;
  551. }
  552. #region Public Properties
  553. public virtual int Count {
  554. get { return object_items.Count; }
  555. }
  556. public virtual bool IsReadOnly {
  557. get { return false; }
  558. }
  559. public virtual object this [int index] {
  560. get {
  561. if (index < 0 || index >= Count)
  562. throw new ArgumentOutOfRangeException ("Index of out range");
  563. return object_items[index];
  564. }
  565. set {
  566. if (index < 0 || index >= Count)
  567. throw new ArgumentOutOfRangeException ("Index of out range");
  568. object_items[index] = value;
  569. }
  570. }
  571. bool ICollection.IsSynchronized {
  572. get { return false; }
  573. }
  574. object ICollection.SyncRoot {
  575. get { return this; }
  576. }
  577. bool IList.IsFixedSize {
  578. get { return false; }
  579. }
  580. #endregion Public Properties
  581. #region Public Methods
  582. public int Add (object item)
  583. {
  584. int idx;
  585. idx = AddItem (item);
  586. return idx;
  587. }
  588. public void AddRange (object[] items)
  589. {
  590. foreach (object mi in items)
  591. AddItem (mi);
  592. }
  593. public virtual void Clear ()
  594. {
  595. object_items.Clear ();
  596. listbox_items.Clear ();
  597. }
  598. public virtual bool Contains (object obj)
  599. {
  600. return object_items.Contains (obj);
  601. }
  602. public void CopyTo (object[] dest, int arrayIndex)
  603. {
  604. object_items.CopyTo (dest, arrayIndex);
  605. }
  606. void ICollection.CopyTo (Array dest, int index)
  607. {
  608. object_items.CopyTo (dest, index);
  609. }
  610. public virtual IEnumerator GetEnumerator ()
  611. {
  612. return object_items.GetEnumerator ();
  613. }
  614. int IList.Add (object item)
  615. {
  616. return Add (item);
  617. }
  618. public virtual int IndexOf (object value)
  619. {
  620. return object_items.IndexOf (value);
  621. }
  622. public virtual void Insert (int index, object item)
  623. {
  624. throw new NotImplementedException ();
  625. }
  626. public virtual void Remove (object value)
  627. {
  628. RemoveAt (IndexOf (value));
  629. }
  630. public virtual void RemoveAt (int index)
  631. {
  632. if (index < 0 || index >= Count)
  633. throw new ArgumentOutOfRangeException ("Index of out range");
  634. object_items.RemoveAt (index);
  635. listbox_items.RemoveAt (index);
  636. //owner.UpdateItemInfo (false, -1, -1);
  637. }
  638. #endregion Public Methods
  639. #region Private Methods
  640. private int AddItem (object item)
  641. {
  642. int cnt = object_items.Count;
  643. object_items.Add (item);
  644. listbox_items.Add (new ComboBox.ComboBoxItem (cnt));
  645. return cnt;
  646. }
  647. internal ComboBox.ComboBoxItem GetComboBoxItem (int index)
  648. {
  649. if (index < 0 || index >= Count)
  650. throw new ArgumentOutOfRangeException ("Index of out range");
  651. return (ComboBox.ComboBoxItem) listbox_items[index];
  652. }
  653. internal void SetComboBoxItem (ComboBox.ComboBoxItem item, int index)
  654. {
  655. if (index < 0 || index >= Count)
  656. throw new ArgumentOutOfRangeException ("Index of out range");
  657. listbox_items[index] = item;
  658. }
  659. #endregion Private Methods
  660. }
  661. /*
  662. class ListBoxPopUp
  663. */
  664. internal class ListBoxPopUp : Control
  665. {
  666. private ComboBox owner;
  667. private bool need_vscrollbar;
  668. private VScrollBar vscrollbar_ctrl;
  669. private int top_item; /* First item that we show the in the current page */
  670. private int last_item; /* Last visible item */
  671. private int highlighted_item; /* Item that is currently selected */
  672. private Rectangle textarea_rect; /* Rectangle of the drawable text area */
  673. public ListBoxPopUp (ComboBox owner): base ()
  674. {
  675. this.owner = owner;
  676. need_vscrollbar = false;
  677. top_item = 0;
  678. last_item = 0;
  679. highlighted_item = -1;
  680. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  681. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  682. MouseUp += new MouseEventHandler (OnMouseUpPUW);
  683. Paint += new PaintEventHandler (OnPaintPUW);
  684. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  685. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  686. /* Vertical scrollbar */
  687. vscrollbar_ctrl = new VScrollBar ();
  688. vscrollbar_ctrl.Minimum = 0;
  689. vscrollbar_ctrl.SmallChange = 1;
  690. vscrollbar_ctrl.LargeChange = 1;
  691. vscrollbar_ctrl.Maximum = 0;
  692. vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
  693. vscrollbar_ctrl.Visible = false;
  694. }
  695. protected override CreateParams CreateParams
  696. {
  697. get {
  698. CreateParams cp = base.CreateParams;
  699. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
  700. cp.ExStyle |= (int)WindowStyles.WS_EX_TOOLWINDOW;
  701. return cp;
  702. }
  703. }
  704. #region Private Methods
  705. protected override void CreateHandle ()
  706. {
  707. base.CreateHandle ();
  708. Controls.Add (vscrollbar_ctrl);
  709. }
  710. // Calcs the listbox area
  711. internal void CalcListBoxArea ()
  712. {
  713. int width, height;
  714. width = owner.ClientRectangle.Width;
  715. if (owner.Items.Count <= owner.MaxDropDownItems) {
  716. height = owner.ItemHeight * owner.Items.Count;
  717. need_vscrollbar = false;
  718. }
  719. else {
  720. height = owner.ItemHeight * owner.MaxDropDownItems;
  721. need_vscrollbar = true;
  722. vscrollbar_ctrl.Height = height - 1;
  723. vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - 1, 1);
  724. vscrollbar_ctrl.Maximum = owner.Items.Count - owner.MaxDropDownItems;
  725. }
  726. if (vscrollbar_ctrl.Visible != need_vscrollbar)
  727. vscrollbar_ctrl.Visible = need_vscrollbar;
  728. Size = new Size (width, height);
  729. textarea_rect = ClientRectangle;
  730. // Exclude decorations
  731. textarea_rect.X += 1;
  732. textarea_rect.Y += 1;
  733. textarea_rect.Width -= 1;
  734. textarea_rect.Height -= 1;
  735. if (need_vscrollbar)
  736. textarea_rect.Width -= vscrollbar_ctrl.Width;
  737. last_item = LastVisibleItem ();
  738. }
  739. private void Draw (Rectangle clip)
  740. {
  741. Rectangle cl = ClientRectangle;
  742. if (owner.Items.Count > 0) {
  743. Rectangle item_rect;
  744. DrawItemState state = DrawItemState.None;
  745. for (int i = top_item; i < last_item; i++) {
  746. item_rect = GetItemDisplayRectangle (i, top_item);
  747. if (clip.IntersectsWith (item_rect) == false)
  748. continue;
  749. /* Draw item */
  750. state = DrawItemState.None;
  751. if (i == highlighted_item)
  752. state |= DrawItemState.Selected;
  753. owner.OnDrawItem (new DrawItemEventArgs (DeviceContext, owner.Font, item_rect,
  754. i, state, owner.ForeColor, owner.BackColor));
  755. }
  756. }
  757. //DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  758. // (owner.BackColor), ClientRectangle);
  759. DeviceContext.DrawRectangle (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorWindowFrame),
  760. cl.X, cl.Y, cl.Width - 1, cl.Height - 1);
  761. }
  762. private Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  763. {
  764. if (index < 0 || index >= owner.Items.Count)
  765. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  766. Rectangle item_rect = new Rectangle ();
  767. item_rect.X = 0;
  768. item_rect.Y = owner.ItemHeight * (index - first_displayble);
  769. item_rect.Height = owner.ItemHeight;
  770. item_rect.Width = textarea_rect.Width;
  771. return item_rect;
  772. }
  773. public void HideWindow ()
  774. {
  775. owner.ButtonReleased ();
  776. Hide ();
  777. }
  778. private int IndexFromPointDisplayRectangle (int x, int y)
  779. {
  780. for (int i = top_item; i < last_item; i++) {
  781. if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
  782. return i;
  783. }
  784. return -1;
  785. }
  786. private int LastVisibleItem ()
  787. {
  788. Rectangle item_rect;
  789. int top_y = textarea_rect.Y + textarea_rect.Height;
  790. int i = 0;
  791. for (i = top_item; i < owner.Items.Count; i++) {
  792. item_rect = GetItemDisplayRectangle (i, top_item);
  793. if (item_rect.Y > top_y)
  794. return i;
  795. }
  796. return i;
  797. }
  798. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  799. {
  800. /* Click outside the client area destroys the popup */
  801. if (ClientRectangle.Contains (e.X, e.Y) == false) {
  802. HideWindow ();
  803. return;
  804. }
  805. /* Click on an element */
  806. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  807. if (index == -1) return;
  808. owner.SelectedIndex = index;
  809. HideWindow ();
  810. }
  811. private void OnMouseUpPUW (object sender, MouseEventArgs e)
  812. {
  813. }
  814. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  815. {
  816. Rectangle invalidate;
  817. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  818. if (index == -1) return;
  819. /* Previous item */
  820. if (highlighted_item != -1) {
  821. invalidate = GetItemDisplayRectangle (highlighted_item, top_item);
  822. if (ClientRectangle.Contains (invalidate))
  823. Invalidate (invalidate);
  824. }
  825. highlighted_item = index;
  826. /* Current item */
  827. invalidate = GetItemDisplayRectangle (highlighted_item, top_item);
  828. if (ClientRectangle.Contains (invalidate))
  829. Invalidate (invalidate);
  830. }
  831. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  832. {
  833. if (Width <= 0 || Height <= 0 || Visible == false)
  834. return;
  835. Draw (pevent.ClipRectangle);
  836. pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  837. }
  838. public void ShowWindow ()
  839. {
  840. CalcListBoxArea ();
  841. Show ();
  842. Refresh ();
  843. }
  844. // Value Changed
  845. private void VerticalScrollEvent (object sender, EventArgs e)
  846. {
  847. top_item = vscrollbar_ctrl.Value;
  848. last_item = LastVisibleItem ();
  849. Refresh ();
  850. }
  851. #endregion Private Methods
  852. }
  853. }
  854. }