Commit before breaking everything
[matches/honours.git] / research / transmission_spectroscopy / TOF / Win32++ / include / ribbon.h
1 // Win32++   Version 7.3\r
2 // Released: 30th November 2011\r
3 //\r
4 //      David Nash\r
5 //      email: [email protected]\r
6 //      url: https://sourceforge.net/projects/win32-framework\r
7 //\r
8 //\r
9 // Copyright (c) 2005-2011  David Nash\r
10 //\r
11 // Permission is hereby granted, free of charge, to\r
12 // any person obtaining a copy of this software and\r
13 // associated documentation files (the "Software"),\r
14 // to deal in the Software without restriction, including\r
15 // without limitation the rights to use, copy, modify,\r
16 // merge, publish, distribute, sublicense, and/or sell\r
17 // copies of the Software, and to permit persons to whom\r
18 // the Software is furnished to do so, subject to the\r
19 // following conditions:\r
20 //\r
21 // The above copyright notice and this permission notice\r
22 // shall be included in all copies or substantial portions\r
23 // of the Software.\r
24 //\r
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF\r
26 // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED\r
27 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A\r
28 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT\r
29 // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR\r
30 // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
31 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
32 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE\r
33 // OR OTHER DEALINGS IN THE SOFTWARE.\r
34 //\r
35 ////////////////////////////////////////////////////////\r
36 \r
37 \r
38 ///////////////////////////////////////////////////////\r
39 // ribbon.h\r
40 //  Declaration of the following classes:\r
41 //  CRibbon and CRibbonFrame\r
42 //\r
43 \r
44 #ifndef _WIN32XX_RIBBON_H_\r
45 #define _WIN32XX_RIBBON_H_\r
46 \r
47 \r
48 // Notes: 1) The Windows 7 SDK must be installed and its directories added to the IDE\r
49 //        2) The ribbon only works on OS Windows 7 and above\r
50 \r
51 //#include <strsafe.h>\r
52 #include <UIRibbon.h>                                   // Contained within the Windows 7 SDK   \r
53 #include <UIRibbonPropertyHelpers.h>\r
54 \r
55 namespace Win32xx\r
56 {\r
57         // Defines the callback entry-point methods for the Ribbon framework.\r
58         class CRibbon : public IUICommandHandler, public IUIApplication\r
59         {\r
60         public:\r
61                 CRibbon() : m_cRef(1), m_pRibbonFramework(NULL) {}\r
62                 ~CRibbon(); \r
63 \r
64                 // IUnknown methods.\r
65                 STDMETHOD_(ULONG, AddRef());\r
66                 STDMETHOD_(ULONG, Release());\r
67                 STDMETHOD(QueryInterface(REFIID iid, void** ppv));\r
68 \r
69                 // IUIApplication methods\r
70                 STDMETHOD(OnCreateUICommand)(UINT nCmdID, __in UI_COMMANDTYPE typeID, \r
71                         __deref_out IUICommandHandler** ppCommandHandler);\r
72 \r
73                 STDMETHOD(OnDestroyUICommand)(UINT32 commandId, __in UI_COMMANDTYPE typeID,\r
74                         __in_opt IUICommandHandler* commandHandler);\r
75                         \r
76                 STDMETHOD(OnViewChanged)(UINT viewId, __in UI_VIEWTYPE typeId, __in IUnknown* pView,\r
77                         UI_VIEWVERB verb, INT uReasonCode);                     \r
78 \r
79                 // IUICommandHandle methods\r
80                 STDMETHODIMP Execute(UINT nCmdID, UI_EXECUTIONVERB verb, __in_opt const PROPERTYKEY* key, __in_opt const PROPVARIANT* ppropvarValue, \r
81                                                                                   __in_opt IUISimplePropertySet* pCommandExecutionProperties);\r
82 \r
83                 STDMETHODIMP UpdateProperty(UINT nCmdID, __in REFPROPERTYKEY key, __in_opt const PROPVARIANT* ppropvarCurrentValue, \r
84                                                                                                  __out PROPVARIANT* ppropvarNewValue);  \r
85                 \r
86                 bool virtual CreateRibbon(CWnd* pWnd);\r
87                 void virtual DestroyRibbon();\r
88                 IUIFramework* GetRibbonFramework() { return m_pRibbonFramework; }\r
89 \r
90         private:\r
91                 IUIFramework* m_pRibbonFramework;\r
92                 LONG m_cRef;                            // Reference count.\r
93 \r
94         };\r
95 \r
96 \r
97         class CRibbonFrame : public CFrame, public CRibbon\r
98         {\r
99         public:\r
100                 // A nested class for the MRU item properties\r
101                 class CRecentFiles : public IUISimplePropertySet\r
102                 {\r
103                 public:\r
104                         CRecentFiles(PWSTR wszFullPath);\r
105                         ~CRecentFiles() {}              \r
106 \r
107                         // IUnknown methods.\r
108                         STDMETHODIMP_(ULONG) AddRef();\r
109                         STDMETHODIMP_(ULONG) Release();\r
110                         STDMETHODIMP QueryInterface(REFIID iid, void** ppv);\r
111                         \r
112                         // IUISimplePropertySet methods \r
113                         STDMETHODIMP GetValue(__in REFPROPERTYKEY key, __out PROPVARIANT *value);\r
114 \r
115                 private:\r
116                         LONG m_cRef;                        // Reference count.\r
117                         WCHAR m_wszDisplayName[MAX_PATH];\r
118                         WCHAR m_wszFullPath[MAX_PATH];\r
119                 };\r
120 \r
121                 typedef Shared_Ptr<CRecentFiles> RecentFilesPtr;\r
122 \r
123                 CRibbonFrame() : m_uRibbonHeight(0) {}\r
124                 virtual ~CRibbonFrame() {}\r
125                 virtual CRect GetViewRect() const;\r
126                 virtual void OnCreate();\r
127                 virtual void OnDestroy();\r
128                 virtual STDMETHODIMP OnViewChanged(UINT32 viewId, UI_VIEWTYPE typeId, IUnknown* pView, UI_VIEWVERB verb, INT32 uReasonCode);\r
129                 virtual HRESULT PopulateRibbonRecentItems(__deref_out PROPVARIANT* pvarValue);\r
130                 virtual void UpdateMRUMenu();\r
131                 \r
132                 UINT GetRibbonHeight() const { return m_uRibbonHeight; }\r
133 \r
134         private:\r
135                 std::vector<RecentFilesPtr> m_vRecentFiles;\r
136                 void SetRibbonHeight(UINT uRibbonHeight) { m_uRibbonHeight = uRibbonHeight; }\r
137                 UINT m_uRibbonHeight;\r
138         };\r
139 \r
140 }\r
141 \r
142 \r
143 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r
144 \r
145 \r
146 namespace Win32xx\r
147 {\r
148         //////////////////////////////////////////////\r
149         // Definitions for the CRibbon class\r
150         //\r
151 \r
152         inline CRibbon::~CRibbon() \r
153         {\r
154                 // Reference count must be 1 or we have a leak!\r
155                 assert(m_cRef == 1);            \r
156         }\r
157 \r
158         // IUnknown method implementations.\r
159         inline STDMETHODIMP_(ULONG) CRibbon::AddRef()\r
160         {\r
161                 return InterlockedIncrement(&m_cRef);\r
162         }\r
163 \r
164         inline STDMETHODIMP_(ULONG) CRibbon::Release()\r
165         {\r
166                 LONG cRef = InterlockedDecrement(&m_cRef);\r
167                 return cRef;\r
168         }\r
169 \r
170         inline STDMETHODIMP CRibbon::Execute(UINT nCmdID, UI_EXECUTIONVERB verb, __in_opt const PROPERTYKEY* key, __in_opt const PROPVARIANT* ppropvarValue, \r
171                                                                                   __in_opt IUISimplePropertySet* pCommandExecutionProperties)\r
172         {\r
173                 UNREFERENCED_PARAMETER (nCmdID);\r
174                 UNREFERENCED_PARAMETER (verb);\r
175                 UNREFERENCED_PARAMETER (key);\r
176                 UNREFERENCED_PARAMETER (ppropvarValue);\r
177                 UNREFERENCED_PARAMETER (pCommandExecutionProperties);\r
178 \r
179                 return E_NOTIMPL;\r
180         }\r
181 \r
182         inline STDMETHODIMP CRibbon::QueryInterface(REFIID iid, void** ppv)\r
183         {\r
184                 if (iid == __uuidof(IUnknown))\r
185                 {\r
186                         *ppv = static_cast<IUnknown*>(static_cast<IUIApplication*>(this));\r
187                 }\r
188                 else if (iid == __uuidof(IUICommandHandler))\r
189                 {\r
190                         *ppv = static_cast<IUICommandHandler*>(this);\r
191                 }\r
192                 else if (iid == __uuidof(IUIApplication))\r
193                 {\r
194                         *ppv = static_cast<IUIApplication*>(this);\r
195                 }\r
196                 else \r
197                 {\r
198                         *ppv = NULL;\r
199                         return E_NOINTERFACE;\r
200                 }\r
201 \r
202                 AddRef();\r
203                 return S_OK;\r
204         }\r
205 \r
206         // Called by the Ribbon framework for each command specified in markup, to bind the Command to an IUICommandHandler.\r
207         inline STDMETHODIMP CRibbon::OnCreateUICommand(UINT nCmdID, __in UI_COMMANDTYPE typeID, \r
208                                                                                                  __deref_out IUICommandHandler** ppCommandHandler)\r
209         {\r
210                 UNREFERENCED_PARAMETER(typeID);\r
211                 UNREFERENCED_PARAMETER(nCmdID);\r
212 \r
213                 // By default we use the single command handler provided as part of CRibbon.\r
214                 // Override this function to account for multiple command handlers.             \r
215 \r
216                 return QueryInterface(IID_PPV_ARGS(ppCommandHandler));\r
217         }\r
218 \r
219         // Called when the state of the Ribbon changes, for example, created, destroyed, or resized.\r
220         inline STDMETHODIMP CRibbon::OnViewChanged(UINT viewId, __in UI_VIEWTYPE typeId, __in IUnknown* pView, \r
221                                                                                          UI_VIEWVERB verb, INT uReasonCode)\r
222         {\r
223                 UNREFERENCED_PARAMETER(viewId);\r
224                 UNREFERENCED_PARAMETER(typeId);\r
225                 UNREFERENCED_PARAMETER(pView);\r
226                 UNREFERENCED_PARAMETER(verb);\r
227                 UNREFERENCED_PARAMETER(uReasonCode);\r
228 \r
229 \r
230                 return E_NOTIMPL;\r
231         }\r
232 \r
233         // Called by the Ribbon framework for each command at the time of ribbon destruction.\r
234         inline STDMETHODIMP CRibbon::OnDestroyUICommand(UINT32 nCmdID, __in UI_COMMANDTYPE typeID,\r
235                                                                                                   __in_opt IUICommandHandler* commandHandler)\r
236         {\r
237                 UNREFERENCED_PARAMETER(commandHandler);\r
238                 UNREFERENCED_PARAMETER(typeID);\r
239                 UNREFERENCED_PARAMETER(nCmdID);\r
240 \r
241                 return E_NOTIMPL;\r
242         }\r
243 \r
244         // Called by the Ribbon framework when a command property (PKEY) needs to be updated.\r
245         inline STDMETHODIMP CRibbon::UpdateProperty(UINT nCmdID, __in REFPROPERTYKEY key, __in_opt const PROPVARIANT* ppropvarCurrentValue, \r
246                                                                                                  __out PROPVARIANT* ppropvarNewValue)\r
247         {\r
248                 UNREFERENCED_PARAMETER(nCmdID);\r
249                 UNREFERENCED_PARAMETER(key);\r
250                 UNREFERENCED_PARAMETER(ppropvarCurrentValue);\r
251                 UNREFERENCED_PARAMETER(ppropvarNewValue);\r
252 \r
253                 return E_NOTIMPL;\r
254         }\r
255 \r
256         inline bool CRibbon::CreateRibbon(CWnd* pWnd)\r
257         {       \r
258                 ::CoInitialize(NULL);\r
259 \r
260                 // Instantiate the Ribbon framework object.\r
261                 ::CoCreateInstance(CLSID_UIRibbonFramework, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_pRibbonFramework));\r
262 \r
263                 // Connect the host application to the Ribbon framework.\r
264                 HRESULT hr = m_pRibbonFramework->Initialize(pWnd->GetHwnd(), this);\r
265                 if (FAILED(hr))\r
266                 {\r
267                         return false;\r
268                 }\r
269 \r
270                 // Load the binary markup. APPLICATION_RIBBON is the default name generated by uicc.\r
271                 hr = m_pRibbonFramework->LoadUI(GetModuleHandle(NULL), L"APPLICATION_RIBBON");\r
272                 if (FAILED(hr))\r
273                 {\r
274                         return false;\r
275                 }\r
276 \r
277                 return true;\r
278         }\r
279 \r
280         inline void CRibbon::DestroyRibbon()\r
281         {\r
282                 if (m_pRibbonFramework)\r
283                 {\r
284                         m_pRibbonFramework->Destroy();\r
285                         m_pRibbonFramework->Release();\r
286                         m_pRibbonFramework = NULL;\r
287                 }\r
288         }\r
289         \r
290         \r
291         //////////////////////////////////////////////\r
292         // Definitions for the CRibbonFrame class\r
293         //\r
294 \r
295         inline CRect CRibbonFrame::GetViewRect() const\r
296         {\r
297                 // Get the frame's client area\r
298                 CRect rcFrame = GetClientRect();\r
299 \r
300                 // Get the statusbar's window area\r
301                 CRect rcStatus;\r
302                 if (GetStatusBar().IsWindowVisible() || !IsWindowVisible())\r
303                         rcStatus = GetStatusBar().GetWindowRect();\r
304 \r
305                 // Get the top rebar or toolbar's window area\r
306                 CRect rcTop;\r
307                 if (IsReBarSupported() && m_bUseReBar)\r
308                         rcTop = GetReBar().GetWindowRect();\r
309                 else\r
310                         if (m_bUseToolBar && GetToolBar().IsWindowVisible())\r
311                                 rcTop = GetToolBar().GetWindowRect();\r
312 \r
313                 // Return client size less the rebar and status windows\r
314                 int top = rcFrame.top + rcTop.Height() + m_uRibbonHeight;\r
315                 int left = rcFrame.left;\r
316                 int right = rcFrame.right;\r
317                 int bottom = rcFrame.Height() - (rcStatus.Height());\r
318                 if ((bottom <= top) ||( right <= left))\r
319                         top = left = right = bottom = 0;\r
320 \r
321                 CRect rcView(left, top, right, bottom);\r
322                 return rcView;\r
323         }\r
324 \r
325         inline void CRibbonFrame::OnCreate()\r
326         {\r
327                 // OnCreate is called automatically during window creation when a\r
328                 // WM_CREATE message received.\r
329 \r
330                 // Tasks such as setting the icon, creating child windows, or anything\r
331                 // associated with creating windows are normally performed here.\r
332 \r
333                 if (GetWinVersion() >= 2601)    // WinVersion >= Windows 7\r
334                 {               \r
335                         m_bUseReBar = FALSE;                    // Don't use rebars\r
336                         m_bUseToolBar = FALSE;                  // Don't use a toolbar\r
337                         \r
338                         CFrame::OnCreate();\r
339 \r
340                         if (CreateRibbon(this))\r
341                                 TRACE(_T("Ribbon Created Succesfully\n"));\r
342                         else\r
343                                 throw CWinException(_T("Failed to create ribbon"));\r
344                 }\r
345                 else \r
346                 {\r
347                         CFrame::OnCreate();\r
348                 }\r
349         }\r
350 \r
351         inline void CRibbonFrame::OnDestroy()\r
352         {\r
353                 DestroyRibbon();\r
354                 CFrame::OnDestroy();\r
355         }\r
356 \r
357         inline STDMETHODIMP CRibbonFrame::OnViewChanged(UINT32 viewId, UI_VIEWTYPE typeId, IUnknown* pView, UI_VIEWVERB verb, INT32 uReasonCode)\r
358         {\r
359                 UNREFERENCED_PARAMETER(viewId);\r
360                 UNREFERENCED_PARAMETER(uReasonCode);\r
361 \r
362                 HRESULT hr = E_NOTIMPL;\r
363 \r
364                 // Checks to see if the view that was changed was a Ribbon view.\r
365                 if (UI_VIEWTYPE_RIBBON == typeId)\r
366                 {\r
367                         switch (verb)\r
368                         {           \r
369                                 // The view was newly created.\r
370                         case UI_VIEWVERB_CREATE:\r
371                                 hr = S_OK;\r
372                                 break;\r
373 \r
374                                 // The view has been resized.  For the Ribbon view, the application should\r
375                                 // call GetHeight to determine the height of the ribbon.\r
376                         case UI_VIEWVERB_SIZE:\r
377                                 {\r
378                                         IUIRibbon* pRibbon = NULL;\r
379                                         UINT uRibbonHeight;\r
380 \r
381                                         hr = pView->QueryInterface(IID_PPV_ARGS(&pRibbon));\r
382                                         if (SUCCEEDED(hr))\r
383                                         {\r
384                                                 // Call to the framework to determine the desired height of the Ribbon.\r
385                                                 hr = pRibbon->GetHeight(&uRibbonHeight);\r
386                                                 SetRibbonHeight(uRibbonHeight);\r
387                                                 pRibbon->Release();\r
388 \r
389                                                 RecalcLayout();\r
390                                                 // Use the ribbon height to position controls in the client area of the window.\r
391                                         }\r
392                                 }\r
393                                 break;\r
394                                 // The view was destroyed.\r
395                         case UI_VIEWVERB_DESTROY:\r
396                                 hr = S_OK;\r
397                                 break;\r
398                         }\r
399                 }  \r
400 \r
401                 return hr; \r
402         }\r
403 \r
404         inline HRESULT CRibbonFrame::PopulateRibbonRecentItems(__deref_out PROPVARIANT* pvarValue)\r
405         {\r
406                 LONG iCurrentFile = 0;\r
407                 std::vector<CString> FileNames = GetMRUEntries();\r
408                 std::vector<CString>::iterator iter;\r
409                 int iFileCount = FileNames.size();\r
410                 HRESULT hr = E_FAIL;\r
411                 SAFEARRAY* psa = SafeArrayCreateVector(VT_UNKNOWN, 0, iFileCount);\r
412                 m_vRecentFiles.clear();\r
413                 \r
414                 if (psa != NULL)\r
415                 {\r
416                         for (iter = FileNames.begin(); iter < FileNames.end(); ++iter)\r
417                         {\r
418                                 CString strCurrentFile = (*iter);\r
419                                 WCHAR wszCurrentFile[MAX_PATH] = {0L};\r
420                                 lstrcpynW(wszCurrentFile, T2W(strCurrentFile), MAX_PATH);\r
421                                 \r
422                                 CRecentFiles* pRecentFiles = new CRecentFiles(wszCurrentFile);\r
423                                 m_vRecentFiles.push_back(RecentFilesPtr(pRecentFiles));\r
424                                 hr = SafeArrayPutElement(psa, &iCurrentFile, static_cast<void*>(pRecentFiles));\r
425                                 ++iCurrentFile;\r
426                         }\r
427 \r
428                         SAFEARRAYBOUND sab = {iCurrentFile,0};\r
429                         SafeArrayRedim(psa, &sab);\r
430                         hr = UIInitPropertyFromIUnknownArray(UI_PKEY_RecentItems, psa, pvarValue);\r
431 \r
432                         SafeArrayDestroy(psa);  // Calls release for each element in the array\r
433                 }\r
434 \r
435                 return hr;\r
436         }\r
437 \r
438         inline void CRibbonFrame::UpdateMRUMenu()\r
439         {\r
440                 // Suppress UpdateMRUMenu when ribbon is used\r
441                 if (0 != GetRibbonFramework()) return;\r
442 \r
443                 CFrame::UpdateMRUMenu();\r
444         }\r
445 \r
446 \r
447         ////////////////////////////////////////////////////////\r
448         // Declaration of the nested CRecentFiles class\r
449         //\r
450         inline CRibbonFrame::CRecentFiles::CRecentFiles(PWSTR wszFullPath) : m_cRef(1)\r
451         {\r
452                 SHFILEINFOW sfi;\r
453                 DWORD_PTR dwPtr = NULL;\r
454                 m_wszFullPath[0] = L'\0';\r
455                 m_wszDisplayName[0] = L'\0';\r
456 \r
457                 if (NULL != lstrcpynW(m_wszFullPath, wszFullPath, MAX_PATH))\r
458                 {    \r
459                         dwPtr = ::SHGetFileInfoW(wszFullPath, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_DISPLAYNAME | SHGFI_USEFILEATTRIBUTES);\r
460                 \r
461                         if (dwPtr != NULL)\r
462                         {\r
463                                 lstrcpynW(m_wszDisplayName, sfi.szDisplayName, MAX_PATH);\r
464                         }\r
465                         else // Provide a reasonable fallback.\r
466                         {\r
467                                 lstrcpynW(m_wszDisplayName, m_wszFullPath, MAX_PATH);\r
468                         }\r
469                 }\r
470         }\r
471 \r
472         inline STDMETHODIMP_(ULONG) CRibbonFrame::CRecentFiles::AddRef()\r
473         {\r
474                 return InterlockedIncrement(&m_cRef);\r
475         }\r
476 \r
477         inline STDMETHODIMP_(ULONG) CRibbonFrame::CRecentFiles::Release()\r
478         {\r
479                 return InterlockedDecrement(&m_cRef);\r
480         }\r
481 \r
482         inline STDMETHODIMP CRibbonFrame::CRecentFiles::QueryInterface(REFIID iid, void** ppv)\r
483         {\r
484                 if (!ppv)\r
485                 {\r
486                         return E_POINTER;\r
487                 }\r
488 \r
489                 if (iid == __uuidof(IUnknown))\r
490                 {\r
491                         *ppv = static_cast<IUnknown*>(this);\r
492                 }\r
493                 else if (iid == __uuidof(IUISimplePropertySet))\r
494                 {\r
495                         *ppv = static_cast<IUISimplePropertySet*>(this);\r
496                 }\r
497                 else \r
498                 {\r
499                         *ppv = NULL;\r
500                         return E_NOINTERFACE;\r
501                 }\r
502 \r
503                 AddRef();\r
504                 return S_OK;\r
505         }\r
506 \r
507         // IUISimplePropertySet methods.\r
508         inline STDMETHODIMP CRibbonFrame::CRecentFiles::GetValue(__in REFPROPERTYKEY key, __out PROPVARIANT *ppropvar)\r
509         {\r
510                 HRESULT hr = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED);\r
511 \r
512                 if (key == UI_PKEY_Label)\r
513                 {\r
514                         hr = UIInitPropertyFromString(key, m_wszDisplayName, ppropvar);\r
515                 }\r
516                 else if (key == UI_PKEY_LabelDescription)\r
517                 {\r
518                         hr = UIInitPropertyFromString(key, m_wszDisplayName, ppropvar);\r
519                 }\r
520 \r
521                 return hr;\r
522         }\r
523 \r
524 } // namespace Win32xx\r
525 \r
526 #endif  // _WIN32XX_RIBBON_H_\r
527 \r

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