Theme.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. CStringA csPathA = CTextConvert::ConvertToChar(csPath);
  96. TiXmlDocument doc(csPathA);
  97. if(!doc.LoadFile())
  98. {
  99. m_csLastError.Format(_T("Error loading Theme %s - reason = %s"), csPath, doc.ErrorDesc());
  100. ASSERT(!m_csLastError);
  101. Log(m_csLastError);
  102. return false;
  103. }
  104. TiXmlElement *ItemHeader = doc.FirstChildElement("Ditto_Theme_File");
  105. if(!ItemHeader)
  106. {
  107. m_csLastError.Format(_T("Error finding the section Ditto_Theme_File"));
  108. ASSERT(!m_csLastError);
  109. Log(m_csLastError);
  110. return false;
  111. }
  112. CString csVersion = ItemHeader->Attribute("Version");
  113. m_lFileVersion = ATOI(csVersion);
  114. m_csAuthor = ItemHeader->Attribute("Author");
  115. m_csNotes = ItemHeader->Attribute("Notes");
  116. if(bHeaderOnly)
  117. return true;
  118. LoadColor(ItemHeader, "CaptionLeft", m_CaptionLeft);
  119. LoadColor(ItemHeader, "CaptionRight", m_CaptionRight);
  120. LoadColor(ItemHeader, "CaptionLeftTopMost", m_CaptionLeftTopMost);
  121. LoadColor(ItemHeader, "CaptionRightTopMost", m_CaptionRightTopMost);
  122. LoadColor(ItemHeader, "CaptionLeftNotConnected", m_CaptionLeftNotConnected);
  123. LoadColor(ItemHeader, "CaptionRightNotConnected", m_CaptionRightNotConnected);
  124. LoadColor(ItemHeader, "CaptionTextColor", m_CaptionTextColor);
  125. LoadColor(ItemHeader, "ListBoxOddRowsBG", m_ListBoxOddRowsBG);
  126. LoadColor(ItemHeader, "ListBoxEvenRowsBG", m_ListBoxEvenRowsBG);
  127. LoadColor(ItemHeader, "ListBoxOddRowsText", m_ListBoxOddRowsText);
  128. LoadColor(ItemHeader, "ListBoxEvenRowsText", m_ListBoxEvenRowsText);
  129. LoadColor(ItemHeader, "ListBoxSelectedBG", m_ListBoxSelectedBG);
  130. LoadColor(ItemHeader, "ListBoxSelectedNoFocusBG", m_ListBoxSelectedNoFocusBG);
  131. LoadColor(ItemHeader, "ListBoxSelectedText", m_ListBoxSelectedText);
  132. LoadColor(ItemHeader, "ListBoxSelectedNoFocusText", m_ListBoxSelectedNoFocusText);
  133. LoadColor(ItemHeader, "ClipPastedColor", m_clipPastedColor);
  134. LoadColor(ItemHeader, "MainWindowBG", m_mainWindowBG);
  135. LoadColor(ItemHeader, "SearchTextBoxFocusBG", m_searchTextBoxFocusBG);
  136. LoadColor(ItemHeader, "SearchTextBoxFocusText", m_searchTextBoxFocusText);
  137. LoadColor(ItemHeader, "SearchTextBoxFocusBorder", m_searchTextBoxFocusBorder);
  138. LoadColor(ItemHeader, "SearchTextHighlight", m_searchTextHighlight);
  139. LoadColor(ItemHeader, "Border", m_Border);
  140. LoadColor(ItemHeader, "BorderTopMost", m_BorderTopMost);
  141. LoadColor(ItemHeader, "BorderNotConnected", m_BorderNotConnected);
  142. LoadColor(ItemHeader, "GroupTreeBG", m_groupTreeBG);
  143. LoadColor(ItemHeader, "GroupTreeText", m_groupTreeText);
  144. LoadInt(ItemHeader, "CaptionSize", m_captionSize);
  145. LoadInt(ItemHeader, "CaptionFontSize", m_captionFontSize);
  146. LoadColor(ItemHeader, "DescriptionWindowBG", m_descriptionWindowBG);
  147. LoadColor(ItemHeader, "DescriptionWindowText", m_descriptionWindowText);
  148. if (followWindows10Theme)
  149. {
  150. LoadWindowsAccentColor();
  151. }
  152. return true;
  153. }
  154. void CTheme::LoadWindowsAccentColor()
  155. {
  156. DWORD accent = Windows10AccentColor();
  157. if (accent != -1)
  158. {
  159. //windows seems to be bgr, convert to rgb
  160. auto r = GetRValue(accent);
  161. auto g = GetGValue(accent);
  162. auto b = GetBValue(accent);
  163. m_clipPastedColor = RGB(b, g, r);
  164. m_searchTextBoxFocusBorder = m_clipPastedColor;
  165. m_searchTextHighlight = m_clipPastedColor;
  166. //if (Windows10ColorTitleBar())
  167. //{
  168. // m_CaptionRight = m_clipPastedColor;
  169. // m_CaptionLeft = m_clipPastedColor;
  170. // m_Border = m_clipPastedColor;
  171. //}
  172. }
  173. }
  174. bool CTheme::LoadColor(TiXmlElement *pParent, CStringA csNode, COLORREF &Color)
  175. {
  176. int intValue = 0;
  177. return LoadElement(pParent, csNode, Color, intValue);
  178. }
  179. bool CTheme::LoadInt(TiXmlElement *pParent, CStringA csNode, int &intValue)
  180. {
  181. COLORREF colorValue = 0;
  182. return LoadElement(pParent, csNode, colorValue, intValue);
  183. }
  184. bool CTheme::LoadElement(TiXmlElement *pParent, CStringA csNode, COLORREF &Color, int &intValue)
  185. {
  186. TiXmlElement *pColorNode = pParent->FirstChildElement(csNode);
  187. if(pColorNode == NULL)
  188. {
  189. m_csLastError.Format(_T("Theme Load, error loading Node = %s"), csNode);
  190. Log(m_csLastError);
  191. return false;
  192. }
  193. TiXmlNode *pColor = pColorNode->FirstChild();
  194. if(pColor == NULL)
  195. {
  196. m_csLastError.Format(_T("Theme Load, error getting node text for = %s"), csNode);
  197. Log(m_csLastError);
  198. return false;
  199. }
  200. CString csColor = pColor->Value();
  201. if (csColor == _T(""))
  202. {
  203. return false;
  204. }
  205. if (csColor.Find(_T("RGB")) >= 0)
  206. {
  207. csColor = csColor.Trim();
  208. csColor.Replace(_T("RGB("), _T(""));
  209. csColor.Replace(_T(")"), _T(""));
  210. CTokenizer token(csColor, _T(","));
  211. CString csR;
  212. CString csG;
  213. CString csB;
  214. token.Next(csR);
  215. token.Next(csG);
  216. token.Next(csB);
  217. csR = csR.Trim();
  218. csG = csG.Trim();
  219. csB = csB.Trim();
  220. //Only the first is valid they entered the RGB value as a single number
  221. if (csR != "" && csG == "" && csB == "")
  222. {
  223. Color = ATOI(csR);
  224. }
  225. else
  226. {
  227. Color = RGB(ATOI(csR), ATOI(csG), ATOI(csB));
  228. }
  229. }
  230. else
  231. {
  232. intValue = ATOI(csColor);
  233. }
  234. return true;
  235. }