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 #include "NstWindowCustom.hpp"
26 #include "NstCtrlListBox.hpp"
27 #include <WindowsX.h>
28 
29 namespace Nestopia
30 {
31 	namespace Window
32 	{
33 		namespace Control
34 		{
35 			NST_COMPILE_ASSERT( LB_ERR < 0 && LB_ERRSPACE < 0 );
36 
operator ULONG_PTR() const37 			ListBox::Item::DataProxy::operator ULONG_PTR () const
38 			{
39 				return ListBox_GetItemData( item.control, item.index );
40 			}
41 
operator =(const ULONG_PTR data) const42 			void ListBox::Item::DataProxy::operator = (const ULONG_PTR data) const
43 			{
44 				ListBox_SetItemData( item.control, item.index, data );
45 			}
46 
operator <<(wcstring const string) const47 			void ListBox::Item::TextProxy::operator << (wcstring const string) const
48 			{
49 				NST_ASSERT( string );
50 
51 				const int selection = ListBox_GetCurSel( item.control );
52 
53 				ListBox_DeleteString( item.control, item.index );
54 				ListBox_InsertString( item.control, item.index, string );
55 
56 				if (selection != LB_ERR)
57 					ListBox_SetCurSel( item.control, selection );
58 			}
59 
Select() const60 			void ListBox::Item::Select() const
61 			{
62 				ListBox_SetCurSel( control, index );
63 			}
64 
Remove() const65 			void ListBox::Item::Remove() const
66 			{
67 				ListBox_DeleteString( control, index );
68 			}
69 
Selection() const70 			ListBox::Item ListBox::Selection() const
71 			{
72 				return Item( control, ListBox_GetCurSel( control ) );
73 			}
74 
AnySelection() const75 			bool ListBox::AnySelection() const
76 			{
77 				return ListBox_GetCurSel( control ) != LB_ERR;
78 			}
79 
Add(wcstring const text) const80 			ListBox::Item ListBox::Add(wcstring const text) const
81 			{
82 				NST_ASSERT( text );
83 				return Item( control, ListBox_AddString( control, text ) );
84 			}
85 
Insert(const uint index,wcstring const text) const86 			ListBox::Item ListBox::Insert(const uint index,wcstring const text) const
87 			{
88 				NST_ASSERT( text );
89 				return Item( control, ListBox_InsertString( control, index, text ) );
90 			}
91 
Size() const92 			uint ListBox::Size() const
93 			{
94 				const int size = ListBox_GetCount( control );
95 				return NST_MAX(size,0);
96 			}
97 
Reserve(const uint capacity) const98 			void ListBox::Reserve(const uint capacity) const
99 			{
100 				if (capacity < Size())
101 					control.Send( LB_INITSTORAGE, capacity, 0 );
102 			}
103 
Clear() const104 			void ListBox::Clear() const
105 			{
106 				ListBox_ResetContent( control );
107 			}
108 
HScrollBar(HWND h)109 			ListBox::HScrollBar::HScrollBar(HWND h)
110 			: width(0), hWnd(h), hDC(h ? ::GetDC(h) : NULL)
111 			{
112 				NST_VERIFY( hWnd );
113 			}
114 
Update(wcstring string,uint length)115 			void ListBox::HScrollBar::Update(wcstring string,uint length)
116 			{
117 				NST_ASSERT( string || !length );
118 
119 				SIZE size;
120 
121 				if (hDC && ::GetTextExtentPoint32( hDC, string, length, &size ) && width < size.cx)
122 					width = size.cx;
123 			}
124 
~HScrollBar()125 			ListBox::HScrollBar::~HScrollBar()
126 			{
127 				if (hDC)
128 				{
129 					::ReleaseDC( hWnd, hDC );
130 
131 					if (width)
132 					{
133 						RECT rect;
134 						::GetClientRect( hWnd, &rect );
135 
136 						if (width > rect.right)
137 							ListBox_SetHorizontalExtent( hWnd, width );
138 					}
139 				}
140 			}
141 		}
142 	}
143 }
144