1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011 - 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(MF_BYPOSITION
, 0);
70 m_sEntries
.RemoveAll();
71 m_nDefault
= m_nMenuResult
= -1;
72 m_bMenuIsActive
= TRUE
;
75 INT_PTR
CMenuButton::AddEntry(const CString
& sEntry
)
77 INT_PTR ret
= m_sEntries
.Add(sEntry
);
78 m_btnMenu
.AppendMenu(MF_STRING
| MF_BYCOMMAND
, m_sEntries
.GetCount(), sEntry
);
79 if (m_sEntries
.GetCount() == 2)
80 m_bMenuIsActive
= FALSE
;
87 INT_PTR
CMenuButton::AddEntries(const CStringArray
& sEntries
)
90 for (int index
= 0; index
< sEntries
.GetCount(); index
++)
93 ret
= AddEntry(sEntries
[index
]);
95 AddEntry(sEntries
[index
]);
100 BEGIN_MESSAGE_MAP(CMenuButton
, CMFCMenuButton
)
102 ON_CONTROL_REFLECT_EX(BN_CLICKED
, &CMenuButton::OnClicked
)
106 BOOL
CMenuButton::OnClicked()
108 SetCurrentEntry(GetCurrentEntry());
110 // let the parent handle the message the usual way
114 BOOL
CMenuButton::PreTranslateMessage(MSG
* pMsg
)
116 if(pMsg
->message
== WM_KEYDOWN
)
126 return CMFCMenuButton::PreTranslateMessage(pMsg
);
129 void CMenuButton::OnDestroy()
131 m_sEntries
.RemoveAll();
133 CMFCMenuButton::OnDestroy();
136 void CMenuButton::OnDraw(CDC
* pDC
, const CRect
& rect
, UINT uiState
)
138 if (m_bMenuIsActive
&& !m_bRealMenuIsActive
)
139 CMFCButton::OnDraw(pDC
, rect
, uiState
);
141 CMFCMenuButton::OnDraw(pDC
, rect
, uiState
);
144 void CMenuButton::OnShowMenu()
146 m_bRealMenuIsActive
= true;
147 CMFCMenuButton::OnShowMenu();
148 m_bRealMenuIsActive
= false;