| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098 |
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2004 Novell, Inc.
- //
- // Authors:
- // Jordi Mas i Hernandez, [email protected]
- //
- //
- // NOT COMPLETE
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Reflection;
- namespace System.Windows.Forms
- {
- public class ComboBox : ListControl
- {
- private DrawMode draw_mode;
- private ComboBoxStyle dropdown_style;
- private int dropdown_width;
- private int max_length;
- private int preferred_height;
- private int selected_index;
- private object selected_item;
- internal ObjectCollection items;
- private bool suspend_ctrlupdate;
- private int maxdrop_items;
- private bool integral_height;
- private bool sorted;
- internal ComboBoxInfo combobox_info;
- private readonly int def_button_width = 16;
- private bool clicked;
- private ListBoxPopUp listbox_popup;
- private StringFormat string_format;
- internal class ComboBoxInfo
- {
- internal int item_height; /* Item's height */
- internal Rectangle textarea_rect; /* Rectangle of the editable text area - decorations */
- internal Rectangle button_rect;
- internal bool show_button; /* Is the DropDown button shown? */
- internal Rectangle client_rect; /* Client Rectangle. Usually = ClientRectangle except when IntegralHeight has been applied*/
- internal ButtonState button_status; /* Drop button status */
- internal Size listbox_size;
- public ComboBoxInfo ()
- {
- button_status = ButtonState.Normal;
- show_button = false;
- item_height = 0;
- }
- }
- internal class ComboBoxItem
- {
- internal int Index;
- public ComboBoxItem (int index)
- {
- Index = index;
- }
- }
- public ComboBox ()
- {
- BackColor = ThemeEngine.Current.ColorWindow;
- draw_mode = DrawMode.Normal;
- selected_index = -1;
- selected_item = null;
- maxdrop_items = 8;
- combobox_info = new ComboBoxInfo ();
- combobox_info.item_height = FontHeight;
- suspend_ctrlupdate = false;
- clicked = false;
- items = new ObjectCollection (this);
- string_format = new StringFormat ();
- CBoxInfo.show_button = true;
- listbox_popup = null;
- /* Events */
- MouseDown += new MouseEventHandler (OnMouseDownCB);
- MouseUp += new MouseEventHandler (OnMouseUpCB);
-
- Console.WriteLine ("ComboBox is still an on going effort. Only ComboBoxStyle.DropDownList is implemented");
- }
- #region Events
- public new event EventHandler BackgroundImageChanged;
- public event DrawItemEventHandler DrawItem;
- public event EventHandler DropDown;
- public event EventHandler DropDownStyleChanged;
- public event MeasureItemEventHandler MeasureItem;
- public new event PaintEventHandler Paint;
- public event EventHandler SelectedIndexChanged;
- public event EventHandler SelectionChangeCommitted;
- #endregion Events
- #region Public Properties
- public override Color BackColor {
- get { return base.BackColor; }
- set {
- if (base.BackColor == value)
- return;
- base.BackColor = value;
- Refresh ();
- }
- }
- public override Image BackgroundImage {
- get { return base.BackgroundImage; }
- set {
- if (base.BackgroundImage == value)
- return;
- base.BackgroundImage = value;
- if (BackgroundImageChanged != null)
- BackgroundImageChanged (this, EventArgs.Empty);
- Refresh ();
- }
- }
- protected override CreateParams CreateParams {
- get { return base.CreateParams;}
- }
- protected override Size DefaultSize {
- get { return new Size (121, PreferredHeight); }
- }
- public DrawMode DrawMode {
- get { return draw_mode; }
- set {
- if (!Enum.IsDefined (typeof (DrawMode), value))
- throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for DrawMode", value));
- if (draw_mode == value)
- return;
- draw_mode = value;
- Refresh ();
- }
- }
- public ComboBoxStyle DropDownStyle {
- get { return dropdown_style; }
- set {
- if (!Enum.IsDefined (typeof (ComboBoxStyle), value))
- throw new InvalidEnumArgumentException (string.Format("Enum argument value '{0}' is not valid for ComboBoxStyle", value));
- if (dropdown_style == value)
- return;
- dropdown_style = value;
- Refresh ();
- }
- }
- public int DropDownWidth {
- get { return dropdown_width; }
- set {
- if (dropdown_width == value)
- return;
- dropdown_width = value;
- Refresh ();
- }
- }
- public override bool Focused {
- get { return base.Focused; }
- }
- public override Color ForeColor {
- get { return base.ForeColor; }
- set {
- if (base.ForeColor == value)
- return;
- base.ForeColor = value;
- Refresh ();
- }
- }
- public bool IntegralHeight {
- get { return integral_height; }
- set {
- if (integral_height == value)
- return;
- integral_height = value;
- }
- }
- public virtual int ItemHeight {
- get { return combobox_info.item_height; }
- set {
- if (value < 0)
- throw new ArgumentOutOfRangeException ("The item height value is less than zero");
- combobox_info.item_height = value;
- Refresh ();
- }
- }
- public ComboBox.ObjectCollection Items {
- get { return items; }
- }
- public int MaxDropDownItems {
- get { return maxdrop_items; }
- set {
- if (maxdrop_items == value)
- return;
- maxdrop_items = value;
- }
- }
- public int MaxLength {
- get { return max_length; }
- set {
- if (max_length == value)
- return;
- max_length = value;
- }
- }
- public int PreferredHeight {
- get { return preferred_height; }
- }
- public override int SelectedIndex {
- get { return selected_index; }
- set {
- if (value < -2 || value >= Items.Count)
- throw new ArgumentOutOfRangeException ("Index of out range");
- if (selected_index == value)
- return;
- selected_index = value;
- OnSelectedIndexChanged (new EventArgs ());
- Refresh ();
- }
- }
- public object SelectedItem {
- get {
- if (selected_index !=-1 && Items.Count > 0)
- return Items[selected_index];
- else
- return null;
- }
- set {
- if (selected_item == value)
- return;
- int index = Items.IndexOf (value);
- if (index == -1)
- return;
- selected_index = index;
- OnSelectedItemChanged (new EventArgs ());
- Refresh ();
- }
- }
-
- public string SelectedText {
- get {throw new NotImplementedException ();}
- set {}
- }
- public int SelectionLength {
- get {throw new NotImplementedException ();}
- set {}
- }
- public int SelectionStart {
- get {throw new NotImplementedException (); }
- set {}
- }
- public bool Sorted {
- get { return sorted; }
- set {
- if (sorted == value)
- return;
- sorted = value;
- }
- }
- public override string Text {
- get { return ""; /*throw new NotImplementedException ();*/ }
- set {}
- }
- #endregion Public Properties
- #region Private Properties
- internal ComboBoxInfo CBoxInfo {
- get { return combobox_info; }
- }
- #endregion Private Properties
- #region Public Methods
- protected virtual void AddItemsCore (object[] value)
- {
- }
- public void BeginUpdate ()
- {
- suspend_ctrlupdate = true;
- }
- protected virtual void Dispose (bool disposing)
- {
- }
- public void EndUpdate ()
- {
- suspend_ctrlupdate = false;
- Refresh ();
- }
- public int FindString (string s)
- {
- return FindString (s, 0);
- }
- public int FindString (string s, int startIndex)
- {
- for (int i = startIndex; i < Items.Count; i++) {
- if ((Items[i].ToString ()).StartsWith (s))
- return i;
- }
- return -1;
- }
- public int FindStringExact (string s)
- {
- return FindStringExact (s, 0);
- }
- public int FindStringExact (string s, int startIndex)
- {
- for (int i = startIndex; i < Items.Count; i++) {
- if ((Items[i].ToString ()).Equals (s))
- return i;
- }
- return -1;
- }
- public int GetItemHeight (int index)
- {
- throw new NotImplementedException ();
- }
- protected override bool IsInputKey (Keys keyData)
- {
- return false;
- }
- protected override void OnBackColorChanged (EventArgs e)
- {
- base.OnBackColorChanged (e);
- }
- protected override void OnDataSourceChanged (EventArgs e)
- {
- base.OnDataSourceChanged (e);
- }
- protected override void OnDisplayMemberChanged (EventArgs e)
- {
- base.OnDisplayMemberChanged (e);
- }
- protected virtual void OnDrawItem (DrawItemEventArgs e)
- {
- if (DrawItem != null && (DrawMode == DrawMode.OwnerDrawFixed || DrawMode == DrawMode.OwnerDrawVariable))
- DrawItem (this, e);
- if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) {
- e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
- (ThemeEngine.Current.ColorHilight), e.Bounds);
- e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
- ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHilightText),
- e.Bounds, string_format);
- // It seems to be a bug in CPDrawFocusRectangle
- //ThemeEngine.Current.CPDrawFocusRectangle (e.Graphics, e.Bounds,
- // ThemeEngine.Current.ColorHilightText, BackColor);
- }
- else {
- e.Graphics.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
- (e.BackColor), e.Bounds);
- e.Graphics.DrawString (Items[e.Index].ToString (), e.Font,
- ThemeEngine.Current.ResPool.GetSolidBrush (e.ForeColor),
- e.Bounds, string_format);
- }
- }
- protected virtual void OnDropDownStyleChanged (EventArgs e)
- {
- }
- protected override void OnFontChanged (EventArgs e)
- {
- base.OnFontChanged (e);
- }
- protected override void OnForeColorChanged (EventArgs e)
- {
- base.OnForeColorChanged (e);
- }
- protected override void OnHandleCreated (EventArgs e)
- {
- base.OnHandleCreated (e);
- CalcTextArea ();
- }
- protected override void OnHandleDestroyed (EventArgs e)
- {
- base.OnHandleDestroyed (e);
- }
- protected override void OnKeyPress (KeyPressEventArgs e)
- {
- }
- protected virtual void OnMeasureItem (MeasureItemEventArgs e)
- {
- }
- protected override void OnParentBackColorChanged (EventArgs e)
- {
- base.OnParentBackColorChanged (e);
- }
- protected override void OnResize (EventArgs e)
- {
- base.OnResize (e);
- CalcTextArea ();
- }
- protected override void OnSelectedIndexChanged (EventArgs e)
- {
- base.OnSelectedIndexChanged (e);
- if (SelectedIndexChanged != null)
- SelectedIndexChanged (this, e);
- }
- protected virtual void OnSelectedItemChanged (EventArgs e)
- {
- }
- protected override void OnSelectedValueChanged (EventArgs e)
- {
- base.OnSelectedValueChanged (e);
- }
- protected virtual void OnSelectionChangeCommitted (EventArgs e)
- {
- }
- protected override void RefreshItem (int index)
- {
- }
- protected virtual void Select (int start, int lenght)
- {
- }
- public void SelectAll ()
- {
- }
- protected override void SetBoundsCore (int x, int y, int width, int height, BoundsSpecified specified)
- {
- base.SetBoundsCore (x, y, width, height, specified);
- }
- protected override void SetItemCore (int index, object value)
- {
- if (index < 0 || index >= Items.Count)
- return;
- Items[index] = value;
- }
- protected override void SetItemsCore (IList value)
- {
- }
- public override string ToString ()
- {
- throw new NotImplementedException ();
- }
- protected override void WndProc (ref Message m)
- {
- switch ((Msg) m.Msg) {
- case Msg.WM_PAINT: {
- PaintEventArgs paint_event;
- paint_event = XplatUI.PaintEventStart (Handle);
- OnPaintCB (paint_event);
- XplatUI.PaintEventEnd (Handle);
- return;
- }
- case Msg.WM_ERASEBKGND:
- m.Result = (IntPtr) 1;
- return;
- default:
- break;
- }
- base.WndProc (ref m);
- }
- #endregion Public Methods
- #region Private Methods
- internal void ButtonReleased ()
- {
- combobox_info.button_status = ButtonState.Normal;
- Invalidate (combobox_info.button_rect);
- }
- // Calcs the text area size
- internal void CalcTextArea ()
- {
- combobox_info.textarea_rect = ClientRectangle;
- combobox_info.textarea_rect.Y += ThemeEngine.Current.DrawComboBoxDecorationTop ();
- combobox_info.textarea_rect.X += ThemeEngine.Current.DrawComboBoxDecorationLeft ();
- combobox_info.textarea_rect.Height -= ThemeEngine.Current.DrawComboBoxDecorationBottom ();
- combobox_info.textarea_rect.Width -= ThemeEngine.Current.DrawComboBoxDecorationRight ();
- if (CBoxInfo.show_button) {
- combobox_info.textarea_rect.Width -= def_button_width;
- combobox_info.button_rect = new Rectangle (combobox_info.textarea_rect.X + combobox_info.textarea_rect.Width,
- combobox_info.textarea_rect.Y, def_button_width, combobox_info.textarea_rect.Height);
- }
- }
- private void CreateListBoxPopUp ()
- {
- listbox_popup = new ListBoxPopUp (this);
- listbox_popup.Location = PointToScreen (new Point (ClientRectangle.X, ClientRectangle.Y + ClientRectangle.Height));
- listbox_popup.Size = combobox_info.listbox_size;
- }
- internal void Draw (Rectangle clip)
- {
-
- // No edit control, we paint the edit are ourselfs
- if (dropdown_style == ComboBoxStyle.DropDownList) {
- if (selected_index != -1) {
-
- OnDrawItem (new DrawItemEventArgs (DeviceContext, Font, combobox_info.textarea_rect,
- selected_index, DrawItemState.Selected,
- ForeColor, BackColor));
- }
- else
- DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor),
- ClientRectangle);
- }
- if (CBoxInfo.show_button) {
- DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorButtonFace),
- combobox_info.button_rect);
- ThemeEngine.Current.CPDrawComboButton (DeviceContext,
- combobox_info.button_rect, combobox_info.button_status);
- }
- ThemeEngine.Current.DrawComboBoxDecorations (DeviceContext, this);
- }
- internal virtual void OnMouseDownCB (object sender, MouseEventArgs e)
- {
- /* Click On button*/
- if (clicked == false && combobox_info.button_rect.Contains (e.X, e.Y)) {
- clicked = true;
- if (combobox_info.button_status == ButtonState.Normal) {
- combobox_info.button_status = ButtonState.Pushed;
- }
- else {
- if (combobox_info.button_status == ButtonState.Pushed) {
- combobox_info.button_status = ButtonState.Normal;
- }
- }
- if (combobox_info.button_status == ButtonState.Pushed) {
- if (listbox_popup == null)
- CreateListBoxPopUp ();
- listbox_popup.ShowWindow ();
- }
- Invalidate (combobox_info.button_rect);
- }
- }
- internal virtual void OnMouseUpCB (object sender, MouseEventArgs e)
- {
- /* Click on button*/
- if (clicked == true && combobox_info.button_rect.Contains (e.X, e.Y)) {
- clicked = false;
- }
- }
- private void OnPaintCB (PaintEventArgs pevent)
- {
- if (Width <= 0 || Height <= 0 || Visible == false || suspend_ctrlupdate == true)
- return;
- /* Copies memory drawing buffer to screen*/
- Draw (ClientRectangle);
- pevent.Graphics.DrawImage (ImageBuffer, ClientRectangle, ClientRectangle, GraphicsUnit.Pixel);
- if (Paint != null)
- Paint (this, pevent);
- }
- #endregion Private Methods
- /*
- ComboBox.ObjectCollection
- */
- public class ObjectCollection : IList, ICollection, IEnumerable
- {
- private ComboBox owner;
- internal ArrayList object_items = new ArrayList ();
- internal ArrayList listbox_items = new ArrayList ();
- public ObjectCollection (ComboBox owner)
- {
- this.owner = owner;
- }
- #region Public Properties
- public virtual int Count {
- get { return object_items.Count; }
- }
- public virtual bool IsReadOnly {
- get { return false; }
- }
- public virtual object this [int index] {
- get {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException ("Index of out range");
- return object_items[index];
- }
- set {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException ("Index of out range");
- object_items[index] = value;
- }
- }
- bool ICollection.IsSynchronized {
- get { return false; }
- }
- object ICollection.SyncRoot {
- get { return this; }
- }
- bool IList.IsFixedSize {
- get { return false; }
- }
- #endregion Public Properties
- #region Public Methods
- public int Add (object item)
- {
- int idx;
- idx = AddItem (item);
- return idx;
- }
- public void AddRange (object[] items)
- {
- foreach (object mi in items)
- AddItem (mi);
- }
- public virtual void Clear ()
- {
- object_items.Clear ();
- listbox_items.Clear ();
- }
- public virtual bool Contains (object obj)
- {
- return object_items.Contains (obj);
- }
- public void CopyTo (object[] dest, int arrayIndex)
- {
- object_items.CopyTo (dest, arrayIndex);
- }
- void ICollection.CopyTo (Array dest, int index)
- {
- object_items.CopyTo (dest, index);
- }
- public virtual IEnumerator GetEnumerator ()
- {
- return object_items.GetEnumerator ();
- }
- int IList.Add (object item)
- {
- return Add (item);
- }
- public virtual int IndexOf (object value)
- {
- return object_items.IndexOf (value);
- }
- public virtual void Insert (int index, object item)
- {
- throw new NotImplementedException ();
- }
- public virtual void Remove (object value)
- {
- RemoveAt (IndexOf (value));
- }
- public virtual void RemoveAt (int index)
- {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException ("Index of out range");
- object_items.RemoveAt (index);
- listbox_items.RemoveAt (index);
- //owner.UpdateItemInfo (false, -1, -1);
- }
- #endregion Public Methods
- #region Private Methods
- private int AddItem (object item)
- {
- int cnt = object_items.Count;
- object_items.Add (item);
- listbox_items.Add (new ComboBox.ComboBoxItem (cnt));
- return cnt;
- }
- internal ComboBox.ComboBoxItem GetComboBoxItem (int index)
- {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException ("Index of out range");
- return (ComboBox.ComboBoxItem) listbox_items[index];
- }
- internal void SetComboBoxItem (ComboBox.ComboBoxItem item, int index)
- {
- if (index < 0 || index >= Count)
- throw new ArgumentOutOfRangeException ("Index of out range");
- listbox_items[index] = item;
- }
- #endregion Private Methods
- }
- /*
- class ListBoxPopUp
- */
- internal class ListBoxPopUp : Control
- {
- private ComboBox owner;
- private bool need_vscrollbar;
- private VScrollBar vscrollbar_ctrl;
- private int top_item; /* First item that we show the in the current page */
- private int last_item; /* Last visible item */
- private int highlighted_item; /* Item that is currently selected */
- private Rectangle textarea_rect; /* Rectangle of the drawable text area */
- public ListBoxPopUp (ComboBox owner): base ()
- {
- this.owner = owner;
- need_vscrollbar = false;
- top_item = 0;
- last_item = 0;
- highlighted_item = -1;
- MouseDown += new MouseEventHandler (OnMouseDownPUW);
- MouseMove += new MouseEventHandler (OnMouseMovePUW);
- MouseUp += new MouseEventHandler (OnMouseUpPUW);
- Paint += new PaintEventHandler (OnPaintPUW);
- SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
- SetStyle (ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
- /* Vertical scrollbar */
- vscrollbar_ctrl = new VScrollBar ();
- vscrollbar_ctrl.Minimum = 0;
- vscrollbar_ctrl.SmallChange = 1;
- vscrollbar_ctrl.LargeChange = 1;
- vscrollbar_ctrl.Maximum = 0;
- vscrollbar_ctrl.ValueChanged += new EventHandler (VerticalScrollEvent);
- vscrollbar_ctrl.Visible = false;
- }
- protected override CreateParams CreateParams
- {
- get {
- CreateParams cp = base.CreateParams;
- cp.Style = unchecked ((int)(WindowStyles.WS_POPUP | WindowStyles.WS_VISIBLE | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_CLIPCHILDREN));
- cp.ExStyle |= (int)WindowStyles.WS_EX_TOOLWINDOW;
- return cp;
- }
- }
- #region Private Methods
- protected override void CreateHandle ()
- {
- base.CreateHandle ();
- Controls.Add (vscrollbar_ctrl);
- }
- // Calcs the listbox area
- internal void CalcListBoxArea ()
- {
- int width, height;
- width = owner.ClientRectangle.Width;
- if (owner.Items.Count <= owner.MaxDropDownItems) {
- height = owner.ItemHeight * owner.Items.Count;
- need_vscrollbar = false;
- }
- else {
- height = owner.ItemHeight * owner.MaxDropDownItems;
- need_vscrollbar = true;
- vscrollbar_ctrl.Height = height - 1;
- vscrollbar_ctrl.Location = new Point (width - vscrollbar_ctrl.Width - 1, 1);
- vscrollbar_ctrl.Maximum = owner.Items.Count - owner.MaxDropDownItems;
- }
- if (vscrollbar_ctrl.Visible != need_vscrollbar)
- vscrollbar_ctrl.Visible = need_vscrollbar;
- Size = new Size (width, height);
- textarea_rect = ClientRectangle;
- // Exclude decorations
- textarea_rect.X += 1;
- textarea_rect.Y += 1;
- textarea_rect.Width -= 1;
- textarea_rect.Height -= 1;
- if (need_vscrollbar)
- textarea_rect.Width -= vscrollbar_ctrl.Width;
- last_item = LastVisibleItem ();
- }
- private void Draw (Rectangle clip)
- {
- Rectangle cl = ClientRectangle;
- if (owner.Items.Count > 0) {
- Rectangle item_rect;
- DrawItemState state = DrawItemState.None;
- for (int i = top_item; i < last_item; i++) {
- item_rect = GetItemDisplayRectangle (i, top_item);
- if (clip.IntersectsWith (item_rect) == false)
- continue;
- /* Draw item */
- state = DrawItemState.None;
- if (i == highlighted_item)
- state |= DrawItemState.Selected;
- owner.OnDrawItem (new DrawItemEventArgs (DeviceContext, owner.Font, item_rect,
- i, state, owner.ForeColor, owner.BackColor));
- }
- }
- //DeviceContext.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush
- // (owner.BackColor), ClientRectangle);
- DeviceContext.DrawRectangle (ThemeEngine.Current.ResPool.GetPen (ThemeEngine.Current.ColorWindowFrame),
- cl.X, cl.Y, cl.Width - 1, cl.Height - 1);
- }
- private Rectangle GetItemDisplayRectangle (int index, int first_displayble)
- {
- if (index < 0 || index >= owner.Items.Count)
- throw new ArgumentOutOfRangeException ("GetItemRectangle index out of range.");
- Rectangle item_rect = new Rectangle ();
- item_rect.X = 0;
- item_rect.Y = owner.ItemHeight * (index - first_displayble);
- item_rect.Height = owner.ItemHeight;
- item_rect.Width = textarea_rect.Width;
- return item_rect;
- }
- public void HideWindow ()
- {
- owner.ButtonReleased ();
- Hide ();
- }
- private int IndexFromPointDisplayRectangle (int x, int y)
- {
- for (int i = top_item; i < last_item; i++) {
- if (GetItemDisplayRectangle (i, top_item).Contains (x, y) == true)
- return i;
- }
- return -1;
- }
- private int LastVisibleItem ()
- {
- Rectangle item_rect;
- int top_y = textarea_rect.Y + textarea_rect.Height;
- int i = 0;
- for (i = top_item; i < owner.Items.Count; i++) {
- item_rect = GetItemDisplayRectangle (i, top_item);
- if (item_rect.Y > top_y)
- return i;
- }
- return i;
- }
- private void OnMouseDownPUW (object sender, MouseEventArgs e)
- {
- /* Click outside the client area destroys the popup */
- if (ClientRectangle.Contains (e.X, e.Y) == false) {
- HideWindow ();
- return;
- }
- /* Click on an element */
- int index = IndexFromPointDisplayRectangle (e.X, e.Y);
- if (index == -1) return;
- owner.SelectedIndex = index;
- HideWindow ();
- }
- private void OnMouseUpPUW (object sender, MouseEventArgs e)
- {
- }
- private void OnMouseMovePUW (object sender, MouseEventArgs e)
- {
- Rectangle invalidate;
- int index = IndexFromPointDisplayRectangle (e.X, e.Y);
- if (index == -1) return;
- /* Previous item */
- if (highlighted_item != -1) {
- invalidate = GetItemDisplayRectangle (highlighted_item, top_item);
- if (ClientRectangle.Contains (invalidate))
- Invalidate (invalidate);
- }
- highlighted_item = index;
- /* Current item */
- invalidate = GetItemDisplayRectangle (highlighted_item, top_item);
- if (ClientRectangle.Contains (invalidate))
- Invalidate (invalidate);
- }
- private void OnPaintPUW (Object o, PaintEventArgs pevent)
- {
- if (Width <= 0 || Height <= 0 || Visible == false)
- return;
- Draw (pevent.ClipRectangle);
- pevent.Graphics.DrawImage (ImageBuffer, pevent.ClipRectangle, pevent.ClipRectangle, GraphicsUnit.Pixel);
- }
- public void ShowWindow ()
- {
- CalcListBoxArea ();
- Show ();
- Refresh ();
- }
- // Value Changed
- private void VerticalScrollEvent (object sender, EventArgs e)
- {
- top_item = vscrollbar_ctrl.Value;
- last_item = LastVisibleItem ();
- Refresh ();
- }
- #endregion Private Methods
- }
- }
- }
|