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.
20 #include "MainWindow.h"
21 #include "UnicodeUtils.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*/)
31 , m_bShowFindBar(false)
33 , m_directFunction(NULL
)
34 , m_directPointer(NULL
)
37 SetWindowTitle(_T("TortoiseGitUDiff"));
40 CMainWindow::~CMainWindow(void)
44 bool CMainWindow::RegisterAndCreateWindow()
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
;
54 wcx
.hInstance
= hResource
;
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);
74 LRESULT CALLBACK
CMainWindow::WinMsgHandler(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
76 if (uMsg
== TaskBarButtonCreated
)
78 SetUUIDOverlayIcon(hwnd
);
90 return DoCommand(LOWORD(wParam
));
95 if (GET_KEYSTATE_WPARAM(wParam
) == MK_SHIFT
)
98 SendEditor(SCI_LINESCROLL
, -GET_WHEEL_DELTA_WPARAM(wParam
)/40, 0);
101 return DefWindowProc(hwnd
, uMsg
, wParam
, lParam
);
107 GetClientRect(*this, &rect
);
110 ::SetWindowPos(m_hWndEdit
, HWND_TOP
,
112 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
-30,
114 ::SetWindowPos(m_FindBar
, HWND_TOP
,
115 rect
.left
, rect
.bottom
-30,
116 rect
.right
-rect
.left
, 30,
121 ::SetWindowPos(m_hWndEdit
, HWND_TOP
,
123 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
,
125 ::ShowWindow(m_FindBar
, SW_HIDE
);
129 case WM_GETMINMAXINFO
:
131 MINMAXINFO
* mmi
= (MINMAXINFO
*)lParam
;
132 mmi
->ptMinTrackSize
.x
= 100;
133 mmi
->ptMinTrackSize
.y
= 100;
141 ::DestroyWindow(m_hwnd
);
144 SetFocus(m_hWndEdit
);
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
);
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
);
165 case COMMITMONITOR_FINDEXIT
:
168 GetClientRect(*this, &rect
);
169 m_bShowFindBar
= false;
170 ::ShowWindow(m_FindBar
, SW_HIDE
);
171 ::SetWindowPos(m_hWndEdit
, HWND_TOP
,
173 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
,
177 case COMMITMONITOR_FINDRESET
:
178 SendEditor(SCI_SETSELECTIONSTART
, 0);
179 SendEditor(SCI_SETSELECTIONEND
, 0);
180 SendEditor(SCI_SEARCHANCHOR
);
183 return DefWindowProc(hwnd
, uMsg
, wParam
, lParam
);
189 LRESULT
CMainWindow::DoCommand(int id
)
194 loadOrSaveFile(true);
197 loadOrSaveFile(false);
200 loadOrSaveFile(false, m_filename
);
203 ::PostQuitMessage(0);
205 case IDM_SHOWFINDBAR
:
207 m_bShowFindBar
= true;
208 ::ShowWindow(m_FindBar
, SW_SHOW
);
210 GetClientRect(*this, &rect
);
211 ::SetWindowPos(m_hWndEdit
, HWND_TOP
,
213 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
-30,
215 ::SetWindowPos(m_FindBar
, HWND_TOP
,
216 rect
.left
, rect
.bottom
-30,
217 rect
.right
-rect
.left
, 30,
219 ::SetFocus(m_FindBar
);
220 SendEditor(SCI_SETSELECTIONSTART
, 0);
221 SendEditor(SCI_SETSELECTIONEND
, 0);
222 SendEditor(SCI_SEARCHANCHOR
);
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
);
232 SendEditor(SCI_SEARCHANCHOR
);
233 SendEditor(SCI_SEARCHPREV
, m_bMatchCase
? SCFIND_MATCHCASE
: 0, (LPARAM
)CUnicodeUtils::StdGetUTF8(m_findtext
).c_str());
234 SendEditor(SCI_SCROLLCARET
);
237 if (IsWindowVisible(m_FindBar
))
240 GetClientRect(*this, &rect
);
241 m_bShowFindBar
= false;
242 ::ShowWindow(m_FindBar
, SW_HIDE
);
243 ::SetWindowPos(m_hWndEdit
, HWND_TOP
,
245 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
,
251 case ID_FILE_SETTINGS
:
253 tstring gitCmd
= _T(" /command:settings /page:blame");
263 std::wstring
CMainWindow::GetAppDirectory()
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
);
275 } while(len
== bufferlen
);
277 path
= path
.substr(0, path
.rfind('\\') + 1);
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(
301 WS_CHILD
| WS_VSCROLL
| WS_HSCROLL
| WS_CLIPCHILDREN
,
302 CW_USEDEFAULT
, CW_USEDEFAULT
,
303 CW_USEDEFAULT
, CW_USEDEFAULT
,
308 if (m_hWndEdit
== NULL
)
312 GetClientRect(*this, &rect
);
313 ::SetWindowPos(m_hWndEdit
, HWND_TOP
,
315 rect
.right
-rect
.left
, rect
.bottom
-rect
.top
,
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
);
351 bool CMainWindow::LoadFile(HANDLE hFile
)
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
);
369 bool CMainWindow::LoadFile(LPCTSTR filename
)
373 _tfopen_s(&fp
, filename
, _T("rb"));
379 size_t lenFile
= fread(data
, 1, sizeof(data
), fp
);
380 bool bUTF8
= IsUTF8(data
, lenFile
);
383 SendEditor(SCI_ADDTEXT
, lenFile
,
384 reinterpret_cast<LPARAM
>(static_cast<char *>(data
)));
385 lenFile
= fread(data
, 1, sizeof(data
), fp
);
389 m_filename
= filename
;
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
)
434 _tfopen_s(&fp
, filename
, _T("w+b"));
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
);
444 SendEditor(SCI_SETSAVEPOINT
);
445 ::ShowWindow(m_hWndEdit
, SW_SHOW
);
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
));
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
);
463 SendEditor(SCI_STYLESETSIZE
, style
, size
);
465 SendEditor(SCI_STYLESETFONT
, style
, reinterpret_cast<LPARAM
>(face
));
468 bool CMainWindow::IsUTF8(LPVOID pBuffer
, size_t cb
)
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
++)
481 pVal16
= (UINT16
*)pBuffer
;
482 if (*pVal16
== 0xFEFF)
486 if (*pVal16
== 0xBBEF)
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))
499 pVal8
= (UINT8
*)pBuffer
;
501 for (size_t i
=0; i
<(cb
-3); ++i
)
503 if ((*pVal8
& 0xE0)==0xC0)
506 if ((*pVal8
& 0xC0)!=0x80)
510 if ((*pVal8
& 0xF0)==0xE0)
513 if ((*pVal8
& 0xC0)!=0x80)
516 if ((*pVal8
& 0xC0)!=0x80)
520 if ((*pVal8
& 0xF8)==0xF0)
523 if ((*pVal8
& 0xC0)!=0x80)
526 if ((*pVal8
& 0xC0)!=0x80)
529 if ((*pVal8
& 0xC0)!=0x80)
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
);
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
;
562 ofn
.Flags
|= OFN_PATHMUSTEXIST
| OFN_FILEMUSTEXIST
;
564 ofn
.Flags
|= OFN_OVERWRITEPROMPT
;
565 // Display the Open dialog box.
568 if (GetOpenFileName(&ofn
)==TRUE
)
570 LoadFile(ofn
.lpstrFile
);
575 if (filename
.empty())
577 if (GetSaveFileName(&ofn
)==TRUE
)
579 SaveFile(ofn
.lpstrFile
);
583 SaveFile(filename
.c_str());