ComboBox.cs 50 KB

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