1 #define __SP_ACTION_C__
3 /** \file
4 * SPAction implementation
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2003 Lauris Kaplinski
10 *
11 * This code is in public domain
12 */
14 #include <string.h>
17 #include "helper/action.h"
19 static void sp_action_class_init (SPActionClass *klass);
20 static void sp_action_init (SPAction *action);
21 static void sp_action_finalize (NRObject *object);
23 static NRActiveObjectClass *parent_class;
25 /**
26 * Register SPAction class and return its type.
27 */
28 NRType
29 sp_action_get_type (void)
30 {
31 static unsigned int type = 0;
32 if (!type) {
33 type = nr_object_register_type (NR_TYPE_ACTIVE_OBJECT,
34 "SPAction",
35 sizeof (SPActionClass),
36 sizeof (SPAction),
37 (void (*) (NRObjectClass *)) sp_action_class_init,
38 (void (*) (NRObject *)) sp_action_init);
39 }
40 return type;
41 }
43 /**
44 * SPAction vtable initialization.
45 */
46 static void
47 sp_action_class_init (SPActionClass *klass)
48 {
49 NRObjectClass * object_class;
51 object_class = (NRObjectClass *) klass;
53 parent_class = (NRActiveObjectClass *) (((NRObjectClass *) klass)->parent);
55 object_class->finalize = sp_action_finalize;
56 object_class->cpp_ctor = NRObject::invoke_ctor<SPAction>;
57 }
59 /**
60 * Callback for SPAction object initialization.
61 */
62 static void
63 sp_action_init (SPAction *action)
64 {
65 action->sensitive = 0;
66 action->active = 0;
67 action->view = NULL;
68 action->id = action->name = action->tip = NULL;
69 action->image = NULL;
70 }
72 /**
73 * Called before SPAction object destruction.
74 */
75 static void
76 sp_action_finalize (NRObject *object)
77 {
78 SPAction *action;
80 action = (SPAction *) object;
82 if (action->image) free (action->image);
83 if (action->tip) free (action->tip);
84 if (action->name) free (action->name);
85 if (action->id) free (action->id);
87 ((NRObjectClass *) (parent_class))->finalize (object);
88 }
90 /**
91 * Create new SPAction object and set its properties.
92 */
93 SPAction *
94 sp_action_new(Inkscape::UI::View::View *view,
95 const gchar *id,
96 const gchar *name,
97 const gchar *tip,
98 const gchar *image,
99 Inkscape::Verb * verb)
100 {
101 SPAction *action = (SPAction *)nr_object_new(SP_TYPE_ACTION);
103 action->view = view;
104 action->sensitive = TRUE;
105 if (id) action->id = strdup (id);
106 if (name) action->name = strdup (name);
107 if (tip) action->tip = strdup (tip);
108 if (image) action->image = strdup (image);
109 action->verb = verb;
111 return action;
112 }
114 /**
115 \return None
116 \brief Executes an action
117 \param action The action to be executed
118 \param data Data that is passed into the action. This depends
119 on the situation that the action is used in.
121 This function implements the 'action' in SPActions. It first validates
122 its parameters, making sure it got an action passed in. Then it
123 turns that action into its parent class of NRActiveObject. The
124 NRActiveObject allows for listeners to be attached to it. This
125 function goes through those listeners and calls them with the
126 vector that was attached to the listener.
127 */
128 void
129 sp_action_perform (SPAction *action, void * data)
130 {
131 NRActiveObject *aobject;
133 nr_return_if_fail (action != NULL);
134 nr_return_if_fail (SP_IS_ACTION (action));
136 aobject = NR_ACTIVE_OBJECT(action);
137 if (aobject->callbacks) {
138 unsigned int i;
139 for (i = 0; i < aobject->callbacks->length; i++) {
140 NRObjectListener *listener;
141 SPActionEventVector *avector;
143 listener = &aobject->callbacks->listeners[i];
144 avector = (SPActionEventVector *) listener->vector;
146 if ((listener->size >= sizeof (SPActionEventVector)) && avector != NULL && avector->perform != NULL) {
147 avector->perform (action, listener->data, data);
148 }
149 }
150 }
151 }
153 /**
154 * Change activation in all actions that can be taken with the action.
155 */
156 void
157 sp_action_set_active (SPAction *action, unsigned int active)
158 {
159 nr_return_if_fail (action != NULL);
160 nr_return_if_fail (SP_IS_ACTION (action));
162 if (active != action->active) {
163 NRActiveObject *aobject;
164 action->active = active;
165 aobject = (NRActiveObject *) action;
166 if (aobject->callbacks) {
167 unsigned int i;
168 for (i = 0; i < aobject->callbacks->length; i++) {
169 NRObjectListener *listener;
170 SPActionEventVector *avector;
171 listener = aobject->callbacks->listeners + i;
172 avector = (SPActionEventVector *) listener->vector;
173 if ((listener->size >= sizeof (SPActionEventVector)) && avector->set_active) {
174 avector->set_active (action, active, listener->data);
175 }
176 }
177 }
178 }
179 }
181 /**
182 * Change sensitivity in all actions that can be taken with the action.
183 */
184 void
185 sp_action_set_sensitive (SPAction *action, unsigned int sensitive)
186 {
187 nr_return_if_fail (action != NULL);
188 nr_return_if_fail (SP_IS_ACTION (action));
190 if (sensitive != action->sensitive) {
191 NRActiveObject *aobject;
192 action->sensitive = sensitive;
193 aobject = (NRActiveObject *) action;
194 if (aobject->callbacks) {
195 unsigned int i;
196 for (i = 0; i < aobject->callbacks->length; i++) {
197 NRObjectListener *listener;
198 SPActionEventVector *avector;
199 listener = aobject->callbacks->listeners + i;
200 avector = (SPActionEventVector *) listener->vector;
201 if ((listener->size >= sizeof (SPActionEventVector)) && avector->set_sensitive) {
202 avector->set_sensitive (action, sensitive, listener->data);
203 }
204 }
205 }
206 }
207 }
209 /**
210 * Return View associated with the action.
211 */
212 Inkscape::UI::View::View *
213 sp_action_get_view (SPAction *action)
214 {
215 g_return_val_if_fail (SP_IS_ACTION (action), NULL);
216 return action->view;
217 }
219 /*
220 Local Variables:
221 mode:c++
222 c-file-style:"stroustrup"
223 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
224 indent-tabs-mode:nil
225 fill-column:99
226 End:
227 */
228 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :