Theme.cpp 6.3 KB

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