Apply backgroundcolors.patch
[TortoiseGit.git] / src / TortoiseMerge / AppUtils.cpp
blobd631ba4ddb3ec66d0ee56f02e155e8105914709a
1 // TortoiseGitMerge - a Diff/Patch program
3 // Copyright (C) 2010-2011, 2014-2016, 2019-2020 - 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"
38 #include "ClipboardHelper.h"
40 BOOL CAppUtils::GetVersionedFile(CString sPath, CString sVersion, CString sSavePath, CSysProgressDlg* progDlg, HWND hWnd /*=nullptr*/)
42 CString sSCMPath = CRegString(L"Software\\TortoiseGitMerge\\SCMPath", L"");
43 if (sSCMPath.IsEmpty())
45 // no path set, so use TortoiseGit as default
46 sSCMPath = CPathUtils::GetAppDirectory() + L"TortoiseGitProc.exe";
47 sSCMPath += L" /command:cat /path:\"%1\" /revision:%2 /savepath:\"%3\" /hwnd:%4";
49 CString sTemp;
50 sTemp.Format(L"%p", static_cast<void*>(hWnd));
51 sSCMPath.Replace(L"%1", sPath);
52 sSCMPath.Replace(L"%2", sVersion);
53 sSCMPath.Replace(L"%3", sSavePath);
54 sSCMPath.Replace(L"%4", sTemp);
55 // start the external SCM program to fetch the specific version of the file
56 PROCESS_INFORMATION process;
57 if (!CCreateProcessHelper::CreateProcess(nullptr, sSCMPath.GetBuffer(), &process))
59 CFormatMessageWrapper errorDetails;
60 MessageBox(nullptr, errorDetails, L"TortoiseGitMerge", MB_OK | MB_ICONERROR);
61 return FALSE;
63 DWORD ret = 0;
66 ret = WaitForSingleObject(process.hProcess, 100);
67 } while ((ret == WAIT_TIMEOUT) && (!progDlg || !progDlg->HasUserCancelled()));
68 CloseHandle(process.hThread);
69 CloseHandle(process.hProcess);
71 if (progDlg && progDlg->HasUserCancelled())
73 return FALSE;
75 if (!PathFileExists(sSavePath))
76 return FALSE;
77 return TRUE;
80 bool CAppUtils::CreateUnifiedDiff(const CString& orig, const CString& modified, const CString& output, int contextsize, bool bShowError)
82 CString diffContext;
83 if (contextsize >= 0)
84 diffContext.Format(L"--unified=%d", contextsize);
85 CString cmd, err;
86 cmd.Format(L"git.exe diff --no-index %s -- \"%s\" \"%s\"", static_cast<LPCWSTR>(diffContext), static_cast<LPCWSTR>(orig), static_cast<LPCWSTR>(modified));
88 int result = g_Git.RunLogFile(cmd, output, &err);
89 if (result != 0 && result != 1 && bShowError)
91 MessageBox(nullptr, L"Failed to create patch.\n" + err, L"TortoiseGit", MB_OK | MB_ICONERROR);
92 return false;
94 return true;
97 bool CAppUtils::HasClipboardFormat(UINT format)
99 CClipboardHelper clipboardHelper;
100 if (clipboardHelper.Open(nullptr))
102 UINT enumFormat = 0;
105 if (enumFormat == format)
107 CloseClipboard();
108 return true;
110 } while((enumFormat = EnumClipboardFormats(enumFormat))!=0);
112 return false;
115 COLORREF CAppUtils::IntenseColor(long scale, COLORREF col)
117 // if the color is already dark (gray scale below 127),
118 // then lighten the color by 'scale', otherwise darken it
119 int Gray = (static_cast<int>(GetRValue(col)) + GetGValue(col) + GetBValue(col)) / 3;
120 if (Gray > 127)
122 long red = MulDiv(GetRValue(col),(255-scale),255);
123 long green = MulDiv(GetGValue(col),(255-scale),255);
124 long blue = MulDiv(GetBValue(col),(255-scale),255);
126 return RGB(red, green, blue);
128 long R = MulDiv(255-GetRValue(col),scale,255)+GetRValue(col);
129 long G = MulDiv(255-GetGValue(col),scale,255)+GetGValue(col);
130 long B = MulDiv(255-GetBValue(col),scale,255)+GetBValue(col);
132 return RGB(R, G, B);