GCC Code Coverage Report


Directory: src/gate/
File: src/gate/ui/labels.c
Date: 2025-12-12 23:40:09
Exec Total Coverage
Lines: 34 37 91.9%
Functions: 3 3 100.0%
Branches: 10 20 50.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 if ((ctrl != NULL) && (hwnd != NULL))
41 {
42 HWND hwndCtrl = GATE_UI_WINAPI_GET_HWND(ctrl);
43 if (NULL != hwndCtrl)
44 {
45 switch (msg)
46 {
47 case WM_SIZE:
48 {
49 if (hwndCtrl == (HWND)hwnd)
50 {
51 InvalidateRect(hwndCtrl, NULL, TRUE);
52 }
53 return false;
54 }
55 case WM_COMMAND:
56 {
57 if (hwndCtrl == (HWND)lParam)
58 {
59 gate_ui_label_t* label = (gate_ui_label_t*)ctrl;
60 switch (HIWORD(wParam))
61 {
62 #if !defined(GATE_SYS_WIN16)
63 case STN_CLICKED:
64 {
65 if (label->on_click != NULL)
66 {
67 label->on_click(ctrl);
68 }
69 return true;
70 }
71 #endif
72 default:
73 {
74 break;
75 }
76 }
77 }
78 break;
79 }
80 case WM_DRAWITEM:
81 {
82 DRAWITEMSTRUCT* drw = (DRAWITEMSTRUCT*)lParam;
83 if ((drw->hwndItem == hwndCtrl)
84 #if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WIN16)
85 && (drw->CtlType == ODT_STATIC)
86 #endif
87 )
88 {
89 HDC hdc = drw->hDC;
90 gate_uint32_t styles = (gate_uint32_t)(gate_uintptr_t)GATE_UI_WINAPI_GET_CTRL_PARAM(ctrl);
91 UINT flags = DT_VCENTER | DT_SINGLELINE | DT_NOCLIP;
92 RECT rect = drw->rcItem;
93 HBRUSH hbrush;
94 TCHAR txtbuffer[1024];
95 int txtbufferused = GetWindowText(drw->hwndItem, txtbuffer, sizeof(txtbuffer) / sizeof(txtbuffer[0]));
96
97 GetClientRect(drw->hwndItem, &rect);
98
99 if (styles & GATE_UI_FLAG_LABEL_RIGHT)
100 {
101 flags |= DT_RIGHT;
102 }
103 else if (styles & GATE_UI_FLAG_LABEL_CENTER)
104 {
105 flags |= DT_CENTER;
106 }
107 else
108 {
109 flags |= DT_LEFT;
110 }
111
112 #if defined(GATE_SYS_WIN16)
113 hbrush = (HBRUSH)GetStockObject(WHITE_BRUSH);
114 #else
115 hbrush = (HBRUSH)SendMessage(GetParent(hwndCtrl), WM_CTLCOLORSTATIC, (WPARAM)hdc, (LPARAM)hwndCtrl);
116 #endif
117 FillRect(hdc, &rect, hbrush);
118 DrawText(hdc, txtbuffer, txtbufferused, &rect, flags);
119 *lresult = TRUE;
120 return true;
121 }
122 }
123 default:
124 {
125 break;
126 }
127 }
128 }
129 }
130 return false;
131 }
132
133 gate_result_t gate_ui_label_perform_action(gate_ui_label_t* label, gate_enumint_t action)
134 {
135 gate_ui_ctrl_t* const ctrl = &label->ctrl;
136 HWND const hwnd = GATE_UI_WINAPI_GET_HWND(ctrl);
137 gate_intptr_t lresult = 0;
138 switch (action)
139 {
140 case GATE_UI_ACTION_ACTIVATE:
141 gate_ui_label_events(hwnd, ctrl, WM_COMMAND, (gate_uintptr_t)MAKELONG(0, STN_CLICKED), (gate_uintptr_t)hwnd, &lresult);
142 return GATE_RESULT_OK;
143 default:
144 break;
145 }
146 return GATE_RESULT_NOTSUPPORTED;
147 }
148
149 gate_result_t gate_ui_label_create(
150 gate_ui_label_t* label, gate_ui_ctrl_t* parent,
151 gate_ui_position_t const* position,
152 gate_string_t const* caption,
153 gate_uint32_t flags,
154 void* userparam
155 )
156 {
157 gate_result_t ret;
158 do
159 {
160 gate_uint32_t styles, exstyles;
161 HWND hwndParent;
162 DWORD winversion_major = 0;
163 gate_ui_host_t* host = gate_ui_ctrl_get_host(parent);
164 if (host == NULL)
165 {
166 ret = GATE_RESULT_INVALIDSTATE;
167 break;
168 }
169 hwndParent = GATE_UI_WINAPI_GET_HWND(parent);
170 if (hwndParent == NULL)
171 {
172 ret = GATE_RESULT_INVALIDSTATE;
173 break;
174 }
175
176 gate_mem_clear(label, sizeof(gate_ui_label_t));
177
178 exstyles = 0;
179 styles = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
180 #if !defined(GATE_SYS_WIN16)
181 styles |= SS_NOTIFY;
182 #if !defined(GATE_SYS_WINCE)
183 if (!GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_MULTILINE))
184 {
185 gate_win32_get_version(&winversion_major, NULL, NULL, NULL);
186 if (winversion_major >= 4)
187 {
188 styles |= SS_OWNERDRAW;
189 }
190 else
191 {
192 /* owner-drawn static controls not supported before NT4/9x */
193 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_RIGHT))
194 {
195 styles |= SS_RIGHT;
196 }
197 else if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_CENTER))
198 {
199 styles |= SS_CENTER;
200 }
201 else
202 {
203 styles |= SS_LEFTNOWORDWRAP;
204 }
205 }
206 }
207 else
208 #endif
209 #endif
210 {
211 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_RIGHT))
212 {
213 styles |= SS_RIGHT;
214 }
215 else if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_CENTER))
216 {
217 styles |= SS_CENTER;
218 }
219 else
220 {
221 styles |= SS_LEFT;
222 }
223 }
224
225 GATE_UI_WINAPI_SET_CTRL_PARAM(&label->ctrl, (gate_uintptr_t)flags);
226
227 if (!GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_ENABLED)) styles |= WS_DISABLED;
228 //if(GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_VISIBLE)) styles |= WS_VISIBLE;
229
230 ret = gate_ui_winapi_create(&label->ctrl, host, hwndParent, _T("STATIC"), position, styles, exstyles, caption, userparam, true);
231 if (GATE_SUCCEEDED(ret))
232 {
233 HWND hwndCtrl = GATE_UI_WINAPI_GET_HWND(&label->ctrl);
234 gate_ui_winapi_register_event(host, hwndParent, WM_COMMAND, &gate_ui_label_events, &label->ctrl);
235 gate_ui_winapi_register_event(host, hwndParent, WM_DRAWITEM, &gate_ui_label_events, &label->ctrl);
236 gate_ui_winapi_register_event(host, hwndCtrl, WM_SIZE, &gate_ui_label_events, &label->ctrl);
237 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_VISIBLE))
238 {
239 gate_ui_ctrl_set_visible(&label->ctrl, true);
240 }
241 }
242 } while (0);
243
244 return ret;
245 }
246
247
248 #endif /*GATE_UI_WINAPI*/
249
250
251
252 #if defined(GATE_UI_GTK)
253
254 #include "gate/ui/gateui_gtk.h"
255
256 static gate_uint32_t gate_ui_gtk_label_get_text_length(gate_ui_ctrl_t* ctrl)
257 {
258 gint char_count = 0;
259 GtkLabel* label = GTK_LABEL(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl));
260 if (label)
261 {
262 gchar const* ptr_text = gtk_label_get_text(label);
263 if (ptr_text)
264 {
265 char_count = gate_str_length(ptr_text);
266 }
267 }
268 return (gate_uint32_t)char_count;
269 }
270 static gate_result_t gate_ui_gtk_label_get_text(gate_ui_ctrl_t* ctrl, gate_string_t* text)
271 {
272 gate_result_t ret = GATE_RESULT_FAILED;
273 GtkLabel* label = GTK_LABEL(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl));
274 if (label)
275 {
276 gchar const* ptr_text = gtk_label_get_text(label);
277 if (ptr_text)
278 {
279 if (NULL == gate_string_create(text, ptr_text, gate_str_length(ptr_text)))
280 {
281 ret = GATE_RESULT_OUTOFMEMORY;
282 }
283 else
284 {
285 ret = GATE_RESULT_OK;
286 }
287 }
288 else
289 {
290 gate_string_create_empty(text);
291 ret = GATE_RESULT_OK;
292 }
293 }
294 return ret;
295
296 }
297 static gate_result_t gate_ui_gtk_label_set_text(gate_ui_ctrl_t* ctrl, gate_string_t const* text)
298 {
299 gate_result_t ret = GATE_RESULT_FAILED;
300 GtkLabel* label = GTK_LABEL(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl));
301 if (label)
302 {
303 gchar text_buffer[8192];
304 gate_str_print_text(text_buffer, sizeof(text_buffer), text->str, text->length);
305 gtk_label_set_text(label, text_buffer);
306 ret = GATE_RESULT_OK;
307 }
308 return ret;
309 }
310
311 static gate_ui_gtk_dispatcher_t gate_ui_gtk_label_dispatcher =
312 {
313 gate_ui_gtk_label_get_text_length,
314 gate_ui_gtk_label_get_text,
315 gate_ui_gtk_label_set_text,
316 NULL,
317 NULL,
318 NULL
319 };
320
321 static void gate_ui_label_clicked(GtkWidget* label, gpointer user_data)
322 {
323 gate_ui_label_t* label_ctrl = (gate_ui_label_t*)user_data;
324 if (label_ctrl)
325 {
326 if (label_ctrl->on_click != NULL)
327 {
328 label_ctrl->on_click(&label_ctrl->ctrl);
329 }
330 }
331 }
332
333 gate_result_t gate_ui_label_create(gate_ui_label_t* label, gate_ui_ctrl_t* parent, gate_ui_position_t const* position,
334 gate_string_t const* caption, gate_uint32_t flags, void* userparam)
335 {
336 gate_result_t ret = GATE_RESULT_OK;
337 GtkWidget* widget;
338 gate_ui_host_t* host = GATE_UI_GTK_GET_CTRL_HOST(parent);
339
340 gate_mem_clear(label, sizeof(gate_ui_label_t));
341 widget = gtk_label_new(NULL);
342 if (widget == NULL)
343 {
344 return GATE_RESULT_FAILED;
345 }
346
347 ret = gate_ui_gtk_ctrl_init(&label->ctrl, widget, host, userparam, parent,
348 &gate_ui_gtk_label_dispatcher, false, false, position, &flags);
349
350 if (GATE_SUCCEEDED(ret))
351 {
352 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_RIGHT))
353 {
354 gtk_label_set_xalign(GTK_LABEL(widget), 1.0f);
355 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_RIGHT);
356 }
357 else if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_CENTER))
358 {
359 gtk_label_set_xalign(GTK_LABEL(widget), 0.5f);
360 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
361 }
362 else
363 {
364 gtk_label_set_xalign(GTK_LABEL(widget), 0.0f);
365 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_LEFT);
366 }
367
368 if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_LABEL_MULTILINE))
369 {
370 }
371
372 if (gate_string_length(caption) > 0)
373 {
374 gate_ui_ctrl_set_text(&label->ctrl, caption);
375 }
376
377 g_signal_connect(G_OBJECT(widget), "clicked", G_CALLBACK(gate_ui_label_clicked), (gpointer)label);
378 }
379
380 return ret;
381 }
382
383 gate_result_t gate_ui_label_perform_action(gate_ui_label_t* label, gate_enumint_t action)
384 {
385 GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&label->ctrl);
386 switch(action)
387 {
388 case GATE_UI_ACTION_ACTIVATE:
389 gate_ui_label_clicked(widget, (gpointer)label);
390 return GATE_RESULT_OK;
391 }
392 return GATE_RESULT_NOTSUPPORTED;
393 }
394
395
396 #endif /* GATE_UI_GTK */
397
398
399
400 #if defined(GATE_UI_MOTIF)
401
402 #include "gate/ui/gateui_motif.h"
403 #include <Xm/Label.h>
404
405 1 static void gate_ui_label_event_onclick(Widget w, XtPointer ptr_data, XEvent* xevent, Boolean* ptr_continue_dispatch)
406 {
407 1 gate_ui_label_t* const label = (gate_ui_label_t*)ptr_data;
408
5/10
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
1 if (label && label->on_click && xevent && (xevent->type == ButtonPress) && (xevent->xbutton.button == Button1))
409 {
410 1 label->on_click(&label->ctrl);
411 }
412
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_continue_dispatch)
413 {
414 1 *ptr_continue_dispatch = FALSE;
415 }
416 1 }
417
418 1 gate_result_t gate_ui_label_perform_action(gate_ui_label_t* label, gate_enumint_t action)
419 {
420 1 Widget const w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&label->ctrl);
421 XEvent xevent;
422 1 Boolean continue_dispatch = TRUE;
423
424
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 switch(action)
425 {
426 1 case GATE_UI_ACTION_ACTIVATE:
427 1 gate_mem_clear(&xevent, sizeof(xevent));
428 1 xevent.xbutton.type = ButtonPress;
429 1 xevent.xbutton.button = Button1;
430 1 gate_ui_label_event_onclick(w, (XtPointer)label, &xevent, &continue_dispatch);
431 1 return GATE_RESULT_OK;
432 }
433 return GATE_RESULT_NOTSUPPORTED;
434 }
435
436
437 1 gate_result_t gate_ui_label_create(gate_ui_label_t* label, gate_ui_ctrl_t* parent, gate_ui_position_t const* position,
438 gate_string_t const* caption, gate_uint32_t flags, void* userparam)
439 {
440 1 gate_result_t ret = GATE_RESULT_FAILED;
441
442 do
443 {
444 1 Widget w = NULL;
445 Arg args[12];
446 1 unsigned args_count = 0;
447
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!label || !parent)
448 {
449 ret = GATE_RESULT_INVALIDARG;
450 break;
451 }
452
453 1 XtSetArg(args[args_count], XmNalignment, XmALIGNMENT_BEGINNING); ++args_count;
454 1 XtSetArg(args[args_count], XmNrecomputeSize, 0); ++args_count;
455 1 XtSetArg(args[args_count], XmNmarginLeft, 0); ++args_count;
456 1 XtSetArg(args[args_count], XmNmarginTop, 0); ++args_count;
457 1 XtSetArg(args[args_count], XmNmarginRight, 0); ++args_count;
458 1 XtSetArg(args[args_count], XmNmarginBottom, 0); ++args_count;
459
460 1 ret = gate_ui_motif_ctrl_create(&label->ctrl, xmLabelWidgetClass, userparam, NULL, parent,
461 position, &flags, false, NULL, args, args_count);
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATE_BREAK_IF_FAILED(ret);
463
464 1 w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&label->ctrl);
465 1 ret = gate_ui_motif_widget_set_label(w, caption);
466 1 XtAddEventHandler(w, ButtonPressMask, False, &gate_ui_label_event_onclick, label);
467
468 } while (0);
469
470 1 return ret;
471 }
472 #endif /* GATE_UI_MOTIF */
473
474 #if defined(GATE_UI_WASMHTML)
475
476 #include "gate/ui/gateui_wasmhtml.h"
477
478 static gate_string_t const html_class_label = GATE_STRING_INIT_STATIC("div");
479
480 gate_result_t gate_ui_label_create(gate_ui_label_t* label, gate_ui_ctrl_t* parent,
481 gate_ui_position_t const* position, gate_string_t const* caption,
482 gate_uint32_t flags, void* userparam)
483 {
484 gate_result_t ret = GATE_RESULT_FAILED;
485 gate_ui_host_t* host = gate_ui_ctrl_get_host(parent);
486 gate_uint32_t parent_id = gate_ui_wasm_ctrl_get_container(parent);
487 gate_uint32_t new_id = 0;
488
489 gate_ui_wasm_new_ctrl_id(host, &new_id);
490 ret = gate_ui_wasm_ctrl_create(host, new_id, parent_id, &html_class_label, userparam, &label->ctrl);
491 if (position)
492 {
493 gate_ui_wasm_ctrl_set_position(&label->ctrl, &position->pos, &position->size);
494 }
495 gate_ui_wasm_ctrl_set_text(&label->ctrl, caption);
496 return ret;
497 }
498
499 gate_result_t gate_ui_label_perform_action(gate_ui_label_t* label, gate_enumint_t action)
500 {
501 return GATE_RESULT_NOTSUPPORTED;
502 }
503
504
505 #endif /* GATE_UI_WASMHTML */
506