Refactored: More information hiding
[TortoiseGit.git] / src / TortoiseUDiff / MainWindow.cpp
blobdb18fc2b39a60bd1616cefe5418323f3baa7b26c
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2012 - TortoiseSVN
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.
19 #include "StdAfx.h"
20 #include "MainWindow.h"
21 #include "UnicodeUtils.h"
22 #include "SysInfo.h"
23 #include "StringUtils.h"
24 #include "TaskbarUUID.h"
25 #include "CreateProcessHelper.h"
27 const UINT TaskBarButtonCreated = RegisterWindowMessage(L"TaskbarButtonCreated");
29 CMainWindow::CMainWindow(HINSTANCE hInst, const WNDCLASSEX* wcx /* = NULL*/)
30 : CWindow(hInst, wcx)
31 , m_bShowFindBar(false)
32 , m_bMatchCase(false)
33 , m_directFunction(NULL)
34 , m_directPointer(NULL)
35 , m_hWndEdit(NULL)
37 SetWindowTitle(_T("TortoiseGitUDiff"));
40 CMainWindow::~CMainWindow(void)
44 bool CMainWindow::RegisterAndCreateWindow()
46 WNDCLASSEX wcx;
48 // Fill in the window class structure with default parameters
49 wcx.cbSize = sizeof(WNDCLASSEX);
50 wcx.style = CS_HREDRAW | CS_VREDRAW;
51 wcx.lpfnWndProc = CWindow::stWinMsgHandler;
52 wcx.cbClsExtra = 0;
53 wcx.cbWndExtra = 0;
54 wcx.hInstance = hResource;
55 wcx.hCursor = NULL;
56 wcx.lpszClassName = ResString(hResource, IDS_APP_TITLE);
57 wcx.hIcon = LoadIcon(hResource, MAKEINTRESOURCE(IDI_TORTOISEUDIFF));
58 wcx.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
59 wcx.lpszMenuName = MAKEINTRESOURCE(IDC_TORTOISEUDIFF);
60 wcx.hIconSm = LoadIcon(wcx.hInstance, MAKEINTRESOURCE(IDI_TORTOISEUDIFF));
61 if (RegisterWindow(&wcx))
63 if (Create(WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX | WS_SYSMENU | WS_CLIPCHILDREN, NULL))
65 m_FindBar.SetParent(*this);
66 m_FindBar.Create(hResource, IDD_FINDBAR, *this);
67 UpdateWindow(*this);
68 return true;
71 return false;
74 LRESULT CALLBACK CMainWindow::WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
76 if (uMsg == TaskBarButtonCreated)
78 SetUUIDOverlayIcon(hwnd);
80 switch (uMsg)
82 case WM_CREATE:
84 m_hwnd = hwnd;
85 Initialize();
87 break;
88 case WM_COMMAND:
90 return DoCommand(LOWORD(wParam));
92 break;
93 case WM_MOUSEWHEEL:
95 if (GET_KEYSTATE_WPARAM(wParam) == MK_SHIFT)
97 // scroll sideways
98 SendEditor(SCI_LINESCROLL, -GET_WHEEL_DELTA_WPARAM(wParam)/40, 0);
100 else
101 return DefWindowProc(hwnd, uMsg, wParam, lParam);
103 break;
104 case WM_SIZE:
106 RECT rect;
107 GetClientRect(*this, &rect);
108 if (m_bShowFindBar)
110 ::SetWindowPos(m_hWndEdit, HWND_TOP,
111 rect.left, rect.top,
112 rect.right-rect.left, rect.bottom-rect.top-30,
113 SWP_SHOWWINDOW);
114 ::SetWindowPos(m_FindBar, HWND_TOP,
115 rect.left, rect.bottom-30,
116 rect.right-rect.left, 30,
117 SWP_SHOWWINDOW);
119 else
121 ::SetWindowPos(m_hWndEdit, HWND_TOP,
122 rect.left, rect.top,
123 rect.right-rect.left, rect.bottom-rect.top,
124 SWP_SHOWWINDOW);
125 ::ShowWindow(m_FindBar, SW_HIDE);
128 break;
129 case WM_GETMINMAXINFO:
131 MINMAXINFO * mmi = (MINMAXINFO*)lParam;
132 mmi->ptMinTrackSize.x = 100;
133 mmi->ptMinTrackSize.y = 100;
134 return 0;
136 break;
137 case WM_DESTROY:
138 PostQuitMessage(0);
139 break;
140 case WM_CLOSE:
141 ::DestroyWindow(m_hwnd);
142 break;
143 case WM_SETFOCUS:
144 SetFocus(m_hWndEdit);
145 break;
146 case COMMITMONITOR_FINDMSGNEXT:
148 SendEditor(SCI_CHARRIGHT);
149 SendEditor(SCI_SEARCHANCHOR);
150 m_bMatchCase = !!wParam;
151 m_findtext = (LPCTSTR)lParam;
152 SendEditor(SCI_SEARCHNEXT, m_bMatchCase ? SCFIND_MATCHCASE : 0, (LPARAM)CUnicodeUtils::StdGetUTF8(m_findtext).c_str());
153 SendEditor(SCI_SCROLLCARET);
155 break;
156 case COMMITMONITOR_FINDMSGPREV:
158 SendEditor(SCI_SEARCHANCHOR);
159 m_bMatchCase = !!wParam;
160 m_findtext = (LPCTSTR)lParam;
161 SendEditor(SCI_SEARCHPREV, m_bMatchCase ? SCFIND_MATCHCASE : 0, (LPARAM)CUnicodeUtils::StdGetUTF8(m_findtext).c_str());
162 SendEditor(SCI_SCROLLCARET);
164 break;
165 case COMMITMONITOR_FINDEXIT:
167 RECT rect;
168 GetClientRect(*this, &rect);
169 m_bShowFindBar = false;
170 ::ShowWindow(m_FindBar, SW_HIDE);
171 ::SetWindowPos(m_hWndEdit, HWND_TOP,
172 rect.left, rect.top,
173 rect.right-rect.left, rect.bottom-rect.top,
174 SWP_SHOWWINDOW);
176 break;
177 case COMMITMONITOR_FINDRESET:
178 SendEditor(SCI_SETSELECTIONSTART, 0);
179 SendEditor(SCI_SETSELECTIONEND, 0);
180 SendEditor(SCI_SEARCHANCHOR);
181 break;
182 default:
183 return DefWindowProc(hwnd, uMsg, wParam, lParam);
186 return 0;
189 LRESULT CMainWindow::DoCommand(int id)
191 switch (id)
193 case ID_FILE_OPEN:
194 loadOrSaveFile(true);
195 break;
196 case ID_FILE_SAVEAS:
197 loadOrSaveFile(false);
198 break;
199 case ID_FILE_SAVE:
200 loadOrSaveFile(false, m_filename);
201 break;
202 case ID_FILE_EXIT:
203 ::PostQuitMessage(0);
204 return 0;
205 case IDM_SHOWFINDBAR:
207 m_bShowFindBar = true;
208 ::ShowWindow(m_FindBar, SW_SHOW);
209 RECT rect;
210 GetClientRect(*this, &rect);
211 ::SetWindowPos(m_hWndEdit, HWND_TOP,
212 rect.left, rect.top,
213 rect.right-rect.left, rect.bottom-rect.top-30,
214 SWP_SHOWWINDOW);
215 ::SetWindowPos(m_FindBar, HWND_TOP,
216 rect.left, rect.bottom-30,
217 rect.right-rect.left, 30,
218 SWP_SHOWWINDOW);
219 ::SetFocus(m_FindBar);
220 SendEditor(SCI_SETSELECTIONSTART, 0);
221 SendEditor(SCI_SETSELECTIONEND, 0);
222 SendEditor(SCI_SEARCHANCHOR);
224 break;
225 case IDM_FINDNEXT:
226 SendEditor(SCI_CHARRIGHT);
227 SendEditor(SCI_SEARCHANCHOR);
228 SendEditor(SCI_SEARCHNEXT, m_bMatchCase ? SCFIND_MATCHCASE : 0, (LPARAM)CUnicodeUtils::StdGetUTF8(m_findtext).c_str());
229 SendEditor(SCI_SCROLLCARET);
230 break;
231 case IDM_FINDPREV:
232 SendEditor(SCI_SEARCHANCHOR);
233 SendEditor(SCI_SEARCHPREV, m_bMatchCase ? SCFIND_MATCHCASE : 0, (LPARAM)CUnicodeUtils::StdGetUTF8(m_findtext).c_str());
234 SendEditor(SCI_SCROLLCARET);
235 break;
236 case IDM_FINDEXIT:
237 if (IsWindowVisible(m_FindBar))
239 RECT rect;
240 GetClientRect(*this, &rect);
241 m_bShowFindBar = false;
242 ::ShowWindow(m_FindBar, SW_HIDE);
243 ::SetWindowPos(m_hWndEdit, HWND_TOP,
244 rect.left, rect.top,
245 rect.right-rect.left, rect.bottom-rect.top,
246 SWP_SHOWWINDOW);
248 else
249 PostQuitMessage(0);
250 break;
251 case ID_FILE_SETTINGS:
253 tstring gitCmd = _T(" /command:settings /page:blame");
254 RunCommand(gitCmd);
256 break;
257 default:
258 break;
260 return 1;
263 std::wstring CMainWindow::GetAppDirectory()
265 std::wstring path;
266 DWORD len = 0;
267 DWORD bufferlen = MAX_PATH; // MAX_PATH is not the limit here!
270 bufferlen += MAX_PATH; // MAX_PATH is not the limit here!
271 TCHAR * pBuf = new TCHAR[bufferlen];
272 len = GetModuleFileName(NULL, pBuf, bufferlen);
273 path = pBuf;
274 delete [] pBuf;
275 } while(len == bufferlen);
277 path = path.substr(0, path.rfind('\\') + 1);
278 return path;
281 void CMainWindow::RunCommand(const std::wstring& command)
283 tstring tortoiseProcPath = GetAppDirectory() + _T("TortoiseGitProc.exe");
284 CCreateProcessHelper::CreateProcessDetached(tortoiseProcPath.c_str(), const_cast<TCHAR*>(command.c_str()));
287 LRESULT CMainWindow::SendEditor(UINT Msg, WPARAM wParam, LPARAM lParam)
289 if (m_directFunction)
291 return ((SciFnDirect) m_directFunction)(m_directPointer, Msg, wParam, lParam);
293 return ::SendMessage(m_hWndEdit, Msg, wParam, lParam);
296 bool CMainWindow::Initialize()
298 m_hWndEdit = ::CreateWindow(
299 _T("Scintilla"),
300 _T("Source"),
301 WS_CHILD | WS_VSCROLL | WS_HSCROLL | WS_CLIPCHILDREN,
302 CW_USEDEFAULT, CW_USEDEFAULT,
303 CW_USEDEFAULT, CW_USEDEFAULT,
304 *this,
306 hResource,
308 if (m_hWndEdit == NULL)
309 return false;
311 RECT rect;
312 GetClientRect(*this, &rect);
313 ::SetWindowPos(m_hWndEdit, HWND_TOP,
314 rect.left, rect.top,
315 rect.right-rect.left, rect.bottom-rect.top,
316 SWP_SHOWWINDOW);
318 m_directFunction = SendMessage(m_hWndEdit, SCI_GETDIRECTFUNCTION, 0, 0);
319 m_directPointer = SendMessage(m_hWndEdit, SCI_GETDIRECTPOINTER, 0, 0);
321 // Set up the global default style. These attributes are used wherever no explicit choices are made.
322 SetAStyle(STYLE_DEFAULT, ::GetSysColor(COLOR_WINDOWTEXT), ::GetSysColor(COLOR_WINDOW),
323 // Reusing TortoiseBlame's setting which already have an user friendly
324 // pane in TortoiseSVN's Settings dialog, while there is no such
325 // pane for TortoiseUDiff.
326 CRegStdDWORD(_T("Software\\TortoiseGit\\BlameFontSize"), 10),
327 CUnicodeUtils::StdGetUTF8(CRegStdString(_T("Software\\TortoiseGit\\BlameFontName"), _T("Courier New"))).c_str());
328 SendEditor(SCI_SETTABWIDTH, 4);
329 SendEditor(SCI_SETREADONLY, TRUE);
330 LRESULT pix = SendEditor(SCI_TEXTWIDTH, STYLE_LINENUMBER, (LPARAM)"_99999");
331 SendEditor(SCI_SETMARGINWIDTHN, 0, pix);
332 SendEditor(SCI_SETMARGINWIDTHN, 1);
333 SendEditor(SCI_SETMARGINWIDTHN, 2);
334 //Set the default windows colors for edit controls
335 SendEditor(SCI_STYLESETFORE, STYLE_DEFAULT, ::GetSysColor(COLOR_WINDOWTEXT));
336 SendEditor(SCI_STYLESETBACK, STYLE_DEFAULT, ::GetSysColor(COLOR_WINDOW));
337 SendEditor(SCI_SETSELFORE, TRUE, ::GetSysColor(COLOR_HIGHLIGHTTEXT));
338 SendEditor(SCI_SETSELBACK, TRUE, ::GetSysColor(COLOR_HIGHLIGHT));
339 SendEditor(SCI_SETCARETFORE, ::GetSysColor(COLOR_WINDOWTEXT));
340 CRegStdDWORD used2d(L"Software\\TortoiseGit\\ScintillaDirect2D", FALSE);
341 if (SysInfo::Instance().IsWin7OrLater() && DWORD(used2d))
343 SendEditor(SCI_SETTECHNOLOGY, SC_TECHNOLOGY_DIRECTWRITE);
344 SendEditor(SCI_SETBUFFEREDDRAW, 0);
346 SendEditor(SCI_SETFONTQUALITY, SC_EFF_QUALITY_DEFAULT);
348 return true;
351 bool CMainWindow::LoadFile(HANDLE hFile)
353 InitEditor();
354 char data[4096];
355 DWORD dwRead = 0;
357 BOOL bRet = ReadFile(hFile, data, sizeof(data), &dwRead, NULL);
358 bool bUTF8 = IsUTF8(data, dwRead);
359 while ((dwRead > 0) && (bRet))
361 SendEditor(SCI_ADDTEXT, dwRead,
362 reinterpret_cast<LPARAM>(static_cast<char *>(data)));
363 bRet = ReadFile(hFile, data, sizeof(data), &dwRead, NULL);
365 SetupWindow(bUTF8);
366 return true;
369 bool CMainWindow::LoadFile(LPCTSTR filename)
371 InitEditor();
372 FILE *fp = NULL;
373 _tfopen_s(&fp, filename, _T("rb"));
374 if (!fp)
375 return false;
377 //SetTitle();
378 char data[4096];
379 size_t lenFile = fread(data, 1, sizeof(data), fp);
380 bool bUTF8 = IsUTF8(data, lenFile);
381 while (lenFile > 0)
383 SendEditor(SCI_ADDTEXT, lenFile,
384 reinterpret_cast<LPARAM>(static_cast<char *>(data)));
385 lenFile = fread(data, 1, sizeof(data), fp);
387 fclose(fp);
388 SetupWindow(bUTF8);
389 m_filename = filename;
390 return true;
393 void CMainWindow::InitEditor()
395 SendEditor(SCI_SETREADONLY, FALSE);
396 SendEditor(SCI_CLEARALL);
397 SendEditor(EM_EMPTYUNDOBUFFER);
398 SendEditor(SCI_SETSAVEPOINT);
399 SendEditor(SCI_CANCEL);
400 SendEditor(SCI_SETUNDOCOLLECTION, 0);
403 void CMainWindow::SetupWindow(bool bUTF8)
405 SendEditor(SCI_SETCODEPAGE, bUTF8 ? SC_CP_UTF8 : GetACP());
407 SendEditor(SCI_SETUNDOCOLLECTION, 1);
408 ::SetFocus(m_hWndEdit);
409 SendEditor(EM_EMPTYUNDOBUFFER);
410 SendEditor(SCI_SETSAVEPOINT);
411 SendEditor(SCI_GOTOPOS, 0);
413 SendEditor(SCI_CLEARDOCUMENTSTYLE, 0, 0);
414 SendEditor(SCI_SETSTYLEBITS, 5, 0);
416 //SetAStyle(SCE_DIFF_DEFAULT, RGB(0, 0, 0));
417 SetAStyle(SCE_DIFF_COMMAND, RGB(0x0A, 0x24, 0x36));
418 SetAStyle(SCE_DIFF_POSITION, RGB(0xFF, 0, 0));
419 SetAStyle(SCE_DIFF_HEADER, RGB(0x80, 0, 0), RGB(0xFF, 0xFF, 0x80));
420 SetAStyle(SCE_DIFF_COMMENT, RGB(0, 0x80, 0));
421 SendEditor(SCI_STYLESETBOLD, SCE_DIFF_COMMENT, TRUE);
422 SetAStyle(SCE_DIFF_DELETED, ::GetSysColor(COLOR_WINDOWTEXT), RGB(0xFF, 0x80, 0x80));
423 SetAStyle(SCE_DIFF_ADDED, ::GetSysColor(COLOR_WINDOWTEXT), RGB(0x80, 0xFF, 0x80));
425 SendEditor(SCI_SETLEXER, SCLEX_DIFF);
426 SendEditor(SCI_SETKEYWORDS, 0, (LPARAM)"revision");
427 SendEditor(SCI_COLOURISE, 0, -1);
428 ::ShowWindow(m_hWndEdit, SW_SHOW);
431 bool CMainWindow::SaveFile(LPCTSTR filename)
433 FILE *fp = NULL;
434 _tfopen_s(&fp, filename, _T("w+b"));
435 if (!fp)
436 return false;
438 int len = SendEditor(SCI_GETTEXT, 0, 0);
439 char * data = new char[len+1];
440 SendEditor(SCI_GETTEXT, len, (LPARAM)data);
441 fwrite(data, sizeof(char), len-1, fp);
442 fclose(fp);
444 SendEditor(SCI_SETSAVEPOINT);
445 ::ShowWindow(m_hWndEdit, SW_SHOW);
446 return true;
449 void CMainWindow::SetTitle(LPCTSTR title)
451 size_t len = _tcslen(title);
452 TCHAR * pBuf = new TCHAR[len+40];
453 _stprintf_s(pBuf, len+40, _T("%s - TortoiseGitUDiff"), title);
454 SetWindowTitle(std::wstring(pBuf));
455 delete [] pBuf;
458 void CMainWindow::SetAStyle(int style, COLORREF fore, COLORREF back, int size, const char *face)
460 SendEditor(SCI_STYLESETFORE, style, fore);
461 SendEditor(SCI_STYLESETBACK, style, back);
462 if (size >= 1)
463 SendEditor(SCI_STYLESETSIZE, style, size);
464 if (face)
465 SendEditor(SCI_STYLESETFONT, style, reinterpret_cast<LPARAM>(face));
468 bool CMainWindow::IsUTF8(LPVOID pBuffer, size_t cb)
470 if (cb < 2)
471 return true;
472 UINT16 * pVal16 = (UINT16 *)pBuffer;
473 UINT8 * pVal8 = (UINT8 *)(pVal16+1);
474 // scan the whole buffer for a 0x0000 sequence
475 // if found, we assume a binary file
476 for (size_t i=0; i<(cb-2); i=i+2)
478 if (0x0000 == *pVal16++)
479 return false;
481 pVal16 = (UINT16 *)pBuffer;
482 if (*pVal16 == 0xFEFF)
483 return false;
484 if (cb < 3)
485 return false;
486 if (*pVal16 == 0xBBEF)
488 if (*pVal8 == 0xBF)
489 return true;
491 // check for illegal UTF8 chars
492 pVal8 = (UINT8 *)pBuffer;
493 for (size_t i=0; i<cb; ++i)
495 if ((*pVal8 == 0xC0)||(*pVal8 == 0xC1)||(*pVal8 >= 0xF5))
496 return false;
497 pVal8++;
499 pVal8 = (UINT8 *)pBuffer;
500 bool bUTF8 = false;
501 for (size_t i=0; i<(cb-3); ++i)
503 if ((*pVal8 & 0xE0)==0xC0)
505 pVal8++;i++;
506 if ((*pVal8 & 0xC0)!=0x80)
507 return false;
508 bUTF8 = true;
510 if ((*pVal8 & 0xF0)==0xE0)
512 pVal8++;i++;
513 if ((*pVal8 & 0xC0)!=0x80)
514 return false;
515 pVal8++;i++;
516 if ((*pVal8 & 0xC0)!=0x80)
517 return false;
518 bUTF8 = true;
520 if ((*pVal8 & 0xF8)==0xF0)
522 pVal8++;i++;
523 if ((*pVal8 & 0xC0)!=0x80)
524 return false;
525 pVal8++;i++;
526 if ((*pVal8 & 0xC0)!=0x80)
527 return false;
528 pVal8++;i++;
529 if ((*pVal8 & 0xC0)!=0x80)
530 return false;
531 bUTF8 = true;
533 pVal8++;
535 if (bUTF8)
536 return true;
537 return false;
540 void CMainWindow::loadOrSaveFile(bool doLoad, const wstring& filename /* = L"" */)
542 OPENFILENAME ofn = {0}; // common dialog box structure
543 TCHAR szFile[MAX_PATH] = {0}; // buffer for file name
544 // Initialize OPENFILENAME
545 ofn.lStructSize = sizeof(OPENFILENAME);
546 ofn.hwndOwner = *this;
547 ofn.lpstrFile = szFile;
548 ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);
549 TCHAR filter[1024];
550 LoadString(hResource, IDS_PATCHFILEFILTER, filter, sizeof(filter)/sizeof(TCHAR));
551 CStringUtils::PipesToNulls(filter);
552 ofn.lpstrFilter = filter;
553 ofn.nFilterIndex = 1;
554 ofn.lpstrFileTitle = NULL;
555 ofn.nMaxFileTitle = 0;
556 ofn.lpstrInitialDir = NULL;
557 TCHAR fileTitle[1024];
558 LoadString(hResource, doLoad ? IDS_OPENPATCH : IDS_SAVEPATCH, fileTitle, sizeof(fileTitle)/sizeof(TCHAR));
559 ofn.lpstrTitle = fileTitle;
560 ofn.Flags = OFN_ENABLESIZING | OFN_EXPLORER;
561 if(doLoad)
562 ofn.Flags |= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
563 else
564 ofn.Flags |= OFN_OVERWRITEPROMPT;
565 // Display the Open dialog box.
566 if( doLoad )
568 if (GetOpenFileName(&ofn)==TRUE)
570 LoadFile(ofn.lpstrFile);
573 else
575 if (filename.empty())
577 if (GetSaveFileName(&ofn)==TRUE)
579 SaveFile(ofn.lpstrFile);
582 else
583 SaveFile(filename.c_str());