Skip to content

Commit

Permalink
Main Menu toggle Show/Hide (WinMerge#2509) (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Nov 8, 2024
1 parent 8cb4c33 commit 844163f
Show file tree
Hide file tree
Showing 46 changed files with 202 additions and 66 deletions.
86 changes: 37 additions & 49 deletions Src/MainFrm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,6 @@ BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
ON_WM_NCCALCSIZE()
ON_WM_SIZE()
ON_WM_SYSCOMMAND()
ON_WM_ENTERMENULOOP()
ON_WM_EXITMENULOOP()
ON_UPDATE_COMMAND_UI_RANGE(CMenuBar::FIRST_MENUID, CMenuBar::FIRST_MENUID + 10, OnUpdateMenuBarMenuItem)
// [File] menu
ON_COMMAND(ID_FILE_NEW, (OnFileNew<2, ID_MERGE_COMPARE_TEXT>))
Expand Down Expand Up @@ -253,6 +251,8 @@ BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
// [View] menu
ON_COMMAND(ID_VIEW_SELECTFONT, OnViewSelectfont)
ON_COMMAND(ID_VIEW_USEDEFAULTFONT, OnViewUsedefaultfont)
ON_COMMAND(ID_VIEW_MENU_BAR, OnViewMenuBar)
ON_UPDATE_COMMAND_UI(ID_VIEW_MENU_BAR, OnUpdateViewMenuBar)
ON_COMMAND(ID_VIEW_STATUS_BAR, OnViewStatusBar)
ON_COMMAND(ID_VIEW_TAB_BAR, OnViewTabBar)
ON_UPDATE_COMMAND_UI(ID_VIEW_TAB_BAR, OnUpdateViewTabBar)
Expand Down Expand Up @@ -325,9 +325,6 @@ BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
ON_MESSAGE(WMU_CHILDFRAMEREMOVED, &CMainFrame::OnChildFrameRemoved)
ON_MESSAGE(WMU_CHILDFRAMEACTIVATE, &CMainFrame::OnChildFrameActivate)
ON_MESSAGE(WMU_CHILDFRAMEACTIVATED, &CMainFrame::OnChildFrameActivated)
// Main menu toggle switch
ON_COMMAND(ID_VIEW_MAIN_MENU, OnToggleMainMenu)
ON_UPDATE_COMMAND_UI(ID_VIEW_MAIN_MENU, OnUpdateToggleMainMenu)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

Expand Down Expand Up @@ -2253,6 +2250,26 @@ BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
return __super::PreTranslateMessage(pMsg);
}

/**
* @brief Show/hide menubar.
*/
void CMainFrame::OnViewMenuBar()
{
const bool bMenuVisible = !GetOptionsMgr()->GetBool(OPT_SHOW_MENUBAR);
__super::ShowControlBar(&m_wndMenuBar, bMenuVisible, 0);
GetOptionsMgr()->SaveOption(OPT_SHOW_MENUBAR, bMenuVisible);
m_wndMenuBar.SetAlwaysVisible(bMenuVisible);
UpdateSystemMenu();
}

/**
* @brief Updates "Menu Bar" menuitem.
*/
void CMainFrame::OnUpdateViewMenuBar(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(GetOptionsMgr()->GetBool(OPT_SHOW_MENUBAR));
}

/**
* @brief Show/hide statusbar.
*/
Expand Down Expand Up @@ -2620,9 +2637,10 @@ BOOL CMainFrame::CreateToolbar()
__super::ShowControlBar(&m_wndToolBar, false, 0);
}

if (GetOptionsMgr()->GetBool(OPT_HIDE_MAINMENU))
if (!GetOptionsMgr()->GetBool(OPT_SHOW_MENUBAR))
{
__super::ShowControlBar(&m_wndMenuBar, false, 0);
m_wndMenuBar.SetAlwaysVisible(false);
}

m_wndReBar.LoadStateFromString(GetOptionsMgr()->GetString(OPT_REBAR_STATE).c_str());
Expand Down Expand Up @@ -3728,61 +3746,31 @@ LRESULT CMainFrame::OnChildFrameActivated(WPARAM wParam, LPARAM lParam)
return 1;
}

void CMainFrame::OnToggleMainMenu()
{
const bool bMenuVisible = static_cast<bool>(m_wndMenuBar.IsVisible());
__super::ShowControlBar(&m_wndMenuBar, !bMenuVisible, 0);
GetOptionsMgr()->SaveOption(OPT_HIDE_MAINMENU, bMenuVisible);
UpdateSystemMenu();
}

void CMainFrame::OnUpdateToggleMainMenu(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(!GetOptionsMgr()->GetBool(OPT_HIDE_MAINMENU));
}

