1
0

Theme.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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_groupTreeBG = RGB(240, 240, 240);
  44. m_groupTreeText = RGB(127, 127, 127);
  45. m_descriptionWindowBG = RGB(240, 240, 240);// GetSysColor(COLOR_INFOBK);//RGB(240, 240, 240);//
  46. /*int r = GetRValue(m_descriptionWindowBG);
  47. int g = GetGValue(m_descriptionWindowBG);
  48. int b = GetBValue(m_descriptionWindowBG);*/
  49. m_descriptionWindowText = RGB(0, 0, 0);
  50. m_captionSize = 25;
  51. m_captionFontSize = 19;
  52. }
  53. bool CTheme::Load(CString csTheme, bool bHeaderOnly, bool bCheckLastWriteTime)
  54. {
  55. if (csTheme.IsEmpty())
  56. {
  57. if (DarkAppWindows10Setting())
  58. {
  59. csTheme = _T("DarkerDitto");
  60. Log(_T("Loading theme based on windows setting of dark mode for apps"));
  61. }
  62. }
  63. if (csTheme.IsEmpty() || csTheme == _T("Ditto") || csTheme == _T("(Default)") || csTheme == _T("(Ditto)"))
  64. {
  65. LoadDefaults();
  66. m_LastWriteTime = 0;
  67. m_lastTheme = _T("");
  68. Log(_T("Loading default ditto values for themes"));
  69. return false;
  70. }
  71. CString csPath = CGetSetOptions::GetPath(PATH_THEMES);
  72. csPath += csTheme;
  73. csPath += ".xml";
  74. __int64 LastWrite = GetLastWriteTime(csPath);
  75. if(bCheckLastWriteTime)
  76. {
  77. if(m_lastTheme == csTheme &&
  78. LastWrite == m_LastWriteTime)
  79. {
  80. return true;
  81. }
  82. }
  83. LoadDefaults();
  84. m_LastWriteTime = LastWrite;
  85. m_lastTheme = csTheme;
  86. Log(StrF(_T("Loading Theme %s"), csPath));
  87. CStringA csPathA = CTextConvert::ConvertToChar(csPath);
  88. TiXmlDocument doc(csPathA);
  89. if(!doc.LoadFile())
  90. {
  91. m_csLastError.Format(_T("Error loading Theme %s - reason = %s"), csPath, doc.ErrorDesc());
  92. ASSERT(!m_csLastError);
  93. Log(m_csLastError);
  94. return false;
  95. }
  96. TiXmlElement *ItemHeader = doc.FirstChildElement("Ditto_Theme_File");
  97. if(!ItemHeader)
  98. {
  99. m_csLastError.Format(_T("Error finding the section Ditto_Theme_File"));
  100. ASSERT(!m_csLastError);
  101. Log(m_csLastError);
  102. return false;
  103. }
  104. CString csVersion = ItemHeader->Attribute("Version");
  105. m_lFileVersion = ATOI(csVersion);
  106. m_csAuthor = ItemHeader->Attribute("Author");
  107. m_csNotes = ItemHeader->Attribute("Notes");
  108. if(bHeaderOnly)
  109. return true;
  110. LoadColor(ItemHeader, "CaptionLeft", m_CaptionLeft);
  111. LoadColor(ItemHeader, "CaptionRight", m_CaptionRight);
  112. LoadColor(ItemHeader, "CaptionLeftTopMost", m_CaptionLeftTopMost);
  113. LoadColor(ItemHeader, "CaptionRightTopMost", m_CaptionRightTopMost);
  114. LoadColor(ItemHeader, "CaptionLeftNotConnected", m_CaptionLeftNotConnected);
  115. LoadColor(ItemHeader, "CaptionRightNotConnected", m_CaptionRightNotConnected);
  116. LoadColor(ItemHeader, "CaptionTextColor", m_CaptionTextColor);
  117. LoadColor(ItemHeader, "ListBoxOddRowsBG", m_ListBoxOddRowsBG);
  118. LoadColor(ItemHeader, "ListBoxEvenRowsBG", m_ListBoxEvenRowsBG);
  119. LoadColor(ItemHeader, "ListBoxOddRowsText", m_ListBoxOddRowsText);
  120. LoadColor(ItemHeader, "ListBoxEvenRowsText", m_ListBoxEvenRowsText);
  121. LoadColor(ItemHeader, "ListBoxSelectedBG", m_ListBoxSelectedBG);
  122. LoadColor(ItemHeader, "ListBoxSelectedNoFocusBG", m_ListBoxSelectedNoFocusBG);
  123. LoadColor(ItemHeader, "ListBoxSelectedText", m_ListBoxSelectedText);
  124. LoadColor(ItemHeader, "ListBoxSelectedNoFocusText", m_ListBoxSelectedNoFocusText);
  125. LoadColor(ItemHeader, "ClipPastedColor", m_clipPastedColor);
  126. LoadColor(ItemHeader, "MainWindowBG", m_mainWindowBG);
  127. LoadColor(ItemHeader, "SearchTextBoxFocusBG", m_searchTextBoxFocusBG);
  128. LoadColor(ItemHeader, "SearchTextBoxFocusText", m_searchTextBoxFocusText);
  129. LoadColor(ItemHeader, "Border", m_Border);
  130. LoadColor(ItemHeader, "BorderTopMost", m_BorderTopMost);
  131. LoadColor(ItemHeader, "BorderNotConnected", m_BorderNotConnected);
  132. LoadColor(ItemHeader, "GroupTreeBG", m_groupTreeBG);
  133. LoadColor(ItemHeader, "GroupTreeText", m_groupTreeText);
  134. LoadInt(ItemHeader, "CaptionSize", m_captionSize);
  135. LoadInt(ItemHeader, "CaptionFontSize", m_captionFontSize);
  136. LoadColor(ItemHeader, "DescriptionWindowBG", m_descriptionWindowBG);
  137. LoadColor(ItemHeader, "DescriptionWindowText", m_descriptionWindowText);
  138. return true;
  139. }
  140. bool CTheme::LoadColor(TiXmlElement *pParent, CStringA csNode, COLORREF &Color)
  141. {
  142. int intValue = 0;
  143. return LoadElement(pParent, csNode, Color, intValue);
  144. }
  145. bool CTheme::LoadInt(TiXmlElement *pParent, CStringA csNode, int &intValue)
  146. {
  147. COLORREF colorValue = 0;
  148. return LoadElement(pParent, csNode, colorValue, intValue);
  149. }
  150. bool CTheme::LoadElement(TiXmlElement *pParent, CStringA csNode, COLORREF &Color, int &intValue)
  151. {
  152. TiXmlElement *pColorNode = pParent->FirstChildElement(csNode);
  153. if(pColorNode == NULL)
  154. {
  155. m_csLastError.Format(_T("Theme Load, error loading Node = %s"), csNode);
  156. Log(m_csLastError);
  157. return false;
  158. }
  159. TiXmlNode *pColor = pColorNode->FirstChild();
  160. if(pColor == NULL)
  161. {
  162. m_csLastError.Format(_T("Theme Load, error getting node text for = %s"), csNode);
  163. Log(m_csLastError);
  164. return false;
  165. }
  166. CString csColor = pColor->Value();
  167. if (csColor == _T(""))
  168. {
  169. return false;
  170. }
  171. if (csColor.Find(_T("RGB")) >= 0)
  172. {
  173. csColor = csColor.Trim();
  174. csColor.Replace(_T("RGB("), _T(""));
  175. csColor.Replace(_T(")"), _T(""));
  176. CTokenizer token(csColor, _T(","));
  177. CString csR;
  178. CString csG;
  179. CString csB;
  180. token.Next(csR);
  181. token.Next(csG);
  182. token.Next(csB);
  183. csR = csR.Trim();
  184. csG = csG.Trim();
  185. csB = csB.Trim();
  186. //Only the first is valid they entered the RGB value as a single number
  187. if (csR != "" && csG == "" && csB == "")
  188. {
  189. Color = ATOI(csR);
  190. }
  191. else
  192. {
  193. Color = RGB(ATOI(csR), ATOI(csG), ATOI(csB));
  194. }
  195. }
  196. else
  197. {
  198. intValue = ATOI(csColor);
  199. }
  200. return true;
  201. }