1 /* DreamChess
2 **
3 ** DreamChess is the legal property of its developers, whose names are too
4 ** numerous to list here. Please refer to the AUTHORS.txt file distributed
5 ** with this source distribution.
6 **
7 ** This program is free software: you can redistribute it and/or modify
8 ** it under the terms of the GNU General Public License as published by
9 ** the Free Software Foundation, either version 3 of the License, or
10 ** (at your option) any later version.
11 **
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
16 **
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <gamegui/clipping.h>
25 #include <gamegui/edit.h>
26
27 static gg_colour_t col_grey = {0.5f, 0.5f, 0.5f, 1.0f};
28
29 static gg_colour_t col_texthighlight = {0.55f, 0.65f, 0.95f, 1.0f};
30
31 static gg_colour_t col_text = {1.0f, 1.0f, 1.0f, 1.0f};
32
gg_edit_get_class_id(void)33 gg_class_id gg_edit_get_class_id(void) {
34 GG_CHILD(gg_widget_get_class_id())
35 }
36
37 #if 0
38 static int string_width(char *s, int n)
39 {
40 int retval;
41 char c = s[n];
42
43 s[n] = 0;
44 gg_system_get_string_size(s, &retval, NULL);
45 s[n] = c;
46 return retval;
47 }
48 #endif
49
gg_edit_render(gg_widget_t * widget,int x,int y,int focus)50 void gg_edit_render(gg_widget_t *widget, int x, int y, int focus) {
51 gg_edit_t *edit = GG_EDIT(widget);
52 gg_rect_t rect;
53 gg_colour_t *colour = &col_text;
54 struct gg_edit_line *line;
55
56 switch (focus) {
57 case GG_FOCUS_DISABLED:
58 colour = &col_grey;
59 break;
60 case GG_FOCUS_ONE:
61 case GG_FOCUS_ALL:
62 colour = &col_texthighlight;
63 }
64
65 /* TODO Fix temporary hack */
66 if (!widget->enabled)
67 colour = &col_grey;
68
69 gg_system_draw_rect(x, y, edit->width_a, edit->height_a, colour);
70
71 x += EDIT_SPACING;
72 y += EDIT_SPACING;
73
74 rect.x = x;
75 rect.y = y;
76 rect.width = edit->width_a - 2 * EDIT_SPACING;
77 rect.height = edit->height_a - 2 * EDIT_SPACING;
78 gg_clipping_adjust(&rect);
79
80 TAILQ_FOREACH(line, &edit->lines, entries) {
81 gg_system_draw_string(line->text, x, y, colour, 0, 0);
82 y += edit->line_height;
83 }
84
85 gg_clipping_undo();
86 }
87
gg_edit_input(gg_widget_t * widget,gg_event_t event)88 int gg_edit_input(gg_widget_t *widget, gg_event_t event) {
89 return 1;
90 }
91
gg_edit_append(gg_edit_t * edit,char * text)92 void gg_edit_append(gg_edit_t *edit, char *text) {
93 struct gg_edit_line *line = malloc(sizeof(struct gg_edit_line));
94 line->text = strdup(text);
95 TAILQ_INSERT_TAIL(&edit->lines, line, entries);
96 }
97
gg_edit_init(gg_edit_t * edit,int width,int height)98 void gg_edit_init(gg_edit_t *edit, int width, int height) {
99 gg_container_init((gg_container_t *)edit);
100
101 edit->render = gg_edit_render;
102 edit->input = gg_edit_input;
103 edit->id = gg_edit_get_class_id();
104 TAILQ_INIT(&edit->lines);
105 edit->enabled = 1;
106 edit->width = width + EDIT_SPACING * 2;
107 edit->height = height + EDIT_SPACING * 2;
108 edit->display_pos = 0;
109 gg_system_get_string_size("W", NULL, &edit->line_height);
110 edit->line_height += EDIT_LINE_SPACING;
111 gg_container_append(GG_CONTAINER(edit), gg_scrollbarv_create(edit->height));
112 }
113
gg_edit_create(int width,int height)114 gg_widget_t *gg_edit_create(int width, int height) {
115 gg_edit_t *edit = malloc(sizeof(gg_edit_t));
116
117 gg_edit_init(edit, width, height);
118
119 return GG_WIDGET(edit);
120 }
121