ADG: corrected g-ir-scanner warnings where possible
[adg.git] / src / adg / adg-title-block.c
blobe984e8590769ffac1053aa479a75a4b3fc86af5a
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.
33 * Since: 1.0
34 **/
36 /**
37 * AdgTitleBlock:
39 * All fields are privates and should not be used directly.
40 * Use its public methods instead.
42 * Since: 1.0
43 **/
46 #include "adg-internal.h"
47 #include "adg-table.h"
49 #include "adg-title-block.h"
50 #include "adg-title-block-private.h"
53 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_title_block_parent_class)
56 G_DEFINE_TYPE(AdgTitleBlock, adg_title_block, ADG_TYPE_TABLE)
58 enum {
59 PROP_0,
60 PROP_TITLE,
61 PROP_DRAWING,
62 PROP_SIZE,
63 PROP_SCALE,
64 PROP_AUTHOR,
65 PROP_DATE,
66 PROP_LOGO,
67 PROP_PROJECTION
71 static void _adg_finalize (GObject *object);
72 static void _adg_get_property (GObject *object,
73 guint prop_id,
74 GValue *value,
75 GParamSpec *pspec);
76 static void _adg_set_property (GObject *object,
77 guint prop_id,
78 const GValue *value,
79 GParamSpec *pspec);
80 static AdgTable * _adg_get_table (AdgTitleBlock *title_block);
83 static void
84 adg_title_block_class_init(AdgTitleBlockClass *klass)
86 GObjectClass *gobject_class;
87 GParamSpec *param;
89 gobject_class = (GObjectClass *) klass;
91 g_type_class_add_private(klass, sizeof(AdgTitleBlockPrivate));
93 gobject_class->finalize = _adg_finalize;
94 gobject_class->set_property = _adg_set_property;
95 gobject_class->get_property = _adg_get_property;
97 param = g_param_spec_string("title",
98 P_("Title"),
99 P_("A descriptive title of the drawing"),
100 NULL,
101 G_PARAM_READWRITE);
102 g_object_class_install_property(gobject_class, PROP_TITLE, param);
104 param = g_param_spec_string("drawing",
105 P_("Drawing Name"),
106 P_("The name of the drawing: the ADG canvas does not make any assumtpion on this text string"),
107 NULL,
108 G_PARAM_READWRITE);
109 g_object_class_install_property(gobject_class, PROP_DRAWING, param);
111 param = g_param_spec_string("size",
112 P_("Media Size"),
113 P_("The media size to be used to print the drawing, usually something like \"A3\" or \"Letter\""),
114 NULL,
115 G_PARAM_READWRITE);
116 g_object_class_install_property(gobject_class, PROP_SIZE, param);
118 param = g_param_spec_string("scale",
119 P_("Scale"),
120 P_("The scale of the drawing, if it makes sense"),
121 NULL,
122 G_PARAM_READWRITE);
123 g_object_class_install_property(gobject_class, PROP_SCALE, param);
125 param = g_param_spec_string("author",
126 P_("Author"),
127 P_("Name and last name of the author of the drawing"),
128 NULL,
129 G_PARAM_READWRITE);
130 g_object_class_install_property(gobject_class, PROP_AUTHOR, param);
132 param = g_param_spec_string("date",
133 P_("Date"),
134 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"),
135 NULL,
136 G_PARAM_READWRITE);
137 g_object_class_install_property(gobject_class, PROP_DATE, param);
139 param = g_param_spec_object("logo",
140 P_("Logo"),
141 P_("An entity to be displayed in the title block as the logo of the owner: the containing cell has a 1:1 ratio"),
142 ADG_TYPE_ENTITY,
143 G_PARAM_READWRITE);
144 g_object_class_install_property(gobject_class, PROP_LOGO, param);
146 param = g_param_spec_object("projection",
147 P_("Projection Scheme"),
148 P_("The entity usually reserved to identify the projection scheme adopted by this drawing"),
149 ADG_TYPE_ENTITY,
150 G_PARAM_READWRITE);
151 g_object_class_install_property(gobject_class, PROP_PROJECTION, param);
154 static void
155 adg_title_block_init(AdgTitleBlock *title_block)
157 AdgTitleBlockPrivate *data = G_TYPE_INSTANCE_GET_PRIVATE(title_block,
158 ADG_TYPE_TITLE_BLOCK,
159 AdgTitleBlockPrivate);
160 data->author = NULL;
161 data->title = NULL;
162 data->drawing = NULL;
163 data->size = NULL;
164 data->scale = NULL;
165 data->author = NULL;
166 data->date = NULL;
167 data->projection = NULL;
169 title_block->data = data;
172 static void
173 _adg_finalize(GObject *object)
175 AdgTitleBlockPrivate *data = ((AdgTitleBlock *) object)->data;
177 g_free(data->title);
178 g_free(data->drawing);
179 g_free(data->size);
180 g_free(data->scale);
181 g_free(data->author);
182 g_free(data->date);
184 if (_ADG_OLD_OBJECT_CLASS->finalize)
185 _ADG_OLD_OBJECT_CLASS->finalize(object);
188 static void
189 _adg_get_property(GObject *object, guint prop_id,
190 GValue *value, GParamSpec *pspec)
192 AdgTitleBlockPrivate *data = ((AdgTitleBlock *) object)->data;
194 switch (prop_id) {
195 case PROP_TITLE:
196 g_value_set_string(value, data->title);
197 break;
198 case PROP_DRAWING:
199 g_value_set_string(value, data->drawing);
200 break;
201 case PROP_SIZE:
202 g_value_set_string(value, data->size);
203 break;
204 case PROP_SCALE:
205 g_value_set_string(value, data->scale);
206 break;
207 case PROP_AUTHOR:
208 g_value_set_string(value, data->author);
209 break;
210 case PROP_DATE:
211 g_value_set_string(value, data->date);
212 break;
213 case PROP_LOGO:
214 g_value_set_object(value, data->logo);
215 break;
216 case PROP_PROJECTION:
217 g_value_set_object(value, data->projection);
218 break;
219 default:
220 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
221 break;
225 static void
226 _adg_set_property(GObject *object, guint prop_id,
227 const GValue *value, GParamSpec *pspec)
229 AdgTitleBlock *title_block;
230 AdgTitleBlockPrivate *data;
231 AdgTable *table;
232 AdgTableCell *cell;
234 title_block = (AdgTitleBlock *) object;
235 data = title_block->data;
236 table = _adg_get_table(title_block);
238 switch (prop_id) {
239 case PROP_TITLE:
240 g_free(data->title);
241 data->title = g_value_dup_string(value);
242 cell = adg_table_cell(table, "title");
243 adg_table_cell_set_text_value(cell, data->title);
244 break;
245 case PROP_DRAWING:
246 g_free(data->drawing);
247 data->drawing = g_value_dup_string(value);
248 cell = adg_table_cell(table, "drawing");
249 adg_table_cell_set_text_value(cell, data->drawing);
250 break;
251 case PROP_SIZE:
252 g_free(data->size);
253 data->size = g_value_dup_string(value);
254 cell = adg_table_cell(table, "size");
255 adg_table_cell_set_text_value(cell, data->size);
256 break;
257 case PROP_SCALE:
258 g_free(data->scale);
259 data->scale = g_value_dup_string(value);
260 cell = adg_table_cell(table, "scale");
261 adg_table_cell_set_text_value(cell, data->scale);
262 break;
263 case PROP_AUTHOR:
264 g_free(data->author);
265 data->author = g_value_dup_string(value);
266 cell = adg_table_cell(table, "author");
267 adg_table_cell_set_text_value(cell, data->author);
268 break;
269 case PROP_DATE:
270 g_free(data->date);
271 if (g_value_get_string(value) == NULL) {
272 /* NULL means the date must be automatically updated */
273 GDate *gdate;
274 char buffer[100] = { 0 };
276 gdate = g_date_new();
277 g_date_set_time_t(gdate, time (NULL));
278 g_date_strftime(buffer, sizeof(buffer), "%x", gdate);
279 g_date_free(gdate);
281 data->date = g_strdup(buffer);
282 } else {
283 data->date = g_value_dup_string(value);
285 cell = adg_table_cell(table, "date");
286 adg_table_cell_set_text_value(cell, data->date);
287 break;
288 case PROP_LOGO:
289 data->logo = g_value_get_object(value);
290 cell = adg_table_cell(table, "logo");
291 adg_table_cell_set_value(cell, data->logo);
292 adg_table_cell_set_value_pos_explicit(cell, 0.5, 1, 0.5, 0.5);
293 break;
294 case PROP_PROJECTION:
295 data->projection = g_value_get_object(value);
296 cell = adg_table_cell(table, "projection");
297 adg_table_cell_set_value(cell, data->projection);
298 adg_table_cell_set_value_pos_explicit(cell, 0.5, 0.5, 0.5, 0.5);
299 break;
300 default:
301 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
302 break;
308 * adg_title_block_new:
310 * Creates a new empty title block entity. The #AdgEntity:local-method
311 * property is set by default to #ADG_MIX_DISABLED, that is the
312 * title block is not subject to any local transformations.
314 * Returns: (transfer full): the newly created title block entity.
316 * Since: 1.0
318 AdgTitleBlock *
319 adg_title_block_new(void)
321 return g_object_new(ADG_TYPE_TITLE_BLOCK,
322 "local-method", ADG_MIX_DISABLED, NULL);
326 * adg_title_block_set_title:
327 * @title_block: an #AdgTitleBlock entity
328 * @title: the new title
330 * Sets a new title on the title block.
332 * Since: 1.0
334 void
335 adg_title_block_set_title(AdgTitleBlock *title_block, const gchar *title)
337 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
338 g_object_set(title_block, "title", title, NULL);
342 * adg_title_block_get_title:
343 * @title_block: an #AdgTitleBlock entity
345 * Gets the descriptive title associated to this title block.
346 * The returned string is owned by @title_block and should not
347 * be modifed or freed.
349 * Returns: (transfer none): the title or %NULL on no title or errors.
351 * Since: 1.0
353 const gchar *
354 adg_title_block_get_title(AdgTitleBlock *title_block)
356 AdgTitleBlockPrivate *data;
358 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
360 data = title_block->data;
362 return data->title;
366 * adg_title_block_set_drawing:
367 * @title_block: an #AdgTitleBlock entity
368 * @drawing: the new drawing name
370 * Sets a new drawing name on the title block.
372 * Since: 1.0
374 void
375 adg_title_block_set_drawing(AdgTitleBlock *title_block, const gchar *drawing)
377 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
378 g_object_set(title_block, "drawing", drawing, NULL);
382 * adg_title_block_get_drawing:
383 * @title_block: an #AdgTitleBlock entity
385 * Gets the drawing name, commonly used to specify the file name.
386 * The returned string is owned by @title_block and should not
387 * be modifed or freed.
389 * Returns: (transfer none): the drawing name or %NULL on no name or errors.
391 * Since: 1.0
393 const gchar *
394 adg_title_block_get_drawing(AdgTitleBlock *title_block)
396 AdgTitleBlockPrivate *data;
398 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
400 data = title_block->data;
402 return data->drawing;
406 * adg_title_block_set_size:
407 * @title_block: an #AdgTitleBlock entity
408 * @size: the new size
410 * Sets a new size on the title block.
412 * Since: 1.0
414 void
415 adg_title_block_set_size(AdgTitleBlock *title_block, const gchar *size)
417 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
418 g_object_set(title_block, "size", size, NULL);
422 * adg_title_block_get_size:
423 * @title_block: an #AdgTitleBlock entity
425 * Gets the media size (a descriptive name) where this drawing will
426 * be printed. Usually contains something like "A4" or "Letter".
427 * The returned string is owned by @title_block and should not
428 * be modifed or freed.
430 * Returns: (transfer none): the size or %NULL on no size or errors.
432 * Since: 1.0
434 const gchar *
435 adg_title_block_get_size(AdgTitleBlock *title_block)
437 AdgTitleBlockPrivate *data;
439 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
441 data = title_block->data;
443 return data->size;
447 * adg_title_block_set_scale:
448 * @title_block: an #AdgTitleBlock entity
449 * @scale: the new scale
451 * Sets a new scale on the title block.
453 * Since: 1.0
455 void
456 adg_title_block_set_scale(AdgTitleBlock *title_block, const gchar *scale)
458 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
459 g_object_set(title_block, "scale", scale, NULL);
463 * adg_title_block_get_scale:
464 * @title_block: an #AdgTitleBlock entity
466 * Gets the scale descriptive name of the drawing.
468 * Returns: (transfer none): the scale text or %NULL on no scale or errors.
470 * Since: 1.0
472 const gchar *
473 adg_title_block_get_scale(AdgTitleBlock *title_block)
475 AdgTitleBlockPrivate *data;
477 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
479 data = title_block->data;
481 return data->scale;
485 * adg_title_block_set_author:
486 * @title_block: an #AdgTitleBlock entity
487 * @author: the new author
489 * Sets a new author on the title block.
491 * Since: 1.0
493 void
494 adg_title_block_set_author(AdgTitleBlock *title_block, const gchar *author)
496 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
497 g_object_set(title_block, "author", author, NULL);
501 * adg_title_block_get_author:
502 * @title_block: an #AdgTitleBlock entity
504 * Gets the author's name of the drawing.
506 * Returns: (transfer none): the author or %NULL on no author or errors.
508 * Since: 1.0
510 const gchar *
511 adg_title_block_get_author(AdgTitleBlock *title_block)
513 AdgTitleBlockPrivate *data;
515 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
517 data = title_block->data;
519 return data->author;
523 * adg_title_block_set_date:
524 * @title_block: an #AdgTitleBlock entity
525 * @date: the new date
527 * Sets a new date on the title block. By default the date is
528 * set to %NULL and it will be implicitely rendered using the
529 * preferred representation for the current local of the actual
530 * date. This is roughly equivalent to:
532 * |[
533 * strftime(buffer, sizeof(buffer), "%x", now);
534 * adg_title_block_set_date(title_block, buffer);
535 * ]|
537 * To not render any value, use an empty string as @date.
539 * Since: 1.0
541 void
542 adg_title_block_set_date(AdgTitleBlock *title_block, const gchar *date)
544 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
545 g_object_set(title_block, "date", date, NULL);
549 * adg_title_block_get_date:
550 * @title_block: an #AdgTitleBlock entity
552 * Gets the date of the rendering set on @title_block.
554 * Returns: (transfer none): the date or %NULL on no date or errors.
556 * Since: 1.0
558 const gchar *
559 adg_title_block_get_date(AdgTitleBlock *title_block)
561 AdgTitleBlockPrivate *data;
563 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
565 data = title_block->data;
567 return data->date;
571 * adg_title_block_set_logo:
572 * @title_block: an #AdgTitleBlock entity
573 * @logo: the new logo
575 * Sets a new logo on the title block. This function will add
576 * a reference to @logo, removing the eventual reference held
577 * to the old logo, hence possibly destroying the old endity.
579 * The space reserved for the logo is 56x56, so try to keep the
580 * new logo near this size or scale it accordingly.
582 * Since: 1.0
584 void
585 adg_title_block_set_logo(AdgTitleBlock *title_block, AdgEntity *logo)
587 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
588 g_object_set(title_block, "logo", logo, NULL);
592 * adg_title_block_logo:
593 * @title_block: an #AdgTitleBlock entity
595 * Gets the logo bound to this title block.
596 * The returned object is owned by @title_block and should not
597 * be unreferenced although can be freely modified.
599 * Returns: (transfer none): the logo or %NULL on no logo or errors.
601 * Since: 1.0
603 AdgEntity *
604 adg_title_block_logo(AdgTitleBlock *title_block)
606 AdgTitleBlockPrivate *data;
608 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
610 data = title_block->data;
612 return data->logo;
616 * adg_title_block_set_projection:
617 * @title_block: an #AdgTitleBlock entity
618 * @projection: the new projection
620 * Sets a new projection symbol on the title block. This function
621 * will add a reference to @projection, removing the eventual
622 * reference held to the old symbol, hence possibly destroying
623 * the old endity.
625 * The space reserved for the projection is 56x56, so try to keep the
626 * new projection near this size or scale it accordingly.
628 * Since: 1.0
630 void
631 adg_title_block_set_projection(AdgTitleBlock *title_block,
632 AdgEntity *projection)
634 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
635 g_object_set(title_block, "projection", projection, NULL);
639 * adg_title_block_projection:
640 * @title_block: an #AdgTitleBlock entity
642 * Gets the projection bound to this title block.
643 * The returned object is owned by @title_block and should not
644 * be unreferenced although can be freely modified.
646 * Returns: (transfer none): the projection or
647 * %NULL on no projection or errors.
649 * Since: 1.0
651 AdgEntity *
652 adg_title_block_projection(AdgTitleBlock *title_block)
654 AdgTitleBlockPrivate *data;
656 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
658 data = title_block->data;
660 return data->projection;
664 static AdgTable *
665 _adg_get_table(AdgTitleBlock *title_block)
667 AdgTable *table = (AdgTable *) title_block;
669 if (adg_table_get_n_rows(table) == 0) {
670 AdgTableRow *row;
671 AdgTableCell *cell;
673 /* First row */
674 row = adg_table_row_new(table);
676 cell = adg_table_cell_new(row, 62);
678 cell = adg_table_cell_new(row, 200);
679 adg_table_cell_set_name(cell, "title");
680 adg_table_cell_set_text_title(cell, _("TITLE"));
681 adg_table_cell_switch_frame(cell, TRUE);
683 /* Second row */
684 row = adg_table_row_new(table);
686 cell = adg_table_cell_new(row, 62);
687 adg_table_cell_set_name(cell, "logo");
689 cell = adg_table_cell_new(row, 40);
690 adg_table_cell_set_name(cell, "size");
691 adg_table_cell_set_text_title(cell, _("SIZE"));
692 adg_table_cell_switch_frame(cell, TRUE);
694 cell = adg_table_cell_new(row, 60);
695 adg_table_cell_set_name(cell, "scale");
696 adg_table_cell_set_text_title(cell, _("SCALE"));
697 adg_table_cell_switch_frame(cell, TRUE);
699 cell = adg_table_cell_new(row, 100);
700 adg_table_cell_set_name(cell, "drawing");
701 adg_table_cell_set_text_title(cell, _("DRAWING"));
702 adg_table_cell_switch_frame(cell, TRUE);
704 /* Third row */
705 row = adg_table_row_new(table);
707 cell = adg_table_cell_new(row, 62);
708 adg_table_cell_set_name(cell, "projection");
709 adg_table_cell_switch_frame(cell, TRUE);
711 cell = adg_table_cell_new(row, 100);
712 adg_table_cell_set_name(cell, "author");
713 adg_table_cell_set_text_title(cell, _("AUTHOR"));
714 adg_table_cell_switch_frame(cell, TRUE);
716 cell = adg_table_cell_new(row, 100);
717 adg_table_cell_set_name(cell, "date");
718 adg_table_cell_set_text_title(cell, _("DATE"));
719 adg_table_cell_switch_frame(cell, TRUE);
722 return table;