[ADG] Moved include dependencies from .h to .c
[adg.git] / src / adg / adg-title-block.c
blob2632329e1ade5302fa9745022a6c82c6b5b43f93
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009,2010,2011 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-title-block
23 * @short_description: A title block entity
25 * Title blocks are commonly used in technical drawings to include
26 * additional information not strictly related to physical dimensions,
27 * such as title, material of the represented part, special treatments,
28 * date and scale etc.
30 * Actually this entity is only a place-holder: it will be implemented
31 * properly in a 0.6.x release, after having AdgToyTable in place.
32 **/
34 /**
35 * AdgTitleBlock:
37 * All fields are privates and should not be used directly.
38 * Use its public methods instead.
39 **/
42 #include "adg-internal.h"
43 #include "adg-table.h"
45 #include "adg-title-block.h"
46 #include "adg-title-block-private.h"
49 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_title_block_parent_class)
52 G_DEFINE_TYPE(AdgTitleBlock, adg_title_block, ADG_TYPE_TABLE);
54 enum {
55 PROP_0,
56 PROP_TITLE,
57 PROP_DRAWING,
58 PROP_SIZE,
59 PROP_SCALE,
60 PROP_AUTHOR,
61 PROP_DATE,
62 PROP_LOGO,
63 PROP_PROJECTION
67 static void _adg_finalize (GObject *object);
68 static void _adg_get_property (GObject *object,
69 guint prop_id,
70 GValue *value,
71 GParamSpec *pspec);
72 static void _adg_set_property (GObject *object,
73 guint prop_id,
74 const GValue *value,
75 GParamSpec *pspec);
76 static AdgTable * _adg_get_table (AdgTitleBlock *title_block);
79 static void
80 adg_title_block_class_init(AdgTitleBlockClass *klass)
82 GObjectClass *gobject_class;
83 GParamSpec *param;
85 gobject_class = (GObjectClass *) klass;
87 g_type_class_add_private(klass, sizeof(AdgTitleBlockPrivate));
89 gobject_class->finalize = _adg_finalize;
90 gobject_class->set_property = _adg_set_property;
91 gobject_class->get_property = _adg_get_property;
93 param = g_param_spec_string("title",
94 P_("Title"),
95 P_("A descriptive title of the drawing"),
96 NULL,
97 G_PARAM_READWRITE);
98 g_object_class_install_property(gobject_class, PROP_TITLE, param);
100 param = g_param_spec_string("drawing",
101 P_("Drawing Name"),
102 P_("The name of the drawing: the ADG canvas does not make any assumtpion on this text string"),
103 NULL,
104 G_PARAM_READWRITE);
105 g_object_class_install_property(gobject_class, PROP_DRAWING, param);
107 param = g_param_spec_string("size",
108 P_("Media Size"),
109 P_("The media size to be used to print the drawing, usually something like \"A3\" or \"Letter\""),
110 NULL,
111 G_PARAM_READWRITE);
112 g_object_class_install_property(gobject_class, PROP_SIZE, param);
114 param = g_param_spec_string("scale",
115 P_("Scale"),
116 P_("The scale of the drawing, if it makes sense"),
117 NULL,
118 G_PARAM_READWRITE);
119 g_object_class_install_property(gobject_class, PROP_SCALE, param);
121 param = g_param_spec_string("author",
122 P_("Author"),
123 P_("Name and last name of the author of the drawing"),
124 NULL,
125 G_PARAM_READWRITE);
126 g_object_class_install_property(gobject_class, PROP_AUTHOR, param);
128 param = g_param_spec_string("date",
129 P_("Date"),
130 P_("The date this drawing has been generated: setting it to an empty string will fallback to today in the preferred representation for the current locale"),
131 NULL,
132 G_PARAM_READWRITE);
133 g_object_class_install_property(gobject_class, PROP_DATE, param);
135 param = g_param_spec_object("logo",
136 P_("Logo"),
137 P_("An entity to be displayed in the title block as the logo of the owner: the containing cell has a 1:1 ratio"),
138 ADG_TYPE_ENTITY,
139 G_PARAM_READWRITE);
140 g_object_class_install_property(gobject_class, PROP_LOGO, param);
142 param = g_param_spec_object("projection",
143 P_("Projection Scheme"),
144 P_("The entity usually reserved to identify the projection scheme adopted by this drawing"),
145 ADG_TYPE_ENTITY,
146 G_PARAM_READWRITE);
147 g_object_class_install_property(gobject_class, PROP_PROJECTION, param);
150 static void
151 adg_title_block_init(AdgTitleBlock *title_block)
153 AdgTitleBlockPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(title_block,
154 ADG_TYPE_TITLE_BLOCK,
155 AdgTitleBlockPrivate);
156 data->author = NULL;
157 data->title = NULL;
158 data->drawing = NULL;
159 data->size = NULL;
160 data->scale = NULL;
161 data->author = NULL;
162 data->date = NULL;
163 data->projection = NULL;
165 title_block->data = data;
168 static void
169 _adg_finalize(GObject *object)
171 AdgTitleBlockPrivate *data = ((AdgTitleBlock *) object)->data;
173 g_free(data->title);
174 g_free(data->drawing);
175 g_free(data->size);
176 g_free(data->scale);
177 g_free(data->author);
178 g_free(data->date);
180 if (_ADG_OLD_OBJECT_CLASS->finalize)
181 _ADG_OLD_OBJECT_CLASS->finalize(object);
184 static void
185 _adg_get_property(GObject *object, guint prop_id,
186 GValue *value, GParamSpec *pspec)
188 AdgTitleBlockPrivate *data = ((AdgTitleBlock *) object)->data;
190 switch (prop_id) {
191 case PROP_TITLE:
192 g_value_set_string(value, data->title);
193 break;
194 case PROP_DRAWING:
195 g_value_set_string(value, data->drawing);
196 break;
197 case PROP_SIZE:
198 g_value_set_string(value, data->size);
199 break;
200 case PROP_SCALE:
201 g_value_set_string(value, data->scale);
202 break;
203 case PROP_AUTHOR:
204 g_value_set_string(value, data->author);
205 break;
206 case PROP_DATE:
207 g_value_set_string(value, data->date);
208 break;
209 case PROP_LOGO:
210 g_value_set_object(value, data->logo);
211 break;
212 case PROP_PROJECTION:
213 g_value_set_object(value, data->projection);
214 break;
215 default:
216 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
217 break;
221 static void
222 _adg_set_property(GObject *object, guint prop_id,
223 const GValue *value, GParamSpec *pspec)
225 AdgTitleBlock *title_block;
226 AdgTitleBlockPrivate *data;
227 AdgTable *table;
228 AdgTableCell *cell;
230 title_block = (AdgTitleBlock *) object;
231 data = title_block->data;
232 table = _adg_get_table(title_block);
234 switch (prop_id) {
235 case PROP_TITLE:
236 g_free(data->title);
237 data->title = g_value_dup_string(value);
238 cell = adg_table_cell(table, "title");
239 adg_table_cell_set_text_value(cell, data->title);
240 break;
241 case PROP_DRAWING:
242 g_free(data->drawing);
243 data->drawing = g_value_dup_string(value);
244 cell = adg_table_cell(table, "drawing");
245 adg_table_cell_set_text_value(cell, data->drawing);
246 break;
247 case PROP_SIZE:
248 g_free(data->size);
249 data->size = g_value_dup_string(value);
250 cell = adg_table_cell(table, "size");
251 adg_table_cell_set_text_value(cell, data->size);
252 break;
253 case PROP_SCALE:
254 g_free(data->scale);
255 data->scale = g_value_dup_string(value);
256 cell = adg_table_cell(table, "scale");
257 adg_table_cell_set_text_value(cell, data->scale);
258 break;
259 case PROP_AUTHOR:
260 g_free(data->author);
261 data->author = g_value_dup_string(value);
262 cell = adg_table_cell(table, "author");
263 adg_table_cell_set_text_value(cell, data->author);
264 break;
265 case PROP_DATE:
266 g_free(data->date);
267 if (g_value_get_string(value) == NULL) {
268 /* NULL means the date must be automatically updated */
269 GDate *gdate;
270 char buffer[100] = { 0 };
272 gdate = g_date_new();
273 g_date_set_time_t(gdate, time (NULL));
274 g_date_strftime(buffer, sizeof(buffer), "%x", gdate);
275 g_date_free(gdate);
277 data->date = g_strdup(buffer);
278 } else {
279 data->date = g_value_dup_string(value);
281 cell = adg_table_cell(table, "date");
282 adg_table_cell_set_text_value(cell, data->date);
283 break;
284 case PROP_LOGO:
285 data->logo = g_value_get_object(value);
286 cell = adg_table_cell(table, "logo");
287 adg_table_cell_set_value(cell, data->logo);
288 adg_table_cell_set_value_pos_explicit(cell, 0.5, 1, 0.5, 0.5);
289 break;
290 case PROP_PROJECTION:
291 data->projection = g_value_get_object(value);
292 cell = adg_table_cell(table, "projection");
293 adg_table_cell_set_value(cell, data->projection);
294 adg_table_cell_set_value_pos_explicit(cell, 0.5, 0.5, 0.5, 0.5);
295 break;
296 default:
297 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
298 break;
304 * adg_title_block_new:
306 * Creates a new empty title block entity. The #AdgEntity:local-method
307 * property is set by default to #ADG_MIX_DISABLED, that is the
308 * title block is not subject to any local transformations.
310 * Returns: the newly created title block entity
312 AdgTitleBlock *
313 adg_title_block_new(void)
315 return g_object_new(ADG_TYPE_TITLE_BLOCK,
316 "local-method", ADG_MIX_DISABLED, NULL);
320 * adg_title_block_set_title:
321 * @title_block: an #AdgTitleBlock entity
322 * @title: the new title
324 * Sets a new title on the title block.
326 void
327 adg_title_block_set_title(AdgTitleBlock *title_block, const gchar *title)
329 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
330 g_object_set(title_block, "title", title, NULL);
334 * adg_title_block_get_title:
335 * @title_block: an #AdgTitleBlock entity
337 * Gets the descriptive title associated to this title block.
338 * The returned string is owned by @title_block and should not
339 * be modifed or freed.
341 * Returns: the title or %NULL on no title or errors
343 const gchar *
344 adg_title_block_get_title(AdgTitleBlock *title_block)
346 AdgTitleBlockPrivate *data;
348 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
350 data = title_block->data;
352 return data->title;
356 * adg_title_block_set_drawing:
357 * @title_block: an #AdgTitleBlock entity
358 * @drawing: the new drawing name
360 * Sets a new drawing name on the title block.
362 void
363 adg_title_block_set_drawing(AdgTitleBlock *title_block, const gchar *drawing)
365 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
366 g_object_set(title_block, "drawing", drawing, NULL);
370 * adg_title_block_get_drawing:
371 * @title_block: an #AdgTitleBlock entity
373 * Gets the drawing name, commonly used to specify the file name.
374 * The returned string is owned by @title_block and should not
375 * be modifed or freed.
377 * Returns: the drawing name or %NULL on no name or errors
379 const gchar *
380 adg_title_block_get_drawing(AdgTitleBlock *title_block)
382 AdgTitleBlockPrivate *data;
384 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
386 data = title_block->data;
388 return data->drawing;
392 * adg_title_block_set_size:
393 * @title_block: an #AdgTitleBlock entity
394 * @size: the new size
396 * Sets a new size on the title block.
398 void
399 adg_title_block_set_size(AdgTitleBlock *title_block, const gchar *size)
401 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
402 g_object_set(title_block, "size", size, NULL);
406 * adg_title_block_get_size:
407 * @title_block: an #AdgTitleBlock entity
409 * Gets the media size (a descriptive name) where this drawing will
410 * be printed. Usually contains something like "A4" or "Letter".
411 * The returned string is owned by @title_block and should not
412 * be modifed or freed.
414 * Returns: the size or %NULL on no size or errors
416 const gchar *
417 adg_title_block_get_size(AdgTitleBlock *title_block)
419 AdgTitleBlockPrivate *data;
421 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
423 data = title_block->data;
425 return data->size;
429 * adg_title_block_set_scale:
430 * @title_block: an #AdgTitleBlock entity
431 * @scale: the new scale
433 * Sets a new scale on the title block.
435 void
436 adg_title_block_set_scale(AdgTitleBlock *title_block, const gchar *scale)
438 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
439 g_object_set(title_block, "scale", scale, NULL);
443 * adg_title_block_get_scale:
444 * @title_block: an #AdgTitleBlock entity
446 * Gets the scale descriptive name of the drawing.
448 * Returns: the scale text or %NULL on no scale or errors
450 const gchar *
451 adg_title_block_get_scale(AdgTitleBlock *title_block)
453 AdgTitleBlockPrivate *data;
455 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
457 data = title_block->data;
459 return data->scale;
463 * adg_title_block_set_author:
464 * @title_block: an #AdgTitleBlock entity
465 * @author: the new author
467 * Sets a new author on the title block.
469 void
470 adg_title_block_set_author(AdgTitleBlock *title_block, const gchar *author)
472 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
473 g_object_set(title_block, "author", author, NULL);
477 * adg_title_block_get_author:
478 * @title_block: an #AdgTitleBlock entity
480 * Gets the author's name of the drawing.
482 * Returns: the author or %NULL on no author or errors
484 const gchar *
485 adg_title_block_get_author(AdgTitleBlock *title_block)
487 AdgTitleBlockPrivate *data;
489 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
491 data = title_block->data;
493 return data->author;
497 * adg_title_block_set_date:
498 * @title_block: an #AdgTitleBlock entity
499 * @date: the new date
501 * Sets a new date on the title block. By default the date is
502 * set to %NULL and it will be implicitely rendered using the
503 * preferred representation for the current local of the actual
504 * date. This is roughly equivalent to:
506 * |[
507 * strftime(buffer, sizeof(buffer), "%x", now);
508 * adg_title_block_set_date(title_block, buffer);
509 * ]|
511 * To not render any value, use an empty string as @date.
513 void
514 adg_title_block_set_date(AdgTitleBlock *title_block, const gchar *date)
516 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
517 g_object_set(title_block, "date", date, NULL);
521 * adg_title_block_get_date:
522 * @title_block: an #AdgTitleBlock entity
524 * Gets the date of the rendering set on @title_block.
526 * Returns: the date or %NULL on no date or errors
528 const gchar *
529 adg_title_block_get_date(AdgTitleBlock *title_block)
531 AdgTitleBlockPrivate *data;
533 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
535 data = title_block->data;
537 return data->date;
541 * adg_title_block_set_logo:
542 * @title_block: an #AdgTitleBlock entity
543 * @logo: the new logo
545 * Sets a new logo on the title block. This function will add
546 * a reference to @logo, removing the eventual reference held
547 * to the old logo, hence possibly destroying the old endity.
549 * The space reserved for the logo is 56x56, so try to keep the
550 * new logo near this size or scale it accordingly.
552 void
553 adg_title_block_set_logo(AdgTitleBlock *title_block, AdgEntity *logo)
555 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
556 g_object_set(title_block, "logo", logo, NULL);
560 * adg_title_block_logo:
561 * @title_block: an #AdgTitleBlock entity
563 * Gets the logo bound to this title block.
564 * The returned object is owned by @title_block and should not
565 * be unreferenced although can be freely modified.
567 * Returns: the logo or %NULL on no logo or errors
569 AdgEntity *
570 adg_title_block_logo(AdgTitleBlock *title_block)
572 AdgTitleBlockPrivate *data;
574 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
576 data = title_block->data;
578 return data->logo;
582 * adg_title_block_set_projection:
583 * @title_block: an #AdgTitleBlock entity
584 * @projection: the new projection
586 * Sets a new projection symbol on the title block. This function
587 * will add a reference to @projection, removing the eventual
588 * reference held to the old symbol, hence possibly destroying
589 * the old endity.
591 * The space reserved for the projection is 56x56, so try to keep the
592 * new projection near this size or scale it accordingly.
594 void
595 adg_title_block_set_projection(AdgTitleBlock *title_block,
596 AdgEntity *projection)
598 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
599 g_object_set(title_block, "projection", projection, NULL);
603 * adg_title_block_projection:
604 * @title_block: an #AdgTitleBlock entity
606 * Gets the projection bound to this title block.
607 * The returned object is owned by @title_block and should not
608 * be unreferenced although can be freely modified.
610 * Returns: the projection or %NULL on no projection or errors
612 AdgEntity *
613 adg_title_block_projection(AdgTitleBlock *title_block)
615 AdgTitleBlockPrivate *data;
617 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
619 data = title_block->data;
621 return data->projection;
625 static AdgTable *
626 _adg_get_table(AdgTitleBlock *title_block)
628 AdgTable *table = (AdgTable *) title_block;
630 if (adg_table_get_n_rows(table) == 0) {
631 AdgTableRow *row;
632 AdgTableCell *cell;
634 /* First row */
635 row = adg_table_row_new(table);
637 cell = adg_table_cell_new(row, 62);
639 cell = adg_table_cell_new(row, 200);
640 adg_table_cell_set_name(cell, "title");
641 adg_table_cell_set_text_title(cell, _("TITLE"));
642 adg_table_cell_switch_frame(cell, TRUE);
644 /* Second row */
645 row = adg_table_row_new(table);
647 cell = adg_table_cell_new(row, 62);
648 adg_table_cell_set_name(cell, "logo");
650 cell = adg_table_cell_new(row, 40);
651 adg_table_cell_set_name(cell, "size");
652 adg_table_cell_set_text_title(cell, _("SIZE"));
653 adg_table_cell_switch_frame(cell, TRUE);
655 cell = adg_table_cell_new(row, 60);
656 adg_table_cell_set_name(cell, "scale");
657 adg_table_cell_set_text_title(cell, _("SCALE"));
658 adg_table_cell_switch_frame(cell, TRUE);
660 cell = adg_table_cell_new(row, 100);
661 adg_table_cell_set_name(cell, "drawing");
662 adg_table_cell_set_text_title(cell, _("DRAWING"));
663 adg_table_cell_switch_frame(cell, TRUE);
665 /* Third row */
666 row = adg_table_row_new(table);
668 cell = adg_table_cell_new(row, 62);
669 adg_table_cell_set_name(cell, "projection");
670 adg_table_cell_switch_frame(cell, TRUE);
672 cell = adg_table_cell_new(row, 100);
673 adg_table_cell_set_name(cell, "author");
674 adg_table_cell_set_text_title(cell, _("AUTHOR"));
675 adg_table_cell_switch_frame(cell, TRUE);
677 cell = adg_table_cell_new(row, 100);
678 adg_table_cell_set_name(cell, "date");
679 adg_table_cell_set_text_title(cell, _("DATE"));
680 adg_table_cell_switch_frame(cell, TRUE);
683 return table;