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)
OBJECTS = $(patsubst %.cpp,%.o, $(SOURCES))
TYRO_LIB = build/Tyro.a
BASE_LIB = build/Tyro.a
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_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_CXXFLAGS = $(shell wx-config --cxxflags)
@ -38,7 +41,7 @@ ifeq ($(OS),Linux)
LDLIBS += -lssh2
endif
ifeq ($(OS),Windows_NT)
CXXFLAGS += -DNDEBUG -static
CXXFLAGS += -static
CXX += -I/include -DWIN32
LDLIBS += -L/lib -lwsock32 -lssh2
endif
@ -48,7 +51,7 @@ CXX += -Iinclude -I. -I/usr/local/include
ifdef $(DEV)
all: CXXFLAGS = $(DEV_CXXFLAGS)
endif
all: build json_wrapper $(TYRO_LIB) $(PROGRAM)
all: build json_wrapper $(BASE_LIB) $(PROGRAM)
ifeq ($(OS),Darwin)
all: Tyro.app
endif
@ -66,16 +69,21 @@ json_wrapper_build:
build:
@mkdir -p build
$(TYRO_LIB): build $(OBJECTS)
$(BASE_LIB): build $(OBJECTS)
ar rcs $@ $(OBJECTS)
ranlib $@
$(WIDGET_OBJ): CXXFLAGS += $(WX_CXXFLAGS)
$(WIDGET_LIB): $(WIDGET_OBJ)
ar rcs $@ $(WIDGET_OBJ)
ranlib $@
$(PROGRAM): CXXFLAGS += $(WX_CXXFLAGS)
$(PROGRAM):
$(CXX) $(CXXFLAGS) $(PROGRAM_SRC) $(TYRO_LIB) $(WX_LDLIBS) $(LDLIBS) -o $(PROGRAM)
$(PROGRAM): $(WIDGET_LIB)
$(CXX) $(CXXFLAGS) $(PROGRAM_SRC) $(WIDGET_LIB) $(BASE_LIB) $(WX_LDLIBS) $(LDLIBS) -o $(PROGRAM)
lib: $(OBJECTS) $(TYRO_LIB)
lib: $(OBJECTS) $(BASE_LIB)
run:
ifneq ($(OS),Darwin)
@ -84,7 +92,6 @@ else
open -a $(PWD)/build/Tyro.app
endif
run-grind:
valgrind $(PROGRAM)
@ -110,7 +117,7 @@ msw_resource:
$(WX_RES) resources/platform/msw/resource.rc -O coff -o 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)
# OS X application bundle
@ -118,7 +125,7 @@ Tyro.app:
ifndef DEV
strip -SXx $(PROGRAM)
endif
SetFile -t APPL $(TYRO_LIB)
SetFile -t APPL $(BASE_LIB)
-mkdir -p build/Tyro.app/Contents/MacOS
-mkdir -p build/Tyro.app/Contents/Resources/English.lproj
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 resources/platform/osx/tyro.icns build/Tyro.app/Contents/Resources/
$(TESTS): $(TYRO_LIB)
$(foreach var, $(TEST_SRC), $(CXX) $(CXXFLAGS) $(var) $(TYRO_LIB) $(WX_LDLIBS) $(LDLIBS) -o $(patsubst %.cpp,%, $(var));)
$(TESTS): $(BASE_LIB)
$(foreach var, $(TEST_SRC), $(CXX) $(CXXFLAGS) $(var) $(BASE_LIB) $(WX_LDLIBS) $(LDLIBS) -o $(patsubst %.cpp,%, $(var));)
.PHONY: tests
tests: $(TESTS)
@ -139,7 +146,7 @@ clean:
rm -f config/json2c.exe
rm -f config/*_json.h
rm -rf *.o
rm -rf build/Tyro.app
rm -rf build $(OBJECTS) $(PROGRAM) $(TYRO_LIB) $(TESTS)
rm -rf build
rm -rf build $(OBJECTS) $(PROGRAM) $(BASE_LIB) $(TESTS)
find . -name "*.gc*" -exec rm {} \;
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)
{
lang_config = new LangConfig();
#include <config/themes_json.h>
theme_config = new TyroConfig();
theme_config->LoadJson(themes_json);
theme_config = new ThemeConfig();
// Map language types to their lexers
lexerMap["batch"] = wxSTC_LEX_BATCH;
@ -124,7 +121,7 @@ void EditPane::Highlight(wxString filePath)
*/
void EditPane::ApplyTheme(string lang, string theme)
{
this->SetTheme(theme);
theme_config->SetTheme(theme);
// Get the keywords and mapping for the selected language
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
*
@ -383,14 +340,14 @@ void EditPane::_ApplyTheme(JsonValue &lexer_map)
);
#endif
static const wxColor default_background = this->GetThemeColor("background", "default");
static const wxColor default_foreground = this->GetThemeColor("foreground", "default");
wxColor line_number_background = ( ! this->GetThemeValue("line_numbers", "background").isNull())
? (this->GetThemeColor("line_numbers", "background"))
static const wxColor default_background = theme_config->GetThemeColor("background", "default");
static const wxColor default_foreground = theme_config->GetThemeColor("foreground", "default");
wxColor line_number_background = ( ! theme_config->GetThemeValue("line_numbers", "background").isNull())
? (theme_config->GetThemeColor("line_numbers", "background"))
: default_background;
wxColor line_number_foreground = ( ! this->GetThemeValue("line_numbers", "foreground").isNull())
? (this->GetThemeColor("line_numbers", "foreground"))
wxColor line_number_foreground = ( ! theme_config->GetThemeValue("line_numbers", "foreground").isNull())
? (theme_config->GetThemeColor("line_numbers", "foreground"))
: default_foreground;
// Set default colors/ fonts
@ -419,40 +376,33 @@ void EditPane::_ApplyTheme(JsonValue &lexer_map)
//wxLogDebug("Lexer constant: %i", i);
// 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
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
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
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
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
#include "../wx_common.h"
#include "../base/settings/Config.h"
#include "../settings/LangConfig.h"
#include "../settings/ThemeConfig.h"
#include <wx/stc/stc.h>
@ -27,8 +27,7 @@ private:
StringConstMap lexerMap;
StringConstMap::iterator lexerMapIt;
LangConfig *lang_config;
TyroConfig *theme_config;
JsonValue current_theme;
ThemeConfig *theme_config;
enum myMargins
{
MARGIN_FOLD,
@ -41,8 +40,6 @@ private:
void OnMarginClick(wxStyledTextEvent &event);
void OnCharAdded(wxStyledTextEvent &event);
void SetTheme(string theme_name);
JsonValue GetThemeValue(string type, string key);
wxColor GetThemeColor(string type, string key);
void _ApplyTheme(JsonValue &lexer_map);
};