[docs] Updated TODO.xml
[adg.git] / adg / adg-context.c
blob6392b11814294efd016f4c17d978cc9ac12726a9
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-context
23 * @short_description: Base class for styling the rendering process
25 * The context is a collection of styles, one for each #AdgStyle derived
26 * class. To achieve this result, there is a common register where all
27 * the style derived class are stored and every #AdgContext instance keeps
28 * its own register of style instances (one instance per class).
30 * Although this could be implemented by accessing the underlying registers
31 * using GType id, to be able to access efficiently, that is O(1) magnitude,
32 * the concept of slot was introduced.
34 * The slot serves the same purpose than a GType, that is identify a type
35 * class, but the slots are a strict sequence starting from 0, useful to be
36 * used as an array index.
37 **/
39 /**
40 * AdgContext:
42 * All fields are private and should not be used directly.
43 * Use its public methods instead.
44 **/
46 /**
47 * AdgContextFiller:
48 * @style_class: the new style class
49 * @user_data: the user provided data
51 * Callback to be used to get the default instance for
52 * newly registered style classes.
54 * Return value: the instance to use for the passed in style class
55 **/
58 #include "adg-context.h"
59 #include "adg-context-private.h"
60 #include "adg-intl.h"
63 static AdgStyle * context_filler (AdgStyleClass *style_class,
64 gpointer user_data);
65 static void fill_style_slots (AdgContext *context,
66 guint last_slot);
68 static GPtrArray *class_slots = NULL;
71 G_DEFINE_TYPE(AdgContext, adg_context, G_TYPE_OBJECT);
74 static void
75 adg_context_class_init(AdgContextClass *klass)
77 g_type_class_add_private(klass, sizeof(AdgContextPrivate));
80 static void
81 adg_context_init(AdgContext *context)
83 AdgContextPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(context,
84 ADG_TYPE_CONTEXT,
85 AdgContextPrivate);
87 data->style_slots = g_ptr_array_sized_new(class_slots ?
88 class_slots->len : 10);
89 data->context_filler = context_filler;
90 data->user_data = NULL;
92 context->data = data;
96 /**
97 * adg_context_get_slot:
98 * @type: an #AdgStyle type
100 * Gets the slot associated to @type. If not found, registers a new
101 * style family by assigning a new slot id to it: the internal register
102 * will keep a reference to this style class.
104 * Return value: the requested slot id
106 AdgStyleSlot
107 adg_context_get_slot(GType type)
109 AdgStyleClass *klass = g_type_class_ref(type);
111 g_return_val_if_fail(ADG_IS_STYLE_CLASS(klass), -1);
113 if (G_UNLIKELY(class_slots == NULL)) {
114 class_slots = g_ptr_array_sized_new(10);
115 } else {
116 guint n;
117 for (n = 0; n < class_slots->len; ++n)
118 if (class_slots->pdata[n] == klass)
119 return n;
122 g_ptr_array_add(class_slots, klass);
123 return class_slots->len - 1;
127 * adg_context_new:
128 * @context_filler: callback to use to fill uninitialized styles
129 * @user_data: additional pointer to pass to @context_filler
131 * Constructs a new empty context. If @context_filler is %NULL, the default
132 * filler callback will be used (the new style instances will be %NULL).
134 * Return value: a new context
136 AdgContext *
137 adg_context_new(AdgContextFiller context_filler, gpointer user_data)
139 AdgContext *context = g_object_new(ADG_TYPE_CONTEXT, NULL);
141 if (context_filler) {
142 AdgContextPrivate *data = context->data;
144 data->context_filler = context_filler;
145 data->user_data = user_data;
148 return context;
152 * adg_context_get_style:
153 * @context: an #AdgContext instance
154 * @slot: the style slot where to get the style
156 * Gets a style instance from the specified @slot of @context.
158 * Return value: the style instance
160 AdgStyle *
161 adg_context_get_style(AdgContext *context, AdgStyleSlot slot)
163 AdgContextPrivate *data;
164 GPtrArray *style_slots;
166 g_return_val_if_fail(ADG_IS_CONTEXT(context), NULL);
167 g_return_val_if_fail(slot >= 0 && slot < class_slots->len, NULL);
169 data = context->data;
170 style_slots = data->style_slots;
172 if (G_UNLIKELY(slot >= style_slots->len))
173 fill_style_slots(context, slot);
175 return (AdgStyle *) g_ptr_array_index(style_slots, slot);
179 * adg_context_set_style:
180 * @context: an #AdgContext instance
181 * @style: the new style to include
183 * Sets a new style inside @context. The old style (if any)
184 * will be unreferenced while a new reference will be added to @style.
186 void
187 adg_context_set_style(AdgContext *context, AdgStyle *style)
189 AdgContextPrivate *data;
190 AdgStyleSlot slot;
191 GPtrArray *style_slots;
193 g_return_if_fail(ADG_IS_CONTEXT(context));
194 g_return_if_fail(ADG_IS_STYLE(style));
196 data = context->data;
197 slot = adg_context_get_slot(G_TYPE_FROM_INSTANCE(style));
198 style_slots = data->style_slots;
200 g_object_ref(style);
202 if (G_LIKELY(slot < style_slots->len)) {
203 g_object_unref(style_slots->pdata[slot]);
204 style_slots->pdata[slot] = style;
205 } else {
206 fill_style_slots(context, slot - 1);
207 g_ptr_array_add(style_slots, style);
212 static AdgStyle *
213 context_filler(AdgStyleClass *style_class, gpointer user_data)
215 return NULL;
218 static void
219 fill_style_slots(AdgContext *context, guint last_slot)
221 AdgContextPrivate *data;
222 GPtrArray *style_slots;
223 AdgStyleClass *klass;
224 AdgStyle *style;
225 guint n;
227 data = context->data;
228 style_slots = data->style_slots;
230 for (n = style_slots->len; n <= last_slot; ++n) {
231 klass = (AdgStyleClass *) g_ptr_array_index(class_slots, n);
232 style = data->context_filler(klass, data->user_data);
233 g_object_ref((GObject *) style);
234 g_ptr_array_add(style_slots, style);