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.
42 * All fields are private and should not be used directly.
43 * Use its public methods instead.
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 * Returns: the instance to use for the passed in style class
58 #include "adg-context.h"
59 #include "adg-context-private.h"
63 static AdgStyle
* context_filler (AdgStyleClass
*style_class
,
65 static void fill_style_slots (AdgContext
*context
,
68 static GPtrArray
*class_slots
= NULL
;
71 G_DEFINE_TYPE(AdgContext
, adg_context
, G_TYPE_OBJECT
);
75 adg_context_class_init(AdgContextClass
*klass
)
77 g_type_class_add_private(klass
, sizeof(AdgContextPrivate
));
81 adg_context_init(AdgContext
*context
)
83 AdgContextPrivate
*data
= G_TYPE_INSTANCE_GET_PRIVATE(context
,
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
;
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 * Returns: the requested slot id
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);
117 for (n
= 0; n
< class_slots
->len
; ++n
)
118 if (class_slots
->pdata
[n
] == klass
)
122 g_ptr_array_add(class_slots
, klass
);
123 return class_slots
->len
- 1;
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 * Returns: a new context
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
;
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 * Returns: the style instance
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.
187 adg_context_set_style(AdgContext
*context
, AdgStyle
*style
)
189 AdgContextPrivate
*data
;
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
;
202 if (G_LIKELY(slot
< style_slots
->len
)) {
203 g_object_unref(style_slots
->pdata
[slot
]);
204 style_slots
->pdata
[slot
] = style
;
206 fill_style_slots(context
, slot
- 1);
207 g_ptr_array_add(style_slots
, style
);
213 context_filler(AdgStyleClass
*style_class
, gpointer user_data
)
219 fill_style_slots(AdgContext
*context
, guint last_slot
)
221 AdgContextPrivate
*data
;
222 GPtrArray
*style_slots
;
223 AdgStyleClass
*klass
;
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
);