void CMainFrame::UpdateSystemMenu()
{
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu == nullptr)
return;
BOOL bChecked = !GetOptionsMgr()->GetBool(OPT_HIDE_MAINMENU);
bool bFound = false;
const int cnt = pSysMenu->GetMenuItemCount();
for (int i = 0; i < cnt; ++i)
if (pSysMenu->GetMenuItemID(i) == ID_SHOW_MAIN_MENU)
{
pSysMenu->CheckMenuItem(i, MF_BYPOSITION | (bChecked ? MF_CHECKED : MF_UNCHECKED));
return; // Add only once
}
String menuTxt = theApp.LoadString(static_cast<UINT>(IDS_SHOW_MAIN_MENU));
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, ID_SHOW_MAIN_MENU, menuTxt.c_str());
pSysMenu->CheckMenuItem(ID_SHOW_MAIN_MENU, MF_BYCOMMAND | (bChecked ? MF_CHECKED : MF_UNCHECKED));
if (pSysMenu->GetMenuItemID(i) == ID_VIEW_MENU_BAR)
bFound = true;
if (!bFound)
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, ID_VIEW_MENU_BAR, _("Men&u Bar").c_str());
}
const bool bChecked = GetOptionsMgr()->GetBool(OPT_SHOW_MENUBAR);
pSysMenu->CheckMenuItem(ID_VIEW_MENU_BAR, MF_BYCOMMAND | (bChecked ? MF_CHECKED : MF_UNCHECKED));
}

void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam)
{
if (nID == ID_SHOW_MAIN_MENU) {
OnToggleMainMenu();
if (nID == ID_VIEW_MENU_BAR)
{
OnViewMenuBar();
return;
}
__super::OnSysCommand(nID, lParam);
}

void CMainFrame::OnEnterMenuLoop(BOOL bMainMenu)
{
__super::OnEnterMenuLoop(bMainMenu);
if (!bMainMenu)
return;
if (GetOptionsMgr()->GetBool(OPT_HIDE_MAINMENU) && !m_wndMenuBar.IsVisible())
__super::ShowControlBar(&m_wndMenuBar, true, 0);
}

void CMainFrame::OnExitMenuLoop(BOOL bMainMenu)
{
__super::OnExitMenuLoop(bMainMenu);
if (!bMainMenu)
return;
if (GetOptionsMgr()->GetBool(OPT_HIDE_MAINMENU) && m_wndMenuBar.IsVisible())
__super::ShowControlBar(&m_wndMenuBar, false, 0);
}
6 changes: 2 additions & 4 deletions Src/MainFrm.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,9 @@ class CMainFrame : public CMDIFrameWnd
afx_msg LRESULT OnChildFrameActivate(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnChildFrameActivated(WPARAM wParam, LPARAM lParam);
afx_msg void OnUpdateMenuBarMenuItem(CCmdUI* pCmdUI);
afx_msg void OnToggleMainMenu();
afx_msg void OnUpdateToggleMainMenu(CCmdUI* pCmdUI);
afx_msg void OnViewMenuBar();
afx_msg void OnUpdateViewMenuBar(CCmdUI* pCmdUI);
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnEnterMenuLoop(BOOL bMainMenu);
afx_msg void OnExitMenuLoop(BOOL bMainMenu);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()

Expand Down
37 changes: 34 additions & 3 deletions Src/MenuBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "StdAfx.h"
#include "MenuBar.h"

static const UINT UWM_SHOWPOPUPMENU = WM_APP + 1;
HHOOK CMenuBar::m_hHook = nullptr;
CMenuBar* CMenuBar::m_pThis = nullptr;

Expand All @@ -21,6 +20,7 @@ IMPLEMENT_DYNAMIC(CMenuBar, CToolBar)

BEGIN_MESSAGE_MAP(CMenuBar, CToolBar)
ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)
ON_WM_TIMER()
ON_WM_KILLFOCUS()
ON_WM_SETFOCUS()
ON_WM_MOUSEMOVE()
Expand All @@ -32,6 +32,7 @@ END_MESSAGE_MAP()

CMenuBar::CMenuBar()
: m_hMenu(nullptr)
, m_bAlwaysVisible(true)
, m_bActive(false)
, m_bMouseTracking(false)
, m_nMDIButtonDown(-1)
Expand Down Expand Up @@ -192,21 +193,41 @@ void CMenuBar::LoseFocus()
m_hwndOldFocus = nullptr;
ShowKeyboardCues(false);
GetToolBarCtrl().SetHotItem(-1);
if (!m_bAlwaysVisible)
SetTimer(MENUBAR_TIMER_ID, 200, nullptr);
}

void CMenuBar::Show(bool visible)
{
static_cast<CFrameWnd*>(AfxGetMainWnd())->ShowControlBar(this, visible, 0);
}

