Revert "Workaround cannot operate with keyboard when MenuButton has only 1 entry"
[TortoiseGit.git] / src / Utils / MiscUI / MenuButton.cpp
bloba179e9e8fbb62653bf4d3fc4dcd828ca19756b57
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011 - Sven Strickroth <email@cs-ware.de>
5 //based on:
6 // Copyright (C) 2003-2006,2008 - Stefan Kueng
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software Foundation,
20 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "stdafx.h"
23 #include "MenuButton.h"
26 #ifdef _DEBUG
27 #undef THIS_FILE
28 static char THIS_FILE[]=__FILE__;
29 #define new DEBUG_NEW
30 #endif
33 IMPLEMENT_DYNCREATE(CMenuButton, CMFCMenuButton)
35 CMenuButton::CMenuButton(void) : CMFCMenuButton()
36 , m_nDefault(-1)
37 , m_bMarkDefault(TRUE)
38 , m_bRealMenuIsActive(false)
40 m_bOSMenu = TRUE;
41 m_bDefaultClick = TRUE;
42 m_bTransparent = TRUE;
43 m_bMenuIsActive = TRUE;
45 m_btnMenu.CreatePopupMenu();
46 m_hMenu = m_btnMenu.GetSafeHmenu();
49 CMenuButton::~CMenuButton(void)
53 bool CMenuButton::SetCurrentEntry(INT_PTR entry)
55 if (entry < 0 || entry >= m_sEntries.GetCount())
56 return false;
58 m_nDefault = entry + 1;
59 SetWindowText(m_sEntries[entry]);
60 if (m_bMarkDefault)
61 m_btnMenu.SetDefaultItem((UINT)m_nDefault, FALSE);
63 return true;
66 void CMenuButton::RemoveAll()
68 for (int index = 0; index < m_sEntries.GetCount(); index++)
69 m_btnMenu.RemoveMenu(0, MF_BYPOSITION);
70 m_sEntries.RemoveAll();
71 m_nDefault = m_nMenuResult = -1;
72 m_bMenuIsActive = TRUE;
75 INT_PTR CMenuButton::AddEntry(UINT iconId, const CString& sEntry)
77 INT_PTR ret = m_sEntries.Add(sEntry);
78 m_btnMenu.AppendMenuIcon(m_sEntries.GetCount(), sEntry, iconId);
79 if (m_sEntries.GetCount() == 2)
80 m_bMenuIsActive = FALSE;
82 if (ret == 0)
83 SetCurrentEntry(ret);
84 return ret;
87 INT_PTR CMenuButton::AddEntry(const CString& sEntry)
89 INT_PTR ret = m_sEntries.Add(sEntry);
90 m_btnMenu.AppendMenuIcon(m_sEntries.GetCount(), sEntry);
91 if (m_sEntries.GetCount() == 2)
92 m_bMenuIsActive = FALSE;
94 if (ret == 0)
95 SetCurrentEntry(ret);
96 return ret;
99 INT_PTR CMenuButton::AddEntries(const CStringArray& sEntries)
101 INT_PTR ret = -1;
102 for (int index = 0; index < sEntries.GetCount(); index++)
104 if (index == 0)
105 ret = AddEntry(sEntries[index]);
106 else
107 AddEntry(sEntries[index]);
109 return ret;
112 BEGIN_MESSAGE_MAP(CMenuButton, CMFCMenuButton)
113 ON_WM_DESTROY()
114 ON_CONTROL_REFLECT_EX(BN_CLICKED, &CMenuButton::OnClicked)
115 END_MESSAGE_MAP()
118 BOOL CMenuButton::OnClicked()
120 SetCurrentEntry(GetCurrentEntry());
122 // let the parent handle the message the usual way
123 return FALSE;
126 BOOL CMenuButton::PreTranslateMessage(MSG* pMsg)
128 if(pMsg->message == WM_KEYDOWN)
130 switch(pMsg->wParam)
132 case VK_F4:
133 OnShowMenu();
134 return TRUE;
138 return CMFCMenuButton::PreTranslateMessage(pMsg);
141 void CMenuButton::OnDestroy()
143 m_sEntries.RemoveAll();
145 CMFCMenuButton::OnDestroy();
148 void CMenuButton::OnDraw(CDC* pDC, const CRect& rect, UINT uiState)
150 if (m_bMenuIsActive && !m_bRealMenuIsActive)
151 CMFCButton::OnDraw(pDC, rect, uiState);
152 else
153 CMFCMenuButton::OnDraw(pDC, rect, uiState);
156 void CMenuButton::OnShowMenu()
158 m_bRealMenuIsActive = true;
159 CMFCMenuButton::OnShowMenu();
160 m_bRealMenuIsActive = false;