Refactor a lot of tree view selection code into convenience

This commit is contained in:
Christian Dywan 2008-08-29 03:20:31 +02:00
parent 311e30bbfe
commit 6dc95a7a9c
3 changed files with 138 additions and 115 deletions

View file

@ -2146,28 +2146,26 @@ midori_panel_bookmarks_row_activated_cb (GtkTreeView* treeview,
} }
static void static void
midori_panel_bookmarks_cursor_or_row_changed_cb (GtkTreeView* treeview, midori_panel_bookmarks_cursor_or_row_changed_cb (GtkTreeView* tree_view,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview); GtkTreeModel* model;
if (selection) GtkTreeIter iter;
{ KatzeXbelItem* item;
GtkTreeModel* model; gboolean is_separator;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
gtk_tree_model_get (model, &iter, 0, &item, -1);
gboolean is_separator = katze_xbel_item_is_separator (item); if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
_action_set_sensitive (browser, "BookmarkEdit", !is_separator); {
_action_set_sensitive (browser, "BookmarkDelete", TRUE); gtk_tree_model_get (model, &iter, 0, &item, -1);
}
else is_separator = katze_xbel_item_is_separator (item);
{ _action_set_sensitive (browser, "BookmarkEdit", !is_separator);
_action_set_sensitive (browser, "BookmarkEdit", FALSE); _action_set_sensitive (browser, "BookmarkDelete", TRUE);
_action_set_sensitive (browser, "BookmarkDelete", FALSE); }
} else
{
_action_set_sensitive (browser, "BookmarkEdit", FALSE);
_action_set_sensitive (browser, "BookmarkDelete", FALSE);
} }
} }
@ -2192,29 +2190,25 @@ midori_panel_bookmarks_button_release_event_cb (GtkWidget* widget,
GdkEventButton* event, GdkEventButton* event,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
GtkTreeModel* model;
GtkTreeIter iter;
KatzeXbelItem* item;
if (event->button != 2 && event->button != 3) if (event->button != 2 && event->button != 3)
return FALSE; return FALSE;
GtkTreeSelection* selection = gtk_tree_view_get_selection ( if (sokoke_tree_view_get_selected_iter (GTK_TREE_VIEW (widget), &model, &iter))
GTK_TREE_VIEW (widget));
if (selection)
{ {
GtkTreeModel* model; gtk_tree_model_get (model, &iter, 0, &item, -1);
GtkTreeIter iter; if (event->button == 2 && katze_xbel_item_is_bookmark (item))
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{ {
KatzeXbelItem* item; const gchar* uri = katze_xbel_bookmark_get_href (item);
gtk_tree_model_get (model, &iter, 0, &item, -1); gint n = midori_browser_add_uri (browser, uri);
if (event->button == 2 && katze_xbel_item_is_bookmark (item)) midori_browser_set_current_page (browser, n);
{
const gchar* uri = katze_xbel_bookmark_get_href (item);
gint n = midori_browser_add_uri (browser, uri);
midori_browser_set_current_page (browser, n);
}
else
_midori_panel_bookmarks_popup (widget, event, item, browser);
return TRUE;
} }
else
_midori_panel_bookmarks_popup (widget, event, item, browser);
return TRUE;
} }
return FALSE; return FALSE;
} }
@ -2223,18 +2217,14 @@ static void
midori_panel_bookmarks_popup_menu_cb (GtkWidget* widget, midori_panel_bookmarks_popup_menu_cb (GtkWidget* widget,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
GtkTreeSelection* selection = gtk_tree_view_get_selection ( GtkTreeModel* model;
GTK_TREE_VIEW (widget)); GtkTreeIter iter;
if (selection) KatzeXbelItem* item;
if (sokoke_tree_view_get_selected_iter (GTK_TREE_VIEW (widget), &model, &iter))
{ {
GtkTreeModel* model; gtk_tree_model_get (model, &iter, 0, &item, -1);
GtkTreeIter iter; _midori_panel_bookmarks_popup (widget, NULL, item, browser);
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
gtk_tree_model_get (model, &iter, 0, &item, -1);
_midori_panel_bookmarks_popup (widget, NULL, item, browser);
}
} }
} }
@ -2565,20 +2555,18 @@ static void
_action_bookmark_open_activate (GtkAction* action, _action_bookmark_open_activate (GtkAction* action,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks); GtkTreeView* tree_view;
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview); GtkTreeModel* model;
if (selection) GtkTreeIter iter;
KatzeXbelItem* item;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{ {
GtkTreeModel* model; gtk_tree_model_get (model, &iter, 0, &item, -1);
GtkTreeIter iter; if (katze_xbel_item_is_bookmark (item))
if (gtk_tree_selection_get_selected (selection, &model, &iter)) g_object_set (midori_browser_get_current_web_view (browser),
{ "uri", katze_xbel_bookmark_get_href (item), NULL);
KatzeXbelItem* item;
gtk_tree_model_get (model, &iter, 0, &item, -1);
if (katze_xbel_item_is_bookmark (item))
g_object_set (midori_browser_get_current_web_view (browser),
"uri", katze_xbel_bookmark_get_href(item), NULL);
}
} }
} }
@ -2586,21 +2574,20 @@ static void
_action_bookmark_open_tab_activate (GtkAction* action, _action_bookmark_open_tab_activate (GtkAction* action,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks); GtkTreeView* tree_view;
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview); GtkTreeModel* model;
if (selection) GtkTreeIter iter;
KatzeXbelItem* item;
gint n;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{ {
GtkTreeModel* model; gtk_tree_model_get (model, &iter, 0, &item, -1);
GtkTreeIter iter; if (katze_xbel_item_is_bookmark (item))
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{ {
KatzeXbelItem* item; n = midori_browser_add_xbel_item (browser, item);
gtk_tree_model_get (model, &iter, 0, &item, -1); _midori_browser_set_current_page_smartly (browser, n);
if (katze_xbel_item_is_bookmark (item))
{
gint n = midori_browser_add_xbel_item (browser, item);
_midori_browser_set_current_page_smartly (browser, n);
}
} }
} }
} }
@ -2609,21 +2596,20 @@ static void
_action_bookmark_open_window_activate (GtkAction* action, _action_bookmark_open_window_activate (GtkAction* action,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks); GtkTreeView* tree_view;
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview); GtkTreeModel* model;
if (selection) GtkTreeIter iter;
KatzeXbelItem* item;
gint n;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{ {
GtkTreeModel* model; gtk_tree_model_get (model, &iter, 0, &item, -1);
GtkTreeIter iter; if (katze_xbel_item_is_bookmark (item))
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{ {
KatzeXbelItem* item; n = midori_browser_add_xbel_item (browser, item);
gtk_tree_model_get (model, &iter, 0, &item, -1); _midori_browser_set_current_page_smartly (browser, n);
if (katze_xbel_item_is_bookmark (item))
{
gint n = midori_browser_add_xbel_item (browser, item);
_midori_browser_set_current_page_smartly (browser, n);
}
} }
} }
} }
@ -2632,19 +2618,17 @@ static void
_action_bookmark_edit_activate (GtkAction* action, _action_bookmark_edit_activate (GtkAction* action,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks); GtkTreeView* tree_view;
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview); GtkTreeModel* model;
if (selection) GtkTreeIter iter;
KatzeXbelItem* item;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{ {
GtkTreeModel* model; gtk_tree_model_get (model, &iter, 0, &item, -1);
GtkTreeIter iter; if (!katze_xbel_item_is_separator (item))
if (gtk_tree_selection_get_selected (selection, &model, &iter)) midori_browser_edit_bookmark_dialog_new (browser, item);
{
KatzeXbelItem* item;
gtk_tree_model_get (model, &iter, 0, &item, -1);
if (!katze_xbel_item_is_separator (item))
midori_browser_edit_bookmark_dialog_new (browser, item);
}
} }
} }
@ -2677,22 +2661,21 @@ static void
_action_bookmark_delete_activate (GtkAction* action, _action_bookmark_delete_activate (GtkAction* action,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks); GtkTreeView* tree_view;
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview); GtkTreeModel* model;
if (selection) GtkTreeIter iter;
KatzeXbelItem* item;
KatzeXbelItem* parent;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{ {
GtkTreeModel* model; gtk_tree_model_get (model, &iter, 0, &item, -1);
GtkTreeIter iter; parent = katze_xbel_item_get_parent (item);
if (gtk_tree_selection_get_selected (selection, &model, &iter)) katze_xbel_folder_remove_item (parent, item);
{ /* This is a preliminary hack, until we fix it properly again */
KatzeXbelItem* item; gtk_tree_store_remove (GTK_TREE_STORE (model), &iter);
gtk_tree_model_get (model, &iter, 0, &item, -1); katze_xbel_item_unref (item);
KatzeXbelItem* parent = katze_xbel_item_get_parent (item);
katze_xbel_folder_remove_item (parent, item);
/* This is a preliminary hack, until we fix it properly again */
gtk_tree_store_remove (GTK_TREE_STORE (model), &iter);
katze_xbel_item_unref (item);
}
} }
} }