void CMenuBar::OnSetFocus(CWnd* pOldWnd)
{
m_bActive = true;
m_hwndOldFocus = pOldWnd->m_hWnd;
if (!m_bAlwaysVisible)
{
KillTimer(MENUBAR_TIMER_ID);
Show(true);
}
__super::OnSetFocus(pOldWnd);
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), WM_ENTERMENULOOP, 1, 0);
}

void CMenuBar::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == MENUBAR_TIMER_ID)
{
KillTimer(MENUBAR_TIMER_ID);
if (!m_bAlwaysVisible)
Show(false);
}
}

void CMenuBar::OnKillFocus(CWnd* pNewWnd)
{
LoseFocus();
__super::OnKillFocus(pNewWnd);
::PostMessage(AfxGetMainWnd()->GetSafeHwnd(), WM_EXITMENULOOP, 1, 0);
}

void CMenuBar::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
Expand Down Expand Up @@ -323,6 +344,12 @@ void CMenuBar::OnMenuBarMenuItem(UINT nID)
AfxGetMainWnd()->PostMessage(WM_KEYUP, VK_DOWN, 0);
}

if (!m_bAlwaysVisible)
{
KillTimer(MENUBAR_TIMER_ID);
Show(true);
}

m_hHook = SetWindowsHookEx(WH_MSGFILTER, MsgFilterProc, nullptr, GetCurrentThreadId());

CRect rc;
Expand All @@ -331,6 +358,10 @@ void CMenuBar::OnMenuBarMenuItem(UINT nID)
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, rc.left, rc.bottom, AfxGetMainWnd());

UnhookWindowsHookEx(m_hHook);
m_hHook = nullptr;

if (!m_bAlwaysVisible)
SetTimer(MENUBAR_TIMER_ID, 200, nullptr);
}

void CMenuBar::OnUpdateMenuBarMenuItem(CCmdUI* pCmdUI)
Expand Down
6 changes: 6 additions & 0 deletions Src/MenuBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class CMenuBar : public CToolBar
DECLARE_DYNAMIC(CMenuBar)
public:
constexpr static int FIRST_MENUID = 10000;
constexpr static UINT UWM_SHOWPOPUPMENU = WM_APP + 1;
constexpr static UINT MENUBAR_TIMER_ID = 100;

CMenuBar();

Expand All @@ -23,10 +25,12 @@ class CMenuBar : public CToolBar
void OnMenuBarMenuItem(UINT nID);
void OnUpdateMenuBarMenuItem(CCmdUI* pCmdUI);
BOOL PreTranslateMessage(MSG* pMsg);
void SetAlwaysVisible(bool visible) { m_bAlwaysVisible = visible; }

protected:
//{{AFX_MSG(CMenuBar)
afx_msg void OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnSetFocus(CWnd* pOldWnd);
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
Expand All @@ -44,9 +48,11 @@ class CMenuBar : public CToolBar
CRect GetMDIButtonRect(int nItem) const;
void ShowKeyboardCues(bool show);
void LoseFocus();
void Show(bool visible);

HMENU m_hMenu;
bool m_bActive;
bool m_bAlwaysVisible;
bool m_bMouseTracking;
bool m_bShowKeyboardCues;
int m_nMDIButtonDown;
Expand Down
8 changes: 4 additions & 4 deletions Src/Merge.rc
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ BEGIN
MENUITEM "&Big", ID_TOOLBAR_BIG
MENUITEM "&Huge", ID_TOOLBAR_HUGE
END
MENUITEM "Main Men&u", ID_VIEW_MAIN_MENU
MENUITEM "Men&u Bar", ID_VIEW_MENU_BAR
MENUITEM "&Status Bar", ID_VIEW_STATUS_BAR
END
POPUP "&Tools"
Expand Down Expand Up @@ -508,7 +508,7 @@ BEGIN
MENUITEM "&Big", ID_TOOLBAR_BIG
MENUITEM "&Huge", ID_TOOLBAR_HUGE
END
MENUITEM "Main Men&u", ID_VIEW_MAIN_MENU
MENUITEM "Men&u Bar", ID_VIEW_MENU_BAR
MENUITEM "&Status Bar", ID_VIEW_STATUS_BAR
MENUITEM SEPARATOR
MENUITEM "Com&pare Statistics...", ID_VIEW_DIR_STATISTICS
Expand Down Expand Up @@ -750,7 +750,7 @@ BEGIN
MENUITEM "&Big", ID_TOOLBAR_BIG
MENUITEM "&Huge", ID_TOOLBAR_HUGE
END
MENUITEM "Main Men&u", ID_VIEW_MAIN_MENU
MENUITEM "Men&u Bar", ID_VIEW_MENU_BAR
MENUITEM "&Status Bar", ID_VIEW_STATUS_BAR
MENUITEM "Diff &Pane", ID_VIEW_DETAIL_BAR
MENUITEM "Lo&cation Pane", ID_VIEW_LOCATION_BAR
Expand Down Expand Up @@ -4577,7 +4577,7 @@ END

