diff --git a/src/selectiondialog.cc b/src/selectiondialog.cc index f4c82cc..1cb27e2 100644 --- a/src/selectiondialog.cc +++ b/src/selectiondialog.cc @@ -59,31 +59,6 @@ list_view_text(use_markup), start_mark(start_mark), show_search_entry(show_searc resize(); }); - list_view_text.signal_event_after().connect([this](GdkEvent* event){ - if(event->type==GDK_KEY_PRESS || event->type==GDK_BUTTON_PRESS) { - update_tooltips(); - } - }); - if(show_search_entry) { - search_entry.signal_event_after().connect([this](GdkEvent* event){ - if(event->type==GDK_KEY_PRESS || event->type==GDK_BUTTON_PRESS) { - update_tooltips(); - } - }); - } - - list_view_text.signal_cursor_changed().connect([this]() { - if(!shown) - return; - auto it=list_view_text.get_selection()->get_selected(); - if(it) { - std::string row; - it->get_value(0, row); - if(on_changed) - on_changed(row); - } - }); - scrolled_window.add(list_view_text); if(!show_search_entry) window->add(scrolled_window); @@ -99,10 +74,8 @@ SelectionDialogBase::~SelectionDialogBase() { text_view.get_buffer()->delete_mark(start_mark); } -void SelectionDialogBase::add_row(const std::string& row, const std::string& tooltip) { +void SelectionDialogBase::add_row(const std::string& row) { list_view_text.append(row); - if(tooltip.size()>0) - tooltip_texts[row]=tooltip; } void SelectionDialogBase::show() { @@ -116,45 +89,11 @@ void SelectionDialogBase::hide() { return; shown=false; window->hide(); - if(tooltips) - tooltips->hide(); if(on_hide) on_hide(); list_view_text.clear(); } -void SelectionDialogBase::update_tooltips() { - auto it=list_view_text.get_selection()->get_selected(); - if(it) { - std::string row; - it->get_value(0, row); - if(row!=last_row || last_row.size()==0) { - if(tooltips) - tooltips->hide(); - auto it=tooltip_texts.find(row); - if(it!=tooltip_texts.end()) { - auto tooltip_text=it->second; - if(tooltip_text.size()>0) { - tooltips=std::unique_ptr(new Tooltips()); - auto get_tooltip_buffer=[this, tooltip_text]() { - auto tooltip_buffer=Gtk::TextBuffer::create(text_view.get_buffer()->get_tag_table()); - tooltip_buffer->insert_with_tag(tooltip_buffer->get_insert()->get_iter(), tooltip_text, "def:note"); - return tooltip_buffer; - }; - tooltips->emplace_back(get_tooltip_buffer, text_view, text_view.get_buffer()->create_mark(start_mark->get_iter()), text_view.get_buffer()->create_mark(text_view.get_buffer()->get_insert()->get_iter())); - tooltips->show(true); - } - } - } - last_row=row; - } - else { - last_row=""; - if(tooltips) - tooltips->hide(); - } -} - void SelectionDialogBase::move() { Gdk::Rectangle rectangle; text_view.get_iter_location(start_mark->get_iter(), rectangle); @@ -190,7 +129,21 @@ void SelectionDialogBase::resize() { } } -SelectionDialog::SelectionDialog(Gtk::TextView& text_view, Glib::RefPtr start_mark, bool show_search_entry, bool use_markup) : SelectionDialogBase(text_view, start_mark, show_search_entry, use_markup) {} +SelectionDialog::SelectionDialog(Gtk::TextView& text_view, Glib::RefPtr start_mark, bool show_search_entry, bool use_markup) : SelectionDialogBase(text_view, start_mark, show_search_entry, use_markup) { + list_view_text.signal_cursor_changed().connect([this]() { + if(!shown) + return; + auto it=list_view_text.get_selection()->get_selected(); + std::string row; + if(it) + it->get_value(0, row); + if(last_row==row) + return; + if(on_changed) + on_changed(row); + last_row=row; + }); +} void SelectionDialog::show() { SelectionDialogBase::show(); @@ -284,10 +237,8 @@ void SelectionDialog::show() { return true; }); - if(list_view_text.get_model()->children().size()>0) { + if(list_view_text.get_model()->children().size()>0) list_view_text.set_cursor(list_view_text.get_model()->get_path(list_view_text.get_model()->children().begin())); - update_tooltips(); - } } bool SelectionDialog::on_key_press(GdkEventKey* key) { @@ -331,7 +282,26 @@ bool SelectionDialog::on_key_press(GdkEventKey* key) { return false; } -CompletionDialog::CompletionDialog(Gtk::TextView& text_view, Glib::RefPtr start_mark) : SelectionDialogBase(text_view, start_mark, false, false) {} +CompletionDialog::CompletionDialog(Gtk::TextView& text_view, Glib::RefPtr start_mark) : SelectionDialogBase(text_view, start_mark, false, false) { + list_view_text.signal_event_after().connect([this](GdkEvent* event){ + if(event->type==GDK_KEY_PRESS || event->type==GDK_BUTTON_PRESS) { + update_tooltips(); + } + }); + if(show_search_entry) { + search_entry.signal_event_after().connect([this](GdkEvent* event){ + if(event->type==GDK_KEY_PRESS || event->type==GDK_BUTTON_PRESS) { + update_tooltips(); + } + }); + } +} + +void CompletionDialog::add_row(const std::string& row, const std::string& tooltip) { + SelectionDialogBase::add_row(row); + if(tooltip.size()>0) + tooltip_texts[row]=tooltip; +} void CompletionDialog::show() { SelectionDialogBase::show(); @@ -384,6 +354,14 @@ void CompletionDialog::show() { } } +void CompletionDialog::hide() { + if(!shown) + return; + SelectionDialogBase::hide(); + if(tooltips) + tooltips->hide(); +} + void CompletionDialog::select(bool hide_window) { row_in_entry=true; @@ -480,3 +458,35 @@ bool CompletionDialog::on_key_press(GdkEventKey* key) { return true; return false; } + +void CompletionDialog::update_tooltips() { + auto it=list_view_text.get_selection()->get_selected(); + if(it) { + std::string row; + it->get_value(0, row); + if(row!=last_row || last_row.size()==0) { + if(tooltips) + tooltips->hide(); + auto it=tooltip_texts.find(row); + if(it!=tooltip_texts.end()) { + auto tooltip_text=it->second; + if(tooltip_text.size()>0) { + tooltips=std::unique_ptr(new Tooltips()); + auto get_tooltip_buffer=[this, tooltip_text]() { + auto tooltip_buffer=Gtk::TextBuffer::create(text_view.get_buffer()->get_tag_table()); + tooltip_buffer->insert_with_tag(tooltip_buffer->get_insert()->get_iter(), tooltip_text, "def:note"); + return tooltip_buffer; + }; + tooltips->emplace_back(get_tooltip_buffer, text_view, text_view.get_buffer()->create_mark(start_mark->get_iter()), text_view.get_buffer()->create_mark(text_view.get_buffer()->get_insert()->get_iter())); + tooltips->show(true); + } + } + } + last_row=row; + } + else { + last_row=""; + if(tooltips) + tooltips->hide(); + } +} diff --git a/src/selectiondialog.h b/src/selectiondialog.h index 36f2747..0c9b1e5 100644 --- a/src/selectiondialog.h +++ b/src/selectiondialog.h @@ -29,20 +29,18 @@ class SelectionDialogBase { public: SelectionDialogBase(Gtk::TextView& text_view, Glib::RefPtr start_mark, bool show_search_entry, bool use_markup); ~SelectionDialogBase(); - virtual void add_row(const std::string& row, const std::string& tooltip=""); - virtual void show(); - virtual void hide(); - virtual void move(); + void add_row(const std::string& row); + void show(); + void hide(); + void move(); std::function on_hide; - std::function on_changed; std::function on_select; Glib::RefPtr start_mark; bool shown=false; protected: - virtual void resize(); - virtual void update_tooltips(); + void resize(); Gtk::TextView& text_view; std::unique_ptr window; @@ -50,22 +48,25 @@ protected: ListViewText list_view_text; Gtk::Entry search_entry; bool show_search_entry; - std::unique_ptr tooltips; - std::unordered_map tooltip_texts; - std::string last_row; }; class SelectionDialog : public SelectionDialogBase { public: SelectionDialog(Gtk::TextView& text_view, Glib::RefPtr start_mark, bool show_search_entry=true, bool use_markup=false); bool on_key_press(GdkEventKey* key); - void show() override; + void show(); + + std::function on_changed; +private: + std::string last_row; }; class CompletionDialog : public SelectionDialogBase { public: CompletionDialog(Gtk::TextView& text_view, Glib::RefPtr start_mark); - void show() override; + void add_row(const std::string& row, const std::string& tooltip); + void show(); + void hide(); bool on_key_release(GdkEventKey* key); bool on_key_press(GdkEventKey* key); @@ -74,6 +75,11 @@ private: int show_offset; bool row_in_entry=false; + + void update_tooltips(); + std::unique_ptr tooltips; + std::unordered_map tooltip_texts; + std::string last_row; }; #endif // JUCI_SELECTIONDIALOG_H_ \ No newline at end of file diff --git a/src/window.cc b/src/window.cc index 0e1ea3a..15c5d40 100644 --- a/src/window.cc +++ b/src/window.cc @@ -1012,6 +1012,10 @@ void Window::set_menu_actions() { }; view->selection_dialog->on_changed=[this, rows, iter](const std::string &selected) { + if(selected.empty()) { + debug_variable_tooltips->hide(); + return; + } if(notebook.get_current_page()!=-1) { auto view=notebook.get_current_view(); debug_variable_tooltips=std::unique_ptr(new Tooltips());