ComboBox.cs 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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. // Not implemented in TextBox yet
  315. //return textbox_ctrl.SelectionStart;
  316. return 0;
  317. }
  318. set {
  319. if (dropdown_style == ComboBoxStyle.DropDownList)
  320. return;
  321. // Not implemented in TextBox yet
  322. //if (textbox_ctrl.SelectionStart == value)
  323. // return;
  324. //textbox_ctrl.SelectionStart = value;
  325. }
  326. }
  327. public bool Sorted {
  328. get { return sorted; }
  329. set {
  330. if (sorted == value)
  331. return;
  332. sorted = value;
  333. }
  334. }
  335. public override string Text {
  336. get { return ""; /*throw new NotImplementedException ();*/ }
  337. set {}
  338. }
  339. #endregion Public Properties
  340. #region Private Properties
  341. internal ComboBoxInfo CBoxInfo {
  342. get { return combobox_info; }
  343. }
  344. #endregion Private Properties
  345. #region Public Methods
  346. protected virtual void AddItemsCore (object[] value)
  347. {
  348. }
  349. public void BeginUpdate ()
  350. {
  351. suspend_ctrlupdate = true;
  352. }
  353. protected virtual void Dispose (bool disposing)
  354. {
  355. }
  356. public void EndUpdate ()
  357. {
  358. suspend_ctrlupdate = false;
  359. Refresh ();
  360. }
  361. public int FindString (string s)
  362. {
  363. return FindString (s, 0);
  364. }
  365. public int FindString (string s, int startIndex)
  366. {
  367. for (int i = startIndex; i < Items.Count; i++) {
  368. if ((Items[i].ToString ()).StartsWith (s))
  369. return i;
  370. }
  371. return -1;
  372. }
  373. public int FindStringExact (string s)
  374. {
  375. return FindStringExact (s, 0);
  376. }
  377. public int FindStringExact (string s, int startIndex)
  378. {
  379. for (int i = startIndex; i < Items.Count; i++) {
  380. if ((Items[i].ToString ()).Equals (s))
  381. return i;
  382. }
  383. return -1;
  384. }
  385. public int GetItemHeight (int index)
  386. {
  387. throw new NotImplementedException ();
  388. }
  389. protected override bool IsInputKey (Keys keyData)
  390. {
  391. return false;
  392. }
  393. protected override void OnBackColorChanged (EventArgs e)
  394. {
  395. base.OnBackColorChanged (e);
  396. }
  397. protected override void OnDataSourceChanged (EventArgs e)
  398. {
  399. base.OnDataSourceChanged (e);
  400. }
  401. protected override void OnDisplayMemberChanged (EventArgs e)
  402. {
  403. base.OnDisplayMemberChanged (e);
  404. }
  405. protected virtual void OnDrawItem (DrawItemEventArgs e)
  406. {
  407. if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable)) {
  408. DrawItem (this, e);
  409. return;
  410. }
  411. Rectangle text_draw = e.Bounds;
  412. if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
  413. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  414. (ThemeEngine.Current.ColorHilight), text_draw);
  415. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  416. ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHilightText),
  417. text_draw, string_format);
  418. // It seems to be a bug in CPDrawFocusRectangle
  419. //ThemeEngine.Current.CPDrawFocusRectangle (e.Graphics, e.Bounds,
  420. // ThemeEngine.Current.ColorHilightText, BackColor);
  421. }
  422. else {
  423. e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  424. (e.BackColor), e.Bounds);
  425. e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
  426. ThemeEngine.Current.ResPool.GetSolidBrush (e.ForeColor),
  427. text_draw, string_format);
  428. }
  429. }
  430. protected virtual void OnDropDownStyleChanged (EventArgs e)
  431. {
  432. }
  433. protected override void OnFontChanged (EventArgs e)
  434. {
  435. base.OnFontChanged (e);
  436. if (textbox_ctrl != null) {
  437. textbox_ctrl.Font = Font;
  438. }
  439. combobox_info.item_height = FontHeight + 2;
  440. }
  441. protected override void OnForeColorChanged (EventArgs e)
  442. {
  443. base.OnForeColorChanged (e);
  444. }
  445. protected override void OnHandleCreated (EventArgs e)
  446. {
  447. base.OnHandleCreated (e);
  448. CalcTextArea ();
  449. }
  450. protected override void OnHandleDestroyed (EventArgs e)
  451. {
  452. base.OnHandleDestroyed (e);
  453. }
  454. protected override void OnKeyPress (KeyPressEventArgs e)
  455. {
  456. }
  457. protected virtual void OnMeasureItem (MeasureItemEventArgs e)
  458. {
  459. }
  460. protected override void OnParentBackColorChanged (EventArgs e)
  461. {
  462. base.OnParentBackColorChanged (e);
  463. }
  464. protected override void OnResize (EventArgs e)
  465. {
  466. base.OnResize (e);
  467. CalcTextArea ();
  468. }
  469. protected override void OnSelectedIndexChanged (EventArgs e)
  470. {
  471. base.OnSelectedIndexChanged (e);
  472. if (SelectedIndexChanged != null)
  473. SelectedIndexChanged (this, e);
  474. }
  475. protected virtual void OnSelectedItemChanged (EventArgs e)
  476. {
  477. }
  478. protected override void OnSelectedValueChanged (EventArgs e)
  479. {
  480. base.OnSelectedValueChanged (e);
  481. }
  482. protected virtual void OnSelectionChangeCommitted (EventArgs e)
  483. {
  484. }
  485. protected override void RefreshItem (int index)
  486. {
  487. }
  488. protected virtual void Select (int start, int lenght)
  489. {
  490. }
  491. public void SelectAll ()
  492. {
  493. }
  494. protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
  495. {
  496. base.SetBoundsCore (x, y, width, height, specified);
  497. }
  498. protected override void SetItemCore (int index, object value)
  499. {
  500. if (index < 0 || index >= Items.Count)
  501. return;
  502. Items[index] = value;
  503. }
  504. protected override void SetItemsCore (IList value)
  505. {
  506. }
  507. public override string ToString ()
  508. {
  509. throw new NotImplementedException ();
  510. }
  511. protected override void WndProc (ref Message m)
  512. {
  513. switch ((Msg) m.Msg) {
  514. case Msg.WM_PAINT: {
  515. PaintEventArgs paint_event;
  516. paint_event = XplatUI.PaintEventStart (Handle);
  517. OnPaintCB (paint_event);
  518. XplatUI.PaintEventEnd (Handle);
  519. return;
  520. }
  521. case Msg.WM_ERASEBKGND:
  522. m.Result = (IntPtr) 1;
  523. return;
  524. default:
  525. break;
  526. }
  527. base.WndProc (ref m);
  528. }
  529. #endregion Public Methods
  530. #region Private Methods
  531. internal void ButtonReleased ()
  532. {
  533. combobox_info.button_status = ButtonState.Normal;
  534. Invalidate (combobox_info.button_rect);
  535. }
  536. // Calcs the text area size
  537. internal void CalcTextArea ()
  538. {
  539. combobox_info.textarea = ClientRectangle;
  540. /* Edit area */
  541. combobox_info.textarea.Height = ItemHeight + ThemeEngine.Current.DrawComboBoxEditDecorationTop () +
  542. ThemeEngine.Current.DrawComboBoxEditDecorationBottom () + 2;
  543. // TODO: Does the +2 changes a different font resolutions?
  544. /* Edit area - minus decorations (text drawable area) */
  545. combobox_info.textarea_drawable = combobox_info.textarea;
  546. combobox_info.textarea_drawable.Y += ThemeEngine.Current.DrawComboBoxEditDecorationTop ();
  547. combobox_info.textarea_drawable.X += ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
  548. combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationBottom ();
  549. combobox_info.textarea_drawable.Height -= ThemeEngine.Current.DrawComboBoxEditDecorationTop();
  550. combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationRight ();
  551. combobox_info.textarea_drawable.Width -= ThemeEngine.Current.DrawComboBoxEditDecorationLeft ();
  552. /* Non-drawable area */
  553. Region area = new Region (ClientRectangle);
  554. area.Exclude (combobox_info.textarea);
  555. RectangleF bounds = area.GetBounds (DeviceContext);
  556. combobox_info.listbox_area = new Rectangle ((int)bounds.X, (int)bounds.Y,
  557. (int)bounds.Width, (int)bounds.Height);
  558. if (CBoxInfo.show_button) {
  559. combobox_info.textarea_drawable.Width -= def_button_width;
  560. combobox_info.button_rect = new Rectangle (combobox_info.textarea_drawable.X + combobox_info.textarea_drawable.Width,
  561. combobox_info.textarea_drawable.Y, def_button_width, combobox_info.textarea_drawable.Height);
  562. }
  563. if (dropdown_style != ComboBoxStyle.DropDownList) { /* There is an edit control*/
  564. if (textbox_ctrl != null) {
  565. textbox_ctrl.Location = new Point (combobox_info.textarea_drawable.X, combobox_info.textarea_drawable.Y);
  566. textbox_ctrl.Size = new Size (combobox_info.textarea_drawable.Width, combobox_info.textarea_drawable.Height);
  567. }
  568. }
  569. if (listbox_ctrl != null && dropdown_style == ComboBoxStyle.Simple) {
  570. listbox_ctrl.Location = new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
  571. combobox_info.textarea.Height);
  572. listbox_ctrl.CalcListBoxArea ();
  573. }
  574. }
  575. private void CreateComboListBox ()
  576. {
  577. Console.WriteLine ("CreateComboListBox called");
  578. listbox_ctrl = new ComboListBox (this);
  579. //listbox_ctrl.Size = combobox_info.listbox_size;
  580. listbox_ctrl.Size = new Size (100, 100);
  581. }
  582. internal void Draw (Rectangle clip)
  583. {
  584. // No edit control, we paint the edit ourselfs
  585. if (dropdown_style == ComboBoxStyle.DropDownList) {
  586. if (selected_index != -1) {
  587. Rectangle item_rect = combobox_info.textarea_drawable;
  588. item_rect.Height = ItemHeight;
  589. OnDrawItem (new DrawItemEventArgs (DeviceContext, Font, item_rect,
  590. selected_index, DrawItemState.Selected,
  591. ForeColor, BackColor));
  592. }
  593. else
  594. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
  595. combobox_info.textarea_drawable);
  596. }
  597. if (clip.IntersectsWith (combobox_info.listbox_area) == true) {
  598. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (Parent.BackColor),
  599. combobox_info.listbox_area);
  600. }
  601. if (CBoxInfo.show_button) {
  602. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorButtonFace),
  603. combobox_info.button_rect);
  604. ThemeEngine.Current.CPDrawComboButton (DeviceContext,
  605. combobox_info.button_rect, combobox_info.button_status);
  606. }
  607. ThemeEngine.Current.DrawComboBoxEditDecorations (DeviceContext, this, combobox_info.textarea);
  608. }
  609. internal virtual void OnMouseDownCB (object sender, MouseEventArgs e)
  610. {
  611. /* Click On button*/
  612. if (clicked == false && combobox_info.button_rect.Contains (e.X, e.Y)) {
  613. clicked = true;
  614. if (combobox_info.button_status == ButtonState.Normal) {
  615. combobox_info.button_status = ButtonState.Pushed;
  616. }
  617. else {
  618. if (combobox_info.button_status == ButtonState.Pushed) {
  619. combobox_info.button_status = ButtonState.Normal;
  620. }
  621. }
  622. if (combobox_info.button_status == ButtonState.Pushed) {
  623. if (listbox_ctrl == null) {
  624. CreateComboListBox ();
  625. }
  626. listbox_ctrl.Location = PointToScreen (new Point (combobox_info.textarea.X, combobox_info.textarea.Y +
  627. combobox_info.textarea.Height));
  628. listbox_ctrl.ShowWindow ();
  629. }
  630. Invalidate (combobox_info.button_rect);
  631. }
  632. }
  633. internal virtual void OnMouseUpCB (object sender, MouseEventArgs e)
  634. {
  635. /* Click on button*/
  636. if (clicked == true && combobox_info.button_rect.Contains (e.X, e.Y)) {
  637. clicked = false;
  638. }
  639. }
  640. private void OnPaintCB (PaintEventArgs pevent)
  641. {
  642. if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
  643. return;
  644. Rectangle rect = ClientRectangle;
  645. /* Copies memory drawing buffer to screen*/
  646. //if (rect.Height > Height)
  647. // rect.Height = Height;
  648. //if (rect.Height > 20)
  649. // rect.Height = 20;
  650. Console.WriteLine ("ComboBox.OnPaint {0}", rect);
  651. Draw (rect);
  652. pevent.Graphics.DrawImage (ImageBuffer, rect, rect, GraphicsUnit.Pixel);
  653. if (Paint != null)
  654. Paint (this, pevent);
  655. }
  656. #endregion Private Methods
  657. /*
  658. ComboBox.ObjectCollection
  659. */
  660. public class ObjectCollection : IList, ICollection, IEnumerable
  661. {
  662. private ComboBox owner;
  663. internal ArrayList object_items = new ArrayList ();
  664. internal ArrayList listbox_items = new ArrayList ();
  665. public ObjectCollection (ComboBox owner)
  666. {
  667. this.owner = owner;
  668. }
  669. #region Public Properties
  670. public virtual int Count {
  671. get { return object_items.Count; }
  672. }
  673. public virtual bool IsReadOnly {
  674. get { return false; }
  675. }
  676. public virtual object this [int index] {
  677. get {
  678. if (index < 0 || index >= Count)
  679. throw new ArgumentOutOfRangeException ("Index of out range");
  680. return object_items[index];
  681. }
  682. set {
  683. if (index < 0 || index >= Count)
  684. throw new ArgumentOutOfRangeException ("Index of out range");
  685. object_items[index] = value;
  686. }
  687. }
  688. bool ICollection.IsSynchronized {
  689. get { return false; }
  690. }
  691. object ICollection.SyncRoot {
  692. get { return this; }
  693. }
  694. bool IList.IsFixedSize {
  695. get { return false; }
  696. }
  697. #endregion Public Properties
  698. #region Public Methods
  699. public int Add (object item)
  700. {
  701. int idx;
  702. idx = AddItem (item);
  703. return idx;
  704. }
  705. public void AddRange (object[] items)
  706. {
  707. foreach (object mi in items)
  708. AddItem (mi);
  709. }
  710. public virtual void Clear ()
  711. {
  712. object_items.Clear ();
  713. listbox_items.Clear ();
  714. }
  715. public virtual bool Contains (object obj)
  716. {
  717. return object_items.Contains (obj);
  718. }
  719. public void CopyTo (object[] dest, int arrayIndex)
  720. {
  721. object_items.CopyTo (dest, arrayIndex);
  722. }
  723. void ICollection.CopyTo (Array dest, int index)
  724. {
  725. object_items.CopyTo (dest, index);
  726. }
  727. public virtual IEnumerator GetEnumerator ()
  728. {
  729. return object_items.GetEnumerator ();
  730. }
  731. int IList.Add (object item)
  732. {
  733. return Add (item);
  734. }
  735. public virtual int IndexOf (object value)
  736. {
  737. return object_items.IndexOf (value);
  738. }
  739. public virtual void Insert (int index, object item)
  740. {
  741. throw new NotImplementedException ();
  742. }
  743. public virtual void Remove (object value)
  744. {
  745. RemoveAt (IndexOf (value));
  746. }
  747. public virtual void RemoveAt (int index)
  748. {
  749. if (index < 0 || index >= Count)
  750. throw new ArgumentOutOfRangeException ("Index of out range");
  751. object_items.RemoveAt (index);
  752. listbox_items.RemoveAt (index);
  753. //owner.UpdateItemInfo (false, -1, -1);
  754. }
  755. #endregion Public Methods
  756. #region Private Methods
  757. private int AddItem (object item)
  758. {
  759. int cnt = object_items.Count;
  760. object_items.Add (item);
  761. listbox_items.Add (new ComboBox.ComboBoxItem (cnt));
  762. return cnt;
  763. }
  764. internal ComboBox.ComboBoxItem GetComboBoxItem (int index)
  765. {
  766. if (index < 0 || index >= Count)
  767. throw new ArgumentOutOfRangeException ("Index of out range");
  768. return (ComboBox.ComboBoxItem) listbox_items[index];
  769. }
  770. internal void SetComboBoxItem (ComboBox.ComboBoxItem item, int index)
  771. {
  772. if (index < 0 || index >= Count)
  773. throw new ArgumentOutOfRangeException ("Index of out range");
  774. listbox_items[index] = item;
  775. }
  776. #endregion Private Methods
  777. }
  778. /*
  779. class ComboListBox
  780. */
  781. internal class ComboListBox : Control
  782. {
  783. private ComboBox owner;
  784. private bool need_vscrollbar;
  785. private VScrollBar vscrollbar_ctrl;
  786. private int top_item; /* First item that we show the in the current page */
  787. private int last_item; /* Last visible item */
  788. private int highlighted_item; /* Item that is currently selected */
  789. private Rectangle textarea_drawable; /* Rectangle of the drawable text area */
  790. public ComboListBox (ComboBox owner) : base ()
  791. {
  792. this.owner = owner;
  793. need_vscrollbar = false;
  794. top_item = 0;
  795. last_item = 0;
  796. highlighted_item = -1;
  797. MouseDown += new MouseEventHandler (OnMouseDownPUW);
  798. MouseMove += new MouseEventHandler (OnMouseMovePUW);
  799. MouseUp += new MouseEventHandler (OnMouseUpPUW);
  800. Paint += new PaintEventHandler (OnPaintPUW);
  801. SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  802. SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
  803. /* Vertical scrollbar */
  804. vscrollbar_ctrl = new VScrollBar ();
  805. vscrollbar_ctrl.Minimum = 0;
  806. vscrollbar_ctrl.SmallChange = 1;
  807. vscrollbar_ctrl.LargeChange = 1;
  808. vscrollbar_ctrl.Maximum = 0;
  809. vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
  810. vscrollbar_ctrl.Visible = false;
  811. }
  812. protected override CreateParams CreateParams
  813. {
  814. get {
  815. CreateParams cp = base.CreateParams;
  816. if (owner != null && owner.DropDownStyle != ComboBoxStyle.Simple) {
  817. cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
  818. cp.ExStyle |= (int)WindowStyles.WS_EX_TOOLWINDOW;
  819. }
  820. return cp;
  821. }
  822. }
  823. #region Private Methods
  824. protected override void CreateHandle ()
  825. {
  826. base.CreateHandle ();
  827. Controls.Add (vscrollbar_ctrl);
  828. }
  829. // Calcs the listbox area
  830. internal void CalcListBoxArea ()
  831. {
  832. int width, height;
  833. if (owner.DropDownStyle == ComboBoxStyle.Simple) {
  834. width = owner.CBoxInfo.listbox_area.Width;
  835. height = owner.CBoxInfo.listbox_area.Height;
  836. if (owner.IntegralHeight == true) {
  837. int remaining = (height - 2 -
  838. ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
  839. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle)) %
  840. (owner.ItemHeight - 2);
  841. Console.WriteLine("CalcListBoxArea h:{0} remaining {1}, itemh {2}",
  842. height - 2 -
  843. ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle)-
  844. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle),
  845. remaining, (owner.ItemHeight - 2));
  846. if (remaining > 0) {
  847. height -= remaining;
  848. }
  849. }
  850. }
  851. else {
  852. width = owner.ClientRectangle.Width;
  853. if (owner.Items.Count <= owner.MaxDropDownItems) {
  854. height = owner.ItemHeight * owner.Items.Count;
  855. }
  856. else {
  857. height = owner.ItemHeight * owner.MaxDropDownItems;
  858. }
  859. }
  860. if (owner.Items.Count <= owner.MaxDropDownItems) {
  861. need_vscrollbar = false;
  862. }
  863. else {
  864. need_vscrollbar = true;
  865. vscrollbar_ctrl.Height = height - ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle) -
  866. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  867. vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle),
  868. ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle));
  869. vscrollbar_ctrl.Maximum = owner.Items.Count - owner.MaxDropDownItems;
  870. }
  871. if (vscrollbar_ctrl.Visible != need_vscrollbar)
  872. vscrollbar_ctrl.Visible = need_vscrollbar;
  873. Size = new Size (width, height);
  874. textarea_drawable = ClientRectangle;
  875. textarea_drawable.Width = width;
  876. textarea_drawable.Height = height;
  877. // Exclude decorations
  878. textarea_drawable.X += ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
  879. textarea_drawable.Y += ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  880. textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationRight (owner.DropDownStyle);
  881. textarea_drawable.Width -= ThemeEngine.Current.DrawComboListBoxDecorationLeft (owner.DropDownStyle);
  882. textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationBottom (owner.DropDownStyle);
  883. textarea_drawable.Height -= ThemeEngine.Current.DrawComboListBoxDecorationTop (owner.DropDownStyle);
  884. if (need_vscrollbar)
  885. textarea_drawable.Width -= vscrollbar_ctrl.Width;
  886. last_item = LastVisibleItem ();
  887. Console.WriteLine ("ComboListBox.CalcListBoxArea {0} ", textarea_drawable);
  888. }
  889. private void Draw (Rectangle clip)
  890. {
  891. Rectangle cl = ClientRectangle;
  892. DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
  893. (owner.BackColor), ClientRectangle);
  894. if (owner.Items.Count > 0) {
  895. Rectangle item_rect;
  896. DrawItemState state = DrawItemState.None;
  897. for (int i = top_item; i < last_item; i++) {
  898. item_rect = GetItemDisplayRectangle (i, top_item);
  899. if (clip.IntersectsWith (item_rect) == false)
  900. continue;
  901. /* Draw item */
  902. state = DrawItemState.None;
  903. if (i == highlighted_item)
  904. state |= DrawItemState.Selected;
  905. Console.WriteLine ("Item {0} {1}", i, item_rect);
  906. owner.OnDrawItem (new DrawItemEventArgs (DeviceContext, owner.Font, item_rect,
  907. i, state, owner.ForeColor, owner.BackColor));
  908. }
  909. }
  910. ThemeEngine.Current.DrawComboListBoxDecorations (DeviceContext, owner, ClientRectangle);
  911. }
  912. private Rectangle GetItemDisplayRectangle (int index, int first_displayble)
  913. {
  914. if (index < 0 || index >= owner.Items.Count)
  915. throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
  916. Rectangle item_rect = new Rectangle ();
  917. item_rect.X = 0;
  918. item_rect.Y = 2 + ((owner.ItemHeight - 2) * (index - first_displayble));
  919. item_rect.Height = owner.ItemHeight;
  920. item_rect.Width = textarea_drawable.Width;
  921. return item_rect;
  922. }
  923. public void HideWindow ()
  924. {
  925. owner.ButtonReleased ();
  926. Hide ();
  927. }
  928. private int IndexFromPointDisplayRectangle (int x, int y)
  929. {
  930. for (int i = top_item; i < last_item; i++) {
  931. if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
  932. return i;
  933. }
  934. return -1;
  935. }
  936. private int LastVisibleItem ()
  937. {
  938. Rectangle item_rect;
  939. int top_y = textarea_drawable.Y + textarea_drawable.Height;
  940. int i = 0;
  941. for (i = top_item; i < owner.Items.Count; i++) {
  942. item_rect = GetItemDisplayRectangle (i, top_item);
  943. if (item_rect.Y > top_y)
  944. return i;
  945. }
  946. return i;
  947. }
  948. private void OnMouseDownPUW (object sender, MouseEventArgs e)
  949. {
  950. /* Click outside the client area destroys the popup */
  951. if (ClientRectangle.Contains (e.X, e.Y) == false) {
  952. HideWindow ();
  953. return;
  954. }
  955. /* Click on an element */
  956. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  957. if (index == -1) return;
  958. owner.SelectedIndex = index;
  959. HideWindow ();
  960. }
  961. private void OnMouseUpPUW (object sender, MouseEventArgs e)
  962. {
  963. }
  964. private void OnMouseMovePUW (object sender, MouseEventArgs e)
  965. {
  966. Rectangle invalidate;
  967. int index = IndexFromPointDisplayRectangle (e.X, e.Y);
  968. if (index == -1) return;
  969. /* Previous item */
  970. if (highlighted_item != -1) {
  971. invalidate = GetItemDisplayRectangle (highlighted_item, top_item);
  972. if (ClientRectangle.Contains (invalidate))
  973. Invalidate (invalidate);
  974. }
  975. highlighted_item = index;
  976. /* Current item */
  977. invalidate = GetItemDisplayRectangle (highlighted_item, top_item);
  978. if (ClientRectangle.Contains (invalidate))
  979. Invalidate (invalidate);
  980. }
  981. private void OnPaintPUW (Object o, PaintEventArgs pevent)
  982. {
  983. if (Width <= 0 || Height <= 0 || Visible == false)
  984. return;
  985. Draw (pevent.ClipRectangle);
  986. pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
  987. }
  988. public void ShowWindow ()
  989. {
  990. CalcListBoxArea ();
  991. Show ();
  992. Refresh ();
  993. if (owner.DropDown != null)
  994. owner.DropDown (owner, EventArgs.Empty);
  995. }
  996. // Value Changed
  997. private void VerticalScrollEvent (object sender, EventArgs e)
  998. {
  999. top_item = vscrollbar_ctrl.Value;
  1000. last_item = LastVisibleItem ();
  1001. Refresh ();
  1002. }
  1003. #endregion Private Methods
  1004. }
  1005. }
  1006. }