OSDN Git Service

compiler-calculated maximum value for `m_SourceDefs` (#966)
[winmerge-jp/winmerge-jp.git] / Src / MergeApp.cpp
1 #include "StdAfx.h"
2 #include "MergeApp.h"
3 #include "Merge.h"
4 #include "VersionInfo.h"
5 #include "paths.h"
6 #include "Constants.h"
7 #include "unicoder.h"
8
9 // Get user language description of error, if available
10 String GetSysError(int nerr /* =-1 */)
11 {
12         if (nerr == -1)
13                 nerr = GetLastError();
14         LPVOID lpMsgBuf;
15         String str = _T("?");
16         if (FormatMessage( 
17                 FORMAT_MESSAGE_ALLOCATE_BUFFER | 
18                 FORMAT_MESSAGE_FROM_SYSTEM | 
19                 FORMAT_MESSAGE_IGNORE_INSERTS,
20                 nullptr,
21                 nerr,
22                 0, // Default language
23                 (LPTSTR) &lpMsgBuf,
24                 0,
25                 nullptr 
26                 ))
27         {
28                 str = (LPCTSTR)lpMsgBuf;
29                 // Free the buffer.
30                 LocalFree( lpMsgBuf );
31         }
32         return str;
33 }
34
35 /**
36  * @brief Get Options Manager.
37  * @return Pointer to OptionsMgr.
38  */
39 COptionsMgr * GetOptionsMgr()
40 {
41         CMergeApp *pApp = static_cast<CMergeApp *>(AfxGetApp());
42         return pApp->GetMergeOptionsMgr();
43 }
44
45 // Send message to log and debug window
46 void LogErrorString(const String& sz)
47 {
48         if (sz.empty()) return;
49         CString now = COleDateTime::GetCurrentTime().Format();
50         TRACE(_T("%s: %s\n"), (LPCTSTR)now, sz.c_str());
51 }
52
53 // Send message to log and debug window
54 void LogErrorStringUTF8(const std::string& sz)
55 {
56         if (sz.empty()) return;
57         String str = ucr::toTString(sz);
58         CString now = COleDateTime::GetCurrentTime().Format();
59         TRACE(_T("%s: %s\n"), (LPCTSTR)now, str.c_str());
60 }
61
62 /**
63  * @brief Load string resource and return as CString.
64  * @param [in] id Resource string ID.
65  * @return Resource string as CString.
66  */
67 String LoadResString(unsigned id)
68 {
69         return theApp.LoadString(id);
70 }
71
72 String tr(const std::string &str)
73 {
74         String translated_str;
75         theApp.TranslateString(str, translated_str);
76         return translated_str;
77 }
78
79 String tr(const char *msgctxt, const std::string &str)
80 {
81         String translated_str;
82         if (msgctxt)
83                 theApp.TranslateString("\x01\"" + std::string(msgctxt) + "\"" + str, translated_str);
84         else
85                 theApp.TranslateString(str, translated_str);
86         return translated_str;
87 }
88
89 void AppErrorMessageBox(const String& msg)
90 {
91         AppMsgBox::error(msg);
92 }
93
94 namespace AppMsgBox
95 {
96
97 namespace detail
98 {
99         int convert_to_winflags(int flags)
100         {
101                 int newflags = 0;
102
103                 if ((flags & (YES | NO | CANCEL)) == (YES | NO | CANCEL)) newflags |= MB_YESNOCANCEL;
104                 else if ((flags & (YES | NO)) == (YES | NO)) newflags |= MB_YESNO;
105                 else if ((flags & (OK | CANCEL)) == (OK | CANCEL)) newflags |= MB_OKCANCEL;
106                 else if ((flags & OK) == OK) newflags |= MB_OK;
107         
108                 if (flags & YES_TO_ALL) newflags |= MB_YES_TO_ALL;
109                 if (flags & DONT_DISPLAY_AGAIN) newflags |= MB_DONT_DISPLAY_AGAIN;
110
111                 return newflags;
112         }
113
114         int convert_resp(int resp)
115         {
116                 switch (resp)
117                 {
118                 case IDOK:
119                         return OK;
120                 case IDCANCEL:
121                         return CANCEL;
122                 case IDNO:
123                         return NO;
124                 case IDYES:
125                         return YES;
126                 case IDYESTOALL:
127                         return YES_TO_ALL;
128                 default:
129                         return OK;
130                 }
131         }
132 }
133
134 int error(const String& msg, int type)
135 {
136         return detail::convert_resp(AfxMessageBox(msg.c_str(), detail::convert_to_winflags(type) | MB_ICONSTOP));
137 }
138
139 int warning(const String& msg, int type)
140 {
141         return detail::convert_resp(AfxMessageBox(msg.c_str(), detail::convert_to_winflags(type) | MB_ICONWARNING));
142 }
143
144 int information(const String& msg, int type)
145 {
146         return detail::convert_resp(AfxMessageBox(msg.c_str(), detail::convert_to_winflags(type) | MB_ICONINFORMATION));
147 }
148
149 }
150
151 AboutInfo::AboutInfo()
152 {
153         CVersionInfo verinfo;
154         version = strutils::format_string1(_("Version %1"), verinfo.GetProductVersion());
155         private_build = verinfo.GetPrivateBuild();
156         if (!private_build.empty())
157         {
158                 version += _T(" + ") + private_build;
159         }
160
161         if (version.find(_T(" - ")) != String::npos)
162         {
163                 strutils::replace(version, _T(" - "), _T("\n"));
164                 version += _T(" ");
165         }
166         else
167         {
168                 version += _T("\n");
169         }
170
171 #if defined _M_IX86
172         version += _T(" ");
173         version += _T("x86");
174 #elif defined _M_IA64
175         version += _T(" IA64");
176 #elif defined _M_X64
177         version += _T(" ");
178         version += _("X64");
179 #elif defined _M_ARM64
180         version += _T(" ARM64");
181 #endif
182
183 #if defined _DEBUG
184         version += _T(" (");
185         version += _T("Debug");
186         version += _T(")");
187 #endif
188
189         copyright = _("WinMerge comes with ABSOLUTELY NO WARRANTY. This is free software and you are welcome to redistribute it under certain circumstances; see the GNU General Public License in the Help menu for details.");
190         copyright += _T("\n");
191         copyright += verinfo.GetLegalCopyright();
192         copyright += _T(" - All rights reserved.");
193
194         website = WinMergeURL;
195 }