diff --git a/juci/config.json b/juci/config.json index 90723a7..0d35a91 100644 --- a/juci/config.json +++ b/juci/config.json @@ -2,6 +2,7 @@ "source": { "colors": { "text_color": "black", + "search": "orange", "string": "#CC0000", "namespace_ref": "#990099", "type": "#0066FF", diff --git a/juci/notebook.cc b/juci/notebook.cc index bc89429..fa52666 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -1,6 +1,8 @@ #include #include "notebook.h" #include "logging.h" +#include // c-library + Notebook::View::View() { pack2(notebook); @@ -55,8 +57,7 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller Gtk::AccelKey(keybindings.config_ .key_map()["edit_find"]), [this]() { - OnEditSearch(); - // TODO(Oyvang) Zalox, Forgi)Create function OnEditFind(); + entry.show_search(""); }); keybindings.action_group_menu()-> add(Gtk::Action::create("EditCopy", @@ -65,15 +66,19 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller .key_map()["edit_copy"]), [this]() { - OnEditCopy(); + if (Pages() != 0) { + CurrentTextView().get_buffer()->copy_clipboard(refClipboard_); + } }); keybindings.action_group_menu()-> add(Gtk::Action::create("EditCut", "Cut"), Gtk::AccelKey(keybindings.config_ - .key_map()["edit_cut"]), + .key_map()["edit_cut"]), [this]() { - OnEditCut(); + if (Pages() != 0) { + CurrentTextView().get_buffer()->cut_clipboard(refClipboard_); + } }); keybindings.action_group_menu()-> add(Gtk::Action::create("EditPaste", @@ -81,7 +86,9 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller Gtk::AccelKey(keybindings.config_ .key_map()["edit_paste"]), [this]() { - OnEditPaste(); + if (Pages() != 0) { + CurrentTextView().get_buffer()->paste_clipboard(refClipboard_); + } }); keybindings.action_group_menu()-> @@ -147,12 +154,12 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller entry.button_next.signal_clicked(). connect( [this]() { - Search(true); + search(true); }); entry.button_prev.signal_clicked(). connect( [this]() { - Search(false); + search(false); }); INFO("Notebook signal handlers sucsess"); } @@ -171,7 +178,7 @@ void Notebook::Controller::OnOpenFile(std::string path) { editor_vec_.push_back(new Gtk::HBox()); scrolledtext_vec_.back()->add(*text_vec_.back()->view); editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); - size_t pos = path.find_last_of("/\\"); + size_t pos = path.find_last_of("/\\"); // TODO #windows std::string filename=path; if(pos!=std::string::npos) filename=path.substr(pos+1); @@ -211,77 +218,39 @@ void Notebook::Controller::OnCloseCurrentPage() { void Notebook::Controller::OnFileNewFile() { entry.show_set_filename(); } -void Notebook::Controller::OnEditCopy() { - if (Pages() != 0) { - CurrentTextView().get_buffer()->copy_clipboard(refClipboard_); - } -} -void Notebook::Controller::OnEditPaste() { - if (Pages() != 0) { - CurrentTextView().get_buffer()->paste_clipboard(refClipboard_); - } -} -void Notebook::Controller::OnEditCut() { - if (Pages() != 0) { - CurrentTextView().get_buffer()->cut_clipboard(refClipboard_); - } -} -std::string Notebook::Controller::GetCursorWord() { - INFO("Notebook get cursor word"); - std::string word; - Gtk::TextIter start, end; - start = CurrentTextView().get_buffer()->get_insert()->get_iter(); - end = CurrentTextView().get_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(); - } - } - word = CurrentTextView().get_buffer()->get_text(start, end); - // TODO(Oyvang) fix selected text - return word; -} - -void Notebook::Controller::OnEditSearch() { - search_match_end_ = - CurrentTextView().get_buffer()->get_iter_at_offset(0); - entry.show_search(GetCursorWord()); -} - -void Notebook::Controller::Search(bool forward) { - INFO("Notebook search"); - std::string search_word; - search_word = entry(); - Gtk::TextIter test; - - if ( !forward ) { - if ( search_match_start_ == 0 || - search_match_start_.get_line_offset() == 0) { - search_match_start_ = CurrentTextView().get_buffer()->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_); +void Notebook::Controller::search(bool forward) { + INFO("Notebook search"); + auto start = CurrentTextView().search_start; + auto end = CurrentTextView().search_end; + // fetch buffer and greate settings + auto buffer = CurrentTextView().get_source_buffer(); + auto settings = gtk_source_search_settings_new(); + // get search text from entry + gtk_source_search_settings_set_search_text(settings, entry().c_str()); + // make sure the search continues + gtk_source_search_settings_set_wrap_around(settings, true); + auto context = gtk_source_search_context_new(buffer->gobj(), settings); + gtk_source_search_context_set_highlight(context, forward); + auto itr = buffer->get_insert()->get_iter(); + buffer->remove_tag_by_name("search", start ? start : itr, end ? end : itr); + if (forward) { + DEBUG("Doing forward search"); + gtk_source_search_context_forward(context, + end ? end.gobj() : itr.gobj(), + start.gobj(), + end.gobj()); } else { - if ( search_match_end_ == 0 ) { - search_match_end_ = CurrentTextView().get_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_); + DEBUG("Doing backward search"); + gtk_source_search_context_backward(context, + start ? start.gobj() : itr.gobj(), + start.gobj(), + end.gobj()); } + buffer->apply_tag_by_name("search", start, end); + CurrentTextView().scroll_to(end); + CurrentTextView().search_start = start; + CurrentTextView().search_end = end; } void Notebook::Controller diff --git a/juci/notebook.h b/juci/notebook.h index 6c96f5e..5aba1bf 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -31,11 +31,6 @@ namespace Notebook { int CurrentPage(); Gtk::Notebook& Notebook(); void OnCloseCurrentPage(); - std::string GetCursorWord(); - void OnEditCopy(); - void OnEditCut(); - void OnEditPaste(); - void OnEditSearch(); void OnFileNewFile(); bool OnSaveFile(); bool OnSaveFile(std::string path); @@ -43,7 +38,7 @@ namespace Notebook { Gtk::TreeViewColumn* column); void OnOpenFile(std::string filename); int Pages(); - void Search(bool forward); + void search(bool forward); View view; std::string OnSaveFileAs(); std::string project_path; @@ -61,8 +56,6 @@ namespace Notebook { std::vector scrolledtext_vec_; std::vector editor_vec_; std::list listTargets_; - Gtk::TextIter search_match_end_; - Gtk::TextIter search_match_start_; Glib::RefPtr refClipboard_; }; // class controller } // namespace Notebook diff --git a/juci/source.cc b/juci/source.cc index 7fc9cf1..1275cca 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -27,11 +27,11 @@ config(config), file_path(file_path), project_path(project_path) { set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); set_show_line_numbers(config.show_line_numbers); set_highlight_current_line(config.highlight_current_line); - sourcefile s(file_path); get_source_buffer()->get_undo_manager()->begin_not_undoable_action(); get_source_buffer()->set_text(s.get_content()); get_source_buffer()->get_undo_manager()->end_not_undoable_action(); + search_start = search_end = this->get_buffer()->end(); } string Source::View::get_line(size_t line_number) { diff --git a/juci/source.h b/juci/source.h index 7653ba1..d6ec5b1 100644 --- a/juci/source.h +++ b/juci/source.h @@ -63,6 +63,7 @@ namespace Source { std::string get_line_before_insert(); std::string file_path; std::string project_path; + Gtk::TextIter search_start, search_end; protected: const Source::Config& config; bool on_key_press(GdkEventKey* key);