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.
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.
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
50 #include "adg-context.h"
51 #include "adg-context-private.h"
55 static AdgStyle
* context_filler (AdgStyleClass
*style_class
,
57 static void fill_style_slots (AdgContext
*context
,
60 static GPtrArray
*class_slots
= NULL
;
63 G_DEFINE_TYPE(AdgContext
, adg_context
, G_TYPE_OBJECT
)
67 adg_context_class_init(AdgContextClass
*klass
)
69 g_type_class_add_private(klass
, sizeof(AdgContextPrivate
));
73 adg_context_init(AdgContext
*context
)
75 AdgContextPrivate
*priv
= G_TYPE_INSTANCE_GET_PRIVATE(context
,
79 priv
->style_slots
= g_ptr_array_sized_new(class_slots
?
80 class_slots
->len
: 10);
81 priv
->context_filler
= context_filler
;
82 priv
->user_data
= NULL
;
89 * adg_context_get_slot:
90 * @type: an #AdgStyle type
92 * Gets the slot associated to @type. If not found, registers a new
93 * style family by assigning a new slot id to it: the internal register
94 * will keep a reference to this style class.
96 * Return value: the requested slot id
99 adg_context_get_slot(GType type
)
101 AdgStyleClass
*klass
= g_type_class_ref(type
);
103 g_return_val_if_fail(ADG_IS_STYLE_CLASS(klass
), -1);
105 if (G_UNLIKELY(class_slots
== NULL
)) {
106 class_slots
= g_ptr_array_sized_new(10);
109 for (n
= 0; n
< class_slots
->len
; ++n
)
110 if (class_slots
->pdata
[n
] == klass
)
114 g_ptr_array_add(class_slots
, klass
);
115 return class_slots
->len
- 1;
120 * @context_filler: callback to use to fill uninitialized styles
121 * @user_data: additional pointer to pass to @context_filler
123 * Constructs a new empty context. If @context_filler is %NULL, the default
124 * filler callback will be used (the new style instances will be %NULL).
126 * Return value: a new context
129 adg_context_new(AdgContextFiller context_filler
, gpointer user_data
)
131 AdgContext
*context
= g_object_new(ADG_TYPE_CONTEXT
, NULL
);
133 if (context_filler
) {
134 context
->priv
->context_filler
= context_filler
;
135 context
->priv
->user_data
= user_data
;
142 * adg_context_get_style:
143 * @context: an #AdgContext instance
144 * @slot: the style slot where to get the style
146 * Gets a style instance from the specified @slot of @context.
148 * Return value: the style instance
151 adg_context_get_style(AdgContext
*context
, AdgStyleSlot slot
)
153 GPtrArray
*style_slots
;
155 g_return_val_if_fail(ADG_IS_CONTEXT(context
), NULL
);
156 g_return_val_if_fail(slot
>= 0 && slot
< class_slots
->len
, NULL
);
158 style_slots
= context
->priv
->style_slots
;
160 if (G_UNLIKELY(slot
>= style_slots
->len
))
161 fill_style_slots(context
, slot
);
163 return (AdgStyle
*) g_ptr_array_index(style_slots
, slot
);
167 * adg_context_set_style:
168 * @context: an #AdgContext instance
169 * @style: the new style to include
171 * Sets a new style inside @context. The old style (if any)
172 * will be unreferenced while a new reference will be added to @style.
175 adg_context_set_style(AdgContext
*context
, AdgStyle
*style
)
178 GPtrArray
*style_slots
;
180 g_return_if_fail(ADG_IS_CONTEXT(context
));
181 g_return_if_fail(ADG_IS_STYLE(style
));
183 slot
= adg_context_get_slot(G_TYPE_FROM_INSTANCE(style
));
184 style_slots
= context
->priv
->style_slots
;
188 if (G_LIKELY(slot
< style_slots
->len
)) {
189 g_object_unref(style_slots
->pdata
[slot
]);
190 style_slots
->pdata
[slot
] = style
;
192 fill_style_slots(context
, slot
- 1);
193 g_ptr_array_add(style_slots
, style
);
199 context_filler(AdgStyleClass
*style_class
, gpointer user_data
)
205 fill_style_slots(AdgContext
*context
, guint last_slot
)
207 GPtrArray
*style_slots
;
208 AdgStyleClass
*klass
;
212 style_slots
= context
->priv
->style_slots
;
214 for (n
= style_slots
->len
; n
<= last_slot
; ++n
) {
215 klass
= (AdgStyleClass
*) g_ptr_array_index(class_slots
, n
);
217 context
->priv
->context_filler(klass
, context
->priv
->user_data
);
218 g_object_ref((GObject
*) style
);
219 g_ptr_array_add(style_slots
, style
);