File: NstWindowGeneric.cpp

package info (click to toggle)
nestopia 1.52.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,140 kB
  • sloc: cpp: 127,444; xml: 27,234; ansic: 3,635; makefile: 949; sh: 19
file content (426 lines) | stat: -rw-r--r-- 8,927 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Nestopia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
////////////////////////////////////////////////////////////////////////////////////////

#include "NstString.hpp"
#include "NstWindowGeneric.hpp"
#include <WindowsX.h>

namespace Nestopia
{
	namespace Window
	{
		Rect Generic::GetPlacement() const
		{
			WINDOWPLACEMENT placement;

			placement.length = sizeof(placement);

			if (::GetWindowPlacement( hWnd, &placement ))
				return placement.rcNormalPosition;

			return 0;
		}

		void Generic::SetPlacement(const Rect& rect) const
		{
			WINDOWPLACEMENT placement;

			placement.length = sizeof(placement);

			if (::GetWindowPlacement( hWnd, &placement ))
			{
				placement.length = sizeof(placement);
				placement.rcNormalPosition = rect;
				::SetWindowPlacement( hWnd, &placement );
			}
		}

		Point Generic::SizeProxy::Coordinates() const
		{
			return Rect::Window(hWnd).Size();
		}

		void Generic::SizeProxy::Set(const Point& p) const
		{
			::SetWindowPos
			(
				hWnd,
				NULL,
				0,
				0,
				p.x,
				p.y,
				SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE
			);
		}

		void Generic::SizeProxy::operator = (const Point& p) const
		{
			Set( p );
		}

		void Generic::SizeProxy::operator += (const Point& p) const
		{
			Set( Coordinates() + p );
		}

		void Generic::SizeProxy::operator -= (const Point& p) const
		{
			Set( Coordinates() - p );
		}

		Point Generic::PositionProxy::Coordinates() const
		{
			return Rect::Window(hWnd).Position();
		}

		void Generic::PositionProxy::Set(Point p) const
		{
			if (HWND const hParent = ::GetParent( hWnd ))
				p.ClientTransform( hParent );

			::SetWindowPos
			(
				hWnd,
				NULL,
				p.x,
				p.y,
				0,
				0,
				SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE
			);
		}

		void Generic::PositionProxy::operator = (const Point& p) const
		{
			Set( p );
		}

		void Generic::PositionProxy::operator += (const Point& p) const
		{
			Set( Coordinates() + p );
		}

		void Generic::PositionProxy::operator -= (const Point& p) const
		{
			Set( Coordinates() - p );
		}

		void Generic::PositionProxy::BringBehind(HWND hOther) const
		{
			::SetWindowPos( hWnd, hOther, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE );
		}

		Generic::StyleProxy::operator long () const
		{
			return ::GetWindowLong( hWnd, ex ? GWL_EXSTYLE : GWL_STYLE ) & flags;
		}

		void Generic::StyleProxy::operator = (bool on) const
		{
			const long style = ::GetWindowLong( hWnd, ex ? GWL_EXSTYLE : GWL_STYLE );

			if ((style & flags) != (on ? flags : 0))
			{
				::SetWindowLong( hWnd, ex ? GWL_EXSTYLE : GWL_STYLE, (style & ~flags) | (on ? flags : 0) );
				::SetWindowPos( hWnd, NULL, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED );
			}
		}

		void Generic::Post(uint msg) const
		{
			Post( msg, 0, 0 );
		}

		LONG_PTR Generic::Send(uint msg) const
		{
			return Send( msg, 0, 0 );
		}

		void Generic::SendCommand(uint id) const
		{
			Send( WM_COMMAND, id, 0 );
		}

		void Generic::PostCommand(uint id) const
		{
			Post( WM_COMMAND, id, 0 );
		}

		bool Generic::Enabled() const
		{
			return ::IsWindowEnabled( hWnd );
		}

		bool Generic::Active() const
		{
			return hWnd && hWnd == ::GetActiveWindow();
		}

		bool Generic::Focused() const
		{
			return hWnd && hWnd == ::GetFocus();
		}

		bool Generic::Visible() const
		{
			return ::IsWindowVisible( hWnd );
		}

		bool Generic::Enable(bool enable) const
		{
			bool focused = (!enable && ::GetFocus() == hWnd);
			bool prevDisabled = ::EnableWindow( hWnd, enable );

			if (!prevDisabled && focused)
			{
				if (HWND parent = ::GetParent( hWnd ))
					::SetFocus( parent );
			}

			return prevDisabled;
		}

		void Generic::Show(bool show) const
		{
			::ShowWindow( hWnd, show ? SW_SHOW : SW_HIDE );
		}

		void Generic::Maximize() const
		{
			if (SameThread())
				Send( WM_SYSCOMMAND, SC_MAXIMIZE, 0 );
			else
				Post( WM_SYSCOMMAND, SC_MAXIMIZE, 0 );
		}

		void Generic::Minimize() const
		{
			if (SameThread())
				Send( WM_SYSCOMMAND, SC_MINIMIZE, 0 );
			else
				Post( WM_SYSCOMMAND, SC_MINIMIZE, 0 );
		}

