Get rid of magic numbers
[TortoiseGit.git] / src / TortoiseMerge / AppUtils.cpp
blob2cde24060221e7c372d1bc376bad5d0538d95eef
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2010-2011,2014-2016 - TortoiseGit
4 // Copyright (C) 2006-2010, 2012-2014 - 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 "registry.h"
22 #include "AppUtils.h"
23 #include "PathUtils.h"
24 #include "UnicodeUtils.h"
25 #include "SysProgressDlg.h"
27 #pragma warning(push)
28 #include "svn_pools.h"
29 #include "svn_io.h"
30 #include "svn_path.h"
31 #include "svn_diff.h"
32 #include "svn_string.h"
33 #include "svn_utf.h"
34 #pragma warning(pop)
35 #include "Git.h"
36 #include "CreateProcessHelper.h"
37 #include "FormatMessageWrapper.h"
39 BOOL CAppUtils::GetVersionedFile(CString sPath, CString sVersion, CString sSavePath, CSysProgressDlg* progDlg, HWND hWnd /*=nullptr*/)
41 CString sSCMPath = CRegString(L"Software\\TortoiseGitMerge\\SCMPath", L"");
42 if (sSCMPath.IsEmpty())
44 // no path set, so use TortoiseGit as default
45 sSCMPath = CPathUtils::GetAppDirectory() + L"TortoiseGitProc.exe";
46 sSCMPath += L" /command:cat /path:\"%1\" /revision:%2 /savepath:\"%3\" /hwnd:%4";
48 CString sTemp;
49 sTemp.Format(L"%p", (void*)hWnd);
50 sSCMPath.Replace(L"%1", sPath);
51 sSCMPath.Replace(L"%2", sVersion);
52 sSCMPath.Replace(L"%3", sSavePath);
53 sSCMPath.Replace(L"%4", sTemp);
54 // start the external SCM program to fetch the specific version of the file
55 PROCESS_INFORMATION process;
56 if (!CCreateProcessHelper::CreateProcess(nullptr, sSCMPath.GetBuffer(), &process))
58 CFormatMessageWrapper errorDetails;
59 MessageBox(nullptr, errorDetails, L"TortoiseGitMerge", MB_OK | MB_ICONERROR);
60 return FALSE;
62 DWORD ret = 0;
65 ret = WaitForSingleObject(process.hProcess, 100);
66 } while ((ret == WAIT_TIMEOUT) && (!progDlg || !progDlg->HasUserCancelled()));
67 CloseHandle(process.hThread);
68 CloseHandle(process.hProcess);
70 if (progDlg && progDlg->HasUserCancelled())
72 return FALSE;
74 if (!PathFileExists(sSavePath))
75 return FALSE;
76 return TRUE;
79 bool CAppUtils::CreateUnifiedDiff(const CString& orig, const CString& modified, const CString& output, int contextsize, bool bShowError)
81 CString diffContext;
82 if (contextsize >= 0)
83 diffContext.Format(L"--unified=%d", contextsize);
84 CString cmd, err;
85 cmd.Format(L"git.exe diff --no-index %s -- \"%s\" \"%s\"", (LPCTSTR)diffContext, (LPCTSTR)orig, (LPCTSTR)modified);
87 int result = g_Git.RunLogFile(cmd, output, &err);
88 if (result != 0 && result != 1 && bShowError)
90 MessageBox(nullptr, L"Failed to create patch.\n" + err, L"TortoiseGit", MB_OK | MB_ICONERROR);
91 return false;
93 return true;
96 bool CAppUtils::HasClipboardFormat(UINT format)
98 if (OpenClipboard(nullptr))
100 UINT enumFormat = 0;
103 if (enumFormat == format)
105 CloseClipboard();
106 return true;
108 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);
109 CloseClipboard();
111 return false;
114 COLORREF CAppUtils::IntenseColor(long scale, COLORREF col)
116 // if the color is already dark (gray scale below 127),
117 // then lighten the color by 'scale', otherwise darken it
118 int Gray = (((int)GetRValue(col)) + GetGValue(col) + GetBValue(col))/3;
119 if (Gray > 127)
121 long red = MulDiv(GetRValue(col),(255-scale),255);
122 long green = MulDiv(GetGValue(col),(255-scale),255);
123 long blue = MulDiv(GetBValue(col),(255-scale),255);
125 return RGB(red, green, blue);
127 long R = MulDiv(255-GetRValue(col),scale,255)+GetRValue(col);
128 long G = MulDiv(255-GetGValue(col),scale,255)+GetGValue(col);
129 long B = MulDiv(255-GetBValue(col),scale,255)+GetBValue(col);
131 return RGB(R, G, B);