stockobjects.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "main.h"
  2. #include "./stockobjects.h"
  3. #include "./colors.h"
  4. typedef struct MlStockObjects
  5. {
  6. CRITICAL_SECTION lock;
  7. HDC cachedDc;
  8. HFONT skinFont;
  9. HFONT systemFont;
  10. HBRUSH brushes[BRUSH_MAX - BRUSH_MIN + 1];
  11. HPEN pens[PEN_MAX - PEN_MIN + 1];
  12. } MlStockObjects;
  13. static MlStockObjects stockObjects;
  14. static void
  15. MlStockObjects_ResetEx(MlStockObjects *self)
  16. {
  17. INT index;
  18. if (NULL != self->cachedDc)
  19. {
  20. DeleteDC(self->cachedDc);
  21. self->cachedDc = NULL;
  22. }
  23. if (NULL != self->skinFont)
  24. {
  25. DeleteObject(self->skinFont);
  26. self->skinFont = NULL;
  27. }
  28. if (NULL != self->systemFont)
  29. {
  30. DeleteObject(self->systemFont);
  31. self->systemFont = NULL;
  32. }
  33. for (index = 0; index < ARRAYSIZE(self->brushes); index++)
  34. {
  35. if (NULL != self->brushes[index])
  36. DeleteObject(self->brushes[index]);
  37. }
  38. ZeroMemory(self->brushes, sizeof(self->brushes));
  39. for (index = 0; index < ARRAYSIZE(self->pens); index++)
  40. {
  41. if (NULL != self->pens[index])
  42. DeleteObject(self->pens[index]);
  43. }
  44. ZeroMemory(self->pens, sizeof(self->pens));
  45. }
  46. static BYTE
  47. MlStockObjects_GetSysFontQuality()
  48. {
  49. BOOL smoothingEnabled;
  50. if (FALSE == SystemParametersInfoW(SPI_GETFONTSMOOTHING, 0, &smoothingEnabled, 0) ||
  51. FALSE == smoothingEnabled)
  52. {
  53. return DEFAULT_QUALITY;
  54. }
  55. UINT smootingType;
  56. if (FALSE == SystemParametersInfoW(SPI_GETFONTSMOOTHINGTYPE, 0, &smootingType, 0))
  57. return DEFAULT_QUALITY;
  58. if (FE_FONTSMOOTHINGCLEARTYPE == smootingType)
  59. return CLEARTYPE_QUALITY;
  60. return ANTIALIASED_QUALITY;
  61. }
  62. static HDC
  63. MlStockObjects_GetCachedDc(MlStockObjects *self)
  64. {
  65. if (NULL == self->cachedDc)
  66. {
  67. HDC baseDC = GetDCEx(g_hwnd, NULL, DCX_WINDOW | DCX_CACHE | DCX_NORESETATTRS);
  68. self->cachedDc = CreateCompatibleDC(baseDC);
  69. ReleaseDC(g_hwnd, baseDC);
  70. }
  71. return self->cachedDc;
  72. }
  73. static HFONT
  74. MlStockObjects_GetSkinFont(MlStockObjects *self)
  75. {
  76. if (NULL == self->skinFont &&
  77. NULL != g_config &&
  78. 0 != g_config->ReadInt(L"plfont_everywhere", 1))
  79. {
  80. INT height = (INT)SendMessageW(plugin.hwndParent, WM_WA_IPC, 3, IPC_GET_GENSKINBITMAP);
  81. DWORD charset = (DWORD)SendMessageW(plugin.hwndParent, WM_WA_IPC, 2, IPC_GET_GENSKINBITMAP);
  82. char *fontname = (char*)SendMessageW(plugin.hwndParent, WM_WA_IPC, 1, IPC_GET_GENSKINBITMAP);
  83. self->skinFont = CreateFontA(-height, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, charset, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DRAFT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontname);
  84. }
  85. return self->skinFont;
  86. }
  87. static HFONT
  88. MlStockObjects_GetSystemFont(MlStockObjects *self)
  89. {
  90. if (NULL == self->systemFont)
  91. {
  92. LOGFONTW lf;
  93. if (FALSE == SystemParametersInfoW(SPI_GETICONTITLELOGFONT, sizeof(lf), &lf, 0))
  94. return NULL;
  95. lf.lfQuality = MlStockObjects_GetSysFontQuality();
  96. self->systemFont = CreateFontIndirectW(&lf);
  97. }
  98. return self->systemFont;
  99. }
  100. static HBRUSH
  101. MlStockObjects_GetBrush(MlStockObjects *self, UINT type)
  102. {
  103. if (NULL == self->brushes[type - BRUSH_MIN])
  104. {
  105. INT color;
  106. switch(type)
  107. {
  108. case WNDBCK_BRUSH: color = WADLG_WNDBG; break;
  109. case ITEMBCK_BRUSH: color = WADLG_ITEMBG; break;
  110. case HILITE_BRUSH: color = WADLG_HILITE; break;
  111. case ITEMTEXT_BRUSH: color = WADLG_ITEMFG; break;
  112. default: return NULL;
  113. }
  114. self->brushes[type - BRUSH_MIN] = CreateSolidBrush(WADlg_getColor(color));
  115. }
  116. return self->brushes[type - BRUSH_MIN];
  117. }
  118. static HPEN
  119. MlStockObjects_GetPen(MlStockObjects *self, UINT type)
  120. {
  121. if (NULL == self->pens[type - PEN_MIN])
  122. {
  123. COLORREF rgb;
  124. switch(type)
  125. {
  126. case HILITE_PEN: rgb = WADlg_getColor(WADLG_HILITE); break;
  127. case HEADERTOP_PEN: rgb = WADlg_getColor(WADLG_LISTHEADER_FRAME_TOPCOLOR); break;
  128. case HEADERMIDDLE_PEN: rgb = WADlg_getColor(WADLG_LISTHEADER_FRAME_MIDDLECOLOR); break;
  129. case HEADERBOTTOM_PEN: rgb = WADlg_getColor(WADLG_LISTHEADER_FRAME_BOTTOMCOLOR); break;
  130. case WNDBCK_PEN: rgb = WADlg_getColor(WADLG_WNDBG); break;
  131. case MENUBORDER_PEN: MLGetSkinColor(MLSO_MENU, MP_FRAME, 0, &rgb); break;
  132. case TOOLTIPBORDER_PEN: MLGetSkinColor(MLSO_TOOLTIP, TTP_FRAME, 0, &rgb); break;
  133. case ITEMBCK_PEN: rgb = WADlg_getColor(WADLG_ITEMBG); break;
  134. case ITEMTEXT_PEN: rgb = WADlg_getColor(WADLG_ITEMFG); break;
  135. default: return NULL;
  136. }
  137. self->pens[type - PEN_MIN] = CreatePen(PS_SOLID, 0, rgb);
  138. }
  139. return self->pens[type - PEN_MIN];
  140. }
  141. static HANDLE
  142. MlStockObjects_GetEx(MlStockObjects *self, UINT type)
  143. {
  144. HANDLE handle;
  145. EnterCriticalSection(&self->lock);
  146. if (CACHED_DC == type)
  147. handle = MlStockObjects_GetCachedDc(self);
  148. else if (SKIN_FONT == type)
  149. handle = MlStockObjects_GetSkinFont(self);
  150. else if (DEFAULT_FONT == type)
  151. handle = MlStockObjects_GetSystemFont(self);
  152. else if (BRUSH_MIN <= type && BRUSH_MAX >= type)
  153. handle = MlStockObjects_GetBrush(self, type);
  154. else if (PEN_MIN <= type && PEN_MAX >= type)
  155. handle = MlStockObjects_GetPen(self, type);
  156. else
  157. handle = NULL;
  158. LeaveCriticalSection(&self->lock);
  159. return handle;
  160. }
  161. void
  162. MlStockObjects_Init()
  163. {
  164. ZeroMemory(&stockObjects, sizeof(MlStockObjects));
  165. InitializeCriticalSection(&stockObjects.lock);
  166. }
  167. void
  168. MlStockObjects_Free()
  169. {
  170. MlStockObjects_ResetEx(&stockObjects);
  171. DeleteCriticalSection(&stockObjects.lock);
  172. }
  173. HANDLE
  174. MlStockObjects_Get(UINT type)
  175. {
  176. return MlStockObjects_GetEx(&stockObjects, type);
  177. }
  178. void
  179. MlStockObjects_Reset()
  180. {
  181. MlStockObjects_ResetEx(&stockObjects);
  182. }