PageRenderTime 60ms CodeModel.GetById 21ms 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

Large files files are truncated, but you can click here to view the full file

  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.

Large files files are truncated, but you can click here to view the full file