From 2d4449447814989ee3342c3f3267513c0b593525 Mon Sep 17 00:00:00 2001 From: oyvang Date: Fri, 13 Mar 2015 09:03:24 +0100 Subject: [PATCH 1/7] linenumbers --- juci/notebook.cc | 24 ++++++++++++++++-------- juci/notebook.h | 2 ++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index 371de09..dc771ed 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -7,6 +7,7 @@ Notebook::Model::Model() { Notebook::View::View() : view_(Gtk::ORIENTATION_VERTICAL){ + } Gtk::Box& Notebook::View::view() { view_.pack_start(notebook_); @@ -14,14 +15,9 @@ Gtk::Box& Notebook::View::view() { } Notebook::Controller::Controller(Keybindings::Controller& keybindings){ - scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); - source_vec_.push_back(new Source::Controller); - scrolledwindow_vec_.back()->add(source_vec_.back()->view()); - source_vec_.back()->OnNewEmptyFile(); - notebook().append_page(*scrolledwindow_vec_.back(), "juCi++"); - notebook().set_focus_child(*scrolledwindow_vec_.back()); + OnNewPage("juCi++"); refClipboard = Gtk::Clipboard::get(); - + keybindings.action_group_menu()->add(Gtk::Action::create("FileMenu", Gtk::Stock::FILE)); /* File->New files */ @@ -115,8 +111,18 @@ Gtk::Box& Notebook::Controller::entry_view(){ void Notebook::Controller::OnNewPage(std::string name) { scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); source_vec_.push_back(new Source::Controller); - scrolledwindow_vec_.back()->add(source_vec_.back()->view()); + label_vec_.push_back(new Gtk::Label); + label_vec_.back()->set_text("1\n"); + label_vec_.back()->set_justify(Gtk::Justification::JUSTIFY_RIGHT); + label_vec_.back()->set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_START); + paned_vec_.push_back(new Gtk::Paned()); + paned_vec_.back()->set_orientation(Gtk::Orientation::ORIENTATION_HORIZONTAL); + paned_vec_.back()->add1(*label_vec_.back()); + paned_vec_.back()->pack2(source_vec_.back()->view(),true,false); + scrolledwindow_vec_.back()->add(*paned_vec_.back()); source_vec_.back()->OnNewEmptyFile(); + scrolledwindow_vec_.back()->set_policy(Gtk::PolicyType::POLICY_NEVER, + Gtk::PolicyType::POLICY_AUTOMATIC); notebook().append_page(*scrolledwindow_vec_.back(), name); notebook().show_all_children(); notebook().set_focus_child(*scrolledwindow_vec_.back()); @@ -161,6 +167,8 @@ void Notebook::Controller::OnOpenFile(std::string path) { source_vec_.push_back(new Source::Controller); scrolledwindow_vec_.back()->add(source_vec_.back()->view()); source_vec_.back()->OnOpenFile(path); + scrolledwindow_vec_.back()->set_policy(Gtk::PolicyType::POLICY_NEVER, + Gtk::PolicyType::POLICY_AUTOMATIC); unsigned pos = path.find_last_of("/\\"); notebook().append_page(*scrolledwindow_vec_.back(), path.substr(pos+1)); notebook().show_all_children(); diff --git a/juci/notebook.h b/juci/notebook.h index 1692270..1281076 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -35,6 +35,8 @@ namespace Notebook { Entry::Controller entry_; std::vector source_vec_; std::vector scrolledwindow_vec_; + std::vector label_vec_; + std::vector paned_vec_; Glib::RefPtr refClipboard; std::list listTargets; std::string GetCursorWord(); From 4e91c3e467c06bee6564c854d45adf25c4fb3331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Lien=20Sell=C3=A6g?= Date: Tue, 17 Mar 2015 09:24:53 +0100 Subject: [PATCH 2/7] Teddy --- juci/source.cc | 18 +++++++++--------- juci/source.h | 13 ++++++++++++- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/juci/source.cc b/juci/source.cc index ca45c53..1739386 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -31,8 +31,8 @@ string Source::View::GetLine(const Gtk::TextIter &begin) { // Applies theme in textview void Source::View::ApplyTheme(const Source::Theme &theme) { for (auto &item : theme.tagtable()) { - std::cout << "Apply: f: " << item.first << ", s: " << - item.second << std::endl; + // std::cout << "Apply: f: " << item.first << ", s: " << + // item.second << std::endl; get_buffer()->create_tag(item.first)->property_foreground() = item.second; } } @@ -56,15 +56,15 @@ void Source::View::OnOpenFile(std::vector &locations, if (end < 0) end = 0; if (begin < 0) begin = 0; - std::cout << "Libc: "; - std::cout << "type: " << type; - std::cout << " linum_s: " << linum_start+1 << " linum_e: " << linum_end+1; - std::cout << ", begin: " << begin << ", end: " << end << std::endl; + // std::cout << "Libc: "; + // std::cout << "type: " << type; + // std::cout << " linum_s: " << linum_start+1 << " linum_e: " << linum_end+1; + // std::cout << ", begin: " << begin << ", end: " << end << std::endl; Gtk::TextIter begin_iter = buffer->get_iter_at_line_offset(linum_start, begin); Gtk::TextIter end_iter = buffer->get_iter_at_line_offset(linum_end, end); - std::cout << get_buffer()->get_text(begin_iter, end_iter) << std::endl; + // std::cout << get_buffer()->get_text(begin_iter, end_iter) << std::endl; if (begin_iter.get_line() == end_iter.get_line()) { buffer->apply_tag_by_name(theme.typetable().at(type), begin_iter, end_iter); @@ -108,7 +108,7 @@ void Source::Theme::SetTagTable( //// Model //// /////////////// Source::Model::Model() : - theme_() {/* + theme_() { std::cout << "Model constructor run" << std::endl; boost::property_tree::ptree pt; boost::property_tree::json_parser::read_json("config.json", pt); @@ -124,7 +124,7 @@ Source::Model::Model() : // std::cout << "inserting type. " << pi.first << pi.second.get_value() << std::endl; } } - }*/ + } } Source::Theme& Source::Model::theme() { diff --git a/juci/source.h b/juci/source.h index 135d0a3..65a145d 100644 --- a/juci/source.h +++ b/juci/source.h @@ -10,7 +10,18 @@ using std::string; namespace Source { - class Theme { + + class Config() { + // læs json + + // + + private: + + } + + + class Config { public: const std::unordered_map& tagtable() const; const std::unordered_map& typetable() const; From d97040744744a1b69e85dad5d5221ae793f07b79 Mon Sep 17 00:00:00 2001 From: tedjk Date: Thu, 19 Mar 2015 13:30:32 +0100 Subject: [PATCH 3/7] config ready to test on source, starting with keybindings --- juci/CMakeLists.txt | 4 ++- juci/config.cc | 25 +++++++++++++++ juci/config.h | 30 ++++++++++++++++++ juci/config.json | 46 ++++++++++++++++++---------- juci/keybindings.cc | 25 ++++++++++----- juci/keybindings.h | 24 +++++++++++++-- juci/notebook.cc | 10 +++--- juci/notebook.h | 5 ++- juci/source.cc | 74 +++++++++++++++++++++++++++++---------------- juci/source.h | 28 ++++++----------- juci/window.cc | 13 +++----- juci/window.h | 10 ++++-- 12 files changed, 206 insertions(+), 88 deletions(-) create mode 100644 juci/config.cc create mode 100644 juci/config.h diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index ec9f2ac..84e5973 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -72,10 +72,12 @@ endif() # name of the executable on Windows will be example.exe add_executable(${project_name} #list of every needed file to create the executable - juci.cc + juci.cc keybindings menu source + config.h + config.cc sourcefile.h sourcefile.cc window diff --git a/juci/config.cc b/juci/config.cc new file mode 100644 index 0000000..9a93184 --- /dev/null +++ b/juci/config.cc @@ -0,0 +1,25 @@ +#include "config.h" + +MainConfig::MainConfig() { + boost::property_tree::json_parser::read_json("config.json", cfg_); + GenerateSource(); + GenerateKeybindings(); + + // keybindings_cfg_ = cfg_.get_child("keybindings"); + // notebook_cfg_ = cfg_.get_child("notebook"); + // menu_cfg_ = cfg_.get_child("menu"); +} + +void MainConfig::GenerateSource(){ + boost::property_tree::ptree source_json = cfg_.get_child("source"); + boost::property_tree::ptree syntax_json = source_json.get_child("syntax"); + boost::property_tree::ptree colors_json = source_json.get_child("colors"); + for ( auto &i : syntax_json ) + source_cfg_.InsertTag(i.first, i.second.get_value()); + + for ( auto &i : colors_json ) + source_cfg_.InsertType(i.first, i.second.get_value()); +} + +void MainConfig::GenerateKeybindings(){ +} diff --git a/juci/config.h b/juci/config.h new file mode 100644 index 0000000..56eb476 --- /dev/null +++ b/juci/config.h @@ -0,0 +1,30 @@ +#include +#include +#include +#include "source.h" +#include "keybindings.h" + class MainConfig { + public: + MainConfig(); + const Source::Config& source_cfg() {return source_cfg_; } + const Keybindings::Config& keybindings_cfg() {return keybindings_cfg_; } + + void PrintMenu(); + //boost::property_tree::ptree& source_cfg(); + //boost::property_tree::ptree& keybindings_cfg(); + //boost::property_tree::ptree& notebookk_cfg(); + //boost::property_tree::ptree& menu_cfg(); + + boost::property_tree::ptree cfg_; + // boost::property_tree::ptree sourcecfg_; + // boost::property_tree::ptree keybindings_cfg_; + // boost::property_tree::ptree notebook_cfg_; + // boost::property_tree::ptree menu_cfg_; + + Source::Config source_cfg_; + Keybindings::Config keybindings_cfg_; + + void GenerateSource(); + void GenerateKeybindings(); + }; + diff --git a/juci/config.json b/juci/config.json index 6af5298..c27f2b9 100644 --- a/juci/config.json +++ b/juci/config.json @@ -1,19 +1,33 @@ { - "colors": { - "text_color": "#333333", - "string" : "#CC0000", - "namespace_ref" : "#990099", - "type" : "#0066FF", - "keyword": "blue", - "comment": "grey", - "own": "pink" - }, - "syntax": { - "43": "type", - "46": "namespace_ref", - "109": "string", - "702": "keyword", - "703": "own", - "705": "comment" + "source": { + "colors": { + "text_color": "#333333", + "string": "#CC0000", + "namespace_ref": "#990099", + "type": "#0066FF", + "keyword": "blue", + "comment": "grey", + "own": "pink" + }, + "syntax": { + "43": "type", + "46": "namespace_ref", + "109": "string", + "702": "keyword", + "703": "own", + "705": "comment" } + }, + "example": { + "key": "value", + "key2": [ + "val1", + "val2", + 3 + ], + "key3": "value" + }, + "keybindings": { + "path" : "keybindings.xml" + } } diff --git a/juci/keybindings.cc b/juci/keybindings.cc index 234d9d9..48860e7 100644 --- a/juci/keybindings.cc +++ b/juci/keybindings.cc @@ -1,7 +1,8 @@ #include "keybindings.h" -Keybindings::Model::Model() { - menu_ui_string_ = +Keybindings::Model::Model(const Keybindings::Config &config) : + menu_ui_string_(config.menu_xml()) { + /* menu_ui_string_ = " " " " " " @@ -27,15 +28,15 @@ Keybindings::Model::Model() { " " " " " " - // " " - // " " - // " " + // " " + // " " + // " " " " " " " " " " " " - " "; + " ";*/ hidden_ui_string_ = " " @@ -43,12 +44,13 @@ Keybindings::Model::Model() { " " " " " "; -}; +} Keybindings::Model::~Model() { } -Keybindings::Controller::Controller() { +Keybindings::Controller::Controller(const Keybindings::Config &config) : + config_(config), model_(config) { action_group_menu_ = Gtk::ActionGroup::create(); ui_manager_menu_ = Gtk::UIManager::create(); action_group_hidden_ = Gtk::ActionGroup::create(); @@ -75,3 +77,10 @@ void Keybindings::Controller::BuildHiddenMenu() { ui_manager_hidden_->insert_action_group(action_group_hidden_); } +Keybindings::Config::Config(const Keybindings::Config &original) : + menu_xml_(original.menu_xml()) { +} + +void Keybindings::Config::SetMenu(std::string &menu_xml) { + menu_xml_ = menu_xml; +} diff --git a/juci/keybindings.h b/juci/keybindings.h index adf3283..8ad9b22 100644 --- a/juci/keybindings.h +++ b/juci/keybindings.h @@ -6,9 +6,22 @@ #include "gtkmm.h" namespace Keybindings { + + class Config{ + public: + Config(const Config &original); + Config(); + const std::string& menu_xml() const {return menu_xml_;} + void SetMenu(std::string &menu_xml); + + private: + std::string menu_xml_; + std::string hidden_ui_string_; + };//Config + class Model { public: - Model(); + Model(const Keybindings::Config &config); virtual ~Model(); std::string menu_ui_string(){return menu_ui_string_;} std::string hidden_ui_string(){return hidden_ui_string_;} @@ -16,9 +29,10 @@ namespace Keybindings { std::string menu_ui_string_; std::string hidden_ui_string_; }; // Model + class Controller { public: - Controller(); + Controller(const Keybindings::Config &config); virtual ~Controller(); Glib::RefPtr action_group_menu() { return action_group_menu_; @@ -40,8 +54,14 @@ namespace Keybindings { Glib::RefPtr ui_manager_hidden_; Glib::RefPtr action_group_hidden_; // private: + Keybindings::Config config_; Keybindings::Model model_; + + + };//Controller + + } #endif // JUCI_KEYBINDINGS_H_ diff --git a/juci/notebook.cc b/juci/notebook.cc index 371de09..45f662b 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -12,16 +12,16 @@ Gtk::Box& Notebook::View::view() { view_.pack_start(notebook_); return view_; } -Notebook::Controller::Controller(Keybindings::Controller& keybindings){ +Notebook::Controller::Controller(Keybindings::Controller& keybindings, + const Source::Config &source_cfg) { scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); - source_vec_.push_back(new Source::Controller); + source_vec_.push_back(new Source::Controller(source_cfg)); scrolledwindow_vec_.back()->add(source_vec_.back()->view()); source_vec_.back()->OnNewEmptyFile(); notebook().append_page(*scrolledwindow_vec_.back(), "juCi++"); notebook().set_focus_child(*scrolledwindow_vec_.back()); refClipboard = Gtk::Clipboard::get(); - keybindings.action_group_menu()->add(Gtk::Action::create("FileMenu", Gtk::Stock::FILE)); /* File->New files */ @@ -114,7 +114,7 @@ Gtk::Box& Notebook::Controller::entry_view(){ } void Notebook::Controller::OnNewPage(std::string name) { scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); - source_vec_.push_back(new Source::Controller); + source_vec_.push_back(new Source::Controller(source_config_)); scrolledwindow_vec_.back()->add(source_vec_.back()->view()); source_vec_.back()->OnNewEmptyFile(); notebook().append_page(*scrolledwindow_vec_.back(), name); @@ -158,7 +158,7 @@ void Notebook::Controller::OnEditCut() { void Notebook::Controller::OnOpenFile(std::string path) { scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); - source_vec_.push_back(new Source::Controller); + source_vec_.push_back(new Source::Controller(source_config_)); scrolledwindow_vec_.back()->add(source_vec_.back()->view()); source_vec_.back()->OnOpenFile(path); unsigned pos = path.find_last_of("/\\"); diff --git a/juci/notebook.h b/juci/notebook.h index 1692270..f5a4e0f 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -18,13 +18,14 @@ namespace Notebook { View(); Gtk::Box& view(); Gtk::Notebook& notebook() { return notebook_; } + protected: Gtk::Box view_; Gtk::Notebook notebook_; }; class Controller { public: - Controller(Keybindings::Controller& keybindings); + Controller(Keybindings::Controller& keybindings,const Source::Config& config); Gtk::Box& view(); Gtk::Box& entry_view(); void OnNewPage(std::string name); @@ -50,7 +51,9 @@ namespace Notebook { void OnEditCut(); void OnEditSearch(); void Search(bool forward); + Source::Config& source_config() { return source_config_; } private: + Source::Config source_config_; bool is_new_file; Gtk::TextIter search_match_end_; Gtk::TextIter search_match_start_; diff --git a/juci/source.cc b/juci/source.cc index a7a95c9..34ed54d 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -29,8 +29,8 @@ string Source::View::GetLine(const Gtk::TextIter &begin) { // Source::View::ApplyTheme() // Applies theme in textview -void Source::View::ApplyTheme(const Source::Theme &theme) { - for (auto &item : theme.tagtable()) { +void Source::View::ApplyConfig(const Source::Config &config) { + for (auto &item : config.tagtable()) { // std::cout << "Apply: f: " << item.first << ", s: " << // item.second << std::endl; get_buffer()->create_tag(item.first)->property_foreground() = item.second; @@ -38,13 +38,13 @@ void Source::View::ApplyTheme(const Source::Theme &theme) { } void Source::View::OnOpenFile(std::vector &locations, - const Source::Theme &theme) { - ApplyTheme(theme); + const Source::Config &config) { + ApplyConfig(config); Glib::RefPtr buffer = get_buffer(); for (auto &loc : locations) { string type = std::to_string(loc.kind()); try { - theme.typetable().at(type); + config.typetable().at(type); } catch (std::exception) { continue; } @@ -66,40 +66,51 @@ void Source::View::OnOpenFile(std::vector &locations, Gtk::TextIter end_iter = buffer->get_iter_at_line_offset(linum_end, end); // std::cout << get_buffer()->get_text(begin_iter, end_iter) << std::endl; if (begin_iter.get_line() == end_iter.get_line()) { - buffer->apply_tag_by_name(theme.typetable().at(type), + buffer->apply_tag_by_name(config.typetable().at(type), begin_iter, end_iter); } } } -// Source::View::Theme::tagtable() + + +// Source::View::Config::Config(Config &config) +// copy-constructor +Source::Config::Config(const Source::Config &original) { + SetTagTable(original.tagtable()); + SetTypeTable(original.typetable()); +} + +Source::Config::Config(){} + +// Source::View::Config::tagtable() // returns a const refrence to the tagtable -const std::unordered_map& Source::Theme::tagtable() const { +const std::unordered_map& Source::Config::tagtable() const { return tagtable_; } -// Source::View::Theme::tagtable() +// Source::View::Config::tagtable() // returns a const refrence to the tagtable -const std::unordered_map& Source::Theme::typetable() const { +const std::unordered_map& Source::Config::typetable() const { return typetable_; } -void Source::Theme::InsertTag(const string &key, const string &value) { +void Source::Config::InsertTag(const string &key, const string &value) { tagtable_[key] = value; } -// Source::View::Theme::SetTagTable() +// Source::View::Config::SetTagTable() // sets the tagtable for the view -void Source::Theme::SetTypeTable( +void Source::Config::SetTypeTable( const std::unordered_map &typetable) { typetable_ = typetable; } -void Source::Theme::InsertType(const string &key, const string &value) { +void Source::Config::InsertType(const string &key, const string &value) { typetable_[key] = value; } -// Source::View::Theme::SetTagTable() +// Source::View::Config::SetTagTable() // sets the tagtable for the view -void Source::Theme::SetTagTable( +void Source::Config::SetTagTable( const std::unordered_map &tagtable) { tagtable_ = tagtable; } @@ -107,8 +118,7 @@ void Source::Theme::SetTagTable( /////////////// //// Model //// /////////////// -Source::Model::Model() : - theme_() { +/*Source::Model::Model() { std::cout << "Model constructor run" << std::endl; boost::property_tree::ptree pt; boost::property_tree::json_parser::read_json("config.json", pt); @@ -116,19 +126,23 @@ Source::Model::Model() : boost::property_tree::ptree props = pt.get_child(i.first); for (auto &pi : props) { if (i.first.compare("syntax")) { // checks the config-file - theme_.InsertTag(pi.first, pi.second.get_value()); + config_.InsertTag(pi.first, pi.second.get_value()); // std::cout << "inserting tag. " << pi.first << pi.second.get_value() << std::endl; } if (i.first.compare("colors")) { // checks the config-file - theme_.InsertType(pi.first, pi.second.get_value()); + config_.InsertType(pi.first, pi.second.get_value()); // std::cout << "inserting type. " << pi.first << pi.second.get_value() << std::endl; } } - } + } +}*/ + +Source::Model::Model(const Source::Config &config) : + config_(config) { } -Source::Theme& Source::Model::theme() { - return theme_; +Source::Config& Source::Model::config() { + return config_; } const string Source::Model::filepath() { @@ -149,12 +163,20 @@ SetSourceLocations(const std::vector &locations) { // Source::Controller::Controller() // Constructor for Controller -Source::Controller::Controller() { - //std::cout << "Controller constructor run" << std::endl; +Source::Controller::Controller(const Source::Config &config) : + model_(config) { view().get_buffer()->signal_changed().connect([this](){ this->OnLineEdit(); }); } + +/*Source::Controller::Controller(){ + //std::cout << "Controller constructor run" << std::endl; + + view().get_buffer()->signal_changed().connect([this](){ + this->OnLineEdit(); + }); + }*/ // Source::Controller::view() // return shared_ptr to the view Source::View& Source::Controller::view() { @@ -185,7 +207,7 @@ void Source::Controller::OnOpenFile(const string &filename) { int offset = view().get_buffer()->end().get_line_offset(); Clang::TranslationUnit tu(filename.c_str(), linums, offset); model().SetSourceLocations(tu.getSourceLocations()); - view().OnOpenFile(model().getSourceLocations(), model().theme()); + view().OnOpenFile(model().getSourceLocations(), model().config()); } Glib::RefPtr Source::Controller::buffer(){ diff --git a/juci/source.h b/juci/source.h index 171759b..315e2df 100644 --- a/juci/source.h +++ b/juci/source.h @@ -11,18 +11,10 @@ using std::string; namespace Source { - class Config() { - // læs json - - // - - private: - - } - - class Config { public: + Config(const Config &original); + Config(); const std::unordered_map& tagtable() const; const std::unordered_map& typetable() const; void SetTagTable(const std::unordered_map &tagtable); @@ -33,24 +25,24 @@ namespace Source { private: std::unordered_map tagtable_; std::unordered_map typetable_; - string background_; - }; // class Theme + }; // class Config class View : public Gtk::TextView { public: View(); string UpdateLine(); - void ApplyTheme(const Theme &theme); + void ApplyConfig(const Config &config); void OnOpenFile(std::vector &locations, - const Theme &theme); + const Config &config); private: string GetLine(const Gtk::TextIter &begin); }; // class View class Model{ public: - Model(); - Theme& theme(); + Model(const Source::Config &config); + //Model(); + Config& config(); const string filepath(); void SetFilePath(const string &filepath); void SetSourceLocations( @@ -60,13 +52,14 @@ namespace Source { } private: - Theme theme_; + Source::Config config_; string filepath_; std::vector locations_; }; class Controller { public: + Controller(const Source::Config &config); Controller(); View& view(); Model& model(); @@ -77,7 +70,6 @@ namespace Source { private: void OnLineEdit(); void OnSaveFile(); - protected: View view_; Model model_; diff --git a/juci/window.cc b/juci/window.cc index 05014e1..77572cf 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -2,11 +2,13 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), - notebook_(keybindings_), - menu_(keybindings_){ + main_config_(), + notebook_(keybindings_(main_config().keybindings_cfg()), main_config().source_cfg()), + menu_(keybindings_(main_config().keybindings_cfg())) { set_title("juCi++"); set_default_size(600, 400); add(window_box_); + keybindings_.action_group_menu()->add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT), [this]() { @@ -34,12 +36,6 @@ Window::Window() : } // Window constructor void Window::OnWindowHide(){ - //TODO forgie: find out how to 'remove' the pointers - //TODO forgie: Make shared_ptr - //libjuci::PluginApi::notebook_ = - // std::shared_ptr(nullptr); - // libjuci::PluginApi::menu_ = - // std::shared_ptr(nullptr); hide(); } @@ -90,5 +86,4 @@ void Window::OnOpenFile() { break; } } - } diff --git a/juci/window.h b/juci/window.h index eac2412..12a1967 100644 --- a/juci/window.h +++ b/juci/window.h @@ -4,21 +4,27 @@ #include #include "gtkmm.h" #include "api.h" +#include "config.h" #include + class Window : public Gtk::Window { public: Window(); + MainConfig& main_config() {return main_config_;} Gtk::Box window_box_; //private: + MainConfig main_config_; Keybindings::Controller keybindings_; Menu::Controller menu_; Notebook::Controller notebook_; + + private: //signal handlers void OnWindowHide(); void OnOpenFile(); -}; -#endif // JUCI_WINDOW_H_ +}; +#endif // JUCI_WINDOW_H From 477dda390dc250cfc9fadfef814b91c1fd81fde6 Mon Sep 17 00:00:00 2001 From: oyvang Date: Fri, 20 Mar 2015 10:44:38 +0100 Subject: [PATCH 4/7] Working linenumbers --- juci/notebook.cc | 257 ++++++++++++++++++++++++++++++++--------------- juci/notebook.h | 63 +++++++----- 2 files changed, 214 insertions(+), 106 deletions(-) diff --git a/juci/notebook.cc b/juci/notebook.cc index dc771ed..d3d3b72 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -1,22 +1,21 @@ + #include "notebook.h" Notebook::Model::Model() { cc_extension = ".cc"; h_extension = ".h"; }; -Notebook::View::View() : - view_(Gtk::ORIENTATION_VERTICAL){ - - -} -Gtk::Box& Notebook::View::view() { - view_.pack_start(notebook_); - return view_; +Notebook::View::View(){ + linenumbers_.set_margin_top(30); + linenumbers_.set_justify(Gtk::Justification::JUSTIFY_RIGHT); + linenumbers_.set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_START); + //line.add(linenumbers_); + view_.pack_start(notebook_); } -Notebook::Controller::Controller(Keybindings::Controller& keybindings){ +Notebook::Controller::Controller(Keybindings::Controller& keybindings) { OnNewPage("juCi++"); - refClipboard = Gtk::Clipboard::get(); + refClipboard_ = Gtk::Clipboard::get(); keybindings.action_group_menu()->add(Gtk::Action::create("FileMenu", Gtk::Stock::FILE)); @@ -27,21 +26,21 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){ "New empty file", "Create a new file"), [this]() { - is_new_file = true; + is_new_file_ = true; OnFileNewEmptyfile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("FileNewCC", "New cc file"), Gtk::AccelKey("c"), [this]() { - is_new_file = true; + is_new_file_ = true; OnFileNewCCFile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("FileNewH", "New h file"), Gtk::AccelKey("h"), [this]() { - is_new_file = true; + is_new_file_ = true; OnFileNewHeaderFile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("WindowCloseTab", @@ -53,7 +52,7 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){ keybindings.action_group_menu()->add(Gtk::Action::create("EditFind", Gtk::Stock::FIND), [this]() { - is_new_file = false; + is_new_file_ = false; OnEditSearch(); //TODO(Oyvang, Zalox, Forgi)Create function OnEditFind(); }); @@ -74,9 +73,9 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){ }); entry_.view_.entry().signal_activate().connect( [this]() { - if(is_new_file){ + if(is_new_file_){ OnNewPage(entry_.text()); - entry_.OnHideEntries(is_new_file); + entry_.OnHideEntries(is_new_file_); }else{ Search(true); } @@ -84,11 +83,11 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){ entry_.button_apply().signal_clicked().connect( [this]() { OnNewPage(entry_.text()); - entry_.OnHideEntries(is_new_file); + entry_.OnHideEntries(is_new_file_); }); entry_.button_close().signal_clicked().connect( [this]() { - entry_.OnHideEntries(is_new_file); + entry_.OnHideEntries(is_new_file_); }); entry_.button_next().signal_clicked().connect( [this]() { @@ -99,43 +98,120 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){ Search(false); }); + source_vec_.back()->view(). + signal_scroll_event().connect( + sigc::mem_fun( + this, + &Notebook::Controller:: + scroll_event_callback)); + }//Constructor -Gtk::Box& Notebook::Controller::view() { + + +bool Notebook::Controller::scroll_event_callback(GdkEventScroll* scroll_event) { + + Glib::RefPtr adj = + scrolledeverything_vec_.back()-> + get_vscrollbar()->get_adjustment(); + + if(scroll_event->delta_y == -1) { + adj->set_value(adj->get_value()-10); + } else { + adj->set_value(adj->get_value()+10); + } + + source_vec_.back()->view().set_vadjustment(adj); + linenumbers.back()->view().set_vadjustment(adj); + + + return true; + } + +Notebook::Controller::~Controller() { + for (auto &i : source_vec_) delete i; + for (auto &i : label_vec_) delete i; + for (auto &i : box_h) delete i; + for (auto &i : box_l) delete i; + for (auto &i : box_m) delete i; + for (auto &i : scrolledeverything_vec_) delete i; + for (auto &i : scrolledwindow_vec_) delete i; +} + +Gtk::HBox& Notebook::Controller::view() { return view_.view(); } -Gtk::Box& Notebook::Controller::entry_view(){ - return entry_.view(); +Gtk::Box& Notebook::Controller::entry_view() { + return entry_.view();y } + + void Notebook::Controller::OnNewPage(std::string name) { + std::cout << "new page" << std::endl;y + OnCreatePage(); + std::cout << "oppretta pages" << std::endl; + source_vec_.back()->OnNewEmptyFile(); + Notebook().append_page(*box_m.back(), name); + Notebook().show_all_children(); + Notebook().set_current_page(Pages()-1); + BufferChangeHandler(source_vec_.back()->view().get_buffer()); +} + +void Notebook::Controller::OnOpenFile(std::string path) { + OnCreatePage(); + source_vec_.back()->OnOpenFile(path); + unsigned pos = path.find_last_of("/\\"); + Notebook().append_page(*scrolledwindow_vec_.back(), path.substr(pos+1)); + Notebook().show_all_children(); + Notebook().set_focus_child(*scrolledwindow_vec_.back()); + Notebook().set_current_page(Pages()-1); + BufferChangeHandler(source_vec_.back()->view().get_buffer()); + OnBufferChange(); +} + +void Notebook::Controller::OnCreatePage(){ scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); source_vec_.push_back(new Source::Controller); - label_vec_.push_back(new Gtk::Label); - label_vec_.back()->set_text("1\n"); - label_vec_.back()->set_justify(Gtk::Justification::JUSTIFY_RIGHT); - label_vec_.back()->set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_START); - paned_vec_.push_back(new Gtk::Paned()); - paned_vec_.back()->set_orientation(Gtk::Orientation::ORIENTATION_HORIZONTAL); - paned_vec_.back()->add1(*label_vec_.back()); - paned_vec_.back()->pack2(source_vec_.back()->view(),true,false); - scrolledwindow_vec_.back()->add(*paned_vec_.back()); - source_vec_.back()->OnNewEmptyFile(); - scrolledwindow_vec_.back()->set_policy(Gtk::PolicyType::POLICY_NEVER, - Gtk::PolicyType::POLICY_AUTOMATIC); - notebook().append_page(*scrolledwindow_vec_.back(), name); - notebook().show_all_children(); - notebook().set_focus_child(*scrolledwindow_vec_.back()); - notebook().set_current_page(pages()-1); + linenumbers.push_back(new Source::Controller); + scrolledeverything_vec_.push_back(new Gtk::ScrolledWindow()); + label_vec_.push_back(new Gtk::Label()); + // box_l.push_back(new Gtk::HBox()); + //box_h.push_back(new Gtk::HBox()); + box_m.push_back(new Gtk::HBox()); + scrolledeverything_vec_.back()->add(source_vec_.back()->view()); + linesscroll.add(linenumbers.back()->view()); + linenumbers.back()->view().get_buffer()->set_text("1\n"); + + linesscroll.get_vscrollbar()->hide(); + + linenumbers.back()->view().set_editable(false); + linenumbers.back()->view().set_sensitive(false); + box_m.back()->pack_start(linesscroll,false,false); + box_m.back()->pack_start(*scrolledeverything_vec_.back(), true, true); + } + void Notebook::Controller::OnCloseCurrentPage() { //TODO (oyvang, zalox, forgi) Save a temp file, in case you close one you dont want to close? - int page = currentPage(); - notebook().remove_page(page); + int page = CurrentPage(); + Notebook().remove_page(page); delete source_vec_.at(page); + delete label_vec_.at(page); + delete box_h.at(page); + delete box_l.at(page); + delete box_m.at(page); + delete scrolledeverything_vec_.at(page); delete scrolledwindow_vec_.at(page); + source_vec_.erase(source_vec_.begin()+ page); + label_vec_.erase(label_vec_.begin()+page); + box_h.erase(box_h.begin()+page); + box_l.erase(box_l.begin()+page); + box_m.erase(box_m.begin()+page); + scrolledeverything_vec_.erase(scrolledeverything_vec_.begin()+page); scrolledwindow_vec_.erase(scrolledwindow_vec_.begin()+page); + } void Notebook::Controller::OnFileNewEmptyfile() { entry_.OnShowSetFilenName(""); @@ -147,40 +223,26 @@ void Notebook::Controller::OnFileNewHeaderFile() { entry_.OnShowSetFilenName(model_.h_extension); } void Notebook::Controller::OnEditCopy() { - if (pages() != 0) { - buffer()->copy_clipboard(refClipboard); + if (Pages() != 0) { + Buffer()->copy_clipboard(refClipboard_); } } void Notebook::Controller::OnEditPaste() { - if (pages() != 0) { - buffer()->paste_clipboard(refClipboard); + if (Pages() != 0) { + Buffer()->paste_clipboard(refClipboard_); } } void Notebook::Controller::OnEditCut() { - if (pages() != 0) { - buffer()->cut_clipboard(refClipboard); + if (Pages() != 0) { + Buffer()->cut_clipboard(refClipboard_); } } -void Notebook::Controller::OnOpenFile(std::string path) { - scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); - source_vec_.push_back(new Source::Controller); - scrolledwindow_vec_.back()->add(source_vec_.back()->view()); - source_vec_.back()->OnOpenFile(path); - scrolledwindow_vec_.back()->set_policy(Gtk::PolicyType::POLICY_NEVER, - Gtk::PolicyType::POLICY_AUTOMATIC); - unsigned pos = path.find_last_of("/\\"); - notebook().append_page(*scrolledwindow_vec_.back(), path.substr(pos+1)); - notebook().show_all_children(); - notebook().set_focus_child(*scrolledwindow_vec_.back()); - notebook().set_current_page(pages()-1); -} - std::string Notebook::Controller::GetCursorWord(){ std::string word; Gtk::TextIter start,end; - start = buffer()->get_insert()->get_iter(); - end = buffer()->get_insert()->get_iter(); + start = Buffer()->get_insert()->get_iter(); + end = Buffer()->get_insert()->get_iter(); if(!end.ends_line()) { while(!end.ends_word()){ end.forward_char(); @@ -191,16 +253,14 @@ std::string Notebook::Controller::GetCursorWord(){ start.backward_char(); } } - word = buffer()->get_text(start,end); - + word = Buffer()->get_text(start,end); //TODO(Oyvang)fix selected text return word; } -void Notebook::Controller::OnEditSearch(){ - search_match_end_ = buffer()->get_iter_at_offset(0); +void Notebook::Controller::OnEditSearch() { + search_match_end_ = Buffer()->get_iter_at_offset(0); entry_.OnShowSearch(GetCursorWord()); - } void Notebook::Controller::Search(bool forward){ @@ -210,7 +270,7 @@ void Notebook::Controller::Search(bool forward){ if(!forward){ if(search_match_start_ == 0 || search_match_start_.get_line_offset() == 0) { - search_match_start_= buffer()->end(); + search_match_start_= Buffer()->end(); } search_match_start_.backward_search(search_word, Gtk::TextSearchFlags::TEXT_SEARCH_TEXT_ONLY| @@ -218,36 +278,71 @@ void Notebook::Controller::Search(bool forward){ search_match_start_, search_match_end_); }else{ if(search_match_end_ == 0) { - search_match_end_= buffer()->begin(); + search_match_end_= Buffer()->begin(); } search_match_end_.forward_search(search_word, Gtk::TextSearchFlags::TEXT_SEARCH_TEXT_ONLY | Gtk::TextSearchFlags::TEXT_SEARCH_VISIBLE_ONLY, search_match_start_, search_match_end_); } +} - // std::cout << "matc_start - " - // << search_match_start_.get_line_offset() - // //<< test.get_line_offset() - // << " || match_end - " - // << search_match_end_.get_line_offset() - // << std::endl; +void Notebook::Controller::OnBufferChange() { + int page = CurrentPage(); + int line_nr = Buffer()->get_line_count(); + + + + Glib::RefPtr mark = Gtk::TextBuffer::Mark::create(); + Glib::RefPtr mark_lines = Gtk::TextBuffer::Mark::create(); + + if(source_vec_.at(page)->view().get_buffer()->get_insert()->get_iter().starts_line() && + source_vec_.at(page)->view().get_buffer()->get_insert()->get_iter().get_line() == Buffer()->end().get_line()){ + + + std::string lines ="1"; + for (int it = 2; it <= line_nr; ++it) { + lines.append("\n"+ std::to_string(it)+""); + +} + linenumbers.back()->view().get_buffer()->set_text(lines); + + + source_vec_.at(page)->view().get_buffer()->add_mark(mark, Buffer()->end()); + linenumbers.at(page)->view().get_buffer()->add_mark(mark_lines,linenumbers.at(page)->view().get_buffer()->end()); + + source_vec_.at(page)->view().scroll_to(mark); + linenumbers.at(page)->view().scroll_to(mark_lines); + }else{ + source_vec_.at(page)->view().get_buffer()->add_mark(mark, Buffer()->get_insert()->get_iter()); + } } -int Notebook::Controller::currentPage(){ - return notebook().get_current_page(); +int Notebook::Controller::CurrentPage() { + return Notebook().get_current_page(); } -Glib::RefPtr Notebook::Controller::buffer(){ - return source_vec_.at(currentPage())->view().get_buffer(); +Glib::RefPtr Notebook::Controller::Buffer() { + return source_vec_.at(CurrentPage())->view().get_buffer(); } -int Notebook::Controller::pages(){ - return notebook().get_n_pages(); +int Notebook::Controller::Pages() { + return Notebook().get_n_pages(); } -Gtk::Notebook& Notebook::Controller::notebook(){ +Gtk::Notebook& Notebook::Controller::Notebook() { return view_.notebook(); } +Gtk::Label& Notebook::Controller::Linenumbers() { + return view_.linenumbers(); +} + +void Notebook::Controller::BufferChangeHandler(Glib::RefPtr + buffer) { + buffer->signal_changed().connect( + [this]() { + OnBufferChange(); + }); +} diff --git a/juci/notebook.h b/juci/notebook.h index 1281076..395f541 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -16,46 +16,59 @@ namespace Notebook { class View { public: View(); - Gtk::Box& view(); - Gtk::Notebook& notebook() { return notebook_; } + Gtk::HBox& view() {return view_;} + Gtk::Notebook& notebook() {return notebook_; } + Gtk::Label& linenumbers() {return linenumbers_;} + Gtk::ScrolledWindow text, line; + protected: - Gtk::Box view_; + Gtk::HBox view_; Gtk::Notebook notebook_; + Gtk::Label linenumbers_; }; class Controller { public: + Gtk::ScrolledWindow linesscroll; Controller(Keybindings::Controller& keybindings); - Gtk::Box& view(); + ~Controller(); + Glib::RefPtr Buffer(); + int CurrentPage(); Gtk::Box& entry_view(); - void OnNewPage(std::string name); + Gtk::Notebook& Notebook(); + Gtk::Label& Linenumbers(); + void OnBufferChange(); void OnCloseCurrentPage(); - void OnOpenFile(std::string filename); - View view_; - Model model_; - Entry::Controller entry_; - std::vector source_vec_; - std::vector scrolledwindow_vec_; - std::vector label_vec_; - std::vector paned_vec_; - Glib::RefPtr refClipboard; - std::list listTargets; std::string GetCursorWord(); - Glib::RefPtr buffer(); - Gtk::Notebook& notebook(); - int currentPage(); - int pages(); - void OnFileNewEmptyfile(); - void OnFileNewCCFile(); - void OnFileNewHeaderFile(); void OnEditCopy(); - void OnEditPaste(); void OnEditCut(); + void OnEditPaste(); void OnEditSearch(); + void OnFileNewCCFile(); + void OnFileNewEmptyfile(); + void OnFileNewHeaderFile(); + void OnNewPage(std::string name); + void OnOpenFile(std::string filename); + void OnCreatePage(); + bool scroll_event_callback(GdkEventScroll* scroll_event); + int Pages(); + Gtk::HBox& view(); void Search(bool forward); - private: - bool is_new_file; + View view_; + Model model_; + bool is_new_file_; + Entry::Controller entry_; + std::vector source_vec_, linenumbers; + std::vector paned; + std::vector scrolledwindow_vec_, scrolledeverything_vec_; + std::vector label_vec_; + std::vector box_l, box_h, box_m; + std::list listTargets_; Gtk::TextIter search_match_end_; Gtk::TextIter search_match_start_; + Glib::RefPtr refClipboard_; + + protected: + void BufferChangeHandler(Glib::RefPtr buffer); }; // class controller } // namespace Notebook From 2362a4a01cb0c73ba43fd3f53a5e92c7873f5f30 Mon Sep 17 00:00:00 2001 From: tedjk Date: Fri, 20 Mar 2015 13:47:25 +0100 Subject: [PATCH 5/7] added config to source, notebook, menu and keybindings. --- juci/config.cc | 27 +++++++++++++++---- juci/config.h | 19 +++++++------- juci/config.json | 9 ++++--- juci/keybindings.cc | 64 ++++++++++++++++----------------------------- juci/keybindings.h | 31 +++++++++++----------- juci/menu.cc | 12 ++------- juci/notebook.cc | 11 +++++--- juci/notebook.h | 5 ++-- juci/window.cc | 5 ++-- juci/window.h | 7 ++--- 10 files changed, 92 insertions(+), 98 deletions(-) diff --git a/juci/config.cc b/juci/config.cc index 9a93184..5aaeead 100644 --- a/juci/config.cc +++ b/juci/config.cc @@ -1,16 +1,17 @@ #include "config.h" -MainConfig::MainConfig() { + +MainConfig::MainConfig() : +keybindings_cfg_(), source_cfg_() { boost::property_tree::json_parser::read_json("config.json", cfg_); GenerateSource(); GenerateKeybindings(); - // keybindings_cfg_ = cfg_.get_child("keybindings"); // notebook_cfg_ = cfg_.get_child("notebook"); // menu_cfg_ = cfg_.get_child("menu"); } -void MainConfig::GenerateSource(){ +void MainConfig::GenerateSource() { boost::property_tree::ptree source_json = cfg_.get_child("source"); boost::property_tree::ptree syntax_json = source_json.get_child("syntax"); boost::property_tree::ptree colors_json = source_json.get_child("colors"); @@ -18,8 +19,24 @@ void MainConfig::GenerateSource(){ source_cfg_.InsertTag(i.first, i.second.get_value()); for ( auto &i : colors_json ) - source_cfg_.InsertType(i.first, i.second.get_value()); + source_cfg_.InsertType(i.first, i.second.get_value()); } -void MainConfig::GenerateKeybindings(){ +void MainConfig::GenerateKeybindings() { + string line; + std::ifstream menu_xml("menu.xml"); + if (menu_xml.is_open()) { + while (getline(menu_xml, line)) { + keybindings_cfg_.AppendXml(line); + } + } + boost::property_tree::ptree keys_json = cfg_.get_child("keybindings"); + for (auto &i : keys_json) + keybindings_cfg_.key_map()[i.first] = i.second.get_value(); +} +Keybindings::Config& MainConfig::keybindings_cfg() { + return keybindings_cfg_; +} +const Source::Config& MainConfig::source_cfg() { + return source_cfg_; } diff --git a/juci/config.h b/juci/config.h index 56eb476..4b40506 100644 --- a/juci/config.h +++ b/juci/config.h @@ -1,30 +1,31 @@ #include #include #include -#include "source.h" +#include #include "keybindings.h" +#include "source.h" + class MainConfig { - public: + public: MainConfig(); - const Source::Config& source_cfg() {return source_cfg_; } - const Keybindings::Config& keybindings_cfg() {return keybindings_cfg_; } - + const Source::Config& source_cfg(); + Keybindings::Config& keybindings_cfg(); + void PrintMenu(); //boost::property_tree::ptree& source_cfg(); //boost::property_tree::ptree& keybindings_cfg(); //boost::property_tree::ptree& notebookk_cfg(); //boost::property_tree::ptree& menu_cfg(); - + boost::property_tree::ptree cfg_; // boost::property_tree::ptree sourcecfg_; - // boost::property_tree::ptree keybindings_cfg_; + boost::property_tree::ptree key_tree_; // boost::property_tree::ptree notebook_cfg_; // boost::property_tree::ptree menu_cfg_; - + Source::Config source_cfg_; Keybindings::Config keybindings_cfg_; void GenerateSource(); void GenerateKeybindings(); }; - diff --git a/juci/config.json b/juci/config.json index c27f2b9..4bd8f75 100644 --- a/juci/config.json +++ b/juci/config.json @@ -18,6 +18,12 @@ "705": "comment" } }, + "keybindings": { + "split_window": "s", + "new_h_file": "h", + "new_cc_file": "c", + "close_tab": "w" + }, "example": { "key": "value", "key2": [ @@ -26,8 +32,5 @@ 3 ], "key3": "value" - }, - "keybindings": { - "path" : "keybindings.xml" } } diff --git a/juci/keybindings.cc b/juci/keybindings.cc index 48860e7..b7b8a36 100644 --- a/juci/keybindings.cc +++ b/juci/keybindings.cc @@ -1,63 +1,29 @@ #include "keybindings.h" -Keybindings::Model::Model(const Keybindings::Config &config) : +Keybindings::Model::Model(Keybindings::Config &config) : menu_ui_string_(config.menu_xml()) { - /* menu_ui_string_ = - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - " " - // " " - // " " - // " " - " " - " " - " " - " " - " " - " ";*/ - - hidden_ui_string_ = + /* hidden_ui_string_ = " " " " " " " " - " "; + " ";*/ } Keybindings::Model::~Model() { } -Keybindings::Controller::Controller(const Keybindings::Config &config) : +Keybindings::Controller::Controller(Keybindings::Config &config) : config_(config), model_(config) { action_group_menu_ = Gtk::ActionGroup::create(); ui_manager_menu_ = Gtk::UIManager::create(); action_group_hidden_ = Gtk::ActionGroup::create(); ui_manager_hidden_ = Gtk::UIManager::create(); } + Keybindings::Controller::~Controller() { } + void Keybindings::Controller::BuildMenu() { try { ui_manager_menu_->add_ui_from_string(model_.menu_ui_string()); @@ -77,10 +43,24 @@ void Keybindings::Controller::BuildHiddenMenu() { ui_manager_hidden_->insert_action_group(action_group_hidden_); } -Keybindings::Config::Config(const Keybindings::Config &original) : - menu_xml_(original.menu_xml()) { +Keybindings::Config::Config(Keybindings::Config &original) { + SetMenu(original.menu_xml()); + SetKeyMap(original.key_map()); +} + +Keybindings::Config::Config() { + menu_xml_ = ""; + std::unordered_map key_map(); +} + +void Keybindings::Config::AppendXml(std::string &child) { + menu_xml_ += child; } void Keybindings::Config::SetMenu(std::string &menu_xml) { menu_xml_ = menu_xml; } + +void Keybindings::Config::SetKeyMap(std::unordered_map &key_map) { + key_map_ = key_map; +} diff --git a/juci/keybindings.h b/juci/keybindings.h index 8ad9b22..8273a38 100644 --- a/juci/keybindings.h +++ b/juci/keybindings.h @@ -2,29 +2,34 @@ #ifndef JUCI_KEYBINDINGS_H_ #define JUCI_KEYBINDINGS_H_ -#include "iostream" +#include #include "gtkmm.h" +#include +//#include "config.h" //TODO :: remove? namespace Keybindings { - class Config{ + class Config { public: - Config(const Config &original); + Config(Config &original); Config(); - const std::string& menu_xml() const {return menu_xml_;} + std::string& menu_xml() { return menu_xml_; } + std::unordered_map& key_map() { return key_map_; } + void AppendXml(std::string &child); void SetMenu(std::string &menu_xml); - + void SetKeyMap(std::unordered_map &key_map); private: + std::unordered_map key_map_; std::string menu_xml_; std::string hidden_ui_string_; };//Config class Model { public: - Model(const Keybindings::Config &config); + Model(Keybindings::Config &config); virtual ~Model(); - std::string menu_ui_string(){return menu_ui_string_;} - std::string hidden_ui_string(){return hidden_ui_string_;} + std::string menu_ui_string() { return menu_ui_string_; } + std::string hidden_ui_string() { return hidden_ui_string_; } //private: std::string menu_ui_string_; std::string hidden_ui_string_; @@ -32,7 +37,7 @@ namespace Keybindings { class Controller { public: - Controller(const Keybindings::Config &config); + explicit Controller(Keybindings::Config &config); virtual ~Controller(); Glib::RefPtr action_group_menu() { return action_group_menu_; @@ -55,13 +60,7 @@ namespace Keybindings { Glib::RefPtr action_group_hidden_; // private: Keybindings::Config config_; - Keybindings::Model model_; - - - + Keybindings::Model model_; };//Controller - - } - #endif // JUCI_KEYBINDINGS_H_ diff --git a/juci/menu.cc b/juci/menu.cc index db504a4..e3e0368 100644 --- a/juci/menu.cc +++ b/juci/menu.cc @@ -28,21 +28,13 @@ Menu::Controller::Controller(Keybindings::Controller& keybindings) : "_Window")); keybindings_.action_group_menu()->add(Gtk::Action::create("WindowSplitWindow", "Split window"), - Gtk::AccelKey("S"), + Gtk::AccelKey(keybindings_.config_ + .key_map()["split_window"]),//"S"), [this]() { OnWindowSplitWindow(); }); keybindings_.action_group_menu()->add(Gtk::Action::create("PluginMenu", "_Plugins")); - //Moved to ApiServiceProvider - /*keybindings_.action_group_menu() - ->add(Gtk::Action::create("PluginSnippet", "Snippet")); - keybindings_.action_group_menu()->add(Gtk::Action::create("PluginAddSnippet", - "Add snippet"), - Gtk::AccelKey("space"), - [this]() { - OnPluginAddSnippet(); - });*/ keybindings_.action_group_menu()->add(Gtk::Action::create("HelpMenu", Gtk::Stock::HELP)); keybindings_.action_group_menu()->add(Gtk::Action::create("HelpAbout", diff --git a/juci/notebook.cc b/juci/notebook.cc index 45f662b..c54b477 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -36,21 +36,24 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings, }); keybindings.action_group_menu()->add(Gtk::Action::create("FileNewCC", "New cc file"), - Gtk::AccelKey("c"), + Gtk::AccelKey(keybindings.config_ + .key_map()["new_cc_file"]), [this]() { is_new_file = true; OnFileNewCCFile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("FileNewH", "New h file"), - Gtk::AccelKey("h"), + Gtk::AccelKey(keybindings.config_ + .key_map()["new_h_file"]), [this]() { is_new_file = true; OnFileNewHeaderFile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("WindowCloseTab", "Close tab"), - Gtk::AccelKey("w"), + Gtk::AccelKey(keybindings.config_ + .key_map()["close_tab"]), [this]() { OnCloseCurrentPage(); }); @@ -59,7 +62,7 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings, [this]() { is_new_file = false; OnEditSearch(); - //TODO(Oyvang, Zalox, Forgi)Create function OnEditFind(); + //TODO(Oyvang, Zalox, Forgi)Create function OnEditFind(); }); keybindings.action_group_menu()->add(Gtk::Action::create("EditCopy", Gtk::Stock::COPY), diff --git a/juci/notebook.h b/juci/notebook.h index f5a4e0f..90243e6 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -25,7 +25,8 @@ namespace Notebook { }; class Controller { public: - Controller(Keybindings::Controller& keybindings,const Source::Config& config); + Controller(Keybindings::Controller& keybindings, + const Source::Config& config); Gtk::Box& view(); Gtk::Box& entry_view(); void OnNewPage(std::string name); @@ -51,7 +52,7 @@ namespace Notebook { void OnEditCut(); void OnEditSearch(); void Search(bool forward); - Source::Config& source_config() { return source_config_; } + const Source::Config& source_config() { return source_config_; } private: Source::Config source_config_; bool is_new_file; diff --git a/juci/window.cc b/juci/window.cc index 77572cf..efd34b1 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -3,8 +3,9 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), main_config_(), - notebook_(keybindings_(main_config().keybindings_cfg()), main_config().source_cfg()), - menu_(keybindings_(main_config().keybindings_cfg())) { + keybindings_(main_config_.keybindings_cfg()), + notebook_( keybindings(), main_config_.source_cfg()), + menu_( keybindings() ) { set_title("juCi++"); set_default_size(600, 400); add(window_box_); diff --git a/juci/window.h b/juci/window.h index 12a1967..c95e632 100644 --- a/juci/window.h +++ b/juci/window.h @@ -1,8 +1,6 @@ #ifndef JUCI_WINDOW_H_ #define JUCI_WINDOW_H_ -#include -#include "gtkmm.h" #include "api.h" #include "config.h" #include @@ -11,7 +9,7 @@ class Window : public Gtk::Window { public: Window(); - MainConfig& main_config() {return main_config_;} + MainConfig& main_config() { return main_config_; } Gtk::Box window_box_; //private: MainConfig main_config_; @@ -19,12 +17,11 @@ public: Menu::Controller menu_; Notebook::Controller notebook_; - + Keybindings::Controller& keybindings() { return keybindings_; } private: //signal handlers void OnWindowHide(); void OnOpenFile(); - }; #endif // JUCI_WINDOW_H From d99ae48cbf546cdf6b95d2c49b6eafc4b45d810d Mon Sep 17 00:00:00 2001 From: tedjk Date: Fri, 20 Mar 2015 14:34:32 +0100 Subject: [PATCH 6/7] reformatted code --- juci/api.cc | 115 +++++++++++++++++++++---------------------------- juci/api.h | 87 +++++++++++++++++-------------------- juci/config.h | 38 ++++++---------- juci/source.h | 2 +- juci/window.cc | 8 ++-- 5 files changed, 106 insertions(+), 144 deletions(-) diff --git a/juci/api.cc b/juci/api.cc index 1057bf8..998e6b0 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -1,15 +1,11 @@ - #include "api.h" Menu::Controller* PluginApi::menu_; Notebook::Controller* PluginApi::notebook_; - ///////////////////////////// //// API ServiceProvider //// ///////////////////////////// - -std::string PluginApi::ProjectPath() -{ +std::string PluginApi::ProjectPath() { int MAXPATHLEN = 50; char temp[MAXPATHLEN]; return ( getcwd(temp, MAXPATHLEN) ? std::string( temp ) : std::string("") ); @@ -19,10 +15,9 @@ void PluginApi::ReplaceWord(std::string word) { Glib::RefPtr buffer = libjuci::BufferFromNotebook(); Gtk::TextIter word_start = libjuci::IterFromNotebook(); Gtk::TextIter word_end = libjuci::IterFromNotebook(); - libjuci::IterToWordStart(word_start); libjuci::IterToWordEnd(word_end); - if(word_start != word_end) { + if (word_start != word_end) { buffer->erase(word_start, word_end); Gtk::TextIter current = libjuci::IterFromNotebook(); buffer->insert(current, word); @@ -34,62 +29,53 @@ void PluginApi::ReplaceLine(std::string line) { } std::string PluginApi::GetWord() { - // TODO forgie: use notebook's functions - Glib::RefPtr buffer = libjuci::BufferFromNotebook(); + Glib::RefPtr buffer = libjuci::BufferFromNotebook(); Gtk::TextIter word_start = libjuci::IterFromNotebook(); Gtk::TextIter word_end = libjuci::IterFromNotebook(); libjuci::IterToWordStart(word_start); libjuci::IterToWordEnd(word_end); - if(word_start < word_end) { + if (word_start < word_end) { std::string word = buffer->get_text(word_start, word_end); return word; } return ""; } -//runs the pythonscript that initiates every plugin within plugins/ void PluginApi::InitPlugins() { - //libjuci::LoadPlugin(g_project_root+"plugins.py"); libjuci::LoadPlugin(ProjectPath()+"/plugins.py"); } -void PluginApi::AddMenuElement(std::string plugin_name){ +void PluginApi::AddMenuElement(std::string plugin_name) { AddMenuXml(plugin_name, "PluginMenu"); std::string plugin_action_name = plugin_name+"Menu"; - menu_->keybindings_.action_group_menu() ->add(Gtk::Action::create(plugin_action_name, plugin_name)); } void PluginApi::AddSubMenuElement(std::string parent_menu, - std::string menu_name, - std::string menu_func_name, - std::string plugin_path, - std::string menu_keybinding) { - + std::string menu_name, + std::string menu_func_name, + std::string plugin_path, + std::string menu_keybinding) { AddSubMenuXml(menu_func_name, parent_menu); - menu_->keybindings_.action_group_menu() ->add(Gtk::Action::create(menu_func_name, - menu_name), - Gtk::AccelKey(menu_keybinding), - [=]() { - libjuci::LoadPluginFunction(menu_func_name, plugin_path); - }); + menu_name), + Gtk::AccelKey(menu_keybinding), + [=]() { + libjuci::LoadPluginFunction(menu_func_name, plugin_path); + }); } void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) { - std::string temp_menu = menu_->keybindings_.model_.menu_ui_string(); - std::size_t plugin_menu_pos = temp_menu.find(parent_menu); - // +2 gets you outside of the tag:<'menu_name'> ref: keybindings.cc - plugin_menu_pos+=parent_menu.size() +2; + // +2 gets you outside of the tag:<'menu_name'> ref: keybindings.cc + plugin_menu_pos+=parent_menu.size() +2; std::string menu_prefix = temp_menu.substr(0, plugin_menu_pos); std::string menu_suffix = temp_menu.substr(plugin_menu_pos); - std::string menu_input = " " " "; @@ -98,17 +84,17 @@ void PluginApi::AddMenuXml(std::string plugin_name, std::string parent_menu) { menu_prefix + menu_input + menu_suffix; } -void PluginApi::AddSubMenuXml(std::string plugin_name, std::string parent_menu){ +void PluginApi::AddSubMenuXml(std::string plugin_name, + std::string parent_menu) { std::string temp_menu = menu_->keybindings_.model_.menu_ui_string(); std::size_t parent_menu_pos = temp_menu.find(parent_menu); - // +2 gets you outside of the tag:<'menu_name'> ref: keybindings.cc - parent_menu_pos+=parent_menu.size() +2; + // +2 gets you outside of the tag:<'menu_name'> ref: keybindings.cc + parent_menu_pos+=parent_menu.size() +2; std::string menu_prefix = temp_menu.substr(0, parent_menu_pos); std::string menu_suffix = temp_menu.substr(parent_menu_pos); - + std::string menu_input =""; - menu_->keybindings_.model_.menu_ui_string_ = menu_prefix + menu_input + menu_suffix; } @@ -122,27 +108,26 @@ void libjuci::ReplaceWord(const std::string word) { void libjuci::ReplaceLine(const std::string line) { std::cout << "unimplemented function: 'libjuci::ReplaceLine()' called" - << std::endl; + << std::endl; } std::string libjuci::GetWord() { return PluginApi::GetWord(); } -void libjuci::AddMenuElement(std::string plugin_name){ +void libjuci::AddMenuElement(std::string plugin_name) { PluginApi::AddMenuElement(plugin_name); } void libjuci::AddSubMenuElement(std::string parent_menu, - std::string menu_name, - std::string menu_func_name, - std::string plugin_path, - std::string menu_keybinding) { - + std::string menu_name, + std::string menu_func_name, + std::string plugin_path, + std::string menu_keybinding) { PluginApi::AddSubMenuElement(parent_menu, - menu_name, - menu_func_name, - plugin_path, - menu_keybinding); + menu_name, + menu_func_name, + plugin_path, + menu_keybinding); } ////////////////////////////// @@ -151,49 +136,47 @@ void libjuci::AddSubMenuElement(std::string parent_menu, namespace bp = boost::python; bp::api::object libjuci::OpenPythonScript(const std::string path, - bp::api::object python_name_space) { + bp::api::object python_name_space) { bp::str str_path(path); return bp::exec_file(str_path, python_name_space); } void libjuci::LoadPlugin(const std::string& plugin_name) { - try{ + try { Py_Initialize(); bp::api::object main_module = bp::import("__main__"); bp::api::object main_namespace = main_module.attr("__dict__"); bp::api::object ignored2 = OpenPythonScript(plugin_name, main_namespace); - - }catch(bp::error_already_set const&) { + }catch (bp::error_already_set const&) { PyErr_Print(); } } void libjuci::LoadPluginFunction(const std::string &function_name, - const std::string &plugin_path){ - try{ + const std::string &plugin_path) { + try { Py_Initialize(); bp::api::object main_module = bp::import("__main__"); bp::api::object main_namespace = main_module.attr("__dict__"); bp::api::object ignored2 = OpenPythonScript(plugin_path, main_namespace); bp::str func_name(function_name); bp::api::object returnValue = bp::eval(func_name, main_namespace); - }catch(bp::error_already_set const&) { + }catch (bp::error_already_set const&) { PyErr_Print(); } } void libjuci::InitPlugin(const std::string& plugin_path) { - try{ + try { Py_Initialize(); bp::api::object main_module = bp::import("__main__"); bp::api::object main_namespace = main_module.attr("__dict__"); bp::api::object ignored2 = OpenPythonScript(plugin_path, main_namespace); - bp::object returnValue = bp::eval("initPlugin()", main_namespace); - //std::string return_value = bp::extract(pySnippet); - //do something with return_value - }catch(bp::error_already_set const&) { + bp::object returnValue = bp::eval("initPlugin()", main_namespace); + // do something with return_value + }catch (bp::error_already_set const&) { PyErr_Print(); } } @@ -202,30 +185,30 @@ void libjuci::InitPlugin(const std::string& plugin_path) { //// Glib wrappers //// /////////////////////// void libjuci::IterToWordStart(Gtk::TextIter &iter) { - if(!iter.starts_line()) { - while(!iter.starts_word()) { + if (!iter.starts_line()) { + while (!iter.starts_word()) { iter.backward_char(); } } } void libjuci::IterToWordEnd(Gtk::TextIter &iter) { - if(!iter.ends_line()) { - while(!iter.ends_word()) { + if (!iter.ends_line()) { + while (!iter.ends_word()) { iter.forward_char(); } } } Glib::RefPtr libjuci::BufferFromNotebook() { - //finding focused view + // finding focused view int i = 0; - while(!PluginApi::notebook_->source_vec_.at(i)->view().has_focus()) { + while (!PluginApi::notebook_->source_vec_.at(i)->view().has_focus()) { i++; } return Glib::RefPtr(PluginApi::notebook_ - ->source_vec_.at(i) - ->view().get_buffer()); + ->source_vec_.at(i) + ->view().get_buffer()); } Gtk::TextIter libjuci::IterFromNotebook() { diff --git a/juci/api.h b/juci/api.h index 38f0008..eb17fc5 100644 --- a/juci/api.h +++ b/juci/api.h @@ -7,46 +7,39 @@ #include "notebook.h" #include "menu.h" - //////////////////// - //// Plugin Api //// - //////////////////// - class PluginApi { - public: - static Menu::Controller* menu_; - static Notebook::Controller* notebook_; - - static void InitPlugins(); - static std::string ProjectPath(); - - //for Python module: - static std::string GetWord(); - //menu management - static void AddMenuElement(const std::string plugin_name); - static void AddSubMenuElement(const std::string parent_menu, - const std::string menu_name, - const std::string menu_func_name, - const std::string plugin_path, - const std::string menu_keybinding); - //text-buffer functions - static void ReplaceWord(const std::string word); - static void ReplaceLine(const std::string line); - - protected: - static void AddMenuXml(std::string plugin_name, std::string parent_menu); - static void AddSubMenuXml(std::string plugin_name, std::string parent_menu); - }; - +//////////////////// +//// Plugin Api //// +//////////////////// +class PluginApi { +public: + static Menu::Controller* menu_; + static Notebook::Controller* notebook_; + static void InitPlugins(); + static std::string ProjectPath(); + // for Python module: + static std::string GetWord(); + // menu management + static void AddMenuElement(const std::string plugin_name); + static void AddSubMenuElement(const std::string parent_menu, + const std::string menu_name, + const std::string menu_func_name, + const std::string plugin_path, + const std::string menu_keybinding); + // text-buffer functions + static void ReplaceWord(const std::string word); + static void ReplaceLine(const std::string line); +protected: + static void AddMenuXml(std::string plugin_name, std::string parent_menu); + static void AddSubMenuXml(std::string plugin_name, std::string parent_menu); +}; // PluginApi namespace libjuci { - /////////////////////// //// Glib wrappers //// /////////////////////// void IterToWordStart(Gtk::TextIter &iter); void IterToWordEnd(Gtk::TextIter &iter); Gtk::TextIter IterFromNotebook(); - //TODO forgie: make sure it does not get the buffer to the last created textview. Glib::RefPtr BufferFromNotebook(); - /////////////////////// //// Api to python //// /////////////////////// @@ -57,27 +50,25 @@ namespace libjuci { void AddMenuElement(const std::string plugin_name); void AddSubMenuElement(const std::string parent_menu, - const std::string menu_name, - const std::string menu_func_name, - const std::string plugin_path, - const std::string menu_keybinding); - + const std::string menu_name, + const std::string menu_func_name, + const std::string plugin_path, + const std::string menu_keybinding); void AddMenuXml(const std::string plugin_name, - const string parent_menu); + const string parent_menu); void AddSubMenuXml(const std::string plugin_name, - const string parent_menu); - //TODO forgie: Make more functions targeting the python module - + const string parent_menu); ////////////////////////////// //// Boost.Python methods //// ////////////////////////////// - boost::python::api::object OpenPythonScript(const std::string path, - boost::python::api::object python_name_space); - void LoadPlugin(const std::string& plugin_name); + namespace bp = boost::python; + bp::api::object OpenPythonScript(const std::string path, + bp::api::object python_name_space); - void LoadPluginFunction(const std::string &function_name, const std::string &plugin_path); + void LoadPlugin(const std::string& plugin_name); + void LoadPluginFunction(const std::string &function_name, + const std::string &plugin_path); void InitPlugin(const std::string& plugin_path); - -}//libjuci -#endif // JUCI_API_H +} // libjuci +#endif // JUCI_API_H_ diff --git a/juci/config.h b/juci/config.h index 4b40506..4967f25 100644 --- a/juci/config.h +++ b/juci/config.h @@ -5,27 +5,17 @@ #include "keybindings.h" #include "source.h" - class MainConfig { - public: - MainConfig(); - const Source::Config& source_cfg(); - Keybindings::Config& keybindings_cfg(); - - void PrintMenu(); - //boost::property_tree::ptree& source_cfg(); - //boost::property_tree::ptree& keybindings_cfg(); - //boost::property_tree::ptree& notebookk_cfg(); - //boost::property_tree::ptree& menu_cfg(); - - boost::property_tree::ptree cfg_; - // boost::property_tree::ptree sourcecfg_; - boost::property_tree::ptree key_tree_; - // boost::property_tree::ptree notebook_cfg_; - // boost::property_tree::ptree menu_cfg_; - - Source::Config source_cfg_; - Keybindings::Config keybindings_cfg_; - - void GenerateSource(); - void GenerateKeybindings(); - }; +class MainConfig { +public: + MainConfig(); + const Source::Config& source_cfg(); + Keybindings::Config& keybindings_cfg(); + void PrintMenu(); + void GenerateSource(); + void GenerateKeybindings(); +private: + boost::property_tree::ptree cfg_; + boost::property_tree::ptree key_tree_; + Source::Config source_cfg_; + Keybindings::Config keybindings_cfg_; +}; diff --git a/juci/source.h b/juci/source.h index 315e2df..af94a1c 100644 --- a/juci/source.h +++ b/juci/source.h @@ -46,7 +46,7 @@ namespace Source { const string filepath(); void SetFilePath(const string &filepath); void SetSourceLocations( - const std::vector &locations); + const std::vector &locations); std::vector& getSourceLocations() { return locations_; } diff --git a/juci/window.cc b/juci/window.cc index efd34b1..8e96e9c 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -4,8 +4,8 @@ Window::Window() : window_box_(Gtk::ORIENTATION_VERTICAL), main_config_(), keybindings_(main_config_.keybindings_cfg()), - notebook_( keybindings(), main_config_.source_cfg()), - menu_( keybindings() ) { + notebook_(keybindings(), main_config_.source_cfg()), + menu_(keybindings()) { set_title("juCi++"); set_default_size(600, 400); add(window_box_); @@ -26,8 +26,6 @@ Window::Window() : add_accel_group(keybindings_.ui_manager_menu()->get_accel_group()); add_accel_group(keybindings_.ui_manager_hidden()->get_accel_group()); - - //moved here from menu.cc by forgie keybindings_.BuildMenu(); window_box_.pack_start(menu_.view(), Gtk::PACK_SHRINK); @@ -36,7 +34,7 @@ Window::Window() : show_all_children(); } // Window constructor -void Window::OnWindowHide(){ +void Window::OnWindowHide() { hide(); } From a8e571432cb3f469aa4b76d2cd5564547af1644f Mon Sep 17 00:00:00 2001 From: oyvang Date: Fri, 20 Mar 2015 15:08:07 +0100 Subject: [PATCH 7/7] Linenumbers working, know bug->focus new tab --- juci/api.cc | 11 +- juci/notebook.cc | 276 ++++++++++++++++++++++++----------------------- juci/notebook.h | 26 +++-- 3 files changed, 161 insertions(+), 152 deletions(-) diff --git a/juci/api.cc b/juci/api.cc index 1057bf8..995d344 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -220,12 +220,15 @@ void libjuci::IterToWordEnd(Gtk::TextIter &iter) { Glib::RefPtr libjuci::BufferFromNotebook() { //finding focused view int i = 0; - while(!PluginApi::notebook_->source_vec_.at(i)->view().has_focus()) { - i++; + + while(!PluginApi::notebook_->CurrentTextView().has_focus()) { + i++; } + + return Glib::RefPtr(PluginApi::notebook_ - ->source_vec_.at(i) - ->view().get_buffer()); + ->CurrentTextView().get_buffer()); + } Gtk::TextIter libjuci::IterFromNotebook() { diff --git a/juci/notebook.cc b/juci/notebook.cc index d3d3b72..bdc0c30 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -2,14 +2,11 @@ #include "notebook.h" Notebook::Model::Model() { - cc_extension = ".cc"; - h_extension = ".h"; + cc_extension_ = ".cc"; + h_extension_ = ".h"; + scrollvalue_ = 20; }; Notebook::View::View(){ - linenumbers_.set_margin_top(30); - linenumbers_.set_justify(Gtk::Justification::JUSTIFY_RIGHT); - linenumbers_.set_alignment(Gtk::ALIGN_START, Gtk::ALIGN_START); - //line.add(linenumbers_); view_.pack_start(notebook_); } @@ -98,9 +95,8 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings) { Search(false); }); - source_vec_.back()->view(). - signal_scroll_event().connect( - sigc::mem_fun( + text_vec_.back()->view(). + signal_scroll_event().connect(sigc::mem_fun( this, &Notebook::Controller:: scroll_event_callback)); @@ -109,140 +105,139 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings) { }//Constructor - + bool Notebook::Controller::scroll_event_callback(GdkEventScroll* scroll_event) { - + int page = CurrentPage(); + int direction_y = scroll_event->delta_y; + int direction_x = scroll_event->delta_x; + Glib::RefPtr adj = - scrolledeverything_vec_.back()-> + scrolledtext_vec_.at(page)-> get_vscrollbar()->get_adjustment(); - - if(scroll_event->delta_y == -1) { - adj->set_value(adj->get_value()-10); - } else { - adj->set_value(adj->get_value()+10); - } - - source_vec_.back()->view().set_vadjustment(adj); - linenumbers.back()->view().set_vadjustment(adj); - - - return true; - } + + if ( direction_y != 0 ) { + int dir_val = direction_y==-1?-model_.scrollvalue_:+model_.scrollvalue_; + adj->set_value(adj->get_value()+dir_val); + text_vec_.at(page)->view().set_vadjustment(adj); + linenumbers_vec_.at(page)->view().set_vadjustment(adj); + } + if ( direction_x != 0 ) { + int dir_val = direction_x==-1?-model_.scrollvalue_:+model_.scrollvalue_; + adj->set_value(adj->get_value()+dir_val); + text_vec_.at(page)->view().set_hadjustment(adj); + } + return true; +} Notebook::Controller::~Controller() { - for (auto &i : source_vec_) delete i; - for (auto &i : label_vec_) delete i; - for (auto &i : box_h) delete i; - for (auto &i : box_l) delete i; - for (auto &i : box_m) delete i; - for (auto &i : scrolledeverything_vec_) delete i; - for (auto &i : scrolledwindow_vec_) delete i; + for (auto &i : text_vec_) delete i; + for (auto &i : linenumbers_vec_) delete i; + for (auto &i : editor_vec_) delete i; + for (auto &i : scrolledtext_vec_) delete i; + for (auto &i : scrolledline_vec_) delete i; } Gtk::HBox& Notebook::Controller::view() { return view_.view(); } Gtk::Box& Notebook::Controller::entry_view() { - return entry_.view();y + return entry_.view(); } void Notebook::Controller::OnNewPage(std::string name) { - std::cout << "new page" << std::endl;y OnCreatePage(); std::cout << "oppretta pages" << std::endl; - source_vec_.back()->OnNewEmptyFile(); - Notebook().append_page(*box_m.back(), name); + text_vec_.back()->OnNewEmptyFile(); + Notebook().append_page(*editor_vec_.back(), name); Notebook().show_all_children(); Notebook().set_current_page(Pages()-1); - BufferChangeHandler(source_vec_.back()->view().get_buffer()); + Notebook().set_focus_child(text_vec_.at(Pages()-1)->view()); } void Notebook::Controller::OnOpenFile(std::string path) { OnCreatePage(); - source_vec_.back()->OnOpenFile(path); + text_vec_.back()->OnOpenFile(path); unsigned pos = path.find_last_of("/\\"); - Notebook().append_page(*scrolledwindow_vec_.back(), path.substr(pos+1)); + Notebook().append_page(*editor_vec_.back(), path.substr(pos+1)); Notebook().show_all_children(); - Notebook().set_focus_child(*scrolledwindow_vec_.back()); Notebook().set_current_page(Pages()-1); - BufferChangeHandler(source_vec_.back()->view().get_buffer()); + Notebook().set_focus_child(text_vec_.back()->view()); OnBufferChange(); } void Notebook::Controller::OnCreatePage(){ - scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); - source_vec_.push_back(new Source::Controller); - linenumbers.push_back(new Source::Controller); - scrolledeverything_vec_.push_back(new Gtk::ScrolledWindow()); - label_vec_.push_back(new Gtk::Label()); - // box_l.push_back(new Gtk::HBox()); - //box_h.push_back(new Gtk::HBox()); - box_m.push_back(new Gtk::HBox()); - scrolledeverything_vec_.back()->add(source_vec_.back()->view()); - linesscroll.add(linenumbers.back()->view()); - linenumbers.back()->view().get_buffer()->set_text("1\n"); - - linesscroll.get_vscrollbar()->hide(); - - linenumbers.back()->view().set_editable(false); - linenumbers.back()->view().set_sensitive(false); - box_m.back()->pack_start(linesscroll,false,false); - box_m.back()->pack_start(*scrolledeverything_vec_.back(), true, true); - + text_vec_.push_back(new Source::Controller); + std::string temp = "TEXT "; + temp += text_vec_.size(); + text_vec_.back()->view().set_name(temp); + linenumbers_vec_.push_back(new Source::Controller); + scrolledline_vec_.push_back(new Gtk::ScrolledWindow()); + scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); + editor_vec_.push_back(new Gtk::HBox()); + scrolledtext_vec_.back()->add(text_vec_.back()->view()); + scrolledline_vec_.back()->add(linenumbers_vec_.back()->view()); + linenumbers_vec_.back()->view().get_buffer()->set_text("1 \n"); + linenumbers_vec_.back()->view().override_color(Gdk::RGBA("Black")); + linenumbers_vec_.back()-> + view().set_justification(Gtk::Justification::JUSTIFY_RIGHT); + scrolledline_vec_.back()->get_vscrollbar()->hide(); + linenumbers_vec_.back()->view().set_editable(false); + linenumbers_vec_.back()->view().set_sensitive(false); + editor_vec_.back()->pack_start(*scrolledline_vec_.back(),false,false); + editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); + BufferChangeHandler(text_vec_.back()->view().get_buffer()); } void Notebook::Controller::OnCloseCurrentPage() { //TODO (oyvang, zalox, forgi) Save a temp file, in case you close one you dont want to close? - int page = CurrentPage(); - Notebook().remove_page(page); - delete source_vec_.at(page); - delete label_vec_.at(page); - delete box_h.at(page); - delete box_l.at(page); - delete box_m.at(page); - delete scrolledeverything_vec_.at(page); - delete scrolledwindow_vec_.at(page); - - source_vec_.erase(source_vec_.begin()+ page); - label_vec_.erase(label_vec_.begin()+page); - box_h.erase(box_h.begin()+page); - box_l.erase(box_l.begin()+page); - box_m.erase(box_m.begin()+page); - scrolledeverything_vec_.erase(scrolledeverything_vec_.begin()+page); - scrolledwindow_vec_.erase(scrolledwindow_vec_.begin()+page); + if(Pages()!=0){ + int page = CurrentPage(); + Notebook().remove_page(page); + delete text_vec_.at(page); + delete linenumbers_vec_.at(page); + delete scrolledtext_vec_.at(page); + delete scrolledline_vec_.at(page); + delete editor_vec_.at(page); + text_vec_.erase(text_vec_.begin()+ page); + linenumbers_vec_.erase(linenumbers_vec_.begin()+page); + scrolledtext_vec_.erase(scrolledtext_vec_.begin()+page); + scrolledline_vec_.erase(scrolledline_vec_.begin()+page); + editor_vec_.erase(editor_vec_.begin()+page); + } } void Notebook::Controller::OnFileNewEmptyfile() { entry_.OnShowSetFilenName(""); } void Notebook::Controller::OnFileNewCCFile() { - entry_.OnShowSetFilenName(model_.cc_extension); + entry_.OnShowSetFilenName(model_.cc_extension_); } void Notebook::Controller::OnFileNewHeaderFile() { - entry_.OnShowSetFilenName(model_.h_extension); + entry_.OnShowSetFilenName(model_.h_extension_); } void Notebook::Controller::OnEditCopy() { if (Pages() != 0) { - Buffer()->copy_clipboard(refClipboard_); + Buffer(text_vec_.at(CurrentPage()))->copy_clipboard(refClipboard_); } } void Notebook::Controller::OnEditPaste() { if (Pages() != 0) { - Buffer()->paste_clipboard(refClipboard_); + Buffer(text_vec_.at(CurrentPage()))->paste_clipboard(refClipboard_); } } void Notebook::Controller::OnEditCut() { if (Pages() != 0) { - Buffer()->cut_clipboard(refClipboard_); + Buffer(text_vec_.at(CurrentPage()))->cut_clipboard(refClipboard_); } } std::string Notebook::Controller::GetCursorWord(){ + int page = CurrentPage(); std::string word; Gtk::TextIter start,end; - start = Buffer()->get_insert()->get_iter(); - end = Buffer()->get_insert()->get_iter(); + start = Buffer(text_vec_.at(page))->get_insert()->get_iter(); + end = Buffer(text_vec_.at(page))->get_insert()->get_iter(); if(!end.ends_line()) { while(!end.ends_word()){ end.forward_char(); @@ -253,80 +248,96 @@ std::string Notebook::Controller::GetCursorWord(){ start.backward_char(); } } - word = Buffer()->get_text(start,end); + word = Buffer(text_vec_.at(page))->get_text(start,end); //TODO(Oyvang)fix selected text return word; } void Notebook::Controller::OnEditSearch() { - search_match_end_ = Buffer()->get_iter_at_offset(0); + search_match_end_ = + Buffer(text_vec_.at(CurrentPage()))->get_iter_at_offset(0); entry_.OnShowSearch(GetCursorWord()); } void Notebook::Controller::Search(bool forward){ + int page = CurrentPage(); std::string search_word; search_word = entry_.text(); Gtk::TextIter test; - if(!forward){ - if(search_match_start_ == 0 || search_match_start_.get_line_offset() == 0) { - search_match_start_= Buffer()->end(); + if ( !forward ) { + if ( search_match_start_ == 0 || + search_match_start_.get_line_offset() == 0) { + search_match_start_= Buffer(text_vec_.at(CurrentPage()))->end(); } - search_match_start_.backward_search(search_word, - Gtk::TextSearchFlags::TEXT_SEARCH_TEXT_ONLY| - Gtk::TextSearchFlags::TEXT_SEARCH_VISIBLE_ONLY, - search_match_start_, search_match_end_); - }else{ - if(search_match_end_ == 0) { - search_match_end_= Buffer()->begin(); + search_match_start_. + backward_search(search_word, + Gtk::TextSearchFlags::TEXT_SEARCH_TEXT_ONLY | + Gtk::TextSearchFlags::TEXT_SEARCH_VISIBLE_ONLY, + search_match_start_, + search_match_end_); + } else { + if ( search_match_end_ == 0 ) { + search_match_end_= Buffer(text_vec_.at(CurrentPage()))->begin(); } - search_match_end_.forward_search(search_word, - Gtk::TextSearchFlags::TEXT_SEARCH_TEXT_ONLY | - Gtk::TextSearchFlags::TEXT_SEARCH_VISIBLE_ONLY, - search_match_start_, search_match_end_); + search_match_end_. + forward_search(search_word, + Gtk::TextSearchFlags::TEXT_SEARCH_TEXT_ONLY | + Gtk::TextSearchFlags::TEXT_SEARCH_VISIBLE_ONLY, + search_match_start_, + search_match_end_); } } - void Notebook::Controller::OnBufferChange() { int page = CurrentPage(); - int line_nr = Buffer()->get_line_count(); - - - - - - Glib::RefPtr mark = Gtk::TextBuffer::Mark::create(); - Glib::RefPtr mark_lines = Gtk::TextBuffer::Mark::create(); - - if(source_vec_.at(page)->view().get_buffer()->get_insert()->get_iter().starts_line() && - source_vec_.at(page)->view().get_buffer()->get_insert()->get_iter().get_line() == Buffer()->end().get_line()){ - - - std::string lines ="1"; - for (int it = 2; it <= line_nr; ++it) { - lines.append("\n"+ std::to_string(it)+""); - -} - linenumbers.back()->view().get_buffer()->set_text(lines); - + int line_nr = Buffer(text_vec_.at(page))->get_line_count(); + + Glib::RefPtr + mark = Gtk::TextBuffer::Mark::create(); + Glib::RefPtr + mark_lines = Gtk::TextBuffer::Mark::create(); + + if(Buffer(text_vec_.at(page))->get_insert()->get_iter().starts_line() && + Buffer(text_vec_.at(page))->get_insert()->get_iter().get_line() == + Buffer(text_vec_.at(page))->end().get_line()) { + std::string lines ="1 "; + for ( int it = 2; it <= line_nr; ++it ) { + lines.append("\n"+ std::to_string(it)+" "); + } + Buffer(linenumbers_vec_.at(page))->set_text(lines); + + Buffer(text_vec_.at(page))-> + add_mark( + mark, + Buffer(text_vec_.at(page))->end()); + Buffer(linenumbers_vec_.at(page))-> + add_mark( + mark_lines, + Buffer(linenumbers_vec_.at(page))->end()); + + text_vec_.at(page)->view().scroll_to(mark); + linenumbers_vec_.at(page)->view().scroll_to(mark_lines); + }else{ + Buffer(text_vec_.at(page))-> + add_mark( + mark, + Buffer(text_vec_.at(page))-> + get_insert()->get_iter()); + } +} - source_vec_.at(page)->view().get_buffer()->add_mark(mark, Buffer()->end()); - linenumbers.at(page)->view().get_buffer()->add_mark(mark_lines,linenumbers.at(page)->view().get_buffer()->end()); - source_vec_.at(page)->view().scroll_to(mark); - linenumbers.at(page)->view().scroll_to(mark_lines); - }else{ - source_vec_.at(page)->view().get_buffer()->add_mark(mark, Buffer()->get_insert()->get_iter()); - } +Gtk::TextView& Notebook::Controller::CurrentTextView() { + return text_vec_.at(CurrentPage())->view(); } - int Notebook::Controller::CurrentPage() { return Notebook().get_current_page(); } -Glib::RefPtr Notebook::Controller::Buffer() { - return source_vec_.at(CurrentPage())->view().get_buffer(); +Glib::RefPtr +Notebook::Controller::Buffer( Source::Controller *source ) { + return source->view().get_buffer(); } int Notebook::Controller::Pages() { @@ -335,9 +346,6 @@ int Notebook::Controller::Pages() { Gtk::Notebook& Notebook::Controller::Notebook() { return view_.notebook(); } -Gtk::Label& Notebook::Controller::Linenumbers() { - return view_.linenumbers(); -} void Notebook::Controller::BufferChangeHandler(Glib::RefPtr buffer) { diff --git a/juci/notebook.h b/juci/notebook.h index 395f541..ccfdbb6 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -10,32 +10,30 @@ namespace Notebook { class Model { public: Model(); - std::string cc_extension; - std::string h_extension; + std::string cc_extension_; + std::string h_extension_; + int scrollvalue_; }; class View { public: View(); Gtk::HBox& view() {return view_;} Gtk::Notebook& notebook() {return notebook_; } - Gtk::Label& linenumbers() {return linenumbers_;} Gtk::ScrolledWindow text, line; protected: Gtk::HBox view_; Gtk::Notebook notebook_; - Gtk::Label linenumbers_; }; class Controller { public: - Gtk::ScrolledWindow linesscroll; Controller(Keybindings::Controller& keybindings); ~Controller(); - Glib::RefPtr Buffer(); + Glib::RefPtr Buffer( Source::Controller *source); + Gtk::TextView& CurrentTextView(); int CurrentPage(); Gtk::Box& entry_view(); Gtk::Notebook& Notebook(); - Gtk::Label& Linenumbers(); void OnBufferChange(); void OnCloseCurrentPage(); std::string GetCursorWord(); @@ -53,22 +51,22 @@ namespace Notebook { int Pages(); Gtk::HBox& view(); void Search(bool forward); + protected: + void BufferChangeHandler(Glib::RefPtr buffer); + private: View view_; Model model_; bool is_new_file_; Entry::Controller entry_; - std::vector source_vec_, linenumbers; - std::vector paned; - std::vector scrolledwindow_vec_, scrolledeverything_vec_; - std::vector label_vec_; - std::vector box_l, box_h, box_m; + std::vector text_vec_, linenumbers_vec_; + std::vector scrolledtext_vec_, scrolledline_vec_; + std::vector editor_vec_; std::list listTargets_; Gtk::TextIter search_match_end_; Gtk::TextIter search_match_start_; Glib::RefPtr refClipboard_; - protected: - void BufferChangeHandler(Glib::RefPtr buffer); + }; // class controller } // namespace Notebook