ComboBox.cs 51 KB

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