ComboBox.cs 51 KB

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