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_LISTBOX_H
26 #define NST_CTRL_LISTBOX_H
27 
28 #pragma once
29 
30 #include "NstCtrlStandard.hpp"
31 
32 namespace Nestopia
33 {
34 	namespace Window
35 	{
36 		namespace Control
37 		{
38 			class ListBox : public Generic
39 			{
40 				class Item : public ImplicitBool<Item>
41 				{
42 					class DataProxy
43 					{
44 						const Item& item;
45 
46 					public:
47 
DataProxy(const Item & i)48 						DataProxy(const Item& i)
49 						: item(i) {}
50 
51 						operator ULONG_PTR () const;
52 						void operator = (ULONG_PTR) const;
53 					};
54 
55 					class TextProxy
56 					{
57 						const Item& item;
58 
59 					public:
60 
TextProxy(const Item & i)61 						TextProxy(const Item& i)
62 						: item(i) {}
63 
64 						void operator << (wcstring) const;
65 
66 						template<typename T>
67 						uint operator >> (T&) const;
68 					};
69 
70 					const Window::Generic control;
71 					const int index;
72 
73 				public:
74 
Item(Window::Generic w,uint i)75 					Item(Window::Generic w,uint i)
76 					: control(w), index(i) {}
77 
78 					void Remove() const;
79 					void Select() const;
80 
GetIndex() const81 					int GetIndex() const
82 					{
83 						return index;
84 					}
85 
operator !() const86 					bool operator ! () const
87 					{
88 						return index < 0;
89 					}
90 
Data() const91 					DataProxy Data() const
92 					{
93 						return *this;
94 					}
95 
Text() const96 					TextProxy Text() const
97 					{
98 						return *this;
99 					}
100 				};
101 
102 			public:
103 
ListBox(HWND hWnd=NULL)104 				ListBox(HWND hWnd=NULL)
105 				: Generic( hWnd ) {}
106 
ListBox(HWND hWnd,uint id)107 				ListBox(HWND hWnd,uint id)
108 				: Generic( hWnd, id ) {}
109 
110 				Item Add(wcstring) const;
111 				Item Insert(uint,wcstring) const;
112 				Item Selection() const;
113 				bool AnySelection() const;
114 				void Reserve(uint) const;
115 				uint Size() const;
116 				void Clear() const;
117 
118 				template<typename T>
Add(T * list,uint count) const119 				void Add(T* list,uint count) const
120 				{
121 					Reserve( count );
122 
123 					for (uint i=0; i < count; ++i)
124 						Add( list[i] );
125 				}
126 
operator [](uint i) const127 				Item operator [] (uint i) const
128 				{
129 					return Item( control, i );
130 				}
131 
132 				class HScrollBar
133 				{
134 					long width;
135 					HWND const hWnd;
136 					HDC const hDC;
137 
138 				public:
139 
140 					HScrollBar(HWND);
141 					~HScrollBar();
142 
143 					void Update(wcstring,uint);
144 				};
145 			};
146 
147 			template<typename T>
operator >>(T & string) const148 			uint ListBox::Item::TextProxy::operator >> (T& string) const
149 			{
150 				NST_VERIFY( item.control );
151 
152 				const int size = item.control.Send( LB_GETTEXTLEN, item.index, 0 );
153 
154 				if (size > 0)
155 				{
156 					string.Resize( size );
157 
158 					if (item.control.Send( LB_GETTEXT, item.index, static_cast<wchar_t*>(string.Ptr()) ) > 0)
159 						return string.Validate();
160 				}
161 
162 				string.Clear();
163 				return 0;
164 			}
165 		}
166 	}
167 }
168 
169 #endif
170