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,20 +2146,19 @@ midori_panel_bookmarks_row_activated_cb (GtkTreeView* treeview,
}
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)
{
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview);
if (selection)
{
GtkTreeModel* model;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
gboolean is_separator;
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{
gtk_tree_model_get (model, &iter, 0, &item, -1);
gboolean is_separator = katze_xbel_item_is_separator (item);
is_separator = katze_xbel_item_is_separator (item);
_action_set_sensitive (browser, "BookmarkEdit", !is_separator);
_action_set_sensitive (browser, "BookmarkDelete", TRUE);
}
@ -2168,7 +2167,6 @@ midori_panel_bookmarks_cursor_or_row_changed_cb (GtkTreeView* treeview,
_action_set_sensitive (browser, "BookmarkEdit", FALSE);
_action_set_sensitive (browser, "BookmarkDelete", FALSE);
}
}
}
static void
@ -2192,18 +2190,15 @@ midori_panel_bookmarks_button_release_event_cb (GtkWidget* widget,
GdkEventButton* event,
MidoriBrowser* browser)
{
GtkTreeModel* model;
GtkTreeIter iter;
KatzeXbelItem* item;
if (event->button != 2 && event->button != 3)
return FALSE;
GtkTreeSelection* selection = gtk_tree_view_get_selection (
GTK_TREE_VIEW (widget));
if (selection)
if (sokoke_tree_view_get_selected_iter (GTK_TREE_VIEW (widget), &model, &iter))
{
GtkTreeModel* model;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
gtk_tree_model_get (model, &iter, 0, &item, -1);
if (event->button == 2 && katze_xbel_item_is_bookmark (item))
{
@ -2215,7 +2210,6 @@ midori_panel_bookmarks_button_release_event_cb (GtkWidget* widget,
_midori_panel_bookmarks_popup (widget, event, item, browser);
return TRUE;
}
}
return FALSE;
}
@ -2223,19 +2217,15 @@ static void
midori_panel_bookmarks_popup_menu_cb (GtkWidget* widget,
MidoriBrowser* browser)
{
GtkTreeSelection* selection = gtk_tree_view_get_selection (
GTK_TREE_VIEW (widget));
if (selection)
{
GtkTreeModel* model;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
if (sokoke_tree_view_get_selected_iter (GTK_TREE_VIEW (widget), &model, &iter))
{
gtk_tree_model_get (model, &iter, 0, &item, -1);
_midori_panel_bookmarks_popup (widget, NULL, item, browser);
}
}
}
static void
@ -2565,20 +2555,18 @@ static void
_action_bookmark_open_activate (GtkAction* action,
MidoriBrowser* browser)
{
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks);
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview);
if (selection)
{
GtkTreeView* tree_view;
GtkTreeModel* model;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{
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);
}
"uri", katze_xbel_bookmark_get_href (item), NULL);
}
}
@ -2586,66 +2574,62 @@ static void
_action_bookmark_open_tab_activate (GtkAction* action,
MidoriBrowser* browser)
{
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks);
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview);
if (selection)
{
GtkTreeView* tree_view;
GtkTreeModel* model;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
gint n;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{
gtk_tree_model_get (model, &iter, 0, &item, -1);
if (katze_xbel_item_is_bookmark (item))
{
gint n = midori_browser_add_xbel_item (browser, item);
n = midori_browser_add_xbel_item (browser, item);
_midori_browser_set_current_page_smartly (browser, n);
}
}
}
}
static void
_action_bookmark_open_window_activate (GtkAction* action,
MidoriBrowser* browser)
{
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks);
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview);
if (selection)
{
GtkTreeView* tree_view;
GtkTreeModel* model;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
gint n;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{
gtk_tree_model_get (model, &iter, 0, &item, -1);
if (katze_xbel_item_is_bookmark (item))
{
gint n = midori_browser_add_xbel_item (browser, item);
n = midori_browser_add_xbel_item (browser, item);
_midori_browser_set_current_page_smartly (browser, n);
}
}
}
}
static void
_action_bookmark_edit_activate (GtkAction* action,
MidoriBrowser* browser)
{
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks);
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview);
if (selection)
{
GtkTreeView* tree_view;
GtkTreeModel* model;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{
gtk_tree_model_get (model, &iter, 0, &item, -1);
if (!katze_xbel_item_is_separator (item))
midori_browser_edit_bookmark_dialog_new (browser, item);
}
}
}
static void
@ -2677,23 +2661,22 @@ static void
_action_bookmark_delete_activate (GtkAction* action,
MidoriBrowser* browser)
{
GtkTreeView* treeview = GTK_TREE_VIEW (browser->panel_bookmarks);
GtkTreeSelection* selection = gtk_tree_view_get_selection (treeview);
if (selection)
{
GtkTreeView* tree_view;
GtkTreeModel* model;
GtkTreeIter iter;
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
KatzeXbelItem* item;
KatzeXbelItem* parent;
tree_view = GTK_TREE_VIEW (browser->panel_bookmarks);
if (sokoke_tree_view_get_selected_iter (tree_view, &model, &iter))
{
gtk_tree_model_get (model, &iter, 0, &item, -1);
KatzeXbelItem* parent = katze_xbel_item_get_parent (item);
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);
}
}
}
static const GtkActionEntry entries[] = {

View file

@ -611,7 +611,7 @@ sokoke_object_get_boolean (gpointer object,
* @action: a #GtkAction
*
* 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.
*
* Return value: a new #GtkMenuItem
@ -621,8 +621,43 @@ sokoke_action_create_popup_menu_item (GtkAction* action)
{
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);
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*
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__ */