1 ////////////////////////////////////////////////////////////////
2 // Copyright 1998 Paul DiLascia
3 // If this code works, it was written by Paul DiLascia.
4 // If not, I don't know who wrote it.
9 #include "UIsubclass.h"
11 #include "UIFlatBar.h"
14 // CMenuBar uses this private class to intercept messages on behalf
15 // of its owning frame, as well as the MDI client window. Conceptually,
16 // these should be two different hooks, but I want to save code.
18 class CMenuBarFrameHook : public CSubclassWnd {
20 friend class CMenuBar;
24 BOOL Install(CMenuBar* pMenuBar, HWND hWndToHook);
25 virtual LRESULT WindowProc(UINT msg, WPARAM wp, LPARAM lp);
29 // CMenuBar implements an Office 97-style menu bar. Use it the way you would
30 // a CToolBar, only you need not call LoadToolbar. All you have to do is
32 // * Create the CMenuBar from your OnCreate or OnCreateBands handler.
34 // * Call LoadMenu to load a menu. This will set your frame's menu to NULL.
36 // * Implemenent your frame's PreTranslateMessage function, to call
37 // CMenuBar::TranslateFrameMessage.
39 class CTRL_EXT_CLASS CMenuBar : public CFlatToolBar {
41 BOOL m_bAutoRemoveFrameMenu; // set frame's menu to NULL
46 // You must call this from your frame's PreTranslateMessage fn
47 virtual BOOL TranslateFrameMessage(MSG* pMsg);
49 HMENU LoadMenu(HMENU hmenu); // load menu
50 HMENU LoadMenu(LPCTSTR lpszMenuName); // ...from resource file
51 HMENU LoadMenu(UINT nID) {
52 return LoadMenu(MAKEINTRESOURCE(nID));
54 HMENU GetMenu() { return m_hmenu; } // get current menu
56 enum TRACKINGSTATE { // menubar has three states:
57 TRACK_NONE = 0, // * normal, not tracking anything
58 TRACK_BUTTON, // * tracking buttons (F10/Alt mode)
59 TRACK_POPUP // * tracking popups
62 TRACKINGSTATE GetTrackingState(int& iPopup) {
63 iPopup = m_iPopupTracking; return m_iTrackingState;
65 static BOOL bTRACE; // set TRUE to see TRACE msgs
68 friend class CMenuBarFrameHook;
70 CMenuBarFrameHook m_frameHook; // hooks frame window messages
71 CStringArray m_arStrings; // array of menu item names
72 HMENU m_hmenu; // the menu
74 // menu tracking stuff:
75 int m_iPopupTracking; // which popup I'm tracking if any
76 int m_iNewPopup; // next menu to track
77 BOOL m_bProcessRightArrow; // process l/r arrow keys?
78 BOOL m_bProcessLeftArrow; // ...
79 BOOL m_bEscapeWasPressed; // user pressed escape to exit menu
80 CPoint m_ptMouse; // mouse location when tracking popup
81 HMENU m_hMenuTracking; // current popup I'm tracking
83 TRACKINGSTATE m_iTrackingState; // current tracking state
86 void RecomputeToolbarSize();
87 void RecomputeMenuLayout();
89 int GetNextOrPrevButton(int iButton, BOOL bPrev);
90 void SetTrackingState(TRACKINGSTATE iState, int iButton=-1);
91 void TrackPopup(int iButton);
92 void ToggleTrackButtonMode();
93 void CancelMenuAndTrackNewOne(int iButton);
94 void OnMenuSelect(HMENU hmenu, UINT nItemID);
95 CPoint ComputeMenuTrackPoint(const CRect& rcButn, TPMPARAMS& tpm);
97 BOOL IsValidButton(int iButton) const
98 { return 0 <= iButton && iButton < GetButtonCount(); }
100 virtual BOOL OnMenuInput(MSG& m); // handle popup menu input
103 virtual void OnBarStyleChange(DWORD dwOldStyle, DWORD dwNewStyle);
104 int HitTest(CPoint p) const;
106 // command/message handlers
107 afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
108 afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
109 afx_msg void OnMouseMove(UINT nFlags, CPoint point);
110 afx_msg void OnSize(UINT nType, int cx, int cy);
111 afx_msg void OnUpdateMenuButton(CCmdUI* pCmdUI);
112 afx_msg LRESULT OnSetMenuNull(WPARAM wp, LPARAM lp);
114 static LRESULT CALLBACK MenuInputFilter(int code, WPARAM wp, LPARAM lp);
116 DECLARE_DYNAMIC(CMenuBar)
117 DECLARE_MESSAGE_MAP()
120 virtual void AssertValid() const;
121 virtual void Dump(CDumpContext& dc) const;