00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Application.h"
00025 #include "ComMenu.h"
00026
00027 #ifdef _DEBUG
00028 #define new DEBUG_NEW
00029 #undef THIS_FILE
00030 static char THIS_FILE[] = __FILE__;
00031 #endif
00032
00033 BEGIN_MESSAGE_MAP(CComMenu, CCmdTarget)
00034
00035
00036 END_MESSAGE_MAP()
00037
00038 BEGIN_INTERFACE_MAP(CComMenu, CComObject)
00039 INTERFACE_PART(CComMenu, IID_ISMenu, SMenu)
00040 INTERFACE_PART(CComMenu, IID_IEnumVARIANT, EnumVARIANT)
00041 END_INTERFACE_MAP()
00042
00043
00045
00046
00047 CComMenu::CComMenu(HMENU hMenu, UINT nPosition)
00048 {
00049 if ( nPosition == 0xFFFFFFFF )
00050 {
00051 m_hParent = NULL;
00052 m_hMenu = hMenu;
00053 m_nPosition = 0;
00054 }
00055 else
00056 {
00057 m_hParent = hMenu;
00058 m_hMenu = GetSubMenu( hMenu, nPosition );
00059 m_nPosition = nPosition;
00060 }
00061
00062 EnableDispatch( IID_ISMenu );
00063 }
00064
00065 CComMenu::~CComMenu()
00066 {
00067 }
00068
00070
00071
00072 ISMenu* CComMenu::Wrap(HMENU hMenu, UINT nPosition)
00073 {
00074 CComMenu* pWrap = new CComMenu( hMenu, nPosition );
00075 return (ISMenu*)pWrap->GetInterface( IID_ISMenu, FALSE );
00076 }
00077
00079
00080
00081 IMPLEMENT_DISPATCH(CComMenu, SMenu)
00082
00083 STDMETHODIMP CComMenu::XSMenu::get_Application(IApplication FAR* FAR* ppApplication)
00084 {
00085 METHOD_PROLOGUE( CComMenu, SMenu )
00086 *ppApplication = Application.GetApp();
00087 return S_OK;
00088 }
00089
00090 STDMETHODIMP CComMenu::XSMenu::get_UserInterface(IUserInterface FAR* FAR* ppUserInterface)
00091 {
00092 METHOD_PROLOGUE( CComMenu, SMenu )
00093 *ppUserInterface = Application.GetUI();
00094 return S_OK;
00095 }
00096
00097 STDMETHODIMP CComMenu::XSMenu::get__NewEnum(IUnknown FAR* FAR* ppEnum)
00098 {
00099 METHOD_PROLOGUE( CComMenu, SMenu )
00100 if ( pThis->m_hMenu == NULL ) return E_FAIL;
00101 AddRef();
00102 *ppEnum = &pThis->m_xEnumVARIANT;
00103 pThis->m_xEnumVARIANT.m_nIndex = 0;
00104 return S_OK;
00105 }
00106
00107 STDMETHODIMP CComMenu::XSMenu::get_Item(VARIANT vIndex, ISMenu FAR* FAR* ppMenu)
00108 {
00109 METHOD_PROLOGUE( CComMenu, SMenu )
00110 if ( pThis->m_hMenu == NULL ) return E_FAIL;
00111
00112 VARIANT va;
00113 VariantInit( &va );
00114
00115 if ( FAILED( VariantChangeType( &va, (VARIANT FAR*)&vIndex, 0, VT_I4 ) ) )
00116 {
00117 *ppMenu = NULL;
00118 return S_OK;
00119 }
00120
00121 if ( va.lVal < 0 || va.lVal >= GetMenuItemCount( pThis->m_hMenu ) )
00122 {
00123 *ppMenu = NULL;
00124 return S_OK;
00125 }
00126
00127 *ppMenu = CComMenu::Wrap( pThis->m_hMenu, (UINT)va.lVal );
00128
00129 return S_OK;
00130 }
00131
00132 STDMETHODIMP CComMenu::XSMenu::get_Count(LONG FAR* pnCount)
00133 {
00134 METHOD_PROLOGUE( CComMenu, SMenu )
00135 if ( pThis->m_hMenu == NULL ) return E_FAIL;
00136 *pnCount = GetMenuItemCount( pThis->m_hMenu );
00137 return S_OK;
00138 }
00139
00140 STDMETHODIMP CComMenu::XSMenu::get_ItemType(SMenuType FAR* pnType)
00141 {
00142 METHOD_PROLOGUE( CComMenu, SMenu )
00143
00144 if ( pThis->m_hMenu )
00145 {
00146 *pnType = mnuMenu;
00147 }
00148 else
00149 {
00150 *pnType = ( GetMenuItemID( pThis->m_hParent, pThis->m_nPosition ) == ID_SEPARATOR )
00151 ? mnuSeparator : mnuCommand;
00152 }
00153
00154 return S_OK;
00155 }
00156
00157 STDMETHODIMP CComMenu::XSMenu::get_CommandID(LONG FAR* pnCommandID)
00158 {
00159 METHOD_PROLOGUE( CComMenu, SMenu )
00160 if ( pThis->m_hMenu != NULL ) return E_FAIL;
00161 *pnCommandID = (LONG)GetMenuItemID( pThis->m_hParent, pThis->m_nPosition );
00162 return S_OK;
00163 }
00164
00165 STDMETHODIMP CComMenu::XSMenu::put_CommandID(LONG nCommandID)
00166 {
00167 METHOD_PROLOGUE( CComMenu, SMenu )
00168 if ( pThis->m_hMenu != NULL ) return E_FAIL;
00169
00170 MENUITEMINFO pItem;
00171 ZeroMemory( &pItem, sizeof(pItem) );
00172 pItem.cbSize = sizeof(pItem);
00173 pItem.fMask = MIIM_ID;
00174 pItem.wID = (WORD)nCommandID;
00175
00176 SetMenuItemInfo( pThis->m_hParent, pThis->m_nPosition, TRUE, &pItem );
00177
00178 return S_OK;
00179 }
00180
00181 STDMETHODIMP CComMenu::XSMenu::get_Text(BSTR FAR* psText)
00182 {
00183 METHOD_PROLOGUE( CComMenu, SMenu )
00184 if ( pThis->m_hParent == NULL ) return E_FAIL;
00185
00186 CString str;
00187 GetMenuString( pThis->m_hParent, pThis->m_nPosition,
00188 str.GetBuffer( 256 ), 256, MF_BYPOSITION );
00189 str.ReleaseBuffer();
00190 str.SetSysString( psText );
00191
00192 return S_OK;
00193 }
00194
00195 STDMETHODIMP CComMenu::XSMenu::put_Text(BSTR sText)
00196 {
00197 METHOD_PROLOGUE( CComMenu, SMenu )
00198 if ( pThis->m_hParent == NULL ) return E_FAIL;
00199
00200 CString strText( sText );
00201 MENUITEMINFO pItem;
00202
00203 ZeroMemory( &pItem, sizeof(pItem) );
00204 pItem.cbSize = sizeof(pItem);
00205 pItem.fMask = MIIM_TYPE;
00206 pItem.fType = MFT_STRING;
00207 pItem.dwTypeData = (LPTSTR)(LPCTSTR)strText;
00208
00209 SetMenuItemInfo( pThis->m_hParent, pThis->m_nPosition, TRUE, &pItem );
00210
00211 return S_OK;
00212 }
00213
00214 STDMETHODIMP CComMenu::XSMenu::get_HotKey(BSTR FAR* psText)
00215 {
00216 METHOD_PROLOGUE( CComMenu, SMenu )
00217 return E_NOTIMPL;
00218 }
00219
00220 STDMETHODIMP CComMenu::XSMenu::put_HotKey(BSTR sText)
00221 {
00222 METHOD_PROLOGUE( CComMenu, SMenu )
00223 return E_NOTIMPL;
00224 }
00225
00226 STDMETHODIMP CComMenu::XSMenu::Remove()
00227 {
00228 METHOD_PROLOGUE( CComMenu, SMenu )
00229 if ( pThis->m_hParent == NULL ) return E_FAIL;
00230 RemoveMenu( pThis->m_hParent, pThis->m_nPosition, MF_BYPOSITION );
00231 pThis->m_hParent = pThis->m_hMenu = NULL;
00232 return S_OK;
00233 }
00234
00235 STDMETHODIMP CComMenu::XSMenu::InsertSeparator(LONG nPosition)
00236 {
00237 METHOD_PROLOGUE( CComMenu, SMenu )
00238
00239 if ( pThis->m_hMenu == NULL ) return E_FAIL;
00240 if ( nPosition == -1 ) nPosition = GetMenuItemCount( pThis->m_hMenu );
00241 ::InsertMenu( pThis->m_hMenu, (UINT)nPosition, MF_BYPOSITION|MF_SEPARATOR,
00242 ID_SEPARATOR, NULL );
00243
00244 return S_OK;
00245 }
00246
00247 STDMETHODIMP CComMenu::XSMenu::InsertMenu(LONG nPosition, BSTR sText, ISMenu FAR* FAR* ppMenu)
00248 {
00249 METHOD_PROLOGUE( CComMenu, SMenu )
00250
00251 if ( pThis->m_hMenu == NULL ) return E_FAIL;
00252 if ( nPosition == -1 ) nPosition = GetMenuItemCount( pThis->m_hMenu );
00253
00254 ::InsertMenu( pThis->m_hMenu, (UINT)nPosition, MF_BYPOSITION|MF_POPUP,
00255 (UINT)CreatePopupMenu(), CString( sText ) );
00256
00257 if ( ppMenu ) *ppMenu = CComMenu::Wrap( pThis->m_hMenu, nPosition );
00258
00259 return S_OK;
00260 }
00261
00262 STDMETHODIMP CComMenu::XSMenu::InsertCommand(LONG nPosition, LONG nCommandID, BSTR sText, ISMenu FAR* FAR* ppMenu)
00263 {
00264 METHOD_PROLOGUE( CComMenu, SMenu )
00265
00266 if ( pThis->m_hMenu == NULL ) return E_FAIL;
00267 if ( nPosition == -1 ) nPosition = GetMenuItemCount( pThis->m_hMenu );
00268
00269 ::InsertMenu( pThis->m_hMenu, (UINT)nPosition, MF_BYPOSITION|MF_STRING,
00270 (UINT)nCommandID, CString( sText ) );
00271
00272 if ( ppMenu ) *ppMenu = CComMenu::Wrap( pThis->m_hMenu, nPosition );
00273
00274 return S_OK;
00275 }
00276
00278
00279
00280 IMPLEMENT_UNKNOWN( CComMenu, EnumVARIANT )
00281
00282 STDMETHODIMP CComMenu::XEnumVARIANT::Next(ULONG celt, VARIANT FAR* rgvar, ULONG FAR* pceltFetched)
00283 {
00284 METHOD_PROLOGUE( CComMenu, EnumVARIANT )
00285
00286 if ( pceltFetched ) *pceltFetched = 0;
00287 else if ( celt > 1 ) return E_INVALIDARG;
00288
00289 VariantInit( &rgvar[0] );
00290
00291 if ( m_nIndex >= (UINT)GetMenuItemCount( pThis->m_hMenu ) ) return S_FALSE;
00292
00293 rgvar[0].vt = VT_DISPATCH;
00294 rgvar[0].pdispVal = (IDispatch*)CComMenu::Wrap( pThis->m_hMenu, m_nIndex );
00295
00296 m_nIndex++;
00297 if ( pceltFetched ) (*pceltFetched)++;
00298
00299 return S_OK;
00300 }
00301
00302 STDMETHODIMP CComMenu::XEnumVARIANT::Skip(ULONG celt)
00303 {
00304 METHOD_PROLOGUE( CComMenu, EnumVARIANT )
00305
00306 int nCount = GetMenuItemCount( pThis->m_hMenu );
00307
00308 while ( celt-- && m_nIndex++ < (UINT)nCount );
00309
00310 return ( celt == 0 ? S_OK : S_FALSE );
00311 }
00312
00313 STDMETHODIMP CComMenu::XEnumVARIANT::Reset()
00314 {
00315 METHOD_PROLOGUE( CComMenu, EnumVARIANT )
00316 m_nIndex = 0;
00317 return S_OK;
00318 }
00319
00320 STDMETHODIMP CComMenu::XEnumVARIANT::Clone(IEnumVARIANT FAR* FAR* ppenum)
00321 {
00322 METHOD_PROLOGUE( CComMenu, EnumVARIANT )
00323 return E_NOTIMPL;
00324 }