]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/tools/materialeditor/ToggleListView.cpp
hello world
[icculus/iodoom3.git] / neo / tools / materialeditor / ToggleListView.cpp
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
37 IMPLEMENT_DYNCREATE(ToggleListView, CListView)
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
47 /**
48 * Protected constructor used by dynamic creation.
49 */
50 ToggleListView::ToggleListView() {
51         onIcon = NULL;
52         offIcon = NULL;
53         disabledIcon = NULL;
54 }
55
56 /**
57 * Destructor.
58 */
59 ToggleListView::~ToggleListView() {
60 }
61
62
63 /**
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
68 * state.
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.
72 */
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 {
89                 disabledIcon = NULL;
90         }
91 }
92
93 /**
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.
99 */
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
111 /**
112 * Gets the state of an item in the list
113 * @param index Index of the item of which to retreive the state.
114 */
115 int ToggleListView::GetToggleState(int index) {
116         CListCtrl& list = GetListCtrl();
117         assert(index >= 0 && index < list.GetItemCount());
118
119         DWORD data = list.GetItemData(index);
120         return data;
121 }
122
123 /**
124 * Called as the window is being created and initializes icons and window styles
125 */
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
149 /**
150 * Called when the window is being resized.
151 */
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
159 /**
160 * Returns the size of each item in the toggle list.
161 */
162 void ToggleListView::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) {
163         lpMeasureItemStruct->itemHeight = TOGGLELIST_ITEMHEIGHT;        
164 }
165
166 /**
167 * Toggles the state of an item when the user clicks in the window.
168 */
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
200 /**
201 * Sets some window styles before the window is created.
202 */
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;
207
208         return CListView::PreCreateWindow(cs);
209 }
210
211 /**
212 * Responsible for drawing each list item.
213 */
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)) {
249                 case TOGGLE_STATE_DISABLED:
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
285 /**
286 * Draws a 3d rectangle using the given brushes this code was taken from the gui editor
287 */
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