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_CTRL_STANDARD_H
26 #define NST_CTRL_STANDARD_H
27 
28 #pragma once
29 
30 #include <map>
31 #include "NstWindowGeneric.hpp"
32 
33 namespace Nestopia
34 {
35 	namespace Window
36 	{
37 		namespace Control
38 		{
39 			class Generic
40 			{
41 			public:
42 
43 				bool FixedFont() const;
44 				Point GetMaxTextSize() const;
45 
46 			protected:
47 
48 				Window::Generic control;
49 
50 			public:
51 
Generic(HWND hWnd=NULL)52 				Generic(HWND hWnd=NULL)
53 				: control(hWnd) {}
54 
Generic(HWND hWnd,uint id)55 				Generic(HWND hWnd,uint id)
56 				: control( ::GetDlgItem( hWnd, id ) )
57 				{
58 					NST_VERIFY( control );
59 				}
60 
Enable(bool state=true) const61 				void Enable(bool state=true) const
62 				{
63 					control.Enable( state );
64 				}
65 
Disable() const66 				void Disable() const
67 				{
68 					Enable( false );
69 				}
70 
Enabled() const71 				bool Enabled() const
72 				{
73 					return control.Enabled();
74 				}
75 
Redraw() const76 				void Redraw() const
77 				{
78 					control.Redraw();
79 				}
80 
GetWindow() const81 				Window::Generic GetWindow() const
82 				{
83 					return control;
84 				}
85 
GetHandle() const86 				HWND GetHandle() const
87 				{
88 					return control;
89 				}
90 
Text() const91 				Window::Generic::Stream Text() const
92 				{
93 					return control;
94 				}
95 			};
96 
97 			class NotificationHandler
98 			{
99 			public:
100 
101 				template<typename T> struct Entry
102 				{
103 					uint msg;
104 					void (T::*function)(const NMHDR&);
105 				};
106 
107 			private:
108 
109 				typedef Object::Delegate<void,const NMHDR&> Callback;
110 				typedef std::map<uint,Callback> Items;
111 
112 				const uint control;
113 				Items items;
114 				MsgHandler& msgHandler;
115 
116 				void Initialize();
117 				void OnNotify(Param&);
118 
119 				template<typename T>
120 				void Add(T*,const Entry<T>*,uint);
121 
operator ()(uint id)122 				Callback& operator () (uint id)
123 				{
124 					return items[id];
125 				}
126 
127 			public:
128 
129 				NotificationHandler(uint,MsgHandler&);
130 
131 				template<typename T,uint N>
132 				NotificationHandler(uint,MsgHandler&,T*,const Entry<T>(&)[N]);
133 
134 				~NotificationHandler();
135 
136 				template<typename T,uint N>
Add(T * data,const Entry<T> (& entries)[N])137 				void Add(T* data,const Entry<T>(&entries)[N])
138 				{
139 					Add( data, entries, N );
140 				}
141 			};
142 
143 			template<typename T,uint N>
NotificationHandler(const uint id,MsgHandler & m,T * const data,const Entry<T> (& entries)[N])144 			NotificationHandler::NotificationHandler
145 			(
146 				const uint id,
147 				MsgHandler& m,
148 				T* const data,
149 				const Entry<T>(&entries)[N]
150 			)
151 			: control(id), msgHandler(m)
152 			{
153 				Initialize();
154 				Add( data, entries, N );
155 			}
156 
157 			template<typename T>
Add(T * const object,const Entry<T> * it,const uint count)158 			void NotificationHandler::Add(T* const object,const Entry<T>* it,const uint count)
159 			{
160 				for (const Entry<T>* const end = it + count; it != end; ++it)
161 					(*this)( it->msg ).Set( object, it->function );
162 			}
163 		}
164 	}
165 }
166 
167 #endif
168