📄 submenu.cpp
字号:
/*********************************************************************************
SubMenu.cpp
-----------
author : Tamas Bara
copyright : (C) 2002-2004 by SnoozeSoft
email : snoozesoft@compuserve.de
*********************************************************************************/
/*********************************************************************************
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
* *
*********************************************************************************/
#include "StdAfx.h"
#include "systraymanager.h"
SubMenu::SubMenu( UINT id, // menu id
jobject jObject ) // java menu object reference
{
m_id = id;
m_jobject = jObject;
m_hMenu = CreatePopupMenu();
}
SubMenu::~SubMenu()
{
DestroyMenu( m_hMenu );
}
void SubMenu::addItem( int id, LPCWSTR label, bool checkable, bool checked, bool enabled )
{
SubMenu* psMenu = 0;
wstring temp = label;
// check whether the label refers to a submenu
if( temp.find( L"#SUB<" ) == 0
&& temp.find( L"><" ) != temp.npos
&& temp.rfind( L">" ) == temp.length() - 1 )
{
// submenu reference string format:
// #SUB<id><label>
// extract the id of the submenu
int begin = temp.find( L"<" ) + 1;
wstring sid = temp.substr( begin, temp.find( L">" ) - begin );
// extract the label of the submenu
begin = temp.find( L"><" ) + 2;
temp = temp.substr( begin, temp.size() - begin - 1 );
int menuId;
swscanf( sid.c_str(), L"%d", &menuId );
psMenu = SysTrayManager::findSubMenu( menuId );
}
MENUITEMINFO miInfo;
miInfo.cbSize = sizeof( MENUITEMINFO );
miInfo.fMask = MIIM_TYPE;
miInfo.fType = wcscmp( label, L"#SEP" ) ? MFT_STRING : MFT_SEPARATOR;
miInfo.dwTypeData = psMenu ? wcsdup( temp.c_str() ) : wcsdup( label );
miInfo.cch = psMenu ? wcslen( temp.c_str() ) : wcslen( label );
UINT pos = GetMenuItemCount( m_hMenu ) - id;
InsertMenuItem( m_hMenu, pos, TRUE, &miInfo );
UINT newId = id + m_id * ( SysTrayManager::SUB_ID_BEGIN - 1 );
for( int i = pos; i > -1; i-- )
{
miInfo.fMask = MIIM_ID;
miInfo.wID = newId++;
SetMenuItemInfo( m_hMenu, i, TRUE, &miInfo );
}
if( psMenu )
{
miInfo.fMask = MIIM_SUBMENU;
miInfo.hSubMenu = psMenu->getMenuHandle();
SetMenuItemInfo( m_hMenu, 0, TRUE, &miInfo );
}
if( checkable )
{
m_checkableItems.push_back( id );
if( checked ) checkItem( id, checked );
}
if( !enabled ) enableItem( id, enabled );
}
void SubMenu::enableItem( int id, bool enable )
{
UINT uEnable = enable ? ( MF_BYCOMMAND | MF_ENABLED ) : ( MF_BYCOMMAND | MF_GRAYED );
EnableMenuItem( m_hMenu, m_id * ( SysTrayManager::SUB_ID_BEGIN - 1 ) + id, uEnable );
}
void SubMenu::toggleCheckForItem( int id )
{
// check whether the specified item is checkable
list< int >::iterator iter = m_checkableItems.begin();
while( iter != m_checkableItems.end() )
{
if( *iter == id ) break;
iter++;
}
if( iter != m_checkableItems.end() )
{
// toggle check mark
int realId = m_id * ( SysTrayManager::SUB_ID_BEGIN - 1 ) + id;
UINT uCheck = GetMenuState( m_hMenu, realId, MF_BYCOMMAND );
uCheck = ( uCheck & MF_CHECKED ) ? MF_UNCHECKED : MF_CHECKED;
CheckMenuItem( m_hMenu, realId, uCheck );
}
}
void SubMenu::checkItem( int id, bool check )
{
UINT uCheck = check ? MF_CHECKED : MF_UNCHECKED;
CheckMenuItem( m_hMenu, m_id * ( SysTrayManager::SUB_ID_BEGIN - 1 ) + id, uCheck );
}
void SubMenu::setItemLabel( int id, LPCWSTR label )
{
MENUITEMINFO miInfo;
miInfo.cbSize = sizeof( MENUITEMINFO );
miInfo.fMask = MIIM_TYPE;
miInfo.fType = MFT_STRING;
miInfo.dwTypeData = wcsdup( label );
miInfo.cch = wcslen( label );
SetMenuItemInfo( m_hMenu, m_id * ( SysTrayManager::SUB_ID_BEGIN - 1 ) + id, FALSE, &miInfo );
}
void SubMenu::removeItem( int id )
{
MENUITEMINFO miInfo;
miInfo.cbSize = sizeof( MENUITEMINFO );
RemoveMenu( m_hMenu, m_id * ( SysTrayManager::SUB_ID_BEGIN - 1 ) + id, MF_BYCOMMAND );
UINT newId = m_id * ( SysTrayManager::SUB_ID_BEGIN - 1 ) + id;
UINT pos = GetMenuItemCount( m_hMenu ) - id - 1;
for( int i = pos; i > -1; i-- )
{
miInfo.fMask = MIIM_ID;
miInfo.wID = newId++;
SetMenuItemInfo( m_hMenu, i, TRUE, &miInfo );
}
}
void SubMenu::removeAll()
{
MENUITEMINFO miInfo;
miInfo.cbSize = sizeof( MENUITEMINFO );
int count = GetMenuItemCount( m_hMenu );
for( int i = 0; i < count; i++ )
RemoveMenu( m_hMenu, 0, MF_BYPOSITION );
m_checkableItems.clear();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -