ComboBox.cs 57 KB

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