widgetStyle.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "main.h"
  2. #include "./widgetStyle.h"
  3. static void
  4. WidgetStyle_DeleteGdiObject(UINT flags, UINT flagsMask, HGDIOBJ object)
  5. {
  6. if (NULL != object && 0 != (flagsMask & flags))
  7. DeleteObject(object);
  8. }
  9. static BOOL
  10. WidgetStyle_SetBrushInt(HBRUSH *brush, WidgetStyleFlags *flags, WidgetStyleFlags flagsMask,
  11. HBRUSH sourceBrush, WidgetStyleAssignFlags assignFlags)
  12. {
  13. LOGBRUSH lb;
  14. WidgetStyle_DeleteGdiObject(*flags, flagsMask, *brush);
  15. if (0 == (WIDGETSTYLE_COPY_OBJECT & assignFlags))
  16. {
  17. if (0 == (WIDGETSTYLE_OWN_OBJECT & assignFlags))
  18. *flags &= ~flagsMask;
  19. else
  20. *flags |= flagsMask;
  21. *brush = sourceBrush;
  22. return TRUE;
  23. }
  24. *brush = (sizeof(lb) == GetObjectW(sourceBrush, sizeof(lb), &lb)) ?
  25. CreateBrushIndirect(&lb) : NULL;
  26. if (NULL == *brush)
  27. {
  28. *flags &= ~flagsMask;
  29. return FALSE;
  30. }
  31. *flags |= flagsMask;
  32. return TRUE;
  33. }
  34. static BOOL
  35. WidgetStyle_SetFontInt(HFONT *font, WidgetStyleFlags *flags, WidgetStyleFlags flagsMask,
  36. HFONT sourceFont, WidgetStyleAssignFlags assignFlags)
  37. {
  38. LOGFONTW lf = {0};
  39. WidgetStyle_DeleteGdiObject(*flags, flagsMask, *font);
  40. if (0 == (WIDGETSTYLE_COPY_OBJECT & assignFlags))
  41. {
  42. if (0 == (WIDGETSTYLE_OWN_OBJECT & assignFlags))
  43. *flags &= ~flagsMask;
  44. else
  45. *flags |= flagsMask;
  46. *font = sourceFont;
  47. return TRUE;
  48. }
  49. *font = (sizeof(lf) == GetObjectW(sourceFont, sizeof(lf), &lf)) ? CreateFontIndirectW(&lf) : NULL;
  50. if (NULL == *font)
  51. {
  52. *flags &= ~flagsMask;
  53. return FALSE;
  54. }
  55. *flags |= flagsMask;
  56. return TRUE;
  57. }
  58. void
  59. WidgetStyle_Free(WidgetStyle *self)
  60. {
  61. if (NULL == self)
  62. return;
  63. WidgetStyle_DeleteGdiObject(self->flags, WIDGETSTYLE_OWN_TEXT_FONT, self->textFont);
  64. WidgetStyle_DeleteGdiObject(self->flags, WIDGETSTYLE_OWN_TITLE_FONT, self->titleFont);
  65. WidgetStyle_DeleteGdiObject(self->flags, WIDGETSTYLE_OWN_CATEGORY_FONT, self->categoryFont);
  66. WidgetStyle_DeleteGdiObject(self->flags, WIDGETSTYLE_OWN_BACK_BRUSH, self->backBrush);
  67. WidgetStyle_DeleteGdiObject(self->flags, WIDGETSTYLE_OWN_CATEGORY_BRUSH, self->categoryBrush);
  68. }
  69. BOOL
  70. WidgetStyle_SetBackBrush(WidgetStyle *self, HBRUSH brush, WidgetStyleAssignFlags flags)
  71. {
  72. return WidgetStyle_SetBrushInt(&self->backBrush, &self->flags,
  73. WIDGETSTYLE_OWN_BACK_BRUSH, brush, flags);
  74. }
  75. BOOL
  76. WidgetStyle_SetCategoryBrush(WidgetStyle *self, HBRUSH brush, WidgetStyleAssignFlags flags)
  77. {
  78. return WidgetStyle_SetBrushInt(&self->categoryBrush, &self->flags,
  79. WIDGETSTYLE_OWN_CATEGORY_BRUSH, brush, flags);
  80. }
  81. BOOL
  82. WidgetStyle_SetTextFont(WidgetStyle *self, HFONT font, WidgetStyleAssignFlags flags)
  83. {
  84. return WidgetStyle_SetFontInt(&self->textFont, &self->flags,
  85. WIDGETSTYLE_OWN_TEXT_FONT, font, flags);
  86. }
  87. BOOL
  88. WidgetStyle_SetTitleFont(WidgetStyle *self, HFONT font, WidgetStyleAssignFlags flags)
  89. {
  90. return WidgetStyle_SetFontInt(&self->titleFont, &self->flags,
  91. WIDGETSTYLE_OWN_TITLE_FONT, font, flags);
  92. }
  93. BOOL
  94. WidgetStyle_SetCategoryFont(WidgetStyle *self, HFONT font, WidgetStyleAssignFlags flags)
  95. {
  96. return WidgetStyle_SetFontInt(&self->categoryFont, &self->flags,
  97. WIDGETSTYLE_OWN_CATEGORY_FONT, font, flags);
  98. }
  99. static COLORREF
  100. WidgetStyle_GetCategoryLineColor(COLORREF categoryBackColor)
  101. {
  102. COLORREF categoryLineColor;
  103. size_t index;
  104. const int categoryLineColors[] =
  105. {
  106. WADLG_LISTHEADER_FRAME_MIDDLECOLOR,
  107. WADLG_LISTHEADER_FRAME_BOTTOMCOLOR,
  108. WADLG_LISTHEADER_FRAME_TOPCOLOR,
  109. WADLG_HILITE,
  110. };
  111. for (index = 0; index < ARRAYSIZE(categoryLineColors); index++)
  112. {
  113. categoryLineColor = Graphics_GetSkinColor(categoryLineColors[index]);
  114. int distance = Graphics_GetColorDistance(categoryLineColor, categoryBackColor);
  115. if (distance < 0) distance = -distance;
  116. if (distance >= 40) break;
  117. }
  118. return categoryLineColor;
  119. }
  120. static COLORREF
  121. WidgetStyle_GetBorderColor(COLORREF backColor, COLORREF textColor)
  122. {
  123. COLORREF borderColor;
  124. for(int step = 0;; step++)
  125. {
  126. switch(step)
  127. {
  128. case 0:
  129. borderColor = Graphics_GetSkinColor(WADLG_HILITE);
  130. break;
  131. case 1:
  132. borderColor = Graphics_BlendColors(Graphics_GetSkinColor(WADLG_SELBAR_FGCOLOR), Graphics_GetSkinColor(WADLG_SELBAR_BGCOLOR), 17);
  133. borderColor = Graphics_BlendColors(borderColor, backColor, 229);
  134. break;
  135. default:
  136. return textColor;
  137. }
  138. int distance = Graphics_GetColorDistance(borderColor, backColor);
  139. if (distance < 0) distance = -distance;
  140. if (distance >= 40)
  141. break;
  142. }
  143. return borderColor;
  144. }
  145. BOOL
  146. WidgetStyle_UpdateDefaultColors(WidgetStyle *style)
  147. {
  148. #define WIDGETSTYLE_SET_COLOR(_colorName, _color)\
  149. {COLORREF _colorCopy = (_color);\
  150. if (WIDGETSTYLE_##_colorName##_COLOR(style) != _colorCopy)\
  151. {WIDGETSTYLE_SET_##_colorName##_COLOR(style, _colorCopy);\
  152. styleChanged = TRUE;}}
  153. #define WIDGETSTYLE_SET_COLOR_BLEND(_colorName, _colorTop, _colorBottom, _alpha)\
  154. WIDGETSTYLE_SET_COLOR(_colorName, Graphics_BlendColors((_colorTop), (_colorBottom), (_alpha)))
  155. COLORREF widgetBackColor, widgetTextColor, categoryBackColor, categoryTextColor;
  156. BOOL styleChanged;
  157. HBRUSH brush;
  158. if (NULL == style)
  159. return FALSE;
  160. styleChanged = FALSE;
  161. widgetBackColor = Graphics_GetSkinColor(WADLG_ITEMBG);
  162. widgetTextColor = Graphics_GetSkinColor(WADLG_ITEMFG);
  163. categoryBackColor = Graphics_GetSkinColor(WADLG_LISTHEADER_BGCOLOR);
  164. categoryTextColor = Graphics_GetSkinColor(WADLG_LISTHEADER_FONTCOLOR);
  165. if (WIDGETSTYLE_BACK_COLOR(style) != widgetBackColor ||
  166. NULL == WIDGETSTYLE_BACK_BRUSH(style))
  167. {
  168. brush = CreateSolidBrush(widgetBackColor);
  169. WIDGETSTYLE_SET_BACK_BRUSH(style, brush, WIDGETSTYLE_OWN_OBJECT);
  170. styleChanged = TRUE;
  171. }
  172. if (WIDGETSTYLE_CATEGORY_BACK_COLOR(style) != categoryBackColor ||
  173. NULL == WIDGETSTYLE_CATEGORY_BRUSH(style))
  174. {
  175. brush = CreateSolidBrush(categoryBackColor);
  176. WIDGETSTYLE_SET_CATEGORY_BRUSH(style, brush, WIDGETSTYLE_OWN_OBJECT);
  177. styleChanged = TRUE;
  178. }
  179. WIDGETSTYLE_SET_COLOR(BACK, widgetBackColor);
  180. WIDGETSTYLE_SET_COLOR(TEXT, widgetTextColor);
  181. WIDGETSTYLE_SET_COLOR_BLEND(TITLE, widgetTextColor, widgetBackColor, 210);
  182. WIDGETSTYLE_SET_COLOR(BORDER, WidgetStyle_GetBorderColor(widgetBackColor, widgetTextColor));
  183. WIDGETSTYLE_SET_COLOR(IMAGE_BACK, widgetBackColor);
  184. WIDGETSTYLE_SET_COLOR(IMAGE_FRONT, widgetTextColor);
  185. WIDGETSTYLE_SET_COLOR(SELECT_BACK, Graphics_GetSkinColor(WADLG_SELBAR_BGCOLOR));
  186. WIDGETSTYLE_SET_COLOR(SELECT_FRONT, Graphics_GetSkinColor(WADLG_SELBAR_FGCOLOR));
  187. WIDGETSTYLE_SET_COLOR_BLEND(INACTIVE_SELECT_BACK, WIDGETSTYLE_SELECT_BACK_COLOR(style), widgetBackColor, 192);
  188. WIDGETSTYLE_SET_COLOR_BLEND(INACTIVE_SELECT_FRONT, WIDGETSTYLE_SELECT_FRONT_COLOR(style), widgetBackColor, 192);
  189. WIDGETSTYLE_SET_COLOR(CATEGORY_BACK, categoryBackColor);
  190. WIDGETSTYLE_SET_COLOR(CATEGORY_TEXT, categoryTextColor);
  191. WIDGETSTYLE_SET_COLOR(CATEGORY_LINE, WidgetStyle_GetCategoryLineColor(categoryBackColor));
  192. WIDGETSTYLE_SET_COLOR_BLEND(CATEGORY_EMPTY_TEXT, widgetTextColor, widgetBackColor, 210);
  193. WIDGETSTYLE_SET_COLOR_BLEND(TEXT_EDITOR_BORDER, widgetTextColor, widgetBackColor, 140);
  194. return styleChanged;
  195. }
  196. BOOL
  197. WidgetStyle_UpdateDefaultFonts(WidgetStyle *style, HFONT baseFont, long unitWidth, long unitHeight)
  198. {
  199. HFONT tempFont;
  200. if (NULL == style)
  201. return FALSE;
  202. tempFont = Graphics_DuplicateFont(baseFont, 3, FALSE, TRUE);
  203. WIDGETSTYLE_SET_TITLE_FONT(style, tempFont, WIDGETSTYLE_OWN_OBJECT);
  204. tempFont = Graphics_DuplicateFont(baseFont, 0, FALSE, TRUE);
  205. WIDGETSTYLE_SET_CATEGORY_FONT(style, tempFont, WIDGETSTYLE_OWN_OBJECT);
  206. WIDGETSTYLE_SET_TEXT_FONT(style, baseFont, WIDGETSTYLE_LINK_OBJECT);
  207. WIDGETSTYLE_SET_UNIT_SIZE(style, unitWidth, unitHeight);
  208. return TRUE;
  209. }