ComboBox.cs 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013
  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-2005 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. //
  26. // NOT COMPLETE
  27. using System;
  28. using System.Drawing;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Reflection;
  32. using System.ComponentModel.Design;
  33. using System.ComponentModel.Design.Serialization;
  34. using System.Runtime.InteropServices;
  35. namespace System.Windows.Forms
  36. {
  37. [DefaultProperty("Items")]
  38. [DefaultEvent("SelectedIndexChanged")]
  39. [Designer ("System.Windows.Forms.Design.ComboBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. public class ComboBox : ListControl
  41. {
  42. private DrawMode draw_mode;
  43. private ComboBoxStyle dropdown_style;
  44. private int dropdown_width;
  45. private const int preferred_height = 20;
  46. private int selected_index;
  47. private object selected_item;
  48. internal ObjectCollection items = null;
  49. private bool suspend_ctrlupdate;
  50. private int maxdrop_items;
  51. private bool integral_height;
  52. private bool sorted;
  53. internal ComboBoxInfo combobox_info;
  54. private readonly int def_button_width = 16;
  55. private bool clicked;
  56. private int max_length;
  57. private ComboListBox listbox_ctrl;
  58. private TextBox textbox_ctrl;
  59. private bool process_textchanged_event;
  60. [ComVisible(true)]
  61. public class ChildAccessibleObject : AccessibleObject {
  62. private ComboBox owner;
  63. private IntPtr handle;
  64. public ChildAccessibleObject (ComboBox owner, IntPtr handle) {
  65. this.owner = owner;
  66. this.handle = handle;
  67. }
  68. public override string Name {
  69. get {
  70. return base.Name;
  71. }
  72. }
  73. }
  74. internal class ComboBoxInfo
  75. {
  76. internal int item_height; /* Item's height */
  77. internal Rectangle textarea; /* Rectangle of the editable text area */
  78. internal Rectangle textarea_drawable; /* Rectangle of the editable text area - decorations - button if present*/
  79. internal Rectangle button_rect;
  80. internal bool show_button; /* Is the DropDown button shown? */
  81. internal ButtonState button_status; /* Drop button status */
  82. internal int original_height; /* Control's height is recalculated for not Simple Styles */
  83. internal Rectangle listbox_area; /* ListBox area in Simple combox, not used in the rest */
  84. internal bool droppeddown; /* Is the associated ListBox dropped down? */
  85. internal int combosimple_height; /* Simple default height */
  86. public ComboBoxInfo ()
  87. {
  88. button_status = ButtonState.Normal;
  89. show_button = false;
  90. item_height = 0;
  91. droppeddown = false;
  92. original_height = -1;
  93. combosimple_height = 150;
  94. }
  95. }
  96. internal class ComboBoxItem
  97. {
  98. internal int Index;
  99. internal int ItemHeight; /* Only used for OwnerDrawVariable */
  100. public ComboBoxItem (int index)
  101. {
  102. Index = index;
  103. ItemHeight = -1;
  104. }
  105. }
  106. public ComboBox ()
  107. {
  108. items = new ObjectCollection (this);
  109. listbox_ctrl = null;
  110. textbox_ctrl = null;
  111. combobox_info = new ComboBoxInfo ();
  112. combobox_info.item_height = FontHeight + 2;
  113. dropdown_style = (ComboBoxStyle)(-1);
  114. DropDownStyle = ComboBoxStyle.DropDown;
  115. BackColor = ThemeEngine.Current.ColorWindow;
  116. draw_mode = DrawMode.Normal;
  117. selected_index = -1;
  118. selected_item = null;
  119. maxdrop_items = 8;
  120. suspend_ctrlupdate = false;
  121. clicked = false;
  122. dropdown_width = -1;
  123. max_length = 0;
  124. integral_height = true;
  125. process_textchanged_event = true;
  126. has_focus = false;
  127. /* Events */
  128. MouseDown += new MouseEventHandler (OnMouseDownCB);
  129. MouseUp += new MouseEventHandler (OnMouseUpCB);
  130. MouseMove += new MouseEventHandler (OnMouseMoveCB);
  131. KeyDown +=new KeyEventHandler(OnKeyDownCB);
  132. }
  133. #region events
  134. [Browsable (false)]
  135. [EditorBrowsable (EditorBrowsableState.Never)]
  136. public new event EventHandler BackgroundImageChanged;
  137. public event DrawItemEventHandler DrawItem;
  138. public event EventHandler DropDown;
  139. public event EventHandler DropDownStyleChanged;
  140. public event MeasureItemEventHandler MeasureItem;
  141. [Browsable (false)]
  142. [EditorBrowsable (EditorBrowsableState.Never)]
  143. public new event PaintEventHandler Paint;
  144. public event EventHandler SelectedIndexChanged;
  145. public event EventHandler SelectionChangeCommitted;
  146. #endregion Events
  147. #region Public Properties
  148. public override Color BackColor {
  149. get { return base.BackColor; }
  150. set {
  151. if (base.BackColor == value)
  152. return;
  153. base.BackColor = value;
  154. Refresh ();
  155. }
  156. }
  157. [Browsable (false)]
  158. [EditorBrowsable (EditorBrowsableState.Never)]
  159. public override Image BackgroundImage {
  160. get { return base.BackgroundImage; }
  161. set {
  162. if (base.BackgroundImage == value)
  163. return;
  164. base.BackgroundImage = value;
  165. if (BackgroundImageChanged != null)
  166. BackgroundImageChanged (this, EventArgs.Empty);
  167. Refresh ();
  168. }
  169. }
  170. protected override CreateParams CreateParams {
  171. get { return base.CreateParams;}
  172. }
  173. protected override Size DefaultSize {
  174. get { return new Size (121, PreferredHeight); }
  175. }
  176. [RefreshProperties(RefreshProperties.Repaint)]
  177. [DefaultValue (DrawMode.Normal)]
  178. public DrawMode DrawMode {
  179. get { return draw_mode; }
  180. set {
  181. if (!Enum.IsDefined (typeof (DrawMode), value))
  182. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
  183. if (draw_mode == value)
  184. return;
  185. draw_mode = value;
  186. Refresh ();
  187. }
  188. }
  189. [DefaultValue (ComboBoxStyle.DropDown)]
  190. [RefreshProperties(RefreshProperties.Repaint)]
  191. public ComboBoxStyle DropDownStyle {
  192. get { return dropdown_style; }
  193. set {
  194. if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
  195. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
  196. if (dropdown_style == value)
  197. return;
  198. if (dropdown_style == ComboBoxStyle.Simple) {
  199. if (listbox_ctrl != null) {
  200. Controls.RemoveImplicit (listbox_ctrl);
  201. listbox_ctrl.Dispose ();
  202. listbox_ctrl = null;
  203. Height = combobox_info.original_height;
  204. }
  205. }
  206. if (dropdown_style != ComboBoxStyle.DropDownList && value == ComboBoxStyle.DropDownList) {
  207. if (textbox_ctrl != null) {
  208. Controls.RemoveImplicit (textbox_ctrl);
  209. textbox_ctrl.Dispose ();
  210. textbox_ctrl = null;
  211. }
  212. }
  213. dropdown_style = value;
  214. if (dropdown_style == ComboBoxStyle.Simple) {
  215. CBoxInfo.show_button = false;
  216. combobox_info.original_height = Height;
  217. Height = combobox_info.combosimple_height;
  218. CreateComboListBox ();
  219. if (IsHandleCreated == true) {
  220. Controls.AddImplicit (listbox_ctrl);
  221. }
  222. }
  223. else {
  224. CBoxInfo.show_button = true;
  225. CBoxInfo.button_status = ButtonState.Normal;
  226. }
  227. if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
  228. textbox_ctrl = new FixedSizeTextBox ();
  229. textbox_ctrl.BorderStyle = BorderStyle.None;
  230. textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
  231. textbox_ctrl.KeyPress += new KeyPressEventHandler(textbox_ctrl_KeyPress);
  232. textbox_ctrl.KeyDown += new KeyEventHandler (OnKeyDownCB);
  233. textbox_ctrl.GotFocus += new EventHandler(textbox_ctrl_GotFocus);
  234. textbox_ctrl.LostFocus += new EventHandler(textbox_ctrl_LostFocus);
  235. if (IsHandleCreated == true) {
  236. Controls.AddImplicit (textbox_ctrl);
  237. }
  238. }
  239. if (DropDownStyleChanged != null)
  240. DropDownStyleChanged (this, EventArgs.Empty);
  241. CalcTextArea ();
  242. Refresh ();
  243. }
  244. }
  245. public int DropDownWidth {
  246. get {
  247. if (dropdown_width == -1)
  248. return Width;
  249. return dropdown_width;
  250. }
  251. set {
  252. if (dropdown_width == value)
  253. return;
  254. if (value < 1)
  255. throw new ArgumentException ("The DropDownWidth value is less than one");
  256. dropdown_width = value;
  257. }
  258. }
  259. [Browsable (false)]
  260. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  261. public bool DroppedDown {
  262. get {
  263. if (dropdown_style == ComboBoxStyle.Simple)
  264. return true;
  265. return CBoxInfo.droppeddown;
  266. }
  267. set {
  268. if (dropdown_style == ComboBoxStyle.Simple)
  269. return;
  270. if (value == true) {
  271. DropDownListBox ();
  272. }
  273. else {
  274. listbox_ctrl.Hide ();
  275. }
  276. if (DropDown != null)
  277. DropDown (this, EventArgs.Empty);
  278. }
  279. }
  280. public override bool Focused {
  281. get { return base.Focused; }
  282. }
  283. public override Color ForeColor {
  284. get { return base.ForeColor; }
  285. set {
  286. if (base.ForeColor == value)
  287. return;
  288. base.ForeColor = value;
  289. Refresh ();
  290. }
  291. }
  292. [DefaultValue (true)]
  293. [Localizable (true)]
  294. public bool IntegralHeight {
  295. get { return integral_height; }
  296. set {
  297. if (integral_height == value)
  298. return;
  299. integral_height = value;
  300. Refresh ();
  301. }
  302. }
  303. [Localizable (true)]
  304. public int ItemHeight {
  305. get { return combobox_info.item_height; }
  306. set {
  307. if (value < 0)
  308. throw new ArgumentException ("The item height value is less than zero");
  309. combobox_info.item_height = value;
  310. CalcTextArea ();
  311. Refresh ();
  312. }
  313. }
  314. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  315. [Localizable (true)]
  316. [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  317. public ComboBox.ObjectCollection Items {
  318. get { return items; }
  319. }
  320. [DefaultValue (8)]
  321. [Localizable (true)]
  322. public int MaxDropDownItems {
  323. get { return maxdrop_items; }
  324. set {
  325. if (maxdrop_items == value)
  326. return;
  327. maxdrop_items = value;
  328. }
  329. }
  330. [DefaultValue (0)]
  331. [Localizable (true)]
  332. public int MaxLength {
  333. get { return max_length; }
  334. set {
  335. if (max_length == value)
  336. return;
  337. max_length = value;
  338. if (dropdown_style != ComboBoxStyle.DropDownList) {
  339. if (value < 0) {
  340. value = 0;
  341. }
  342. textbox_ctrl.MaxLength = value;
  343. }
  344. }
  345. }
  346. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  347. [Browsable (false)]
  348. public int PreferredHeight {
  349. get { return preferred_height; }
  350. }
  351. [Browsable (false)]
  352. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  353. public override int SelectedIndex {
  354. get { return selected_index; }
  355. set {
  356. if (value <= -2 || value >= Items.Count)
  357. throw new ArgumentOutOfRangeException ("Index of out range");
  358. if (selected_index == value)
  359. return;
  360. selected_index = value;
  361. if (dropdown_style != ComboBoxStyle.DropDownList) {
  362. SetControlText (GetItemText (Items[selected_index]));
  363. }
  364. OnSelectedIndexChanged (new EventArgs ());
  365. OnSelectedValueChanged (new EventArgs ());
  366. OnSelectedItemChanged (new EventArgs ());
  367. Refresh ();
  368. }
  369. }
  370. [Browsable (false)]
  371. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  372. [Bindable(true)]
  373. public object SelectedItem {
  374. get {
  375. if (selected_index !=-1 && Items !=null && Items.Count > 0)
  376. return Items[selected_index];
  377. else
  378. return null;
  379. }
  380. set {
  381. int index = Items.IndexOf (value);
  382. if (index == -1)
  383. return;
  384. if (selected_index == index)
  385. return;
  386. selected_index = index;
  387. if (dropdown_style != ComboBoxStyle.DropDownList) {
  388. SetControlText (GetItemText (Items[selected_index]));
  389. }
  390. OnSelectedItemChanged (new EventArgs ());
  391. Refresh ();
  392. }
  393. }
  394. [Browsable (false)]
  395. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  396. public string SelectedText {
  397. get {
  398. if (dropdown_style == ComboBoxStyle.DropDownList)
  399. return "";
  400. return textbox_ctrl.SelectedText;
  401. }
  402. set {
  403. if (dropdown_style == ComboBoxStyle.DropDownList) {
  404. return;
  405. }
  406. textbox_ctrl.SelectedText = value;
  407. }
  408. }
  409. [Browsable (false)]
  410. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  411. public int SelectionLength {
  412. get {
  413. if (dropdown_style == ComboBoxStyle.DropDownList)
  414. return 0;
  415. return textbox_ctrl.SelectionLength;
  416. }
  417. set {
  418. if (dropdown_style == ComboBoxStyle.DropDownList)
  419. return;
  420. if (textbox_ctrl.SelectionLength == value)
  421. return;
  422. textbox_ctrl.SelectionLength = value;
  423. }
  424. }
  425. [Browsable (false)]
  426. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  427. public int SelectionStart {
  428. get {
  429. if (dropdown_style == ComboBoxStyle.DropDownList)
  430. return 0;
  431. return textbox_ctrl.SelectionStart;
  432. }
  433. set {
  434. if (dropdown_style == ComboBoxStyle.DropDownList)
  435. return;
  436. if (textbox_ctrl.SelectionStart == value)
  437. return;
  438. textbox_ctrl.SelectionStart = value;
  439. }
  440. }
  441. [DefaultValue (false)]
  442. public bool Sorted {
  443. get { return sorted; }
  444. set {
  445. if (sorted == value)
  446. return;
  447. sorted = value;
  448. }
  449. }
  450. [Bindable (true)]
  451. [Localizable (true)]
  452. public override string Text {
  453. get {
  454. if (dropdown_style != ComboBoxStyle.DropDownList) {
  455. if (textbox_ctrl != null) {
  456. return textbox_ctrl.Text;
  457. }
  458. }
  459. if (SelectedItem != null) {
  460. return GetItemText (SelectedItem);
  461. }
  462. return base.Text;
  463. }
  464. set {
  465. if (value == null) {
  466. SelectedIndex = -1;
  467. return;
  468. }
  469. int index = FindString (value);
  470. if (index != -1) {
  471. SelectedIndex = index;
  472. return;
  473. }
  474. if (dropdown_style != ComboBoxStyle.DropDownList) {
  475. textbox_ctrl.Text = GetItemText (value);
  476. }
  477. }
  478. }
  479. #endregion Public Properties
  480. #region Private Properties
  481. internal ComboBoxInfo CBoxInfo {
  482. get { return combobox_info; }
  483. }
  484. #endregion Private Properties
  485. #region Public Methods
  486. protected virtual void AddItemsCore (object[] value)
  487. {
  488. }
  489. public void BeginUpdate ()
  490. {
  491. suspend_ctrlupdate = true;
  492. }
  493. protected override void Dispose (bool disposing)
  494. {
  495. if (disposing == true) {
  496. if (listbox_ctrl != null) {
  497. listbox_ctrl.Dispose ();
  498. Controls.RemoveImplicit (listbox_ctrl);
  499. listbox_ctrl = null;
  500. }
  501. if (textbox_ctrl != null) {
  502. Controls.RemoveImplicit (textbox_ctrl);
  503. textbox_ctrl.Dispose ();
  504. textbox_ctrl = null;
  505. }
  506. }
  507. base.Dispose (disposing);
  508. }
  509. public void EndUpdate ()
  510. {
  511. suspend_ctrlupdate = false;
  512. UpdatedItems ();
  513. }
  514. public int FindString (string s)
  515. {
  516. return FindString (s, -1);
  517. }
  518. public int FindString (string s, int startIndex)
  519. {
  520. if (Items.Count == 0)
  521. return -1; // No exception throwing if empty
  522. if (startIndex < -1 || startIndex >= Items.Count - 1)
  523. throw new ArgumentOutOfRangeException ("Index of out range");
  524. startIndex++;
  525. for (int i = startIndex; i < Items.Count; i++) {
  526. if ((GetItemText (Items[i])).StartsWith (s))
  527. return i;
  528. }
  529. return -1;
  530. }
  531. public int FindStringExact (string s)
  532. {
  533. return FindStringExact (s, -1);
  534. }
  535. public int FindStringExact (string s, int startIndex)
  536. {
  537. if (Items.Count == 0)
  538. return -1; // No exception throwing if empty
  539. if (startIndex < -1 || startIndex >= Items.Count - 1)
  540. throw new ArgumentOutOfRangeException ("Index of out range");
  541. startIndex++;
  542. for (int i = startIndex; i < Items.Count; i++) {
  543. if ((GetItemText (Items[i])).Equals (s))
  544. return i;
  545. }
  546. return -1;
  547. }
  548. public int GetItemHeight (int index)
  549. {
  550. if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated == true) {
  551. if (index < 0 || index >= Items.Count )
  552. throw new ArgumentOutOfRangeException ("The item height value is less than zero");
  553. if ((Items.GetComboBoxItem (index)).ItemHeight != -1) {
  554. return (Items.GetComboBoxItem (index)).ItemHeight;
  555. }
  556. MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
  557. OnMeasureItem (args);
  558. (Items.GetComboBoxItem (index)).ItemHeight = args.ItemHeight;
  559. return args.ItemHeight;
  560. }
  561. return ItemHeight;
  562. }
  563. protected override bool IsInputKey (Keys keyData)
  564. {
  565. switch (keyData) {
  566. case Keys.Up:
  567. case Keys.Down:
  568. case Keys.PageUp:
  569. case Keys.PageDown:
  570. return true;
  571. default:
  572. return false;
  573. }
  574. }
  575. protected override void OnBackColorChanged (EventArgs e)
  576. {
  577. base.OnBackColorChanged (e);
  578. }
  579. protected override void OnDataSourceChanged (EventArgs e)
  580. {
  581. base.OnDataSourceChanged (e);
  582. BindDataItems (items);
  583. if (DataSource == null || DataManager == null) {
  584. SelectedIndex = -1;
  585. }
  586. else {
  587. SelectedIndex = DataManager.Position;
  588. }
  589. }
  590. protected override void OnDisplayMemberChanged (EventArgs e)
  591. {
  592. base.OnDisplayMemberChanged (e);
  593. if (DataManager == null || !IsHandleCreated)
  594. return;
  595. BindDataItems (items);
  596. SelectedIndex = DataManager.Position;
  597. }
  598. protected virtual void OnDrawItem (DrawItemEventArgs e)
  599. {
  600. if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable)) {
  601. DrawItem (this, e);
  602. return;
  603. }
  604. ThemeEngine.Current.DrawComboBoxItem (this, e);
  605. }
  606. protected virtual void OnDropDown (EventArgs e)
  607. {
  608. if (DropDown != null)
  609. DropDown (this, e);
  610. }
  611. protected virtual void OnDropDownStyleChanged (EventArgs e)
  612. {
  613. if (DropDownStyleChanged != null)
  614. DropDownStyleChanged (this, e);
  615. }
  616. protected override void OnFontChanged (EventArgs e)
  617. {
  618. base.OnFontChanged (e);
  619. if (textbox_ctrl != null) {
  620. textbox_ctrl.Font = Font;
  621. }
  622. combobox_info.item_height = FontHeight + 2;
  623. CalcTextArea ();
  624. }
  625. protected override void OnForeColorChanged (EventArgs e)
  626. {
  627. base.OnForeColorChanged (e);
  628. }
  629. [EditorBrowsable(EditorBrowsableState.Advanced)]
  630. protected override void OnGotFocus (EventArgs e) {
  631. has_focus = true;
  632. Invalidate ();
  633. }
  634. [EditorBrowsable(EditorBrowsableState.Advanced)]
  635. protected override void OnLostFocus (EventArgs e) {
  636. has_focus = false;
  637. Invalidate ();
  638. }
  639. protected override void OnHandleCreated (EventArgs e)
  640. {
  641. base.OnHandleCreated (e);
  642. if (listbox_ctrl != null) {
  643. Controls.AddImplicit (listbox_ctrl);
  644. if (dropdown_style == ComboBoxStyle.Simple)
  645. Height = combobox_info.combosimple_height;
  646. }
  647. if (textbox_ctrl != null) {
  648. Controls.AddImplicit (textbox_ctrl);
  649. }
  650. CalcTextArea ();
  651. }
  652. protected override void OnHandleDestroyed (EventArgs e)
  653. {
  654. base.OnHandleDestroyed (e);
  655. }
  656. protected override void OnKeyPress (KeyPressEventArgs e)
  657. {
  658. base.OnKeyPress (e);
  659. }
  660. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  661. {
  662. if (MeasureItem != null)
  663. MeasureItem (this, e);
  664. }
  665. protected override void OnParentBackColorChanged (EventArgs e)
  666. {
  667. base.OnParentBackColorChanged (e);
  668. }
  669. protected override void OnResize (EventArgs e)
  670. {
  671. base.OnResize (e);
  672. AdjustHeightForDropDown ();
  673. if (listbox_ctrl != null)
  674. listbox_ctrl.CalcListBoxArea ();
  675. CalcTextArea ();
  676. }
  677. protected override void OnSelectedIndexChanged (EventArgs e)
  678. {
  679. base.OnSelectedIndexChanged (e);
  680. if (SelectedIndexChanged != null)
  681. SelectedIndexChanged (this, e);
  682. }
  683. protected virtual void OnSelectedItemChanged (EventArgs e)
  684. {
  685. }
  686. protected override void OnSelectedValueChanged (EventArgs e)
  687. {
  688. base.OnSelectedValueChanged (e);
  689. }
  690. protected virtual void OnSelectionChangeCommitted (EventArgs e)
  691. {
  692. if (SelectionChangeCommitted != null)
  693. SelectionChangeCommitted (this, e);
  694. }
  695. protected override void RefreshItem (int index)
  696. {
  697. if (index < 0 || index >= Items.Count)
  698. throw new ArgumentOutOfRangeException ("Index of out range");
  699. if (draw_mode == DrawMode.OwnerDrawVariable) {
  700. (Items.GetComboBoxItem (index)).ItemHeight = -1;
  701. }
  702. }
  703. public void Select (int start, int lenght)
  704. {
  705. if (start < 0)
  706. throw new ArgumentException ("Start cannot be less than zero");
  707. if (lenght < 0)
  708. throw new ArgumentException ("Start cannot be less than zero");
  709. if (dropdown_style == ComboBoxStyle.DropDownList)
  710. return;
  711. textbox_ctrl.Select (start, lenght);
  712. }
  713. public void SelectAll ()
  714. {
  715. if (dropdown_style == ComboBoxStyle.DropDownList)
  716. return;
  717. textbox_ctrl.SelectAll ();
  718. }
  719. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  720. {
  721. base.SetBoundsCore (x, y, width, height, specified);
  722. }
  723. protected override void SetItemCore (int index, object value)
  724. {
  725. if (index < 0 || index >= Items.Count)
  726. return;
  727. Items[index] = value;
  728. }
  729. protected override void SetItemsCore (IList value)
  730. {
  731. Items.AddRange (value);
  732. }
  733. public override string ToString ()
  734. {
  735. return base.ToString () + ", Items.Count:" + Items.Count;
  736. }
  737. protected override void WndProc (ref Message m)
  738. {
  739. switch ((Msg) m.Msg) {
  740. case Msg.WM_PAINT: {
  741. PaintEventArgs paint_event;
  742. paint_event = XplatUI.PaintEventStart (Handle, true);
  743. OnPaintCB (paint_event);
  744. XplatUI.PaintEventEnd (Handle, true);
  745. return;
  746. }
  747. case Msg.WM_ERASEBKGND:
  748. m.Result = (IntPtr) 1;
  749. return;
  750. default:
  751. break;
  752. }
  753. base.WndProc (ref m);
  754. }
  755. #endregion Public Methods
  756. #region Private Methods
  757. private void AdjustHeightForDropDown ()
  758. {
  759. if (dropdown_style == ComboBoxStyle.Simple)
  760. return;
  761. int new_height = combobox_info.item_height + ThemeEngine.Current.DrawComboBoxEditDecorationTop () +
  762. ThemeEngine.Current.DrawComboBoxEditDecorationBottom () + 2;
  763. if (Height == new_height)
  764. return;
  765. combobox_info.original_height = Height;
  766. Height = new_height;
  767. }
  768. private void textbox_ctrl_KeyPress(object sender, KeyPressEventArgs e)
  769. {
  770. OnKeyPress (e);
  771. }
  772. private void textbox_ctrl_GotFocus(object sender, EventArgs e )
  773. {
  774. OnGotFocus(e);
  775. }
  776. private void textbox_ctrl_LostFocus(object sender, EventArgs e )
  777. {
  778. OnLostFocus(e);
  779. }
  780. // Calcs the text area size
  781. internal void CalcTextArea ()
  782. {
  783. combobox_info.textarea = ClientRectangle;
  784. /* Edit area */
  785. combobox_info.textarea.Height = ItemHeight + ThemeEngine.Current.DrawComboBoxEditDecorationTop () +
  786. ThemeEngine.Current.DrawComboBoxEditDecorationBottom () + 2;
  787. // TODO: Does the +2 change at different font resolutions?
  788. /* Edit area - minus decorations (text drawable area) */
  789. combobox_info.textarea_drawable = combobox_info.textarea;
  790. combobox_info.textarea_drawable.Y += ThemeEngine.Current.DrawComboBoxEditDecorationTop ();
  791. combobox_info.textarea_drawable.X += ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
  792. combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationBottom ();
  793. combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationTop();
  794. combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationRight ();
  795. combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
  796. /* Non-drawable area */
  797. Region area = new Region (ClientRectangle);
  798. area.Exclude (combobox_info.textarea);
  799. RectangleF bounds = area.GetBounds (DeviceContext);
  800. combobox_info.listbox_area = new Rectangle ((int)bounds.X, (int)bounds.Y,
  801. (int)bounds.Width, (int)bounds.Height);
  802. if (CBoxInfo.show_button) {
  803. combobox_info.textarea_drawable.Width -= def_button_width;
  804. combobox_info.button_rect = new Rectangle (combobox_info.textarea_drawable.X + combobox_info.textarea_drawable.Width,
  805. combobox_info.textarea_drawable.Y, def_button_width, combobox_info.textarea_drawable.Height);
  806. }
  807. if (dropdown_style != ComboBoxStyle.DropDownList) { /* There is an edit control*/
  808. if (textbox_ctrl != null) {
  809. textbox_ctrl.Location = new Point (combobox_info.textarea_drawable.X, combobox_info.textarea_drawable.Y);
  810. textbox_ctrl.Size = new Size (combobox_info.textarea_drawable.Width, combobox_info.textarea_drawable.Height);
  811. }
  812. }
  813. if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
  814. listbox_ctrl.Location = new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
  815. combobox_info.textarea.Height);
  816. listbox_ctrl.CalcListBoxArea ();
  817. }
  818. area.Dispose ();
  819. }
  820. private void CreateComboListBox ()
  821. {
  822. listbox_ctrl = new ComboListBox (this);
  823. }
  824. internal void Draw (Rectangle clip, Graphics dc)
  825. {
  826. // No edit control, we paint the edit ourselfs
  827. if (dropdown_style == ComboBoxStyle.DropDownList) {
  828. DrawItemState state = DrawItemState.None;
  829. Rectangle item_rect = combobox_info.textarea_drawable;
  830. item_rect.Height = ItemHeight + 2;
  831. if (has_focus == true) {
  832. state = DrawItemState.Selected;
  833. state |= DrawItemState.Focus;
  834. }
  835. state |= DrawItemState.ComboBoxEdit;
  836. OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect,
  837. selected_index, state, ForeColor, BackColor));
  838. }
  839. if (DropDownStyle == ComboBoxStyle.Simple && clip.IntersectsWith (combobox_info.listbox_area) == true) {
  840. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (Parent.BackColor),
  841. combobox_info.listbox_area);
  842. }
  843. if (CBoxInfo.show_button) {
  844. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorControl),
  845. combobox_info.button_rect);
  846. ThemeEngine.Current.CPDrawComboButton (dc,
  847. combobox_info.button_rect, combobox_info.button_status);
  848. }
  849. ThemeEngine.Current.DrawComboBoxEditDecorations (dc, this, combobox_info.textarea);
  850. }
  851. internal void DropDownListBox ()
  852. {
  853. if (DropDownStyle == ComboBoxStyle.Simple)
  854. return;
  855. if (listbox_ctrl == null) {
  856. CreateComboListBox ();
  857. }
  858. listbox_ctrl.Location = PointToScreen (new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
  859. combobox_info.textarea.Height));
  860. if (listbox_ctrl.ShowWindow () == true) {
  861. CBoxInfo.droppeddown = true;
  862. }
  863. combobox_info.button_status = ButtonState.Pushed;
  864. if (dropdown_style == ComboBoxStyle.DropDownList) {
  865. Invalidate (combobox_info.textarea_drawable);
  866. }
  867. }
  868. internal void DropDownListBoxFinished ()
  869. {
  870. if (DropDownStyle == ComboBoxStyle.Simple)
  871. return;
  872. combobox_info.button_status = ButtonState.Normal;
  873. Invalidate (combobox_info.button_rect);
  874. CBoxInfo.droppeddown = false;
  875. clicked = false;
  876. }
  877. private int FindStringCaseInsensitive (string search)
  878. {
  879. if (search.Length == 0) {
  880. return -1;
  881. }
  882. for (int i = 0; i < Items.Count; i++)
  883. {
  884. if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
  885. return i;
  886. }
  887. return -1;
  888. }
  889. private void OnKeyDownCB(object sender, KeyEventArgs e)
  890. {
  891. switch (e.KeyCode)
  892. {
  893. case Keys.Up:
  894. SelectedIndex = Math.Max(SelectedIndex-1, 0);
  895. break;
  896. case Keys.Down:
  897. SelectedIndex = Math.Min(SelectedIndex+1, Items.Count-1);
  898. break;
  899. case Keys.PageUp:
  900. if (listbox_ctrl != null)
  901. SelectedIndex = Math.Max(SelectedIndex- (listbox_ctrl.page_size-1), 0);
  902. break;
  903. case Keys.PageDown:
  904. if (listbox_ctrl != null)
  905. SelectedIndex = Math.Min(SelectedIndex+(listbox_ctrl.page_size-1), Items.Count-1);
  906. break;
  907. default:
  908. break;
  909. }
  910. }
  911. internal virtual void OnMouseDownCB (object sender, MouseEventArgs e)
  912. {
  913. /* Click On button*/
  914. Rectangle hit_rect;
  915. if (dropdown_style == ComboBoxStyle.DropDownList) {
  916. hit_rect = combobox_info.textarea;
  917. } else {
  918. hit_rect = combobox_info.button_rect;
  919. }
  920. if (hit_rect.Contains (e.X, e.Y)) {
  921. if (clicked == false) {
  922. clicked = true;
  923. DropDownListBox ();
  924. } else {
  925. listbox_ctrl.Hide ();
  926. DropDownListBoxFinished ();
  927. }
  928. Invalidate (combobox_info.button_rect);
  929. }
  930. }
  931. internal virtual void OnMouseMoveCB (object sender, MouseEventArgs e)
  932. {
  933. /* When there are no items, act as a regular button */
  934. if (clicked == true && Items.Count == 0 &&
  935. combobox_info.button_rect.Contains (e.X, e.Y) == false) {
  936. DropDownListBoxFinished ();
  937. }
  938. }
  939. internal virtual void OnMouseUpCB (object sender, MouseEventArgs e)
  940. {
  941. /* Click on button*/
  942. if (clicked == true && combobox_info.button_rect.Contains (e.X, e.Y)) {
  943. DropDownListBoxFinished ();
  944. }
  945. }
  946. private void OnPaintCB (PaintEventArgs pevent)
  947. {
  948. if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
  949. return;
  950. /* Copies memory drawing buffer to screen*/
  951. Draw (ClientRectangle, pevent.Graphics);
  952. if (Paint != null)
  953. Paint (this, pevent);
  954. }
  955. private void OnTextChangedEdit (object sender, EventArgs e)
  956. {
  957. if (process_textchanged_event == false)
  958. return;
  959. int item = FindStringCaseInsensitive (textbox_ctrl.Text);
  960. if (item == -1)
  961. return;
  962. if (listbox_ctrl != null) {
  963. listbox_ctrl.SetTopItem (item);
  964. listbox_ctrl.SetHighLightedItem (Items[item]);
  965. }
  966. }
  967. internal void SetControlText (string s)
  968. {
  969. process_textchanged_event = false;
  970. textbox_ctrl.Text = s;
  971. process_textchanged_event = true;
  972. }
  973. private void UpdatedItems ()
  974. {
  975. if (listbox_ctrl != null) {
  976. listbox_ctrl.UpdateLastVisibleItem ();
  977. listbox_ctrl.CalcListBoxArea ();
  978. listbox_ctrl.Refresh ();
  979. }
  980. }
  981. #endregion Private Methods
  982. /*
  983. ComboBox.ObjectCollection
  984. */
  985. [ListBindableAttribute (false)]
  986. public class ObjectCollection : IList, ICollection, IEnumerable
  987. {
  988. private ComboBox owner;
  989. internal ArrayList object_items = new ArrayList ();
  990. internal ArrayList combobox_items = new ArrayList ();
  991. public ObjectCollection (ComboBox owner)
  992. {
  993. this.owner = owner;
  994. }
  995. #region Public Properties
  996. public virtual int Count {
  997. get { return object_items.Count; }
  998. }
  999. public virtual bool IsReadOnly {
  1000. get { return false; }
  1001. }
  1002. [Browsable (false)]
  1003. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1004. public virtual object this [int index] {
  1005. get {
  1006. if (index < 0 || index >= Count)
  1007. throw new ArgumentOutOfRangeException ("Index of out range");
  1008. return object_items[index];
  1009. }
  1010. set {
  1011. if (index < 0 || index >= Count)
  1012. throw new ArgumentOutOfRangeException ("Index of out range");
  1013. object_items[index] = value;
  1014. }
  1015. }
  1016. bool ICollection.IsSynchronized {
  1017. get { return false; }
  1018. }
  1019. object ICollection.SyncRoot {
  1020. get { return this; }
  1021. }
  1022. bool IList.IsFixedSize {
  1023. get { return false; }
  1024. }
  1025. #endregion Public Properties
  1026. #region Private Properties
  1027. internal ArrayList ObjectItems {
  1028. get { return object_items;}
  1029. set {
  1030. object_items = value;
  1031. }
  1032. }
  1033. internal ArrayList ListBoxItems {
  1034. get { return combobox_items;}
  1035. set {
  1036. combobox_items = value;
  1037. }
  1038. }
  1039. #endregion Private Properties
  1040. #region Public Methods
  1041. public int Add (object item)
  1042. {
  1043. int idx;
  1044. idx = AddItem (item);
  1045. owner.UpdatedItems ();
  1046. return idx;
  1047. }
  1048. public void AddRange (object[] items)
  1049. {
  1050. foreach (object mi in items)
  1051. AddItem (mi);
  1052. owner.UpdatedItems ();
  1053. }
  1054. public virtual void Clear ()
  1055. {
  1056. owner.selected_index = -1;
  1057. object_items.Clear ();
  1058. combobox_items.Clear ();
  1059. owner.UpdatedItems ();
  1060. owner.Refresh ();
  1061. }
  1062. public virtual bool Contains (object obj)
  1063. {
  1064. return object_items.Contains (obj);
  1065. }
  1066. public void CopyTo (object[] dest, int arrayIndex)
  1067. {
  1068. object_items.CopyTo (dest, arrayIndex);
  1069. }
  1070. void ICollection.CopyTo (Array dest, int index)
  1071. {
  1072. object_items.CopyTo (dest, index);
  1073. }
  1074. public virtual IEnumerator GetEnumerator ()
  1075. {
  1076. return object_items.GetEnumerator ();
  1077. }
  1078. int IList.Add (object item)
  1079. {
  1080. return Add (item);
  1081. }
  1082. public virtual int IndexOf (object value)
  1083. {
  1084. return object_items.IndexOf (value);
  1085. }
  1086. public virtual void Insert (int index, object item)
  1087. {
  1088. if (index < 0 || index > Count)
  1089. throw new ArgumentOutOfRangeException ("Index of out range");
  1090. ObjectCollection new_items = new ObjectCollection (owner);
  1091. object sel_item = owner.SelectedItem;
  1092. owner.BeginUpdate ();
  1093. for (int i = 0; i < index; i++) {
  1094. new_items.AddItem (ObjectItems[i]);
  1095. }
  1096. new_items.AddItem (item);
  1097. for (int i = index; i < Count; i++){
  1098. new_items.AddItem (ObjectItems[i]);
  1099. }
  1100. ObjectItems = new_items.ObjectItems;
  1101. ListBoxItems = new_items.ListBoxItems;
  1102. if (sel_item != null) {
  1103. int idx = IndexOf (sel_item);
  1104. owner.selected_index = idx;
  1105. owner.listbox_ctrl.SetHighLightedItem (owner.Items[idx]);
  1106. }
  1107. owner.EndUpdate (); // Calls UpdatedItems
  1108. }
  1109. public virtual void Remove (object value)
  1110. {
  1111. if (IndexOf (value) == owner.SelectedIndex)
  1112. owner.SelectedItem = null;
  1113. RemoveAt (IndexOf (value));
  1114. }
  1115. public virtual void RemoveAt (int index)
  1116. {
  1117. if (index < 0 || index >= Count)
  1118. throw new ArgumentOutOfRangeException ("Index of out range");
  1119. if (index == owner.SelectedIndex)
  1120. owner.SelectedItem = null;
  1121. object_items.RemoveAt (index);
  1122. combobox_items.RemoveAt (index);
  1123. owner.UpdatedItems ();
  1124. }
  1125. #endregion Public Methods
  1126. #region Private Methods
  1127. private int AddItem (object item)
  1128. {
  1129. int cnt = object_items.Count;
  1130. object_items.Add (item);
  1131. combobox_items.Add (new ComboBox.ComboBoxItem (cnt));
  1132. return cnt;
  1133. }
  1134. internal void AddRange (IList items)
  1135. {
  1136. foreach (object mi in items)
  1137. AddItem (mi);
  1138. owner.UpdatedItems ();
  1139. }
  1140. internal ComboBox.ComboBoxItem GetComboBoxItem (int index)
  1141. {
  1142. if (index < 0 || index >= Count)
  1143. throw new ArgumentOutOfRangeException ("Index of out range");
  1144. return (ComboBox.ComboBoxItem) combobox_items[index];
  1145. }
  1146. internal void SetComboBoxItem (ComboBox.ComboBoxItem item, int index)
  1147. {
  1148. if (index < 0 || index >= Count)
  1149. throw new ArgumentOutOfRangeException ("Index of out range");
  1150. combobox_items[index] = item;
  1151. }
  1152. #endregion Private Methods
  1153. }
  1154. /*
  1155. class ComboListBox
  1156. */
  1157. internal class ComboListBox : Control
  1158. {
  1159. private ComboBox owner;
  1160. private VScrollBarLB vscrollbar_ctrl;
  1161. private int top_item; /* First item that we show the in the current page */
  1162. private int last_item; /* Last visible item */
  1163. public object highlighted_item; /* Item that is currently selected */
  1164. internal int page_size; /* Number of listbox items per page */
  1165. private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
  1166. internal enum ItemNavigation
  1167. {
  1168. First,
  1169. Last,
  1170. Next,
  1171. Previous,
  1172. NextPage,
  1173. PreviousPage,
  1174. }
  1175. class VScrollBarLB : VScrollBar
  1176. {
  1177. public VScrollBarLB ()
  1178. {
  1179. }
  1180. public void FireMouseDown (MouseEventArgs e)
  1181. {
  1182. OnMouseDown (e);
  1183. }
  1184. public void FireMouseUp (MouseEventArgs e)
  1185. {
  1186. OnMouseUp (e);
  1187. }
  1188. public void FireMouseMove (MouseEventArgs e)
  1189. {
  1190. OnMouseMove (e);
  1191. }
  1192. }
  1193. public ComboListBox (ComboBox owner) : base ()
  1194. {
  1195. this.owner = owner;
  1196. top_item = 0;
  1197. last_item = 0;
  1198. page_size = 0;
  1199. highlighted_item = null;
  1200. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  1201. MouseUp += new MouseEventHandler (OnMouseUpPUW);
  1202. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  1203. KeyDown += new KeyEventHandler (OnKeyDownPUW);
  1204. Paint += new PaintEventHandler (OnPaintPUW);
  1205. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  1206. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  1207. }
  1208. protected override CreateParams CreateParams
  1209. {
  1210. get {
  1211. CreateParams cp = base.CreateParams;
  1212. if (owner != null && owner.DropDownStyle != ComboBoxStyle.Simple) {
  1213. cp.Style ^= (int) WindowStyles.WS_CHILD;
  1214. cp.Style |= (int) WindowStyles.WS_POPUP;
  1215. cp.ExStyle |= (int)(WindowStyles.WS_EX_TOOLWINDOW | WindowStyles.WS_EX_TOPMOST);
  1216. }
  1217. return cp;
  1218. }
  1219. }
  1220. #region Private Methods
  1221. // Calcs the listbox area
  1222. internal void CalcListBoxArea ()
  1223. {
  1224. int width, height;
  1225. int item_height = owner.ItemHeight;
  1226. bool show_scrollbar = false;
  1227. if (owner.DropDownStyle == ComboBoxStyle.Simple) {
  1228. width = owner.CBoxInfo.listbox_area.Width;
  1229. height = owner.CBoxInfo.listbox_area.Height;
  1230. if (owner.IntegralHeight == true) {
  1231. int remaining = (height -
  1232. ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
  1233. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle)) %
  1234. (item_height - 2);
  1235. if (remaining > 0) {
  1236. height -= remaining;
  1237. }
  1238. }
  1239. }
  1240. else { // DropDown or DropDownList
  1241. width = owner.DropDownWidth;
  1242. int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
  1243. if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
  1244. height = 0;
  1245. for (int i = 0; i < count; i++) {
  1246. height += owner.GetItemHeight (i);
  1247. }
  1248. } else {
  1249. height = (item_height - 2) * count;
  1250. }
  1251. height += ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
  1252. height += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  1253. }
  1254. if (owner.Items.Count <= owner.MaxDropDownItems) {
  1255. /* Does not need vertical scrollbar*/
  1256. if (vscrollbar_ctrl != null) {
  1257. vscrollbar_ctrl.Visible = false;
  1258. }
  1259. }
  1260. else {
  1261. /* Need vertical scrollbar */
  1262. if (vscrollbar_ctrl == null) {
  1263. vscrollbar_ctrl = new VScrollBarLB ();
  1264. vscrollbar_ctrl.Minimum = 0;
  1265. vscrollbar_ctrl.SmallChange = 1;
  1266. vscrollbar_ctrl.LargeChange = 1;
  1267. vscrollbar_ctrl.Maximum = 0;
  1268. vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
  1269. Controls.AddImplicit (vscrollbar_ctrl);
  1270. }
  1271. vscrollbar_ctrl.Height = height - ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
  1272. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  1273. vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle),
  1274. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle));
  1275. vscrollbar_ctrl.Maximum = owner.Items.Count - (owner.DropDownStyle == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items);
  1276. show_scrollbar = vscrollbar_ctrl.Visible = true;
  1277. int hli = GetHighLightedIndex ();
  1278. if (hli > 0) {
  1279. hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
  1280. vscrollbar_ctrl.Value = hli;
  1281. }
  1282. }
  1283. Size = new Size (width, height);
  1284. textarea_drawable = ClientRectangle;
  1285. textarea_drawable.Width = width;
  1286. textarea_drawable.Height = height;
  1287. // Exclude decorations
  1288. textarea_drawable.X += ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
  1289. textarea_drawable.Y += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  1290. textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
  1291. textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
  1292. textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
  1293. textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  1294. if (vscrollbar_ctrl != null && show_scrollbar)
  1295. textarea_drawable.Width -= vscrollbar_ctrl.Width;
  1296. last_item = LastVisibleItem ();
  1297. page_size = textarea_drawable.Height / (item_height - 2);
  1298. }
  1299. private void Draw (Rectangle clip, Graphics dc)
  1300. {
  1301. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  1302. (owner.BackColor), clip);
  1303. if (owner.Items.Count > 0) {
  1304. Rectangle item_rect;
  1305. DrawItemState state = DrawItemState.None;
  1306. for (int i = top_item; i <= last_item; i++) {
  1307. item_rect = GetItemDisplayRectangle (i, top_item);
  1308. if (clip.IntersectsWith (item_rect) == false)
  1309. continue;
  1310. /* Draw item */
  1311. state = DrawItemState.None;
  1312. if (i == GetHighLightedIndex () ) {
  1313. state |= DrawItemState.Selected;
  1314. if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
  1315. state |= DrawItemState.Focus;
  1316. }
  1317. }
  1318. owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
  1319. i, state, owner.ForeColor, owner.BackColor));
  1320. }
  1321. }
  1322. ThemeEngine.Current.DrawComboListBoxDecorations (dc, owner, ClientRectangle);
  1323. }
  1324. public int GetHighLightedIndex ()
  1325. {
  1326. return owner.Items.IndexOf (highlighted_item);
  1327. }
  1328. public object GetHighLightedItem ()
  1329. {
  1330. return highlighted_item;
  1331. }
  1332. private Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  1333. {
  1334. if (index < 0 || index >= owner.Items.Count)
  1335. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  1336. Rectangle item_rect = new Rectangle ();
  1337. int height = owner.GetItemHeight (index);
  1338. item_rect.X = ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
  1339. item_rect.Width = textarea_drawable.Width;
  1340. item_rect.Y = ((height - 2) * (index - first_displayble));
  1341. if (owner.dropdown_style == ComboBoxStyle.Simple)
  1342. item_rect.Y += 2;
  1343. item_rect.Height = height;
  1344. return item_rect;
  1345. }
  1346. public void HideWindow ()
  1347. {
  1348. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  1349. return;
  1350. Capture = false;
  1351. Hide ();
  1352. highlighted_item = -1;
  1353. owner.DropDownListBoxFinished ();
  1354. }
  1355. private int IndexFromPointDisplayRectangle (int x, int y)
  1356. {
  1357. for (int i = top_item; i <= last_item; i++) {
  1358. if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
  1359. return i;
  1360. }
  1361. return -1;
  1362. }
  1363. protected override bool IsInputKey (Keys keyData)
  1364. {
  1365. return owner.IsInputKey (keyData);
  1366. }
  1367. private int LastVisibleItem ()
  1368. {
  1369. Rectangle item_rect;
  1370. int top_y = textarea_drawable.Y + textarea_drawable.Height;
  1371. int i = 0;
  1372. for (i = top_item; i < owner.Items.Count; i++) {
  1373. item_rect = GetItemDisplayRectangle (i, top_item);
  1374. if (item_rect.Y + item_rect.Height > top_y) {
  1375. return i;
  1376. }
  1377. }
  1378. return i - 1;
  1379. }
  1380. private void NavigateItemVisually (ItemNavigation navigation)
  1381. {
  1382. int item = -1;
  1383. switch (navigation) {
  1384. case ItemNavigation.Next: {
  1385. if (GetHighLightedIndex () + 1 < owner.Items.Count) {
  1386. if (GetHighLightedIndex () + 1 > last_item) {
  1387. top_item++;
  1388. vscrollbar_ctrl.Value = top_item;
  1389. }
  1390. item = GetHighLightedIndex () + 1;
  1391. }
  1392. break;
  1393. }
  1394. case ItemNavigation.Previous: {
  1395. if (GetHighLightedIndex () > 0) {
  1396. if (GetHighLightedIndex () - 1 < top_item) {
  1397. top_item--;
  1398. vscrollbar_ctrl.Value = top_item;
  1399. }
  1400. item = GetHighLightedIndex () - 1;
  1401. }
  1402. break;
  1403. }
  1404. case ItemNavigation.NextPage: {
  1405. if (GetHighLightedIndex () + page_size - 1 >= owner.Items.Count) {
  1406. top_item = owner.Items.Count - page_size;
  1407. vscrollbar_ctrl.Value = top_item;
  1408. item = owner.Items.Count - 1;
  1409. }
  1410. else {
  1411. if (GetHighLightedIndex () + page_size - 1 > last_item) {
  1412. top_item = GetHighLightedIndex ();
  1413. vscrollbar_ctrl.Value = GetHighLightedIndex ();
  1414. }
  1415. item = GetHighLightedIndex () + page_size - 1;
  1416. }
  1417. break;
  1418. }
  1419. case ItemNavigation.PreviousPage: {
  1420. /* Go to the first item*/
  1421. if (GetHighLightedIndex () - (page_size - 1) <= 0) {
  1422. top_item = 0;
  1423. vscrollbar_ctrl.Value = top_item;
  1424. item = 0;
  1425. }
  1426. else { /* One page back */
  1427. if (GetHighLightedIndex () - (page_size - 1) < top_item) {
  1428. top_item = GetHighLightedIndex () - (page_size - 1);
  1429. vscrollbar_ctrl.Value = top_item;
  1430. }
  1431. item = GetHighLightedIndex () - (page_size - 1);
  1432. }
  1433. break;
  1434. }
  1435. default:
  1436. break;
  1437. }
  1438. if (item != -1) {
  1439. SetHighLightedItem (owner.Items[item]);
  1440. owner.OnSelectionChangeCommitted (new EventArgs ());
  1441. if (owner.DropDownStyle == ComboBoxStyle.Simple) {
  1442. owner.SetControlText (owner.GetItemText (owner.Items[item]));
  1443. }
  1444. }
  1445. }
  1446. private void OnKeyDownPUW (object sender, KeyEventArgs e)
  1447. {
  1448. switch (e.KeyCode) {
  1449. case Keys.Up:
  1450. NavigateItemVisually (ItemNavigation.Previous);
  1451. break;
  1452. case Keys.Down:
  1453. NavigateItemVisually (ItemNavigation.Next);
  1454. break;
  1455. case Keys.PageUp:
  1456. NavigateItemVisually (ItemNavigation.PreviousPage);
  1457. break;
  1458. case Keys.PageDown:
  1459. NavigateItemVisually (ItemNavigation.NextPage);
  1460. break;
  1461. default:
  1462. break;
  1463. }
  1464. }
  1465. public void SetHighLightedItem (object item)
  1466. {
  1467. Rectangle invalidate = Rectangle.Empty;
  1468. int hli;
  1469. if (GetHighLightedItem () == item)
  1470. return;
  1471. /* Previous item */
  1472. hli = GetHighLightedIndex ();
  1473. if (hli != -1) {
  1474. invalidate = GetItemDisplayRectangle (hli, top_item);
  1475. }
  1476. highlighted_item = item;
  1477. if (highlighted_item != null) {
  1478. /* Current item */
  1479. invalidate = Rectangle.Union (invalidate,
  1480. GetItemDisplayRectangle (GetHighLightedIndex (), top_item));
  1481. }
  1482. if (ClientRectangle.IntersectsWith (invalidate))
  1483. Invalidate (invalidate);
  1484. }
  1485. public void SetTopItem (int item)
  1486. {
  1487. top_item = item;
  1488. UpdateLastVisibleItem ();
  1489. Refresh ();
  1490. }
  1491. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  1492. {
  1493. Rectangle scrollbar_screenrect;
  1494. Point mouse_screen, scrollbar_screen;
  1495. mouse_screen = PointToScreen (new Point (e.X, e.Y));
  1496. /* Click on an element ? */
  1497. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  1498. if (index != -1) {
  1499. owner.SelectedIndex = index;
  1500. SetHighLightedItem (owner.Items[index]);
  1501. owner.OnSelectionChangeCommitted (new EventArgs ());
  1502. HideWindow ();
  1503. return;
  1504. }
  1505. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  1506. return;
  1507. /* Reroute event to scrollbar */
  1508. if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
  1509. scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
  1510. scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
  1511. scrollbar_screenrect.X = scrollbar_screen.X;
  1512. scrollbar_screenrect.Y = scrollbar_screen.Y;
  1513. if (scrollbar_screenrect.Contains (mouse_screen)){
  1514. Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
  1515. vscrollbar_ctrl.FireMouseDown (new MouseEventArgs (e.Button, e.Clicks,
  1516. pnt_client.X, pnt_client.Y, e.Delta));
  1517. } else { /* Click in a non-client area*/
  1518. HideWindow ();
  1519. }
  1520. } else { /* Click in a non-client area*/
  1521. HideWindow ();
  1522. }
  1523. }
  1524. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  1525. {
  1526. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  1527. return;
  1528. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  1529. if (index != -1) {
  1530. SetHighLightedItem (owner.Items[index]);
  1531. return;
  1532. }
  1533. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  1534. return;
  1535. /* Reroute event to scrollbar */
  1536. if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
  1537. Rectangle scrollbar_screenrect;
  1538. Point mouse_screen, scrollbar_screen;
  1539. mouse_screen = PointToScreen (new Point (e.X, e.Y));
  1540. scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
  1541. scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
  1542. scrollbar_screenrect.X = scrollbar_screen.X;
  1543. scrollbar_screenrect.Y = scrollbar_screen.Y;
  1544. if (scrollbar_screenrect.Contains (mouse_screen)){
  1545. Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
  1546. vscrollbar_ctrl.FireMouseMove (new MouseEventArgs (e.Button, e.Clicks,
  1547. pnt_client.X, pnt_client.Y, e.Delta));
  1548. }
  1549. }
  1550. }
  1551. private void OnMouseUpPUW (object sender, MouseEventArgs e)
  1552. {
  1553. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  1554. return;
  1555. /* Reroute event to scrollbar */
  1556. Rectangle scrollbar_screenrect;
  1557. Point mouse_screen, scrollbar_screen;
  1558. mouse_screen = PointToScreen (new Point (e.X, e.Y));
  1559. if (vscrollbar_ctrl != null && vscrollbar_ctrl.Visible == true) {
  1560. scrollbar_screenrect = vscrollbar_ctrl.ClientRectangle;
  1561. scrollbar_screen = PointToScreen (vscrollbar_ctrl.Location);
  1562. scrollbar_screenrect.X = scrollbar_screen.X;
  1563. scrollbar_screenrect.Y = scrollbar_screen.Y;
  1564. if (scrollbar_screenrect.Contains (mouse_screen)){
  1565. Point pnt_client = vscrollbar_ctrl.PointToClient (mouse_screen);
  1566. vscrollbar_ctrl.FireMouseUp (new MouseEventArgs (e.Button, e.Clicks,
  1567. pnt_client.X, pnt_client.Y, e.Delta));
  1568. }
  1569. }
  1570. }
  1571. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  1572. {
  1573. Draw (pevent.ClipRectangle,pevent.Graphics);
  1574. }
  1575. public bool ShowWindow ()
  1576. {
  1577. if (owner.DropDownStyle != ComboBoxStyle.Simple && owner.Items.Count == 0)
  1578. return false;
  1579. SetHighLightedItem (owner.SelectedItem);
  1580. int index = GetHighLightedIndex ();
  1581. CalcListBoxArea ();
  1582. Show ();
  1583. if (owner.DropDownStyle != ComboBoxStyle.Simple) {
  1584. Capture = true;
  1585. }
  1586. Refresh ();
  1587. if (owner.DropDown != null) {
  1588. owner.DropDown (owner, EventArgs.Empty);
  1589. }
  1590. return true;
  1591. }
  1592. public void UpdateLastVisibleItem ()
  1593. {
  1594. last_item = LastVisibleItem ();
  1595. }
  1596. // Value Changed
  1597. private void VerticalScrollEvent (object sender, EventArgs e)
  1598. {
  1599. if (top_item == vscrollbar_ctrl.Value)
  1600. return;
  1601. top_item = vscrollbar_ctrl.Value;
  1602. UpdateLastVisibleItem ();
  1603. Refresh ();
  1604. }
  1605. #endregion Private Methods
  1606. }
  1607. }
  1608. }