ComboBox.cs 52 KB

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