Minor syntactical changes for readability.
[xuni.git] / src / widget / textbox.c
blob2c5f9ffaf9d2bf4af03850a8ebcec6add406ddb3
1 /*! \file textbox.c
3 Implements textbox widgets. Some textbox-specific code is sprinkled
4 elsewhere at the moment too, such as in label.c and in functions in gui.c
5 like activate_widget() and revert_widget().
6 */
8 #include "SDL_gfxPrimitives.h" /* for vlineRGBA() */
10 #include "../graphics.h"
11 #include "../memory.h"
12 #include "font.h"
13 #include "widgets.h"
14 #include "textbox.h"
15 #include "box.h"
16 #include "label.h"
18 static void free_textbox(struct xuni_t *xuni, struct widget_t *widget);
19 static void reposition_textbox(struct xuni_t *xuni, struct widget_t *widget);
20 static void rescale_textbox(struct xuni_t *xuni, struct widget_t *widget);
21 static void paint_textbox(struct xuni_t *xuni, struct widget_t *widget);
23 void textbox_widget_event(struct xuni_t *xuni, struct widget_t *widget,
24 enum widget_event_t event) {
26 static void (*function[])(struct xuni_t *xuni, struct widget_t *widget)
27 = {
29 free_textbox,
31 paint_textbox,
32 reposition_textbox,
33 rescale_textbox
36 call_widget_event_func(xuni, widget, event, function,
37 sizeof(function) / sizeof(*function));
40 void init_textbox(struct widget_t *widget, struct xuni_t *xuni,
41 const char *text, size_t font) {
43 widget->type = WIDGET_TEXTBOX;
44 widget->p.textbox = xuni_memory_allocate(sizeof(*widget->p.textbox));
46 widget->p.textbox->leftpos = 0;
48 add_allocate_widget_compose(widget, "alwaysinbox");
50 init_widget_pos(last_compose_widget(widget), 0, 0, 100.0, 100.0,
51 POS_PACK_NONE);
52 init_box(last_compose_widget(widget), BOX_STYLE_ALWAYSIN, 0);
53 /*last_compose_widget(widget)->visibility &= ~WIDGET_VISIBILITY_SELABLE;*/
54 last_compose_widget(widget)->visibility &= ~WIDGET_VISIBILITY_INDEPENDENT;
56 add_allocate_widget_compose(widget, "text");
58 init_widget_pos(last_compose_widget(widget), 0, 0, 100.0, 100.0,
59 POS_PACK_NONE);
60 init_label(last_compose_widget(widget), font, text, LABEL_ALIGN_LEFT,
61 255, 255, 255);
62 /* !!! why does reposition_widget() not work? */
63 /* !!! why was this here? */
64 /*widget_event(xuni, widget, WIDGET_EVENT_RESCALE);*/
65 last_compose_widget(widget)->visibility &= ~WIDGET_VISIBILITY_INDEPENDENT;
67 widget->visibility &= ~WIDGET_VISIBILITY_NOT_COMPOSE;
70 static void free_textbox(struct xuni_t *xuni, struct widget_t *widget) {
71 xuni_memory_free(widget->p.textbox);
74 static void paint_textbox(struct xuni_t *xuni, struct widget_t *widget) {
75 set_box_type(xuni->gui, widget,
76 widget->compose->widget[WID_TEXTBOX_BOX]);
77 paint_box_previous_state(xuni,
78 widget->compose->widget[WID_TEXTBOX_BOX]);
80 /*widget_event(xuni, widget->compose->widget[WID_TEXTBOX_BOX],
81 WIDGET_EVENT_PAINT);*/
83 widget_event(xuni, widget->compose->widget[WID_TEXTBOX_LABEL],
84 WIDGET_EVENT_PAINT);
86 /*paint_textbox_text(xuni->smode->screen, xuni->font, xuni->gui,
87 widget->p.textbox->data.scroll.text,
88 &widget->pos->real,
89 xuni->gui->active.widget == widget->compose->widget[0]);*/
92 static void reposition_textbox(struct xuni_t *xuni, struct widget_t *widget) {
96 static void rescale_textbox(struct xuni_t *xuni, struct widget_t *widget) {
97 /*struct widget_t *label = widget->compose->widget[WID_TEXTBOX_LABEL];
99 if(label->p.label && label->p.label->label) {
100 if(label->p.label->label->w > widget->pos->real.w) {
101 int offset = label->p.label->label->w - widget->pos->real.w;
103 set_widget_clip(label, 0, 0, offset, 0,
104 label->p.label->label->w - offset, label->p.label->label->h);
109 int textbox_is_empty(struct widget_t *widget) {
110 const char *data = get_textbox_data(widget);
112 return !data || !*data;
115 const char *get_textbox_data(struct widget_t *widget) {
116 return widget->compose->widget[WID_TEXTBOX_LABEL]->p.label->text;
119 /*! Clears the data in the textbox \a widget. Does not affect the textbox is
120 the textbox is already empty.
122 \param xuni A pointer to the main xuni structure.
123 \param widget The textbox widget to clear the data of.
125 void clear_textbox_data(struct xuni_t *xuni, struct widget_t *widget) {
126 struct widget_t *label = widget->compose->widget[WID_TEXTBOX_LABEL];
128 if(label->p.label->text && *label->p.label->text) {
129 xuni_memory_free((void *)label->p.label->text);
130 label->p.label->text = 0;
132 widget_event(xuni, widget, WIDGET_EVENT_RESCALE);
136 /*! Sets the data of the textbox \a widget. Automatically rescales the textbox
137 after setting the data.
139 \param xuni A pointer to the main xuni structure.
140 \param widget The textbox to set the data of.
141 \param text The text to set the textbox data to.
143 void set_textbox_data(struct xuni_t *xuni, struct widget_t *widget,
144 const char *text) {
146 struct widget_t *label = widget->compose->widget[WID_TEXTBOX_LABEL];
148 xuni_memory_free((void *)label->p.label->text);
149 label->p.label->text = text;
151 widget_event(xuni, widget, WIDGET_EVENT_RESCALE);