]>
cygwin.com Git - cygwin-apps/setup.git/blob - window.cc
2 * Copyright (c) 2001, 2002, 2003 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 #include "RECTWrapper.h"
25 ATOM
Window::WindowClassAtom
= 0;
26 HINSTANCE
Window::AppInstance
= NULL
;
37 // Delete any fonts we created.
39 for (i
= 0; i
< FontCounter
; i
++)
41 DeleteObject (Fonts
[i
]);
45 // FIXME: Maybe do some reference counting and do this Unregister
46 // when there are no more of us left. Not real critical unless
47 // we're in a DLL which we're not right now.
48 //UnregisterClass(WindowClassAtom, InstanceHandle);
52 Window::FirstWindowProcReflector (HWND hwnd
, UINT uMsg
, WPARAM wParam
,
57 if(uMsg
== WM_NCCREATE
)
59 // This is the first message a window gets (so MSDN says anyway).
60 // Take this opportunity to "link" the HWND to the 'this' ptr, steering
61 // messages to the class instance's WindowProc().
62 wnd
= reinterpret_cast<Window
*>(((LPCREATESTRUCT
)lParam
)->lpCreateParams
);
64 // Set a backreference to this class instance in the HWND.
65 SetWindowLongPtr (hwnd
, GWL_USERDATA
, reinterpret_cast<LONG_PTR
>(wnd
));
67 // Set a new WindowProc now that we have the peliminaries done.
68 // We could instead simply do the contents of Window::WindowProcReflector
69 // in the 'else' clause below, but this way we eliminate an unnecessary 'if/else' on
70 // every message. Yeah, it's probably not worth the trouble.
71 SetWindowLongPtr (hwnd
, GWL_WNDPROC
, (LONG_PTR
) & Window::WindowProcReflector
);
72 // Finally, store the window handle in the class.
73 wnd
->WindowHandle
= hwnd
;
77 // Should never get here.
81 return wnd
->WindowProc (uMsg
, wParam
, lParam
);
85 Window::WindowProcReflector (HWND hwnd
, UINT uMsg
, WPARAM wParam
,
90 // Get our this pointer
91 This
= reinterpret_cast<Window
*>(GetWindowLongPtr (hwnd
, GWL_USERDATA
));
93 return This
->WindowProc (uMsg
, wParam
, lParam
);
97 Window::Create (Window
* parent
, DWORD Style
)
99 // First register the window class, if we haven't already
100 if (registerWindowClass () == false)
102 // Registration failed
106 // Save our parent, we'll probably need it eventually.
109 // Create the window instance
110 WindowHandle
= CreateWindowEx (
113 "MainWindowClass", //MAKEINTATOM(WindowClassAtom), // window class atom (name)
114 "Hello", // no title-bar string yet
117 // Default positions and size
119 CW_USEDEFAULT
, CW_USEDEFAULT
, CW_USEDEFAULT
,
122 NULL
? (HWND
) NULL
: parent
->GetHWND (),
125 // The application instance
127 // The this ptr, which we'll use to set up the WindowProc reflection.
130 if (WindowHandle
== NULL
)
140 Window::registerWindowClass ()
142 if (WindowClassAtom
== 0)
144 // We're not registered yet
148 wc
.cbSize
= sizeof (wc
);
149 // Some sensible style defaults
150 wc
.style
= CS_DBLCLKS
| CS_HREDRAW
| CS_VREDRAW
;
151 // Our default window procedure. This replaces itself
152 // on the first call with the simpler Window::WindowProcReflector().
153 wc
.lpfnWndProc
= Window::FirstWindowProcReflector
;
156 // One pointer to REFLECTION_INFO in the extra window instance bytes
159 wc
.hInstance
= GetInstance ();
160 // Use a bunch of system defaults for the GUI elements
164 wc
.hbrBackground
= (HBRUSH
) (COLOR_BACKGROUND
+ 1);
166 wc
.lpszMenuName
= NULL
;
167 // We'll get a little crazy here with the class name
168 wc
.lpszClassName
= "MainWindowClass";
170 // All set, try to register
171 WindowClassAtom
= RegisterClassEx (&wc
);
173 if (WindowClassAtom
== 0)
180 // We're registered, or already were before the call,
181 // return success in either case.
186 Window::Show (int State
)
188 ::ShowWindow (WindowHandle
, State
);
192 Window::GetWindowRect() const
195 ::GetWindowRect(WindowHandle
, &retval
);
200 Window::GetClientRect() const
203 ::GetClientRect(WindowHandle
, &retval
);
208 Window::MoveWindow(long x
, long y
, long w
, long h
, bool Repaint
)
210 return ::MoveWindow (WindowHandle
, x
, y
, w
, h
, Repaint
);
214 Window::MoveWindow(const RECTWrapper
&r
, bool Repaint
)
216 return ::MoveWindow (WindowHandle
, r
.left
, r
.top
, r
.width(), r
.height(), Repaint
);
220 Window::CenterWindow ()
222 RECT WindowRect
, ParentRect
;
223 int WindowWidth
, WindowHeight
;
226 // Get the window rectangle
227 WindowRect
= GetWindowRect ();
229 if (GetParent () == NULL
)
231 // Center on desktop window
232 ::GetWindowRect (GetDesktopWindow (), &ParentRect
);
236 // Center on client area of parent
237 ::GetClientRect (GetParent ()->GetHWND (), &ParentRect
);
240 WindowWidth
= WindowRect
.right
- WindowRect
.left
;
241 WindowHeight
= WindowRect
.bottom
- WindowRect
.top
;
243 // Find center of area we're centering on
244 p
.x
= (ParentRect
.right
- ParentRect
.left
) / 2;
245 p
.y
= (ParentRect
.bottom
- ParentRect
.top
) / 2;
247 // Convert that to screen coords
248 if (GetParent () == NULL
)
250 ClientToScreen (GetDesktopWindow (), &p
);
254 ClientToScreen (GetParent ()->GetHWND (), &p
);
257 // Calculate new top left corner for window
258 p
.x
-= WindowWidth
/ 2;
259 p
.y
-= WindowHeight
/ 2;
261 // And finally move the window
262 MoveWindow (p
.x
, p
.y
, WindowWidth
, WindowHeight
);
266 Window::WindowProc (UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
268 return DefWindowProc (WindowHandle
, uMsg
, wParam
, lParam
);
272 Window::MessageLoop ()
277 while (GetMessage (&msg
, NULL
, 0, 0) != 0
278 && GetMessage (&msg
, (HWND
) NULL
, 0, 0) != -1)
280 if (!IsWindow (WindowHandle
) || !IsDialogMessage (WindowHandle
, &msg
))
282 TranslateMessage (&msg
);
283 DispatchMessage (&msg
);
291 Window::PostMessage (UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
293 ::PostMessage (GetHWND (), uMsg
, wParam
, lParam
);
297 Window::IsButtonChecked (int nIDButton
) const
299 return ::IsDlgButtonChecked (GetHWND (), nIDButton
);
303 Window::SetDlgItemFont (int id
, const TCHAR
* fontname
, int Pointsize
,
304 int Weight
, bool Italic
, bool Underline
,
309 ctrl
= GetDlgItem (id
);
312 // Couldn't get that ID
316 // We need the DC for the point size calculation.
317 HDC hdc
= GetDC (ctrl
);
319 // Create the font. We have to keep it around until the dialog item
320 // goes away - basically until we're destroyed.
323 CreateFont (-MulDiv (Pointsize
, GetDeviceCaps (hdc
, LOGPIXELSY
), 72), 0,
324 0, 0, Weight
, Italic
? TRUE
: FALSE
,
325 Underline
? TRUE
: FALSE
, Strikeout
? TRUE
: FALSE
,
326 ANSI_CHARSET
, OUT_TT_PRECIS
, CLIP_DEFAULT_PRECIS
,
327 PROOF_QUALITY
, DEFAULT_PITCH
| FF_DONTCARE
, fontname
);
330 // Font creation failed
334 // Set the new font, and redraw any text which was already in the item.
335 SendMessage (ctrl
, WM_SETFONT
, (WPARAM
) hfnt
, TRUE
);
337 // Save it for later.
338 Fonts
[FontCounter
] = hfnt
;
345 Window::SetWindowText (const String
& s
)
347 ::SetWindowText (WindowHandle
, s
.cstr_oneuse ());
351 Window::ScreenToClient(const RECT
&r
) const
358 ::ScreenToClient(GetHWND(), &tl
);
361 ::ScreenToClient(GetHWND(), &br
);
This page took 0.052424 seconds and 5 git commands to generate.