Do not include version.h in CrashReport.h in order to speed up building
[TortoiseGit.git] / src / TortoiseGitBlame / TortoiseGitBlame.cpp
blobf862ecf6b90fa47ba2a2134c39f62cd295c505f0
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2013 - 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"
36 #ifdef _DEBUG
37 #define new DEBUG_NEW
38 #endif
41 // CTortoiseGitBlameApp
43 BEGIN_MESSAGE_MAP(CTortoiseGitBlameApp, CWinAppEx)
44 ON_COMMAND(ID_APP_ABOUT, &CTortoiseGitBlameApp::OnAppAbout)
45 // Standard file based document commands
46 ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
47 ON_COMMAND(ID_FILE_SETTINGS, &CTortoiseGitBlameApp::OnFileSettings)
48 // Standard print setup command
49 ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinAppEx::OnFilePrintSetup)
50 END_MESSAGE_MAP()
53 // CTortoiseGitBlameApp construction
55 CTortoiseGitBlameApp::CTortoiseGitBlameApp()
57 SetDllDirectory(L"");
58 CCrashReportTGit crasher(L"TortoiseGitBlame " _T(APP_X64_STRING), TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD);
59 CCrashReport::Instance().AddUserInfoToReport(L"CommandLine", GetCommandLine());
60 EnableHtmlHelp();
62 m_gdiplusToken = NULL;
63 m_bHiColorIcons = TRUE;
66 // The one and only CTortoiseGitBlameApp object
67 CTortoiseGitBlameApp theApp;
68 CString sOrigCWD;
70 // CTortoiseGitBlameApp initialization
72 BOOL CTortoiseGitBlameApp::InitInstance()
75 DWORD len = GetCurrentDirectory(0, NULL);
76 if (len)
78 std::unique_ptr<TCHAR[]> originalCurrentDirectory(new TCHAR[len]);
79 if (GetCurrentDirectory(len, originalCurrentDirectory.get()))
81 sOrigCWD = originalCurrentDirectory.get();
82 sOrigCWD = CPathUtils::GetLongPathname(sOrigCWD);
87 //set the resource dll for the required language
88 CRegDWORD loc = CRegDWORD(_T("Software\\TortoiseGit\\LanguageID"), 1033);
89 long langId = loc;
90 CString langDll;
91 HINSTANCE hInst = NULL;
94 langDll.Format(_T("%sLanguages\\TortoiseGitBlame%d.dll"), (LPCTSTR)CPathUtils::GetAppParentDirectory(), langId);
96 hInst = LoadLibrary(langDll);
97 CString sVer = _T(STRPRODUCTVER);
98 CString sFileVer = CPathUtils::GetVersionFromFile(langDll);
99 if (sFileVer.Compare(sVer)!=0)
101 FreeLibrary(hInst);
102 hInst = NULL;
104 if (hInst != NULL)
105 AfxSetResourceHandle(hInst);
106 else
108 DWORD lid = SUBLANGID(langId);
109 lid--;
110 if (lid > 0)
112 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
114 else
115 langId = 0;
117 } while ((hInst == NULL) && (langId != 0));
118 TCHAR buf[6];
119 _tcscpy_s(buf, _T("en"));
120 langId = loc;
121 CString sHelppath;
122 sHelppath = this->m_pszHelpFilePath;
123 sHelppath = sHelppath.MakeLower();
124 sHelppath.Replace(_T(".chm"), _T("_en.chm"));
125 free((void*)m_pszHelpFilePath);
126 m_pszHelpFilePath=_tcsdup(sHelppath);
127 sHelppath = CPathUtils::GetAppParentDirectory() + _T("Languages\\TortoiseGitBlame_en.chm");
130 GetLocaleInfo(MAKELCID(langId, SORT_DEFAULT), LOCALE_SISO639LANGNAME, buf, _countof(buf));
131 CString sLang = _T("_");
132 sLang += buf;
133 sHelppath.Replace(_T("_en"), sLang);
134 if (PathFileExists(sHelppath))
136 free((void*)m_pszHelpFilePath);
137 m_pszHelpFilePath=_tcsdup(sHelppath);
138 break;
140 sHelppath.Replace(sLang, _T("_en"));
141 GetLocaleInfo(MAKELCID(langId, SORT_DEFAULT), LOCALE_SISO3166CTRYNAME, buf, _countof(buf));
142 sLang += _T("_");
143 sLang += buf;
144 sHelppath.Replace(_T("_en"), sLang);
145 if (PathFileExists(sHelppath))
147 free((void*)m_pszHelpFilePath);
148 m_pszHelpFilePath=_tcsdup(sHelppath);
149 break;
151 sHelppath.Replace(sLang, _T("_en"));
153 DWORD lid = SUBLANGID(langId);
154 lid--;
155 if (lid > 0)
157 langId = MAKELANGID(PRIMARYLANGID(langId), lid);
159 else
160 langId = 0;
161 } while (langId);
162 setlocale(LC_ALL, "");
163 // We need to explicitly set the thread locale to the system default one to avoid possible problems with saving files in its original codepage
164 // The problems occures when the language of OS differs from the regional settings
165 // See the details here: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100887
166 SetThreadLocale(LOCALE_SYSTEM_DEFAULT);
168 // InitCommonControlsEx() is required on Windows XP if an application
169 // manifest specifies use of ComCtl32.dll version 6 or later to enable
170 // visual styles. Otherwise, any window creation will fail.
171 INITCOMMONCONTROLSEX InitCtrls;
172 InitCtrls.dwSize = sizeof(InitCtrls);
173 // Set this to include all the common control classes you want to use
174 // in your application.
175 InitCtrls.dwICC = ICC_WIN95_CLASSES;
176 InitCommonControlsEx(&InitCtrls);
178 CWinAppEx::InitInstance();
180 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
181 Gdiplus::GdiplusStartup(&m_gdiplusToken,&gdiplusStartupInput,NULL);
183 // Initialize OLE libraries
184 if (!AfxOleInit())
186 AfxMessageBox(IDP_OLE_INIT_FAILED);
187 return FALSE;
189 AfxEnableControlContainer();
190 // Standard initialization
191 // If you are not using these features and wish to reduce the size
192 // of your final executable, you should remove from the following
193 // the specific initialization routines you do not need
194 // Change the registry key under which our settings are stored
196 SetRegistryKey(_T("TortoiseGit"));
197 LoadStdProfileSettings(4); // Load standard INI file options (including MRU)
199 InitContextMenuManager();
201 InitKeyboardManager();
203 InitTooltipManager();
204 CMFCToolTipInfo ttParams;
205 ttParams.m_bVislManagerTheme = TRUE;
206 theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
207 RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);
209 // Register the application's document templates. Document templates
210 // serve as the connection between documents, frame windows and views
211 CSingleDocTemplate* pDocTemplate;
212 pDocTemplate = new CSingleDocTemplate(
213 IDR_TORTOISE_GIT_BLAME_MAINFRAME,
214 RUNTIME_CLASS(CTortoiseGitBlameDoc),
215 RUNTIME_CLASS(CMainFrame), // main SDI frame window
216 RUNTIME_CLASS(CTortoiseGitBlameView));
217 if (!pDocTemplate)
218 return FALSE;
219 AddDocTemplate(pDocTemplate);
223 // Parse command line for standard shell commands, DDE, file open
224 CCommandLineInfo cmdInfo;
225 ParseCommandLine(cmdInfo);
227 // Dispatch commands specified on the command line. Will return FALSE if
228 // app was launched with /RegServer, /Register, /Unregserver or /Unregister.
229 if (!ProcessShellCommand(cmdInfo))
230 return FALSE;
232 // The one and only window has been initialized, so show and update it
233 m_pMainWnd->ShowWindow(SW_SHOW);
234 m_pMainWnd->UpdateWindow();
235 // call DragAcceptFiles only if there's a suffix
236 // In an SDI app, this should occur after ProcessShellCommand
237 return TRUE;
240 // CAboutDlg dialog used for App About
242 class CAboutDlg : public CDialog
244 public:
245 CAboutDlg();
247 // Dialog Data
248 enum { IDD = IDD_ABOUTBOX };
250 protected:
251 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
252 BOOL OnInitDialog();
254 // Implementation
255 DECLARE_MESSAGE_MAP()
258 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
262 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
264 CDialog::DoDataExchange(pDX);
267 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
268 END_MESSAGE_MAP()
270 BOOL CAboutDlg::OnInitDialog()
272 CDialog::OnInitDialog();
274 TCHAR verbuf[1024] = {0};
275 TCHAR maskbuf[1024] = {0};
276 if (!::LoadString(GetModuleHandle(NULL), IDS_VERSION, maskbuf, _countof(maskbuf)))
278 SecureZeroMemory(maskbuf, sizeof(maskbuf));
280 _stprintf_s(verbuf, maskbuf, TGIT_VERMAJOR, TGIT_VERMINOR, TGIT_VERMICRO, TGIT_VERBUILD);
281 SetDlgItemText(IDC_VERSION, verbuf);
283 return FALSE;
286 // App command to run the dialog
287 void CTortoiseGitBlameApp::OnAppAbout()
289 CAboutDlg aboutDlg;
290 aboutDlg.DoModal();
293 void CTortoiseGitBlameApp::OnFileSettings()
295 CCommonAppUtils::RunTortoiseGitProc(_T(" /command:settings /page:blame"));
298 // CTortoiseGitBlameApp customization load/save methods
300 void CTortoiseGitBlameApp::PreLoadState()
302 GetContextMenuManager()->AddMenu(IDR_BLAME_POPUP, IDR_BLAME_POPUP);
305 void CTortoiseGitBlameApp::LoadCustomState()
309 void CTortoiseGitBlameApp::SaveCustomState()
313 // CTortoiseGitBlameApp message handlers
315 int CTortoiseGitBlameApp::ExitInstance()
317 Gdiplus::GdiplusShutdown(m_gdiplusToken);
318 return CWinAppEx::ExitInstance();