ComboBox.cs 50 KB

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