1 // =================================
  2 // Copyright (c) 2021 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 
  8 namespace System.Windows
  9 {
 10     public class delegate void SelectedIndexChangedEventHandler();
 11 
 12     public enum ListBoxStyle : long
 13     {
 14         LBS_COMBOBOX = 0x8000
 15         LBS_DISABLENOSCROLL = 0x1000
 16         LBS_EXTENDEDSEL = 0x0800
 17         LBS_HASSTRINGS = 0x0040
 18         LBS_MULTICOLUMN = 0x0200
 19         LBS_MULTIPLESEL = 0x0008
 20         LBS_NODATA = 0x2000
 21         LBS_NOINTEGRALHEIGHT = 0x0100
 22         LBS_NOREDRAW = 0x0004
 23         LBS_NOSEL = 0x4000
 24         LBS_NOTIFY = 0x0001
 25         LBS_OWNERDRAWFIXED = 0x0010
 26         LBS_OWNERDRAWVARIABLE = 0x0020
 27         LBS_SORT = 0x0002
 28         LBS_STANDARD = LBS_NOTIFY | LBS_SORT | WindowStyle.WS_VSCROLL | WindowStyle.WS_BORDER
 29         LBS_USETABSTOPS = 0x0080
 30         LBS_WANTKEYBOARDINPUT = 0x0400
 31     }
 32 
 33     public nothrow ControlCreateParams& ListBoxControlCreateParams(ControlCreateParams& controlCreateParamsListBoxStyle listBoxStyle)
 34     {
 35         return controlCreateParams.SetWindowClassName("LISTBOX").
 36             SetWindowClassBackgroundColor(SystemColor.COLOR_WINDOW).SetBackgroundColor(Color.White()).
 37             SetWindowStyle(cast<WindowStyle>(DefaultChildWindowStyle() | WindowStyle.WS_TABSTOP | WindowStyle.WS_VSCROLL | listBoxStyle));
 38     }
 39 
 40     public nothrow ControlCreateParams& ListBoxControlCreateParams(ControlCreateParams& controlCreateParams)
 41     {
 42         return ListBoxControlCreateParams(controlCreateParamscast<ListBoxStyle>(ListBoxStyle.LBS_NOTIFY | ListBoxStyle.LBS_HASSTRINGS));
 43     }
 44 
 45     public class ListBoxCreateParams
 46     {
 47         public nothrow ListBoxCreateParams(ControlCreateParams& controlCreateParams_) : controlCreateParams(controlCreateParams_)
 48         {
 49         }
 50         public nothrow ListBoxCreateParams& Defaults()
 51         {
 52             return *this;
 53         }
 54         public ControlCreateParams& controlCreateParams;
 55     }
 56 
 57     public class ListBox : Control
 58     {
 59         public ListBox(ListBoxStyle listBoxStyleconst Color& backgroundColorconst string& textconst Point& locationconst Size& size
 60             Dock dockAnchors anchors) : 
 61             base("LISTBOX"DefaultWindowClassStyle()cast<WindowStyle>(DefaultChildWindowStyle() | WindowStyle.WS_TABSTOP | listBoxStyle)
 62                 DefaultExtendedWindowStyle()backgroundColortextlocationsizedockanchors)
 63         {
 64         }
 65         public ListBox(const Point& locationconst Size& sizeDock dockAnchors anchors) : 
 66             this(cast<ListBoxStyle>(ListBoxStyle.LBS_NOTIFY | ListBoxStyle.LBS_HASSTRINGS | WindowStyle.WS_VSCROLL)Color.White()
 67             string()locationsizedockanchors)
 68         {
 69         }
 70         public ListBox(ListBoxCreateParams& createParams) : base(createParams.controlCreateParams)
 71         {
 72         }
 73         public void AddItem(const string& item)
 74         {
 75             if (Handle() != null)
 76             {
 77                 wstring wstr(ToUtf16(item));
 78                 WinSendMessage(Handle()LB_ADDSTRING0ucast<long>(cast<ulong>(cast<void*>(wstr.Chars()))));
 79             }
 80         }
 81         public void DeleteItem(int itemIndex)
 82         {
 83             if (Handle() != null)
 84             {
 85                 WinSendMessage(Handle()LB_DELETESTRINGcast<uint>(itemIndex)0);
 86             }
 87         }
 88         public void InsertItem(int indexconst string& item)
 89         {
 90             if (Handle() != null)
 91             {
 92                 wstring wstr(ToUtf16(item));
 93                 WinSendMessage(Handle()LB_INSERTSTRINGcast<uint>(index)cast<long>(cast<ulong>(cast<void*>(wstr.Chars()))));
 94             }
 95         }
 96         public string GetItem(int index)
 97         {
 98             if (Handle() != null)
 99             {
100                 int size = cast<int>(WinSendMessage(Handle()LB_GETTEXTLENcast<uint>(index)0));
101                 UniquePtr<wchar> buf(cast<wchar*>(MemAlloc(size * sizeof(wchar))));
102                 WinSendMessage(Handle()LB_GETTEXTcast<uint>(index)cast<long>(cast<ulong>(cast<void*>(buf.Get()))));
103                 return ToUtf8(buf.Get());
104             }
105             return string();
106         }
107         public void SetItem(int indexconst string& item)
108         {
109             DeleteItem(index);
110             InsertItem(indexitem);
111         }
112         public int GetItemCount() const
113         {
114             if (Handle() != null)
115             {
116                 return cast<int>(WinSendMessage(Handle()LB_GETCOUNT0u0));
117             }
118             else
119             {
120                 return -1;
121             }
122         }
123         public int GetTopIndex() const
124         {
125             if (Handle() != null)
126             {
127                 return cast<int>(WinSendMessage(Handle()LB_GETTOPINDEX0u0));
128             }
129             else
130             {
131                 return -1;
132             }
133         }
134         public void SetTopIndex(int topIndex)
135         {
136             if (Handle() != null)
137             {
138                 WinSendMessage(Handle()LB_SETTOPINDEXcast<uint>(topIndex)0);
139             }
140         }
141         public int GetSelectedIndex() const
142         {
143             if (Handle() != null)
144             {
145                 return cast<int>(WinSendMessage(Handle()LB_GETCURSEL0u0));
146             }
147             else
148             {
149                 return -1;
150             }
151         }
152         public void SetSelectedIndex(int selectedIndex)
153         {
154             if (Handle() != null)
155             {
156                 WinSendMessage(Handle()LB_SETCURSELcast<uint>(selectedIndex)0);
157             }
158         }
159         protected override void OnCreated()
160         {
161             base->OnCreated();
162             Graphics graphics = Graphics.FromWindowHandle(Handle());
163             const FontHandle& fontHandle = GetFontHandle(graphics);
164             if (!fontHandle.IsNull())
165             {
166                 SendSetFontMessage(fontHandle);
167             }
168         }
169         internal void SelectedIndexChangedInternal()
170         {
171             OnSelectedIndexChanged();
172         }
173         protected virtual void OnSelectedIndexChanged()
174         {
175             selectedIndexChangedEvent.Fire();
176         }
177         public nothrow Event<SelectedIndexChangedEventHandler>& SelectedIndexChangedEvent() const
178         {
179             return selectedIndexChangedEvent;
180         }
181         private Event<SelectedIndexChangedEventHandler> selectedIndexChangedEvent;
182     }
183 }