ComboBox.cs 52 KB

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