Ankit Kulshrestha Ankit Kulshrestha - 10 days ago 4
C++ Question

Loading the full file in GtkTextView using Gtkmm

I want to load a file, then display it's contents. I'm using Gtkmm for this and I've been able to popup the filechooser dialog. However, upon choosing the file only the last statement in the file is displayed. Here's what I'm doing:

case(RESPONSE_OK):
{
std::string line,filename;
std::ifstream fs;

while(std::getline(fs, line)) {
display->get_buffer()->setText(line);
}
fs.close();
break;

}


Do I need to select the textbuffer limits and if so, how do I do that?

Answer

set_text() replaces the contents of the TextBuffer. Use any variant of the insert functions instead.

It may look something like (you may need to add newlines)

case(RESPONSE_OK):
{
  std::string line,filename;
  std::ifstream fs;

  auto buffer = display->get_buffer();
  buffer->set_text("");
  auto insert_at = buffer->begin();
  while(std::getline(fs, line)) {
     insert_at = buffer->insert(insert_at, line);
  }
  break;
}