View file

@ -611,7 +611,7 @@ sokoke_object_get_boolean (gpointer object,
* @action: a #GtkAction * @action: a #GtkAction
* *
* Creates a menu item from an action, just like * Creates a menu item from an action, just like
* gtk_action_create_menu_item() but it won't * gtk_action_create_menu_item(), but it won't
* display an accelerator. * display an accelerator.
* *
* Return value: a new #GtkMenuItem * Return value: a new #GtkMenuItem
@ -621,8 +621,43 @@ sokoke_action_create_popup_menu_item (GtkAction* action)
{ {
GtkWidget* menuitem; GtkWidget* menuitem;
menuitem = gtk_image_menu_item_new_with_mnemonic (""); g_return_val_if_fail (GTK_IS_ACTION (action), NULL);
menuitem = gtk_image_menu_item_new ();
gtk_action_connect_proxy (action, menuitem); gtk_action_connect_proxy (action, menuitem);
return menuitem; return menuitem;
} }
/**
* sokoke_tree_view_get_selected_iter:
* @tree_view: a #GtkTreeView
* @model: a pointer to store the model, or %NULL
* @iter: a pointer to store the iter, or %NULL
*
* Determines whether there is a selection in the tree view
* and sets the @iter to the current selection.
*
* If there is a selection and @model is not %NULL, it is
* set to the model, mainly for convenience.
*
* Either @model or @iter or both can be %NULL in which case
* no value will be assigned in any case.
*
* Return value: %TRUE if there is a selection
**/
gboolean
sokoke_tree_view_get_selected_iter (GtkTreeView* tree_view,
GtkTreeModel** model,
GtkTreeIter* iter)
{
GtkTreeSelection* selection;
g_return_val_if_fail (GTK_IS_TREE_VIEW (tree_view), FALSE);
selection = gtk_tree_view_get_selection (tree_view);
if (selection)
if (gtk_tree_selection_get_selected (selection, model, iter))
return TRUE;
return FALSE;
}

View file

@ -131,4 +131,9 @@ sokoke_object_get_boolean (gpointer object,
GtkWidget* GtkWidget*
sokoke_action_create_popup_menu_item (GtkAction* action); sokoke_action_create_popup_menu_item (GtkAction* action);
gboolean
sokoke_tree_view_get_selected_iter (GtkTreeView* tree_view,
GtkTreeModel** model,
GtkTreeIter* iter);
#endif /* !__SOKOKE_H__ */ #endif /* !__SOKOKE_H__ */