📄 commandfactory.cpp
字号:
if(TRUE == bAddButtons)
{
//remember the determination as to whether this was a compose or open view
dwCmdUiOptions |= (bOpenMailView?CUIO_OLDMAIL_CONTEXT:CUIO_NEWMAIL_CONTEXT);
//decrypt/verify message
hrRet = InternalAddToolbarItem(pIToolbarItems,"IDB_ALT_DECRYPT",
IDS_HLPCMD_DECRYPT_MSG, dwCmdUiOptions|CUIO_IS_AN_OPERATION,
IDM_DECRYPT_VERIFY);
//encrypt message before sending
hrRet = InternalAddToolbarItem(pIToolbarItems,"IDB_ALT_ENCRYPT",
IDS_HLPCMD_ENCRYPT_ONSEND, dwCmdUiOptions|CUIO_IS_AN_OPTION,
IDM_AUTO_ENCRYPT_ONSEND);
//sign message before sending
hrRet = InternalAddToolbarItem(pIToolbarItems,"IDB_ALT_SIGN",
IDS_HLPCMD_SIGN_ONSEND, dwCmdUiOptions|CUIO_IS_AN_OPTION,
IDM_AUTO_SIGN_ONSEND);
//launch PGP keys
hrRet = InternalAddToolbarItem(pIToolbarItems,"IDB_PGPKEYS",
IDS_HLPCMD_LAUNCH_PGPKEYS, dwCmdUiOptions|CUIO_IS_AN_OPERATION,
IDM_LAUNCH_PGPKEYS);
}
}
cleanup:
if(NULL != pIDisp)
{
pIDisp->Release();
pIDisp=NULL;
}
if(NULL != pIToolbar)
{
pIToolbar->Release();
pIToolbar=NULL;
}
if(NULL != pIToolbarItems)
{
pIToolbarItems->Release();
pIToolbarItems=NULL;
}
return hrRet;
}
//////////////////////////////////////////////////////////////////
// Name:CCommandFactory::Init
//
// Description:This method is the first method of Command::Factory
// called. It is used to tell GroupWise what UI items will be
// modified.
//
// In: long lcid - This is the locale ID of the application driving
// the C3po.
//
// Out: long FAR* plCmdFlags - The C3po server returns any combination
// of the following bits:
//
// eGW_CMDINIT_MENUS The C3po intends to modify the menus
// (CustomizeMenu Interface)
// eGW_CMDINIT_TOOLBARS The C3po intends to modify the toolbar
// (CustomizeMenu Interface)
// eGW_CMDINIT_CONTEXT_MENUS - The C3po intends to modify the
// context menus (CustomizeMenu Interface)
// eGW_CMDINIT_NO_PREDEFINED - Optimization flag that indicates
// the C3po will never respond to predefined
// commands. Returning this flag will supress
// calls to the WantCommand method for predefined
// commands.
// Comments:
////////////////////////////////////////////////////////////////////
STDMETHODIMP CCommandFactory::Init(long lcid, long FAR* plCmdFlags)
{
PgpGwTrace("CCommandFactory::Init\n");
*plCmdFlags = eGW_CMDINIT_MENUS|eGW_CMDINIT_TOOLBARS/*|eGW_CMDINIT_CONTEXT_MENUS*/;
return S_OK;
}
STDMETHODIMP CCommandFactory::InternalAddMenuItem(UINT uiMenuItemStringId,
IGWMenuItems *pigwmParentMenuItems, int iMenuCommandId,
DWORD dwMenuUIOptions, UINT uiMenuItemHlpStringId/*=NULL*/)
{
IDispatch *pIDispTemp=NULL, *pIDispTempTwo=NULL, *pIDispTempThree=NULL;
CGWCommand *pcgwCmdTemp=NULL;
char *pszMenuItemString=NULL;
char *pszMenuItemHelpString=NULL;
BSTR bstrCaption=NULL;
HRESULT hrRet=S_OK;
BOOL bRet=TRUE;
VARIANT vTmp={0};
//check parameters
pgpAssert(0 != uiMenuItemStringId);//menu string id cannot be zero
pgpAssert(NULL != pigwmParentMenuItems);//the parent interface must be passed
if((0 == uiMenuItemStringId) || (NULL == pigwmParentMenuItems))
{
hrRet=E_INVALIDARG;//bad parameters passed to us
goto cleanup;
}
//get the menu string from the string table
bRet=SafeLoadString(uiMenuItemStringId, &pszMenuItemString);
pgpAssert((TRUE == bRet) && (NULL != pszMenuItemString));
if(FALSE == bRet)//if we could not get the string
{
hrRet=E_FAIL;//may be due to low memory or bad resource id passed
goto cleanup;
}
//if a menu help string resource id was passed to us
if(0 != uiMenuItemHlpStringId)
{
//get the menu help string from string table
bRet=SafeLoadString(uiMenuItemHlpStringId, &pszMenuItemHelpString);
pgpAssert((TRUE == bRet) && (NULL != pszMenuItemHelpString));
if(FALSE == bRet)//if we could not get the menu help string
{
hrRet=E_FAIL;//low memory or bad resource id ??
goto cleanup;
}
}
try
{
//create new GWCommand object for menu item
pcgwCmdTemp = new CGWCommand(iMenuCommandId, dwMenuUIOptions);
}
catch(...)
{
hrRet=E_OUTOFMEMORY;
goto cleanup;
}
if(NULL == pcgwCmdTemp)
{
hrRet=E_OUTOFMEMORY;
goto cleanup;
}
//request the dispatch interface of the command object
pgpAssert(NULL == pIDispTemp);
hrRet = pcgwCmdTemp->QueryInterface(IID_IDispatch, (void**)&pIDispTemp);
pgpAssert(SUCCEEDED(hrRet) && (NULL != pIDispTemp));
if(FAILED(hrRet) || (NULL == pIDispTemp))
goto cleanup;
//prepare the menu text
pgpAssert(NULL == bstrCaption);
bstrCaption = A2WBSTR(pszMenuItemString);
pgpAssert(NULL != bstrCaption);
if(NULL == bstrCaption)
{
hrRet=E_OUTOFMEMORY;
goto cleanup;
}
//set long prompt for menu item. if the help string was passed
//blank we set the help string same as the menu string
pcgwCmdTemp->m_bstrLongPrompt = (NULL != pszMenuItemHelpString)?
A2WBSTR(pszMenuItemHelpString):A2WBSTR(pszMenuItemString);
pgpAssert(NULL != pcgwCmdTemp->m_bstrLongPrompt);
if(NULL == pcgwCmdTemp->m_bstrLongPrompt)
{
hrRet=E_OUTOFMEMORY;
goto cleanup;
}
//add the menu item. we do this at the absolute end for the cleanup
//code to be simpler - we dont have to remove the menu for any reason
pgpAssert(NULL != pIDispTemp);
pgpAssert(NULL == pIDispTempTwo);
hrRet = pigwmParentMenuItems->Add(bstrCaption, pIDispTemp, vTmp, &pIDispTempTwo);
pgpAssert(SUCCEEDED(hrRet) && (NULL != pIDispTempTwo));
if(FAILED(hrRet) || (NULL == pIDispTempTwo))
goto cleanup;
//add a separator before this item if we are called upon to do so
if(dwMenuUIOptions & CUIO_PREPEND_MENU_SEPARATOR)
{
pgpAssert(NULL != pIDispTempTwo);
pgpAssert(NULL == pIDispTempThree);
pgpAssert(VT_EMPTY == vTmp.vt);
vTmp.vt = VT_BSTR;
V_BSTR(&vTmp)=A2WBSTR(pszMenuItemString);
hrRet = pigwmParentMenuItems->AddSeparator(vTmp, &pIDispTempThree);
pgpAssert(SUCCEEDED(hrRet) && (NULL != pIDispTempThree));
if(FAILED(hrRet) || (NULL == pIDispTempThree))
goto cleanup;
}
cleanup:
if(NULL != pszMenuItemString)
{
free(pszMenuItemString);
pszMenuItemString=NULL;
}
if(NULL != pszMenuItemHelpString)
{
free(pszMenuItemHelpString);
pszMenuItemHelpString=NULL;
}
if(NULL != bstrCaption)
{
SysFreeString(bstrCaption);
bstrCaption=NULL;
}
if(NULL != pIDispTemp)
{
pIDispTemp->Release();
pIDispTemp=NULL;
}
if(NULL != pIDispTempTwo)
{
pIDispTempTwo->Release();
pIDispTempTwo=NULL;
}
if(NULL != pIDispTempThree)
{
pIDispTempThree->Release();
pIDispTempThree=NULL;
}
if((NULL != vTmp.bstrVal) && (VT_BSTR == vTmp.vt))
{
SysFreeString(vTmp.bstrVal);
vTmp.bstrVal=NULL;
vTmp.vt=VT_EMPTY;
}
if(FAILED(hrRet))
{
if(NULL != pcgwCmdTemp)
{
pcgwCmdTemp->Release();
pcgwCmdTemp=NULL;
}
}
return hrRet;
}
STDMETHODIMP CCommandFactory::InternalAddToolbarItem(IGWToolbarItems *pigwtToolBarItems,
char *pszIconIdString, UINT uiToolHlpStrId/*char *pszToolHelpString*/, DWORD dwToolUIOptions,
int iToolCommandId)
{
BSTR bstrItemHelpString=NULL, bstrFilename=NULL, bstrIconId=NULL;
CGWCommand *pcgwCmdTemp=NULL;
IGWToolbarItem *pIToolbarItem=NULL;
IDispatch *pIDisp=NULL, *pIDispCmd=NULL;
VARIANT vrTmp={0};
char *pszIconFileName=NULL;
char *pszToolHelpString=NULL;
HRESULT hrRet=S_OK;
BOOL bRet=TRUE;
DWORD dwRet=0;
//check parameters passed to us
pgpAssert(NULL != pigwtToolBarItems);//must have the toolbaritems iface passed to us
pgpAssert(NULL != pszIconIdString);//must have the icon resource string id
pgpAssert(0 != uiToolHlpStrId);
if((NULL == pigwtToolBarItems) ||
(NULL == pszIconIdString) || (0 == uiToolHlpStrId))
{
hrRet=E_INVALIDARG;
goto cleanup;
}
//if tool help string id was passed to us
if(0 != uiToolHlpStrId)
{
//get the toolbar item help string from string table
bRet=SafeLoadString(uiToolHlpStrId, &pszToolHelpString);
pgpAssert((TRUE == bRet) && (NULL != pszToolHelpString));
if(FALSE == bRet)//if we could not get the tool help string
{
hrRet=E_FAIL;//low memory or bad resource id ??
goto cleanup;
}
}
//add to Toolbar a new button
bstrItemHelpString = A2WBSTR(pszToolHelpString);
pgpAssert(NULL != bstrItemHelpString);
if(NULL == bstrItemHelpString)
{
hrRet=E_OUTOFMEMORY;
goto cleanup;
}
//create new GWCommand
try
{
pcgwCmdTemp = new CGWCommand(iToolCommandId, dwToolUIOptions);
}
catch(...)
{
pgpAssert(FALSE);
hrRet=E_OUTOFMEMORY;
}
pgpAssert(NULL != pcgwCmdTemp);
if(NULL == pcgwCmdTemp)
{
hrRet=E_OUTOFMEMORY;
goto cleanup;
}
//request dispatch interface to the command object
pgpAssert(NULL == pIDispCmd);
hrRet = pcgwCmdTemp->QueryInterface(IID_IDispatch, (void**)&pIDispCmd);
pgpAssert(SUCCEEDED(hrRet) && (NULL != pIDispCmd));
if(FAILED(hrRet) || (NULL == pIDispCmd))
goto cleanup;
VariantInit(&vrTmp);//we want the toolbar button at the end
//add the toolbar item
pgpAssert(NULL == pIDisp);
hrRet = pigwtToolBarItems->Add(bstrItemHelpString, pIDispCmd, vrTmp, &pIDisp);
pgpAssert(SUCCEEDED(hrRet) && (NULL != pIDisp));
if(FAILED(hrRet) || (NULL == pIDisp))
goto cleanup;
if(NULL != bstrItemHelpString)
{
SysFreeString(bstrItemHelpString);
bstrItemHelpString=NULL;
}
//get the icon file name
pszIconFileName = GetIconsFilePath();
pgpAssert(NULL != pszIconFileName);
if(NULL == pszIconFileName)
{
hrRet=E_OUTOFMEMORY;
goto cleanup;
}
bstrFilename = A2WBSTR(pszIconFileName);
pgpAssert(NULL != bstrFilename);
if(NULL == bstrFilename)
{
hrRet=E_OUTOFMEMORY;
goto cleanup;
}
//prepare name of bitmap resource
bstrIconId = A2WBSTR(pszIconIdString);
pgpAssert(NULL != bstrIconId);
if(NULL == bstrIconId)
{
hrRet=E_OUTOFMEMORY;
goto cleanup;
}
//get GWToolbarItem iface of the button we just added
pgpAssert(NULL == pIToolbarItem);
hrRet = pIDisp->QueryInterface(IID_IGWToolbarItem, (void**)&pIToolbarItem);
pgpAssert(SUCCEEDED(hrRet) && (NULL != pIToolbarItem));
//if we got the interface to the toolbar item
if(SUCCEEDED(hrRet) && (NULL != pIToolbarItem))
{
//set bitmap of the toolbar item
VariantInit(&vrTmp);
vrTmp.vt = VT_BSTR;
V_BSTR(&vrTmp)=bstrIconId;
hrRet = pIToolbarItem->SetBitmap(bstrFilename, vrTmp);
pgpAssert(SUCCEEDED(hrRet));
}
//NOTE: we did not do cleanup under failure condition of not being able
//to get the interface to the item we just now added. we tolerate failures
//after this point to keep the cleanup much simpler(ssd)
hrRet=S_OK;
//set the tooltip for the toolbar item
pcgwCmdTemp->m_bstrToolTip = A2WBSTR(pszToolHelpString);
pgpAssert(NULL != pcgwCmdTemp->m_bstrToolTip);
cleanup:
if(NULL != pszToolHelpString)
{
free(pszToolHelpString);
pszToolHelpString=NULL;
}
if(NULL != bstrIconId)
{
SysFreeString(bstrIconId);
bstrIconId=NULL;
}
if(NULL != pszIconFileName)
{
free(pszIconFileName);
pszIconFileName=NULL;
}
if(NULL != bstrFilename)
{
SysFreeString(bstrFilename);
bstrFilename=NULL;
}
if(NULL != bstrItemHelpString)
{
SysFreeString(bstrItemHelpString);
bstrItemHelpString=NULL;
}
if(NULL != pIToolbarItem)
{
pIToolbarItem->Release();
pIToolbarItem=NULL;
}
if(NULL != pIDispCmd)
{
pIDispCmd->Release();
pIDispCmd=NULL;
}
if(NULL != pIDisp)
{
pIDisp->Release();
pIDisp=NULL;
}
if(NULL != pcgwCmdTemp)
{
pcgwCmdTemp->Release();
pcgwCmdTemp=NULL;
}
return hrRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -