Make the font of CMenuButton identical with parent dialog
[TortoiseGit.git] / src / Utils / MiscUI / MenuButton.cpp
blobd45aecddd43a7b8e3390120ec81abc6a6fe4c6b7
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011,2015 - 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 void CMenuButton::FixFont()
114 CWnd* pWnd = GetParent();
116 if (pWnd == nullptr)
117 return;
119 CFont* pFont = pWnd->GetFont();
120 if (pFont == nullptr)
121 return;
123 LOGFONT logfont;
124 if (pFont->GetLogFont(&logfont) == 0)
125 return;
127 if (m_Font.CreateFontIndirect(&logfont) == FALSE)
128 return;
130 SetFont(&m_Font);
131 Invalidate();
134 BEGIN_MESSAGE_MAP(CMenuButton, CMFCMenuButton)
135 ON_WM_DESTROY()
136 ON_CONTROL_REFLECT_EX(BN_CLICKED, &CMenuButton::OnClicked)
137 END_MESSAGE_MAP()
140 BOOL CMenuButton::OnClicked()
142 SetCurrentEntry(GetCurrentEntry());
144 // let the parent handle the message the usual way
145 return FALSE;
148 BOOL CMenuButton::PreTranslateMessage(MSG* pMsg)
150 if(pMsg->message == WM_KEYDOWN)
152 switch(pMsg->wParam)
154 case VK_RETURN:
155 case VK_SPACE:
156 if (m_bMenuIsActive && !m_bRealMenuIsActive)
158 GetParent()->SendMessage(WM_COMMAND, MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED), (LPARAM)m_hWnd);
159 return TRUE;
161 case VK_F4:
162 OnShowMenu();
163 return TRUE;
167 return CMFCMenuButton::PreTranslateMessage(pMsg);
170 void CMenuButton::OnDestroy()
172 m_sEntries.RemoveAll();
174 CMFCMenuButton::OnDestroy();
177 void CMenuButton::OnDraw(CDC* pDC, const CRect& rect, UINT uiState)
179 if (m_bMenuIsActive && !m_bRealMenuIsActive)
180 CMFCButton::OnDraw(pDC, rect, uiState);
181 else
182 CMFCMenuButton::OnDraw(pDC, rect, uiState);
184 if (m_Font.m_hObject == nullptr)
185 FixFont();
188 void CMenuButton::OnShowMenu()
190 m_bRealMenuIsActive = true;
191 CMFCMenuButton::OnShowMenu();
192 m_bRealMenuIsActive = false;