From 95aacda3cbabf84c8b963a11907b3bfef709cade Mon Sep 17 00:00:00 2001 From: spx Date: Wed, 6 Feb 2013 19:12:16 +0900 Subject: [PATCH] Code cleanup --- Library/Measure.cpp | 18 +++--------------- Library/Meter.cpp | 30 +++++++----------------------- Library/Meter.h | 4 ++-- Library/MeterBar.cpp | 2 +- Library/MeterString.cpp | 2 +- Library/MeterWindow.cpp | 2 -- Library/Mouse.cpp | 24 +++++++++++++++++------- Library/Section.cpp | 35 +++++++++++++++++++++++++++++++++++ Library/Section.h | 5 +++++ 9 files changed, 71 insertions(+), 51 deletions(-) diff --git a/Library/Measure.cpp b/Library/Measure.cpp index 208d566b7..0b3b71b08 100644 --- a/Library/Measure.cpp +++ b/Library/Measure.cpp @@ -119,6 +119,8 @@ void CMeasure::ReadOptions(CConfigParser& parser, const WCHAR* section) { bool oldOnChangeActionEmpty = m_OnChangeAction.empty(); + CSection::ReadOptions(parser, section); + // Clear substitutes to prevent from being added more than once. if (!m_Substitute.empty()) { @@ -129,12 +131,6 @@ void CMeasure::ReadOptions(CConfigParser& parser, const WCHAR* section) m_Disabled = 0!=parser.ReadInt(section, L"Disabled", 0); - int updateDivider = parser.ReadInt(section, L"UpdateDivider", 1); - if (updateDivider != m_UpdateDivider) - { - m_UpdateCounter = m_UpdateDivider = updateDivider; - } - m_MinValue = parser.ReadFloat(section, L"MinValue", m_MinValue); m_MaxValue = parser.ReadFloat(section, L"MaxValue", m_MaxValue); @@ -149,13 +145,10 @@ void CMeasure::ReadOptions(CConfigParser& parser, const WCHAR* section) m_IfEqualValue = (int64_t)parser.ReadFloat(section, L"IfEqualValue", 0.0); m_IfEqualAction = parser.ReadString(section, L"IfEqualAction", L"", false); - m_OnUpdateAction = parser.ReadString(section, L"OnUpdateAction", L"", false); m_OnChangeAction = parser.ReadString(section, L"OnChangeAction", L"", false); m_AverageSize = parser.ReadUInt(section, L"AverageSize", 0); - m_DynamicVariables = 0!=parser.ReadInt(section, L"DynamicVariables", 0); - m_RegExpSubstitute = 0!=parser.ReadInt(section, L"RegExpSubstitute", 0); std::wstring subs = parser.ReadString(section, L"Substitute", L""); if (!subs.empty()) @@ -173,9 +166,6 @@ void CMeasure::ReadOptions(CConfigParser& parser, const WCHAR* section) } } - const std::wstring& group = parser.ReadString(section, L"Group", L""); - InitializeGroup(group); - if (m_Initialized && oldOnChangeActionEmpty && !m_OnChangeAction.empty()) { @@ -450,9 +440,7 @@ bool CMeasure::Update() if (!m_Disabled) { // Only update the counter if the divider - ++m_UpdateCounter; - if (m_UpdateCounter < m_UpdateDivider) return false; - m_UpdateCounter = 0; + if (!UpdateCounter()) return false; // Call derived method to update value UpdateValue(); diff --git a/Library/Meter.cpp b/Library/Meter.cpp index f2dcd6261..637827ae5 100644 --- a/Library/Meter.cpp +++ b/Library/Meter.cpp @@ -169,15 +169,13 @@ RECT CMeter::GetMeterRect() /* ** Checks if the given point is inside the meter. +** This function doesn't check Hidden state, so check it before calling this function if needed. ** */ bool CMeter::HitTest(int x, int y) { - if (x >= GetX() && x < GetX() + GetW() && y >= GetY() && y < GetY() + GetH()) - { - return true; - } - return false; + int p; + return (x >= (p = GetX()) && x < p + m_W && y >= (p = GetY()) && y < p + m_H); } /* @@ -231,6 +229,8 @@ void CMeter::ReadOptions(CConfigParser& parser, const WCHAR* section) parser.SetStyleTemplate(style); } + CSection::ReadOptions(parser, section); + BindMeasures(parser, section); int oldX = m_X; @@ -321,26 +321,17 @@ void CMeter::ReadOptions(CConfigParser& parser, const WCHAR* section) m_SolidColor2 = parser.ReadColor(section, L"SolidColor2", m_SolidColor.GetValue()); m_SolidAngle = (Gdiplus::REAL)parser.ReadFloat(section, L"GradientAngle", 0.0); - m_OnUpdateAction = parser.ReadString(section, L"OnUpdateAction", L"", false); - m_Mouse.ReadOptions(parser, section); m_HasMouseAction = m_Mouse.HasButtonAction() || m_Mouse.HasScrollAction(); m_ToolTipText = parser.ReadString(section, L"ToolTipText", L""); m_ToolTipTitle = parser.ReadString(section, L"ToolTipTitle", L""); m_ToolTipIcon = parser.ReadString(section, L"ToolTipIcon", L""); - m_ToolTipWidth = (int)parser.ReadFloat(section, L"ToolTipWidth", 1000); + m_ToolTipWidth = parser.ReadInt(section, L"ToolTipWidth", 1000); m_ToolTipType = 0!=parser.ReadInt(section, L"ToolTipType", 0); m_ToolTipHidden = 0!=parser.ReadInt(section, L"ToolTipHidden", m_MeterWindow->GetMeterToolTipHidden()); - int updateDivider = parser.ReadInt(section, L"UpdateDivider", 1); - if (updateDivider != m_UpdateDivider) - { - m_UpdateCounter = m_UpdateDivider = updateDivider; - } - m_AntiAlias = 0!=parser.ReadInt(section, L"AntiAlias", 0); - m_DynamicVariables = 0!=parser.ReadInt(section, L"DynamicVariables", 0); std::vector matrix = parser.ReadFloats(section, L"TransformationMatrix"); if (matrix.size() == 6) @@ -361,9 +352,6 @@ void CMeter::ReadOptions(CConfigParser& parser, const WCHAR* section) LogWithArgs(LOG_ERROR, L"Meter: Incorrect number of values in TransformationMatrix=%s", parser.ReadString(section, L"TransformationMatrix", L"").c_str()); } - - const std::wstring& group = parser.ReadString(section, L"Group", L""); - InitializeGroup(group); } /* @@ -432,11 +420,7 @@ CMeter* CMeter::Create(const WCHAR* meter, CMeterWindow* meterWindow, const WCHA bool CMeter::Update() { // Only update the meter's value when the divider is equal to the counter - ++m_UpdateCounter; - if (m_UpdateCounter < m_UpdateDivider) return false; - m_UpdateCounter = 0; - - return true; + return UpdateCounter(); } /* diff --git a/Library/Meter.h b/Library/Meter.h index d082f8c7e..4520dd5f9 100644 --- a/Library/Meter.h +++ b/Library/Meter.h @@ -65,8 +65,8 @@ class CMeter : public CSection void CreateToolTip(CMeterWindow* meterWindow); void UpdateToolTip(); - virtual void Hide(); - virtual void Show(); + void Hide(); + void Show(); bool IsHidden() { return m_Hidden; } const Gdiplus::Matrix* GetTransformationMatrix() { return m_Transformation; } diff --git a/Library/MeterBar.cpp b/Library/MeterBar.cpp index 25775686a..4225d2dbc 100644 --- a/Library/MeterBar.cpp +++ b/Library/MeterBar.cpp @@ -108,7 +108,7 @@ void CMeterBar::ReadOptions(CConfigParser& parser, const WCHAR* section) m_Border = parser.ReadInt(section, L"BarBorder", 0); - m_Flip = parser.ReadInt(section, L"Flip", 0) == 1; + m_Flip = 0!=parser.ReadInt(section, L"Flip", 0); const WCHAR* orientation = parser.ReadString(section, L"BarOrientation", L"VERTICAL").c_str(); if (_wcsicmp(L"VERTICAL", orientation) == 0) diff --git a/Library/MeterString.cpp b/Library/MeterString.cpp index c2646fa7f..fe2944385 100644 --- a/Library/MeterString.cpp +++ b/Library/MeterString.cpp @@ -377,7 +377,7 @@ void CMeterString::ReadOptions(CConfigParser& parser, const WCHAR* section) m_FontFace = L"Arial"; } - m_FontSize = (int)parser.ReadFloat(section, L"FontSize", 10); + m_FontSize = parser.ReadInt(section, L"FontSize", 10); if (m_FontSize < 0) { m_FontSize = 10; diff --git a/Library/MeterWindow.cpp b/Library/MeterWindow.cpp index cc41dc6ca..0584bd020 100644 --- a/Library/MeterWindow.cpp +++ b/Library/MeterWindow.cpp @@ -709,8 +709,6 @@ void CMeterWindow::ChangeSingleZPos(ZPOSITION zPos, bool all) */ void CMeterWindow::RunBang(BANGCOMMAND bang, const std::vector& args) { - if (!m_Window) return; - switch (bang) { case BANG_REFRESH: diff --git a/Library/Mouse.cpp b/Library/Mouse.cpp index 0822188d7..7a4aac119 100644 --- a/Library/Mouse.cpp +++ b/Library/Mouse.cpp @@ -73,7 +73,22 @@ void CMouse::ReadOptions(CConfigParser& parser, const WCHAR* section) const WCHAR* defaultMouseCursor = (section == L"Rainmeter") ? L"HAND" : L""; const WCHAR* mouseCursor = parser.ReadString(section, L"MouseActionCursorName", defaultMouseCursor).c_str(); - if (_wcsicmp(mouseCursor, L"HAND") == 0) + + auto inheritSkinDefault = [&]() + { + // Inherit from [Rainmeter]. + m_CursorType = m_MeterWindow->GetMouse().GetCursorType(); + if (m_CursorType == MOUSECURSOR_CUSTOM) + { + mouseCursor = m_MeterWindow->GetParser().ReadString(L"Rainmeter", L"MouseActionCursorName", L"").c_str(); + } + }; + + if (*mouseCursor == L'\0') // meters' default + { + inheritSkinDefault(); + } + else if (_wcsicmp(mouseCursor, L"HAND") == 0) // skin's default { m_CursorType = MOUSECURSOR_HAND; } @@ -103,12 +118,7 @@ void CMouse::ReadOptions(CConfigParser& parser, const WCHAR* section) } else { - // Inherit from [Rainmeter]. - m_CursorType = m_MeterWindow->GetMouse().GetCursorType(); - if (m_CursorType == MOUSECURSOR_CUSTOM) - { - mouseCursor = m_MeterWindow->GetParser().ReadString(L"Rainmeter", L"MouseActionCursorName", L"").c_str(); - } + inheritSkinDefault(); } if (m_CursorType == MOUSECURSOR_CUSTOM) diff --git a/Library/Section.cpp b/Library/Section.cpp index b2168cc61..d0c33774f 100644 --- a/Library/Section.cpp +++ b/Library/Section.cpp @@ -18,6 +18,7 @@ #include "StdAfx.h" #include "Section.h" +#include "ConfigParser.h" #include "Rainmeter.h" extern CRainmeter* Rainmeter; @@ -41,6 +42,40 @@ CSection::~CSection() { } +/* +** Read the common options specified in the ini file. The inherited classes must +** call this base implementation if they overwrite this method. +** +*/ +void CSection::ReadOptions(CConfigParser& parser, const WCHAR* section) +{ + int updateDivider = parser.ReadInt(section, L"UpdateDivider", 1); + if (updateDivider != m_UpdateDivider) + { + m_UpdateCounter = m_UpdateDivider = updateDivider; + } + + m_DynamicVariables = 0!=parser.ReadInt(section, L"DynamicVariables", 0); + + m_OnUpdateAction = parser.ReadString(section, L"OnUpdateAction", L"", false); + + const std::wstring& group = parser.ReadString(section, L"Group", L""); + InitializeGroup(group); +} + +/* +** Updates the counter value +** +*/ +bool CSection::UpdateCounter() +{ + ++m_UpdateCounter; + if (m_UpdateCounter < m_UpdateDivider) return false; + m_UpdateCounter = 0; + + return true; +} + /* ** Execute OnUpdateAction if action is set ** diff --git a/Library/Section.h b/Library/Section.h index 211cea6dd..2b97348ed 100644 --- a/Library/Section.h +++ b/Library/Section.h @@ -24,6 +24,7 @@ #include "Group.h" class CMeterWindow; +class CConfigParser; class CSection : public CGroup { @@ -48,6 +49,10 @@ class CSection : public CGroup protected: CSection(CMeterWindow* meterWindow, const WCHAR* name); + virtual void ReadOptions(CConfigParser& parser, const WCHAR* section); + + bool UpdateCounter(); + const std::wstring m_Name; // Name of this Section bool m_DynamicVariables; // If true, the section contains dynamic variables