From 2161c76bf408b1f2a534b4aea1e7106b83a90817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Fri, 7 Aug 2015 15:18:33 +0200 Subject: [PATCH] gtk-3.0 theme support and support for gtksourceview styles --- src/config.cc | 46 ++++++++++++---------------------------------- src/config.h | 8 ++++---- src/juci.cc | 13 +++++++------ src/singletons.cc | 5 ++++- src/singletons.h | 7 +++++++ src/theme.h | 14 ++++++++++++++ src/tooltips.cc | 1 - src/window.cc | 15 ++++++++++++--- src/window.h | 8 ++++++-- 9 files changed, 66 insertions(+), 51 deletions(-) create mode 100644 src/theme.h diff --git a/src/config.cc b/src/config.cc index 16dd271..604efd1 100644 --- a/src/config.cc +++ b/src/config.cc @@ -5,15 +5,25 @@ #include "files.h" #include "sourcefile.h" -MainConfig::MainConfig(Menu &menu) : menu(menu) { +MainConfig::MainConfig() { find_or_create_config_files(); boost::property_tree::json_parser::read_json(Singleton::config_dir() + "config.json", cfg); + Singleton::Config::window()->keybindings = cfg.get_child("keybindings"); GenerateSource(); - GenerateKeybindings(); + GenerateTheme(); GenerateDirectoryFilter(); GenerateTerminalCommands(); } +void MainConfig::GenerateTheme() { + auto config = Singleton::Config::theme(); + auto props = cfg.get_child("theme"); + for (auto &prop : props) { + if (prop.first == "theme") config->theme = prop.second.get_value(); + if (prop.first == "main") config->main = prop.second.get_value(); + } +} + void MainConfig::find_or_create_config_files() { std::vector files = {"config.json", "menu.xml", "plugins.py"}; boost::filesystem::create_directories(boost::filesystem::path(Singleton::config_dir())); @@ -34,23 +44,6 @@ void MainConfig::GenerateSource() { auto source_json = cfg.get_child("source"); auto clang_types_json = source_json.get_child("clang_types"); auto style_json = source_json.get_child("style"); - auto visual_json = source_json.get_child("visual"); - for (auto &i : visual_json) { - if (i.first == "background") - source_cfg->background = i.second.get_value(); - else if (i.first == "background_selected") - source_cfg->background_selected = i.second.get_value(); - else if (i.first == "background_tooltips") - source_cfg->background_tooltips = i.second.get_value(); - else if (i.first == "show_line_numbers") - source_cfg->show_line_numbers = i.second.get_value() == "1"; - else if (i.first == "highlight_current_line") - source_cfg->highlight_current_line = i.second.get_value() == "1"; - else if (i.first == "font") - source_cfg->font = i.second.get_value(); - else if (i.first == "theme") - source_cfg->theme = i.second.get_value(); - } source_cfg->tab_size = source_json.get("tab_size"); for (unsigned c = 0; c < source_cfg->tab_size; c++) source_cfg->tab+=" "; @@ -72,21 +65,6 @@ void MainConfig::GenerateTerminalCommands() { terminal_cfg->run_command=(i.second.get_value()); //TODO: run_commands array->one run_command? } -void MainConfig::GenerateKeybindings() { - boost::filesystem::path path(Singleton::config_dir() + "menu.xml"); - if (!boost::filesystem::is_regular_file(path)) { - std::cerr << "menu.xml not found" << std::endl; - throw; - } - menu.ui = juci::filesystem::read(path); - boost::property_tree::ptree keys_json = cfg.get_child("keybindings"); - for (auto &i : keys_json) { - auto key=i.second.get_value(); - menu.key_map[i.first] = key; - } - DEBUG("Keybindings fetched"); -} - void MainConfig::GenerateDirectoryFilter() { auto dir_cfg=Singleton::Config::directories(); DEBUG("Fetching directory filter"); diff --git a/src/config.h b/src/config.h index d1ea90b..86e8c5d 100644 --- a/src/config.h +++ b/src/config.h @@ -6,16 +6,16 @@ class MainConfig { public: - MainConfig(Menu &menu); + MainConfig(); void find_or_create_config_files(); void PrintMenu(); void GenerateSource(); - void GenerateKeybindings(); void GenerateDirectoryFilter(); + void GenerateTheme(); void GenerateTerminalCommands(); + private: boost::property_tree::ptree cfg; - boost::property_tree::ptree key_tree; - Menu &menu; + }; #endif diff --git a/src/juci.cc b/src/juci.cc index 9aeadeb..5d9cd9a 100644 --- a/src/juci.cc +++ b/src/juci.cc @@ -1,5 +1,6 @@ #include "juci.h" #include "singletons.h" +#include "config.h" void init_logging() { add_common_attributes(); @@ -44,12 +45,12 @@ void app::on_activate() { } app::app() : Gtk::Application("no.sout.juci", Gio::APPLICATION_HANDLES_COMMAND_LINE) { - auto css_provider = Gtk::CssProvider::create(); - if (css_provider->load_from_path(Singleton::style_dir() + "juci.css")) { - auto style_context = Gtk::StyleContext::create(); - style_context->add_provider(css_provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); - style_context->add_provider_for_screen(window->get_screen(), css_provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); - } + MainConfig(); // Read the configs here + auto css_provider = Gtk::CssProvider::get_default(); + auto style_context = Gtk::StyleContext::create(); + auto screen = Gdk::Screen::get_default(); + style_context->add_provider_for_screen(screen, css_provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); + css_provider->load_from_path(Singleton::theme_dir() + Singleton::Config::theme()->current_theme()); } int main(int argc, char *argv[]) { diff --git a/src/singletons.cc b/src/singletons.cc index 076bf46..08e627d 100644 --- a/src/singletons.cc +++ b/src/singletons.cc @@ -3,8 +3,11 @@ std::unique_ptr Singleton::Config::source_=std::unique_ptr(new Source::Config()); std::unique_ptr Singleton::Config::terminal_=std::unique_ptr(new Terminal::Config()); std::unique_ptr Singleton::Config::directories_=std::unique_ptr(new Directories::Config()); - std::unique_ptr Singleton::terminal_=std::unique_ptr(); +std::unique_ptr Singleton::Config::theme_ = std::unique_ptr(new Theme::Config()); +std::unique_ptr Singleton::Config::window_ = std::unique_ptr(new Window::Config()); + + Terminal *Singleton::terminal() { if(!terminal_) terminal_=std::unique_ptr(new Terminal()); diff --git a/src/singletons.h b/src/singletons.h index b449311..4a92eb3 100644 --- a/src/singletons.h +++ b/src/singletons.h @@ -2,9 +2,11 @@ #define JUCI_SINGLETONS_H_ #include "source.h" +#include "window.h" #include "directories.h" #include "terminal.h" #include "notebook.h" +#include "theme.h" #include "menu.h" #include #include @@ -16,12 +18,17 @@ public: static Source::Config *source() {return source_.get();} static Terminal::Config *terminal() {return terminal_.get();} static Directories::Config *directories() {return directories_.get();} + static Theme::Config *theme() { return theme_.get(); } + static Window::Config *window() { return window_.get(); } private: static std::unique_ptr source_; + static std::unique_ptr theme_; + static std::unique_ptr window_; static std::unique_ptr terminal_; static std::unique_ptr directories_; }; static std::string config_dir() { return std::string(getenv("HOME")) + "/.juci/config/"; } + static std::string theme_dir() { return std::string(getenv("HOME")) + "/.juci/gtk-themes/"; } static std::string log_dir() { return std::string(getenv("HOME")) + "/.juci/log/"; } static std::string style_dir() { return std::string(getenv("HOME")) + "/.juci/styles/"; } static Terminal *terminal(); diff --git a/src/theme.h b/src/theme.h new file mode 100644 index 0000000..7b915ea --- /dev/null +++ b/src/theme.h @@ -0,0 +1,14 @@ +#ifndef JUCI_THEME_H_ +#define JUCI_THEME_H_ + +#include + +namespace Theme { + class Config { + public: + std::string current_theme() { return theme + "/" + main; } + std::string theme, main; + }; +} + +#endif // JUCI_THEME_H_ diff --git a/src/tooltips.cc b/src/tooltips.cc index 5afc1f3..c76e7dc 100644 --- a/src/tooltips.cc +++ b/src/tooltips.cc @@ -54,7 +54,6 @@ void Tooltip::adjust(bool disregard_drawn) { tooltip_widget=std::unique_ptr(new Gtk::TextView(create_tooltip_buffer())); wrap_lines(tooltip_widget->get_buffer()); tooltip_widget->set_editable(false); - tooltip_widget->override_background_color(Gdk::RGBA(Singleton::Config::source()->background_tooltips)); window->add(*tooltip_widget); auto layout=Pango::Layout::create(tooltip_widget->get_pango_context()); diff --git a/src/window.cc b/src/window.cc index b999717..11ee2d5 100644 --- a/src/window.cc +++ b/src/window.cc @@ -12,15 +12,24 @@ namespace sigc { SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE } +void Window::generate_keybindings() { + boost::filesystem::path path(Singleton::config_dir() + "menu.xml"); + menu.ui = juci::filesystem::read(path); + for (auto &i : Singleton::Config::window()->keybindings) { + auto key = i.second.get_value(); + menu.key_map[i.first] = key; + } +} + Window::Window() : box(Gtk::ORIENTATION_VERTICAL) { INFO("Create Window"); set_title("juCi++"); set_default_size(600, 400); set_events(Gdk::POINTER_MOTION_MASK|Gdk::FOCUS_CHANGE_MASK|Gdk::SCROLL_MASK); add(box); - - MainConfig(this->menu); //Read the configs here - PluginApi(&this->notebook, &this->menu); //Initialise plugins + + generate_keybindings(); + PluginApi(&this->notebook, &this->menu); create_menu(); box.pack_start(menu.get_widget(), Gtk::PACK_SHRINK); diff --git a/src/window.h b/src/window.h index 44c6afb..e7602c9 100644 --- a/src/window.h +++ b/src/window.h @@ -6,12 +6,17 @@ #include "entrybox.h" #include "notebook.h" #include "menu.h" +#include class Window : public Gtk::Window { public: Window(); Notebook notebook; Directories directories; + class Config { + public: + boost::property_tree::ptree keybindings; + }; protected: bool on_key_press_event(GdkEventKey *event); bool on_delete_event (GdkEventAny *event); @@ -32,16 +37,15 @@ private: void open_folder_dialog(); void open_file_dialog(); void save_file_dialog(); - void search_and_replace_entry(); void goto_line_entry(); void rename_token_entry(); + void generate_keybindings(); std::string last_search; std::string last_replace; bool case_sensitive_search=true; bool regex_search=false; bool search_entry_shown=false; - }; #endif // JUCI_WINDOW_H