Add checkbox to select all tags in Delete Remote Tag Dialog
[TortoiseGit.git] / src / Utils / MiscUI / MenuButton.cpp
blob40fcb3369008933a55862cdf2b0cfa3e0d8c9f40
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(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;
82 if (ret == 0)
83 SetCurrentEntry(ret);
84 return ret;
87 INT_PTR CMenuButton::AddEntries(const CStringArray& sEntries)
89 INT_PTR ret = -1;
90 for (int index = 0; index < sEntries.GetCount(); index++)
92 if (index == 0)
93 ret = AddEntry(sEntries[index]);
94 else
95 AddEntry(sEntries[index]);
97 return ret;
100 BEGIN_MESSAGE_MAP(CMenuButton, CMFCMenuButton)
101 ON_WM_DESTROY()
102 ON_CONTROL_REFLECT_EX(BN_CLICKED, &CMenuButton::OnClicked)
103 END_MESSAGE_MAP()
106 BOOL CMenuButton::OnClicked()
108 SetCurrentEntry(GetCurrentEntry());
110 // let the parent handle the message the usual way
111 return FALSE;
114 BOOL CMenuButton::PreTranslateMessage(MSG* pMsg)
116 if(pMsg->message == WM_KEYDOWN)
118 switch(pMsg->wParam)
120 case VK_F4:
121 OnShowMenu();
122 return TRUE;
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);
140 else
141 CMFCMenuButton::OnDraw(pDC, rect, uiState);
144 void CMenuButton::OnShowMenu()
146 m_bRealMenuIsActive = true;
147 CMFCMenuButton::OnShowMenu();
148 m_bRealMenuIsActive = false;