File: label.c

package info (click to toggle)
s3d 0.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,708 kB
  • sloc: ansic: 23,609; python: 488; perl: 98; makefile: 31; sh: 29
file content (101 lines) | stat: -rw-r--r-- 2,647 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// SPDX-License-Identifier: LGPL-2.1-or-later
/* SPDX-FileCopyrightText: 2006-2015  Simon Wunderlich <sw@simonwunderlich.de>
 */


#include <s3d.h>
#include <s3dw.h>
#include <s3dw_int.h>
#include <s3dlib.h>
#include <stdlib.h> /* malloc() */
#include <string.h> /* strdup() */

void s3dw_label_draw(s3dw_widget *widget)
{
	s3dw_label *label = (s3dw_label *)widget;
	float length;
	widget->oid = s3d_draw_string(label->text, &length);
	s3d_pep_materials_a(widget->oid, widget->style->text_mat, 1);
	s3d_link(widget->oid, widget->parent->oid);
	s3d_translate(widget->oid, widget->x, -widget->y, 0.1);
	widget->width = length + 1;
	widget->height = 2;
}
/* show the label */
void s3dw_label_show(s3dw_widget *widget)
{
	s3d_flags_on(widget->oid, S3D_OF_VISIBLE | S3D_OF_SELECTABLE);
}
/* hides the label */
void s3dw_label_hide(s3dw_widget *widget)
{
	s3d_flags_off(widget->oid, S3D_OF_VISIBLE | S3D_OF_SELECTABLE);
}

/** \brief change label text
 *
 * Change the text in the referenced label to the specified text.
 */
void s3dw_label_change_text(s3dw_label *label, const char *text)
{
	s3dw_widget *widget = (s3dw_widget *)label;

	/* redraw the text ... */
	free(label->text);
	label->text = strdup(text);
	s3dw_label_erase(widget);
	s3dw_label_draw(widget);
	if (widget->flags&S3DW_ONSCREEN)
		s3dw_label_show(widget);
}

/** \brief create a new label in the surface
 *
 * Creates a new label on the surface, with "text" written on it and the upper
 * left corner at (posx,posy) on the surface.
 *
 * See s3dw_label for information about callbacks which may be defined.
 */
s3dw_label *s3dw_label_new(const s3dw_surface *surface, const char *text, float posx, float posy)
{
	s3dw_label *label;
	s3dw_widget *widget;
	label = (s3dw_label *)malloc(sizeof(s3dw_label));
	widget = s3dw_widget_new((s3dw_widget *)label);
	widget->type = S3DW_TLABEL;
	widget->x = posx;
	widget->y = posy;
	label->text = strdup(text);
	label->onclick = s3dw_nothing;
	s3dw_widget_append((s3dw_widget *)surface, widget);
	s3dw_label_draw(widget);
	return label;
}

void s3dw_label_erase(s3dw_widget *widget)
{
	s3d_del_object(widget->oid);
}
/* destroy the label */
void s3dw_label_destroy(s3dw_widget *widget)
{
	s3dw_label *label = (s3dw_label *)widget;
	s3dw_label_erase(widget);
	free(label->text);
	free(label);
}
/* handle key events */
int s3dw_label_event_key(s3dw_widget *S3DUNUSED(widget), struct s3d_key_event *S3DUNUSED(keys))
{
	return 0;
}
/* handle click events */
int s3dw_label_event_click(s3dw_widget *widget, uint32_t oid)
{
	s3dw_label *label = (s3dw_label *)widget;
	if (widget->oid == oid) {
		label->onclick(widget);
		return 1;
	}
	return 0;
}