1 ////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // Nestopia - NES/Famicom emulator written in C++
4 //
5 // Copyright (C) 2003-2008 Martin Freij
6 //
7 // This file is part of Nestopia.
8 //
9 // Nestopia is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Nestopia is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Nestopia; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 //
23 ////////////////////////////////////////////////////////////////////////////////////////
24 
25 #ifndef NST_WINDOW_GENERIC_H
26 #define NST_WINDOW_GENERIC_H
27 
28 #pragma once
29 
30 #include "NstWindowRect.hpp"
31 
32 namespace Nestopia
33 {
34 	namespace Window
35 	{
36 		class Generic
37 		{
38 		protected:
39 
40 			HWND hWnd;
41 
42 		public:
43 
Generic(HWND h=NULL)44 			Generic(HWND h=NULL)
45 			: hWnd(h) {}
46 
operator =(HWND h)47 			Generic& operator = (HWND h)
48 			{
49 				hWnd = h;
50 				return *this;
51 			}
52 
operator HWND() const53 			operator HWND () const
54 			{
55 				return hWnd;
56 			}
57 
58 		private:
59 
60 			class SizeProxy
61 			{
62 				HWND const hWnd;
63 
64 				void Set(const Point&) const;
65 
66 			public:
67 
SizeProxy(HWND h)68 				SizeProxy(HWND h)
69 				: hWnd(h) {}
70 
71 				void operator  = (const Point&) const;
72 				void operator += (const Point&) const;
73 				void operator -= (const Point&) const;
74 
75 				Point Coordinates() const;
76 
operator Point() const77 				operator Point () const
78 				{
79 					return Coordinates();
80 				}
81 			};
82 
83 			class PositionProxy
84 			{
85 				HWND const hWnd;
86 
87 				void Set(Point) const;
88 
89 			public:
90 
PositionProxy(HWND h)91 				PositionProxy(HWND h)
92 				: hWnd(h) {}
93 
94 				void operator  = (const Point&) const;
95 				void operator += (const Point&) const;
96 				void operator -= (const Point&) const;
97 
98 				void BringBehind(HWND) const;
99 
100 				Point Coordinates() const;
101 
operator Point() const102 				operator Point () const
103 				{
104 					return Coordinates();
105 				}
106 			};
107 
108 			class StyleProxy
109 			{
110 				HWND const hWnd;
111 				const long flags;
112 				const bool ex;
113 
114 			public:
115 
StyleProxy(HWND h,bool e,long f)116 				StyleProxy(HWND h,bool e,long f)
117 				: hWnd(h), flags(f), ex(e) {}
118 
119 				void operator = (bool) const;
120 				operator long () const;
121 			};
122 
123 		public:
124 
Coordinates() const125 			Rect::Window Coordinates() const
126 			{
127 				return hWnd;
128 			}
129 
ClientCoordinates() const130 			Point::Client ClientCoordinates() const
131 			{
132 				return hWnd;
133 			}
134 
PictureCoordinates() const135 			Point::Picture PictureCoordinates() const
136 			{
137 				return hWnd;
138 			}
139 
NonClientCoordinates() const140 			Point::NonClient NonClientCoordinates() const
141 			{
142 				return hWnd;
143 			}
144 
Size() const145 			SizeProxy Size() const
146 			{
147 				return hWnd;
148 			}
149 
Position() const150 			PositionProxy Position() const
151 			{
152 				return hWnd;
153 			}
154 
Style(long flags) const155 			StyleProxy Style(long flags) const
156 			{
157 				return StyleProxy( hWnd, false, flags );
158 			}
159 
ExStyle(long flags) const160 			StyleProxy ExStyle(long flags) const
161 			{
162 				return StyleProxy( hWnd, true, flags );
163 			}
164 
165 			void Maximize() const;
166 			void Minimize() const;
167 			void Restore() const;
168 
169 			bool Enabled() const;
170 			bool Active() const;
171 			bool Focused() const;
172 			bool Maximized() const;
173 			bool Minimized() const;
174 			bool Restored() const;
175 			bool Visible() const;
176 
177 			Rect GetPlacement() const;
178 			void SetPlacement(const Rect&) const;
179 
180 			bool SameThread() const;
181 
182 			bool Enable(bool=true) const;
183 			void Show(bool=true) const;
184 			bool Activate() const;
185 			void Redraw(bool=true) const;
186 			void Close() const;
187 			bool Destroy();
188 
189 			void MakeTopMost(bool=true) const;
190 			void MakeWindowed(long,long) const;
191 			void MakeFullscreen() const;
192 
193 			Generic Parent() const;
194 
195 			LONG_PTR Send(uint) const;
196 			void     Post(uint) const;
197 
198 			void SendCommand(uint) const;
199 			void PostCommand(uint) const;
200 
201 			template<typename T,typename U>
Send(uint msg,T t,U u) const202 			LONG_PTR Send(uint msg,T t,U u) const
203 			{
204 				NST_COMPILE_ASSERT( sizeof(t) <= sizeof(WPARAM) && sizeof(u) <= sizeof(LPARAM) );
205 				return ::SendMessage( hWnd, msg, (WPARAM) t, (LPARAM) u );
206 			}
207 
208 			template<typename T,typename U>
Post(uint msg,T t,U u) const209 			void Post(uint msg,T t,U u) const
210 			{
211 				NST_COMPILE_ASSERT( sizeof(T) <= sizeof(WPARAM) && sizeof(U) <= sizeof(LPARAM) );
212 				::PostMessage( hWnd, msg, (WPARAM) t, (LPARAM) u );
213 			}
214 
215 			static Generic Find(wcstring);
216 
217 			class Stream
218 			{
219 				HWND const hWnd;
220 
221 				int GetTextLength(const char*) const;
222 				int GetTextLength(const wchar_t*) const;
223 
224 				int GetText(char*,uint) const;
225 				int GetText(wchar_t*,uint) const;
226 
227 			public:
228 
Stream(Generic g)229 				Stream(Generic g)
230 				: hWnd(g.hWnd) {}
231 
232 				void operator << (const char*) const;
233 				void operator << (const wchar_t*) const;
234 				void operator << (int) const;
235 				void operator << (uint) const;
236 
237 				template<typename T>
238 				uint operator >> (T&) const;
239 
240 				uint operator >> (long&) const;
241 				uint operator >> (ulong&) const;
242 
operator >>(schar & i) const243 				uint operator >> ( schar&  i ) const { long  t; uint r = operator >> (t); i = schar  (t); return r; }
operator >>(uchar & i) const244 				uint operator >> ( uchar&  i ) const { ulong t; uint r = operator >> (t); i = uchar  (t); return r; }
operator >>(short & i) const245 				uint operator >> ( short&  i ) const { long  t; uint r = operator >> (t); i = short  (t); return r; }
operator >>(ushort & i) const246 				uint operator >> ( ushort& i ) const { ulong t; uint r = operator >> (t); i = ushort (t); return r; }
operator >>(int & i) const247 				uint operator >> ( int&    i ) const { long  t; uint r = operator >> (t); i = int    (t); return r; }
operator >>(uint & i) const248 				uint operator >> ( uint&   i ) const { ulong t; uint r = operator >> (t); i = uint   (t); return r; }
249 
250 				void Clear() const;
251 			};
252 
Text() const253 			Stream Text() const
254 			{
255 				return *this;
256 			}
257 
258 			class LockDraw
259 			{
260 				HWND const hWnd;
261 
262 			public:
263 
264 				LockDraw(HWND);
265 				~LockDraw();
266 			};
267 		};
268 
269 		template<typename T>
operator >>(T & string) const270 		uint Generic::Stream::operator >> (T& string) const
271 		{
272 			const int size = GetTextLength( string.Ptr() );
273 
274 			if (size > 0)
275 			{
276 				string.Resize( size );
277 
278 				if (GetText( string.Ptr(), string.Length() ) > 0)
279 					return string.Validate();
280 			}
281 
282 			string.Clear();
283 			return 0;
284 		}
285 	}
286 }
287 
288 #endif
289