		void Generic::Restore() const
		{
			if (SameThread())
				Send( WM_SYSCOMMAND, SC_RESTORE, 0 );
			else
				Post( WM_SYSCOMMAND, SC_RESTORE, 0 );
		}

		bool Generic::Maximized() const
		{
			return ::IsZoomed( hWnd );
		}

		bool Generic::Minimized() const
		{
			return ::IsIconic( hWnd );
		}

		bool Generic::Restored() const
		{
			return !::IsZoomed( hWnd ) && !::IsIconic( hWnd );
		}

		bool Generic::SameThread() const
		{
			return ::GetCurrentThreadId() == ::GetWindowThreadProcessId( hWnd, NULL );
		}

		void Generic::MakeTopMost(bool topMost) const
		{
			::SetWindowPos
			(
				hWnd,
				topMost ? HWND_TOPMOST : HWND_NOTOPMOST,
				0,
				0,
				0,
				0,
				SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE
			);
		}

		void Generic::MakeWindowed(long style,long exStyle) const
		{
			::SetWindowLongPtr( hWnd, GWL_STYLE, style );
			::SetWindowLongPtr( hWnd, GWL_EXSTYLE, exStyle );
			::SetWindowPos( hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
		}

		void Generic::MakeFullscreen() const
		{
			::SetWindowLongPtr( hWnd, GWL_STYLE, WS_POPUP|WS_VISIBLE );
			::SetWindowLongPtr( hWnd, GWL_EXSTYLE, 0 );

			::SetWindowPos
			(
				hWnd,
				NULL,
				0,
				0,
				::GetSystemMetrics( SM_CXSCREEN ),
				::GetSystemMetrics( SM_CYSCREEN ),
				SWP_NOZORDER|SWP_FRAMECHANGED
			);
		}

		bool Generic::Activate() const
		{
			if (::IsWindowEnabled( hWnd ))
			{
				if (::IsIconic( hWnd ))
					Restore();
				else
					::SetForegroundWindow( hWnd );

				return true;
			}

			return false;
		}

		void Generic::Redraw(bool redraw) const
		{
			if (redraw)
				::InvalidateRect( hWnd, NULL, true );
			else
				::ValidateRect( hWnd, NULL );
		}

		Generic Generic::Parent() const
		{
			return ::GetParent( hWnd );
		}

		void Generic::Close() const
		{
			if (hWnd)
			{
				if (SameThread())
					Send( WM_SYSCOMMAND, SC_CLOSE, 0 );
				else
					Post( WM_SYSCOMMAND, SC_CLOSE, 0 );
			}
		}

		bool Generic::Destroy()
		{
			NST_VERIFY( !hWnd || ::IsWindow( hWnd ) );

			if (hWnd)
			{
				bool result = ::DestroyWindow( hWnd );
				hWnd = NULL;
				return result;
			}

			return false;
		}

		Generic Generic::Find(wcstring className)
		{
			return className ? ::FindWindow( className, NULL ) : NULL;
		}

		int Generic::Stream::GetTextLength(const char*) const
		{
			return ::GetWindowTextLengthA( hWnd );
		}

		int Generic::Stream::GetTextLength(const wchar_t*) const
		{
			return ::GetWindowTextLengthW( hWnd );
		}

		int Generic::Stream::GetText(char* string,uint maxLength) const
		{
			return ::GetWindowTextA( hWnd, string, maxLength + 1 );
		}

		int Generic::Stream::GetText(wchar_t* string,uint maxLength) const
		{
			return ::GetWindowTextW( hWnd, string, maxLength + 1 );
		}

		void Generic::Stream::operator << (const char* string) const
		{
			NST_ASSERT( string );
			::SetWindowTextA( hWnd, string );
		}

		void Generic::Stream::operator << (const wchar_t* string) const
		{
			NST_ASSERT( string );
			::SetWindowTextW( hWnd, string );
		}

		void Generic::Stream::operator << (int value) const
		{
			operator << (ValueString(value).Ptr());
		}

		void Generic::Stream::operator << (uint value) const
		{
			operator << (ValueString(value).Ptr());
		}

		uint Generic::Stream::operator >> (ulong& value) const
		{
			String::Stack<16,wchar_t> string;
			string.ShrinkTo( ::GetWindowText( hWnd, string.Ptr(), 16+1 ) );
			string >> value;
			return string.Length();
		}

		uint Generic::Stream::operator >> (long& value) const
		{
			String::Stack<16,wchar_t> string;
			string.ShrinkTo( ::GetWindowText( hWnd, string.Ptr(), 16+1 ) );
			string >> value;
			return string.Length();
		}

		void Generic::Stream::Clear() const
		{
			::SetWindowText( hWnd, L"" );
		}

		Generic::LockDraw::LockDraw(HWND h)
		: hWnd(h)
		{
			NST_ASSERT( hWnd );
			SetWindowRedraw( hWnd, false );
		}

		Generic::LockDraw::~LockDraw()
		{
			SetWindowRedraw( hWnd, true );
			::InvalidateRect( hWnd, NULL, false );
		}
	}
}