Release s3d 0.2.2
[s3d.git] / libs3dw / label.c
blob23843aba5df14af0a75fa68ddcde17eb492648db
1 /*
2 * label.c
4 * Copyright (C) 2006-2011 Simon Wunderlich <dotslash@packetmixer.de>
6 * This file is part of the s3d Widgets, a Widget Library for s3d.
7 * See http://s3d.berlios.de/ for more updates.
9 * s3d Widgets is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * s3d Widgets is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the s3d Widgets; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <s3d.h>
26 #include <s3dw.h>
27 #include <s3dw_int.h>
28 #include <stdlib.h> /* malloc() */
29 #include <string.h> /* strdup() */
31 void s3dw_label_draw(s3dw_widget *widget)
33 s3dw_label *label = (s3dw_label *)widget;
34 float length;
35 widget->oid = s3d_draw_string(label->text, &length);
36 s3d_pep_materials_a(widget->oid, widget->style->text_mat, 1);
37 s3d_link(widget->oid, widget->parent->oid);
38 s3d_translate(widget->oid, widget->x, -widget->y, 0.1);
39 widget->width = length + 1;
40 widget->height = 2;
42 /* show the label */
43 void s3dw_label_show(s3dw_widget *widget)
45 s3d_flags_on(widget->oid, S3D_OF_VISIBLE | S3D_OF_SELECTABLE);
47 /* hides the label */
48 void s3dw_label_hide(s3dw_widget *widget)
50 s3d_flags_off(widget->oid, S3D_OF_VISIBLE | S3D_OF_SELECTABLE);
53 /** \brief change label text
55 * Change the text in the referenced label to the specified text.
57 void s3dw_label_change_text(s3dw_label *label, const char *text)
59 s3dw_widget *widget = (s3dw_widget *)label;
61 /* redraw the text ... */
62 free(label->text);
63 label->text = strdup(text);
64 s3dw_label_erase(widget);
65 s3dw_label_draw(widget);
66 if (widget->flags&S3DW_ONSCREEN)
67 s3dw_label_show(widget);
70 /** \brief create a new label in the surface
72 * Creates a new label on the surface, with "text" written on it and the upper
73 * left corner at (posx,posy) on the surface.
75 * See s3dw_label for information about callbacks which may be defined.
77 s3dw_label *s3dw_label_new(const s3dw_surface *surface, const char *text, float posx, float posy)
79 s3dw_label *label;
80 s3dw_widget *widget;
81 label = (s3dw_label *)malloc(sizeof(s3dw_label));
82 widget = s3dw_widget_new((s3dw_widget *)label);
83 widget->type = S3DW_TLABEL;
84 widget->x = posx;
85 widget->y = posy;
86 label->text = strdup(text);
87 label->onclick = s3dw_nothing;
88 s3dw_widget_append((s3dw_widget *)surface, widget);
89 s3dw_label_draw(widget);
90 return label;
93 void s3dw_label_erase(s3dw_widget *widget)
95 s3d_del_object(widget->oid);
97 /* destroy the label */
98 void s3dw_label_destroy(s3dw_widget *widget)
100 s3dw_label *label = (s3dw_label *)widget;
101 s3dw_label_erase(widget);
102 free(label->text);
103 free(label);
105 /* handle key events */
106 int s3dw_label_event_key(s3dw_widget *S3DUNUSED(widget), struct s3d_key_event *S3DUNUSED(keys))
108 return 0;
110 /* handle click events */
111 int s3dw_label_event_click(s3dw_widget *widget, uint32_t oid)
113 s3dw_label *label = (s3dw_label *)widget;
114 if (widget->oid == oid) {
115 label->onclick(widget);
116 return 1;
118 return 0;