ComboBox.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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 Novell, Inc.
  21. //
  22. // Authors:
  23. // Jordi Mas i Hernandez, [email protected]
  24. //
  25. //
  26. // NOT COMPLETE
  27. using System;
  28. using System.Drawing;
  29. using System.Collections;
  30. using System.ComponentModel;
  31. using System.Reflection;
  32. namespace System.Windows.Forms
  33. {
  34. public class ComboBox : ListControl
  35. {
  36. private DrawMode draw_mode;
  37. private ComboBoxStyle dropdown_style;
  38. private int dropdown_width;
  39. private int max_length;
  40. private int preferred_height;
  41. private int selected_index;
  42. private object selected_item;
  43. internal ObjectCollection items;
  44. private bool suspend_ctrlupdate;
  45. private int maxdrop_items;
  46. private bool integral_height;
  47. private bool sorted;
  48. internal ComboBoxInfo combobox_info;
  49. private readonly int def_button_width = 16;
  50. private bool clicked;
  51. private ComboListBox listbox_ctrl;
  52. private StringFormat string_format;
  53. private TextBox textbox_ctrl;
  54. internal class ComboBoxInfo
  55. {
  56. internal int item_height; /* Item's height */
  57. internal Rectangle textarea; /* Rectangle of the editable text area */
  58. internal Rectangle textarea_drawable; /* Rectangle of the editable text area - decorations */
  59. internal Rectangle button_rect;
  60. internal bool show_button; /* Is the DropDown button shown? */
  61. internal ButtonState button_status; /* Drop button status */
  62. internal Size listbox_size;
  63. internal Rectangle listbox_area; /* ListBox area in Simple combox, not used in the rest */
  64. public ComboBoxInfo ()
  65. {
  66. button_status = ButtonState.Normal;
  67. show_button = false;
  68. item_height = 0;
  69. }
  70. }
  71. internal class ComboBoxItem
  72. {
  73. internal int Index;
  74. public ComboBoxItem (int index)
  75. {
  76. Index = index;
  77. }
  78. }
  79. public ComboBox ()
  80. {
  81. listbox_ctrl = null;
  82. textbox_ctrl = null;
  83. combobox_info = new ComboBoxInfo ();
  84. DropDownStyle = ComboBoxStyle.DropDown;
  85. BackColor = ThemeEngine.Current.ColorWindow;
  86. draw_mode = DrawMode.Normal;
  87. selected_index = -1;
  88. selected_item = null;
  89. maxdrop_items = 8;
  90. combobox_info.item_height = FontHeight + 2;
  91. suspend_ctrlupdate = false;
  92. clicked = false;
  93. items = new ObjectCollection (this);
  94. string_format = new StringFormat ();
  95. /* Events */
  96. MouseDown += new MouseEventHandler (OnMouseDownCB);
  97. MouseUp += new MouseEventHandler (OnMouseUpCB);
  98. }
  99. #region Events
  100. public new event EventHandler BackgroundImageChanged;
  101. public event DrawItemEventHandler DrawItem;
  102. public event EventHandler DropDown;
  103. public event EventHandler DropDownStyleChanged;
  104. public event MeasureItemEventHandler MeasureItem;
  105. public new event PaintEventHandler Paint;
  106. public event EventHandler SelectedIndexChanged;
  107. public event EventHandler SelectionChangeCommitted;
  108. #endregion Events
  109. #region Public Properties
  110. public override Color BackColor {
  111. get { return base.BackColor; }
  112. set {
  113. if (base.BackColor == value)
  114. return;
  115. base.BackColor = value;
  116. Refresh ();
  117. }
  118. }
  119. public override Image BackgroundImage {
  120. get { return base.BackgroundImage; }
  121. set {
  122. if (base.BackgroundImage == value)
  123. return;
  124. base.BackgroundImage = value;
  125. if (BackgroundImageChanged != null)
  126. BackgroundImageChanged (this, EventArgs.Empty);
  127. Refresh ();
  128. }
  129. }
  130. protected override CreateParams CreateParams {
  131. get { return base.CreateParams;}
  132. }
  133. protected override Size DefaultSize {
  134. get { return new Size (121, PreferredHeight); }
  135. }
  136. public DrawMode DrawMode {
  137. get { return draw_mode; }
  138. set {
  139. if (!Enum.IsDefined (typeof (DrawMode), value))
  140. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
  141. if (draw_mode == value)
  142. return;
  143. draw_mode = value;
  144. Refresh ();
  145. }
  146. }
  147. public ComboBoxStyle DropDownStyle {
  148. get { return dropdown_style; }
  149. set {
  150. if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
  151. throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
  152. if (dropdown_style == value)
  153. return;
  154. if (dropdown_style != ComboBoxStyle.DropDownList) {
  155. if (textbox_ctrl != null) {
  156. Controls.Remove (textbox_ctrl);
  157. textbox_ctrl.Dispose ();
  158. textbox_ctrl = null;
  159. }
  160. }
  161. dropdown_style = value;
  162. if (dropdown_style == ComboBoxStyle.Simple) {
  163. CBoxInfo.show_button = false;
  164. CreateComboListBox ();
  165. Controls.Add (listbox_ctrl);
  166. }
  167. else {
  168. CBoxInfo.show_button = true;
  169. if (listbox_ctrl != null) {
  170. listbox_ctrl.Dispose ();
  171. Controls.Remove (listbox_ctrl);
  172. listbox_ctrl = null;
  173. }
  174. }
  175. if (dropdown_style != ComboBoxStyle.DropDownList) {
  176. textbox_ctrl = new TextBox ();
  177. Controls.Add (textbox_ctrl);
  178. CalcTextArea ();
  179. }
  180. if (DropDownStyleChanged != null)
  181. DropDownStyleChanged (this, EventArgs.Empty);
  182. Refresh ();
  183. }
  184. }
  185. public int DropDownWidth {
  186. get { return dropdown_width; }
  187. set {
  188. if (dropdown_width == value)
  189. return;
  190. dropdown_width = value;
  191. Refresh ();
  192. }
  193. }
  194. public override bool Focused {
  195. get { return base.Focused; }
  196. }
  197. public override Color ForeColor {
  198. get { return base.ForeColor; }
  199. set {
  200. if (base.ForeColor == value)
  201. return;
  202. base.ForeColor = value;
  203. Refresh ();
  204. }
  205. }
  206. public bool IntegralHeight {
  207. get { return integral_height; }
  208. set {
  209. if (integral_height == value)
  210. return;
  211. integral_height = value;
  212. Refresh ();
  213. }
  214. }
  215. public virtual int ItemHeight {
  216. get { return combobox_info.item_height; }
  217. set {
  218. if (value < 0)
  219. throw new ArgumentOutOfRangeException ("The item height value is less than zero");
  220. combobox_info.item_height = value;
  221. Refresh ();
  222. }
  223. }
  224. public ComboBox.ObjectCollection Items {
  225. get { return items; }
  226. }
  227. public int MaxDropDownItems {
  228. get { return maxdrop_items; }
  229. set {
  230. if (maxdrop_items == value)
  231. return;
  232. maxdrop_items = value;
  233. }
  234. }
  235. public int MaxLength {
  236. get { return max_length; }
  237. set {
  238. if (max_length == value)
  239. return;
  240. max_length = value;
  241. }
  242. }
  243. public int PreferredHeight {
  244. get { return preferred_height; }
  245. }
  246. public override int SelectedIndex {
  247. get { return selected_index; }
  248. set {
  249. if (value < -2 || value >= Items.Count)
  250. throw new ArgumentOutOfRangeException ("Index of out range");
  251. if (selected_index == value)
  252. return;
  253. selected_index = value;
  254. if (dropdown_style != ComboBoxStyle.DropDownList) {
  255. textbox_ctrl.Text = Items[selected_index].ToString ();
  256. }
  257. OnSelectedIndexChanged (new EventArgs ());
  258. Refresh ();
  259. }
  260. }
  261. public object SelectedItem {
  262. get {
  263. if (selected_index !=-1 && Items.Count > 0)
  264. return Items[selected_index];
  265. else
  266. return null;
  267. }
  268. set {
  269. if (selected_item == value)
  270. return;
  271. int index = Items.IndexOf (value);
  272. if (index == -1)
  273. return;
  274. selected_index = index;
  275. if (dropdown_style != ComboBoxStyle.DropDownList) {
  276. textbox_ctrl.Text = Items[selected_index].ToString ();
  277. }
  278. OnSelectedItemChanged (new EventArgs ());
  279. Refresh ();
  280. }
  281. }
  282. public string SelectedText {
  283. get {
  284. if (dropdown_style == ComboBoxStyle.DropDownList)
  285. return "";
  286. return textbox_ctrl.Text;
  287. }
  288. set {
  289. if (dropdown_style == ComboBoxStyle.DropDownList)
  290. return;
  291. if (textbox_ctrl.Text == value)
  292. return;
  293. textbox_ctrl.SelectedText = value;
  294. }
  295. }
  296. public int SelectionLength {
  297. get {
  298. if (dropdown_style == ComboBoxStyle.DropDownList)
  299. return 0;
  300. return textbox_ctrl.SelectionLength;
  301. }
  302. set {
  303. if (dropdown_style == ComboBoxStyle.DropDownList)
  304. return;
  305. if (textbox_ctrl.SelectionLength == value)
  306. return;
  307. textbox_ctrl.SelectionLength = value;
  308. }
  309. }
  310. public int SelectionStart {
  311. get {
  312. if (dropdown_style == ComboBoxStyle.DropDownList)
  313. return 0;
  314. return textbox_ctrl.SelectionStart;
  315. }
  316. set {
  317. if (dropdown_style == ComboBoxStyle.DropDownList)
  318. return;
  319. if (textbox_ctrl.SelectionStart == value)
  320. return;
  321. textbox_ctrl.SelectionStart = value;
  322. }
  323. }
  324. public bool Sorted {
  325. get { return sorted; }
  326. set {
  327. if (sorted == value)
  328. return;
  329. sorted = value;
  330. }
  331. }
  332. public override string Text {
  333. get { return ""; /*throw new NotImplementedException ();*/ }
  334. set {}
  335. }
  336. #endregion Public Properties
  337. #region Private Properties
  338. internal ComboBoxInfo CBoxInfo {
  339. get { return combobox_info; }
  340. }
  341. #endregion Private Properties
  342. #region Public Methods
  343. protected virtual void AddItemsCore (object[] value)
  344. {
  345. }
  346. public void BeginUpdate ()
  347. {
  348. suspend_ctrlupdate = true;
  349. }
  350. protected virtual void Dispose (bool disposing)
  351. {
  352. }
  353. public void EndUpdate ()
  354. {
  355. suspend_ctrlupdate = false;
  356. Refresh ();
  357. }
  358. public int FindString (string s)
  359. {
  360. return FindString (s, 0);
  361. }
  362. public int FindString (string s, int startIndex)
  363. {
  364. for (int i = startIndex; i < Items.Count; i++) {
  365. if ((Items[i].ToString ()).StartsWith (s))
  366. return i;
  367. }
  368. return -1;
  369. }
  370. public int FindStringExact (string s)
  371. {
  372. return FindStringExact (s, 0);
  373. }
  374. public int FindStringExact (string s, int startIndex)
  375. {
  376. for (int i = startIndex; i < Items.Count; i++) {
  377. if ((Items[i].ToString ()).Equals (s))
  378. return i;
  379. }
  380. return -1;
  381. }
  382. public int GetItemHeight (int index)
  383. {
  384. throw new NotImplementedException ();
  385. }
  386. protected override bool IsInputKey (Keys keyData)
  387. {
  388. return false;
  389. }
  390. protected override void OnBackColorChanged (EventArgs e)
  391. {
  392. base.OnBackColorChanged (e);
  393. }
  394. protected override void OnDataSourceChanged (EventArgs e)
  395. {
  396. base.OnDataSourceChanged (e);
  397. }
  398. protected override void OnDisplayMemberChanged (EventArgs e)
  399. {
  400. base.OnDisplayMemberChanged (e);
  401. }
  402. protected virtual void OnDrawItem (DrawItemEventArgs e)
  403. {
  404. if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable)) {
  405. DrawItem (this, e);
  406. return;
  407. }
  408. Rectangle text_draw = e.Bounds;
  409. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
  410. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  411. (ThemeEngine.Current.ColorHilight), text_draw);
  412. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  413. ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHilightText),
  414. text_draw, string_format);
  415. // It seems to be a bug in CPDrawFocusRectangle
  416. //ThemeEngine.Current.CPDrawFocusRectangle (e.Graphics, e.Bounds,
  417. // ThemeEngine.Current.ColorHilightText, BackColor);
  418. }
  419. else {
  420. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  421. (e.BackColor), e.Bounds);
  422. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  423. ThemeEngine.Current.ResPool.GetSolidBrush (e.ForeColor),
  424. text_draw, string_format);
  425. }
  426. }
  427. protected virtual void OnDropDownStyleChanged (EventArgs e)
  428. {
  429. }
  430. protected override void OnFontChanged (EventArgs e)
  431. {
  432. base.OnFontChanged (e);
  433. if (textbox_ctrl != null) {
  434. textbox_ctrl.Font = Font;
  435. }
  436. combobox_info.item_height = FontHeight + 2;
  437. }
  438. protected override void OnForeColorChanged (EventArgs e)
  439. {
  440. base.OnForeColorChanged (e);
  441. }
  442. protected override void OnHandleCreated (EventArgs e)
  443. {
  444. base.OnHandleCreated (e);
  445. CalcTextArea ();
  446. }
  447. protected override void OnHandleDestroyed (EventArgs e)
  448. {
  449. base.OnHandleDestroyed (e);
  450. }
  451. protected override void OnKeyPress (KeyPressEventArgs e)
  452. {
  453. }
  454. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  455. {
  456. }
  457. protected override void OnParentBackColorChanged (EventArgs e)
  458. {
  459. base.OnParentBackColorChanged (e);
  460. }
  461. protected override void OnResize (EventArgs e)
  462. {
  463. base.OnResize (e);
  464. CalcTextArea ();
  465. }
  466. protected override void OnSelectedIndexChanged (EventArgs e)
  467. {
  468. base.OnSelectedIndexChanged (e);
  469. if (SelectedIndexChanged != null)
  470. SelectedIndexChanged (this, e);
  471. }
  472. protected virtual void OnSelectedItemChanged (EventArgs e)
  473. {
  474. }
  475. protected override void OnSelectedValueChanged (EventArgs e)
  476. {
  477. base.OnSelectedValueChanged (e);
  478. }
  479. protected virtual void OnSelectionChangeCommitted (EventArgs e)
  480. {
  481. }
  482. protected override void RefreshItem (int index)
  483. {
  484. }
  485. protected virtual void Select (int start, int lenght)
  486. {
  487. }
  488. public void SelectAll ()
  489. {
  490. }
  491. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  492. {
  493. base.SetBoundsCore (x, y, width, height, specified);
  494. }
  495. protected override void SetItemCore (int index, object value)
  496. {
  497. if (index < 0 || index >= Items.Count)
  498. return;
  499. Items[index] = value;
  500. }
  501. protected override void SetItemsCore (IList value)
  502. {
  503. }
  504. public override string ToString ()
  505. {
  506. throw new NotImplementedException ();
  507. }
  508. protected override void WndProc (ref Message m)
  509. {
  510. switch ((Msg) m.Msg) {
  511. case Msg.WM_PAINT: {
  512. PaintEventArgs paint_event;
  513. paint_event = XplatUI.PaintEventStart (Handle);
  514. OnPaintCB (paint_event);
  515. XplatUI.PaintEventEnd (Handle);
  516. return;
  517. }
  518. case Msg.WM_ERASEBKGND:
  519. m.Result = (IntPtr) 1;
  520. return;
  521. default:
  522. break;
  523. }
  524. base.WndProc (ref m);
  525. }
  526. #endregion Public Methods
  527. #region Private Methods
  528. internal void ButtonReleased ()
  529. {
  530. combobox_info.button_status = ButtonState.Normal;
  531. Invalidate (combobox_info.button_rect);
  532. }
  533. // Calcs the text area size
  534. internal void CalcTextArea ()
  535. {
  536. combobox_info.textarea = ClientRectangle;
  537. /* Edit area */
  538. combobox_info.textarea.Height = ItemHeight + ThemeEngine.Current.DrawComboBoxEditDecorationTop () +
  539. ThemeEngine.Current.DrawComboBoxEditDecorationBottom () + 2;
  540. // TODO: Does the +2 changes a different font resolutions?
  541. /* Edit area - minus decorations (text drawable area) */
  542. combobox_info.textarea_drawable = combobox_info.textarea;
  543. combobox_info.textarea_drawable.Y += ThemeEngine.Current.DrawComboBoxEditDecorationTop ();
  544. combobox_info.textarea_drawable.X += ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
  545. combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationBottom ();
  546. combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationTop();
  547. combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationRight ();
  548. combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
  549. /* Non-drawable area */
  550. Region area = new Region (ClientRectangle);
  551. area.Exclude (combobox_info.textarea);
  552. RectangleF bounds = area.GetBounds (DeviceContext);
  553. combobox_info.listbox_area = new Rectangle ((int)bounds.X, (int)bounds.Y,
  554. (int)bounds.Width, (int)bounds.Height);
  555. if (CBoxInfo.show_button) {
  556. combobox_info.textarea_drawable.Width -= def_button_width;
  557. combobox_info.button_rect = new Rectangle (combobox_info.textarea_drawable.X + combobox_info.textarea_drawable.Width,
  558. combobox_info.textarea_drawable.Y, def_button_width, combobox_info.textarea_drawable.Height);
  559. }
  560. if (dropdown_style != ComboBoxStyle.DropDownList) { /* There is an edit control*/
  561. if (textbox_ctrl != null) {
  562. textbox_ctrl.Location = new Point (combobox_info.textarea_drawable.X, combobox_info.textarea_drawable.Y);
  563. textbox_ctrl.Size = new Size (combobox_info.textarea_drawable.Width, combobox_info.textarea_drawable.Height);
  564. }
  565. }
  566. if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
  567. listbox_ctrl.Location = new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
  568. combobox_info.textarea.Height);
  569. listbox_ctrl.CalcListBoxArea ();
  570. }
  571. }
  572. private void CreateComboListBox ()
  573. {
  574. listbox_ctrl = new ComboListBox (this);
  575. //listbox_ctrl.Size = combobox_info.listbox_size;
  576. listbox_ctrl.Size = new Size (100, 100);
  577. }
  578. internal void Draw (Rectangle clip)
  579. {
  580. // No edit control, we paint the edit ourselfs
  581. if (dropdown_style == ComboBoxStyle.DropDownList) {
  582. if (selected_index != -1) {
  583. Rectangle item_rect = combobox_info.textarea_drawable;
  584. item_rect.Height = ItemHeight;
  585. OnDrawItem (new DrawItemEventArgs (DeviceContext, Font, item_rect,
  586. selected_index, DrawItemState.Selected,
  587. ForeColor, BackColor));
  588. }
  589. else
  590. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
  591. combobox_info.textarea_drawable);
  592. }
  593. if (clip.IntersectsWith (combobox_info.listbox_area) == true) {
  594. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (Parent.BackColor),
  595. combobox_info.listbox_area);
  596. }
  597. if (CBoxInfo.show_button) {
  598. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorButtonFace),
  599. combobox_info.button_rect);
  600. ThemeEngine.Current.CPDrawComboButton (DeviceContext,
  601. combobox_info.button_rect, combobox_info.button_status);
  602. }
  603. ThemeEngine.Current.DrawComboBoxEditDecorations (DeviceContext, this, combobox_info.textarea);
  604. }
  605. internal virtual void OnMouseDownCB (object sender, MouseEventArgs e)
  606. {
  607. /* Click On button*/
  608. if (clicked == false && combobox_info.button_rect.Contains (e.X, e.Y)) {
  609. clicked = true;
  610. if (combobox_info.button_status == ButtonState.Normal) {
  611. combobox_info.button_status = ButtonState.Pushed;
  612. }
  613. else {
  614. if (combobox_info.button_status == ButtonState.Pushed) {
  615. combobox_info.button_status = ButtonState.Normal;
  616. }
  617. }
  618. if (combobox_info.button_status == ButtonState.Pushed) {
  619. if (listbox_ctrl == null) {
  620. CreateComboListBox ();
  621. }
  622. listbox_ctrl.Location = PointToScreen (new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
  623. combobox_info.textarea.Height));
  624. listbox_ctrl.ShowWindow ();
  625. }
  626. Invalidate (combobox_info.button_rect);
  627. }
  628. }
  629. internal virtual void OnMouseUpCB (object sender, MouseEventArgs e)
  630. {
  631. /* Click on button*/
  632. if (clicked == true && combobox_info.button_rect.Contains (e.X, e.Y)) {
  633. clicked = false;
  634. }
  635. }
  636. private void OnPaintCB (PaintEventArgs pevent)
  637. {
  638. if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
  639. return;
  640. Rectangle rect = ClientRectangle;
  641. /* Copies memory drawing buffer to screen*/
  642. Draw (rect);
  643. pevent.Graphics.DrawImage (ImageBuffer, rect, rect, GraphicsUnit.Pixel);
  644. if (Paint != null)
  645. Paint (this, pevent);
  646. }
  647. #endregion Private Methods
  648. /*
  649. ComboBox.ObjectCollection
  650. */
  651. public class ObjectCollection : IList, ICollection, IEnumerable
  652. {
  653. private ComboBox owner;
  654. internal ArrayList object_items = new ArrayList ();
  655. internal ArrayList listbox_items = new ArrayList ();
  656. public ObjectCollection (ComboBox owner)
  657. {
  658. this.owner = owner;
  659. }
  660. #region Public Properties
  661. public virtual int Count {
  662. get { return object_items.Count; }
  663. }
  664. public virtual bool IsReadOnly {
  665. get { return false; }
  666. }
  667. public virtual object this [int index] {
  668. get {
  669. if (index < 0 || index >= Count)
  670. throw new ArgumentOutOfRangeException ("Index of out range");
  671. return object_items[index];
  672. }
  673. set {
  674. if (index < 0 || index >= Count)
  675. throw new ArgumentOutOfRangeException ("Index of out range");
  676. object_items[index] = value;
  677. }
  678. }
  679. bool ICollection.IsSynchronized {
  680. get { return false; }
  681. }
  682. object ICollection.SyncRoot {
  683. get { return this; }
  684. }
  685. bool IList.IsFixedSize {
  686. get { return false; }
  687. }
  688. #endregion Public Properties
  689. #region Public Methods
  690. public int Add (object item)
  691. {
  692. int idx;
  693. idx = AddItem (item);
  694. return idx;
  695. }
  696. public void AddRange (object[] items)
  697. {
  698. foreach (object mi in items)
  699. AddItem (mi);
  700. }
  701. public virtual void Clear ()
  702. {
  703. object_items.Clear ();
  704. listbox_items.Clear ();
  705. }
  706. public virtual bool Contains (object obj)
  707. {
  708. return object_items.Contains (obj);
  709. }
  710. public void CopyTo (object[] dest, int arrayIndex)
  711. {
  712. object_items.CopyTo (dest, arrayIndex);
  713. }
  714. void ICollection.CopyTo (Array dest, int index)
  715. {
  716. object_items.CopyTo (dest, index);
  717. }
  718. public virtual IEnumerator GetEnumerator ()
  719. {
  720. return object_items.GetEnumerator ();
  721. }
  722. int IList.Add (object item)
  723. {
  724. return Add (item);
  725. }
  726. public virtual int IndexOf (object value)
  727. {
  728. return object_items.IndexOf (value);
  729. }
  730. public virtual void Insert (int index, object item)
  731. {
  732. throw new NotImplementedException ();
  733. }
  734. public virtual void Remove (object value)
  735. {
  736. RemoveAt (IndexOf (value));
  737. }
  738. public virtual void RemoveAt (int index)
  739. {
  740. if (index < 0 || index >= Count)
  741. throw new ArgumentOutOfRangeException ("Index of out range");
  742. object_items.RemoveAt (index);
  743. listbox_items.RemoveAt (index);
  744. //owner.UpdateItemInfo (false, -1, -1);
  745. }
  746. #endregion Public Methods
  747. #region Private Methods
  748. private int AddItem (object item)
  749. {
  750. int cnt = object_items.Count;
  751. object_items.Add (item);
  752. listbox_items.Add (new ComboBox.ComboBoxItem (cnt));
  753. return cnt;
  754. }
  755. internal ComboBox.ComboBoxItem GetComboBoxItem (int index)
  756. {
  757. if (index < 0 || index >= Count)
  758. throw new ArgumentOutOfRangeException ("Index of out range");
  759. return (ComboBox.ComboBoxItem) listbox_items[index];
  760. }
  761. internal void SetComboBoxItem (ComboBox.ComboBoxItem item, int index)
  762. {
  763. if (index < 0 || index >= Count)
  764. throw new ArgumentOutOfRangeException ("Index of out range");
  765. listbox_items[index] = item;
  766. }
  767. #endregion Private Methods
  768. }
  769. /*
  770. class ComboListBox
  771. */
  772. internal class ComboListBox : Control
  773. {
  774. private ComboBox owner;
  775. private bool need_vscrollbar;
  776. private VScrollBar vscrollbar_ctrl;
  777. private int top_item; /* First item that we show the in the current page */
  778. private int last_item; /* Last visible item */
  779. private int highlighted_item; /* Item that is currently selected */
  780. internal int page_size; /* Number of listbox items per page */
  781. private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
  782. internal enum ItemNavigation
  783. {
  784. First,
  785. Last,
  786. Next,
  787. Previous,
  788. NextPage,
  789. PreviousPage,
  790. }
  791. public ComboListBox (ComboBox owner) : base ()
  792. {
  793. this.owner = owner;
  794. need_vscrollbar = false;
  795. top_item = 0;
  796. last_item = 0;
  797. page_size = 0;
  798. highlighted_item = -1;
  799. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  800. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  801. MouseUp += new MouseEventHandler (OnMouseUpPUW);
  802. KeyDown += new KeyEventHandler (OnKeyDownPUW);
  803. Paint += new PaintEventHandler (OnPaintPUW);
  804. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  805. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  806. /* Vertical scrollbar */
  807. vscrollbar_ctrl = new VScrollBar ();
  808. vscrollbar_ctrl.Minimum = 0;
  809. vscrollbar_ctrl.SmallChange = 1;
  810. vscrollbar_ctrl.LargeChange = 1;
  811. vscrollbar_ctrl.Maximum = 0;
  812. vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
  813. vscrollbar_ctrl.Visible = false;
  814. }
  815. protected override CreateParams CreateParams
  816. {
  817. get {
  818. CreateParams cp = base.CreateParams;
  819. if (owner != null && owner.DropDownStyle != ComboBoxStyle.Simple) {
  820. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
  821. cp.ExStyle |= (int)WindowStyles.WS_EX_TOOLWINDOW;
  822. }
  823. return cp;
  824. }
  825. }
  826. #region Private Methods
  827. protected override void CreateHandle ()
  828. {
  829. base.CreateHandle ();
  830. Controls.Add (vscrollbar_ctrl);
  831. }
  832. // Calcs the listbox area
  833. internal void CalcListBoxArea ()
  834. {
  835. int width, height;
  836. if (owner.DropDownStyle == ComboBoxStyle.Simple) {
  837. width = owner.CBoxInfo.listbox_area.Width;
  838. height = owner.CBoxInfo.listbox_area.Height;
  839. if (owner.IntegralHeight == true) {
  840. int remaining = (height - 2 -
  841. ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
  842. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle)) %
  843. (owner.ItemHeight - 2);
  844. Console.WriteLine("CalcListBoxArea h:{0} remaining {1}, itemh {2}",
  845. height - 2 -
  846. ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle)-
  847. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle),
  848. remaining, (owner.ItemHeight - 2));
  849. if (remaining > 0) {
  850. height -= remaining;
  851. }
  852. }
  853. }
  854. else {
  855. width = owner.ClientRectangle.Width;
  856. if (owner.Items.Count <= owner.MaxDropDownItems) {
  857. height = (owner.ItemHeight - 2) * owner.Items.Count;
  858. }
  859. else {
  860. height = (owner.ItemHeight - 2) * owner.MaxDropDownItems;
  861. }
  862. height += ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
  863. height += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  864. }
  865. if (owner.Items.Count <= owner.MaxDropDownItems) {
  866. need_vscrollbar = false;
  867. }
  868. else {
  869. need_vscrollbar = true;
  870. vscrollbar_ctrl.Height = height - ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
  871. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  872. vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle),
  873. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle));
  874. vscrollbar_ctrl.Maximum = owner.Items.Count - owner.MaxDropDownItems;
  875. }
  876. if (vscrollbar_ctrl.Visible != need_vscrollbar)
  877. vscrollbar_ctrl.Visible = need_vscrollbar;
  878. Size = new Size (width, height);
  879. textarea_drawable = ClientRectangle;
  880. textarea_drawable.Width = width;
  881. textarea_drawable.Height = height;
  882. // Exclude decorations
  883. textarea_drawable.X += ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
  884. textarea_drawable.Y += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  885. textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
  886. textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
  887. textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
  888. textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  889. if (need_vscrollbar)
  890. textarea_drawable.Width -= vscrollbar_ctrl.Width;
  891. last_item = LastVisibleItem ();
  892. page_size = textarea_drawable.Height / (owner.ItemHeight - 2);
  893. Console.WriteLine ("ComboListBox.CalcListBoxArea {0} page_size {1}, dh: {2}, itemh {3}", textarea_drawable,
  894. page_size, textarea_drawable.Height, (owner.ItemHeight - 2));
  895. }
  896. private void Draw (Rectangle clip)
  897. {
  898. Rectangle cl = ClientRectangle;
  899. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  900. (owner.BackColor), ClientRectangle);
  901. if (owner.Items.Count > 0) {
  902. Rectangle item_rect;
  903. DrawItemState state = DrawItemState.None;
  904. for (int i = top_item; i <= last_item; i++) {
  905. item_rect = GetItemDisplayRectangle (i, top_item);
  906. if (clip.IntersectsWith (item_rect) == false)
  907. continue;
  908. /* Draw item */
  909. state = DrawItemState.None;
  910. if (i == highlighted_item)
  911. state |= DrawItemState.Selected;
  912. owner.OnDrawItem (new DrawItemEventArgs (DeviceContext, owner.Font, item_rect,
  913. i, state, owner.ForeColor, owner.BackColor));
  914. }
  915. }
  916. ThemeEngine.Current.DrawComboListBoxDecorations (DeviceContext, owner, ClientRectangle);
  917. }
  918. private Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  919. {
  920. if (index < 0 || index >= owner.Items.Count)
  921. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  922. Rectangle item_rect = new Rectangle ();
  923. item_rect.X = 0;
  924. item_rect.Y = 2 + ((owner.ItemHeight - 2) * (index - first_displayble));
  925. item_rect.Height = owner.ItemHeight;
  926. item_rect.Width = textarea_drawable.Width;
  927. return item_rect;
  928. }
  929. public void HideWindow ()
  930. {
  931. if (owner.DropDownStyle == ComboBoxStyle.Simple)
  932. return;
  933. owner.ButtonReleased ();
  934. Hide ();
  935. }
  936. private int IndexFromPointDisplayRectangle (int x, int y)
  937. {
  938. for (int i = top_item; i <= last_item; i++) {
  939. if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
  940. return i;
  941. }
  942. return -1;
  943. }
  944. private int LastVisibleItem ()
  945. {
  946. Rectangle item_rect;
  947. int top_y = textarea_drawable.Y + textarea_drawable.Height;
  948. int i = 0;
  949. for (i = top_item; i < owner.Items.Count; i++) {
  950. item_rect = GetItemDisplayRectangle (i, top_item);
  951. if (item_rect.Y + item_rect.Height > top_y) {
  952. return i;
  953. }
  954. }
  955. return i;
  956. }
  957. private void NavigateItem (ItemNavigation navigation)
  958. {
  959. switch (navigation) {
  960. case ItemNavigation.Next: {
  961. if (highlighted_item + 1 < owner.Items.Count) {
  962. if (highlighted_item + 1 > last_item) {
  963. top_item++;
  964. vscrollbar_ctrl.Value = top_item;
  965. }
  966. SetHighLightedItem (highlighted_item + 1);
  967. }
  968. break;
  969. }
  970. case ItemNavigation.Previous: {
  971. if (highlighted_item > 0) {
  972. if (highlighted_item - 1 < top_item) {
  973. top_item--;
  974. vscrollbar_ctrl.Value = top_item;
  975. }
  976. SetHighLightedItem (highlighted_item - 1);
  977. }
  978. break;
  979. }
  980. case ItemNavigation.NextPage: {
  981. if (highlighted_item + page_size - 1 > owner.Items.Count) {
  982. top_item = owner.Items.Count - page_size;
  983. vscrollbar_ctrl.Value = top_item;
  984. SetHighLightedItem (owner.Items.Count - 1);
  985. }
  986. else {
  987. if (highlighted_item + page_size - 1 > last_item) {
  988. top_item = highlighted_item;
  989. vscrollbar_ctrl.Value = highlighted_item;
  990. }
  991. SetHighLightedItem (highlighted_item + page_size - 1);
  992. }
  993. break;
  994. }
  995. case ItemNavigation.PreviousPage: {
  996. /* Go to the first item*/
  997. if (highlighted_item - (page_size - 1) <= 0) {
  998. top_item = 0;
  999. vscrollbar_ctrl.Value = top_item;
  1000. SetHighLightedItem (0);
  1001. }
  1002. else { /* One page back */
  1003. if (highlighted_item - (page_size - 1) < top_item) {
  1004. top_item = highlighted_item - (page_size - 1);
  1005. vscrollbar_ctrl.Value = top_item;
  1006. }
  1007. SetHighLightedItem (highlighted_item - (page_size - 1));
  1008. }
  1009. break;
  1010. }
  1011. default:
  1012. break;
  1013. }
  1014. }
  1015. private void OnKeyDownPUW (object sender, KeyEventArgs e)
  1016. {
  1017. switch (e.KeyCode) {
  1018. case Keys.Up:
  1019. NavigateItem (ItemNavigation.Previous);
  1020. break;
  1021. case Keys.Down:
  1022. NavigateItem (ItemNavigation.Next);
  1023. break;
  1024. case Keys.PageUp:
  1025. NavigateItem (ItemNavigation.PreviousPage);
  1026. break;
  1027. case Keys.PageDown:
  1028. NavigateItem (ItemNavigation.NextPage);
  1029. break;
  1030. default:
  1031. break;
  1032. }
  1033. }
  1034. private void SetHighLightedItem (int index)
  1035. {
  1036. Rectangle invalidate;
  1037. /* Previous item */
  1038. if (highlighted_item != -1) {
  1039. invalidate = GetItemDisplayRectangle (highlighted_item, top_item);
  1040. if (ClientRectangle.Contains (invalidate))
  1041. Invalidate (invalidate);
  1042. }
  1043. highlighted_item = index;
  1044. /* Current item */
  1045. invalidate = GetItemDisplayRectangle (highlighted_item, top_item);
  1046. if (ClientRectangle.Contains (invalidate))
  1047. Invalidate (invalidate);
  1048. }
  1049. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  1050. {
  1051. /* Click outside the client area destroys the popup */
  1052. if (ClientRectangle.Contains (e.X, e.Y) == false) {
  1053. HideWindow ();
  1054. return;
  1055. }
  1056. /* Click on an element */
  1057. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  1058. if (index == -1) return;
  1059. owner.SelectedIndex = index;
  1060. HideWindow ();
  1061. }
  1062. private void OnMouseUpPUW (object sender, MouseEventArgs e)
  1063. {
  1064. }
  1065. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  1066. {
  1067. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  1068. if (index != -1)
  1069. SetHighLightedItem (index);
  1070. }
  1071. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  1072. {
  1073. if (Width <= 0 || Height <= 0 || Visible == false)
  1074. return;
  1075. Draw (pevent.ClipRectangle);
  1076. pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  1077. }
  1078. public void ShowWindow ()
  1079. {
  1080. CalcListBoxArea ();
  1081. Show ();
  1082. Refresh ();
  1083. if (owner.DropDown != null)
  1084. owner.DropDown (owner, EventArgs.Empty);
  1085. }
  1086. // Value Changed
  1087. private void VerticalScrollEvent (object sender, EventArgs e)
  1088. {
  1089. top_item = vscrollbar_ctrl.Value;
  1090. last_item = LastVisibleItem ();
  1091. Refresh ();
  1092. }
  1093. #endregion Private Methods
  1094. }
  1095. }
  1096. }