From caf06ff4e8800ff69eee59b6cf3c277f8463bb47 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Mon, 31 Jan 2011 21:58:41 +0100 Subject: Introduce LdUndoAction. --- CMakeLists.txt | 2 + liblogdiag/ld-undo-action.c | 128 ++++++++++++++++++++++++++++++++++++++++++++ liblogdiag/ld-undo-action.h | 76 ++++++++++++++++++++++++++ liblogdiag/liblogdiag.h | 1 + 4 files changed, 207 insertions(+) create mode 100644 liblogdiag/ld-undo-action.c create mode 100644 liblogdiag/ld-undo-action.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7fa70ac..4c83c36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -110,6 +110,7 @@ set (project_DOC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/docs/reference") set (liblogdiag_SOURCES liblogdiag/ld-marshal.c liblogdiag/ld-types.c + liblogdiag/ld-undo-action.c liblogdiag/ld-diagram.c liblogdiag/ld-diagram-object.c liblogdiag/ld-diagram-symbol.c @@ -125,6 +126,7 @@ set (liblogdiag_HEADERS liblogdiag/liblogdiag.h liblogdiag/ld-marshal.h liblogdiag/ld-types.h + liblogdiag/ld-undo-action.h liblogdiag/ld-diagram.h liblogdiag/ld-diagram-object.h liblogdiag/ld-diagram-symbol.h diff --git a/liblogdiag/ld-undo-action.c b/liblogdiag/ld-undo-action.c new file mode 100644 index 0000000..924a03e --- /dev/null +++ b/liblogdiag/ld-undo-action.c @@ -0,0 +1,128 @@ +/* + * ld-undo-action.c + * + * This file is a part of logdiag. + * Copyright Přemysl Janouch 2011. All rights reserved. + * + * See the file LICENSE for licensing information. + * + */ + +#include + +#include "liblogdiag.h" +#include "config.h" + + +/** + * SECTION:ld-undo-action + * @short_description: An action that can be undone. + * @see_also: #LdDiagram, #LdDiagramObject + * + * #LdUndoAction represents an action that can be reverted. + */ + +/* + * LdUndoActionPrivate: + * @undo_func: a callback to undo the action. + * @redo_func: a callback to redo the action. + * @destroy_func: a callback to destroy user data. + * @user_data: data given by the user. + */ +struct _LdUndoActionPrivate +{ + LdUndoActionFunc undo_func; + LdUndoActionFunc redo_func; + LdUndoActionFunc destroy_func; + gpointer user_data; +}; + +static void ld_undo_action_finalize (GObject *gobject); + + +G_DEFINE_TYPE (LdUndoAction, ld_undo_action, G_TYPE_OBJECT); + +static void +ld_undo_action_class_init (LdUndoActionClass *klass) +{ + GObjectClass *object_class; + + object_class = G_OBJECT_CLASS (klass); + object_class->finalize = ld_undo_action_finalize; + + g_type_class_add_private (klass, sizeof (LdUndoActionPrivate)); +} + +static void +ld_undo_action_init (LdUndoAction *self) +{ + self->priv = G_TYPE_INSTANCE_GET_PRIVATE + (self, LD_TYPE_UNDO_ACTION, LdUndoActionPrivate); +} + +static void +ld_undo_action_finalize (GObject *gobject) +{ + LdUndoAction *self; + + self = LD_UNDO_ACTION (gobject); + if (self->priv->destroy_func) + self->priv->destroy_func (self->priv->user_data); + + /* Chain up to the parent class. */ + G_OBJECT_CLASS (ld_undo_action_parent_class)->finalize (gobject); +} + + +/** + * ld_undo_action_new: + * @undo_func: a callback to undo the action. + * @redo_func: a callback to redo the action. + * @destroy_func: (allow-none): a callback to destroy user data. + * @user_data: user data passed to callbacks. + * + * Return value: a new #LdUndoAction object. + */ +LdUndoAction * +ld_undo_action_new (LdUndoActionFunc undo_func, + LdUndoActionFunc redo_func, LdUndoActionFunc destroy_func, + gpointer user_data) +{ + LdUndoAction *self; + + g_return_val_if_fail (undo_func != NULL, NULL); + g_return_val_if_fail (redo_func != NULL, NULL); + + self = g_object_new (LD_TYPE_UNDO_ACTION, NULL); + self->priv->undo_func = undo_func; + self->priv->redo_func = redo_func; + self->priv->destroy_func = destroy_func; + self->priv->user_data = user_data; + return self; +} + +/** + * ld_undo_action_undo: + * @self: an #LdUndoAction object. + * + * Undo the action handled by this object. + */ +void +ld_undo_action_undo (LdUndoAction *self) +{ + g_return_if_fail (LD_IS_UNDO_ACTION (self)); + self->priv->undo_func (self->priv->user_data); +} + +/** + * ld_undo_action_redo: + * @self: an #LdUndoAction object. + * + * Redo the action handled by this object. + */ +void +ld_undo_action_redo (LdUndoAction *self) +{ + g_return_if_fail (LD_IS_UNDO_ACTION (self)); + self->priv->redo_func (self->priv->user_data); +} diff --git a/liblogdiag/ld-undo-action.h b/liblogdiag/ld-undo-action.h new file mode 100644 index 0000000..141b255 --- /dev/null +++ b/liblogdiag/ld-undo-action.h @@ -0,0 +1,76 @@ +/* + * ld-undo-action.h + * + * This file is a part of logdiag. + * Copyright Přemysl Janouch 2011. All rights reserved. + * + * See the file LICENSE for licensing information. + * + */ + +#ifndef __LD_UNDO_ACTION_H__ +#define __LD_UNDO_ACTION_H__ + +G_BEGIN_DECLS + + +#define LD_TYPE_UNDO_ACTION (ld_undo_action_get_type ()) +#define LD_UNDO_ACTION(obj) (G_TYPE_CHECK_INSTANCE_CAST \ + ((obj), LD_TYPE_UNDO_ACTION, LdUndoAction)) +#define LD_UNDO_ACTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST \ + ((klass), LD_TYPE_UNDO_ACTION, LdUndoActionClass)) +#define LD_IS_UNDO_ACTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE \ + ((obj), LD_TYPE_UNDO_ACTION)) +#define LD_IS_UNDO_ACTION_CLASS(klass) (G_TYPE_CHECK_INSTANCE_TYPE \ + ((klass), LD_TYPE_UNDO_ACTION)) +#define LD_UNDO_ACTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS \ + ((obj), LD_UNDO_ACTION, LdUndoActionClass)) + +typedef struct _LdUndoAction LdUndoAction; +typedef struct _LdUndoActionPrivate LdUndoActionPrivate; +typedef struct _LdUndoActionClass LdUndoActionClass; + + +/** + * LdUndoAction: + */ +struct _LdUndoAction +{ +/*< private >*/ + GObject parent_instance; + LdUndoActionPrivate *priv; +}; + +/** + * LdUndoActionClass: + */ +struct _LdUndoActionClass +{ +/*< private >*/ + GObjectClass parent_class; +}; + + +/** + * LdUndoActionFunc: + * @user_data: user data passed to ld_undo_action_new(). + * + * A callback function prototype for actions. + */ +typedef void (*LdUndoActionFunc) (gpointer user_data); + + +GType ld_undo_action_get_type (void) G_GNUC_CONST; + +/* TODO: Extend the methods (with eg. a string description). */ +LdUndoAction *ld_undo_action_new (LdUndoActionFunc undo_func, + LdUndoActionFunc redo_func, LdUndoActionFunc destroy_func, + gpointer user_data); +void ld_undo_action_undo (LdUndoAction *self); +void ld_undo_action_redo (LdUndoAction *self); + + +G_END_DECLS + +#endif /* ! __LD_UNDO_ACTION_H__ */ + diff --git a/liblogdiag/liblogdiag.h b/liblogdiag/liblogdiag.h index 61b6149..769662e 100644 --- a/liblogdiag/liblogdiag.h +++ b/liblogdiag/liblogdiag.h @@ -21,6 +21,7 @@ #include "ld-symbol-category.h" #include "ld-library.h" +#include "ld-undo-action.h" #include "ld-diagram-object.h" #include "ld-diagram-symbol.h" #include "ld-diagram.h" -- cgit v1.2.3-70-g09d2