diff --git a/juci/selectiondialog.cc b/juci/selectiondialog.cc index b618c12..8eca51d 100644 --- a/juci/selectiondialog.cc +++ b/juci/selectiondialog.cc @@ -1,6 +1,6 @@ #include "selectiondialog.h" -SelectionDialog::SelectionDialog(Source::View& view): Gtk::Dialog(), view(view), +SelectionDialog::SelectionDialog(Gtk::TextView& text_view): Gtk::Dialog(), text_view(text_view), list_view_text(1, false, Gtk::SelectionMode::SELECTION_SINGLE) { scrolled_window.set_policy(Gtk::PolicyType::POLICY_NEVER, Gtk::PolicyType::POLICY_NEVER); list_view_text.set_enable_search(true); @@ -15,7 +15,7 @@ void SelectionDialog::show(const std::map& rows) { } scrolled_window.add(list_view_text); get_vbox()->pack_start(scrolled_window); - set_transient_for((Gtk::Window&)(*view.get_toplevel())); + set_transient_for((Gtk::Window&)(*text_view.get_toplevel())); show_all(); int popup_x = get_width(); int popup_y = rows.size() * 20; @@ -39,7 +39,7 @@ bool SelectionDialog::close(GdkEventFocus*) { void SelectionDialog::adjust(int current_x, int current_y) { INFO("SelectionDialog set size"); - int view_x = view.get_width(); + int view_x = text_view.get_width(); int view_y = 150; bool is_never_scroll_x = true; bool is_never_scroll_y = true; @@ -62,22 +62,22 @@ void SelectionDialog::adjust(int current_x, int current_y) { INFO("SelectionDialog set position"); Gdk::Rectangle temp1, temp2; - view.get_cursor_locations(view.get_source_buffer()->get_insert()->get_iter(), temp1, temp2); + text_view.get_cursor_locations(text_view.get_buffer()->get_insert()->get_iter(), temp1, temp2); int view_edge_x = 0; int view_edge_y = 0; int x, y; - view.buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_WIDGET, + text_view.buffer_to_window_coords(Gtk::TextWindowType::TEXT_WINDOW_WIDGET, temp1.get_x(), temp1.get_y(), x, y); - Glib::RefPtr gdkw = view.get_window(Gtk::TextWindowType::TEXT_WINDOW_WIDGET); + Glib::RefPtr gdkw = text_view.get_window(Gtk::TextWindowType::TEXT_WINDOW_WIDGET); gdkw->get_origin(view_edge_x, view_edge_y); x += view_edge_x; y += view_edge_y; - if ((view_edge_x-x)*-1 > view.get_width()-current_x) { + if ((view_edge_x-x)*-1 > text_view.get_width()-current_x) { x -= current_x; if (x < view_edge_x) x = view_edge_x; } - if ((view_edge_y-y)*-1 > view.get_height()-current_y) { + if ((view_edge_y-y)*-1 > text_view.get_height()-current_y) { y -= (current_y+14) + 15; if (x < view_edge_y) y = view_edge_y +15; } diff --git a/juci/selectiondialog.h b/juci/selectiondialog.h index 6ea11c4..70e16de 100644 --- a/juci/selectiondialog.h +++ b/juci/selectiondialog.h @@ -4,7 +4,7 @@ class SelectionDialog : public Gtk::Dialog { public: - SelectionDialog(Source::View& view); + SelectionDialog(Gtk::TextView& text_view); void show(const std::map& rows); bool close(GdkEventFocus*); @@ -13,7 +13,7 @@ public: private: void adjust(int current_x, int current_y); - Source::View& view; + Gtk::TextView& text_view; Gtk::ScrolledWindow scrolled_window; Gtk::ListViewText list_view_text; }; \ No newline at end of file diff --git a/juci/source.cc b/juci/source.cc index 0a5ad6c..b7d794d 100644 --- a/juci/source.cc +++ b/juci/source.cc @@ -21,7 +21,7 @@ bool Source::Config::legal_extension(std::string e) const { ////////////// //// View //// ////////////// -Source::View::View(const Config& config, const std::string& file_path, const std::string& project_path): +Source::View::View(const Source::Config& config, const std::string& file_path, const std::string& project_path): config(config), file_path(file_path), project_path(project_path) { Gsv::init(); set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); @@ -50,13 +50,52 @@ string Source::View::get_line_before_insert() { return line; } +bool Source::View::on_key_press(GdkEventKey* key) { + //Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text. + if(key->keyval==GDK_KEY_Tab && key->state==0) { + Gtk::TextIter selection_start, selection_end; + get_source_buffer()->get_selection_bounds(selection_start, selection_end); + int line_start=selection_start.get_line(); + int line_end=selection_end.get_line(); + for(int line=line_start;line<=line_end;line++) { + Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line); + get_source_buffer()->insert(line_it, config.tab); + } + return true; + } + //Indent left when clicking shift-tab, no matter where in the line the cursor is. Also works on selected text. + else if((key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) && key->state==GDK_SHIFT_MASK) { + Gtk::TextIter selection_start, selection_end; + get_source_buffer()->get_selection_bounds(selection_start, selection_end); + int line_start=selection_start.get_line(); + int line_end=selection_end.get_line(); + + for(int line_nr=line_start;line_nr<=line_end;line_nr++) { + string line=get_line(line_nr); + if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) + return true; + } + + for(int line_nr=line_start;line_nr<=line_end;line_nr++) { + Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); + Gtk::TextIter line_plus_it=line_it; + + for(unsigned c=0;cerase(line_it, line_plus_it); + } + return true; + } + return false; +} + ////////////////// //// ClangView /// ////////////////// clang::Index Source::ClangView::clang_index(0, 1); -Source::ClangView::ClangView(const Config& config, const std::string& file_path, const std::string& project_path): -Source::View::View(config, file_path, project_path), +Source::ClangView::ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path): +Source::View(config, file_path, project_path), parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) { override_font(Pango::FontDescription(config.font)); override_background_color(Gdk::RGBA(config.background)); @@ -410,41 +449,6 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { } return true; } - //Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text. - if(key->keyval==GDK_KEY_Tab && key->state==0) { - Gtk::TextIter selection_start, selection_end; - get_source_buffer()->get_selection_bounds(selection_start, selection_end); - int line_start=selection_start.get_line(); - int line_end=selection_end.get_line(); - for(int line=line_start;line<=line_end;line++) { - Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line); - get_source_buffer()->insert(line_it, config.tab); - } - return true; - } - //Indent left when clicking shift-tab, no matter where in the line the cursor is. Also works on selected text. - else if((key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) && key->state==GDK_SHIFT_MASK) { - Gtk::TextIter selection_start, selection_end; - get_source_buffer()->get_selection_bounds(selection_start, selection_end); - int line_start=selection_start.get_line(); - int line_end=selection_end.get_line(); - - for(int line_nr=line_start;line_nr<=line_end;line_nr++) { - string line=get_line(line_nr); - if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) - return true; - } - - for(int line_nr=line_start;line_nr<=line_end;line_nr++) { - Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr); - Gtk::TextIter line_plus_it=line_it; - - for(unsigned c=0;cerase(line_it, line_plus_it); - } - return true; - } //Indent left when writing } on a new line else if(key->keyval==GDK_KEY_braceright) { string line=get_line_before_insert(); @@ -479,7 +483,8 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { } } } - return false; + + return Source::View::on_key_press(key); } //////////////////// @@ -489,8 +494,7 @@ bool Source::ClangView::on_key_press(GdkEventKey* key) { // Source::Controller::Controller() // Constructor for Controller Source::Controller::Controller(const Source::Config &config, - const std::string& file_path, std::string project_path) : - config(config) { + const std::string& file_path, std::string project_path) { if(project_path=="") { project_path=boost::filesystem::path(file_path).parent_path().string(); } diff --git a/juci/source.h b/juci/source.h index 7c7b2a2..e779c35 100644 --- a/juci/source.h +++ b/juci/source.h @@ -60,25 +60,31 @@ namespace Source { class View : public Gsv::View { public: - View(const Config& config, const std::string& file_path, const std::string& project_path); + View(const Source::Config& config, const std::string& file_path, const std::string& project_path); std::string get_line(size_t line_number); std::string get_line_before_insert(); - virtual std::vector get_autocomplete_suggestions(int line_number, int column) {return std::vector();} std::string file_path; std::string project_path; protected: - const Config& config; + const Source::Config& config; + bool on_key_press(GdkEventKey* key); }; // class View class GenericView : public View { public: - GenericView(const Config& config, const std::string& file_path, const std::string& project_path): - View(config, file_path, project_path) {} + GenericView(const Source::Config& config, const std::string& file_path, const std::string& project_path): + View(config, file_path, project_path) { + signal_key_press_event().connect(sigc::mem_fun(*this, &Source::GenericView::on_key_press), false); + } + private: + bool on_key_press(GdkEventKey* key) { + return Source::View::on_key_press(key); + } }; class ClangView : public View { public: - ClangView(const Config& config, const std::string& file_path, const std::string& project_path); + ClangView(const Source::Config& config, const std::string& file_path, const std::string& project_path); ~ClangView(); // inits the syntax highligthing on file open void init_syntax_highlighting(const std::map @@ -123,10 +129,7 @@ namespace Source { bool is_saved = true; - std::unique_ptr view; - - private: - const Config& config; + std::unique_ptr view; }; // class Controller } // namespace Source #endif // JUCI_SOURCE_H_