1 // DotXkeymacs.cpp: implementation of the CDotXkeymacs class.
\r
3 //////////////////////////////////////////////////////////////////////
\r
8 #include "xkeymacs.h"
\r
9 #include "DotXkeymacs.h"
\r
13 static char THIS_FILE[]=__FILE__;
\r
14 #define new DEBUG_NEW
\r
17 #pragma data_seg(".xkmcs")
\r
18 CObList CDotXkeymacs::m_oFunctionDefinition;
\r
19 int CDotXkeymacs::m_nIndex[MAX_APP][MAX_COMMAND_TYPE][MAX_KEY] = {'\0'};
\r
20 const TCHAR CDotXkeymacs::m_szExt[] = _T("xkeymacs");
\r
23 //////////////////////////////////////////////////////////////////////
\r
24 // Construction/Destruction
\r
25 //////////////////////////////////////////////////////////////////////
\r
27 CDotXkeymacs::CDotXkeymacs()
\r
32 CDotXkeymacs::~CDotXkeymacs()
\r
37 void CDotXkeymacs::Load(LPCTSTR lpszFileName)
\r
39 CStdioFile oDotXkeymacs;
\r
40 if (oDotXkeymacs.Open(lpszFileName, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeRead | CFile::shareDenyWrite | CFile::typeText)) {
\r
42 while (oDotXkeymacs.ReadString(szRead)) {
\r
43 if (IsFunctionDefinition(szRead)) {
\r
44 CFunctionDefinition *pFunctionDefinition = new CFunctionDefinition(GetSymbol(szRead), GetDefinition(szRead));
\r
46 // Delete a listed definition which has the same symbol as a new one.
\r
47 for (POSITION currentPos, pos = m_oFunctionDefinition.GetHeadPosition(); (currentPos = pos) != NULL; ) {
\r
48 CFunctionDefinition *pCurrentDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetNext(pos);
\r
50 if (pCurrentDefinition->GetSymbol() == pFunctionDefinition->GetSymbol()) {
\r
51 CFunctionDefinition *pOverwritten = (CFunctionDefinition *)m_oFunctionDefinition.GetAt(currentPos);
\r
52 m_oFunctionDefinition.RemoveAt(currentPos);
\r
53 delete pOverwritten;
\r
57 m_oFunctionDefinition.AddTail((CObject *)pFunctionDefinition);
\r
63 void CDotXkeymacs::LoadMainData(LPCTSTR lpszFileName)
\r
65 TCHAR szModuleFileName[MAX_PATH] = {'\0'};
\r
66 TCHAR szDrive[_MAX_DRIVE] = {'\0'};
\r
67 TCHAR szDir[_MAX_DIR] = {'\0'};
\r
69 if (GetModuleFileName(NULL, szModuleFileName, sizeof(szModuleFileName))) {
\r
70 _tsplitpath(szModuleFileName, szDrive, szDir, NULL, NULL);
\r
73 TCHAR szOldPath[MAX_PATH] = {'\0'}; // This path is used by XKeymacs 3.22 and earlier
\r
74 _tmakepath(szOldPath, szDrive, szDir, lpszFileName, m_szExt);
\r
76 PathAppend(szDir, _T("etc"));
\r
77 TCHAR szEtc[MAX_PATH] = {'\0'};
\r
78 _tmakepath(szEtc, szDrive, szDir, NULL, NULL);
\r
79 (void)_tmkdir(szEtc); // make etc directory if needed
\r
81 TCHAR szPath[MAX_PATH] = {'\0'};
\r
82 _tmakepath(szPath, szDrive, szDir, lpszFileName, m_szExt);
\r
84 if (_trename(szOldPath, szPath)) { // try to move old file as backup when rename returns an error because files exist in both directorys
\r
85 TCHAR szBackupPath[MAX_PATH] = {'\0'};
\r
86 TCHAR szBackupFlag[_MAX_FNAME] = _T("~");
\r
87 _tmakepath(szBackupPath, szDrive, szDir, _tcscat(szBackupFlag, lpszFileName), m_szExt);
\r
88 (void)_trename(szOldPath, szBackupPath); // do nothing when a backup file has existed already
\r
94 void CDotXkeymacs::LoadUserData(LPCTSTR lpszFileName)
\r
96 TCHAR szPath[MAX_PATH] = {'\0'};
\r
97 if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_APPDATA, TRUE)) {
\r
98 _tmakepath(szPath, NULL, szPath, lpszFileName, m_szExt);
\r
103 void CDotXkeymacs::Load()
\r
105 static LPCTSTR szFileName = _T("dot");
\r
107 ClearFunctionDefinition();
\r
108 LoadMainData(GetLanguage()); // just for localization
\r
109 LoadMainData(szFileName);
\r
110 LoadUserData(szFileName); // overwrite main data
\r
113 BOOL CDotXkeymacs::IsFunctionDefinition(CString szFunctionDefinition)
\r
115 return !_tcsncmp(szFunctionDefinition, CString(MAKEINTRESOURCE(IDS_FSET)), _tcslen(CString(MAKEINTRESOURCE(IDS_FSET))));
\r
118 CString CDotXkeymacs::GetSymbol(CString szFunctionDefinition)
\r
120 const int nFirst = _tcslen(CString(MAKEINTRESOURCE(IDS_FSET))) + _tcslen(_T("'"));
\r
121 return szFunctionDefinition.Mid(nFirst, szFunctionDefinition.Find(_T(' '), nFirst) - nFirst);
\r
124 CString CDotXkeymacs::GetDefinition(CString szFunctionDefinition)
\r
126 const int nFirst = szFunctionDefinition.Find(_T(' '), _tcslen(CString(MAKEINTRESOURCE(IDS_FSET)))) + _tcslen(_T("'"));
\r
127 return szFunctionDefinition.Mid(nFirst, szFunctionDefinition.GetLength() - nFirst - _tcslen(_T(")")));
\r
130 void CDotXkeymacs::ClearFunctionDefinition()
\r
132 while (!m_oFunctionDefinition.IsEmpty()) {
\r
133 CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetHead();
\r
134 delete pFunctionDefinition;
\r
135 pFunctionDefinition = NULL;
\r
136 m_oFunctionDefinition.RemoveHead();
\r
138 memset(m_nIndex, -1, sizeof(m_nIndex));
\r
141 int CDotXkeymacs::GetFunctionNumber()
\r
143 return m_oFunctionDefinition.GetCount();
\r
146 CString CDotXkeymacs::GetFunctionSymbol(int nIndex)
\r
148 if (nIndex < 0 || m_oFunctionDefinition.GetCount() <= nIndex) {
\r
152 if (CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetAt(m_oFunctionDefinition.FindIndex(nIndex))) {
\r
153 return pFunctionDefinition->GetSymbol();
\r
158 CString CDotXkeymacs::GetFunctionDefinition(int nIndex)
\r
160 if (nIndex < 0 || m_oFunctionDefinition.GetCount() <= nIndex) {
\r
164 if (CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetAt(m_oFunctionDefinition.FindIndex(nIndex))) {
\r
165 return pFunctionDefinition->GetDefinition();
\r
170 CString CDotXkeymacs::GetFunctionDefinition(CString szSymbol)
\r
172 for (POSITION pos = m_oFunctionDefinition.GetHeadPosition(); pos;) {
\r
173 CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetNext(pos);
\r
174 if (!pFunctionDefinition->GetSymbol().Compare(szSymbol)) {
\r
175 return pFunctionDefinition->GetDefinition();
\r
179 return CString(_T("Undefined Command"));
\r
182 void CDotXkeymacs::ClearKey(int nIndex, int nApplicationID)
\r
184 if (nIndex < 0 || m_oFunctionDefinition.GetCount() <= nIndex) {
\r
188 if (CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetAt(m_oFunctionDefinition.FindIndex(nIndex))) {
\r
189 pFunctionDefinition->ClearKey(nApplicationID);
\r
192 for (int nCommandType = 0; nCommandType < MAX_COMMAND_TYPE; ++nCommandType) {
\r
193 for (int nKey = 0; nKey < MAX_KEY; ++nKey) {
\r
194 if (m_nIndex[nApplicationID][nCommandType][nKey] == nIndex) {
\r
195 m_nIndex[nApplicationID][nCommandType][nKey] = -1;
\r
201 void CDotXkeymacs::SetKey(int nIndex, int nApplicationID, int nCommandType, int nKey)
\r
203 if (nIndex < 0 || m_oFunctionDefinition.GetCount() <= nIndex) {
\r
207 if (CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetAt(m_oFunctionDefinition.FindIndex(nIndex))) {
\r
208 pFunctionDefinition->SetKey(nApplicationID, nCommandType, nKey);
\r
209 m_nIndex[nApplicationID][nCommandType][nKey] = nIndex;
\r
213 int CDotXkeymacs::GetIndex(CString szSymbol)
\r
216 for (nIndex = 0; nIndex < m_oFunctionDefinition.GetCount(); ++nIndex) {
\r
217 CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetAt(m_oFunctionDefinition.FindIndex(nIndex));
\r
218 if (!pFunctionDefinition->GetSymbol().Compare(szSymbol)) {
\r
225 int CDotXkeymacs::GetKeyNumber(int nIndex, int nApplicationID)
\r
227 if (nIndex < 0 || m_oFunctionDefinition.GetCount() <= nIndex) {
\r
231 CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetAt(m_oFunctionDefinition.FindIndex(nIndex));
\r
232 return pFunctionDefinition->GetKeyNumber(nApplicationID);
\r
235 void CDotXkeymacs::GetKey(int nIndex, int nApplicationID, int nKeyID, int *pCommandType, int *pKey)
\r
237 if (nIndex < 0 || m_oFunctionDefinition.GetCount() <= nIndex) {
\r
241 CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetAt(m_oFunctionDefinition.FindIndex(nIndex));
\r
242 pFunctionDefinition->GetKey(nApplicationID, nKeyID, pCommandType, pKey);
\r
245 int CDotXkeymacs::GetIndex(int nApplicationID, int nCommandType, int nKey)
\r
247 return m_nIndex[nApplicationID][nCommandType][nKey];
\r
250 void CDotXkeymacs::RemoveKey(const int nIndex, const int nApplicationID, const int nCommandType, const int nKey)
\r
252 if (nIndex < 0 || m_oFunctionDefinition.GetCount() <= nIndex) {
\r
256 CFunctionDefinition *pFunctionDefinition = (CFunctionDefinition *)m_oFunctionDefinition.GetAt(m_oFunctionDefinition.FindIndex(nIndex));
\r
257 if (pFunctionDefinition) {
\r
258 pFunctionDefinition->RemoveKey(nApplicationID, nCommandType, nKey);
\r
262 void CDotXkeymacs::RemoveKey(const int nApplicationID, const int nCommandType, const int nKey)
\r
264 for (int nIndex = 0; nIndex < m_oFunctionDefinition.GetCount(); ++nIndex) {
\r
265 RemoveKey(nIndex, nApplicationID, nCommandType, nKey);
\r
269 LPCTSTR CDotXkeymacs::GetLanguage()
\r
271 LPCTSTR szLanguage = _T("unknown");
\r
273 TCHAR lptstrFilename[MAX_PATH] = {'\0'};
\r
274 _tmakepath(lptstrFilename, NULL, _tgetenv(_T("windir")), _T("explorer"), _T("exe"));
\r
275 DWORD dwLen = GetFileVersionInfoSize(lptstrFilename, NULL);
\r
278 LPVOID lpData = malloc(dwLen);
\r
280 if (lpData && GetFileVersionInfo(lptstrFilename, NULL, dwLen, lpData)) {
\r
281 Translate_t *lpTranslate;
\r
282 UINT cbTranslate = 0;
\r
284 if (VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &cbTranslate) && sizeof(Translate_t) <= cbTranslate) {
\r
285 for (int i = 0; i < sizeof(Languages)/sizeof(Languages[0]); ++i) {
\r
286 if (Languages[i].wLanguage == lpTranslate->wLanguage) {
\r
287 szLanguage = Languages[i].szLanguage;
\r