PageRenderTime 70ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/danipen/mono
C# | 2817 lines | 2229 code | 487 blank | 101 comment | 604 complexity | 12213990fe0967c4e061632cb138e58b MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // 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-2006 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, jordi@ximian.com
  24. // Mike Kestner <mkestner@novell.com>
  25. // Daniel Nauck (dna(at)mono-project(dot)de)
  26. using System;
  27. using System.Collections;
  28. using System.ComponentModel;
  29. using System.ComponentModel.Design;
  30. using System.ComponentModel.Design.Serialization;
  31. using System.Drawing;
  32. using System.Globalization;
  33. using System.Reflection;
  34. using System.Runtime.InteropServices;
  35. using System.Diagnostics;
  36. namespace System.Windows.Forms
  37. {
  38. [DefaultProperty("Items")]
  39. [DefaultEvent("SelectedIndexChanged")]
  40. [Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  41. [DefaultBindingProperty ("Text")]
  42. [ClassInterface (ClassInterfaceType.AutoDispatch)]
  43. [ComVisible(true)]
  44. public class ComboBox : ListControl
  45. {
  46. private DrawMode draw_mode = DrawMode.Normal;
  47. private ComboBoxStyle dropdown_style;
  48. private int dropdown_width = -1;
  49. private int selected_index = -1;
  50. private ObjectCollection items;
  51. private bool suspend_ctrlupdate;
  52. private int maxdrop_items = 8;
  53. private bool integral_height = true;
  54. private bool sorted;
  55. private int max_length;
  56. private ComboListBox listbox_ctrl;
  57. private ComboTextBox textbox_ctrl;
  58. private bool process_textchanged_event = true;
  59. private bool process_texchanged_autoscroll = true;
  60. private bool item_height_specified;
  61. private int item_height;
  62. private int requested_height = -1;
  63. private Hashtable item_heights;
  64. private bool show_dropdown_button;
  65. private ButtonState button_state = ButtonState.Normal;
  66. private bool dropped_down;
  67. private Rectangle text_area;
  68. private Rectangle button_area;
  69. private Rectangle listbox_area;
  70. private const int button_width = 16;
  71. bool drop_down_button_entered;
  72. private AutoCompleteStringCollection auto_complete_custom_source = null;
  73. private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
  74. private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
  75. private FlatStyle flat_style;
  76. private int drop_down_height;
  77. const int default_drop_down_height = 106;
  78. [ComVisible(true)]
  79. public class ChildAccessibleObject : AccessibleObject {
  80. public ChildAccessibleObject (ComboBox owner, IntPtr handle)
  81. : base (owner)
  82. {
  83. }
  84. public override string Name {
  85. get {
  86. return base.Name;
  87. }
  88. }
  89. }
  90. public ComboBox ()
  91. {
  92. items = new ObjectCollection (this);
  93. DropDownStyle = ComboBoxStyle.DropDown;
  94. item_height = FontHeight + 2;
  95. background_color = ThemeEngine.Current.ColorWindow;
  96. border_style = BorderStyle.None;
  97. drop_down_height = default_drop_down_height;
  98. flat_style = FlatStyle.Standard;
  99. /* Events */
  100. MouseDown += new MouseEventHandler (OnMouseDownCB);
  101. MouseUp += new MouseEventHandler (OnMouseUpCB);
  102. MouseMove += new MouseEventHandler (OnMouseMoveCB);
  103. MouseWheel += new MouseEventHandler (OnMouseWheelCB);
  104. MouseEnter += new EventHandler (OnMouseEnter);
  105. MouseLeave += new EventHandler (OnMouseLeave);
  106. KeyDown +=new KeyEventHandler(OnKeyDownCB);
  107. }
  108. #region events
  109. [Browsable (false)]
  110. [EditorBrowsable (EditorBrowsableState.Never)]
  111. public new event EventHandler BackgroundImageChanged {
  112. add { base.BackgroundImageChanged += value; }
  113. remove { base.BackgroundImageChanged -= value; }
  114. }
  115. [Browsable (false)]
  116. [EditorBrowsable (EditorBrowsableState.Never)]
  117. public new event EventHandler BackgroundImageLayoutChanged
  118. {
  119. add { base.BackgroundImageLayoutChanged += value; }
  120. remove { base.BackgroundImageLayoutChanged -= value; }
  121. }
  122. [Browsable (false)]
  123. [EditorBrowsable (EditorBrowsableState.Never)]
  124. public new event EventHandler DoubleClick
  125. {
  126. add { base.DoubleClick += value; }
  127. remove { base.DoubleClick -= value; }
  128. }
  129. static object DrawItemEvent = new object ();
  130. static object DropDownEvent = new object ();
  131. static object DropDownStyleChangedEvent = new object ();
  132. static object MeasureItemEvent = new object ();
  133. static object SelectedIndexChangedEvent = new object ();
  134. static object SelectionChangeCommittedEvent = new object ();
  135. static object DropDownClosedEvent = new object ();
  136. static object TextUpdateEvent = new object ();
  137. public event DrawItemEventHandler DrawItem {
  138. add { Events.AddHandler (DrawItemEvent, value); }
  139. remove { Events.RemoveHandler (DrawItemEvent, value); }
  140. }
  141. public event EventHandler DropDown {
  142. add { Events.AddHandler (DropDownEvent, value); }
  143. remove { Events.RemoveHandler (DropDownEvent, value); }
  144. }
  145. public event EventHandler DropDownClosed
  146. {
  147. add { Events.AddHandler (DropDownClosedEvent, value); }
  148. remove { Events.RemoveHandler (DropDownClosedEvent, value); }
  149. }
  150. public event EventHandler DropDownStyleChanged {
  151. add { Events.AddHandler (DropDownStyleChangedEvent, value); }
  152. remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
  153. }
  154. public event MeasureItemEventHandler MeasureItem {
  155. add { Events.AddHandler (MeasureItemEvent, value); }
  156. remove { Events.RemoveHandler (MeasureItemEvent, value); }
  157. }
  158. [Browsable (false)]
  159. [EditorBrowsable (EditorBrowsableState.Never)]
  160. public new event EventHandler PaddingChanged
  161. {
  162. add { base.PaddingChanged += value; }
  163. remove { base.PaddingChanged -= value; }
  164. }
  165. [Browsable (false)]
  166. [EditorBrowsable (EditorBrowsableState.Never)]
  167. public new event PaintEventHandler Paint {
  168. add { base.Paint += value; }
  169. remove { base.Paint -= value; }
  170. }
  171. public event EventHandler SelectedIndexChanged {
  172. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  173. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  174. }
  175. public event EventHandler SelectionChangeCommitted {
  176. add { Events.AddHandler (SelectionChangeCommittedEvent, value); }
  177. remove { Events.RemoveHandler (SelectionChangeCommittedEvent, value); }
  178. }
  179. public event EventHandler TextUpdate
  180. {
  181. add { Events.AddHandler (TextUpdateEvent, value); }
  182. remove { Events.RemoveHandler (TextUpdateEvent, value); }
  183. }
  184. #endregion Events
  185. #region Public Properties
  186. [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
  187. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  188. [Browsable (true)]
  189. [EditorBrowsable (EditorBrowsableState.Always)]
  190. [Localizable (true)]
  191. [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
  192. "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  193. public AutoCompleteStringCollection AutoCompleteCustomSource {
  194. get {
  195. if(auto_complete_custom_source == null) {
  196. auto_complete_custom_source = new AutoCompleteStringCollection ();
  197. auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
  198. }
  199. return auto_complete_custom_source;
  200. }
  201. set {
  202. if(auto_complete_custom_source == value)
  203. return;
  204. if(auto_complete_custom_source != null) //remove eventhandler from old collection
  205. auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
  206. auto_complete_custom_source = value;
  207. if(auto_complete_custom_source != null)
  208. auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
  209. SetTextBoxAutoCompleteData ();
  210. }
  211. }
  212. [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
  213. [Browsable (true)]
  214. [EditorBrowsable (EditorBrowsableState.Always)]
  215. [DefaultValue (AutoCompleteMode.None)]
  216. public AutoCompleteMode AutoCompleteMode {
  217. get { return auto_complete_mode; }
  218. set {
  219. if(auto_complete_mode == value)
  220. return;
  221. if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
  222. throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
  223. auto_complete_mode = value;
  224. SetTextBoxAutoCompleteData ();
  225. }
  226. }
  227. [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
  228. [Browsable (true)]
  229. [EditorBrowsable (EditorBrowsableState.Always)]
  230. [DefaultValue (AutoCompleteSource.None)]
  231. public AutoCompleteSource AutoCompleteSource {
  232. get { return auto_complete_source; }
  233. set {
  234. if(auto_complete_source == value)
  235. return;
  236. if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
  237. throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
  238. auto_complete_source = value;
  239. SetTextBoxAutoCompleteData ();
  240. }
  241. }
  242. void SetTextBoxAutoCompleteData ()
  243. {
  244. if (textbox_ctrl == null)
  245. return;
  246. textbox_ctrl.AutoCompleteMode = auto_complete_mode;
  247. if (auto_complete_source == AutoCompleteSource.ListItems) {
  248. textbox_ctrl.AutoCompleteSource = AutoCompleteSource.CustomSource;
  249. textbox_ctrl.AutoCompleteCustomSource = null;
  250. textbox_ctrl.AutoCompleteInternalSource = this;
  251. } else {
  252. textbox_ctrl.AutoCompleteSource = auto_complete_source;
  253. textbox_ctrl.AutoCompleteCustomSource = auto_complete_custom_source;
  254. textbox_ctrl.AutoCompleteInternalSource = null;
  255. }
  256. }
  257. public override Color BackColor {
  258. get { return base.BackColor; }
  259. set {
  260. if (base.BackColor == value)
  261. return;
  262. base.BackColor = value;
  263. Refresh ();
  264. }
  265. }
  266. [Browsable (false)]
  267. [EditorBrowsable (EditorBrowsableState.Never)]
  268. public override Image BackgroundImage {
  269. get { return base.BackgroundImage; }
  270. set {
  271. if (base.BackgroundImage == value)
  272. return;
  273. base.BackgroundImage = value;
  274. Refresh ();
  275. }
  276. }
  277. [Browsable (false)]
  278. [EditorBrowsable (EditorBrowsableState.Never)]
  279. public override ImageLayout BackgroundImageLayout {
  280. get { return base.BackgroundImageLayout; }
  281. set { base.BackgroundImageLayout = value; }
  282. }
  283. protected override CreateParams CreateParams {
  284. get { return base.CreateParams;}
  285. }
  286. [DefaultValue ((string)null)]
  287. [AttributeProvider (typeof (IListSource))]
  288. [RefreshProperties (RefreshProperties.Repaint)]
  289. [MWFCategory("Data")]
  290. public new object DataSource {
  291. get { return base.DataSource; }
  292. set { base.DataSource = value; }
  293. }
  294. protected override Size DefaultSize {
  295. get { return new Size (121, 21); }
  296. }
  297. [RefreshProperties(RefreshProperties.Repaint)]
  298. [DefaultValue (DrawMode.Normal)]
  299. [MWFCategory("Behavior")]
  300. public DrawMode DrawMode {
  301. get { return draw_mode; }
  302. set {
  303. if (!Enum.IsDefined (typeof (DrawMode), value))
  304. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
  305. if (draw_mode == value)
  306. return;
  307. if (draw_mode == DrawMode.OwnerDrawVariable)
  308. item_heights = null;
  309. draw_mode = value;
  310. if (draw_mode == DrawMode.OwnerDrawVariable)
  311. item_heights = new Hashtable ();
  312. Refresh ();
  313. }
  314. }
  315. [Browsable (true)]
  316. [DefaultValue (106)]
  317. [EditorBrowsable (EditorBrowsableState.Always)]
  318. [MWFCategory("Behavior")]
  319. public int DropDownHeight {
  320. get {
  321. return drop_down_height;
  322. }
  323. set {
  324. if (value < 1)
  325. throw new ArgumentOutOfRangeException ("DropDownHeight", "DropDownHeight must be greater than 0.");
  326. if (value == drop_down_height)
  327. return;
  328. drop_down_height = value;
  329. IntegralHeight = false;
  330. }
  331. }
  332. [DefaultValue (ComboBoxStyle.DropDown)]
  333. [RefreshProperties(RefreshProperties.Repaint)]
  334. [MWFCategory("Appearance")]
  335. public ComboBoxStyle DropDownStyle {
  336. get { return dropdown_style; }
  337. set {
  338. if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
  339. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
  340. if (dropdown_style == value)
  341. return;
  342. SuspendLayout ();
  343. if (dropdown_style == ComboBoxStyle.Simple) {
  344. if (listbox_ctrl != null) {
  345. Controls.RemoveImplicit (listbox_ctrl);
  346. listbox_ctrl.Dispose ();
  347. listbox_ctrl = null;
  348. }
  349. }
  350. dropdown_style = value;
  351. if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
  352. Controls.RemoveImplicit (textbox_ctrl);
  353. textbox_ctrl.Dispose ();
  354. textbox_ctrl = null;
  355. }
  356. if (dropdown_style == ComboBoxStyle.Simple) {
  357. show_dropdown_button = false;
  358. CreateComboListBox ();
  359. Controls.AddImplicit (listbox_ctrl);
  360. listbox_ctrl.Visible = true;
  361. // This should give us a 150 default height
  362. // for Simple mode if size hasn't been set
  363. // (DefaultSize doesn't work for us in this case)
  364. if (requested_height == -1)
  365. requested_height = 150;
  366. } else {
  367. show_dropdown_button = true;
  368. button_state = ButtonState.Normal;
  369. }
  370. if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
  371. textbox_ctrl = new ComboTextBox (this);
  372. object selected_item = SelectedItem;
  373. if (selected_item != null)
  374. textbox_ctrl.Text = GetItemText (selected_item);
  375. textbox_ctrl.BorderStyle = BorderStyle.None;
  376. textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
  377. textbox_ctrl.KeyPress += new KeyPressEventHandler (OnTextKeyPress);
  378. textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
  379. textbox_ctrl.ContextMenu = ContextMenu;
  380. textbox_ctrl.TopMargin = 1; // since we don't have borders, adjust manually the top
  381. if (IsHandleCreated == true)
  382. Controls.AddImplicit (textbox_ctrl);
  383. SetTextBoxAutoCompleteData ();
  384. }
  385. ResumeLayout ();
  386. OnDropDownStyleChanged (EventArgs.Empty);
  387. LayoutComboBox ();
  388. UpdateComboBoxBounds ();
  389. Refresh ();
  390. }
  391. }
  392. [MWFCategory("Behavior")]
  393. public int DropDownWidth {
  394. get {
  395. if (dropdown_width == -1)
  396. return Width;
  397. return dropdown_width;
  398. }
  399. set {
  400. if (dropdown_width == value)
  401. return;
  402. if (value < 1)
  403. throw new ArgumentOutOfRangeException ("DropDownWidth",
  404. "The DropDownWidth value is less than one.");
  405. dropdown_width = value;
  406. }
  407. }
  408. [Browsable (false)]
  409. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  410. public bool DroppedDown {
  411. get {
  412. if (dropdown_style == ComboBoxStyle.Simple)
  413. return true;
  414. return dropped_down;
  415. }
  416. set {
  417. if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
  418. return;
  419. if (value)
  420. DropDownListBox ();
  421. else
  422. listbox_ctrl.HideWindow ();
  423. }
  424. }
  425. [DefaultValue (FlatStyle.Standard)]
  426. [Localizable (true)]
  427. [MWFCategory("Appearance")]
  428. public FlatStyle FlatStyle {
  429. get { return flat_style; }
  430. set {
  431. if (!Enum.IsDefined (typeof (FlatStyle), value))
  432. throw new InvalidEnumArgumentException ("FlatStyle", (int) value, typeof (FlatStyle));
  433. flat_style = value;
  434. LayoutComboBox ();
  435. Invalidate ();
  436. }
  437. }
  438. public override bool Focused {
  439. get { return base.Focused; }
  440. }
  441. public override Color ForeColor {
  442. get { return base.ForeColor; }
  443. set {
  444. if (base.ForeColor == value)
  445. return;
  446. base.ForeColor = value;
  447. Refresh ();
  448. }
  449. }
  450. [DefaultValue (true)]
  451. [Localizable (true)]
  452. [MWFCategory("Behavior")]
  453. public bool IntegralHeight {
  454. get { return integral_height; }
  455. set {
  456. if (integral_height == value)
  457. return;
  458. integral_height = value;
  459. UpdateComboBoxBounds ();
  460. Refresh ();
  461. }
  462. }
  463. [Localizable (true)]
  464. [MWFCategory("Behavior")]
  465. public int ItemHeight {
  466. get {
  467. if (item_height == -1) {
  468. SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
  469. item_height = (int) sz.Height;
  470. }
  471. return item_height;
  472. }
  473. set {
  474. if (value < 1)
  475. throw new ArgumentOutOfRangeException ("ItemHeight",
  476. "The item height value is less than one.");
  477. item_height_specified = true;
  478. item_height = value;
  479. if (IntegralHeight)
  480. UpdateComboBoxBounds ();
  481. LayoutComboBox ();
  482. Refresh ();
  483. }
  484. }
  485. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  486. [Localizable (true)]
  487. [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  488. [MergableProperty (false)]
  489. [MWFCategory("Data")]
  490. public ComboBox.ObjectCollection Items {
  491. get { return items; }
  492. }
  493. [DefaultValue (8)]
  494. [Localizable (true)]
  495. [MWFCategory("Behavior")]
  496. public int MaxDropDownItems {
  497. get { return maxdrop_items; }
  498. set {
  499. if (maxdrop_items == value)
  500. return;
  501. maxdrop_items = value;
  502. }
  503. }
  504. public override Size MaximumSize {
  505. get { return base.MaximumSize; }
  506. set {
  507. base.MaximumSize = new Size (value.Width, 0);
  508. }
  509. }
  510. [DefaultValue (0)]
  511. [Localizable (true)]
  512. [MWFCategory("Behavior")]
  513. public int MaxLength {
  514. get { return max_length; }
  515. set {
  516. if (max_length == value)
  517. return;
  518. max_length = value;
  519. if (dropdown_style != ComboBoxStyle.DropDownList) {
  520. if (value < 0) {
  521. value = 0;
  522. }
  523. textbox_ctrl.MaxLength = value;
  524. }
  525. }
  526. }
  527. public override Size MinimumSize {
  528. get { return base.MinimumSize; }
  529. set {
  530. base.MinimumSize = new Size (value.Width, 0);
  531. }
  532. }
  533. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  534. [EditorBrowsable (EditorBrowsableState.Never)]
  535. [Browsable (false)]
  536. public new Padding Padding {
  537. get { return base.Padding; }
  538. set { base.Padding = value; }
  539. }
  540. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  541. [Browsable (false)]
  542. public int PreferredHeight {
  543. get { return Font.Height + 8; }
  544. }
  545. [Browsable (false)]
  546. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  547. public override int SelectedIndex {
  548. get { return selected_index; }
  549. set {
  550. SetSelectedIndex (value, false);
  551. }
  552. }
  553. [Browsable (false)]
  554. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  555. [Bindable(true)]
  556. public object SelectedItem {
  557. get { return selected_index == -1 ? null : Items [selected_index]; }
  558. set {
  559. object item = selected_index == -1 ? null : Items [selected_index];
  560. if (item == value)
  561. return;
  562. if (value == null)
  563. SelectedIndex = -1;
  564. else
  565. SelectedIndex = Items.IndexOf (value);
  566. }
  567. }
  568. [Browsable (false)]
  569. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  570. public string SelectedText {
  571. get {
  572. if (dropdown_style == ComboBoxStyle.DropDownList)
  573. return string.Empty;
  574. string retval = textbox_ctrl.SelectedText;
  575. return retval;
  576. }
  577. set {
  578. if (dropdown_style == ComboBoxStyle.DropDownList)
  579. return;
  580. textbox_ctrl.SelectedText = value;
  581. }
  582. }
  583. [Browsable (false)]
  584. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  585. public int SelectionLength {
  586. get {
  587. if (dropdown_style == ComboBoxStyle.DropDownList)
  588. return 0;
  589. int result = textbox_ctrl.SelectionLength;
  590. return result == -1 ? 0 : result;
  591. }
  592. set {
  593. if (dropdown_style == ComboBoxStyle.DropDownList)
  594. return;
  595. if (textbox_ctrl.SelectionLength == value)
  596. return;
  597. textbox_ctrl.SelectionLength = value;
  598. }
  599. }
  600. [Browsable (false)]
  601. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  602. public int SelectionStart {
  603. get {
  604. if (dropdown_style == ComboBoxStyle.DropDownList)
  605. return 0;
  606. return textbox_ctrl.SelectionStart;
  607. }
  608. set {
  609. if (dropdown_style == ComboBoxStyle.DropDownList)
  610. return;
  611. if (textbox_ctrl.SelectionStart == value)
  612. return;
  613. textbox_ctrl.SelectionStart = value;
  614. }
  615. }
  616. [DefaultValue (false)]
  617. [MWFCategory("Behavior")]
  618. public bool Sorted {
  619. get { return sorted; }
  620. set {
  621. if (sorted == value)
  622. return;
  623. sorted = value;
  624. SelectedIndex = -1;
  625. if (sorted) {
  626. Items.Sort ();
  627. LayoutComboBox ();
  628. }
  629. }
  630. }
  631. [Bindable (true)]
  632. [Localizable (true)]
  633. public override string Text {
  634. get {
  635. if (dropdown_style != ComboBoxStyle.DropDownList) {
  636. if (textbox_ctrl != null) {
  637. return textbox_ctrl.Text;
  638. }
  639. }
  640. if (SelectedItem != null)
  641. return GetItemText (SelectedItem);
  642. return base.Text;
  643. }
  644. set {
  645. if (value == null) {
  646. if (SelectedIndex == -1) {
  647. if (dropdown_style != ComboBoxStyle.DropDownList)
  648. SetControlText (string.Empty, false);
  649. } else {
  650. SelectedIndex = -1;
  651. }
  652. return;
  653. }
  654. // don't set the index if value exactly matches text of selected item
  655. if (SelectedItem == null || string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) != 0)
  656. {
  657. // find exact match using case-sensitive comparison, and if does
  658. // not result in any match then use case-insensitive comparison
  659. int index = FindStringExact (value, -1, false);
  660. if (index == -1) {
  661. index = FindStringExact (value, -1, true);
  662. }
  663. if (index != -1) {
  664. SelectedIndex = index;
  665. return;
  666. }
  667. }
  668. // set directly the passed value
  669. if (dropdown_style != ComboBoxStyle.DropDownList)
  670. textbox_ctrl.Text = value;
  671. }
  672. }
  673. #endregion Public Properties
  674. #region Internal Properties
  675. internal Rectangle ButtonArea {
  676. get { return button_area; }
  677. }
  678. internal Rectangle TextArea {
  679. get { return text_area; }
  680. }
  681. #endregion
  682. #region UIA Framework Properties
  683. internal TextBox UIATextBox {
  684. get { return textbox_ctrl; }
  685. }
  686. internal ComboListBox UIAComboListBox {
  687. get { return listbox_ctrl; }
  688. }
  689. #endregion UIA Framework Properties
  690. #region Public Methods
  691. [Obsolete ("This method has been deprecated")]
  692. protected virtual void AddItemsCore (object[] value)
  693. {
  694. }
  695. public void BeginUpdate ()
  696. {
  697. suspend_ctrlupdate = true;
  698. }
  699. protected override AccessibleObject CreateAccessibilityInstance ()
  700. {
  701. return base.CreateAccessibilityInstance ();
  702. }
  703. protected override void CreateHandle ()
  704. {
  705. base.CreateHandle ();
  706. }
  707. protected override void Dispose (bool disposing)
  708. {
  709. if (disposing) {
  710. if (listbox_ctrl != null) {
  711. listbox_ctrl.Dispose ();
  712. Controls.RemoveImplicit (listbox_ctrl);
  713. listbox_ctrl = null;
  714. }
  715. if (textbox_ctrl != null) {
  716. Controls.RemoveImplicit (textbox_ctrl);
  717. textbox_ctrl.Dispose ();
  718. textbox_ctrl = null;
  719. }
  720. }
  721. base.Dispose (disposing);
  722. }
  723. public void EndUpdate ()
  724. {
  725. suspend_ctrlupdate = false;
  726. UpdatedItems ();
  727. Refresh ();
  728. }
  729. public int FindString (string s)
  730. {
  731. return FindString (s, -1);
  732. }
  733. public int FindString (string s, int startIndex)
  734. {
  735. if (s == null || Items.Count == 0)
  736. return -1;
  737. if (startIndex < -1 || startIndex >= Items.Count)
  738. throw new ArgumentOutOfRangeException ("startIndex");
  739. int i = startIndex;
  740. if (i == (Items.Count - 1))
  741. i = -1;
  742. do {
  743. i++;
  744. if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
  745. return i;
  746. if (i == (Items.Count - 1))
  747. i = -1;
  748. } while (i != startIndex);
  749. return -1;
  750. }
  751. public int FindStringExact (string s)
  752. {
  753. return FindStringExact (s, -1);
  754. }
  755. public int FindStringExact (string s, int startIndex)
  756. {
  757. return FindStringExact (s, startIndex, true);
  758. }
  759. private int FindStringExact (string s, int startIndex, bool ignoreCase)
  760. {
  761. if (s == null || Items.Count == 0)
  762. return -1;
  763. if (startIndex < -1 || startIndex >= Items.Count)
  764. throw new ArgumentOutOfRangeException ("startIndex");
  765. int i = startIndex;
  766. if (i == (Items.Count - 1))
  767. i = -1;
  768. do {
  769. i++;
  770. if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
  771. return i;
  772. if (i == (Items.Count - 1))
  773. i = -1;
  774. } while (i != startIndex);
  775. return -1;
  776. }
  777. public int GetItemHeight (int index)
  778. {
  779. if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
  780. if (index < 0 || index >= Items.Count )
  781. throw new ArgumentOutOfRangeException ("The item height value is less than zero");
  782. object item = Items [index];
  783. if (item_heights.Contains (item))
  784. return (int) item_heights [item];
  785. MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
  786. OnMeasureItem (args);
  787. item_heights [item] = args.ItemHeight;
  788. return args.ItemHeight;
  789. }
  790. return ItemHeight;
  791. }
  792. protected override bool IsInputKey (Keys keyData)
  793. {
  794. switch (keyData & ~Keys.Modifiers) {
  795. case Keys.Up:
  796. case Keys.Down:
  797. case Keys.Left:
  798. case Keys.Right:
  799. case Keys.PageUp:
  800. case Keys.PageDown:
  801. case Keys.Home:
  802. case Keys.End:
  803. return true;
  804. default:
  805. return false;
  806. }
  807. }
  808. protected override void OnBackColorChanged (EventArgs e)
  809. {
  810. base.OnBackColorChanged (e);
  811. if (textbox_ctrl != null)
  812. textbox_ctrl.BackColor = BackColor;
  813. }
  814. protected override void OnDataSourceChanged (EventArgs e)
  815. {
  816. base.OnDataSourceChanged (e);
  817. BindDataItems ();
  818. /**
  819. ** This 'Debugger.IsAttached' hack is here because of
  820. ** Xamarin Bug #2234, which noted that when changing
  821. ** the DataSource, in Windows exceptions are eaten
  822. ** when SelectedIndexChanged is fired. However, when
  823. ** the debugger is running (i.e. in MonoDevelop), we
  824. ** want to be alerted of exceptions.
  825. **/
  826. if (Debugger.IsAttached) {
  827. SetSelectedIndex ();
  828. } else {
  829. try {
  830. SetSelectedIndex ();
  831. } catch {
  832. //ignore exceptions here per
  833. //bug 2234
  834. }
  835. }
  836. }
  837. private void SetSelectedIndex ()
  838. {
  839. if (DataSource == null || DataManager == null) {
  840. SelectedIndex = -1;
  841. }
  842. else {
  843. SelectedIndex = DataManager.Position;
  844. }
  845. }
  846. protected override void OnDisplayMemberChanged (EventArgs e)
  847. {
  848. base.OnDisplayMemberChanged (e);
  849. if (DataManager == null)
  850. return;
  851. SelectedIndex = DataManager.Position;
  852. if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
  853. SetControlText (GetItemText (Items [selected_index]), true);
  854. if (!IsHandleCreated)
  855. return;
  856. Invalidate ();
  857. }
  858. protected virtual void OnDrawItem (DrawItemEventArgs e)
  859. {
  860. DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
  861. if (eh != null)
  862. eh (this, e);
  863. }
  864. internal void HandleDrawItem (DrawItemEventArgs e)
  865. {
  866. // Only raise OnDrawItem if we are in an OwnerDraw mode
  867. switch (DrawMode) {
  868. case DrawMode.OwnerDrawFixed:
  869. case DrawMode.OwnerDrawVariable:
  870. OnDrawItem (e);
  871. break;
  872. default:
  873. ThemeEngine.Current.DrawComboBoxItem (this, e);
  874. break;
  875. }
  876. }
  877. protected virtual void OnDropDown (EventArgs e)
  878. {
  879. EventHandler eh = (EventHandler)(Events [DropDownEvent]);
  880. if (eh != null)
  881. eh (this, e);
  882. }
  883. protected virtual void OnDropDownClosed (EventArgs e)
  884. {
  885. EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
  886. if (eh != null)
  887. eh (this, e);
  888. }
  889. protected virtual void OnDropDownStyleChanged (EventArgs e)
  890. {
  891. EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
  892. if (eh != null)
  893. eh (this, e);
  894. }
  895. protected override void OnFontChanged (EventArgs e)
  896. {
  897. base.OnFontChanged (e);
  898. if (textbox_ctrl != null)
  899. textbox_ctrl.Font = Font;
  900. if (!item_height_specified)
  901. item_height = Font.Height + 2;
  902. if (IntegralHeight)
  903. UpdateComboBoxBounds ();
  904. LayoutComboBox ();
  905. }
  906. protected override void OnForeColorChanged (EventArgs e)
  907. {
  908. base.OnForeColorChanged (e);
  909. if (textbox_ctrl != null)
  910. textbox_ctrl.ForeColor = ForeColor;
  911. }
  912. [EditorBrowsable(EditorBrowsableState.Advanced)]
  913. protected override void OnGotFocus (EventArgs e)
  914. {
  915. if (dropdown_style == ComboBoxStyle.DropDownList) {
  916. // We draw DDL styles manually, so they require a
  917. // refresh to have their selection drawn
  918. Invalidate ();
  919. }
  920. if (textbox_ctrl != null) {
  921. textbox_ctrl.SetSelectable (false);
  922. textbox_ctrl.ShowSelection = Enabled;
  923. textbox_ctrl.ActivateCaret (true);
  924. textbox_ctrl.SelectAll ();
  925. }
  926. base.OnGotFocus (e);
  927. }
  928. [EditorBrowsable(EditorBrowsableState.Advanced)]
  929. protected override void OnLostFocus (EventArgs e)
  930. {
  931. if (dropdown_style == ComboBoxStyle.DropDownList) {
  932. // We draw DDL styles manually, so they require a
  933. // refresh to have their selection drawn
  934. Invalidate ();
  935. }
  936. if (listbox_ctrl != null && dropped_down) {
  937. listbox_ctrl.HideWindow ();
  938. }
  939. if (textbox_ctrl != null) {
  940. textbox_ctrl.SetSelectable (true);
  941. textbox_ctrl.ActivateCaret (false);
  942. textbox_ctrl.ShowSelection = false;
  943. textbox_ctrl.SelectionLength = 0;
  944. textbox_ctrl.HideAutoCompleteList ();
  945. }
  946. base.OnLostFocus (e);
  947. }
  948. protected override void OnHandleCreated (EventArgs e)
  949. {
  950. base.OnHandleCreated (e);
  951. SetBoundsInternal (Left, Top, Width, PreferredHeight, BoundsSpecified.None);
  952. if (textbox_ctrl != null)
  953. Controls.AddImplicit (textbox_ctrl);
  954. LayoutComboBox ();
  955. UpdateComboBoxBounds ();
  956. }
  957. protected override void OnHandleDestroyed (EventArgs e)
  958. {
  959. base.OnHandleDestroyed (e);
  960. }
  961. protected override void OnKeyPress (KeyPressEventArgs e)
  962. {
  963. if (dropdown_style == ComboBoxStyle.DropDownList) {
  964. int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex + 1);
  965. if (index != -1) {
  966. SelectedIndex = index;
  967. if (DroppedDown) { //Scroll into view
  968. if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
  969. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
  970. // Or, selecting an item earlier in the list.
  971. if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
  972. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
  973. }
  974. }
  975. }
  976. base.OnKeyPress (e);
  977. }
  978. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  979. {
  980. MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
  981. if (eh != null)
  982. eh (this, e);
  983. }
  984. protected override void OnParentBackColorChanged (EventArgs e)
  985. {
  986. base.OnParentBackColorChanged (e);
  987. }
  988. protected override void OnResize (EventArgs e)
  989. {
  990. LayoutComboBox ();
  991. if (listbox_ctrl != null)
  992. listbox_ctrl.CalcListBoxArea ();
  993. }
  994. protected override void OnSelectedIndexChanged (EventArgs e)
  995. {
  996. base.OnSelectedIndexChanged (e);
  997. EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
  998. if (eh != null)
  999. eh (this, e);
  1000. }
  1001. protected virtual void OnSelectedItemChanged (EventArgs e)
  1002. {
  1003. }
  1004. protected override void OnSelectedValueChanged (EventArgs e)
  1005. {
  1006. base.OnSelectedValueChanged (e);
  1007. }
  1008. protected virtual void OnSelectionChangeCommitted (EventArgs e)
  1009. {
  1010. EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
  1011. if (eh != null)
  1012. eh (this, e);
  1013. }
  1014. protected override void RefreshItem (int index)
  1015. {
  1016. if (index < 0 || index >= Items.Count)
  1017. throw new ArgumentOutOfRangeException ("index");
  1018. if (draw_mode == DrawMode.OwnerDrawVariable)
  1019. item_heights.Remove (Items [index]);
  1020. }
  1021. protected override void RefreshItems ()
  1022. {
  1023. for (int i = 0; i < Items.Count; i++) {
  1024. RefreshItem (i);
  1025. }
  1026. LayoutComboBox ();
  1027. Refresh ();
  1028. if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
  1029. SetControlText (GetItemText (Items [selected_index]), false);
  1030. }
  1031. public override void ResetText ()
  1032. {
  1033. Text = String.Empty;
  1034. }
  1035. protected override bool ProcessKeyEventArgs (ref Message m)
  1036. {
  1037. return base.ProcessKeyEventArgs (ref m);
  1038. }
  1039. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1040. protected override void OnKeyDown (KeyEventArgs e)
  1041. {
  1042. base.OnKeyDown (e);
  1043. }
  1044. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1045. protected override void OnValidating (CancelEventArgs e)
  1046. {
  1047. base.OnValidating (e);
  1048. }
  1049. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1050. protected override void OnTextChanged (EventArgs e)
  1051. {
  1052. base.OnTextChanged (e);
  1053. }
  1054. protected virtual void OnTextUpdate (EventArgs e)
  1055. {
  1056. EventHandler eh = (EventHandler) Events [TextUpdateEvent];
  1057. if (eh != null)
  1058. eh (this, e);
  1059. }
  1060. protected override void OnMouseLeave (EventArgs e)
  1061. {
  1062. if (flat_style == FlatStyle.Popup)
  1063. Invalidate ();
  1064. base.OnMouseLeave (e);
  1065. }
  1066. protected override void OnMouseEnter (EventArgs e)
  1067. {
  1068. if (flat_style == FlatStyle.Popup)
  1069. Invalidate ();
  1070. base.OnMouseEnter (e);
  1071. }
  1072. protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
  1073. {
  1074. base.ScaleControl (factor, specified);
  1075. }
  1076. public void Select (int start, int length)
  1077. {
  1078. if (start < 0)
  1079. throw new ArgumentException ("Start cannot be less than zero");
  1080. if (length < 0)
  1081. throw new ArgumentException ("length cannot be less than zero");
  1082. if (dropdown_style == ComboBoxStyle.DropDownList)
  1083. return;
  1084. textbox_ctrl.Select (start, length);
  1085. }
  1086. public void SelectAll ()
  1087. {
  1088. if (dropdown_style == ComboBoxStyle.DropDownList)
  1089. return;
  1090. if (textbox_ctrl != null) {
  1091. textbox_ctrl.ShowSelection = true;
  1092. textbox_ctrl.SelectAll ();
  1093. }
  1094. }
  1095. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  1096. {
  1097. bool vertically_anchored = (Anchor & AnchorStyles.Top) != 0 && (Anchor & AnchorStyles.Bottom) != 0;
  1098. bool vertically_docked = Dock == DockStyle.Left || Dock == DockStyle.Right || Dock == DockStyle.Fill;
  1099. if ((specified & BoundsSpecified.Height) != 0 ||
  1100. (specified == BoundsSpecified.None && (vertically_anchored || vertically_docked))) {
  1101. requested_height = height;
  1102. height = SnapHeight (height);
  1103. }
  1104. base.SetBoundsCore (x, y, width, height, specified);
  1105. }
  1106. protected override void SetItemCore (int index, object value)
  1107. {
  1108. if (index < 0 || index >= Items.Count)
  1109. return;
  1110. Items[index] = value;
  1111. }
  1112. protected override void SetItemsCore (IList value)
  1113. {
  1114. BeginUpdate ();
  1115. try {
  1116. Items.Clear ();
  1117. Items.AddRange (value);
  1118. } finally {
  1119. EndUpdate ();
  1120. }
  1121. }
  1122. public override string ToString ()
  1123. {
  1124. return base.ToString () + ", Items.Count:" + Items.Count;
  1125. }
  1126. protected override void WndProc (ref Message m)
  1127. {
  1128. switch ((Msg) m.Msg) {
  1129. case Msg.WM_KEYUP:
  1130. case Msg.WM_KEYDOWN:
  1131. Keys keys = (Keys) m.WParam.ToInt32 ();
  1132. // Don't pass the message to base if auto complete is being used and available.
  1133. if (textbox_ctrl != null && textbox_ctrl.CanNavigateAutoCompleteList) {
  1134. XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
  1135. return;
  1136. }
  1137. if (keys == Keys.Up || keys == Keys.Down)
  1138. break;
  1139. goto case Msg.WM_CHAR;
  1140. case Msg.WM_CHAR:
  1141. // Call our own handler first and send the message to the TextBox if still needed
  1142. if (!ProcessKeyMessage (ref m) && textbox_ctrl != null)
  1143. XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
  1144. return;
  1145. case Msg.WM_MOUSELEAVE:
  1146. Point location = PointToClient (Control.MousePosition);
  1147. if (ClientRectangle.Contains (location))
  1148. return;
  1149. break;
  1150. default:
  1151. break;
  1152. }
  1153. base.WndProc (ref m);
  1154. }
  1155. #endregion Public Methods
  1156. #region Private Methods
  1157. void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
  1158. if(auto_complete_source == AutoCompleteSource.CustomSource) {
  1159. //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
  1160. }
  1161. }
  1162. internal override bool InternalCapture {
  1163. get { return Capture; }
  1164. set {}
  1165. }
  1166. void LayoutComboBox ()
  1167. {
  1168. int border = ThemeEngine.Current.Border3DSize.Width;
  1169. text_area = ClientRectangle;
  1170. text_area.Height = PreferredHeight;
  1171. listbox_area = ClientRectangle;
  1172. listbox_area.Y = text_area.Bottom + 3;
  1173. listbox_area.Height -= (text_area.Height + 2);
  1174. Rectangle prev_button_area = button_area;
  1175. if (DropDownStyle == ComboBoxStyle.Simple)
  1176. button_area = Rectangle.Empty;
  1177. else {
  1178. button_area = text_area;
  1179. button_area.X = text_area.Right - button_width - border;
  1180. button_area.Y = text_area.Y + border;
  1181. button_area.Width = button_width;
  1182. button_area.Height = text_area.Height - 2 * border;
  1183. if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
  1184. button_area.Inflate (1, 1);
  1185. button_area.X += 2;
  1186. button_area.Width -= 2;
  1187. }
  1188. }
  1189. if (button_area != prev_button_area) {
  1190. prev_button_area.Y -= border;
  1191. prev_button_area.Width += border;
  1192. prev_button_area.Height += 2 * border;
  1193. Invalidate (prev_button_area);
  1194. Invalidate (button_area);
  1195. }
  1196. if (textbox_ctrl != null) {
  1197. int text_border = border + 1;
  1198. textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
  1199. textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
  1200. textbox_ctrl.Height = text_area.Height - text_border * 2;
  1201. }
  1202. if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
  1203. listbox_ctrl.Location = listbox_area.Location;
  1204. listbox_ctrl.CalcListBoxArea ();
  1205. }
  1206. }
  1207. private void CreateComboListBox ()
  1208. {
  1209. listbox_ctrl = new ComboListBox (this);
  1210. listbox_ctrl.HighlightedIndex = SelectedIndex;
  1211. }
  1212. internal void Draw (Rectangle clip, Graphics dc)
  1213. {
  1214. Theme theme = ThemeEngine.Current;
  1215. FlatStyle style = FlatStyle.Standard;
  1216. bool is_flat = false;
  1217. style = this.FlatStyle;
  1218. is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
  1219. theme.ComboBoxDrawBackground (this, dc, clip, style);
  1220. int border = theme.Border3DSize.Width;
  1221. // No edit control, we paint the edit ourselves
  1222. if (dropdown_style == ComboBoxStyle.DropDownList) {
  1223. DrawItemState state = DrawItemState.None;
  1224. Color back_color = BackColor;
  1225. Color fore_color = ForeColor;
  1226. Rectangle item_rect = text_area;
  1227. item_rect.X += border;
  1228. item_rect.Y += border;
  1229. item_rect.Width -= (button_area.Width + 2 * border);
  1230. item_rect.Height -= 2 * border;
  1231. if (Focused) {
  1232. state = DrawItemState.Selected;
  1233. state |= DrawItemState.Focus;
  1234. back_color = SystemColors.Highlight;
  1235. fore_color = SystemColors.HighlightText;
  1236. }
  1237. state |= DrawItemState.ComboBoxEdit;
  1238. HandleDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, fore_color, back_color));
  1239. }
  1240. if (show_dropdown_button) {
  1241. ButtonState current_state;
  1242. if (is_enabled)
  1243. current_state = button_state;
  1244. else
  1245. current_state = ButtonState.Inactive;
  1246. if (is_flat || theme.ComboBoxNormalDropDownButtonHasTransparentBackground (this, current_state))
  1247. dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
  1248. if (is_flat) {
  1249. theme.DrawFlatStyleComboButton (dc, button_area, current_state);
  1250. } else {
  1251. theme.ComboBoxDrawNormalDropDownButton (this, dc, clip, button_area, current_state);
  1252. }
  1253. }
  1254. }
  1255. internal bool DropDownButtonEntered {
  1256. get { return drop_down_button_entered; }
  1257. private set {
  1258. if (drop_down_button_entered == value)
  1259. return;
  1260. drop_down_button_entered = value;
  1261. if (ThemeEngine.Current.ComboBoxDropDownButtonHasHotElementStyle (this))
  1262. Invalidate (button_area);
  1263. }
  1264. }
  1265. internal void DropDownListBox ()
  1266. {
  1267. DropDownButtonEntered = false;
  1268. if (DropDownStyle == ComboBoxStyle.Simple)
  1269. return;
  1270. if (listbox_ctrl == null)
  1271. CreateComboListBox ();
  1272. listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
  1273. FindMatchOrSetIndex(SelectedIndex);
  1274. if (textbox_ctrl != null)
  1275. textbox_ctrl.HideAutoCompleteList ();
  1276. if (listbox_ctrl.ShowWindow ())
  1277. dropped_down = true;
  1278. button_state = ButtonState.Pushed;
  1279. if (dropdown_style == ComboBoxStyle.DropDownList)
  1280. Invalidate (text_area);
  1281. }
  1282. internal void DropDownListBoxFinished ()
  1283. {
  1284. if (DropDownStyle == ComboBoxStyle.Simple)
  1285. return;
  1286. FindMatchOrSetIndex (SelectedIndex);
  1287. button_state = ButtonState.Normal;
  1288. Invalidate (button_area);
  1289. dropped_down = false;
  1290. OnDropDownClosed (EventArgs.Empty);
  1291. /*
  1292. * Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
  1293. * this causes the popup to appear under the main form. This is horrible but necessary
  1294. */
  1295. // If the user opens a new form in an event, it will close our dropdown,
  1296. // so we need a null check here
  1297. if (listbox_ctrl != null) {
  1298. listbox_ctrl.Dispose ();
  1299. listbox_ctrl = null;
  1300. }
  1301. // The auto complete list could have been shown after the listbox,
  1302. // so make sure it's hidden.
  1303. if (textbox_ctrl != null)
  1304. textbox_ctrl.HideAutoCompleteList ();
  1305. }
  1306. private int FindStringCaseInsensitive (string search)
  1307. {
  1308. if (search.Length == 0) {
  1309. return -1;
  1310. }
  1311. for (int i = 0; i < Items.Count; i++)
  1312. {
  1313. if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
  1314. return i;
  1315. }
  1316. return -1;
  1317. }
  1318. // Search in the list for the substring, starting the search at the list
  1319. // position specified, the search wraps thus covering all the list.
  1320. internal int FindStringCaseInsensitive (string search, int start_index)
  1321. {
  1322. if (search.Length == 0) {
  1323. return -1;
  1324. }
  1325. // Accept from first item to after last item. i.e. all cases of (SelectedIndex+1).
  1326. if (start_index < 0 || start_index > Items.Count)
  1327. throw new ArgumentOutOfRangeException("start_index");
  1328. for (int i = 0; i < Items.Count; i++) {
  1329. int index = (i + start_index) % Items.Count;
  1330. if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
  1331. return index;
  1332. }
  1333. return -1;
  1334. }
  1335. internal override bool IsInputCharInternal (char charCode)
  1336. {
  1337. return true;
  1338. }
  1339. internal override ContextMenu ContextMenuInternal {
  1340. get {
  1341. return base.ContextMenuInternal;
  1342. }
  1343. set {
  1344. base.ContextMenuInternal = value;
  1345. if (textbox_ctrl != null) {
  1346. textbox_ctrl.ContextMenu = value;
  1347. }
  1348. }
  1349. }
  1350. internal void RestoreContextMenu ()
  1351. {
  1352. textbox_ctrl.RestoreContextMenu ();
  1353. }
  1354. private void OnKeyDownCB(object sender, KeyEventArgs e)
  1355. {
  1356. if (Items.Count == 0)
  1357. return;
  1358. // for keyboard navigation, we have to do our own scroll, since
  1359. // the default behaviour for the SelectedIndex property is a little different,
  1360. // setting the selected index in the top always
  1361. int offset;
  1362. switch (e.KeyCode)
  1363. {
  1364. case Keys.Up:
  1365. FindMatchOrSetIndex(Math.Max(SelectedIndex - 1, 0));
  1366. if (DroppedDown)
  1367. if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
  1368. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
  1369. break;
  1370. case Keys.Down:
  1371. if ((e.Modifiers & Keys.Alt) == Keys.Alt)
  1372. DropDownListBox ();
  1373. else
  1374. FindMatchOrSetIndex(Math.Min(SelectedIndex + 1, Items.Count - 1));
  1375. if (DroppedDown)
  1376. if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
  1377. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
  1378. break;
  1379. case Keys.PageUp:
  1380. offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
  1381. if (offset < 1)
  1382. offset = 1;
  1383. SetSelectedIndex (Math.Max (SelectedIndex - offset, 0), true);
  1384. if (DroppedDown)
  1385. if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
  1386. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
  1387. break;
  1388. case Keys.PageDown:
  1389. if (SelectedIndex == -1) {
  1390. SelectedIndex = 0;
  1391. if (dropdown_style != ComboBoxStyle.Simple)
  1392. return;
  1393. }
  1394. offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
  1395. if (offset < 1)
  1396. offset = 1;
  1397. SetSelectedIndex (Math.Min (SelectedIndex + offset, Items.Count - 1), true);
  1398. if (DroppedDown)
  1399. if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
  1400. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
  1401. break;
  1402. case Keys.Enter:
  1403. case Keys.Escape:
  1404. if (listbox_ctrl != null && listbox_ctrl.Visible)
  1405. DropDownListBoxFinished ();
  1406. break;
  1407. case Keys.Home:
  1408. if (dropdown_style == ComboBoxStyle.DropDownList) {
  1409. SelectedIndex = 0;
  1410. if (DroppedDown)
  1411. if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
  1412. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
  1413. }
  1414. break;
  1415. case Keys.End:
  1416. if (dropdown_style == ComboBoxStyle.DropDownList) {
  1417. SetSelectedIndex (Items.Count - 1, true);
  1418. if (DroppedDown)
  1419. if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
  1420. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
  1421. }
  1422. break;
  1423. default:
  1424. break;
  1425. }
  1426. }
  1427. void SetSelectedIndex (int value, bool supressAutoScroll)
  1428. {
  1429. if (selected_index == value)
  1430. return;
  1431. if (value <= -2 || value >= Items.Count)
  1432. throw new ArgumentOutOfRangeException ("SelectedIndex");
  1433. selected_index = value;
  1434. if (dropdown_style != ComboBoxStyle.DropDownList) {
  1435. if (value == -1)
  1436. SetControlText (string.Empty, false, supressAutoScroll);
  1437. else
  1438. SetControlText (GetItemText (Items [value]), false, supressAutoScroll);
  1439. }
  1440. if (DropDownStyle == ComboBoxStyle.DropDownList)
  1441. Invalidate ();
  1442. if (listbox_ctrl != null)
  1443. listbox_ctrl.HighlightedIndex = value;
  1444. OnSelectedValueChanged (EventArgs.Empty);
  1445. OnSelectedIndexChanged (EventArgs.Empty);
  1446. OnSelectedItemChanged (EventArgs.Empty);
  1447. }
  1448. // If no item is currently selected, and an item is found matching the text
  1449. // in the textbox, then selected that item. Otherwise the item at the given
  1450. // index is selected.
  1451. private void FindMatchOrSetIndex(int index)
  1452. {
  1453. int match = -1;
  1454. if (SelectedIndex == -1 && Text.Length != 0)
  1455. match = FindStringCaseInsensitive(Text);
  1456. if (match != -1)
  1457. SetSelectedIndex (match, true);
  1458. else
  1459. SetSelectedIndex (index, true);
  1460. }
  1461. void OnMouseDownCB (object sender, MouseEventArgs e)
  1462. {
  1463. Rectangle area;
  1464. if (DropDownStyle == ComboBoxStyle.DropDownList)
  1465. area = ClientRectangle;
  1466. else
  1467. area = button_area;
  1468. if (area.Contains (e.X, e.Y)) {
  1469. if (Items.Count > 0)
  1470. DropDownListBox ();
  1471. else {
  1472. button_state = ButtonState.Pushed;
  1473. OnDropDown (EventArgs.Empty);
  1474. }
  1475. Invalidate (button_area);
  1476. Update ();
  1477. }
  1478. Capture = true;
  1479. }
  1480. void OnMouseEnter (object sender, EventArgs e)
  1481. {
  1482. if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this))
  1483. Invalidate ();
  1484. }
  1485. void OnMouseLeave (object sender, EventArgs e)
  1486. {
  1487. if (ThemeEngine.Current.CombBoxBackgroundHasHotElementStyle (this)) {
  1488. drop_down_button_entered = false;
  1489. Invalidate ();
  1490. } else {
  1491. if (show_dropdown_button)
  1492. DropDownButtonEntered = false;
  1493. }
  1494. }
  1495. void OnMouseMoveCB (object sender, MouseEventArgs e)
  1496. {
  1497. if (show_dropdown_button && !dropped_down)
  1498. DropDownButtonEntered = button_area.Contains (e.Location);
  1499. if (DropDownStyle == ComboBoxStyle.Simple)
  1500. return;
  1501. if (listbox_ctrl != null && listbox_ctrl.Visible) {
  1502. Point location = listbox_ctrl.PointToClient (Control.MousePosition);
  1503. if (listbox_ctrl.ClientRectangle.Contains (location))
  1504. listbox_ctrl.Capture = true;
  1505. }
  1506. }
  1507. void OnMouseUpCB (object sender, MouseEventArgs e)
  1508. {
  1509. Capture = false;
  1510. button_state = ButtonState.Normal;
  1511. Invalidate (button_area);
  1512. OnClick (EventArgs.Empty);
  1513. if (dropped_down)
  1514. listbox_ctrl.Capture = true;
  1515. }
  1516. private void OnMouseWheelCB (object sender, MouseEventArgs me)
  1517. {
  1518. if (Items.Count == 0)
  1519. return;
  1520. if (listbox_ctrl != null && listbox_ctrl.Visible) {
  1521. int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
  1522. listbox_ctrl.Scroll (-lines);
  1523. } else {
  1524. int lines = me.Delta / 120;
  1525. int index = SelectedIndex - lines;
  1526. if (index < 0)
  1527. index = 0;
  1528. else if (index >= Items.Count)
  1529. index = Items.Count - 1;
  1530. SelectedIndex = index;
  1531. }
  1532. }
  1533. MouseEventArgs TranslateMouseEventArgs (MouseEventArgs args)
  1534. {
  1535. Point loc = PointToClient (Control.MousePosition);
  1536. return new MouseEventArgs (args.Button, args.Clicks, loc.X, loc.Y, args.Delta);
  1537. }
  1538. internal override void OnPaintInternal (PaintEventArgs pevent)
  1539. {
  1540. if (suspend_ctrlupdate)
  1541. return;
  1542. Draw (ClientRectangle, pevent.Graphics);
  1543. }
  1544. private void OnTextBoxClick (object sender, EventArgs e)
  1545. {
  1546. OnClick (e);
  1547. }
  1548. private void OnTextChangedEdit (object sender, EventArgs e)
  1549. {
  1550. if (process_textchanged_event == false)
  1551. return;
  1552. int item = FindStringCaseInsensitive (textbox_ctrl.Text);
  1553. if (item == -1) {
  1554. // Setting base.Text below will raise this event
  1555. // if we found something
  1556. OnTextChanged (EventArgs.Empty);
  1557. return;
  1558. }
  1559. if (listbox_ctrl != null) {
  1560. // Set as top item
  1561. if (process_texchanged_autoscroll)
  1562. listbox_ctrl.EnsureTop (item);
  1563. }
  1564. base.Text = textbox_ctrl.Text;
  1565. }
  1566. private void OnTextKeyPress (object sender, KeyPressEventArgs e)
  1567. {
  1568. selected_index = -1;
  1569. if (listbox_ctrl != null)
  1570. listbox_ctrl.HighlightedIndex = -1;
  1571. }
  1572. internal void SetControlText (string s, bool suppressTextChanged)
  1573. {
  1574. SetControlText (s, suppressTextChanged, false);
  1575. }
  1576. internal void SetControlText (string s, bool suppressTextChanged, bool supressAutoScroll)
  1577. {
  1578. if (suppressTextChanged)
  1579. process_textchanged_event = false;
  1580. if (supressAutoScroll)
  1581. process_texchanged_autoscroll = false;
  1582. textbox_ctrl.Text = s;
  1583. textbox_ctrl.SelectAll ();
  1584. process_textchanged_event = true;
  1585. process_texchanged_autoscroll = true;
  1586. }
  1587. void UpdateComboBoxBounds ()
  1588. {
  1589. if (requested_height == -1)
  1590. return;
  1591. // Save the requested height since set bounds can destroy it
  1592. int save_height = requested_height;
  1593. SetBounds (bounds.X, bounds.Y, bounds.Width, SnapHeight (requested_height),
  1594. BoundsSpecified.Height);
  1595. requested_height = save_height;
  1596. }
  1597. int SnapHeight (int height)
  1598. {
  1599. if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
  1600. if (IntegralHeight) {
  1601. int border = ThemeEngine.Current.Border3DSize.Height;
  1602. int lb_height = (height - PreferredHeight - 2) - border * 2;
  1603. if (lb_height > ItemHeight) {
  1604. int partial = (lb_height) % ItemHeight;
  1605. height -= partial;
  1606. } else if (lb_height < ItemHeight)
  1607. height = PreferredHeight;
  1608. }
  1609. } else
  1610. height = PreferredHeight;
  1611. return height;
  1612. }
  1613. private void UpdatedItems ()
  1614. {
  1615. if (listbox_ctrl != null) {
  1616. listbox_ctrl.UpdateLastVisibleItem ();
  1617. listbox_ctrl.CalcListBoxArea ();
  1618. listbox_ctrl.Refresh ();
  1619. }
  1620. }
  1621. #endregion Private Methods
  1622. [ListBindableAttribute (false)]
  1623. public class ObjectCollection : IList, ICollection, IEnumerable
  1624. {
  1625. private ComboBox owner;
  1626. internal ArrayList object_items = new ArrayList ();
  1627. #region UIA Framework Events
  1628. //NOTE:
  1629. // We are using Reflection to add/remove internal events.
  1630. // Class ListProvider uses the events.
  1631. //
  1632. //Event used to generate UIA StructureChangedEvent
  1633. static object UIACollectionChangedEvent = new object ();
  1634. internal event CollectionChangeEventHandler UIACollectionChanged {
  1635. add { owner.Events.AddHandler (UIACollectionChangedEvent, value); }
  1636. remove { owner.Events.RemoveHandler (UIACollectionChangedEvent, value); }
  1637. }
  1638. internal void OnUIACollectionChangedEvent (CollectionChangeEventArgs args)
  1639. {
  1640. CollectionChangeEventHandler eh
  1641. = (CollectionChangeEventHandler) owner.Events [UIACollectionChangedEvent];
  1642. if (eh != null)
  1643. eh (owner, args);
  1644. }
  1645. #endregion UIA Framework Events
  1646. public ObjectCollection (ComboBox owner)
  1647. {
  1648. this.owner = owner;
  1649. }
  1650. #region Public Properties
  1651. public int Count {
  1652. get { return object_items.Count; }
  1653. }
  1654. public bool IsReadOnly {
  1655. get { return false; }
  1656. }
  1657. [Browsable (false)]
  1658. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1659. public virtual object this [int index] {
  1660. get {
  1661. if (index < 0 || index >= Count)
  1662. throw new ArgumentOutOfRangeException ("index");
  1663. return object_items[index];
  1664. }
  1665. set {
  1666. if (index < 0 || index >= Count)
  1667. throw new ArgumentOutOfRangeException ("index");
  1668. if (value == null)
  1669. throw new ArgumentNullException ("value");
  1670. //UIA Framework event: Item Removed
  1671. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, object_items [index]));
  1672. object_items[index] = value;
  1673. //UIA Framework event: Item Added
  1674. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, value));
  1675. if (owner.listbox_ctrl != null)
  1676. owner.listbox_ctrl.InvalidateItem (index);
  1677. if (index == owner.SelectedIndex) {
  1678. if (owner.textbox_ctrl == null)
  1679. owner.Refresh ();
  1680. else {
  1681. owner.textbox_ctrl.Text = value.ToString ();
  1682. owner.textbox_ctrl.SelectAll ();
  1683. }
  1684. }
  1685. }
  1686. }
  1687. bool ICollection.IsSynchronized {
  1688. get { return false; }
  1689. }
  1690. object ICollection.SyncRoot {
  1691. get { return this; }
  1692. }
  1693. bool IList.IsFixedSize {
  1694. get { return false; }
  1695. }
  1696. #endregion Public Properties
  1697. #region Public Methods
  1698. public int Add (object item)
  1699. {
  1700. int idx;
  1701. idx = AddItem (item, false);
  1702. owner.UpdatedItems ();
  1703. return idx;
  1704. }
  1705. public void AddRange (object[] items)
  1706. {
  1707. if (items == null)
  1708. throw new ArgumentNullException ("items");
  1709. foreach (object mi in items)
  1710. AddItem (mi, true);
  1711. if (owner.sorted)
  1712. Sort ();
  1713. owner.UpdatedItems ();
  1714. }
  1715. public void Clear ()
  1716. {
  1717. owner.selected_index = -1;
  1718. object_items.Clear ();
  1719. owner.UpdatedItems ();
  1720. owner.Refresh ();
  1721. //UIA Framework event: Items list cleared
  1722. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Refresh, null));
  1723. }
  1724. public bool Contains (object value)
  1725. {
  1726. if (value == null)
  1727. throw new ArgumentNullException ("value");
  1728. return object_items.Contains (value);
  1729. }
  1730. public void CopyTo (object [] destination, int arrayIndex)
  1731. {
  1732. object_items.CopyTo (destination, arrayIndex);
  1733. }
  1734. void ICollection.CopyTo (Array destination, int index)
  1735. {
  1736. object_items.CopyTo (destination, index);
  1737. }
  1738. public IEnumerator GetEnumerator ()
  1739. {
  1740. return object_items.GetEnumerator ();
  1741. }
  1742. int IList.Add (object item)
  1743. {
  1744. return Add (item);
  1745. }
  1746. public int IndexOf (object value)
  1747. {
  1748. if (value == null)
  1749. throw new ArgumentNullException ("value");
  1750. return object_items.IndexOf (value);
  1751. }
  1752. public void Insert (int index, object item)
  1753. {
  1754. if (index < 0 || index > Count)
  1755. throw new ArgumentOutOfRangeException ("index");
  1756. if (item == null)
  1757. throw new ArgumentNullException ("item");
  1758. owner.BeginUpdate ();
  1759. if (owner.Sorted)
  1760. AddItem (item, false);
  1761. else {
  1762. object_items.Insert (index, item);
  1763. //UIA Framework event: Item added
  1764. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
  1765. }
  1766. owner.EndUpdate (); // Calls UpdatedItems
  1767. }
  1768. public void Remove (object value)
  1769. {
  1770. if (value == null)
  1771. return;
  1772. int index = IndexOf (value);
  1773. if (index >= 0)
  1774. RemoveAt (index);
  1775. }
  1776. public void RemoveAt (int index)
  1777. {
  1778. if (index < 0 || index >= Count)
  1779. throw new ArgumentOutOfRangeException ("index");
  1780. if (index < owner.SelectedIndex)
  1781. --owner.SelectedIndex;
  1782. else if (index == owner.SelectedIndex)
  1783. owner.SelectedIndex = -1;
  1784. object removed = object_items [index];
  1785. object_items.RemoveAt (index);
  1786. owner.UpdatedItems ();
  1787. //UIA Framework event: Item removed
  1788. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Remove, removed));
  1789. }
  1790. #endregion Public Methods
  1791. #region Private Methods
  1792. private int AddItem (object item, bool suspend)
  1793. {
  1794. // suspend means do not sort as we put new items in, we will do a
  1795. // big sort at the end
  1796. if (item == null)
  1797. throw new ArgumentNullException ("item");
  1798. if (owner.Sorted && !suspend) {
  1799. int index = 0;
  1800. foreach (object o in object_items) {
  1801. if (String.Compare (item.ToString (), o.ToString ()) < 0) {
  1802. object_items.Insert (index, item);
  1803. // If we added the new item before the selectedindex
  1804. // bump the selectedindex by one, behavior differs if
  1805. // Handle has not been created.
  1806. if (index <= owner.selected_index && owner.IsHandleCreated)
  1807. owner.selected_index++;
  1808. //UIA Framework event: Item added
  1809. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
  1810. return index;
  1811. }
  1812. index++;
  1813. }
  1814. }
  1815. object_items.Add (item);
  1816. //UIA Framework event: Item added
  1817. OnUIACollectionChangedEvent (new CollectionChangeEventArgs (CollectionChangeAction.Add, item));
  1818. return object_items.Count - 1;
  1819. }
  1820. internal void AddRange (IList items)
  1821. {
  1822. foreach (object mi in items)
  1823. AddItem (mi, false);
  1824. if (owner.sorted)
  1825. Sort ();
  1826. owner.UpdatedItems ();
  1827. }
  1828. internal void Sort ()
  1829. {
  1830. // If the objects the user put here don't have their own comparer,
  1831. // use one that compares based on the object's ToString
  1832. if (object_items.Count > 0 && object_items[0] is IComparer)
  1833. object_items.Sort ();
  1834. else
  1835. object_items.Sort (new ObjectComparer (owner));
  1836. }
  1837. private class ObjectComparer : IComparer
  1838. {
  1839. private ListControl owner;
  1840. public ObjectComparer (ListControl owner)
  1841. {
  1842. this.owner = owner;
  1843. }
  1844. #region IComparer Members
  1845. public int Compare (object x, object y)
  1846. {
  1847. return string.Compare (owner.GetItemText (x), owner.GetItemText (y));
  1848. }
  1849. #endregion
  1850. }
  1851. #endregion Private Methods
  1852. }
  1853. internal class ComboTextBox : TextBox {
  1854. private ComboBox owner;
  1855. public ComboTextBox (ComboBox owner)
  1856. {
  1857. this.owner = owner;
  1858. ShowSelection = false;
  1859. owner.EnabledChanged += OwnerEnabledChangedHandler;
  1860. owner.LostFocus += OwnerLostFocusHandler;
  1861. }
  1862. void OwnerEnabledChangedHandler (object o, EventArgs args)
  1863. {
  1864. ShowSelection = owner.Focused && owner.Enabled;
  1865. }
  1866. void OwnerLostFocusHandler (object o, EventArgs args)
  1867. {
  1868. if (IsAutoCompleteAvailable)
  1869. owner.Text = Text;
  1870. }
  1871. protected override void OnKeyDown (KeyEventArgs args)
  1872. {
  1873. if (args.KeyCode == Keys.Enter && IsAutoCompleteAvailable)
  1874. owner.Text = Text;
  1875. base.OnKeyDown (args);
  1876. }
  1877. internal override void OnAutoCompleteValueSelected (EventArgs args)
  1878. {
  1879. base.OnAutoCompleteValueSelected (args);
  1880. owner.Text = Text;
  1881. }
  1882. internal void SetSelectable (bool selectable)
  1883. {
  1884. SetStyle (ControlStyles.Selectable, selectable);
  1885. }
  1886. internal void ActivateCaret (bool active)
  1887. {
  1888. if (active)
  1889. document.CaretHasFocus ();
  1890. else
  1891. document.CaretLostFocus ();
  1892. }
  1893. internal override void OnTextUpdate ()
  1894. {
  1895. base.OnTextUpdate ();
  1896. owner.OnTextUpdate (EventArgs.Empty);
  1897. }
  1898. protected override void OnGotFocus (EventArgs e)
  1899. {
  1900. owner.Select (false, true);
  1901. }
  1902. protected override void OnLostFocus (EventArgs e)
  1903. {
  1904. owner.Select (false, true);
  1905. }
  1906. // We have to pass these events to our owner - MouseMove is not, however.
  1907. protected override void OnMouseDown (MouseEventArgs e)
  1908. {
  1909. base.OnMouseDown (e);
  1910. owner.OnMouseDown (owner.TranslateMouseEventArgs (e));
  1911. }
  1912. protected override void OnMouseUp (MouseEventArgs e)
  1913. {
  1914. base.OnMouseUp (e);
  1915. owner.OnMouseUp (owner.TranslateMouseEventArgs (e));
  1916. }
  1917. protected override void OnMouseClick (MouseEventArgs e)
  1918. {
  1919. base.OnMouseClick (e);
  1920. owner.OnMouseClick (owner.TranslateMouseEventArgs (e));
  1921. }
  1922. protected override void OnMouseDoubleClick (MouseEventArgs e)
  1923. {
  1924. base.OnMouseDoubleClick (e);
  1925. owner.OnMouseDoubleClick (owner.TranslateMouseEventArgs (e));
  1926. }
  1927. public override bool Focused {
  1928. get {
  1929. return owner.Focused;
  1930. }
  1931. }
  1932. internal override bool ActivateOnShow { get { return false; } }
  1933. }
  1934. internal class ComboListBox : Control
  1935. {
  1936. private ComboBox owner;
  1937. private VScrollBarLB vscrollbar_ctrl;
  1938. private int top_item; /* First item that we show the in the current page */
  1939. private int last_item; /* Last visible item */
  1940. internal int page_size; /* Number of listbox items per page */
  1941. private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
  1942. internal enum ItemNavigation
  1943. {
  1944. First,
  1945. Last,
  1946. Next,
  1947. Previous,
  1948. NextPage,
  1949. PreviousPage,
  1950. }
  1951. #region UIA Framework: Properties
  1952. internal int UIATopItem {
  1953. get { return top_item; }
  1954. }
  1955. internal int UIALastItem {
  1956. get { return last_item; }
  1957. }
  1958. internal ScrollBar UIAVScrollBar {
  1959. get { return vscrollbar_ctrl; }
  1960. }
  1961. #endregion
  1962. class VScrollBarLB : VScrollBar
  1963. {
  1964. public VScrollBarLB ()
  1965. {
  1966. }
  1967. internal override bool InternalCapture {
  1968. get { return Capture; }
  1969. set { }
  1970. }
  1971. public void FireMouseDown (MouseEventArgs e)
  1972. {
  1973. if (!Visible)
  1974. return;
  1975. e = TranslateEvent (e);
  1976. OnMouseDown (e);
  1977. }
  1978. public void FireMouseUp (MouseEventArgs e)
  1979. {
  1980. if (!Visible)
  1981. return;
  1982. e = TranslateEvent (e);
  1983. OnMouseUp (e);
  1984. }
  1985. public void FireMouseMove (MouseEventArgs e)
  1986. {
  1987. if (!Visible)
  1988. return;
  1989. e = TranslateEvent (e);
  1990. OnMouseMove (e);
  1991. }
  1992. MouseEventArgs TranslateEvent (MouseEventArgs e)
  1993. {
  1994. Point loc = PointToClient (Control.MousePosition);
  1995. return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
  1996. }
  1997. }
  1998. public ComboListBox (ComboBox owner)
  1999. {
  2000. this.owner = owner;
  2001. top_item = 0;
  2002. last_item = 0;
  2003. page_size = 0;
  2004. MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
  2005. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  2006. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  2007. this.is_visible = false;
  2008. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  2009. InternalBorderStyle = BorderStyle.Fixed3D;
  2010. else
  2011. InternalBorderStyle = BorderStyle.FixedSingle;
  2012. }
  2013. protected override CreateParams CreateParams
  2014. {
  2015. get {
  2016. CreateParams cp = base.CreateParams;
  2017. if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
  2018. return cp;
  2019. cp.Style ^= (int)WindowStyles.WS_CHILD;
  2020. cp.Style ^= (int)WindowStyles.WS_VISIBLE;
  2021. cp.Style |= (int)WindowStyles.WS_POPUP;
  2022. cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
  2023. return cp;
  2024. }
  2025. }
  2026. internal override bool InternalCapture {
  2027. get {
  2028. return Capture;
  2029. }
  2030. set {
  2031. }
  2032. }
  2033. internal override bool ActivateOnShow { get { return false; } }
  2034. #region Private Methods
  2035. // Calcs the listbox area
  2036. internal void CalcListBoxArea ()
  2037. {
  2038. int width, height;
  2039. bool show_scrollbar;
  2040. if (owner.DropDownStyle == ComboBoxStyle.Simple) {
  2041. Rectangle area = owner.listbox_area;
  2042. width = area.Width;
  2043. height = area.Height;
  2044. show_scrollbar = owner.Items.Count * owner.ItemHeight > height;
  2045. // No calculation needed
  2046. if (height <= 0 || width <= 0)
  2047. return;
  2048. }
  2049. else { // DropDown or DropDownList
  2050. width = owner.DropDownWidth;
  2051. int visible_items_count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
  2052. if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
  2053. height = 0;
  2054. for (int i = 0; i < visible_items_count; i++) {
  2055. height += owner.GetItemHeight (i);
  2056. }
  2057. show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
  2058. } else {
  2059. if (owner.DropDownHeight == default_drop_down_height) { // ignore DropDownHeight
  2060. height = owner.ItemHeight * visible_items_count;
  2061. show_scrollbar = owner.Items.Count > owner.MaxDropDownItems;
  2062. } else {
  2063. // ignore visible items count, and use manual height instead
  2064. height = owner.DropDownHeight;
  2065. show_scrollbar = (owner.Items.Count * owner.ItemHeight) > height;
  2066. }
  2067. }
  2068. }
  2069. page_size = Math.Max (height / owner.ItemHeight, 1);
  2070. ComboBoxStyle dropdown_style = owner.DropDownStyle;
  2071. if (!show_scrollbar) {
  2072. if (vscrollbar_ctrl != null)
  2073. vscrollbar_ctrl.Visible = false;
  2074. if (dropdown_style != ComboBoxStyle.Simple)
  2075. height = owner.ItemHeight * owner.items.Count;
  2076. } else {
  2077. /* Need vertical scrollbar */
  2078. if (vscrollbar_ctrl == null) {
  2079. vscrollbar_ctrl = new VScrollBarLB ();
  2080. vscrollbar_ctrl.Minimum = 0;
  2081. vscrollbar_ctrl.SmallChange = 1;
  2082. vscrollbar_ctrl.LargeChange = 1;
  2083. vscrollbar_ctrl.Maximum = 0;
  2084. vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
  2085. Controls.AddImplicit (vscrollbar_ctrl);
  2086. }
  2087. vscrollbar_ctrl.Dock = DockStyle.Right;
  2088. vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
  2089. int large = page_size;
  2090. if (large < 1)
  2091. large = 1;
  2092. vscrollbar_ctrl.LargeChange = large;
  2093. vscrollbar_ctrl.Visible = true;
  2094. int hli = HighlightedIndex;
  2095. if (hli > 0) {
  2096. hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
  2097. vscrollbar_ctrl.Value = hli;
  2098. }
  2099. }
  2100. Size = new Size (width, height);
  2101. textarea_drawable = ClientRectangle;
  2102. textarea_drawable.Width = width;
  2103. textarea_drawable.Height = height;
  2104. if (vscrollbar_ctrl != null && show_scrollbar)
  2105. textarea_drawable.Width -= vscrollbar_ctrl.Width;
  2106. last_item = LastVisibleItem ();
  2107. }
  2108. private void Draw (Rectangle clip, Graphics dc)
  2109. {
  2110. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
  2111. if (owner.Items.Count > 0) {
  2112. for (int i = top_item; i <= last_item; i++) {
  2113. Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
  2114. if (!clip.IntersectsWith (item_rect))
  2115. continue;
  2116. DrawItemState state = DrawItemState.None;
  2117. Color back_color = owner.BackColor;
  2118. Color fore_color = owner.ForeColor;
  2119. if (i == HighlightedIndex) {
  2120. state |= DrawItemState.Selected;
  2121. back_color = SystemColors.Highlight;
  2122. fore_color = SystemColors.HighlightText;
  2123. if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
  2124. state |= DrawItemState.Focus;
  2125. }
  2126. }
  2127. owner.HandleDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
  2128. i, state, fore_color, back_color));
  2129. }
  2130. }
  2131. }
  2132. int highlighted_index = -1;
  2133. public int HighlightedIndex {
  2134. get { return highlighted_index; }
  2135. set {
  2136. if (highlighted_index == value)
  2137. return;
  2138. if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
  2139. Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
  2140. highlighted_index = value;
  2141. if (highlighted_index != -1)
  2142. Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
  2143. }
  2144. }
  2145. private Rectangle GetItemDisplayRectangle (int index, int top_index)
  2146. {
  2147. if (index < 0 || index >= owner.Items.Count)
  2148. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  2149. Rectangle item_rect = new Rectangle ();
  2150. int height = owner.GetItemHeight (index);
  2151. item_rect.X = 0;
  2152. item_rect.Width = textarea_drawable.Width;
  2153. if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
  2154. item_rect.Y = 0;
  2155. for (int i = top_index; i < index; i++)
  2156. item_rect.Y += owner.GetItemHeight (i);
  2157. } else
  2158. item_rect.Y = height * (index - top_index);
  2159. item_rect.Height = height;
  2160. return item_rect;
  2161. }
  2162. public void HideWindow ()
  2163. {
  2164. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  2165. return;
  2166. Capture = false;
  2167. Hide ();
  2168. owner.DropDownListBoxFinished ();
  2169. }
  2170. private int IndexFromPointDisplayRectangle (int x, int y)
  2171. {
  2172. for (int i = top_item; i <= last_item; i++) {
  2173. if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
  2174. return i;
  2175. }
  2176. return -1;
  2177. }
  2178. public void InvalidateItem (int index)
  2179. {
  2180. if (Visible)
  2181. Invalidate (GetItemDisplayRectangle (index, top_item));
  2182. }
  2183. public int LastVisibleItem ()
  2184. {
  2185. Rectangle item_rect;
  2186. int top_y = textarea_drawable.Y + textarea_drawable.Height;
  2187. int i = 0;
  2188. for (i = top_item; i < owner.Items.Count; i++) {
  2189. item_rect = GetItemDisplayRectangle (i, top_item);
  2190. if (item_rect.Y + item_rect.Height > top_y) {
  2191. return i;
  2192. }
  2193. }
  2194. return i - 1;
  2195. }
  2196. public void SetTopItem (int item)
  2197. {
  2198. if (top_item == item)
  2199. return;
  2200. top_item = item;
  2201. UpdateLastVisibleItem ();
  2202. Invalidate ();
  2203. }
  2204. public int FirstVisibleItem ()
  2205. {
  2206. return top_item;
  2207. }
  2208. public void EnsureTop (int item)
  2209. {
  2210. if (owner.Items.Count == 0)
  2211. return;
  2212. if (vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
  2213. return;
  2214. int max = vscrollbar_ctrl.Maximum - page_size + 1;
  2215. if (item > max)
  2216. item = max;
  2217. else if (item < vscrollbar_ctrl.Minimum)
  2218. item = vscrollbar_ctrl.Minimum;
  2219. vscrollbar_ctrl.Value = item;
  2220. }
  2221. bool scrollbar_grabbed = false;
  2222. bool InScrollBar {
  2223. get {
  2224. if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
  2225. return false;
  2226. return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
  2227. }
  2228. }
  2229. protected override void OnMouseDown (MouseEventArgs e)
  2230. {
  2231. if (InScrollBar) {
  2232. vscrollbar_ctrl.FireMouseDown (e);
  2233. scrollbar_grabbed = true;
  2234. }
  2235. }
  2236. protected override void OnMouseMove (MouseEventArgs e)
  2237. {
  2238. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  2239. return;
  2240. if (scrollbar_grabbed || (!Capture && InScrollBar)) {
  2241. vscrollbar_ctrl.FireMouseMove (e);
  2242. return;
  2243. }
  2244. Point pt = PointToClient (Control.MousePosition);
  2245. int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
  2246. if (index != -1)
  2247. HighlightedIndex = index;
  2248. }
  2249. protected override void OnMouseUp (MouseEventArgs e)
  2250. {
  2251. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  2252. if (scrollbar_grabbed) {
  2253. vscrollbar_ctrl.FireMouseUp (e);
  2254. scrollbar_grabbed = false;
  2255. if (index != -1)
  2256. HighlightedIndex = index;
  2257. return;
  2258. }
  2259. if (index == -1) {
  2260. HideWindow ();
  2261. return;
  2262. }
  2263. bool is_change = owner.SelectedIndex != index;
  2264. owner.SetSelectedIndex (index, true);
  2265. owner.OnSelectionChangeCommitted (new EventArgs ());
  2266. // If the user selected the already selected item, SelectedIndex
  2267. // won't fire these events, but .Net does, so we do it here
  2268. if (!is_change) {
  2269. owner.OnSelectedValueChanged (EventArgs.Empty);
  2270. owner.OnSelectedIndexChanged (EventArgs.Empty);
  2271. }
  2272. HideWindow ();
  2273. }
  2274. internal override void OnPaintInternal (PaintEventArgs pevent)
  2275. {
  2276. Draw (pevent.ClipRectangle,pevent.Graphics);
  2277. }
  2278. public bool ShowWindow ()
  2279. {
  2280. if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
  2281. return false;
  2282. HighlightedIndex = owner.SelectedIndex;
  2283. CalcListBoxArea ();
  2284. Show ();
  2285. Refresh ();
  2286. owner.OnDropDown (EventArgs.Empty);
  2287. return true;
  2288. }
  2289. public void UpdateLastVisibleItem ()
  2290. {
  2291. last_item = LastVisibleItem ();
  2292. }
  2293. public void Scroll (int delta)
  2294. {
  2295. if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
  2296. return;
  2297. int max = vscrollbar_ctrl.Maximum - page_size + 1;
  2298. int val = vscrollbar_ctrl.Value + delta;
  2299. if (val > max)
  2300. val = max;
  2301. else if (val < vscrollbar_ctrl.Minimum)
  2302. val = vscrollbar_ctrl.Minimum;
  2303. vscrollbar_ctrl.Value = val;
  2304. }
  2305. private void OnMouseWheelCLB (object sender, MouseEventArgs me)
  2306. {
  2307. if (owner.Items.Count == 0)
  2308. return;
  2309. int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
  2310. Scroll (-lines);
  2311. }
  2312. // Value Changed
  2313. private void VerticalScrollEvent (object sender, EventArgs e)
  2314. {
  2315. if (top_item == vscrollbar_ctrl.Value)
  2316. return;
  2317. top_item = vscrollbar_ctrl.Value;
  2318. UpdateLastVisibleItem ();
  2319. Invalidate ();
  2320. }
  2321. protected override void WndProc(ref Message m) {
  2322. if (m.Msg == (int)Msg.WM_SETFOCUS) {
  2323. owner.Select (false, true);
  2324. }
  2325. base.WndProc (ref m);
  2326. }
  2327. #endregion Private Methods
  2328. }
  2329. }
  2330. }