1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011,2015 - Sven Strickroth <email@cs-ware.de>
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.
23 #include "MenuButton.h"
28 static char THIS_FILE
[]=__FILE__
;
33 IMPLEMENT_DYNCREATE(CMenuButton
, CMFCMenuButton
)
35 CMenuButton::CMenuButton(void) : CMFCMenuButton()
37 , m_bMarkDefault(TRUE
)
38 , m_bRealMenuIsActive(false)
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())
58 m_nDefault
= entry
+ 1;
59 SetWindowText(m_sEntries
[entry
]);
61 m_btnMenu
.SetDefaultItem((UINT
)m_nDefault
, FALSE
);
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
;
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
;
99 INT_PTR
CMenuButton::AddEntries(const CStringArray
& sEntries
)
102 for (int index
= 0; index
< sEntries
.GetCount(); index
++)
105 ret
= AddEntry(sEntries
[index
]);
107 AddEntry(sEntries
[index
]);
112 void CMenuButton::FixFont()
114 CWnd
* pWnd
= GetParent();
119 CFont
* pFont
= pWnd
->GetFont();
120 if (pFont
== nullptr)
124 if (pFont
->GetLogFont(&logfont
) == 0)
127 if (m_Font
.CreateFontIndirect(&logfont
) == FALSE
)
134 BEGIN_MESSAGE_MAP(CMenuButton
, CMFCMenuButton
)
136 ON_CONTROL_REFLECT_EX(BN_CLICKED
, &CMenuButton::OnClicked
)
140 BOOL
CMenuButton::OnClicked()
142 SetCurrentEntry(GetCurrentEntry());
144 // let the parent handle the message the usual way
148 BOOL
CMenuButton::PreTranslateMessage(MSG
* pMsg
)
150 if(pMsg
->message
== WM_KEYDOWN
)
156 if (m_bMenuIsActive
&& !m_bRealMenuIsActive
)
158 GetParent()->SendMessage(WM_COMMAND
, MAKEWPARAM(GetDlgCtrlID(), BN_CLICKED
), (LPARAM
)m_hWnd
);
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
);
182 CMFCMenuButton::OnDraw(pDC
, rect
, uiState
);
184 if (m_Font
.m_hObject
== nullptr)
188 void CMenuButton::OnShowMenu()
190 m_bRealMenuIsActive
= true;
191 CMFCMenuButton::OnShowMenu();
192 m_bRealMenuIsActive
= false;