adg: use G_PRIVATE_ADD and friends
[adg.git] / src / adg / adg-title-block.c
blob3fa31503934b4200cfa58947bcd1e755d9564b40
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2019 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"
48 #include "adg-table.h"
49 #include "adg-table-row.h"
50 #include "adg-table-cell.h"
52 #include "adg-title-block.h"
53 #include "adg-title-block-private.h"
56 #define _ADG_OLD_OBJECT_CLASS ((GObjectClass *) adg_title_block_parent_class)
59 G_DEFINE_TYPE_WITH_PRIVATE(AdgTitleBlock, adg_title_block, ADG_TYPE_TABLE);
61 enum {
62 PROP_0,
63 PROP_TITLE,
64 PROP_DRAWING,
65 PROP_SIZE,
66 PROP_SCALE,
67 PROP_AUTHOR,
68 PROP_DATE,
69 PROP_LOGO,
70 PROP_PROJECTION
74 static void _adg_finalize (GObject *object);
75 static void _adg_get_property (GObject *object,
76 guint prop_id,
77 GValue *value,
78 GParamSpec *pspec);
79 static void _adg_set_property (GObject *object,
80 guint prop_id,
81 const GValue *value,
82 GParamSpec *pspec);
85 static void
86 adg_title_block_class_init(AdgTitleBlockClass *klass)
88 GObjectClass *gobject_class;
89 GParamSpec *param;
91 gobject_class = (GObjectClass *) klass;
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 AdgTable *table = (AdgTable *) title_block;
158 AdgTitleBlockPrivate *data = adg_title_block_get_instance_private(title_block);
159 AdgTableRow *row;
161 data->author = NULL;
162 data->title = NULL;
163 data->drawing = NULL;
164 data->size = NULL;
165 data->scale = NULL;
166 data->author = NULL;
167 data->date = NULL;
168 data->projection = NULL;
170 /* By default the title block should be floating */
171 adg_entity_switch_floating((AdgEntity *) title_block, TRUE);
173 /* Create the title block template*/
175 /* First row */
176 row = adg_table_row_new(table);
177 adg_table_cell_new_with_width(row, 62);
178 adg_table_cell_new_full(row, 200, "title", _("TITLE"), TRUE);
180 /* Second row */
181 row = adg_table_row_new(table);
182 adg_table_cell_new_full(row, 62, "logo", NULL, FALSE);
183 adg_table_cell_new_full(row, 40, "size", _("SIZE"), TRUE);
184 adg_table_cell_new_full(row, 60, "scale", _("SCALE"), TRUE);
185 adg_table_cell_new_full(row, 100, "drawing", _("DRAWING"), TRUE);
187 /* Third row */
188 row = adg_table_row_new(table);
189 adg_table_cell_new_full(row, 62, "projection", NULL, TRUE);
190 adg_table_cell_new_full(row, 100, "author", _("AUTHOR"), TRUE);
191 adg_table_cell_new_full(row, 100, "date", _("DATE"), TRUE);
194 static void
195 _adg_finalize(GObject *object)
197 AdgTitleBlockPrivate *data = adg_title_block_get_instance_private((AdgTitleBlock *) object);
199 g_free(data->title);
200 g_free(data->drawing);
201 g_free(data->size);
202 g_free(data->scale);
203 g_free(data->author);
204 g_free(data->date);
206 if (_ADG_OLD_OBJECT_CLASS->finalize)
207 _ADG_OLD_OBJECT_CLASS->finalize(object);
210 static void
211 _adg_get_property(GObject *object, guint prop_id,
212 GValue *value, GParamSpec *pspec)
214 AdgTitleBlockPrivate *data = adg_title_block_get_instance_private((AdgTitleBlock *) object);
216 switch (prop_id) {
217 case PROP_TITLE:
218 g_value_set_string(value, data->title);
219 break;
220 case PROP_DRAWING:
221 g_value_set_string(value, data->drawing);
222 break;
223 case PROP_SIZE:
224 g_value_set_string(value, data->size);
225 break;
226 case PROP_SCALE:
227 g_value_set_string(value, data->scale);
228 break;
229 case PROP_AUTHOR:
230 g_value_set_string(value, data->author);
231 break;
232 case PROP_DATE:
233 g_value_set_string(value, data->date);
234 break;
235 case PROP_LOGO:
236 g_value_set_object(value, data->logo);
237 break;
238 case PROP_PROJECTION:
239 g_value_set_object(value, data->projection);
240 break;
241 default:
242 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
243 break;
247 static void
248 _adg_set_property(GObject *object, guint prop_id,
249 const GValue *value, GParamSpec *pspec)
251 AdgTitleBlockPrivate *data = adg_title_block_get_instance_private((AdgTitleBlock *) object);
252 AdgTable *table = (AdgTable *) object;
253 AdgTableCell *cell;
255 switch (prop_id) {
256 case PROP_TITLE:
257 g_free(data->title);
258 data->title = g_value_dup_string(value);
259 cell = adg_table_get_cell(table, "title");
260 adg_table_cell_set_text_value(cell, data->title);
261 break;
262 case PROP_DRAWING:
263 g_free(data->drawing);
264 data->drawing = g_value_dup_string(value);
265 cell = adg_table_get_cell(table, "drawing");
266 adg_table_cell_set_text_value(cell, data->drawing);
267 break;
268 case PROP_SIZE:
269 g_free(data->size);
270 data->size = g_value_dup_string(value);
271 cell = adg_table_get_cell(table, "size");
272 adg_table_cell_set_text_value(cell, data->size);
273 break;
274 case PROP_SCALE:
275 g_free(data->scale);
276 data->scale = g_value_dup_string(value);
277 cell = adg_table_get_cell(table, "scale");
278 adg_table_cell_set_text_value(cell, data->scale);
279 break;
280 case PROP_AUTHOR:
281 g_free(data->author);
282 data->author = g_value_dup_string(value);
283 cell = adg_table_get_cell(table, "author");
284 adg_table_cell_set_text_value(cell, data->author);
285 break;
286 case PROP_DATE:
287 g_free(data->date);
288 if (g_value_get_string(value) == NULL) {
289 /* NULL means the date must be automatically updated */
290 GDate *gdate;
291 char buffer[100] = { 0 };
293 gdate = g_date_new();
294 g_date_set_time_t(gdate, time (NULL));
295 g_date_strftime(buffer, sizeof(buffer), "%x", gdate);
296 g_date_free(gdate);
298 data->date = g_strdup(buffer);
299 } else {
300 data->date = g_value_dup_string(value);
302 cell = adg_table_get_cell(table, "date");
303 adg_table_cell_set_text_value(cell, data->date);
304 break;
305 case PROP_LOGO:
306 data->logo = g_value_get_object(value);
307 cell = adg_table_get_cell(table, "logo");
308 adg_table_cell_set_value(cell, data->logo);
309 adg_table_cell_set_value_pos_explicit(cell, 0.5, 1, 0.5, 0.5);
310 break;
311 case PROP_PROJECTION:
312 data->projection = g_value_get_object(value);
313 cell = adg_table_get_cell(table, "projection");
314 adg_table_cell_set_value(cell, data->projection);
315 adg_table_cell_set_value_pos_explicit(cell, 0.5, 0.5, 0.5, 0.5);
316 break;
317 default:
318 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
319 break;
325 * adg_title_block_new:
327 * Creates a new empty title block entity.
329 * Returns: (transfer full): the newly created title block entity.
331 * Since: 1.0
333 AdgTitleBlock *
334 adg_title_block_new(void)
336 return g_object_new(ADG_TYPE_TITLE_BLOCK, NULL);
340 * adg_title_block_set_title:
341 * @title_block: an #AdgTitleBlock entity
342 * @title: the new title
344 * Sets a new title on the title block.
346 * Since: 1.0
348 void
349 adg_title_block_set_title(AdgTitleBlock *title_block, const gchar *title)
351 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
352 g_object_set(title_block, "title", title, NULL);
356 * adg_title_block_get_title:
357 * @title_block: an #AdgTitleBlock entity
359 * Gets the descriptive title associated to this title block.
360 * The returned string is owned by @title_block and should not
361 * be modifed or freed.
363 * Returns: (transfer none): the title or <constant>NULL</constant> on no title or errors.
365 * Since: 1.0
367 const gchar *
368 adg_title_block_get_title(AdgTitleBlock *title_block)
370 AdgTitleBlockPrivate *data;
372 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
374 data = adg_title_block_get_instance_private(title_block);
375 return data->title;
379 * adg_title_block_set_drawing:
380 * @title_block: an #AdgTitleBlock entity
381 * @drawing: the new drawing name
383 * Sets a new drawing name on the title block.
385 * Since: 1.0
387 void
388 adg_title_block_set_drawing(AdgTitleBlock *title_block, const gchar *drawing)
390 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
391 g_object_set(title_block, "drawing", drawing, NULL);
395 * adg_title_block_get_drawing:
396 * @title_block: an #AdgTitleBlock entity
398 * Gets the drawing name, commonly used to specify the file name.
399 * The returned string is owned by @title_block and should not
400 * be modifed or freed.
402 * Returns: (transfer none): the drawing name or <constant>NULL</constant> on no name or errors.
404 * Since: 1.0
406 const gchar *
407 adg_title_block_get_drawing(AdgTitleBlock *title_block)
409 AdgTitleBlockPrivate *data;
411 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
413 data = adg_title_block_get_instance_private(title_block);
414 return data->drawing;
418 * adg_title_block_set_size:
419 * @title_block: an #AdgTitleBlock entity
420 * @size: the new size
422 * Sets a new size on the title block.
424 * Since: 1.0
426 void
427 adg_title_block_set_size(AdgTitleBlock *title_block, const gchar *size)
429 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
430 g_object_set(title_block, "size", size, NULL);
434 * adg_title_block_get_size:
435 * @title_block: an #AdgTitleBlock entity
437 * Gets the media size (a descriptive name) where this drawing will
438 * be printed. Usually contains something like <constant>"A4"</constant>
439 * or <constant>"Letter"</constant>.
440 * The returned string is owned by @title_block and should not
441 * be modifed or freed.
443 * Returns: (transfer none): the size or <constant>NULL</constant> on no size or errors.
445 * Since: 1.0
447 const gchar *
448 adg_title_block_get_size(AdgTitleBlock *title_block)
450 AdgTitleBlockPrivate *data;
452 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
454 data = adg_title_block_get_instance_private(title_block);
455 return data->size;
459 * adg_title_block_set_scale:
460 * @title_block: an #AdgTitleBlock entity
461 * @scale: the new scale
463 * Sets a new scale on the title block.
465 * Since: 1.0
467 void
468 adg_title_block_set_scale(AdgTitleBlock *title_block, const gchar *scale)
470 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
471 g_object_set(title_block, "scale", scale, NULL);
475 * adg_title_block_get_scale:
476 * @title_block: an #AdgTitleBlock entity
478 * Gets the scale descriptive name of the drawing.
480 * Returns: (transfer none): the scale text or <constant>NULL</constant> on no scale or errors.
482 * Since: 1.0
484 const gchar *
485 adg_title_block_get_scale(AdgTitleBlock *title_block)
487 AdgTitleBlockPrivate *data;
489 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
491 data = adg_title_block_get_instance_private(title_block);
492 return data->scale;
496 * adg_title_block_set_author:
497 * @title_block: an #AdgTitleBlock entity
498 * @author: the new author
500 * Sets a new author on the title block.
502 * Since: 1.0
504 void
505 adg_title_block_set_author(AdgTitleBlock *title_block, const gchar *author)
507 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
508 g_object_set(title_block, "author", author, NULL);
512 * adg_title_block_get_author:
513 * @title_block: an #AdgTitleBlock entity
515 * Gets the author's name of the drawing.
517 * Returns: (transfer none): the author or <constant>NULL</constant> on no author or errors.
519 * Since: 1.0
521 const gchar *
522 adg_title_block_get_author(AdgTitleBlock *title_block)
524 AdgTitleBlockPrivate *data;
526 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
528 data = adg_title_block_get_instance_private(title_block);
529 return data->author;
533 * adg_title_block_set_date:
534 * @title_block: an #AdgTitleBlock entity
535 * @date: the new date
537 * Sets a new date on the title block. By default the date is
538 * set to <constant>NULL</constant> and it will be implicitely
539 * rendered using the*preferred representation for the current
540 * local of the actual date. This is roughly equivalent to:
542 * <informalexample><programlisting language="C">
543 * strftime(buffer, sizeof(buffer), "%x", now);
544 * adg_title_block_set_date(title_block, buffer);
545 * </programlisting></informalexample>
547 * To not render any value, use an empty string as @date.
549 * Since: 1.0
551 void
552 adg_title_block_set_date(AdgTitleBlock *title_block, const gchar *date)
554 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
555 g_object_set(title_block, "date", date, NULL);
559 * adg_title_block_get_date:
560 * @title_block: an #AdgTitleBlock entity
562 * Gets the date of the rendering set on @title_block.
564 * Returns: (transfer none): the date or <constant>NULL</constant> on no date or errors.
566 * Since: 1.0
568 const gchar *
569 adg_title_block_get_date(AdgTitleBlock *title_block)
571 AdgTitleBlockPrivate *data;
573 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
575 data = adg_title_block_get_instance_private(title_block);
576 return data->date;
580 * adg_title_block_set_logo:
581 * @title_block: an #AdgTitleBlock entity
582 * @logo: the new logo
584 * Sets a new logo on the title block. This function will add
585 * a reference to @logo, removing the eventual reference held
586 * to the old logo, hence possibly destroying the old endity.
588 * The space reserved for the logo is 56x56, so try to keep the
589 * new logo near this size or scale it accordingly.
591 * Since: 1.0
593 void
594 adg_title_block_set_logo(AdgTitleBlock *title_block, AdgEntity *logo)
596 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
597 g_object_set(title_block, "logo", logo, NULL);
601 * adg_title_block_logo:
602 * @title_block: an #AdgTitleBlock entity
604 * Gets the logo bound to this title block.
605 * The returned object is owned by @title_block and should not
606 * be unreferenced although can be freely modified.
608 * Returns: (transfer none): the logo or <constant>NULL</constant> on no logo or errors.
610 * Since: 1.0
612 AdgEntity *
613 adg_title_block_logo(AdgTitleBlock *title_block)
615 AdgTitleBlockPrivate *data;
617 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
619 data = adg_title_block_get_instance_private(title_block);
620 return data->logo;
624 * adg_title_block_set_projection:
625 * @title_block: an #AdgTitleBlock entity
626 * @projection: the new projection
628 * Sets a new projection symbol on the title block. This function
629 * will add a reference to @projection, removing the eventual
630 * reference held to the old symbol, hence possibly destroying
631 * the old endity.
633 * The space reserved for the projection is 56x56, so try to keep the
634 * new projection near this size or scale it accordingly.
636 * Since: 1.0
638 void
639 adg_title_block_set_projection(AdgTitleBlock *title_block,
640 AdgEntity *projection)
642 g_return_if_fail(ADG_IS_TITLE_BLOCK(title_block));
643 g_object_set(title_block, "projection", projection, NULL);
647 * adg_title_block_projection:
648 * @title_block: an #AdgTitleBlock entity
650 * Gets the projection bound to this title block.
651 * The returned object is owned by @title_block and should not
652 * be unreferenced although can be freely modified.
654 * Returns: (transfer none): the projection or <constant>NULL</constant> on no projection or errors.
656 * Since: 1.0
658 AdgEntity *
659 adg_title_block_projection(AdgTitleBlock *title_block)
661 AdgTitleBlockPrivate *data;
663 g_return_val_if_fail(ADG_IS_TITLE_BLOCK(title_block), NULL);
665 data = adg_title_block_get_instance_private(title_block);
666 return data->projection;