1 ////////////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 1998 by Thierry Maurel
3 // All rights reserved
4 //
5 // Distribute freely, except: don't remove my name from the source or
6 // documentation (don't take credit for my work), mark your changes (don't
7 // get me blamed for your possible bugs), don't alter or remove this
8 // notice.
9 // No warrantee of any kind, express or implied, is included with this
10 // software; use at your own risk, responsibility for damages (if any) to
11 // anyone resulting from the use of this software rests entirely with the
12 // user.
13 //
14 // Send bug reports, bug fixes, enhancements, requests, flames, etc., and
15 // I'll try to keep a version up to date. I can be reached as follows:
16 // tmaurel@caramail.com (or tmaurel@hol.fr)
17 //
18 ////////////////////////////////////////////////////////////////////////////////
19 // File : CmdAccelOb.cpp
20 // Project : AccelsEditor
21 ////////////////////////////////////////////////////////////////////////////////
22 // Version : 1.0 * Author : T.Maurel
23 // Date : 17.08.98
24 //
25 // Remarks :
26 //
27 ////////////////////////////////////////////////////////////////////////////////
28
29 #include "stdafx.h"
30 #include "CmdAccelOb.h"
31
32 ////////////////////////////////////////////////////////////////////////
33 //
34 //
35 MAPVIRTKEYS mapVirtKeys[] = {
36 {VK_LBUTTON, "VK_LBUTTON"},
37 {VK_RBUTTON, "VK_RBUTTON"},
38 {VK_CANCEL, "VK_CANCEL"},
39 {VK_MBUTTON, "VK_MBUTTON"},
40 {VK_BACK, "BACK"},
41 {VK_TAB, "TAB"},
42 {VK_CLEAR, "VK_CLEAR"},
43 {VK_RETURN, "RETURN"},
44 {VK_SHIFT, "SHIFT"},
45 {VK_CONTROL, "CONTROL"},
46 {VK_MENU, "MENU"},
47 {VK_PAUSE, "PAUSE"},
48 {VK_CAPITAL, "CAPITAL"},
49 {VK_ESCAPE, "ESCAPE"},
50 {VK_SPACE, "SPACE"},
51 {VK_PRIOR, "PRIOR"},
52 {VK_NEXT, "NEXT"},
53 {VK_END, "END"},
54 {VK_HOME, "HOME"},
55 {VK_LEFT, "LEFT"},
56 {VK_UP, "UP"},
57 {VK_RIGHT, "RIGHT"},
58 {VK_DOWN, "DOWN"},
59 {VK_SELECT, "VK_SELECT"},
60 {VK_PRINT, "PRINT"},
61 {VK_EXECUTE, "EXECUTE"},
62 {VK_SNAPSHOT, "SNAPSHOT"},
63 {VK_INSERT, "INSERT"},
64 {VK_DELETE, "DELETE"},
65 {VK_HELP, "VK_HELP"},
66 {WORD('0'), "0"},
67 {WORD('1'), "1"},
68 {WORD('2'), "2"},
69 {WORD('3'), "3"},
70 {WORD('4'), "4"},
71 {WORD('5'), "5"},
72 {WORD('6'), "6"},
73 {WORD('7'), "7"},
74 {WORD('8'), "8"},
75 {WORD('9'), "9"},
76 {WORD('A'), "A"},
77 {WORD('B'), "B"},
78 {WORD('C'), "C"},
79 {WORD('D'), "D"},
80 {WORD('E'), "E"},
81 {WORD('F'), "F"},
82 {WORD('G'), "G"},
83 {WORD('H'), "H"},
84 {WORD('I'), "I"},
85 {WORD('J'), "J"},
86 {WORD('K'), "K"},
87 {WORD('L'), "L"},
88 {WORD('M'), "M"},
89 {WORD('N'), "N"},
90 {WORD('O'), "O"},
91 {WORD('P'), "P"},
92 {WORD('Q'), "Q"},
93 {WORD('R'), "R"},
94 {WORD('S'), "S"},
95 {WORD('T'), "T"},
96 {WORD('U'), "U"},
97 {WORD('V'), "V"},
98 {WORD('W'), "W"},
99 {WORD('X'), "X"},
100 {WORD('Y'), "Y"},
101 {WORD('Z'), "Z"},
102 {VK_LWIN, "VK_LWIN"},
103 {VK_RWIN, "VK_RWIN"},
104 {VK_APPS, "VK_APPS"},
105 {VK_NUMPAD0, "NUMPAD0"},
106 {VK_NUMPAD1, "NUMPAD1"},
107 {VK_NUMPAD2, "NUMPAD2"},
108 {VK_NUMPAD3, "NUMPAD3"},
109 {VK_NUMPAD4, "NUMPAD4"},
110 {VK_NUMPAD5, "NUMPAD5"},
111 {VK_NUMPAD6, "NUMPAD6"},
112 {VK_NUMPAD7, "NUMPAD7"},
113 {VK_NUMPAD8, "NUMPAD8"},
114 {VK_NUMPAD9, "NUMPAD9"},
115 {VK_MULTIPLY, "MULTIPLY"},
116 {VK_ADD, "ADD"},
117 {VK_SEPARATOR, "SEPARATOR"},
118 {VK_SUBTRACT, "SUBTRACT"},
119 {VK_DECIMAL, "DECIMAL"},
120 {VK_DIVIDE, "DIVIDE"},
121 {VK_F1, "F1"},
122 {VK_F2, "F2"},
123 {VK_F3, "F3"},
124 {VK_F4, "F4"},
125 {VK_F5, "F5"},
126 {VK_F6, "F6"},
127 {VK_F7, "F7"},
128 {VK_F8, "F8"},
129 {VK_F9, "F9"},
130 {VK_F10, "F10"},
131 {VK_F11, "F11"},
132 {VK_F12, "F12"},
133 {VK_F13, "F13"},
134 {VK_F14, "F14"},
135 {VK_F15, "F15"},
136 {VK_F16, "F16"},
137 {VK_F17, "F17"},
138 {VK_F18, "F18"},
139 {VK_F19, "F19"},
140 {VK_F20, "F20"},
141 {VK_F21, "F21"},
142 {VK_F22, "F22"},
143 {VK_F23, "F23"},
144 {VK_F24, "F24"},
145 {VK_NUMLOCK, "NUMLOCK"},
146 {VK_SCROLL, "VK_SCROLL"},
147 {VK_ATTN, "VK_ATTN"},
148 {VK_CRSEL, "VK_CRSEL"},
149 {VK_EXSEL, "VK_EXSEL"},
150 {VK_EREOF, "VK_EREOF"},
151 {VK_PLAY, "VK_PLAY"},
152 {VK_ZOOM, "VK_ZOOM"},
153 {VK_NONAME, "VK_NONAME"},
154 {VK_PA1, "VK_PA1"},
155 {VK_OEM_CLEAR, "VK_OEM_CLEAR"},
156 };
157
158
159 ////////////////////////////////////////////////////////////////////////
160 //
161 //
162 MAPVIRTKEYS mapVirtSysKeys[] = {
163 {FCONTROL, "Ctrl"},
164 {FALT, "Alt"},
165 {FSHIFT, "Shift"},
166 };
167
168
169 ////////////////////////////////////////////////////////////////////////
170 // helper fct for external access
171 ////////////////////////////////////////////////////////////////////////
172 //
173 //
mapVirtKeysStringFromWORD(WORD wKey)174 TCHAR* mapVirtKeysStringFromWORD(WORD wKey)
175 {
176 for (int index = 0; index < sizeof(mapVirtKeys)/sizeof(mapVirtKeys[0]); index++) {
177 if (mapVirtKeys[index].wKey == wKey)
178 return mapVirtKeys[index].szKey;
179 }
180 return NULL;
181 }
182
183
184
185 ////////////////////////////////////////////////////////////////////////
186 //
187 #define DEFAULT_ACCEL 0x01
188 #define USER_ACCEL 0x02
189
190
191 ////////////////////////////////////////////////////////////////////////
192 //
193 ////////////////////////////////////////////////////////////////////////
194 //
195 //
CAccelsOb()196 CAccelsOb::CAccelsOb()
197 {
198 m_cVirt = 0;
199 m_wKey = 0;
200 m_bLocked = false;
201 }
202
203
204 ////////////////////////////////////////////////////////////////////////
205 //
206 //
CAccelsOb(CAccelsOb * pFrom)207 CAccelsOb::CAccelsOb(CAccelsOb* pFrom)
208 {
209 ASSERT(pFrom != NULL);
210
211 m_cVirt = pFrom->m_cVirt;
212 m_wKey = pFrom->m_wKey;
213 m_bLocked = pFrom->m_bLocked;
214 }
215
216
217 ////////////////////////////////////////////////////////////////////////
218 //
219 //
CAccelsOb(BYTE cVirt,WORD wKey,bool bLocked)220 CAccelsOb::CAccelsOb(BYTE cVirt, WORD wKey, bool bLocked)
221 {
222 m_cVirt = cVirt;
223 m_wKey = wKey;
224 m_bLocked = bLocked;
225 }
226
227
228 ////////////////////////////////////////////////////////////////////////
229 //
230 //
CAccelsOb(LPACCEL pACCEL)231 CAccelsOb::CAccelsOb(LPACCEL pACCEL)
232 {
233 ASSERT(pACCEL != NULL);
234
235 m_cVirt = pACCEL->fVirt;
236 m_wKey = pACCEL->key;
237 m_bLocked = false;
238 }
239
240
241 ////////////////////////////////////////////////////////////////////////
242 //
243 //
operator =(const CAccelsOb & from)244 CAccelsOb& CAccelsOb::operator=(const CAccelsOb& from)
245 {
246 m_cVirt = from.m_cVirt;
247 m_wKey = from.m_wKey;
248 m_bLocked = from.m_bLocked;
249
250 return *this;
251 }
252
253
254 ////////////////////////////////////////////////////////////////////////
255 //
256 ////////////////////////////////////////////////////////////////////////
257 //
258 //
GetString(CString & szBuffer)259 void CAccelsOb::GetString(CString& szBuffer)
260 {
261 szBuffer = "";
262 // in case of the object is not assigned, we avoid error messages
263 if (m_wKey == 0)
264 return;
265
266 // modifiers part
267 for (int i = 0; i < sizetable(mapVirtSysKeys); i++) {
268 if (m_cVirt & mapVirtSysKeys[i].wKey) {
269 szBuffer += mapVirtSysKeys[i].szKey;
270 szBuffer += "+";
271 }
272 }
273 // and virtual key part
274 for (i = 0; i < sizetable(mapVirtKeys); i++) {
275 if (m_wKey == mapVirtKeys[i].wKey) {
276 szBuffer += mapVirtKeys[i].szKey;
277 return;
278 }
279 }
280 AfxMessageBox("Internal error : (CAccelsOb::GetString) m_wKey invalid");
281 }
282
283
284 ////////////////////////////////////////////////////////////////////////
285 //
286 //
IsEqual(WORD wKey,bool bCtrl,bool bAlt,bool bShift)287 bool CAccelsOb::IsEqual(WORD wKey, bool bCtrl, bool bAlt, bool bShift)
288 {
289 // CString szTemp;
290 // GetString(szTemp);
291
292
293 bool m_bCtrl = (m_cVirt & FCONTROL) ? true : false;
294 bool bRet = (bCtrl == m_bCtrl);
295
296 bool m_bAlt = (m_cVirt & FALT) ? true : false;
297 bRet &= (bAlt == m_bAlt);
298
299 bool m_bShift = (m_cVirt & FSHIFT) ? true : false;
300 bRet &= (bShift == m_bShift);
301
302 bRet &= static_cast<bool>(m_wKey == wKey);
303
304 return bRet;
305 }
306
307
308 ////////////////////////////////////////////////////////////////////////
309 //
310 //
GetData()311 DWORD CAccelsOb::GetData()
312 {
313 BYTE cLocalCodes = 0;
314 if (m_bLocked)
315 cLocalCodes = DEFAULT_ACCEL;
316 else
317 cLocalCodes = USER_ACCEL;
318
319 WORD bCodes = MAKEWORD(m_cVirt, cLocalCodes);
320 return MAKELONG(m_wKey, bCodes);
321 }
322
323
324 ////////////////////////////////////////////////////////////////////////
325 //
326 //
SetData(DWORD dwDatas)327 bool CAccelsOb::SetData(DWORD dwDatas)
328 {
329 m_wKey = LOWORD(dwDatas);
330
331 WORD bCodes = HIWORD(dwDatas);
332 m_cVirt = LOBYTE(bCodes);
333
334 BYTE cLocalCodes = HIBYTE(bCodes);
335 m_bLocked = static_cast<bool>(cLocalCodes == DEFAULT_ACCEL);
336 return true;
337 }
338
339 ////////////////////////////////////////////////////////////////////////
340 //
341 #ifdef _DEBUG
342 ////////////////////////////////////////////////////////////////////////
343 //
344 //
AssertValid() const345 void CAccelsOb::AssertValid() const
346 {
347 CObject::AssertValid();
348 }
349
350 ////////////////////////////////////////////////////////////////////////
351 //
352 //
Dump(CDumpContext & dc) const353 void CAccelsOb::Dump(CDumpContext& dc) const
354 {
355 dc << "\t\t";
356 CObject::Dump(dc);
357 dc << "\t\tlocked=" << m_bLocked << ", cVirt=" << m_cVirt << ", wKey=" << m_wKey << "\n\n";
358
359 }
360 #endif
361
362 ////////////////////////////////////////////////////////////////////////
363 //
364 ////////////////////////////////////////////////////////////////////////
365 //
366 //
CCmdAccelOb()367 CCmdAccelOb::CCmdAccelOb()
368 {
369 }
370
371
372 ////////////////////////////////////////////////////////////////////////
373 //
374 //
CCmdAccelOb(WORD wIDCommand,LPCTSTR szCommand)375 CCmdAccelOb::CCmdAccelOb(WORD wIDCommand, LPCTSTR szCommand)
376 {
377 ASSERT(szCommand != NULL);
378
379 m_wIDCommand = wIDCommand;
380 m_szCommand = szCommand;
381 }
382
383
384 ////////////////////////////////////////////////////////////////////////
385 //
386 //
CCmdAccelOb(BYTE cVirt,WORD wIDCommand,WORD wKey,LPCTSTR szCommand,bool bLocked)387 CCmdAccelOb::CCmdAccelOb(BYTE cVirt, WORD wIDCommand, WORD wKey, LPCTSTR szCommand, bool bLocked)
388 {
389 ASSERT(szCommand != NULL);
390
391 m_wIDCommand = wIDCommand;
392 m_szCommand = szCommand;
393
394 CAccelsOb* pAccel = DEBUG_NEW CAccelsOb(cVirt, wKey, bLocked);
395 ASSERT(pAccel != NULL);
396 m_Accels.AddTail(pAccel);
397 }
398
399
400 ////////////////////////////////////////////////////////////////////////
401 //
402 //
~CCmdAccelOb()403 CCmdAccelOb::~CCmdAccelOb()
404 {
405 POSITION pos = m_Accels.GetHeadPosition();
406 while (pos != NULL)
407 delete m_Accels.GetNext(pos);
408 m_Accels.RemoveAll();
409 }
410
411
412 ////////////////////////////////////////////////////////////////////////
413 //
414 ////////////////////////////////////////////////////////////////////////
415 //
416 //
Add(BYTE cVirt,WORD wKey,bool bLocked)417 void CCmdAccelOb::Add(BYTE cVirt, WORD wKey, bool bLocked)
418 {
419 CAccelsOb* pAccel = DEBUG_NEW CAccelsOb(cVirt, wKey, bLocked);
420 ASSERT(pAccel != NULL);
421 m_Accels.AddTail(pAccel);
422 }
423
424
425 ////////////////////////////////////////////////////////////////////////
426 //
427 //
Add(CAccelsOb * pAccel)428 void CCmdAccelOb::Add(CAccelsOb* pAccel)
429 {
430 ASSERT(pAccel != NULL);
431 m_Accels.AddTail(pAccel);
432 }
433
434
435 ////////////////////////////////////////////////////////////////////////
436 //
437 //
operator =(const CCmdAccelOb & from)438 CCmdAccelOb& CCmdAccelOb::operator=(const CCmdAccelOb& from)
439 {
440 Reset();
441
442 m_wIDCommand = from.m_wIDCommand;
443 m_szCommand = from.m_szCommand;
444
445 CAccelsOb* pAccel;
446 POSITION pos = from.m_Accels.GetHeadPosition();
447 while (pos != NULL) {
448 pAccel = DEBUG_NEW CAccelsOb(from.m_Accels.GetNext(pos));
449 ASSERT(pAccel != NULL);
450 m_Accels.AddTail(pAccel);
451 }
452 return *this;
453 }
454
455
456 ////////////////////////////////////////////////////////////////////////
457 //
458 //
DeleteUserAccels()459 void CCmdAccelOb::DeleteUserAccels()
460 {
461 CAccelsOb* pAccel;
462 POSITION prevPos;
463 POSITION pos = m_Accels.GetHeadPosition();
464 while (pos != NULL) {
465 prevPos = pos;
466 pAccel = m_Accels.GetNext(pos);
467 if (!pAccel->m_bLocked) {
468 delete pAccel;
469 m_Accels.RemoveAt(prevPos);
470 }
471 }
472 }
473
474
475 ////////////////////////////////////////////////////////////////////////
476 //
477 //
Reset()478 void CCmdAccelOb::Reset()
479 {
480 m_wIDCommand = 0;
481 m_szCommand = "Empty command";
482
483 CAccelsOb* pAccel;
484 POSITION pos = m_Accels.GetHeadPosition();
485 while (pos != NULL) {
486 pAccel = m_Accels.GetNext(pos);
487 delete pAccel;
488 }
489 }
490
491 ////////////////////////////////////////////////////////////////////////
492 //
493 #ifdef _DEBUG
494 ////////////////////////////////////////////////////////////////////////
495 //
496 //
AssertValid() const497 void CCmdAccelOb::AssertValid() const
498 {
499 // call base class function first
500 CObject::AssertValid();
501 }
502
503
504 ////////////////////////////////////////////////////////////////////////
505 //
506 //
Dump(CDumpContext & dc) const507 void CCmdAccelOb::Dump( CDumpContext& dc ) const
508 {
509 // call base class function first
510 dc << "\t";
511 CObject::Dump( dc );
512
513 // now do the stuff for our specific class
514 dc << "\tIDCommand = " << m_wIDCommand;
515 dc << "\n\tszCommand = " << m_szCommand;
516 dc << "\n\tAccelerators = {\n";
517
518 CAccelsOb* pAccel;
519 POSITION pos = m_Accels.GetHeadPosition();
520 while (pos != NULL) {
521 pAccel = m_Accels.GetNext(pos);
522 dc << pAccel;
523 }
524 dc << "\t}\n";
525 }
526 #endif
527