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 "RegOptionsMgr.h"
39 #include "HexMergeDoc.h"
40 #include "HexMergeFrm.h"
41 #include "HexMergeView.h"
49 #include "PropBackups.h"
50 #include "FileOrFolderSelect.h"
52 #include "FileFilterHelper.h"
53 #include "LineFiltersList.h"
54 #include "SyntaxColors.h"
55 #include "OptionsSyntaxColors.h"
57 #include "ProjectFile.h"
58 #include "MergeEditView.h"
59 #include "LanguageSelect.h"
60 #include "OptionsDef.h"
61 #include "MergeCmdLineInfo.h"
62 #include "ConflictFileParser.h"
65 #include "stringdiffs.h"
67 #include "SourceControl.h"
69 #include "Constants.h"
71 // For shutdown cleanup
77 static char THIS_FILE[] = __FILE__;
82 /** @brief Location for command line help to open. */
83 static TCHAR CommandLineHelpLocation[] = _T("::/htmlhelp/Command_line.html");
85 // registry dir to WinMerge
86 static String f_RegDir = _T("Software\\Thingamahoochie\\WinMerge");
88 /** @brief Backup file extension. */
89 static const TCHAR BACKUP_FILE_EXT[] = _T("bak");
93 * @brief Turn STL exceptions into MFC exceptions.
94 * Based on the article "Visual C++ Exception-Handling Instrumentation"
95 * by Eugene Gershnik, published at http://www.drdobbs.com/184416600.
96 * Rethrow fix inspired by http://www.spinics.net/lists/wine/msg05996.html.
99 namespace Turn_STL_exceptions_into_MFC_exceptions
101 # ifndef _STATIC_CPPLIB
102 # error This hack only works with _STATIC_CPPLIB defined.
105 class CDisguisedSTLException : public CException
108 std::exception *m_pSTLException;
110 CDisguisedSTLException(std::exception *pSTLException)
111 : m_pSTLException(pSTLException)
114 virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT)
116 _sntprintf(lpszError, nMaxError, _T("%hs"), m_pSTLException->what());
121 const DWORD CPP_EXCEPTION = 0xE06D7363;
122 const DWORD MS_MAGIC = 0x19930520;
124 extern "C" void __stdcall _CxxThrowException(void *pObject, _s__ThrowInfo const *pObjectInfo)
126 __declspec(thread) static ULONG_PTR args[3] = { MS_MAGIC, 0, 0 };
129 pObject = reinterpret_cast<void *>(args[1]);
130 pObjectInfo = reinterpret_cast<_s__ThrowInfo const *>(args[2]);
134 args[1] = (ULONG_PTR)pObject;
135 args[2] = (ULONG_PTR)pObjectInfo;
138 if (pObjectInfo->pCatchableTypeArray && (i = pObjectInfo->pCatchableTypeArray->nCatchableTypes))
140 const char *name = typeid(std::exception).raw_name();
141 if (pObjectInfo->pCatchableTypeArray->arrayOfCatchableTypes[i - 1]->pType->name == name)
143 throw new CDisguisedSTLException(static_cast<std::exception *>(pObject));
146 RaiseException(CPP_EXCEPTION, EXCEPTION_NONCONTINUABLE, sizeof(args)/sizeof(args[0]), args);
152 /////////////////////////////////////////////////////////////////////////////
155 BEGIN_MESSAGE_MAP(CMergeApp, CWinApp)
156 //{{AFX_MSG_MAP(CMergeApp)
157 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
158 ON_COMMAND(ID_HELP, OnHelp)
159 ON_COMMAND_EX_RANGE(ID_FILE_MRU_FILE1, ID_FILE_MRU_FILE16, OnOpenRecentFile)
160 ON_COMMAND(ID_FILE_MERGINGMODE, OnMergingMode)
161 ON_UPDATE_COMMAND_UI(ID_FILE_MERGINGMODE, OnUpdateMergingMode)
162 ON_UPDATE_COMMAND_UI(ID_STATUS_MERGINGMODE, OnUpdateMergingStatus)
164 // Standard file based document commands
165 //ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
166 //ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
167 // Standard print setup command
168 ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
171 static void AddEnglishResourceHook();
174 * @brief Mapping from command line argument name (eg, ignorews) to WinMerge
175 * option name (eg, Settings/IgnoreSpace).
177 * These arguments take an optional colon and number, like so:
179 * "/ignoreblanklines" (makes WinMerge ignore blank lines)
180 * "/ignoreblanklines:1" (makes WinMerge ignore blank lines)
181 * "/ignoreblanklines:0" (makes WinMerge not ignore blank lines)
186 LPCTSTR WinMergeOptionName;
190 /////////////////////////////////////////////////////////////////////////////
191 // CMergeApp construction
193 CMergeApp::CMergeApp() :
194 m_bNeedIdleTimer(FALSE)
197 , m_pHexMergeTemplate(0)
199 , m_mainThreadScripts(NULL)
200 , m_nLastCompareResult(0)
201 , m_bNonInteractive(false)
202 , m_pOptions(new CRegOptionsMgr())
203 , m_pGlobalFileFilter(new FileFilterHelper())
204 , m_nActiveOperations(0)
205 , m_pLangDlg(new CLanguageSelect())
206 , m_bEscShutdown(FALSE)
207 , m_bClearCaseTool(FALSE)
208 , m_bExitIfNoDiff(MergeCmdLineInfo::Disabled)
209 , m_pLineFilters(new LineFiltersList())
210 , m_pSyntaxColors(new SyntaxColors())
211 , m_pSourceControl(new SourceControl())
212 , m_bMergingMode(FALSE)
214 // add construction code here,
215 // Place all significant initialization in InitInstance
218 CMergeApp::~CMergeApp()
222 /////////////////////////////////////////////////////////////////////////////
223 // The one and only CMergeApp object
227 /////////////////////////////////////////////////////////////////////////////
228 // CMergeApp initialization
231 * @brief Initialize WinMerge application instance.
232 * @return TRUE if application initialization succeeds (and we'll run it),
233 * FALSE if something failed and we exit the instance.
234 * @todo We could handle these failure situations more gratefully, i.e. show
235 * at least some error message to the user..
237 BOOL CMergeApp::InitInstance()
239 // Prevents DLL hijacking
240 HMODULE hLibrary = GetModuleHandle(_T("kernel32.dll"));
241 BOOL (WINAPI *pfnSetSearchPathMode)(DWORD) = (BOOL (WINAPI *)(DWORD))GetProcAddress(hLibrary, "SetSearchPathMode");
242 if (pfnSetSearchPathMode)
243 pfnSetSearchPathMode(0x00000001L /*BASE_SEARCH_PATH_ENABLE_SAFE_SEARCHMODE*/ | 0x00008000L /*BASE_SEARCH_PATH_PERMANENT*/);
244 BOOL (WINAPI *pfnSetDllDirectoryA)(LPCSTR) = (BOOL (WINAPI *)(LPCSTR))GetProcAddress(hLibrary, "SetDllDirectoryA");
245 if (pfnSetDllDirectoryA)
246 pfnSetDllDirectoryA("");
248 JumpList::SetCurrentProcessExplicitAppUserModelID(L"Thingamahoochie.WinMerge");
250 InitCommonControls(); // initialize common control library
251 CWinApp::InitInstance(); // call parent class method
253 // Runtime switch so programmer may set this in interactive debugger
257 // get current setting
258 int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
259 // Keep freed memory blocks in the heap's linked list and mark them as freed
260 tmpFlag |= _CRTDBG_DELAY_FREE_MEM_DF;
261 // Call _CrtCheckMemory at every allocation and deallocation request.
262 // WARNING: This slows down WinMerge *A LOT*
263 tmpFlag |= _CRTDBG_CHECK_ALWAYS_DF;
264 // Set the new state for the flag
265 _CrtSetDbgFlag( tmpFlag );
268 // CCrystalEdit Drag and Drop functionality needs AfxOleInit.
271 TRACE(_T("AfxOleInitFailed. OLE functionality disabled"));
274 // Standard initialization
275 // If you are not using these features and wish to reduce the size
276 // of your final executable, you should remove from the following
277 // the specific initialization routines you do not need.
279 // Revoke the standard OLE Message Filter to avoid drawing frame while loading files.
280 COleMessageFilter* pOldFilter = AfxOleGetMessageFilter();
281 pOldFilter->Revoke();
283 // Only needed by VC6
286 Enable3dControls(); // Call this when using MFC in a shared DLL
288 Enable3dControlsStatic(); // Call this when linking to MFC statically
292 // Load registry keys from WinMerge.reg if existing WinMerge.reg
293 env_LoadRegistryFromFile(paths_ConcatPath(env_GetProgPath(), _T("WinMerge.reg")));
295 OptionsInit(); // Implementation in OptionsInit.cpp
297 // Initialize temp folder
298 String instTemp = env_GetPerInstanceString(TempFolderPrefix);
299 if (GetOptionsMgr()->GetBool(OPT_USE_SYSTEM_TEMP_PATH))
300 env_SetTempPath(paths_ConcatPath(env_GetSystemTempPath(), instTemp));
302 env_SetTempPath(paths_ConcatPath(GetOptionsMgr()->GetString(OPT_CUSTOM_TEMP_PATH), instTemp));
304 // Cleanup left over tempfiles from previous instances.
305 // Normally this should not neet to do anything - but if for some reason
306 // WinMerge did not delete temp files this makes sure they are removed.
309 // Parse command-line arguments.
310 MergeCmdLineInfo cmdInfo = GetCommandLine();
312 // If paths were given to commandline we consider this being an invoke from
313 // commandline (from other application, shellextension etc).
314 BOOL bCommandLineInvoke = cmdInfo.m_Files.GetSize() > 0;
316 // WinMerge registry settings are stored under HKEY_CURRENT_USER/Software/Thingamahoochie
317 // This is the name of the company of the original author (Dean Grimm)
318 SetRegistryKey(_T("Thingamahoochie"));
320 BOOL bSingleInstance = GetOptionsMgr()->GetBool(OPT_SINGLE_INSTANCE) ||
321 (true == cmdInfo.m_bSingleInstance);
323 // Create exclusion mutex name
324 TCHAR szDesktopName[MAX_PATH] = _T("Win9xDesktop");
325 DWORD dwLengthNeeded;
326 GetUserObjectInformation(GetThreadDesktop(GetCurrentThreadId()), UOI_NAME,
327 szDesktopName, sizeof(szDesktopName), &dwLengthNeeded);
328 TCHAR szMutexName[MAX_PATH + 40];
329 // Combine window class name and desktop name to form a unique mutex name.
330 // As the window class name is decorated to distinguish between ANSI and
331 // UNICODE build, so will be the mutex name.
332 wsprintf(szMutexName, _T("%s-%s"), CMainFrame::szClassName, szDesktopName);
333 HANDLE hMutex = CreateMutex(NULL, FALSE, szMutexName);
335 WaitForSingleObject(hMutex, INFINITE);
336 if (bSingleInstance && GetLastError() == ERROR_ALREADY_EXISTS)
338 // Activate previous instance and send commandline to it
339 HWND hWnd = FindWindow(CMainFrame::szClassName, NULL);
343 ShowWindow(hWnd, SW_RESTORE);
344 SetForegroundWindow(GetLastActivePopup(hWnd));
345 LPTSTR cmdLine = GetCommandLine();
346 COPYDATASTRUCT data = { 0, (lstrlen(cmdLine) + 1) * sizeof(TCHAR), cmdLine};
347 if (SendMessage(hWnd, WM_COPYDATA, NULL, (LPARAM)&data))
349 ReleaseMutex(hMutex);
356 LoadStdProfileSettings(8); // Load standard INI file options (including MRU)
358 InitializeFileFilters();
360 // Read last used filter from registry
361 // If filter fails to set, reset to default
362 const String filterString = m_pOptions->GetString(OPT_FILEFILTER_CURRENT);
363 BOOL bFilterSet = m_pGlobalFileFilter->SetFilter(filterString.c_str());
366 String filter = m_pGlobalFileFilter->GetFilterNameOrMask();
367 m_pOptions->SaveOption(OPT_FILEFILTER_CURRENT, filter);
370 UpdateCodepageModule();
372 if (m_pSourceControl)
373 m_pSourceControl->InitializeSourceControlMembers();
375 g_bUnpackerMode = theApp.GetProfileInt(_T("Settings"), _T("UnpackerMode"), PLUGIN_MANUAL);
376 g_bPredifferMode = theApp.GetProfileInt(_T("Settings"), _T("PredifferMode"), PLUGIN_MANUAL);
379 Options::SyntaxColors::Load(m_pSyntaxColors.get());
382 m_pLineFilters->Initialize(GetOptionsMgr());
384 // If there are no filters loaded, and there is filter string in previous
385 // option string, import old filters to new place.
386 if (m_pLineFilters->GetCount() == 0)
388 String oldFilter = theApp.GetProfileString(_T("Settings"), _T("RegExps"));
389 if (!oldFilter.empty())
390 m_pLineFilters->Import(oldFilter);
393 // Check if filter folder is set, and create it if not
394 String pathMyFolders = GetOptionsMgr()->GetString(OPT_FILTER_USERPATH);
395 if (pathMyFolders.empty())
397 // No filter path, set it to default and make sure it exists.
398 String pathFilters = GetDefaultFilterUserPath(TRUE);
399 GetOptionsMgr()->SaveOption(OPT_FILTER_USERPATH, pathFilters);
400 theApp.m_pGlobalFileFilter->SetFileFilterPath(pathFilters.c_str());
403 sd_Init(); // String diff init
404 sd_SetBreakChars(GetOptionsMgr()->GetString(OPT_BREAK_SEPARATORS).c_str());
406 m_bMergingMode = GetOptionsMgr()->GetBool(OPT_MERGE_MODE);
408 // Initialize i18n (multiple language) support
410 m_pLangDlg->InitializeLanguage((WORD)GetOptionsMgr()->GetInt(OPT_SELECTED_LANGUAGE));
412 AddEnglishResourceHook(); // Use English string when l10n (foreign) string missing
414 m_mainThreadScripts = new CAssureScriptsForThread;
416 // Register the application's document templates. Document templates
417 // serve as the connection between documents, frame windows and views.
420 m_pOpenTemplate = new CMultiDocTemplate(
422 RUNTIME_CLASS(COpenDoc),
423 RUNTIME_CLASS(COpenFrame), // custom MDI child frame
424 RUNTIME_CLASS(COpenView));
425 AddDocTemplate(m_pOpenTemplate);
428 m_pDiffTemplate = new CMultiDocTemplate(
430 RUNTIME_CLASS(CMergeDoc),
431 RUNTIME_CLASS(CChildFrame), // custom MDI child frame
432 RUNTIME_CLASS(CMergeEditView));
433 AddDocTemplate(m_pDiffTemplate);
436 m_pHexMergeTemplate = new CMultiDocTemplate(
438 RUNTIME_CLASS(CHexMergeDoc),
439 RUNTIME_CLASS(CHexMergeFrame), // custom MDI child frame
440 RUNTIME_CLASS(CHexMergeView));
441 AddDocTemplate(m_pHexMergeTemplate);
444 m_pDirTemplate = new CMultiDocTemplate(
446 RUNTIME_CLASS(CDirDoc),
447 RUNTIME_CLASS(CDirFrame), // custom MDI child frame
448 RUNTIME_CLASS(CDirView));
449 AddDocTemplate(m_pDirTemplate);
451 // create main MDI Frame window
452 CMainFrame* pMainFrame = new CMainFrame;
453 if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
457 ReleaseMutex(hMutex);
462 m_pMainWnd = pMainFrame;
463 // Enable drag&drop files
464 pMainFrame->ModifyStyleEx(NULL, WS_EX_ACCEPTFILES);
466 // Init menus -- hMenuDefault is for MainFrame
467 pMainFrame->m_hMenuDefault = pMainFrame->NewDefaultMenu();
470 // Note : for Windows98 compatibility, use FromHandle and not Attach/Detach
471 CMenu * pNewMenu = CMenu::FromHandle(pMainFrame->m_hMenuDefault);
472 pMainFrame->MDISetMenu(pNewMenu, NULL);
474 // The main window has been initialized, so activate and update it.
475 pMainFrame->ActivateFrame(cmdInfo.m_nCmdShow);
476 pMainFrame->UpdateWindow();
478 // Since this function actually opens paths for compare it must be
479 // called after initializing CMainFrame!
480 BOOL bContinue = TRUE;
481 if (ParseArgsAndDoOpen(cmdInfo, pMainFrame) == FALSE && bCommandLineInvoke)
485 ReleaseMutex(hMutex);
487 if (m_bNonInteractive)
492 // If user wants to cancel the compare, close WinMerge
493 if (bContinue == FALSE)
495 pMainFrame->PostMessage(WM_CLOSE, 0, 0);
501 static void OpenContributersFile(int&)
503 theApp.OpenFileToExternalEditor(paths_ConcatPath(env_GetProgPath(), ContributorsPath));
506 // App command to run the dialog
507 void CMergeApp::OnAppAbout()
510 aboutDlg.m_onclick_contributers += Poco::delegate(OpenContributersFile);
512 aboutDlg.m_onclick_contributers.clear();
515 /////////////////////////////////////////////////////////////////////////////
516 // CMergeApp commands
519 * @brief Called when application is about to exit.
520 * This functions is called when application is exiting, so this is
521 * good place to do cleanups.
522 * @return Application's exit value (returned from WinMain()).
524 int CMergeApp::ExitInstance()
528 // Save registry keys if existing WinMerge.reg
529 env_SaveRegistryToFile(paths_ConcatPath(env_GetProgPath(), _T("WinMerge.reg")), RegDir);
532 const String temp = env_GetTempPath();
533 ClearTempfolder(temp);
534 delete m_mainThreadScripts;
535 CWinApp::ExitInstance();
539 static void AddEnglishResourceHook()
542 // After calling AfxSetResourceHandle to point to a language
543 // resource DLL, then the application is no longer on the
544 // resource lookup (defined by AfxFindResourceHandle).
546 // Add a dummy extension DLL record whose resource handle
547 // points to the application resources, just to provide
548 // fallback to English for any resources missing from
549 // the language resource DLL.
551 // (Why didn't Microsoft think of this? Bruno Haible who
552 // made gettext certainly thought of this.)
554 // NB: This does not fix the problem that if a control is
555 // missing from a dialog (because it was added only to the
556 // English version, for example) then the DDX_ function is
557 // going to fail. I see no easy way to intercept all DDX
558 // functions except by macro overriding the call--Perry, 2002-12-07.
560 static AFX_EXTENSION_MODULE FakeEnglishDLL = { NULL, NULL };
561 memset(&FakeEnglishDLL, 0, sizeof(FakeEnglishDLL));
562 FakeEnglishDLL.hModule = AfxGetApp()->m_hInstance;
563 FakeEnglishDLL.hResource = FakeEnglishDLL.hModule;
564 FakeEnglishDLL.bInitialized = TRUE;
565 new CDynLinkLibrary(FakeEnglishDLL); // hook into MFC extension DLL chain
570 int CMergeApp::DoMessageBox( LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt )
572 // This is a convenient point for breakpointing !!!
574 // Create a handle to store the parent window of the message box.
575 CWnd* pParentWnd = CWnd::GetActiveWindow();
577 // Check whether an active window was retrieved successfully.
578 if ( pParentWnd == NULL )
580 // Try to retrieve a handle to the last active popup.
581 CWnd * mainwnd = GetMainWnd();
583 pParentWnd = mainwnd->GetLastActivePopup();
586 // Use our own message box implementation, which adds the
587 // do not show again checkbox, and implements it on subsequent calls
588 // (if caller set the style)
590 if (m_bNonInteractive)
593 // Create the message box dialog.
594 CMessageBoxDialog dlgMessage(pParentWnd, lpszPrompt, _T(""), nType,
597 if (m_pMainWnd->IsIconic())
598 m_pMainWnd->ShowWindow(SW_RESTORE);
600 // Display the message box dialog and return the result.
601 return static_cast<int>(dlgMessage.DoModal());
605 * @brief Set flag so that application will broadcast notification at next
606 * idle time (via WM_TIMER id=IDLE_TIMER)
608 void CMergeApp::SetNeedIdleTimer()
610 m_bNeedIdleTimer = TRUE;
613 BOOL CMergeApp::OnIdle(LONG lCount)
615 if (CWinApp::OnIdle(lCount))
618 // If anyone has requested notification when next idle occurs, send it
619 if (m_bNeedIdleTimer)
621 m_bNeedIdleTimer = FALSE;
622 m_pMainWnd->SendMessageToDescendants(WM_TIMER, IDLE_TIMER, lCount, TRUE, FALSE);
628 * @brief Load any known file filters.
630 * This function loads filter files from paths we know contain them.
631 * @note User's filter location may not be set yet.
633 void CMergeApp::InitializeFileFilters()
635 CString filterPath = GetProfileString(_T("Settings"), _T("UserFilterPath"), _T(""));
637 if (!filterPath.IsEmpty())
639 m_pGlobalFileFilter->SetUserFilterPath((LPCTSTR)filterPath);
641 m_pGlobalFileFilter->LoadAllFileFilters();
644 /** @brief Read command line arguments and open files for comparison.
646 * The name of the function is a legacy code from the time that this function
647 * actually parsed the command line. Today the parsing is done using the
648 * MergeCmdLineInfo class.
649 * @param [in] cmdInfo Commandline parameters info.
650 * @param [in] pMainFrame Pointer to application main frame.
651 * @return TRUE if we opened the compare, FALSE if the compare was canceled.
653 BOOL CMergeApp::ParseArgsAndDoOpen(MergeCmdLineInfo& cmdInfo, CMainFrame* pMainFrame)
655 BOOL bCompared = FALSE;
656 m_bNonInteractive = cmdInfo.m_bNonInteractive;
658 // Set the global file filter.
659 if (!cmdInfo.m_sFileFilter.empty())
661 m_pGlobalFileFilter->SetFilter(cmdInfo.m_sFileFilter.c_str());
665 if (cmdInfo.m_nCodepage)
667 UpdateDefaultCodepage(2,cmdInfo.m_nCodepage);
670 // Unless the user has requested to see WinMerge's usage open files for
672 if (cmdInfo.m_bShowUsage)
674 ShowHelp(CommandLineHelpLocation);
678 // Set the required information we need from the command line:
680 m_bClearCaseTool = cmdInfo.m_bClearCaseTool;
681 m_bExitIfNoDiff = cmdInfo.m_bExitIfNoDiff;
682 m_bEscShutdown = cmdInfo.m_bEscShutdown;
684 m_strSaveAsPath = cmdInfo.m_sOutputpath.c_str();
686 m_strDescriptions[0] = cmdInfo.m_sLeftDesc;
687 if (cmdInfo.m_Files.GetSize() < 3)
689 m_strDescriptions[1] = cmdInfo.m_sRightDesc;
693 m_strDescriptions[1] = cmdInfo.m_sMiddleDesc;
694 m_strDescriptions[2] = cmdInfo.m_sRightDesc;
697 if (cmdInfo.m_Files.GetSize() > 2)
699 cmdInfo.m_dwLeftFlags |= FFILEOPEN_CMDLINE;
700 cmdInfo.m_dwMiddleFlags |= FFILEOPEN_CMDLINE;
701 cmdInfo.m_dwRightFlags |= FFILEOPEN_CMDLINE;
702 DWORD dwFlags[3] = {cmdInfo.m_dwLeftFlags, cmdInfo.m_dwMiddleFlags, cmdInfo.m_dwRightFlags};
703 bCompared = pMainFrame->DoFileOpen(&cmdInfo.m_Files,
704 dwFlags, cmdInfo.m_bRecurse, NULL,
705 cmdInfo.m_sPreDiffer.c_str());
707 else if (cmdInfo.m_Files.GetSize() > 1)
709 DWORD dwFlags[3] = {cmdInfo.m_dwLeftFlags, cmdInfo.m_dwRightFlags, FFILEOPEN_NONE};
710 bCompared = pMainFrame->DoFileOpen(&cmdInfo.m_Files,
711 dwFlags, cmdInfo.m_bRecurse, NULL,
712 cmdInfo.m_sPreDiffer.c_str());
714 else if (cmdInfo.m_Files.GetSize() == 1)
716 String sFilepath = cmdInfo.m_Files[0];
717 if (IsProjectFile(sFilepath))
719 bCompared = LoadAndOpenProjectFile(sFilepath);
721 else if (IsConflictFile(sFilepath))
723 bCompared = pMainFrame->DoOpenConflict(sFilepath.c_str());
727 DWORD dwFlags[3] = {cmdInfo.m_dwLeftFlags, cmdInfo.m_dwRightFlags, FFILEOPEN_NONE};
728 bCompared = pMainFrame->DoFileOpen(&cmdInfo.m_Files,
729 dwFlags, cmdInfo.m_bRecurse, NULL,
730 cmdInfo.m_sPreDiffer.c_str());
733 else if (cmdInfo.m_Files.GetSize() == 0) // if there are no input args, we can check the display file dialog flag
735 BOOL showFiles = m_pOptions->GetBool(OPT_SHOW_SELECT_FILES_AT_STARTUP);
737 pMainFrame->DoFileOpen();
743 void CMergeApp::UpdateDefaultCodepage(int cpDefaultMode, int cpCustomCodepage)
747 switch (cpDefaultMode)
750 ucr::setDefaultCodepage(GetACP());
754 wLangId = GetLangId();
755 if (GetLocaleInfo(wLangId, LOCALE_IDEFAULTANSICODEPAGE, buff, sizeof(buff)/sizeof(buff[0])))
756 ucr::setDefaultCodepage(_ttol(buff));
758 ucr::setDefaultCodepage(GetACP());
761 ucr::setDefaultCodepage(cpCustomCodepage);
764 // no other valid option
766 ucr::setDefaultCodepage(GetACP());
771 * @brief Send current option settings into codepage module
773 void CMergeApp::UpdateCodepageModule()
775 // Get current codepage settings from the options module
776 // and push them into the codepage module
777 UpdateDefaultCodepage(GetOptionsMgr()->GetInt(OPT_CP_DEFAULT_MODE), GetOptionsMgr()->GetInt(OPT_CP_DEFAULT_CUSTOM));
780 /** @brief Open help from mainframe when user presses F1*/
781 void CMergeApp::OnHelp()
787 * @brief Open given file to external editor specified in options.
788 * @param [in] file Full path to file to open.
790 * Opens file to defined (in Options/system), Notepad by default,
791 * external editor. Path is decorated with quotation marks if needed
792 * (contains spaces). Also '$file' in editor path is replaced by
794 * @param [in] file Full path to file to open.
795 * @param [in] nLineNumber Line number to go to.
797 void CMergeApp::OpenFileToExternalEditor(const String& file, int nLineNumber/* = 1*/)
799 String sCmd = GetOptionsMgr()->GetString(OPT_EXT_EDITOR_CMD);
801 string_replace(sCmd, _T("$linenum"), string_to_str(nLineNumber));
803 size_t nIndex = sCmd.find(_T("$file"));
804 if (nIndex != String::npos)
806 sFile.insert(0, _T("\""));
807 string_replace(sCmd, _T("$file"), sFile);
808 nIndex = sCmd.find(' ', nIndex + sFile.length());
809 if (nIndex != String::npos)
810 sCmd.insert(nIndex, _T("\""));
822 STARTUPINFO stInfo = {0};
823 stInfo.cb = sizeof(STARTUPINFO);
824 PROCESS_INFORMATION processInfo;
826 retVal = CreateProcess(NULL, (LPTSTR)sCmd.c_str(),
827 NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
828 &stInfo, &processInfo);
832 // Error invoking external editor
833 String msg = string_format_string1(_("Failed to execute external editor: %1"), sCmd);
834 AfxMessageBox(msg.c_str(), MB_ICONSTOP);
838 CloseHandle(processInfo.hThread);
839 CloseHandle(processInfo.hProcess);
844 * @brief Open file, if it exists, else open url
846 void CMergeApp::OpenFileOrUrl(LPCTSTR szFile, LPCTSTR szUrl)
848 if (paths_DoesPathExist(szFile) == IS_EXISTING_FILE)
849 ShellExecute(NULL, _T("open"), _T("notepad.exe"), szFile, NULL, SW_SHOWNORMAL);
851 ShellExecute(NULL, _T("open"), szUrl, NULL, NULL, SW_SHOWNORMAL);
855 * @brief Show Help - this is for opening help from outside mainframe.
856 * @param [in] helpLocation Location inside help, if NULL main help is opened.
858 void CMergeApp::ShowHelp(LPCTSTR helpLocation /*= NULL*/)
860 String sPath = env_GetProgPath();
861 LANGID LangId = GetLangId();
862 if (PRIMARYLANGID(LangId) == LANG_JAPANESE)
863 sPath = paths_ConcatPath(sPath, DocsPath_ja);
865 sPath = paths_ConcatPath(sPath, DocsPath);
866 if (helpLocation == NULL)
868 if (paths_DoesPathExist(sPath) == IS_EXISTING_FILE)
869 ::HtmlHelp(NULL, sPath.c_str(), HH_DISPLAY_TOC, NULL);
871 ShellExecute(NULL, _T("open"), DocsURL, NULL, NULL, SW_SHOWNORMAL);
875 if (paths_DoesPathExist(sPath) == IS_EXISTING_FILE)
877 sPath += helpLocation;
878 ::HtmlHelp(NULL, sPath.c_str(), HH_DISPLAY_TOPIC, NULL);
884 * @brief Creates backup before file is saved or copied over.
885 * This function handles formatting correct path and filename for
886 * backup file. Formatting is done based on several options available
887 * for users in Options/Backups dialog. After path is formatted, file
888 * is simply just copied. Not much error checking, just if copying
889 * succeeded or failed.
890 * @param [in] bFolder Are we creating backup in folder compare?
891 * @param [in] pszPath Full path to file to backup.
892 * @return TRUE if backup succeeds, or isn't just done.
894 BOOL CMergeApp::CreateBackup(BOOL bFolder, const String& pszPath)
896 // If user doesn't want to backups in folder compare, return
897 // success so operations don't abort.
898 if (bFolder && !(GetOptionsMgr()->GetBool(OPT_BACKUP_FOLDERCMP)))
900 // Likewise if user doesn't want backups in file compare
901 else if (!bFolder && !(GetOptionsMgr()->GetBool(OPT_BACKUP_FILECMP)))
904 // create backup copy of file if destination file exists
905 if (paths_DoesPathExist(pszPath) == IS_EXISTING_FILE)
912 paths_SplitFilename(paths_GetLongPath(pszPath), &path, &filename, &ext);
914 // Determine backup folder
915 if (GetOptionsMgr()->GetInt(OPT_BACKUP_LOCATION) ==
916 PropBackups::FOLDER_ORIGINAL)
918 // Put backups to same folder than original file
921 else if (GetOptionsMgr()->GetInt(OPT_BACKUP_LOCATION) ==
922 PropBackups::FOLDER_GLOBAL)
924 // Put backups to global folder defined in options
925 bakPath = GetOptionsMgr()->GetString(OPT_BACKUP_GLOBALFOLDER);
929 bakPath = paths_GetLongPath(bakPath);
933 _RPTF0(_CRT_ERROR, "Unknown backup location!");
936 BOOL success = FALSE;
937 if (GetOptionsMgr()->GetBool(OPT_BACKUP_ADD_BAK))
939 // Don't add dot if there is no existing extension
942 ext += BACKUP_FILE_EXT;
945 // Append time to filename if wanted so
946 // NOTE just adds timestamp at the moment as I couldn't figure out
947 // nice way to add a real time (invalid chars etc).
948 if (GetOptionsMgr()->GetBool(OPT_BACKUP_ADD_TIME))
953 tm = localtime(&curtime);
955 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);
960 // Append filename and extension (+ optional .bak) to path
961 if ((bakPath.length() + filename.length() + ext.length())
965 bakPath = paths_ConcatPath(bakPath, filename);
971 success = CopyFile(pszPath.c_str(), bakPath.c_str(), FALSE);
975 String msg = string_format_string1(
976 _("Unable to backup original file:\n%1\n\nContinue anyway?"),
978 if (AfxMessageBox(msg.c_str(), MB_YESNO | MB_ICONWARNING | MB_DONT_ASK_AGAIN) != IDYES)
984 // we got here because we're either not backing up of there was nothing to backup
989 * @brief Sync file to Version Control System
990 * @param pszDest [in] Where to copy (incl. filename)
991 * @param bApplyToAll [in,out] Apply user selection to all items
992 * @param psError [out] Error string that can be shown to user in caller func
993 * @return User selection or -1 if error happened
994 * @sa CMainFrame::HandleReadonlySave()
995 * @sa CDirView::PerformActionList()
997 int CMergeApp::SyncFileToVCS(const String& pszDest, BOOL &bApplyToAll,
1000 String sActionError;
1001 String strSavePath(pszDest);
1004 nVerSys = GetOptionsMgr()->GetInt(OPT_VCS_SYSTEM);
1006 int nRetVal = HandleReadonlySave(strSavePath, TRUE, bApplyToAll);
1007 if (nRetVal == IDCANCEL || nRetVal == IDNO)
1010 // If VC project opened from VSS sync and version control used
1011 if ((nVerSys == SourceControl::VCS_VSS4 || nVerSys == SourceControl::VCS_VSS5) && m_pSourceControl->m_bVCProjSync)
1013 if (!m_pSourceControl->m_vssHelper.ReLinkVCProj(strSavePath, sError))
1020 * @brief Checks if path (file/folder) is read-only and asks overwriting it.
1022 * @param strSavePath [in,out] Path where to save (file or folder)
1023 * @param bMultiFile [in] Single file or multiple files/folder
1024 * @param bApplyToAll [in,out] Apply last user selection for all items?
1025 * @return Users selection:
1026 * - IDOK: Item was not readonly, no actions
1027 * - IDYES/IDYESTOALL: Overwrite readonly item
1028 * - IDNO: User selected new filename (single file) or user wants to skip
1029 * - IDCANCEL: Cancel operation
1030 * @sa CMainFrame::SyncFileToVCS()
1031 * @sa CMergeDoc::DoSave()
1033 int CMergeApp::HandleReadonlySave(String& strSavePath, BOOL bMultiFile,
1037 UINT userChoice = 0;
1039 BOOL bFileRO = FALSE;
1040 BOOL bFileExists = FALSE;
1048 TFile file(strSavePath);
1049 bFileExists = file.exists();
1051 bFileRO = !file.canWrite();
1056 nVerSys = GetOptionsMgr()->GetInt(OPT_VCS_SYSTEM);
1058 if (bFileExists && bFileRO)
1060 // Version control system used?
1061 // Checkout file from VCS and modify, don't ask about overwriting
1063 if (nVerSys != SourceControl::VCS_NONE)
1065 bool bRetVal = m_pSourceControl->SaveToVersionControl(strSavePath);
1072 // Don't ask again if its already asked
1077 // Prompt for user choice
1080 // Multiple files or folder
1081 str = string_format_string1(_("%1\nis marked read-only. Would you like to override the read-only item?"), strSavePath);
1082 userChoice = AfxMessageBox(str.c_str(), MB_YESNOCANCEL |
1083 MB_ICONWARNING | MB_DEFBUTTON3 | MB_DONT_ASK_AGAIN |
1084 MB_YES_TO_ALL, IDS_SAVEREADONLY_MULTI);
1089 str = string_format_string1(_("%1 is marked read-only. Would you like to override the read-only file ? (No to save as new filename.)"), strSavePath);
1090 userChoice = AfxMessageBox(str.c_str(), MB_YESNOCANCEL |
1091 MB_ICONWARNING | MB_DEFBUTTON2 | MB_DONT_ASK_AGAIN,
1092 IDS_SAVEREADONLY_FMT);
1097 // Overwrite read-only file
1099 bApplyToAll = TRUE; // Don't ask again (no break here)
1101 CFile::GetStatus(strSavePath.c_str(), status);
1102 status.m_mtime = 0; // Avoid unwanted changes
1103 status.m_attribute &= ~CFile::readOnly;
1104 CFile::SetStatus(strSavePath.c_str(), status);
1108 // Save to new filename (single) /skip this item (multiple)
1112 if (SelectFile(AfxGetMainWnd()->GetSafeHwnd(), s, strSavePath.c_str(), _("Save As"), _T(""), FALSE))
1134 * @brief Shows VSS error from exception and writes log.
1136 void CMergeApp::ShowVSSError(CException *e, const String& strItem)
1138 TCHAR errStr[1024] = {0};
1139 if (e->GetErrorMessage(errStr, 1024))
1141 String errMsg = theApp.LoadString(IDS_VSS_ERRORFROM);
1142 String logMsg = errMsg;
1147 if (!strItem.empty())
1149 errMsg += _T("\n\n");
1154 LogErrorString(logMsg);
1155 AfxMessageBox(errMsg.c_str(), MB_ICONSTOP);
1159 LogErrorString(_T("VSSError (unable to GetErrorMessage)"));
1160 e->ReportError(MB_ICONSTOP, IDS_VSS_RUN_ERROR);
1165 * @brief Is specified file a project file?
1166 * @param [in] filepath Full path to file to check.
1167 * @return true if file is a projectfile.
1169 bool CMergeApp::IsProjectFile(const String& filepath) const
1172 paths_SplitFilename(filepath, NULL, NULL, &ext);
1173 if (string_compare_nocase(ext, ProjectFile::PROJECTFILE_EXT) == 0)
1179 bool CMergeApp::LoadProjectFile(const String& sProject, ProjectFile &project)
1181 if (sProject.empty())
1186 project.Read(sProject);
1188 catch (Poco::Exception& e)
1190 String sErr = _("Unknown error attempting to open project file");
1191 sErr += ucr::toTString(e.displayText());
1192 String msg = string_format_string2(_("Cannot open file\n%1\n\n%2"), sProject, sErr);
1193 AfxMessageBox(msg.c_str(), MB_ICONSTOP);
1200 bool CMergeApp::SaveProjectFile(const String& sProject, const ProjectFile &project)
1204 project.Save(sProject);
1206 catch (Poco::Exception& e)
1208 String sErr = _("Unknown error attempting to save project file");
1209 sErr += ucr::toTString(e.displayText());
1210 String msg = string_format_string2(_("Cannot open file\n%1\n\n%2"), sProject, sErr);
1211 AfxMessageBox(msg.c_str(), MB_ICONSTOP);
1219 * @brief Read project and perform comparison specified
1220 * @param [in] sProject Full path to project file.
1221 * @return TRUE if loading project file and starting compare succeeded.
1223 bool CMergeApp::LoadAndOpenProjectFile(const String& sProject)
1225 ProjectFile project;
1226 if (!LoadProjectFile(sProject, project))
1230 BOOL bLeftReadOnly = FALSE;
1231 BOOL bMiddleReadOnly = FALSE;
1232 BOOL bRightReadOnly = FALSE;
1233 bool bRecursive = FALSE;
1234 project.GetPaths(files, bRecursive);
1235 bLeftReadOnly = project.GetLeftReadOnly();
1236 bMiddleReadOnly = project.GetMiddleReadOnly();
1237 bRightReadOnly = project.GetRightReadOnly();
1238 if (project.HasFilter())
1240 String filter = project.GetFilter();
1241 filter = string_trim_ws(filter);
1242 m_pGlobalFileFilter->SetFilter(filter);
1244 if (project.HasSubfolders())
1245 bRecursive = project.GetSubfolders() > 0;
1247 DWORD dwFlags[3] = {
1248 files.GetPath(0).empty() ? FFILEOPEN_NONE : FFILEOPEN_PROJECT,
1249 files.GetPath(1).empty() ? FFILEOPEN_NONE : FFILEOPEN_PROJECT,
1250 files.GetPath(2).empty() ? FFILEOPEN_NONE : FFILEOPEN_PROJECT
1253 dwFlags[0] |= FFILEOPEN_READONLY;
1254 if (files.GetSize() == 2)
1257 dwFlags[1] |= FFILEOPEN_READONLY;
1261 if (bMiddleReadOnly)
1262 dwFlags[1] |= FFILEOPEN_READONLY;
1264 dwFlags[2] |= FFILEOPEN_READONLY;
1267 WriteProfileInt(_T("Settings"), _T("Recurse"), bRecursive);
1269 BOOL rtn = GetMainFrame()->DoFileOpen(&files, dwFlags, bRecursive);
1271 AddToRecentProjectsMRU(sProject.c_str());
1276 * @brief Return windows language ID of current WinMerge GUI language
1278 WORD CMergeApp::GetLangId() const
1280 return m_pLangDlg->GetLangId();
1284 * @brief Lang aware version of CStatusBar::SetIndicators()
1286 void CMergeApp::SetIndicators(CStatusBar &sb, const UINT *rgid, int n) const
1288 m_pLangDlg->SetIndicators(sb, rgid, n);
1292 * @brief Translate menu to current WinMerge GUI language
1294 void CMergeApp::TranslateMenu(HMENU h) const
1296 m_pLangDlg->TranslateMenu(h);
1300 * @brief Translate dialog to current WinMerge GUI language
1302 void CMergeApp::TranslateDialog(HWND h) const
1304 m_pLangDlg->TranslateDialog(h);
1308 * @brief Load string and translate to current WinMerge GUI language
1310 String CMergeApp::LoadString(UINT id) const
1312 return m_pLangDlg->LoadString(id);
1315 bool CMergeApp::TranslateString(const std::string& str, String& translated_str) const
1317 return m_pLangDlg->TranslateString(str, translated_str);
1321 * @brief Load dialog caption and translate to current WinMerge GUI language
1323 std::wstring CMergeApp::LoadDialogCaption(LPCTSTR lpDialogTemplateID) const
1325 return m_pLangDlg->LoadDialogCaption(lpDialogTemplateID);
1329 * @brief Get default editor path.
1330 * @return full path to the editor program executable.
1332 String CMergeApp::GetDefaultEditor() const
1334 return paths_ConcatPath(env_GetWindowsDirectory(), _T("NOTEPAD.EXE"));
1338 * @brief Get default user filter folder path.
1339 * This function returns the default filter path for user filters.
1340 * If wanted so (@p bCreate) path can be created if it does not
1341 * exist yet. But you really want to create the patch only when
1342 * there is no user path defined.
1343 * @param [in] bCreate If TRUE filter path is created if it does
1345 * @return Default folder for user filters.
1347 String CMergeApp::GetDefaultFilterUserPath(BOOL bCreate /*=FALSE*/) const
1349 String pathMyFolders = env_GetMyDocuments();
1350 String pathFilters(pathMyFolders);
1351 pathFilters = paths_ConcatPath(pathFilters, DefaultRelativeFilterPath);
1353 if (bCreate && !paths_CreateIfNeeded(pathFilters))
1355 // Failed to create a folder, check it didn't already
1357 DWORD errCode = GetLastError();
1358 if (errCode != ERROR_ALREADY_EXISTS)
1360 // Failed to create a folder for filters, fallback to
1361 // "My Documents"-folder. It is not worth the trouble to
1362 // bother user about this or user more clever solutions.
1363 pathFilters = pathMyFolders;
1371 * @brief Adds specified file to the recent projects list.
1372 * @param [in] sPathName Path to project file
1374 void CMergeApp::AddToRecentProjectsMRU(LPCTSTR sPathName)
1376 // sPathName will be added to the top of the MRU list.
1377 // If sPathName already exists in the MRU list, it will be moved to the top
1378 if (m_pRecentFileList != NULL) {
1379 m_pRecentFileList->Add(sPathName);
1380 m_pRecentFileList->WriteList();
1385 * @brief Handles menu selection from recent projects list
1386 * @param [in] nID Menu ID of the selected item
1388 BOOL CMergeApp::OnOpenRecentFile(UINT nID)
1390 return LoadAndOpenProjectFile((const TCHAR *)m_pRecentFileList->m_arrNames[nID-ID_FILE_MRU_FILE1]);
1394 * @brief Return if doc is in Merging/Editing mode
1396 bool CMergeApp::GetMergingMode() const
1398 return m_bMergingMode;
1402 * @brief Set doc to Merging/Editing mode
1404 void CMergeApp::SetMergingMode(bool bMergingMode)
1406 m_bMergingMode = bMergingMode;
1407 GetOptionsMgr()->SaveOption(OPT_MERGE_MODE, m_bMergingMode);
1411 * @brief Switch Merging/Editing mode and update
1412 * buffer read-only states accordingly
1414 void CMergeApp::OnMergingMode()
1416 bool bMergingMode = GetMergingMode();
1419 LangMessageBox(IDS_MERGE_MODE, MB_ICONINFORMATION | MB_DONT_DISPLAY_AGAIN);
1420 SetMergingMode(!bMergingMode);
1424 * @brief Update Menuitem for Merging Mode
1426 void CMergeApp::OnUpdateMergingMode(CCmdUI* pCmdUI)
1428 pCmdUI->Enable(true);
1429 pCmdUI->SetCheck(GetMergingMode());
1433 * @brief Update MergingMode UI in statusbar
1435 void CMergeApp::OnUpdateMergingStatus(CCmdUI *pCmdUI)
1437 String text = theApp.LoadString(IDS_MERGEMODE_MERGING);
1438 pCmdUI->SetText(text.c_str());
1439 pCmdUI->Enable(GetMergingMode());