STRINGTABLE
BEGIN
IDS_SHOW_MAIN_MENU "Main Men&u"
IDS_VIEW_MENU_BAR "Men&u Bar"
END

#endif // English (United States) resources
Expand Down
2 changes: 1 addition & 1 deletion Src/OptionsDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ inline const String OPT_SHOW_MISSING_LEFT_ONLY {_T("Settings/ShowMissingLeftOnly
inline const String OPT_SHOW_MISSING_MIDDLE_ONLY {_T("Settings/ShowMissingMiddleOnly"s)};
inline const String OPT_SHOW_MISSING_RIGHT_ONLY {_T("Settings/ShowMissingRightOnly"s)};
inline const String OPT_TREE_MODE {_T("Settings/TreeMode"s)};
inline const String OPT_HIDE_MAINMENU {_T("Settings/HideMainMenu"s)};

// Show/hide toolbar/statusbar/tabbar
inline const String OPT_SHOW_MENUBAR {_T("Settings/ShowMenubar"s)};
inline const String OPT_SHOW_TOOLBAR {_T("Settings/ShowToolbar"s)};
inline const String OPT_SHOW_STATUSBAR {_T("Settings/ShowStatusbar"s)};
inline const String OPT_SHOW_TABBAR {_T("Settings/ShowTabbar"s)};
Expand Down
2 changes: 1 addition & 1 deletion Src/OptionsInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ void Init(COptionsMgr *pOptions)
pOptions->InitOption(OPT_SHOW_MISSING_LEFT_ONLY, true);
pOptions->InitOption(OPT_SHOW_MISSING_MIDDLE_ONLY, true);
pOptions->InitOption(OPT_SHOW_MISSING_RIGHT_ONLY, true);
pOptions->InitOption(OPT_HIDE_MAINMENU, false);

pOptions->InitOption(OPT_SHOW_MENUBAR, true);
pOptions->InitOption(OPT_SHOW_TOOLBAR, true);
pOptions->InitOption(OPT_SHOW_STATUSBAR, true);
pOptions->InitOption(OPT_SHOW_TABBAR, true);
Expand Down
5 changes: 2 additions & 3 deletions Src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -1038,9 +1038,7 @@
#define ID_SEL_DIFF_COPY_2ND_3WAY 33274
#define ID_MICE_L2RNEXT 33275
#define ID_MICE_R2LNEXT 33276
#define ID_VIEW_MAIN_MENU 33277
#define ID_SHOW_MAIN_MENU 33278
#define IDS_SHOW_MAIN_MENU 33279
#define ID_VIEW_MENU_BAR 33277
#define ID_TABBAR_AUTO_MAXWIDTH 33351
#define ID_IMG_VIEWDIFFERENCES 33353
#define ID_IMG_ZOOM_25 33354
Expand Down Expand Up @@ -1791,6 +1789,7 @@
#define IDS_COPY_GRANULARITY_INLINE 44542
#define IDS_COPY_GRANULARITY_LINE 44543
#define IDS_COPY_GRANULARITY_Character 44544
#define IDS_VIEW_MENU_BAR 44545

// Next default values for new objects
//
Expand Down
3 changes: 3 additions & 0 deletions Translations/WinMerge/Arabic.po
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ msgstr "&كبير"
msgid "&Huge"
msgstr "&ضخم"

msgid "Men&u Bar"
msgstr ""

msgid "&Status Bar"
msgstr "شريط ال&حالة"

Expand Down
3 changes: 3 additions & 0 deletions Translations/WinMerge/Basque.po
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ msgstr "&Handia"
msgid "&Huge"
msgstr ""

msgid "Men&u Bar"
msgstr ""

#, c-format
msgid "&Status Bar"
msgstr "&Egoera Barra"
Expand Down
3 changes: 3 additions & 0 deletions Translations/WinMerge/Brazilian.po
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ msgstr "&Grande"
msgid "&Huge"
msgstr "&Enorme"

msgid "Men&u Bar"
msgstr ""

msgid "&Status Bar"
msgstr "&Barra de Status"

Expand Down
3 changes: 3 additions & 0 deletions Translations/WinMerge/Bulgarian.po
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ msgstr "&Голям"
msgid "&Huge"
msgstr "&Огромен"

msgid "Men&u Bar"
msgstr ""

msgid "&Status Bar"
msgstr "&Лента за състоянието"

Expand Down
Loading

0 comments on commit 844163f

Please sign in to comment.