Upgrade libgit2
[TortoiseGit.git] / src / TortoiseUDiff / TortoiseUDiff.cpp
blob34f484df78caa25da1fc864ec6d5543b07c36793
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2011-2023 - TortoiseGit
4 // Copyright (C) 2003-2008, 2010-2012, 2014-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 #include "stdafx.h"
21 #include "TortoiseUDiff.h"
22 #include "MainWindow.h"
23 #include "CmdLineParser.h"
24 #include "TaskbarUUID.h"
25 #include "registry.h"
26 #include "LangDll.h"
27 #include "Monitor.h"
28 #include "../version.h"
29 #include "../Utils/CrashReport.h"
30 #pragma warning(push)
31 #pragma warning(disable: 4458)
32 #include <GdiPlus.h>
33 #pragma warning(pop)
34 #include <commctrl.h>
35 #pragma comment(lib, "comctl32.lib")
37 #pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
39 HINSTANCE hResource; // the resource dll
41 int APIENTRY wWinMain(HINSTANCE hInstance,
42 HINSTANCE /*hPrevInstance*/,
43 LPWSTR lpCmdLine,
44 int /*nCmdShow*/)
46 SetDllDirectory(L"");
47 SetTaskIDPerUUID();
48 CRegStdDWORD loc = CRegStdDWORD(L"Software\\TortoiseGit\\LanguageID", 1033);
49 long langId = loc;
50 MSG msg;
51 HACCEL hAccelTable;
53 #if ENABLE_CRASHHANLDER && !_M_ARM64
54 CCrashReportTGit crasher(L"TortoiseGitUDiff " TEXT(APP_X64_STRING), TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD, TGIT_VERDATE);
55 CCrashReport::Instance().AddUserInfoToReport(L"CommandLine", GetCommandLine());
56 #endif
58 CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
59 SCOPE_EXIT { CoUninitialize(); };
61 CLangDll langDLL;
62 hResource = langDLL.Init(L"TortoiseGitUDiff", langId);
63 if (!hResource)
64 hResource = hInstance;
66 CCmdLineParser parser(lpCmdLine);
68 if (parser.HasKey(L"?") || parser.HasKey(L"help"))
70 ResString rHelp(hResource, IDS_COMMANDLINEHELP);
71 MessageBox(nullptr, rHelp, L"TortoiseGitUDiff", MB_ICONINFORMATION);
72 return 0;
75 INITCOMMONCONTROLSEX used = {
76 sizeof(INITCOMMONCONTROLSEX),
77 ICC_STANDARD_CLASSES | ICC_BAR_CLASSES
79 InitCommonControlsEx(&used);
81 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
82 ULONG_PTR gdiplusToken;
83 Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
85 SCOPE_EXIT { Gdiplus::GdiplusShutdown(gdiplusToken); };
87 CMainWindow mainWindow(hResource);
88 mainWindow.SetRegistryPath(L"Software\\TortoiseGit\\UDiffViewerWindowPos_" + GetMonitorSetupHash());
89 if (parser.HasVal(L"title"))
90 mainWindow.SetTitle(parser.GetVal(L"title"));
91 else if (parser.HasVal(L"patchfile"))
92 mainWindow.SetTitle(parser.GetVal(L"patchfile"));
93 else if (lpCmdLine[0] && !parser.HasKey(L"p"))
95 // remove double quotes
96 std::wstring path = lpCmdLine;
97 path.erase(std::remove(path.begin(), path.end(), L'"'), path.end());
98 mainWindow.SetTitle(path.c_str());
100 else
102 ResString rPipeTitle(hResource, IDS_PIPETITLE);
103 mainWindow.SetTitle(rPipeTitle);
106 if (!mainWindow.RegisterAndCreateWindow())
107 return -1;
109 bool bLoadedSuccessfully = false;
110 if ((lpCmdLine[0] == L'\0') || (parser.HasKey(L"p")))
112 // input from console pipe
113 // set console to raw mode
114 DWORD oldMode;
115 GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldMode);
116 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
118 bLoadedSuccessfully = mainWindow.LoadFile(GetStdHandle(STD_INPUT_HANDLE), parser.HasKey(L"p"));
120 else if (parser.HasVal(L"patchfile"))
121 bLoadedSuccessfully = mainWindow.LoadFile(parser.GetVal(L"patchfile"));
122 else if (lpCmdLine[0] != L'\0')
124 // remove double quotes
125 std::wstring path = lpCmdLine;
126 path.erase(std::remove(path.begin(), path.end(), '"'), path.end());
127 bLoadedSuccessfully = mainWindow.LoadFile(path.c_str());
130 if (!bLoadedSuccessfully)
131 return 1;
133 ::ShowWindow(mainWindow.GetHWNDEdit(), SW_SHOW);
134 ::SetFocus(mainWindow.GetHWNDEdit());
136 hAccelTable = LoadAccelerators(hResource, MAKEINTRESOURCE(IDC_TORTOISEUDIFF));
138 // Main message loop:
139 while (GetMessage(&msg, nullptr, 0, 0))
141 if (!TranslateAccelerator(mainWindow, hAccelTable, &msg))
143 TranslateMessage(&msg);
144 DispatchMessage(&msg);
147 return static_cast<int>(msg.wParam);