From 5eb5aa3ee8398cd820cadd6b7e053e2622b47cbf Mon Sep 17 00:00:00 2001 From: oyvang Date: Wed, 11 Mar 2015 15:51:32 +0100 Subject: [PATCH] Fixed search and optimized some code --- juci/api.cc | 2 +- juci/entry.cc | 51 ++++++++------- juci/entry.h | 22 +++---- juci/notebook.cc | 165 +++++++++++++++++++++++++++++++---------------- juci/notebook.h | 18 +++++- juci/source.cc | 10 ++- juci/source.h | 1 + 7 files changed, 170 insertions(+), 99 deletions(-) diff --git a/juci/api.cc b/juci/api.cc index ee38351..e55c4ef 100644 --- a/juci/api.cc +++ b/juci/api.cc @@ -55,7 +55,7 @@ void libjuci::ApiServiceProvider::AddKeybinding() { std::cout << "ctrl alt space" << std::endl; libjuci::LoadPlugin("snippet"); }); - std::cout << "addkeybinding" << std::endl; + //std::cout << "addkeybinding" << std::endl; } /////////////////////// diff --git a/juci/entry.cc b/juci/entry.cc index 7be5861..011ce6c 100644 --- a/juci/entry.cc +++ b/juci/entry.cc @@ -1,18 +1,11 @@ #include "entry.h" - -Entry::Model::Model() : - next_("Next"), - prev_("Prev"){ - std::cout<<"Model Entry"<add(source_vec_.back()->view()); source_vec_.back()->OnNewEmptyFile(); - view_.notebook().append_page(*scrolledwindow_vec_.back(), "juCi++"); + 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", @@ -27,21 +31,21 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){ "New empty file", "Create a new file"), [this]() { - isnewfile = true; + is_new_file = true; OnFileNewEmptyfile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("FileNewCC", "New cc file"), Gtk::AccelKey("c"), [this]() { - isnewfile = true; + is_new_file = true; OnFileNewCCFile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("FileNewH", "New h file"), Gtk::AccelKey("h"), [this]() { - isnewfile = true; + is_new_file = true; OnFileNewHeaderFile(); }); keybindings.action_group_menu()->add(Gtk::Action::create("WindowCloseTab", @@ -53,7 +57,7 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){ keybindings.action_group_menu()->add(Gtk::Action::create("EditFind", Gtk::Stock::FIND), [this]() { - isnewfile = false; + is_new_file = false; OnEditSearch(); //TODO(Oyvang, Zalox, Forgi)Create function OnEditFind(); }); @@ -74,13 +78,33 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){ }); entry_.view_.entry().signal_activate().connect( [this]() { - if(isnewfile){ + if(is_new_file){ OnNewPage(entry_.text()); + entry_.OnHideEntries(is_new_file); }else{ - Search(); + Search(true); } - entry_.OnHideEntries(); }); + entry_.button_apply().signal_clicked().connect( + [this]() { + OnNewPage(entry_.text()); + entry_.OnHideEntries(is_new_file); + }); + entry_.button_close().signal_clicked().connect( + [this]() { + entry_.OnHideEntries(is_new_file); + }); + entry_.button_next().signal_clicked().connect( + [this]() { + Search(true); + }); + entry_.button_prev().signal_clicked().connect( + [this]() { + Search(false); + }); + + + }//Constructor Gtk::Box& Notebook::Controller::view() { return view_.view(); @@ -93,15 +117,15 @@ void Notebook::Controller::OnNewPage(std::string name) { source_vec_.push_back(new Source::Controller); scrolledwindow_vec_.back()->add(source_vec_.back()->view()); source_vec_.back()->OnNewEmptyFile(); - view_.notebook().append_page(*scrolledwindow_vec_.back(), name); - view_.notebook().show_all_children(); - view_.notebook().set_focus_child(*scrolledwindow_vec_.back()); - view_.notebook().set_current_page(view_.notebook().get_n_pages()-1); + notebook().append_page(*scrolledwindow_vec_.back(), name); + notebook().show_all_children(); + notebook().set_focus_child(*scrolledwindow_vec_.back()); + notebook().set_current_page(pages()-1); } void Notebook::Controller::OnCloseCurrentPage() { //TODO (oyvang, zalox, forgi) Save a temp file, in case you close one you dont want to close? - int page = view_.notebook().get_current_page(); - view_.notebook().remove_page(page); + int page = currentPage(); + notebook().remove_page(page); delete source_vec_.at(page); delete scrolledwindow_vec_.at(page); source_vec_.erase(source_vec_.begin()+ page); @@ -111,33 +135,24 @@ void Notebook::Controller::OnFileNewEmptyfile() { entry_.OnShowSetFilenName(""); } void Notebook::Controller::OnFileNewCCFile() { - entry_.OnShowSetFilenName(".cc"); + entry_.OnShowSetFilenName(model_.cc_extension); } void Notebook::Controller::OnFileNewHeaderFile() { - entry_.OnShowSetFilenName(".h"); + entry_.OnShowSetFilenName(model_.h_extension); } void Notebook::Controller::OnEditCopy() { - if (view_.notebook().get_n_pages() != 0) { - int source_pos = view_.notebook().get_current_page(); - Glib::RefPtr buffer = source_vec_.at(source_pos) - ->view().get_buffer(); - buffer->copy_clipboard(refClipboard); + if (pages() != 0) { + buffer()->copy_clipboard(refClipboard); } } void Notebook::Controller::OnEditPaste() { - if (view_.notebook().get_n_pages() != 0) { - int source_pos = view_.notebook().get_current_page(); - Glib::RefPtr buffer = source_vec_.at(source_pos) - ->view().get_buffer(); - buffer->paste_clipboard(refClipboard); + if (pages() != 0) { + buffer()->paste_clipboard(refClipboard); } } void Notebook::Controller::OnEditCut() { - if (view_.notebook().get_n_pages() != 0) { - int source_pos = view_.notebook().get_current_page(); - Glib::RefPtr buffer = source_vec_.at(source_pos) - ->view().get_buffer(); - buffer->cut_clipboard(refClipboard); + if (pages() != 0) { + buffer()->cut_clipboard(refClipboard); } } @@ -147,48 +162,84 @@ void Notebook::Controller::OnOpenFile(std::string path) { scrolledwindow_vec_.back()->add(source_vec_.back()->view()); source_vec_.back()->OnOpenFile(path); unsigned pos = path.find_last_of("/\\"); - view_.notebook().append_page(*scrolledwindow_vec_.back(), path.substr(pos+1)); - view_.notebook().show_all_children(); - view_.notebook().set_focus_child(*scrolledwindow_vec_.back()); - view_.notebook().set_current_page(view_.notebook().get_n_pages()-1); + 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(){ - int page = view_.notebook().get_current_page(); std::string word; Gtk::TextIter start,end; - start=source_vec_.at(page)->view().get_buffer()->begin(); - end=source_vec_.at(page)->view().get_buffer()->end(); - // if(!source_vec_.at(page)->view().get_buffer()->get_selection_bounds(start, - // end)){ - start=source_vec_.at(page)->view().get_buffer()->get_insert()->get_iter(); - end=source_vec_.at(page)->view().get_buffer()->get_insert()->get_iter(); - if(!end.ends_line()) { - while(!end.ends_word()){ - end.forward_char(); - } + start = buffer()->get_insert()->get_iter(); + end = buffer()->get_insert()->get_iter(); + if(!end.ends_line()) { + while(!end.ends_word()){ + end.forward_char(); } - if(!start.starts_line()) { - while(!start.starts_word()){ - start.backward_char(); - } + } + if(!start.starts_line()) { + while(!start.starts_word()){ + start.backward_char(); } - word = source_vec_.at(page)->view().get_buffer()->get_text(start,end); + } + word = buffer()->get_text(start,end); - //TODO(Oyvang)fix selected text - return word; + //TODO(Oyvang)fix selected text + return word; } void Notebook::Controller::OnEditSearch(){ + search_match_end_ = buffer()->get_iter_at_offset(0); entry_.OnShowSearch(GetCursorWord()); } -void Notebook::Controller::Search(){ +void Notebook::Controller::Search(bool forward){ std::string search_word; search_word = entry_.text(); - //TODO(oyvang) create seacrh function - std::cout<<"funka"<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_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; + + +} + + +int Notebook::Controller::currentPage(){ + return notebook().get_current_page(); } +Glib::RefPtr Notebook::Controller::buffer(){ + return source_vec_.at(currentPage())->view().get_buffer(); +} +int Notebook::Controller::pages(){ + return notebook().get_n_pages(); +} +Gtk::Notebook& Notebook::Controller::notebook(){ + return view_.notebook(); +} diff --git a/juci/notebook.h b/juci/notebook.h index ac1f6ef..1692270 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -7,6 +7,12 @@ #include "source.h" namespace Notebook { + class Model { + public: + Model(); + std::string cc_extension; + std::string h_extension; + }; class View { public: View(); @@ -25,12 +31,17 @@ namespace Notebook { void OnCloseCurrentPage(); void OnOpenFile(std::string filename); View view_; + Model model_; Entry::Controller entry_; std::vector source_vec_; std::vector scrolledwindow_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(); @@ -38,10 +49,11 @@ namespace Notebook { void OnEditPaste(); void OnEditCut(); void OnEditSearch(); - void Search(); + void Search(bool forward); private: - bool isnewfile; - + bool is_new_file; + Gtk::TextIter search_match_end_; + Gtk::TextIter search_match_start_; }; // class controller } // namespace Notebook diff --git a/juci/source.cc b/juci/source.cc index ca45c53..d2634b3 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -8,14 +8,14 @@ //// View //// ////////////// Source::View::View() { - std::cout << "View constructor run" << std::endl; + // std::cout << "View constructor run" << std::endl; override_font(Pango::FontDescription("Monospace")); } // Source::View::UpdateLine // returns the new line string Source::View::UpdateLine() { Gtk::TextIter line(get_buffer()->get_insert()->get_iter()); - std::cout << line.get_line() << std::endl; + //std::cout << line.get_line() << std::endl; // for each word --> check what it is --> apply appropriate tag return ""; } @@ -150,7 +150,7 @@ SetSourceLocations(const std::vector &locations) { // Source::Controller::Controller() // Constructor for Controller Source::Controller::Controller() { - std::cout << "Controller constructor run" << std::endl; + //std::cout << "Controller constructor run" << std::endl; view().get_buffer()->signal_changed().connect([this](){ this->OnLineEdit(); }); @@ -187,3 +187,7 @@ void Source::Controller::OnOpenFile(const string &filename) { model().SetSourceLocations(tu.getSourceLocations()); view().OnOpenFile(model().getSourceLocations(), model().theme()); } + +Glib::RefPtr Source::Controller::buffer(){ + return view().get_buffer(); +} diff --git a/juci/source.h b/juci/source.h index 135d0a3..13b59ba 100644 --- a/juci/source.h +++ b/juci/source.h @@ -61,6 +61,7 @@ namespace Source { Model& model(); void OnNewEmptyFile(); void OnOpenFile(const string &filename); + Glib::RefPtr buffer(); private: void OnLineEdit();