aduiImage.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 Autodesk, Inc. All rights reserved.
  4. //
  5. // Use of this software is subject to the terms of the Autodesk license
  6. // agreement provided at the time of installation or download, or which
  7. // otherwise accompanies this software in either electronic or hard copy form.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. //
  11. // DESCRIPTION:
  12. //
  13. // Declarations for various image classes:
  14. //
  15. // CAdUiImage
  16. // CAdUiImageResource
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19. #ifndef _aduiImage_h
  20. #define _aduiImage_h
  21. #if _MSC_VER >= 1000
  22. #pragma once
  23. #endif // _MSC_VER >= 1000
  24. #ifndef _WINDOWS_ // From windows.h, AFX and MFC don't mix
  25. #include <afxwin.h> // MFC core and standard components
  26. #include <afxext.h> // MFC extension
  27. #endif
  28. // GDIPlus will not compile with our MACRO for "new", therefore we are saving off
  29. // our macro while we compile GDIPlus, then set it back.
  30. #pragma push_macro("new")
  31. #undef new
  32. #include <minmax.h>
  33. #include <gdiplus.h>
  34. #undef min
  35. #undef max
  36. #pragma pop_macro("new")
  37. #include <WinDef.h> //For definitions of HINSTANCE...etc if not already included
  38. #include "aduiTheme.h"
  39. #define ICON_16 16
  40. #define ICON_32 32
  41. ///////////////////////////////////////////////////////////////////////////////
  42. /// class CAdUiImage
  43. ///////////////////////////////////////////////////////////////////////////////
  44. /// <summary>
  45. /// Image class provides dark and light theme support to get dark or light theme
  46. /// image resource.
  47. /// </summary>
  48. class ADUI_PORT CAdUiImage
  49. {
  50. public:
  51. /// <summary>
  52. /// Create a new CAdUiImage object.
  53. /// </summary>
  54. /// <param name="bIsThemed">
  55. /// If true, dark/light theme support for image resource.
  56. /// </param>
  57. CAdUiImage(bool bIsThemed = false);
  58. /// <summary>
  59. /// Create a new CAdUiImage object with resource file name.
  60. /// </summary>
  61. /// <param name="pFile">
  62. /// Image resource file name.
  63. /// </param>
  64. /// <param name="bIsThemed">
  65. /// If true, dark/light theme support for image resource.
  66. /// </param>
  67. CAdUiImage(LPCWSTR pFile, bool bIsThemed = false);
  68. /// <summary>
  69. /// Create a new CAdUiImage object from bitmap handle.
  70. /// </summary>
  71. /// <param name="hbmp">
  72. /// Bitmap handle for creating new CAdUiImage class
  73. /// </param>
  74. /// <param name="format">
  75. /// Specify the image format.
  76. /// </param>
  77. CAdUiImage(HBITMAP hbmp, Gdiplus::PixelFormat format);
  78. /// <summary>
  79. /// Destroys this CAdUiImage object.
  80. /// </summary>
  81. virtual ~CAdUiImage();
  82. /// <summary>
  83. /// Set CAdUiImage resource empty.
  84. /// </summary>
  85. virtual void Empty();
  86. /// <summary>
  87. /// Reload image resource.
  88. /// If this class is themed CAdUiImage class, resource will be reloaded based on drak/light theme.
  89. /// </summary>
  90. virtual bool Reload();
  91. /// <summary>
  92. /// Load image resource by resource file name.
  93. /// </summary>
  94. /// <param name="pFile">
  95. /// Specify the resource file name.
  96. /// </param>
  97. /// <returns>
  98. /// Reture true if load resource success. Otherwise, return false.
  99. /// </returns>
  100. bool Load(LPCWSTR pFile);
  101. /// <summary>
  102. /// Get whether this object is themed.
  103. /// </summary>
  104. /// <returns>
  105. /// whether this object is themed.
  106. /// </returns>
  107. bool IsThemed() { return m_bIsThemed; }
  108. /// <summary>
  109. /// Set this object IsThemed flag.
  110. /// </summary>
  111. /// <param name="bIsThemed">
  112. /// If true, dark/light theme support for image resource.
  113. /// </param>
  114. void SetIsThemed(bool bIsThemed) { m_bIsThemed = bIsThemed; }
  115. /// <summary>
  116. /// Set CAdUiTheme that is used as current theme.
  117. /// </summary>
  118. /// <param name="theme">
  119. /// Specify the new CAdUiTheme.
  120. /// </param>
  121. void SetTheme(const CAdUiTheme * theme);
  122. /// <summary>
  123. /// Get image width.
  124. /// </summary>
  125. /// <returns>
  126. /// Image width.
  127. /// </returns>
  128. unsigned int GetWidth() const;
  129. /// <summary>
  130. /// Get iamge height.
  131. /// </summary>
  132. /// <returns>
  133. /// Image height.
  134. /// </returns>
  135. unsigned int GetHeight() const;
  136. /// <summary>
  137. /// Get Gdiplus::Bitmap pointer.
  138. /// </summary>
  139. /// <returns>
  140. /// Gdiplus::Bitmap pointer.
  141. /// </returns>
  142. Gdiplus::Bitmap* GetBitmap() const { return m_pBitmap; }
  143. /// <summary>
  144. /// Draw current image resource.
  145. /// </summary>
  146. /// <param name="pDC">
  147. /// Specify DC that used to draw image resource.
  148. /// </param>
  149. /// <param name="x">
  150. /// Start point x value.
  151. /// </param>
  152. /// <param name="y">
  153. /// Start point y value.
  154. /// </param>
  155. /// <param name="isDisabled">
  156. /// If true, draw image without colour.
  157. /// </param>
  158. /// <returns>
  159. /// If success, return true. Otherwise, return false.
  160. /// </returns>
  161. bool DrawImage(CDC* pDC, int x, int y, bool isDisabled = false);
  162. /// <summary>
  163. /// Draw current image resource.
  164. /// </summary>
  165. /// <param name="pDC">
  166. /// Specify DC that used to draw image resource.
  167. /// </param>
  168. /// <param name="x">
  169. /// Start point x value.
  170. /// </param>
  171. /// <param name="y">
  172. /// Start point y value.
  173. /// </param>
  174. /// <param name="width">
  175. /// Specify the image drawing width.
  176. /// </param>
  177. /// <param name="height">
  178. /// Specify the image drawing height.
  179. /// </param>
  180. /// <param name="isDisabled">
  181. /// If true, draw image without colour.
  182. /// </param>
  183. /// <returns>
  184. /// If success, return true. Otherwise, return false.
  185. /// </returns>
  186. bool DrawImage(CDC* pDC, int x, int y, int width, int height, bool isDisabled = false);
  187. /// <summary>
  188. /// Draw current image resource that is scaled based on its DPI and the screen DPI.
  189. /// </summary>
  190. /// <param name="pDC">
  191. /// Specify DC that used to draw image resource.
  192. /// </param>
  193. /// <param name="x">
  194. /// Start point x value.
  195. /// </param>
  196. /// <param name="y">
  197. /// Start point y value.
  198. /// </param>
  199. /// <param name="srcx">
  200. /// Integer that specifies the x-coordinate of the upper-left corner of the portion of the source image to be drawn.
  201. /// </param>
  202. /// <param name="srcy">
  203. /// Integer that specifies the y-coordinate of the upper-left corner of the portion of the source image to be drawn.
  204. /// </param>
  205. /// <param name="srcwidth">
  206. /// Integer that specifies the width of the portion of the source image to be drawn.
  207. /// </param>
  208. /// <param name="srcheight">
  209. /// Integer that specifies the height of the portion of the source image to be drawn.
  210. /// </param>
  211. /// <param name="isDisabled">
  212. /// If true, draw image without colour.
  213. /// </param>
  214. /// <returns>
  215. /// If success, return true. Otherwise, return false.
  216. /// </returns>
  217. bool DrawImage(CDC* pDC, int x, int y, int srcx, int srcy, int srcwidth, int srcheight, bool isDisabled = false);
  218. /// <summary>
  219. /// Get themed file name based on the orginal file name.
  220. /// </summary>
  221. /// <param name="pName">
  222. /// Specify the orginal file name.
  223. /// </param>
  224. /// <param name="pTheme">
  225. /// Specify the theme that is used to decide theme.
  226. /// </param>
  227. /// <returns>
  228. /// Reture themed file name based on the orginal file name.
  229. /// </returns>
  230. static CString GetThemedFileName(LPCTSTR pName, const CAdUiTheme * pTheme = NULL);
  231. protected:
  232. Gdiplus::Bitmap* m_pBitmap;
  233. bool m_bIsThemed;
  234. const CAdUiTheme* m_pTheme;
  235. CString m_name;
  236. /// <summary>
  237. /// Return true, if the theme is dark.
  238. /// </summary>
  239. /// <returns>
  240. /// Return true, if the theme is dark.
  241. /// </returns>
  242. bool CAdUiImage::IsThemeDark() const;
  243. /// <summary>
  244. /// Return true, if the current palette theme is dark.
  245. /// </summary>
  246. /// <returns>
  247. /// Return true, if the current palette theme is dark.
  248. /// </returns>
  249. static bool IsCurrentPaletteThemeDark();
  250. private:
  251. void commonCtr(bool bIsThemed);
  252. void clear();
  253. bool drawImage(Gdiplus::Graphics & graphics, int x, int y, int width, int height, bool isDisabled);
  254. static unsigned int g_instanceCount;
  255. static Gdiplus::GdiplusStartupInput g_gdiplusStartupInput;
  256. static ULONG_PTR g_gdiplusToken;
  257. // not allowed
  258. CAdUiImage(const CAdUiImage & image);
  259. CAdUiImage& operator=(const CAdUiImage & image);
  260. };
  261. ///////////////////////////////////////////////////////////////////////////////
  262. /// class CAdUiImageResource
  263. ///////////////////////////////////////////////////////////////////////////////
  264. /// <summary>
  265. /// Image class provides dark and light theme support to get dark or light theme
  266. /// resource from resource ID string.
  267. /// </summary>
  268. class ADUI_PORT CAdUiImageResource : public CAdUiImage
  269. {
  270. protected:
  271. HGLOBAL m_hBuffer;
  272. UINT m_lightId;
  273. UINT m_darkId;
  274. LPCTSTR m_pType;
  275. CString m_strType;
  276. HMODULE m_hInst;
  277. public:
  278. /// <summary>
  279. /// Create a new object without resource.
  280. /// </summary>
  281. CAdUiImageResource();
  282. /// <summary>
  283. /// Create a new CAdUiImageResource object.
  284. /// </summary>
  285. /// <param name="bIsThemed">
  286. /// If true, dark/light theme support for image resource.
  287. /// </param>
  288. CAdUiImageResource(bool bIsThemed);
  289. /// <summary>
  290. /// Create a new CAdUiImageResource object by resource ID string.
  291. /// </summary>
  292. /// <param name="pName">
  293. /// Specify the resource ID string.
  294. /// </param>
  295. /// <param name="pType">
  296. /// Specify the type of resource data.
  297. /// </param>
  298. /// <param name="hInst">
  299. /// Specify resource module handle.
  300. /// </param>
  301. /// <param name="bIsThemed">
  302. /// If true, dark/light theme support for image resource.
  303. /// </param>
  304. CAdUiImageResource(LPCTSTR pName, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL, bool bIsThemed = false);
  305. /// <summary>
  306. /// Create a new CAdUiImageResource object by resource ID.
  307. /// </summary>
  308. /// <param name="id">
  309. /// Specify the resource ID.
  310. /// </param>
  311. /// <param name="pType">
  312. /// Specify the type of resource data.
  313. /// </param>
  314. /// <param name="hInst">
  315. /// Specify resource module handle.
  316. /// </param>
  317. CAdUiImageResource(UINT id, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL);
  318. /// <summary>
  319. /// Create a new CAdUiImageResource object by resource ID.
  320. /// </summary>
  321. /// <param name="id">
  322. /// Specify the resource ID.
  323. /// </param>
  324. /// <param name="type">
  325. /// Specify the type of resource data.
  326. /// </param>
  327. /// <param name="hInst">
  328. /// Specify resource module handle.
  329. /// </param>
  330. CAdUiImageResource(UINT id, UINT type, HMODULE hInst = NULL);
  331. /// <summary>
  332. /// Create a new CAdUiImageResource object.
  333. /// </summary>
  334. /// <param name="lightId">
  335. /// Specify the light theme resource ID.
  336. /// </param>
  337. /// <param name="darkId">
  338. /// Specify the dark theme resource ID.
  339. /// </param>
  340. /// <param name="pType">
  341. /// Specify the type of resource data.
  342. /// </param>
  343. /// <param name="hInst">
  344. /// Specify resource module handle.
  345. /// </param>
  346. CAdUiImageResource(UINT lightId, UINT darkId, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL);
  347. /// <summary>
  348. /// Create a new CAdUiImageResource object.
  349. /// </summary>
  350. /// <param name="lightId">
  351. /// Specify the light theme resource ID.
  352. /// </param>
  353. /// <param name="darkId">
  354. /// Specify the dark theme resource ID.
  355. /// </param>
  356. /// <param name="type">
  357. /// Specify the type of resource data.
  358. /// </param>
  359. /// <param name="hInst">
  360. /// Specify resource module handle.
  361. /// </param>
  362. CAdUiImageResource(UINT lightId, UINT darkId, UINT type, HMODULE hInst = NULL);
  363. /// <summary>
  364. /// Destroys this CAdUiImageResource object.
  365. /// </summary>
  366. virtual ~CAdUiImageResource();
  367. /// <summary>
  368. /// Set CAdUiImage resource empty.
  369. /// </summary>
  370. virtual void Empty();
  371. /// <summary>
  372. /// Reload image resource.
  373. /// If this class is themed CAdUiImage class, resource will be reloaded based on drak/light theme.
  374. /// </summary>
  375. virtual bool Reload();
  376. /// <summary>
  377. /// Load resource by resource ID string.
  378. /// </summary>
  379. /// <param name="pName">
  380. /// Specify resource ID string.
  381. /// </param>
  382. /// <param name="pType">
  383. /// Specify the type of resource data.
  384. /// </param>
  385. /// <param name="hInst">
  386. /// Specify resource module handle.
  387. /// </param>
  388. /// <returns>
  389. /// If success, return true. Otherwise, return false.
  390. /// </returns>
  391. bool Load(LPCTSTR pName, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL);
  392. /// <summary>
  393. /// Load resource by resource ID.
  394. /// </summary>
  395. /// <param name="id">
  396. /// Specify resource ID.
  397. /// </param>
  398. /// <param name="pType">
  399. /// Specify the type of resource data.
  400. /// </param>
  401. /// <param name="hInst">
  402. /// Specify resource module handle.
  403. /// </param>
  404. /// <returns>
  405. /// If success, return true. Otherwise, return false.
  406. /// </returns>
  407. bool Load(UINT id, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL);
  408. /// <summary>
  409. /// Load resource by resource ID.
  410. /// </summary>
  411. /// <param name="id">
  412. /// Specify resource ID.
  413. /// </param>
  414. /// <param name="type">
  415. /// Specify the type of resource data.
  416. /// </param>
  417. /// <param name="hInst">
  418. /// Specify resource module handle.
  419. /// </param>
  420. /// <returns>
  421. /// If success, return true. Otherwise, return false.
  422. /// </returns>
  423. bool Load(UINT id, UINT type, HMODULE hInst = NULL);
  424. /// <summary>
  425. /// Load resource by resource ID.
  426. /// </summary>
  427. /// <param name="lightId">
  428. /// Specify the light theme resource ID.
  429. /// </param>
  430. /// <param name="darkId">
  431. /// Specify the dark theme resource ID.
  432. /// </param>
  433. /// <param name="pType">
  434. /// Specify the type of resource data.
  435. /// </param>
  436. /// <param name="hInst">
  437. /// Specify resource module handle.
  438. /// </param>
  439. /// <returns>
  440. /// If success, return true. Otherwise, return false.
  441. /// </returns>
  442. bool Load(UINT lightId, UINT darkId, LPCTSTR pType = RT_RCDATA, HMODULE hInst = NULL);
  443. /// <summary>
  444. /// Load resource by resource ID.
  445. /// </summary>
  446. /// <param name="lightId">
  447. /// Specify the light theme resource ID.
  448. /// </param>
  449. /// <param name="darkId">
  450. /// Specify the dark theme resource ID.
  451. /// </param>
  452. /// <param name="type">
  453. /// Specify the type of resource data.
  454. /// </param>
  455. /// <param name="hInst">
  456. /// Specify resource module handle.
  457. /// </param>
  458. /// <returns>
  459. /// If success, return true. Otherwise, return false.
  460. /// </returns>>
  461. bool Load(UINT lightId, UINT darkId, UINT type, HMODULE hInst = NULL);
  462. /// <summary>
  463. /// Get themed file name based on the orginal file name.
  464. /// </summary>
  465. /// <param name="pName">
  466. /// Specify the orginal file name.
  467. /// </param>
  468. /// <param name="pTheme">
  469. /// Specify the theme that is used to decide theme.
  470. /// </param>
  471. /// <returns>
  472. /// Reture themed file name based on the orginal file name.
  473. /// </returns>
  474. static CString GetThemedResourceName(LPCTSTR pName, const CAdUiTheme * pTheme = NULL);
  475. private:
  476. bool loadIcon(LPCTSTR pName, HMODULE hInst = NULL);
  477. bool loadResource(LPCTSTR pName, LPCTSTR pType, HMODULE hInst);
  478. bool isPredefinedResourceType(LPCTSTR pType);
  479. bool isValidPredefinedResourceType(LPCTSTR pType);
  480. void saveType(LPCTSTR pType);
  481. private:
  482. void clear();
  483. // not allowed
  484. CAdUiImageResource(const CAdUiImageResource & image);
  485. CAdUiImageResource& operator=(const CAdUiImageResource & image);
  486. };
  487. #endif // _aduiImage_h