Reputation: 22011
I am using this code:
class editbook
{
GtkWidget* _nbook;
std::vector<GtkWidget*> _srcset; //and so on...
...........................................................................................
void editbook::add_page()
{
GtkWidget* tmp = gtk_source_view_new();
_srcset.push_back(tmp);
gtk_notebook_append_page(GTK_NOTEBOOK(_nbook),tmp,gtk_label_new("untitled"));
}
...........................................................................................
void editbook::set_text(const std::string& text)
{
int index = gtk_notebook_get_current_page(GTK_NOTEBOOK(_nbook));
GtkTextBuffer* tbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(_srcset[index]));
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(tbuffer),text.c_str(),-1);
}
Compiles fine. But gives this weird runtime error:
Segementation Fault: return 139
I have traced down the problem to: gtk_text_view_get_buffer(GTK_TEXT_VIEW(_srcset[index]));
NOTE: I am using GtkSourceView instead of GtkTextView, but that may not be a problem because I am gettin the same error when I try GtkTextView.
NOTE: I am using Gtk 2x
NOTE: I am not sure whether to tag this question with C or C++. bec. Gtk+ is a C lib. But I am using C++. So I'll just tag both for now.
Upvotes: 0
Views: 202
Reputation: 11395
The problem in your code could be that the child widget added to GtkNotebook
through gtk_notebook_append_page
is not visible, try showing the child widget through gtk_widget_show
call. Something on these lines :
void editbook::add_page()
{
GtkWidget* tmp = gtk_source_view_new();
_srcset.push_back(tmp);
gtk_widget_show(tmp); //Show the child widget to make it visible
gtk_notebook_append_page(GTK_NOTEBOOK(_nbook),tmp,gtk_label_new("untitled"));
}
When you use gtk_notebook_get_current_page
if none of the child widget are visible then it returns -1
, which I think might be happening in your case & as index
is -1
when you use operator[]
which doesn't check for bounds the program crashes. I strongly suggest you use vector::at
instead of using operator[]
so that you get std::out_of_range
exception during run time to indicate the problem. You could use:
void editbook::set_text(const std::string& text)
{
int index = gtk_notebook_get_current_page(GTK_NOTEBOOK(_nbook));
GtkTextBuffer* tbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(_srcset.at(index)));
gtk_text_buffer_set_text(GTK_TEXT_BUFFER(tbuffer),text.c_str(),-1);
}
Hope this helps!
Upvotes: 1