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