doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ToggleListView.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
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.
13 
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.
18 
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/>.
21 
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.
23 
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.
25 
26 ===========================================================================
27 */
28 #include "../../idlib/precompiled.h"
29 #pragma hdrstop
30 
31 #include "ToggleListView.h"
32 
33 #define TOGGLELIST_ITEMHEIGHT 22
34 #define TEXT_OFFSET 6
35 
36 
38 
39 BEGIN_MESSAGE_MAP(ToggleListView, CListView)
40  ON_WM_CREATE()
41  ON_WM_SIZE()
42  ON_WM_MEASUREITEM_REFLECT()
43  ON_NOTIFY_REFLECT(NM_CLICK, OnNMClick)
44 END_MESSAGE_MAP()
45 
46 
50 ToggleListView::ToggleListView() {
51  onIcon = NULL;
52  offIcon = NULL;
53  disabledIcon = NULL;
54 }
55 
60 }
61 
62 
73 void ToggleListView::SetToggleIcons(LPCSTR disabled, LPCSTR on, LPCSTR off) {
74  if(on) {
75  onIcon = (HICON)LoadImage ( AfxGetInstanceHandle(), on, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR|LR_LOADMAP3DCOLORS );
76  } else {
77  onIcon = NULL;
78  }
79 
80  if(off) {
81  offIcon = (HICON)LoadImage ( AfxGetInstanceHandle(), off, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR|LR_LOADMAP3DCOLORS );
82  } else {
83  offIcon = NULL;
84  }
85 
86  if(disabled) {
87  disabledIcon = (HICON)LoadImage ( AfxGetInstanceHandle(), disabled, IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR|LR_LOADMAP3DCOLORS );
88  } else {
90  }
91 }
92 
100 void ToggleListView::SetToggleState(int index, int toggleState, bool notify) {
101  CListCtrl& list = GetListCtrl();
102  assert(index >= 0 && index < list.GetItemCount());
103 
104  int oldState = GetToggleState(index);
105  list.SetItemData(index, toggleState);
106 
107  if(notify && oldState != toggleState)
108  OnStateChanged(index, toggleState);
109 }
110 
116  CListCtrl& list = GetListCtrl();
117  assert(index >= 0 && index < list.GetItemCount());
118 
119  DWORD data = list.GetItemData(index);
120  return data;
121 }
122 
126 int ToggleListView::OnCreate(LPCREATESTRUCT lpCreateStruct) {
127  if (CListView::OnCreate(lpCreateStruct) == -1)
128  return -1;
129 
130  CListCtrl& list = GetListCtrl();
131 
132  list.SetExtendedStyle(LVS_EX_FULLROWSELECT);
133 
134  //Turn off the horizontal scroll bar
135  //Todo: Figure out why the damn scroll bar pops up
136  list.ModifyStyle(WS_HSCROLL, 0L);
137 
138 
139  //Insert the one column
140  LVCOLUMN col;
141  col.mask = 0;
142  list.InsertColumn(0, &col);
143 
144  SetToggleIcons();
145 
146  return 0;
147 }
148 
152 void ToggleListView::OnSize(UINT nType, int cx, int cy) {
153  CListView::OnSize(nType, cx, cy);
154 
155  CListCtrl& list = GetListCtrl();
156  list.SetColumnWidth(0, cx-1);
157 }
158 
162 void ToggleListView::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) {
163  lpMeasureItemStruct->itemHeight = TOGGLELIST_ITEMHEIGHT;
164 }
165 
169 void ToggleListView::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult) {
170  CListCtrl& list = GetListCtrl();
171 
172  DWORD dwpos = GetMessagePos();
173 
174  LVHITTESTINFO info;
175  info.pt.x = LOWORD(dwpos);
176  info.pt.y = HIWORD(dwpos);
177 
178  ::MapWindowPoints(HWND_DESKTOP, pNMHDR->hwndFrom, &info.pt, 1);
179 
180  int index = list.HitTest(&info);
181  if ( index != -1 ) {
182  int toggleState = GetToggleState(index);
183  if(toggleState != TOGGLE_STATE_DISABLED) {
184 
185  RECT rItem;
186  list.GetItemRect(index, &rItem, LVIR_BOUNDS);
187 
188  if ( info.pt.x < TOGGLELIST_ITEMHEIGHT ) {
189  if(toggleState == TOGGLE_STATE_ON) {
190  SetToggleState(index, TOGGLE_STATE_OFF, true);
191  } else {
192  SetToggleState(index, TOGGLE_STATE_ON, true);
193  }
194  }
195  }
196  }
197  *pResult = 0;
198 }
199 
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;
207 
208  return CListView::PreCreateWindow(cs);
209 }
210 
214 void ToggleListView::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {
215 
216  CListCtrl& ListCtrl=GetListCtrl();
217  int nItem = lpDrawItemStruct->itemID;
218 
219  // get item data
220  LV_ITEM lvi;
221  _TCHAR szBuff[MAX_PATH];
222 
223  memset(&lvi, 0, sizeof(LV_ITEM));
224  lvi.mask = LVIF_TEXT;
225  lvi.iItem = nItem;
226  lvi.pszText = szBuff;
227  lvi.cchTextMax = sizeof(szBuff);
228  ListCtrl.GetItem(&lvi);
229 
230  RECT rDraw;
231 
232 
233  CopyRect ( &rDraw, &lpDrawItemStruct->rcItem );
234  rDraw.right = rDraw.left + TOGGLELIST_ITEMHEIGHT;
235  rDraw.top ++;
236 
237  rDraw.right ++;
238  FrameRect ( lpDrawItemStruct->hDC, &rDraw, (HBRUSH)GetStockObject ( BLACK_BRUSH ) );
239  rDraw.right --;
240 
241  FillRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_3DFACE ) );
242 
243  Draw3dRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_3DHILIGHT ), GetSysColorBrush ( COLOR_3DSHADOW ) );
244 
245  InflateRect ( &rDraw, -3, -3 );
246  Draw3dRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_3DSHADOW ), GetSysColorBrush ( COLOR_3DHILIGHT ) );
247 
248  switch(GetToggleState(lvi.iItem)) {
250  if(disabledIcon) {
251  DrawIconEx ( lpDrawItemStruct->hDC, rDraw.left, rDraw.top, disabledIcon, 16, 16,0, NULL, DI_NORMAL );
252  }
253  break;
254  case TOGGLE_STATE_ON:
255  if(onIcon) {
256  DrawIconEx ( lpDrawItemStruct->hDC, rDraw.left, rDraw.top, onIcon, 16, 16,0, NULL, DI_NORMAL );
257  }
258  break;
259  case TOGGLE_STATE_OFF:
260  if(offIcon) {
261  DrawIconEx ( lpDrawItemStruct->hDC, rDraw.left, rDraw.top, offIcon, 16, 16,0, NULL, DI_NORMAL );
262  }
263  break;
264  };
265 
266  CopyRect ( &rDraw, &lpDrawItemStruct->rcItem );
267  rDraw.left += TOGGLELIST_ITEMHEIGHT;
268  rDraw.left += 1;
269 
270  if ( lpDrawItemStruct->itemState & ODS_SELECTED ) {
271  FillRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_HIGHLIGHT ) );
272  } else {
273  FillRect ( lpDrawItemStruct->hDC, &rDraw, GetSysColorBrush ( COLOR_WINDOW ) );
274  }
275 
276  rDraw.left += TEXT_OFFSET;
277 
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 );
281 
282 }
283 
284 
288 void ToggleListView::Draw3dRect (HDC hDC, RECT* rect, HBRUSH topLeft, HBRUSH bottomRight) {
289  RECT rOut;
290 
291  SetRect ( &rOut, rect->left, rect->top, rect->right - 1, rect->top + 1 );
292  FillRect ( hDC,&rOut, topLeft );
293 
294  SetRect ( &rOut, rect->left, rect->top, rect->left + 1, rect->bottom );
295  FillRect( hDC,&rOut, topLeft );
296 
297  SetRect ( &rOut, rect->right, rect->top, rect->right -1, rect->bottom );
298  FillRect( hDC,&rOut, bottomRight );
299 
300  SetRect ( &rOut, rect->left, rect->bottom, rect->right, rect->bottom - 1 );
301  FillRect( hDC,&rOut, bottomRight );
302 }
303 
304 
305 
306 
assert(prefInfo.fullscreenBtn)
#define TEXT_OFFSET
CONST PIXELFORMATDESCRIPTOR UINT
Definition: win_qgl.cpp:47
virtual BOOL PreCreateWindow(CREATESTRUCT &cs)
Sets some window styles before the window is created.
DWORD
Definition: win_qgl.cpp:61
int GetToggleState(int index)
Gets the state of an item in the list.
void SetToggleState(int index, int toggleState, bool notify=false)
Sets the state of an item in the list.
virtual void OnStateChanged(int index, int toggleState)
void SetToggleIcons(LPCSTR disabled=NULL, LPCSTR on=NULL, LPCSTR off=NULL)
Sets the tree icons to dispay for each of the three states.
#define BOOL
Definition: mprintf.c:71
afx_msg void OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
Toggles the state of an item when the user clicks in the window.
GLuint index
Definition: glext.h:3476
virtual ~ToggleListView()
Destructor.
#define NULL
Definition: Lib.h:88
HDC hDC
Definition: wglext.h:383
void Draw3dRect(HDC hDC, RECT *rect, HBRUSH topLeft, HBRUSH bottomRight)
Draws a 3d rectangle using the given brushes this code was taken from the gui editor.
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:2853
Definition: eax4.h:1413
typedef HDC(WINAPI *PFNWGLGETCURRENTREADDCARBPROC)(void)
IMPLEMENT_DYNCREATE(CCamWnd, CWnd)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct)
Called as the window is being created and initializes icons and window styles.
afx_msg void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
Returns the size of each item in the toggle list.
A simple list view that supports a toggle button.
afx_msg void OnSize(UINT nType, int cx, int cy)
Called when the window is being resized.
#define TOGGLELIST_ITEMHEIGHT
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
Responsible for drawing each list item.