2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code 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 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code 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.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
26 ===========================================================================
28 #include "../../idlib/precompiled.h"
31 #include "ToggleListView.h"
33 #define TOGGLELIST_ITEMHEIGHT 22
37 IMPLEMENT_DYNCREATE(ToggleListView, CListView)
39 BEGIN_MESSAGE_MAP(ToggleListView, CListView)
42 ON_WM_MEASUREITEM_REFLECT()
43 ON_NOTIFY_REFLECT(NM_CLICK, OnNMClick)
48 * Protected constructor used by dynamic creation.
50 ToggleListView::ToggleListView() {
59 ToggleListView::~ToggleListView() {
64 * Sets the tree icons to dispay for each of the three states. Sets the
65 * icons to display for each of the three states. The values passed in
66 * are the resource name that can be generated using MAKEINTRESOUCE. If
67 * the value passed in is NULL then an icon will not be drawn for that
69 * @param disabled The icon to draw when the state is TOGGLE_STATE_DISABLED.
70 * @param on The icon to draw when the state is TOGGLE_STATE_ON.
71 * @param off The icon to draw when the state is TOGGLE_STATE_OFF.
73 void ToggleListView::SetToggleIcons(LPCSTR disabled, LPCSTR on, LPCSTR off) {
75 onIcon = (HICON)LoadImage ( AfxGetInstanceHandle(), on, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR|LR_LOADMAP3DCOLORS );
81 offIcon = (HICON)LoadImage ( AfxGetInstanceHandle(), off, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR|LR_LOADMAP3DCOLORS );
87 disabledIcon = (HICON)LoadImage ( AfxGetInstanceHandle(), disabled, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR|LR_LOADMAP3DCOLORS );
94 * Sets the state of an item in the list.
95 * @param index Index of the item whose state should be changed.
96 * @param toggleState The state to set
97 * @param notify Determines if the notification method OnStateChanged should
98 * be called. OnStateChanged will also not be called if the state has not changed.
100 void ToggleListView::SetToggleState(int index, int toggleState, bool notify) {
101 CListCtrl& list = GetListCtrl();
102 assert(index >= 0 && index < list.GetItemCount());
104 int oldState = GetToggleState(index);
105 list.SetItemData(index, toggleState);
107 if(notify && oldState != toggleState)
108 OnStateChanged(index, toggleState);
112 * Gets the state of an item in the list
113 * @param index Index of the item of which to retreive the state.
115 int ToggleListView::GetToggleState(int index) {
116 CListCtrl& list = GetListCtrl();
117 assert(index >= 0 && index < list.GetItemCount());
119 DWORD data = list.GetItemData(index);
124 * Called as the window is being created and initializes icons and window styles
126 int ToggleListView::OnCreate(LPCREATESTRUCT lpCreateStruct) {
127 if (CListView::OnCreate(lpCreateStruct) == -1)
130 CListCtrl& list = GetListCtrl();
132 list.SetExtendedStyle(LVS_EX_FULLROWSELECT);
134 //Turn off the horizontal scroll bar
135 //Todo: Figure out why the damn scroll bar pops up
136 list.ModifyStyle(WS_HSCROLL, 0L);
139 //Insert the one column
142 list.InsertColumn(0, &col);
150 * Called when the window is being resized.
152 void ToggleListView::OnSize(UINT nType, int cx, int cy) {
153 CListView::OnSize(nType, cx, cy);
155 CListCtrl& list = GetListCtrl();
156 list.SetColumnWidth(0, cx-1);
160 * Returns the size of each item in the toggle list.
162 void ToggleListView::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) {
163 lpMeasureItemStruct->itemHeight = TOGGLELIST_ITEMHEIGHT;
167 * Toggles the state of an item when the user clicks in the window.
169 void ToggleListView::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult) {
170 CListCtrl& list = GetListCtrl();
172 DWORD dwpos = GetMessagePos();
175 info.pt.x = LOWORD(dwpos);
176 info.pt.y = HIWORD(dwpos);
178 ::MapWindowPoints(HWND_DESKTOP, pNMHDR->hwndFrom, &info.pt, 1);
180 int index = list.HitTest(&info);
182 int toggleState = GetToggleState(index);
183 if(toggleState != TOGGLE_STATE_DISABLED) {
186 list.GetItemRect(index, &rItem, LVIR_BOUNDS);
188 if ( info.pt.x < TOGGLELIST_ITEMHEIGHT ) {
189 if(toggleState == TOGGLE_STATE_ON) {
190 SetToggleState(index, TOGGLE_STATE_OFF, true);
192 SetToggleState(index, TOGGLE_STATE_ON, true);
201 * Sets some window styles before the window is created.
203 BOOL ToggleListView::PreCreateWindow(CREATESTRUCT& cs) {
204 //Set the required style for the toggle view
205 cs.style &= ~LVS_TYPEMASK;
206 cs.style |= LVS_REPORT | LVS_OWNERDRAWFIXED | LVS_NOCOLUMNHEADER | LVS_SHOWSELALWAYS;
208 return CListView::PreCreateWindow(cs);
212 * Responsible for drawing each list item.
214 void ToggleListView::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {
216 CListCtrl& ListCtrl=GetListCtrl();
217 int nItem = lpDrawItemStruct->itemID;
221 _TCHAR szBuff[MAX_PATH];
223 memset(&lvi, 0, sizeof(LV_ITEM));
224 lvi.mask = LVIF_TEXT;
226 lvi.pszText = szBuff;
227 lvi.cchTextMax = sizeof(szBuff);
228 ListCtrl.GetItem(&lvi);
233 CopyRect ( &rDraw, &lpDrawItemStruct->rcItem );
234 rDraw.right = rDraw.left + TOGGLELIST_ITEMHEIGHT;
238 FrameRect ( lpDrawItemStruct->hDC, &rDraw, (HBRUSH)GetStockObject ( BLACK_BRUSH ) );
241 FillRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_3DFACE ) );
243 Draw3dRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_3DHILIGHT ), GetSysColorBrush ( COLOR_3DSHADOW ) );
245 InflateRect ( &rDraw, -3, -3 );
246 Draw3dRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_3DSHADOW ), GetSysColorBrush ( COLOR_3DHILIGHT ) );
248 switch(GetToggleState(lvi.iItem)) {
249 case TOGGLE_STATE_DISABLED:
251 DrawIconEx ( lpDrawItemStruct->hDC, rDraw.left, rDraw.top, disabledIcon, 16, 16,0, NULL, DI_NORMAL );
254 case TOGGLE_STATE_ON:
256 DrawIconEx ( lpDrawItemStruct->hDC, rDraw.left, rDraw.top, onIcon, 16, 16,0, NULL, DI_NORMAL );
259 case TOGGLE_STATE_OFF:
261 DrawIconEx ( lpDrawItemStruct->hDC, rDraw.left, rDraw.top, offIcon, 16, 16,0, NULL, DI_NORMAL );
266 CopyRect ( &rDraw, &lpDrawItemStruct->rcItem );
267 rDraw.left += TOGGLELIST_ITEMHEIGHT;
270 if ( lpDrawItemStruct->itemState & ODS_SELECTED ) {
271 FillRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_HIGHLIGHT ) );
273 FillRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_WINDOW ) );
276 rDraw.left += TEXT_OFFSET;
278 int colorIndex = ( (lpDrawItemStruct->itemState & ODS_SELECTED ) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT );
279 SetTextColor ( lpDrawItemStruct->hDC, GetSysColor ( colorIndex ) );
280 DrawText ( lpDrawItemStruct->hDC, szBuff, strlen(szBuff), &rDraw, DT_LEFT|DT_VCENTER|DT_SINGLELINE );
286 * Draws a 3d rectangle using the given brushes this code was taken from the gui editor
288 void ToggleListView::Draw3dRect (HDC hDC, RECT* rect, HBRUSH topLeft, HBRUSH bottomRight) {
291 SetRect ( &rOut, rect->left, rect->top, rect->right - 1, rect->top + 1 );
292 FillRect ( hDC,&rOut, topLeft );
294 SetRect ( &rOut, rect->left, rect->top, rect->left + 1, rect->bottom );
295 FillRect( hDC,&rOut, topLeft );
297 SetRect ( &rOut, rect->right, rect->top, rect->right -1, rect->bottom );
298 FillRect( hDC,&rOut, bottomRight );
300 SetRect ( &rOut, rect->left, rect->bottom, rect->right, rect->bottom - 1 );
301 FillRect( hDC,&rOut, bottomRight );