OSDN Git Service

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