2 * Copyright (c) 2001, Gary R. Van Sickle.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * A copy of the GNU General Public License can be found at
12 * Written by Gary R. Van Sickle <g.r.vansickle@worldnet.att.net>
16 // This is the implementation of the Window class. It serves both as a window class
17 // in its own right and as a base class for other window-like classes (e.g. PropertyPage,
23 ATOM
Window::WindowClassAtom
= 0;
24 HINSTANCE
Window::AppInstance
= NULL
;
26 // FIXME: I know, this is brutal. Mutexing should at least make window creation threadsafe,
27 // but if somebody has any ideas as to how to get rid of it entirely, please tell me / do so.
28 struct REFLECTION_INFO
35 REFLECTION_INFO ReflectionInfo
;
46 // Delete any fonts we created.
48 for (i
= 0; i
< FontCounter
; i
++)
50 DeleteObject (Fonts
[i
]);
54 // FIXME: Maybe do some reference counting and do this Unregister
55 // when there are no more of us left. Not real critical unless
56 // we're in a DLL which we're not right now.
57 //UnregisterClass(WindowClassAtom, InstanceHandle);
61 Window::FirstWindowProcReflector (HWND hwnd
, UINT uMsg
, WPARAM wParam
,
64 // Get our this pointer
65 REFLECTION_INFO
*rip
= &ReflectionInfo
;
69 rip
->FirstCall
= false;
71 // Set the Window handle so the real WindowProc has one to work with.
72 rip
->This
->WindowHandle
= hwnd
;
74 // Set a backreference to this class instance in the HWND.
75 // FIXME: Should really be SetWindowLongPtr(), but it appears to
76 // not be defined yet.
77 SetWindowLong (hwnd
, GWL_USERDATA
, (LONG
) rip
->This
);
79 // Set a new WindowProc now that we have the peliminaries done.
80 // Like subclassing, only not.
81 SetWindowLong (hwnd
, GWL_WNDPROC
, (LONG
) & Window::WindowProcReflector
);
84 return rip
->This
->WindowProc (uMsg
, wParam
, lParam
);
88 Window::WindowProcReflector (HWND hwnd
, UINT uMsg
, WPARAM wParam
,
93 // Get our this pointer
94 // FIXME: Should really be GetWindowLongPtr(), but it appears to
95 // not be defined yet.
96 This
= (Window
*) GetWindowLong (hwnd
, GWL_USERDATA
);
98 return This
->WindowProc (uMsg
, wParam
, lParam
);
101 bool Window::Create (Window
* parent
, DWORD Style
)
103 // First register the window class, if we haven't already
104 if (RegisterWindowClass () == false)
106 // Registration failed
110 // Set up the reflection info, so that the Windows window can find us.
111 ReflectionInfo
.This
= this;
112 ReflectionInfo
.FirstCall
= true;
116 // Create the window instance
117 WindowHandle
= CreateWindow ("MainWindowClass", //MAKEINTATOM(WindowClassAtom), // window class atom (name)
118 "Hello", // no title-bar string yet
121 // Default positions and size
123 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
126 NULL
? (HWND
) NULL
: parent
->GetHWND (),
129 // The application instance
130 GetInstance (), (LPVOID
) NULL
);
132 if (WindowHandle
== NULL
)
141 bool Window::RegisterWindowClass ()
143 if (WindowClassAtom
== 0)
145 // We're not registered yet
149 wc
.cbSize
= sizeof (wc
);
150 // Some sensible style defaults
151 wc
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
;
152 // Our default window procedure. This replaces itself
153 // on the first call with the simpler Window::WindowProcReflector().
154 wc
.lpfnWndProc
= Window::FirstWindowProcReflector
;
157 // One pointer to REFLECTION_INFO in the extra window instance bytes
160 wc
.hInstance
= GetInstance ();
161 // Use a bunch of system defaults for the GUI elements
165 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+ 1);
167 wc
.lpszMenuName
= NULL
;
168 // We'll get a little crazy here with the class name
169 wc
.lpszClassName
= "MainWindowClass";
171 // All set, try to register
172 WindowClassAtom
= RegisterClassEx (&wc
);
174 if (WindowClassAtom
== 0)
181 // We're registered, or already were before the call,
182 // return success in either case.
187 Window::Show (int State
)
189 ::ShowWindow (WindowHandle
, State
);
193 Window::CenterWindow ()
195 RECT WindowRect
, ParentRect
;
196 int WindowWidth
, WindowHeight
;
199 // Get the window rectangle
200 GetWindowRect (GetHWND (), &WindowRect
);
202 if (GetParent () == NULL
)
204 // Center on desktop window
205 GetWindowRect (GetDesktopWindow (), &ParentRect
);
209 // Center on client area of parent
210 GetClientRect (GetParent ()->GetHWND (), &ParentRect
);
213 WindowWidth
= WindowRect
.right
- WindowRect
.left
;
214 WindowHeight
= WindowRect
.bottom
- WindowRect
.top
;
216 // Find center of area we're centering on
217 p
.x
= (ParentRect
.right
- ParentRect
.left
) / 2;
218 p
.y
= (ParentRect
.bottom
- ParentRect
.top
) / 2;
220 // Convert that to screen coords
221 if (GetParent () == NULL
)
223 ClientToScreen (GetDesktopWindow (), &p
);
227 ClientToScreen (GetParent ()->GetHWND (), &p
);
230 // Calculate new top left corner for window
231 p
.x
-= WindowWidth
/ 2;
232 p
.y
-= WindowHeight
/ 2;
234 // And finally move the window
235 MoveWindow (GetHWND (), p
.x
, p
.y
, WindowWidth
, WindowHeight
, TRUE
);
238 LRESULT
Window::WindowProc (UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
243 return DefWindowProc (WindowHandle
, uMsg
, wParam
, lParam
);
249 bool Window::MessageLoop ()
254 while (GetMessage (&msg
, NULL
, 0, 0) != 0
255 && GetMessage (&msg
, (HWND
) NULL
, 0, 0) != -1)
257 if (!IsWindow (WindowHandle
) || !IsDialogMessage (WindowHandle
, &msg
))
259 TranslateMessage (&msg
);
260 DispatchMessage (&msg
);
268 Window::PostMessage (UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
270 ::PostMessage (GetHWND (), uMsg
, wParam
, lParam
);
273 UINT
Window::IsButtonChecked (int nIDButton
) const
275 return::IsDlgButtonChecked (GetHWND (), nIDButton
);
279 Window::SetDlgItemFont (int id
, const TCHAR
* fontname
, int Pointsize
,
280 int Weight
, bool Italic
, bool Underline
,
284 ctrl
= GetDlgItem (id
);
287 // Couldn't get that ID
291 // We need the DC for the point size calculation.
292 HDC hdc
= GetDC (ctrl
);
294 // Create the font. We have to keep it around until the dialog item
295 // goes away - basically until we're destroyed.
298 CreateFont (-MulDiv (Pointsize
, GetDeviceCaps (hdc
, LOGPIXELSY
), 72), 0,
299 0, 0, Weight
, Italic
? TRUE
: FALSE
,
300 Underline
? TRUE
: FALSE
, Strikeout
? TRUE
: FALSE
,
301 ANSI_CHARSET
, OUT_TT_PRECIS
, CLIP_DEFAULT_PRECIS
,
302 PROOF_QUALITY
, DEFAULT_PITCH
| FF_DONTCARE
, fontname
);
305 // Font creation failed
309 // Set the new fint, and redraw any text which was already in the item.
310 SendMessage (ctrl
, WM_SETFONT
, (WPARAM
) hfnt
, TRUE
);
312 // Save it for later.
313 Fonts
[FontCounter
] = hfnt
;