Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1601 lines
43 KiB

  1. #include "shellprv.h"
  2. #include "clsobj.h"
  3. #include "basebar.h"
  4. #include "bands.h"
  5. #include "menubar.h"
  6. #include "menuband.h"
  7. #include "isfband.h"
  8. #include "util.h"
  9. #include "apithk.h"
  10. #undef WINEVENT_VALID //It's tripping on this...
  11. #include "winable.h"
  12. #include "oleacc.h"
  13. #ifdef UNIX
  14. #include "unixstuff.h"
  15. #endif
  16. #define THISCLASS CMenuDeskBar
  17. #define SUPERCLASS CBaseBar
  18. // Don't fade the menu if it's larger than this magical number. Based on experiments
  19. // on a Pentium II - 233
  20. #define MAGICAL_NO_FADE_HEIGHT 600
  21. // For TraceMsg
  22. #define DM_POPUP DM_TRACE
  23. #define UP 0
  24. #define DOWN 1
  25. #define LEFT 2
  26. #define RIGHT 3
  27. #ifdef ENABLE_CHANNELS
  28. IDeskBand * ChannelBand_Create(LPCITEMIDLIST pidl);
  29. #endif // ENABLE_CHANNELS
  30. STDAPI CMenuDeskBar_CreateInstance(IUnknown* pUnkOuter, REFIID riid, void** ppv)
  31. {
  32. // aggregation checking is handled in class factory
  33. HRESULT hr = E_OUTOFMEMORY;
  34. CMenuDeskBar *pwbar = new CMenuDeskBar();
  35. if (pwbar)
  36. {
  37. hr = pwbar->QueryInterface(riid, ppv);
  38. pwbar->Release();
  39. }
  40. return hr;
  41. }
  42. CMenuDeskBar::CMenuDeskBar() : SUPERCLASS()
  43. {
  44. _dwMode = DBIF_VIEWMODE_VERTICAL;
  45. _iIconSize = BMICON_SMALL;
  46. }
  47. CMenuDeskBar::~CMenuDeskBar()
  48. {
  49. SetSite(NULL);
  50. }
  51. STDMETHODIMP CMenuDeskBar::QueryInterface(REFIID riid, void **ppvObj)
  52. {
  53. HRESULT hres;
  54. static const QITAB qit[] = {
  55. QITABENT(THISCLASS, IMenuPopup),
  56. QITABENT(THISCLASS, IObjectWithSite),
  57. QITABENT(THISCLASS, IBanneredBar),
  58. QITABENT(THISCLASS, IInitializeObject),
  59. { 0 },
  60. };
  61. hres = QISearch(this, (LPCQITAB)qit, riid, ppvObj);
  62. if (FAILED(hres))
  63. hres = SUPERCLASS::QueryInterface(riid, ppvObj);
  64. return hres;
  65. }
  66. /*----------------------------------------------------------
  67. Purpose: IMenuPopup::SetSubmenu method
  68. */
  69. STDMETHODIMP CMenuDeskBar::SetSubMenu(IMenuPopup* pmp, BOOL fSet)
  70. {
  71. if (fSet)
  72. {
  73. if (_pmpChild)
  74. _pmpChild->Release();
  75. _pmpChild = pmp;
  76. _pmpChild->AddRef();
  77. }
  78. else
  79. {
  80. if (_pmpChild && SHIsSameObject(pmp, _pmpChild))
  81. {
  82. _pmpChild->Release();
  83. _pmpChild = NULL;
  84. }
  85. }
  86. return S_OK;
  87. }
  88. void CMenuDeskBar::_PopDown()
  89. {
  90. DAD_ShowDragImage(FALSE);
  91. if (_pmpChild)
  92. _pmpChild->OnSelect(MPOS_CANCELLEVEL);
  93. // ShowWindow(_hwnd, SW_HIDE);
  94. SetWindowPos(_hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_HIDEWINDOW);
  95. ShowDW(FALSE);
  96. if (_pmpParent)
  97. {
  98. _pmpParent->SetSubMenu(this, FALSE);
  99. }
  100. UIActivateIO(FALSE, NULL);
  101. _fActive = FALSE;
  102. DAD_ShowDragImage(TRUE);
  103. }
  104. /*----------------------------------------------------------
  105. Purpose: IMenuPopup::OnSelect method
  106. */
  107. STDMETHODIMP CMenuDeskBar::OnSelect(DWORD dwSelectType)
  108. {
  109. switch (dwSelectType)
  110. {
  111. case MPOS_CHILDTRACKING:
  112. if (_pmpParent)
  113. _pmpParent->OnSelect(dwSelectType);
  114. break;
  115. case MPOS_SELECTRIGHT:
  116. case MPOS_SELECTLEFT:
  117. if (_pmpParent)
  118. _pmpParent->OnSelect(dwSelectType);
  119. break;
  120. case MPOS_EXECUTE:
  121. case MPOS_FULLCANCEL:
  122. _PopDown();
  123. if (_pmpParent)
  124. _pmpParent->OnSelect(dwSelectType);
  125. break;
  126. case MPOS_CANCELLEVEL:
  127. _PopDown();
  128. break;
  129. }
  130. return S_OK;
  131. }
  132. void SetExpandedBorder(HWND hwnd, BOOL fExpanded)
  133. {
  134. #ifdef MAINWIN
  135. // IEUNIX : WS_DLGFRAME implementaion looks ugly on UNIX.
  136. fExpanded = TRUE;
  137. #endif
  138. DWORD dwStyle = GetWindowLong(hwnd, GWL_STYLE);
  139. DWORD dwExStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
  140. if (fExpanded)
  141. {
  142. dwStyle |= WS_BORDER;
  143. dwStyle &= ~WS_DLGFRAME;
  144. }
  145. else
  146. {
  147. dwStyle &= ~WS_BORDER;
  148. dwStyle |= WS_DLGFRAME;
  149. }
  150. SetWindowLong(hwnd, GWL_STYLE, dwStyle);
  151. SetWindowLong(hwnd, GWL_EXSTYLE, dwExStyle);
  152. SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
  153. SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
  154. UpdateWindow(hwnd);
  155. }
  156. void CMenuDeskBar::_OnCreate()
  157. {
  158. if (!_fFlatMenuMode)
  159. SetExpandedBorder(_hwnd, _fExpanded);
  160. }
  161. DWORD CMenuDeskBar::_GetClassStyle()
  162. {
  163. DWORD dwStyle = CS_SAVEBITS; // Faster repaint for menus when they go away
  164. if (IsOS(OS_WHISTLERORGREATER))
  165. {
  166. dwStyle |= CS_DROPSHADOW; // Cool dropshadow effect on whistler....
  167. }
  168. return dwStyle;
  169. }
  170. DWORD CMenuDeskBar::_GetExStyle()
  171. {
  172. #ifndef MAINWIN
  173. return WS_EX_TOOLWINDOW | WS_EX_TOPMOST;
  174. #else
  175. return WS_EX_TOOLWINDOW | WS_EX_TOPMOST | WS_EX_MW_UNMANAGED_WINDOW;
  176. #endif
  177. }
  178. // We use the following structure to pass a whole bunch of information from
  179. // the GetPopupWindowPosition to WillFit function. We have WillFit function
  180. // to cut the amount of duplicate code in getpopup window position. The order
  181. // in which different the sides are checked is the only difference for popping
  182. // up a window on a particular side.
  183. //
  184. // Having this function helps us to do that check by means of a parameter instead
  185. // of repeating portions of code again and again.
  186. typedef struct {
  187. RECT rcAvail; // Available dimensions b/t monitor edge and exclude edge
  188. SIZE sizeAdjust; // Size of menu edge
  189. int cyMonitor; // Size of monitor
  190. int cxMonitor;
  191. int cx; // Size of menu
  192. int cy;
  193. int cyExtendDiff; // Difference b/t calc'd size and available size
  194. RECT *prcResult;
  195. RECT *prcExclude; // Exclude rect
  196. RECT *prcMonitor;
  197. } PopupInfo;
  198. #define TOP 0
  199. #define BOTTOM 1
  200. #define LEFT 2
  201. #define RIGHT 3
  202. /*----------------------------------------------------------
  203. Purpose: Attempt to fit and position a menu in the given direction
  204. relative to an exclude rect.
  205. Setting fForce to TRUE will cause the menu size to be adjusted
  206. to fit, if necessary.
  207. This function only sets the top and left coords, not the bottom
  208. and right coords.
  209. Returns TRUE if the desired direction can be accomplished.
  210. */
  211. BOOL WillFit(PopupInfo * pinfo, int side, BOOL fForce)
  212. {
  213. BOOL bRet = FALSE;
  214. LPRECT prcResult = pinfo->prcResult;
  215. pinfo->cyExtendDiff = 0;
  216. switch(side)
  217. {
  218. case TOP:
  219. pinfo->cyExtendDiff = pinfo->cy - pinfo->rcAvail.top;
  220. if (fForce)
  221. {
  222. // Doesn't make sense to subtract a negative value
  223. ASSERT(pinfo->cyExtendDiff >= 0);
  224. // +2 for some breathing room at the edge of the screen
  225. pinfo->cy -= pinfo->cyExtendDiff + 2;
  226. }
  227. // Can the menu be positioned above?
  228. if (pinfo->cy <= pinfo->rcAvail.top)
  229. {
  230. // Yes
  231. prcResult->top = pinfo->prcExclude->top - pinfo->cy;
  232. goto AdjustHorzPos;
  233. }
  234. break;
  235. case BOTTOM:
  236. pinfo->cyExtendDiff = pinfo->cy - pinfo->rcAvail.bottom;
  237. if (fForce)
  238. {
  239. // Doesn't make sense to subtract a negative value
  240. ASSERT(pinfo->cyExtendDiff >= 0);
  241. // +2 for some breathing room at the edge of the screen
  242. pinfo->cy -= pinfo->cyExtendDiff + 2;
  243. }
  244. // Can the menu be positioned below?
  245. if (pinfo->cy <= pinfo->rcAvail.bottom)
  246. {
  247. // Yes
  248. prcResult->top = pinfo->prcExclude->bottom;
  249. AdjustHorzPos:
  250. prcResult->left = max(pinfo->prcExclude->left, pinfo->prcMonitor->left);
  251. // Can the menu be positioned relative to its left edge (hanging right)?
  252. if (prcResult->left + pinfo->cx >= pinfo->prcMonitor->right)
  253. {
  254. // No; move it in so it is on the screen
  255. // (cx has already been adjusted to fit inside the monitor dimensions)
  256. prcResult->left = pinfo->prcMonitor->right - pinfo->cx - 1;
  257. }
  258. bRet = TRUE;
  259. }
  260. break;
  261. case LEFT:
  262. // Can the menu be positioned to the left?
  263. if (pinfo->cx <= pinfo->rcAvail.left || fForce)
  264. {
  265. // Yes
  266. // When cascading left, the menu does not overlap. Also align
  267. // so the client rect is vertically aligned with the exclude top.
  268. prcResult->left = pinfo->prcExclude->left - pinfo->cx - 1;
  269. goto AdjustVerticalPos;
  270. }
  271. break;
  272. case RIGHT:
  273. // Can the menu be positioned to the right?
  274. if (pinfo->cx <= pinfo->rcAvail.right || fForce)
  275. {
  276. // Yes
  277. // Adjust the menu to slightly overlap the parent menu. Also align
  278. // so the client rect is vertically aligned with the exclude top.
  279. prcResult->left = pinfo->prcExclude->right - pinfo->sizeAdjust.cx;
  280. AdjustVerticalPos:
  281. prcResult->top = pinfo->prcExclude->top - pinfo->sizeAdjust.cy;
  282. // Can the menu be positioned relative to its top edge (hanging down)?
  283. if (prcResult->top + pinfo->cy >= pinfo->prcMonitor->bottom)
  284. {
  285. // No; can it be positioned relative to its bottom edge (hanging up)?
  286. prcResult->top = pinfo->prcExclude->bottom + pinfo->sizeAdjust.cy - pinfo->cy;
  287. if (prcResult->top < pinfo->prcMonitor->top)
  288. {
  289. // No; move the menu so it fits, but isn't vertically snapped.
  290. // (cy has already been adjusted to fit inside the monitor
  291. // dimensions)
  292. prcResult->top = pinfo->prcMonitor->bottom - pinfo->cy - 1;
  293. }
  294. }
  295. bRet = TRUE;
  296. }
  297. break;
  298. }
  299. return bRet;
  300. }
  301. void CMenuDeskBar::_GetPopupWindowPosition(RECT* prcDesired, RECT* prcExclude,
  302. RECT *prcResult, SIZE * psizeAdjust, UINT uSide)
  303. {
  304. PopupInfo info;
  305. MONITORINFO mi;
  306. HMONITOR hMonitor;
  307. RECT rcMonitor;
  308. int cyExtendDiff = 0;
  309. // Is this going to display the banner bitmap?
  310. if (_iIconSize == BMICON_LARGE)
  311. {
  312. // Yes; add that to the dimensions
  313. prcDesired->right += _sizeBmp.cx;
  314. }
  315. // First get the monitor information
  316. hMonitor = MonitorFromRect(prcExclude, MONITOR_DEFAULTTONEAREST);
  317. mi.cbSize = sizeof(mi);
  318. if (GetMonitorInfo(hMonitor, &mi))
  319. {
  320. rcMonitor = mi.rcMonitor;
  321. // Set the result rectangle same as the desired window
  322. prcResult->left = prcDesired->left;
  323. prcResult->top = prcDesired->top;
  324. // Calculate some sizes needed for calculation
  325. info.rcAvail.left = prcExclude->left - rcMonitor.left;
  326. info.rcAvail.right = rcMonitor.right - prcExclude->right;
  327. info.rcAvail.top = prcExclude->top - rcMonitor.top;
  328. info.rcAvail.bottom = rcMonitor.bottom - prcExclude->bottom;
  329. info.sizeAdjust = *psizeAdjust;
  330. info.cyMonitor = RECTHEIGHT(rcMonitor);
  331. info.cxMonitor = RECTWIDTH(rcMonitor);
  332. info.cx = RECTWIDTH(*prcDesired);
  333. info.cy = RECTHEIGHT(*prcDesired);
  334. // If the desired rect is bigger than monitor then clip it to the monitor size
  335. if (info.cy > info.cyMonitor)
  336. info.cy = info.cyMonitor;
  337. if (info.cx > info.cxMonitor)
  338. info.cx = info.cxMonitor;
  339. info.prcResult = prcResult;
  340. info.prcExclude = prcExclude;
  341. info.prcMonitor = &rcMonitor;
  342. //Now Adjust the rectangle for the correct position
  343. switch(uSide)
  344. {
  345. int iSide;
  346. case MENUBAR_TOP:
  347. if (WillFit(&info, TOP, FALSE))
  348. {
  349. _uSide = MENUBAR_TOP;
  350. }
  351. else
  352. {
  353. // We couldn't fit it above, how badly did we fall short?
  354. cyExtendDiff = info.cyExtendDiff;
  355. if (WillFit(&info, BOTTOM, FALSE))
  356. _uSide = MENUBAR_BOTTOM;
  357. // We can't fit it below either, which dir was closest?
  358. // If they are equal, default to requested direction
  359. else if (info.cyExtendDiff < cyExtendDiff)
  360. {
  361. _uSide = MENUBAR_BOTTOM;
  362. WillFit(&info, BOTTOM, TRUE);
  363. }
  364. else
  365. {
  366. _uSide = MENUBAR_TOP;
  367. WillFit(&info, TOP, TRUE);
  368. }
  369. }
  370. break;
  371. case MENUBAR_BOTTOM:
  372. if (WillFit(&info, BOTTOM, FALSE))
  373. {
  374. _uSide = MENUBAR_BOTTOM;
  375. }
  376. else
  377. {
  378. // We couldn't fit it below, how badly did we fall short?
  379. cyExtendDiff = info.cyExtendDiff;
  380. if (WillFit(&info, TOP, FALSE))
  381. _uSide = MENUBAR_TOP;
  382. // We can't fit it above either, which dir was closest?
  383. // If they are equal, default to requested direction
  384. else if (info.cyExtendDiff < cyExtendDiff)
  385. {
  386. _uSide = MENUBAR_TOP;
  387. WillFit(&info, TOP, TRUE);
  388. }
  389. else
  390. {
  391. _uSide = MENUBAR_BOTTOM;
  392. WillFit(&info, BOTTOM, TRUE);
  393. }
  394. }
  395. break;
  396. case MENUBAR_LEFT:
  397. if (WillFit(&info, LEFT, FALSE))
  398. {
  399. _uSide = MENUBAR_LEFT;
  400. }
  401. else if (WillFit(&info, RIGHT, FALSE))
  402. {
  403. _uSide = MENUBAR_RIGHT;
  404. }
  405. else
  406. {
  407. // fit where have most room and can show most of menu.
  408. if ((info.cx - (info.prcExclude)->right) > (info.prcExclude)->left)
  409. {
  410. _uSide = MENUBAR_RIGHT;
  411. iSide = RIGHT;
  412. }
  413. else
  414. {
  415. _uSide = MENUBAR_LEFT;
  416. iSide = LEFT;
  417. }
  418. WillFit(&info, iSide, TRUE);
  419. }
  420. break;
  421. case MENUBAR_RIGHT:
  422. if (WillFit(&info, RIGHT, FALSE))
  423. {
  424. _uSide = MENUBAR_RIGHT;
  425. }
  426. else if (WillFit(&info, LEFT, FALSE))
  427. {
  428. _uSide = MENUBAR_LEFT;
  429. }
  430. else
  431. {
  432. // fit where have most room and can show most of menu.
  433. if ((info.cx - (info.prcExclude)->right) >= (info.prcExclude)->left)
  434. {
  435. _uSide = MENUBAR_RIGHT;
  436. iSide = RIGHT;
  437. }
  438. else
  439. {
  440. _uSide = MENUBAR_LEFT;
  441. iSide = LEFT;
  442. }
  443. WillFit(&info, iSide, TRUE);
  444. }
  445. break;
  446. }
  447. // Finally set the bottom and right
  448. if (prcResult->top < rcMonitor.top)
  449. prcResult->top = rcMonitor.top;
  450. if (prcResult->left < rcMonitor.left)
  451. prcResult->left = rcMonitor.left;
  452. prcResult->bottom = prcResult->top + info.cy;
  453. prcResult->right = prcResult->left + info.cx;
  454. if (prcResult->bottom > rcMonitor.bottom)
  455. {
  456. // -2 for some breathing room at the edge of the screen
  457. prcResult->bottom = rcMonitor.bottom - 2;
  458. prcResult->top = prcResult->bottom - info.cy;
  459. }
  460. }
  461. }
  462. void SlideAnimate(HWND hwnd, RECT* prc, UINT uFlags, UINT uSide)
  463. {
  464. DWORD dwAnimateFlags = AW_CENTER;
  465. switch(uSide)
  466. {
  467. case MENUBAR_LEFT: dwAnimateFlags = AW_HOR_NEGATIVE;
  468. break;
  469. case MENUBAR_RIGHT: dwAnimateFlags = AW_HOR_POSITIVE;
  470. break;
  471. case MENUBAR_TOP: dwAnimateFlags = AW_VER_NEGATIVE;
  472. break;
  473. case MENUBAR_BOTTOM: dwAnimateFlags = AW_VER_POSITIVE;
  474. break;
  475. }
  476. AnimateWindow(hwnd, 120, dwAnimateFlags | AW_SLIDE);
  477. }
  478. void AnimateSetMenuPos(HWND hwnd, RECT* prc, UINT uFlags, UINT uSide, BOOL fNoAnimate)
  479. {
  480. // simple check to see if we're too big for animatewindow, based on menu area.
  481. // this is because it has to read from video mem to do the alpha and thats slow, maybe
  482. // let it back if we get hardware acceleration in a future release.
  483. BOOL fPerfBad = (RECTWIDTH(*prc) * RECTHEIGHT(*prc) > 600 * 200);
  484. if (!fNoAnimate && !fPerfBad)
  485. {
  486. BOOL fAnimate = FALSE;
  487. SystemParametersInfo(SPI_GETMENUANIMATION, 0, &fAnimate, 0);
  488. if (fAnimate)
  489. {
  490. SetWindowPos(hwnd, HWND_TOPMOST, prc->left, prc->top,
  491. RECTWIDTH(*prc), RECTHEIGHT(*prc), uFlags);
  492. fAnimate = FALSE;
  493. SystemParametersInfo(SPI_GETMENUFADE, 0, &fAnimate, 0);
  494. if (fAnimate)
  495. {
  496. AnimateWindow(hwnd, 175, AW_BLEND);
  497. }
  498. else
  499. {
  500. SlideAnimate(hwnd, prc, uFlags, uSide);
  501. }
  502. }
  503. else
  504. goto UseSetWindowPos;
  505. }
  506. else
  507. {
  508. UseSetWindowPos:
  509. // Enable the show window so that it gets displayed.
  510. uFlags |= SWP_SHOWWINDOW;
  511. SetWindowPos(hwnd, HWND_TOPMOST, prc->left, prc->top, RECTWIDTH(*prc), RECTHEIGHT(*prc),
  512. uFlags);
  513. }
  514. }
  515. HRESULT CMenuDeskBar::_PositionWindow(POINTL *ppt, RECTL* prcExclude, DWORD dwFlags)
  516. {
  517. ASSERT(IS_VALID_READ_PTR(ppt, POINTL));
  518. ASSERT(NULL == prcExclude || IS_VALID_READ_PTR(prcExclude, RECTL));
  519. BOOL bSetFocus = (dwFlags & MPPF_SETFOCUS);
  520. RECT rcDesired;
  521. RECT rcExclude;
  522. RECT rc;
  523. SIZE sizeAdjust;
  524. UINT uAnimateSide;
  525. BOOL bMirroredWindow=IS_WINDOW_RTL_MIRRORED(_hwnd);
  526. static const iPosition[] = {MENUBAR_TOP, MENUBAR_LEFT, MENUBAR_RIGHT, MENUBAR_BOTTOM};
  527. if (dwFlags & MPPF_POS_MASK)
  528. {
  529. UINT uPosIndex = ((dwFlags & MPPF_POS_MASK) >> 29) - 1;
  530. ASSERT(uPosIndex < 4);
  531. _uSide = iPosition[uPosIndex];
  532. }
  533. if (bSetFocus)
  534. SetForegroundWindow(_hwnd);
  535. _pt = *(POINT*)ppt;
  536. // Get the size of the ideal client rect of the child
  537. RECT rcChild = {0};
  538. // (scotth): This only sets the bottom and the right values
  539. _pDBC->GetSize(DBC_GS_IDEAL, &rcChild);
  540. DWORD dwStyle = GetWindowLong(_hwnd, GWL_STYLE);
  541. DWORD dwExStyle = GetWindowLong(_hwnd, GWL_EXSTYLE);
  542. // Adjust for the window border style
  543. rcDesired = rcChild; // use rcDesired as a temp variable
  544. if (!_fNoBorder)
  545. {
  546. AdjustWindowRectEx(&rcChild, dwStyle, FALSE, dwExStyle);
  547. }
  548. // Calculate the edge of the menu border, and add a fudge factor so
  549. // left/right-cascaded menus overlap the parent menu a bit and are
  550. // correctly aligned vertically.
  551. sizeAdjust.cx = (RECTWIDTH(rcChild) - RECTWIDTH(rcDesired)) / 2;
  552. sizeAdjust.cy = (RECTHEIGHT(rcChild) - RECTHEIGHT(rcDesired)) / 2;
  553. if (prcExclude)
  554. {
  555. CopyRect(&rcExclude, (RECT*)prcExclude);
  556. //
  557. // If mirroring is enabled, let's mirror this guy
  558. // by simulating a different mirrored rect. This is
  559. // only for dropdown menus. [samera]
  560. //
  561. if (bMirroredWindow)
  562. {
  563. if ((_uSide != MENUBAR_LEFT) &&
  564. (_uSide != MENUBAR_RIGHT) )
  565. {
  566. int x;
  567. int iW = rcExclude.right-rcExclude.left;
  568. int icW = (rcChild.right-rcChild.left);
  569. if( icW > iW )
  570. {
  571. x = icW - iW;
  572. rcExclude.left -= x ;
  573. rcExclude.right -= x ;
  574. }
  575. else
  576. {
  577. x = iW - icW;
  578. rcExclude.left += x;
  579. rcExclude.right += x;
  580. }
  581. ppt->x = rcExclude.left;
  582. }
  583. }
  584. TraceMsg(DM_POPUP, "Parent Side is %d ", _uSide);
  585. switch(_uSide)
  586. {
  587. case MENUBAR_LEFT :
  588. rcDesired.left = rcExclude.left - rcChild.right; // right is width
  589. rcDesired.top = rcExclude.top;
  590. break;
  591. case MENUBAR_RIGHT :
  592. rcDesired.left = rcExclude.right;
  593. rcDesired.top = rcExclude.top;
  594. break;
  595. case MENUBAR_TOP:
  596. rcDesired.left = rcExclude.left;
  597. rcDesired.top = rcExclude.top - rcChild.bottom; // bottom is height
  598. break;
  599. case MENUBAR_BOTTOM:
  600. rcDesired.left = rcExclude.left;
  601. rcDesired.top = rcExclude.bottom;
  602. break;
  603. default:
  604. rcDesired.left = _pt.x;
  605. rcDesired.top = _pt.y;
  606. }
  607. }
  608. else
  609. {
  610. SetRectEmpty(&rcExclude);
  611. rcDesired.left = _pt.x;
  612. rcDesired.top = _pt.y;
  613. }
  614. rcDesired.right = rcDesired.left + RECTWIDTH(rcChild);
  615. rcDesired.bottom = rcDesired.top + RECTHEIGHT(rcChild);
  616. _GetPopupWindowPosition(&rcDesired, &rcExclude, &rc, &sizeAdjust, _uSide);
  617. UINT uFlags = SWP_NOOWNERZORDER;
  618. if (!bSetFocus)
  619. uFlags |= SWP_NOACTIVATE;
  620. //
  621. // Open the menus properly. In case of a RTL mirrored window,
  622. // flip the animation side. [samera]
  623. //
  624. if( bMirroredWindow )
  625. {
  626. switch( _uSide )
  627. {
  628. case MENUBAR_LEFT:
  629. uAnimateSide = MENUBAR_RIGHT;
  630. break;
  631. case MENUBAR_RIGHT:
  632. uAnimateSide = MENUBAR_LEFT;
  633. break;
  634. default:
  635. uAnimateSide = _uSide;
  636. }
  637. }
  638. else
  639. {
  640. uAnimateSide = _uSide;
  641. }
  642. TraceMsg(TF_MENUBAND, "CMenuBar::_PositionWindow (%d,%d,%d,%d)",
  643. rc.left, rc.top, rc.right, rc.bottom);
  644. // Last minuite tweak. Since we're in large icon, we need to add this
  645. // so that the bitmap is painted correctly.
  646. if(_iIconSize == BMICON_LARGE && _fExpanded)
  647. rc.right += 1;
  648. // We _DO_ want to do a Z-Order position when this flag is specified. This is
  649. // for full repositioning where we need to preserve the overlap state of all bands.
  650. // Otherwize we just want to size the bar without changing it's z-order.
  651. if (!(dwFlags & MPPF_FORCEZORDER) &&
  652. (S_OK == IUnknown_QueryServiceExec(_punkChild, SID_SMenuBandChild,
  653. &CGID_MenuBand, MBANDCID_ISINSUBMENU, 0, NULL, NULL)))
  654. {
  655. uFlags |= SWP_NOZORDER;
  656. }
  657. // If it's bigger than this magical number, then we don't animate. change to taste
  658. if (RECTHEIGHT(rc) > MAGICAL_NO_FADE_HEIGHT)
  659. dwFlags |= MPPF_NOANIMATE;
  660. AnimateSetMenuPos(_hwnd, &rc, uFlags, uAnimateSide, dwFlags & MPPF_NOANIMATE);
  661. // Save information so we can later resize this window
  662. // We already have: _pt, _uSide
  663. if (prcExclude)
  664. {
  665. _fExcludeRect = TRUE;
  666. CopyRect(&_rcExclude, (RECT*)prcExclude);
  667. }
  668. else
  669. _fExcludeRect = FALSE;
  670. return S_OK;
  671. }
  672. /*----------------------------------------------------------
  673. Purpose: IMenuPopup::Popup method
  674. */
  675. STDMETHODIMP CMenuDeskBar::Popup(POINTL* ppt, RECTL* prcExclude, DWORD dwFlags)
  676. {
  677. HRESULT hr;
  678. // Is the caller telling us to reposition?
  679. if (dwFlags & MPPF_REPOSITION)
  680. {
  681. if (ppt == NULL)
  682. ppt = (POINTL*)&_pt;
  683. if (prcExclude == NULL)
  684. prcExclude = (RECTL*)&_rcExclude;
  685. // Yes; Then we don't need to do any First show stuff.
  686. _PositionWindow(ppt, prcExclude, dwFlags);
  687. return S_OK;
  688. }
  689. ASSERT(IS_VALID_READ_PTR(ppt, POINTL));
  690. ASSERT(NULL == prcExclude || IS_VALID_READ_PTR(prcExclude, RECTL));
  691. if (_pmpParent)
  692. {
  693. _pmpParent->SetSubMenu(this, TRUE);
  694. }
  695. IOleCommandTarget* poct;
  696. hr = IUnknown_QueryService(_punkChild, SID_SMenuBandChild, IID_PPV_ARG(IOleCommandTarget, &poct));
  697. if (SUCCEEDED(hr))
  698. {
  699. // We need to do this before the ShowDW. This saves us from doing the setting twice
  700. // Because in the ShowDW of MenuBand, we actually go an initialize the toolbar with
  701. // the current default setting which should be "No Keyboard Cues." If we set the state
  702. // here, then the state will be "Show keyboard cues." Then we will update the toolbar.
  703. if (dwFlags & MPPF_KEYBOARD)
  704. poct->Exec(&CGID_MenuBand, MBANDCID_KEYBOARD, 0, NULL, NULL);
  705. }
  706. else
  707. {
  708. ASSERT(poct == NULL);
  709. }
  710. _NotifyModeChange(_dwMode);
  711. hr = ShowDW(TRUE);
  712. if (SUCCEEDED(hr) && _pmpParent)
  713. {
  714. VARIANT varg;
  715. // If this Exec fails, don't panic; it just means we use the default side
  716. if (SUCCEEDED(IUnknown_Exec(_pmpParent, &CGID_MENUDESKBAR, MBCID_GETSIDE, 0, NULL, &varg)))
  717. {
  718. if (varg.vt == VT_I4)
  719. {
  720. _uSide = (UINT) varg.lVal;
  721. }
  722. }
  723. }
  724. if (SUCCEEDED(hr))
  725. {
  726. SHPlaySound(TEXT("MenuPopup"));
  727. _PositionWindow(ppt, prcExclude, dwFlags);
  728. // Set focus
  729. UIActivateIO(TRUE, NULL);
  730. _fActive = TRUE;
  731. // Select the first/last item?
  732. if ((dwFlags & (MPPF_INITIALSELECT | MPPF_FINALSELECT)) && poct)
  733. {
  734. DWORD nCmd = (dwFlags & MPPF_INITIALSELECT) ? MBSI_FIRSTITEM : MBSI_LASTITEM;
  735. poct->Exec(&CGID_MenuBand, MBANDCID_SELECTITEM, nCmd, NULL, NULL);
  736. }
  737. }
  738. ATOMICRELEASE(poct);
  739. return hr;
  740. }
  741. /*----------------------------------------------------------
  742. Purpose: IInputObjectSite::OnFocusChangeIS
  743. Returns:
  744. Cond: --
  745. */
  746. HRESULT CMenuDeskBar::OnFocusChangeIS(IUnknown *punk, BOOL fSetFocus)
  747. {
  748. return NOERROR;
  749. }
  750. /*----------------------------------------------------------
  751. Purpose: IObjectWithSite::SetSite method
  752. */
  753. STDMETHODIMP CMenuDeskBar::SetSite(IUnknown* punkSite)
  754. {
  755. ASSERT(NULL == punkSite || IS_VALID_CODE_PTR(punkSite, IUnknown));
  756. if (_fShow)
  757. _PopDown();
  758. ATOMICRELEASE(_punkSite);
  759. ATOMICRELEASE(_pmpParent);
  760. _punkSite = punkSite;
  761. if (_punkSite)
  762. {
  763. _punkSite->AddRef();
  764. IUnknown_QueryService(_punkSite, SID_SMenuPopup, IID_PPV_ARG(IMenuPopup, &_pmpParent));
  765. }
  766. else
  767. {
  768. CloseDW(0);
  769. }
  770. return S_OK;
  771. }
  772. /*----------------------------------------------------------
  773. Purpose: IObjectWithSite::GetSite method
  774. */
  775. STDMETHODIMP CMenuDeskBar::GetSite(REFIID riid, LPVOID* ppvSite)
  776. {
  777. if (_punkSite)
  778. {
  779. return _punkSite->QueryInterface(riid, ppvSite);
  780. }
  781. *ppvSite = NULL;
  782. return E_FAIL;
  783. }
  784. void CMenuDeskBar::AdjustForTheme()
  785. {
  786. if (_fFlatMenuMode)
  787. {
  788. SHSetWindowBits(_hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_DLGFRAME | WS_BORDER, WS_BORDER);
  789. }
  790. else if (!_fExpanded)
  791. {
  792. SHSetWindowBits(_hwnd, GWL_STYLE, WS_CLIPCHILDREN | WS_DLGFRAME | WS_BORDER, WS_CLIPCHILDREN | WS_DLGFRAME);
  793. }
  794. }
  795. /*----------------------------------------------------------
  796. Purpose: IOleCommandTarget::Exec method
  797. */
  798. STDMETHODIMP CMenuDeskBar::Exec(const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt,
  799. VARIANTARG *pvarargIn, VARIANTARG *pvarargOut)
  800. {
  801. if (pguidCmdGroup == NULL)
  802. {
  803. }
  804. else if (IsEqualGUID(CGID_DeskBarClient, *pguidCmdGroup))
  805. {
  806. switch (nCmdID)
  807. {
  808. case DBCID_EMPTY:
  809. // if we have no bands left, close
  810. OnSelect(MPOS_FULLCANCEL);
  811. return S_OK;
  812. default:
  813. return OLECMDERR_E_NOTSUPPORTED;
  814. }
  815. }
  816. else if (IsEqualGUID(CGID_MENUDESKBAR, *pguidCmdGroup))
  817. {
  818. switch(nCmdID)
  819. {
  820. case MBCID_GETSIDE :
  821. pvarargOut->vt = VT_I4;
  822. pvarargOut->lVal = _GetSide();
  823. return S_OK;
  824. case MBCID_RESIZE:
  825. if (_fActive)
  826. {
  827. if (_fExcludeRect)
  828. _PositionWindow((POINTL *)&_pt, (RECTL *)&_rcExclude, 0);
  829. else
  830. _PositionWindow((POINTL *)&_pt, NULL, 0);
  831. }
  832. return S_OK;
  833. case MBCID_SETEXPAND:
  834. if ((BOOL)_fExpanded != (BOOL)nCmdexecopt && !_fFlatMenuMode)
  835. {
  836. _fExpanded = nCmdexecopt;
  837. SetExpandedBorder(_hwnd, _fExpanded);
  838. }
  839. return S_OK;
  840. case MBCID_SETFLAT:
  841. {
  842. _fFlatMenuMode = BOOLIFY(nCmdexecopt);
  843. AdjustForTheme();
  844. }
  845. break;
  846. case MBCID_NOBORDER:
  847. {
  848. _fNoBorder = BOOLIFY(nCmdexecopt);
  849. SetWindowPos(_hwnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
  850. }
  851. break;
  852. default :
  853. return OLECMDERR_E_NOTSUPPORTED;
  854. }
  855. }
  856. return SUPERCLASS::Exec(pguidCmdGroup, nCmdID, nCmdexecopt, pvarargIn, pvarargOut);
  857. }
  858. /*----------------------------------------------------------
  859. Purpose: IServiceProvider::QueryService method
  860. */
  861. STDMETHODIMP CMenuDeskBar::QueryService(REFGUID guidService, REFIID riid, void **ppvObj)
  862. {
  863. if (IsEqualGUID(guidService, SID_SMenuPopup))
  864. {
  865. return QueryInterface(riid, ppvObj);
  866. }
  867. else if (IsEqualIID(guidService, SID_SMenuBandBottom) ||
  868. IsEqualIID(guidService, SID_SMenuBandBottomSelected)||
  869. IsEqualIID(guidService, SID_SMenuBandChild))
  870. {
  871. // SID_SMenuBandBottom queries down
  872. return IUnknown_QueryService(_punkChild, guidService, riid, ppvObj);
  873. }
  874. else
  875. {
  876. HRESULT hres = SUPERCLASS::QueryService(guidService, riid, ppvObj);
  877. if (FAILED(hres))
  878. {
  879. hres = IUnknown_QueryService(_punkSite, guidService, riid, ppvObj);
  880. }
  881. return hres;
  882. }
  883. }
  884. /*----------------------------------------------------------
  885. Purpose: IServiceProvider::QueryService method
  886. */
  887. STDMETHODIMP CMenuDeskBar::SetIconSize(DWORD iIcon)
  888. {
  889. HRESULT hres;
  890. _iIconSize = iIcon;
  891. hres = IUnknown_QueryServiceExec(_punkChild, SID_SMenuBandChild, &CGID_MenuBand,
  892. MBANDCID_SETICONSIZE, iIcon == BMICON_SMALL? ISFBVIEWMODE_SMALLICONS: ISFBVIEWMODE_LARGEICONS, NULL, NULL);
  893. return hres;
  894. }
  895. /*----------------------------------------------------------
  896. Purpose: IServiceProvider::QueryService method
  897. */
  898. STDMETHODIMP CMenuDeskBar::SetBitmap(HBITMAP hBitmap)
  899. {
  900. ASSERT(hBitmap);
  901. BITMAP bm;
  902. _hbmp = hBitmap;
  903. if (_hbmp)
  904. {
  905. if(!GetObject(_hbmp, sizeof(bm), &bm))
  906. return E_FAIL;
  907. _sizeBmp.cx = bm.bmWidth;
  908. _sizeBmp.cy = bm.bmHeight;
  909. // Hack to get color
  910. HDC hdc = GetDC(_hwnd);
  911. if (hdc)
  912. {
  913. HDC hdcMem = CreateCompatibleDC(hdc);
  914. if (hdcMem)
  915. {
  916. HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcMem, _hbmp);
  917. _rgb = GetPixel(hdcMem, 0, 0);
  918. SelectObject(hdcMem, hbmpOld);
  919. DeleteDC(hdcMem);
  920. }
  921. ReleaseDC(_hwnd, hdc);
  922. }
  923. }
  924. return NOERROR;
  925. }
  926. void CMenuDeskBar::_OnSize()
  927. {
  928. RECT rc;
  929. if (!_hwndChild)
  930. return;
  931. GetClientRect(_hwnd, &rc);
  932. if(_iIconSize == BMICON_LARGE)
  933. {
  934. rc.left += _sizeBmp.cx;
  935. if (_fExpanded)
  936. rc.left++;
  937. }
  938. SetWindowPos(_hwndChild, 0,
  939. rc.left, rc.top, RECTWIDTH(rc), RECTHEIGHT(rc),
  940. SWP_NOACTIVATE|SWP_NOZORDER|SWP_FRAMECHANGED);
  941. rc.right = rc.left;
  942. rc.left -= _sizeBmp.cx;
  943. if (_fShow)
  944. InvalidateRect(_hwnd, &rc, TRUE);
  945. }
  946. LRESULT CMenuDeskBar::_DoPaint(HWND hwnd, HDC hdc, DWORD dwFlags)
  947. {
  948. HDC hdcmem;
  949. HBITMAP hbmpOld;
  950. RECT rc;
  951. HBRUSH hbrush;
  952. int iDC = SaveDC(hdc);
  953. GetClientRect(hwnd, &rc);
  954. //Create a compatable DC
  955. hdcmem = CreateCompatibleDC(hdc);
  956. if(hdcmem)
  957. {
  958. // Offset the stuff we're paining if we're expanded
  959. BYTE bOffset = 0;
  960. // Store this for the Bar fill cycle
  961. int cyBitmap = 0;
  962. if (!_fFlatMenuMode)
  963. {
  964. bOffset = _fExpanded? 1 : 0;
  965. }
  966. if (_sizeBmp.cy <= RECTHEIGHT(rc) + 1)
  967. {
  968. //Select the bitmap into the memory DC
  969. hbmpOld = (HBITMAP)SelectObject(hdcmem, _hbmp);
  970. //Blit to the window
  971. BitBlt(hdc, bOffset, rc.bottom - _sizeBmp.cy - bOffset, _sizeBmp.cx, _sizeBmp.cy, hdcmem, 0, 0, SRCCOPY);
  972. // Ok, We need to subtract this value to see how much we need to paint for the banner.
  973. cyBitmap = _sizeBmp.cy;
  974. //Restore the DC
  975. SelectObject(hdcmem, hbmpOld);
  976. }
  977. rc.right = _sizeBmp.cx + bOffset;
  978. if (_fExpanded && !_fFlatMenuMode && !_fNoBorder)
  979. DrawEdge(hdc, &rc, BDR_RAISEDINNER, BF_LEFT | BF_TOP | BF_BOTTOM);
  980. //Paint the rest
  981. hbrush = CreateSolidBrush(_rgb);
  982. if(hbrush)
  983. {
  984. rc.bottom -= cyBitmap + bOffset;
  985. if (_fExpanded)
  986. {
  987. rc.left += bOffset;
  988. rc.top += bOffset;
  989. }
  990. FillRect(hdc, &rc, hbrush);
  991. DeleteObject(hbrush);
  992. }
  993. //Delete the DC.
  994. DeleteDC(hdcmem);
  995. }
  996. RestoreDC(hdc, iDC);
  997. return 0;
  998. }
  999. void CMenuDeskBar::_DoNCPaint(HWND hwnd, HDC hdc)
  1000. {
  1001. if (!_fNoBorder)
  1002. {
  1003. RECT rc;
  1004. // Since we need to paint the border, we get the window rect
  1005. GetWindowRect(hwnd, &rc);
  1006. // then change the rect so that it represents values relative to
  1007. // the origin.
  1008. OffsetRect(&rc, -rc.left, -rc.top);
  1009. if (hdc)
  1010. {
  1011. if (_fFlatMenuMode)
  1012. {
  1013. SHOutlineRect(hdc, &rc, GetSysColor(COLOR_3DSHADOW));
  1014. }
  1015. else
  1016. DrawEdge(hdc, &rc, BDR_RAISEDOUTER, BF_RECT);
  1017. }
  1018. }
  1019. }
  1020. LRESULT CMenuDeskBar::v_WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  1021. {
  1022. LRESULT lres;
  1023. switch (uMsg)
  1024. {
  1025. #ifdef MAINWIN
  1026. case WM_NCPAINTSPECIALFRAME:
  1027. // In case of motif look the MwPaintBorder paints a Etched In
  1028. // border if WM_NCPAINTSPECIALFRAME returns FALSE. We are handling
  1029. // this message here and drawing the Etched Out frame explicitly.
  1030. // wParam - HDC
  1031. if (MwCurrentLook() == LOOK_MOTIF)
  1032. {
  1033. MwPaintSpecialEOBorder( hwnd, (HDC)wParam );
  1034. return TRUE;
  1035. }
  1036. break;
  1037. #endif
  1038. case WM_GETOBJECT:
  1039. if (lParam == OBJID_MENU)
  1040. {
  1041. IAccessible* pacc;
  1042. if (SUCCEEDED(QueryService(SID_SMenuBandChild, IID_PPV_ARG(IAccessible, &pacc))))
  1043. {
  1044. lres = LresultFromObject(IID_IAccessible, wParam, SAFECAST(pacc, IAccessible*));
  1045. pacc->Release();
  1046. return lres;
  1047. }
  1048. }
  1049. break;
  1050. case WM_NCCREATE:
  1051. //
  1052. // Since this is a mirrored menu, then open it
  1053. // on the left (mirrored) edge if possible. WillFit(...) will
  1054. // ensure this for us [samera]
  1055. //
  1056. // Mirror the menu initially if its window is mirrored
  1057. //
  1058. ASSERT(_uSide == 0);
  1059. if (IS_WINDOW_RTL_MIRRORED(_hwnd))
  1060. _uSide = MENUBAR_LEFT;
  1061. else
  1062. _uSide = MENUBAR_RIGHT;
  1063. break;
  1064. case WM_ACTIVATE:
  1065. if (LOWORD(wParam) == WA_INACTIVE)
  1066. {
  1067. if (_fActive && !_pmpChild)
  1068. {
  1069. // if we were active, and the thing going active now
  1070. // is not one of our parent menubars, then cancel everything.
  1071. // if it's a parent of ours going active, assume that
  1072. // they will tell us to die when they want us to...
  1073. if (!_IsMyParent((HWND)lParam))
  1074. OnSelect(MPOS_FULLCANCEL);
  1075. }
  1076. }
  1077. else
  1078. {
  1079. if (_pmpChild)
  1080. {
  1081. // if we're becoming active, and we have a child, that child should go away
  1082. _pmpChild->OnSelect(MPOS_CANCELLEVEL);
  1083. }
  1084. }
  1085. break;
  1086. case WM_PRINTCLIENT:
  1087. if (_iIconSize == BMICON_LARGE)
  1088. {
  1089. _DoPaint(hwnd, (HDC)wParam, (DWORD)lParam);
  1090. return 0;
  1091. }
  1092. break;
  1093. case WM_PAINT:
  1094. // Paint the banner if we're in showing large icons
  1095. if (_iIconSize == BMICON_LARGE)
  1096. {
  1097. PAINTSTRUCT ps;
  1098. BeginPaint(hwnd, &ps);
  1099. _DoPaint(hwnd, ps.hdc, 0);
  1100. EndPaint(hwnd, &ps);
  1101. return 0;
  1102. }
  1103. break;
  1104. case WM_PRINT:
  1105. if ((_fFlatMenuMode || _fExpanded) && PRF_NONCLIENT & lParam)
  1106. {
  1107. HDC hdc = (HDC)wParam;
  1108. DefWindowProcWrap(hwnd, WM_PRINT, wParam, lParam);
  1109. // Do this after so that we look right...
  1110. _DoNCPaint(hwnd, hdc);
  1111. return 1;
  1112. }
  1113. break;
  1114. case WM_NCCALCSIZE:
  1115. if (_fNoBorder)
  1116. {
  1117. return 0;
  1118. }
  1119. else
  1120. {
  1121. return SUPERCLASS::v_WndProc(hwnd, uMsg, wParam, lParam);
  1122. }
  1123. break;
  1124. case WM_NCPAINT:
  1125. if (_fNoBorder)
  1126. {
  1127. return 0;
  1128. }
  1129. else if (_fExpanded || _fFlatMenuMode)
  1130. {
  1131. HDC hdc;
  1132. hdc = GetWindowDC(hwnd);
  1133. if (hdc)
  1134. {
  1135. _DoNCPaint(hwnd, hdc);
  1136. ReleaseDC(hwnd, hdc);
  1137. }
  1138. return 1;
  1139. }
  1140. break;
  1141. case WM_NCHITTEST:
  1142. lres = SUPERCLASS::v_WndProc(hwnd, uMsg, wParam, lParam);
  1143. switch (lres)
  1144. {
  1145. case HTBOTTOM:
  1146. case HTBOTTOMLEFT:
  1147. case HTBOTTOMRIGHT:
  1148. case HTLEFT:
  1149. case HTRIGHT:
  1150. case HTTOP:
  1151. case HTTOPLEFT:
  1152. case HTTOPRIGHT:
  1153. // Don't allow the window to be resized
  1154. lres = HTBORDER;
  1155. break;
  1156. case HTTRANSPARENT:
  1157. // Don't let a click go thru to the window underneath
  1158. lres = HTCLIENT;
  1159. break;
  1160. }
  1161. return lres;
  1162. // HACKHACKHACKHACKHACKHACKHACKHACKHACKHACKHACKHACKHACKHACKHACKHACKHACKHACK
  1163. // (lamadio) 1.25.99
  1164. // This hack is here to fix a problem on down level Windows with Integrated
  1165. // IE4.01, IE5 and Office 2000.
  1166. // The bug revolves around Start Menu not being destroyed when Explorer.exe shuts
  1167. // down. Start Menu unregisters itself at CloseDW, but since the menubar never gets
  1168. // destroyed, Start Menu never deregisters itself.
  1169. // When an System service, such as MSTASK.dll keeps shell32 alive in the background,
  1170. // it leaves an outstanding reference to a change notify. When a new user logs in,
  1171. // O2k and IE5 fire up group conv, generating more than 10 change notify events in the
  1172. // start menu. This causes the batching code to be fired up: Which does not really
  1173. // work without the shell started. GroupConv also adds these events using memory
  1174. // alloced from it's process heap. Since there is an outstanding change notify handler
  1175. // these pidls get forced to be handled. Shell32 then faults derefing a bad pidl.
  1176. // By detecting an Endsession, we can eliminate this problem. Doing a SetClient(NULL)
  1177. // cause Menubar to free it's references to MenuSite. Menusite, calls CloseDW on menuband
  1178. // menuband then causes MNFolder to unregister itself. Since no one is listening any more
  1179. // the crash is avoided.
  1180. case WM_ENDSESSION:
  1181. if (wParam != 0)
  1182. {
  1183. SetClient(NULL);
  1184. }
  1185. break;
  1186. }
  1187. return SUPERCLASS::v_WndProc(hwnd, uMsg, wParam, lParam);
  1188. }
  1189. IMenuPopup* CMenuDeskBar::_GetMenuBarParent(IUnknown* punk)
  1190. {
  1191. IMenuPopup *pmp = NULL;
  1192. IObjectWithSite* pows;
  1193. punk->QueryInterface(IID_PPV_ARG(IObjectWithSite, &pows));
  1194. if (pows)
  1195. {
  1196. IServiceProvider* psp;
  1197. pows->GetSite(IID_PPV_ARG(IServiceProvider, &psp));
  1198. if (psp)
  1199. {
  1200. psp->QueryService(SID_SMenuPopup, IID_PPV_ARG(IMenuPopup, &pmp));
  1201. psp->Release();
  1202. }
  1203. pows->Release();
  1204. }
  1205. return pmp;
  1206. }
  1207. // this assumes that hwnd is a toplevel window and that the menudeskbars are also
  1208. // the only hosts and are themselves toplevel
  1209. BOOL CMenuDeskBar::_IsMyParent(HWND hwnd)
  1210. {
  1211. BOOL fRet = FALSE;
  1212. if (hwnd)
  1213. {
  1214. HWND hwndMenu;
  1215. IMenuPopup *pmpParent = _pmpParent;
  1216. if (pmpParent)
  1217. pmpParent->AddRef();
  1218. while (pmpParent && !fRet &&
  1219. SUCCEEDED(IUnknown_GetWindow(pmpParent, &hwndMenu)))
  1220. {
  1221. if (hwndMenu == hwnd)
  1222. {
  1223. fRet = TRUE;
  1224. }
  1225. IMenuPopup* pmpNext = _GetMenuBarParent(pmpParent);
  1226. pmpParent->Release();
  1227. pmpParent = pmpNext;
  1228. }
  1229. }
  1230. return fRet;
  1231. }
  1232. IMenuPopup* CreateMenuPopup(IMenuPopup* pmpParent, IShellFolder* psf, LPCITEMIDLIST pidl,
  1233. BANDINFOSFB * pbi, BOOL bMenuBand)
  1234. {
  1235. return CreateMenuPopup2(pmpParent, NULL, psf, pidl, pbi, bMenuBand);
  1236. }
  1237. IMenuPopup* CreateMenuPopup2(IMenuPopup* pmpParent, IMenuBand* pmb, IShellFolder* psf, LPCITEMIDLIST pidl,
  1238. BANDINFOSFB * pbi, BOOL bMenuBand)
  1239. {
  1240. ASSERT(pmb == NULL || IS_VALID_CODE_PTR(pmb, IMenuBand));
  1241. ASSERT(psf == NULL || IS_VALID_CODE_PTR(psf, IShellFolder));
  1242. ASSERT(pmpParent == NULL || IS_VALID_CODE_PTR(pmpParent, IMenuPopup));
  1243. ASSERT(pidl && IS_VALID_PIDL(pidl));
  1244. ASSERT(pbi == NULL || IS_VALID_READ_PTR(pbi, BANDINFOSFB));
  1245. IMenuPopup* pmp = NULL;
  1246. IDeskBand *pdb = NULL;
  1247. IBandSite *pbs = NULL;
  1248. HRESULT hres = E_FAIL;
  1249. if (!pdb)
  1250. {
  1251. TraceMsg(TF_MENUBAND, "CreateMenuPopup2 : Did not find a this (0x%x) band.", pidl);
  1252. if (bMenuBand)
  1253. {
  1254. pdb = CMenuBand_Create(psf, pidl, FALSE);
  1255. }
  1256. else
  1257. {
  1258. CISFBand_CreateEx(psf, pidl, IID_PPV_ARG(IDeskBand, &pdb));
  1259. }
  1260. if (pdb)
  1261. {
  1262. if (pbi)
  1263. {
  1264. IShellFolderBand *pisfBand;
  1265. if (SUCCEEDED(pdb->QueryInterface(IID_IShellFolderBand, (LPVOID*)&pisfBand)))
  1266. {
  1267. pisfBand->SetBandInfoSFB(pbi);
  1268. pisfBand->Release();
  1269. }
  1270. }
  1271. if (!pmpParent)
  1272. {
  1273. const CLSID * pclsid;
  1274. if (bMenuBand)
  1275. pclsid = &CLSID_MenuBandSite;
  1276. else
  1277. pclsid = &CLSID_RebarBandSite;
  1278. CoCreateInstance(*pclsid, NULL, CLSCTX_INPROC_SERVER, IID_IBandSite, (LPVOID*)&pbs);
  1279. if (pbs)
  1280. {
  1281. if (bMenuBand)
  1282. {
  1283. BANDSITEINFO bsinfo;
  1284. // Don't show the gripper for vertical menubands
  1285. bsinfo.dwMask = BSIM_STYLE;
  1286. bsinfo.dwStyle = BSIS_NOGRIPPER | BSIS_NODROPTARGET;
  1287. pbs->SetBandSiteInfo(&bsinfo);
  1288. }
  1289. CMenuDeskBar *pcmdb = new CMenuDeskBar();
  1290. if (pcmdb)
  1291. {
  1292. if (SUCCEEDED(pcmdb->SetClient(pbs)))
  1293. pcmdb->QueryInterface(IID_IMenuPopup, (LPVOID *)&pmp);
  1294. pcmdb->Release();
  1295. }
  1296. }
  1297. }
  1298. if (pbs)
  1299. {
  1300. pbs->AddBand(pdb);
  1301. }
  1302. }
  1303. }
  1304. ATOMICRELEASE(pdb);
  1305. ATOMICRELEASE(pbs);
  1306. if (!pmp)
  1307. IUnknown_Set((IUnknown**) &pmp, pmpParent);
  1308. return pmp;
  1309. }