1 ////////////////////////////////////////////////////
\r
6 #include <UIRibbonPropertyHelpers.h>
\r
7 #include "RibbonUI.h"
\r
8 #include "resource.h"
\r
12 // Definitions for the CMainFrame class
\r
13 CMainFrame::CMainFrame()
\r
15 // Constructor for CMainFrame. Its called after CFrame's constructor
\r
17 //Set m_View as the view window of the frame
\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
24 // Load the settings from the registry with 4 MRU entries
\r
25 LoadRegistryMRUSettings(4);
\r
28 CMainFrame::~CMainFrame()
\r
30 // Destructor for CMainFrame.
\r
34 STDMETHODIMP CMainFrame::Execute(UINT32 nCmdID, UI_EXECUTIONVERB verb, const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue, IUISimplePropertySet* pCommandExecutionProperties)
\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
39 if (UI_EXECUTIONVERB_EXECUTE == verb)
\r
52 case IDC_CMD_SAVE_AS:
\r
59 TRACE(_T("Copy\n"));
\r
65 TRACE(_T("Paste\n"));
\r
74 if (ppropvarValue != NULL && key != NULL && UI_PKEY_SelectedItem == *key)
\r
76 UINT uSelectedMRUItem = ppropvarValue->ulVal;
\r
77 MRUFileOpen(uSelectedMRUItem);
\r
80 case IDC_PEN_COLOR: // DropdownColorPicker button pressed
\r
82 if (ppropvarValue != NULL)
\r
84 // Retrieve color type.
\r
85 UINT type = ppropvarValue->uintVal;
\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
92 if (0 <= pCommandExecutionProperties->GetValue(UI_PKEY_Color, &var))
\r
94 UINT color = var.uintVal;
\r
95 m_View.SetPen((COLORREF)color);
\r
102 TRACE(_T("Font dialog button\n"));
\r
104 case IDC_RIBBONHELP:
\r
107 case IDC_CUSTOMIZE_QAT:
\r
108 TRACE(_T("Customize Quick Access ToolBar\n"));
\r
113 str.Format(_T("Unknown Button %d\n"),nCmdID);
\r
123 void CMainFrame::MRUFileOpen(UINT nMRUIndex)
\r
125 CString strMRUText = GetMRUEntry(nMRUIndex);
\r
127 if (m_View.FileOpen(strMRUText))
\r
128 m_PathName = strMRUText;
\r
130 RemoveMRUEntry(strMRUText);
\r
133 BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
\r
135 // Process the messages from the (non-ribbon) Menu and Tool Bar
\r
137 UNREFERENCED_PARAMETER(lParam);
\r
139 switch (LOWORD(wParam))
\r
144 case IDM_FILE_OPEN:
\r
147 case IDM_FILE_SAVE:
\r
150 case IDM_FILE_SAVEAS:
\r
153 case IDM_FILE_PRINT:
\r
157 TRACE(_T("Red pen selected\n"));
\r
158 m_View.SetPen(RGB(255,0,0));
\r
161 TRACE(_T("Blue pen selected\n"));
\r
162 m_View.SetPen(RGB(0,0,255));
\r
164 case IDM_PEN_GREEN:
\r
165 TRACE(_T("Green pen selected\n"));
\r
166 m_View.SetPen(RGB(0,196,0));
\r
168 case IDM_PEN_BLACK:
\r
169 TRACE(_T("Black pen selected\n"));
\r
170 m_View.SetPen(RGB(0,0,0));
\r
172 case IDW_VIEW_STATUSBAR:
\r
175 case IDW_VIEW_TOOLBAR:
\r
178 case IDM_HELP_ABOUT:
\r
181 case IDM_FILE_EXIT:
\r
182 ::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
\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
190 UINT uMRUEntry = LOWORD(wParam) - IDW_FILE_MRU_FILE1;
\r
191 MRUFileOpen(uMRUEntry);
\r
199 void CMainFrame::OnFileOpen()
\r
202 CString str = File.OpenFileDialog(0, OFN_FILEMUSTEXIST, _T("Scribble Files (*.dat)\0*.dat\0\0"), this);
\r
204 if (!str.IsEmpty())
\r
206 // Retrieve the PlotPoint data
\r
207 if (m_View.FileOpen(str))
\r
209 // Save the filename
\r
218 void CMainFrame::OnFileNew()
\r
220 m_View.ClearPoints();
\r
221 m_PathName = _T("");
\r
224 void CMainFrame::OnFileSave()
\r
226 if (m_PathName == _T(""))
\r
229 m_View.FileSave(m_PathName);
\r
232 void CMainFrame::OnFileSaveAs()
\r
235 CString str = File.SaveFileDialog(0, OFN_OVERWRITEPROMPT, _T("Scribble Files (*.dat)\0*.dat\0\0"), _T("dat"), this);
\r
237 // Store the PlotPoint data in the file
\r
238 if (!str.IsEmpty())
\r
242 // Save the file name
\r
243 m_View.FileSave(str);
\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
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
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
264 // Bring up a dialog to choose the printer
\r
266 pd.lStructSize = sizeof( pd );
\r
267 pd.Flags = PD_RETURNDC;
\r
268 pd.hwndOwner = m_hWnd;
\r
270 // Retrieve the printer DC
\r
271 if( !PrintDlg( &pd ) )
\r
273 TRACE(_T("PrintDlg canceled"));
\r
277 // Zero and then initialize the members of a DOCINFO structure.
\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
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
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
294 BITMAPINFOHEADER bi = {0};
\r
295 bi.biSize = sizeof(BITMAPINFOHEADER);
\r
296 bi.biHeight = Height;
\r
297 bi.biWidth = Width;
\r
299 bi.biBitCount = 24;
\r
300 bi.biCompression = BI_RGB;
\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
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
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
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
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
329 throw CWinException(_T("Failed to resize image for printing"));
\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
336 // Inform the driver that document has ended.
\r
337 if(0 > EndDoc(pd.hDC))
\r
338 throw CWinException(_T("EndDoc failed"));
\r
341 void CMainFrame::OnInitialUpdate()
\r
343 // The frame is now created.
\r
344 // Place any additional startup code here.
\r
346 TRACE(_T("Frame created\n"));
\r
349 STDMETHODIMP CMainFrame::UpdateProperty(UINT32 nCmdID, __in REFPROPERTYKEY key, __in_opt const PROPVARIANT *currentValue, __out PROPVARIANT *newValue)
\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
355 HRESULT hr = E_NOTIMPL;
\r
356 if(UI_PKEY_Enabled == key)
\r
358 return UIInitPropertyFromBoolean(UI_PKEY_Enabled, TRUE, newValue);
\r
364 // Set up the Most Recently Used (MRU) menu
\r
365 if (UI_PKEY_Label == key)
\r
367 WCHAR label[MAX_PATH] = L"Recent Files";
\r
368 hr = UIInitPropertyFromString(UI_PKEY_Label, label, newValue);
\r
370 else if (UI_PKEY_RecentItems == key)
\r
372 hr = PopulateRibbonRecentItems(newValue);
\r
376 case IDC_PEN_COLOR:
\r
377 // Set the initial pen color
\r
378 hr = UIInitPropertyFromUInt32(key, RGB(1, 1, 1), newValue);
\r
385 void CMainFrame::SetupToolBar()
\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
405 LRESULT CMainFrame::WndProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
\r
409 // Add case statements for each messages to be handled here
\r
412 // pass unhandled messages on for default processing
\r
413 return WndProcDefault(uMsg, wParam, lParam);
\r