How do you get the begin/end interators for viewable area of TextView in ScrollableWindow?



I was trying to implement a Search bar for Gtk::TextView inside of Gtk::ScrollableWindow.  Basically the code uses a text iterator to search forward in the code for a given text string.  Example:


void TextEditor::SearchClicked(int direction) {
  static Glib::ustring lastsearch {""};
  static Gtk::TextIter it = textbuff->begin();

  Glib::ustring searchtext = searchbar.entry.get_text();
  if (lastsearch != searchtext) {
    it = textbuff->begin();
  }
  lastsearch = searchtext;

  cout << "clicked! " << direction << "\n";

  Gtk::TextIter it_match_begin;
  Gtk::TextIter it_match_end;

  bool found = false;
  if (!direction) {
    found = it.forward_search(searchtext,
                    Gtk::TextSearchFlags::TEXT_SEARCH_CASE_INSENSITIVE,
                    it_match_begin,
                    it_match_end);
  }
  else {
    found = it.backward_search(searchtext,
                    Gtk::TextSearchFlags::TEXT_SEARCH_CASE_INSENSITIVE,
                    it_match_begin,
                    it_match_end);
  }
  if (found) {
    textbuff->select_range(it_match_begin, it_match_end);
   
    if (!direction) {
      it = it_match_end;    
    } else {
      it = it_match_begin;
    }
  }
}


Which works ok, except when the iterator goes beyond the current scroller positions in the ScrolledWindow,  then you need to manually scroll the window down to see the highlighted text.  Any ideas on how to fix this code so that it autoscrolls when it goes beyond the scrolledwindows visable area?




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]