diff options
author | Marco Pesenti Gritti <mpeseng@src.gnome.org> | 2003-04-25 19:42:37 +0800 |
---|---|---|
committer | Marco Pesenti Gritti <mpeseng@src.gnome.org> | 2003-04-25 19:42:37 +0800 |
commit | f490b816b02230dd74fc78610eae674f2aa8e88d (patch) | |
tree | 6e49b0df372307a9fa2a6afe22017d67377e1859 /lib | |
parent | 22bf87253ddffa6a7225ecaad60c15cd0815c974 (diff) | |
download | gsoc2013-epiphany-f490b816b02230dd74fc78610eae674f2aa8e88d.tar gsoc2013-epiphany-f490b816b02230dd74fc78610eae674f2aa8e88d.tar.gz gsoc2013-epiphany-f490b816b02230dd74fc78610eae674f2aa8e88d.tar.bz2 gsoc2013-epiphany-f490b816b02230dd74fc78610eae674f2aa8e88d.tar.lz gsoc2013-epiphany-f490b816b02230dd74fc78610eae674f2aa8e88d.tar.xz gsoc2013-epiphany-f490b816b02230dd74fc78610eae674f2aa8e88d.tar.zst gsoc2013-epiphany-f490b816b02230dd74fc78610eae674f2aa8e88d.zip |
*** empty log message ***
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/egg/egg-toolbar-editor.c | 599 | ||||
-rwxr-xr-x | lib/egg/egg-toolbar-editor.h | 61 | ||||
-rwxr-xr-x | lib/egg/egg-toolbars-group.c | 668 | ||||
-rwxr-xr-x | lib/egg/egg-toolbars-group.h | 103 | ||||
-rwxr-xr-x | lib/egg/egg-toolbars-model.c | 490 | ||||
-rwxr-xr-x | lib/egg/egg-toolbars-model.h | 104 |
6 files changed, 1254 insertions, 771 deletions
diff --git a/lib/egg/egg-toolbar-editor.c b/lib/egg/egg-toolbar-editor.c new file mode 100755 index 000000000..8993d4042 --- /dev/null +++ b/lib/egg/egg-toolbar-editor.c @@ -0,0 +1,599 @@ +/* + * Copyright (C) 2003 Marco Pesenti Gritti + * + * 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, 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 "egg-toolbar-editor.h" +#include "egg-editable-toolbar.h" +#include "eggintl.h" + +#include <string.h> +#include <libxml/tree.h> + +static GtkTargetEntry dest_drag_types[] = { + {EGG_TOOLBAR_ITEM_TYPE, 0, 0}, +}; +static int n_dest_drag_types = G_N_ELEMENTS (dest_drag_types); + +static GtkTargetEntry source_drag_types[] = { + {EGG_TOOLBAR_ITEM_TYPE, 0, 0}, +}; +static int n_source_drag_types = G_N_ELEMENTS (source_drag_types); + +static void egg_toolbar_editor_class_init (EggToolbarEditorClass *klass); +static void egg_toolbar_editor_init (EggToolbarEditor *t); +static void egg_toolbar_editor_finalize (GObject *object); +static void update_editor_sheet (EggToolbarEditor *editor); + +enum +{ + RESPONSE_ADD_TOOLBAR +}; + +enum +{ + PROP_0, + PROP_MENU_MERGE, + PROP_TOOLBARS_MODEL +}; + +static GObjectClass *parent_class = NULL; + +struct EggToolbarEditorPrivate +{ + EggMenuMerge *merge; + EggToolbarsModel *model; + + GtkWidget *table; + GtkWidget *scrolled_window; + + GList *actions_list; +}; + +GType +egg_toolbar_editor_get_type (void) +{ + static GType egg_toolbar_editor_type = 0; + + if (egg_toolbar_editor_type == 0) + { + static const GTypeInfo our_info = { + sizeof (EggToolbarEditorClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) egg_toolbar_editor_class_init, + NULL, + NULL, /* class_data */ + sizeof (EggToolbarEditor), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_toolbar_editor_init + }; + + egg_toolbar_editor_type = g_type_register_static (GTK_TYPE_DIALOG, + "EggToolbarEditor", + &our_info, 0); + } + + return egg_toolbar_editor_type; +} + +static EggAction * +find_action (EggToolbarEditor *t, + const char *name) +{ + GList *l = t->priv->merge->action_groups; + EggAction *action = NULL; + + g_return_val_if_fail (IS_EGG_TOOLBAR_EDITOR (t), NULL); + g_return_val_if_fail (name != NULL, NULL); + + for (; l != NULL; l = l->next) + { + EggAction *tmp; + + tmp = egg_action_group_get_action (EGG_ACTION_GROUP (l->data), name); + if (tmp) + action = tmp; + } + + return action; +} + +static void +egg_toolbar_editor_set_merge (EggToolbarEditor *t, + EggMenuMerge *merge) +{ + g_return_if_fail (EGG_IS_MENU_MERGE (merge)); + g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (t)); + + t->priv->merge = merge; +} + +static void +egg_toolbar_editor_set_model (EggToolbarEditor *t, + EggToolbarsModel *model) +{ + g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (t)); + + t->priv->model = model; +} + +static void +egg_toolbar_editor_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + EggToolbarEditor *t = EGG_TOOLBAR_EDITOR (object); + + switch (prop_id) + { + case PROP_MENU_MERGE: + egg_toolbar_editor_set_merge (t, g_value_get_object (value)); + break; + case PROP_TOOLBARS_MODEL: + egg_toolbar_editor_set_model (t, g_value_get_object (value)); + break; + } +} + +static void +egg_toolbar_editor_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + EggToolbarEditor *t = EGG_TOOLBAR_EDITOR (object); + + switch (prop_id) + { + case PROP_MENU_MERGE: + g_value_set_object (value, t->priv->merge); + break; + case PROP_TOOLBARS_MODEL: + g_value_set_object (value, t->priv->model); + break; + } +} + +static void +egg_toolbar_editor_class_init (EggToolbarEditorClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + parent_class = g_type_class_peek_parent (klass); + + object_class->finalize = egg_toolbar_editor_finalize; + object_class->set_property = egg_toolbar_editor_set_property; + object_class->get_property = egg_toolbar_editor_get_property; + + g_object_class_install_property (object_class, + PROP_MENU_MERGE, + g_param_spec_object ("MenuMerge", + "MenuMerge", + "Menu merge", + EGG_TYPE_MENU_MERGE, + G_PARAM_READWRITE)); + g_object_class_install_property (object_class, + PROP_TOOLBARS_MODEL, + g_param_spec_object ("ToolbarsModel", + "ToolbarsModel", + "Toolbars Model", + EGG_TOOLBARS_MODEL_TYPE, + G_PARAM_READWRITE)); +} + +static void +egg_toolbar_editor_finalize (GObject *object) +{ + EggToolbarEditor *t = EGG_TOOLBAR_EDITOR (object); + + g_return_if_fail (object != NULL); + g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (object)); + + g_free (t->priv); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +GtkWidget * +egg_toolbar_editor_new (EggMenuMerge *merge, + EggToolbarsModel *model) +{ + EggToolbarEditor *t; + + t = EGG_TOOLBAR_EDITOR (g_object_new (EGG_TOOLBAR_EDITOR_TYPE, + "MenuMerge", merge, + "ToolbarsModel", model, + NULL)); + + g_return_val_if_fail (t->priv != NULL, NULL); + + return GTK_WIDGET (t); +} + +static void +editor_drag_data_received_cb (GtkWidget *widget, + GdkDragContext *context, + gint x, + gint y, + GtkSelectionData *selection_data, + guint info, + guint time_, + EggToolbarEditor *editor) +{ + EggAction *action; + + g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor)); + g_return_if_fail (selection_data != NULL); + + action = find_action (editor, (const char *)selection_data->data); + g_return_if_fail (action != NULL); + editor->priv->actions_list = g_list_append + (editor->priv->actions_list, action); + + update_editor_sheet (editor); +} + +static void +editor_drag_data_delete_cb (GtkWidget *widget, + GdkDragContext *context, + EggToolbarEditor *editor) +{ + EggAction *action; + g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor)); + + action = EGG_ACTION (g_object_get_data (G_OBJECT (widget), "egg-action")); + if (action) + { + editor->priv->actions_list = g_list_remove + (editor->priv->actions_list, action); + } + + update_editor_sheet (editor); +} + +static void +drag_data_get_cb (GtkWidget *widget, + GdkDragContext *context, + GtkSelectionData *selection_data, + guint info, + guint32 time, + EggToolbarEditor *editor) +{ + EggAction *action; + const char *target; + + action = EGG_ACTION (g_object_get_data (G_OBJECT (widget), "egg-action")); + + if (action) + { + target = action->name; + } + else + { + target = "separator"; + } + + gtk_selection_data_set (selection_data, + selection_data->target, 8, target, strlen (target)); +} + +static void +editor_close (EggToolbarEditor *editor) +{ + g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor)); + gtk_widget_destroy (GTK_WIDGET (editor)); +} + +static void +editor_add_toolbar (EggToolbarEditor *editor) +{ + g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor)); + + egg_toolbars_model_add_toolbar (editor->priv->model, "UserCreated"); +} + +static void +dialog_response_cb (GtkDialog *dialog, + gint response_id, + EggToolbarEditor *editor) +{ + switch (response_id) + { + case RESPONSE_ADD_TOOLBAR: + editor_add_toolbar (editor); + break; + case GTK_RESPONSE_CLOSE: + editor_close (editor); + break; + } +} + +static gchar * +elide_underscores (const gchar *original) +{ + gchar *q, *result; + const gchar *p; + gboolean last_underscore; + + q = result = g_malloc (strlen (original) + 1); + last_underscore = FALSE; + + for (p = original; *p; p++) + { + if (!last_underscore && *p == '_') + last_underscore = TRUE; + else + { + last_underscore = FALSE; + *q++ = *p; + } + } + + *q = '\0'; + + return result; +} + +static GtkWidget * +editor_create_item (EggToolbarEditor *editor, + const char *stock_id, + const char *label_text, + GdkDragAction action) +{ + GtkWidget *event_box; + GtkWidget *vbox; + GtkWidget *icon; + GtkWidget *label; + gchar *label_no_mnemonic = NULL; + + event_box = gtk_event_box_new (); + gtk_widget_show (event_box); + gtk_drag_source_set (event_box, + GDK_BUTTON1_MASK, + source_drag_types, n_source_drag_types, action); + g_signal_connect (event_box, "drag_data_get", + G_CALLBACK (drag_data_get_cb), editor); + g_signal_connect (event_box, "drag_data_delete", + G_CALLBACK (editor_drag_data_delete_cb), editor); + + vbox = gtk_vbox_new (0, FALSE); + gtk_widget_show (vbox); + gtk_container_add (GTK_CONTAINER (event_box), vbox); + + icon = gtk_image_new_from_stock + (stock_id ? stock_id : GTK_STOCK_DND, + GTK_ICON_SIZE_LARGE_TOOLBAR); + gtk_widget_show (icon); + gtk_box_pack_start (GTK_BOX (vbox), icon, FALSE, TRUE, 0); + label_no_mnemonic = elide_underscores (label_text); + label = gtk_label_new (label_no_mnemonic); + g_free (label_no_mnemonic); + gtk_widget_show (label); + gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0); + + return event_box; +} + +static void +update_editor_sheet (EggToolbarEditor *editor) +{ + GList *l; + GList *to_drag = editor->priv->actions_list; + int x, y, height, width; + GtkWidget *table; + GtkWidget *viewport; + GtkWidget *item; + + g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor)); + + viewport = GTK_BIN (editor->priv->scrolled_window)->child; + if (viewport) + { + table = GTK_BIN (viewport)->child; + gtk_container_remove (GTK_CONTAINER (viewport), table); + } + table = gtk_table_new (0, 0, TRUE); + editor->priv->table = table; + gtk_container_set_border_width (GTK_CONTAINER (table), 12); + gtk_widget_show (table); + gtk_scrolled_window_add_with_viewport + (GTK_SCROLLED_WINDOW (editor->priv->scrolled_window), table); + gtk_drag_dest_set (table, GTK_DEST_DEFAULT_ALL, + dest_drag_types, n_dest_drag_types, GDK_ACTION_MOVE); + g_signal_connect (table, "drag_data_received", + G_CALLBACK (editor_drag_data_received_cb), editor); + + x = y = 0; + width = 4; + height = (g_list_length (to_drag) - 1) / width + 1; + gtk_table_resize (GTK_TABLE (editor->priv->table), height, width); + + for (l = to_drag; l != NULL; l = l->next) + { + EggAction *action = (l->data); + + item = editor_create_item (editor, action->stock_id, + action->short_label, GDK_ACTION_MOVE); + g_object_set_data (G_OBJECT (item), "egg-action", action); + gtk_table_attach_defaults (GTK_TABLE (editor->priv->table), + item, x, x + 1, y, y + 1); + + x++; + if (x >= width) + { + x = 0; + y++; + } + } + + item = editor_create_item (editor, NULL, _("Separator"), + GDK_ACTION_COPY); + gtk_table_attach_defaults (GTK_TABLE (editor->priv->table), + item, x, x + 1, y, y + 1); +} + +static void +setup_editor (EggToolbarEditor *editor) +{ + GtkWidget *scrolled_window; + GtkWidget *vbox; + GtkWidget *label_hbox; + GtkWidget *image; + GtkWidget *label; + + g_return_if_fail (IS_EGG_TOOLBAR_EDITOR (editor)); + + gtk_dialog_set_has_separator (GTK_DIALOG (editor), FALSE); + gtk_window_set_title (GTK_WINDOW (editor), "Toolbar editor"); + + vbox = gtk_vbox_new (FALSE, 12); + gtk_container_set_border_width (GTK_CONTAINER (vbox), 12); + gtk_widget_show (vbox); + gtk_container_add (GTK_CONTAINER (GTK_DIALOG (editor)->vbox), vbox); + scrolled_window = gtk_scrolled_window_new (NULL, NULL); + editor->priv->scrolled_window = scrolled_window; + gtk_widget_show (scrolled_window); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), + GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); + gtk_box_pack_start (GTK_BOX (vbox), scrolled_window, TRUE, TRUE, 0); + label_hbox = gtk_hbox_new (FALSE, 6); + gtk_widget_show (label_hbox); + gtk_box_pack_start (GTK_BOX (vbox), label_hbox, FALSE, FALSE, 0); + image = + gtk_image_new_from_stock (GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG); + gtk_widget_show (image); + gtk_box_pack_start (GTK_BOX (label_hbox), image, FALSE, FALSE, 0); + label = gtk_label_new (_("Drag an item onto the toolbars above to add it, " + "from the toolbars in the items table to remove it.")); + gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); + gtk_widget_show (label); + gtk_box_pack_start (GTK_BOX (label_hbox), label, FALSE, TRUE, 0); + + gtk_dialog_add_button (GTK_DIALOG (editor), + _("_Add a New Toolbar"), RESPONSE_ADD_TOOLBAR); + gtk_dialog_add_button (GTK_DIALOG (editor), + GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE); + + g_signal_connect (editor, "response", + G_CALLBACK (dialog_response_cb), editor); +} + +static void +egg_toolbar_editor_init (EggToolbarEditor *t) +{ + t->priv = g_new0 (EggToolbarEditorPrivate, 1); + + t->priv->merge = NULL; + t->priv->actions_list = NULL; + + setup_editor (t); +} + +static void +egg_toolbar_editor_add_action (EggToolbarEditor *editor, + const char *action_name) +{ + EggAction *action; + + action = find_action (editor, action_name); + g_return_if_fail (action != NULL); + + editor->priv->actions_list = g_list_append + (editor->priv->actions_list, action); +} + +static void +parse_item_list (EggToolbarEditor *t, + xmlNodePtr child) +{ + while (child) + { + if (xmlStrEqual (child->name, "toolitem")) + { + xmlChar *verb; + + verb = xmlGetProp (child, "verb"); + egg_toolbar_editor_add_action (t, verb); + xmlFree (verb); + } + child = child->next; + } +} + +gboolean +model_has_action (EggToolbarsModel *model, EggAction *action) +{ + int i, l, n_items, n_toolbars; + + n_toolbars = egg_toolbars_model_n_toolbars (model); + for (i = 0; i < n_toolbars; i++) + { + n_items = egg_toolbars_model_n_items (model, i); + for (l = 0; l < n_items; l++) + { + const char *name; + gboolean sep; + + name = egg_toolbars_model_item_nth (model, i, l, &sep); + if (!sep && strcmp (name, action->name) == 0) return TRUE; + } + } + + return FALSE; +} + +void +egg_toolbar_editor_load_actions (EggToolbarEditor *editor, + const char *xml_file) +{ + xmlDocPtr doc; + xmlNodePtr root; + xmlNodePtr child; + GList *l, *tmp; + + doc = xmlParseFile (xml_file); + root = xmlDocGetRootElement (doc); + child = root->children; + + while (child) + { + if (xmlStrEqual (child->name, "available")) + { + parse_item_list (editor, child->children); + } + child = child->next; + } + + xmlFreeDoc (doc); + + /* Remove the already used items */ + tmp = g_list_copy (editor->priv->actions_list); + for (l = editor->priv->actions_list; l != NULL; l = l->next) + { + EggAction *action = EGG_ACTION (l->data); + + if (model_has_action (editor->priv->model, action)) + { + tmp = g_list_remove (tmp, action); + } + } + g_list_free (editor->priv->actions_list); + editor->priv->actions_list = tmp; + + update_editor_sheet (editor); +} diff --git a/lib/egg/egg-toolbar-editor.h b/lib/egg/egg-toolbar-editor.h new file mode 100755 index 000000000..71cdfe31e --- /dev/null +++ b/lib/egg/egg-toolbar-editor.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2003 Marco Pesenti Gritti + * + * 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, 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. + */ + +#ifndef EGG_TOOLBAR_EDITOR_H +#define EGG_TOOLBAR_EDITOR_H + +#include <gtk/gtkdialog.h> + +#include "egg-menu-merge.h" +#include "egg-toolbars-model.h" + +G_BEGIN_DECLS + +typedef struct EggToolbarEditorClass EggToolbarEditorClass; + +#define EGG_TOOLBAR_EDITOR_TYPE (egg_toolbar_editor_get_type ()) +#define EGG_TOOLBAR_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TOOLBAR_EDITOR_TYPE, EggToolbarEditor)) +#define EGG_TOOLBAR_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TOOLBAR_EDITOR_TYPE, EggToolbarEditorClass)) +#define IS_EGG_TOOLBAR_EDITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TOOLBAR_EDITOR_TYPE)) +#define IS_EGG_TOOLBAR_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EGG_TOOLBAR_EDITOR_TYPE)) +#define EGG_TOOLBAR_EDITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EGG_TOOLBAR_EDITOR_TYPE, EggToolbarEditorClass)) + + +typedef struct EggToolbarEditor EggToolbarEditor; +typedef struct EggToolbarEditorPrivate EggToolbarEditorPrivate; + +struct EggToolbarEditor +{ + GtkDialog parent_object; + EggToolbarEditorPrivate *priv; +}; + +struct EggToolbarEditorClass +{ + GtkDialogClass parent_class; +}; + +GType egg_toolbar_editor_get_type (void); +GtkWidget *egg_toolbar_editor_new (EggMenuMerge *merge, + EggToolbarsModel *model); +void egg_toolbar_editor_load_actions (EggToolbarEditor *editor, + const char *xml_file); + +G_END_DECLS + +#endif diff --git a/lib/egg/egg-toolbars-group.c b/lib/egg/egg-toolbars-group.c deleted file mode 100755 index 975456b8e..000000000 --- a/lib/egg/egg-toolbars-group.c +++ /dev/null @@ -1,668 +0,0 @@ -/* - * Copyright (C) 2002 Marco Pesenti Gritti - * - * 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, 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 "egg-toolbars-group.h" - -#include <string.h> - -static void egg_toolbars_group_class_init (EggToolbarsGroupClass *klass); -static void egg_toolbars_group_init (EggToolbarsGroup *t); -static void egg_toolbars_group_finalize (GObject *object); - -enum -{ - CHANGED, - LAST_SIGNAL -}; - -static guint egg_toolbars_group_signals[LAST_SIGNAL] = { 0 }; - -static GObjectClass *parent_class = NULL; - -struct EggToolbarsGroupPrivate -{ - GNode *available_actions; - GNode *toolbars; - char *defaults; - char *user; -}; - -GType -egg_toolbars_group_get_type (void) -{ - static GType egg_toolbars_group_type = 0; - - if (egg_toolbars_group_type == 0) - { - static const GTypeInfo our_info = { - sizeof (EggToolbarsGroupClass), - NULL, /* base_init */ - NULL, /* base_finalize */ - (GClassInitFunc) egg_toolbars_group_class_init, - NULL, - NULL, /* class_data */ - sizeof (EggToolbarsGroup), - 0, /* n_preallocs */ - (GInstanceInitFunc) egg_toolbars_group_init - }; - - egg_toolbars_group_type = g_type_register_static (G_TYPE_OBJECT, - "EggToolbarsGroup", - &our_info, 0); - } - - return egg_toolbars_group_type; - -} - -static xmlDocPtr -egg_toolbars_group_to_xml (EggToolbarsGroup *t) -{ - GNode *l1, *l2, *tl; - xmlDocPtr doc; - - g_return_val_if_fail (IS_EGG_TOOLBARS_GROUP (t), NULL); - - tl = t->priv->toolbars; - - xmlIndentTreeOutput = TRUE; - doc = xmlNewDoc ("1.0"); - doc->children = xmlNewDocNode (doc, NULL, "toolbars", NULL); - - for (l1 = tl->children; l1 != NULL; l1 = l1->next) - { - xmlNodePtr tnode; - - tnode = xmlNewChild (doc->children, NULL, "toolbar", NULL); - - for (l2 = l1->children; l2 != NULL; l2 = l2->next) - { - xmlNodePtr node; - EggToolbarsItem *item = l2->data; - - if (item->separator) - { - node = xmlNewChild (tnode, NULL, "separator", NULL); - } - else - { - node = xmlNewChild (tnode, NULL, "toolitem", NULL); - xmlSetProp (node, "verb", item->action); - } - } - } - - return doc; -} - -static void -toolbars_group_save (EggToolbarsGroup *t) -{ - xmlDocPtr doc; - - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (t)); - - doc = egg_toolbars_group_to_xml (t); - xmlSaveFormatFile (t->priv->user, doc, 1); - xmlFreeDoc (doc); -} - -static EggToolbarsToolbar * -toolbars_toolbar_new (void) -{ - EggToolbarsToolbar *toolbar; - static int id = 0; - - toolbar = g_new0 (EggToolbarsToolbar, 1); - toolbar->id = g_strdup_printf ("Toolbar%d", id); - - id++; - - return toolbar; -} - -static EggToolbarsItem * -toolbars_item_new (const char *action, - gboolean separator) -{ - EggToolbarsItem *item; - static int id = 0; - - g_return_val_if_fail (action != NULL, NULL); - - item = g_new0 (EggToolbarsItem, 1); - item->action = g_strdup (action); - item->separator = separator; - item->id = g_strdup_printf ("TI%d", id); - - id++; - - return item; -} - -static void -free_toolbar_node (EggToolbarsToolbar *toolbar) -{ - g_return_if_fail (toolbar != NULL); - - g_free (toolbar->id); - g_free (toolbar); -} - -static void -free_item_node (EggToolbarsItem *item) -{ - g_return_if_fail (item != NULL); - - g_free (item->action); - g_free (item->id); - g_free (item); -} - -static void -add_action (EggToolbarsGroup *t, - GNode *parent, - int pos, - const char *name) -{ - EggToolbarsItem *item; - GNode *node; - gboolean separator; - - separator = (strcmp (name, "separator") == 0); - item = toolbars_item_new (name, separator); - item->parent = parent->data; - node = g_node_new (item); - - g_node_insert (parent, pos, node); -} - -void -egg_toolbars_group_add_item (EggToolbarsGroup *t, - EggToolbarsToolbar *parent, - int pos, - const char *name) -{ - GNode *parent_node; - - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (t)); - g_return_if_fail (parent != NULL); - g_return_if_fail (name != NULL); - - parent_node = - g_node_find (t->priv->toolbars, G_IN_ORDER, G_TRAVERSE_ALL, parent); - - add_action (t, parent_node, pos, name); - - toolbars_group_save (t); - - g_signal_emit (G_OBJECT (t), egg_toolbars_group_signals[CHANGED], 0); -} - -static void -parse_item_list (EggToolbarsGroup *t, - xmlNodePtr child, - GNode *parent) -{ - while (child) - { - if (xmlStrEqual (child->name, "toolitem")) - { - xmlChar *verb; - - verb = xmlGetProp (child, "verb"); - add_action (t, parent, -1, verb); - - xmlFree (verb); - } - else if (xmlStrEqual (child->name, "separator")) - { - add_action (t, parent, -1, "separator"); - } - - child = child->next; - } -} - -static GNode * -add_toolbar (EggToolbarsGroup *t) -{ - EggToolbarsToolbar *toolbar; - GNode *node; - - toolbar = toolbars_toolbar_new (); - node = g_node_new (toolbar); - g_node_append (t->priv->toolbars, node); - - return node; -} - -EggToolbarsToolbar * -egg_toolbars_group_add_toolbar (EggToolbarsGroup *t) -{ - GNode *node; - - g_return_val_if_fail (IS_EGG_TOOLBARS_GROUP (t), NULL); - - node = add_toolbar (t); - g_return_val_if_fail (node != NULL, NULL); - - toolbars_group_save (t); - - g_signal_emit (G_OBJECT (t), egg_toolbars_group_signals[CHANGED], 0); - - return node->data; -} - -static void -parse_toolbars (EggToolbarsGroup *t, - xmlNodePtr child) -{ - while (child) - { - if (xmlStrEqual (child->name, "toolbar")) - { - GNode *node; - - node = add_toolbar (t); - parse_item_list (t, child->children, node); - } - - child = child->next; - } -} - -static void -load_defaults (EggToolbarsGroup *t) -{ - xmlDocPtr doc; - xmlNodePtr child; - xmlNodePtr root; - const char *xml_filepath; - - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (t)); - - xml_filepath = t->priv->defaults; - - doc = xmlParseFile (xml_filepath); - root = xmlDocGetRootElement (doc); - - child = root->children; - while (child) - { - if (xmlStrEqual (child->name, "available")) - { - t->priv->available_actions = g_node_new (NULL); - parse_item_list (t, child->children, t->priv->available_actions); - } - else if (xmlStrEqual (child->name, "default") && - t->priv->toolbars == NULL) - { - t->priv->toolbars = g_node_new (NULL); - parse_toolbars (t, child->children); - } - - child = child->next; - } - - xmlFreeDoc (doc); -} - -static void -load_toolbar (EggToolbarsGroup *t) -{ - xmlDocPtr doc; - xmlNodePtr root; - const char *xml_filepath = t->priv->user; - - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (t)); - - if (!g_file_test (xml_filepath, G_FILE_TEST_EXISTS)) - return; - - doc = xmlParseFile (xml_filepath); - root = xmlDocGetRootElement (doc); - - t->priv->toolbars = g_node_new (NULL); - parse_toolbars (t, root->children); - - xmlFreeDoc (doc); -} - -char * -egg_toolbars_group_to_string (EggToolbarsGroup *t) -{ - GString *s; - GNode *l1, *l2, *tl; - char *result; - int k = 0; - - g_return_val_if_fail (IS_EGG_TOOLBARS_GROUP (t), NULL); - - tl = t->priv->toolbars; - - g_return_val_if_fail (tl != NULL, NULL); - - s = g_string_new (NULL); - g_string_append (s, "<Root>"); - for (l1 = tl->children; l1 != NULL; l1 = l1->next) - { - int i = 0; - EggToolbarsToolbar *toolbar = l1->data; - - g_string_append_printf (s, "<dockitem name=\"%s\">\n", toolbar->id); - - for (l2 = l1->children; l2 != NULL; l2 = l2->next) - { - EggToolbarsItem *item = l2->data; - - if (item->separator) - { - g_string_append_printf - (s, "<placeholder name=\"PlaceHolder%d-%d\">" - "<separator name=\"%s\"/>" - "</placeholder>\n", i, k, item->id); - } - else - { - g_string_append_printf - (s, "<placeholder name=\"PlaceHolder%d-%d\">" - "<toolitem name=\"%s\" verb=\"%s\"/>" - "</placeholder>\n", i, k, item->id, item->action); - } - i++; - } - - g_string_append (s, "</dockitem>\n"); - - k++; - } - g_string_append (s, "</Root>"); - - result = g_string_free (s, FALSE); - - return result; -} - -static void -egg_toolbars_group_class_init (EggToolbarsGroupClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - parent_class = g_type_class_peek_parent (klass); - - object_class->finalize = egg_toolbars_group_finalize; - - egg_toolbars_group_signals[CHANGED] = - g_signal_new ("changed", - G_OBJECT_CLASS_TYPE (object_class), - G_SIGNAL_RUN_LAST, - G_STRUCT_OFFSET (EggToolbarsGroupClass, changed), - NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); - -} - -static void -egg_toolbars_group_init (EggToolbarsGroup *t) -{ - t->priv = g_new0 (EggToolbarsGroupPrivate, 1); - - t->priv->available_actions = NULL; - t->priv->toolbars = NULL; - t->priv->user = NULL; - t->priv->defaults = NULL; -} - -static void -egg_toolbars_group_finalize (GObject *object) -{ - EggToolbarsGroup *t = EGG_TOOLBARS_GROUP (object); - - g_return_if_fail (object != NULL); - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (object)); - - g_node_children_foreach (t->priv->available_actions, G_IN_ORDER, - (GNodeForeachFunc) free_item_node, NULL); - egg_toolbars_group_foreach_toolbar - (t, (EggToolbarsGroupForeachToolbarFunc) free_toolbar_node, NULL); - egg_toolbars_group_foreach_item - (t, (EggToolbarsGroupForeachItemFunc) free_item_node, NULL); - g_node_destroy (t->priv->available_actions); - g_node_destroy (t->priv->toolbars); - - g_free (t->priv->user); - g_free (t->priv->defaults); - - g_free (t->priv); - - G_OBJECT_CLASS (parent_class)->finalize (object); -} - -EggToolbarsGroup * -egg_toolbars_group_new (void) -{ - EggToolbarsGroup *t; - - t = EGG_TOOLBARS_GROUP (g_object_new (EGG_TOOLBARS_GROUP_TYPE, NULL)); - - g_return_val_if_fail (t->priv != NULL, NULL); - - return t; -} - -static void -remove_action (EggToolbarsItem *item, - gpointer *data) -{ - char *action = data[0]; - EggToolbarsGroup *group = EGG_TOOLBARS_GROUP (data[1]); - - if (strcmp (item->action, action) == 0) - { - egg_toolbars_group_remove_item (group, item); - } -} - -void -egg_toolbars_group_remove_action (EggToolbarsGroup *group, - const char *action) -{ - gpointer data[2]; - data[0] = (char *)action; - data[1] = group; - egg_toolbars_group_foreach_item - (group, (EggToolbarsGroupForeachItemFunc) remove_action, data); - g_signal_emit (G_OBJECT (group), egg_toolbars_group_signals[CHANGED], 0); -} - -void -egg_toolbars_group_remove_toolbar (EggToolbarsGroup *t, - EggToolbarsToolbar *toolbar) -{ - GNode *node; - - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (t)); - g_return_if_fail (toolbar != NULL); - - node = g_node_find (t->priv->toolbars, G_IN_ORDER, G_TRAVERSE_ALL, toolbar); - g_return_if_fail (node != NULL); - free_toolbar_node (node->data); - g_node_destroy (node); - - toolbars_group_save (t); - - g_signal_emit (G_OBJECT (t), egg_toolbars_group_signals[CHANGED], 0); -} - -void -egg_toolbars_group_remove_item (EggToolbarsGroup *t, - EggToolbarsItem *item) -{ - GNode *node; - GNode *toolbar; - - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (t)); - g_return_if_fail (item != NULL); - - node = g_node_find (t->priv->toolbars, G_IN_ORDER, G_TRAVERSE_ALL, item); - g_return_if_fail (node != NULL); - toolbar = node->parent; - free_item_node (node->data); - g_node_destroy (node); - - if (g_node_n_children (toolbar) == 0) - { - free_toolbar_node (toolbar->data); - g_node_destroy (toolbar); - } - - toolbars_group_save (t); - - g_signal_emit (G_OBJECT (t), egg_toolbars_group_signals[CHANGED], 0); -} - -void -egg_toolbars_group_set_source (EggToolbarsGroup *group, - const char *defaults, - const char *user) -{ - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (group)); - g_return_if_fail (defaults != NULL); - - group->priv->defaults = g_strdup (defaults); - group->priv->user = g_strdup (user); - - load_toolbar (group); - load_defaults (group); -} - -static gboolean -is_item_in_toolbars (EggToolbarsGroup *group, - const char *action) -{ - GNode *l1, *l2; - - g_return_val_if_fail (IS_EGG_TOOLBARS_GROUP (group), FALSE); - g_return_val_if_fail (action != NULL, FALSE); - - for (l1 = group->priv->toolbars->children; l1 != NULL; l1 = l1->next) - { - for (l2 = l1->children; l2 != NULL; l2 = l2->next) - { - EggToolbarsItem *item; - - item = (EggToolbarsItem *) l2->data; - if (strcmp (action, item->action) == 0) - return TRUE; - } - } - - return FALSE; -} - -void -egg_toolbars_group_foreach_available (EggToolbarsGroup *group, - EggToolbarsGroupForeachItemFunc func, - gpointer data) -{ - GNode *l1; - - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (group)); - - for (l1 = group->priv->available_actions->children; l1 != NULL; - l1 = l1->next) - { - EggToolbarsItem *item; - - item = (EggToolbarsItem *) l1->data; - - if (!is_item_in_toolbars (group, item->action)) - { - func (item, data); - } - } -} - -void -egg_toolbars_group_foreach_toolbar (EggToolbarsGroup *group, - EggToolbarsGroupForeachToolbarFunc func, - gpointer data) -{ - GNode *l1; - - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (group)); - - for (l1 = group->priv->toolbars->children; l1 != NULL; l1 = l1->next) - { - func (l1->data, data); - } -} - -void -egg_toolbars_group_foreach_item (EggToolbarsGroup *group, - EggToolbarsGroupForeachItemFunc func, - gpointer data) -{ - GNode *l1, *l2; - - g_return_if_fail (IS_EGG_TOOLBARS_GROUP (group)); - - for (l1 = group->priv->toolbars->children; l1 != NULL; l1 = l1->next) - { - for (l2 = l1->children; l2 != NULL; l2 = l2->next) - { - func (l2->data, data); - } - } -} - -char * -egg_toolbars_group_get_path (EggToolbarsGroup *t, - gpointer item) -{ - GNode *node; - char *path = NULL; - EggToolbarsItem *titem; - EggToolbarsToolbar *toolbar; - - g_return_val_if_fail (IS_EGG_TOOLBARS_GROUP (t), NULL); - - node = g_node_find (t->priv->toolbars, G_IN_ORDER, G_TRAVERSE_ALL, item); - g_return_val_if_fail (node != NULL, NULL); - titem = (EggToolbarsItem *) node->data; - toolbar = (EggToolbarsToolbar *) node->data; - - switch (g_node_depth (node)) - { - case 2: - path = g_strdup_printf ("/%s", toolbar->id); - break; - case 3: - path = g_strdup_printf - ("/Toolbar%d/PlaceHolder%d-%d/%s", - g_node_child_position (node->parent->parent, node->parent), - g_node_child_position (node->parent, node), - g_node_child_position (node->parent->parent, node->parent), - titem->id); - break; - default: - g_assert_not_reached (); - } - - return path; -} diff --git a/lib/egg/egg-toolbars-group.h b/lib/egg/egg-toolbars-group.h deleted file mode 100755 index ec4163281..000000000 --- a/lib/egg/egg-toolbars-group.h +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2003 Marco Pesenti Gritti - * - * 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, 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. - */ - -#ifndef EGG_TOOLBARS_GROUP_H -#define EGG_TOOLBARS_GROUP_H - -#include <gtk/gtkwidget.h> -#include <libxml/parser.h> - -G_BEGIN_DECLS - -typedef struct EggToolbarsGroupClass EggToolbarsGroupClass; - -#define EGG_TOOLBARS_GROUP_TYPE (egg_toolbars_group_get_type ()) -#define EGG_TOOLBARS_GROUP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TOOLBARS_GROUP_TYPE, EggToolbarsGroup)) -#define EGG_TOOLBARS_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TOOLBARS_GROUP_TYPE, EggToolbarsGroupClass)) -#define IS_EGG_TOOLBARS_GROUP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TOOLBARS_GROUP_TYPE)) -#define IS_EGG_TOOLBARS_GROUP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EGG_TOOLBARS_GROUP_TYPE)) -#define EGG_TOOLBARS_GROUP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EGG_TOOLBARS_GROUP_TYPE, EggToolbarsGroupClass)) - - -typedef struct EggToolbarsGroup EggToolbarsGroup; -typedef struct EggToolbarsGroupPrivate EggToolbarsGroupPrivate; - -typedef struct -{ - char *id; -} EggToolbarsToolbar; - -typedef struct -{ - char *id; - gboolean separator; - char *action; - EggToolbarsToolbar *parent; -} EggToolbarsItem; - -typedef void (*EggToolbarsGroupForeachToolbarFunc) (EggToolbarsToolbar *toolbar, - gpointer data); -typedef void (*EggToolbarsGroupForeachItemFunc) (EggToolbarsItem *item, - gpointer data); - -struct EggToolbarsGroup -{ - GObject parent_object; - EggToolbarsGroupPrivate *priv; -}; - -struct EggToolbarsGroupClass -{ - GObjectClass parent_class; - - void (*changed) (EggToolbarsGroup * group); -}; - -GType egg_toolbars_group_get_type (void); -EggToolbarsGroup *egg_toolbars_group_new (void); -void egg_toolbars_group_set_source (EggToolbarsGroup *group, - const char *defaults, - const char *user); -void egg_toolbars_group_remove_action (EggToolbarsGroup *group, - const char *action); - -/* These should be used only by editable toolbar */ -EggToolbarsToolbar *egg_toolbars_group_add_toolbar (EggToolbarsGroup *t); -void egg_toolbars_group_add_item (EggToolbarsGroup *t, - EggToolbarsToolbar *parent, - int position, - const char *name); -void egg_toolbars_group_remove_toolbar (EggToolbarsGroup *t, - EggToolbarsToolbar *toolbar); -void egg_toolbars_group_remove_item (EggToolbarsGroup *t, - EggToolbarsItem *item); -void egg_toolbars_group_foreach_available (EggToolbarsGroup *group, - EggToolbarsGroupForeachItemFunc func, - gpointer data); -void egg_toolbars_group_foreach_toolbar (EggToolbarsGroup * group, - EggToolbarsGroupForeachToolbarFunc func, - gpointer data); -void egg_toolbars_group_foreach_item (EggToolbarsGroup *group, - EggToolbarsGroupForeachItemFunc func, - gpointer data); -char *egg_toolbars_group_to_string (EggToolbarsGroup *t); -char *egg_toolbars_group_get_path (EggToolbarsGroup *t, - gpointer item); - -G_END_DECLS -#endif diff --git a/lib/egg/egg-toolbars-model.c b/lib/egg/egg-toolbars-model.c new file mode 100755 index 000000000..cc54e6fb1 --- /dev/null +++ b/lib/egg/egg-toolbars-model.c @@ -0,0 +1,490 @@ +/* + * Copyright (C) 2002 Marco Pesenti Gritti + * + * 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, 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 "egg-toolbars-model.h" +#include "eggmarshalers.h" + +#include <string.h> +#include <libxml/tree.h> + +static void egg_toolbars_model_class_init (EggToolbarsModelClass *klass); +static void egg_toolbars_model_init (EggToolbarsModel *t); +static void egg_toolbars_model_finalize (GObject *object); + +enum +{ + ITEM_ADDED, + ITEM_REMOVED, + TOOLBAR_ADDED, + TOOLBAR_REMOVED, + LAST_SIGNAL +}; + +typedef struct +{ + char *name; +} EggToolbarsToolbar; + +typedef struct +{ + char *action_name; + gboolean separator; +} EggToolbarsItem; + +static guint egg_toolbars_model_signals[LAST_SIGNAL] = { 0 }; + +static GObjectClass *parent_class = NULL; + +struct EggToolbarsModelPrivate +{ + GNode *toolbars; +}; + +GType +egg_toolbars_model_get_type (void) +{ + static GType egg_toolbars_model_type = 0; + + if (egg_toolbars_model_type == 0) + { + static const GTypeInfo our_info = { + sizeof (EggToolbarsModelClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) egg_toolbars_model_class_init, + NULL, + NULL, /* class_data */ + sizeof (EggToolbarsModel), + 0, /* n_preallocs */ + (GInstanceInitFunc) egg_toolbars_model_init + }; + + egg_toolbars_model_type = g_type_register_static (G_TYPE_OBJECT, + "EggToolbarsModel", + &our_info, 0); + } + + return egg_toolbars_model_type; + +} + +static xmlDocPtr +egg_toolbars_model_to_xml (EggToolbarsModel *t) +{ + GNode *l1, *l2, *tl; + xmlDocPtr doc; + + g_return_val_if_fail (IS_EGG_TOOLBARS_MODEL (t), NULL); + + tl = t->priv->toolbars; + + xmlIndentTreeOutput = TRUE; + doc = xmlNewDoc ("1.0"); + doc->children = xmlNewDocNode (doc, NULL, "toolbars", NULL); + + for (l1 = tl->children; l1 != NULL; l1 = l1->next) + { + xmlNodePtr tnode; + EggToolbarsToolbar *toolbar = l1->data; + + tnode = xmlNewChild (doc->children, NULL, "toolbar", NULL); + xmlSetProp (tnode, "name", toolbar->name); + + for (l2 = l1->children; l2 != NULL; l2 = l2->next) + { + xmlNodePtr node; + EggToolbarsItem *item = l2->data; + + if (item->separator) + { + node = xmlNewChild (tnode, NULL, "separator", NULL); + } + else + { + node = xmlNewChild (tnode, NULL, "toolitem", NULL); + xmlSetProp (node, "verb", item->action_name); + } + } + } + + return doc; +} + +void +egg_toolbars_model_save (EggToolbarsModel *t, + const char *xml_file) +{ + xmlDocPtr doc; + + g_return_if_fail (IS_EGG_TOOLBARS_MODEL (t)); + + doc = egg_toolbars_model_to_xml (t); + xmlSaveFormatFile (xml_file, doc, 1); + xmlFreeDoc (doc); +} + +static EggToolbarsToolbar * +toolbars_toolbar_new (const char *name) +{ + EggToolbarsToolbar *toolbar; + + toolbar = g_new0 (EggToolbarsToolbar, 1); + toolbar->name = g_strdup (name); + + return toolbar; +} + +static EggToolbarsItem * +toolbars_item_new (const char *action, + gboolean separator) +{ + EggToolbarsItem *item; + + g_return_val_if_fail (action != NULL, NULL); + + item = g_new0 (EggToolbarsItem, 1); + item->action_name = g_strdup (action); + item->separator = separator; + + return item; +} + +static void +free_toolbar_node (EggToolbarsToolbar *toolbar) +{ + g_return_if_fail (toolbar != NULL); + + g_free (toolbar->name); + g_free (toolbar); +} + +static void +free_item_node (EggToolbarsItem *item) +{ + g_return_if_fail (item != NULL); + + g_free (item->action_name); + g_free (item); +} + +void +egg_toolbars_model_add_separator (EggToolbarsModel *t, + int toolbar_position, + int position) +{ + GNode *parent_node; + GNode *node; + EggToolbarsItem *item; + + g_return_if_fail (IS_EGG_TOOLBARS_MODEL (t)); + + parent_node = g_node_nth_child (t->priv->toolbars, toolbar_position); + item = toolbars_item_new ("separator", TRUE); + node = g_node_new (item); + g_node_insert (parent_node, position, node); + + g_signal_emit (G_OBJECT (t), egg_toolbars_model_signals[ITEM_ADDED], 0, + toolbar_position, position); +} + +const char * +egg_toolbars_model_add_item (EggToolbarsModel *t, + int toolbar_position, + int position, + GdkAtom type, + const char *name) +{ + EggToolbarsModelClass *klass = EGG_TOOLBARS_MODEL_GET_CLASS (t); + return klass->add_item (t, toolbar_position, position, type, name); +} + +const char * +impl_add_item (EggToolbarsModel *t, + int toolbar_position, + int position, + GdkAtom type, + const char *name) +{ + GNode *parent_node; + GNode *node; + EggToolbarsItem *item; + + g_return_val_if_fail (IS_EGG_TOOLBARS_MODEL (t), NULL); + g_return_val_if_fail (name != NULL, NULL); + + parent_node = g_node_nth_child (t->priv->toolbars, toolbar_position); + item = toolbars_item_new (name, FALSE); + node = g_node_new (item); + g_node_insert (parent_node, position, node); + + g_signal_emit (G_OBJECT (t), egg_toolbars_model_signals[ITEM_ADDED], 0, + toolbar_position, position); + + return item->action_name; +} + +static void +parse_item_list (EggToolbarsModel *t, + xmlNodePtr child, + int position) +{ + while (child) + { + if (xmlStrEqual (child->name, "toolitem")) + { + xmlChar *verb; + + verb = xmlGetProp (child, "verb"); + egg_toolbars_model_add_item (t, position, -1, NULL, verb); + xmlFree (verb); + } + else if (xmlStrEqual (child->name, "separator")) + { + egg_toolbars_model_add_separator (t, position, -1); + } + + child = child->next; + } +} + +int +egg_toolbars_model_add_toolbar (EggToolbarsModel *t, const char *name) +{ + GNode *node; + + g_return_val_if_fail (IS_EGG_TOOLBARS_MODEL (t), -1); + + node = g_node_new (toolbars_toolbar_new (name)); + g_node_insert (t->priv->toolbars, -1, node); + + g_signal_emit (G_OBJECT (t), egg_toolbars_model_signals[TOOLBAR_ADDED], 0); + + return g_node_child_position (t->priv->toolbars, node); +} + +static void +parse_toolbars (EggToolbarsModel *t, + xmlNodePtr child) +{ + while (child) + { + if (xmlStrEqual (child->name, "toolbar")) + { + xmlChar *name; + int position; + + name = xmlGetProp (child, "name"); + position = egg_toolbars_model_add_toolbar (t, name); + xmlFree (name); + + parse_item_list (t, child->children, position); + } + + child = child->next; + } +} + +void +egg_toolbars_model_load (EggToolbarsModel *t, + const char *xml_file) +{ + xmlDocPtr doc; + xmlNodePtr root; + + g_return_if_fail (IS_EGG_TOOLBARS_MODEL (t)); + + doc = xmlParseFile (xml_file); + root = xmlDocGetRootElement (doc); + + t->priv->toolbars = g_node_new (NULL); + parse_toolbars (t, root->children); + + xmlFreeDoc (doc); +} + +static void +egg_toolbars_model_class_init (EggToolbarsModelClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + parent_class = g_type_class_peek_parent (klass); + + object_class->finalize = egg_toolbars_model_finalize; + + klass->add_item = impl_add_item; + + egg_toolbars_model_signals[ITEM_ADDED] = + g_signal_new ("item_added", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolbarsModelClass, item_added), + NULL, NULL, _egg_marshal_VOID__INT_INT, + G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT); + egg_toolbars_model_signals[TOOLBAR_ADDED] = + g_signal_new ("toolbar_added", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolbarsModelClass, toolbar_added), + NULL, NULL, g_cclosure_marshal_VOID__INT, + G_TYPE_NONE, 1, G_TYPE_INT); + egg_toolbars_model_signals[ITEM_REMOVED] = + g_signal_new ("item_removed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolbarsModelClass, item_removed), + NULL, NULL, _egg_marshal_VOID__INT_INT, + G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT); + egg_toolbars_model_signals[TOOLBAR_REMOVED] = + g_signal_new ("toolbar_removed", + G_OBJECT_CLASS_TYPE (object_class), + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (EggToolbarsModelClass, toolbar_removed), + NULL, NULL, g_cclosure_marshal_VOID__INT, + G_TYPE_NONE, 1, G_TYPE_INT); +} + +static void +egg_toolbars_model_init (EggToolbarsModel *t) +{ + t->priv = g_new0 (EggToolbarsModelPrivate, 1); + + t->priv->toolbars = NULL; +} + +static void +egg_toolbars_model_finalize (GObject *object) +{ + EggToolbarsModel *t = EGG_TOOLBARS_MODEL (object); + + g_return_if_fail (object != NULL); + g_return_if_fail (IS_EGG_TOOLBARS_MODEL (object)); + + /* FIXME free nodes */ + g_node_destroy (t->priv->toolbars); + + g_free (t->priv); + + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +EggToolbarsModel * +egg_toolbars_model_new (void) +{ + EggToolbarsModel *t; + + t = EGG_TOOLBARS_MODEL (g_object_new (EGG_TOOLBARS_MODEL_TYPE, NULL)); + + g_return_val_if_fail (t->priv != NULL, NULL); + + return t; +} + +void +egg_toolbars_model_remove_toolbar (EggToolbarsModel *t, + int position) +{ + GNode *node; + + g_return_if_fail (IS_EGG_TOOLBARS_MODEL (t)); + + node = g_node_nth_child (t->priv->toolbars, position); + g_return_if_fail (node != NULL); + + free_toolbar_node (node->data); + g_node_destroy (node); + + g_signal_emit (G_OBJECT (t), egg_toolbars_model_signals[TOOLBAR_REMOVED], 0, position); +} + +void +egg_toolbars_model_remove_item (EggToolbarsModel *t, + int toolbar_position, + int position) +{ + GNode *node, *toolbar; + + g_return_if_fail (IS_EGG_TOOLBARS_MODEL (t)); + + toolbar = g_node_nth_child (t->priv->toolbars, toolbar_position); + g_return_if_fail (toolbar != NULL); + + node = g_node_nth_child (toolbar, position); + g_return_if_fail (node != NULL); + + free_item_node (node->data); + g_node_destroy (node); + + g_signal_emit (G_OBJECT (t), egg_toolbars_model_signals[ITEM_REMOVED], 0, + toolbar_position, position); +} + +int +egg_toolbars_model_n_items (EggToolbarsModel *t, + int toolbar_position) +{ + GNode *toolbar; + + toolbar = g_node_nth_child (t->priv->toolbars, toolbar_position); + g_return_val_if_fail (toolbar != NULL, -1); + + return g_node_n_children (toolbar); +} + +const char * +egg_toolbars_model_item_nth (EggToolbarsModel *t, + int toolbar_position, + int position, + gboolean *is_separator) +{ + GNode *toolbar; + GNode *item; + EggToolbarsItem *idata; + + toolbar = g_node_nth_child (t->priv->toolbars, toolbar_position); + g_return_val_if_fail (toolbar != NULL, NULL); + + item = g_node_nth_child (toolbar, position); + g_return_val_if_fail (item != NULL, NULL); + + idata = item->data; + + *is_separator = idata->separator; + + return idata->action_name; +} + +int +egg_toolbars_model_n_toolbars (EggToolbarsModel *t) +{ + return g_node_n_children (t->priv->toolbars); +} + +const char * +egg_toolbars_model_toolbar_nth (EggToolbarsModel *t, + int position) +{ + GNode *toolbar; + EggToolbarsToolbar *tdata; + + toolbar = g_node_nth_child (t->priv->toolbars, position); + g_return_val_if_fail (toolbar != NULL, NULL); + + tdata = toolbar->data; + + return tdata->name; +} diff --git a/lib/egg/egg-toolbars-model.h b/lib/egg/egg-toolbars-model.h new file mode 100755 index 000000000..16e3865a0 --- /dev/null +++ b/lib/egg/egg-toolbars-model.h @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2003 Marco Pesenti Gritti + * + * 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, 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. + */ + +#ifndef EGG_TOOLBARS_MODEL_H +#define EGG_TOOLBARS_MODEL_H + +#include <glib.h> +#include <glib-object.h> +#include "gdk/gdktypes.h" + +G_BEGIN_DECLS + +typedef struct EggToolbarsModelClass EggToolbarsModelClass; + +#define EGG_TOOLBARS_MODEL_TYPE (egg_toolbars_model_get_type ()) +#define EGG_TOOLBARS_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TOOLBARS_MODEL_TYPE, EggToolbarsModel)) +#define EGG_TOOLBARS_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TOOLBARS_MODEL_TYPE, EggToolbarsModelClass)) +#define IS_EGG_TOOLBARS_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TOOLBARS_MODEL_TYPE)) +#define IS_EGG_TOOLBARS_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EGG_TOOLBARS_MODEL_TYPE)) +#define EGG_TOOLBARS_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EGG_TOOLBARS_MODEL_TYPE, EggToolbarsModelClass)) + +typedef struct EggToolbarsModel EggToolbarsModel; +typedef struct EggToolbarsModelPrivate EggToolbarsModelPrivate; + +struct EggToolbarsModel +{ + GObject parent_object; + EggToolbarsModelPrivate *priv; +}; + +struct EggToolbarsModelClass +{ + GObjectClass parent_class; + + /* Signals */ + void (* item_added) (EggToolbarsModel *group, + int toolbar_position, + int position); + void (* item_removed) (EggToolbarsModel *group, + int toolbar_position, + int position); + void (* toolbar_added) (EggToolbarsModel *group, + int position); + void (* toolbar_removed) (EggToolbarsModel *group, + int position); + + /* Virtual Table */ + const char * (* add_item) (EggToolbarsModel *t, + int toolbar_position, + int position, + GdkAtom type, + const char *item_name); + +}; + +GType egg_toolbars_model_get_type (void); +EggToolbarsModel *egg_toolbars_model_new (void); +void egg_toolbars_model_load (EggToolbarsModel *t, + const char *xml_file); +void egg_toolbars_model_save (EggToolbarsModel *t, + const char *xml_file); +int egg_toolbars_model_add_toolbar (EggToolbarsModel *t, + const char *name); +void egg_toolbars_model_add_separator (EggToolbarsModel *t, + int toolbar_position, + int position); +const char *egg_toolbars_model_add_item (EggToolbarsModel *t, + int toolbar_position, + int position, + GdkAtom type, + const char *item_name); +void egg_toolbars_model_remove_toolbar (EggToolbarsModel *t, + int position); +void egg_toolbars_model_remove_item (EggToolbarsModel *t, + int toolbar_position, + int position); +int egg_toolbars_model_n_items (EggToolbarsModel *t, + int toolbar_position); +const char *egg_toolbars_model_item_nth (EggToolbarsModel *t, + int toolbar_position, + int position, + gboolean *is_separator); +int egg_toolbars_model_n_toolbars (EggToolbarsModel *t); +const char *egg_toolbars_model_toolbar_nth (EggToolbarsModel *t, + int position); + +G_END_DECLS + +#endif |