Beispiel #1
0
void wxMenuCmd::Update()
{
	wxString str = m_pItem->GetItemLabelText();

#ifdef __WXGTK__
	// on GTK, an optimization in wxMenu::SetItemLabel checks
	// if the new label is identic to the old and in this
	// case, it returns without doing nothing... :-(
	// to solve the problem, a space is added or removed 
	// from the label to ovverride this optimization check
	str.Trim();
	if (str == m_pItem->GetItemLabelText())
		str += wxT(" ");
#endif

	if (m_nShortcuts <= 0) {
	
		wxKBLogDebug(wxT("wxMenuCmd::Update - no shortcuts defined for [%s]"), str.c_str());

		// no more shortcuts for this menuitem: SetItemLabel()
		// will delete the hotkeys associated...
		m_pItem->SetItemLabel(str);
		return;
	}

	wxString newtext = str+wxT("\t")+GetShortcut(0)->GetStr();
	wxKBLogDebug(wxT("wxMenuCmd::Update - setting the new text to [%s]"), newtext.c_str());
	
#if defined( __WXMSW__ )

	// change the accelerator...
	m_pItem->SetItemLabel(newtext);
	m_pItem->GetMenu()->UpdateAccel(m_pItem);

	// we could also do that in this way:
	//    wxAcceleratorEntry acc = GetAccelerator(0);
	//    m_pItem->SetAccel(&acc);
	// but this is just slower because wxMenuItem::SetAccel
	// creates a string from the accelerator entry we give to it
	// (i.e. it internally builds a string analogue to our 'newtext')
	// and then calls wxMenuItem::SetItemLabel...

#elif defined( __WXGTK__ )

	// on GTK, the SetAccel() function doesn't have any effect...	   
	m_pItem->SetItemLabel(newtext);

#ifdef __WXGTK20__

	//   gtk_menu_item_set_accel_path(GTK_MENU_ITEM(m_pItem), wxGTK_CONV(newtext));

#endif
#endif
}
UINT CShortcutManager::ProcessMessage(const MSG* pMsg, DWORD* pShortcut) const
{
	// 只处理可用的快捷键
	if (!IsWindowEnabled() || !IsWindowVisible())
	{
		return FALSE;
	}

	// 只处理键盘消息
	if (pMsg->message != WM_KEYDOWN && pMsg->message != WM_SYSKEYDOWN)
	{
		return FALSE;
	}

	CWnd* pWnd = CWnd::FromHandle(pMsg->hwnd);

	CWnd* pMainWnd = GetCWnd();
	CWnd* pTopParent = pWnd->GetParentOwner();

	if (pTopParent != pMainWnd)
	{
		return FALSE;
	}

	switch (pMsg->wParam)
	{
	case VK_CONTROL:
	case VK_SHIFT:
	case VK_MENU:
	case VK_NUMLOCK:
	case VK_SCROLL:
	case VK_CAPITAL:
		return FALSE;

		// 不去处理 return/cancel 键
	case VK_RETURN:
	case VK_CANCEL:
		return FALSE;

	case VK_MBUTTON:
		break;

		// 快捷键
	default: 
		{
			//不去处理发往hotkey控件的消息
			if (IsClass(pMsg->hwnd, WC_HOTKEY))
			{
				return FALSE;
			}

			// get 获取快捷方式DWORD值
			BOOL bExtKey = (pMsg->lParam & 0x01000000);
			DWORD dwShortcut = GetShortcut((WORD)pMsg->wParam, bExtKey);

			// 查找相应的命令ID
			UINT nCmdID = 0;

			if (!m_mapShortcut2ID.Lookup(dwShortcut, nCmdID) || !nCmdID)
			{
				return FALSE;
			}

			if (m_wInvalidComb & HKCOMB_EDITCTRLS)
			{
				if (IsEditControl(pMsg->hwnd))
				{
					if (IsEditShortcut(dwShortcut))
					{
						return FALSE;
					}

					WORD wModifiers = HIWORD(dwShortcut);

					if (pMsg->wParam >= VK_F1 && pMsg->wParam <= VK_F24)
					{
						// ok
					}
					// 3. else must have <ctrl> or <alt>
					else
					{
						if (!(wModifiers & (HOTKEYF_ALT | HOTKEYF_CONTROL)))
						{
							return FALSE;
						}
					}
				}
			}

			// 返回 command ID
			if (m_bAutoSendCmds)
			{
				SendMessage(NULL, WM_COMMAND, nCmdID, 0);
			}

			if (pShortcut)
			{
				*pShortcut = dwShortcut;
			}

			return nCmdID;
		}
	}

	return FALSE;
}