diff options
author | Marco Pesenti Gritti <marco@it.gnome.org> | 2003-04-17 20:59:52 +0800 |
---|---|---|
committer | Marco Pesenti Gritti <mpeseng@src.gnome.org> | 2003-04-17 20:59:52 +0800 |
commit | 609b92cfa632385313d1d2421b516bdb41fdbf00 (patch) | |
tree | 09fc963d624f5723fc75551eb125b3d7d825f795 /lib | |
parent | 9e94d5208ee7d936c6d3595435b9364fec5850dd (diff) | |
download | gsoc2013-epiphany-609b92cfa632385313d1d2421b516bdb41fdbf00.tar gsoc2013-epiphany-609b92cfa632385313d1d2421b516bdb41fdbf00.tar.gz gsoc2013-epiphany-609b92cfa632385313d1d2421b516bdb41fdbf00.tar.bz2 gsoc2013-epiphany-609b92cfa632385313d1d2421b516bdb41fdbf00.tar.lz gsoc2013-epiphany-609b92cfa632385313d1d2421b516bdb41fdbf00.tar.xz gsoc2013-epiphany-609b92cfa632385313d1d2421b516bdb41fdbf00.tar.zst gsoc2013-epiphany-609b92cfa632385313d1d2421b516bdb41fdbf00.zip |
New abstracted implementations. Nicer and will be useful for history
2003-04-17 Marco Pesenti Gritti <marco@it.gnome.org>
* lib/widgets/Makefile.am:
* lib/widgets/ephy-node-view.c:
* lib/widgets/ephy-node-view.h:
* lib/widgets/ephy-tree-model-node.c:
* lib/widgets/ephy-tree-model-node.h:
New abstracted implementations.
Nicer and will be useful for
history dialog interface rehash to make
it more consistent with bookmarks (this
was the hard part of the work).
* src/bookmarks/Makefile.am:
* src/bookmarks/ephy-bookmarks-editor.c:
Use them.
* src/history-dialog.c:
Temp hack to keep the thing building until
I actually rewrite history dialog.
2
Diffstat (limited to 'lib')
-rw-r--r-- | lib/widgets/Makefile.am | 4 | ||||
-rw-r--r-- | lib/widgets/ephy-node-view.c | 1099 | ||||
-rw-r--r-- | lib/widgets/ephy-node-view.h | 116 | ||||
-rw-r--r-- | lib/widgets/ephy-tree-model-node.c | 727 | ||||
-rw-r--r-- | lib/widgets/ephy-tree-model-node.h | 85 |
5 files changed, 2031 insertions, 0 deletions
diff --git a/lib/widgets/Makefile.am b/lib/widgets/Makefile.am index 809041a49..d2d27ca03 100644 --- a/lib/widgets/Makefile.am +++ b/lib/widgets/Makefile.am @@ -21,10 +21,14 @@ libephywidgets_la_SOURCES = \ ephy-ellipsizing-label.h \ ephy-location-entry.c \ ephy-location-entry.h \ + ephy-node-view.c \ + ephy-node-view.h \ ephy-notebook.c \ ephy-notebook.h \ ephy-spinner.c \ ephy-spinner.h \ + ephy-tree-model-node.c \ + ephy-tree-model-node.h \ ephy-tree-model-sort.c \ ephy-tree-model-sort.h diff --git a/lib/widgets/ephy-node-view.c b/lib/widgets/ephy-node-view.c new file mode 100644 index 000000000..1d8eba5cd --- /dev/null +++ b/lib/widgets/ephy-node-view.c @@ -0,0 +1,1099 @@ +/* + * Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + */ + +#include <gtk/gtktreeselection.h> +#include <gtk/gtktreeviewcolumn.h> +#include <gtk/gtkcellrenderertext.h> +#include <gtk/gtkcellrendererpixbuf.h> +#include <gtk/gtkwindow.h> +#include <gdk/gdkkeysyms.h> +#include <libgnome/gnome-i18n.h> + +#include "eggtreemodelfilter.h" +#include "ephy-node-view.h" +#include "ephy-tree-model-sort.h" +#include "eggtreemultidnd.h" +#include "ephy-dnd.h" +#include "ephy-marshal.h" +#include "string.h" + +static void ephy_node_view_class_init (EphyNodeViewClass *klass); +static void ephy_node_view_init (EphyNodeView *view); +static void ephy_node_view_finalize (GObject *object); +static void ephy_node_view_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void ephy_node_view_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); + +struct EphyNodeViewPrivate +{ + EphyNode *root; + + EphyTreeModelNode *nodemodel; + GtkTreeModel *filtermodel; + GtkTreeModel *sortmodel; + GtkCellRenderer *editable_renderer; + GtkTreeViewColumn *editable_column; + EphyTreeModelNodeColumn editable_node_column; + + EphyNodeFilter *filter; + + EphyNode *selected_node; + + GtkTargetList *drag_targets; + + int default_sort_column_id; + int priority_prop_id; + int priority_column_id; + + gboolean editing; + int editable_property; +}; + +enum +{ + NODE_ACTIVATED, + NODE_SELECTED, + NODE_DROPPED, + SHOW_POPUP, + LAST_SIGNAL +}; + +enum +{ + PROP_0, + PROP_ROOT, + PROP_FILTER +}; + +static EphyNodeView *target_view; + +static GObjectClass *parent_class = NULL; + +static guint ephy_node_view_signals[LAST_SIGNAL] = { 0 }; + +GType +ephy_node_view_get_type (void) +{ + static GType ephy_node_view_type = 0; + + if (ephy_node_view_type == 0) + { + static const GTypeInfo our_info = + { + sizeof (EphyNodeViewClass), + NULL, + NULL, + (GClassInitFunc) ephy_node_view_class_init, + NULL, + NULL, + sizeof (EphyNodeView), + 0, + (GInstanceInitFunc) ephy_node_view_init + }; + + ephy_node_view_type = g_type_register_static (GTK_TYPE_TREE_VIEW, + "EphyNodeView", + &our_info, 0); + } + + return ephy_node_view_type; +} + +static void +ephy_node_view_class_init (EphyNodeViewClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + parent_class = g_type_class_peek_parent (klass); + + object_class->finalize = ephy_node_view_finalize; + + object_class->set_property = ephy_node_view_set_property; + object_class->get_property = ephy_node_view_get_property; + + g_object_class_install_property (object_class, + PROP_ROOT, + g_param_spec_object ("root", + "Root node", + "Root node", + EPHY_TYPE_NODE, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property (object_class, + PROP_FILTER, + g_param_spec_object ("filter", + "Filter object", + "Filter object", + EPHY_TYPE_NODE_FILTER, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + + ephy_node_view_signals[NODE_ACTIVATED] = + g_signal_new ("node_activated", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EphyNodeViewClass, node_activated), + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, + 1, + EPHY_TYPE_NODE); + ephy_node_view_signals[NODE_SELECTED] = + g_signal_new ("node_selected", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EphyNodeViewClass, node_selected), + NULL, NULL, + g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, + 1, + EPHY_TYPE_NODE); + ephy_node_view_signals[NODE_DROPPED] = + g_signal_new ("node_dropped", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EphyNodeViewClass, node_dropped), + NULL, NULL, + ephy_marshal_VOID__OBJECT_POINTER, + G_TYPE_NONE, + 2, + EPHY_TYPE_NODE, + G_TYPE_POINTER); + ephy_node_view_signals[SHOW_POPUP] = + g_signal_new ("show_popup", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EphyNodeViewClass, show_popup), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, + 0); +} + +static void +ephy_node_view_finalize (GObject *object) +{ + EphyNodeView *view; + + g_return_if_fail (object != NULL); + g_return_if_fail (EPHY_IS_NODE_VIEW (object)); + + view = EPHY_NODE_VIEW (object); + + g_return_if_fail (view->priv != NULL); + + g_object_unref (G_OBJECT (view->priv->sortmodel)); + g_object_unref (G_OBJECT (view->priv->filtermodel)); + g_object_unref (G_OBJECT (view->priv->nodemodel)); + + g_free (view->priv); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static EphyNode * +get_node_from_path (EphyNodeView *view, GtkTreePath *path) +{ + EphyNode *node; + GtkTreeIter iter, iter2; + + gtk_tree_model_get_iter (view->priv->sortmodel, &iter, path); + gtk_tree_model_sort_convert_iter_to_child_iter + (GTK_TREE_MODEL_SORT (view->priv->sortmodel), &iter2, &iter); + egg_tree_model_filter_convert_iter_to_child_iter + (EGG_TREE_MODEL_FILTER (view->priv->filtermodel), &iter, &iter2); + node = ephy_tree_model_node_node_from_iter (view->priv->nodemodel, &iter); + + return node; +} + +static gboolean +drag_motion_cb (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + guint time, + EphyNodeView *view) +{ + EphyNode *node; + GtkTreePath *path = NULL; + GtkTreeViewDropPosition pos; + gboolean res; + EphyNodeViewPriority priority; + + g_signal_stop_emission_by_name (widget, "drag_motion"); + + res = gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), + x, y, &path, &pos); + if (!res) return TRUE; + + node = get_node_from_path (view, path); + + priority = ephy_node_get_property_int (node, view->priv->priority_prop_id); + + if (priority != EPHY_NODE_VIEW_ALL_PRIORITY && + priority != EPHY_NODE_VIEW_SPECIAL_PRIORITY) + + { + gtk_tree_view_set_drag_dest_row (GTK_TREE_VIEW (widget), path, + GTK_TREE_VIEW_DROP_INTO_OR_AFTER); + gdk_drag_status (context, context->suggested_action, time); + } + else + { + gdk_drag_status (context, 0, time); + } + + return TRUE; +} + +static gboolean +drag_drop_cb (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + guint time, + EphyNodeView *view) +{ + GdkAtom target; + + g_signal_stop_emission_by_name (widget, "drag_drop"); + + target = gtk_drag_dest_find_target (widget, context, + view->priv->drag_targets); + + if (target != GDK_NONE) + { + gtk_drag_get_data (widget, context, target, time); + } + + return TRUE; +} + +static gboolean +drag_data_received_cb (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + GtkSelectionData *selection_data, + guint info, + guint time, + EphyNodeView *view) +{ + GtkTreePath *path = NULL; + GtkTreeViewDropPosition pos; + + g_signal_stop_emission_by_name (widget, "drag_data_received"); + + if (gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget), + x, y, &path, &pos)) + { + EphyNode *node; + GList *src_nodes; + + node = get_node_from_path (view, path); + + src_nodes = ephy_dnd_node_list_extract_nodes + (selection_data->data); + + g_signal_emit (G_OBJECT (view), + ephy_node_view_signals[NODE_DROPPED], 0, + node, src_nodes); + + g_list_free (src_nodes); + + } + + return TRUE; +} + +void +ephy_node_view_enable_drag_dest (EphyNodeView *view, + GtkTargetEntry *types, + int n_types) +{ + GtkWidget *treeview; + + g_return_if_fail (view != NULL); + + treeview = GTK_WIDGET (view); + + gtk_tree_view_enable_model_drag_dest (GTK_TREE_VIEW (treeview), + types, n_types, + GDK_ACTION_COPY); + view->priv->drag_targets = gtk_target_list_new (types, n_types); + + g_signal_connect (treeview, "drag_data_received", + G_CALLBACK (drag_data_received_cb), view); + g_signal_connect (treeview, "drag_drop", + G_CALLBACK (drag_drop_cb), view); + g_signal_connect (treeview, "drag_motion", + G_CALLBACK (drag_motion_cb), view); +} + +static void +filter_changed_cb (EphyNodeFilter *filter, + EphyNodeView *view) +{ + GtkWidget *window; + + g_return_if_fail (EPHY_IS_NODE_VIEW (view)); + + window = gtk_widget_get_toplevel (GTK_WIDGET (view)); + + if (window != NULL && window->window != NULL) + { + /* nice busy cursor */ + GdkCursor *cursor; + + cursor = gdk_cursor_new (GDK_WATCH); + gdk_window_set_cursor (window->window, cursor); + gdk_cursor_unref (cursor); + + gdk_flush (); + + gdk_window_set_cursor (window->window, NULL); + + /* no flush: this will cause the cursor to be reset + * only when the UI is free again */ + } +} + +static void +ephy_node_view_selection_changed_cb (GtkTreeSelection *selection, + EphyNodeView *view) +{ + GList *list; + EphyNode *node = NULL; + + view->priv->selected_node = NULL; + + list = ephy_node_view_get_selection (view); + if (list) + { + node = EPHY_NODE (list->data); + } + g_list_free (list); + + g_signal_emit (G_OBJECT (view), ephy_node_view_signals[NODE_SELECTED], 0, node); +} + +static void +ephy_node_view_row_activated_cb (GtkTreeView *treeview, + GtkTreePath *path, + GtkTreeViewColumn *column, + EphyNodeView *view) +{ + GtkTreeIter iter, iter2; + EphyNode *node; + + gtk_tree_model_get_iter (view->priv->sortmodel, &iter, path); + gtk_tree_model_sort_convert_iter_to_child_iter + (GTK_TREE_MODEL_SORT (view->priv->sortmodel), &iter2, &iter); + egg_tree_model_filter_convert_iter_to_child_iter + (EGG_TREE_MODEL_FILTER (view->priv->filtermodel), &iter, &iter2); + + node = ephy_tree_model_node_node_from_iter (view->priv->nodemodel, &iter); + + g_signal_emit (G_OBJECT (view), ephy_node_view_signals[NODE_ACTIVATED], 0, node); +} + +static gboolean +ephy_node_view_key_press_cb (GtkTreeView *treeview, + GdkEventKey *event, + EphyNodeView *view) +{ + if ((event->state & GDK_SHIFT_MASK) && + (event->keyval == GDK_F10)) + { + g_signal_emit (G_OBJECT (view), ephy_node_view_signals[SHOW_POPUP], 0); + + return TRUE; + } + + return FALSE; +} + +static gboolean +ephy_node_view_button_press_cb (GtkTreeView *treeview, + GdkEventButton *event, + EphyNodeView *view) +{ + GtkTreePath *path; + GtkTreeSelection *selection; + gboolean result = FALSE; + + if (event->button == 3) + { + if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (treeview), + event->x, + event->y, + &path, + NULL, NULL, NULL)) + { + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)); + if (gtk_tree_selection_path_is_selected (selection, path)) + { + /* We handle the event (so the view won't be + * changed by the user click) because the user + * clicked on an already selected element */ + result = TRUE; + } + else + { + view->priv->selected_node = + get_node_from_path (view, path); + } + + target_view = view; + g_signal_emit (G_OBJECT (view), ephy_node_view_signals[SHOW_POPUP], 0); + target_view = NULL; + } + } + + return result; +} + +static void +ephy_node_view_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EphyNodeView *view = EPHY_NODE_VIEW (object); + + switch (prop_id) + { + case PROP_ROOT: + view->priv->root = g_value_get_object (value); + break; + case PROP_FILTER: + view->priv->filter = g_value_get_object (value); + + if (view->priv->filter != NULL) + { + g_signal_connect_object (G_OBJECT (view->priv->filter), + "changed", + G_CALLBACK (filter_changed_cb), + G_OBJECT (view), + 0); + } + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +ephy_node_view_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EphyNodeView *view = EPHY_NODE_VIEW (object); + + switch (prop_id) + { + case PROP_ROOT: + g_value_set_object (value, view->priv->root); + break; + case PROP_FILTER: + g_value_set_object (value, view->priv->filter); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +node_from_sort_iter_cb (EphyTreeModelSort *model, + GtkTreeIter *iter, + void **node, + EphyNodeView *view) +{ + GtkTreeIter filter_iter, node_iter; + + gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model), + &filter_iter, iter); + egg_tree_model_filter_convert_iter_to_child_iter (EGG_TREE_MODEL_FILTER (view->priv->filtermodel), + &node_iter, &filter_iter); + *node = ephy_tree_model_node_node_from_iter + (EPHY_TREE_MODEL_NODE (view->priv->nodemodel), &node_iter); +} + +static void +ephy_node_view_construct (EphyNodeView *view) +{ + GtkTreeSelection *selection; + + + view->priv->nodemodel = ephy_tree_model_node_new (view->priv->root, + view->priv->filter); + view->priv->filtermodel = egg_tree_model_filter_new (GTK_TREE_MODEL (view->priv->nodemodel), + NULL); + egg_tree_model_filter_set_visible_column (EGG_TREE_MODEL_FILTER (view->priv->filtermodel), + EPHY_TREE_MODEL_NODE_COL_VISIBLE); + view->priv->sortmodel = ephy_tree_model_sort_new (view->priv->filtermodel); + g_signal_connect_object (G_OBJECT (view->priv->sortmodel), + "node_from_iter", + G_CALLBACK (node_from_sort_iter_cb), + view, + 0); + gtk_tree_view_set_model (GTK_TREE_VIEW (view), GTK_TREE_MODEL (view->priv->sortmodel)); + g_signal_connect_object (G_OBJECT (view), + "button_press_event", + G_CALLBACK (ephy_node_view_button_press_cb), + view, + 0); + g_signal_connect_object (G_OBJECT (view), + "key_press_event", + G_CALLBACK (ephy_node_view_key_press_cb), + view, + 0); + g_signal_connect_object (G_OBJECT (view), + "row_activated", + G_CALLBACK (ephy_node_view_row_activated_cb), + view, + 0); + + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view)); + gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE); + g_signal_connect_object (G_OBJECT (selection), + "changed", + G_CALLBACK (ephy_node_view_selection_changed_cb), + view, + 0); +} + +GtkWidget * +ephy_node_view_new (EphyNode *root, + EphyNodeFilter *filter) +{ + EphyNodeView *view; + + view = EPHY_NODE_VIEW (g_object_new (EPHY_TYPE_NODE_VIEW, + "filter", filter, + "root", root, + NULL)); + + ephy_node_view_construct (view); + + g_return_val_if_fail (view->priv != NULL, NULL); + + return GTK_WIDGET (view); +} + +static void +cell_renderer_edited (GtkCellRendererText *cell, + const char *path_str, + const char *new_text, + EphyNodeView *view) +{ + GValue value = { 0, }; + GtkTreePath *path; + GtkTreeIter iter, iter2; + EphyNode *node; + + view->priv->editing = FALSE; + + g_object_set (G_OBJECT (view->priv->editable_renderer), + "editable", FALSE, + NULL); + + path = gtk_tree_path_new_from_string (path_str); + gtk_tree_model_get_iter (view->priv->sortmodel, &iter, path); + gtk_tree_model_sort_convert_iter_to_child_iter + (GTK_TREE_MODEL_SORT (view->priv->sortmodel), &iter2, &iter); + egg_tree_model_filter_convert_iter_to_child_iter + (EGG_TREE_MODEL_FILTER (view->priv->filtermodel), &iter, &iter2); + node = ephy_tree_model_node_node_from_iter (view->priv->nodemodel, &iter); + + g_value_init (&value, G_TYPE_STRING); + g_value_set_string (&value, new_text); + ephy_node_set_property (node, + view->priv->editable_property, + &value); + g_value_unset (&value); +} + +static int +ephy_node_view_sort_func (GtkTreeModel *model, + GtkTreeIter *a, + GtkTreeIter *b, + gpointer user_data) +{ + GList *order; + GList *l; + int retval = 0; + + g_return_val_if_fail (model != NULL, 0); + g_return_val_if_fail (user_data != NULL, 0); + + order = (GList *) user_data; + + for (l = order; l != NULL && retval == 0; l = g_list_next (l)) + { + EphyTreeModelNodeColumn column = GPOINTER_TO_INT (l->data); + GType type = gtk_tree_model_get_column_type (model, column); + GValue a_value = {0, }; + GValue b_value = {0, }; + gchar *stra, *strb; + + gtk_tree_model_get_value (model, a, column, &a_value); + gtk_tree_model_get_value (model, b, column, &b_value); + + switch (G_TYPE_FUNDAMENTAL (type)) + { + case G_TYPE_STRING: + stra = g_utf8_casefold (g_value_get_string (&a_value), -1); + strb = g_utf8_casefold (g_value_get_string (&b_value), -1); + g_return_val_if_fail (stra != NULL || strb != NULL, 0); + retval = g_utf8_collate (stra, strb); + g_free (stra); + g_free (strb); + break; + case G_TYPE_INT: + if (g_value_get_int (&a_value) < g_value_get_int (&b_value)) + retval = -1; + else if (g_value_get_int (&a_value) == g_value_get_int (&b_value)) + retval = 0; + else + retval = 1; + break; + case G_TYPE_BOOLEAN: + if (g_value_get_boolean (&a_value) < g_value_get_boolean (&b_value)) + retval = -1; + else if (g_value_get_boolean (&a_value) == g_value_get_boolean (&b_value)) + retval = 0; + else + retval = 1; + break; + default: + g_warning ("Attempting to sort on invalid type %s\n", g_type_name (type)); + break; + } + + g_value_unset (&a_value); + g_value_unset (&b_value); + } + + return retval; + +} + +static gboolean +set_sort_column_id (EphyNodeView *view) +{ + GList *sort_order = NULL; + sort_order = g_list_append (sort_order, GINT_TO_POINTER (view->priv->priority_column_id)); + sort_order = g_list_append (sort_order, GINT_TO_POINTER (view->priv->default_sort_column_id)); + + gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (view->priv->sortmodel), + GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, + GTK_SORT_ASCENDING); + gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (view->priv->sortmodel), + ephy_node_view_sort_func, + sort_order, + (GtkDestroyNotify) g_list_free); + + return FALSE; +} + +static void +provide_priority (EphyNode *node, GValue *value, EphyNodeView *view) +{ + int priority; + + g_value_init (value, G_TYPE_INT); + priority = ephy_node_get_property_int (node, view->priv->priority_prop_id); + if (priority == EPHY_NODE_VIEW_ALL_PRIORITY || + priority == EPHY_NODE_VIEW_SPECIAL_PRIORITY) + g_value_set_int (value, priority); + else + g_value_set_int (value, EPHY_NODE_VIEW_NORMAL_PRIORITY); +} + +static void +provide_text_weight (EphyNode *node, GValue *value, EphyNodeView *view) +{ + int priority; + + g_value_init (value, G_TYPE_INT); + priority = ephy_node_get_property_int + (node, view->priv->priority_prop_id); + if (priority == EPHY_NODE_VIEW_ALL_PRIORITY || + priority == EPHY_NODE_VIEW_SPECIAL_PRIORITY) + g_value_set_int (value, PANGO_WEIGHT_BOLD); + else + g_value_set_int (value, PANGO_WEIGHT_NORMAL); +} + +GtkTreeViewColumn * +ephy_node_view_add_column (EphyNodeView *view, + const char *title, + GType value_type, + int prop_id, + int priority_prop_id, + gboolean editable, + gboolean sortable) +{ + GtkTreeViewColumn *gcolumn; + GtkCellRenderer *renderer; + int column; + + g_return_val_if_fail (!editable || view->priv->editable_renderer == NULL, NULL); + + column = ephy_tree_model_node_add_prop_column + (view->priv->nodemodel, value_type, prop_id); + + gcolumn = (GtkTreeViewColumn *) gtk_tree_view_column_new (); + renderer = gtk_cell_renderer_text_new (); + + if (editable) + { + view->priv->editable_renderer = renderer; + view->priv->editable_column = gcolumn; + view->priv->editable_node_column = column; + g_signal_connect (renderer, "edited", G_CALLBACK (cell_renderer_edited), view); + } + + gtk_tree_view_column_pack_start (gcolumn, renderer, TRUE); + gtk_tree_view_column_set_attributes (gcolumn, renderer, + "text", column, + NULL); + if (priority_prop_id > 0) + { + int wcol; + + wcol = ephy_tree_model_node_add_func_column + (view->priv->nodemodel, G_TYPE_INT, + (EphyTreeModelNodeValueFunc) provide_text_weight, + view); + gtk_tree_view_column_add_attribute (gcolumn, renderer, + "weight", wcol); + } + + gtk_tree_view_column_set_sizing (gcolumn, + GTK_TREE_VIEW_COLUMN_AUTOSIZE); + gtk_tree_view_column_set_title (gcolumn, title); + gtk_tree_view_append_column (GTK_TREE_VIEW (view), + gcolumn); + + if (sortable) + { + int scol; + + scol = ephy_tree_model_node_add_func_column + (view->priv->nodemodel, G_TYPE_INT, + (EphyTreeModelNodeValueFunc) provide_priority, + view); + view->priv->priority_column_id = scol; + + view->priv->priority_prop_id = priority_prop_id; + view->priv->default_sort_column_id = column; + g_idle_add ((GSourceFunc) set_sort_column_id, view); + } + + return gcolumn; +} + +void +ephy_node_view_add_icon_column (EphyNodeView *view, + EphyTreeModelNodeValueFunc func) +{ + GtkTreeViewColumn *gcolumn; + GtkCellRenderer *renderer; + int column; + + column = ephy_tree_model_node_add_func_column + (view->priv->nodemodel, GDK_TYPE_PIXBUF, func, NULL); + + gcolumn = (GtkTreeViewColumn *) gtk_tree_view_column_new (); + renderer = gtk_cell_renderer_pixbuf_new (); + gtk_tree_view_column_pack_start (gcolumn, renderer, TRUE); + gtk_tree_view_column_set_attributes (gcolumn, renderer, + "pixbuf", column, + NULL); + gtk_tree_view_column_set_sizing (gcolumn, + GTK_TREE_VIEW_COLUMN_GROW_ONLY); + gtk_tree_view_append_column (GTK_TREE_VIEW (view), + gcolumn); +} + +static void +ephy_node_view_init (EphyNodeView *view) +{ + view->priv = g_new0 (EphyNodeViewPrivate, 1); + view->priv->editable_renderer = NULL; + view->priv->editing = TRUE; + view->priv->selected_node = NULL; +} + +static void +get_selection (GtkTreeModel *model, + GtkTreePath *path, + GtkTreeIter *iter, + void **data) +{ + GtkTreeModelSort *sortmodel = GTK_TREE_MODEL_SORT (model); + EggTreeModelFilter *filtermodel = EGG_TREE_MODEL_FILTER (sortmodel->child_model); + EphyTreeModelNode *nodemodel = EPHY_TREE_MODEL_NODE (filtermodel->child_model); + GList **list = (GList **) data; + GtkTreeIter *iter2 = gtk_tree_iter_copy (iter); + GtkTreeIter iter3; + GtkTreeIter iter4; + EphyNode *node; + + gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model), + &iter3, iter2); + egg_tree_model_filter_convert_iter_to_child_iter (filtermodel, &iter4, &iter3); + + node = ephy_tree_model_node_node_from_iter (nodemodel, &iter4); + + gtk_tree_iter_free (iter2); + + *list = g_list_prepend (*list, node); +} + +GList * +ephy_node_view_get_selection (EphyNodeView *view) +{ + GList *list = NULL; + GtkTreeSelection *selection; + + if (view->priv->selected_node) + { + list = g_list_append (list, view->priv->selected_node); + } + else + { + selection = gtk_tree_view_get_selection + (GTK_TREE_VIEW (view)); + + gtk_tree_selection_selected_foreach + (selection, + (GtkTreeSelectionForeachFunc) get_selection, + (void **) &list); + } + + return list; +} + +void +ephy_node_view_remove (EphyNodeView *view) +{ + GList *list; + EphyNode *node; + GtkTreeIter iter, iter2; + GtkTreePath *path; + + /* Before removing we try to select the next node in the view. If that is + * not available we try with the previous one, and if that is absent too, + * we do not select anything (which equals to select the topic "All") + */ + + list = ephy_node_view_get_selection (view); + g_return_if_fail (list != NULL); + node = EPHY_NODE ((g_list_last (list))->data); + ephy_tree_model_node_iter_from_node (EPHY_TREE_MODEL_NODE (view->priv->nodemodel), + node, &iter); + egg_tree_model_filter_convert_child_iter_to_iter (EGG_TREE_MODEL_FILTER (view->priv->filtermodel), + &iter2, &iter); + gtk_tree_model_sort_convert_child_iter_to_iter (GTK_TREE_MODEL_SORT (view->priv->sortmodel), + &iter, &iter2); + iter2 = iter; + + if (gtk_tree_model_iter_next (GTK_TREE_MODEL (view->priv->sortmodel), &iter)) + { + path = gtk_tree_model_get_path (GTK_TREE_MODEL (view->priv->sortmodel), &iter); + gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE); + gtk_tree_path_free (path); + } + else + { + path = gtk_tree_model_get_path (GTK_TREE_MODEL (view->priv->sortmodel), &iter2); + if (gtk_tree_path_prev (path)) + { + gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), path, NULL, FALSE); + } + gtk_tree_path_free (path); + } + + for (; list != NULL; list = list->next) + { + ephy_node_unref (EPHY_NODE (list->data)); + } + + g_list_free (list); +} + +void +ephy_node_view_select_node (EphyNodeView *view, + EphyNode *node) +{ + GtkTreeIter iter, iter2; + GValue val = { 0, }; + gboolean visible; + GtkTreeSelection *selection; + GtkTreePath *path; + + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view)); + + g_return_if_fail (node != NULL); + + ephy_tree_model_node_iter_from_node (EPHY_TREE_MODEL_NODE (view->priv->nodemodel), + node, &iter); + gtk_tree_model_get_value (GTK_TREE_MODEL (view->priv->nodemodel), &iter, + EPHY_TREE_MODEL_NODE_COL_VISIBLE, &val); + visible = g_value_get_boolean (&val); + g_value_unset (&val); + if (visible == FALSE) return; + + egg_tree_model_filter_convert_child_iter_to_iter (EGG_TREE_MODEL_FILTER (view->priv->filtermodel), + &iter2, &iter); + gtk_tree_model_sort_convert_child_iter_to_iter (GTK_TREE_MODEL_SORT (view->priv->sortmodel), + &iter, &iter2); + + path = gtk_tree_model_get_path (GTK_TREE_MODEL (view->priv->sortmodel), &iter); + gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), + path, NULL, FALSE); + gtk_tree_path_free (path); +} + +void +ephy_node_view_enable_drag_source (EphyNodeView *view, + GtkTargetEntry *types, + int n_types, + guint prop_id) +{ + GtkWidget *treeview; + + g_return_if_fail (view != NULL); + + treeview = GTK_WIDGET (view); + + egg_tree_multi_drag_add_drag_support (GTK_TREE_VIEW (treeview)); + + gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW (treeview), + GDK_BUTTON1_MASK, + types, n_types, + GDK_ACTION_COPY); + + ephy_tree_model_sort_set_drag_property (EPHY_TREE_MODEL_SORT (view->priv->sortmodel), + prop_id); +} + +void +ephy_node_view_edit (EphyNodeView *view) +{ + GtkTreeSelection *selection; + GList *rows; + GtkTreeModel *model; + + g_return_if_fail (view->priv->editable_renderer != NULL); + + selection = gtk_tree_view_get_selection + (GTK_TREE_VIEW (view)); + rows = gtk_tree_selection_get_selected_rows (selection, &model); + if (rows == NULL) return; + + g_object_set (G_OBJECT (view->priv->editable_renderer), + "editable", TRUE, + NULL); + + gtk_tree_view_set_cursor (GTK_TREE_VIEW (view), + (GtkTreePath *)rows->data, + view->priv->editable_column, + TRUE); + + view->priv->editing = TRUE; + + g_list_foreach (rows, (GFunc)gtk_tree_path_free, NULL); + g_list_free (rows); +} + +gboolean +ephy_node_view_is_editing (EphyNodeView *view, + int property) +{ + view->priv->editable_property = property; + + return view->priv->editing; +} + +gboolean +ephy_node_view_is_target (EphyNodeView *view) +{ + if (target_view == view) + { + return TRUE; + } + else if (target_view != NULL) + { + return FALSE; + } + else + { + return gtk_widget_is_focus (GTK_WIDGET (view)); + } +} + +gboolean +ephy_node_view_has_selection (EphyNodeView *view, gboolean *multiple) +{ + GtkTreeSelection *selection; + int rows; + + if (view->priv->selected_node) + { + rows = 1; + } + else + { + selection = gtk_tree_view_get_selection + (GTK_TREE_VIEW (view)); + + rows = gtk_tree_selection_count_selected_rows (selection); + } + + if (multiple) + { + *multiple = rows > 1; + } + + return rows > 0; +} + +void +ephy_node_view_enable_sort (EphyNodeView *view, + GtkTreeIterCompareFunc sort_func, + gpointer data, + GtkDestroyNotify destroy) +{ + gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (view->priv->sortmodel), + GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, + GTK_SORT_ASCENDING); + gtk_tree_sortable_set_default_sort_func (GTK_TREE_SORTABLE (view->priv->sortmodel), + sort_func, data, destroy); +} diff --git a/lib/widgets/ephy-node-view.h b/lib/widgets/ephy-node-view.h new file mode 100644 index 000000000..148b376b1 --- /dev/null +++ b/lib/widgets/ephy-node-view.h @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + */ + +#ifndef __EPHY_NODE_VIEW_H +#define __EPHY_NODE_VIEW_H + +#include <gtk/gtktreeview.h> +#include <gtk/gtkdnd.h> + +#include "ephy-tree-model-node.h" +#include "ephy-node-filter.h" + +G_BEGIN_DECLS + +#define EPHY_TYPE_NODE_VIEW (ephy_node_view_get_type ()) +#define EPHY_NODE_VIEW(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EPHY_TYPE_NODE_VIEW, EphyNodeView)) +#define EPHY_NODE_VIEW_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), EPHY_TYPE_NODE_VIEW, EphyNodeViewClass)) +#define EPHY_IS_NODE_VIEW(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EPHY_TYPE_NODE_VIEW)) +#define EPHY_IS_NODE_VIEW_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EPHY_TYPE_NODE_VIEW)) +#define EPHY_NODE_VIEW_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EPHY_TYPE_NODE_VIEW, EphyNodeViewClass)) + +typedef struct EphyNodeViewPrivate EphyNodeViewPrivate; + +typedef struct +{ + GtkTreeView parent; + + EphyNodeViewPrivate *priv; +} EphyNodeView; + +typedef enum +{ + EPHY_NODE_VIEW_ALL_PRIORITY, + EPHY_NODE_VIEW_SPECIAL_PRIORITY, + EPHY_NODE_VIEW_NORMAL_PRIORITY +} EphyNodeViewPriority; + +typedef struct +{ + GtkTreeViewClass parent; + + void (*node_activated) (EphyNodeView *view, EphyNode *node); + void (*node_selected) (EphyNodeView *view, EphyNode *node); + void (*node_dropped) (EphyNodeView *view, EphyNode *node, GList *nodes); + void (*show_popup) (EphyNodeView *view); +} EphyNodeViewClass; + +GType ephy_node_view_get_type (void); + +GtkWidget *ephy_node_view_new (EphyNode *root, + EphyNodeFilter *filter); + +void ephy_node_view_enable_dnd (EphyNodeView *view); + +GtkTreeViewColumn *ephy_node_view_add_column (EphyNodeView *view, + const char *title, + GType value_type, + int prop_id, + int priority_prop_id, + gboolean editable, + gboolean sortable); + +void ephy_node_view_add_icon_column (EphyNodeView *view, + EphyTreeModelNodeValueFunc func); + +void ephy_node_view_remove (EphyNodeView *view); + +gboolean ephy_node_view_has_selection (EphyNodeView *view, + gboolean *multiple); + +GList *ephy_node_view_get_selection (EphyNodeView *view); + +void ephy_node_view_select_node (EphyNodeView *view, + EphyNode *node); + +void ephy_node_view_enable_drag_source (EphyNodeView *view, + GtkTargetEntry *types, + int n_types, + guint prop_id); + +void ephy_node_view_enable_drag_dest (EphyNodeView *view, + GtkTargetEntry *types, + int n_types); + +void ephy_node_view_edit (EphyNodeView *view); + +gboolean ephy_node_view_is_editing (EphyNodeView *view, + int property); + +gboolean ephy_node_view_is_target (EphyNodeView *view); + +void ephy_node_view_enable_sort (EphyNodeView *view, + GtkTreeIterCompareFunc sort_func, + gpointer data, + GtkDestroyNotify destroy); + +G_END_DECLS + +#endif /* EPHY_NODE_VIEW_H */ diff --git a/lib/widgets/ephy-tree-model-node.c b/lib/widgets/ephy-tree-model-node.c new file mode 100644 index 000000000..35d36c98d --- /dev/null +++ b/lib/widgets/ephy-tree-model-node.c @@ -0,0 +1,727 @@ +/* + * Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + */ + +#include <config.h> +#include <gtk/gtktreeview.h> +#include <gdk-pixbuf/gdk-pixbuf.h> +#include <libgnome/gnome-i18n.h> +#include <time.h> +#include <string.h> + +#include "ephy-node-filter.h" +#include "ephy-tree-model-node.h" +#include "ephy-stock-icons.h" +#include "ephy-node.h" +#include "ephy-debug.h" + +static void ephy_tree_model_node_class_init (EphyTreeModelNodeClass *klass); +static void ephy_tree_model_node_init (EphyTreeModelNode *model); +static void ephy_tree_model_node_finalize (GObject *object); +static void ephy_tree_model_node_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec); +static void ephy_tree_model_node_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec); +static guint ephy_tree_model_node_get_flags (GtkTreeModel *tree_model); +static gboolean ephy_tree_model_node_get_iter (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreePath *path); +static GtkTreePath *ephy_tree_model_node_get_path (GtkTreeModel *tree_model, + GtkTreeIter *iter); +static gboolean ephy_tree_model_node_iter_next (GtkTreeModel *tree_model, + GtkTreeIter *iter); +static gboolean ephy_tree_model_node_iter_children (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *parent); +static gboolean ephy_tree_model_node_iter_has_child (GtkTreeModel *tree_model, + GtkTreeIter *iter); +static int ephy_tree_model_node_iter_n_children (GtkTreeModel *tree_model, + GtkTreeIter *iter); +static gboolean ephy_tree_model_node_iter_nth_child (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *parent, + int n); +static gboolean ephy_tree_model_node_iter_parent (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *child); +static void ephy_tree_model_node_tree_model_init (GtkTreeModelIface *iface); +static void root_child_removed_cb (EphyNode *node, + EphyNode *child, + EphyTreeModelNode *model); +static void root_child_added_cb (EphyNode *node, + EphyNode *child, + EphyTreeModelNode *model); +static void root_child_changed_cb (EphyNode *node, + EphyNode *child, + EphyTreeModelNode *model); +static inline void ephy_tree_model_node_update_node (EphyTreeModelNode *model, + EphyNode *node, + int idx); +static void root_destroyed_cb (EphyNode *node, + EphyTreeModelNode *model); +static inline GtkTreePath *get_path_real (EphyTreeModelNode *model, + EphyNode *node); + +struct EphyTreeModelNodePrivate +{ + EphyNode *root; + + EphyNodeFilter *filter; + + GList *columns; + int columns_num; +}; + +typedef struct +{ + GType type; + int prop_id; + EphyTreeModelNodeValueFunc func; + gpointer user_data; +} EphyTreeModelNodeColData; + +enum +{ + PROP_0, + PROP_ROOT, + PROP_FILTER +}; + +static GObjectClass *parent_class = NULL; + +GType +ephy_tree_model_node_get_type (void) +{ + static GType ephy_tree_model_node_type = 0; + + if (ephy_tree_model_node_type == 0) + { + static const GTypeInfo our_info = + { + sizeof (EphyTreeModelNodeClass), + NULL, + NULL, + (GClassInitFunc) ephy_tree_model_node_class_init, + NULL, + NULL, + sizeof (EphyTreeModelNode), + 0, + (GInstanceInitFunc) ephy_tree_model_node_init + }; + + static const GInterfaceInfo tree_model_info = + { + (GInterfaceInitFunc) ephy_tree_model_node_tree_model_init, + NULL, + NULL + }; + + ephy_tree_model_node_type = g_type_register_static (G_TYPE_OBJECT, + "EphyTreeModelNode", + &our_info, 0); + + g_type_add_interface_static (ephy_tree_model_node_type, + GTK_TYPE_TREE_MODEL, + &tree_model_info); + } + + return ephy_tree_model_node_type; +} + +static void +ephy_tree_model_node_class_init (EphyTreeModelNodeClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + parent_class = g_type_class_peek_parent (klass); + + object_class->finalize = ephy_tree_model_node_finalize; + + object_class->set_property = ephy_tree_model_node_set_property; + object_class->get_property = ephy_tree_model_node_get_property; + + g_object_class_install_property (object_class, + PROP_ROOT, + g_param_spec_object ("root", + "Root node", + "Root node", + EPHY_TYPE_NODE, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); + g_object_class_install_property (object_class, + PROP_FILTER, + g_param_spec_object ("filter", + "Filter object", + "Filter object", + EPHY_TYPE_NODE_FILTER, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); +} + +static void +ephy_tree_model_node_init (EphyTreeModelNode *model) +{ + GtkWidget *dummy; + + do + { + model->stamp = g_random_int (); + } + while (model->stamp == 0); + + model->priv = g_new0 (EphyTreeModelNodePrivate, 1); + + dummy = gtk_tree_view_new (); + + gtk_widget_destroy (dummy); + + model->priv->columns = NULL; + model->priv->columns_num = EPHY_TREE_MODEL_NODE_BUILTIN_COLUMNS; +} + +static void +ephy_tree_model_node_finalize (GObject *object) +{ + EphyTreeModelNode *model; + + g_return_if_fail (object != NULL); + g_return_if_fail (EPHY_IS_TREE_MODEL_NODE (object)); + + model = EPHY_TREE_MODEL_NODE (object); + + g_return_if_fail (model->priv != NULL); + + g_list_foreach (model->priv->columns, (GFunc) g_free, NULL); + g_list_free (model->priv->columns); + + g_free (model->priv); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +static void +filter_changed_cb (EphyNodeFilter *filter, + EphyTreeModelNode *model) +{ + GPtrArray *kids; + int i; + + kids = ephy_node_get_children (model->priv->root); + + for (i = 0; i < kids->len; i++) + { + ephy_tree_model_node_update_node (model, + g_ptr_array_index (kids, i), + i); + } + + ephy_node_thaw (model->priv->root); +} + +static void +ephy_tree_model_node_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (object); + + switch (prop_id) + { + case PROP_ROOT: + model->priv->root = g_value_get_object (value); + + g_signal_connect_object (G_OBJECT (model->priv->root), + "child_added", + G_CALLBACK (root_child_added_cb), + G_OBJECT (model), + 0); + g_signal_connect_object (G_OBJECT (model->priv->root), + "child_removed", + G_CALLBACK (root_child_removed_cb), + G_OBJECT (model), + 0); + g_signal_connect_object (G_OBJECT (model->priv->root), + "child_changed", + G_CALLBACK (root_child_changed_cb), + G_OBJECT (model), + 0); + g_signal_connect_object (G_OBJECT (model->priv->root), + "destroyed", + G_CALLBACK (root_destroyed_cb), + G_OBJECT (model), + 0); + + break; + case PROP_FILTER: + model->priv->filter = g_value_get_object (value); + + if (model->priv->filter != NULL) + { + g_signal_connect_object (G_OBJECT (model->priv->filter), + "changed", + G_CALLBACK (filter_changed_cb), + G_OBJECT (model), + 0); + } + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +ephy_tree_model_node_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (object); + + switch (prop_id) + { + case PROP_ROOT: + g_value_set_object (value, model->priv->root); + break; + case PROP_FILTER: + g_value_set_object (value, model->priv->filter); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +EphyTreeModelNode * +ephy_tree_model_node_new (EphyNode *root, + EphyNodeFilter *filter) +{ + EphyTreeModelNode *model; + + model = EPHY_TREE_MODEL_NODE (g_object_new (EPHY_TYPE_TREE_MODEL_NODE, + "filter", filter, + "root", root, + NULL)); + + g_return_val_if_fail (model->priv != NULL, NULL); + + return model; +} + +int +ephy_tree_model_node_add_prop_column (EphyTreeModelNode *model, + GType value_type, + int prop_id) +{ + EphyTreeModelNodeColData *col; + + col = g_new0 (EphyTreeModelNodeColData, 1); + col->prop_id = prop_id; + col->type = value_type; + col->func = NULL; + col->user_data = NULL; + + model->priv->columns = g_list_append (model->priv->columns, col); + model->priv->columns_num++; + + return model->priv->columns_num; +} + +int +ephy_tree_model_node_add_func_column (EphyTreeModelNode *model, + GType value_type, + EphyTreeModelNodeValueFunc func, + gpointer user_data) +{ + EphyTreeModelNodeColData *col; + + col = g_new0 (EphyTreeModelNodeColData, 1); + col->prop_id = -1; + col->type = value_type; + col->func = func; + col->user_data = user_data; + + model->priv->columns = g_list_append (model->priv->columns, col); + model->priv->columns_num++; + + return model->priv->columns_num; +} + +static int +ephy_tree_model_node_get_n_columns (GtkTreeModel *tree_model) +{ + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (tree_model); + + return model->priv->columns_num; +} + +static GType +ephy_tree_model_node_get_column_type (GtkTreeModel *tree_model, + int index) +{ + int list_index; + EphyTreeModelNodeColData *col; + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (tree_model); + + if (index == EPHY_TREE_MODEL_NODE_COL_VISIBLE) + return G_TYPE_BOOLEAN; + + list_index = index - EPHY_TREE_MODEL_NODE_BUILTIN_COLUMNS - 1; + col = g_list_nth_data (model->priv->columns, list_index); + + return col->type; +} + +static void +ephy_tree_model_node_get_value (GtkTreeModel *tree_model, + GtkTreeIter *iter, + int column, + GValue *value) +{ + int list_index; + EphyTreeModelNodeColData *col; + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (tree_model); + EphyNode *node; + + g_return_if_fail (EPHY_IS_TREE_MODEL_NODE (tree_model)); + g_return_if_fail (iter != NULL); + g_return_if_fail (iter->stamp == model->stamp); + g_return_if_fail (EPHY_IS_NODE (iter->user_data)); + + if (model->priv->root == NULL) + return; + + node = EPHY_NODE (iter->user_data); + + if (column == EPHY_TREE_MODEL_NODE_COL_VISIBLE) + { + g_value_init (value, G_TYPE_BOOLEAN); + + if (model->priv->filter != NULL) + { + g_value_set_boolean (value, + ephy_node_filter_evaluate (model->priv->filter, node)); + } + else + { + g_value_set_boolean (value, TRUE); + } + } + else + { + list_index = column - EPHY_TREE_MODEL_NODE_BUILTIN_COLUMNS - 1; + col = g_list_nth_data (model->priv->columns, list_index); + + g_return_if_fail (col != NULL); + + if (col->prop_id > 0) + { + ephy_node_get_property (node, + col->prop_id, + value); + } + else + { + col->func (node, value, col->user_data); + } + } +} + +static void +ephy_tree_model_node_tree_model_init (GtkTreeModelIface *iface) +{ + iface->get_flags = ephy_tree_model_node_get_flags; + iface->get_iter = ephy_tree_model_node_get_iter; + iface->get_path = ephy_tree_model_node_get_path; + iface->iter_next = ephy_tree_model_node_iter_next; + iface->iter_children = ephy_tree_model_node_iter_children; + iface->iter_has_child = ephy_tree_model_node_iter_has_child; + iface->iter_n_children = ephy_tree_model_node_iter_n_children; + iface->iter_nth_child = ephy_tree_model_node_iter_nth_child; + iface->iter_parent = ephy_tree_model_node_iter_parent; + iface->get_n_columns = ephy_tree_model_node_get_n_columns; + iface->get_column_type = ephy_tree_model_node_get_column_type; + iface->get_value = ephy_tree_model_node_get_value; +} + +static guint +ephy_tree_model_node_get_flags (GtkTreeModel *tree_model) +{ + return 0; +} + +static gboolean +ephy_tree_model_node_get_iter (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreePath *path) +{ + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (tree_model); + int i; + + g_return_val_if_fail (EPHY_IS_TREE_MODEL_NODE (model), FALSE); + g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE); + + if (model->priv->root == NULL) + return FALSE; + + i = gtk_tree_path_get_indices (path)[0]; + + iter->stamp = model->stamp; + iter->user_data = ephy_node_get_nth_child (model->priv->root, i); + + if (iter->user_data == NULL) + { + iter->stamp = 0; + return FALSE; + } + + return TRUE; +} + +static inline GtkTreePath * +get_path_real (EphyTreeModelNode *model, + EphyNode *node) +{ + GtkTreePath *retval; + + retval = gtk_tree_path_new (); + gtk_tree_path_append_index (retval, ephy_node_get_child_index (model->priv->root, node)); + + return retval; +} + +static GtkTreePath * +ephy_tree_model_node_get_path (GtkTreeModel *tree_model, + GtkTreeIter *iter) +{ + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (tree_model); + EphyNode *node; + + g_return_val_if_fail (EPHY_IS_TREE_MODEL_NODE (tree_model), NULL); + g_return_val_if_fail (iter != NULL, NULL); + g_return_val_if_fail (iter->user_data != NULL, NULL); + g_return_val_if_fail (iter->stamp == model->stamp, NULL); + + if (model->priv->root == NULL) + return NULL; + + node = EPHY_NODE (iter->user_data); + + if (node == model->priv->root) + return gtk_tree_path_new (); + + return get_path_real (model, node); +} + +static gboolean +ephy_tree_model_node_iter_next (GtkTreeModel *tree_model, + GtkTreeIter *iter) +{ + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (tree_model); + EphyNode *node; + + g_return_val_if_fail (iter != NULL, FALSE); + g_return_val_if_fail (iter->user_data != NULL, FALSE); + g_return_val_if_fail (iter->stamp == EPHY_TREE_MODEL_NODE (tree_model)->stamp, FALSE); + + if (model->priv->root == NULL) + return FALSE; + + node = EPHY_NODE (iter->user_data); + + if (node == model->priv->root) + return FALSE; + + iter->user_data = ephy_node_get_next_child (model->priv->root, node); + + return (iter->user_data != NULL); +} + +static gboolean +ephy_tree_model_node_iter_children (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *parent) +{ + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (tree_model); + + if (model->priv->root == NULL) + return FALSE; + + if (parent != NULL) + return FALSE; + + iter->stamp = model->stamp; + iter->user_data = model->priv->root; + + return TRUE; +} + +static gboolean +ephy_tree_model_node_iter_has_child (GtkTreeModel *tree_model, + GtkTreeIter *iter) +{ + return FALSE; +} + +static int +ephy_tree_model_node_iter_n_children (GtkTreeModel *tree_model, + GtkTreeIter *iter) +{ + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (tree_model); + + g_return_val_if_fail (EPHY_IS_TREE_MODEL_NODE (tree_model), -1); + + if (model->priv->root == NULL) + return 0; + + if (iter == NULL) + return ephy_node_get_n_children (model->priv->root); + + g_return_val_if_fail (model->stamp == iter->stamp, -1); + + return 0; +} + +static gboolean +ephy_tree_model_node_iter_nth_child (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *parent, + int n) +{ + EphyTreeModelNode *model = EPHY_TREE_MODEL_NODE (tree_model); + EphyNode *node; + + g_return_val_if_fail (EPHY_IS_TREE_MODEL_NODE (tree_model), FALSE); + + if (model->priv->root == NULL) + return FALSE; + + if (parent != NULL) + return FALSE; + + node = ephy_node_get_nth_child (model->priv->root, n); + + if (node != NULL) + { + iter->stamp = model->stamp; + iter->user_data = node; + return TRUE; + } + else + return FALSE; +} + +static gboolean +ephy_tree_model_node_iter_parent (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *child) +{ + return FALSE; +} + +EphyNode * +ephy_tree_model_node_node_from_iter (EphyTreeModelNode *model, + GtkTreeIter *iter) +{ + return EPHY_NODE (iter->user_data); +} + +void +ephy_tree_model_node_iter_from_node (EphyTreeModelNode *model, + EphyNode *node, + GtkTreeIter *iter) +{ + iter->stamp = model->stamp; + iter->user_data = node; +} + +static void +root_child_removed_cb (EphyNode *node, + EphyNode *child, + EphyTreeModelNode *model) +{ + GtkTreePath *path; + + path = get_path_real (model, child); + gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path); + gtk_tree_path_free (path); +} + +static void +root_child_added_cb (EphyNode *node, + EphyNode *child, + EphyTreeModelNode *model) +{ + GtkTreePath *path; + GtkTreeIter iter; + + ephy_tree_model_node_iter_from_node (model, child, &iter); + + path = get_path_real (model, child); + gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter); + gtk_tree_path_free (path); +} + +static inline void +ephy_tree_model_node_update_node (EphyTreeModelNode *model, + EphyNode *node, + int idx) +{ + GtkTreePath *path; + GtkTreeIter iter; + + ephy_tree_model_node_iter_from_node (model, node, &iter); + + if (idx >= 0) + { + path = gtk_tree_path_new (); + gtk_tree_path_append_index (path, idx); + } + else + { + path = get_path_real (model, node); + } + + LOG ("Updating row") + + gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter); + gtk_tree_path_free (path); +} + +static void +root_child_changed_cb (EphyNode *node, + EphyNode *child, + EphyTreeModelNode *model) +{ + ephy_tree_model_node_update_node (model, child, -1); +} + +static void +root_destroyed_cb (EphyNode *node, + EphyTreeModelNode *model) +{ + model->priv->root = NULL; + + /* no need to do other stuff since we should have had a bunch of child_removed + * signals already */ +} + diff --git a/lib/widgets/ephy-tree-model-node.h b/lib/widgets/ephy-tree-model-node.h new file mode 100644 index 000000000..d50e9732f --- /dev/null +++ b/lib/widgets/ephy-tree-model-node.h @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * $Id$ + */ + +#ifndef __EPHY_TREE_MODEL_NODE_H +#define __EPHY_TREE_MODEL_NODE_H + +#include <gtk/gtktreemodel.h> + +#include "ephy-node.h" +#include "ephy-node-filter.h" + +G_BEGIN_DECLS + +#define EPHY_TYPE_TREE_MODEL_NODE (ephy_tree_model_node_get_type ()) +#define EPHY_TREE_MODEL_NODE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), EPHY_TYPE_TREE_MODEL_NODE, EphyTreeModelNode)) +#define EPHY_TREE_MODEL_NODE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), EPHY_TYPE_TREE_MODEL_NODE, EphyTreeModelNodeClass)) +#define EPHY_IS_TREE_MODEL_NODE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), EPHY_TYPE_TREE_MODEL_NODE)) +#define EPHY_IS_TREE_MODEL_NODE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), EPHY_TYPE_TREE_MODEL_NODE)) +#define EPHY_TREE_MODEL_NODE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), EPHY_TYPE_TREE_MODEL_NODE, EphyTreeModelNodeClass)) + +typedef enum +{ + EPHY_TREE_MODEL_NODE_COL_VISIBLE, + EPHY_TREE_MODEL_NODE_BUILTIN_COLUMNS +} EphyTreeModelNodeColumn; + +typedef void (*EphyTreeModelNodeValueFunc) (EphyNode *node, GValue *value, gpointer user_data); + +typedef struct EphyTreeModelNodePrivate EphyTreeModelNodePrivate; + +typedef struct +{ + GObject parent; + + EphyTreeModelNodePrivate *priv; + + int stamp; +} EphyTreeModelNode; + +typedef struct +{ + GObjectClass parent; +} EphyTreeModelNodeClass; + +GType ephy_tree_model_node_get_type (void); + +EphyTreeModelNode *ephy_tree_model_node_new (EphyNode *root, + EphyNodeFilter *filter); + +int ephy_tree_model_node_add_prop_column (EphyTreeModelNode *model, + GType value_type, + int prop_id); + +int ephy_tree_model_node_add_func_column (EphyTreeModelNode *model, + GType value_type, + EphyTreeModelNodeValueFunc func, + gpointer user_data); + +EphyNode *ephy_tree_model_node_node_from_iter (EphyTreeModelNode *model, + GtkTreeIter *iter); + +void ephy_tree_model_node_iter_from_node (EphyTreeModelNode *model, + EphyNode *node, + GtkTreeIter *iter); + +G_END_DECLS + +#endif /* EPHY_TREE_MODEL_NODE_H */ |