Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / samples / Notepad / src / Mainfrm.cpp
1 /////////////////////////////////////////////////\r
2 // Mainfrm.cpp\r
3 \r
4 #include "stdafx.h"\r
5 #include "mainfrm.h"\r
6 #include <richedit.h>\r
7 #include "resource.h"\r
8 \r
9 \r
10 //For Visual C++ 6 and without a modern SDK\r
11 #ifndef DWORD_PTR\r
12 #define DWORD_PTR DWORD\r
13 #endif\r
14 \r
15 \r
16 // definitions for the CMainFrame class\r
17 CMainFrame::CMainFrame()\r
18 {\r
19         m_strPathName = _T("");\r
20         SetView(m_RichView);\r
21 \r
22         // Set the registry key name, and load the initial window position\r
23         // Use a registry key name like "CompanyName\\Application"\r
24         LoadRegistrySettings(_T("Win32++\\Notepad Sample"));\r
25 \r
26         // Load the settings from the registry with 5 MRU entries\r
27         LoadRegistryMRUSettings(5);\r
28 }\r
29 \r
30 CMainFrame::~CMainFrame()\r
31 {\r
32 }\r
33 \r
34 void CMainFrame::OnInitialUpdate()\r
35 {\r
36         DragAcceptFiles(TRUE);\r
37         SetWindowTitle();\r
38         m_RichView.SetFocus();\r
39 }\r
40 \r
41 LRESULT CMainFrame::OnNotify(WPARAM wParam, LPARAM lParam)\r
42 {\r
43         NMHDR* pNMH;\r
44         pNMH = (LPNMHDR) lParam;\r
45         switch (pNMH->code)\r
46         {\r
47         case EN_DROPFILES:\r
48                 {\r
49                         ENDROPFILES* ENDrop = (ENDROPFILES*)lParam;\r
50                         HDROP hDropInfo = (HDROP) ENDrop->hDrop;\r
51                         OnDropFiles(hDropInfo);\r
52                 }\r
53                 return TRUE;\r
54         }\r
55 \r
56         return CFrame::OnNotify(wParam, lParam);\r
57 }\r
58 \r
59 BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)\r
60 {\r
61         UNREFERENCED_PARAMETER(lParam);\r
62 \r
63         switch (LOWORD(wParam))\r
64         {\r
65         case IDM_FILE_NEW:\r
66                 OnFileNew();\r
67                 return TRUE;\r
68         case IDM_FILE_OPEN:\r
69                 OnFileOpen();\r
70                 return TRUE;\r
71         case IDM_FILE_SAVE:\r
72                 OnFileSave();\r
73                 return TRUE;\r
74         case IDM_FILE_SAVEAS:\r
75                 OnFileSaveAs();\r
76                 return TRUE;\r
77         case IDM_FILE_PRINT:\r
78                 OnFilePrint();\r
79                 return TRUE;\r
80         case IDM_EDIT_COPY:\r
81                 OnEditCopy();\r
82                 return TRUE;\r
83         case IDM_EDIT_PASTE:\r
84                 OnEditPaste();\r
85                 return TRUE;\r
86         case IDM_EDIT_CUT:\r
87                 OnEditCut();\r
88                 return TRUE;\r
89         case IDM_EDIT_DELETE:\r
90                 OnEditDelete();\r
91                 return TRUE;\r
92         case IDM_EDIT_REDO:\r
93                 OnEditRedo();\r
94                 return TRUE;\r
95         case IDM_EDIT_UNDO:\r
96                 OnEditUndo();\r
97                 return TRUE;\r
98         case IDM_FILE_EXIT:\r
99                 ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);\r
100                 return TRUE;\r
101         case IDW_VIEW_STATUSBAR:\r
102                 OnViewStatusBar();\r
103                 return TRUE;\r
104         case IDW_VIEW_TOOLBAR:\r
105                 OnViewToolBar();\r
106                 return TRUE;\r
107         case IDM_HELP_ABOUT:\r
108                 OnHelp();\r
109                 return TRUE;\r
110                         case IDW_FILE_MRU_FILE1:\r
111         case IDW_FILE_MRU_FILE2:\r
112         case IDW_FILE_MRU_FILE3:\r
113         case IDW_FILE_MRU_FILE4:\r
114         case IDW_FILE_MRU_FILE5:\r
115                 {\r
116                         UINT nMRUIndex = LOWORD(wParam) - IDW_FILE_MRU_FILE1;\r
117                         CString strMRUText = GetMRUEntry(nMRUIndex);\r
118 \r
119                         if (ReadFile(strMRUText))\r
120                                 m_strPathName = strMRUText;\r
121                         else\r
122                                 RemoveMRUEntry(strMRUText);\r
123 \r
124                         SetWindowTitle();\r
125                         return TRUE;\r
126                 }\r
127 \r
128         } // switch cmd\r
129 \r
130         return FALSE;\r
131 } // CMainFrame::OnCommand(...)\r
132 \r
133 \r
134 void CMainFrame::OnFileNew()\r
135 {\r
136         m_RichView.SetWindowText(_T(""));\r
137         m_strPathName = _T("");\r
138         SetWindowTitle();\r
139         m_RichView.SetFontDefaults();\r
140         m_RichView.SendMessage(EM_SETMODIFY, FALSE, 0);\r
141 }\r
142 \r
143 void CMainFrame::OnFilePrint()\r
144 {\r
145         PRINTDLG pd;\r
146 \r
147         // Initialize PRINTDLG\r
148         ZeroMemory(&pd, sizeof(pd));\r
149         pd.lStructSize = sizeof(pd);\r
150         pd.hwndOwner   = m_hWnd;\r
151         pd.hDevMode    = NULL;     // Don't forget to free or store hDevMode\r
152         pd.hDevNames   = NULL;     // Don't forget to free or store hDevNames\r
153         pd.Flags       = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;\r
154         pd.nCopies     = 1;\r
155         pd.nFromPage   = 0xFFFF;\r
156         pd.nToPage     = 0xFFFF;\r
157         pd.nMinPage    = 1;\r
158         pd.nMaxPage    = 0xFFFF;\r
159         pd.hwndOwner = m_hWnd;\r
160 \r
161         if (PrintDlg(&pd)==TRUE)\r
162         {\r
163                 HDC hPrinterDC = pd.hDC;\r
164 \r
165                 // This code basically taken from MS KB article Q129860\r
166 \r
167                 FORMATRANGE fr;\r
168                 int     nHorizRes   = ::GetDeviceCaps(hPrinterDC, HORZRES);\r
169                 int     nVertRes    = ::GetDeviceCaps(hPrinterDC, VERTRES);\r
170                 int nLogPixelsX = ::GetDeviceCaps(hPrinterDC, LOGPIXELSX);\r
171                 int nLogPixelsY = ::GetDeviceCaps(hPrinterDC, LOGPIXELSY);\r
172                 LONG lTextLength;   // Length of document.\r
173                 LONG lTextPrinted;  // Amount of document printed.\r
174 \r
175                 // Ensure the printer DC is in MM_TEXT mode.\r
176                 ::SetMapMode ( hPrinterDC, MM_TEXT );\r
177 \r
178                 // Rendering to the same DC we are measuring.\r
179                 ZeroMemory(&fr, sizeof(fr));\r
180                 fr.hdc = hPrinterDC;\r
181                 fr.hdcTarget = hPrinterDC;\r
182 \r
183                 // Set up the page.\r
184                 int margin = 200; // 1440 TWIPS = 1 inch.\r
185                 fr.rcPage.left     = fr.rcPage.top = margin;\r
186                 fr.rcPage.right    = (nHorizRes/nLogPixelsX) * 1440 - margin;\r
187                 fr.rcPage.bottom   = (nVertRes/nLogPixelsY) * 1440 - margin;\r
188 \r
189                 // Set up margins all around.\r
190                 fr.rc.left   = fr.rcPage.left ;//+ 1440;\r
191                 fr.rc.top    = fr.rcPage.top ;//+ 1440;\r
192                 fr.rc.right  = fr.rcPage.right ;//- 1440;\r
193                 fr.rc.bottom = fr.rcPage.bottom ;//- 1440;\r
194 \r
195                 // Default the range of text to print as the entire document.\r
196                 fr.chrg.cpMin = 0;\r
197                 fr.chrg.cpMax = -1;\r
198                 m_RichView.SendMessage(EM_FORMATRANGE, true, (LPARAM)&fr);\r
199 \r
200                 // Set up the print job (standard printing stuff here).\r
201                 DOCINFO di;\r
202                 ZeroMemory(&di, sizeof(di));\r
203                 di.cbSize = sizeof(DOCINFO);\r
204                 di.lpszDocName = m_strPathName;\r
205 \r
206                 // Do not print to file.\r
207                 di.lpszOutput = NULL;\r
208 \r
209                 // Start the document.\r
210                 ::StartDoc(hPrinterDC, &di);\r
211                 GETTEXTLENGTHEX tl;\r
212                 tl.flags = GTL_NUMCHARS;\r
213 \r
214                 // Find out real size of document in characters.\r
215                 lTextLength = (LONG)m_RichView.SendMessage(EM_GETTEXTLENGTHEX, (WPARAM)&tl, 0L);\r
216 \r
217                 do\r
218                 {\r
219                         // Start the page.\r
220                         ::StartPage(hPrinterDC);\r
221 \r
222                         // Print as much text as can fit on a page. The return value is\r
223                         // the index of the first character on the next page. Using TRUE\r
224                         // for the wParam parameter causes the text to be printed.\r
225                         lTextPrinted = (LONG)::SendMessage(m_RichView.GetHwnd(), EM_FORMATRANGE, true, (LPARAM)&fr);\r
226 \r
227                         m_RichView.SendMessage(EM_DISPLAYBAND, 0, (LPARAM)&fr.rc);\r
228 \r
229                         // Print last page.\r
230                         ::EndPage(hPrinterDC);\r
231 \r
232                         // If there is more text to print, adjust the range of characters\r
233                         // to start printing at the first character of the next page.\r
234                         if (lTextPrinted < lTextLength)\r
235                         {\r
236                                 fr.chrg.cpMin = (LONG)lTextPrinted;\r
237                                 fr.chrg.cpMax = -1;\r
238                         }\r
239                 }\r
240                 while (lTextPrinted < lTextLength);\r
241 \r
242                 // Tell the control to release cached information.\r
243                 m_RichView.SendMessage(EM_FORMATRANGE, false, 0L);\r
244 \r
245                 ::EndDoc (hPrinterDC);\r
246 \r
247                 // Delete DC when done.\r
248                 ::DeleteDC(hPrinterDC);\r
249         }\r
250 }\r
251 \r
252 void CMainFrame::OnEditCut()\r
253 {\r
254         m_RichView.SendMessage(WM_CUT, 0, 0);\r
255 }\r
256 void CMainFrame::OnEditCopy()\r
257 {\r
258         m_RichView.SendMessage(WM_COPY, 0, 0);\r
259 }\r
260 void CMainFrame::OnEditPaste()\r
261 {\r
262         m_RichView.SendMessage(EM_PASTESPECIAL, CF_TEXT, 0);\r
263 }\r
264 void CMainFrame::OnEditDelete()\r
265 {\r
266         m_RichView.SendMessage(WM_CLEAR, 0, 0);\r
267 }\r
268 void CMainFrame::OnEditRedo()\r
269 {\r
270         m_RichView.SendMessage(EM_REDO, 0, 0);\r
271 }\r
272 void CMainFrame::OnEditUndo()\r
273 {\r
274         m_RichView.SendMessage(EM_UNDO, 0, 0);\r
275 }\r
276 \r
277 void CMainFrame::OnClose()\r
278 {\r
279         //Check for unsaved text\r
280         BOOL bChanged = (BOOL)m_RichView.SendMessage(EM_GETMODIFY, 0, 0);\r
281         if (bChanged)\r
282                 if (::MessageBox(NULL, _T("Save changes to this document"), _T("TextEdit"), MB_YESNO | MB_ICONWARNING) == IDYES)\r
283                         OnFileSave();\r
284 \r
285         CFrame::OnClose();\r
286 }\r
287 \r
288 void CMainFrame::OnDropFiles(HDROP hDropInfo)\r
289 {\r
290         TCHAR szFileName[_MAX_PATH];\r
291         ::DragQueryFile((HDROP)hDropInfo, 0, (LPTSTR)szFileName, _MAX_PATH);\r
292 \r
293         ReadFile(szFileName);\r
294 }\r
295 \r
296 BOOL CMainFrame::ReadFile(LPCTSTR szFileName)\r
297 {\r
298         // Open the file for reading\r
299         CFile File;\r
300         if (!File.Open(szFileName, OPEN_EXISTING))\r
301         {\r
302                 CString str = _T("Failed to load:  ");\r
303                 str += szFileName;\r
304                 ::MessageBox(NULL, str, _T("Warning"), MB_ICONWARNING);\r
305                 return FALSE;\r
306         }\r
307 \r
308         //Set default font and color\r
309         m_RichView.SetFontDefaults();\r
310 \r
311         EDITSTREAM es;\r
312         es.dwCookie =  (DWORD_PTR) File.GetHandle();\r
313         es.pfnCallback = (EDITSTREAMCALLBACK) MyStreamInCallback;\r
314         m_RichView.SendMessage(EM_STREAMIN, SF_TEXT, (LPARAM)&es);\r
315 \r
316         //Clear the modified text flag\r
317         m_RichView.SendMessage(EM_SETMODIFY, FALSE, 0);\r
318 \r
319         return TRUE;\r
320 }\r
321 \r
322 BOOL CMainFrame::WriteFile(LPCTSTR szFileName)\r
323 {\r
324         // Open the file for writing\r
325         CFile File;\r
326         if (!File.Open(szFileName, CREATE_ALWAYS))\r
327         {\r
328                 CString str = _T("Failed to write:  ");\r
329                 str += szFileName;\r
330                 ::MessageBox(NULL, str, _T("Warning"), MB_ICONWARNING);\r
331                 return FALSE;\r
332         }\r
333 \r
334         EDITSTREAM es;\r
335 \r
336         es.dwCookie =  (DWORD_PTR) File.GetHandle();\r
337         es.dwError = 0;\r
338         es.pfnCallback = (EDITSTREAMCALLBACK) MyStreamOutCallback;\r
339 \r
340         m_RichView.SendMessage(EM_STREAMOUT, SF_TEXT, (LPARAM)&es);\r
341 \r
342         //Clear the modified text flag\r
343         m_RichView.SendMessage(EM_SETMODIFY, FALSE, 0);\r
344 \r
345         return TRUE;\r
346 }\r
347 \r
348 void CMainFrame::OnFileOpen()\r
349 {\r
350         // szFilters is a text string that includes two file name filters:\r
351         // "*.my" for "MyType Files" and "*.*' for "All Files."\r
352         CString Filters( _T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0"), 46);\r
353         CFile File;\r
354         CString str = File.OpenFileDialog(0, OFN_FILEMUSTEXIST, Filters, this);\r
355 \r
356         if (!str.IsEmpty())\r
357         {\r
358                 ReadFile(str);\r
359                 SetFileName(str);\r
360                 AddMRUEntry(str);\r
361                 SetWindowTitle();\r
362         }\r
363 }\r
364 \r
365 void CMainFrame::OnFileSave()\r
366 {\r
367         if (m_strPathName == _T(""))\r
368                 OnFileSaveAs();\r
369         else\r
370                 WriteFile(m_strPathName);\r
371 }\r
372 \r
373 void CMainFrame::OnFileSaveAs()\r
374 {\r
375         // szFilters is a text string that includes two file name filters:\r
376         // "*.my" for "MyType Files" and "*.*' for "All Files."\r
377         CString Filters(_T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0\0"), 46);\r
378         CFile File;\r
379         CString str = File.SaveFileDialog(0, OFN_OVERWRITEPROMPT, Filters, _T("txt"), this);\r
380         \r
381         if (!str.IsEmpty())\r
382         {\r
383                 WriteFile(str);\r
384                 SetFileName(str);\r
385                 AddMRUEntry(str);\r
386                 SetWindowTitle();\r
387         }\r
388 }\r
389 \r
390 void CMainFrame::SetFileName(LPCTSTR szFilePathName)\r
391 {\r
392         //Truncate and save file name\r
393         int i = lstrlen(szFilePathName)+1;\r
394         while ((--i > 0) && (szFilePathName[i-1] != _T('\\')));\r
395 \r
396         m_strPathName = szFilePathName+i;\r
397 }\r
398 \r
399 void CMainFrame::SetWindowTitle()\r
400 {\r
401     CString Title;\r
402 \r
403         if (m_strPathName == _T("")) Title = _T("TextEdit - Untitled");\r
404 \r
405         else Title = _T("TextEdit - ") + m_strPathName;\r
406         SetWindowText(Title);\r
407 }\r
408 \r
409 void CMainFrame::SetupToolBar()\r
410 {\r
411         // Define the resource IDs for the toolbar\r
412         AddToolBarButton( IDM_FILE_NEW   );\r
413         AddToolBarButton( IDM_FILE_OPEN  );\r
414         AddToolBarButton( IDM_FILE_SAVE  );\r
415         AddToolBarButton( 0 );                          // Separator\r
416         AddToolBarButton( IDM_EDIT_CUT   );\r
417         AddToolBarButton( IDM_EDIT_COPY  );\r
418         AddToolBarButton( IDM_EDIT_PASTE );\r
419         AddToolBarButton( 0 );                          // Separator\r
420         AddToolBarButton( IDM_FILE_PRINT );\r
421         AddToolBarButton( 0 );                          // Separator\r
422         AddToolBarButton( IDM_HELP_ABOUT );\r
423 \r
424 }\r
425 \r
426 LRESULT CMainFrame::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
427 {\r
428 //      switch (uMsg)\r
429 //      {\r
430 //\r
431 //      }\r
432 \r
433         return WndProcDefault(uMsg, wParam, lParam);\r
434 }\r
435 \r
436 DWORD CALLBACK CMainFrame::MyStreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)\r
437 {\r
438         // Required for StreamIn\r
439         if (!cb)\r
440                 return (1);\r
441 \r
442         *pcb = 0;\r
443         if (!::ReadFile((HANDLE)(DWORD_PTR) dwCookie, pbBuff, cb, (LPDWORD)pcb, NULL))\r
444                 ::MessageBox(NULL, _T("ReadFile Failed"), _T(""), MB_OK);\r
445 \r
446         return 0;\r
447 }\r
448 \r
449 DWORD CALLBACK CMainFrame::MyStreamOutCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)\r
450 {\r
451         // Required for StreamOut\r
452         if(!cb)\r
453                 return (1);\r
454 \r
455         *pcb = 0;\r
456         if (!::WriteFile((HANDLE)(DWORD_PTR)dwCookie, pbBuff, cb, (LPDWORD)pcb, NULL))\r
457                 ::MessageBox(NULL, _T("WriteFile Failed"), _T(""), MB_OK);\r
458         return 0;\r
459 }\r
460 \r

UCC git Repository :: git.ucc.asn.au