Extend static functions in CAppUtils with a window handle parameter
[TortoiseGit.git] / src / TortoiseProc / RenameDlg.cpp
blob7584c62010c949e23e90569ea06585d547f57750
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2009, 2011-2013, 2015-2017 - TortoiseGit
4 // Copyright (C) 2003-2011, 2013 - 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 #include "stdafx.h"
21 #include "TortoiseProc.h"
22 #include "TGitPath.h"
23 #include "RenameDlg.h"
24 #include "AppUtils.h"
25 #include "ControlsBridge.h"
26 #include "Git.h"
27 #include "MessageBox.h"
29 IMPLEMENT_DYNAMIC(CRenameDlg, CHorizontalResizableStandAloneDialog)
30 CRenameDlg::CRenameDlg(CWnd* pParent /*=nullptr*/)
31 : CHorizontalResizableStandAloneDialog(CRenameDlg::IDD, pParent)
32 , m_renameRequired(true)
33 , m_pInputValidator(nullptr)
34 , m_bBalloonVisible(false)
35 , m_sBaseDir(g_Git.m_CurrentDir)
39 CRenameDlg::~CRenameDlg()
43 void CRenameDlg::DoDataExchange(CDataExchange* pDX)
45 CHorizontalResizableStandAloneDialog::DoDataExchange(pDX);
46 DDX_Text(pDX, IDC_NAME, m_name);
50 BEGIN_MESSAGE_MAP(CRenameDlg, CHorizontalResizableStandAloneDialog)
51 ON_EN_SETFOCUS(IDC_NAME, &CRenameDlg::OnEnSetfocusName)
52 ON_BN_CLICKED(IDC_BUTTON_BROWSE_REF, &CRenameDlg::OnBnClickedButtonBrowseRef)
53 END_MESSAGE_MAP()
55 BOOL CRenameDlg::OnInitDialog()
57 CHorizontalResizableStandAloneDialog::OnInitDialog();
58 CAppUtils::MarkWindowAsUnpinnable(m_hWnd);
60 SHAutoComplete(GetDlgItem(IDC_NAME)->m_hWnd, SHACF_DEFAULT);
62 if (!m_windowtitle.IsEmpty())
63 this->SetWindowText(m_windowtitle);
64 if (!m_label.IsEmpty())
65 SetDlgItemText(IDC_LABEL, m_label);
67 if (!m_name.IsEmpty())
69 CString sTmp;
70 sTmp.Format(IDS_RENAME_INFO, (LPCWSTR)m_name);
71 SetDlgItemText(IDC_RENINFOLABEL, sTmp);
74 AddAnchor(IDC_RENINFOLABEL, TOP_LEFT, TOP_RIGHT);
75 AddAnchor(IDC_LABEL, TOP_LEFT);
76 AddAnchor(IDC_NAME, TOP_LEFT, TOP_RIGHT);
77 AddAnchor(IDC_BUTTON_BROWSE_REF, TOP_RIGHT);
78 AddAnchor(IDOK, BOTTOM_RIGHT);
79 AddAnchor(IDCANCEL, BOTTOM_RIGHT);
81 CControlsBridge::AlignHorizontally(this, IDC_LABEL, IDC_NAME);
82 if (hWndExplorer)
83 CenterWindow(CWnd::FromHandle(hWndExplorer));
84 EnableSaveRestore(L"RenameDlg");
85 m_originalName = m_name;
86 return TRUE;
89 void CRenameDlg::OnOK()
91 UpdateData();
92 m_name.Trim();
93 bool nameAllowed = ((m_originalName != m_name) || !m_renameRequired) && !m_name.IsEmpty();
94 if (!nameAllowed)
96 m_bBalloonVisible = true;
97 ShowEditBalloon(IDC_NAME, IDS_WARN_RENAMEREQUIRED, IDS_ERR_ERROR, TTI_ERROR);
98 return;
101 CTGitPath path(m_name);
102 if (!path.IsValidOnWindows() || !PathIsRelative(m_name))
104 m_bBalloonVisible = true;
105 ShowEditBalloon(IDC_NAME, IDS_WARN_NOVALIDPATH, IDS_ERR_ERROR, TTI_ERROR);
106 return;
109 if (m_pInputValidator)
111 CString sError = m_pInputValidator(IDC_NAME, m_name);
112 if (!sError.IsEmpty())
114 m_bBalloonVisible = true;
115 ShowEditBalloon(IDC_NAME, sError, CString(MAKEINTRESOURCE(IDS_ERR_ERROR)), TTI_ERROR);
116 return;
120 CHorizontalResizableStandAloneDialog::OnOK();
123 void CRenameDlg::OnCancel()
125 // find out if there's a balloon tip showing and if there is,
126 // hide that tooltip but do NOT exit the dialog.
127 if (m_bBalloonVisible)
129 Edit_HideBalloonTip(GetDlgItem(IDC_NAME)->GetSafeHwnd());
130 m_bBalloonVisible = false;
131 return;
134 CHorizontalResizableStandAloneDialog::OnCancel();
137 void CRenameDlg::OnEnSetfocusName()
139 m_bBalloonVisible = false;
142 void CRenameDlg::OnBnClickedButtonBrowseRef()
144 CString ext;
145 CString path;
146 if (!m_originalName.IsEmpty())
148 CTGitPath origname(m_sBaseDir);
149 origname.AppendPathString(m_originalName);
150 ext = origname.GetFileExtension();
151 path = origname.GetWinPathString();
154 if (CAppUtils::FileOpenSave(path, nullptr, AFX_IDD_FILESAVE, 0, false, GetSafeHwnd(), ext.Mid(1), !path.IsEmpty()))
156 GetDlgItem(IDC_NAME)->SetFocus();
157 CTGitPath target(path);
158 CString targetRoot;
159 if (!target.HasAdminDir(&targetRoot) || g_Git.m_CurrentDir.CompareNoCase(targetRoot) != 0)
161 CMessageBox::Show(GetSafeHwnd(), IDS_ERR_MUSTBESAMEWT, IDS_APPNAME, MB_OK | MB_ICONEXCLAMATION);
162 return;
164 CString relPath;
165 if (!PathRelativePathTo(CStrBuf(relPath, MAX_PATH), m_sBaseDir, FILE_ATTRIBUTE_DIRECTORY, path, FILE_ATTRIBUTE_DIRECTORY))
166 return;
167 if (CStringUtils::StartsWith(relPath, L".\\"))
168 relPath = relPath.Mid(2);
169 m_name = relPath;
170 UpdateData(FALSE);