1 /* 2 * Copyright 1999-2009 Jeremy Friesner 3 * Copyright 2009 Haiku, Inc. All rights reserved. 4 * Distributed under the terms of the MIT License. 5 * 6 * Authors: 7 * Jeremy Friesner 8 */ 9 10 11 #include "MetaKeyStateMap.h" 12 13 14 #include <stdio.h> 15 #include <string.h> 16 17 18 #include "BitFieldTesters.h" 19 20 21 MetaKeyStateMap::MetaKeyStateMap() 22 : 23 fKeyName(NULL) 24 { 25 // User MUST call SetInfo() before using! 26 } 27 28 29 MetaKeyStateMap::MetaKeyStateMap(const char* name) 30 { 31 SetInfo(name); 32 } 33 34 35 void 36 MetaKeyStateMap::SetInfo(const char* keyName) 37 { 38 fKeyName = new char[strlen(keyName) + 1]; 39 strcpy(fKeyName, keyName); 40 } 41 42 43 MetaKeyStateMap::~MetaKeyStateMap() 44 { 45 delete [] fKeyName; 46 int nr = fStateDescs.CountItems(); 47 for (int i = 0; i < nr; i++) 48 delete [] ((const char*) fStateDescs.ItemAt(i)); 49 50 nr = fStateTesters.CountItems(); 51 for (int j = 0; j < nr; j++) 52 delete ((BitFieldTester*) fStateTesters.ItemAt(j)); 53 // _stateBits are stored in-line, no need to delete them 54 } 55 56 57 void 58 MetaKeyStateMap::AddState(const char* d, const BitFieldTester* t) 59 { 60 char* copy = new char[strlen(d) + 1]; 61 strcpy(copy, d); 62 fStateDescs.AddItem(copy); 63 fStateTesters.AddItem((void *)t); 64 } 65 66 67 int 68 MetaKeyStateMap::GetNumStates() const 69 { 70 return fStateTesters.CountItems(); 71 } 72 73 74 const BitFieldTester* 75 MetaKeyStateMap::GetNthStateTester(int stateNum) const 76 { 77 return ((const BitFieldTester*) fStateTesters.ItemAt(stateNum)); 78 } 79 80 81 const char* 82 MetaKeyStateMap::GetNthStateDesc(int stateNum) const 83 { 84 return ((const char*) fStateDescs.ItemAt(stateNum)); 85 } 86 87 88 const char* 89 MetaKeyStateMap::GetName() const 90 { 91 return fKeyName; 92 } 93