1 /////////////////////////////////////////////////////////////////////////////
2 // WinMerge: an interactive diff/merge utility
3 // Copyright (C) 1997-2000 Thingamahoochie Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (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
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 /////////////////////////////////////////////////////////////////////////////
24 * @brief Defines the class behaviors for the application.
30 #include "Constants.h"
31 #include "UnicodeString.h"
33 #include "Environment.h"
34 #include "OptionsMgr.h"
35 #include "OptionsInit.h"
36 #include "RegOptionsMgr.h"
40 #include "HexMergeDoc.h"
41 #include "HexMergeFrm.h"
42 #include "HexMergeView.h"
50 #include "PropBackups.h"
51 #include "FileOrFolderSelect.h"
53 #include "FileFilterHelper.h"
54 #include "LineFiltersList.h"
55 #include "FilterCommentsManager.h"
56 #include "SyntaxColors.h"
57 #include "OptionsSyntaxColors.h"
59 #include "ProjectFile.h"
60 #include "MergeEditView.h"
61 #include "LanguageSelect.h"
62 #include "OptionsDef.h"
63 #include "MergeCmdLineInfo.h"
64 #include "ConflictFileParser.h"
66 #include "stringdiffs.h"
68 #include "SourceControl.h"
71 // For shutdown cleanup
80 /** @brief Location for command line help to open. */
81 static TCHAR CommandLineHelpLocation[] = _T("::/htmlhelp/Command_line.html");
83 // registry dir to WinMerge
84 static String f_RegDir = _T("Software\\Thingamahoochie\\WinMerge");
86 /** @brief Backup file extension. */
87 static const TCHAR BACKUP_FILE_EXT[] = _T("bak");
91 * @brief Turn STL exceptions into MFC exceptions.
92 * Based on the article "Visual C++ Exception-Handling Instrumentation"
93 * by Eugene Gershnik, published at http://www.drdobbs.com/184416600.
94 * Rethrow fix inspired by http://www.spinics.net/lists/wine/msg05996.html.
97 namespace Turn_STL_exceptions_into_MFC_exceptions
99 # ifndef _STATIC_CPPLIB
100 # error This hack only works with _STATIC_CPPLIB defined.
103 class CDisguisedSTLException : public CException
106 std::exception *m_pSTLException;
108 CDisguisedSTLException(std::exception *pSTLException)
109 : m_pSTLException(pSTLException)
112 virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT)
114 StringCchPrintf(lpszError, nMaxError, _T("%hs"), m_pSTLException->what());
119 const DWORD CPP_EXCEPTION = 0xE06D7363;
120 const DWORD MS_MAGIC = 0x19930520;
122 extern "C" void __stdcall _CxxThrowException(void *pObject, _s__ThrowInfo const *pObjectInfo)
124 __declspec(thread) static ULONG_PTR args[3] = { MS_MAGIC, 0, 0 };
127 pObject = reinterpret_cast<void *>(args[1]);
128 pObjectInfo = reinterpret_cast<_s__ThrowInfo const *>(args[2]);
132 args[1] = (ULONG_PTR)pObject;
133 args[2] = (ULONG_PTR)pObjectInfo;
136 if (pObjectInfo->pCatchableTypeArray && (i = pObjectInfo->pCatchableTypeArray->nCatchableTypes))
138 const char *name = typeid(std::exception).raw_name();
139 if (pObjectInfo->pCatchableTypeArray->arrayOfCatchableTypes[i - 1]->pType->name == name)
141 throw new CDisguisedSTLException(static_cast<std::exception *>(pObject));
144 RaiseException(CPP_EXCEPTION, EXCEPTION_NONCONTINUABLE, sizeof(args)/sizeof(args[0]), args);
150 /////////////////////////////////////////////////////////////////////////////
153 BEGIN_MESSAGE_MAP(CMergeApp, CWinApp)
154 //{{AFX_MSG_MAP(CMergeApp)
155 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
156 ON_COMMAND(ID_HELP, OnHelp)
157 ON_COMMAND_EX_RANGE(ID_FILE_PROJECT_MRU_FIRST, ID_FILE_PROJECT_MRU_LAST, OnOpenRecentFile)
158 ON_UPDATE_COMMAND_UI(ID_FILE_PROJECT_MRU_FIRST, &CWinApp::OnUpdateRecentFileMenu)
159 ON_COMMAND(ID_FILE_MERGINGMODE, OnMergingMode)
160 ON_UPDATE_COMMAND_UI(ID_FILE_MERGINGMODE, OnUpdateMergingMode)
161 ON_UPDATE_COMMAND_UI(ID_STATUS_MERGINGMODE, OnUpdateMergingStatus)
163 // Standard file based document commands
164 //ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
165 //ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
166 // Standard print setup command
167 ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
171 * @brief Mapping from command line argument name (eg, ignorews) to WinMerge
172 * option name (eg, Settings/IgnoreSpace).
174 * These arguments take an optional colon and number, like so:
176 * "/ignoreblanklines" (makes WinMerge ignore blank lines)
177 * "/ignoreblanklines:1" (makes WinMerge ignore blank lines)
178 * "/ignoreblanklines:0" (makes WinMerge not ignore blank lines)
183 LPCTSTR WinMergeOptionName;
187 /////////////////////////////////////////////////////////////////////////////
188 // CMergeApp construction
190 CMergeApp::CMergeApp() :
191 m_bNeedIdleTimer(FALSE)
194 , m_pHexMergeTemplate(0)
196 , m_mainThreadScripts(NULL)
197 , m_nLastCompareResult(0)
198 , m_bNonInteractive(false)
199 , m_pOptions(new CRegOptionsMgr())
200 , m_pGlobalFileFilter(new FileFilterHelper())
201 , m_nActiveOperations(0)
202 , m_pLangDlg(new CLanguageSelect())
203 , m_bEscShutdown(FALSE)
204 , m_bClearCaseTool(FALSE)
205 , m_bExitIfNoDiff(MergeCmdLineInfo::Disabled)
206 , m_pLineFilters(new LineFiltersList())
207 , m_pFilterCommentsManager(new FilterCommentsManager())
208 , m_pSyntaxColors(new SyntaxColors())
209 , m_pSourceControl(new SourceControl())
210 , m_bMergingMode(FALSE)
212 // add construction code here,
213 // Place all significant initialization in InitInstance
216 CMergeApp::~CMergeApp()
220 /////////////////////////////////////////////////////////////////////////////
221 // The one and only CMergeApp object
225 /////////////////////////////////////////////////////////////////////////////
226 // CMergeApp initialization
229 * @brief Initialize WinMerge application instance.
230 * @return TRUE if application initialization succeeds (and we'll run it),
231 * FALSE if something failed and we exit the instance.
232 * @todo We could handle these failure situations more gratefully, i.e. show
233 * at least some error message to the user..
235 BOOL CMergeApp::InitInstance()
237 // Prevents DLL hijacking
238 HMODULE hLibrary = GetModuleHandle(_T("kernel32.dll"));
239 BOOL (WINAPI *pfnSetSearchPathMode)(DWORD) = (BOOL (WINAPI *)(DWORD))GetProcAddress(hLibrary, "SetSearchPathMode");
240 if (pfnSetSearchPathMode)
241 pfnSetSearchPathMode(0x00000001L /*BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE*/ | 0x00008000L /*BASE_SEARCH_PATH_PERMANENT*/);
242 BOOL (WINAPI *pfnSetDllDirectoryA)(LPCSTR) = (BOOL (WINAPI *)(LPCSTR))GetProcAddress(hLibrary, "SetDllDirectoryA");
243 if (pfnSetDllDirectoryA)
244 pfnSetDllDirectoryA("");
246 JumpList::SetCurrentProcessExplicitAppUserModelID(L"Thingamahoochie.WinMerge");
248 InitCommonControls(); // initialize common control library
249 CWinApp::InitInstance(); // call parent class method
251 // Runtime switch so programmer may set this in interactive debugger
255 // get current setting
256 int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
257 // Keep freed memory blocks in the heap's linked list and mark them as freed
258 tmpFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
259 // Call _CrtCheckMemory at every allocation and deallocation request.
260 // WARNING: This slows down WinMerge *A LOT*
261 tmpFlag |= _CRTDBG_CHECK_ALWAYS_DF;
262 // Set the new state for the flag
263 _CrtSetDbgFlag( tmpFlag );
266 // CCrystalEdit Drag and Drop functionality needs AfxOleInit.
269 TRACE(_T("AfxOleInitFailed. OLE functionality disabled"));
272 // Standard initialization
273 // If you are not using these features and wish to reduce the size
274 // of your final executable, you should remove from the following
275 // the specific initialization routines you do not need.
277 // Revoke the standard OLE Message Filter to avoid drawing frame while loading files.
278 COleMessageFilter* pOldFilter = AfxOleGetMessageFilter();
279 pOldFilter->Revoke();
281 // Load registry keys from WinMerge.reg if existing WinMerge.reg
282 env::LoadRegistryFromFile(paths::ConcatPath(env::GetProgPath(), _T("WinMerge.reg")));
284 Options::Init(m_pOptions.get()); // Implementation in OptionsInit.cpp
286 // Initialize temp folder
289 // Cleanup left over tempfiles from previous instances.
290 // Normally this should not neet to do anything - but if for some reason
291 // WinMerge did not delete temp files this makes sure they are removed.
294 // Parse command-line arguments.
295 MergeCmdLineInfo cmdInfo(GetCommandLine());
297 // If paths were given to commandline we consider this being an invoke from
298 // commandline (from other application, shellextension etc).
299 BOOL bCommandLineInvoke = cmdInfo.m_Files.GetSize() > 0;
301 // WinMerge registry settings are stored under HKEY_CURRENT_USER/Software/Thingamahoochie
302 // This is the name of the company of the original author (Dean Grimm)
303 SetRegistryKey(_T("Thingamahoochie"));
305 BOOL bSingleInstance = GetOptionsMgr()->GetBool(OPT_SINGLE_INSTANCE) ||
306 (true == cmdInfo.m_bSingleInstance);
308 // Create exclusion mutex name
309 TCHAR szDesktopName[MAX_PATH] = _T("Win9xDesktop");
310 DWORD dwLengthNeeded;
311 GetUserObjectInformation(GetThreadDesktop(GetCurrentThreadId()), UOI_NAME,
312 szDesktopName, sizeof(szDesktopName), &dwLengthNeeded);
313 TCHAR szMutexName[MAX_PATH + 40];
314 // Combine window class name and desktop name to form a unique mutex name.
315 // As the window class name is decorated to distinguish between ANSI and
316 // UNICODE build, so will be the mutex name.
317 wsprintf(szMutexName, _T("%s-%s"), CMainFrame::szClassName, szDesktopName);
318 HANDLE hMutex = CreateMutex(NULL, FALSE, szMutexName);
320 WaitForSingleObject(hMutex, INFINITE);
321 if (bSingleInstance && GetLastError() == ERROR_ALREADY_EXISTS)
323 // Activate previous instance and send commandline to it
324 HWND hWnd = FindWindow(CMainFrame::szClassName, NULL);
328 ShowWindow(hWnd, SW_RESTORE);
329 SetForegroundWindow(GetLastActivePopup(hWnd));
330 LPTSTR cmdLine = GetCommandLine();
331 COPYDATASTRUCT data = { 0, (lstrlen(cmdLine) + 1) * sizeof(TCHAR), cmdLine};
332 if (SendMessage(hWnd, WM_COPYDATA, NULL, (LPARAM)&data))
334 ReleaseMutex(hMutex);
341 LoadStdProfileSettings(GetOptionsMgr()->GetInt(OPT_MRU_MAX)); // Load standard INI file options (including MRU)
343 InitializeFileFilters();
345 // Read last used filter from registry
346 // If filter fails to set, reset to default
347 const String filterString = m_pOptions->GetString(OPT_FILEFILTER_CURRENT);
348 BOOL bFilterSet = m_pGlobalFileFilter->SetFilter(filterString);
351 String filter = m_pGlobalFileFilter->GetFilterNameOrMask();
352 m_pOptions->SaveOption(OPT_FILEFILTER_CURRENT, filter);
356 UpdateCodepageModule();
358 if (m_pSourceControl)
359 m_pSourceControl->InitializeSourceControlMembers();
361 FileTransform::g_bUnpackerMode = theApp.GetProfileInt(_T("Settings"), _T("UnpackerMode"), PLUGIN_MANUAL);
362 FileTransform::g_bPredifferMode = theApp.GetProfileInt(_T("Settings"), _T("PredifferMode"), PLUGIN_MANUAL);
364 NONCLIENTMETRICS ncm = { sizeof NONCLIENTMETRICS };
365 if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof NONCLIENTMETRICS, &ncm, 0))
367 const int lfHeight = -MulDiv(9, CClientDC(CWnd::GetDesktopWindow()).GetDeviceCaps(LOGPIXELSY), 72);
368 if (abs(ncm.lfMenuFont.lfHeight) > abs(lfHeight))
369 ncm.lfMenuFont.lfHeight = lfHeight;
370 if (wcscmp(ncm.lfMenuFont.lfFaceName, L"Meiryo") == 0 || wcscmp(ncm.lfMenuFont.lfFaceName, L"\U000030e1\U000030a4\U000030ea\U000030aa"/* "Meiryo" in Japanese */) == 0)
371 wcscpy_s(ncm.lfMenuFont.lfFaceName, L"Meiryo UI");
372 m_fontGUI.CreateFontIndirect(&ncm.lfMenuFont);
376 Options::SyntaxColors::Load(GetOptionsMgr(), m_pSyntaxColors.get());
379 m_pLineFilters->Initialize(GetOptionsMgr());
381 // If there are no filters loaded, and there is filter string in previous
382 // option string, import old filters to new place.
383 if (m_pLineFilters->GetCount() == 0)
385 String oldFilter = theApp.GetProfileString(_T("Settings"), _T("RegExps"));
386 if (!oldFilter.empty())
387 m_pLineFilters->Import(oldFilter);
390 // Check if filter folder is set, and create it if not
391 String pathMyFolders = GetOptionsMgr()->GetString(OPT_FILTER_USERPATH);
392 if (pathMyFolders.empty())
394 // No filter path, set it to default and make sure it exists.
395 pathMyFolders = GetOptionsMgr()->GetDefault<String>(OPT_FILTER_USERPATH);
396 GetOptionsMgr()->SaveOption(OPT_FILTER_USERPATH, pathMyFolders);
397 theApp.m_pGlobalFileFilter->SetUserFilterPath(pathMyFolders);
399 if (!paths::CreateIfNeeded(pathMyFolders))
401 // Failed to create a folder, check it didn't already
403 DWORD errCode = GetLastError();
404 if (errCode != ERROR_ALREADY_EXISTS)
406 // Failed to create a folder for filters, fallback to
407 // "My Documents"-folder. It is not worth the trouble to
408 // bother user about this or user more clever solutions.
409 GetOptionsMgr()->SaveOption(OPT_FILTER_USERPATH, env::GetMyDocuments());
413 strdiff::Init(); // String diff init
414 strdiff::SetBreakChars(GetOptionsMgr()->GetString(OPT_BREAK_SEPARATORS).c_str());
416 m_bMergingMode = GetOptionsMgr()->GetBool(OPT_MERGE_MODE);
418 // Initialize i18n (multiple language) support
420 m_pLangDlg->InitializeLanguage((WORD)GetOptionsMgr()->GetInt(OPT_SELECTED_LANGUAGE));
422 m_mainThreadScripts = new CAssureScriptsForThread;
424 // Register the application's document templates. Document templates
425 // serve as the connection between documents, frame windows and views.
428 m_pOpenTemplate = new CMultiDocTemplate(
430 RUNTIME_CLASS(COpenDoc),
431 RUNTIME_CLASS(COpenFrame), // custom MDI child frame
432 RUNTIME_CLASS(COpenView));
433 AddDocTemplate(m_pOpenTemplate);
436 m_pDiffTemplate = new CMultiDocTemplate(
438 RUNTIME_CLASS(CMergeDoc),
439 RUNTIME_CLASS(CChildFrame), // custom MDI child frame
440 RUNTIME_CLASS(CMergeEditView));
441 AddDocTemplate(m_pDiffTemplate);
444 m_pHexMergeTemplate = new CMultiDocTemplate(
446 RUNTIME_CLASS(CHexMergeDoc),
447 RUNTIME_CLASS(CHexMergeFrame), // custom MDI child frame
448 RUNTIME_CLASS(CHexMergeView));
449 AddDocTemplate(m_pHexMergeTemplate);
452 m_pDirTemplate = new CMultiDocTemplate(
454 RUNTIME_CLASS(CDirDoc),
455 RUNTIME_CLASS(CDirFrame), // custom MDI child frame
456 RUNTIME_CLASS(CDirView));
457 AddDocTemplate(m_pDirTemplate);
459 // create main MDI Frame window
460 CMainFrame* pMainFrame = new CMainFrame;
461 if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
465 ReleaseMutex(hMutex);
470 m_pMainWnd = pMainFrame;
472 // Init menus -- hMenuDefault is for MainFrame
473 pMainFrame->m_hMenuDefault = pMainFrame->NewDefaultMenu();
476 // Note : for Windows98 compatibility, use FromHandle and not Attach/Detach
477 CMenu * pNewMenu = CMenu::FromHandle(pMainFrame->m_hMenuDefault);
478 pMainFrame->MDISetMenu(pNewMenu, NULL);
480 // The main window has been initialized, so activate and update it.
481 pMainFrame->ActivateFrame(cmdInfo.m_nCmdShow);
482 pMainFrame->UpdateWindow();
484 // Since this function actually opens paths for compare it must be
485 // called after initializing CMainFrame!
486 BOOL bContinue = TRUE;
487 if (ParseArgsAndDoOpen(cmdInfo, pMainFrame) == FALSE && bCommandLineInvoke)
491 ReleaseMutex(hMutex);
493 if (m_bNonInteractive)
498 // If user wants to cancel the compare, close WinMerge
499 if (bContinue == FALSE)
501 pMainFrame->PostMessage(WM_CLOSE, 0, 0);
507 static void OpenContributersFile(int&)
509 theApp.OpenFileToExternalEditor(paths::ConcatPath(env::GetProgPath(), ContributorsPath));
512 // App command to run the dialog
513 void CMergeApp::OnAppAbout()
516 aboutDlg.m_onclick_contributers += Poco::delegate(OpenContributersFile);
518 aboutDlg.m_onclick_contributers.clear();
521 /////////////////////////////////////////////////////////////////////////////
522 // CMergeApp commands
525 * @brief Called when application is about to exit.
526 * This functions is called when application is exiting, so this is
527 * good place to do cleanups.
528 * @return Application's exit value (returned from WinMain()).
530 int CMergeApp::ExitInstance()
534 // Save registry keys if existing WinMerge.reg
535 env::SaveRegistryToFile(paths::ConcatPath(env::GetProgPath(), _T("WinMerge.reg")), RegDir);
538 const String temp = env::GetTemporaryPath();
539 ClearTempfolder(temp);
540 delete m_mainThreadScripts;
541 CWinApp::ExitInstance();
545 int CMergeApp::DoMessageBox( LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt )
547 // This is a convenient point for breakpointing !!!
549 // Create a handle to store the parent window of the message box.
550 CWnd* pParentWnd = CWnd::GetActiveWindow();
552 // Check whether an active window was retrieved successfully.
553 if ( pParentWnd == NULL )
555 // Try to retrieve a handle to the last active popup.
556 CWnd * mainwnd = GetMainWnd();
558 pParentWnd = mainwnd->GetLastActivePopup();
561 // Use our own message box implementation, which adds the
562 // do not show again checkbox, and implements it on subsequent calls
563 // (if caller set the style)
565 if (m_bNonInteractive)
568 // Create the message box dialog.
569 CMessageBoxDialog dlgMessage(pParentWnd, lpszPrompt, _T(""), nType | MB_RIGHT_ALIGN,
572 if (m_pMainWnd->IsIconic())
573 m_pMainWnd->ShowWindow(SW_RESTORE);
575 // Display the message box dialog and return the result.
576 return static_cast<int>(dlgMessage.DoModal());
580 * @brief Set flag so that application will broadcast notification at next
581 * idle time (via WM_TIMER id=IDLE_TIMER)
583 void CMergeApp::SetNeedIdleTimer()
585 m_bNeedIdleTimer = TRUE;
588 BOOL CMergeApp::OnIdle(LONG lCount)
590 if (CWinApp::OnIdle(lCount))
593 // If anyone has requested notification when next idle occurs, send it
594 if (m_bNeedIdleTimer)
596 m_bNeedIdleTimer = FALSE;
597 m_pMainWnd->SendMessageToDescendants(WM_TIMER, IDLE_TIMER, lCount, TRUE, FALSE);
603 * @brief Load any known file filters.
605 * This function loads filter files from paths we know contain them.
606 * @note User's filter location may not be set yet.
608 void CMergeApp::InitializeFileFilters()
610 CString filterPath = GetProfileString(_T("Settings"), _T("UserFilterPath"), _T(""));
612 if (!filterPath.IsEmpty())
614 m_pGlobalFileFilter->SetUserFilterPath((LPCTSTR)filterPath);
616 m_pGlobalFileFilter->LoadAllFileFilters();
619 /** @brief Read command line arguments and open files for comparison.
621 * The name of the function is a legacy code from the time that this function
622 * actually parsed the command line. Today the parsing is done using the
623 * MergeCmdLineInfo class.
624 * @param [in] cmdInfo Commandline parameters info.
625 * @param [in] pMainFrame Pointer to application main frame.
626 * @return TRUE if we opened the compare, FALSE if the compare was canceled.
628 BOOL CMergeApp::ParseArgsAndDoOpen(MergeCmdLineInfo& cmdInfo, CMainFrame* pMainFrame)
630 BOOL bCompared = FALSE;
632 m_bNonInteractive = cmdInfo.m_bNonInteractive;
634 // Set the global file filter.
635 if (!cmdInfo.m_sFileFilter.empty())
637 m_pGlobalFileFilter->SetFilter(cmdInfo.m_sFileFilter);
641 if (cmdInfo.m_nCodepage)
643 UpdateDefaultCodepage(2,cmdInfo.m_nCodepage);
646 // Unless the user has requested to see WinMerge's usage open files for
648 if (cmdInfo.m_bShowUsage)
650 ShowHelp(CommandLineHelpLocation);
654 // Set the required information we need from the command line:
656 m_bClearCaseTool = cmdInfo.m_bClearCaseTool;
657 m_bExitIfNoDiff = cmdInfo.m_bExitIfNoDiff;
658 m_bEscShutdown = cmdInfo.m_bEscShutdown;
660 m_strSaveAsPath = cmdInfo.m_sOutputpath;
662 strDesc[0] = cmdInfo.m_sLeftDesc;
663 if (cmdInfo.m_Files.GetSize() < 3)
665 strDesc[1] = cmdInfo.m_sRightDesc;
669 strDesc[1] = cmdInfo.m_sMiddleDesc;
670 strDesc[2] = cmdInfo.m_sRightDesc;
673 if (cmdInfo.m_Files.GetSize() > 2)
675 cmdInfo.m_dwLeftFlags |= FFILEOPEN_CMDLINE;
676 cmdInfo.m_dwMiddleFlags |= FFILEOPEN_CMDLINE;
677 cmdInfo.m_dwRightFlags |= FFILEOPEN_CMDLINE;
678 DWORD dwFlags[3] = {cmdInfo.m_dwLeftFlags, cmdInfo.m_dwMiddleFlags, cmdInfo.m_dwRightFlags};
679 bCompared = pMainFrame->DoFileOpen(&cmdInfo.m_Files,
680 dwFlags, strDesc, cmdInfo.m_sReportFile, cmdInfo.m_bRecurse, NULL,
681 cmdInfo.m_sPreDiffer);
683 else if (cmdInfo.m_Files.GetSize() > 1)
685 DWORD dwFlags[3] = {cmdInfo.m_dwLeftFlags, cmdInfo.m_dwRightFlags, FFILEOPEN_NONE};
686 bCompared = pMainFrame->DoFileOpen(&cmdInfo.m_Files,
687 dwFlags, strDesc, cmdInfo.m_sReportFile, cmdInfo.m_bRecurse, NULL,
688 cmdInfo.m_sPreDiffer);
690 else if (cmdInfo.m_Files.GetSize() == 1)
692 String sFilepath = cmdInfo.m_Files[0];
693 if (IsProjectFile(sFilepath))
695 bCompared = LoadAndOpenProjectFile(sFilepath);
697 else if (IsConflictFile(sFilepath))
699 bCompared = pMainFrame->DoOpenConflict(sFilepath);
703 DWORD dwFlags[3] = {cmdInfo.m_dwLeftFlags, cmdInfo.m_dwRightFlags, FFILEOPEN_NONE};
704 bCompared = pMainFrame->DoFileOpen(&cmdInfo.m_Files,
705 dwFlags, strDesc, cmdInfo.m_sReportFile, cmdInfo.m_bRecurse, NULL,
706 cmdInfo.m_sPreDiffer);
709 else if (cmdInfo.m_Files.GetSize() == 0) // if there are no input args, we can check the display file dialog flag
711 BOOL showFiles = m_pOptions->GetBool(OPT_SHOW_SELECT_FILES_AT_STARTUP);
713 pMainFrame->DoFileOpen();
719 void CMergeApp::UpdateDefaultCodepage(int cpDefaultMode, int cpCustomCodepage)
723 switch (cpDefaultMode)
726 ucr::setDefaultCodepage(GetACP());
730 wLangId = GetLangId();
731 if (GetLocaleInfo(wLangId, LOCALE_IDEFAULTANSICODEPAGE, buff, sizeof(buff)/sizeof(buff[0])))
732 ucr::setDefaultCodepage(_ttol(buff));
734 ucr::setDefaultCodepage(GetACP());
737 ucr::setDefaultCodepage(cpCustomCodepage);
740 // no other valid option
742 ucr::setDefaultCodepage(GetACP());
747 * @brief Send current option settings into codepage module
749 void CMergeApp::UpdateCodepageModule()
751 // Get current codepage settings from the options module
752 // and push them into the codepage module
753 UpdateDefaultCodepage(GetOptionsMgr()->GetInt(OPT_CP_DEFAULT_MODE), GetOptionsMgr()->GetInt(OPT_CP_DEFAULT_CUSTOM));
756 /** @brief Open help from mainframe when user presses F1*/
757 void CMergeApp::OnHelp()
763 * @brief Open given file to external editor specified in options.
764 * @param [in] file Full path to file to open.
766 * Opens file to defined (in Options/system), Notepad by default,
767 * external editor. Path is decorated with quotation marks if needed
768 * (contains spaces). Also '$file' in editor path is replaced by
770 * @param [in] file Full path to file to open.
771 * @param [in] nLineNumber Line number to go to.
773 void CMergeApp::OpenFileToExternalEditor(const String& file, int nLineNumber/* = 1*/)
775 String sCmd = GetOptionsMgr()->GetString(OPT_EXT_EDITOR_CMD);
777 strutils::replace(sCmd, _T("$linenum"), strutils::to_str(nLineNumber));
779 size_t nIndex = sCmd.find(_T("$file"));
780 if (nIndex != String::npos)
782 sFile.insert(0, _T("\""));
783 strutils::replace(sCmd, _T("$file"), sFile);
784 nIndex = sCmd.find(' ', nIndex + sFile.length());
785 if (nIndex != String::npos)
786 sCmd.insert(nIndex, _T("\""));
798 STARTUPINFO stInfo = { sizeof STARTUPINFO };
799 PROCESS_INFORMATION processInfo;
801 retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(),
802 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
803 &stInfo, &processInfo);
807 // Error invoking external editor
808 String msg = strutils::format_string1(_("Failed to execute external editor: %1"), sCmd);
809 AfxMessageBox(msg.c_str(), MB_ICONSTOP);
813 CloseHandle(processInfo.hThread);
814 CloseHandle(processInfo.hProcess);
819 * @brief Open file, if it exists, else open url
821 void CMergeApp::OpenFileOrUrl(LPCTSTR szFile, LPCTSTR szUrl)
823 if (paths::DoesPathExist(szFile) == paths::IS_EXISTING_FILE)
824 ShellExecute(NULL, _T("open"), _T("notepad.exe"), szFile, NULL, SW_SHOWNORMAL);
826 ShellExecute(NULL, _T("open"), szUrl, NULL, NULL, SW_SHOWNORMAL);
830 * @brief Show Help - this is for opening help from outside mainframe.
831 * @param [in] helpLocation Location inside help, if NULL main help is opened.
833 void CMergeApp::ShowHelp(LPCTSTR helpLocation /*= NULL*/)
835 String sPath = env::GetProgPath();
836 LANGID LangId = GetLangId();
837 if (PRIMARYLANGID(LangId) == LANG_JAPANESE)
838 sPath = paths::ConcatPath(sPath, DocsPath_ja);
840 sPath = paths::ConcatPath(sPath, DocsPath);
841 if (helpLocation == NULL)
843 if (paths::DoesPathExist(sPath) == paths::IS_EXISTING_FILE)
844 ::HtmlHelp(NULL, sPath.c_str(), HH_DISPLAY_TOC, NULL);
846 ShellExecute(NULL, _T("open"), DocsURL, NULL, NULL, SW_SHOWNORMAL);
850 if (paths::DoesPathExist(sPath) == paths::IS_EXISTING_FILE)
852 sPath += helpLocation;
853 ::HtmlHelp(NULL, sPath.c_str(), HH_DISPLAY_TOPIC, NULL);
859 * @brief Creates backup before file is saved or copied over.
860 * This function handles formatting correct path and filename for
861 * backup file. Formatting is done based on several options available
862 * for users in Options/Backups dialog. After path is formatted, file
863 * is simply just copied. Not much error checking, just if copying
864 * succeeded or failed.
865 * @param [in] bFolder Are we creating backup in folder compare?
866 * @param [in] pszPath Full path to file to backup.
867 * @return TRUE if backup succeeds, or isn't just done.
869 BOOL CMergeApp::CreateBackup(BOOL bFolder, const String& pszPath)
871 // If user doesn't want to backups in folder compare, return
872 // success so operations don't abort.
873 if (bFolder && !(GetOptionsMgr()->GetBool(OPT_BACKUP_FOLDERCMP)))
875 // Likewise if user doesn't want backups in file compare
876 else if (!bFolder && !(GetOptionsMgr()->GetBool(OPT_BACKUP_FILECMP)))
879 // create backup copy of file if destination file exists
880 if (paths::DoesPathExist(pszPath) == paths::IS_EXISTING_FILE)
887 paths::SplitFilename(paths::GetLongPath(pszPath), &path, &filename, &ext);
889 // Determine backup folder
890 if (GetOptionsMgr()->GetInt(OPT_BACKUP_LOCATION) ==
891 PropBackups::FOLDER_ORIGINAL)
893 // Put backups to same folder than original file
896 else if (GetOptionsMgr()->GetInt(OPT_BACKUP_LOCATION) ==
897 PropBackups::FOLDER_GLOBAL)
899 // Put backups to global folder defined in options
900 bakPath = GetOptionsMgr()->GetString(OPT_BACKUP_GLOBALFOLDER);
904 bakPath = paths::GetLongPath(bakPath);
908 _RPTF0(_CRT_ERROR, "Unknown backup location!");
911 BOOL success = FALSE;
912 if (GetOptionsMgr()->GetBool(OPT_BACKUP_ADD_BAK))
914 // Don't add dot if there is no existing extension
917 ext += BACKUP_FILE_EXT;
920 // Append time to filename if wanted so
921 // NOTE just adds timestamp at the moment as I couldn't figure out
922 // nice way to add a real time (invalid chars etc).
923 if (GetOptionsMgr()->GetBool(OPT_BACKUP_ADD_TIME))
928 tm = localtime(&curtime);
930 timestr.Format(_T("%04d%02d%02d%02d%02d%02d"), tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
935 // Append filename and extension (+ optional .bak) to path
936 if ((bakPath.length() + filename.length() + ext.length())
940 bakPath = paths::ConcatPath(bakPath, filename);
946 success = CopyFile(pszPath.c_str(), bakPath.c_str(), FALSE);
950 String msg = strutils::format_string1(
951 _("Unable to backup original file:\n%1\n\nContinue anyway?"),
953 if (AfxMessageBox(msg.c_str(), MB_YESNO | MB_ICONWARNING | MB_DONT_ASK_AGAIN) != IDYES)
959 // we got here because we're either not backing up of there was nothing to backup
964 * @brief Sync file to Version Control System
965 * @param pszDest [in] Where to copy (incl. filename)
966 * @param bApplyToAll [in,out] Apply user selection to all items
967 * @param psError [out] Error string that can be shown to user in caller func
968 * @return User selection or -1 if error happened
969 * @sa CMainFrame::HandleReadonlySave()
970 * @sa CDirView::PerformActionList()
972 int CMergeApp::SyncFileToVCS(const String& pszDest, BOOL &bApplyToAll,
976 String strSavePath(pszDest);
979 nVerSys = GetOptionsMgr()->GetInt(OPT_VCS_SYSTEM);
981 int nRetVal = HandleReadonlySave(strSavePath, TRUE, bApplyToAll);
982 if (nRetVal == IDCANCEL || nRetVal == IDNO)
985 // If VC project opened from VSS sync and version control used
986 if ((nVerSys == SourceControl::VCS_VSS4 || nVerSys == SourceControl::VCS_VSS5) && m_pSourceControl->m_bVCProjSync)
988 if (!m_pSourceControl->m_vssHelper.ReLinkVCProj(strSavePath, sError))
995 * @brief Checks if path (file/folder) is read-only and asks overwriting it.
997 * @param strSavePath [in,out] Path where to save (file or folder)
998 * @param bMultiFile [in] Single file or multiple files/folder
999 * @param bApplyToAll [in,out] Apply last user selection for all items?
1000 * @return Users selection:
1001 * - IDOK: Item was not readonly, no actions
1002 * - IDYES/IDYESTOALL: Overwrite readonly item
1003 * - IDNO: User selected new filename (single file) or user wants to skip
1004 * - IDCANCEL: Cancel operation
1005 * @sa CMainFrame::SyncFileToVCS()
1006 * @sa CMergeDoc::DoSave()
1008 int CMergeApp::HandleReadonlySave(String& strSavePath, BOOL bMultiFile,
1013 BOOL bFileRO = FALSE;
1014 BOOL bFileExists = FALSE;
1022 TFile file(strSavePath);
1023 bFileExists = file.exists();
1025 bFileRO = !file.canWrite();
1030 nVerSys = GetOptionsMgr()->GetInt(OPT_VCS_SYSTEM);
1032 if (bFileExists && bFileRO)
1034 UINT userChoice = 0;
1035 // Version control system used?
1036 // Checkout file from VCS and modify, don't ask about overwriting
1038 if (nVerSys != SourceControl::VCS_NONE)
1040 bool bRetVal = m_pSourceControl->SaveToVersionControl(strSavePath);
1047 // Don't ask again if its already asked
1052 // Prompt for user choice
1055 // Multiple files or folder
1056 str = strutils::format_string1(_("%1\nis marked read-only. Would you like to override the read-only item?"), strSavePath);
1057 userChoice = AfxMessageBox(str.c_str(), MB_YESNOCANCEL |
1058 MB_ICONWARNING | MB_DEFBUTTON3 | MB_DONT_ASK_AGAIN |
1059 MB_YES_TO_ALL, IDS_SAVEREADONLY_MULTI);
1064 str = strutils::format_string1(_("%1 is marked read-only. Would you like to override the read-only file ? (No to save as new filename.)"), strSavePath);
1065 userChoice = AfxMessageBox(str.c_str(), MB_YESNOCANCEL |
1066 MB_ICONWARNING | MB_DEFBUTTON2 | MB_DONT_ASK_AGAIN,
1067 IDS_SAVEREADONLY_FMT);
1072 // Overwrite read-only file
1074 bApplyToAll = TRUE; // Don't ask again (no break here)
1076 CFile::GetStatus(strSavePath.c_str(), status);
1077 status.m_mtime = 0; // Avoid unwanted changes
1078 status.m_attribute &= ~CFile::readOnly;
1079 CFile::SetStatus(strSavePath.c_str(), status);
1083 // Save to new filename (single) /skip this item (multiple)
1087 if (SelectFile(AfxGetMainWnd()->GetSafeHwnd(), s, strSavePath.c_str(), _("Save As"), _T(""), FALSE))
1109 * @brief Shows VSS error from exception and writes log.
1111 void CMergeApp::ShowVSSError(CException *e, const String& strItem)
1113 TCHAR errStr[1024] = {0};
1114 if (e->GetErrorMessage(errStr, 1024))
1116 String errMsg = theApp.LoadString(IDS_VSS_ERRORFROM);
1117 String logMsg = errMsg;
1122 if (!strItem.empty())
1124 errMsg += _T("\n\n");
1129 LogErrorString(logMsg);
1130 AfxMessageBox(errMsg.c_str(), MB_ICONSTOP);
1134 LogErrorString(_T("VSSError (unable to GetErrorMessage)"));
1135 e->ReportError(MB_ICONSTOP, IDS_VSS_RUN_ERROR);
1140 * @brief Is specified file a project file?
1141 * @param [in] filepath Full path to file to check.
1142 * @return true if file is a projectfile.
1144 bool CMergeApp::IsProjectFile(const String& filepath) const
1147 paths::SplitFilename(filepath, NULL, NULL, &ext);
1148 if (strutils::compare_nocase(ext, ProjectFile::PROJECTFILE_EXT) == 0)
1154 bool CMergeApp::LoadProjectFile(const String& sProject, ProjectFile &project)
1156 if (sProject.empty())
1161 project.Read(sProject);
1163 catch (Poco::Exception& e)
1165 String sErr = _("Unknown error attempting to open project file");
1166 sErr += ucr::toTString(e.displayText());
1167 String msg = strutils::format_string2(_("Cannot open file\n%1\n\n%2"), sProject, sErr);
1168 AfxMessageBox(msg.c_str(), MB_ICONSTOP);
1175 bool CMergeApp::SaveProjectFile(const String& sProject, const ProjectFile &project)
1179 project.Save(sProject);
1181 catch (Poco::Exception& e)
1183 String sErr = _("Unknown error attempting to save project file");
1184 sErr += ucr::toTString(e.displayText());
1185 String msg = strutils::format_string2(_("Cannot open file\n%1\n\n%2"), sProject, sErr);
1186 AfxMessageBox(msg.c_str(), MB_ICONSTOP);
1194 * @brief Read project and perform comparison specified
1195 * @param [in] sProject Full path to project file.
1196 * @return TRUE if loading project file and starting compare succeeded.
1198 bool CMergeApp::LoadAndOpenProjectFile(const String& sProject, const String& sReportFile)
1200 ProjectFile project;
1201 if (!LoadProjectFile(sProject, project))
1205 BOOL bLeftReadOnly = FALSE;
1206 BOOL bMiddleReadOnly = FALSE;
1207 BOOL bRightReadOnly = FALSE;
1208 bool bRecursive = FALSE;
1209 project.GetPaths(files, bRecursive);
1210 bLeftReadOnly = project.GetLeftReadOnly();
1211 bMiddleReadOnly = project.GetMiddleReadOnly();
1212 bRightReadOnly = project.GetRightReadOnly();
1213 if (project.HasFilter())
1215 String filter = project.GetFilter();
1216 filter = strutils::trim_ws(filter);
1217 m_pGlobalFileFilter->SetFilter(filter);
1219 if (project.HasSubfolders())
1220 bRecursive = project.GetSubfolders() > 0;
1222 DWORD dwFlags[3] = {
1223 static_cast<DWORD>(files.GetPath(0).empty() ? FFILEOPEN_NONE : FFILEOPEN_PROJECT),
1224 static_cast<DWORD>(files.GetPath(1).empty() ? FFILEOPEN_NONE : FFILEOPEN_PROJECT),
1225 static_cast<DWORD>(files.GetPath(2).empty() ? FFILEOPEN_NONE : FFILEOPEN_PROJECT)
1228 dwFlags[0] |= FFILEOPEN_READONLY;
1229 if (files.GetSize() == 2)
1232 dwFlags[1] |= FFILEOPEN_READONLY;
1236 if (bMiddleReadOnly)
1237 dwFlags[1] |= FFILEOPEN_READONLY;
1239 dwFlags[2] |= FFILEOPEN_READONLY;
1242 GetOptionsMgr()->SaveOption(OPT_CMP_INCLUDE_SUBDIRS, bRecursive);
1244 BOOL rtn = GetMainFrame()->DoFileOpen(&files, dwFlags, NULL, sReportFile, bRecursive);
1246 AddToRecentProjectsMRU(sProject.c_str());
1251 * @brief Return windows language ID of current WinMerge GUI language
1253 WORD CMergeApp::GetLangId() const
1255 return m_pLangDlg->GetLangId();
1259 * @brief Lang aware version of CStatusBar::SetIndicators()
1261 void CMergeApp::SetIndicators(CStatusBar &sb, const UINT *rgid, int n) const
1263 m_pLangDlg->SetIndicators(sb, rgid, n);
1267 * @brief Translate menu to current WinMerge GUI language
1269 void CMergeApp::TranslateMenu(HMENU h) const
1271 m_pLangDlg->TranslateMenu(h);
1275 * @brief Translate dialog to current WinMerge GUI language
1277 void CMergeApp::TranslateDialog(HWND h) const
1279 CWnd *pWnd = CWnd::FromHandle(h);
1280 pWnd->SetFont(const_cast<CFont *>(&m_fontGUI));
1281 pWnd->SendMessageToDescendants(WM_SETFONT, (WPARAM)m_fontGUI.m_hObject, MAKELPARAM(FALSE, 0), TRUE);
1283 m_pLangDlg->TranslateDialog(h);
1287 * @brief Load string and translate to current WinMerge GUI language
1289 String CMergeApp::LoadString(UINT id) const
1291 return m_pLangDlg->LoadString(id);
1294 bool CMergeApp::TranslateString(const std::string& str, String& translated_str) const
1296 return m_pLangDlg->TranslateString(str, translated_str);
1300 * @brief Load dialog caption and translate to current WinMerge GUI language
1302 std::wstring CMergeApp::LoadDialogCaption(LPCTSTR lpDialogTemplateID) const
1304 return m_pLangDlg->LoadDialogCaption(lpDialogTemplateID);
1308 * @brief Adds specified file to the recent projects list.
1309 * @param [in] sPathName Path to project file
1311 void CMergeApp::AddToRecentProjectsMRU(LPCTSTR sPathName)
1313 // sPathName will be added to the top of the MRU list.
1314 // If sPathName already exists in the MRU list, it will be moved to the top
1315 if (m_pRecentFileList != NULL) {
1316 m_pRecentFileList->Add(sPathName);
1317 m_pRecentFileList->WriteList();
1321 void CMergeApp::SetupTempPath()
1323 String instTemp = env::GetPerInstanceString(TempFolderPrefix);
1324 if (GetOptionsMgr()->GetBool(OPT_USE_SYSTEM_TEMP_PATH))
1325 env::SetTemporaryPath(paths::ConcatPath(env::GetSystemTempPath(), instTemp));
1327 env::SetTemporaryPath(paths::ConcatPath(GetOptionsMgr()->GetString(OPT_CUSTOM_TEMP_PATH), instTemp));
1331 * @brief Handles menu selection from recent projects list
1332 * @param [in] nID Menu ID of the selected item
1334 BOOL CMergeApp::OnOpenRecentFile(UINT nID)
1336 return LoadAndOpenProjectFile(static_cast<const TCHAR *>(m_pRecentFileList->m_arrNames[nID-ID_FILE_PROJECT_MRU_FIRST]));
1340 * @brief Return if doc is in Merging/Editing mode
1342 bool CMergeApp::GetMergingMode() const
1344 return m_bMergingMode;
1348 * @brief Set doc to Merging/Editing mode
1350 void CMergeApp::SetMergingMode(bool bMergingMode)
1352 m_bMergingMode = bMergingMode;
1353 GetOptionsMgr()->SaveOption(OPT_MERGE_MODE, m_bMergingMode);
1357 * @brief Switch Merging/Editing mode and update
1358 * buffer read-only states accordingly
1360 void CMergeApp::OnMergingMode()
1362 bool bMergingMode = GetMergingMode();
1365 LangMessageBox(IDS_MERGE_MODE, MB_ICONINFORMATION | MB_DONT_DISPLAY_AGAIN);
1366 SetMergingMode(!bMergingMode);
1370 * @brief Update Menuitem for Merging Mode
1372 void CMergeApp::OnUpdateMergingMode(CCmdUI* pCmdUI)
1374 pCmdUI->Enable(true);
1375 pCmdUI->SetCheck(GetMergingMode());
1379 * @brief Update MergingMode UI in statusbar
1381 void CMergeApp::OnUpdateMergingStatus(CCmdUI *pCmdUI)
1383 String text = theApp.LoadString(IDS_MERGEMODE_MERGING);
1384 pCmdUI->SetText(text.c_str());
1385 pCmdUI->Enable(GetMergingMode());