1 // PropertiesAdvanced.cpp : implementation file
\r
5 #include "xkeymacs.h"
\r
6 #include "DotXkeymacs.h"
\r
8 #include "Properties.h"
\r
9 #include "PropertiesAdvanced.h"
\r
12 #define new DEBUG_NEW
\r
14 static char THIS_FILE[] = __FILE__;
\r
17 /////////////////////////////////////////////////////////////////////////////
\r
18 // CPropertiesAdvanced property page
\r
19 int CPropertiesAdvanced::m_nApplicationID = 0;
\r
20 int CPropertiesAdvanced::m_nCommandID = 0;
\r
21 CEdit * CPropertiesAdvanced::m_pNewKey = NULL;
\r
22 CButton * CPropertiesAdvanced::m_pAssign = NULL;
\r
23 CStatic * CPropertiesAdvanced::m_pCurrentlyAssigned = NULL;
\r
24 CListBox * CPropertiesAdvanced::m_pCurrentKeys = NULL;
\r
25 HHOOK CPropertiesAdvanced::m_hKeyboardHook = NULL;
\r
26 BOOL CPropertiesAdvanced::m_bC_x = FALSE;
\r
27 BOOL CPropertiesAdvanced::m_bC = TRUE;
\r
28 BOOL CPropertiesAdvanced::m_bM = TRUE;
\r
29 BOOL CPropertiesAdvanced::m_bShift = TRUE;
\r
30 int CPropertiesAdvanced::m_nAssignCommandType = 0;
\r
31 int CPropertiesAdvanced::m_nAssignKey = 0;
\r
32 int CPropertiesAdvanced::m_nCommandIDs[MAX_COMMAND_TYPE][MAX_KEY] = {'\0'};
\r
34 IMPLEMENT_DYNCREATE(CPropertiesAdvanced, CPropertyPage)
\r
36 CPropertiesAdvanced::CPropertiesAdvanced() : CPropertyPage(CPropertiesAdvanced::IDD)
\r
38 //{{AFX_DATA_INIT(CPropertiesAdvanced)
\r
39 m_bEnableCUA = FALSE;
\r
43 CPropertiesAdvanced::~CPropertiesAdvanced()
\r
47 void CPropertiesAdvanced::DoDataExchange(CDataExchange* pDX)
\r
49 CPropertyPage::DoDataExchange(pDX);
\r
50 //{{AFX_DATA_MAP(CPropertiesAdvanced)
\r
51 DDX_Control(pDX, IDC_CURRENTLY_ASSIGNED, m_cCurrentlyAssigned);
\r
52 DDX_Control(pDX, IDC_RESET_ALL, m_cResetAll);
\r
53 DDX_Control(pDX, IDC_REMOVE, m_cRemove);
\r
54 DDX_Control(pDX, IDC_NEW_KEY, m_cNewKey);
\r
55 DDX_Control(pDX, IDC_DESCRIPTION, m_cDescription);
\r
56 DDX_Control(pDX, IDC_CURRENT_KEYS, m_cCurrentKeys);
\r
57 DDX_Control(pDX, IDC_COMMANDS, m_cCommands);
\r
58 DDX_Control(pDX, IDC_CATEGORY, m_cCategory);
\r
59 DDX_Control(pDX, IDC_ASSIGN, m_cAssign);
\r
60 DDX_Check(pDX, IDC_ENABLE_CUA, m_bEnableCUA);
\r
65 BEGIN_MESSAGE_MAP(CPropertiesAdvanced, CPropertyPage)
\r
66 //{{AFX_MSG_MAP(CPropertiesAdvanced)
\r
68 ON_CBN_SELCHANGE(IDC_CATEGORY, OnSelchangeCategory)
\r
69 ON_LBN_SELCHANGE(IDC_COMMANDS, OnSelchangeCommands)
\r
70 ON_LBN_SELCHANGE(IDC_CURRENT_KEYS, OnSelchangeCurrentKeys)
\r
71 ON_EN_SETFOCUS(IDC_NEW_KEY, OnSetfocusNewKey)
\r
72 ON_BN_CLICKED(IDC_ASSIGN, OnAssign)
\r
73 ON_BN_CLICKED(IDC_REMOVE, OnRemove)
\r
74 ON_BN_CLICKED(IDC_RESET_ALL, OnResetAll)
\r
75 ON_BN_CLICKED(IDC_C_X, OnCX)
\r
76 ON_EN_KILLFOCUS(IDC_NEW_KEY, OnKillfocusNewKey)
\r
77 ON_BN_CLICKED(IDC_ENABLE_CUA, OnEnableCua)
\r
81 /////////////////////////////////////////////////////////////////////////////
\r
82 // CPropertiesAdvanced message handlers
\r
84 BOOL CPropertiesAdvanced::OnSetActive()
\r
86 m_pProperties->EnableControl(ADVANCED_TAB);
\r
87 SetDialogData(m_pProperties->GetCurrentApplication());
\r
89 return CPropertyPage::OnSetActive();
\r
92 void CPropertiesAdvanced::SetDialogData(CString szApplicationName)
\r
94 UpdateDialogData(szApplicationName, FALSE);
\r
97 void CPropertiesAdvanced::UpdateDialogData(CString szApplicationName, BOOL bSaveAndValidate)
\r
99 m_nApplicationID = m_pProperties->GetApplicationID();
\r
101 if (bSaveAndValidate) { // GetDialogData
\r
105 if (m_nApplicationID == MAX_APP) { // FIXME
\r
110 m_bEnableCUA = CProfile::GetEnableCUA(m_nApplicationID);
\r
112 if (!bSaveAndValidate) { // SetDialogData
\r
116 if (!bSaveAndValidate) { // SetDialogData
\r
121 void CPropertiesAdvanced::GetDialogData()
\r
123 UpdateDialogData(m_pProperties->GetCurrentApplication(), TRUE);
\r
126 int CPropertiesAdvanced::OnCreate(LPCREATESTRUCT lpCreateStruct)
\r
128 if (CPropertyPage::OnCreate(lpCreateStruct) == -1)
\r
131 m_pProperties = (CProperties *)GetParent()->GetParent();
\r
136 BOOL CPropertiesAdvanced::OnKillActive()
\r
140 return CPropertyPage::OnKillActive();
\r
143 void CPropertiesAdvanced::EnableControl()
\r
145 BOOL bEnable = m_pProperties->IsEnableControl();
\r
147 m_cCategory.EnableWindow(bEnable);
\r
148 m_cCommands.EnableWindow(bEnable);
\r
149 m_cCurrentKeys.EnableWindow(bEnable);
\r
150 m_cNewKey.EnableWindow(bEnable);
\r
152 m_cAssign.EnableWindow(FALSE);
\r
153 m_cRemove.EnableWindow(FALSE);
\r
154 m_cResetAll.EnableWindow(bEnable);
\r
157 BOOL CPropertiesAdvanced::OnInitDialog()
\r
159 CPropertyPage::OnInitDialog();
\r
161 m_nApplicationID = m_pProperties->GetApplicationID();
\r
162 InitCategoryList();
\r
165 SetDialogData(m_pProperties->GetCurrentApplication());
\r
167 m_cAssign.EnableWindow(FALSE);
\r
168 m_cRemove.EnableWindow(FALSE);
\r
170 m_cCurrentlyAssigned.SetWindowText(_T(""));
\r
172 return TRUE; // return TRUE unless you set the focus to a control
\r
173 // EXCEPTION: OCX Property Pages should return FALSE
\r
176 void CPropertiesAdvanced::InitCategoryList()
\r
178 int nCategoryIDs[128] = {'\0'};
\r
180 // get all category type
\r
181 m_cCategory.ResetContent();
\r
182 for (int nCommandID = 1; nCommandID < MAX_COMMAND; ++nCommandID) {
\r
183 CString szCommandName = CCommands::GetCommandName(nCommandID);
\r
184 if (szCommandName.IsEmpty()) {
\r
187 int nCategoryID = CCommands::GetCategoryID(nCommandID);
\r
188 if (!nCategoryID) {
\r
192 BOOL bExist = FALSE;
\r
193 for (int i = 0; nCategoryIDs[i]; ++i) {
\r
194 if (nCategoryIDs[i] == nCategoryID) {
\r
199 nCategoryIDs[i] = nCategoryID;
\r
203 // sort category type in combo box
\r
204 int nMinimumID = 0;
\r
205 for (int i = 0; nCategoryIDs[i]; ++i) {
\r
207 for (int j = 0; nCategoryIDs[j]; ++j) {
\r
208 if (nMinimumID < nCategoryIDs[j]
\r
209 && (nID == 0 || nCategoryIDs[j] < nID)) {
\r
210 nID = nCategoryIDs[j];
\r
213 m_cCategory.InsertString(-1, CString(MAKEINTRESOURCE(nID)));
\r
217 if (CDotXkeymacs::GetFunctionNumber()) {
\r
218 m_cCategory.InsertString(-1, CString(MAKEINTRESOURCE(IDS_ORIGINAL)));
\r
221 m_cCategory.SetCurSel(0);
\r
224 void CPropertiesAdvanced::SetCommands()
\r
226 m_cCommands.ResetContent();
\r
228 CString szCategory;
\r
229 m_cCategory.GetLBText(m_cCategory.GetCurSel(), szCategory);
\r
230 if (szCategory.Compare(CString(MAKEINTRESOURCE(IDS_ORIGINAL)))) {
\r
231 for (int nCommandID = 1; nCommandID < MAX_COMMAND; ++nCommandID) {
\r
232 CString szCommandName = CCommands::GetCommandName(nCommandID);
\r
233 if (szCommandName.IsEmpty()) {
\r
237 if (szCategory == CString(MAKEINTRESOURCE(CCommands::GetCategoryID(nCommandID)))) {
\r
238 m_cCommands.AddString(szCommandName);
\r
242 for (int nFunctionID = 0; nFunctionID < CDotXkeymacs::GetFunctionNumber(); ++nFunctionID) {
\r
243 BOOL bOriginal = TRUE;
\r
244 for (int nCommandID = 1; nCommandID < MAX_COMMAND; ++nCommandID) {
\r
245 CString szCommandName = CCommands::GetCommandName(nCommandID);
\r
246 if (szCommandName.IsEmpty()) {
\r
250 if (szCommandName == CDotXkeymacs::GetFunctionSymbol(nFunctionID)) {
\r
251 bOriginal = FALSE; // overwriting build-in keybindings
\r
256 m_cCommands.AddString(CDotXkeymacs::GetFunctionSymbol(nFunctionID));
\r
261 m_cCommands.SetCurSel(0);
\r
265 void CPropertiesAdvanced::SetCurrentKeys()
\r
267 m_cCurrentKeys.ResetContent();
\r
268 m_cRemove.EnableWindow(FALSE);
\r
270 CString szCategory;
\r
271 m_cCategory.GetLBText(m_cCategory.GetCurSel(), szCategory);
\r
273 CString szCurrentCommandName;
\r
274 m_cCommands.GetText(m_cCommands.GetCurSel(), szCurrentCommandName);
\r
276 if (szCategory.Compare(CString(MAKEINTRESOURCE(IDS_ORIGINAL)))) {
\r
277 for (int nCommandType = 0; nCommandType < MAX_COMMAND_TYPE; ++nCommandType) {
\r
278 for (int nKey = 0; nKey < MAX_KEY; ++nKey) {
\r
279 if (CCommands::GetCommandName(CProfile::GetCommandID(m_nApplicationID, nCommandType, nKey)) == szCurrentCommandName) {
\r
281 sz.Format(_T("%s%s"), CProfile::CommandType2String(nCommandType), CProfile::Key2String(nKey));
\r
282 m_cCurrentKeys.AddString(sz);
\r
287 CString szCommandName;
\r
288 for (int nCommandID = 0; nCommandID < MAX_COMMAND; ++nCommandID) {
\r
289 szCommandName = CCommands::GetCommandName(nCommandID);
\r
290 if (szCommandName.IsEmpty()) {
\r
293 if (szCommandName == szCurrentCommandName) {
\r
294 m_nCommandID = nCommandID;
\r
299 m_cDescription.SetWindowText(CString(MAKEINTRESOURCE(CCommands::GetDescriptionID(m_nCommandID))));
\r
301 // overwrite by original command's description if needed
\r
302 for (int nFunctionID = 0; nFunctionID < CDotXkeymacs::GetFunctionNumber(); ++nFunctionID) {
\r
303 if (szCommandName == CDotXkeymacs::GetFunctionSymbol(nFunctionID)) {
\r
304 m_cDescription.SetWindowText(CDotXkeymacs::GetFunctionDefinition(szCurrentCommandName));
\r
309 const int nIndex = CDotXkeymacs::GetIndex(szCurrentCommandName);
\r
310 for (int nKeyID = 0; nKeyID < CDotXkeymacs::GetKeyNumber(nIndex, m_nApplicationID); ++nKeyID) {
\r
311 int nCommandType = 0;
\r
313 CDotXkeymacs::GetKey(nIndex, m_nApplicationID, nKeyID, &nCommandType, &nKey);
\r
316 sz.Format(_T("%s%s"), CProfile::CommandType2String(nCommandType), CProfile::Key2String(nKey));
\r
317 m_cCurrentKeys.AddString(sz);
\r
320 m_cDescription.SetWindowText(CDotXkeymacs::GetFunctionDefinition(szCurrentCommandName));
\r
324 void CPropertiesAdvanced::OnSelchangeCategory()
\r
330 void CPropertiesAdvanced::OnSelchangeCommands()
\r
332 BOOL bEnableWindow = FALSE;
\r
333 CString szCurrentCommandName;
\r
334 m_cCommands.GetText(m_cCommands.GetCurSel(), szCurrentCommandName);
\r
335 if (szCurrentCommandName.CompareNoCase(_T("C-"))
\r
336 && szCurrentCommandName.CompareNoCase(_T("Meta for Alt"))
\r
337 && szCurrentCommandName.CompareNoCase(_T("Pass Through"))) {
\r
338 bEnableWindow = TRUE;
\r
340 GetDlgItem(IDC_C_X)->EnableWindow(bEnableWindow);
\r
341 m_bC = bEnableWindow;
\r
342 m_bM = bEnableWindow;
\r
343 m_bShift = bEnableWindow;
\r
349 void CPropertiesAdvanced::OnSelchangeCurrentKeys()
\r
351 TCHAR szKeyBind[128] = {'\0'};
\r
352 m_cCurrentKeys.GetText(m_cCurrentKeys.GetCurSel(), szKeyBind);
\r
353 CProfile::ReadKeyBind(&m_nRemoveCommandType, &m_nRemoveKey, szKeyBind);
\r
354 m_cRemove.EnableWindow();
\r
357 void CPropertiesAdvanced::OnSetfocusNewKey()
\r
359 m_pNewKey = &m_cNewKey;
\r
360 m_pAssign = &m_cAssign;
\r
361 m_pCurrentlyAssigned = &m_cCurrentlyAssigned;
\r
362 m_pCurrentKeys = &m_cCurrentKeys;
\r
363 m_hKeyboardHook = ::SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)CPropertiesAdvanced::KeyboardProc, NULL, AfxGetApp()->m_nThreadID);
\r
365 m_bC_x = IsDlgButtonChecked(IDC_C_X) && GetDlgItem(IDC_C_X)->IsWindowEnabled();
\r
368 void CPropertiesAdvanced::OnAssign()
\r
370 // Remove Current Setting
\r
371 CProfile::SetCommandID(m_nApplicationID, m_nAssignCommandType, m_nAssignKey, 0);
\r
372 SetCommandID(m_nAssignCommandType, m_nAssignKey, 0);
\r
373 CDotXkeymacs::RemoveKey(m_nApplicationID, m_nAssignCommandType, m_nAssignKey);
\r
375 // Assign New Setting
\r
377 szItem.Format(_T("%s%s"), CProfile::CommandType2String(m_nAssignCommandType), CProfile::Key2String(m_nAssignKey));
\r
378 if (m_cCurrentKeys.FindString(-1, szItem) == LB_ERR) { // This key bind has not assignd to the same command yet.
\r
379 CString szCategory;
\r
380 m_cCategory.GetLBText(m_cCategory.GetCurSel(), szCategory);
\r
382 if (szCategory.Compare(CString(MAKEINTRESOURCE(IDS_ORIGINAL)))) {
\r
383 CProfile::SetCommandID(m_nApplicationID, m_nAssignCommandType, m_nAssignKey, m_nCommandID);
\r
384 SetCommandID(m_nAssignCommandType, m_nAssignKey, m_nCommandID);
\r
386 CString szCurrentCommandName;
\r
387 m_cCommands.GetText(m_cCommands.GetCurSel(), szCurrentCommandName);
\r
388 CDotXkeymacs::SetKey(CDotXkeymacs::GetIndex(szCurrentCommandName), m_nApplicationID, m_nAssignCommandType, m_nAssignKey);
\r
390 m_cCurrentKeys.AddString(szItem);
\r
396 void CPropertiesAdvanced::OnRemove()
\r
398 CString szCategory;
\r
399 m_cCategory.GetLBText(m_cCategory.GetCurSel(), szCategory);
\r
401 if (szCategory.Compare(CString(MAKEINTRESOURCE(IDS_ORIGINAL)))) {
\r
402 CProfile::SetCommandID(m_nApplicationID, m_nRemoveCommandType, m_nRemoveKey, 0);
\r
403 SetCommandID(m_nRemoveCommandType, m_nRemoveKey, 0);
\r
405 CString szCurrentCommandName;
\r
406 m_cCommands.GetText(m_cCommands.GetCurSel(), szCurrentCommandName);
\r
407 CDotXkeymacs::RemoveKey(CDotXkeymacs::GetIndex(szCurrentCommandName), m_nApplicationID, m_nRemoveCommandType, m_nRemoveKey);
\r
410 m_cCurrentKeys.DeleteString(m_cCurrentKeys.GetCurSel());
\r
411 m_cRemove.EnableWindow(FALSE);
\r
412 m_nRemoveCommandType = 0;
\r
416 void CPropertiesAdvanced::OnResetAll()
\r
418 CProfile::LoadData();
\r
423 void CPropertiesAdvanced::ClearNewKey()
\r
425 m_cNewKey.SetWindowText(_T(""));
\r
426 m_cAssign.EnableWindow(FALSE);
\r
427 m_nAssignCommandType = 0;
\r
429 m_cCurrentlyAssigned.SetWindowText(_T(""));
\r
432 LRESULT CALLBACK CPropertiesAdvanced::KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
\r
434 if (code < 0 || code == HC_NOREMOVE) {
\r
435 return ::CallNextHookEx(m_hKeyboardHook, code, wParam, lParam);
\r
438 if (lParam & BEING_RELEASED) { // Key Up
\r
439 } else { // Key Down
\r
442 if (lParam & EXTENDED_KEY) {
\r
443 wParam = VK_RCONTROL;
\r
445 wParam = VK_LCONTROL;
\r
449 if (lParam & EXTENDED_KEY) {
\r
456 if (lParam & EXTENDED_KEY) {
\r
457 wParam = VK_RSHIFT;
\r
459 wParam = VK_LSHIFT;
\r
465 m_nAssignKey = wParam;
\r
472 void CPropertiesAdvanced::SetNewKey()
\r
475 int nCommandType = NONE;
\r
478 szNewKey += _T("Ctrl+X ");
\r
479 nCommandType += CONTROLX;
\r
481 if (IsCtrlDown()) {
\r
482 szNewKey += _T("Ctrl+");
\r
483 nCommandType += CONTROL;
\r
485 if (IsMetaDown()) {
\r
486 szNewKey += _T("Meta+");
\r
487 nCommandType += META;
\r
489 if (IsShiftDown()) {
\r
490 szNewKey += _T("Shift+");
\r
491 nCommandType += SHIFT;
\r
494 m_nAssignCommandType = nCommandType;
\r
496 szNewKey += CProfile::Key2String(m_nAssignKey);
\r
498 m_pNewKey->SetWindowText(szNewKey);
\r
502 BOOL bEnable = TRUE;
\r
504 && m_pCurrentKeys->FindString(-1, szNewKey) != LB_ERR) { // This key bind is already assigned.
\r
507 m_pAssign->EnableWindow(bEnable);
\r
510 if (m_pCurrentlyAssigned) {
\r
511 CString szCurrentlyAssigned(_T("Currently assigned to:\n"));
\r
513 if (m_nCommandIDs[m_nAssignCommandType][m_nAssignKey] || CDotXkeymacs::GetIndex(m_nApplicationID, m_nAssignCommandType, m_nAssignKey) == -1) {
\r
514 szCurrentlyAssigned += CCommands::GetCommandName(m_nCommandIDs[m_nAssignCommandType][m_nAssignKey]);
\r
516 szCurrentlyAssigned += CDotXkeymacs::GetFunctionSymbol(CDotXkeymacs::GetIndex(m_nApplicationID, m_nAssignCommandType, m_nAssignKey));
\r
518 m_pCurrentlyAssigned->SetWindowText(szCurrentlyAssigned);
\r
522 void CPropertiesAdvanced::SetCommandID(int nCommandType, int nKey, int nCommandID)
\r
524 m_nCommandIDs[nCommandType][nKey] = nCommandID;
\r
526 // Set C-x if it is needed.
\r
527 if ((nCommandType & CONTROLX)) {
\r
528 // Get CommandID of C-x.
\r
529 for (int nCommandIDofC_x = 0; nCommandIDofC_x < MAX_COMMAND; ++nCommandIDofC_x) {
\r
530 if (!_tcsicmp(CCommands::GetCommandName(nCommandIDofC_x), _T("C-x"))) {
\r
536 m_nCommandIDs[CONTROL]['X'] = nCommandIDofC_x;
\r
538 for (int i = 0; i < MAX_COMMAND_TYPE; ++i) { // i is command-type.
\r
539 if (!(i & CONTROLX)) {
\r
542 for (int j = 0; j < MAX_KEY; ++j) { // j is key.
\r
543 if (m_nCommandIDs[i][j]) {
\r
544 m_nCommandIDs[CONTROL]['X'] = nCommandIDofC_x; // needless
\r
549 m_nCommandIDs[CONTROL]['X'] = 0;
\r
554 void CPropertiesAdvanced::InitCommandIDs()
\r
556 for (int nCommandType = 0; nCommandType < MAX_COMMAND_TYPE; ++nCommandType) {
\r
557 for (int nKey = 0; nKey < MAX_KEY; ++nKey) {
\r
558 SetCommandID(nCommandType, nKey, CProfile::GetCommandID(m_nApplicationID, nCommandType, nKey));
\r
563 void CPropertiesAdvanced::OnCX()
\r
565 if (m_nAssignKey) {
\r
566 OnSetfocusNewKey();
\r
571 void CPropertiesAdvanced::OnKillfocusNewKey()
\r
573 if (m_hKeyboardHook) {
\r
574 ::UnhookWindowsHookEx(m_hKeyboardHook);
\r
576 m_hKeyboardHook = NULL;
\r
579 void CPropertiesAdvanced::OnEnableCua()
\r
582 CProfile::SetEnableCUA(m_nApplicationID, m_bEnableCUA);
\r
585 BOOL CPropertiesAdvanced::IsCtrlDown()
\r
587 return m_bC && IsFooDown(CString(_T("C-")));
\r
590 BOOL CPropertiesAdvanced::IsMetaDown()
\r
592 return m_bM && IsFooDown(CString(_T("Meta for Alt")));
\r
595 BOOL CPropertiesAdvanced::IsShiftDown()
\r
599 && (GetKeyState(VK_SHIFT ) < 0
\r
600 || GetKeyState(VK_LSHIFT) < 0
\r
601 || GetKeyState(VK_RSHIFT) < 0);
\r
603 // return m_bShift && IsFooDown(CString("Foo"));
\r
606 BOOL CPropertiesAdvanced::IsFooDown(CString szCommandName)
\r
608 for (int nKey = 0; nKey < MAX_KEY; ++nKey) {
\r
609 if (CCommands::GetCommandName(CProfile::GetCommandID(0, NONE, nKey)) == szCommandName) { // FIXME
\r
610 if (GetKeyState(nKey) < 0) {
\r