]>
cygwin.com Git - cygwin-apps/setup.git/blob - window.cc
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,
24 ATOM
Window::WindowClassAtom
= 0;
25 HINSTANCE
Window::AppInstance
= NULL
;
36 // Delete any fonts we created.
38 for (i
= 0; i
< FontCounter
; i
++)
40 DeleteObject (Fonts
[i
]);
44 // FIXME: Maybe do some reference counting and do this Unregister
45 // when there are no more of us left. Not real critical unless
46 // we're in a DLL which we're not right now.
47 //UnregisterClass(WindowClassAtom, InstanceHandle);
51 Window::FirstWindowProcReflector (HWND hwnd
, UINT uMsg
, WPARAM wParam
,
56 if(uMsg
== WM_NCCREATE
)
58 // This is the first message a window gets (so MSDN says anyway).
59 // Take this opportunity to "link" the HWND to the 'this' ptr, steering
60 // messages to the class instance's WindowProc().
61 wnd
= reinterpret_cast<Window
*>(((LPCREATESTRUCT
)lParam
)->lpCreateParams
);
63 // Set a backreference to this class instance in the HWND.
64 SetWindowLongPtr (hwnd
, GWL_USERDATA
, (LONG_PTR
) wnd
);
66 // Set a new WindowProc now that we have the peliminaries done.
67 // We could instead simply do the contents of Window::WindowProcReflector
68 // in the 'else' clause below, but this way we eliminate an unnecessary 'if/else' on
69 // every message. Yeah, it's probably not worth the trouble.
70 SetWindowLongPtr (hwnd
, GWL_WNDPROC
, (LONG_PTR
) & Window::WindowProcReflector
);
74 // Should never get here.
78 return wnd
->WindowProc (uMsg
, wParam
, lParam
);
82 Window::WindowProcReflector (HWND hwnd
, UINT uMsg
, WPARAM wParam
,
87 // Get our this pointer
88 This
= (Window
*) GetWindowLongPtr (hwnd
, GWL_USERDATA
);
90 return This
->WindowProc (uMsg
, wParam
, lParam
);
93 bool Window::Create (Window
* parent
, DWORD Style
)
95 // First register the window class, if we haven't already
96 if (RegisterWindowClass () == false)
98 // Registration failed
104 // Create the window instance
105 WindowHandle
= CreateWindowEx (
108 "MainWindowClass", //MAKEINTATOM(WindowClassAtom), // window class atom (name)
109 "Hello", // no title-bar string yet
112 // Default positions and size
114 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
117 NULL
? (HWND
) NULL
: parent
->GetHWND (),
120 // The application instance
122 // The this ptr, which we'll use to set up the WindowProc reflection.
125 if (WindowHandle
== NULL
)
134 bool Window::RegisterWindowClass ()
136 if (WindowClassAtom
== 0)
138 // We're not registered yet
142 wc
.cbSize
= sizeof (wc
);
143 // Some sensible style defaults
144 wc
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
;
145 // Our default window procedure. This replaces itself
146 // on the first call with the simpler Window::WindowProcReflector().
147 wc
.lpfnWndProc
= Window::FirstWindowProcReflector
;
150 // One pointer to REFLECTION_INFO in the extra window instance bytes
153 wc
.hInstance
= GetInstance ();
154 // Use a bunch of system defaults for the GUI elements
158 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+ 1);
160 wc
.lpszMenuName
= NULL
;
161 // We'll get a little crazy here with the class name
162 wc
.lpszClassName
= "MainWindowClass";
164 // All set, try to register
165 WindowClassAtom
= RegisterClassEx (&wc
);
167 if (WindowClassAtom
== 0)
174 // We're registered, or already were before the call,
175 // return success in either case.
180 Window::Show (int State
)
182 ::ShowWindow (WindowHandle
, State
);
186 Window::GetWindowRect() const
189 ::GetWindowRect(WindowHandle
, &retval
);
194 Window::GetClientRect() const
197 ::GetClientRect(WindowHandle
, &retval
);
202 Window::MoveWindow(long x
, long y
, long w
, long h
, bool Repaint
)
204 return ::MoveWindow (WindowHandle
, x
, y
, w
, h
, Repaint
);
208 Window::CenterWindow ()
210 RECT WindowRect
, ParentRect
;
211 int WindowWidth
, WindowHeight
;
214 // Get the window rectangle
215 WindowRect
= GetWindowRect ();
217 if (GetParent () == NULL
)
219 // Center on desktop window
220 ::GetWindowRect (GetDesktopWindow (), &ParentRect
);
224 // Center on client area of parent
225 ::GetClientRect (GetParent ()->GetHWND (), &ParentRect
);
228 WindowWidth
= WindowRect
.right
- WindowRect
.left
;
229 WindowHeight
= WindowRect
.bottom
- WindowRect
.top
;
231 // Find center of area we're centering on
232 p
.x
= (ParentRect
.right
- ParentRect
.left
) / 2;
233 p
.y
= (ParentRect
.bottom
- ParentRect
.top
) / 2;
235 // Convert that to screen coords
236 if (GetParent () == NULL
)
238 ClientToScreen (GetDesktopWindow (), &p
);
242 ClientToScreen (GetParent ()->GetHWND (), &p
);
245 // Calculate new top left corner for window
246 p
.x
-= WindowWidth
/ 2;
247 p
.y
-= WindowHeight
/ 2;
249 // And finally move the window
250 MoveWindow (p
.x
, p
.y
, WindowWidth
, WindowHeight
);
253 LRESULT
Window::WindowProc (UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
258 return DefWindowProc (WindowHandle
, uMsg
, wParam
, lParam
);
264 bool Window::MessageLoop ()
269 while (GetMessage (&msg
, NULL
, 0, 0) != 0
270 && GetMessage (&msg
, (HWND
) NULL
, 0, 0) != -1)
272 if (!IsWindow (WindowHandle
) || !IsDialogMessage (WindowHandle
, &msg
))
274 TranslateMessage (&msg
);
275 DispatchMessage (&msg
);
283 Window::PostMessage (UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
285 ::PostMessage (GetHWND (), uMsg
, wParam
, lParam
);
288 UINT
Window::IsButtonChecked (int nIDButton
) const
290 return::IsDlgButtonChecked (GetHWND (), nIDButton
);
294 Window::SetDlgItemFont (int id
, const TCHAR
* fontname
, int Pointsize
,
295 int Weight
, bool Italic
, bool Underline
,
300 ctrl
= GetDlgItem (id
);
303 // Couldn't get that ID
307 // We need the DC for the point size calculation.
308 HDC hdc
= GetDC (ctrl
);
310 // Create the font. We have to keep it around until the dialog item
311 // goes away - basically until we're destroyed.
314 CreateFont (-MulDiv (Pointsize
, GetDeviceCaps (hdc
, LOGPIXELSY
), 72), 0,
315 0, 0, Weight
, Italic
? TRUE
: FALSE
,
316 Underline
? TRUE
: FALSE
, Strikeout
? TRUE
: FALSE
,
317 ANSI_CHARSET
, OUT_TT_PRECIS
, CLIP_DEFAULT_PRECIS
,
318 PROOF_QUALITY
, DEFAULT_PITCH
| FF_DONTCARE
, fontname
);
321 // Font creation failed
325 // Set the new fint, and redraw any text which was already in the item.
326 SendMessage (ctrl
, WM_SETFONT
, (WPARAM
) hfnt
, TRUE
);
328 // Save it for later.
329 Fonts
[FontCounter
] = hfnt
;
336 Window::SetWindowText (const String
& s
)
338 ::SetWindowText (WindowHandle
, s
.cstr_oneuse ());
342 Window::ScreenToClient(const RECT
&r
) const
349 ::ScreenToClient(GetHWND(), &tl
);
352 ::ScreenToClient(GetHWND(), &br
);
This page took 0.092297 seconds and 5 git commands to generate.