cat_engine_basement/windows/core/window/
mod.rs

1use super::*;
2
3mod window;
4pub use window::Window;
5
6use core::{
7    mem::{
8        transmute,
9        transmute_copy,
10    },
11    ptr::NonNull,
12};
13
14use winapi::{
15    shared::{
16        windef::{
17            HWND,
18        },
19    },
20    um::{
21        winuser::{
22            DefWindowProcA,
23            DefWindowProcW,
24
25            // window styles
26            WS_BORDER,
27            WS_CAPTION,
28            WS_CHILDWINDOW,
29            WS_CLIPCHILDREN,
30            WS_CLIPSIBLINGS,
31            WS_DISABLED,
32            WS_DLGFRAME,
33            WS_HSCROLL,
34            WS_MAXIMIZE,
35            WS_MAXIMIZEBOX,
36            WS_MINIMIZE,
37            WS_MINIMIZEBOX,
38            WS_OVERLAPPED,
39            WS_POPUP,
40            WS_SIZEBOX,
41            WS_SYSMENU,
42            WS_VISIBLE,
43            WS_VSCROLL,
44            // extended window styles
45            WS_EX_ACCEPTFILES,
46            WS_EX_APPWINDOW,
47            WS_EX_CLIENTEDGE,
48            WS_EX_COMPOSITED,
49            WS_EX_CONTEXTHELP,
50            WS_EX_CONTROLPARENT,
51            WS_EX_DLGMODALFRAME,
52            WS_EX_LAYERED,
53            WS_EX_LAYOUTRTL,
54            WS_EX_LEFT,
55            WS_EX_LEFTSCROLLBAR,
56            WS_EX_MDICHILD,
57            WS_EX_NOACTIVATE,
58            WS_EX_NOINHERITLAYOUT,
59            WS_EX_NOPARENTNOTIFY,
60            WS_EX_NOREDIRECTIONBITMAP,
61            WS_EX_RIGHT,
62            WS_EX_RTLREADING,
63            WS_EX_STATICEDGE,
64            WS_EX_TOOLWINDOW,
65            WS_EX_TOPMOST,
66            WS_EX_TRANSPARENT,
67            WS_EX_WINDOWEDGE,
68            // show command
69            SW_HIDE,
70            SW_SHOWNORMAL,
71            SW_SHOWMINIMIZED,
72            SW_SHOWMAXIMIZED,
73            SW_SHOWNOACTIVATE,
74            SW_SHOW,
75            SW_MINIMIZE,
76            SW_SHOWMINNOACTIVE,
77            SW_SHOWNA,
78            SW_RESTORE,
79            SW_SHOWDEFAULT,
80            SW_FORCEMINIMIZE,
81            // other
82            GWL_EXSTYLE,
83            GWL_STYLE,
84            GWLP_USERDATA,
85            GWLP_WNDPROC,
86            CW_USEDEFAULT,
87            RDW_INTERNALPAINT,
88            RDW_INVALIDATE,
89            SWP_SHOWWINDOW,
90            SWP_NOREPOSITION,
91            SWP_NOSIZE,
92            SWP_NOMOVE,
93            SWP_FRAMECHANGED,
94            GWLP_HINSTANCE,
95            GWLP_ID,
96            // class data
97            GCW_ATOM,
98            GCL_CBCLSEXTRA,
99            GCL_CBWNDEXTRA,
100            GCLP_HBRBACKGROUND,
101            GCLP_HCURSOR,
102            GCLP_HICON,
103            GCLP_HICONSM,
104            GCLP_HMODULE,
105            GCLP_MENUNAME,
106            GCL_STYLE,
107            GCLP_WNDPROC,
108        },
109    }
110};
111
112/// A replacement for `HWND`.
113/// Can be wraped with `Option` with null pointer optimization.
114#[derive(Clone,Copy,Debug)]
115#[repr(transparent)]
116pub struct WindowHandle{
117    inner:NonNull<HWND>,
118}
119
120implement_handle_wrapper!(WindowHandle,HWND);
121
122/// Represents a window style.
123#[repr(u32)]
124#[derive(Clone,Copy,Debug)]
125pub enum WindowStyle{
126    /// The window has a thin-line border.
127    /// 
128    /// 0x00800000
129    Border=WS_BORDER,
130
131    /// The window has a title bar (includes the WS_BORDER style).
132    /// 
133    /// 0x00C00000
134    Caption=WS_CAPTION,
135
136    /// The window is a child window. A window with this style cannot have a menu bar.
137    /// This style cannot be used with the WS_POPUP style.
138    /// 
139    /// 0x40000000
140    ChildWindow=WS_CHILDWINDOW,
141
142    /// Excludes the area occupied by child windows when drawing occurs within the parent window.
143    /// This style is used when creating the parent window.
144    /// 
145    /// 0x02000000
146    ClipChildren=WS_CLIPCHILDREN,
147
148    /// Clips child windows relative to each other;
149    /// that is, when a particular child window receives a WM_PAINT message,
150    /// the WS_CLIPSIBLINGS style clips all other overlapping child windows
151    /// out of the region of the child window to be updated.
152    /// If WS_CLIPSIBLINGS is not specified and child windows overlap,
153    /// it is possible, when drawing within the client area of a child window,
154    /// to draw within the client area of a neighboring child window.
155    /// 
156    /// 0x04000000
157    ClipSiblings=WS_CLIPSIBLINGS,
158
159    /// The window has a border of a style typically used with dialog boxes.
160    /// A window with this style cannot have a title bar.
161    /// 
162    /// 0x00400000
163    DialogueFrame=WS_DLGFRAME,
164
165    /// The window has a horizontal scroll bar.
166    /// 
167    /// 0x00100000
168    HorizontalScroll=WS_HSCROLL,
169
170    /// The window has a vertical scroll bar.
171    /// 
172    /// 0x00200000
173    VerticalScroll=WS_VSCROLL,
174
175    /// The window is initially maximized.
176    /// 
177    /// 0x01000000
178    Maximized=WS_MAXIMIZE,
179
180    /// The window is a control that can receive the keyboard focus when the user presses the TAB key.
181    /// Pressing the TAB key changes the keyboard focus to the next control with the WS_TABSTOP style.
182    /// You can turn this style on and off to change dialog box navigation.
183    /// To change this style after a window has been created, use the SetWindowLong function.
184    /// For user-created windows and modeless dialogs to work with tab stops, alter the message loop to call the IsDialogMessage function.
185    /// 
186    /// The window has a maximize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.
187    /// 
188    /// 0x00010000
189    MaximizeBox=WS_MAXIMIZEBOX,
190
191    /// The window is initially minimized.
192    /// 
193    /// 0x20000000
194    Minimized=WS_MINIMIZE,
195
196    /// The window is the first control of a group of controls.
197    /// The group consists of this first control and all controls defined after it, up to the next control with the WS_GROUP style.
198    /// The first control in each group usually has the WS_TABSTOP style so that the user can move from group to group.
199    /// The user can subsequently change the keyboard focus from one control in the group to the next control in the group by using the direction keys.
200    /// You can turn this style on and off to change dialog box navigation.
201    /// To change this style after a window has been created, use the SetWindowLong function.
202    /// 
203    /// The window has a minimize button.
204    /// Cannot be combined with the WS_EX_CONTEXTHELP style.
205    /// The WS_SYSMENU style must also be specified.
206    /// 
207    /// 0x00020000
208    MinimizeBox=WS_MINIMIZEBOX,
209
210    /// The window is an overlapped window.
211    /// An overlapped window has a title bar and a border.
212    /// 
213    /// 0x00000000
214    OverLapped=WS_OVERLAPPED,
215
216    /// The window is a pop-up window.
217    /// This style cannot be used with the WS_CHILD style.
218    /// 
219    /// 0x80000000
220    PopUp=WS_POPUP,
221
222    /// The window has a sizing border.
223    /// 
224    /// 0x00040000
225    SizeBox=WS_SIZEBOX,
226
227    /// The window has a window menu on its title bar.
228    /// The WS_CAPTION style must also be specified.
229    /// 
230    /// 0x00080000
231    SystemMenu=WS_SYSMENU,
232
233    /// The window is initially visible.
234    /// This style can be turned on and off by using the ShowWindow or SetWindowPos function.
235    /// 
236    /// 0x10000000
237    Visible=WS_VISIBLE,
238
239    /// The window is initially disabled.
240    /// A disabled window cannot receive input from the user.
241    /// To change this after a window has been created, use the EnableWindow function.
242    /// 
243    /// 0x08000000
244    Disabled=WS_DISABLED,
245}
246
247/// Represents an extended window style.
248#[repr(u32)]
249#[derive(Clone,Copy,Debug)]
250pub enum ExtendedWindowStyle{
251    /// The window accepts drag-drop files.
252    /// 
253    /// 0x00000010
254    AcceptFiles=WS_EX_ACCEPTFILES,
255
256    /// Forces a top-level window onto the taskbar when the window is visible.
257    /// 
258    /// 0x00040000
259    AppWwindow=WS_EX_APPWINDOW,
260
261    /// The window has a border with a sunken edge.
262    /// 
263    /// 0x00000200
264    ClientEdge=WS_EX_CLIENTEDGE,
265
266    /// Paints all descendants of a window in bottom-to-top painting order using double-buffering.
267    /// Bottom-to-top painting order allows a descendent window to have translucency (alpha) and transparency (colour-key) effects,
268    /// but only if the descendent window also has the WS_EX_TRANSPARENT bit set.
269    /// Double-buffering allows the window and its descendents to be painted without flicker.
270    /// This cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.
271    /// Windows 2000: This style is not supported.
272    /// 
273    /// 0x02000000
274    Composited=WS_EX_COMPOSITED,
275
276    /// The title bar of the window includes a question mark.
277    /// When the user clicks the question mark, the cursor changes to a question mark with a pointer.
278    /// If the user then clicks a child window, the child receives a WM_HELP message.
279    /// The child window should pass the message to the parent window procedure,
280    /// which should call the WinHelp function using the HELP_WM_HELP command.
281    /// The Help application displays a pop-up window that typically contains help for the child window.
282    /// WS_EX_CONTEXTHELP cannot be used with the WS_MAXIMIZEBOX or WS_MINIMIZEBOX styles.
283    /// 
284    /// 0x00000400
285    ContextHelp=WS_EX_CONTEXTHELP,
286
287    /// The window itself contains child windows that should take part in dialog box navigation.
288    /// If this style is specified, the dialog manager recurses into children of this window
289    /// when performing navigation operations such as handling the TAB key,
290    /// an arrow key, or a keyboard mnemonic.
291    /// 
292    /// 0x00010000
293    ControlParent=WS_EX_CONTROLPARENT,
294
295    /// The window has a double border;
296    /// the window can, optionally,
297    /// be created with a title bar by specifying the WS_CAPTION style in the dwStyle parameter.
298    /// 
299    /// 0x00000001
300    DLGMODALFRAME=WS_EX_DLGMODALFRAME,
301
302    /// The window is a layered window.
303    /// This style cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.
304    /// Windows 8: The WS_EX_LAYERED style is supported for top-level windows and child windows.
305    /// Previous Windows versions support WS_EX_LAYERED only for top-level windows.
306    /// 
307    /// 0x00080000
308    Layered=WS_EX_LAYERED,
309
310    /// If the shell language is Hebrew, Arabic, or another language that supports reading order alignment,
311    /// the horizontal origin of the window is on the right edge.
312    /// Increasing horizontal values advance to the left.
313    /// 
314    /// 0x00400000
315    LayoutRTL=WS_EX_LAYOUTRTL,
316
317    /// The window has generic left-aligned properties.
318    /// The window text is displayed using left-to-right reading-order properties.
319    /// 
320    /// The vertical scroll bar (if present) is to the right of the client area.
321    /// 
322    /// This is the default.
323    /// 
324    /// 0x00000000
325    Default=WS_EX_LEFT,
326
327    /// If the shell language is Hebrew, Arabic, or another language that supports reading order alignment,
328    /// the vertical scroll bar (if present) is to the left of the client area.
329    /// For other languages, the style is ignored.
330    /// 
331    /// 0x00004000
332    LeftScrollBar=WS_EX_LEFTSCROLLBAR,
333
334    /// The window is a MDI child window.
335    /// 
336    /// 0x00000040
337    MDIChild=WS_EX_MDICHILD,
338
339    /// A top-level window created with this style does not become the foreground window when the user clicks it.
340    /// The system does not bring this window to the foreground when the user minimizes or closes the foreground window.
341    /// The window should not be activated through programmatic access or via keyboard navigation by accessible technology, such as Narrator.
342    /// To activate the window, use the SetActiveWindow or SetForegroundWindow function.
343    /// The window does not appear on the taskbar by default.
344    /// To force the window to appear on the taskbar, use the WS_EX_APPWINDOW style.
345    /// 
346    /// 0x08000000
347    NoActive=WS_EX_NOACTIVATE,
348
349    /// The window does not pass its window layout to its child windows.
350    /// 
351    /// 0x00100000
352    NoInheritLayout=WS_EX_NOINHERITLAYOUT,
353
354    /// The child window created with this style does not send the WM_PARENTNOTIFY message
355    /// to its parent window when it is created or destroyed.
356    /// 
357    /// 0x00000004
358    NoParentNotify=WS_EX_NOPARENTNOTIFY,
359
360    /// The window does not render to a redirection surface.
361    /// This is for windows that do not have visible content or that use mechanisms other than surfaces to provide their visual.
362    /// 
363    /// 0x00200000
364    NoDirectionBitmap=WS_EX_NOREDIRECTIONBITMAP,
365
366    /// The window has generic "right-aligned" properties.
367    /// This depends on the window class.
368    /// This style has an effect only if the shell language is Hebrew, Arabic, or another language that supports reading-order alignment; otherwise, the style is ignored.
369    /// Using the WS_EX_RIGHT style for static or edit controls has the same effect as using the SS_RIGHT or ES_RIGHT style, respectively.
370    /// Using this style with button controls has the same effect as using BS_RIGHT and BS_RIGHTBUTTON styles.
371    /// 
372    /// 0x00001000
373    Right=WS_EX_RIGHT,
374
375    /// If the shell language is Hebrew, Arabic, or another language that supports reading-order alignment,
376    /// the window text is displayed using right-to-left reading-order properties.
377    /// For other languages, the style is ignored.
378    /// 
379    /// 0x00002000
380    RTLReading=WS_EX_RTLREADING,
381
382    /// The window has a three-dimensional border style intended to be used for items that do not accept user input.
383    /// 
384    /// 0x00020000
385    StaticEdge=WS_EX_STATICEDGE,
386
387    /// The window is intended to be used as a floating toolbar.
388    /// A tool window has a title bar that is shorter than a normal title bar, and the window title is drawn using a smaller font.
389    /// A tool window does not appear in the taskbar or in the dialog that appears when the user presses ALT+TAB.
390    /// If a tool window has a system menu, its icon is not displayed on the title bar.
391    /// However, you can display the system menu by right-clicking or by typing ALT+SPACE.
392    /// 
393    /// 0x00000080
394    ToolWindow=WS_EX_TOOLWINDOW,
395
396    /// The window should be placed above all non-topmost windows and should stay above them,
397    /// even when the window is deactivated.
398    /// To add or remove this style, use the SetWindowPos function.
399    /// 
400    /// 0x00000008
401    TopMost=WS_EX_TOPMOST,
402
403    /// The window should not be painted until siblings beneath the window (that were created by the same thread) have been painted.
404    /// The window appears transparent because the bits of underlying sibling windows have already been painted.
405    /// To achieve transparency without these restrictions, use the SetWindowRgn function.
406    /// 
407    /// 0x00000020
408    Transparent=WS_EX_TRANSPARENT,
409
410    /// The window has a border with a raised edge.
411    /// 
412    /// 0x00000100
413    WindowEdge=WS_EX_WINDOWEDGE,
414}
415
416#[repr(i32)]
417#[derive(Clone,Copy,Debug)]
418pub enum WindowData{
419    /// Sets a new window style.
420    Style=GWL_STYLE,
421
422    /// Sets a new extended window style.
423    ExtendedStyle=GWL_EXSTYLE,
424
425    /// Sets a new application instance handle.
426    InstanceHandle=GWLP_HINSTANCE,
427
428    /// Sets a new identifier of the child window.
429    /// The window cannot be a top-level window.
430    ChildID=GWLP_ID,
431
432    /// Sets the user data associated with the window.
433    /// This data is intended for use by the application that created the window.
434    /// Its value is initially zero.
435    UserData=GWLP_USERDATA,
436
437    /// Sets a new address for the window procedure.
438    WindowProcedure=GWLP_WNDPROC,
439
440    ///Sets the return value of a message processed in the dialog box procedure.
441    MessageResult=0i32,
442
443    /// Sets the new pointer to the dialogue box procedure.
444    DialogueBoxProcedure=8i32,
445
446    /// Sets new extra information that is private to the application,
447    /// such as handles or pointers.
448    User=16i32,
449}
450
451/// Controls how the window is to be shown.
452#[repr(i32)]
453pub enum ShowCommand{
454    /// Hides the window and activates another window.
455    Hide=SW_HIDE,
456
457    /// Activates and displays a window.
458    /// If the window is minimized or maximized,
459    /// the system restores it to its original size and position.
460    /// An application should specify this flag
461    /// when displaying the window for the first time.
462    ShowNormal=SW_SHOWNORMAL,
463
464    /// Activates the window and displays it as a minimized window.
465    ShowMinimized=SW_SHOWMINIMIZED,
466
467    /// Activates the window and displays it as a maximized window.
468    ShowMaximized=SW_SHOWMAXIMIZED,
469
470    /// Displays a window in its most recent size and position.
471    /// This value is similar to `ShowCommand::ShowNormal`,
472    /// except that the window is not activated.
473    ShowNoActivate=SW_SHOWNOACTIVATE,
474
475    /// Activates the window and displays it in its current size and position.
476    Show=SW_SHOW,
477
478    /// Minimizes the specified window and activates the next top-level window in the Z order.
479    Minimize=SW_MINIMIZE,
480
481    /// Displays the window as a minimized window.
482    /// This value is similar to `ShowCommand::ShowMinimized`,
483    /// except the window is not activated.
484    ShowMinimizedNoActivate=SW_SHOWMINNOACTIVE,
485
486    /// Displays the window in its current size and position.
487    /// This value is similar to `ShowCommand::Show`,
488    /// except that the window is not activated.
489    ShowNoActivate2=SW_SHOWNA,
490
491    /// Activates and displays the window.
492    /// If the window is minimized or maximized,
493    /// the system restores it to its original size and position.
494    /// An application should specify this flag when restoring a minimized window.
495    Restore=SW_RESTORE,
496
497    /// Sets the show state based on the `ShowCommand` value
498    /// specifiedin the `STARTUPINFO` structure passed to the CreateProcess function
499    /// by the program that started the application.
500    ShowDefault=SW_SHOWDEFAULT,
501
502    /// Minimizes a window, even if the thread that owns the window is not responding.
503    /// This flag should only be used when minimizing windows from a different thread.
504    ForceMinimized=SW_FORCEMINIMIZE,
505}
506
507
508/// Represents window styles.
509pub struct WindowStyles{
510    pub flag:u32
511}
512
513impl WindowStyles{
514    /// Creates a flag with the given styles.
515    pub const fn raw(flag:u32)->WindowStyles{
516        Self{
517            flag,
518        }
519    }
520
521    /// Creates a flag with no styles set.
522    pub const fn new()->WindowStyles{
523        Self{
524            flag:0u32,
525        }
526    }
527
528    /// Sets a style.
529    pub const fn set(mut self,style:WindowStyle)->WindowStyles{
530        self.flag|=style as u32;
531        self
532    }
533
534    /// Removes a style.
535    pub const fn remove(mut self,style:WindowStyle)->WindowStyles{
536        self.flag&=!(style as u32);
537        self
538    }
539}
540
541/// Represents extended window styles.
542pub struct ExtendedWindowStyles{
543    pub flag:u32
544}
545
546impl ExtendedWindowStyles{
547    /// Creates a flag with the given styles.
548    pub const fn raw(flag:u32)->ExtendedWindowStyles{
549        Self{
550            flag,
551        }
552    }
553
554    /// Creates a flag with no styles set.
555    pub const fn new()->ExtendedWindowStyles{
556        Self{
557            flag:0u32,
558        }
559    }
560
561    /// Sets a style.
562    pub const fn set(mut self,style:ExtendedWindowStyle)->ExtendedWindowStyles{
563        self.flag|=style as u32;
564        self
565    }
566
567    /// Removes a style.
568    pub const fn remove(mut self,style:ExtendedWindowStyle)->ExtendedWindowStyles{
569        self.flag&=!(style as u32);
570        self
571    }
572}
573
574#[repr(i32)]
575#[derive(Clone,Copy,Debug)]
576pub enum ClassData{
577    /// Retrieves an ATOM value that uniquely identifies the window class.
578    /// This is the same atom that the RegisterClassEx function returns.
579    Atom=GCW_ATOM,
580
581    /// Retrieves the size, in bytes, of the extra memory associated with the class.
582    ClassExtraDataSize=GCL_CBCLSEXTRA,
583
584    /// Retrieves the size, in bytes, of the extra window memory associated with each window in the class.
585    /// For information on how to access this memory, see `Window::get_window_long_ptr`.
586    WindowExtraDataSize=GCL_CBWNDEXTRA,
587
588    /// Retrieves a handle to the background brush associated with the class.
589    BackgroundBrushHandle=GCLP_HBRBACKGROUND,
590
591    /// Retrieves a handle to the cursor associated with the class.
592    CursorHandle=GCLP_HCURSOR,
593
594    /// Retrieves a handle to the icon associated with the class.
595    IconHandle=GCLP_HICON,
596
597    /// Retrieves a handle to the small icon associated with the class.
598    SmallIconHandle=GCLP_HICONSM,
599
600    /// Retrieves a handle to the module that registered the class.
601    ModuleHandle=GCLP_HMODULE,
602
603    /// Retrieves the pointer to the menu name string.
604    /// The string identifies the menu resource associated with the class.
605    MenuName=GCLP_MENUNAME,
606
607    /// Retrieves the window-class style bits.
608    ClassStyle=GCL_STYLE,
609
610    /// Retrieves the address of the window procedure,
611    /// or a handle representing the address of the window procedure.
612    /// You must use the CallWindowProc function to call the window procedure.
613    WindowProcedute=GCLP_WNDPROC,
614
615    ExtraData=0i32,
616}
617
618
619/// Calls the default window procedure
620/// to provide default processing for any window messages
621/// that an application does not process.
622/// This function ensures that every message is processed.
623/// `default_window_procedure` is called
624/// with the same parameters received by the window procedure.
625pub unsafe extern "system" fn default_window_procedure(
626    window_handle:WindowHandle,
627    message:u32,
628    w_param:usize,
629    l_param:isize
630)->isize{
631    DefWindowProcW(window_handle.as_raw(),message,w_param,l_param)
632}
633
634pub unsafe extern "system" fn default_window_procedure_ansi(
635    window_handle:WindowHandle,
636    message:u32,
637    w_param:usize,
638    l_param:isize
639)->isize{
640    DefWindowProcA(window_handle.as_raw(),message,w_param,l_param)
641}