Merge branch 'master' of git.ucc.asn.au:/matches/honours
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / samples / RibbonFrame / src / Mainfrm.cpp
1 ////////////////////////////////////////////////////\r
2 // Mainfrm.cpp\r
3 \r
4 #include "stdafx.h"\r
5 \r
6 #include <UIRibbonPropertyHelpers.h>\r
7 #include "RibbonUI.h"\r
8 #include "resource.h"\r
9 #include "mainfrm.h"\r
10 \r
11 \r
12 // Definitions for the CMainFrame class\r
13 CMainFrame::CMainFrame()\r
14 {\r
15         // Constructor for CMainFrame. Its called after CFrame's constructor\r
16 \r
17         //Set m_View as the view window of the frame\r
18         SetView(m_View);\r
19 \r
20         // Set the registry key name, and load the initial window position\r
21         // Use a registry key name like "CompanyName\\Application"\r
22         LoadRegistrySettings(_T("Win32++\\Frame"));\r
23 \r
24         // Load the settings from the registry with 4 MRU entries\r
25         LoadRegistryMRUSettings(4);\r
26 }\r
27 \r
28 CMainFrame::~CMainFrame()\r
29 {\r
30         // Destructor for CMainFrame.\r
31         DestroyRibbon();\r
32 }\r
33 \r
34 STDMETHODIMP CMainFrame::Execute(UINT32 nCmdID, UI_EXECUTIONVERB verb, const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue, IUISimplePropertySet* pCommandExecutionProperties)\r
35 {\r
36         // This function is called when a ribbon button is pressed. \r
37         // Refer to IUICommandHandler::Execute in the Windows 7 SDK documentation \r
38 \r
39         if (UI_EXECUTIONVERB_EXECUTE == verb)\r
40         {\r
41                 switch(nCmdID)\r
42                 {\r
43                 case IDC_CMD_NEW:\r
44                         OnFileNew();\r
45                         break;\r
46                 case IDC_CMD_OPEN:\r
47                         OnFileOpen();\r
48                         break;\r
49                 case IDC_CMD_SAVE:\r
50                         OnFileSave();\r
51                         break;\r
52                 case IDC_CMD_SAVE_AS:\r
53                         OnFileSaveAs();\r
54                         break;\r
55                 case IDC_CMD_PRINT:\r
56                         OnFilePrint();\r
57                          break;\r
58                 case IDC_CMD_COPY:\r
59                         TRACE(_T("Copy\n"));\r
60                         break;\r
61                 case IDC_CMD_CUT:\r
62                         TRACE(_T("Cut\n"));\r
63                         break;\r
64                 case IDC_CMD_PASTE:\r
65                         TRACE(_T("Paste\n"));\r
66                         break;\r
67                 case IDC_CMD_ABOUT:\r
68                         OnHelp();\r
69                         break;\r
70                 case IDC_CMD_EXIT:\r
71                         PostQuitMessage(0);\r
72                         break;\r
73                 case IDC_MRULIST:\r
74                         if (ppropvarValue != NULL && key != NULL && UI_PKEY_SelectedItem == *key)\r
75                         {       \r
76                                 UINT uSelectedMRUItem = ppropvarValue->ulVal;\r
77                                 MRUFileOpen(uSelectedMRUItem);\r
78                         }\r
79                         break;\r
80                 case IDC_PEN_COLOR:  // DropdownColorPicker button pressed\r
81                         {                       \r
82                                 if (ppropvarValue != NULL)\r
83                                 {\r
84                                         // Retrieve color type. \r
85                                         UINT type = ppropvarValue->uintVal;     \r
86                                 \r
87                                         // The Ribbon framework passes color as additional property if the color type is RGB.\r
88                                         if (type == UI_SWATCHCOLORTYPE_RGB && pCommandExecutionProperties != NULL)\r
89                                         {\r
90                                                 // Retrieve color.\r
91                                                 PROPVARIANT var;\r
92                                                 if (0 <= pCommandExecutionProperties->GetValue(UI_PKEY_Color, &var))\r
93                                                 {       \r
94                                                         UINT color = var.uintVal;\r
95                                                         m_View.SetPen((COLORREF)color);\r
96                                                 }\r
97                                         }\r
98                                 }\r
99                         }\r
100                         break;\r
101                 case IDC_RICHFONT:\r
102                         TRACE(_T("Font dialog button\n"));\r
103                         break;\r
104                 case IDC_RIBBONHELP:\r
105                         OnHelp();\r
106                         break;\r
107                 case IDC_CUSTOMIZE_QAT:\r
108                         TRACE(_T("Customize Quick Access ToolBar\n"));\r
109                         break;\r
110                 default:\r
111                         {\r
112                                 CString str;\r
113                                 str.Format(_T("Unknown Button %d\n"),nCmdID);\r
114                                 TRACE(str);\r
115                         }\r
116                         break;\r
117                 }\r
118         }\r
119 \r
120         return S_OK;\r
121 }\r
122 \r
123 void CMainFrame::MRUFileOpen(UINT nMRUIndex)\r
124 {\r
125         CString strMRUText = GetMRUEntry(nMRUIndex);\r
126 \r
127         if (m_View.FileOpen(strMRUText))\r
128                 m_PathName = strMRUText;\r
129         else\r
130                 RemoveMRUEntry(strMRUText);\r
131 }\r
132 \r
133 BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)\r
134 {\r
135         // Process the messages from the (non-ribbon) Menu and Tool Bar\r
136 \r
137         UNREFERENCED_PARAMETER(lParam);\r
138 \r
139         switch (LOWORD(wParam))\r
140         {\r
141         case IDM_FILE_NEW:\r
142                 OnFileNew();\r
143                 return TRUE;\r
144         case IDM_FILE_OPEN:\r
145                 OnFileOpen();\r
146                 return TRUE;\r
147         case IDM_FILE_SAVE:\r
148                 OnFileSave();\r
149                 return TRUE;\r
150         case IDM_FILE_SAVEAS:\r
151                 OnFileSaveAs();\r
152                 return TRUE;\r
153         case IDM_FILE_PRINT:\r
154                 OnFilePrint();\r
155                 return TRUE;\r
156         case IDM_PEN_RED:\r
157                 TRACE(_T("Red pen selected\n"));\r
158                 m_View.SetPen(RGB(255,0,0));\r
159                 return TRUE;\r
160         case IDM_PEN_BLUE:\r
161                 TRACE(_T("Blue pen selected\n"));\r
162                 m_View.SetPen(RGB(0,0,255));\r
163                 return TRUE;\r
164         case IDM_PEN_GREEN:\r
165                 TRACE(_T("Green pen selected\n"));\r
166                 m_View.SetPen(RGB(0,196,0));\r
167                 return TRUE;\r
168         case IDM_PEN_BLACK:\r
169                 TRACE(_T("Black pen selected\n"));\r
170                 m_View.SetPen(RGB(0,0,0));\r
171                 return TRUE;\r
172         case IDW_VIEW_STATUSBAR:\r
173                 OnViewStatusBar();\r
174                 return TRUE;\r
175         case IDW_VIEW_TOOLBAR:\r
176                 OnViewToolBar();\r
177                 return TRUE;\r
178         case IDM_HELP_ABOUT:\r
179                 OnHelp();\r
180                 return TRUE;\r
181         case IDM_FILE_EXIT:\r
182                 ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);\r
183                 return TRUE;\r
184         case IDW_FILE_MRU_FILE1:\r
185         case IDW_FILE_MRU_FILE2:\r
186         case IDW_FILE_MRU_FILE3:\r
187         case IDW_FILE_MRU_FILE4:\r
188         case IDW_FILE_MRU_FILE5:\r
189                 {\r
190                         UINT uMRUEntry = LOWORD(wParam) - IDW_FILE_MRU_FILE1;\r
191                         MRUFileOpen(uMRUEntry);\r
192                 }\r
193                 return TRUE;\r
194         }\r
195 \r
196         return FALSE;\r
197 }\r
198 \r
199 void CMainFrame::OnFileOpen()\r
200 {\r
201         CFile File;\r
202         CString str = File.OpenFileDialog(0, OFN_FILEMUSTEXIST, _T("Scribble Files (*.dat)\0*.dat\0\0"), this);\r
203 \r
204         if (!str.IsEmpty())\r
205         {\r
206                 // Retrieve the PlotPoint data\r
207                 if (m_View.FileOpen(str))\r
208                 {\r
209                         // Save the filename\r
210                         m_PathName = str;\r
211                         AddMRUEntry(str);\r
212                 }\r
213                 else\r
214                         m_PathName=_T("");\r
215         }\r
216 }\r
217 \r
218 void CMainFrame::OnFileNew()\r
219 {\r
220         m_View.ClearPoints();\r
221         m_PathName = _T("");\r
222 }\r
223 \r
224 void CMainFrame::OnFileSave()\r
225 {\r
226         if (m_PathName == _T(""))\r
227                 OnFileSaveAs();\r
228         else\r
229                 m_View.FileSave(m_PathName);\r
230 }\r
231 \r
232 void CMainFrame::OnFileSaveAs()\r
233 {\r
234         CFile File;\r
235                 CString str = File.SaveFileDialog(0, OFN_OVERWRITEPROMPT, _T("Scribble Files (*.dat)\0*.dat\0\0"), _T("dat"), this);\r
236 \r
237         // Store the PlotPoint data in the file\r
238         if (!str.IsEmpty())\r
239         {\r
240                 m_PathName = str;\r
241 \r
242                 // Save the file name\r
243                 m_View.FileSave(str);\r
244                 AddMRUEntry(str);\r
245         }\r
246 }\r
247 \r
248 // Sends the bitmap extracted from the View window to a printer of your choice\r
249 // This function provides a useful reference for printing bitmaps in general\r
250 void CMainFrame::OnFilePrint()\r
251 {\r
252         // Get the dimensions of the View window\r
253         CRect rcView = m_View.GetClientRect();\r
254         int Width = rcView.Width();\r
255         int Height = rcView.Height();\r
256 \r
257         // Copy the bitmap from the View window\r
258         CClientDC ViewDC(&m_View);\r
259         CMemDC MemDC(&ViewDC);\r
260         CBitmap bmView(::CreateCompatibleBitmap(ViewDC, Width, Height));\r
261         MemDC.SelectObject(&bmView);\r
262         BitBlt(MemDC, 0, 0, Width, Height, ViewDC, 0, 0, SRCCOPY);\r
263 \r
264         // Bring up a dialog to choose the printer\r
265         PRINTDLG pd = {0};\r
266         pd.lStructSize = sizeof( pd );\r
267         pd.Flags = PD_RETURNDC;\r
268         pd.hwndOwner = m_hWnd;\r
269 \r
270         // Retrieve the printer DC\r
271         if( !PrintDlg( &pd ) )\r
272         {\r
273                 TRACE(_T("PrintDlg canceled"));\r
274                 return;\r
275         }\r
276 \r
277         // Zero and then initialize the members of a DOCINFO structure.\r
278         DOCINFO di;\r
279         memset( &di, 0, sizeof(DOCINFO) );\r
280         di.cbSize = sizeof(DOCINFO);\r
281         di.lpszDocName = _T("Scribble Printout");\r
282         di.lpszOutput = (LPTSTR) NULL;\r
283         di.lpszDatatype = (LPTSTR) NULL;\r
284         di.fwType = 0;\r
285 \r
286         // Begin a print job by calling the StartDoc function.\r
287         if (SP_ERROR == StartDoc(pd.hDC, &di))\r
288                 throw CWinException(_T("Failed to start print job"));\r
289 \r
290         // Inform the driver that the application is about to begin sending data.\r
291         if (0 > StartPage(pd.hDC))\r
292                 throw CWinException(_T("StartPage failed"));\r
293 \r
294         BITMAPINFOHEADER bi = {0};\r
295         bi.biSize = sizeof(BITMAPINFOHEADER);\r
296         bi.biHeight = Height;\r
297         bi.biWidth = Width;\r
298         bi.biPlanes = 1;\r
299         bi.biBitCount =  24;\r
300         bi.biCompression = BI_RGB;\r
301 \r
302         // Note: BITMAPINFO and BITMAPINFOHEADER are the same for 24 bit bitmaps\r
303         // Get the size of the image data\r
304         MemDC.GetDIBits(&bmView, 0, Height, NULL, (BITMAPINFO*)&bi, DIB_RGB_COLORS);\r
305 \r
306         // Retrieve the image data\r
307         std::vector<byte> vBits(bi.biSizeImage, 0);     // a vector to hold the byte array\r
308         byte* pByteArray = &vBits.front();\r
309         MemDC.GetDIBits(&bmView, 0, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS);\r
310 \r
311         // Determine the scaling factors required to print the bitmap and retain its original proportions.\r
312         float fLogPelsX1 = (float) ViewDC.GetDeviceCaps(LOGPIXELSX);\r
313         float fLogPelsY1 = (float) ViewDC.GetDeviceCaps(LOGPIXELSY);\r
314         float fLogPelsX2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSX);\r
315         float fLogPelsY2 = (float) GetDeviceCaps(pd.hDC, LOGPIXELSY);\r
316         float fScaleX = MAX(fLogPelsX1, fLogPelsX2) / MIN(fLogPelsX1, fLogPelsX2);\r
317         float fScaleY = MAX(fLogPelsY1, fLogPelsY2) / MIN(fLogPelsY1, fLogPelsY2);\r
318 \r
319     // Compute the coordinates of the upper left corner of the centered bitmap.\r
320         int cWidthPels = GetDeviceCaps(pd.hDC, HORZRES);\r
321         int xLeft = ((cWidthPels / 2) - ((int) (((float) Width) * fScaleX)) / 2);\r
322         int cHeightPels = GetDeviceCaps(pd.hDC, VERTRES);\r
323         int yTop = ((cHeightPels / 2) - ((int) (((float) Height) * fScaleY)) / 2);\r
324 \r
325     // Use StretchDIBits to scale the bitmap and maintain its original proportions\r
326         if (GDI_ERROR == (UINT)StretchDIBits(pd.hDC, xLeft, yTop, (int) ((float) Width * fScaleX),\r
327                 (int) ((float) Height * fScaleY), 0, 0, Width, Height, pByteArray, (BITMAPINFO*)&bi, DIB_RGB_COLORS, SRCCOPY))\r
328         {\r
329                 throw CWinException(_T("Failed to resize image for printing"));\r
330         }\r
331 \r
332         // Inform the driver that the page is finished.\r
333         if (0 > EndPage(pd.hDC))\r
334                 throw CWinException(_T("EndPage failed"));\r
335 \r
336         // Inform the driver that document has ended.\r
337         if(0 > EndDoc(pd.hDC))\r
338                 throw CWinException(_T("EndDoc failed"));\r
339 }\r
340 \r
341 void CMainFrame::OnInitialUpdate()\r
342 {\r
343         // The frame is now created.\r
344         // Place any additional startup code here.\r
345 \r
346         TRACE(_T("Frame created\n"));\r
347 }\r
348 \r
349 STDMETHODIMP CMainFrame::UpdateProperty(UINT32 nCmdID, __in REFPROPERTYKEY key,  __in_opt  const PROPVARIANT *currentValue, __out PROPVARIANT *newValue) \r
350 {   \r
351         // This function is called when a ribbon button is updated. \r
352         // Refer to IUICommandHandler::UpdateProperty in the Windows 7 SDK documentation\r
353         UNREFERENCED_PARAMETER(currentValue);\r
354 \r
355     HRESULT hr = E_NOTIMPL;\r
356     if(UI_PKEY_Enabled == key)\r
357     {\r
358         return UIInitPropertyFromBoolean(UI_PKEY_Enabled, TRUE, newValue);\r
359     }\r
360 \r
361         switch(nCmdID)\r
362     {\r
363     case IDC_MRULIST:\r
364         // Set up the Most Recently Used (MRU) menu\r
365                 if (UI_PKEY_Label == key)\r
366         {\r
367             WCHAR label[MAX_PATH] = L"Recent Files";\r
368             hr = UIInitPropertyFromString(UI_PKEY_Label, label, newValue);\r
369         }\r
370         else if (UI_PKEY_RecentItems == key)\r
371         {\r
372             hr = PopulateRibbonRecentItems(newValue);\r
373         }\r
374         break;\r
375 \r
376         case IDC_PEN_COLOR:\r
377                 // Set the initial pen color\r
378                 hr = UIInitPropertyFromUInt32(key, RGB(1, 1, 1), newValue);\r
379                 break;\r
380         } \r
381 \r
382         return hr;\r
383 }\r
384 \r
385 void CMainFrame::SetupToolBar()\r
386 {\r
387         // Define our toolbar (used when the ribbon is not displayed)\r
388         AddToolBarButton( IDM_FILE_NEW   );\r
389         AddToolBarButton( IDM_FILE_OPEN  );\r
390         AddToolBarButton( IDM_FILE_SAVE  );\r
391         AddToolBarButton( 0 );                          // Separator\r
392         AddToolBarButton( IDM_EDIT_CUT,   FALSE );\r
393         AddToolBarButton( IDM_EDIT_COPY,  FALSE );\r
394         AddToolBarButton( IDM_EDIT_PASTE, FALSE );\r
395         AddToolBarButton( IDM_FILE_PRINT );\r
396         AddToolBarButton( 0 );                          // Separator\r
397         AddToolBarButton( IDM_PEN_RED    );\r
398         AddToolBarButton( IDM_PEN_BLUE   );\r
399         AddToolBarButton( IDM_PEN_GREEN  );\r
400         AddToolBarButton( IDM_PEN_BLACK  );\r
401         AddToolBarButton( 0 );                          // Separator\r
402         AddToolBarButton( IDM_HELP_ABOUT );\r
403 }\r
404 \r
405 LRESULT CMainFrame::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)\r
406 {\r
407 //      switch (uMsg)\r
408 //      {\r
409 //              Add case statements for each messages to be handled here\r
410 //      }\r
411 \r
412         // pass unhandled messages on for default processing\r
413         return WndProcDefault(uMsg, wParam, lParam);\r
414 }\r
415 \r

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