From d33df03dd7fa97360d79c93b4887739c31e1b711 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 23 Jun 2015 17:49:16 +0200 Subject: [PATCH] Moved completiondialog-code to seperate file: selectiondialog.h/cc. The selectiondialog is now started from Source::ClangView. Some cleanup. See source.cc lines 340-345 for example use of SelectionDialog. --- juci/CMakeLists.txt | 3 + juci/notebook.cc | 195 -------------------------------------------- juci/notebook.h | 17 ---- juci/source.cc | 72 ++++++++++++++++ juci/source.h | 1 + juci/window.cc | 7 -- juci/window.h | 1 - 7 files changed, 76 insertions(+), 220 deletions(-) diff --git a/juci/CMakeLists.txt b/juci/CMakeLists.txt index b7f4dfa..4dcf244 100644 --- a/juci/CMakeLists.txt +++ b/juci/CMakeLists.txt @@ -99,6 +99,7 @@ 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.h juci.cc keybindings.h keybindings.cc @@ -106,6 +107,8 @@ add_executable(${project_name} menu.cc source.h source.cc + selectiondialog.h + selectiondialog.cc config.h config.cc sourcefile.h diff --git a/juci/notebook.cc b/juci/notebook.cc index 177f104..b2fb0b8 100644 --- a/juci/notebook.cc +++ b/juci/notebook.cc @@ -22,7 +22,6 @@ Notebook::Controller::Controller(Gtk::Window* window, INFO("Create notebook"); window_ = window; refClipboard_ = Gtk::Clipboard::get(); - ispopup = false; view().pack1(directories_.widget(), true, true); CreateKeybindings(keybindings); INFO("Notebook Controller Success"); @@ -176,114 +175,6 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller INFO("Notebook signal handlers sucsess"); } -bool Notebook::Controller:: OnMouseRelease(GdkEventButton* button) { - if (button->button == 1 && ispopup) { - popup_.response(Gtk::RESPONSE_DELETE_EVENT); - return true; - } - return false; -} - -bool Notebook::Controller::OnKeyRelease(GdkEventKey* key) { - return GeneratePopup(key->keyval); -} - -bool Notebook::Controller::GeneratePopup(int key_id) { - INFO("Notebook genereate popup, getting iters"); - std::string path = text_vec_.at(CurrentPage())->view->file_path; - if (!source_config().legal_extension(path.substr(path.find_last_of(".") + 1))) return false; - // Get function to fill popup with suggests item vector under is for testing - Gtk::TextIter beg = CurrentTextView().get_buffer()->get_insert()->get_iter(); - Gtk::TextIter end = CurrentTextView().get_buffer()->get_insert()->get_iter(); - Gtk::TextIter tmp = CurrentTextView().get_buffer()->get_insert()->get_iter(); - Gtk::TextIter tmp1 = CurrentTextView().get_buffer()->get_insert()->get_iter(); - Gtk::TextIter line = - CurrentTextView().get_buffer()->get_iter_at_line(tmp.get_line()); - if (end.backward_char() && end.backward_char()) { - bool illegal_chars = - end.backward_search("\"", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line) - || - end.backward_search("//", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line); - INFO("Notebook genereate popup, checking key_id"); - if (illegal_chars) { - return false; - } - std::string c = text_vec_[CurrentPage()]->buffer()->get_text(end, beg); - switch (key_id) { - case 46: - break; - case 58: - if (c != "::") return false; - break; - case 60: - if (c != "->") return false; - break; - case 62: - if (c != "->") return false; - break; - default: - return false; - } - } else { - return false; - } - INFO("Notebook genereate popup, getting autocompletions"); - std::vector acdata=text_vec_.at(CurrentPage())->view-> - get_autocomplete_suggestions(beg.get_line()+1, - beg.get_line_offset()+2); - std::map items; - for (auto &data : acdata) { - std::stringstream ss; - std::string return_value; - for (auto &chunk : data.chunks) { - switch (chunk.kind) { - case clang::CompletionChunk_ResultType: - return_value = chunk.chunk; - break; - case clang::CompletionChunk_Informative: break; - default: ss << chunk.chunk; break; - } - } - if (ss.str().length() > 0) { // if length is 0 the result is empty - items[ss.str() + " --> " + return_value] = ss.str(); - } - } - Gtk::ScrolledWindow popup_scroll_; - Gtk::ListViewText listview_(1, false, Gtk::SelectionMode::SELECTION_SINGLE); - popup_scroll_.set_policy(Gtk::PolicyType::POLICY_NEVER, - Gtk::PolicyType::POLICY_NEVER); - listview_.set_enable_search(true); - listview_.set_headers_visible(false); - listview_.set_hscroll_policy(Gtk::ScrollablePolicy::SCROLL_NATURAL); - listview_.set_activate_on_single_click(true); - if (items.empty()) { - items["No suggestions found..."] = ""; - } - for (auto &i : items) { - listview_.append(i.first); - } - popup_scroll_.add(listview_); - popup_.get_vbox()->pack_start(popup_scroll_); - popup_.set_transient_for(*window_); - popup_.show_all(); - INFO("Notebook genereate popup, moving popup"); - int popup_x = popup_.get_width(); - int popup_y = items.size() * 20; - PopupSetSize(popup_scroll_, popup_x, popup_y); - int x, y; - FindPopupPosition(CurrentTextView(), popup_x, popup_y, x, y); - popup_.move(x, y+15); - INFO("Notebook genereate popup, create handler"); - PopupSelectHandler(popup_, listview_, &items); - ispopup = true; - INFO("Notebook genereate popup, run popup"); - popup_.run(); - INFO("Notebook genereate popup, hide popup"); - popup_.hide(); - ispopup = false; - return true; -} - Notebook::Controller::~Controller() { INFO("Notebook destructor"); for (auto &i : editor_vec_) delete i; @@ -467,12 +358,6 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr } void Notebook::Controller::set_source_handlers(Source::Controller& controller) { - controller.view->signal_button_release_event(). - connect(sigc::mem_fun(*this, &Notebook::Controller::OnMouseRelease), false); - - controller.view->signal_key_release_event(). - connect(sigc::mem_fun(*this, &Notebook::Controller::OnKeyRelease), false); - //Add star on tab label when the page is not saved: controller.buffer()->signal_changed().connect([this]() { if(text_vec_.at(CurrentPage())->is_saved) { @@ -487,86 +372,6 @@ void Notebook::Controller::set_source_handlers(Source::Controller& controller) { }); } -void Notebook::Controller::PopupSelectHandler(Gtk::Dialog &popup, - Gtk::ListViewText &listview, - std::map - *items) { - listview.signal_row_activated(). - connect([this, &listview, &popup, items](const Gtk::TreeModel::Path& path, - Gtk::TreeViewColumn*) { - std::string selected = items-> - at(listview.get_text(listview.get_selected()[0])); - CurrentTextView().get_buffer()->insert_at_cursor(selected); - popup.response(Gtk::RESPONSE_DELETE_EVENT); - }); -} -void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll, - int ¤t_x, - int ¤t_y) { - INFO("Notebook popup set size"); - int textview_x = CurrentTextView().get_width(); - int textview_y = 150; - bool is_never_scroll_x = true; - bool is_never_scroll_y = true; - if (current_x > textview_x) { - current_x = textview_x; - is_never_scroll_x = false; - } - if (current_y > textview_y) { - current_y = textview_y; - is_never_scroll_y = false; - } - scroll.set_size_request(current_x, current_y); - if (!is_never_scroll_x && !is_never_scroll_y) { - scroll.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC, - Gtk::PolicyType::POLICY_AUTOMATIC); - } else if (!is_never_scroll_x && is_never_scroll_y) { - scroll.set_policy(Gtk::PolicyType::POLICY_AUTOMATIC, - Gtk::PolicyType::POLICY_NEVER); - } else if (is_never_scroll_x && !is_never_scroll_y) { - scroll.set_policy(Gtk::PolicyType::POLICY_NEVER, - Gtk::PolicyType::POLICY_AUTOMATIC); - } -} - -std::string Notebook::Controller::CurrentPagePath(){ - return text_vec_.at(CurrentPage())->view->file_path; -} - -void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, - int popup_x, - int popup_y, - int &x, - int &y) { - INFO("Notebook popup find position"); - Gdk::Rectangle temp1, temp2; - textview.get_cursor_locations( - CurrentTextView(). - get_buffer()->get_insert()-> - get_iter(), temp1, temp2); - int textview_edge_x = 0; - int textview_edge_y = 0; - textview.buffer_to_window_coords( - Gtk::TextWindowType::TEXT_WINDOW_WIDGET, - temp1.get_x(), - temp1.get_y(), - x, y); - Glib::RefPtr gdkw = - CurrentTextView().get_window(Gtk::TextWindowType::TEXT_WINDOW_WIDGET); - gdkw->get_origin(textview_edge_x, textview_edge_y); - - x += textview_edge_x; - y += textview_edge_y; - if ((textview_edge_x-x)*-1 > textview.get_width()-popup_x) { - x -= popup_x; - if (x < textview_edge_x) x = textview_edge_x; - } - if ((textview_edge_y-y)*-1 > textview.get_height()-popup_y) { - y -= (popup_y+14) + 15; - if (x < textview_edge_y) y = textview_edge_y +15; - } -} - bool Notebook::Controller:: OnSaveFile() { std::string path=text_vec_.at(CurrentPage())->view->file_path; return OnSaveFile(path); diff --git a/juci/notebook.h b/juci/notebook.h index 0ffff31..21791ba 100644 --- a/juci/notebook.h +++ b/juci/notebook.h @@ -63,30 +63,15 @@ namespace Notebook { int Pages(); Directories::Controller& directories() { return directories_; } Gtk::Paned& view(); - bool GeneratePopup(int key); void Search(bool forward); Source::Config& source_config() { return source_config_; } - bool OnMouseRelease(GdkEventButton* button); - bool OnKeyRelease(GdkEventKey* key); std::string OnSaveFileAs(); bool LegalExtension(std::string extension); std::string project_path; protected: void set_source_handlers(Source::Controller& controller); - void PopupSelectHandler(Gtk::Dialog &popup, - Gtk::ListViewText &listview, - std::map - *items); private: void CreateKeybindings(Keybindings::Controller& keybindings); - void FindPopupPosition(Gtk::TextView& textview, - int popup_x, - int popup_y, - int &x, - int &y); - void PopupSetSize(Gtk::ScrolledWindow& scroll, - int ¤t_x, - int ¤t_y); void AskToSaveDialog(); Glib::RefPtr m_refBuilder; Glib::RefPtr refActionGroup; @@ -104,8 +89,6 @@ namespace Notebook { Gtk::TextIter search_match_end_; Gtk::TextIter search_match_start_; Glib::RefPtr refClipboard_; - bool ispopup; - Gtk::Dialog popup_; Gtk::Window* window_; }; // class controller } // namespace Notebook diff --git a/juci/source.cc b/juci/source.cc index 8377728..0a5ad6c 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -6,6 +6,7 @@ #include "logging.h" #include #include +#include "selectiondialog.h" bool Source::Config::legal_extension(std::string e) const { std::transform(e.begin(), e.end(),e.begin(), ::tolower); @@ -119,6 +120,7 @@ parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { }); signal_key_press_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_press), false); + signal_key_release_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_release), false); } Source::ClangView::~ClangView() { @@ -275,6 +277,76 @@ highlight_token(clang::Token *token, end_offset), token_kind); } +bool Source::ClangView::on_key_release(GdkEventKey* key) { + INFO("Source::ClangView::on_key_release getting iters"); + // Get function to fill popup with suggests item vector under is for testing + Gtk::TextIter beg = get_source_buffer()->get_insert()->get_iter(); + Gtk::TextIter end = get_source_buffer()->get_insert()->get_iter(); + Gtk::TextIter tmp = get_source_buffer()->get_insert()->get_iter(); + Gtk::TextIter tmp1 = get_source_buffer()->get_insert()->get_iter(); + Gtk::TextIter line = get_source_buffer()->get_iter_at_line(tmp.get_line()); + if (end.backward_char() && end.backward_char()) { + bool illegal_chars = + end.backward_search("\"", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line) + || + end.backward_search("//", Gtk::TEXT_SEARCH_VISIBLE_ONLY, tmp, tmp1, line); + INFO("Source::ClangView::on_key_release checking key->keyval"); + if (illegal_chars) { + return false; + } + std::string c = get_source_buffer()->get_text(end, beg); + switch (key->keyval) { + case 46: + break; + case 58: + if (c != "::") return false; + break; + case 60: + if (c != "->") return false; + break; + case 62: + if (c != "->") return false; + break; + default: + return false; + } + } else { + return false; + } + INFO("Source::ClangView::on_key_release getting autocompletions"); + std::vector acdata=get_autocomplete_suggestions(beg.get_line()+1, + beg.get_line_offset()+2); + std::map rows; + for (auto &data : acdata) { + std::stringstream ss; + std::string return_value; + for (auto &chunk : data.chunks) { + switch (chunk.kind) { + case clang::CompletionChunk_ResultType: + return_value = chunk.chunk; + break; + case clang::CompletionChunk_Informative: break; + default: ss << chunk.chunk; break; + } + } + if (ss.str().length() > 0) { // if length is 0 the result is empty + rows[ss.str() + " --> " + return_value] = ss.str(); + } + } + if (rows.empty()) { + rows["No suggestions found..."] = ""; + } + + SelectionDialog selection_dialog(*this); + selection_dialog.on_select=[this, &rows](Gtk::ListViewText& list_view_text){ + std::string selected = rows.at(list_view_text.get_text(list_view_text.get_selected()[0])); + get_source_buffer()->insert_at_cursor(selected); + }; + selection_dialog.show(rows); + + return true; +} + //TODO: replace indentation methods with a better implementation or //maybe use libclang bool Source::ClangView::on_key_press(GdkEventKey* key) { diff --git a/juci/source.h b/juci/source.h index 1deba5c..7c7b2a2 100644 --- a/juci/source.h +++ b/juci/source.h @@ -103,6 +103,7 @@ namespace Source { std::vector *source_ranges); std::vector get_compilation_commands(); bool on_key_press(GdkEventKey* key); + bool on_key_release(GdkEventKey* key); Glib::Dispatcher parse_done; Glib::Dispatcher parse_start; diff --git a/juci/window.cc b/juci/window.cc index e139437..e3c116a 100644 --- a/juci/window.cc +++ b/juci/window.cc @@ -104,10 +104,6 @@ Window::Window() : execute.detach(); } }); - this->signal_button_release_event(). - connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false); - terminal_.Terminal().signal_button_release_event(). - connect(sigc::mem_fun(*this,&Window::OnMouseRelease),false); add_accel_group(keybindings_.ui_manager_menu()->get_accel_group()); add_accel_group(keybindings_.ui_manager_hidden()->get_accel_group()); @@ -203,9 +199,6 @@ void Window::OnOpenFile() { } } } -bool Window::OnMouseRelease(GdkEventButton *button){ - return notebook_.OnMouseRelease(button); -} bool Window::SaveFile() { if(notebook_.OnSaveFile()) { diff --git a/juci/window.h b/juci/window.h index d60dfdb..344a51e 100644 --- a/juci/window.h +++ b/juci/window.h @@ -32,7 +32,6 @@ public: void OnWindowHide(); void OnOpenFile(); void OnFileOpenFolder(); - bool OnMouseRelease(GdkEventButton* button); bool SaveFile(); bool SaveFileAs(); };