Don't use magic numbers
[TortoiseGit.git] / src / Utils / MiscUI / StandAloneDlg.h
blob7a843f6c02c3b4804a224a0d349921f8aa8403a0
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2015-2016 - TortoiseGit
4 // Copyright (C) 2003-2015 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
22 #include "ResizableDialog.h"
23 #include "TaskbarUUID.h"
24 #include "Tooltip.h"
25 #include "CommonDialogFunctions.h"
27 #pragma comment(lib, "htmlhelp.lib")
29 #define DIALOG_BLOCKHORIZONTAL 1
30 #define DIALOG_BLOCKVERTICAL 2
32 /**
33 * \ingroup TortoiseProc
35 * A template which can be used as the base-class of dialogs which form the main dialog
36 * of a 'dialog-style application'
37 * Just provides the boiler-plate code for dealing with application icons
39 * \remark Replace all references to CDialog or CResizableDialog in your dialog class with
40 * either CResizableStandAloneDialog, CStandAloneDialog or CStateStandAloneDialog, as appropriate
42 template <typename BaseType> class CStandAloneDialogTmpl : public BaseType, protected CommonDialogFunctions<BaseType>
44 protected:
45 CStandAloneDialogTmpl(UINT nIDTemplate, CWnd* pParentWnd = nullptr) : BaseType(nIDTemplate, pParentWnd), CommonDialogFunctions(this)
47 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
49 virtual BOOL OnInitDialog()
51 BaseType::OnInitDialog();
53 // Set the icon for this dialog. The framework does this automatically
54 // when the application's main window is not a dialog
55 SetIcon(m_hIcon, TRUE); // Set big icon
56 SetIcon(m_hIcon, FALSE); // Set small icon
58 EnableToolTips();
59 m_tooltips.Create(this);
61 return FALSE;
64 virtual BOOL PreTranslateMessage(MSG* pMsg)
66 m_tooltips.RelayEvent(pMsg, this);
67 if (pMsg->message == WM_KEYDOWN)
69 int nVirtKey = (int) pMsg->wParam;
71 if (nVirtKey == 'A' && (GetKeyState(VK_CONTROL) & 0x8000 ) )
73 TCHAR buffer[129];
74 ::GetClassName(pMsg->hwnd, buffer, _countof(buffer) - 1);
76 if (_wcsnicmp(buffer, L"EDIT", _countof(buffer) - 1) == 0)
78 ::PostMessage(pMsg->hwnd,EM_SETSEL,0,-1);
79 return TRUE;
83 return BaseType::PreTranslateMessage(pMsg);
85 afx_msg void OnPaint()
87 if (IsIconic())
89 CPaintDC dc(this); // device context for painting
91 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
93 // Center icon in client rectangle
94 int cxIcon = GetSystemMetrics(SM_CXICON);
95 int cyIcon = GetSystemMetrics(SM_CYICON);
96 CRect rect;
97 GetClientRect(&rect);
98 int x = (rect.Width() - cxIcon + 1) / 2;
99 int y = (rect.Height() - cyIcon + 1) / 2;
101 // Draw the icon
102 dc.DrawIcon(x, y, m_hIcon);
104 else
105 BaseType::OnPaint();
108 * Wrapper around the CWnd::EnableWindow() method, but
109 * makes sure that a control that has the focus is not disabled
110 * before the focus is passed on to the next control.
112 BOOL DialogEnableWindow(UINT nID, BOOL bEnable)
114 CWnd * pwndDlgItem = GetDlgItem(nID);
115 if (!pwndDlgItem)
116 return FALSE;
117 if (bEnable)
118 return pwndDlgItem->EnableWindow(bEnable);
119 if (GetFocus() == pwndDlgItem)
121 SendMessage(WM_NEXTDLGCTL, 0, FALSE);
123 return pwndDlgItem->EnableWindow(bEnable);
127 * Refreshes the cursor by forcing a WM_SETCURSOR message.
129 void RefreshCursor()
131 POINT pt;
132 GetCursorPos(&pt);
133 SetCursorPos(pt.x, pt.y);
136 protected:
137 CToolTips m_tooltips;
139 DECLARE_MESSAGE_MAP()
141 private:
142 HCURSOR OnQueryDragIcon()
144 return static_cast<HCURSOR>(m_hIcon);
146 protected:
147 virtual void HtmlHelp(DWORD_PTR dwData, UINT nCmd = 0x000F)
149 CWinApp* pApp = AfxGetApp();
150 ASSERT_VALID(pApp);
151 ASSERT(pApp->m_pszHelpFilePath);
152 // to call HtmlHelp the m_fUseHtmlHelp must be set in
153 // the application's constructor
154 ASSERT(pApp->m_eHelpType == afxHTMLHelp);
156 CWaitCursor wait;
158 PrepareForHelp();
159 // run the HTML Help engine
160 if (!::HtmlHelp(m_hWnd, pApp->m_pszHelpFilePath, nCmd, dwData))
162 AfxMessageBox(AFX_IDP_FAILED_TO_LAUNCH_HELP);
166 afx_msg LRESULT OnTaskbarButtonCreated(WPARAM /*wParam*/, LPARAM /*lParam*/)
168 SetUUIDOverlayIcon(m_hWnd);
169 return 0;
172 HICON m_hIcon;
175 class CStateDialog : public CDialog, public CResizableWndState
177 public:
178 CStateDialog()
179 : CDialog()
180 , m_bEnableSaveRestore(false)
181 , m_bRectOnly(false)
183 CStateDialog(UINT nIDTemplate, CWnd* pParentWnd = nullptr)
184 : CDialog(nIDTemplate, pParentWnd)
185 , m_bEnableSaveRestore(false)
186 , m_bRectOnly(false)
188 CStateDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = nullptr)
189 : CDialog(lpszTemplateName, pParentWnd)
190 , m_bEnableSaveRestore(false)
191 , m_bRectOnly(false)
193 virtual ~CStateDialog() {};
195 private:
196 // flags
197 bool m_bEnableSaveRestore;
198 bool m_bRectOnly;
200 // internal status
201 CString m_sSection; // section name (identifies a parent window)
203 protected:
204 // overloaded method, but since this dialog class is for non-resizable dialogs,
205 // the bHorzResize and bVertResize params are ignored and passed as false
206 // to the base method.
207 void EnableSaveRestore(LPCTSTR pszSection, bool bRectOnly = FALSE, BOOL bHorzResize = TRUE, BOOL bVertResize = TRUE)
209 UNREFERENCED_PARAMETER(bHorzResize);
210 UNREFERENCED_PARAMETER(bVertResize);
211 m_sSection = pszSection;
213 m_bEnableSaveRestore = true;
214 m_bRectOnly = bRectOnly;
216 // restore immediately
217 LoadWindowRect(pszSection, bRectOnly, false, false);
220 virtual CWnd* GetResizableWnd() const
222 // make the layout know its parent window
223 return CWnd::FromHandle(m_hWnd);
226 afx_msg void OnDestroy()
228 if (m_bEnableSaveRestore)
229 SaveWindowRect(m_sSection, m_bRectOnly);
230 CDialog::OnDestroy();
233 DECLARE_MESSAGE_MAP()
236 class CResizableStandAloneDialog : public CStandAloneDialogTmpl<CResizableDialog>
238 public:
239 CResizableStandAloneDialog(UINT nIDTemplate, CWnd* pParentWnd = nullptr);
241 private:
242 DECLARE_DYNAMIC(CResizableStandAloneDialog)
244 protected:
245 virtual BOOL OnInitDialog();
246 afx_msg void OnSizing(UINT fwSide, LPRECT pRect);
247 afx_msg void OnMoving(UINT fwSide, LPRECT pRect);
248 afx_msg void OnNcMButtonUp(UINT nHitTest, CPoint point);
249 afx_msg void OnNcRButtonUp(UINT nHitTest, CPoint point);
250 afx_msg LRESULT OnNcHitTest(CPoint point);
252 DECLARE_MESSAGE_MAP()
254 protected:
255 int m_nResizeBlock;
256 long m_width;
257 long m_height;
259 void BlockResize(int block)
261 m_nResizeBlock = block;
264 void EnableSaveRestore(LPCTSTR pszSection, bool bRectOnly = FALSE)
266 // call the base method with the bHorzResize and bVertResize parameters
267 // figured out from the resize block flags.
268 __super::EnableSaveRestore(pszSection, bRectOnly, (m_nResizeBlock & DIALOG_BLOCKHORIZONTAL) == 0, (m_nResizeBlock & DIALOG_BLOCKVERTICAL) == 0);
271 private:
272 bool m_bVertical;
273 bool m_bHorizontal;
274 CRect m_rcOrgWindowRect;
277 class CStandAloneDialog : public CStandAloneDialogTmpl<CDialog>
279 public:
280 CStandAloneDialog(UINT nIDTemplate, CWnd* pParentWnd = nullptr);
281 protected:
282 DECLARE_MESSAGE_MAP()
283 private:
284 DECLARE_DYNAMIC(CStandAloneDialog)
287 class CStateStandAloneDialog : public CStandAloneDialogTmpl<CStateDialog>
289 public:
290 CStateStandAloneDialog(UINT nIDTemplate, CWnd* pParentWnd = nullptr);
291 protected:
292 DECLARE_MESSAGE_MAP()
293 private:
294 DECLARE_DYNAMIC(CStateStandAloneDialog)
297 const UINT TaskBarButtonCreated = RegisterWindowMessage(L"TaskbarButtonCreated");
299 BEGIN_TEMPLATE_MESSAGE_MAP(CStandAloneDialogTmpl, BaseType, BaseType)
300 ON_WM_PAINT()
301 ON_WM_QUERYDRAGICON()
302 ON_REGISTERED_MESSAGE(TaskBarButtonCreated, OnTaskbarButtonCreated)
303 END_MESSAGE_MAP()