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.
22 #include "ResizableDialog.h"
23 #include "TaskbarUUID.h"
25 #include "CommonDialogFunctions.h"
27 #pragma comment(lib, "htmlhelp.lib")
29 #define DIALOG_BLOCKHORIZONTAL 1
30 #define DIALOG_BLOCKVERTICAL 2
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
>
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
59 m_tooltips
.Create(this);
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 ) )
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);
83 return BaseType::PreTranslateMessage(pMsg
);
85 afx_msg
void OnPaint()
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
);
98 int x
= (rect
.Width() - cxIcon
+ 1) / 2;
99 int y
= (rect
.Height() - cyIcon
+ 1) / 2;
102 dc
.DrawIcon(x
, y
, m_hIcon
);
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
);
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.
133 SetCursorPos(pt
.x
, pt
.y
);
137 CToolTips m_tooltips
;
139 DECLARE_MESSAGE_MAP()
142 HCURSOR
OnQueryDragIcon()
144 return static_cast<HCURSOR
>(m_hIcon
);
147 virtual void HtmlHelp(DWORD_PTR dwData
, UINT nCmd
= 0x000F)
149 CWinApp
* pApp
= AfxGetApp();
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
);
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
);
175 class CStateDialog
: public CDialog
, public CResizableWndState
180 , m_bEnableSaveRestore(false)
183 CStateDialog(UINT nIDTemplate
, CWnd
* pParentWnd
= nullptr)
184 : CDialog(nIDTemplate
, pParentWnd
)
185 , m_bEnableSaveRestore(false)
188 CStateDialog(LPCTSTR lpszTemplateName
, CWnd
* pParentWnd
= nullptr)
189 : CDialog(lpszTemplateName
, pParentWnd
)
190 , m_bEnableSaveRestore(false)
193 virtual ~CStateDialog() {};
197 bool m_bEnableSaveRestore
;
201 CString m_sSection
; // section name (identifies a parent window)
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
>
239 CResizableStandAloneDialog(UINT nIDTemplate
, CWnd
* pParentWnd
= nullptr);
242 DECLARE_DYNAMIC(CResizableStandAloneDialog
)
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()
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);
274 CRect m_rcOrgWindowRect
;
277 class CStandAloneDialog
: public CStandAloneDialogTmpl
<CDialog
>
280 CStandAloneDialog(UINT nIDTemplate
, CWnd
* pParentWnd
= nullptr);
282 DECLARE_MESSAGE_MAP()
284 DECLARE_DYNAMIC(CStandAloneDialog
)
287 class CStateStandAloneDialog
: public CStandAloneDialogTmpl
<CStateDialog
>
290 CStateStandAloneDialog(UINT nIDTemplate
, CWnd
* pParentWnd
= nullptr);
292 DECLARE_MESSAGE_MAP()
294 DECLARE_DYNAMIC(CStateStandAloneDialog
)
297 const UINT TaskBarButtonCreated
= RegisterWindowMessage(L
"TaskbarButtonCreated");
299 BEGIN_TEMPLATE_MESSAGE_MAP(CStandAloneDialogTmpl
, BaseType
, BaseType
)
301 ON_WM_QUERYDRAGICON()
302 ON_REGISTERED_MESSAGE(TaskBarButtonCreated
, OnTaskbarButtonCreated
)