Theme.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "stdafx.h"
  2. #include ".\theme.h"
  3. #include "shared/TextConvert.h"
  4. #include "Misc.h"
  5. #include "Options.h"
  6. #include "shared/Tokenizer.h"
  7. #include "CP_Main.h"
  8. CTheme::CTheme(void)
  9. {
  10. m_lFileVersion = 0;
  11. m_LastWriteTime = 0;
  12. m_lastTheme = _T("");
  13. LoadDefaults();
  14. }
  15. CTheme::~CTheme(void)
  16. {
  17. }
  18. void CTheme::LoadDefaults()
  19. {
  20. m_CaptionLeft = RGB(255, 255, 255);
  21. m_CaptionRight = RGB(204, 204, 204);
  22. m_Border = RGB(204, 204, 204);
  23. m_BorderTopMost = RGB(204, 204, 204);
  24. m_BorderNotConnected = RGB(204, 204, 204);
  25. m_CaptionLeftTopMost = RGB(255, 255, 255);
  26. m_CaptionRightTopMost = RGB(204, 204, 204);
  27. m_CaptionLeftNotConnected = RGB(255, 255, 255);
  28. m_CaptionRightNotConnected = RGB(255, 255, 0);
  29. m_CaptionTextColor = RGB(191, 191, 191);
  30. m_ListBoxOddRowsBG = RGB(255, 255, 255);
  31. m_ListBoxEvenRowsBG = RGB(243, 243, 243);
  32. m_ListBoxOddRowsText = RGB(0, 0, 0);
  33. m_ListBoxEvenRowsText = RGB(0, 0, 0);
  34. m_ListBoxSelectedBG = RGB(204, 204, 204);
  35. m_ListBoxSelectedNoFocusBG = RGB(204, 204, 204);
  36. m_ListBoxSelectedText = RGB(0, 0, 0);
  37. m_ListBoxSelectedNoFocusText = RGB(0, 0, 0);
  38. m_clipPastedColor = RGB(0, 255, 0);
  39. m_listSmallQuickPasteIndexColor = RGB(180, 180, 180);
  40. m_mainWindowBG = RGB(240, 240, 240);
  41. m_searchTextBoxFocusBG = RGB(255, 255, 255);
  42. m_searchTextBoxFocusText = RGB(0, 0, 0);
  43. m_searchTextBoxFocusBorder = RGB(255, 255, 255);
  44. m_searchTextHighlight = RGB(255, 0, 0);
  45. m_groupTreeBG = RGB(240, 240, 240);
  46. m_groupTreeText = RGB(127, 127, 127);
  47. m_descriptionWindowBG = RGB(240, 240, 240);// GetSysColor(COLOR_INFOBK);//RGB(240, 240, 240);//
  48. /*int r = GetRValue(m_descriptionWindowBG);
  49. int g = GetGValue(m_descriptionWindowBG);
  50. int b = GetBValue(m_descriptionWindowBG);*/
  51. m_descriptionWindowText = RGB(0, 0, 0);
  52. m_captionSize = 25;
  53. m_captionFontSize = 19;
  54. }
  55. bool CTheme::Load(CString csTheme, bool bHeaderOnly, bool bCheckLastWriteTime)
  56. {
  57. bool followWindows10Theme = false;
  58. if (csTheme.IsEmpty())
  59. {
  60. followWindows10Theme = true;
  61. if (DarkAppWindows10Setting())
  62. {
  63. csTheme = _T("DarkerDitto");
  64. Log(_T("Loading theme based on windows setting of dark mode for apps"));
  65. }
  66. }
  67. if (csTheme.IsEmpty() || csTheme == _T("Ditto") || csTheme == _T("(Default)") || csTheme == _T("(Ditto)"))
  68. {
  69. LoadDefaults();
  70. if (followWindows10Theme)
  71. {
  72. LoadWindowsAccentColor();
  73. }
  74. m_LastWriteTime = 0;
  75. m_lastTheme = _T("");
  76. Log(_T("Loading default ditto values for themes"));
  77. return false;
  78. }
  79. CString csPath = CGetSetOptions::GetPath(PATH_THEMES);
  80. csPath += csTheme;
  81. csPath += ".xml";
  82. __int64 LastWrite = GetLastWriteTime(csPath);
  83. if(bCheckLastWriteTime)
  84. {
  85. if(m_lastTheme == csTheme &&
  86. LastWrite == m_LastWriteTime)
  87. {
  88. return true;
  89. }
  90. }
  91. LoadDefaults();
  92. m_LastWriteTime = LastWrite;
  93. m_lastTheme = csTheme;
  94. Log(StrF(_T("Loading Theme %s"), csPath));
  95. TiXmlDocument doc;
  96. if(!doc.LoadFile(csPath.GetBuffer()))
  97. {
  98. m_csLastError.Format(_T("Error loading Theme %s - reason = %s"), csPath, doc.ErrorDesc());
  99. ASSERT(!m_csLastError);
  100. Log(m_csLastError);
  101. return false;
  102. }
  103. TiXmlElement *ItemHeader = doc.FirstChildElement("Ditto_Theme_File");
  104. if(!ItemHeader)
  105. {
  106. m_csLastError.Format(_T("Error finding the section Ditto_Theme_File"));
  107. ASSERT(!m_csLastError);
  108. Log(m_csLastError);
  109. return false;
  110. }
  111. CString csVersion = ItemHeader->Attribute("Version");
  112. m_lFileVersion = ATOI(csVersion);
  113. m_csAuthor = ItemHeader->Attribute("Author");
  114. m_csNotes = ItemHeader->Attribute("Notes");
  115. if(bHeaderOnly)
  116. return true;
  117. LoadColor(ItemHeader, "CaptionLeft", m_CaptionLeft);
  118. LoadColor(ItemHeader, "CaptionRight", m_CaptionRight);
  119. LoadColor(ItemHeader, "CaptionLeftTopMost", m_CaptionLeftTopMost);
  120. LoadColor(ItemHeader, "CaptionRightTopMost", m_CaptionRightTopMost);
  121. LoadColor(ItemHeader, "CaptionLeftNotConnected", m_CaptionLeftNotConnected);
  122. LoadColor(ItemHeader, "CaptionRightNotConnected", m_CaptionRightNotConnected);
  123. LoadColor(ItemHeader, "CaptionTextColor", m_CaptionTextColor);
  124. LoadColor(ItemHeader, "ListBoxOddRowsBG", m_ListBoxOddRowsBG);
  125. LoadColor(ItemHeader, "ListBoxEvenRowsBG", m_ListBoxEvenRowsBG);
  126. LoadColor(ItemHeader, "ListBoxOddRowsText", m_ListBoxOddRowsText);
  127. LoadColor(ItemHeader, "ListBoxEvenRowsText", m_ListBoxEvenRowsText);
  128. LoadColor(ItemHeader, "ListBoxSelectedBG", m_ListBoxSelectedBG);
  129. LoadColor(ItemHeader, "ListBoxSelectedNoFocusBG", m_ListBoxSelectedNoFocusBG);
  130. LoadColor(ItemHeader, "ListBoxSelectedText", m_ListBoxSelectedText);
  131. LoadColor(ItemHeader, "ListBoxSelectedNoFocusText", m_ListBoxSelectedNoFocusText);
  132. LoadColor(ItemHeader, "ClipPastedColor", m_clipPastedColor);
  133. LoadColor(ItemHeader, "MainWindowBG", m_mainWindowBG);
  134. LoadColor(ItemHeader, "SearchTextBoxFocusBG", m_searchTextBoxFocusBG);
  135. LoadColor(ItemHeader, "SearchTextBoxFocusText", m_searchTextBoxFocusText);
  136. LoadColor(ItemHeader, "SearchTextBoxFocusBorder", m_searchTextBoxFocusBorder);
  137. LoadColor(ItemHeader, "SearchTextHighlight", m_searchTextHighlight);
  138. LoadColor(ItemHeader, "Border", m_Border);
  139. LoadColor(ItemHeader, "BorderTopMost", m_BorderTopMost);
  140. LoadColor(ItemHeader, "BorderNotConnected", m_BorderNotConnected);
  141. LoadColor(ItemHeader, "GroupTreeBG", m_groupTreeBG);
  142. LoadColor(ItemHeader, "GroupTreeText", m_groupTreeText);
  143. LoadInt(ItemHeader, "CaptionSize", m_captionSize);
  144. LoadInt(ItemHeader, "CaptionFontSize", m_captionFontSize);
  145. LoadColor(ItemHeader, "DescriptionWindowBG", m_descriptionWindowBG);
  146. LoadColor(ItemHeader, "DescriptionWindowText", m_descriptionWindowText);
  147. if (followWindows10Theme)
  148. {
  149. LoadWindowsAccentColor();
  150. }
  151. return true;
  152. }
  153. void CTheme::LoadWindowsAccentColor()
  154. {
  155. DWORD accent = Windows10AccentColor();
  156. if (accent != -1)
  157. {
  158. //windows seems to be bgr, convert to rgb
  159. auto r = GetRValue(accent);
  160. auto g = GetGValue(accent);
  161. auto b = GetBValue(accent);
  162. m_clipPastedColor = RGB(b, g, r);
  163. m_searchTextBoxFocusBorder = m_clipPastedColor;
  164. m_searchTextHighlight = m_clipPastedColor;
  165. //if (Windows10ColorTitleBar())
  166. //{
  167. // m_CaptionRight = m_clipPastedColor;
  168. // m_CaptionLeft = m_clipPastedColor;
  169. // m_Border = m_clipPastedColor;
  170. //}
  171. }
  172. }
  173. bool CTheme::LoadColor(TiXmlElement *pParent, CStringA csNode, COLORREF &Color)
  174. {
  175. int intValue = 0;
  176. return LoadElement(pParent, csNode, Color, intValue);
  177. }
  178. bool CTheme::LoadInt(TiXmlElement *pParent, CStringA csNode, int &intValue)
  179. {
  180. COLORREF colorValue = 0;
  181. return LoadElement(pParent, csNode, colorValue, intValue);
  182. }
  183. bool CTheme::LoadElement(TiXmlElement *pParent, CStringA csNode, COLORREF &Color, int &intValue)
  184. {
  185. TiXmlElement *pColorNode = pParent->FirstChildElement(csNode);
  186. if(pColorNode == NULL)
  187. {
  188. m_csLastError.Format(_T("Theme Load, error loading Node = %s"), csNode);
  189. Log(m_csLastError);
  190. return false;
  191. }
  192. TiXmlNode *pColor = pColorNode->FirstChild();
  193. if(pColor == NULL)
  194. {
  195. m_csLastError.Format(_T("Theme Load, error getting node text for = %s"), csNode);
  196. Log(m_csLastError);
  197. return false;
  198. }
  199. CString csColor = pColor->Value();
  200. if (csColor == _T(""))
  201. {
  202. return false;
  203. }
  204. if (csColor.Find(_T("RGB")) >= 0)
  205. {
  206. csColor = csColor.Trim();
  207. csColor.Replace(_T("RGB("), _T(""));
  208. csColor.Replace(_T(")"), _T(""));
  209. CTokenizer token(csColor, _T(","));
  210. CString csR;
  211. CString csG;
  212. CString csB;
  213. token.Next(csR);
  214. token.Next(csG);
  215. token.Next(csB);
  216. csR = csR.Trim();
  217. csG = csG.Trim();
  218. csB = csB.Trim();
  219. //Only the first is valid they entered the RGB value as a single number
  220. if (csR != "" && csG == "" && csB == "")
  221. {
  222. Color = ATOI(csR);
  223. }
  224. else
  225. {
  226. Color = RGB(ATOI(csR), ATOI(csG), ATOI(csB));
  227. }
  228. }
  229. else
  230. {
  231. intValue = ATOI(csColor);
  232. }
  233. return true;
  234. }