atlribbon.h 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489
  1. // Windows Template Library - WTL version 10.0
  2. // Copyright (C) Microsoft Corporation, WTL Team. All rights reserved.
  3. //
  4. // This file is a part of the Windows Template Library.
  5. // The use and distribution terms for this software are covered by the
  6. // Microsoft Public License (http://opensource.org/licenses/MS-PL)
  7. // which can be found in the file MS-PL.txt at the root folder.
  8. #ifndef __ATLRIBBON_H__
  9. #define __ATLRIBBON_H__
  10. #pragma once
  11. #if (_MSC_VER < 1500)
  12. #error atlribbon.h requires Visual C++ 2008 compiler or higher
  13. #endif
  14. #ifndef _UNICODE
  15. #error atlribbon.h requires the Unicode character set
  16. #endif
  17. #if !defined(NTDDI_WIN7) || (NTDDI_VERSION < NTDDI_WIN7)
  18. #error atlribbon.h requires the Windows 7 SDK or higher
  19. #endif
  20. #ifndef __ATLAPP_H__
  21. #error atlribbon.h requires atlapp.h to be included first
  22. #endif
  23. #include <atlmisc.h> // for RecentDocumentList classes
  24. #include <atlframe.h> // for Frame and UpdateUI classes
  25. #include <atlctrls.h> // required for atlctrlw.h
  26. #include <atlctrlw.h> // for CCommandBarCtrl
  27. #ifndef __ATLSTR_H__
  28. #pragma warning(push)
  29. #pragma warning(disable: 4530) // unwind semantics not enabled
  30. #include <string>
  31. #pragma warning(pop)
  32. #endif
  33. #include <dwmapi.h>
  34. #pragma comment(lib, "dwmapi.lib")
  35. #include "UIRibbon.h"
  36. #include "UIRibbonPropertyHelpers.h"
  37. #pragma comment(lib, "propsys.lib")
  38. #include <Richedit.h> // for CHARFORMAT2
  39. ///////////////////////////////////////////////////////////////////////////////
  40. // Classes in this file:
  41. //
  42. // CRibbonUpdateUI : Automatic mapping of ribbon UI elements
  43. //
  44. // RibbonUI::Text
  45. // RibbonUI::CharFormat
  46. // RibbonUI::ICtrl
  47. // RibbonUI::CtrlImpl
  48. // RibbonUI::CommandCtrlImpl
  49. // RibbonUI::ItemProperty
  50. // RibbonUI::CollectionImplBase
  51. // RibbonUI::CollectionImpl
  52. // RibbonUI::TextCollectionImpl
  53. // RibbonUI::ItemCollectionImpl
  54. // RibbonUI::ComboCollectionImpl
  55. // RibbonUI::CommandCollectionImpl
  56. // RibbonUI::ToolbarCollectionImpl
  57. // RibbonUI::SimpleCollectionImpl
  58. // RibbonUI::CollectionCtrlImpl
  59. // RibbonUI::ToolbarGalleryCtrlImpl
  60. // RibbonUI::SimpleCollectionCtrlImpl
  61. // RibbonUI::RecentItemsCtrlImpl
  62. // RibbonUI::FontCtrlImpl
  63. // RibbonUI::ColorCtrlImpl
  64. // RibbonUI::SpinnerCtrlImpl
  65. //
  66. // RibbonUI::CRibbonImpl
  67. // CRibbonImpl::CRibbonComboCtrl
  68. // CRibbonImpl::CRibbonItemGalleryCtrl
  69. // CRibbonImpl::CRibbonCommandGalleryCtrl
  70. // CRibbonImpl::CRibbonToolbarGalleryCtrl
  71. // CRibbonImpl::CRibbonSimpleComboCtrl
  72. // CRibbonImpl::CRibbonSimpleGalleryCtrl
  73. // CRibbonImpl::CRibbonRecentItemsCtrl
  74. // CRibbonImpl::CRibbonColorCtrl
  75. // CRibbonImpl::CRibbonFontCtrl
  76. // CRibbonImpl::CRibbonSpinnerCtrl
  77. // CRibbonImpl::CRibbonFloatSpinnerCtrl
  78. // CRibbonImpl::CRibbonCommandCtrl
  79. //
  80. // CRibbonFrameWindowImplBase
  81. // CRibbonFrameWindowImpl
  82. // CRibbonMDIFrameWindowImpl
  83. // CRibbonPersist
  84. //
  85. // Global functions:
  86. // RibbonUI::SetPropertyVal()
  87. // RibbonUI::GetImage()
  88. // Constants
  89. #ifndef RIBBONUI_MAX_TEXT
  90. #define RIBBONUI_MAX_TEXT 128
  91. #endif
  92. #define TWIPS_PER_POINT 20 // For font size
  93. namespace WTL
  94. {
  95. ///////////////////////////////////////////////////////////////////////////////
  96. // CRibbonUpdateUI : Automatic mapping of ribbon UI elements
  97. template <class T>
  98. class CRibbonUpdateUI : public CAutoUpdateUI<T>
  99. {
  100. public:
  101. enum
  102. {
  103. UPDUI_RIBBON = 0x0080,
  104. UPDUI_PERSIST = 0x0020
  105. };
  106. bool IsRibbonElement(const CUpdateUIBase::_AtlUpdateUIMap& UIMap)
  107. {
  108. return (UIMap.m_wType & UPDUI_RIBBON) != 0;
  109. }
  110. bool IsRibbonID(UINT nID)
  111. {
  112. for(int i = 0; i < this->m_arrUIMap.GetSize(); i++)
  113. {
  114. if(this->m_arrUIMap[i].m_nID == nID)
  115. return IsRibbonElement(this->m_arrUIMap[i]);
  116. }
  117. return false;
  118. }
  119. // Element
  120. bool UIAddRibbonElement(UINT nID)
  121. {
  122. return this->UIAddElement<UPDUI_RIBBON>(nID);
  123. }
  124. bool UIRemoveRibbonElement(UINT nID)
  125. {
  126. return this->UIRemoveElement<UPDUI_RIBBON>(nID);
  127. }
  128. bool UIPersistElement(UINT nID, bool bPersist = true)
  129. {
  130. return bPersist ?
  131. this->UIAddElement<UPDUI_PERSIST>(nID) :
  132. this->UIRemoveElement<UPDUI_PERSIST>(nID);
  133. }
  134. // methods for Ribbon elements
  135. BOOL UISetText(int nID, LPCWSTR sText, BOOL bForceUpdate = FALSE)
  136. {
  137. T* pT = static_cast<T*>(this);
  138. BOOL bRes = CUpdateUIBase::UISetText(nID, sText, bForceUpdate);
  139. if (pT->IsRibbonUI() && IsRibbonID(nID))
  140. bRes = SUCCEEDED(pT->InvalidateProperty(nID, UI_PKEY_Label));
  141. return bRes;
  142. }
  143. BOOL UISetText(int nID, UINT uIdResource, BOOL bForceUpdate = FALSE)
  144. {
  145. ATL::CTempBuffer<WCHAR> sText(RIBBONUI_MAX_TEXT);
  146. int nRet = ATL::AtlLoadString(uIdResource, sText, RIBBONUI_MAX_TEXT);
  147. if(nRet > 0)
  148. UISetText(nID, sText, bForceUpdate);
  149. return (nRet > 0) ? TRUE : FALSE;
  150. }
  151. LPCTSTR UIGetText(int nID)
  152. {
  153. T* pT = static_cast<T*>(this);
  154. LPCTSTR sUI = CAutoUpdateUI<T>::UIGetText(nID);
  155. // replace 'tab' by 'space' for RibbonUI elements
  156. if (sUI && pT->IsRibbonUI() && IsRibbonID(nID) && wcschr(sUI, L'\t'))
  157. {
  158. static WCHAR sText[RIBBONUI_MAX_TEXT] = {};
  159. wcscpy_s(sText, sUI);
  160. WCHAR* pch = wcschr(sText, L'\t');
  161. if (pch != NULL)
  162. *pch = L' ';
  163. return sText;
  164. }
  165. else
  166. {
  167. return sUI;
  168. }
  169. }
  170. BOOL UIEnable(int nID, BOOL bEnable, BOOL bForceUpdate = FALSE)
  171. {
  172. T* pT = static_cast<T*>(this);
  173. BOOL bRes = CUpdateUIBase::UIEnable(nID, bEnable, bForceUpdate);
  174. if (pT->IsRibbonUI() && IsRibbonID(nID))
  175. bRes = SUCCEEDED(pT->SetProperty((WORD)nID, UI_PKEY_Enabled, bEnable));
  176. return bRes;
  177. }
  178. BOOL UISetCheck(int nID, INT nCheck, BOOL bForceUpdate = FALSE)
  179. {
  180. if ((nCheck == 0) || (nCheck == 1))
  181. return UISetCheck(nID, nCheck != 0, bForceUpdate);
  182. else
  183. return CUpdateUIBase::UISetCheck(nID, nCheck, bForceUpdate);
  184. }
  185. BOOL UISetCheck(int nID, bool bCheck, BOOL bForceUpdate = FALSE)
  186. {
  187. T* pT = static_cast<T*>(this);
  188. BOOL bRes = CUpdateUIBase::UISetCheck(nID, bCheck, bForceUpdate);
  189. if (bRes && pT->IsRibbonUI() && IsRibbonID(nID))
  190. bRes = SUCCEEDED(pT->SetProperty((WORD)nID, UI_PKEY_BooleanValue, bCheck));
  191. return bRes;
  192. }
  193. };
  194. ///////////////////////////////////////////////////////////////////////////////
  195. // RibbonUI namespace
  196. //
  197. namespace RibbonUI
  198. {
  199. // Minimal string allocation support for various PROPERTYKEY values
  200. #ifdef __ATLSTR_H__
  201. typedef ATL::CString Text;
  202. #else
  203. class Text : public std::wstring
  204. {
  205. public:
  206. Text(std::wstring& s) : std::wstring(s)
  207. { }
  208. Text(LPCWSTR s) : std::wstring(s)
  209. { }
  210. Text()
  211. { }
  212. bool IsEmpty()
  213. {
  214. return empty();
  215. }
  216. operator LPCWSTR()
  217. {
  218. return c_str();
  219. }
  220. Text& operator =(LPCWSTR s)
  221. {
  222. return static_cast<Text&>(std::wstring::operator =(s));
  223. }
  224. };
  225. #endif // __ATLSTR_H__
  226. // PROPERTYKEY enum and helpers
  227. enum k_KEY
  228. {
  229. // state
  230. k_Enabled = 1, k_BooleanValue = 200,
  231. // text properties
  232. k_LabelDescription = 2, k_Keytip = 3, k_Label = 4, k_TooltipDescription = 5, k_TooltipTitle = 6,
  233. // image properties
  234. k_LargeImage = 7, k_LargeHighContrastImage = 8, k_SmallImage = 9, k_SmallHighContrastImage = 10,
  235. // collection properties
  236. k_ItemsSource = 101, k_Categories = 102, k_SelectedItem = 104,
  237. // collection item properties
  238. k_CommandId = 100, k_CategoryId = 103, k_CommandType = 105, k_ItemImage = 106,
  239. // combo control property
  240. k_StringValue = 202,
  241. // spinner control properties
  242. k_DecimalValue = 201, k_MaxValue = 203, k_MinValue, k_Increment, k_DecimalPlaces, k_FormatString, k_RepresentativeString = 208,
  243. // font control properties
  244. k_FontProperties = 300, k_FontProperties_Family, k_FontProperties_Size, k_FontProperties_Bold, k_FontProperties_Italic = 304,
  245. k_FontProperties_Underline = 305, k_FontProperties_Strikethrough, k_FontProperties_VerticalPositioning, k_FontProperties_ForegroundColor = 308,
  246. k_FontProperties_BackgroundColor = 309, k_FontProperties_ForegroundColorType, k_FontProperties_BackgroundColorType, k_FontProperties_ChangedProperties = 312,
  247. k_FontProperties_DeltaSize = 313,
  248. // recent items properties
  249. k_RecentItems = 350, k_Pinned = 351,
  250. // color control properties
  251. k_Color = 400, k_ColorType = 401, k_ColorMode,
  252. k_ThemeColorsCategoryLabel = 403, k_StandardColorsCategoryLabel, k_RecentColorsCategoryLabel = 405, k_AutomaticColorLabel = 406,
  253. k_NoColorLabel = 407, k_MoreColorsLabel = 408,
  254. k_ThemeColors = 409, k_StandardColors = 410, k_ThemeColorsTooltips = 411, k_StandardColorsTooltips = 412,
  255. // Ribbon state
  256. k_Viewable = 1000, k_Minimized = 1001, k_QuickAccessToolbarDock = 1002, k_ContextAvailable = 1100,
  257. // Ribbon UI colors
  258. k_GlobalBackgroundColor = 2000, k_GlobalHighlightColor, k_GlobalTextColor = 2002
  259. };
  260. inline k_KEY k_(REFPROPERTYKEY key)
  261. {
  262. return (k_KEY)key.fmtid.Data1;
  263. }
  264. // PROPERTYKEY value assignment and specializations
  265. //
  266. template <typename V>
  267. HRESULT SetPropertyVal(REFPROPERTYKEY key, V val, PROPVARIANT* ppv)
  268. {
  269. switch (k_(key))
  270. {
  271. case k_Enabled:
  272. case k_BooleanValue:
  273. return InitPropVariantFromBoolean(val, ppv);
  274. default:
  275. return UIInitPropertyFromUInt32(key, val, ppv);
  276. }
  277. }
  278. inline HRESULT SetPropertyVal(REFPROPERTYKEY key, DOUBLE val, PROPVARIANT* ppv)
  279. {
  280. return SetPropertyVal(key, (LONG)val, ppv);
  281. }
  282. inline HRESULT SetPropertyVal(REFPROPERTYKEY key, IUIImage* val, PROPVARIANT* ppv)
  283. {
  284. HRESULT hr = UIInitPropertyFromImage(key, val, ppv);
  285. ATLVERIFY(val->Release() == 1);
  286. return hr;
  287. }
  288. inline HRESULT SetPropertyVal(REFPROPERTYKEY key, IUnknown* val, PROPVARIANT* ppv)
  289. {
  290. return UIInitPropertyFromInterface(key, val, ppv);
  291. }
  292. inline HRESULT SetPropertyVal(REFPROPERTYKEY key, IPropertyStore* val, PROPVARIANT* ppv)
  293. {
  294. return UIInitPropertyFromInterface(key, val, ppv);
  295. }
  296. inline HRESULT SetPropertyVal(REFPROPERTYKEY key, SAFEARRAY* val, PROPVARIANT* ppv)
  297. {
  298. return UIInitPropertyFromIUnknownArray(key, val, ppv);
  299. }
  300. inline HRESULT SetPropertyVal(REFPROPERTYKEY key, DECIMAL* val, PROPVARIANT* ppv)
  301. {
  302. return UIInitPropertyFromDecimal(key, *val, ppv);
  303. }
  304. inline HRESULT SetPropertyVal(REFPROPERTYKEY key, bool val, PROPVARIANT* ppv)
  305. {
  306. return UIInitPropertyFromBoolean(key, val, ppv);
  307. }
  308. inline HRESULT SetPropertyVal(REFPROPERTYKEY key, LPCWSTR val, PROPVARIANT* ppv)
  309. {
  310. return UIInitPropertyFromString(key, val, ppv);
  311. }
  312. // CharFormat helper struct for RibbonUI font control
  313. //
  314. struct CharFormat : CHARFORMAT2
  315. {
  316. // Default constructor
  317. CharFormat()
  318. {
  319. cbSize = sizeof(CHARFORMAT2);
  320. Reset();
  321. }
  322. // Copy constructor
  323. CharFormat(const CharFormat& cf)
  324. {
  325. this->CharFormat::CharFormat();
  326. ::CopyMemory(this, &cf, sizeof(CharFormat));
  327. }
  328. // Assign operator
  329. CharFormat& operator =(const CharFormat& cf)
  330. {
  331. ::CopyMemory(this, &cf, sizeof(CharFormat));
  332. return (*this);
  333. }
  334. void Reset()
  335. {
  336. uValue = dwMask = dwEffects = 0;
  337. PropVariantInit(&propvar);
  338. }
  339. void operator <<(IPropertyStore* pStore)
  340. {
  341. if (pStore == NULL)
  342. {
  343. ATLASSERT(FALSE);
  344. return;
  345. }
  346. static void (CharFormat::*Getk_[])(IPropertyStore*) =
  347. {
  348. &CharFormat::Getk_Family,
  349. &CharFormat::Getk_FontProperties_Size,
  350. &CharFormat::Getk_MaskEffectBold,
  351. &CharFormat::Getk_MaskEffectItalic,
  352. &CharFormat::Getk_MaskEffectUnderline,
  353. &CharFormat::Getk_MaskEffectStrikeout,
  354. &CharFormat::Getk_VerticalPositioning,
  355. &CharFormat::Getk_Color,
  356. &CharFormat::Getk_ColorBack,
  357. &CharFormat::Getk_ColorType,
  358. &CharFormat::Getk_ColorTypeBack,
  359. };
  360. DWORD nProps = 0;
  361. Reset();
  362. ATLVERIFY(SUCCEEDED(pStore->GetCount(&nProps)));
  363. for (DWORD iProp = 0; iProp < nProps; iProp++)
  364. {
  365. PROPERTYKEY key;
  366. ATLVERIFY(SUCCEEDED(pStore->GetAt(iProp, &key)));
  367. ATLASSERT(k_(key) >= k_FontProperties_Family);
  368. if (k_(key) <= k_FontProperties_BackgroundColorType)
  369. (this->*Getk_[k_(key) - k_FontProperties_Family])(pStore);
  370. }
  371. }
  372. void operator >>(IPropertyStore* pStore)
  373. {
  374. if (pStore == NULL)
  375. {
  376. ATLASSERT(FALSE);
  377. return;
  378. }
  379. PutFace(pStore);
  380. PutSize(pStore);
  381. PutMaskEffect(CFM_BOLD, CFE_BOLD, UI_PKEY_FontProperties_Bold, pStore);
  382. PutMaskEffect(CFM_ITALIC, CFE_ITALIC, UI_PKEY_FontProperties_Italic, pStore);
  383. PutMaskEffect(CFM_UNDERLINE, CFE_UNDERLINE, UI_PKEY_FontProperties_Underline, pStore);
  384. PutMaskEffect(CFM_STRIKEOUT, CFE_STRIKEOUT, UI_PKEY_FontProperties_Strikethrough, pStore);
  385. PutVerticalPos(pStore);
  386. PutColor(pStore);
  387. PutBackColor(pStore);
  388. }
  389. private:
  390. PROPVARIANT propvar;
  391. UINT uValue;
  392. // Getk_ functions
  393. void Getk_Family(IPropertyStore* pStore)
  394. {
  395. if (SUCCEEDED(pStore->GetValue(UI_PKEY_FontProperties_Family, &propvar)))
  396. {
  397. PropVariantToString(propvar, szFaceName, LF_FACESIZE);
  398. if (*szFaceName)
  399. dwMask |= CFM_FACE;
  400. }
  401. }
  402. void Getk_FontProperties_Size(IPropertyStore* pStore)
  403. {
  404. if (SUCCEEDED(pStore->GetValue(UI_PKEY_FontProperties_Size, &propvar)))
  405. {
  406. DECIMAL decSize = {};
  407. UIPropertyToDecimal(UI_PKEY_FontProperties_Size, propvar, &decSize);
  408. DOUBLE dSize = 0;
  409. VarR8FromDec(&decSize, &dSize);
  410. if (dSize > 0)
  411. {
  412. dwMask |= CFM_SIZE;
  413. yHeight = (LONG)(dSize * TWIPS_PER_POINT);
  414. }
  415. }
  416. }
  417. void Getk_MaskEffectBold(IPropertyStore* pStore)
  418. {
  419. Getk_MaskEffectAll(pStore, CFM_BOLD, CFE_BOLD, UI_PKEY_FontProperties_Bold);
  420. }
  421. void Getk_MaskEffectItalic(IPropertyStore* pStore)
  422. {
  423. Getk_MaskEffectAll(pStore, CFM_ITALIC, CFE_ITALIC, UI_PKEY_FontProperties_Italic);
  424. }
  425. void Getk_MaskEffectUnderline(IPropertyStore* pStore)
  426. {
  427. Getk_MaskEffectAll(pStore, CFM_UNDERLINE, CFE_UNDERLINE, UI_PKEY_FontProperties_Underline);
  428. }
  429. void Getk_MaskEffectStrikeout(IPropertyStore* pStore)
  430. {
  431. Getk_MaskEffectAll(pStore, CFM_STRIKEOUT, CFE_STRIKEOUT, UI_PKEY_FontProperties_Strikethrough);
  432. }
  433. void Getk_MaskEffectAll(IPropertyStore* pStore, DWORD _dwMask, DWORD _dwEffects, REFPROPERTYKEY key)
  434. {
  435. if (SUCCEEDED(pStore->GetValue(key, &propvar)))
  436. {
  437. UIPropertyToUInt32(key, propvar, &uValue);
  438. if ((UI_FONTPROPERTIES)uValue != UI_FONTPROPERTIES_NOTAVAILABLE)
  439. {
  440. dwMask |= _dwMask;
  441. dwEffects |= ((UI_FONTPROPERTIES)uValue == UI_FONTPROPERTIES_SET) ? _dwEffects : 0;
  442. }
  443. }
  444. }
  445. void Getk_VerticalPositioning(IPropertyStore* pStore)
  446. {
  447. if (SUCCEEDED(pStore->GetValue(UI_PKEY_FontProperties_VerticalPositioning, &propvar)))
  448. {
  449. UIPropertyToUInt32(UI_PKEY_FontProperties_VerticalPositioning, propvar, &uValue);
  450. UI_FONTVERTICALPOSITION uVerticalPosition = (UI_FONTVERTICALPOSITION) uValue;
  451. if ((uVerticalPosition != UI_FONTVERTICALPOSITION_NOTAVAILABLE))
  452. {
  453. dwMask |= (CFM_SUPERSCRIPT | CFM_SUBSCRIPT);
  454. if (uVerticalPosition != UI_FONTVERTICALPOSITION_NOTSET)
  455. {
  456. dwEffects |= (uVerticalPosition == UI_FONTVERTICALPOSITION_SUPERSCRIPT) ? CFE_SUPERSCRIPT : CFE_SUBSCRIPT;
  457. }
  458. }
  459. }
  460. }
  461. void Getk_Color(IPropertyStore* pStore)
  462. {
  463. Getk_ColorAll(pStore, CFM_COLOR, UI_PKEY_FontProperties_ForegroundColor);
  464. }
  465. void Getk_ColorBack(IPropertyStore* pStore)
  466. {
  467. Getk_ColorAll(pStore, CFM_BACKCOLOR, UI_PKEY_FontProperties_BackgroundColor);
  468. }
  469. void Getk_ColorAll(IPropertyStore* pStore, DWORD _dwMask, REFPROPERTYKEY key)
  470. {
  471. UINT32 color = 0;
  472. if (SUCCEEDED(pStore->GetValue(key, &propvar)))
  473. {
  474. UIPropertyToUInt32(key, propvar, &color);
  475. dwMask |= _dwMask;
  476. if (_dwMask == CFM_COLOR)
  477. crTextColor = color;
  478. else
  479. crBackColor = color;
  480. }
  481. }
  482. void Getk_ColorType(IPropertyStore* pStore)
  483. {
  484. Getk_ColorTypeAll(pStore, CFM_COLOR, CFE_AUTOCOLOR, UI_SWATCHCOLORTYPE_AUTOMATIC, UI_PKEY_FontProperties_ForegroundColor);
  485. }
  486. void Getk_ColorTypeBack(IPropertyStore* pStore)
  487. {
  488. Getk_ColorTypeAll(pStore, CFM_BACKCOLOR, CFE_AUTOBACKCOLOR, UI_SWATCHCOLORTYPE_NOCOLOR, UI_PKEY_FontProperties_BackgroundColor);
  489. }
  490. void Getk_ColorTypeAll(IPropertyStore* pStore, DWORD _dwMask, DWORD _dwEffects, UI_SWATCHCOLORTYPE _type, REFPROPERTYKEY key)
  491. {
  492. if (SUCCEEDED(pStore->GetValue(key, &propvar)))
  493. {
  494. UIPropertyToUInt32(key, propvar, &uValue);
  495. if (_type == (UI_SWATCHCOLORTYPE)uValue)
  496. {
  497. dwMask |= _dwMask;
  498. dwEffects |= _dwEffects;
  499. }
  500. }
  501. }
  502. // Put functions
  503. void PutMaskEffect(WORD dwMaskVal, WORD dwEffectVal, REFPROPERTYKEY key, IPropertyStore* pStore)
  504. {
  505. PROPVARIANT var;
  506. UI_FONTPROPERTIES uProp = UI_FONTPROPERTIES_NOTAVAILABLE;
  507. if ((dwMask & dwMaskVal) != 0)
  508. uProp = dwEffects & dwEffectVal ? UI_FONTPROPERTIES_SET : UI_FONTPROPERTIES_NOTSET;
  509. SetPropertyVal(key, uProp, &var);
  510. pStore->SetValue(key, var);
  511. }
  512. void PutVerticalPos(IPropertyStore* pStore)
  513. {
  514. PROPVARIANT var;
  515. UI_FONTVERTICALPOSITION uProp = UI_FONTVERTICALPOSITION_NOTAVAILABLE;
  516. if ((dwMask & CFE_SUBSCRIPT) != 0)
  517. {
  518. if ((dwMask & CFM_SUBSCRIPT) && (dwEffects & CFE_SUBSCRIPT))
  519. uProp = UI_FONTVERTICALPOSITION_SUBSCRIPT;
  520. else
  521. uProp = UI_FONTVERTICALPOSITION_SUPERSCRIPT;
  522. }
  523. else if ((dwMask & CFM_OFFSET) != 0)
  524. {
  525. if (yOffset > 0)
  526. uProp = UI_FONTVERTICALPOSITION_SUPERSCRIPT;
  527. else if (yOffset < 0)
  528. uProp = UI_FONTVERTICALPOSITION_SUBSCRIPT;
  529. }
  530. SetPropertyVal(UI_PKEY_FontProperties_VerticalPositioning, uProp, &var);
  531. pStore->SetValue(UI_PKEY_FontProperties_VerticalPositioning, var);
  532. }
  533. void PutFace(IPropertyStore* pStore)
  534. {
  535. PROPVARIANT var;
  536. SetPropertyVal(UI_PKEY_FontProperties_Family,
  537. dwMask & CFM_FACE ? szFaceName : L"", &var);
  538. pStore->SetValue(UI_PKEY_FontProperties_Family, var);
  539. }
  540. void PutSize(IPropertyStore* pStore)
  541. {
  542. PROPVARIANT var;
  543. DECIMAL decVal;
  544. if ((dwMask & CFM_SIZE) != 0)
  545. VarDecFromR8((DOUBLE)yHeight / TWIPS_PER_POINT, &decVal);
  546. else
  547. VarDecFromI4(0, &decVal);
  548. SetPropertyVal(UI_PKEY_FontProperties_Size, &decVal, &var);
  549. pStore->SetValue(UI_PKEY_FontProperties_Size, var);
  550. }
  551. void PutColor(IPropertyStore* pStore)
  552. {
  553. if ((dwMask & CFM_COLOR) != 0)
  554. {
  555. if ((dwEffects & CFE_AUTOCOLOR) == 0)
  556. {
  557. SetPropertyVal(UI_PKEY_FontProperties_ForegroundColorType, UI_SWATCHCOLORTYPE_RGB, &propvar);
  558. pStore->SetValue(UI_PKEY_FontProperties_ForegroundColorType, propvar);
  559. SetPropertyVal(UI_PKEY_FontProperties_ForegroundColor, crTextColor, &propvar);
  560. pStore->SetValue(UI_PKEY_FontProperties_ForegroundColor, propvar);
  561. }
  562. else
  563. {
  564. SetPropertyVal(UI_PKEY_FontProperties_ForegroundColorType, UI_SWATCHCOLORTYPE_AUTOMATIC, &propvar);
  565. pStore->SetValue(UI_PKEY_FontProperties_ForegroundColorType, propvar);
  566. }
  567. }
  568. }
  569. void PutBackColor(IPropertyStore* pStore)
  570. {
  571. if (((dwMask & CFM_BACKCOLOR) != 0) && ((dwEffects & CFE_AUTOBACKCOLOR) == 0))
  572. {
  573. SetPropertyVal(UI_PKEY_FontProperties_BackgroundColorType, UI_SWATCHCOLORTYPE_RGB, &propvar);
  574. pStore->SetValue(UI_PKEY_FontProperties_BackgroundColorType, propvar);
  575. SetPropertyVal(UI_PKEY_FontProperties_BackgroundColor, crBackColor, &propvar);
  576. pStore->SetValue(UI_PKEY_FontProperties_BackgroundColor, propvar);
  577. }
  578. else
  579. {
  580. SetPropertyVal(UI_PKEY_FontProperties_BackgroundColorType, UI_SWATCHCOLORTYPE_NOCOLOR, &propvar);
  581. pStore->SetValue(UI_PKEY_FontProperties_BackgroundColorType, propvar);
  582. }
  583. }
  584. };
  585. // IUIImage helper
  586. //
  587. inline IUIImage* GetImage(HBITMAP hbm, UI_OWNERSHIP owner)
  588. {
  589. ATLASSERT(hbm);
  590. IUIImage* pIUII = NULL;
  591. ATL::CComPtr<IUIImageFromBitmap> pIFB;
  592. if SUCCEEDED(pIFB.CoCreateInstance(CLSID_UIRibbonImageFromBitmapFactory))
  593. ATLVERIFY(SUCCEEDED(pIFB->CreateImage(hbm, owner, &pIUII)));
  594. return pIUII;
  595. }
  596. ///////////////////////////////////////////////////////////////////////////////
  597. // Ribbon control classes
  598. // RibbonUI::ICtrl abstract interface of RibbonUI::CRibbonImpl and all RibbonUI control classes
  599. //
  600. struct ICtrl
  601. {
  602. virtual HRESULT DoExecute(UINT nCmdID, UI_EXECUTIONVERB verb,
  603. const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue,
  604. IUISimplePropertySet* pCommandExecutionProperties) = 0;
  605. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  606. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue) = 0;
  607. };
  608. // RibbonUI::CtrlImpl base class for all ribbon controls
  609. //
  610. template <class T, UINT t_ID>
  611. class ATL_NO_VTABLE CtrlImpl : public ICtrl
  612. {
  613. protected:
  614. T* m_pWndRibbon;
  615. public:
  616. typedef T WndRibbon;
  617. CtrlImpl() : m_pWndRibbon(T::pWndRibbon)
  618. { }
  619. virtual ~CtrlImpl()
  620. { }
  621. WndRibbon& GetWndRibbon()
  622. {
  623. return *m_pWndRibbon;
  624. }
  625. static WORD GetID()
  626. {
  627. return t_ID;
  628. }
  629. Text m_sTxt[5];
  630. // Operations
  631. HRESULT Invalidate()
  632. {
  633. return GetWndRibbon().InvalidateCtrl(GetID());
  634. }
  635. HRESULT Invalidate(REFPROPERTYKEY key, UI_INVALIDATIONS flags = UI_INVALIDATIONS_PROPERTY)
  636. {
  637. return GetWndRibbon().InvalidateProperty(GetID(), key, flags);
  638. }
  639. HRESULT SetText(REFPROPERTYKEY key, LPCWSTR sTxt, bool bUpdate = false)
  640. {
  641. ATLASSERT((k_(key) <= k_TooltipTitle) && (k_(key) >= k_LabelDescription));
  642. m_sTxt[k_(key) - k_LabelDescription] = sTxt;
  643. return bUpdate ?
  644. GetWndRibbon().InvalidateProperty(GetID(), key) :
  645. S_OK;
  646. }
  647. // Implementation
  648. template <typename V>
  649. HRESULT SetProperty(REFPROPERTYKEY key, V val)
  650. {
  651. return GetWndRibbon().SetProperty(GetID(), key, val);
  652. }
  653. HRESULT OnGetText(REFPROPERTYKEY key, PROPVARIANT* ppv)
  654. {
  655. ATLASSERT((k_(key) <= k_TooltipTitle) && (k_(key) >= k_LabelDescription));
  656. const INT iText = k_(key) - k_LabelDescription;
  657. if (m_sTxt[iText].IsEmpty())
  658. if (LPCWSTR sText = GetWndRibbon().OnRibbonQueryText(GetID(), key))
  659. m_sTxt[iText] = sText;
  660. return !m_sTxt[iText].IsEmpty() ?
  661. SetPropertyVal(key, (LPCWSTR)m_sTxt[iText], ppv) :
  662. S_OK;
  663. }
  664. virtual HRESULT DoExecute(UINT nCmdID, UI_EXECUTIONVERB verb,
  665. const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue,
  666. IUISimplePropertySet* pCommandExecutionProperties)
  667. {
  668. ATLASSERT(nCmdID == t_ID);
  669. return GetWndRibbon().DoExecute(nCmdID, verb, key, ppropvarValue, pCommandExecutionProperties);
  670. }
  671. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  672. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  673. {
  674. ATLASSERT(nCmdID == t_ID);
  675. const INT iMax = k_TooltipTitle - k_LabelDescription;
  676. const INT iVal = k_(key) - k_LabelDescription;
  677. return (iVal <= iMax) && (iVal >= 0) ?
  678. OnGetText(key, ppropvarNewValue) :
  679. GetWndRibbon().DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  680. }
  681. };
  682. // CommandCtrlImpl base class for most ribbon controls
  683. //
  684. template <class T, UINT t_ID>
  685. class CommandCtrlImpl : public CtrlImpl<T, t_ID>
  686. {
  687. public:
  688. CBitmap m_hbm[4];
  689. HRESULT SetImage(REFPROPERTYKEY key, HBITMAP hbm, bool bUpdate = false)
  690. {
  691. ATLASSERT((k_(key) <= k_SmallHighContrastImage) && (k_(key) >= k_LargeImage));
  692. m_hbm[k_(key) - k_LargeImage].Attach(hbm);
  693. return bUpdate ?
  694. this->GetWndRibbon().InvalidateProperty(this->GetID(), key) :
  695. S_OK;
  696. }
  697. HRESULT OnGetImage(REFPROPERTYKEY key, PROPVARIANT* ppv)
  698. {
  699. ATLASSERT((k_(key) <= k_SmallHighContrastImage) && (k_(key) >= k_LargeImage));
  700. const INT iImage = k_(key) - k_LargeImage;
  701. if (m_hbm[iImage].IsNull())
  702. m_hbm[iImage] = this->GetWndRibbon().OnRibbonQueryImage(this->GetID(), key);
  703. return m_hbm[iImage].IsNull() ?
  704. E_NOTIMPL :
  705. SetPropertyVal(key, GetImage(m_hbm[iImage], UI_OWNERSHIP_COPY), ppv);
  706. }
  707. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  708. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  709. {
  710. ATLASSERT (nCmdID == this->GetID());
  711. return (k_(key) <= k_SmallHighContrastImage) && (k_(key) >= k_LargeImage) ?
  712. OnGetImage(key, ppropvarNewValue) :
  713. CtrlImpl<T, t_ID>::DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  714. }
  715. };
  716. ///////////////////////////////////////////////////////////////////////////////
  717. // Ribbon collection base classes
  718. // ItemProperty class: ribbon callback for each item in a collection
  719. //
  720. #pragma warning(push)
  721. #pragma warning(disable: 4512) // assignment operator could not be generated
  722. template <class TCollection>
  723. class ItemProperty : public IUISimplePropertySet
  724. {
  725. public:
  726. ItemProperty(UINT i, TCollection* pCollection) : m_Index(i), m_pCollection(pCollection)
  727. { }
  728. const UINT m_Index;
  729. TCollection* m_pCollection;
  730. // IUISimplePropertySet method.
  731. STDMETHODIMP GetValue(REFPROPERTYKEY key, PROPVARIANT *value)
  732. {
  733. return m_pCollection->OnGetItem(m_Index, key, value);
  734. }
  735. // IUnknown methods.
  736. STDMETHODIMP_(ULONG) AddRef()
  737. {
  738. return 1;
  739. }
  740. STDMETHODIMP_(ULONG) Release()
  741. {
  742. return 1;
  743. }
  744. STDMETHODIMP QueryInterface(REFIID iid, void** ppv)
  745. {
  746. if ((iid == __uuidof(IUnknown)) || (iid == __uuidof(IUISimplePropertySet)))
  747. {
  748. *ppv = this;
  749. return S_OK;
  750. }
  751. else
  752. {
  753. return E_NOINTERFACE;
  754. }
  755. }
  756. };
  757. #pragma warning(pop)
  758. // CollectionImplBase: base class for all RibbonUI collections
  759. //
  760. template <class TCollection, size_t t_size>
  761. class CollectionImplBase
  762. {
  763. typedef CollectionImplBase<TCollection, t_size> thisClass;
  764. public:
  765. CollectionImplBase()
  766. {
  767. memset(&m_apItems, 0, sizeof(m_apItems));
  768. for (int i = 0; i < t_size; i++)
  769. m_apItems[i] = new ItemProperty<TCollection>(i, static_cast<TCollection*>(this));
  770. }
  771. ~CollectionImplBase()
  772. {
  773. for (int i = 0; i < t_size; i++)
  774. delete m_apItems[i];
  775. }
  776. // Data members
  777. ItemProperty<TCollection>* m_apItems[t_size];
  778. };
  779. // CollectionImpl: handles categories and collecton resizing
  780. //
  781. template <class TCtrl, size_t t_items, size_t t_categories>
  782. class CollectionImpl : public CollectionImplBase<CollectionImpl<TCtrl, t_items, t_categories>, t_items + t_categories>
  783. {
  784. typedef CollectionImpl<TCtrl, t_items, t_categories> thisClass;
  785. public:
  786. typedef thisClass Collection;
  787. CollectionImpl() : m_size(t_items)
  788. {
  789. ::FillMemory(m_auItemCat, sizeof(m_auItemCat), 0xff); // UI_COLLECTION_INVALIDINDEX
  790. }
  791. UINT32 m_auItemCat[t_items];
  792. Text m_asCatName[__max(t_categories, 1)];
  793. size_t m_size;
  794. // Operations
  795. HRESULT SetItemCategory(UINT uItem, UINT uCat, bool bUpdate = false)
  796. {
  797. ATLASSERT((uItem < t_items) && (uCat < t_categories));
  798. m_auItemCat[uItem] = uCat;
  799. return bUpdate ? InvalidateItems() : S_OK;
  800. }
  801. HRESULT SetCategoryText(UINT uCat, LPCWSTR sText, bool bUpdate = false)
  802. {
  803. ATLASSERT(uCat < t_categories);
  804. m_asCatName[uCat] = sText;
  805. return bUpdate ? InvalidateCategories() : S_OK;
  806. }
  807. HRESULT Resize(size_t size, bool bUpdate = false)
  808. {
  809. ATLASSERT(size <= t_items);
  810. m_size = size;
  811. return bUpdate ? InvalidateItems() : S_OK;
  812. }
  813. // Implementation
  814. HRESULT OnGetItem(UINT uIndex, REFPROPERTYKEY key, PROPVARIANT *value)
  815. {
  816. ATLASSERT(uIndex < t_items + t_categories);
  817. TCtrl* pCtrl = static_cast<TCtrl*>(this);
  818. return uIndex < t_items ?
  819. pCtrl->DoGetItem(uIndex, key, value) :
  820. pCtrl->DoGetCategory(uIndex - t_items, key, value);
  821. }
  822. HRESULT DoGetItem(UINT uItem, REFPROPERTYKEY key, PROPVARIANT *value)
  823. {
  824. ATLASSERT(k_(key) == k_CategoryId);
  825. UINT32 uCat = UI_COLLECTION_INVALIDINDEX;
  826. if (t_categories != 0)
  827. {
  828. if (m_auItemCat[uItem] == UI_COLLECTION_INVALIDINDEX)
  829. {
  830. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  831. m_auItemCat[uItem] = ribbon.OnRibbonQueryItemCategory(TCtrl::GetID(), uItem);
  832. }
  833. uCat = m_auItemCat[uItem];
  834. }
  835. return SetPropertyVal(key, uCat, value);
  836. }
  837. HRESULT DoGetCategory(UINT uCat, REFPROPERTYKEY key, PROPVARIANT *value)
  838. {
  839. HRESULT hr = S_OK;
  840. switch (k_(key))
  841. {
  842. case k_Label:
  843. if (m_asCatName[uCat].IsEmpty())
  844. {
  845. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  846. m_asCatName[uCat] = ribbon.OnRibbonQueryCategoryText(TCtrl::GetID(), uCat);
  847. }
  848. hr = SetPropertyVal(key, (LPCWSTR)m_asCatName[uCat], value);
  849. break;
  850. case k_CategoryId:
  851. hr = SetPropertyVal(key, uCat, value);
  852. break;
  853. default:
  854. ATLASSERT(FALSE);
  855. break;
  856. }
  857. return hr;
  858. }
  859. HRESULT InvalidateItems()
  860. {
  861. return static_cast<TCtrl*>(this)->Invalidate(UI_PKEY_ItemsSource);
  862. }
  863. HRESULT InvalidateCategories()
  864. {
  865. return static_cast<TCtrl*>(this)->Invalidate(UI_PKEY_Categories);
  866. }
  867. HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  868. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* /*ppropvarNewValue*/)
  869. {
  870. ATLASSERT(nCmdID == TCtrl::GetID());
  871. (void)nCmdID; // avoid level 4 warning
  872. HRESULT hr = E_NOTIMPL;
  873. switch (k_(key))
  874. {
  875. case k_ItemsSource:
  876. {
  877. ATL::CComQIPtr<IUICollection> pIUICollection(ppropvarCurrentValue->punkVal);
  878. ATLASSERT(pIUICollection);
  879. hr = pIUICollection->Clear();
  880. for (UINT i = 0; i < m_size; i++)
  881. {
  882. if FAILED(hr = pIUICollection->Add(this->m_apItems[i]))
  883. break;
  884. }
  885. ATLASSERT(SUCCEEDED(hr));
  886. }
  887. break;
  888. case k_Categories:
  889. if (t_categories != 0)
  890. {
  891. ATL::CComQIPtr<IUICollection> pIUICategory(ppropvarCurrentValue->punkVal);
  892. ATLASSERT(pIUICategory.p);
  893. hr = pIUICategory->Clear();
  894. for (UINT i = t_items; i < (t_items + t_categories); i++)
  895. {
  896. if FAILED(hr = pIUICategory->Add(this->m_apItems[i]))
  897. break;
  898. }
  899. ATLASSERT(SUCCEEDED(hr));
  900. }
  901. break;
  902. }
  903. return hr;
  904. }
  905. };
  906. // TextCollectionImpl: handles item labels and selection
  907. //
  908. template <class TCtrl, size_t t_items, size_t t_categories = 0>
  909. class TextCollectionImpl : public CollectionImpl<TCtrl, t_items, t_categories>
  910. {
  911. typedef TextCollectionImpl<TCtrl, t_items, t_categories> thisClass;
  912. public:
  913. typedef thisClass TextCollection;
  914. TextCollectionImpl() : m_uSelected(UI_COLLECTION_INVALIDINDEX)
  915. { }
  916. Text m_asText[t_items];
  917. UINT m_uSelected;
  918. // Operations
  919. HRESULT SetItemText(UINT uItem, LPCWSTR sText, bool bUpdate = false)
  920. {
  921. ATLASSERT(uItem < t_items);
  922. m_asText[uItem] = sText;
  923. return bUpdate ? this->InvalidateItems() : S_OK;
  924. }
  925. UINT GetSelected()
  926. {
  927. return m_uSelected;
  928. }
  929. HRESULT Select(UINT uItem, bool bUpdate = false)
  930. {
  931. ATLASSERT((uItem < t_items) || (uItem == UI_COLLECTION_INVALIDINDEX));
  932. m_uSelected = uItem;
  933. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  934. return bUpdate ?
  935. ribbon.SetProperty(TCtrl::GetID(), UI_PKEY_SelectedItem, uItem) :
  936. S_OK;
  937. }
  938. // Implementation
  939. HRESULT DoGetItem(UINT uItem, REFPROPERTYKEY key, PROPVARIANT *value)
  940. {
  941. ATLASSERT(uItem < t_items);
  942. if (k_(key) == k_Label)
  943. {
  944. if (m_asText[uItem].IsEmpty())
  945. {
  946. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  947. m_asText[uItem] = ribbon.OnRibbonQueryItemText(TCtrl::GetID(), uItem);
  948. }
  949. return SetPropertyVal(key, (LPCWSTR)m_asText[uItem], value);
  950. }
  951. else
  952. {
  953. return CollectionImpl<TCtrl, t_items, t_categories>::Collection::DoGetItem(uItem, key, value);
  954. }
  955. }
  956. HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  957. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  958. {
  959. ATLASSERT(nCmdID == TCtrl::GetID());
  960. if (k_(key) == k_SelectedItem)
  961. {
  962. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  963. UINT uSel = UI_COLLECTION_INVALIDINDEX;
  964. if ((m_uSelected == UI_COLLECTION_INVALIDINDEX) &&
  965. ribbon.OnRibbonQuerySelectedItem(TCtrl::GetID(), uSel))
  966. m_uSelected = uSel;
  967. return SetPropertyVal(key, m_uSelected, ppropvarNewValue);
  968. }
  969. else
  970. {
  971. return CollectionImpl<TCtrl, t_items, t_categories>::Collection::DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  972. }
  973. }
  974. };
  975. // ItemCollectionImpl: handles item image
  976. //
  977. template <class TCtrl, size_t t_items, size_t t_categories = 0>
  978. class ItemCollectionImpl : public TextCollectionImpl<TCtrl, t_items, t_categories>
  979. {
  980. typedef ItemCollectionImpl<TCtrl, t_items, t_categories> thisClass;
  981. public:
  982. typedef thisClass ItemCollection;
  983. CBitmap m_aBitmap[t_items];
  984. // Operations
  985. HRESULT SetItemImage(UINT uIndex, HBITMAP hbm, bool bUpdate = false)
  986. {
  987. ATLASSERT(uIndex < t_items);
  988. m_aBitmap[uIndex] = hbm;
  989. return bUpdate ? this->InvalidateItems() : S_OK;
  990. }
  991. // Implementation
  992. HRESULT DoGetItem(UINT uItem, REFPROPERTYKEY key, PROPVARIANT *value)
  993. {
  994. ATLASSERT(uItem < t_items);
  995. if (k_(key) == k_ItemImage)
  996. {
  997. if (m_aBitmap[uItem].IsNull())
  998. {
  999. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  1000. m_aBitmap[uItem] = ribbon.OnRibbonQueryItemImage(TCtrl::GetID(), uItem);
  1001. }
  1002. return m_aBitmap[uItem].IsNull() ?
  1003. E_NOTIMPL :
  1004. SetPropertyVal(key, GetImage(m_aBitmap[uItem], UI_OWNERSHIP_COPY), value);
  1005. }
  1006. else
  1007. {
  1008. return TextCollectionImpl<TCtrl, t_items, t_categories>::TextCollection::DoGetItem(uItem, key, value);
  1009. }
  1010. }
  1011. };
  1012. // ComboCollectionImpl: handles combo text
  1013. //
  1014. template <class TCtrl, size_t t_items, size_t t_categories = 0>
  1015. class ComboCollectionImpl : public ItemCollectionImpl<TCtrl, t_items, t_categories>
  1016. {
  1017. typedef ComboCollectionImpl<TCtrl, t_items, t_categories> thisClass;
  1018. public:
  1019. typedef thisClass ComboCollection;
  1020. // Operations
  1021. HRESULT SetComboText(LPCWSTR sText)
  1022. {
  1023. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  1024. return ribbon.IsRibbonUI() ?
  1025. ribbon.SetProperty(TCtrl::GetID(), UI_PKEY_StringValue, sText) :
  1026. S_OK;
  1027. }
  1028. LPCWSTR GetComboText()
  1029. {
  1030. static WCHAR sCombo[RIBBONUI_MAX_TEXT] = {};
  1031. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  1032. PROPVARIANT var;
  1033. if (ribbon.IsRibbonUI())
  1034. {
  1035. HRESULT hr = ribbon.GetIUIFrameworkPtr()->GetUICommandProperty(TCtrl::GetID(), UI_PKEY_StringValue, &var);
  1036. ATLASSERT(SUCCEEDED(hr));
  1037. hr = PropVariantToString(var, sCombo, RIBBONUI_MAX_TEXT);
  1038. ATLASSERT(SUCCEEDED(hr));
  1039. return sCombo;
  1040. }
  1041. return NULL;
  1042. }
  1043. };
  1044. // CommandCollectionImpl: handles RibbonUI command collection controls
  1045. //
  1046. template <class TCtrl, size_t t_items, size_t t_categories = 0>
  1047. class CommandCollectionImpl : public CollectionImpl<TCtrl, t_items, t_categories>
  1048. {
  1049. typedef CommandCollectionImpl<TCtrl, t_items, t_categories> thisClass;
  1050. public:
  1051. typedef thisClass CommandCollection;
  1052. CommandCollectionImpl()
  1053. {
  1054. ::ZeroMemory(m_auCmd, sizeof(m_auCmd));
  1055. ::ZeroMemory(m_aCmdType, sizeof(m_aCmdType));
  1056. }
  1057. UINT32 m_auCmd[t_items];
  1058. BYTE m_aCmdType[t_items];
  1059. // Operations
  1060. HRESULT SetItemCommand(UINT uItem, UINT32 uCommandID, bool bUpdate = false)
  1061. {
  1062. ATLASSERT(uItem < t_items);
  1063. if (uCommandID == m_auCmd[uItem])
  1064. return S_OK;
  1065. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  1066. m_auCmd[uItem] = uCommandID;
  1067. if (uCommandID != 0)
  1068. ribbon.UIAddRibbonElement(uCommandID);
  1069. return bUpdate ? this->InvalidateItems() : S_OK;
  1070. }
  1071. HRESULT SetItemCommandType(UINT uItem, UI_COMMANDTYPE type, bool bUpdate = false)
  1072. {
  1073. ATLASSERT(uItem < t_items);
  1074. m_aCmdType[uItem] = (BYTE)type;
  1075. return bUpdate ? this->InvalidateItems() : S_OK;
  1076. }
  1077. // Implementation
  1078. HRESULT DoGetItem(UINT uItem, REFPROPERTYKEY key, PROPVARIANT *value)
  1079. {
  1080. ATLASSERT(uItem < t_items);
  1081. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  1082. HRESULT hr = E_FAIL;
  1083. switch (k_(key))
  1084. {
  1085. case k_CommandId:
  1086. if (m_auCmd[uItem] == 0)
  1087. SetItemCommand(uItem, ribbon.OnRibbonQueryItemCommand(TCtrl::GetID(), uItem));
  1088. hr = SetPropertyVal(key, m_auCmd[uItem], value);
  1089. break;
  1090. case k_CommandType:
  1091. if (m_aCmdType[uItem] == UI_COMMANDTYPE_UNKNOWN)
  1092. SetItemCommandType(uItem, ribbon.OnRibbonQueryItemCommandType(TCtrl::GetID(), uItem));
  1093. hr = SetPropertyVal(key, UINT32(m_aCmdType[uItem]), value);
  1094. break;
  1095. case k_CategoryId:
  1096. default:
  1097. hr = CollectionImpl<TCtrl, t_items, t_categories>::Collection::DoGetItem(uItem, key, value);
  1098. break;
  1099. }
  1100. return hr;
  1101. }
  1102. HRESULT Select(UINT /*uItem*/, bool /*bUpdate*/ = false)
  1103. {
  1104. ATLASSERT(FALSE);
  1105. return S_OK;
  1106. }
  1107. };
  1108. // SimpleCollectionImpl: collection class for ribbon simple collection controls
  1109. //
  1110. template <class TCtrl, size_t t_size, UI_COMMANDTYPE t_CommandType = UI_COMMANDTYPE_ACTION>
  1111. class SimpleCollectionImpl : public CollectionImplBase<SimpleCollectionImpl<TCtrl, t_size>, t_size>
  1112. {
  1113. typedef SimpleCollectionImpl<TCtrl, t_size, t_CommandType> thisClass;
  1114. public:
  1115. typedef CollectionImplBase<thisClass, t_size> CollectionBase;
  1116. typedef thisClass SimpleCollection;
  1117. // Implementation
  1118. HRESULT OnGetItem(UINT uItem, REFPROPERTYKEY key, PROPVARIANT *value)
  1119. {
  1120. ATLASSERT(uItem < t_size);
  1121. typename TCtrl::WndRibbon& ribbon = static_cast<TCtrl*>(this)->GetWndRibbon();
  1122. HRESULT hr = E_NOTIMPL;
  1123. switch (k_(key))
  1124. {
  1125. case k_ItemImage:
  1126. if (HBITMAP hbm = ribbon.DefRibbonQueryItemImage(TCtrl::GetID(), uItem))
  1127. hr = SetPropertyVal(key, GetImage(hbm, UI_OWNERSHIP_TRANSFER), value);
  1128. break;
  1129. case k_Label:
  1130. if (LPCWSTR sText = ribbon.DefRibbonQueryItemText(TCtrl::GetID(), uItem))
  1131. hr = SetPropertyVal(key, (LPCWSTR)sText, value);
  1132. break;
  1133. case k_CommandType:
  1134. hr = SetPropertyVal(key, t_CommandType, value);
  1135. break;
  1136. case k_CommandId:
  1137. hr = SetPropertyVal(key, ribbon.DefRibbonQueryItemCommand(TCtrl::GetID(), uItem), value);
  1138. break;
  1139. case k_CategoryId:
  1140. hr = SetPropertyVal(key, UI_COLLECTION_INVALIDINDEX, value);
  1141. break;
  1142. default:
  1143. ATLASSERT(FALSE);
  1144. break;
  1145. }
  1146. return hr;
  1147. }
  1148. };
  1149. ///////////////////////////////////////////////////////////////////////////////
  1150. // Ribbon collection control classes
  1151. // CollectionCtrlImpl: specializable class for ribbon collection controls
  1152. //
  1153. template <class T, UINT t_ID, class TCollection>
  1154. class CollectionCtrlImpl : public CommandCtrlImpl<T, t_ID>, public TCollection
  1155. {
  1156. typedef CollectionCtrlImpl<T, t_ID, TCollection> thisClass;
  1157. public:
  1158. typedef CommandCtrlImpl<T, t_ID> CommandCtrl;
  1159. typedef TCollection Collection;
  1160. // Implementation
  1161. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  1162. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  1163. {
  1164. ATLASSERT(nCmdID == this->GetID());
  1165. ATLASSERT(ppropvarNewValue);
  1166. HRESULT hr = Collection::DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  1167. if FAILED(hr)
  1168. hr = CommandCtrl::DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  1169. return hr;
  1170. }
  1171. virtual HRESULT DoExecute(UINT nCmdID, UI_EXECUTIONVERB verb,
  1172. const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue,
  1173. IUISimplePropertySet* /*pCommandExecutionProperties*/)
  1174. {
  1175. ATLASSERT (nCmdID == this->GetID());
  1176. (void)nCmdID; // avoid level4 warning
  1177. if (key == NULL) // gallery button pressed
  1178. {
  1179. this->GetWndRibbon().OnRibbonItemSelected(this->GetID(), UI_EXECUTIONVERB_EXECUTE, UI_COLLECTION_INVALIDINDEX);
  1180. return S_OK;
  1181. }
  1182. ATLASSERT(k_(*key) == k_SelectedItem);
  1183. ATLASSERT(ppropvarValue);
  1184. HRESULT hr = S_OK;
  1185. UINT32 uSel = 0xffff;
  1186. hr = UIPropertyToUInt32(*key, *ppropvarValue, &uSel);
  1187. if (SUCCEEDED(hr))
  1188. {
  1189. if (this->GetWndRibbon().OnRibbonItemSelected(this->GetID(), verb, uSel))
  1190. TCollection::Select(uSel);
  1191. }
  1192. return hr;
  1193. }
  1194. };
  1195. // ToolbarGalleryCtrlImpl: base class for ribbon toolbar gallery controls
  1196. //
  1197. template <class T, UINT t_ID, UINT t_idTB, size_t t_size>
  1198. class ToolbarGalleryCtrlImpl : public CollectionCtrlImpl<T, t_ID, CommandCollectionImpl<ToolbarGalleryCtrlImpl<T, t_ID, t_idTB, t_size>, t_size>>
  1199. {
  1200. public:
  1201. ToolbarGalleryCtrlImpl()
  1202. {
  1203. CResource tbres;
  1204. ATLVERIFY(tbres.Load(RT_TOOLBAR, t_idTB));
  1205. _AtlToolBarData* pData = (_AtlToolBarData*)tbres.Lock();
  1206. ATLASSERT(pData);
  1207. ATLASSERT(pData->wVersion == 1);
  1208. WORD* pItems = pData->items();
  1209. INT j = 0;
  1210. for (int i = 0; (i < pData->wItemCount) && (j < t_size); i++)
  1211. {
  1212. if (pItems[i] != 0)
  1213. {
  1214. this->m_aCmdType[j] = UI_COMMANDTYPE_ACTION;
  1215. this->m_auCmd[j++] = pItems[i];
  1216. }
  1217. }
  1218. if (j < t_size)
  1219. this->Resize(j);
  1220. }
  1221. HRESULT DoGetItem(UINT uItem, REFPROPERTYKEY key, PROPVARIANT *value)
  1222. {
  1223. ATLASSERT(uItem < this->m_size);
  1224. ATLASSERT(this->m_auCmd[uItem]);
  1225. HRESULT hr = E_FAIL;
  1226. switch (k_(key))
  1227. {
  1228. case k_CommandId:
  1229. hr = SetPropertyVal(key, this->m_auCmd[uItem], value);
  1230. break;
  1231. case k_CommandType:
  1232. hr = SetPropertyVal(key, UINT32(this->m_aCmdType[uItem]), value);
  1233. break;
  1234. case k_CategoryId:
  1235. hr = SetPropertyVal(key, UI_COLLECTION_INVALIDINDEX, value);
  1236. break;
  1237. default:
  1238. ATLASSERT(FALSE);
  1239. break;
  1240. }
  1241. return hr;
  1242. }
  1243. };
  1244. // SimpleCollectionCtrlImpl: base class for simple gallery and listbox controls
  1245. //
  1246. template <class T, UINT t_ID, size_t t_size, UI_COMMANDTYPE t_CommandType = UI_COMMANDTYPE_ACTION>
  1247. class SimpleCollectionCtrlImpl :
  1248. public CommandCtrlImpl<T, t_ID>,
  1249. public SimpleCollectionImpl<SimpleCollectionCtrlImpl<T, t_ID, t_size, t_CommandType>, t_size, t_CommandType>
  1250. {
  1251. typedef SimpleCollectionCtrlImpl<T, t_ID, t_size, t_CommandType> thisClass;
  1252. public:
  1253. typedef thisClass SimpleCollection;
  1254. SimpleCollectionCtrlImpl() : m_uSelected(0)
  1255. { }
  1256. UINT m_uSelected;
  1257. HRESULT Select(UINT uItem, bool bUpdate = false)
  1258. {
  1259. ATLASSERT((uItem < t_size) || (uItem == UI_COLLECTION_INVALIDINDEX));
  1260. m_uSelected = uItem;
  1261. return bUpdate ?
  1262. this->GetWndRibbon().SetProperty(this->GetID(), UI_PKEY_SelectedItem, uItem) :
  1263. S_OK;
  1264. }
  1265. // Implementation
  1266. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  1267. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  1268. {
  1269. ATLASSERT(nCmdID == this->GetID());
  1270. ATLASSERT(ppropvarNewValue != NULL);
  1271. HRESULT hr = S_OK;
  1272. switch (k_(key))
  1273. {
  1274. case k_ItemsSource:
  1275. {
  1276. ATL::CComQIPtr<IUICollection> pIUICollection(ppropvarCurrentValue->punkVal);
  1277. ATLASSERT(pIUICollection.p);
  1278. hr = pIUICollection->Clear();
  1279. for (UINT i = 0; i < t_size; i++)
  1280. {
  1281. if FAILED(hr = pIUICollection->Add(this->m_apItems[i]))
  1282. break;
  1283. }
  1284. ATLASSERT(SUCCEEDED(hr));
  1285. }
  1286. break;
  1287. case k_SelectedItem:
  1288. hr = SetPropertyVal(UI_PKEY_SelectedItem, m_uSelected, ppropvarNewValue);
  1289. break;
  1290. default:
  1291. hr = CommandCtrlImpl<T, t_ID>::DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  1292. break;
  1293. }
  1294. return hr;
  1295. }
  1296. virtual HRESULT DoExecute(UINT nCmdID, UI_EXECUTIONVERB verb,
  1297. const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue,
  1298. IUISimplePropertySet* /*pCommandExecutionProperties*/)
  1299. {
  1300. ATLASSERT (nCmdID == this->GetID());
  1301. (void)nCmdID; // avoid level 4 warning
  1302. HRESULT hr = S_OK;
  1303. if (key == NULL) // gallery button pressed
  1304. {
  1305. this->GetWndRibbon().OnRibbonItemSelected(this->GetID(), UI_EXECUTIONVERB_EXECUTE, UI_COLLECTION_INVALIDINDEX);
  1306. return hr;
  1307. }
  1308. ATLASSERT(k_(*key) == k_SelectedItem);
  1309. ATLASSERT(ppropvarValue);
  1310. if SUCCEEDED(hr = UIPropertyToUInt32(*key, *ppropvarValue, &m_uSelected))
  1311. this->GetWndRibbon().OnRibbonItemSelected(this->GetID(), verb, m_uSelected);
  1312. return hr;
  1313. }
  1314. };
  1315. // RecentItemsCtrlImpl
  1316. //
  1317. template <class T, UINT t_ID, class TDocList = CRecentDocumentList>
  1318. class RecentItemsCtrlImpl :
  1319. public CtrlImpl<T, t_ID>,
  1320. public CollectionImplBase<RecentItemsCtrlImpl<T, t_ID, TDocList>, TDocList::m_nMaxEntries_Max>,
  1321. public TDocList
  1322. {
  1323. typedef RecentItemsCtrlImpl<T, t_ID, TDocList> thisClass;
  1324. public:
  1325. typedef thisClass RecentItems;
  1326. // Implementation
  1327. HRESULT OnGetItem(UINT uItem, REFPROPERTYKEY key, PROPVARIANT *value)
  1328. {
  1329. ATLASSERT((INT)uItem < this->GetMaxEntries());
  1330. LPCWSTR sPath = this->m_arrDocs[uItem].szDocName;
  1331. HRESULT hr = E_NOTIMPL;
  1332. switch (k_(key))
  1333. {
  1334. case k_Label:
  1335. hr = SetPropertyVal(key, this->GetWndRibbon().OnRibbonQueryRecentItemName(sPath), value);
  1336. break;
  1337. case k_LabelDescription:
  1338. hr = SetPropertyVal(key, sPath, value);
  1339. break;
  1340. default:
  1341. ATLASSERT(FALSE);
  1342. break;
  1343. }
  1344. return hr;
  1345. }
  1346. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  1347. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  1348. {
  1349. ATLASSERT(nCmdID == this->GetID());
  1350. ATLASSERT(ppropvarNewValue);
  1351. HRESULT hr = S_OK;
  1352. switch (k_(key))
  1353. {
  1354. case k_RecentItems:
  1355. if (SAFEARRAY* psa = SafeArrayCreateVector(VT_UNKNOWN, 0, this->m_arrDocs.GetSize()))
  1356. {
  1357. const int iLastIndex = this->m_arrDocs.GetSize() - 1;
  1358. for (LONG i = 0; i <= iLastIndex; i++)
  1359. SafeArrayPutElement(psa, &i, this->m_apItems[iLastIndex - i]); // reverse order
  1360. hr = SetPropertyVal(key, psa, ppropvarNewValue);
  1361. SafeArrayDestroy(psa);
  1362. }
  1363. break;
  1364. default:
  1365. hr = CtrlImpl<T, t_ID>::DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  1366. break;
  1367. }
  1368. return hr;
  1369. }
  1370. virtual HRESULT DoExecute(UINT nCmdID, UI_EXECUTIONVERB verb,
  1371. const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue,
  1372. IUISimplePropertySet* /*pCommandExecutionProperties*/)
  1373. {
  1374. ATLASSERT(nCmdID == this->GetID());
  1375. (void)nCmdID; // avoid level 4 warning
  1376. ATLASSERT(verb == UI_EXECUTIONVERB_EXECUTE);
  1377. (void)verb; // avoid level 4 warning
  1378. ATLASSERT((key) && (k_(*key) == k_SelectedItem));
  1379. ATLASSERT(ppropvarValue);
  1380. UINT32 uSel = 0xffff;
  1381. HRESULT hr = UIPropertyToUInt32(*key, *ppropvarValue, &uSel);
  1382. if SUCCEEDED(hr)
  1383. {
  1384. ATLASSERT(uSel < (UINT)this->GetMaxEntries());
  1385. this->GetWndRibbon().DefCommandExecute(ID_FILE_MRU_FIRST + uSel);
  1386. }
  1387. return hr;
  1388. }
  1389. };
  1390. ///////////////////////////////////////////////////////////////////////////////
  1391. // Ribbon stand-alone control classes
  1392. // FontCtrlImpl
  1393. //
  1394. template <class T, UINT t_ID>
  1395. class FontCtrlImpl : public CtrlImpl<T, t_ID>
  1396. {
  1397. public:
  1398. CharFormat m_cf;
  1399. // Implementation
  1400. virtual HRESULT DoExecute(UINT nCmdID, UI_EXECUTIONVERB verb,
  1401. const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue,
  1402. IUISimplePropertySet* pCommandExecutionProperties)
  1403. {
  1404. ATLASSERT (nCmdID == this->GetID());
  1405. (void)nCmdID; // avoid level 4 warning
  1406. ATLASSERT ((key) && (k_(*key) == k_FontProperties));
  1407. (void)key; // avoid level 4 warning
  1408. HRESULT hr = E_INVALIDARG;
  1409. switch (verb)
  1410. {
  1411. case UI_EXECUTIONVERB_PREVIEW:
  1412. case UI_EXECUTIONVERB_EXECUTE:
  1413. ATLASSERT(pCommandExecutionProperties);
  1414. PROPVARIANT propvar;
  1415. if (SUCCEEDED(hr = pCommandExecutionProperties->GetValue(UI_PKEY_FontProperties_ChangedProperties, &propvar)))
  1416. m_cf << ATL::CComQIPtr<IPropertyStore>(propvar.punkVal);
  1417. break;
  1418. case UI_EXECUTIONVERB_CANCELPREVIEW:
  1419. ATLASSERT(ppropvarValue);
  1420. ATL::CComPtr<IPropertyStore> pStore;
  1421. if (SUCCEEDED(hr = UIPropertyToInterface(UI_PKEY_FontProperties, *ppropvarValue, &pStore)))
  1422. m_cf << pStore;
  1423. break;
  1424. }
  1425. if (SUCCEEDED(hr))
  1426. this->GetWndRibbon().OnRibbonFontCtrlExecute(this->GetID(), verb, &m_cf);
  1427. else
  1428. ATLASSERT(FALSE);
  1429. return hr;
  1430. }
  1431. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  1432. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  1433. {
  1434. if ((k_(key) == k_FontProperties) && (this->GetWndRibbon().OnRibbonQueryFont(t_ID, m_cf)))
  1435. {
  1436. ATL::CComQIPtr<IPropertyStore> pStore(ppropvarCurrentValue->punkVal);
  1437. m_cf >> pStore;
  1438. return SetPropertyVal(key, pStore.p, ppropvarNewValue);
  1439. }
  1440. else
  1441. {
  1442. return CtrlImpl<T, t_ID>::DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  1443. }
  1444. }
  1445. };
  1446. // ColorCtrlImpl
  1447. //
  1448. template <class T, UINT t_ID>
  1449. class ColorCtrlImpl : public CommandCtrlImpl<T, t_ID>
  1450. {
  1451. public:
  1452. ColorCtrlImpl() : m_colorType(UI_SWATCHCOLORTYPE_NOCOLOR), m_color(0x800080) /*MAGENTA*/
  1453. { }
  1454. UINT32 m_colorType; // value in UI_SWATCHCOLORTYPE
  1455. COLORREF m_color;
  1456. Text m_sLabels[6]; // k_MoreColorsLabel to k_ThemeColorsCategoryLabel
  1457. ATL::CSimpleArray<COLORREF> m_aColors[2];
  1458. ATL::CSimpleArray<LPCWSTR> m_aTooltips[2];
  1459. // Operations
  1460. HRESULT SetColor(COLORREF color, bool bUpdate = false)
  1461. {
  1462. if (m_colorType != UI_SWATCHCOLORTYPE_RGB)
  1463. SetColorType(UI_SWATCHCOLORTYPE_RGB, bUpdate);
  1464. m_color = color;
  1465. return bUpdate ? this->SetProperty(UI_PKEY_Color, color) : S_OK;
  1466. }
  1467. HRESULT SetColorType(UI_SWATCHCOLORTYPE type, bool bUpdate = false)
  1468. {
  1469. m_colorType = type;
  1470. return bUpdate ? this->SetProperty(UI_PKEY_ColorType, type) : S_OK;
  1471. }
  1472. HRESULT SetColorLabel(REFPROPERTYKEY key, LPCWSTR sLabel, bool bUpdate = false)
  1473. {
  1474. ATLASSERT((k_(key) >= k_ThemeColorsCategoryLabel) && (k_(key) <= k_MoreColorsLabel));
  1475. m_sLabels[k_(key) - k_ThemeColorsCategoryLabel] = sLabel;
  1476. return bUpdate ? this->SetProperty(key, sLabel) : S_OK;
  1477. }
  1478. HRESULT SetColorArray(REFPROPERTYKEY key, COLORREF* pColor, bool bUpdate = false)
  1479. {
  1480. ATLASSERT((k_(key) == k_ThemeColors) || (k_(key) == k_StandardColors));
  1481. const INT ic = k_(key) - k_ThemeColors;
  1482. m_aColors[ic].RemoveAll();
  1483. while (*pColor != 0x800080) /*MAGENTA*/
  1484. m_aColors[ic].Add(*pColor++);
  1485. if (bUpdate)
  1486. {
  1487. PROPVARIANT var;
  1488. if SUCCEEDED(InitPropVariantFromUInt32Vector(m_aColors[ic].GetData(), m_aColors[ic].GetSize(), &var))
  1489. return this->SetProperty(key, var);
  1490. else
  1491. return E_INVALIDARG;
  1492. }
  1493. else
  1494. {
  1495. return S_OK;
  1496. }
  1497. }
  1498. HRESULT SetColorTooltips(REFPROPERTYKEY key, LPCWSTR* ppsTT, bool bUpdate = false)
  1499. {
  1500. ATLASSERT((k_(key) == k_ThemeColorsTooltips) || (k_(key) == k_StandardColorsTooltips));
  1501. const INT ic = k_(key) - k_ThemeColorsTooltips;
  1502. m_aTooltips[ic].RemoveAll();
  1503. while (*ppsTT)
  1504. m_aTooltips[ic].Add(*ppsTT++);
  1505. if (bUpdate)
  1506. {
  1507. PROPVARIANT var;
  1508. if SUCCEEDED(InitPropVariantFromStringVector(m_aTooltips[ic].GetData(), m_aTooltips[ic].GetSize(), &var))
  1509. return this->SetProperty(key, var);
  1510. else
  1511. return E_INVALIDARG;
  1512. }
  1513. else
  1514. {
  1515. return S_OK;
  1516. }
  1517. }
  1518. // Implementation
  1519. virtual HRESULT DoExecute(UINT nCmdID, UI_EXECUTIONVERB verb,
  1520. const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue,
  1521. IUISimplePropertySet* pCommandExecutionProperties)
  1522. {
  1523. ATLASSERT (nCmdID == this->GetID());
  1524. (void)nCmdID; // avoid level 4 warning
  1525. ATLASSERT (key && (k_(*key) == k_ColorType));
  1526. (void)key; // avoid level 4 warning
  1527. ATLASSERT (ppropvarValue);
  1528. HRESULT hr = PropVariantToUInt32(*ppropvarValue, &m_colorType);
  1529. ATLASSERT(SUCCEEDED(hr));
  1530. if (SUCCEEDED(hr) && (m_colorType == UI_SWATCHCOLORTYPE_RGB))
  1531. {
  1532. ATLASSERT(pCommandExecutionProperties);
  1533. PROPVARIANT var;
  1534. if SUCCEEDED(hr = pCommandExecutionProperties->GetValue(UI_PKEY_Color, &var))
  1535. hr = PropVariantToUInt32(var, &m_color);
  1536. }
  1537. if SUCCEEDED(hr)
  1538. this->GetWndRibbon().OnRibbonColorCtrlExecute(this->GetID(), verb, (UI_SWATCHCOLORTYPE)m_colorType/*uType*/, m_color);
  1539. else
  1540. ATLASSERT(FALSE); // something was wrong
  1541. return hr;
  1542. }
  1543. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  1544. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  1545. {
  1546. ATLASSERT (nCmdID == this->GetID());
  1547. HRESULT hr = E_NOTIMPL;
  1548. switch (k_(key))
  1549. {
  1550. case k_ColorType:
  1551. hr = SetPropertyVal(key, m_colorType, ppropvarNewValue);
  1552. break;
  1553. case k_Color:
  1554. if (m_color == 0x800080) /*MAGENTA*/
  1555. m_color = this->GetWndRibbon().OnRibbonQueryColor(this->GetID());
  1556. hr = SetPropertyVal(key, m_color, ppropvarNewValue);
  1557. break;
  1558. case k_ColorMode:
  1559. break;
  1560. case k_ThemeColorsCategoryLabel:
  1561. case k_StandardColorsCategoryLabel:
  1562. case k_RecentColorsCategoryLabel:
  1563. case k_AutomaticColorLabel:
  1564. case k_NoColorLabel:
  1565. case k_MoreColorsLabel:
  1566. {
  1567. const UINT iLabel = k_(key) - k_ThemeColorsCategoryLabel;
  1568. if (m_sLabels[iLabel].IsEmpty())
  1569. if (LPCWSTR psLabel = this->GetWndRibbon().OnRibbonQueryColorLabel(this->GetID(), key))
  1570. m_sLabels[iLabel] = psLabel;
  1571. if (!m_sLabels[iLabel].IsEmpty())
  1572. hr = SetPropertyVal(key, (LPCWSTR)m_sLabels[iLabel], ppropvarNewValue);
  1573. }
  1574. break;
  1575. case k_ThemeColors:
  1576. case k_StandardColors:
  1577. {
  1578. const INT ic = k_(key) - k_ThemeColors;
  1579. if (!m_aColors[ic].GetSize())
  1580. if (COLORREF* pColor = this->GetWndRibbon().OnRibbonQueryColorArray(this->GetID(), key))
  1581. SetColorArray(key, pColor);
  1582. if (INT iMax = m_aColors[ic].GetSize())
  1583. hr = InitPropVariantFromUInt32Vector(m_aColors[ic].GetData(), iMax, ppropvarNewValue);
  1584. }
  1585. break;
  1586. case k_ThemeColorsTooltips:
  1587. case k_StandardColorsTooltips:
  1588. {
  1589. const INT ic = k_(key) - k_ThemeColorsTooltips;
  1590. if (m_aTooltips[ic].GetSize() == 0)
  1591. if (LPCWSTR* ppsTT = this->GetWndRibbon().OnRibbonQueryColorTooltips(this->GetID(), key))
  1592. SetColorTooltips(key, ppsTT);
  1593. if (INT iMax = m_aTooltips[ic].GetSize())
  1594. hr = InitPropVariantFromStringVector(m_aTooltips[ic].GetData(), iMax, ppropvarNewValue);
  1595. }
  1596. break;
  1597. default:
  1598. hr = CommandCtrlImpl<T, t_ID>::DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  1599. break;
  1600. }
  1601. return hr;
  1602. }
  1603. };
  1604. // SpinnerCtrlImpl
  1605. //
  1606. template <class T, UINT t_ID, typename V = LONG>
  1607. class SpinnerCtrlImpl : public CtrlImpl<T, t_ID>
  1608. {
  1609. public:
  1610. SpinnerCtrlImpl()
  1611. {
  1612. m_Values[0] = m_Values[2] = m_Values[4] = 0;
  1613. m_Values[1] = 100;
  1614. m_Values[3] = 1;
  1615. }
  1616. V m_Values[5];
  1617. // k_DecimalValue = 201, k_MaxValue = 203, k_MinValue, k_Increment, k_DecimalPlaces
  1618. Text m_FormatString;
  1619. Text m_RepresentativeString;
  1620. // Operations
  1621. HRESULT SetDecimalPlaces(V vPlaces, bool bUpdate = false)
  1622. {
  1623. return SetValue(UI_PKEY_DecimalPlaces, vPlaces, bUpdate);
  1624. }
  1625. HRESULT SetMin(V vMin, bool bUpdate = false)
  1626. {
  1627. return SetValue(UI_PKEY_MinValue, vMin, bUpdate);
  1628. }
  1629. HRESULT SetMax(V vMax, bool bUpdate = false)
  1630. {
  1631. return SetValue(UI_PKEY_MaxValue, vMax, bUpdate);
  1632. }
  1633. HRESULT SetVal(V vVal, bool bUpdate = false)
  1634. {
  1635. return SetValue(UI_PKEY_DecimalValue, vVal, bUpdate);
  1636. }
  1637. HRESULT SetIncrement(V vIncrement, bool bUpdate = false)
  1638. {
  1639. return SetValue(UI_PKEY_Increment, vIncrement, bUpdate);
  1640. }
  1641. HRESULT SetFormatString(LPCWSTR sFormat, bool bUpdate = false)
  1642. {
  1643. return SetText(UI_PKEY_FormatString, sFormat, bUpdate);
  1644. }
  1645. HRESULT SetRepresentativeString(LPCWSTR sRepresentative, bool bUpdate = false)
  1646. {
  1647. return SetText(UI_PKEY_RepresentativeString, sRepresentative, bUpdate);
  1648. }
  1649. // Implementation
  1650. HRESULT SetText(REFPROPERTYKEY key, LPCWSTR sText, bool bUpdate = false)
  1651. {
  1652. switch (k_(key))
  1653. {
  1654. case k_FormatString:
  1655. m_FormatString = sText;
  1656. break;
  1657. case k_RepresentativeString:
  1658. m_RepresentativeString = sText;
  1659. break;
  1660. default:
  1661. return CtrlImpl::SetText(key, sText, bUpdate);
  1662. }
  1663. return bUpdate ?
  1664. this->GetWndRibbon().InvalidateProperty(this->GetID(), key) :
  1665. S_OK;
  1666. }
  1667. HRESULT SetValue(REFPROPERTYKEY key, V val, bool bUpdate = false)
  1668. {
  1669. ATLASSERT((k_(key) <= k_DecimalPlaces) && (k_(key) >= k_DecimalValue));
  1670. const INT iVal = k_(key) == k_DecimalValue ? 0 : k_(key) - k_StringValue;
  1671. m_Values[iVal] = val;
  1672. if (bUpdate)
  1673. {
  1674. if(k_(key) == k_DecimalValue)
  1675. {
  1676. DECIMAL decVal;
  1677. InitDecimal(val, &decVal);
  1678. return this->SetProperty(key, &decVal);
  1679. }
  1680. else
  1681. {
  1682. return this->GetWndRibbon().InvalidateProperty(this->GetID(), key);
  1683. }
  1684. }
  1685. else
  1686. {
  1687. return S_OK;
  1688. }
  1689. }
  1690. HRESULT QueryValue(REFPROPERTYKEY key, LONG* plVal)
  1691. {
  1692. return this->GetWndRibbon().OnRibbonQuerySpinnerValue(this->GetID(), key, plVal) ? S_OK : S_FALSE;
  1693. }
  1694. HRESULT QueryValue(REFPROPERTYKEY key, DOUBLE* pdVal)
  1695. {
  1696. return this->GetWndRibbon().OnRibbonQueryFloatSpinnerValue(this->GetID(), key, pdVal) ? S_OK : S_FALSE;
  1697. }
  1698. HRESULT OnGetValue(REFPROPERTYKEY key, PROPVARIANT* ppv)
  1699. {
  1700. ATLASSERT((k_(key) <= k_DecimalPlaces) && (k_(key) >= k_DecimalValue));
  1701. const INT iVal = k_(key) == k_DecimalValue ? 0 : k_(key) - k_StringValue;
  1702. QueryValue(key, m_Values + iVal);
  1703. if (k_(key) == k_DecimalPlaces)
  1704. {
  1705. return SetPropertyVal(key, m_Values[iVal], ppv);
  1706. }
  1707. else
  1708. {
  1709. DECIMAL decVal;
  1710. InitDecimal(m_Values[iVal], &decVal);
  1711. return SetPropertyVal(key, &decVal, ppv);
  1712. }
  1713. }
  1714. HRESULT OnGetText(REFPROPERTYKEY key, Text& sVal, PROPVARIANT* ppv)
  1715. {
  1716. if (LPCWSTR sNew = this->GetWndRibbon().OnRibbonQueryText(this->GetID(), key))
  1717. sVal = sNew;
  1718. return SetPropertyVal(key, (LPCWSTR)sVal, ppv);
  1719. }
  1720. virtual HRESULT DoExecute(UINT nCmdID, UI_EXECUTIONVERB verb,
  1721. const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue,
  1722. IUISimplePropertySet* /*pCommandExecutionProperties*/)
  1723. {
  1724. ATLASSERT (nCmdID == this->GetID());
  1725. (void)nCmdID; // avoid level 4 warning
  1726. ATLASSERT (key && (k_(*key) == k_DecimalValue));
  1727. (void)key; // avoid level 4 warning
  1728. ATLASSERT (verb == UI_EXECUTIONVERB_EXECUTE);
  1729. (void)verb; // avoid level 4 warning
  1730. DECIMAL decVal;
  1731. HRESULT hr = UIPropertyToDecimal(UI_PKEY_DecimalValue, *ppropvarValue, &decVal);
  1732. ATLASSERT(SUCCEEDED(hr));
  1733. hr = InitVal(m_Values[0], &decVal);
  1734. ATLASSERT(SUCCEEDED(hr));
  1735. this->GetWndRibbon().OnRibbonSpinnerCtrlExecute(this->GetID(), &m_Values[0]);
  1736. return hr;
  1737. }
  1738. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  1739. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  1740. {
  1741. ATLASSERT (nCmdID == this->GetID());
  1742. HRESULT hr = E_NOTIMPL;
  1743. switch (k_(key))
  1744. {
  1745. case k_DecimalPlaces:
  1746. case k_DecimalValue:
  1747. case k_Increment:
  1748. case k_MaxValue:
  1749. case k_MinValue:
  1750. hr = OnGetValue(key, ppropvarNewValue);
  1751. break;
  1752. case k_FormatString:
  1753. hr = OnGetText(key, m_FormatString, ppropvarNewValue);
  1754. break;
  1755. case k_RepresentativeString:
  1756. hr = OnGetText(key, m_RepresentativeString, ppropvarNewValue);
  1757. break;
  1758. default:
  1759. hr = CtrlImpl<T, t_ID>::DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  1760. break;
  1761. }
  1762. return hr;
  1763. }
  1764. // decimal conversion helpers
  1765. static HRESULT InitDecimal(LONG& val, DECIMAL* pDecimal)
  1766. {
  1767. return ::VarDecFromI4(val, pDecimal);
  1768. }
  1769. static HRESULT InitDecimal(DOUBLE& val, DECIMAL* pDecimal)
  1770. {
  1771. return ::VarDecFromR8(val, pDecimal);
  1772. }
  1773. static HRESULT InitVal(LONG& val, const DECIMAL* pDecimal)
  1774. {
  1775. return ::VarI4FromDec(pDecimal, &val);
  1776. }
  1777. static HRESULT InitVal(DOUBLE& val, const DECIMAL* pDecimal)
  1778. {
  1779. return ::VarR8FromDec(pDecimal, &val);
  1780. }
  1781. };
  1782. // CRibbonImpl Ribbon implementation class
  1783. //
  1784. template <class T>
  1785. class CRibbonImpl :
  1786. public CRibbonUpdateUI<T>,
  1787. public ICtrl,
  1788. public IUIApplication,
  1789. public IUICommandHandler
  1790. {
  1791. typedef CRibbonImpl<T> thisClass;
  1792. public:
  1793. typedef thisClass Ribbon;
  1794. typedef T WndRibbon;
  1795. CRibbonImpl() : m_bRibbonUI(false), m_hgRibbonSettings(NULL)
  1796. {
  1797. #ifdef _DEBUG
  1798. m_cRef = 1;
  1799. #endif
  1800. pWndRibbon = static_cast<T*>(this);
  1801. HRESULT hr = ::CoInitialize(NULL);
  1802. if(SUCCEEDED(hr))
  1803. if (RunTimeHelper::IsRibbonUIAvailable())
  1804. hr = m_pIUIFramework.CoCreateInstance(CLSID_UIRibbonFramework);
  1805. else
  1806. ATLTRACE2(atlTraceUI, 0, _T("Ribbon UI not available\n"));
  1807. if FAILED(hr)
  1808. ATLTRACE2(atlTraceUI, 0, _T("Ribbon construction failed\n"));
  1809. ATLASSERT(SUCCEEDED(hr));
  1810. }
  1811. virtual ~CRibbonImpl()
  1812. {
  1813. ::GlobalFree(m_hgRibbonSettings);
  1814. m_pIUIFramework.Release();
  1815. ::CoUninitialize();
  1816. }
  1817. ICtrl& GetRibbonCtrl(UINT)
  1818. {
  1819. return static_cast<ICtrl&>(*this);
  1820. }
  1821. ATL::CComPtr<IUIFramework> m_pIUIFramework;
  1822. bool m_bRibbonUI;
  1823. HGLOBAL m_hgRibbonSettings;
  1824. bool IsRibbonUI()
  1825. {
  1826. return m_bRibbonUI;
  1827. }
  1828. IUIFramework* GetIUIFrameworkPtr()
  1829. {
  1830. return m_pIUIFramework;
  1831. }
  1832. template <typename I>
  1833. I* GetRibbonViewPtr(UINT32 uID)
  1834. {
  1835. ATLASSERT(m_pIUIFramework);
  1836. ATL::CComPtr<I> pI;
  1837. return m_pIUIFramework->GetView(uID, __uuidof(I), (void**) &pI) == S_OK ?
  1838. pI :
  1839. NULL;
  1840. }
  1841. IUIRibbon* GetRibbonPtr()
  1842. {
  1843. return GetRibbonViewPtr<IUIRibbon>(0);
  1844. }
  1845. IUIContextualUI* GetMenuPtr(UINT32 uID)
  1846. {
  1847. ATLASSERT(uID);
  1848. return GetRibbonViewPtr<IUIContextualUI>(uID);
  1849. }
  1850. UINT GetRibbonHeight()
  1851. {
  1852. ATLASSERT(IsRibbonUI());
  1853. UINT32 cy = 0;
  1854. if (ATL::CComPtr<IUIRibbon> pIUIRibbon = GetRibbonPtr())
  1855. pIUIRibbon->GetHeight(&cy);
  1856. return cy;
  1857. }
  1858. HRESULT CreateRibbon(LPCWSTR sResName = L"APPLICATION_RIBBON")
  1859. {
  1860. T* pT = static_cast<T*>(this);
  1861. ATLASSERT(GetIUIFrameworkPtr() && !IsRibbonUI());
  1862. ATLASSERT(pT->IsWindow());
  1863. HRESULT hr = m_pIUIFramework->Initialize(pT->m_hWnd, this);
  1864. if (hr == S_OK)
  1865. hr = m_pIUIFramework->LoadUI(ModuleHelper::GetResourceInstance(), sResName);
  1866. return hr;
  1867. }
  1868. HRESULT DestroyRibbon()
  1869. {
  1870. T* pT = static_cast<T*>(this);
  1871. ATLASSERT(GetIUIFrameworkPtr() && IsRibbonUI());
  1872. ATLASSERT(pT->IsWindow());
  1873. HRESULT hRes = m_pIUIFramework->Destroy();
  1874. if (!RunTimeHelper::IsWin7())
  1875. pT->SetWindowRgn(NULL, TRUE); // Vista Basic bug workaround
  1876. return hRes;
  1877. }
  1878. // Ribbon persistency
  1879. HRESULT operator >>(IStream* pIStream)
  1880. {
  1881. ATLASSERT(GetIUIFrameworkPtr());
  1882. ATLASSERT(pIStream);
  1883. HRESULT hr = E_FAIL;
  1884. if (ATL::CComPtr<IUIRibbon> pIUIRibbon = GetRibbonPtr())
  1885. {
  1886. const LARGE_INTEGER li0 = {};
  1887. pIStream->Seek(li0, STREAM_SEEK_SET, NULL);
  1888. hr = pIUIRibbon->SaveSettingsToStream(pIStream);
  1889. pIStream->Commit(STGC_DEFAULT);
  1890. }
  1891. return hr;
  1892. }
  1893. HRESULT operator <<(IStream* pIStream)
  1894. {
  1895. ATLASSERT(GetIUIFrameworkPtr());
  1896. ATLASSERT(pIStream);
  1897. HRESULT hr = E_FAIL;
  1898. if (ATL::CComPtr<IUIRibbon> pIUIRibbon = GetRibbonPtr())
  1899. {
  1900. const LARGE_INTEGER li0 = {};
  1901. pIStream->Seek(li0, STREAM_SEEK_SET, NULL);
  1902. hr = pIUIRibbon->LoadSettingsFromStream(pIStream);
  1903. }
  1904. return hr;
  1905. }
  1906. void ResetRibbonSettings()
  1907. {
  1908. if (m_hgRibbonSettings != NULL)
  1909. {
  1910. ::GlobalFree(m_hgRibbonSettings);
  1911. m_hgRibbonSettings = NULL;
  1912. }
  1913. }
  1914. HRESULT SaveRibbonSettings()
  1915. {
  1916. ATLASSERT(GetIUIFrameworkPtr());
  1917. ATLASSERT(static_cast<T*>(this)->IsWindow());
  1918. HRESULT hr = E_FAIL;
  1919. ATL::CComPtr<IStream> pIStream;
  1920. if SUCCEEDED(hr = ::CreateStreamOnHGlobal(m_hgRibbonSettings, FALSE, &pIStream))
  1921. hr = *this >> pIStream;
  1922. if (SUCCEEDED(hr) && (m_hgRibbonSettings == NULL))
  1923. hr = ::GetHGlobalFromStream(pIStream, &m_hgRibbonSettings);
  1924. if FAILED(hr)
  1925. ResetRibbonSettings();
  1926. return hr;
  1927. }
  1928. HRESULT RestoreRibbonSettings()
  1929. {
  1930. ATLASSERT(GetIUIFrameworkPtr());
  1931. ATLASSERT(m_hgRibbonSettings);
  1932. ATLASSERT(static_cast<T*>(this)->IsWindow());
  1933. HRESULT hr = E_FAIL;
  1934. ATL::CComPtr<IStream> pIStream;
  1935. if SUCCEEDED(hr = ::CreateStreamOnHGlobal(m_hgRibbonSettings, FALSE, &pIStream))
  1936. hr = *this << pIStream;
  1937. if FAILED(hr)
  1938. ResetRibbonSettings();
  1939. return hr;
  1940. }
  1941. // QAT dock states
  1942. UI_CONTROLDOCK GetQATDock()
  1943. {
  1944. ATLASSERT(GetIUIFrameworkPtr());
  1945. ATLASSERT(IsRibbonUI());
  1946. UINT32 uDock = 0;
  1947. PROPVARIANT propvar;
  1948. ATL::CComQIPtr<IPropertyStore>pIPS(GetRibbonPtr());
  1949. if ((pIPS != NULL) && SUCCEEDED(pIPS->GetValue(UI_PKEY_QuickAccessToolbarDock, &propvar)) &&
  1950. SUCCEEDED(UIPropertyToUInt32(UI_PKEY_QuickAccessToolbarDock, propvar, &uDock)))
  1951. return (UI_CONTROLDOCK)uDock;
  1952. ATLASSERT(FALSE); // something was wrong
  1953. return (UI_CONTROLDOCK)0;
  1954. }
  1955. bool SetQATDock(UI_CONTROLDOCK dockState)
  1956. {
  1957. ATLASSERT(GetIUIFrameworkPtr());
  1958. ATLASSERT(IsRibbonUI());
  1959. PROPVARIANT propvar;
  1960. ATLVERIFY(SUCCEEDED(SetPropertyVal(UI_PKEY_QuickAccessToolbarDock, dockState, &propvar)));
  1961. ATL::CComQIPtr<IPropertyStore>pIPS(GetRibbonPtr());
  1962. if ((pIPS != NULL) && SUCCEEDED(pIPS->SetValue(UI_PKEY_QuickAccessToolbarDock, propvar)))
  1963. {
  1964. pIPS->Commit();
  1965. return true;
  1966. }
  1967. ATLASSERT(FALSE); // something was wrong
  1968. return false;
  1969. }
  1970. // Ribbon display states
  1971. bool GetRibbonDisplayState(REFPROPERTYKEY key)
  1972. {
  1973. ATLASSERT(GetIUIFrameworkPtr());
  1974. ATLASSERT(IsRibbonUI());
  1975. ATLASSERT((k_(key) == k_Viewable) || (k_(key) == k_Minimized));
  1976. PROPVARIANT propvar;
  1977. ATL::CComQIPtr<IPropertyStore>pIPS(GetRibbonPtr());
  1978. if ((pIPS != NULL) && SUCCEEDED(pIPS->GetValue(key, &propvar)))
  1979. {
  1980. BOOL bState = FALSE;
  1981. if SUCCEEDED(UIPropertyToBoolean(key, propvar, &bState))
  1982. return (bState != FALSE);
  1983. }
  1984. ATLASSERT(FALSE); // something was wrong
  1985. return false;
  1986. }
  1987. bool SetRibbonDisplayState(REFPROPERTYKEY key, bool bState = true)
  1988. {
  1989. ATLASSERT(GetIUIFrameworkPtr());
  1990. ATLASSERT(IsRibbonUI());
  1991. ATLASSERT((k_(key) == k_Viewable) || (k_(key) == k_Minimized));
  1992. PROPVARIANT propvar;
  1993. ATLVERIFY(SUCCEEDED(SetPropertyVal(key, bState, &propvar)));
  1994. ATL::CComQIPtr<IPropertyStore>pIPS(GetRibbonPtr());
  1995. if ((pIPS != NULL) && SUCCEEDED(pIPS->SetValue(key, propvar)))
  1996. {
  1997. pIPS->Commit();
  1998. return true;
  1999. }
  2000. ATLASSERT(FALSE); // something was wrong
  2001. return false;
  2002. }
  2003. bool IsRibbonMinimized()
  2004. {
  2005. return GetRibbonDisplayState(UI_PKEY_Minimized);
  2006. }
  2007. bool MinimizeRibbon(bool bMinimize = true)
  2008. {
  2009. return SetRibbonDisplayState(UI_PKEY_Minimized, bMinimize);
  2010. }
  2011. bool IsRibbonHidden()
  2012. {
  2013. return !GetRibbonDisplayState(UI_PKEY_Viewable);
  2014. }
  2015. bool HideRibbon(bool bHide = true)
  2016. {
  2017. return SetRibbonDisplayState(UI_PKEY_Viewable, !bHide);
  2018. }
  2019. // Ribbon colors
  2020. UI_HSBCOLOR GetRibbonColor(REFPROPERTYKEY key)
  2021. {
  2022. ATLASSERT(GetIUIFrameworkPtr());
  2023. ATLASSERT(IsRibbonUI());
  2024. ATLASSERT((k_(key) >= k_GlobalBackgroundColor) && (k_(key) <= k_GlobalTextColor));
  2025. PROPVARIANT propvar;
  2026. ATL::CComQIPtr<IPropertyStore>pIPS(GetIUIFrameworkPtr());
  2027. if ((pIPS != NULL) && SUCCEEDED(pIPS->GetValue(key, &propvar)))
  2028. {
  2029. UINT32 color = 0;
  2030. if SUCCEEDED(UIPropertyToUInt32(key, propvar, &color))
  2031. return color;
  2032. }
  2033. ATLASSERT(FALSE); // something was wrong
  2034. return 0;
  2035. }
  2036. bool SetRibbonColor(REFPROPERTYKEY key, UI_HSBCOLOR color)
  2037. {
  2038. ATLASSERT(GetIUIFrameworkPtr());
  2039. ATLASSERT(IsRibbonUI());
  2040. ATLASSERT((k_(key) >= k_GlobalBackgroundColor) && (k_(key) <= k_GlobalTextColor));
  2041. PROPVARIANT propvar;
  2042. ATLVERIFY(SUCCEEDED(SetPropertyVal(key, color, &propvar)));
  2043. ATL::CComQIPtr<IPropertyStore>pIPS(GetIUIFrameworkPtr());
  2044. if ((pIPS != NULL) && SUCCEEDED(pIPS->SetValue(key, propvar)))
  2045. {
  2046. pIPS->Commit();
  2047. return true;
  2048. }
  2049. ATLASSERT(FALSE); // something was wrong
  2050. return false;
  2051. }
  2052. // Ribbon modes
  2053. HRESULT SetRibbonModes(INT32 iModes)
  2054. {
  2055. ATLASSERT(IsRibbonUI());
  2056. return GetIUIFrameworkPtr()->SetModes(iModes);
  2057. }
  2058. // Ribbon contextual tab
  2059. UI_CONTEXTAVAILABILITY GetRibbonContextAvail(UINT32 uID)
  2060. {
  2061. ATLASSERT(GetIUIFrameworkPtr());
  2062. PROPVARIANT propvar;
  2063. if (IsRibbonUI() &&
  2064. SUCCEEDED(GetIUIFrameworkPtr()->GetUICommandProperty(uID, UI_PKEY_ContextAvailable, &propvar)))
  2065. {
  2066. UINT uav;
  2067. if (SUCCEEDED(PropVariantToUInt32(propvar, &uav)))
  2068. {
  2069. CUpdateUIBase::UIEnable(uID, uav != UI_CONTEXTAVAILABILITY_NOTAVAILABLE);
  2070. CUpdateUIBase::UISetCheck(uID, uav == UI_CONTEXTAVAILABILITY_ACTIVE);
  2071. return (UI_CONTEXTAVAILABILITY)uav;
  2072. }
  2073. }
  2074. return UI_CONTEXTAVAILABILITY_NOTAVAILABLE;
  2075. }
  2076. HRESULT SetRibbonContextAvail(UINT32 uID, UI_CONTEXTAVAILABILITY cav)
  2077. {
  2078. CUpdateUIBase::UIEnable(uID, cav != UI_CONTEXTAVAILABILITY_NOTAVAILABLE);
  2079. CUpdateUIBase::UISetCheck(uID, cav == UI_CONTEXTAVAILABILITY_ACTIVE);
  2080. return SetProperty((WORD)uID, UI_PKEY_ContextAvailable, UINT32(cav));
  2081. }
  2082. // Ribbon context menu
  2083. bool HasRibbonMenu(UINT32 uID)
  2084. {
  2085. ATL::CComPtr<IUIContextualUI> pI = GetMenuPtr(uID);
  2086. return pI != NULL;
  2087. }
  2088. HRESULT TrackRibbonMenu(UINT32 uID, INT32 x, INT32 y)
  2089. {
  2090. ATLASSERT(HasRibbonMenu(uID));
  2091. return IsRibbonUI() ?
  2092. ATL::CComPtr<IUIContextualUI>(GetMenuPtr(uID))->ShowAtLocation(x, y) :
  2093. E_FAIL;
  2094. }
  2095. HRESULT TrackRibbonMenu(UINT32 uID, LPARAM lParam)
  2096. {
  2097. return TrackRibbonMenu(uID, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
  2098. }
  2099. // Overrideables
  2100. HBITMAP OnRibbonQueryImage(UINT nCmdID, REFPROPERTYKEY /*key*/)
  2101. {
  2102. return DefRibbonQueryImage(nCmdID);
  2103. }
  2104. LPCWSTR OnRibbonQueryText(UINT nCmdID, REFPROPERTYKEY key)
  2105. {
  2106. return DefRibbonQueryText(nCmdID, key);
  2107. }
  2108. bool OnRibbonQueryState(UINT nCmdID, REFPROPERTYKEY key)
  2109. {
  2110. return DefRibbonQueryState(nCmdID, key);
  2111. }
  2112. UI_CONTEXTAVAILABILITY OnRibbonQueryTabAvail(UINT nCmdID)
  2113. {
  2114. DWORD dwState = this->UIGetState(nCmdID);
  2115. return ((dwState & CUpdateUIBase::UPDUI_DISABLED) == CUpdateUIBase::UPDUI_DISABLED) ?
  2116. UI_CONTEXTAVAILABILITY_NOTAVAILABLE :
  2117. (((dwState & CUpdateUIBase::UPDUI_CHECKED) == CUpdateUIBase::UPDUI_CHECKED) ?
  2118. UI_CONTEXTAVAILABILITY_ACTIVE :
  2119. UI_CONTEXTAVAILABILITY_AVAILABLE);
  2120. }
  2121. LPCWSTR OnRibbonQueryComboText(UINT32 /*uCtrlID*/)
  2122. {
  2123. return NULL;
  2124. }
  2125. LPCWSTR OnRibbonQueryCategoryText(UINT32 /*uCtrlID*/, UINT32 /*uCat*/)
  2126. {
  2127. return L"Category";
  2128. }
  2129. UINT32 OnRibbonQueryItemCategory(UINT32 /*uCtrlID*/, UINT32 /*uItem*/)
  2130. {
  2131. return 0;
  2132. }
  2133. LPCWSTR OnRibbonQueryItemText(UINT32 uCtrlID, UINT32 uItem)
  2134. {
  2135. return DefRibbonQueryItemText(uCtrlID, uItem);
  2136. }
  2137. bool OnRibbonQuerySelectedItem(UINT32 /*uCtrlID*/, UINT32& /*uSel*/)
  2138. {
  2139. return false;
  2140. }
  2141. HBITMAP OnRibbonQueryItemImage(UINT32 uCtrlID, UINT32 uItem)
  2142. {
  2143. return DefRibbonQueryItemImage(uCtrlID, uItem);
  2144. }
  2145. UINT32 OnRibbonQueryItemCommand(UINT32 uCtrlID, UINT32 uItem)
  2146. {
  2147. return DefRibbonQueryItemCommand(uCtrlID, uItem);
  2148. }
  2149. UI_COMMANDTYPE OnRibbonQueryItemCommandType(UINT32 /*uCtrlID*/, UINT32 /*uItem*/)
  2150. {
  2151. return UI_COMMANDTYPE_ACTION;
  2152. }
  2153. LPCWSTR OnRibbonQueryRecentItemName(LPCWSTR sPath)
  2154. {
  2155. return ::PathFindFileName(sPath);
  2156. }
  2157. bool OnRibbonQueryFont(UINT /*nId*/, CHARFORMAT2& /*cf*/)
  2158. {
  2159. return false;
  2160. }
  2161. bool OnRibbonQuerySpinnerValue(UINT /*nCmdID*/, REFPROPERTYKEY /*key*/, LONG* /*pVal*/)
  2162. {
  2163. return false;
  2164. }
  2165. bool OnRibbonQueryFloatSpinnerValue(UINT /*nCmdID*/, REFPROPERTYKEY /*key*/, DOUBLE* /*pVal*/)
  2166. {
  2167. return false;
  2168. }
  2169. COLORREF OnRibbonQueryColor(UINT /*nCmdID*/)
  2170. {
  2171. return 0x800080; /*MAGENTA*/
  2172. }
  2173. LPCWSTR OnRibbonQueryColorLabel(UINT /*nCmdID*/, REFPROPERTYKEY /*key*/)
  2174. {
  2175. return NULL;
  2176. }
  2177. COLORREF* OnRibbonQueryColorArray(UINT /*nCmdID*/, REFPROPERTYKEY /*key*/)
  2178. {
  2179. return NULL;
  2180. }
  2181. LPCWSTR* OnRibbonQueryColorTooltips(UINT /*nCmdID*/, REFPROPERTYKEY /*key*/)
  2182. {
  2183. return NULL;
  2184. }
  2185. bool OnRibbonItemSelected(UINT32 uCtrlID, UI_EXECUTIONVERB verb, UINT32 uItem)
  2186. {
  2187. DefCommandExecute(MAKELONG(uCtrlID, verb), uItem);
  2188. return true;
  2189. }
  2190. void OnRibbonColorCtrlExecute(UINT32 uCtrlID, UI_EXECUTIONVERB verb, UI_SWATCHCOLORTYPE uType, COLORREF color)
  2191. {
  2192. DefRibbonColorCtrlExecute(uCtrlID, verb, uType, color);
  2193. }
  2194. void OnRibbonFontCtrlExecute(UINT32 uCtrlID, UI_EXECUTIONVERB verb, CHARFORMAT2* pcf)
  2195. {
  2196. DefCommandExecute(MAKELONG(uCtrlID, verb), (LPARAM)pcf);
  2197. }
  2198. void OnRibbonSpinnerCtrlExecute(UINT32 uCtrlID, LONG* pVal)
  2199. {
  2200. DefCommandExecute(uCtrlID, *pVal);
  2201. }
  2202. void OnRibbonSpinnerCtrlExecute(UINT32 uCtrlID, DOUBLE* pVal)
  2203. {
  2204. DefCommandExecute(uCtrlID, (LPARAM)pVal);
  2205. }
  2206. void OnRibbonCommandExecute(UINT32 uCmdID)
  2207. {
  2208. DefCommandExecute(uCmdID);
  2209. }
  2210. // Default implementations
  2211. HBITMAP DefRibbonQueryImage(UINT nCmdID)
  2212. {
  2213. return AtlLoadBitmapImage(nCmdID, LR_CREATEDIBSECTION);
  2214. }
  2215. bool DefRibbonQueryState(UINT nCmdID, REFPROPERTYKEY key)
  2216. {
  2217. DWORD dwState = this->UIGetState(nCmdID);
  2218. bool bRet = false;
  2219. switch (k_(key))
  2220. {
  2221. case k_BooleanValue:
  2222. bRet = (dwState & CUpdateUIBase::UPDUI_CHECKED) == CUpdateUIBase::UPDUI_CHECKED;
  2223. break;
  2224. case k_Enabled:
  2225. bRet = (dwState & CUpdateUIBase::UPDUI_DISABLED) != CUpdateUIBase::UPDUI_DISABLED;
  2226. break;
  2227. default:
  2228. ATLASSERT(FALSE);
  2229. break;
  2230. }
  2231. return bRet;
  2232. }
  2233. LPCTSTR DefRibbonQueryText(UINT nCmdID, REFPROPERTYKEY key)
  2234. {
  2235. static WCHAR sText[RIBBONUI_MAX_TEXT] = {};
  2236. if (k_(key) == k_Label)
  2237. return this->UIGetText(nCmdID);
  2238. if (ATL::AtlLoadString(nCmdID, sText, RIBBONUI_MAX_TEXT))
  2239. {
  2240. PWCHAR pTitle = wcschr(sText, L'\n');
  2241. switch (k_(key))
  2242. {
  2243. case k_Keytip:
  2244. if (PWCHAR pAmp = wcschr(sText, L'&'))
  2245. pTitle = pAmp;
  2246. if (pTitle != NULL)
  2247. *(pTitle + 2) = NULL; // fall through
  2248. case k_TooltipTitle:
  2249. return pTitle ? ++pTitle : NULL;
  2250. case k_TooltipDescription:
  2251. case k_LabelDescription:
  2252. if (pTitle != NULL)
  2253. *pTitle = NULL;
  2254. return sText;
  2255. }
  2256. }
  2257. return NULL;
  2258. }
  2259. LPCWSTR DefRibbonQueryItemText(UINT32 uCtrlID, UINT32 uItem)
  2260. {
  2261. return DefRibbonQueryText(uCtrlID + 1 + uItem, UI_PKEY_LabelDescription);
  2262. }
  2263. HBITMAP DefRibbonQueryItemImage(UINT32 uCtrlID, UINT32 uItem)
  2264. {
  2265. return DefRibbonQueryImage(uCtrlID + 1 + uItem);
  2266. }
  2267. UINT32 DefRibbonQueryItemCommand(UINT32 uCtrlID, UINT32 uItem)
  2268. {
  2269. return uCtrlID + 1 + uItem;
  2270. }
  2271. void DefRibbonColorCtrlExecute(UINT32 uCtrlID, UI_EXECUTIONVERB verb, UI_SWATCHCOLORTYPE uType, COLORREF color)
  2272. {
  2273. switch(uType)
  2274. {
  2275. case UI_SWATCHCOLORTYPE_RGB:
  2276. break;
  2277. case UI_SWATCHCOLORTYPE_AUTOMATIC:
  2278. color = ::GetSysColor(COLOR_WINDOWTEXT);
  2279. break;
  2280. case UI_SWATCHCOLORTYPE_NOCOLOR:
  2281. color = ::GetSysColor(COLOR_WINDOW);
  2282. break;
  2283. default:
  2284. ATLASSERT(FALSE);
  2285. break;
  2286. }
  2287. DefCommandExecute(MAKELONG(uCtrlID, verb), color);
  2288. }
  2289. void DefCommandExecute(UINT32 uCmd, LPARAM lParam = 0)
  2290. {
  2291. static_cast<T*>(this)->PostMessage(WM_COMMAND, uCmd, lParam);
  2292. }
  2293. // Elements setting helpers
  2294. HRESULT InvalidateCtrl(UINT32 nID)
  2295. {
  2296. return IsRibbonUI() ?
  2297. GetIUIFrameworkPtr()->InvalidateUICommand(nID, UI_INVALIDATIONS_ALLPROPERTIES, NULL) :
  2298. E_FAIL;
  2299. }
  2300. HRESULT InvalidateProperty(UINT32 nID, REFPROPERTYKEY key, UI_INVALIDATIONS flags = UI_INVALIDATIONS_PROPERTY)
  2301. {
  2302. return IsRibbonUI() ?
  2303. GetIUIFrameworkPtr()->InvalidateUICommand(nID, flags, &key) :
  2304. E_FAIL;
  2305. }
  2306. template <typename V>
  2307. HRESULT SetProperty(WORD wID, REFPROPERTYKEY key, V val)
  2308. {
  2309. if (IsRibbonUI())
  2310. {
  2311. PROPVARIANT var;
  2312. if (SUCCEEDED(RibbonUI::SetPropertyVal(key, val, &var)))
  2313. {
  2314. return SetProperty(wID, key, var);
  2315. }
  2316. return E_INVALIDARG;
  2317. }
  2318. else
  2319. {
  2320. return E_FAIL;
  2321. }
  2322. }
  2323. template <>
  2324. HRESULT SetProperty(WORD nID, REFPROPERTYKEY key, PROPVARIANT var)
  2325. {
  2326. return IsRibbonUI() ?
  2327. GetIUIFrameworkPtr()->SetUICommandProperty(nID, key, var) :
  2328. E_FAIL;
  2329. }
  2330. // Interfaces
  2331. // IUIApplication
  2332. STDMETHODIMP OnViewChanged(UINT32, UI_VIEWTYPE, IUnknown*, UI_VIEWVERB verb, INT32)
  2333. {
  2334. switch (verb)
  2335. {
  2336. case UI_VIEWVERB_CREATE:
  2337. m_bRibbonUI = true;
  2338. if (m_hgRibbonSettings != NULL)
  2339. RestoreRibbonSettings();
  2340. break;
  2341. case UI_VIEWVERB_SIZE:
  2342. static_cast<T*>(this)->UpdateLayout(FALSE);
  2343. break;
  2344. case UI_VIEWVERB_DESTROY:
  2345. SaveRibbonSettings();
  2346. m_bRibbonUI = false;
  2347. break;
  2348. default:
  2349. break;
  2350. }
  2351. return S_OK;
  2352. }
  2353. STDMETHODIMP OnCreateUICommand(UINT32 nCmdID, UI_COMMANDTYPE typeID, IUICommandHandler** ppCommandHandler)
  2354. {
  2355. this->UIAddRibbonElement(nCmdID);
  2356. if (typeID == UI_COMMANDTYPE_CONTEXT)
  2357. CUpdateUIBase::UIEnable(nCmdID, false);
  2358. *ppCommandHandler = this;
  2359. return S_OK;
  2360. }
  2361. STDMETHODIMP OnDestroyUICommand(UINT32 nCmdID, UI_COMMANDTYPE, IUICommandHandler*)
  2362. {
  2363. this->UIRemoveRibbonElement(nCmdID);
  2364. return S_OK;
  2365. }
  2366. // IUICommandHandler
  2367. STDMETHODIMP Execute(UINT nCmdID,
  2368. UI_EXECUTIONVERB verb,
  2369. const PROPERTYKEY* key,
  2370. const PROPVARIANT* ppropvarValue,
  2371. IUISimplePropertySet* pCommandExecutionProperties)
  2372. {
  2373. T* pT =static_cast<T*>(this);
  2374. return pT->GetRibbonCtrl(nCmdID).DoExecute(nCmdID, verb, key, ppropvarValue, pCommandExecutionProperties);
  2375. }
  2376. STDMETHODIMP UpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  2377. const PROPVARIANT* ppropvarCurrentValue, PROPVARIANT* ppropvarNewValue)
  2378. {
  2379. T* pT =static_cast<T*>(this);
  2380. return pT->GetRibbonCtrl(nCmdID).DoUpdateProperty(nCmdID, key, ppropvarCurrentValue, ppropvarNewValue);
  2381. }
  2382. #ifdef _DEBUG
  2383. // IUnknown methods (heavyweight)
  2384. STDMETHODIMP_(ULONG) AddRef()
  2385. {
  2386. return InterlockedIncrement(&m_cRef);
  2387. }
  2388. STDMETHODIMP_(ULONG) Release()
  2389. {
  2390. LONG cRef = InterlockedDecrement(&m_cRef);
  2391. if (cRef == 0) // NoOp for breakpoint
  2392. {
  2393. cRef = 0;
  2394. }
  2395. return cRef;
  2396. }
  2397. STDMETHODIMP QueryInterface(REFIID iid, void** ppv)
  2398. {
  2399. if (ppv == NULL)
  2400. {
  2401. return E_POINTER;
  2402. }
  2403. else if ((iid == __uuidof(IUnknown)) ||
  2404. (iid == __uuidof(IUICommandHandler)) ||
  2405. (iid == __uuidof(IUIApplication)))
  2406. {
  2407. *ppv = this;
  2408. AddRef();
  2409. return S_OK;
  2410. }
  2411. else
  2412. {
  2413. return E_NOINTERFACE;
  2414. }
  2415. }
  2416. LONG m_cRef;
  2417. #else
  2418. // IUnknown methods (lightweight)
  2419. STDMETHODIMP QueryInterface(REFIID iid, void** ppv)
  2420. {
  2421. if ((iid == __uuidof(IUnknown)) ||
  2422. (iid == __uuidof(IUICommandHandler)) ||
  2423. (iid == __uuidof(IUIApplication)))
  2424. {
  2425. *ppv = this;
  2426. return S_OK;
  2427. }
  2428. return E_NOINTERFACE;
  2429. }
  2430. ULONG STDMETHODCALLTYPE AddRef()
  2431. {
  2432. return 1;
  2433. }
  2434. ULONG STDMETHODCALLTYPE Release()
  2435. {
  2436. return 1;
  2437. }
  2438. #endif
  2439. // CRibbonImpl ICtrl implementation
  2440. virtual HRESULT DoExecute(UINT nCmdID, UI_EXECUTIONVERB verb,
  2441. const PROPERTYKEY* key, const PROPVARIANT* ppropvarValue,
  2442. IUISimplePropertySet* /*pCommandExecutionProperties*/)
  2443. {
  2444. if (key != NULL)
  2445. {
  2446. if(k_(*key) != k_BooleanValue)
  2447. {
  2448. ATLTRACE2(atlTraceUI, 0, _T("Control ID %d is not handled\n"), nCmdID);
  2449. return E_NOTIMPL;
  2450. }
  2451. BOOL bChecked = FALSE;
  2452. ATLVERIFY(SUCCEEDED(PropVariantToBoolean(*ppropvarValue, &bChecked)));
  2453. CUpdateUIBase::UISetCheck(nCmdID, bChecked);
  2454. }
  2455. ATLASSERT(verb == UI_EXECUTIONVERB_EXECUTE);
  2456. (void)verb; // avoid level 4 warning
  2457. static_cast<T*>(this)->OnRibbonCommandExecute(nCmdID);
  2458. return S_OK;
  2459. }
  2460. virtual HRESULT DoUpdateProperty(UINT nCmdID, REFPROPERTYKEY key,
  2461. const PROPVARIANT* /*ppropvarCurrentValue*/, PROPVARIANT* ppropvarNewValue)
  2462. {
  2463. T* pT = static_cast<T*>(this);
  2464. HRESULT hr = E_NOTIMPL;
  2465. switch (k_(key))
  2466. {
  2467. case k_LargeImage:
  2468. case k_LargeHighContrastImage:
  2469. case k_SmallImage:
  2470. case k_SmallHighContrastImage:
  2471. if (HBITMAP hbm = pT->OnRibbonQueryImage(nCmdID, key))
  2472. hr = SetPropertyVal(key, GetImage(hbm, UI_OWNERSHIP_TRANSFER), ppropvarNewValue);
  2473. break;
  2474. case k_Label:
  2475. case k_Keytip:
  2476. case k_TooltipTitle:
  2477. case k_TooltipDescription:
  2478. case k_LabelDescription:
  2479. if (LPCWSTR sText = pT->OnRibbonQueryText(nCmdID, key))
  2480. hr = SetPropertyVal(key, sText, ppropvarNewValue);
  2481. break;
  2482. case k_BooleanValue:
  2483. case k_Enabled:
  2484. hr = SetPropertyVal(key, pT->OnRibbonQueryState(nCmdID, key), ppropvarNewValue);
  2485. break;
  2486. case k_ContextAvailable:
  2487. hr = SetPropertyVal(key, pT->OnRibbonQueryTabAvail(nCmdID), ppropvarNewValue);
  2488. break;
  2489. }
  2490. return hr;
  2491. }
  2492. // CRibbonImpl::CRibbonXXXCtrl specialized classes
  2493. //CRibbonComboCtrl
  2494. template <UINT t_ID, size_t t_items, size_t t_categories = 0>
  2495. class CRibbonComboCtrl : public CollectionCtrlImpl<T, t_ID, ComboCollectionImpl<CRibbonComboCtrl<t_ID, t_items, t_categories>, t_items, t_categories>>
  2496. {
  2497. public:
  2498. CRibbonComboCtrl()
  2499. { }
  2500. };
  2501. // CRibbonItemGalleryCtrl
  2502. template <UINT t_ID, size_t t_items, size_t t_categories = 0>
  2503. class CRibbonItemGalleryCtrl : public CollectionCtrlImpl<T, t_ID, ItemCollectionImpl<CRibbonItemGalleryCtrl<t_ID, t_items, t_categories>, t_items, t_categories>>
  2504. {
  2505. public:
  2506. CRibbonItemGalleryCtrl()
  2507. { }
  2508. };
  2509. // CRibbonCommandGalleryCtrl
  2510. template <UINT t_ID, size_t t_items, size_t t_categories = 0>
  2511. class CRibbonCommandGalleryCtrl : public CollectionCtrlImpl<T, t_ID, CommandCollectionImpl<CRibbonCommandGalleryCtrl<t_ID, t_items, t_categories>, t_items, t_categories>>
  2512. {
  2513. public:
  2514. CRibbonCommandGalleryCtrl()
  2515. { }
  2516. };
  2517. // CRibbonToolbarGalleryCtrl
  2518. template <UINT t_ID, UINT t_idTB, size_t t_size>
  2519. class CRibbonToolbarGalleryCtrl : public ToolbarGalleryCtrlImpl<T, t_ID, t_idTB, t_size>
  2520. { };
  2521. // CRibbonSimpleComboCtrl
  2522. template <UINT t_ID, size_t t_size>
  2523. class CRibbonSimpleComboCtrl : public SimpleCollectionCtrlImpl<T, t_ID, t_size>
  2524. { };
  2525. // CRibbonSimpleGalleryCtrl
  2526. template <UINT t_ID, size_t t_size, UI_COMMANDTYPE t_CommandType = UI_COMMANDTYPE_ACTION>
  2527. class CRibbonSimpleGalleryCtrl : public SimpleCollectionCtrlImpl<T, t_ID, t_size, t_CommandType>
  2528. { };
  2529. //CRibbonRecentItemsCtrl
  2530. template <UINT t_ID, class TDocList = CRecentDocumentList>
  2531. class CRibbonRecentItemsCtrl : public RecentItemsCtrlImpl<T, t_ID, TDocList>
  2532. {
  2533. public:
  2534. CRibbonRecentItemsCtrl()
  2535. { }
  2536. };
  2537. // CRibbonColorCtrl
  2538. template <UINT t_ID>
  2539. class CRibbonColorCtrl : public ColorCtrlImpl<T, t_ID>
  2540. {
  2541. public:
  2542. CRibbonColorCtrl()
  2543. { }
  2544. };
  2545. //CRibbonFontCtrl
  2546. template <UINT t_ID>
  2547. class CRibbonFontCtrl : public FontCtrlImpl<T, t_ID>
  2548. {
  2549. public:
  2550. CRibbonFontCtrl()
  2551. { }
  2552. };
  2553. // CRibbonSpinnerCtrl
  2554. template <UINT t_ID>
  2555. class CRibbonSpinnerCtrl : public SpinnerCtrlImpl<T, t_ID, LONG>
  2556. {
  2557. public:
  2558. CRibbonSpinnerCtrl()
  2559. { }
  2560. };
  2561. // CRibbonFloatSpinnerCtrl
  2562. template <UINT t_ID>
  2563. class CRibbonFloatSpinnerCtrl : public SpinnerCtrlImpl<T, t_ID, DOUBLE>
  2564. {
  2565. public:
  2566. CRibbonFloatSpinnerCtrl()
  2567. {
  2568. this->m_Values[4] = 1; // 1 decimal
  2569. }
  2570. };
  2571. // CRibbonCommandCtrl
  2572. template <UINT t_ID>
  2573. class CRibbonCommandCtrl : public CommandCtrlImpl<T, t_ID>
  2574. {
  2575. public:
  2576. CRibbonCommandCtrl()
  2577. { }
  2578. };
  2579. // Control classes access to T instance (re-initialized in constructor)
  2580. static T* pWndRibbon;
  2581. };
  2582. template <class T>
  2583. __declspec(selectany) T* CRibbonImpl<T>::pWndRibbon;
  2584. // Control map element
  2585. #pragma warning(push)
  2586. #pragma warning(disable: 4510 610 4512) // missing default constructor, can't be instatiated, assignment operator could not be generated
  2587. typedef struct
  2588. {
  2589. UINT uID;
  2590. ICtrl& ctrl;
  2591. } _ribbonCtrl;
  2592. #pragma warning(pop)
  2593. } // namespace RibbonUI
  2594. ///////////////////////////////////////////////////////////////////////////////
  2595. // RibbonUI Control map
  2596. // Control map macros
  2597. #define BEGIN_RIBBON_CONTROL_MAP(theClass) \
  2598. WTL::RibbonUI::ICtrl& GetRibbonCtrl(UINT id) \
  2599. { \
  2600. WTL::RibbonUI::_ribbonCtrl _ctrls[] = \
  2601. {
  2602. #define RIBBON_CONTROL(member) {member.GetID(), static_cast<WTL::RibbonUI::ICtrl&>(member)},
  2603. #define END_RIBBON_CONTROL_MAP() \
  2604. {0, *this} \
  2605. }; \
  2606. int i = 0; \
  2607. for(; i < _countof(_ctrls) - 1; i++) \
  2608. if (_ctrls[i].uID == id) \
  2609. break; \
  2610. return _ctrls[i].ctrl; \
  2611. }
  2612. // Control message map macros
  2613. #define RIBBON_GALLERY_CONTROL_HANDLER(id, func) \
  2614. if((uMsg == WM_COMMAND) && (id == LOWORD(wParam))) \
  2615. { \
  2616. bHandled = TRUE; \
  2617. lResult = func((UI_EXECUTIONVERB)HIWORD(wParam), LOWORD(wParam), (UINT)lParam, bHandled); \
  2618. if(bHandled) \
  2619. return TRUE; \
  2620. }
  2621. #define RIBBON_COMBO_CONTROL_HANDLER(id, func) \
  2622. RIBBON_GALLERY_CONTROL_HANDLER(id, func)
  2623. #define RIBBON_FONT_CONTROL_HANDLER(id, func) \
  2624. if((uMsg == WM_COMMAND) && (id == LOWORD(wParam))) \
  2625. { \
  2626. bHandled = TRUE; \
  2627. lResult = func((UI_EXECUTIONVERB)HIWORD(wParam), LOWORD(wParam), (CHARFORMAT2*)lParam, bHandled); \
  2628. if(bHandled) \
  2629. return TRUE; \
  2630. }
  2631. #define RIBBON_COLOR_CONTROL_HANDLER(id, func) \
  2632. if((uMsg == WM_COMMAND) && (id == LOWORD(wParam))) \
  2633. { \
  2634. bHandled = TRUE; \
  2635. lResult = func((UI_EXECUTIONVERB)HIWORD(wParam), LOWORD(wParam), (COLORREF)lParam, bHandled); \
  2636. if(bHandled) \
  2637. return TRUE; \
  2638. }
  2639. #define RIBBON_SPINNER_CONTROL_HANDLER(id, func) \
  2640. if((uMsg == WM_COMMAND) && (id == wParam)) \
  2641. { \
  2642. bHandled = TRUE; \
  2643. lResult = func((WORD)wParam, (LONG)lParam, bHandled); \
  2644. if(bHandled) \
  2645. return TRUE; \
  2646. }
  2647. #define RIBBON_FLOATSPINNER_CONTROL_HANDLER(id, func) \
  2648. if((uMsg == WM_COMMAND) && (id == wParam)) \
  2649. { \
  2650. bHandled = TRUE; \
  2651. lResult = func((WORD)wParam, (DOUBLE*)lParam, bHandled); \
  2652. if(bHandled) \
  2653. return TRUE; \
  2654. }
  2655. // Handler prototypes
  2656. /*
  2657. LRESULT OnRibbonGalleryCtrl(UI_EXECUTIONVERB verb, WORD wID, UINT uSel, BOOL& bHandled);
  2658. LRESULT OnRibbonComboCtrl(UI_EXECUTIONVERB verb, WORD wID, UINT uSel, BOOL& bHandled);
  2659. LRESULT OnRibbonFontCtrl(UI_EXECUTIONVERB verb, WORD wID, CHARFORMAT2* pcf, BOOL& bHandled);
  2660. LRESULT OnRibbonColorCtrl(UI_EXECUTIONVERB verb, WORD wID, COLORREF color, BOOL& bHandled);
  2661. LRESULT OnRibbonSpinnerCtrl(WORD wID, LONG lVal, BOOL& bHandled);
  2662. LRESULT OnRibbonFloatSpinnerCtrl(WORD wID, DOUBLE* pdVal, BOOL& bHandled);
  2663. */
  2664. ///////////////////////////////////////////////////////////////////////////////
  2665. // Ribbon frame classes
  2666. // CRibbonFrameWindowImplBase
  2667. //
  2668. template <class T, class TFrameImpl>
  2669. class ATL_NO_VTABLE CRibbonFrameWindowImplBase : public TFrameImpl, public RibbonUI::CRibbonImpl<T>
  2670. {
  2671. typedef TFrameImpl baseFrame;
  2672. bool m_bUseCommandBarBitmaps;
  2673. bool m_bWin7Fix;
  2674. public:
  2675. // Construction
  2676. CRibbonFrameWindowImplBase(bool bUseCommandBarBitmaps = true) :
  2677. m_bUseCommandBarBitmaps(bUseCommandBarBitmaps), m_bWin7Fix(false)
  2678. {
  2679. __if_not_exists(T::m_CmdBar)
  2680. {
  2681. m_bUseCommandBarBitmaps = false;
  2682. }
  2683. }
  2684. // Win7 Aero fix helpers
  2685. void ResetFrame()
  2686. {
  2687. const MARGINS margins = { 0, 0, 0, 0 };
  2688. ::DwmExtendFrameIntoClientArea(this->m_hWnd, &margins);
  2689. }
  2690. INT CalcWin7Fix()
  2691. {
  2692. ResetFrame();
  2693. RECT rc = {};
  2694. ::AdjustWindowRectEx(&rc, T::GetWndStyle(0), this->GetMenu() != NULL, T::GetWndExStyle(0));
  2695. return -rc.top;
  2696. }
  2697. bool NeedWin7Fix()
  2698. {
  2699. BOOL bComp = FALSE;
  2700. return m_bWin7Fix && RunTimeHelper::IsWin7() && SUCCEEDED(DwmIsCompositionEnabled(&bComp)) && bComp;
  2701. }
  2702. // Operations
  2703. bool UseCommandBarBitmaps(bool bUse)
  2704. {
  2705. __if_exists(T::m_CmdBar)
  2706. {
  2707. return m_bUseCommandBarBitmaps = bUse;
  2708. }
  2709. __if_not_exists(T::m_CmdBar)
  2710. {
  2711. (void)bUse; // avoid level 4 warning
  2712. return false;
  2713. }
  2714. }
  2715. bool ShowRibbonUI(bool bShow, INT32 imodes = UI_MAKEAPPMODE(0), LPCWSTR sResName = L"APPLICATION_RIBBON")
  2716. {
  2717. if (!RunTimeHelper::IsRibbonUIAvailable())
  2718. return false;
  2719. ATLASSERT(this->GetIUIFrameworkPtr());
  2720. if (this->IsRibbonUI() == bShow)
  2721. return bShow;
  2722. bool bVisible = (this->IsWindowVisible() != FALSE);
  2723. if(bVisible && !bShow)
  2724. this->SetRedraw(FALSE);
  2725. if (bShow && ::IsWindow(this->m_hWndToolBar))
  2726. {
  2727. ::ShowWindow(this->m_hWndToolBar, SW_HIDE);
  2728. UpdateLayout();
  2729. }
  2730. m_bWin7Fix = !bShow;
  2731. HRESULT hr = bShow ? this->CreateRibbon(sResName) : this->DestroyRibbon();
  2732. m_bWin7Fix = SUCCEEDED(hr) && !bShow;
  2733. if (SUCCEEDED(hr))
  2734. {
  2735. if(::IsWindow(this->m_hWndToolBar) && !bShow)
  2736. {
  2737. ::ShowWindow(this->m_hWndToolBar, SW_SHOWNA);
  2738. UpdateLayout();
  2739. }
  2740. else if (bShow)
  2741. {
  2742. this->PostMessage(WM_SIZE);
  2743. this->SetRibbonModes(imodes);
  2744. }
  2745. }
  2746. if(bVisible && !bShow)
  2747. {
  2748. this->SetRedraw(TRUE);
  2749. this->RedrawWindow(NULL, NULL, RDW_FRAME | RDW_ERASE | RDW_INVALIDATE | RDW_UPDATENOW | RDW_ALLCHILDREN);
  2750. }
  2751. return SUCCEEDED(hr) ? bShow : !bShow;
  2752. }
  2753. // Overrideables
  2754. HBITMAP OnRibbonQueryImage(UINT nCmdID, REFPROPERTYKEY key)
  2755. {
  2756. if ((key == UI_PKEY_SmallImage) && m_bUseCommandBarBitmaps)
  2757. {
  2758. if (HBITMAP hbm = GetCommandBarBitmap(nCmdID))
  2759. return (HBITMAP)::CopyImage(hbm, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  2760. }
  2761. return this->DefRibbonQueryImage(nCmdID);
  2762. }
  2763. BEGIN_MSG_MAP(CRibbonFrameWindowImplBase)
  2764. if (!this->IsRibbonUI() && NeedWin7Fix())
  2765. {
  2766. MESSAGE_HANDLER(WM_SIZING, OnSizing)
  2767. MESSAGE_HANDLER(WM_SIZE, OnSize)
  2768. MESSAGE_HANDLER(WM_ACTIVATE, OnActivate)
  2769. MESSAGE_HANDLER(WM_NCCALCSIZE, OnNCCalcSize)
  2770. }
  2771. CHAIN_MSG_MAP(CRibbonUpdateUI<T>)
  2772. CHAIN_MSG_MAP(baseFrame)
  2773. END_MSG_MAP()
  2774. // Message handlers for Win7 Aero
  2775. LRESULT OnSizing(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
  2776. {
  2777. switch (wParam)
  2778. {
  2779. case WMSZ_TOP:
  2780. case WMSZ_TOPLEFT:
  2781. case WMSZ_TOPRIGHT:
  2782. this->SetWindowPos(NULL, (LPRECT)lParam, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
  2783. break;
  2784. default:
  2785. this->DefWindowProc();
  2786. break;
  2787. }
  2788. return 1; // handled
  2789. }
  2790. LRESULT OnSize(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  2791. {
  2792. if (wParam != SIZE_MINIMIZED)
  2793. this->SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
  2794. bHandled = FALSE;
  2795. return 1;
  2796. }
  2797. LRESULT OnActivate(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& bHandled)
  2798. {
  2799. if(wParam != WA_INACTIVE)
  2800. this->SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
  2801. bHandled = FALSE;
  2802. return 1;
  2803. }
  2804. LRESULT OnNCCalcSize(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
  2805. {
  2806. ATLASSERT(!this->IsRibbonUI() && NeedWin7Fix());
  2807. LRESULT lRet = this->DefWindowProc();
  2808. if(wParam)
  2809. {
  2810. LPNCCALCSIZE_PARAMS pParams = (LPNCCALCSIZE_PARAMS)lParam;
  2811. pParams->rgrc[0].top = pParams->rgrc[1].top + CalcWin7Fix();
  2812. }
  2813. return lRet;
  2814. }
  2815. // Overrides
  2816. void UpdateLayout(BOOL bResizeBars = TRUE)
  2817. {
  2818. RECT rect = {};
  2819. this->GetClientRect(&rect);
  2820. if (this->IsRibbonUI() && !this->IsRibbonHidden())
  2821. {
  2822. rect.top += this->GetRibbonHeight();
  2823. }
  2824. else if (!this->IsRibbonUI() && NeedWin7Fix())
  2825. {
  2826. ResetFrame();
  2827. }
  2828. // position bars and offset their dimensions
  2829. this->UpdateBarsPosition(rect, bResizeBars);
  2830. // resize client window
  2831. if(this->m_hWndClient != NULL)
  2832. ::SetWindowPos(this->m_hWndClient, NULL, rect.left, rect.top,
  2833. rect.right - rect.left, rect.bottom - rect.top,
  2834. SWP_NOZORDER | SWP_NOACTIVATE);
  2835. }
  2836. // Implementation
  2837. HBITMAP GetCommandBarBitmap(UINT nCmdID)
  2838. {
  2839. __if_exists (T::m_CmdBar)
  2840. {
  2841. ATLASSERT(RunTimeHelper::IsVista());
  2842. T* pT =static_cast<T*>(this);
  2843. int nIndex = pT->m_CmdBar.m_arrCommand.Find((WORD&)nCmdID);
  2844. return (nIndex == -1) ? NULL : pT->m_CmdBar.m_arrVistaBitmap[nIndex];
  2845. }
  2846. __if_not_exists (T::m_CmdBar)
  2847. {
  2848. (void)nCmdID; // avoid level 4 warning
  2849. return NULL;
  2850. }
  2851. }
  2852. };
  2853. // CRibbonFrameWindowImpl
  2854. //
  2855. template <class T, class TBase = ATL::CWindow, class TWinTraits = ATL::CFrameWinTraits>
  2856. class ATL_NO_VTABLE CRibbonFrameWindowImpl : public CRibbonFrameWindowImplBase<T, CFrameWindowImpl<T, TBase, TWinTraits>>
  2857. { };
  2858. // CRibbonMDIFrameWindowImpl
  2859. //
  2860. template <class T, class TBase = CMDIWindow, class TWinTraits = ATL::CFrameWinTraits>
  2861. class ATL_NO_VTABLE CRibbonMDIFrameWindowImpl : public CRibbonFrameWindowImplBase<T, CMDIFrameWindowImpl<T, TBase, TWinTraits>>
  2862. { };
  2863. ///////////////////////////////////////////////////////////////////////////////
  2864. // CRibbonPersist helper for RibbonUI persistency
  2865. class CRibbonPersist
  2866. {
  2867. public:
  2868. CRibbonPersist(LPCWSTR sAppKey)
  2869. {
  2870. ATLASSERT(sAppKey && *sAppKey);
  2871. m_Key.Create(HKEY_CURRENT_USER, sAppKey);
  2872. ATLASSERT(m_Key.m_hKey);
  2873. }
  2874. ATL::CRegKey m_Key;
  2875. LONG Save(bool bRibbonUI, HGLOBAL hgSettings = NULL)
  2876. {
  2877. ATL::CRegKey key;
  2878. const DWORD dwUI = bRibbonUI;
  2879. LONG lRet = key.Create(m_Key, L"Ribbon");
  2880. if(lRet != ERROR_SUCCESS)
  2881. return lRet;
  2882. lRet = key.SetDWORDValue(L"UI", dwUI);
  2883. if(lRet != ERROR_SUCCESS)
  2884. return lRet;
  2885. if (hgSettings != NULL)
  2886. {
  2887. LPBYTE pVal = (LPBYTE)::GlobalLock(hgSettings);
  2888. if (pVal != NULL)
  2889. {
  2890. lRet = key.SetBinaryValue(L"Settings", pVal, (ULONG)::GlobalSize(hgSettings));
  2891. ::GlobalUnlock(hgSettings);
  2892. }
  2893. else
  2894. {
  2895. lRet = GetLastError();
  2896. }
  2897. }
  2898. return lRet;
  2899. }
  2900. LONG Restore(bool& bRibbonUI, HGLOBAL& hgSettings)
  2901. {
  2902. ATLASSERT(hgSettings == NULL);
  2903. ATL::CRegKey key;
  2904. LONG lRet = key.Open(m_Key, L"Ribbon");
  2905. if(lRet != ERROR_SUCCESS)
  2906. return lRet;
  2907. DWORD dwUI = 0xffff;
  2908. lRet = key.QueryDWORDValue(L"UI", dwUI);
  2909. if(lRet == ERROR_SUCCESS)
  2910. bRibbonUI = dwUI == 1;
  2911. else
  2912. return lRet;
  2913. ULONG ulSize = 0;
  2914. lRet = key.QueryBinaryValue(L"Settings", NULL, &ulSize);
  2915. if (lRet == ERROR_SUCCESS)
  2916. {
  2917. ATLASSERT(ulSize != 0);
  2918. hgSettings = ::GlobalAlloc(GHND, ulSize);
  2919. if (hgSettings != NULL)
  2920. {
  2921. LPBYTE pData = (LPBYTE)::GlobalLock(hgSettings);
  2922. if (pData != NULL)
  2923. {
  2924. lRet = key.QueryBinaryValue(L"Settings", pData, &ulSize);
  2925. }
  2926. else
  2927. {
  2928. lRet = GetLastError();
  2929. ::GlobalFree(hgSettings);
  2930. hgSettings = NULL;
  2931. }
  2932. }
  2933. else
  2934. {
  2935. lRet = GetLastError();
  2936. }
  2937. }
  2938. return lRet;
  2939. }
  2940. LONG Delete()
  2941. {
  2942. return m_Key.DeleteSubKey(L"Ribbon");
  2943. }
  2944. };
  2945. } // namespace WTL
  2946. #endif // __ATLRIBBON_H__