ComboBox.cs 64 KB

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