Fix toggle_index: WIDGET_REALIZED_FOR_EVENT error

Clicking on an arrow in the index tree view caused an
"changed" event that was liable to crash zathura. This
commit cleans up the sc_toggle_index command and fixes
the described error by pre-selecting the first item in
the tree.
This commit is contained in:
Moritz Lipp 2010-04-11 20:56:16 +02:00
parent a2309feacf
commit 3999458f11

View file

@ -543,40 +543,29 @@ add_marker(int id)
void void
build_index(GtkTreeModel* model, GtkTreeIter* parent, PopplerIndexIter* index_iter) build_index(GtkTreeModel* model, GtkTreeIter* parent, PopplerIndexIter* index_iter)
{ {
g_static_mutex_lock(&(Zathura.Lock.pdflib_lock));
do do
{ {
GtkTreeIter tree_iter; GtkTreeIter tree_iter;
PopplerIndexIter *child; PopplerIndexIter *child;
PopplerAction *action; PopplerAction *action;
gboolean expand;
gchar *markup; gchar *markup;
action = poppler_index_iter_get_action(index_iter); action = poppler_index_iter_get_action(index_iter);
expand = poppler_index_iter_is_open(index_iter);
if(!action) if(!action)
continue; continue;
markup = g_markup_escape_text (action->any.title, -1); markup = g_markup_escape_text (action->any.title, -1);
gtk_tree_store_append(GTK_TREE_STORE(model), &tree_iter, parent); gtk_tree_store_append(GTK_TREE_STORE(model), &tree_iter, parent);
gtk_tree_store_set(GTK_TREE_STORE(model), &tree_iter, 0, markup, gtk_tree_store_set(GTK_TREE_STORE(model), &tree_iter, 0, markup, 1, action, -1);
1, action, -1);
g_object_weak_ref(G_OBJECT(model), (GWeakNotify) poppler_action_free, action); g_object_weak_ref(G_OBJECT(model), (GWeakNotify) poppler_action_free, action);
g_free(markup); g_free(markup);
child = poppler_index_iter_get_child(index_iter); child = poppler_index_iter_get_child(index_iter);
g_static_mutex_unlock(&(Zathura.Lock.pdflib_lock));
if(child) if(child)
build_index(model, &tree_iter, child); build_index(model, &tree_iter, child);
g_static_mutex_lock(&(Zathura.Lock.pdflib_lock));
poppler_index_iter_free(child); poppler_index_iter_free(child);
} } while(poppler_index_iter_next(index_iter));
while(poppler_index_iter_next(index_iter));
g_static_mutex_unlock(&(Zathura.Lock.pdflib_lock));
} }
void void
@ -1439,43 +1428,25 @@ sc_toggle_index(Argument* argument)
if(!Zathura.PDF.document) if(!Zathura.PDF.document)
return; return;
if(!Zathura.UI.index)
{
GtkWidget *treeview; GtkWidget *treeview;
GtkTreeModel *model; GtkTreeModel *model;
PopplerIndexIter *index_iter;
GtkCellRenderer *renderer; GtkCellRenderer *renderer;
GtkTreeSelection *selection; GtkTreeSelection *selection;
PopplerIndexIter *iter;
if(!Zathura.UI.index)
{
Zathura.UI.index = gtk_scrolled_window_new (NULL, NULL); Zathura.UI.index = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Zathura.UI.index), gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(Zathura.UI.index),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
g_static_mutex_lock(&(Zathura.Lock.pdflib_lock)); if((iter = poppler_index_iter_new(Zathura.PDF.document)))
index_iter = poppler_index_iter_new(Zathura.PDF.document);
g_static_mutex_unlock(&(Zathura.Lock.pdflib_lock));
if(index_iter)
{ {
model = GTK_TREE_MODEL(gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_POINTER)); model = GTK_TREE_MODEL(gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_POINTER));
build_index(model, NULL, index_iter);
g_static_mutex_lock(&(Zathura.Lock.pdflib_lock)); g_static_mutex_lock(&(Zathura.Lock.pdflib_lock));
poppler_index_iter_free(index_iter); build_index(model, NULL, iter);
g_static_mutex_unlock(&(Zathura.Lock.pdflib_lock)); g_static_mutex_unlock(&(Zathura.Lock.pdflib_lock));
treeview = gtk_tree_view_new_with_model(model); poppler_index_iter_free(iter);
g_object_unref(model);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), 0, "Title", renderer,
"markup", 0, NULL);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
g_signal_connect(G_OBJECT(selection), "changed", G_CALLBACK(cb_index_selection_changed),
NULL);
gtk_container_add(GTK_CONTAINER(Zathura.UI.index), treeview);
gtk_widget_show(GTK_WIDGET(treeview));
} }
else else
{ {
@ -1483,19 +1454,30 @@ sc_toggle_index(Argument* argument)
Zathura.UI.index = NULL; Zathura.UI.index = NULL;
return; return;
} }
}
static gboolean show = TRUE; treeview = gtk_tree_view_new_with_model (model);
g_object_unref(model);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW (treeview), 0, "Title",
renderer, "markup", 0, NULL);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(treeview), FALSE);
g_object_set(G_OBJECT(renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
g_object_set(G_OBJECT(gtk_tree_view_get_column(GTK_TREE_VIEW(treeview), 0)), "expand", TRUE, NULL);
if(show) selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
{ gtk_tree_selection_select_path(selection, gtk_tree_path_new_first());
g_signal_connect(G_OBJECT(selection), "changed", G_CALLBACK(cb_index_selection_changed), NULL);
gtk_container_add (GTK_CONTAINER (Zathura.UI.index), treeview);
gtk_widget_show (treeview);
gtk_widget_show(Zathura.UI.index); gtk_widget_show(Zathura.UI.index);
}
static gboolean show = FALSE;
if(!show)
switch_view(Zathura.UI.index); switch_view(Zathura.UI.index);
}
else else
{
switch_view(Zathura.UI.drawing_area); switch_view(Zathura.UI.drawing_area);
}
show = !show; show = !show;
} }
@ -2855,6 +2837,9 @@ cb_index_selection_changed(GtkTreeSelection* treeselection, GtkWidget* action_vi
PopplerDest* destination; PopplerDest* destination;
gtk_tree_model_get(model, &iter, 1, &action, -1); gtk_tree_model_get(model, &iter, 1, &action, -1);
if(!action)
return TRUE;
if(action->type == POPPLER_ACTION_GOTO_DEST) if(action->type == POPPLER_ACTION_GOTO_DEST)
{ {
destination = action->goto_dest.dest; destination = action->goto_dest.dest;
@ -2863,7 +2848,7 @@ cb_index_selection_changed(GtkTreeSelection* treeselection, GtkWidget* action_vi
if(page_number >= 0 && page_number <= Zathura.PDF.number_of_pages) if(page_number >= 0 && page_number <= Zathura.PDF.number_of_pages)
{ {
set_page(page_number - 1); set_page(page_number - 1);
update_status(); /*update_status();*/
} }
} }
} }