31 |
31 |
32 #include "display.h" |
32 #include "display.h" |
33 #include "container.h" |
33 #include "container.h" |
34 #include "../common/context.h" |
34 #include "../common/context.h" |
35 #include "../common/object.h" |
35 #include "../common/object.h" |
|
36 #include "../ui/display.h" |
36 |
37 |
37 static void set_alignment(GtkWidget *widget, float xalign, float yalign) { |
38 static void set_alignment(GtkWidget *widget, float xalign, float yalign) { |
38 #if GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16 |
39 #if GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16 |
39 gtk_label_set_xalign(GTK_LABEL(widget), xalign); |
40 gtk_label_set_xalign(GTK_LABEL(widget), xalign); |
40 gtk_label_set_yalign(GTK_LABEL(widget), yalign); |
41 gtk_label_set_yalign(GTK_LABEL(widget), yalign); |
41 #else |
42 #else |
42 gtk_misc_set_alignment(GTK_MISC(widget), xalign, yalign); |
43 gtk_misc_set_alignment(GTK_MISC(widget), xalign, yalign); |
43 #endif |
44 #endif |
44 } |
45 } |
45 |
46 |
46 UIWIDGET ui_label(UiObject *obj, char *label) { |
47 UIWIDGET ui_label_create(UiObject *obj, UiLabelArgs args) { |
47 GtkWidget *widget = gtk_label_new(label); |
48 UiObject* current = uic_current_obj(obj); |
48 |
49 |
49 UiContainer *ct = uic_get_current_container(obj); |
50 GtkWidget *widget = gtk_label_new(args.label); |
50 ct->add(ct, widget, FALSE); |
51 switch(args.align) { |
|
52 case UI_ALIGN_DEFAULT: break; |
|
53 case UI_ALIGN_LEFT: set_alignment(widget, 0, .5); break; |
|
54 case UI_ALIGN_RIGHT: set_alignment(widget, 1, .5); break; |
|
55 case UI_ALIGN_CENTER: break; // TODO |
|
56 } |
|
57 |
|
58 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING); |
|
59 if(var) { |
|
60 UiString* value = (UiString*)var->value; |
|
61 value->obj = widget; |
|
62 value->get = ui_label_get; |
|
63 value->set = ui_label_set; |
|
64 } |
|
65 |
|
66 UI_APPLY_LAYOUT1(current, args); |
|
67 current->container->add(current->container, widget, FALSE); |
51 |
68 |
52 return widget; |
69 return widget; |
53 } |
70 } |
54 |
71 |
55 UIWIDGET ui_llabel(UiObject *obj, char *label) { |
72 UIWIDGET ui_llabel_create(UiObject* obj, UiLabelArgs args) { |
56 UIWIDGET widget = ui_label(obj, label); |
73 args.align = UI_ALIGN_LEFT; |
57 set_alignment(widget, 0, .5); |
74 return ui_label_create(obj, args); |
58 return widget; |
|
59 } |
75 } |
60 |
76 |
61 UIWIDGET ui_rlabel(UiObject *obj, char *label) { |
77 UIWIDGET ui_rlabel_create(UiObject* obj, UiLabelArgs args) { |
62 UIWIDGET widget = ui_label(obj, label); |
78 args.align = UI_ALIGN_RIGHT; |
63 //gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_RIGHT); |
79 return ui_label_create(obj, args); |
64 |
|
65 set_alignment(widget, 1, .5); |
|
66 return widget; |
|
67 } |
80 } |
68 |
81 |
69 UIWIDGET ui_space(UiObject *obj) { |
82 char* ui_label_get(UiString *s) { |
|
83 if(s->value.ptr) { |
|
84 s->value.free(s->value.ptr); |
|
85 } |
|
86 s->value.ptr = g_strdup(gtk_label_get_text(GTK_LABEL(s->obj))); |
|
87 s->value.free = (ui_freefunc)g_free; |
|
88 return s->value.ptr; |
|
89 } |
|
90 |
|
91 void ui_label_set(UiString *s, const char *value) { |
|
92 gtk_label_set_text(GTK_LABEL(s->obj), value); |
|
93 if(s->value.ptr) { |
|
94 s->value.free(s->value.ptr); |
|
95 s->value.ptr = NULL; |
|
96 s->value.free = NULL; |
|
97 } |
|
98 } |
|
99 |
|
100 UIWIDGET ui_space_deprecated(UiObject *obj) { |
70 GtkWidget *widget = gtk_label_new(""); |
101 GtkWidget *widget = gtk_label_new(""); |
71 UiContainer *ct = uic_get_current_container(obj); |
102 UiContainer *ct = uic_get_current_container(obj); |
72 ct->add(ct, widget, TRUE); |
103 ct->add(ct, widget, TRUE); |
73 |
104 |
74 return widget; |
105 return widget; |
75 } |
106 } |
76 |
107 |
77 UIWIDGET ui_separator(UiObject *obj) { |
108 UIWIDGET ui_separator_deprecated(UiObject *obj) { |
78 #if UI_GTK3 |
109 #if UI_GTK3 |
79 GtkWidget *widget = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL); |
110 GtkWidget *widget = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL); |
80 #else |
111 #else |
81 GtkWidget *widget = gtk_hseparator_new(); |
112 GtkWidget *widget = gtk_hseparator_new(); |
82 #endif |
113 #endif |
86 return widget; |
117 return widget; |
87 } |
118 } |
88 |
119 |
89 /* ------------------------- progress bar ------------------------- */ |
120 /* ------------------------- progress bar ------------------------- */ |
90 |
121 |
91 UIWIDGET ui_progressbar(UiObject *obj, UiDouble *value) { |
122 UIWIDGET ui_progressbar_create(UiObject *obj, UiProgressbarArgs args) { |
92 UiVar *var = malloc(sizeof(UiVar)); |
123 UiObject* current = uic_current_obj(obj); |
93 var->value = value; |
124 |
94 var->type = UI_VAR_SPECIAL; |
|
95 return ui_progressbar_var(obj, var); |
|
96 } |
|
97 |
|
98 UIWIDGET ui_progressbar_nv(UiObject *obj, char *varname) { |
|
99 UiVar *var = uic_create_var(obj->ctx, varname, UI_VAR_DOUBLE); |
|
100 return ui_progressbar_var(obj, var); |
|
101 } |
|
102 |
|
103 UIWIDGET ui_progressbar_var(UiObject *obj, UiVar *var) { |
|
104 GtkWidget *progressbar = gtk_progress_bar_new(); |
125 GtkWidget *progressbar = gtk_progress_bar_new(); |
|
126 |
|
127 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_DOUBLE); |
105 if(var && var->value) { |
128 if(var && var->value) { |
106 UiDouble *value = var->value; |
129 UiDouble *value = var->value; |
107 value->get = ui_progressbar_get; |
130 value->get = ui_progressbar_get; |
108 value->set = ui_progressbar_set; |
131 value->set = ui_progressbar_set; |
109 value->obj = progressbar; |
132 value->obj = progressbar; |
110 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar), 0.5); |
133 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progressbar), 0.5); |
111 } |
134 } |
112 |
135 |
113 UiContainer *ct = uic_get_current_container(obj); |
136 UI_APPLY_LAYOUT1(current, args); |
114 ct->add(ct, progressbar, FALSE); |
137 current->container->add(current->container, progressbar, FALSE); |
115 |
138 |
116 return progressbar; |
139 return progressbar; |
117 } |
140 } |
118 |
141 |
119 double ui_progressbar_get(UiDouble *d) { |
142 double ui_progressbar_get(UiDouble *d) { |
123 |
146 |
124 void ui_progressbar_set(UiDouble *d, double value) { |
147 void ui_progressbar_set(UiDouble *d, double value) { |
125 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(d->obj), value); |
148 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(d->obj), value); |
126 d->value = value; |
149 d->value = value; |
127 } |
150 } |
|
151 |
|
152 |
|
153 /* ------------------------- progress spinner ------------------------- */ |
|
154 |
|
155 UIWIDGET ui_progressspinner_create(UiObject* obj, UiProgressbarSpinnerArgs args) { |
|
156 UiObject* current = uic_current_obj(obj); |
|
157 |
|
158 GtkWidget *spinner = gtk_spinner_new(); |
|
159 |
|
160 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_INTEGER); |
|
161 if(var && var->value) { |
|
162 UiInteger *value = var->value; |
|
163 value->get = ui_spinner_get; |
|
164 value->set = ui_spinner_set; |
|
165 value->obj = spinner; |
|
166 ui_spinner_set(value, value->value); |
|
167 } |
|
168 |
|
169 UI_APPLY_LAYOUT1(current, args); |
|
170 current->container->add(current->container, spinner, FALSE); |
|
171 |
|
172 return spinner; |
|
173 } |
|
174 |
|
175 int64_t ui_spinner_get(UiInteger *i) { |
|
176 return i->value; |
|
177 } |
|
178 |
|
179 void ui_spinner_set(UiInteger *i, int64_t value) { |
|
180 i->value = value; |
|
181 if(i->obj) { |
|
182 GtkSpinner *spinner = GTK_SPINNER(i->obj); |
|
183 if(value != 0) { |
|
184 gtk_spinner_start(spinner); |
|
185 } else { |
|
186 gtk_spinner_stop(spinner); |
|
187 } |
|
188 } |
|
189 } |