Merge branch 'ntd'
[adg.git] / adg / adg-context.c
blob0be0aefab08431c4a1f94da7fce7043c3865aea9
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2008, 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.
20 /**
21 * SECTION:context
22 * @title: AdgContext
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 * AdgContextFiller:
41 * @style_class: the new style class
42 * @user_data: the user provided data
44 * Callback to be used to get the default instance for
45 * newly registered style classes.
47 * Return value: the instance to use for the passed in style class
48 **/
50 #include "adg-context.h"
51 #include "adg-context-private.h"
52 #include "adg-intl.h"
54 #define PARENT_CLASS ((AdgEntityClass *) adg_context_parent_class)
57 static AdgStyle * context_filler (AdgStyleClass *style_class,
58 gpointer user_data);
59 static void fill_style_slots (AdgContext *context,
60 guint last_slot);
62 static GPtrArray *class_slots = NULL;
65 G_DEFINE_TYPE(AdgContext, adg_context, G_TYPE_OBJECT)
68 static void
69 adg_context_class_init(AdgContextClass *klass)
71 g_type_class_add_private(klass, sizeof(AdgContextPrivate));
74 static void
75 adg_context_init(AdgContext *context)
77 AdgContextPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE(context,
78 ADG_TYPE_CONTEXT,
79 AdgContextPrivate);
81 priv->style_slots = g_ptr_array_sized_new(class_slots ?
82 class_slots->len : 10);
83 priv->context_filler = context_filler;
84 priv->user_data = NULL;
86 context->priv = priv;
90 /**
91 * adg_context_get_slot:
92 * @type: an #AdgStyle type
94 * Gets the slot associated to @type. If not found, registers a new
95 * style family by assigning a new slot id to it: the internal register
96 * will keep a reference to this style class.
98 * Return value: the requested slot id
99 **/
100 AdgStyleSlot
101 adg_context_get_slot(GType type)
103 AdgStyleClass *klass = g_type_class_ref(type);
105 g_return_val_if_fail(ADG_IS_STYLE_CLASS(klass), -1);
107 if (G_UNLIKELY(class_slots == NULL)) {
108 class_slots = g_ptr_array_sized_new(10);
109 } else {
110 guint n;
111 for (n = 0; n < class_slots->len; ++n)
112 if (class_slots->pdata[n] == klass)
113 return n;
116 g_ptr_array_add(class_slots, klass);
117 return class_slots->len - 1;
121 * adg_context_new:
122 * @context_filler: callback to use to fill uninitialized styles
123 * @user_data: additional pointer to pass to @context_filler
125 * Constructs a new empty context. If @context_filler is %NULL, the default
126 * filler callback will be used (the new style instances will be %NULL).
128 * Return value: a new context
130 AdgContext *
131 adg_context_new(AdgContextFiller context_filler, gpointer user_data)
133 AdgContext *context = g_object_new(ADG_TYPE_CONTEXT, NULL);
135 if (context_filler) {
136 context->priv->context_filler = context_filler;
137 context->priv->user_data = user_data;
140 return context;
144 * adg_context_get_style:
145 * @context: an #AdgContext instance
146 * @slot: the style slot where to get the style
148 * Gets a style instance from the specified @slot of @context.
150 * Return value: the style instance
152 AdgStyle *
153 adg_context_get_style(AdgContext *context, AdgStyleSlot slot)
155 GPtrArray *style_slots;
157 g_return_val_if_fail(ADG_IS_CONTEXT(context), NULL);
158 g_return_val_if_fail(slot >= 0 && slot < class_slots->len, NULL);
160 style_slots = context->priv->style_slots;
162 if (G_UNLIKELY(slot >= style_slots->len))
163 fill_style_slots(context, slot);
165 return (AdgStyle *) g_ptr_array_index(style_slots, slot);
169 * adg_context_set_style:
170 * @context: an #AdgContext instance
171 * @style: the new style to include
173 * Sets a new style inside @context. The old style (if any)
174 * will be unreferenced while a new reference will be added to @style.
176 void
177 adg_context_set_style(AdgContext *context, AdgStyle *style)
179 AdgStyleSlot slot;
180 GPtrArray *style_slots;
182 g_return_if_fail(ADG_IS_CONTEXT(context));
183 g_return_if_fail(ADG_IS_STYLE(style));
185 slot = adg_context_get_slot(G_TYPE_FROM_INSTANCE(style));
186 style_slots = context->priv->style_slots;
188 g_object_ref(style);
190 if (G_LIKELY(slot < style_slots->len)) {
191 g_object_unref(style_slots->pdata[slot]);
192 style_slots->pdata[slot] = style;
193 } else {
194 fill_style_slots(context, slot - 1);
195 g_ptr_array_add(style_slots, style);
200 static AdgStyle *
201 context_filler(AdgStyleClass *style_class, gpointer user_data)
203 return NULL;
206 static void
207 fill_style_slots(AdgContext *context, guint last_slot)
209 GPtrArray *style_slots;
210 AdgStyleClass *klass;
211 AdgStyle *style;
212 guint n;
214 style_slots = context->priv->style_slots;
216 for (n = style_slots->len; n <= last_slot; ++n) {
217 klass = (AdgStyleClass *) g_ptr_array_index(class_slots, n);
218 style =
219 context->priv->context_filler(klass, context->priv->user_data);
220 g_object_ref((GObject *) style);
221 g_ptr_array_add(style_slots, style);