Refactor theme config into its own class

This commit is contained in:
Tim Warren 2015-05-07 14:06:12 -04:00
parent 6baa24dcf6
commit 033e7bf229
5 changed files with 132 additions and 90 deletions

View File

@ -1,12 +1,15 @@
SOURCES = $(wildcard include/**/*.cpp include/*.cpp src/base/**/*.cpp) SOURCES = $(wildcard include/**/*.cpp include/*.cpp src/base/**/*.cpp)
OBJECTS = $(patsubst %.cpp,%.o, $(SOURCES)) OBJECTS = $(patsubst %.cpp,%.o, $(SOURCES))
TYRO_LIB = build/Tyro.a BASE_LIB = build/Tyro.a
JSON_FILES = $(patsubst config/%.json,%.json, $(wildcard config/*.json)) JSON_FILES = $(patsubst config/%.json,%.json, $(wildcard config/*.json))
PROGRAM_SRC = $(wildcard src/*.cpp src/widgets/*.cpp src/settings/*.cpp) PROGRAM_SRC = $(wildcard src/*.cpp)
PROGRAM = build/Tyro PROGRAM = build/Tyro
PROGRAM_OBJECTS = $(patsubst %.cpp,%.o, $(PROGRAM_SRC))
WIDGET_SRC = $(wildcard src/widgets/*.cpp src/settings/*.cpp)
WIDGET_OBJ = $(patsubst %.cpp,%.o, $(WIDGET_SRC))
WIDGET_LIB = build/widget.a
WX_RES = $(shell wx-config --rescomp) WX_RES = $(shell wx-config --rescomp)
WX_CXXFLAGS = $(shell wx-config --cxxflags) WX_CXXFLAGS = $(shell wx-config --cxxflags)
@ -38,7 +41,7 @@ ifeq ($(OS),Linux)
LDLIBS += -lssh2 LDLIBS += -lssh2
endif endif
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)
CXXFLAGS += -DNDEBUG -static CXXFLAGS += -static
CXX += -I/include -DWIN32 CXX += -I/include -DWIN32
LDLIBS += -L/lib -lwsock32 -lssh2 LDLIBS += -L/lib -lwsock32 -lssh2
endif endif
@ -48,7 +51,7 @@ CXX += -Iinclude -I. -I/usr/local/include
ifdef $(DEV) ifdef $(DEV)
all: CXXFLAGS = $(DEV_CXXFLAGS) all: CXXFLAGS = $(DEV_CXXFLAGS)
endif endif
all: build json_wrapper $(TYRO_LIB) $(PROGRAM) all: build json_wrapper $(BASE_LIB) $(PROGRAM)
ifeq ($(OS),Darwin) ifeq ($(OS),Darwin)
all: Tyro.app all: Tyro.app
endif endif
@ -66,16 +69,21 @@ json_wrapper_build:
build: build:
@mkdir -p build @mkdir -p build
$(TYRO_LIB): build $(OBJECTS) $(BASE_LIB): build $(OBJECTS)
ar rcs $@ $(OBJECTS) ar rcs $@ $(OBJECTS)
ranlib $@ ranlib $@
$(WIDGET_OBJ): CXXFLAGS += $(WX_CXXFLAGS)
$(WIDGET_LIB): $(WIDGET_OBJ)
ar rcs $@ $(WIDGET_OBJ)
ranlib $@
$(PROGRAM): CXXFLAGS += $(WX_CXXFLAGS) $(PROGRAM): CXXFLAGS += $(WX_CXXFLAGS)
$(PROGRAM): $(PROGRAM): $(WIDGET_LIB)
$(CXX) $(CXXFLAGS) $(PROGRAM_SRC) $(TYRO_LIB) $(WX_LDLIBS) $(LDLIBS) -o $(PROGRAM) $(CXX) $(CXXFLAGS) $(PROGRAM_SRC) $(WIDGET_LIB) $(BASE_LIB) $(WX_LDLIBS) $(LDLIBS) -o $(PROGRAM)
lib: $(OBJECTS) $(TYRO_LIB) lib: $(OBJECTS) $(BASE_LIB)
run: run:
ifneq ($(OS),Darwin) ifneq ($(OS),Darwin)
@ -84,7 +92,6 @@ else
open -a $(PWD)/build/Tyro.app open -a $(PWD)/build/Tyro.app
endif endif
run-grind: run-grind:
valgrind $(PROGRAM) valgrind $(PROGRAM)
@ -110,7 +117,7 @@ msw_resource:
$(WX_RES) resources/platform/msw/resource.rc -O coff -o resource.res $(WX_RES) resources/platform/msw/resource.rc -O coff -o resource.res
exe: LDLIBS += resource.res exe: LDLIBS += resource.res
exe: json_wrapper_build json_wrapper $(TYRO_LIB) exe: json_wrapper_build json_wrapper $(BASE_LIB)
exe: msw_resource $(PROGRAM) exe: msw_resource $(PROGRAM)
# OS X application bundle # OS X application bundle
@ -118,7 +125,7 @@ Tyro.app:
ifndef DEV ifndef DEV
strip -SXx $(PROGRAM) strip -SXx $(PROGRAM)
endif endif
SetFile -t APPL $(TYRO_LIB) SetFile -t APPL $(BASE_LIB)
-mkdir -p build/Tyro.app/Contents/MacOS -mkdir -p build/Tyro.app/Contents/MacOS
-mkdir -p build/Tyro.app/Contents/Resources/English.lproj -mkdir -p build/Tyro.app/Contents/Resources/English.lproj
cp resources/platform/osx/Info.plist build/Tyro.app/Contents/ cp resources/platform/osx/Info.plist build/Tyro.app/Contents/
@ -126,8 +133,8 @@ endif
cp build/Tyro build/Tyro.app/Contents/MacOS/Tyro cp build/Tyro build/Tyro.app/Contents/MacOS/Tyro
cp resources/platform/osx/tyro.icns build/Tyro.app/Contents/Resources/ cp resources/platform/osx/tyro.icns build/Tyro.app/Contents/Resources/
$(TESTS): $(TYRO_LIB) $(TESTS): $(BASE_LIB)
$(foreach var, $(TEST_SRC), $(CXX) $(CXXFLAGS) $(var) $(TYRO_LIB) $(WX_LDLIBS) $(LDLIBS) -o $(patsubst %.cpp,%, $(var));) $(foreach var, $(TEST_SRC), $(CXX) $(CXXFLAGS) $(var) $(BASE_LIB) $(WX_LDLIBS) $(LDLIBS) -o $(patsubst %.cpp,%, $(var));)
.PHONY: tests .PHONY: tests
tests: $(TESTS) tests: $(TESTS)
@ -139,7 +146,7 @@ clean:
rm -f config/json2c.exe rm -f config/json2c.exe
rm -f config/*_json.h rm -f config/*_json.h
rm -rf *.o rm -rf *.o
rm -rf build/Tyro.app rm -rf build
rm -rf build $(OBJECTS) $(PROGRAM) $(TYRO_LIB) $(TESTS) rm -rf build $(OBJECTS) $(PROGRAM) $(BASE_LIB) $(TESTS)
find . -name "*.gc*" -exec rm {} \; find . -name "*.gc*" -exec rm {} \;
rm -rf `find . -name "*.dSYM" -print` rm -rf `find . -name "*.dSYM" -print`

View File

@ -0,0 +1,61 @@
#include "ThemeConfig.h"
#include <config/themes_json.h>
ThemeConfig::ThemeConfig()
{
this->LoadJson(themes_json);
this->current_theme = "Solarized";
}
ThemeConfig::~ThemeConfig() {}
void ThemeConfig::SetTheme(string theme_name)
{
JsonValue theme_list = this->GetRoot();
this->current_theme = theme_list.get(theme_name, JsonValue());
}
JsonValue ThemeConfig::GetTheme()
{
return this->current_theme;
}
/**
* Retrieve a setting from the current theme
*
* @param string type
* @param string key
* @return JsonValue
*/
JsonValue ThemeConfig::GetThemeValue(string type, string key)
{
JsonValue value = this->current_theme
.get(type, JsonValue())
.get(key, JsonValue());
return value;
}
/**
* Retrieve the configured color for the specified theme
* @param type
* @param key
* @return
*/
wxColor ThemeConfig::GetThemeColor(string type, string key)
{
JsonValue color_value = this->GetThemeValue(type, key);
if (color_value.isArray())
{
return wxColor(
(unsigned char) color_value[0].asUInt(),
(unsigned char) color_value[1].asUInt(),
(unsigned char) color_value[2].asUInt()
);
}
else
{
return wxColor("black");
}
}

View File

@ -0,0 +1,27 @@
/*
* File: ThemeConfig.h
* Author: twarren
*
* Created on May 7, 2015, 9:39 AM
*/
#ifndef TYRO_THEME_CONFIG_H
#define TYRO_THEME_CONFIG_H
#include "../wx_common.h"
#include "../base/settings/Config.h"
class ThemeConfig : TyroConfig {
public:
ThemeConfig();
~ThemeConfig();
void SetTheme(string theme_name);
JsonValue GetTheme();
JsonValue GetThemeValue(string type, string key);
wxColor GetThemeColor(string type, string key);
private:
JsonValue current_theme;
};
#endif /* TYRO_THEME_CONFIG_H */

View File

@ -5,10 +5,7 @@ EditPane::EditPane(
) : wxStyledTextCtrl (parent, id, pos, size, wxBORDER_NONE) ) : wxStyledTextCtrl (parent, id, pos, size, wxBORDER_NONE)
{ {
lang_config = new LangConfig(); lang_config = new LangConfig();
theme_config = new ThemeConfig();
#include <config/themes_json.h>
theme_config = new TyroConfig();
theme_config->LoadJson(themes_json);
// Map language types to their lexers // Map language types to their lexers
lexerMap["batch"] = wxSTC_LEX_BATCH; lexerMap["batch"] = wxSTC_LEX_BATCH;
@ -124,7 +121,7 @@ void EditPane::Highlight(wxString filePath)
*/ */
void EditPane::ApplyTheme(string lang, string theme) void EditPane::ApplyTheme(string lang, string theme)
{ {
this->SetTheme(theme); theme_config->SetTheme(theme);
// Get the keywords and mapping for the selected language // Get the keywords and mapping for the selected language
JsonValue lexer_map = lang_config->GetLexerMap(lang); JsonValue lexer_map = lang_config->GetLexerMap(lang);
@ -319,46 +316,6 @@ void EditPane::OnCharAdded(wxStyledTextEvent& event)
} }
} }
/**
* Retrieve a setting from the current theme
*
* @param string type
* @param string key
* @return JsonValue
*/
JsonValue EditPane::GetThemeValue(string type, string key)
{
JsonValue value = this->current_theme
.get(type, JsonValue())
.get(key, JsonValue());
return value;
}
/**
* Retrieve the configured color for the specified theme
* @param type
* @param key
* @return
*/
wxColor EditPane::GetThemeColor(string type, string key)
{
JsonValue color_value = this->GetThemeValue(type, key);
if (color_value.isArray())
{
return wxColor(
(unsigned char) color_value[0].asUInt(),
(unsigned char) color_value[1].asUInt(),
(unsigned char) color_value[2].asUInt()
);
}
else
{
return wxColor("black");
}
}
/** /**
* Iterate through the theme settings and apply them * Iterate through the theme settings and apply them
* *
@ -383,14 +340,14 @@ void EditPane::_ApplyTheme(JsonValue &lexer_map)
); );
#endif #endif
static const wxColor default_background = this->GetThemeColor("background", "default"); static const wxColor default_background = theme_config->GetThemeColor("background", "default");
static const wxColor default_foreground = this->GetThemeColor("foreground", "default"); static const wxColor default_foreground = theme_config->GetThemeColor("foreground", "default");
wxColor line_number_background = ( ! this->GetThemeValue("line_numbers", "background").isNull()) wxColor line_number_background = ( ! theme_config->GetThemeValue("line_numbers", "background").isNull())
? (this->GetThemeColor("line_numbers", "background")) ? (theme_config->GetThemeColor("line_numbers", "background"))
: default_background; : default_background;
wxColor line_number_foreground = ( ! this->GetThemeValue("line_numbers", "foreground").isNull()) wxColor line_number_foreground = ( ! theme_config->GetThemeValue("line_numbers", "foreground").isNull())
? (this->GetThemeColor("line_numbers", "foreground")) ? (theme_config->GetThemeColor("line_numbers", "foreground"))
: default_foreground; : default_foreground;
// Set default colors/ fonts // Set default colors/ fonts
@ -419,40 +376,33 @@ void EditPane::_ApplyTheme(JsonValue &lexer_map)
//wxLogDebug("Lexer constant: %i", i); //wxLogDebug("Lexer constant: %i", i);
// Set the foreground color, if it exists // Set the foreground color, if it exists
if ( ! this->GetThemeValue("foreground", key).isNull()) if ( ! theme_config->GetThemeValue("foreground", key).isNull())
{ {
this->StyleSetForeground(i, this->GetThemeColor("foreground", key)); this->StyleSetForeground(i, theme_config->GetThemeColor("foreground", key));
} }
// Set the background color, if it exists // Set the background color, if it exists
if ( ! this->GetThemeValue("background", key).isNull()) if ( ! theme_config->GetThemeValue("background", key).isNull())
{ {
this->StyleSetBackground(i, this->GetThemeColor("background", key)); this->StyleSetBackground(i, theme_config->GetThemeColor("background", key));
} }
// Set bold, if it applies // Set bold, if it applies
if (this->GetThemeValue("bold", key).isBool()) if (theme_config->GetThemeValue("bold", key).isBool())
{ {
this->StyleSetBold(i, this->GetThemeValue("bold", key).asBool()); this->StyleSetBold(i, theme_config->GetThemeValue("bold", key).asBool());
} }
// Italic // Italic
if (this->GetThemeValue("italic", key).isBool()) if (theme_config->GetThemeValue("italic", key).isBool())
{ {
this->StyleSetItalic(i, this->GetThemeValue("italic", key).asBool()); this->StyleSetItalic(i, theme_config->GetThemeValue("italic", key).asBool());
} }
// Underline // Underline
if (this->GetThemeValue("underline", key).isBool()) if (theme_config->GetThemeValue("underline", key).isBool())
{ {
this->StyleSetUnderline(i, this->GetThemeValue("underline", key).asBool()); this->StyleSetUnderline(i, theme_config->GetThemeValue("underline", key).asBool());
} }
} }
} }
void EditPane::SetTheme(string theme_name)
{
JsonValue theme_list = this->theme_config->GetRoot();
this->current_theme = theme_list.get(theme_name, JsonValue());
}

View File

@ -2,8 +2,8 @@
#define TYROEDIT_PANE_H #define TYROEDIT_PANE_H
#include "../wx_common.h" #include "../wx_common.h"
#include "../base/settings/Config.h"
#include "../settings/LangConfig.h" #include "../settings/LangConfig.h"
#include "../settings/ThemeConfig.h"
#include <wx/stc/stc.h> #include <wx/stc/stc.h>
@ -27,8 +27,7 @@ private:
StringConstMap lexerMap; StringConstMap lexerMap;
StringConstMap::iterator lexerMapIt; StringConstMap::iterator lexerMapIt;
LangConfig *lang_config; LangConfig *lang_config;
TyroConfig *theme_config; ThemeConfig *theme_config;
JsonValue current_theme;
enum myMargins enum myMargins
{ {
MARGIN_FOLD, MARGIN_FOLD,
@ -41,8 +40,6 @@ private:
void OnMarginClick(wxStyledTextEvent &event); void OnMarginClick(wxStyledTextEvent &event);
void OnCharAdded(wxStyledTextEvent &event); void OnCharAdded(wxStyledTextEvent &event);
void SetTheme(string theme_name); void SetTheme(string theme_name);
JsonValue GetThemeValue(string type, string key);
wxColor GetThemeColor(string type, string key);
void _ApplyTheme(JsonValue &lexer_map); void _ApplyTheme(JsonValue &lexer_map);
}; };