1 // =================================
  2 // Copyright (c) 2024 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.Collections;
  8 
  9 namespace System.Windows
 10 {
 11     public const uint WM_DESTROY = 2u;
 12     public const uint WM_SIZE = 5u;
 13     public const uint WM_MOVE = 3u;
 14     public const uint WM_ACTIVATE = 6u;
 15     public const uint WM_SETFOCUS = 7u;
 16     public const uint WM_KILLFOCUS = 8u;
 17     public const uint WM_PAINT = 15u;
 18     public const uint WM_CLOSE = 16u;
 19     public const uint WM_SHOWWINDOW = 24u;
 20     public const uint WM_DRAWITEM = 43u;
 21     public const uint WM_SETFONT = 48u;
 22     public const uint WM_GETFONT = 49u;
 23     public const uint WM_KEYDOWN = 256u;
 24     public const uint WM_KEYUP = 257u;
 25     public const uint WM_CHAR = 258u;
 26     public const uint WM_COMMAND = 273u;
 27     public const uint WM_SYSCOMMAND = 274u;
 28     public const uint WM_TIMER = 275u;
 29     public const uint WM_HSCROLL = 276u;
 30     public const uint WM_VSCROLL = 277u;
 31     public const uint WM_MOUSEMOVE = 512u;
 32     public const uint WM_LBUTTONDOWN = 513u;
 33     public const uint WM_LBUTTONUP = 514u;
 34     public const uint WM_LBUTTONDBLCLK = 515u;
 35     public const uint WM_RBUTTONDOWN = 516u;
 36     public const uint WM_RBUTTONUP = 517u;
 37     public const uint WM_MOUSEWHEEL = 522u;
 38     public const uint WM_MOUSELEAVE = 675u;
 39     public const uint WM_CLIPBOARDUPDATE = 797u;
 40     public const uint WM_USER = 1024u;
 41 
 42     public const uint EM_SETSEL = 177u;
 43     public const uint BM_SETSTYLE = 244u;
 44     public const uint LB_GETCURSEL = 392u;
 45     public const uint LB_ADDSTRING = 384u;
 46     public const uint LB_DELETESTRING = 386u;
 47     public const uint LB_SETCURSEL = 390u;
 48     public const uint LB_INSERTSTRING = 385u;
 49     public const uint LB_GETTOPINDEX = 398u;
 50     public const uint LB_SETTOPINDEX = 407u;
 51     public const uint LB_GETCOUNT = 2075u;
 52     public const uint LB_GETTEXTLEN = 394u;
 53     public const uint LB_GETTEXT = 393u;
 54     public const uint BM_GETCHECK = 240u;
 55     public const uint BM_SETCHECK = 241u;
 56 
 57     public const ushort BN_CLICKED = 0u;
 58     public const ushort BN_SETFOCUS = 6u;
 59     public const ushort BN_KILLFOCUS = 7u;
 60     public const ushort EN_CHANGE = 768u;
 61     public const ushort EN_SETFOCUS = 256u;
 62     public const ushort EN_KILLFOCUS = 512u;
 63     public const ushort LBN_SETFOCUS = 4u;
 64     public const ushort LBN_KILLFOCUS = 5u;
 65     public const ushort LBN_SELCHANGE = 1u;
 66     public const ushort BST_CHECKED = 1u;
 67     public const ushort BST_UNCHECKED = 0u;
 68 
 69     public const uint SIZE_MAXIMIZED = 2u;
 70     public const uint SIZE_MINIMIZED = 1u;
 71     public const uint SIZE_RESTORED = 0u;
 72 
 73     public const uint ODT_MENU = 1u;
 74 
 75     public const uint HOVER_DEFAULT = cast<uint>(-1);
 76     public const uint TME_LEAVE = 2u;
 77 
 78     public const uint SC_KEYMENU = 61696u;
 79     public const uint SC_CLOSE = 61536u;
 80 
 81     public const ushort SB_BOTTOM = 7u;
 82     public const ushort SB_ENDSCROLL = 8u;
 83     public const ushort SB_LINEDOWN = 1u;
 84     public const ushort SB_LINEUP = 0u;
 85     public const ushort SB_PAGEDOWN = 3u;
 86     public const ushort SB_PAGEUP = 2u;
 87     public const ushort SB_THUMBPOSITION = 4u;
 88     public const ushort SB_THUMBTRACK = 5u;
 89     public const ushort SB_TOP = 6u;
 90     public const ushort SB_LEFT = 6u;
 91     public const ushort SB_RIGHT = 7u;
 92     public const ushort SB_LINELEFT = 0u;
 93     public const ushort SB_LINERIGHT = 1u;
 94     public const ushort SB_PAGELEFT = 2u;
 95     public const ushort SB_PAGERIGHT = 3u;
 96 
 97     public const uint CM_ENDMODAL = WM_USER + 1u;
 98 
 99     public class PaintEventArgs
100     {
101         public PaintEventArgs(Graphics& graphics_const Rect& clipRect_) : graphics(graphics_)clipRect(clipRect_)errorId(0)
102         {
103         }
104         public Graphics& graphics;
105         public Rect clipRect;
106         public int errorId;
107     }
108 
109     public class EnterLeaveEventArgs
110     {
111         public EnterLeaveEventArgs() : errorId(0)
112         {
113         }
114         public int errorId;
115     }
116 
117     public enum MouseButtons : ulong
118     {
119         none = 0u
120         lbutton = 1u
121         rbutton = 2u
122         shift = 4u
123         control = 8u
124         mbutton = 16u
125         xbutton1 = 32u
126         xbutton2 = 64u
127     }
128 
129     public class MouseEventArgs
130     {
131         public MouseEventArgs(const Point& location_MouseButtons buttons_int clicks_) : location(location_)buttons(buttons_)clicks(clicks_)errorId(0)
132         {
133         }
134         public Point location;
135         public MouseButtons buttons;
136         public int clicks;
137         public int errorId;
138     }
139 
140     public class MouseWheelEventArgs : MouseEventArgs
141     {
142         public MouseWheelEventArgs(const Point& location_MouseButtons buttons_short distance_) : base(location_buttons_0)distance(distance_)
143         {
144         }
145         public short distance;
146         public bool handled;
147     }
148 
149     public class KeyPressEventArgs
150     {
151         public explicit KeyPressEventArgs(wchar keyChar_) : keyChar(keyChar_)handled(false)errorId(0)
152         {
153         }
154         public wchar keyChar;
155         public bool handled;
156         public int errorId;
157     }
158 
159     public class KeyEventArgs
160     {
161         public explicit KeyEventArgs(Keys key_) : key(key_)handled(false)suppressKeyPress(false)errorId(0)
162         {
163         }
164         public Keys key;
165         public bool handled;
166         public bool suppressKeyPress;
167         public int errorId;
168     }
169 
170     public class TreeViewNodeMouseClickEventArgs
171     {
172         public TreeViewNodeMouseClickEventArgs(const Point& location_MouseButtons buttons_int clicks_TreeViewNode* node_) : 
173             location(location_)buttons(buttons_)clicks(clicks_)node(node_)errorId(0)
174         {
175         }
176         public Point location;
177         public MouseButtons buttons;
178         public int clicks;
179         public TreeViewNode* node;
180         public int errorId;
181     }
182 
183     public class TreeViewNodeEventArgs
184     {
185         public explicit TreeViewNodeEventArgs(TreeViewNode* node_) : node(node_)errorId(0)
186         {
187         }
188         public TreeViewNode* node;
189         public int errorId;
190     }
191 
192     public class ControlEventArgs
193     {
194         public explicit ControlEventArgs(Control* control_) : control(control_)errorId(0)
195         {
196         }
197         public Control* control;
198         public int errorId;
199     }
200 
201     public class SizeChangedEventArgs
202     {
203         public explicit SizeChangedEventArgs(uint windowState_) : windowState(windowState_)errorId(0)
204         {
205         }
206         public uint windowState;
207         public int errorId;
208     }
209 
210     public class SizeChangingEventArgs
211     {
212         public SizeChangingEventArgs(const Size& oldSize_const Size& newSize_) : oldSize(oldSize_)newSize(newSize_)errorId(0)
213         {
214         }
215         public Size oldSize;
216         public Size newSize;
217         public int errorId;
218     }
219 
220     public class ClickEventArgs
221     {
222         public ClickEventArgs() : errorId(0)
223         {
224         }
225         public int errorId;
226     }
227 
228     public class RightClickEventArgs
229     {
230         public RightClickEventArgs(Control* control_const Point& location_) : control(control_)location(location_)errorId(0)
231         {
232         }
233         public Control* control;
234         public Point location;
235         public int errorId;
236     }
237 
238     public class ScrollEventArgs
239     {
240         public explicit ScrollEventArgs(ushort request_) : request(request_)errorId(0)
241         {
242         }
243         public ushort request;
244         public int errorId;
245     }
246 
247     public class TimerEventArgs
248     {
249         public explicit TimerEventArgs(ulong timerId_) : timerId(timerId_)errorId(0)
250         {
251         }
252         public ulong timerId;
253         public int errorId;
254     }
255 
256     public class Message
257     {
258         public Message(void* windowHandle_uint msg_ulong wparam_long lparam_long result_) : windowHandle(windowHandle_)msg(msg_)wparam(wparam_)lparam(lparam_)result(result_)
259         {
260         }
261         public inline ushort WParamLoWord() const
262         {
263             return cast<ushort>(wparam & 65535u);
264         }
265         public inline ushort WParamHiWord() const
266         {
267             return cast<ushort>((wparam >> 16u) & 65535u);
268         }
269         public inline void* LParamHandle() const
270         {
271             return cast<void*>(cast<ulong>(lparam));
272         }
273         public inline uint LParamLoDWord() const
274         {
275             return cast<uint>(lparam);
276         }
277         public inline uint LParamHiDWord() const
278         {
279             return cast<uint>((lparam >> 32u) & 4294967295u);
280         }
281         public inline int LParamX() const
282         {
283             return cast<int>(cast<short>(LParamLoDWord()));
284         }
285         public inline int LParamY() const
286         {
287             return cast<int>(cast<short>(LParamLoDWord() >> 16u));
288         }
289         public inline Size LParamSize() const
290         {
291             uint s = LParamLoDWord();
292             ushort sw = cast<ushort>(s);
293             ushort sh = cast<ushort>(s >> 16u);
294             return Size(cast<int>(sw)cast<int>(sh));
295         }
296         public inline Point LParamLocation() const
297         {
298             uint s = LParamLoDWord();
299             ushort sx = cast<ushort>(s);
300             ushort sy = cast<ushort>(s >> 16u);
301             return Point(cast<int>(sx)cast<int>(sy));
302         }
303         public void* windowHandle;
304         public uint msg;
305         public ulong wparam;
306         public long lparam;
307         public long result;
308         public void* originalWndProc;
309     }
310 
311     public class MessageNameMap
312     {
313         static MessageNameMap() : instance(new MessageNameMap())
314         {
315         }
316         public static MessageNameMap& Instance()
317         {
318             return *instance;
319         }
320         [nodiscard]
321         public Result<string> GetMessageName(uint message) const
322         {
323             HashMap<uintstring>.ConstIterator it = map.CFind(message);
324             if (it != map.CEnd())
325             {
326                 return it->second;
327             }
328             else
329             {
330                 auto hexStringResult = ToHexString(message);
331                 if (hexStringResult.Error())
332                 {
333                     return Result<string>(ErrorId(hexStringResult.GetErrorId()));
334                 }
335                 return "WM_" + hexStringResult.Value();
336             }
337         }
338         private MessageNameMap()
339         {
340             map[WM_DESTROY] = "WM_DESTROY";
341             map[WM_SIZE] = "WM_SIZE";
342             map[WM_MOVE] = "WM_MOVE";
343             map[WM_SETFOCUS] = "WM_SETFOCUS";
344             map[WM_KILLFOCUS] = "WM_KILLFOCUS";
345             map[WM_PAINT] = "WM_PAINT";
346             map[WM_CLOSE] = "WM_CLOSE";
347             map[WM_SHOWWINDOW] = "WM_SHOWWINDOW";
348             map[WM_DRAWITEM] = "WM_DRAWITEM";
349             map[WM_SETFONT] = "WM_SETFONT";
350             map[WM_GETFONT] = "WM_GETFONT";
351             map[WM_KEYDOWN] = "WM_KEYDOWN";
352             map[WM_KEYUP] = "WM_KEYUP";
353             map[WM_CHAR] = "WM_CHAR";
354             map[WM_COMMAND] = "WM_COMMAND";
355             map[WM_SYSCOMMAND] = "WM_SYSCOMMAND";
356             map[WM_TIMER] = "WM_TIMER";
357             map[WM_HSCROLL] = "WM_HSCROLL";
358             map[WM_VSCROLL] = "WM_VSCROLL";
359             map[WM_MOUSEMOVE] = "WM_MOUSEMOVE";
360             map[WM_LBUTTONDOWN] = "WM_LBUTTONDOWN";
361             map[WM_LBUTTONUP] = "WM_LBUTTONUP";
362             map[WM_LBUTTONDBLCLK] = "WM_LBUTTONDBLCLK";
363             map[WM_RBUTTONDOWN] = "WM_RBUTTONDOWN";
364             map[WM_RBUTTONUP] = "WM_RBUTTONUP";
365             map[WM_MOUSEWHEEL] = "WM_MOUSEWHEEL";
366             map[WM_MOUSELEAVE] = "WM_MOUSELEAVE";
367             map[WM_CLIPBOARDUPDATE] = "WM_CLIPBOARDUPDATE";
368             map[WM_USER] = "WM_USER";
369         }
370         private static UniquePtr<MessageNameMap> instance;
371         private HashMap<uintstring> map;
372     }