OSDN Git Service

Import from xkeymacssrc347.
[xkeymacs/xkeymacs.git] / xkeymacs / xkeymacs.cpp
1 // xkeymacs.cpp : Defines the class behaviors for the application.\r
2 //\r
3 \r
4 #include "stdafx.h"\r
5 #include "xkeymacs.h"\r
6 #include "Profile.h"\r
7 #include "MainFrm.h"\r
8 \r
9 #ifdef _DEBUG\r
10 #define new DEBUG_NEW\r
11 #undef THIS_FILE\r
12 static char THIS_FILE[] = __FILE__;\r
13 #endif\r
14 \r
15 /////////////////////////////////////////////////////////////////////////////\r
16 // CXkeymacsApp\r
17 \r
18 BEGIN_MESSAGE_MAP(CXkeymacsApp, CWinApp)\r
19         //{{AFX_MSG_MAP(CXkeymacsApp)\r
20         ON_COMMAND(ID_APP_ABOUT, OnAppAbout)\r
21                 // NOTE - the ClassWizard will add and remove mapping macros here.\r
22                 //    DO NOT EDIT what you see in these blocks of generated code!\r
23         //}}AFX_MSG_MAP\r
24         // Standard file based document commands\r
25         ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)\r
26         ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)\r
27 END_MESSAGE_MAP()\r
28 \r
29 /////////////////////////////////////////////////////////////////////////////\r
30 // CXkeymacsApp construction\r
31 \r
32 CXkeymacsApp::CXkeymacsApp()\r
33 {\r
34         // TODO: add construction code here,\r
35         // Place all significant initialization in InitInstance\r
36 \r
37         m_Instance = FirstInstance;\r
38 }\r
39 \r
40 /////////////////////////////////////////////////////////////////////////////\r
41 // The one and only CXkeymacsApp object\r
42 \r
43 CXkeymacsApp theApp;\r
44 \r
45 /////////////////////////////////////////////////////////////////////////////\r
46 // CXkeymacsApp initialization\r
47 \r
48 BOOL CXkeymacsApp::InitInstance()\r
49 {\r
50 //      AfxEnableControlContainer();\r
51 \r
52         // Standard initialization\r
53         // If you are not using these features and wish to reduce the size\r
54         //  of your final executable, you should remove from the following\r
55         //  the specific initialization routines you do not need.\r
56 \r
57         m_hMutex = CreateMutex(FALSE, 0, CString(MAKEINTRESOURCE(AFX_IDS_APP_TITLE)));\r
58 \r
59     if (::GetLastError() == ERROR_ALREADY_EXISTS) {\r
60                 m_Instance = SecondInstance;\r
61         CloseHandle( m_hMutex );\r
62         return FALSE;\r
63     }\r
64 \r
65 #ifdef _AFXDLL\r
66         Enable3dControls();                     // Call this when using MFC in a shared DLL\r
67 #else\r
68         Enable3dControlsStatic();       // Call this when linking to MFC statically\r
69 #endif\r
70 \r
71         // Change the registry key under which our settings are stored.\r
72         // TODO: You should modify this string to be something appropriate\r
73         // such as the name of your company or organization.\r
74 //      SetRegistryKey(_T("Local AppWizard-Generated Applications"));\r
75         SetRegistryKey(IDS_REGISTRY_KEY);\r
76         free((void*)m_pszProfileName);\r
77         m_pszProfileName=_tcsdup(_T("XKeymacs2"));\r
78 //\r
79 //      LoadStdProfileSettings();  // Load standard INI file options (including MRU)\r
80 \r
81         // Parse command line for standard shell commands, DDE, file open\r
82 //      CCommandLineInfo cmdInfo;\r
83 //      ParseCommandLine(cmdInfo);\r
84 \r
85         // Dispatch commands specified on the command line\r
86 //      if (!ProcessShellCommand(cmdInfo))\r
87 //              return FALSE;\r
88 \r
89         // The one and only window has been initialized, so show and update it.\r
90         m_pMainWnd = new CMainFrame;\r
91         m_pMainWnd->ShowWindow(SW_HIDE);\r
92         m_pMainWnd->UpdateWindow();\r
93         SetClassLong(m_pMainWnd->m_hWnd, GCL_HICON, (LONG)LoadIcon(IDR_MAINFRAME));\r
94 \r
95         // set registry key\r
96 //      CMainFrame *pMainWnd = (CMainFrame*)m_pMainWnd;\r
97         CProfile::InitDllData();\r
98 \r
99         return TRUE;\r
100 }\r
101 \r
102 \r
103 /////////////////////////////////////////////////////////////////////////////\r
104 // CAboutDlg dialog used for App About\r
105 \r
106 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)\r
107 {\r
108         //{{AFX_DATA_INIT(CAboutDlg)\r
109         m_szVersionInformation = _T("");\r
110         m_szLegalCopyright = _T("");\r
111         //}}AFX_DATA_INIT\r
112 \r
113         m_szLegalCopyright.Format(_T("Copyright (C) 2001-2006"));       // rough\r
114 \r
115         WORD wMajorVersion              = 0;\r
116         WORD wMinorVersion              = 0;\r
117         WORD wBuildVersion              = 0;\r
118         WORD wRevisionVersion   = 0;\r
119 \r
120         TCHAR szFileName[MAX_PATH] = {'\0'};\r
121         GetModuleFileName(NULL, szFileName, sizeof(szFileName));\r
122         DWORD dwHandle = 0;\r
123 \r
124         DWORD dwLen = GetFileVersionInfoSize(szFileName, &dwHandle);\r
125         if (dwLen) {\r
126                 LPVOID lpData = new BYTE[dwLen];\r
127                 if (GetFileVersionInfo(szFileName, dwHandle, dwLen, lpData)) {\r
128                         VS_FIXEDFILEINFO *pInfo;\r
129                         UINT uLen;\r
130                         if (VerQueryValue(lpData, _T("\\"), (LPVOID *)&pInfo, &uLen)) {\r
131                                 wMajorVersion           = (WORD)((pInfo->dwProductVersionMS >> 16) & 0xffff);\r
132                                 wMinorVersion           = (WORD)((pInfo->dwProductVersionMS      ) & 0xffff);\r
133                                 wBuildVersion           = (WORD)((pInfo->dwProductVersionLS >> 16) & 0xffff);\r
134                                 wRevisionVersion        = (WORD)((pInfo->dwProductVersionLS      ) & 0xffff);\r
135                         }\r
136 \r
137                         Translate_t *lpTranslate = NULL;\r
138                         UINT cbTranslate = 0;\r
139                         if (VerQueryValue(lpData, _T("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &cbTranslate) && sizeof(Translate_t) <= cbTranslate) {\r
140                                 LPVOID lpLegalCopyright = NULL;\r
141                                 UINT uLen = 0;\r
142                                 CString SubBlock;\r
143 \r
144                                 SubBlock.Format(_T("\\StringFileInfo\\%04x%04x\\LegalCopyright"), lpTranslate->wLanguage, lpTranslate->wCodePage);\r
145                                 if (VerQueryValue(lpData, SubBlock.GetBuffer(SubBlock.GetLength()), (LPVOID *)&lpLegalCopyright, &uLen) && uLen) {\r
146                                         m_szLegalCopyright.Format(_T("%s"), lpLegalCopyright);\r
147                                 }\r
148                         }\r
149                 }\r
150                 delete[] lpData;\r
151                 lpData = NULL;\r
152         }\r
153 \r
154         m_szVersionInformation.Format(_T("%s Version %d.%d"), CString(MAKEINTRESOURCE(AFX_IDS_APP_TITLE)), wMajorVersion, wMinorVersion);\r
155 \r
156         if (wBuildVersion) {\r
157                 CString sz;\r
158                 sz.Format(_T(".%d"), wBuildVersion);\r
159                 m_szVersionInformation += sz;\r
160 \r
161                 if (wRevisionVersion) {\r
162                         CString sz;\r
163                         sz.Format(_T(".%d"), wRevisionVersion);\r
164                         m_szVersionInformation += sz;\r
165                 }\r
166         }\r
167 }\r
168 \r
169 void CAboutDlg::DoDataExchange(CDataExchange *pDX)\r
170 {\r
171         CDialog::DoDataExchange(pDX);\r
172         //{{AFX_DATA_MAP(CAboutDlg)\r
173         DDX_Control(pDX, IDC_URL, m_cURL);\r
174         DDX_Text(pDX, IDC_VERSION_INFORMATION, m_szVersionInformation);\r
175         DDX_Text(pDX, IDC_LEGAL_COPYRIGHT, m_szLegalCopyright);\r
176         //}}AFX_DATA_MAP\r
177 }\r
178 \r
179 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)\r
180         //{{AFX_MSG_MAP(CAboutDlg)\r
181         ON_WM_CLOSE()\r
182         ON_BN_CLICKED(IDC_URL, OnUrl)\r
183         ON_WM_CTLCOLOR()\r
184         ON_WM_SETCURSOR()\r
185         //}}AFX_MSG_MAP\r
186 END_MESSAGE_MAP()\r
187 \r
188 // App command to run the dialog\r
189 void CXkeymacsApp::OnAppAbout()\r
190 {\r
191         CAboutDlg aboutDlg;\r
192         aboutDlg.DoModal();\r
193 }\r
194 \r
195 /////////////////////////////////////////////////////////////////////////////\r
196 // CXkeymacsApp message handlers\r
197 \r
198 \r
199 int CXkeymacsApp::ExitInstance() \r
200 {\r
201         // TODO: Add your specialized code here and/or call the base class\r
202 \r
203         if (m_Instance == FirstInstance) {\r
204             ReleaseMutex( m_hMutex );\r
205                 CloseHandle( m_hMutex );\r
206 \r
207                 m_pMainWnd->DestroyWindow();\r
208                 delete m_pMainWnd;\r
209                 m_pMainWnd = NULL;\r
210         }\r
211 \r
212         return CWinApp::ExitInstance();\r
213 }\r
214 \r
215 void CAboutDlg::OnOK() \r
216 {\r
217         // TODO: Add extra validation here\r
218         CDialog::OnOK();\r
219 }\r
220 \r
221 \r
222 void CAboutDlg::OnClose() \r
223 {\r
224         // TODO: Add your message handler code here and/or call default\r
225         CDialog::OnClose();\r
226 }\r
227 \r
228 void CAboutDlg::OnUrl() \r
229 {\r
230         // TODO: Add your message handler code here and/or call default\r
231         CString szURL;\r
232         m_cURL.GetWindowText(szURL);\r
233 \r
234         ShellExecute(m_hWnd, NULL, szURL, NULL, NULL, SW_SHOWNORMAL);\r
235 }\r
236 \r
237 HBRUSH CAboutDlg::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor) \r
238 {\r
239         HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);\r
240         \r
241         // TODO: Change any attributes of the DC here\r
242         COLORREF crBlue = RGB(0, 0, 0xff);\r
243 \r
244         if (pWnd == &m_cURL) {\r
245                 pDC->SetTextColor(crBlue);\r
246         }\r
247 \r
248         // TODO: Return a different brush if the default is not desired\r
249         return hbr;\r
250 }\r
251 \r
252 BOOL CAboutDlg::OnSetCursor(CWnd *pWnd, UINT nHitTest, UINT message) \r
253 {\r
254         // TODO: Add your message handler code here and/or call default\r
255         CRect rect;\r
256         m_cURL.GetWindowRect(rect);\r
257 \r
258         POINT p;\r
259         ::GetCursorPos(&p);\r
260 \r
261         if (rect.PtInRect(p)) {\r
262                 HCURSOR hCursor = (HCURSOR)LoadImage(theApp.m_hInstance, MAKEINTRESOURCE(IDC_HAND_CURSOR),\r
263                                                                                          IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);\r
264                 SetCursor(hCursor);\r
265                 return TRUE;    \r
266         }\r
267 \r
268         return CDialog::OnSetCursor(pWnd, nHitTest, message);\r
269 }\r