Update libgit2
[TortoiseGit.git] / src / TortoiseGitBlame / TortoiseGitBlame.cpp
blob367d687f7fac307f3134d415243e78c7cc30370c
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2014 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // TortoiseGitBlame.cpp : Defines the class behaviors for the application.
23 #include "stdafx.h"
24 #include "afxwinappex.h"
25 #include "TortoiseGitBlame.h"
26 #include "MainFrm.h"
27 #include "../version.h"
28 #include "../Utils/CrashReport.h"
30 #include "TortoiseGitBlameDoc.h"
31 #include "TortoiseGitBlameView.h"
32 #include "CmdLineParser.h"
33 #include "PathUtils.h"
34 #include "CommonAppUtils.h"
35 #include "TaskbarUUID.h"
37 #ifdef _DEBUG
38 #define new DEBUG_NEW
39 #endif
42 // CTortoiseGitBlameApp
44 BEGIN_MESSAGE_MAP(CTortoiseGitBlameApp, CWinAppEx)
45 ON_COMMAND(ID_APP_ABOUT, &CTortoiseGitBlameApp::OnAppAbout)
46 // Standard file based document commands
47 ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
48 ON_COMMAND(ID_FILE_SETTINGS, &CTortoiseGitBlameApp::OnFileSettings)
49 END_MESSAGE_MAP()
52 // CTortoiseGitBlameApp construction
54 CTortoiseGitBlameApp::CTortoiseGitBlameApp()
56 SetDllDirectory(L"");
57 SetTaskIDPerUUID();
58 // prevent from inheriting %GIT_DIR% from parent process by resetting it,
59 // use MSVC function instead of Windows API because MSVC runtime caches environment variables
60 _tputenv(_T("GIT_DIR="));
61 #if ENABLE_CRASHHANLDER
62 CCrashReportTGit crasher(L"TortoiseGitBlame " _T(APP_X64_STRING), TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD, TGIT_VERDATE);
63 CCrashReport::Instance().AddUserInfoToReport(L"CommandLine", GetCommandLine());
64 #endif
65 EnableHtmlHelp();
66 git_libgit2_init();
67 m_nAppLook = 0;
68 m_gdiplusToken = NULL;
69 m_bHiColorIcons = TRUE;
72 CTortoiseGitBlameApp::~CTortoiseGitBlameApp()
74 git_libgit2_shutdown();
77 // The one and only CTortoiseGitBlameApp object
78 CTortoiseGitBlameApp theApp;
79 CString sOrigCWD;
80 CString g_sGroupingUUID;
81 CString g_sGroupingIcon;
82 bool g_bGroupingRemoveIcon = false;
84 // CTortoiseGitBlameApp initialization
86 BOOL CTortoiseGitBlameApp::InitInstance()
89 DWORD len = GetCurrentDirectory(0, NULL);
90 if (len)
92 std::unique_ptr<TCHAR[]> originalCurrentDirectory(new TCHAR[len]);
93 if (GetCurrentDirectory(len, originalCurrentDirectory.get()))
95 sOrigCWD = originalCurrentDirectory.get();
96 sOrigCWD = CPathUtils::GetLongPathname(sOrigCWD);
101 //set the resource dll for the required language
102 CRegDWORD loc = CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
103 long langId = loc;
104 CString langDll;
105 HINSTANCE hInst = NULL;
108 langDll.Format(_T("%sLanguages\\TortoiseGitBlame%ld.dll"), (LPCTSTR)CPathUtils::GetAppParentDirectory(), langId);
110 hInst = LoadLibrary(langDll);
111 CString sVer = _T(STRPRODUCTVER);
112 CString sFileVer = CPathUtils::GetVersionFromFile(langDll);
113 if (sFileVer.Compare(sVer)!=0)
115 FreeLibrary(hInst);
116 hInst = NULL;
118 if (hInst != NULL)
119 AfxSetResourceHandle(hInst);
120 else
122 DWORD lid = SUBLANGID(langId);
123 lid--;
124 if (lid > 0)
126 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
128 else
129 langId = 0;
131 } while ((hInst == NULL) && (langId != 0));
132 TCHAR buf[6] = { 0 };
133 _tcscpy_s(buf, _T("en"));
134 langId = loc;
135 CString sHelppath;
136 sHelppath = this->m_pszHelpFilePath;
137 sHelppath = sHelppath.MakeLower();
138 sHelppath.Replace(_T(".chm"), _T("_en.chm"));
139 free((void*)m_pszHelpFilePath);
140 m_pszHelpFilePath=_tcsdup(sHelppath);
141 sHelppath = CPathUtils::GetAppParentDirectory() + _T("Languages\\TortoiseGitBlame_en.chm");
144 GetLocaleInfo(MAKELCID(langId, SORT_DEFAULT), LOCALE_SISO639LANGNAME, buf, _countof(buf));
145 CString sLang = _T("_");
146 sLang += buf;
147 sHelppath.Replace(_T("_en"), sLang);
148 if (PathFileExists(sHelppath))
150 free((void*)m_pszHelpFilePath);
151 m_pszHelpFilePath=_tcsdup(sHelppath);
152 break;
154 sHelppath.Replace(sLang, _T("_en"));
155 GetLocaleInfo(MAKELCID(langId, SORT_DEFAULT), LOCALE_SISO3166CTRYNAME, buf, _countof(buf));
156 sLang += _T("_");
157 sLang += buf;
158 sHelppath.Replace(_T("_en"), sLang);
159 if (PathFileExists(sHelppath))
161 free((void*)m_pszHelpFilePath);
162 m_pszHelpFilePath=_tcsdup(sHelppath);
163 break;
165 sHelppath.Replace(sLang, _T("_en"));
167 DWORD lid = SUBLANGID(langId);
168 lid--;
169 if (lid > 0)
171 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
173 else
174 langId = 0;
175 } while (langId);
176 setlocale(LC_ALL, "");
177 // We need to explicitly set the thread locale to the system default one to avoid possible problems with saving files in its original codepage
178 // The problems occures when the language of OS differs from the regional settings
179 // See the details here: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100887
180 SetThreadLocale(LOCALE_SYSTEM_DEFAULT);
182 // InitCommonControlsEx() is required on Windows XP if an application
183 // manifest specifies use of ComCtl32.dll version 6 or later to enable
184 // visual styles. Otherwise, any window creation will fail.
185 INITCOMMONCONTROLSEX InitCtrls;
186 InitCtrls.dwSize = sizeof(InitCtrls);
187 // Set this to include all the common control classes you want to use
188 // in your application.
189 InitCtrls.dwICC = ICC_WIN95_CLASSES;
190 InitCommonControlsEx(&InitCtrls);
192 CWinAppEx::InitInstance();
194 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
195 Gdiplus::GdiplusStartup(&m_gdiplusToken,&gdiplusStartupInput,NULL);
197 // Initialize OLE libraries
198 if (!AfxOleInit())
200 AfxMessageBox(IDP_OLE_INIT_FAILED);
201 return FALSE;
203 AfxEnableControlContainer();
204 // Standard initialization
205 // If you are not using these features and wish to reduce the size
206 // of your final executable, you should remove from the following
207 // the specific initialization routines you do not need
208 // Change the registry key under which our settings are stored
210 SetRegistryKey(_T("TortoiseGit"));
211 LoadStdProfileSettings(4); // Load standard INI file options (including MRU)
213 InitContextMenuManager();
215 InitKeyboardManager();
217 InitTooltipManager();
218 CMFCToolTipInfo ttParams;
219 ttParams.m_bVislManagerTheme = TRUE;
220 theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
221 RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);
223 // Register the application's document templates. Document templates
224 // serve as the connection between documents, frame windows and views
225 CSingleDocTemplate* pDocTemplate;
226 pDocTemplate = new CSingleDocTemplate(
227 IDR_TORTOISE_GIT_BLAME_MAINFRAME,
228 RUNTIME_CLASS(CTortoiseGitBlameDoc),
229 RUNTIME_CLASS(CMainFrame), // main SDI frame window
230 RUNTIME_CLASS(CTortoiseGitBlameView));
231 if (!pDocTemplate)
232 return FALSE;
233 AddDocTemplate(pDocTemplate);
235 CCmdLineParser parser = CCmdLineParser(this->m_lpCmdLine);
236 g_sGroupingUUID = parser.GetVal(L"groupuuid");
238 // Parse command line for standard shell commands, DDE, file open
239 CCommandLineInfo cmdInfo;
240 ParseCommandLine(cmdInfo);
242 // Dispatch commands specified on the command line. Will return FALSE if
243 // app was launched with /RegServer, /Register, /Unregserver or /Unregister.
244 if (!ProcessShellCommand(cmdInfo))
245 return FALSE;
247 // The one and only window has been initialized, so show and update it
248 m_pMainWnd->ShowWindow(SW_SHOW);
249 m_pMainWnd->UpdateWindow();
250 // call DragAcceptFiles only if there's a suffix
251 // In an SDI app, this should occur after ProcessShellCommand
252 return TRUE;
255 // CAboutDlg dialog used for App About
257 class CAboutDlg : public CDialog
259 public:
260 CAboutDlg();
262 // Dialog Data
263 enum { IDD = IDD_ABOUTBOX };
265 protected:
266 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
267 BOOL OnInitDialog();
269 // Implementation
270 DECLARE_MESSAGE_MAP()
273 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
277 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
279 CDialog::DoDataExchange(pDX);
282 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
283 END_MESSAGE_MAP()
285 BOOL CAboutDlg::OnInitDialog()
287 CDialog::OnInitDialog();
289 TCHAR verbuf[1024] = {0};
290 TCHAR maskbuf[1024] = {0};
291 ::LoadString(GetModuleHandle(NULL), IDS_VERSION, maskbuf, _countof(maskbuf));
292 _stprintf_s(verbuf, maskbuf, TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD);
293 SetDlgItemText(IDC_VERSION, verbuf);
295 return FALSE;
298 // App command to run the dialog
299 void CTortoiseGitBlameApp::OnAppAbout()
301 CAboutDlg aboutDlg;
302 aboutDlg.DoModal();
305 void CTortoiseGitBlameApp::OnFileSettings()
307 CCommonAppUtils::RunTortoiseGitProc(_T(" /command:settings /page:blame"));
310 // CTortoiseGitBlameApp customization load/save methods
312 void CTortoiseGitBlameApp::PreLoadState()
316 void CTortoiseGitBlameApp::LoadCustomState()
320 void CTortoiseGitBlameApp::SaveCustomState()
324 // CTortoiseGitBlameApp message handlers
326 int CTortoiseGitBlameApp::ExitInstance()
328 Gdiplus::GdiplusShutdown(m_gdiplusToken);
329 return CWinAppEx::ExitInstance();