GCC Code Coverage Report


Directory: src/gate/
File: src/gate/ui/labels.c
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 0 18 0.0%
Functions: 0 1 0.0%
Branches: 0 6 0.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/ui/labels.h"
30 #include "gate/results.h"
31
32 #if defined(GATE_UI_WINAPI)
33
34
35 #include "gate/ui/gateui_winapi.h"
36 #include "gate/platforms.h"
37
38 static gate_bool_t gate_ui_label_events(void* hwnd, gate_ui_ctrl_t* ctrl, gate_uint32_t msg, gate_uintptr_t wParam, gate_intptr_t lParam, gate_intptr_t* lresult)
39 {
40 HWND hwndCtrl;
41 gate_ui_label_t* label;
42 DRAWITEMSTRUCT* drw;
43 HDC hdc;
44 TCHAR txtbuffer[1024];
45 int txtbufferused;
46 gate_uint32_t styles;
47 UINT flags;
48 HBRUSH hbrush;
49 RECT rect;
50
51 if ((ctrl != NULL) && (hwnd != NULL))
52 {
53 hwndCtrl = GATE_UI_WINAPI_GET_HWND(ctrl);
54 if (NULL != hwndCtrl)
55 {
56 switch (msg)
57 {
58 case WM_SIZE:
59 {
60 if (hwndCtrl == (HWND)hwnd)
61 {
62 InvalidateRect(hwndCtrl, NULL, TRUE);
63 }
64 return false;
65 break;
66 }
67 case WM_COMMAND:
68 {
69 if (hwndCtrl == (HWND)lParam)
70 {
71 label = (gate_ui_label_t*)ctrl;
72 switch (HIWORD(wParam))
73 {
74 #if !defined(GATE_SYS_WIN16)
75 case STN_CLICKED:
76 {
77 if (label->on_click != NULL)
78 {
79 label->on_click(ctrl);
80 }
81 return true;
82 }
83 #endif
84 default:
85 {
86 break;
87 }
88 }
89 }
90 break;
91 }
92 case WM_DRAWITEM:
93 {
94 drw = (DRAWITEMSTRUCT*)lParam;
95 if ((drw->hwndItem == hwndCtrl)
96 #if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WIN16)
97 && (drw->CtlType == ODT_STATIC)
98 #endif
99 )
100 {
101 hdc = drw->hDC;
102 txtbufferused = GetWindowText(drw->hwndItem, txtbuffer, sizeof(txtbuffer) / sizeof(txtbuffer[0]));
103
104 rect = drw->rcItem;
105 GetClientRect(drw->hwndItem, &rect);
106 styles = (gate_uint32_t)(gate_uintptr_t)GATE_UI_WINAPI_GET_CTRL_PARAM(ctrl);
107 flags = DT_VCENTER | DT_SINGLELINE | DT_NOCLIP;
108 if (styles & GATE_UI_FLAG_LABEL_RIGHT)
109 {
110 flags |= DT_RIGHT;
111 }
112 else if (styles & GATE_UI_FLAG_LABEL_CENTER)
113 {
114 flags |= DT_CENTER;
115 }
116 else
117 {
118 flags |= DT_LEFT;
119 }
120
121 #if defined(GATE_SYS_WIN16)
122 hbrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
123 #else
124 hbrush = (HBRUSH)SendMessage(GetParent(hwndCtrl), WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwndCtrl);
125 #endif
126 FillRect(hdc, &rect, hbrush);
127
128 DrawText(hdc, txtbuffer, txtbufferused, &rect, flags);
129 *lresult = TRUE;
130 return true;
131 }
132 }
133 default:
134 {
135 break;
136 }
137 }
138 }
139 }
140 return false;
141 }
142
143 gate_result_t gate_ui_label_create(
144 gate_ui_label_t* label, gate_ui_ctrl_t* parent,
145 gate_ui_position_t const* position,
146 gate_string_t const* caption,
147 gate_uint32_t flags,
148 void* userparam
149 )
150 {
151 gate_result_t ret;
152 gate_uint32_t styles, exstyles;
153 gate_ui_host_t* host;
154 HWND hwndParent;
155 HWND hwndCtrl;
156 DWORD winversion_major = 0;
157
158 do
159 {
160 host = gate_ui_ctrl_get_host(parent);
161 if (host == NULL)
162 {
163 ret = GATE_RESULT_INVALIDSTATE;
164 break;
165 }
166 hwndParent = GATE_UI_WINAPI_GET_HWND(parent);
167 if (hwndParent == NULL)
168 {
169 ret = GATE_RESULT_INVALIDSTATE;
170 break;
171 }
172
173 gate_mem_clear(label, sizeof(gate_ui_label_t));
174
175 exstyles = 0;
176 styles = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
177 #if !defined(GATE_SYS_WIN16)
178 styles |= SS_NOTIFY;
179 #if !defined(GATE_SYS_WINCE)
180 if (!GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_MULTILINE))
181 {
182 gate_win32_get_version(&winversion_major, NULL, NULL, NULL);
183 if (winversion_major >= 4)
184 {
185 styles |= SS_OWNERDRAW;
186 }
187 else
188 {
189 /* owner-drawn static controls not supported before NT4/9x */
190 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_RIGHT))
191 {
192 styles |= SS_RIGHT;
193 }
194 else if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_CENTER))
195 {
196 styles |= SS_CENTER;
197 }
198 else
199 {
200 styles |= SS_LEFTNOWORDWRAP;
201 }
202 }
203 }
204 else
205 #endif
206 #endif
207 {
208 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_RIGHT))
209 {
210 styles |= SS_RIGHT;
211 }
212 else if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_CENTER))
213 {
214 styles |= SS_CENTER;
215 }
216 else
217 {
218 styles |= SS_LEFT;
219 }
220 }
221
222 GATE_UI_WINAPI_SET_CTRL_PARAM(&label->ctrl, (gate_uintptr_t)flags);
223
224 if (!GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_ENABLED)) styles |= WS_DISABLED;
225 //if(GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_VISIBLE)) styles |= WS_VISIBLE;
226
227 ret = gate_ui_winapi_create(&label->ctrl, host, hwndParent, _T("STATIC"), position, styles, exstyles, caption, userparam, true);
228 if (GATE_SUCCEEDED(ret))
229 {
230 hwndCtrl = GATE_UI_WINAPI_GET_HWND(&label->ctrl);
231 gate_ui_winapi_register_event(host, hwndParent, WM_COMMAND, &gate_ui_label_events, &label->ctrl);
232 gate_ui_winapi_register_event(host, hwndParent, WM_DRAWITEM, &gate_ui_label_events, &label->ctrl);
233 gate_ui_winapi_register_event(host, hwndCtrl, WM_SIZE, &gate_ui_label_events, &label->ctrl);
234 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_VISIBLE))
235 {
236 gate_ui_ctrl_set_visible(&label->ctrl, true);
237 }
238 }
239 } while (0);
240
241 return ret;
242 }
243
244 #endif /*GATE_UI_WINAPI*/
245
246
247
248 #if defined(GATE_UI_GTK)
249
250 #include "gate/ui/gateui_gtk.h"
251
252 static gate_uint32_t gate_ui_gtk_label_get_text_length(gate_ui_ctrl_t* ctrl)
253 {
254 gint char_count = 0;
255 gchar const* ptr_text = NULL;
256 GtkLabel* label = GTK_LABEL(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl));
257 if (label)
258 {
259 ptr_text = gtk_label_get_text(label);
260 if (ptr_text)
261 {
262 char_count = gate_str_length(ptr_text);
263 }
264 }
265 return (gate_uint32_t)char_count;
266 }
267 static gate_result_t gate_ui_gtk_label_get_text(gate_ui_ctrl_t* ctrl, gate_string_t* text)
268 {
269 gate_result_t ret = GATE_RESULT_FAILED;
270 gchar const* ptr_text = NULL;
271 GtkLabel* label = GTK_LABEL(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl));
272 if (label)
273 {
274 ptr_text = gtk_label_get_text(label);
275 if (ptr_text)
276 {
277 if (NULL == gate_string_create(text, ptr_text, gate_str_length(ptr_text)))
278 {
279 ret = GATE_RESULT_OUTOFMEMORY;
280 }
281 else
282 {
283 ret = GATE_RESULT_OK;
284 }
285 }
286 else
287 {
288 gate_string_create_empty(text);
289 ret = GATE_RESULT_OK;
290 }
291 }
292 return ret;
293
294 }
295 static gate_result_t gate_ui_gtk_label_set_text(gate_ui_ctrl_t* ctrl, gate_string_t const* text)
296 {
297 gate_result_t ret = GATE_RESULT_FAILED;
298 gchar text_buffer[4096];
299 GtkLabel* label = GTK_LABEL(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl));
300 if (label)
301 {
302 gate_str_print_text(text_buffer, sizeof(text_buffer), text->str, text->length);
303 gtk_label_set_text(label, text_buffer);
304 ret = GATE_RESULT_OK;
305 }
306 return ret;
307 }
308
309 static gate_ui_gtk_dispatcher_t gate_ui_gtk_label_dispatcher =
310 {
311 gate_ui_gtk_label_get_text_length,
312 gate_ui_gtk_label_get_text,
313 gate_ui_gtk_label_set_text,
314 NULL,
315 NULL,
316 NULL
317 };
318
319 gate_result_t gate_ui_label_create(gate_ui_label_t* label, gate_ui_ctrl_t* parent, gate_ui_position_t const* position,
320 gate_string_t const* caption, gate_uint32_t flags, void* userparam)
321 {
322 gate_result_t ret = GATE_RESULT_OK;
323 GtkWidget* widget;
324 gate_ui_host_t* host = GATE_UI_GTK_GET_CTRL_HOST(parent);
325
326 gate_mem_clear(label, sizeof(gate_ui_label_t));
327 widget = gtk_label_new(NULL);
328 if (widget == NULL)
329 {
330 return GATE_RESULT_FAILED;
331 }
332
333 ret = gate_ui_gtk_ctrl_init(&label->ctrl, widget, host, userparam, parent,
334 &gate_ui_gtk_label_dispatcher, false, false, position, &flags);
335
336 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_RIGHT))
337 {
338 gtk_label_set_xalign(GTK_LABEL(widget), 1.0f);
339 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_RIGHT);
340 }
341 else if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_CENTER))
342 {
343 gtk_label_set_xalign(GTK_LABEL(widget), 0.5f);
344 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
345 }
346 else
347 {
348 gtk_label_set_xalign(GTK_LABEL(widget), 0.0f);
349 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_LEFT);
350 }
351
352 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_MULTILINE))
353 {
354 }
355
356
357
358 if (gate_string_length(caption) > 0)
359 {
360 gate_ui_ctrl_set_text(&label->ctrl, caption);
361 }
362 return ret;
363 }
364
365 #endif /* GATE_UI_GTK */
366
367
368
369 #if defined(GATE_UI_MOTIF)
370
371 #include "gate/ui/gateui_motif.h"
372 #include <Xm/Label.h>
373
374 gate_result_t gate_ui_label_create(gate_ui_label_t* label, gate_ui_ctrl_t* parent, gate_ui_position_t const* position,
375 gate_string_t const* caption, gate_uint32_t flags, void* userparam)
376 {
377 gate_result_t ret = GATE_RESULT_FAILED;
378 Widget w = NULL;
379 Arg args[12];
380 unsigned args_count = 0;
381
382 do
383 {
384 if (!label || !parent)
385 {
386 ret = GATE_RESULT_INVALIDARG;
387 break;
388 }
389
390 XtSetArg(args[args_count], XmNalignment, XmALIGNMENT_BEGINNING); ++args_count;
391 XtSetArg(args[args_count], XmNrecomputeSize, 0); ++args_count;
392 XtSetArg(args[args_count], XmNmarginLeft, 0); ++args_count;
393 XtSetArg(args[args_count], XmNmarginTop, 0); ++args_count;
394 XtSetArg(args[args_count], XmNmarginRight, 0); ++args_count;
395 XtSetArg(args[args_count], XmNmarginBottom, 0); ++args_count;
396
397 ret = gate_ui_motif_ctrl_create(&label->ctrl, xmLabelWidgetClass, userparam, NULL, parent,
398 position, &flags, false, NULL, args, args_count);
399 GATE_BREAK_IF_FAILED(ret);
400
401 w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&label->ctrl);
402 ret = gate_ui_motif_widget_set_label(w, caption);
403 } while (0);
404
405 return ret;
406 }
407 #endif /* GATE_UI_MOTIF */
408
409 #if defined(GATE_UI_WASMHTML)
410
411 #include "gate/ui/gateui_wasmhtml.h"
412
413 static gate_string_t const html_class_label = GATE_STRING_INIT_STATIC("div");
414
415 gate_result_t gate_ui_label_create(gate_ui_label_t* label, gate_ui_ctrl_t* parent,
416 gate_ui_position_t const* position, gate_string_t const* caption,
417 gate_uint32_t flags, void* userparam)
418 {
419 gate_result_t ret = GATE_RESULT_FAILED;
420 gate_ui_host_t* host = gate_ui_ctrl_get_host(parent);
421 gate_uint32_t parent_id = gate_ui_wasm_ctrl_get_container(parent);
422 gate_uint32_t new_id = 0;
423
424 gate_ui_wasm_new_ctrl_id(host, &new_id);
425 ret = gate_ui_wasm_ctrl_create(host, new_id, parent_id, &html_class_label, userparam, &label->ctrl);
426 if (position)
427 {
428 gate_ui_wasm_ctrl_set_position(&label->ctrl, &position->pos, &position->size);
429 }
430 gate_ui_wasm_ctrl_set_text(&label->ctrl, caption);
431 return ret;
432
433 }
434
435 #endif /* GATE_UI_WASMHTML */
436