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 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "display.h"
33 #include "container.h"
34 #include "../common/context.h"
35 #include "../common/object.h"
36 #include "../ui/display.h"
37
38 #include <cx/printf.h>
39
40 static void set_alignment(GtkWidget *widget,
float xalign,
float yalign) {
41 #if GTK_MAJOR_VERSION >=
4 || (
GTK_MAJOR_VERSION >=
3 &&
GTK_MINOR_VERSION >=
16)
42 gtk_label_set_xalign(
GTK_LABEL(widget), xalign);
43 gtk_label_set_yalign(
GTK_LABEL(widget), yalign);
44 #else
45 gtk_misc_set_alignment(
GTK_MISC(widget), xalign, yalign);
46 #endif
47 }
48
49 UIWIDGET ui_label_create(UiObject *obj, UiLabelArgs args) {
50 UiObject* current = uic_current_obj(obj);
51
52 const char *css_class =
NULL;
53 char *markup =
NULL;
54 if(args.label) {
55 #if GTK_MAJOR_VERSION <
3
56 switch(args.style) {
57 case UI_LABEL_STYLE_DEFAULT:
break;
58 case UI_LABEL_STYLE_TITLE: {
59 cxmutstr m = cx_asprintf(
"<b>%s</b>", args.label);
60 markup = m.ptr;
61 args.label =
NULL;
62 }
63 case UI_LABEL_STYLE_SUBTITLE: {
64 break;
65 }
66 case UI_LABEL_STYLE_DIM: {
67 break;
68 }
69 }
70 # else
71 switch(args.style) {
72 case UI_LABEL_STYLE_DEFAULT:
break;
73 case UI_LABEL_STYLE_TITLE: {
74 css_class =
"ui_label_title";
75 break;
76 }
77 case UI_LABEL_STYLE_SUBTITLE: {
78 css_class =
"subtitle";
79 break;
80 }
81 case UI_LABEL_STYLE_DIM: {
82 css_class =
"dim-label";
83 break;
84 }
85 }
86 # endif
87 }
88
89
90 GtkWidget *widget = gtk_label_new(args.label);
91 if(markup) {
92 gtk_label_set_markup(
GTK_LABEL(widget), markup);
93 free(markup);
94 }
95
96 if(css_class) {
97 WIDGET_ADD_CSS_CLASS(widget, css_class);
98 }
99
100 switch(args.align) {
101 case UI_ALIGN_DEFAULT:
break;
102 case UI_ALIGN_LEFT: set_alignment(widget,
0, .
5);
break;
103 case UI_ALIGN_RIGHT: set_alignment(widget,
1, .
5);
break;
104 case UI_ALIGN_CENTER:
break;
105 }
106
107
108 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname,
UI_VAR_STRING);
109 if(var) {
110 UiString* value = (UiString*)var->value;
111 value->obj = widget;
112 value->get = ui_label_get;
113 value->set = ui_label_set;
114 }
115
116 UI_APPLY_LAYOUT1(current, args);
117 current->container->add(current->container, widget,
FALSE);
118
119 return widget;
120 }
121
122 UIWIDGET ui_llabel_create(UiObject* obj, UiLabelArgs args) {
123 args.align =
UI_ALIGN_LEFT;
124 return ui_label_create(obj, args);
125 }
126
127 UIWIDGET ui_rlabel_create(UiObject* obj, UiLabelArgs args) {
128 args.align =
UI_ALIGN_RIGHT;
129 return ui_label_create(obj, args);
130 }
131
132 char* ui_label_get(UiString *s) {
133 if(s->value.ptr) {
134 s->value.free(s->value.ptr);
135 }
136 s->value.ptr = g_strdup(gtk_label_get_text(
GTK_LABEL(s->obj)));
137 s->value.free = (ui_freefunc)g_free;
138 return s->value.ptr;
139 }
140
141 void ui_label_set(UiString *s,
const char *value) {
142 gtk_label_set_text(
GTK_LABEL(s->obj), value);
143 if(s->value.ptr) {
144 s->value.free(s->value.ptr);
145 s->value.ptr =
NULL;
146 s->value.free =
NULL;
147 }
148 }
149
150 UIWIDGET ui_space_deprecated(UiObject *obj) {
151 GtkWidget *widget = gtk_label_new(
"");
152 UiContainer *ct = uic_get_current_container(obj);
153 ct->add(ct, widget,
TRUE);
154
155 return widget;
156 }
157
158 UIWIDGET ui_separator_deprecated(UiObject *obj) {
159 #if GTK_MAJOR_VERSION >=
3
160 GtkWidget *widget = gtk_separator_new(
GTK_ORIENTATION_HORIZONTAL);
161 #else
162 GtkWidget *widget = gtk_hseparator_new();
163 #endif
164 UiContainer *ct = uic_get_current_container(obj);
165 ct->add(ct, widget,
FALSE);
166
167 return widget;
168 }
169
170
171
172 typedef struct UiProgressBarRange {
173 double min;
174 double max;
175 } UiProgressBarRange;
176
177 UIWIDGET ui_progressbar_create(UiObject *obj, UiProgressbarArgs args) {
178 UiObject* current = uic_current_obj(obj);
179
180 GtkWidget *progressbar = gtk_progress_bar_new();
181 if(args.max > args.min) {
182 UiProgressBarRange *range = malloc(
sizeof(UiProgressBarRange));
183 range->min = args.min;
184 range->max = args.max;
185 g_signal_connect(
186 progressbar,
187 "destroy",
188 G_CALLBACK(ui_destroy_userdata),
189 range);
190 g_object_set_data(
G_OBJECT(progressbar),
"ui_range", range);
191 }
192
193
194 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname,
UI_VAR_DOUBLE);
195 if(var && var->value) {
196 UiDouble *value = var->value;
197 value->get = ui_progressbar_get;
198 value->set = ui_progressbar_set;
199 value->obj = progressbar;
200 ui_progressbar_set(value, value->value);
201 }
202
203 UI_APPLY_LAYOUT1(current, args);
204 current->container->add(current->container, progressbar,
FALSE);
205
206 return progressbar;
207 }
208
209 double ui_progressbar_get(UiDouble *d) {
210 UiProgressBarRange *range = g_object_get_data(d->obj,
"ui_range");
211 double fraction = gtk_progress_bar_get_fraction(
GTK_PROGRESS_BAR(d->obj));
212 if(range) {
213 fraction = range->min + (range->max - range->min) * fraction;
214 }
215 d->value = fraction;
216 return d->value;
217 }
218
219 void ui_progressbar_set(UiDouble *d,
double value) {
220 d->value = value;
221 UiProgressBarRange *range = g_object_get_data(d->obj,
"ui_range");
222 if(range) {
223 value = (value - range->min) / (range->max - range->min);
224 }
225 gtk_progress_bar_set_fraction(
GTK_PROGRESS_BAR(d->obj), value);
226 }
227
228
229
230
231 UIWIDGET ui_progressspinner_create(UiObject* obj, UiProgressbarSpinnerArgs args) {
232 UiObject* current = uic_current_obj(obj);
233
234 GtkWidget *spinner = gtk_spinner_new();
235
236 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname,
UI_VAR_INTEGER);
237 if(var && var->value) {
238 UiInteger *value = var->value;
239 value->get = ui_spinner_get;
240 value->set = ui_spinner_set;
241 value->obj = spinner;
242 ui_spinner_set(value, value->value);
243 }
244
245 UI_APPLY_LAYOUT1(current, args);
246 current->container->add(current->container, spinner,
FALSE);
247
248 return spinner;
249 }
250
251 int64_t ui_spinner_get(UiInteger *i) {
252 return i->value;
253 }
254
255 void ui_spinner_set(UiInteger *i,
int64_t value) {
256 i->value = value;
257 if(i->obj) {
258 GtkSpinner *spinner =
GTK_SPINNER(i->obj);
259 if(value !=
0) {
260 gtk_spinner_start(spinner);
261 }
else {
262 gtk_spinner_stop(spinner);
263 }
264 }
265 }
266