ComboBox.cs 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578
  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-2006 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. // Mike Kestner <[email protected]>
  25. // Daniel Nauck (dna(at)mono-project(dot)de)
  26. using System;
  27. using System.Collections;
  28. using System.ComponentModel;
  29. using System.ComponentModel.Design;
  30. using System.ComponentModel.Design.Serialization;
  31. using System.Drawing;
  32. using System.Globalization;
  33. using System.Reflection;
  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. #if NET_2_0
  41. [DefaultBindingProperty ("Text")]
  42. [ClassInterface (ClassInterfaceType.AutoDispatch)]
  43. [ComVisible(true)]
  44. #endif
  45. public class ComboBox : ListControl
  46. {
  47. private DrawMode draw_mode = DrawMode.Normal;
  48. private ComboBoxStyle dropdown_style;
  49. private int dropdown_width = -1;
  50. private int selected_index = -1;
  51. private ObjectCollection items;
  52. private bool suspend_ctrlupdate;
  53. private int maxdrop_items = 8;
  54. private bool integral_height = true;
  55. private bool sorted;
  56. private int max_length;
  57. private ComboListBox listbox_ctrl;
  58. private ComboTextBox textbox_ctrl;
  59. private bool process_textchanged_event = true;
  60. private bool item_height_specified;
  61. private int item_height;
  62. private int requested_height = -1;
  63. private Hashtable item_heights;
  64. private bool show_dropdown_button;
  65. private ButtonState button_state = ButtonState.Normal;
  66. private bool dropped_down;
  67. private Rectangle text_area;
  68. private Rectangle button_area;
  69. private Rectangle listbox_area;
  70. private const int button_width = 16;
  71. bool drop_down_button_entered;
  72. #if NET_2_0
  73. private AutoCompleteStringCollection auto_complete_custom_source = null;
  74. private AutoCompleteMode auto_complete_mode = AutoCompleteMode.None;
  75. private AutoCompleteSource auto_complete_source = AutoCompleteSource.None;
  76. private int drop_down_height;
  77. private FlatStyle flat_style;
  78. #endif
  79. [ComVisible(true)]
  80. public class ChildAccessibleObject : AccessibleObject {
  81. public ChildAccessibleObject (ComboBox owner, IntPtr handle)
  82. : base (owner)
  83. {
  84. }
  85. public override string Name {
  86. get {
  87. return base.Name;
  88. }
  89. }
  90. }
  91. public ComboBox ()
  92. {
  93. items = new ObjectCollection (this);
  94. DropDownStyle = ComboBoxStyle.DropDown;
  95. item_height = FontHeight + 2;
  96. background_color = ThemeEngine.Current.ColorWindow;
  97. border_style = BorderStyle.None;
  98. #if NET_2_0
  99. drop_down_height = 106;
  100. flat_style = FlatStyle.Standard;
  101. #endif
  102. /* Events */
  103. MouseDown += new MouseEventHandler (OnMouseDownCB);
  104. MouseUp += new MouseEventHandler (OnMouseUpCB);
  105. MouseMove += new MouseEventHandler (OnMouseMoveCB);
  106. MouseWheel += new MouseEventHandler (OnMouseWheelCB);
  107. MouseLeave += new EventHandler (OnMouseLeave);
  108. KeyDown +=new KeyEventHandler(OnKeyDownCB);
  109. }
  110. #region events
  111. [Browsable (false)]
  112. [EditorBrowsable (EditorBrowsableState.Never)]
  113. public new event EventHandler BackgroundImageChanged {
  114. add { base.BackgroundImageChanged += value; }
  115. remove { base.BackgroundImageChanged -= value; }
  116. }
  117. #if NET_2_0
  118. [Browsable (false)]
  119. [EditorBrowsable (EditorBrowsableState.Never)]
  120. public new event EventHandler BackgroundImageLayoutChanged
  121. {
  122. add { base.BackgroundImageLayoutChanged += value; }
  123. remove { base.BackgroundImageLayoutChanged -= value; }
  124. }
  125. [Browsable (false)]
  126. [EditorBrowsable (EditorBrowsableState.Never)]
  127. public new event EventHandler DoubleClick
  128. {
  129. add { base.DoubleClick += value; }
  130. remove { base.DoubleClick -= value; }
  131. }
  132. #endif
  133. static object DrawItemEvent = new object ();
  134. static object DropDownEvent = new object ();
  135. static object DropDownStyleChangedEvent = new object ();
  136. static object MeasureItemEvent = new object ();
  137. static object SelectedIndexChangedEvent = new object ();
  138. static object SelectionChangeCommittedEvent = new object ();
  139. #if NET_2_0
  140. static object DropDownClosedEvent = new object ();
  141. static object TextUpdateEvent = new object ();
  142. #endif
  143. public event DrawItemEventHandler DrawItem {
  144. add { Events.AddHandler (DrawItemEvent, value); }
  145. remove { Events.RemoveHandler (DrawItemEvent, value); }
  146. }
  147. public event EventHandler DropDown {
  148. add { Events.AddHandler (DropDownEvent, value); }
  149. remove { Events.RemoveHandler (DropDownEvent, value); }
  150. }
  151. #if NET_2_0
  152. public event EventHandler DropDownClosed
  153. {
  154. add { Events.AddHandler (DropDownClosedEvent, value); }
  155. remove { Events.RemoveHandler (DropDownClosedEvent, value); }
  156. }
  157. #endif
  158. public event EventHandler DropDownStyleChanged {
  159. add { Events.AddHandler (DropDownStyleChangedEvent, value); }
  160. remove { Events.RemoveHandler (DropDownStyleChangedEvent, value); }
  161. }
  162. public event MeasureItemEventHandler MeasureItem {
  163. add { Events.AddHandler (MeasureItemEvent, value); }
  164. remove { Events.RemoveHandler (MeasureItemEvent, value); }
  165. }
  166. #if NET_2_0
  167. [Browsable (false)]
  168. [EditorBrowsable (EditorBrowsableState.Never)]
  169. public new event EventHandler PaddingChanged
  170. {
  171. add { base.PaddingChanged += value; }
  172. remove { base.PaddingChanged -= value; }
  173. }
  174. #endif
  175. [Browsable (false)]
  176. [EditorBrowsable (EditorBrowsableState.Never)]
  177. public new event PaintEventHandler Paint {
  178. add { base.Paint += value; }
  179. remove { base.Paint -= value; }
  180. }
  181. public event EventHandler SelectedIndexChanged {
  182. add { Events.AddHandler (SelectedIndexChangedEvent, value); }
  183. remove { Events.RemoveHandler (SelectedIndexChangedEvent, value); }
  184. }
  185. public event EventHandler SelectionChangeCommitted {
  186. add { Events.AddHandler (SelectionChangeCommittedEvent, value); }
  187. remove { Events.RemoveHandler (SelectionChangeCommittedEvent, value); }
  188. }
  189. #if NET_2_0
  190. public event EventHandler TextUpdate
  191. {
  192. add { Events.AddHandler (TextUpdateEvent, value); }
  193. remove { Events.RemoveHandler (TextUpdateEvent, value); }
  194. }
  195. #endif
  196. #endregion Events
  197. #region Public Properties
  198. #if NET_2_0
  199. [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
  200. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  201. [Browsable (true)]
  202. [EditorBrowsable (EditorBrowsableState.Always)]
  203. [Localizable (true)]
  204. [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design,
  205. "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  206. public AutoCompleteStringCollection AutoCompleteCustomSource {
  207. get {
  208. if(auto_complete_custom_source == null) {
  209. auto_complete_custom_source = new AutoCompleteStringCollection ();
  210. auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
  211. }
  212. return auto_complete_custom_source;
  213. }
  214. set {
  215. if(auto_complete_custom_source == value)
  216. return;
  217. if(auto_complete_custom_source != null) //remove eventhandler from old collection
  218. auto_complete_custom_source.CollectionChanged -= new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
  219. auto_complete_custom_source = value;
  220. if(auto_complete_custom_source != null)
  221. auto_complete_custom_source.CollectionChanged += new CollectionChangeEventHandler (OnAutoCompleteCustomSourceChanged);
  222. }
  223. }
  224. [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
  225. [Browsable (true)]
  226. [EditorBrowsable (EditorBrowsableState.Always)]
  227. [DefaultValue (AutoCompleteMode.None)]
  228. public AutoCompleteMode AutoCompleteMode {
  229. get { return auto_complete_mode; }
  230. set {
  231. if(auto_complete_mode == value)
  232. return;
  233. if((value < AutoCompleteMode.None) || (value > AutoCompleteMode.SuggestAppend))
  234. throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteMode", value));
  235. auto_complete_mode = value;
  236. }
  237. }
  238. [MonoTODO("AutoCompletion algorithm is currently not implemented.")]
  239. [Browsable (true)]
  240. [EditorBrowsable (EditorBrowsableState.Always)]
  241. [DefaultValue (AutoCompleteSource.None)]
  242. public AutoCompleteSource AutoCompleteSource {
  243. get { return auto_complete_source; }
  244. set {
  245. if(auto_complete_source == value)
  246. return;
  247. if(!Enum.IsDefined (typeof (AutoCompleteSource), value))
  248. throw new InvalidEnumArgumentException (Locale.GetText ("Enum argument value '{0}' is not valid for AutoCompleteSource", value));
  249. auto_complete_source = value;
  250. }
  251. }
  252. #endif
  253. public override Color BackColor {
  254. get { return base.BackColor; }
  255. set {
  256. if (base.BackColor == value)
  257. return;
  258. base.BackColor = value;
  259. Refresh ();
  260. }
  261. }
  262. [Browsable (false)]
  263. [EditorBrowsable (EditorBrowsableState.Never)]
  264. public override Image BackgroundImage {
  265. get { return base.BackgroundImage; }
  266. set {
  267. if (base.BackgroundImage == value)
  268. return;
  269. base.BackgroundImage = value;
  270. Refresh ();
  271. }
  272. }
  273. #if NET_2_0
  274. [Browsable (false)]
  275. [EditorBrowsable (EditorBrowsableState.Never)]
  276. public override ImageLayout BackgroundImageLayout {
  277. get { return base.BackgroundImageLayout; }
  278. set { base.BackgroundImageLayout = value; }
  279. }
  280. #endif
  281. protected override CreateParams CreateParams {
  282. get { return base.CreateParams;}
  283. }
  284. #if NET_2_0
  285. [DefaultValue ((string)null)]
  286. [AttributeProvider (typeof (IListSource))]
  287. [RefreshProperties (RefreshProperties.Repaint)]
  288. public new object DataSource {
  289. get { return base.DataSource; }
  290. set { base.DataSource = value; }
  291. }
  292. #endif
  293. protected override Size DefaultSize {
  294. get { return new Size (121, 21); }
  295. }
  296. [RefreshProperties(RefreshProperties.Repaint)]
  297. [DefaultValue (DrawMode.Normal)]
  298. public DrawMode DrawMode {
  299. get { return draw_mode; }
  300. set {
  301. if (!Enum.IsDefined (typeof (DrawMode), value))
  302. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
  303. if (draw_mode == value)
  304. return;
  305. if (draw_mode == DrawMode.OwnerDrawVariable)
  306. item_heights = null;
  307. draw_mode = value;
  308. if (draw_mode == DrawMode.OwnerDrawVariable)
  309. item_heights = new Hashtable ();
  310. Refresh ();
  311. }
  312. }
  313. #if NET_2_0
  314. [Browsable (true)]
  315. [DefaultValue (106)]
  316. [EditorBrowsable (EditorBrowsableState.Always)]
  317. public int DropDownHeight {
  318. get {
  319. return drop_down_height;
  320. }
  321. set {
  322. if (value < 1)
  323. throw new ArgumentOutOfRangeException ("DropDownHeight", "DropDownHeight must be greater than 0.");
  324. drop_down_height = value;
  325. IntegralHeight = false;
  326. }
  327. }
  328. #endif
  329. [DefaultValue (ComboBoxStyle.DropDown)]
  330. [RefreshProperties(RefreshProperties.Repaint)]
  331. public ComboBoxStyle DropDownStyle {
  332. get { return dropdown_style; }
  333. set {
  334. if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
  335. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
  336. if (dropdown_style == value)
  337. return;
  338. SuspendLayout ();
  339. if (dropdown_style == ComboBoxStyle.Simple) {
  340. if (listbox_ctrl != null) {
  341. Controls.RemoveImplicit (listbox_ctrl);
  342. listbox_ctrl.Dispose ();
  343. listbox_ctrl = null;
  344. }
  345. }
  346. dropdown_style = value;
  347. if (dropdown_style == ComboBoxStyle.DropDownList && textbox_ctrl != null) {
  348. Controls.RemoveImplicit (textbox_ctrl);
  349. textbox_ctrl.Dispose ();
  350. textbox_ctrl = null;
  351. }
  352. if (dropdown_style == ComboBoxStyle.Simple) {
  353. show_dropdown_button = false;
  354. CreateComboListBox ();
  355. Controls.AddImplicit (listbox_ctrl);
  356. listbox_ctrl.Visible = true;
  357. // This should give us a 150 default height
  358. // for Simple mode if size hasn't been set
  359. // (DefaultSize doesn't work for us in this case)
  360. if (requested_height == -1)
  361. requested_height = 150;
  362. } else {
  363. show_dropdown_button = true;
  364. button_state = ButtonState.Normal;
  365. }
  366. if (dropdown_style != ComboBoxStyle.DropDownList && textbox_ctrl == null) {
  367. textbox_ctrl = new ComboTextBox (this);
  368. object selected_item = SelectedItem;
  369. if (selected_item != null)
  370. textbox_ctrl.Text = GetItemText (selected_item);
  371. textbox_ctrl.BorderStyle = BorderStyle.None;
  372. textbox_ctrl.TextChanged += new EventHandler (OnTextChangedEdit);
  373. textbox_ctrl.KeyPress += new KeyPressEventHandler (OnTextKeyPress);
  374. textbox_ctrl.Click += new EventHandler (OnTextBoxClick);
  375. textbox_ctrl.ContextMenu = ContextMenu;
  376. if (IsHandleCreated == true)
  377. Controls.AddImplicit (textbox_ctrl);
  378. }
  379. ResumeLayout ();
  380. OnDropDownStyleChanged (EventArgs.Empty);
  381. LayoutComboBox ();
  382. UpdateComboBoxBounds ();
  383. Refresh ();
  384. }
  385. }
  386. public int DropDownWidth {
  387. get {
  388. if (dropdown_width == -1)
  389. return Width;
  390. return dropdown_width;
  391. }
  392. set {
  393. if (dropdown_width == value)
  394. return;
  395. if (value < 1)
  396. #if NET_2_0
  397. throw new ArgumentOutOfRangeException ("DropDownWidth",
  398. "The DropDownWidth value is less than one.");
  399. #else
  400. throw new ArgumentException ("The DropDownWidth value is less than one.");
  401. #endif
  402. dropdown_width = value;
  403. }
  404. }
  405. [Browsable (false)]
  406. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  407. public bool DroppedDown {
  408. get {
  409. if (dropdown_style == ComboBoxStyle.Simple)
  410. return true;
  411. return dropped_down;
  412. }
  413. set {
  414. if (dropdown_style == ComboBoxStyle.Simple || dropped_down == value)
  415. return;
  416. if (value)
  417. DropDownListBox ();
  418. else
  419. listbox_ctrl.Hide ();
  420. }
  421. }
  422. #if NET_2_0
  423. [DefaultValue (FlatStyle.Standard)]
  424. [Localizable (true)]
  425. public FlatStyle FlatStyle {
  426. get { return flat_style; }
  427. set {
  428. if (!Enum.IsDefined (typeof (FlatStyle), value))
  429. throw new InvalidEnumArgumentException ("FlatStyle", (int) value, typeof (FlatStyle));
  430. flat_style = value;
  431. LayoutComboBox ();
  432. Invalidate ();
  433. }
  434. }
  435. #endif
  436. public override bool Focused {
  437. get { return base.Focused; }
  438. }
  439. public override Color ForeColor {
  440. get { return base.ForeColor; }
  441. set {
  442. if (base.ForeColor == value)
  443. return;
  444. base.ForeColor = value;
  445. Refresh ();
  446. }
  447. }
  448. [DefaultValue (true)]
  449. [Localizable (true)]
  450. public bool IntegralHeight {
  451. get { return integral_height; }
  452. set {
  453. if (integral_height == value)
  454. return;
  455. integral_height = value;
  456. UpdateComboBoxBounds ();
  457. Refresh ();
  458. }
  459. }
  460. [Localizable (true)]
  461. public int ItemHeight {
  462. get {
  463. if (item_height == -1) {
  464. SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
  465. item_height = (int) sz.Height;
  466. }
  467. return item_height;
  468. }
  469. set {
  470. if (value < 1)
  471. #if NET_2_0
  472. throw new ArgumentOutOfRangeException ("ItemHeight",
  473. "The item height value is less than one.");
  474. #else
  475. throw new ArgumentException ("The item height value is less than one.");
  476. #endif
  477. item_height_specified = true;
  478. item_height = value;
  479. if (IntegralHeight)
  480. UpdateComboBoxBounds ();
  481. LayoutComboBox ();
  482. Refresh ();
  483. }
  484. }
  485. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  486. [Localizable (true)]
  487. [Editor ("System.Windows.Forms.Design.ListControlStringCollectionEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  488. #if NET_2_0
  489. [MergableProperty (false)]
  490. #endif
  491. public ComboBox.ObjectCollection Items {
  492. get { return items; }
  493. }
  494. [DefaultValue (8)]
  495. [Localizable (true)]
  496. public int MaxDropDownItems {
  497. get { return maxdrop_items; }
  498. set {
  499. if (maxdrop_items == value)
  500. return;
  501. maxdrop_items = value;
  502. }
  503. }
  504. #if NET_2_0
  505. public override Size MaximumSize {
  506. get { return base.MaximumSize; }
  507. set {
  508. base.MaximumSize = new Size (value.Width, 0);
  509. }
  510. }
  511. #endif
  512. [DefaultValue (0)]
  513. [Localizable (true)]
  514. public int MaxLength {
  515. get { return max_length; }
  516. set {
  517. if (max_length == value)
  518. return;
  519. max_length = value;
  520. if (dropdown_style != ComboBoxStyle.DropDownList) {
  521. if (value < 0) {
  522. value = 0;
  523. }
  524. textbox_ctrl.MaxLength = value;
  525. }
  526. }
  527. }
  528. #if NET_2_0
  529. public override Size MinimumSize {
  530. get { return base.MinimumSize; }
  531. set {
  532. base.MinimumSize = new Size (value.Width, 0);
  533. }
  534. }
  535. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  536. [EditorBrowsable (EditorBrowsableState.Never)]
  537. [Browsable (false)]
  538. public new Padding Padding {
  539. get { return base.Padding; }
  540. set { base.Padding = value; }
  541. }
  542. #endif
  543. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  544. [Browsable (false)]
  545. public int PreferredHeight {
  546. get {
  547. return ItemHeight + 6;
  548. }
  549. }
  550. [Browsable (false)]
  551. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  552. public override int SelectedIndex {
  553. get { return selected_index; }
  554. set {
  555. if (selected_index == value)
  556. return;
  557. if (value <= -2 || value >= Items.Count)
  558. throw new ArgumentOutOfRangeException ("SelectedIndex");
  559. selected_index = value;
  560. if (dropdown_style != ComboBoxStyle.DropDownList) {
  561. if (value == -1)
  562. SetControlText("");
  563. else
  564. SetControlText (GetItemText (Items [value]));
  565. }
  566. if (DropDownStyle == ComboBoxStyle.DropDownList)
  567. Invalidate ();
  568. if (listbox_ctrl != null)
  569. listbox_ctrl.HighlightedIndex = value;
  570. OnSelectedValueChanged (new EventArgs ());
  571. OnSelectedIndexChanged (new EventArgs ());
  572. OnSelectedItemChanged (new EventArgs ());
  573. }
  574. }
  575. [Browsable (false)]
  576. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  577. [Bindable(true)]
  578. public object SelectedItem {
  579. get { return selected_index == -1 ? null : Items [selected_index]; }
  580. set {
  581. object item = selected_index == -1 ? null : Items [selected_index];
  582. if (item == value)
  583. return;
  584. SelectedIndex = Items.IndexOf (value);
  585. }
  586. }
  587. [Browsable (false)]
  588. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  589. public string SelectedText {
  590. get {
  591. if (dropdown_style == ComboBoxStyle.DropDownList)
  592. return string.Empty;
  593. string retval = textbox_ctrl.SelectedText;
  594. #if ONLY_1_1
  595. // On 1.1, the textbox will return null, combobox returns ""
  596. if (retval == null && !textbox_ctrl.IsHandleCreated)
  597. return string.Empty;
  598. #endif
  599. return retval;
  600. }
  601. set {
  602. if (dropdown_style == ComboBoxStyle.DropDownList)
  603. return;
  604. textbox_ctrl.SelectedText = value;
  605. }
  606. }
  607. [Browsable (false)]
  608. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  609. public int SelectionLength {
  610. get {
  611. if (dropdown_style == ComboBoxStyle.DropDownList)
  612. return 0;
  613. int result = textbox_ctrl.SelectionLength;
  614. return result == -1 ? 0 : result;
  615. }
  616. set {
  617. if (dropdown_style == ComboBoxStyle.DropDownList)
  618. return;
  619. if (textbox_ctrl.SelectionLength == value)
  620. return;
  621. textbox_ctrl.SelectionLength = value;
  622. }
  623. }
  624. [Browsable (false)]
  625. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  626. public int SelectionStart {
  627. get {
  628. if (dropdown_style == ComboBoxStyle.DropDownList)
  629. return 0;
  630. return textbox_ctrl.SelectionStart;
  631. }
  632. set {
  633. if (dropdown_style == ComboBoxStyle.DropDownList)
  634. return;
  635. if (textbox_ctrl.SelectionStart == value)
  636. return;
  637. textbox_ctrl.SelectionStart = value;
  638. }
  639. }
  640. [DefaultValue (false)]
  641. public bool Sorted {
  642. get { return sorted; }
  643. set {
  644. if (sorted == value)
  645. return;
  646. sorted = value;
  647. SelectedIndex = -1;
  648. if (sorted) {
  649. Items.Sort ();
  650. LayoutComboBox ();
  651. }
  652. }
  653. }
  654. [Bindable (true)]
  655. [Localizable (true)]
  656. public override string Text {
  657. get {
  658. if (dropdown_style != ComboBoxStyle.DropDownList) {
  659. if (textbox_ctrl != null) {
  660. return textbox_ctrl.Text;
  661. }
  662. }
  663. if (SelectedItem != null)
  664. return GetItemText (SelectedItem);
  665. return base.Text;
  666. }
  667. set {
  668. if (value == null) {
  669. if (SelectedIndex == -1) {
  670. if (dropdown_style != ComboBoxStyle.DropDownList)
  671. SetControlText ("");
  672. } else {
  673. SelectedIndex = -1;
  674. }
  675. return;
  676. }
  677. // do nothing if value exactly matches text of selected item
  678. if (SelectedItem != null && string.Compare (value, GetItemText (SelectedItem), false, CultureInfo.CurrentCulture) == 0)
  679. return;
  680. // find exact match using case-sensitive comparison, and if does
  681. // not result in any match then use case-insensitive comparison
  682. int index = FindStringExact (value, -1, false);
  683. if (index == -1) {
  684. index = FindStringExact (value, -1, true);
  685. }
  686. if (index != -1) {
  687. SelectedIndex = index;
  688. return;
  689. }
  690. if (dropdown_style != ComboBoxStyle.DropDownList)
  691. textbox_ctrl.Text = GetItemText (value);
  692. }
  693. }
  694. #endregion Public Properties
  695. #region Public Methods
  696. #if NET_2_0
  697. [Obsolete ("This method has been deprecated")]
  698. #endif
  699. protected virtual void AddItemsCore (object[] value)
  700. {
  701. }
  702. public void BeginUpdate ()
  703. {
  704. suspend_ctrlupdate = true;
  705. }
  706. #if NET_2_0
  707. protected override AccessibleObject CreateAccessibilityInstance ()
  708. {
  709. return base.CreateAccessibilityInstance ();
  710. }
  711. protected override void CreateHandle ()
  712. {
  713. base.CreateHandle ();
  714. }
  715. #endif
  716. protected override void Dispose (bool disposing)
  717. {
  718. if (disposing) {
  719. if (listbox_ctrl != null) {
  720. listbox_ctrl.Dispose ();
  721. Controls.RemoveImplicit (listbox_ctrl);
  722. listbox_ctrl = null;
  723. }
  724. if (textbox_ctrl != null) {
  725. Controls.RemoveImplicit (textbox_ctrl);
  726. textbox_ctrl.Dispose ();
  727. textbox_ctrl = null;
  728. }
  729. }
  730. base.Dispose (disposing);
  731. }
  732. public void EndUpdate ()
  733. {
  734. suspend_ctrlupdate = false;
  735. UpdatedItems ();
  736. Refresh ();
  737. }
  738. public int FindString (string s)
  739. {
  740. return FindString (s, -1);
  741. }
  742. public int FindString (string s, int startIndex)
  743. {
  744. if (s == null || Items.Count == 0)
  745. return -1;
  746. #if NET_2_0
  747. if (startIndex < -1 || startIndex >= Items.Count)
  748. #else
  749. if (startIndex < -1 || startIndex >= Items.Count - 1)
  750. #endif
  751. throw new ArgumentOutOfRangeException ("startIndex");
  752. int i = startIndex;
  753. #if NET_2_0
  754. if (i == (Items.Count - 1))
  755. i = -1;
  756. #endif
  757. do {
  758. i++;
  759. if (string.Compare (s, 0, GetItemText (Items [i]), 0, s.Length, true) == 0)
  760. return i;
  761. if (i == (Items.Count - 1))
  762. i = -1;
  763. } while (i != startIndex);
  764. return -1;
  765. }
  766. public int FindStringExact (string s)
  767. {
  768. return FindStringExact (s, -1);
  769. }
  770. public int FindStringExact (string s, int startIndex)
  771. {
  772. return FindStringExact (s, startIndex, true);
  773. }
  774. private int FindStringExact (string s, int startIndex, bool ignoreCase)
  775. {
  776. if (s == null || Items.Count == 0)
  777. return -1;
  778. #if NET_2_0
  779. if (startIndex < -1 || startIndex >= Items.Count)
  780. #else
  781. if (startIndex < -1 || startIndex >= Items.Count - 1)
  782. #endif
  783. throw new ArgumentOutOfRangeException ("startIndex");
  784. int i = startIndex;
  785. #if NET_2_0
  786. if (i == (Items.Count - 1))
  787. i = -1;
  788. #endif
  789. do {
  790. i++;
  791. if (string.Compare (s, GetItemText (Items [i]), ignoreCase, CultureInfo.CurrentCulture) == 0)
  792. return i;
  793. if (i == (Items.Count - 1))
  794. i = -1;
  795. } while (i != startIndex);
  796. return -1;
  797. }
  798. public int GetItemHeight (int index)
  799. {
  800. if (DrawMode == DrawMode.OwnerDrawVariable && IsHandleCreated) {
  801. if (index < 0 || index >= Items.Count )
  802. throw new ArgumentOutOfRangeException ("The item height value is less than zero");
  803. object item = Items [index];
  804. if (item_heights.Contains (item))
  805. return (int) item_heights [item];
  806. MeasureItemEventArgs args = new MeasureItemEventArgs (DeviceContext, index, ItemHeight);
  807. OnMeasureItem (args);
  808. item_heights [item] = args.ItemHeight;
  809. return args.ItemHeight;
  810. }
  811. return ItemHeight;
  812. }
  813. protected override bool IsInputKey (Keys keyData)
  814. {
  815. switch (keyData & ~Keys.Modifiers) {
  816. case Keys.Up:
  817. case Keys.Down:
  818. case Keys.Left:
  819. case Keys.Right:
  820. case Keys.PageUp:
  821. case Keys.PageDown:
  822. case Keys.Home:
  823. case Keys.End:
  824. return true;
  825. default:
  826. return false;
  827. }
  828. }
  829. protected override void OnBackColorChanged (EventArgs e)
  830. {
  831. base.OnBackColorChanged (e);
  832. if (textbox_ctrl != null)
  833. textbox_ctrl.BackColor = BackColor;
  834. }
  835. protected override void OnDataSourceChanged (EventArgs e)
  836. {
  837. base.OnDataSourceChanged (e);
  838. BindDataItems ();
  839. if (DataSource == null || DataManager == null) {
  840. SelectedIndex = -1;
  841. }
  842. else {
  843. SelectedIndex = DataManager.Position;
  844. }
  845. }
  846. protected override void OnDisplayMemberChanged (EventArgs e)
  847. {
  848. base.OnDisplayMemberChanged (e);
  849. if (DataManager == null)
  850. return;
  851. if (selected_index != -1 && DropDownStyle != ComboBoxStyle.DropDownList)
  852. SetControlText (GetItemText (Items [selected_index]));
  853. if (!IsHandleCreated)
  854. return;
  855. SelectedIndex = DataManager.Position;
  856. Invalidate ();
  857. }
  858. protected virtual void OnDrawItem (DrawItemEventArgs e)
  859. {
  860. switch (DrawMode) {
  861. case DrawMode.OwnerDrawFixed:
  862. case DrawMode.OwnerDrawVariable:
  863. DrawItemEventHandler eh = (DrawItemEventHandler)(Events [DrawItemEvent]);
  864. if (eh != null)
  865. eh (this, e);
  866. break;
  867. default:
  868. ThemeEngine.Current.DrawComboBoxItem (this, e);
  869. break;
  870. }
  871. }
  872. protected virtual void OnDropDown (EventArgs e)
  873. {
  874. EventHandler eh = (EventHandler)(Events [DropDownEvent]);
  875. if (eh != null)
  876. eh (this, e);
  877. }
  878. #if NET_2_0
  879. protected virtual void OnDropDownClosed (EventArgs e)
  880. {
  881. EventHandler eh = (EventHandler) Events [DropDownClosedEvent];
  882. if (eh != null)
  883. eh (this, e);
  884. }
  885. #endif
  886. protected virtual void OnDropDownStyleChanged (EventArgs e)
  887. {
  888. EventHandler eh = (EventHandler)(Events [DropDownStyleChangedEvent]);
  889. if (eh != null)
  890. eh (this, e);
  891. }
  892. protected override void OnFontChanged (EventArgs e)
  893. {
  894. base.OnFontChanged (e);
  895. if (textbox_ctrl != null)
  896. textbox_ctrl.Font = Font;
  897. if (!item_height_specified) {
  898. SizeF sz = TextRenderer.MeasureString ("The quick brown Fox", Font);
  899. item_height = (int) sz.Height;
  900. }
  901. if (IntegralHeight)
  902. UpdateComboBoxBounds ();
  903. LayoutComboBox ();
  904. }
  905. protected override void OnForeColorChanged (EventArgs e)
  906. {
  907. base.OnForeColorChanged (e);
  908. if (textbox_ctrl != null)
  909. textbox_ctrl.ForeColor = ForeColor;
  910. }
  911. [EditorBrowsable(EditorBrowsableState.Advanced)]
  912. protected override void OnGotFocus (EventArgs e)
  913. {
  914. if (dropdown_style == ComboBoxStyle.DropDownList) {
  915. // We draw DDL styles manually, so they require a
  916. // refresh to have their selection drawn
  917. Invalidate ();
  918. }
  919. if (textbox_ctrl != null) {
  920. textbox_ctrl.SetSelectable (false);
  921. textbox_ctrl.ShowSelection = true;
  922. textbox_ctrl.ActivateCaret (true);
  923. textbox_ctrl.SelectAll ();
  924. }
  925. base.OnGotFocus (e);
  926. }
  927. [EditorBrowsable(EditorBrowsableState.Advanced)]
  928. protected override void OnLostFocus (EventArgs e)
  929. {
  930. if (dropdown_style == ComboBoxStyle.DropDownList) {
  931. // We draw DDL styles manually, so they require a
  932. // refresh to have their selection drawn
  933. Invalidate ();
  934. }
  935. if (listbox_ctrl != null && dropped_down) {
  936. listbox_ctrl.HideWindow ();
  937. }
  938. if (textbox_ctrl != null) {
  939. textbox_ctrl.SetSelectable (true);
  940. textbox_ctrl.ActivateCaret (false);
  941. textbox_ctrl.ShowSelection = false;
  942. textbox_ctrl.SelectionLength = 0;
  943. }
  944. base.OnLostFocus (e);
  945. }
  946. protected override void OnHandleCreated (EventArgs e)
  947. {
  948. base.OnHandleCreated (e);
  949. SetBounds (Left, Top, Width, PreferredHeight, BoundsSpecified.None);
  950. if (textbox_ctrl != null)
  951. Controls.AddImplicit (textbox_ctrl);
  952. LayoutComboBox ();
  953. UpdateComboBoxBounds ();
  954. }
  955. protected override void OnHandleDestroyed (EventArgs e)
  956. {
  957. base.OnHandleDestroyed (e);
  958. }
  959. protected override void OnKeyPress (KeyPressEventArgs e)
  960. {
  961. if (dropdown_style == ComboBoxStyle.DropDownList) {
  962. int index = FindStringCaseInsensitive (e.KeyChar.ToString (), SelectedIndex + 1);
  963. if (index != -1) {
  964. SelectedIndex = index;
  965. if (DroppedDown) { //Scroll into view
  966. if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
  967. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
  968. // Or, selecting an item earlier in the list.
  969. if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
  970. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
  971. }
  972. }
  973. }
  974. base.OnKeyPress (e);
  975. }
  976. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  977. {
  978. MeasureItemEventHandler eh = (MeasureItemEventHandler)(Events [MeasureItemEvent]);
  979. if (eh != null)
  980. eh (this, e);
  981. }
  982. protected override void OnParentBackColorChanged (EventArgs e)
  983. {
  984. base.OnParentBackColorChanged (e);
  985. }
  986. protected override void OnResize (EventArgs e)
  987. {
  988. LayoutComboBox ();
  989. if (listbox_ctrl != null)
  990. listbox_ctrl.CalcListBoxArea ();
  991. }
  992. protected override void OnSelectedIndexChanged (EventArgs e)
  993. {
  994. base.OnSelectedIndexChanged (e);
  995. EventHandler eh = (EventHandler)(Events [SelectedIndexChangedEvent]);
  996. if (eh != null)
  997. eh (this, e);
  998. }
  999. protected virtual void OnSelectedItemChanged (EventArgs e)
  1000. {
  1001. }
  1002. protected override void OnSelectedValueChanged (EventArgs e)
  1003. {
  1004. base.OnSelectedValueChanged (e);
  1005. }
  1006. protected virtual void OnSelectionChangeCommitted (EventArgs e)
  1007. {
  1008. EventHandler eh = (EventHandler)(Events [SelectionChangeCommittedEvent]);
  1009. if (eh != null)
  1010. eh (this, e);
  1011. }
  1012. protected override void RefreshItem (int index)
  1013. {
  1014. if (index < 0 || index >= Items.Count)
  1015. throw new ArgumentOutOfRangeException ("index");
  1016. if (draw_mode == DrawMode.OwnerDrawVariable)
  1017. item_heights.Remove (Items [index]);
  1018. }
  1019. #if NET_2_0
  1020. protected override void RefreshItems ()
  1021. {
  1022. for (int i = 0; i < Items.Count; i++) {
  1023. RefreshItem (i);
  1024. }
  1025. }
  1026. public override void ResetText ()
  1027. {
  1028. Text = String.Empty;
  1029. }
  1030. protected override bool ProcessKeyEventArgs (ref Message m)
  1031. {
  1032. return base.ProcessKeyEventArgs (ref m);
  1033. }
  1034. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1035. protected override void OnKeyDown (KeyEventArgs e)
  1036. {
  1037. base.OnKeyDown (e);
  1038. }
  1039. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1040. protected override void OnValidating (CancelEventArgs e)
  1041. {
  1042. base.OnValidating (e);
  1043. }
  1044. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1045. protected override void OnTextChanged (EventArgs e)
  1046. {
  1047. base.OnTextChanged (e);
  1048. }
  1049. #if NET_2_0
  1050. protected virtual void OnTextUpdate (EventArgs e)
  1051. {
  1052. EventHandler eh = (EventHandler) Events [TextUpdateEvent];
  1053. if (eh != null)
  1054. eh (this, e);
  1055. }
  1056. #endif
  1057. protected override void OnMouseLeave (EventArgs e)
  1058. {
  1059. #if NET_2_0
  1060. if (flat_style == FlatStyle.Popup)
  1061. Invalidate ();
  1062. #endif
  1063. base.OnMouseLeave (e);
  1064. }
  1065. protected override void OnMouseEnter (EventArgs e)
  1066. {
  1067. #if NET_2_0
  1068. if (flat_style == FlatStyle.Popup)
  1069. Invalidate ();
  1070. #endif
  1071. base.OnMouseEnter (e);
  1072. }
  1073. #endif
  1074. #if NET_2_0
  1075. protected override void ScaleControl (SizeF factor, BoundsSpecified specified)
  1076. {
  1077. base.ScaleControl (factor, specified);
  1078. }
  1079. #endif
  1080. public void Select (int start, int length)
  1081. {
  1082. if (start < 0)
  1083. throw new ArgumentException ("Start cannot be less than zero");
  1084. if (length < 0)
  1085. throw new ArgumentException ("length cannot be less than zero");
  1086. if (dropdown_style == ComboBoxStyle.DropDownList)
  1087. return;
  1088. textbox_ctrl.Select (start, length);
  1089. }
  1090. public void SelectAll ()
  1091. {
  1092. if (dropdown_style == ComboBoxStyle.DropDownList)
  1093. return;
  1094. if (textbox_ctrl != null) {
  1095. textbox_ctrl.ShowSelection = true;
  1096. textbox_ctrl.SelectAll ();
  1097. }
  1098. }
  1099. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  1100. {
  1101. bool vertically_anchored = (Anchor & AnchorStyles.Top) != 0 && (Anchor & AnchorStyles.Bottom) != 0;
  1102. if ((specified & BoundsSpecified.Height) != 0 ||
  1103. (specified == BoundsSpecified.None && vertically_anchored)) {
  1104. requested_height = height;
  1105. if (DropDownStyle == ComboBoxStyle.Simple && height > PreferredHeight) {
  1106. if (IntegralHeight) {
  1107. int border = ThemeEngine.Current.Border3DSize.Height;
  1108. int lb_height = height - PreferredHeight - 2;
  1109. if (lb_height - 2 * border > ItemHeight) {
  1110. int partial = (lb_height - 2 * border) % ItemHeight;
  1111. height -= partial;
  1112. } else
  1113. height = PreferredHeight;
  1114. }
  1115. } else
  1116. height = PreferredHeight;
  1117. }
  1118. base.SetBoundsCore (x, y, width, height, specified);
  1119. }
  1120. protected override void SetItemCore (int index, object value)
  1121. {
  1122. if (index < 0 || index >= Items.Count)
  1123. return;
  1124. Items[index] = value;
  1125. }
  1126. protected override void SetItemsCore (IList value)
  1127. {
  1128. BeginUpdate ();
  1129. try {
  1130. Items.Clear ();
  1131. Items.AddRange (value);
  1132. } finally {
  1133. EndUpdate ();
  1134. }
  1135. }
  1136. public override string ToString ()
  1137. {
  1138. return base.ToString () + ", Items.Count:" + Items.Count;
  1139. }
  1140. protected override void WndProc (ref Message m)
  1141. {
  1142. switch ((Msg) m.Msg) {
  1143. case Msg.WM_KEYUP:
  1144. case Msg.WM_KEYDOWN:
  1145. Keys keys = (Keys) m.WParam.ToInt32 ();
  1146. if (keys == Keys.Up || keys == Keys.Down)
  1147. break;
  1148. goto case Msg.WM_CHAR;
  1149. case Msg.WM_CHAR:
  1150. if (textbox_ctrl != null)
  1151. XplatUI.SendMessage (textbox_ctrl.Handle, (Msg) m.Msg, m.WParam, m.LParam);
  1152. break;
  1153. case Msg.WM_MOUSELEAVE:
  1154. Point location = PointToClient (Control.MousePosition);
  1155. if (ClientRectangle.Contains (location))
  1156. return;
  1157. break;
  1158. default:
  1159. break;
  1160. }
  1161. base.WndProc (ref m);
  1162. }
  1163. #endregion Public Methods
  1164. #region Private Methods
  1165. #if NET_2_0
  1166. void OnAutoCompleteCustomSourceChanged(object sender, CollectionChangeEventArgs e) {
  1167. if(auto_complete_source == AutoCompleteSource.CustomSource) {
  1168. //FIXME: handle add, remove and refresh events in AutoComplete algorithm.
  1169. }
  1170. }
  1171. #endif
  1172. internal override bool InternalCapture {
  1173. get { return Capture; }
  1174. set {}
  1175. }
  1176. void LayoutComboBox ()
  1177. {
  1178. int border = ThemeEngine.Current.Border3DSize.Width;
  1179. text_area = ClientRectangle;
  1180. text_area.Height = PreferredHeight;
  1181. listbox_area = ClientRectangle;
  1182. listbox_area.Y = text_area.Bottom + 3;
  1183. listbox_area.Height -= (text_area.Height + 2);
  1184. Rectangle prev_button_area = button_area;
  1185. if (DropDownStyle == ComboBoxStyle.Simple)
  1186. button_area = Rectangle.Empty;
  1187. else {
  1188. button_area = text_area;
  1189. button_area.X = text_area.Right - button_width - border;
  1190. button_area.Y = text_area.Y + border;
  1191. button_area.Width = button_width;
  1192. button_area.Height = text_area.Height - 2 * border;
  1193. #if NET_2_0
  1194. if (flat_style == FlatStyle.Popup || flat_style == FlatStyle.Flat) {
  1195. button_area.Inflate (1, 1);
  1196. button_area.X += 2;
  1197. button_area.Width -= 2;
  1198. }
  1199. #endif
  1200. }
  1201. if (button_area != prev_button_area) {
  1202. prev_button_area.Y -= border;
  1203. prev_button_area.Width += border;
  1204. prev_button_area.Height += 2 * border;
  1205. Invalidate (prev_button_area);
  1206. Invalidate (button_area);
  1207. }
  1208. if (textbox_ctrl != null) {
  1209. int text_border = border + 1;
  1210. textbox_ctrl.Location = new Point (text_area.X + text_border, text_area.Y + text_border);
  1211. textbox_ctrl.Width = text_area.Width - button_area.Width - text_border * 2;
  1212. textbox_ctrl.Height = text_area.Height - text_border * 2;
  1213. }
  1214. if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
  1215. listbox_ctrl.Location = listbox_area.Location;
  1216. listbox_ctrl.CalcListBoxArea ();
  1217. }
  1218. }
  1219. private void CreateComboListBox ()
  1220. {
  1221. listbox_ctrl = new ComboListBox (this);
  1222. listbox_ctrl.HighlightedIndex = SelectedIndex;
  1223. }
  1224. internal void Draw (Rectangle clip, Graphics dc)
  1225. {
  1226. Theme theme = ThemeEngine.Current;
  1227. FlatStyle style = FlatStyle.Standard;
  1228. bool is_flat = false;
  1229. #if NET_2_0
  1230. style = this.FlatStyle;
  1231. is_flat = style == FlatStyle.Flat || style == FlatStyle.Popup;
  1232. #endif
  1233. if (!Enabled)
  1234. dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), bounds);
  1235. if (DropDownStyle == ComboBoxStyle.Simple)
  1236. dc.FillRectangle (theme.ResPool.GetSolidBrush (Parent.BackColor), ClientRectangle);
  1237. if (style == FlatStyle.Popup && (is_entered || Focused)) {
  1238. Rectangle area = text_area;
  1239. area.Height -= 1;
  1240. area.Width -= 1;
  1241. dc.DrawRectangle (theme.ResPool.GetPen (SystemColors.ControlDark), area);
  1242. dc.DrawLine (theme.ResPool.GetPen (SystemColors.ControlDark), button_area.X - 1, button_area.Top, button_area.X - 1, button_area.Bottom);
  1243. }
  1244. if (!is_flat && clip.IntersectsWith (text_area))
  1245. ControlPaint.DrawBorder3D (dc, text_area, Border3DStyle.Sunken);
  1246. int border = theme.Border3DSize.Width;
  1247. // No edit control, we paint the edit ourselves
  1248. if (dropdown_style == ComboBoxStyle.DropDownList) {
  1249. DrawItemState state = DrawItemState.None;
  1250. Rectangle item_rect = text_area;
  1251. item_rect.X += border;
  1252. item_rect.Y += border;
  1253. item_rect.Width -= (button_area.Width + 2 * border);
  1254. item_rect.Height -= 2 * border;
  1255. if (Focused) {
  1256. state = DrawItemState.Selected;
  1257. state |= DrawItemState.Focus;
  1258. }
  1259. state |= DrawItemState.ComboBoxEdit;
  1260. OnDrawItem (new DrawItemEventArgs (dc, Font, item_rect, SelectedIndex, state, ForeColor, BackColor));
  1261. }
  1262. if (show_dropdown_button) {
  1263. ButtonState current_state;
  1264. if (is_enabled)
  1265. current_state = button_state;
  1266. else
  1267. current_state = ButtonState.Inactive;
  1268. if (is_flat || theme.ComboBoxNormalDropDownButtonHasTransparentBackground (this, current_state))
  1269. dc.FillRectangle (theme.ResPool.GetSolidBrush (theme.ColorControl), button_area);
  1270. if (is_flat) {
  1271. theme.DrawFlatStyleComboButton (dc, button_area, current_state);
  1272. } else {
  1273. theme.ComboBoxDrawNormalDropDownButton (this, dc, clip, button_area, current_state);
  1274. }
  1275. }
  1276. }
  1277. internal bool DropDownButtonEntered {
  1278. get { return drop_down_button_entered; }
  1279. private set {
  1280. if (drop_down_button_entered == value)
  1281. return;
  1282. drop_down_button_entered = value;
  1283. if (ThemeEngine.Current.ComboBoxDropDownButtonHasHotElementStyle (this))
  1284. Invalidate (button_area);
  1285. }
  1286. }
  1287. internal void DropDownListBox ()
  1288. {
  1289. DropDownButtonEntered = false;
  1290. if (DropDownStyle == ComboBoxStyle.Simple)
  1291. return;
  1292. if (listbox_ctrl == null)
  1293. CreateComboListBox ();
  1294. listbox_ctrl.Location = PointToScreen (new Point (text_area.X, text_area.Y + text_area.Height));
  1295. FindMatchOrSetIndex(SelectedIndex);
  1296. if (listbox_ctrl.ShowWindow ())
  1297. dropped_down = true;
  1298. button_state = ButtonState.Pushed;
  1299. if (dropdown_style == ComboBoxStyle.DropDownList)
  1300. Invalidate (text_area);
  1301. }
  1302. internal void DropDownListBoxFinished ()
  1303. {
  1304. if (DropDownStyle == ComboBoxStyle.Simple)
  1305. return;
  1306. button_state = ButtonState.Normal;
  1307. Invalidate (button_area);
  1308. dropped_down = false;
  1309. #if NET_2_0
  1310. OnDropDownClosed (EventArgs.Empty);
  1311. #endif
  1312. /*
  1313. * Apples X11 looses override-redirect when doing a Unmap/Map on a previously mapped window
  1314. * this causes the popup to appear under the main form. This is horrible but necessary
  1315. */
  1316. // If the user opens a new form in an event, it will close our dropdown,
  1317. // so we need a null check here
  1318. if (listbox_ctrl != null) {
  1319. listbox_ctrl.Dispose ();
  1320. listbox_ctrl = null;
  1321. }
  1322. }
  1323. private int FindStringCaseInsensitive (string search)
  1324. {
  1325. if (search.Length == 0) {
  1326. return -1;
  1327. }
  1328. for (int i = 0; i < Items.Count; i++)
  1329. {
  1330. if (String.Compare (GetItemText (Items[i]), 0, search, 0, search.Length, true) == 0)
  1331. return i;
  1332. }
  1333. return -1;
  1334. }
  1335. // Search in the list for the substring, starting the search at the list
  1336. // position specified, the search wraps thus covering all the list.
  1337. internal int FindStringCaseInsensitive (string search, int start_index)
  1338. {
  1339. if (search.Length == 0) {
  1340. return -1;
  1341. }
  1342. // Accept from first item to after last item. i.e. all cases of (SelectedIndex+1).
  1343. if (start_index < 0 || start_index > Items.Count)
  1344. throw new ArgumentOutOfRangeException("start_index");
  1345. for (int i = 0; i < Items.Count; i++) {
  1346. int index = (i + start_index) % Items.Count;
  1347. if (String.Compare (GetItemText (Items [index]), 0, search, 0, search.Length, true) == 0)
  1348. return index;
  1349. }
  1350. return -1;
  1351. }
  1352. internal override bool IsInputCharInternal (char charCode)
  1353. {
  1354. return true;
  1355. }
  1356. internal override ContextMenu ContextMenuInternal {
  1357. get {
  1358. return base.ContextMenuInternal;
  1359. }
  1360. set {
  1361. base.ContextMenuInternal = value;
  1362. if (textbox_ctrl != null) {
  1363. textbox_ctrl.ContextMenu = value;
  1364. }
  1365. }
  1366. }
  1367. internal void RestoreContextMenu ()
  1368. {
  1369. textbox_ctrl.RestoreContextMenu ();
  1370. }
  1371. private void OnKeyDownCB(object sender, KeyEventArgs e)
  1372. {
  1373. if (Items.Count == 0)
  1374. return;
  1375. int offset;
  1376. switch (e.KeyCode)
  1377. {
  1378. case Keys.Up:
  1379. FindMatchOrSetIndex(Math.Max(SelectedIndex - 1, 0));
  1380. if (DroppedDown)
  1381. if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
  1382. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
  1383. break;
  1384. case Keys.Down:
  1385. if ((e.Modifiers & Keys.Alt) == Keys.Alt)
  1386. DropDownListBox ();
  1387. else
  1388. FindMatchOrSetIndex(Math.Min(SelectedIndex + 1, Items.Count - 1));
  1389. if (DroppedDown)
  1390. if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
  1391. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
  1392. break;
  1393. case Keys.PageUp:
  1394. offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
  1395. if (offset < 1)
  1396. offset = 1;
  1397. SelectedIndex = Math.Max (SelectedIndex - offset, 0);
  1398. if (DroppedDown)
  1399. if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
  1400. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
  1401. break;
  1402. case Keys.PageDown:
  1403. if (SelectedIndex == -1) {
  1404. SelectedIndex = 0;
  1405. if (dropdown_style != ComboBoxStyle.Simple)
  1406. return;
  1407. }
  1408. offset = listbox_ctrl == null ? MaxDropDownItems - 1 : listbox_ctrl.page_size - 1;
  1409. if (offset < 1)
  1410. offset = 1;
  1411. SelectedIndex = Math.Min (SelectedIndex + offset, Items.Count - 1);
  1412. if (DroppedDown)
  1413. if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
  1414. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
  1415. break;
  1416. case Keys.Escape:
  1417. DropDownListBoxFinished ();
  1418. break;
  1419. case Keys.Home:
  1420. if (dropdown_style == ComboBoxStyle.DropDownList) {
  1421. SelectedIndex = 0;
  1422. if (DroppedDown)
  1423. if (SelectedIndex < listbox_ctrl.FirstVisibleItem ())
  1424. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.FirstVisibleItem ());
  1425. }
  1426. break;
  1427. case Keys.End:
  1428. if (dropdown_style == ComboBoxStyle.DropDownList) {
  1429. SelectedIndex = Items.Count - 1;
  1430. if (DroppedDown)
  1431. if (SelectedIndex >= listbox_ctrl.LastVisibleItem ())
  1432. listbox_ctrl.Scroll (SelectedIndex - listbox_ctrl.LastVisibleItem () + 1);
  1433. }
  1434. break;
  1435. default:
  1436. break;
  1437. }
  1438. }
  1439. // If no item is currently selected, and an item is found matching the text
  1440. // in the textbox, then selected that item. Otherwise the item at the given
  1441. // index is selected.
  1442. private void FindMatchOrSetIndex(int index)
  1443. {
  1444. int match = -1;
  1445. if (SelectedIndex == -1 && Text.Length != 0)
  1446. match = FindStringCaseInsensitive(Text);
  1447. if (match != -1)
  1448. SelectedIndex = match;
  1449. else
  1450. SelectedIndex = index;
  1451. }
  1452. void OnMouseDownCB (object sender, MouseEventArgs e)
  1453. {
  1454. Rectangle area;
  1455. if (DropDownStyle == ComboBoxStyle.DropDownList)
  1456. area = ClientRectangle;
  1457. else
  1458. area = button_area;
  1459. if (area.Contains (e.X, e.Y)) {
  1460. if (Items.Count > 0)
  1461. DropDownListBox ();
  1462. else {
  1463. button_state = ButtonState.Pushed;
  1464. OnDropDown (EventArgs.Empty);
  1465. }
  1466. Invalidate (button_area);
  1467. Update ();
  1468. }
  1469. Capture = true;
  1470. }
  1471. void OnMouseLeave (object sender, EventArgs e)
  1472. {
  1473. if (show_dropdown_button)
  1474. DropDownButtonEntered = false;
  1475. }
  1476. void OnMouseMoveCB (object sender, MouseEventArgs e)
  1477. {
  1478. if (show_dropdown_button && !dropped_down)
  1479. DropDownButtonEntered = button_area.Contains (e.Location);
  1480. if (DropDownStyle == ComboBoxStyle.Simple)
  1481. return;
  1482. if (listbox_ctrl != null && listbox_ctrl.Visible) {
  1483. Point location = listbox_ctrl.PointToClient (Control.MousePosition);
  1484. if (listbox_ctrl.ClientRectangle.Contains (location))
  1485. listbox_ctrl.Capture = true;
  1486. }
  1487. }
  1488. void OnMouseUpCB (object sender, MouseEventArgs e)
  1489. {
  1490. Capture = false;
  1491. button_state = ButtonState.Normal;
  1492. Invalidate (button_area);
  1493. OnClick (EventArgs.Empty);
  1494. if (dropped_down)
  1495. listbox_ctrl.Capture = true;
  1496. }
  1497. private void OnMouseWheelCB (object sender, MouseEventArgs me)
  1498. {
  1499. if (Items.Count == 0)
  1500. return;
  1501. if (listbox_ctrl != null && listbox_ctrl.Visible) {
  1502. int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
  1503. listbox_ctrl.Scroll (-lines);
  1504. } else {
  1505. int lines = me.Delta / 120;
  1506. int index = SelectedIndex - lines;
  1507. if (index < 0)
  1508. index = 0;
  1509. else if (index >= Items.Count)
  1510. index = Items.Count - 1;
  1511. SelectedIndex = index;
  1512. }
  1513. }
  1514. internal override void OnPaintInternal (PaintEventArgs pevent)
  1515. {
  1516. if (suspend_ctrlupdate)
  1517. return;
  1518. Draw (ClientRectangle, pevent.Graphics);
  1519. }
  1520. private void OnTextBoxClick (object sender, EventArgs e)
  1521. {
  1522. OnClick (e);
  1523. }
  1524. private void OnTextChangedEdit (object sender, EventArgs e)
  1525. {
  1526. if (process_textchanged_event == false)
  1527. return;
  1528. OnTextChanged (EventArgs.Empty);
  1529. int item = FindStringCaseInsensitive (textbox_ctrl.Text);
  1530. if (item == -1)
  1531. return;
  1532. // TODO: THIS IS BROKEN-ISH
  1533. // I don't think we should hilight, and setting the top item does weirdness
  1534. // when there is no scrollbar
  1535. if (listbox_ctrl != null) {
  1536. listbox_ctrl.SetTopItem (item);
  1537. listbox_ctrl.HighlightedIndex = item;
  1538. }
  1539. base.Text = textbox_ctrl.Text;
  1540. }
  1541. private void OnTextKeyPress (object sender, KeyPressEventArgs e)
  1542. {
  1543. selected_index = -1;
  1544. }
  1545. internal void SetControlText (string s)
  1546. {
  1547. process_textchanged_event = false;
  1548. textbox_ctrl.Text = s;
  1549. textbox_ctrl.SelectAll ();
  1550. process_textchanged_event = true;
  1551. }
  1552. void UpdateComboBoxBounds ()
  1553. {
  1554. if (requested_height == -1)
  1555. return;
  1556. // Save the requested height since set bounds can destroy it
  1557. int save_height = requested_height;
  1558. SetBounds (bounds.X, bounds.Y, bounds.Width, requested_height, BoundsSpecified.Height);
  1559. requested_height = save_height;
  1560. }
  1561. private void UpdatedItems ()
  1562. {
  1563. if (listbox_ctrl != null) {
  1564. listbox_ctrl.UpdateLastVisibleItem ();
  1565. listbox_ctrl.CalcListBoxArea ();
  1566. listbox_ctrl.Refresh ();
  1567. }
  1568. }
  1569. #endregion Private Methods
  1570. [ListBindableAttribute (false)]
  1571. public class ObjectCollection : IList, ICollection, IEnumerable
  1572. {
  1573. private ComboBox owner;
  1574. internal ArrayList object_items = new ArrayList ();
  1575. public ObjectCollection (ComboBox owner)
  1576. {
  1577. this.owner = owner;
  1578. }
  1579. #region Public Properties
  1580. public int Count {
  1581. get { return object_items.Count; }
  1582. }
  1583. public bool IsReadOnly {
  1584. get { return false; }
  1585. }
  1586. [Browsable (false)]
  1587. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  1588. public virtual object this [int index] {
  1589. get {
  1590. if (index < 0 || index >= Count)
  1591. throw new ArgumentOutOfRangeException ("index");
  1592. return object_items[index];
  1593. }
  1594. set {
  1595. if (index < 0 || index >= Count)
  1596. throw new ArgumentOutOfRangeException ("index");
  1597. if (value == null)
  1598. throw new ArgumentNullException ("value");
  1599. object_items[index] = value;
  1600. if (owner.listbox_ctrl != null)
  1601. owner.listbox_ctrl.InvalidateItem (index);
  1602. if (index == owner.SelectedIndex) {
  1603. if (owner.textbox_ctrl == null)
  1604. owner.Refresh ();
  1605. else
  1606. owner.textbox_ctrl.SelectedText = value.ToString ();
  1607. }
  1608. }
  1609. }
  1610. bool ICollection.IsSynchronized {
  1611. get { return false; }
  1612. }
  1613. object ICollection.SyncRoot {
  1614. get { return this; }
  1615. }
  1616. bool IList.IsFixedSize {
  1617. get { return false; }
  1618. }
  1619. #endregion Public Properties
  1620. #region Public Methods
  1621. public int Add (object item)
  1622. {
  1623. int idx;
  1624. idx = AddItem (item);
  1625. owner.UpdatedItems ();
  1626. return idx;
  1627. }
  1628. public void AddRange (object[] items)
  1629. {
  1630. if (items == null)
  1631. throw new ArgumentNullException ("items");
  1632. foreach (object mi in items)
  1633. AddItem (mi);
  1634. owner.UpdatedItems ();
  1635. }
  1636. public void Clear ()
  1637. {
  1638. owner.selected_index = -1;
  1639. object_items.Clear ();
  1640. owner.UpdatedItems ();
  1641. owner.Refresh ();
  1642. }
  1643. public bool Contains (object value)
  1644. {
  1645. if (value == null)
  1646. throw new ArgumentNullException ("value");
  1647. return object_items.Contains (value);
  1648. }
  1649. #if NET_2_0
  1650. public void CopyTo (object [] destination, int arrayIndex)
  1651. {
  1652. object_items.CopyTo (destination, arrayIndex);
  1653. }
  1654. void ICollection.CopyTo (Array destination, int index)
  1655. {
  1656. object_items.CopyTo (destination, index);
  1657. }
  1658. #else
  1659. public void CopyTo (object [] dest, int arrayIndex)
  1660. {
  1661. object_items.CopyTo (dest, arrayIndex);
  1662. }
  1663. void ICollection.CopyTo (Array dest, int index)
  1664. {
  1665. object_items.CopyTo (dest, index);
  1666. }
  1667. #endif
  1668. public IEnumerator GetEnumerator ()
  1669. {
  1670. return object_items.GetEnumerator ();
  1671. }
  1672. int IList.Add (object item)
  1673. {
  1674. return Add (item);
  1675. }
  1676. public int IndexOf (object value)
  1677. {
  1678. if (value == null)
  1679. throw new ArgumentNullException ("value");
  1680. return object_items.IndexOf (value);
  1681. }
  1682. public void Insert (int index, object item)
  1683. {
  1684. if (index < 0 || index > Count)
  1685. throw new ArgumentOutOfRangeException ("index");
  1686. if (item == null)
  1687. throw new ArgumentNullException ("item");
  1688. owner.BeginUpdate ();
  1689. if (owner.Sorted)
  1690. AddItem (item);
  1691. else
  1692. object_items.Insert (index, item);
  1693. owner.EndUpdate (); // Calls UpdatedItems
  1694. }
  1695. public void Remove (object value)
  1696. {
  1697. if (value == null)
  1698. return;
  1699. if (IndexOf (value) == owner.SelectedIndex)
  1700. owner.SelectedIndex = -1;
  1701. RemoveAt (IndexOf (value));
  1702. }
  1703. public void RemoveAt (int index)
  1704. {
  1705. if (index < 0 || index >= Count)
  1706. throw new ArgumentOutOfRangeException ("index");
  1707. if (index == owner.SelectedIndex)
  1708. owner.SelectedIndex = -1;
  1709. object_items.RemoveAt (index);
  1710. owner.UpdatedItems ();
  1711. }
  1712. #endregion Public Methods
  1713. #region Private Methods
  1714. private int AddItem (object item)
  1715. {
  1716. if (item == null)
  1717. throw new ArgumentNullException ("item");
  1718. if (owner.Sorted) {
  1719. int index = 0;
  1720. foreach (object o in object_items) {
  1721. if (String.Compare (item.ToString (), o.ToString ()) < 0) {
  1722. object_items.Insert (index, item);
  1723. // If we added the new item before the selectedindex
  1724. // bump the selectedindex by one, behavior differs if
  1725. // Handle has not been created.
  1726. if (index <= owner.selected_index && owner.IsHandleCreated)
  1727. owner.selected_index++;
  1728. return index;
  1729. }
  1730. index++;
  1731. }
  1732. }
  1733. object_items.Add (item);
  1734. return object_items.Count - 1;
  1735. }
  1736. internal void AddRange (IList items)
  1737. {
  1738. foreach (object mi in items)
  1739. AddItem (mi);
  1740. owner.UpdatedItems ();
  1741. }
  1742. internal void Sort ()
  1743. {
  1744. object_items.Sort ();
  1745. }
  1746. #endregion Private Methods
  1747. }
  1748. internal class ComboTextBox : TextBox {
  1749. private ComboBox owner;
  1750. public ComboTextBox (ComboBox owner)
  1751. {
  1752. this.owner = owner;
  1753. ShowSelection = false;
  1754. HideSelection = false;
  1755. }
  1756. internal void SetSelectable (bool selectable)
  1757. {
  1758. SetStyle (ControlStyles.Selectable, selectable);
  1759. }
  1760. internal void ActivateCaret (bool active)
  1761. {
  1762. if (active)
  1763. document.CaretHasFocus ();
  1764. else
  1765. document.CaretLostFocus ();
  1766. }
  1767. #if NET_2_0
  1768. internal override void OnTextUpdate ()
  1769. {
  1770. base.OnTextUpdate ();
  1771. owner.OnTextUpdate (EventArgs.Empty);
  1772. }
  1773. #endif
  1774. protected override void OnGotFocus (EventArgs e)
  1775. {
  1776. owner.Select (false, true);
  1777. }
  1778. protected override void OnLostFocus (EventArgs e)
  1779. {
  1780. owner.Select (false, true);
  1781. }
  1782. public override bool Focused {
  1783. get {
  1784. return owner.Focused;
  1785. }
  1786. }
  1787. internal override bool ActivateOnShow { get { return false; } }
  1788. }
  1789. internal class ComboListBox : Control
  1790. {
  1791. private ComboBox owner;
  1792. private VScrollBarLB vscrollbar_ctrl;
  1793. private int top_item; /* First item that we show the in the current page */
  1794. private int last_item; /* Last visible item */
  1795. internal int page_size; /* Number of listbox items per page */
  1796. private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
  1797. internal enum ItemNavigation
  1798. {
  1799. First,
  1800. Last,
  1801. Next,
  1802. Previous,
  1803. NextPage,
  1804. PreviousPage,
  1805. }
  1806. class VScrollBarLB : VScrollBar
  1807. {
  1808. public VScrollBarLB ()
  1809. {
  1810. }
  1811. internal override bool InternalCapture {
  1812. get { return Capture; }
  1813. set { }
  1814. }
  1815. public void FireMouseDown (MouseEventArgs e)
  1816. {
  1817. if (!Visible)
  1818. return;
  1819. e = TranslateEvent (e);
  1820. OnMouseDown (e);
  1821. }
  1822. public void FireMouseUp (MouseEventArgs e)
  1823. {
  1824. if (!Visible)
  1825. return;
  1826. e = TranslateEvent (e);
  1827. OnMouseUp (e);
  1828. }
  1829. public void FireMouseMove (MouseEventArgs e)
  1830. {
  1831. if (!Visible)
  1832. return;
  1833. e = TranslateEvent (e);
  1834. OnMouseMove (e);
  1835. }
  1836. MouseEventArgs TranslateEvent (MouseEventArgs e)
  1837. {
  1838. Point loc = PointToClient (Control.MousePosition);
  1839. return new MouseEventArgs (e.Button, e.Clicks, loc.X, loc.Y, e.Delta);
  1840. }
  1841. }
  1842. public ComboListBox (ComboBox owner)
  1843. {
  1844. this.owner = owner;
  1845. top_item = 0;
  1846. last_item = 0;
  1847. page_size = 0;
  1848. MouseWheel += new MouseEventHandler (OnMouseWheelCLB);
  1849. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  1850. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  1851. this.is_visible = false;
  1852. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  1853. InternalBorderStyle = BorderStyle.Fixed3D;
  1854. else
  1855. InternalBorderStyle = BorderStyle.FixedSingle;
  1856. }
  1857. protected override CreateParams CreateParams
  1858. {
  1859. get {
  1860. CreateParams cp = base.CreateParams;
  1861. if (owner == null || owner.DropDownStyle == ComboBoxStyle.Simple)
  1862. return cp;
  1863. cp.Style ^= (int)WindowStyles.WS_CHILD;
  1864. cp.Style ^= (int)WindowStyles.WS_VISIBLE;
  1865. cp.Style |= (int)WindowStyles.WS_POPUP;
  1866. cp.ExStyle |= (int) WindowExStyles.WS_EX_TOOLWINDOW | (int) WindowExStyles.WS_EX_TOPMOST;
  1867. return cp;
  1868. }
  1869. }
  1870. internal override bool InternalCapture {
  1871. get {
  1872. return Capture;
  1873. }
  1874. set {
  1875. }
  1876. }
  1877. internal override bool ActivateOnShow { get { return false; } }
  1878. #region Private Methods
  1879. // Calcs the listbox area
  1880. internal void CalcListBoxArea ()
  1881. {
  1882. int width, height;
  1883. bool show_scrollbar = false;
  1884. if (owner.DropDownStyle == ComboBoxStyle.Simple) {
  1885. Rectangle area = owner.listbox_area;
  1886. width = area.Width;
  1887. height = area.Height;
  1888. // No calculation needed
  1889. if (height <= 0 || width <= 0)
  1890. return;
  1891. }
  1892. else { // DropDown or DropDownList
  1893. width = owner.DropDownWidth;
  1894. int count = (owner.Items.Count <= owner.MaxDropDownItems) ? owner.Items.Count : owner.MaxDropDownItems;
  1895. if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
  1896. height = 0;
  1897. for (int i = 0; i < count; i++) {
  1898. height += owner.GetItemHeight (i);
  1899. }
  1900. } else {
  1901. #if NET_2_0
  1902. height = owner.DropDownHeight;
  1903. #else
  1904. height = owner.ItemHeight * count;
  1905. #endif
  1906. }
  1907. }
  1908. page_size = Math.Max (height / owner.ItemHeight, 1);
  1909. ComboBoxStyle dropdown_style = owner.DropDownStyle;
  1910. if ((dropdown_style != ComboBoxStyle.Simple && owner.Items.Count <= owner.MaxDropDownItems)
  1911. || (dropdown_style == ComboBoxStyle.Simple && owner.Items.Count * owner.ItemHeight < height)) {
  1912. if (vscrollbar_ctrl != null)
  1913. vscrollbar_ctrl.Visible = false;
  1914. if (dropdown_style != ComboBoxStyle.Simple)
  1915. height = owner.ItemHeight * owner.items.Count;
  1916. } else {
  1917. /* Need vertical scrollbar */
  1918. if (vscrollbar_ctrl == null) {
  1919. vscrollbar_ctrl = new VScrollBarLB ();
  1920. vscrollbar_ctrl.Minimum = 0;
  1921. vscrollbar_ctrl.SmallChange = 1;
  1922. vscrollbar_ctrl.LargeChange = 1;
  1923. vscrollbar_ctrl.Maximum = 0;
  1924. vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
  1925. Controls.AddImplicit (vscrollbar_ctrl);
  1926. }
  1927. vscrollbar_ctrl.Dock = DockStyle.Right;
  1928. vscrollbar_ctrl.Maximum = owner.Items.Count - 1;
  1929. #if NET_2_0
  1930. int large = page_size;
  1931. #else
  1932. int large = (dropdown_style == ComboBoxStyle.Simple ? page_size : owner.maxdrop_items) - 1;
  1933. #endif
  1934. if (large < 1)
  1935. large = 1;
  1936. vscrollbar_ctrl.LargeChange = large;
  1937. show_scrollbar = vscrollbar_ctrl.Visible = true;
  1938. int hli = HighlightedIndex;
  1939. if (hli > 0) {
  1940. hli = Math.Min (hli, vscrollbar_ctrl.Maximum);
  1941. vscrollbar_ctrl.Value = hli;
  1942. }
  1943. }
  1944. Size = new Size (width, height);
  1945. textarea_drawable = ClientRectangle;
  1946. textarea_drawable.Width = width;
  1947. textarea_drawable.Height = height;
  1948. if (vscrollbar_ctrl != null && show_scrollbar)
  1949. textarea_drawable.Width -= vscrollbar_ctrl.Width;
  1950. last_item = LastVisibleItem ();
  1951. }
  1952. private void Draw (Rectangle clip, Graphics dc)
  1953. {
  1954. dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (owner.BackColor), clip);
  1955. if (owner.Items.Count > 0) {
  1956. for (int i = top_item; i <= last_item; i++) {
  1957. Rectangle item_rect = GetItemDisplayRectangle (i, top_item);
  1958. if (!clip.IntersectsWith (item_rect))
  1959. continue;
  1960. DrawItemState state = DrawItemState.None;
  1961. if (i == HighlightedIndex) {
  1962. state |= DrawItemState.Selected;
  1963. if (owner.DropDownStyle == ComboBoxStyle.DropDownList) {
  1964. state |= DrawItemState.Focus;
  1965. }
  1966. }
  1967. owner.OnDrawItem (new DrawItemEventArgs (dc, owner.Font, item_rect,
  1968. i, state, owner.ForeColor, owner.BackColor));
  1969. }
  1970. }
  1971. }
  1972. int highlighted_index = -1;
  1973. public int HighlightedIndex {
  1974. get { return highlighted_index; }
  1975. set {
  1976. if (highlighted_index == value)
  1977. return;
  1978. if (highlighted_index != -1 && highlighted_index < this.owner.Items.Count)
  1979. Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
  1980. highlighted_index = value;
  1981. if (highlighted_index != -1)
  1982. Invalidate (GetItemDisplayRectangle (highlighted_index, top_item));
  1983. }
  1984. }
  1985. private Rectangle GetItemDisplayRectangle (int index, int top_index)
  1986. {
  1987. if (index < 0 || index >= owner.Items.Count)
  1988. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  1989. Rectangle item_rect = new Rectangle ();
  1990. int height = owner.GetItemHeight (index);
  1991. item_rect.X = 0;
  1992. item_rect.Width = textarea_drawable.Width;
  1993. if (owner.DrawMode == DrawMode.OwnerDrawVariable) {
  1994. item_rect.Y = 0;
  1995. for (int i = top_index; i < index; i++)
  1996. item_rect.Y += owner.GetItemHeight (i);
  1997. } else
  1998. item_rect.Y = height * (index - top_index);
  1999. item_rect.Height = height;
  2000. return item_rect;
  2001. }
  2002. public void HideWindow ()
  2003. {
  2004. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  2005. return;
  2006. Capture = false;
  2007. Hide ();
  2008. owner.DropDownListBoxFinished ();
  2009. }
  2010. private int IndexFromPointDisplayRectangle (int x, int y)
  2011. {
  2012. for (int i = top_item; i <= last_item; i++) {
  2013. if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
  2014. return i;
  2015. }
  2016. return -1;
  2017. }
  2018. public void InvalidateItem (int index)
  2019. {
  2020. if (Visible)
  2021. Invalidate (GetItemDisplayRectangle (index, top_item));
  2022. }
  2023. public int LastVisibleItem ()
  2024. {
  2025. Rectangle item_rect;
  2026. int top_y = textarea_drawable.Y + textarea_drawable.Height;
  2027. int i = 0;
  2028. for (i = top_item; i < owner.Items.Count; i++) {
  2029. item_rect = GetItemDisplayRectangle (i, top_item);
  2030. if (item_rect.Y + item_rect.Height > top_y) {
  2031. return i;
  2032. }
  2033. }
  2034. return i - 1;
  2035. }
  2036. public void SetTopItem (int item)
  2037. {
  2038. if (top_item == item)
  2039. return;
  2040. top_item = item;
  2041. UpdateLastVisibleItem ();
  2042. Invalidate ();
  2043. }
  2044. public int FirstVisibleItem ()
  2045. {
  2046. return top_item;
  2047. }
  2048. bool scrollbar_grabbed = false;
  2049. bool InScrollBar {
  2050. get {
  2051. if (vscrollbar_ctrl == null || !vscrollbar_ctrl.is_visible)
  2052. return false;
  2053. return vscrollbar_ctrl.Bounds.Contains (PointToClient (Control.MousePosition));
  2054. }
  2055. }
  2056. protected override void OnMouseDown (MouseEventArgs e)
  2057. {
  2058. if (InScrollBar) {
  2059. vscrollbar_ctrl.FireMouseDown (e);
  2060. scrollbar_grabbed = true;
  2061. }
  2062. }
  2063. protected override void OnMouseMove (MouseEventArgs e)
  2064. {
  2065. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  2066. return;
  2067. if (scrollbar_grabbed || (!Capture && InScrollBar)) {
  2068. vscrollbar_ctrl.FireMouseMove (e);
  2069. return;
  2070. }
  2071. Point pt = PointToClient (Control.MousePosition);
  2072. int index = IndexFromPointDisplayRectangle (pt.X, pt.Y);
  2073. if (index != -1)
  2074. HighlightedIndex = index;
  2075. }
  2076. protected override void OnMouseUp (MouseEventArgs e)
  2077. {
  2078. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  2079. if (scrollbar_grabbed) {
  2080. vscrollbar_ctrl.FireMouseUp (e);
  2081. scrollbar_grabbed = false;
  2082. if (index != -1)
  2083. HighlightedIndex = index;
  2084. return;
  2085. }
  2086. if (index == -1) {
  2087. HideWindow ();
  2088. return;
  2089. }
  2090. owner.SelectedIndex = index;
  2091. owner.OnSelectionChangeCommitted (new EventArgs ());
  2092. HideWindow ();
  2093. }
  2094. internal override void OnPaintInternal (PaintEventArgs pevent)
  2095. {
  2096. Draw (pevent.ClipRectangle,pevent.Graphics);
  2097. }
  2098. public bool ShowWindow ()
  2099. {
  2100. if (owner.DropDownStyle == ComboBoxStyle.Simple && owner.Items.Count == 0)
  2101. return false;
  2102. HighlightedIndex = owner.SelectedIndex;
  2103. CalcListBoxArea ();
  2104. Show ();
  2105. Refresh ();
  2106. owner.OnDropDown (EventArgs.Empty);
  2107. return true;
  2108. }
  2109. public void UpdateLastVisibleItem ()
  2110. {
  2111. last_item = LastVisibleItem ();
  2112. }
  2113. public void Scroll (int delta)
  2114. {
  2115. if (delta == 0 || vscrollbar_ctrl == null || !vscrollbar_ctrl.Visible)
  2116. return;
  2117. int max = vscrollbar_ctrl.Maximum - page_size + 1;
  2118. int val = vscrollbar_ctrl.Value + delta;
  2119. if (val > max)
  2120. val = max;
  2121. else if (val < vscrollbar_ctrl.Minimum)
  2122. val = vscrollbar_ctrl.Minimum;
  2123. vscrollbar_ctrl.Value = val;
  2124. }
  2125. private void OnMouseWheelCLB (object sender, MouseEventArgs me)
  2126. {
  2127. if (owner.Items.Count == 0)
  2128. return;
  2129. int lines = me.Delta / 120 * SystemInformation.MouseWheelScrollLines;
  2130. Scroll (-lines);
  2131. }
  2132. // Value Changed
  2133. private void VerticalScrollEvent (object sender, EventArgs e)
  2134. {
  2135. if (top_item == vscrollbar_ctrl.Value)
  2136. return;
  2137. top_item = vscrollbar_ctrl.Value;
  2138. UpdateLastVisibleItem ();
  2139. Invalidate ();
  2140. }
  2141. protected override void WndProc(ref Message m) {
  2142. if (m.Msg == (int)Msg.WM_SETFOCUS) {
  2143. owner.Select (false, true);
  2144. }
  2145. base.WndProc (ref m);
  2146. }
  2147. #endregion Private Methods
  2148. }
  2149. }
  2150. }