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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006 | #include "gate/ui/gateui_gtk.h"
#if defined(GATE_UI_GTK)
#include "gate/environments.h"
#include "gate/inputs.h"
#if defined(GATE_COMPILER_MSVC)
#pragma comment(lib, "atk-1.0.lib")
#pragma comment(lib, "cairo.lib")
#pragma comment(lib, "fontconfig.lib")
//#pragma comment(lib, "gailutil.lib")
#pragma comment(lib, "gdk-3.0.lib")
#pragma comment(lib, "gdk_pixbuf-2.0.lib")
#pragma comment(lib, "gio-2.0.lib")
#pragma comment(lib, "glib-2.0.lib")
#pragma comment(lib, "gmodule-2.0.lib")
#pragma comment(lib, "gobject-2.0.lib")
#pragma comment(lib, "gthread-2.0.lib")
#pragma comment(lib, "gtk-3.0.lib")
#pragma comment(lib, "pango-1.0.lib")
#pragma comment(lib, "pangocairo-1.0.lib")
#pragma comment(lib, "pangoft2-1.0.lib")
#pragma comment(lib, "pangowin32-1.0.lib")
#endif /* GATE_COMPILER_MSVC */
static char const gate_ui_gtk_widget_attachment_key[] = "gate_widget_ctrl";
static gate_ui_host_t* gate_ui_gtk_registered_host = NULL;
gate_result_t gate_ui_gtk_host_init(gate_ui_host_t* host, gate_uintptr_t apphandle, void* userparam)
{
gate_result_t ret;
int argc = 1;
char program_path[4096];
gate_size_t program_path_len = sizeof(program_path);
char* argv[] = { program_path };
char** ptrargv = argv;
gboolean caninit;
ret = gate_env_app_executable_str(program_path, &program_path_len);
if (GATE_FAILED(ret))
{
program_path[0] = 0; /* try without program path */<--- Variable 'program_path' is assigned a value that is never used.
}
caninit = gtk_init_check(&argc, &ptrargv);
if (!caninit)
{
ret = GATE_RESULT_FAILED;
}
else
{
gate_mem_clear(host, sizeof(gate_ui_host_t));
GATE_UI_GTK_SET_HOST_APPHANDLE(host, apphandle);
GATE_UI_GTK_SET_HOST_USER_PARAM(host, userparam);
ret = GATE_RESULT_OK;
}
return ret;
}
gate_result_t gate_ui_gtk_host_uninit(gate_ui_host_t* host)
{
gate_mem_clear(host, sizeof(gate_ui_host_t));
return GATE_RESULT_OK;
}
gate_result_t gate_ui_gtk_host_run(gate_ui_host_t* host)
{
gate_ui_gtk_registered_host = host;
gtk_main();
return GATE_RESULT_OK;
}
gate_result_t gate_ui_gtk_host_quit(gate_ui_host_t* host)
{
gtk_main_quit();
return GATE_RESULT_OK;
}
GtkWidget* gate_ui_gtk_ctrl_get_widget(gate_ui_ctrl_t* ctrl)
{
GtkWidget* ret = NULL;
if (ctrl != NULL)
{
ret = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
if (ret == GATE_UI_GTK_NO_WIDGET)
{
ret = NULL;
}
}
return ret;
}
gate_result_t gate_ui_gtk_ctrl_destroy_native(gate_ui_ctrl_t* ctrl)
{
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
if (widget != GATE_UI_GTK_NO_WIDGET)
{
g_object_set_data(G_OBJECT(widget), gate_ui_gtk_widget_attachment_key, NULL);
gtk_widget_destroy(widget);
}
GATE_UI_GTK_SET_CTRL_WIDGET(ctrl, NULL);
GATE_UI_GTK_SET_CTRL_HOST(ctrl, NULL);
GATE_UI_GTK_SET_CTRL_DISP(ctrl, NULL);
GATE_UI_GTK_SET_CTRL_CONTAINER(ctrl, NULL);
GATE_UI_GTK_SET_CTRL_PARENT(ctrl, NULL);
GATE_UI_GTK_SET_CTRL_PRIVATE_ARG(ctrl, NULL);
return GATE_RESULT_OK;
}
gate_result_t gate_ui_gtk_ctrl_destroy(gate_ui_ctrl_t* ctrl)
{
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
gate_ui_gtk_dispatcher_t* dispatcher = NULL;
if (widget == NULL)
{
return GATE_RESULT_NOTAVAILABLE;
}
else
{
dispatcher = GATE_UI_GTK_GET_CTRL_DISP(ctrl);
if ((dispatcher != NULL) && (dispatcher->destroy != NULL))
{
return dispatcher->destroy(ctrl);
}
else
{
return gate_ui_gtk_ctrl_destroy_native(ctrl);
}
}
}
gate_bool_t gate_ui_gtk_ctrl_is_visible(gate_ui_ctrl_t* ctrl)
{
gate_bool_t ret = false;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
if ((widget != NULL) && (widget != GATE_UI_GTK_NO_WIDGET))
{
ret = FALSE != gtk_widget_get_visible(widget);
}
return ret;
}
gate_result_t gate_ui_gtk_ctrl_set_visible(gate_ui_ctrl_t* ctrl, gate_bool_t visible)
{
gate_result_t ret;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
if (widget == NULL)
{
ret = GATE_RESULT_NOTAVAILABLE;
}
else
{
if (widget != GATE_UI_GTK_NO_WIDGET)
{
if (visible)
{
gtk_widget_show(widget);
}
else
{
gtk_widget_hide(widget);
}
ret = GATE_RESULT_OK;
}
else
{
ret = GATE_RESULT_INVALIDSTATE;
}
}
return ret;
}
gate_bool_t gate_ui_gtk_ctrl_is_enabled(gate_ui_ctrl_t* ctrl)
{
gate_bool_t ret = false;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
if ((widget != NULL) && (widget != GATE_UI_GTK_NO_WIDGET))
{
ret = FALSE != gtk_widget_get_sensitive(widget);
}
return ret;
}
gate_result_t gate_ui_gtk_ctrl_set_enabled(gate_ui_ctrl_t* ctrl, gate_bool_t enabled)
{
gate_result_t ret;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
if (widget == NULL)
{
ret = GATE_RESULT_NOTAVAILABLE;
}
else
{
if (GATE_UI_GTK_NO_WIDGET == widget)
{
ret = GATE_RESULT_INVALIDSTATE;
}
else
{
gtk_widget_set_sensitive(widget, enabled ? TRUE : FALSE);
ret = GATE_RESULT_OK;
}
}
return ret;
}
gate_bool_t gate_ui_gtk_ctrl_has_focus(gate_ui_ctrl_t* ctrl)
{
gate_bool_t ret = false;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
if ((widget != NULL) && (GATE_UI_GTK_NO_WIDGET != widget))
{
ret = FALSE != gtk_widget_is_focus(widget);
}
return ret;
}
gate_result_t gate_ui_gtk_ctrl_set_focus(gate_ui_ctrl_t* ctrl)
{
gate_result_t ret;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
if (widget == NULL)
{
ret = GATE_RESULT_NOTAVAILABLE;
}
else
{
if (GATE_UI_GTK_NO_WIDGET == widget)
{
ret = GATE_RESULT_INVALIDSTATE;
}
else
{
gtk_widget_grab_focus(widget);
ret = GATE_RESULT_OK;
}
}
return ret;
}
gate_result_t gate_ui_gtk_ctrl_get_position(gate_ui_ctrl_t* ctrl, gate_ui_position_t* pos)
{
gate_result_t ret;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
GtkAllocation widget_alloc;
GtkRequisition requisition, dummy;
/*gint x, y, width, height;*/
if (widget == NULL)
{
ret = GATE_RESULT_NOTAVAILABLE;
}
else
{
if ((pos != NULL) && (GATE_UI_GTK_NO_WIDGET != widget))
{
gtk_widget_get_allocation(widget, &widget_alloc);
pos->pos.x = widget_alloc.x;
pos->pos.y = widget_alloc.y;
pos->size.width = widget_alloc.width;
pos->size.height = widget_alloc.height;
//gtk_widget_size_request(widget, &requisition);
gtk_widget_get_preferred_size(widget, &dummy, &requisition);
//pos->pos.x = requisition.x;
//pos->pos.y = widget_alloc.y;
pos->size.width = requisition.width;
pos->size.height = requisition.height;
}
ret = GATE_RESULT_OK;
}
return ret;
}
gate_result_t gate_ui_gtk_ctrl_set_position(gate_ui_ctrl_t* ctrl, gate_ui_point_t const* position, gate_ui_size_t const* size)
{
gate_result_t ret = GATE_RESULT_FAILED;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
gate_ui_ctrl_t* parent = GATE_UI_GTK_GET_CTRL_PARENT(ctrl);
GtkContainer* parent_container = NULL;
GtkLayout* ptr_layout = NULL;
GtkWindow* ptr_window = NULL;
/*GtkAllocation gtkpos;*/
gint width = 0, height = 0;
do
{
if (widget == NULL)
{
ret = GATE_RESULT_NOTAVAILABLE;
break;
}
if (GATE_UI_GTK_NO_WIDGET == widget)
{
ret = GATE_RESULT_INVALIDSTATE;
break;
}
if (size != NULL)
{
width = size->width < 0 ? 0 : size->width;
height = size->height < 0 ? 0 : size->height;
}
if (parent != NULL)
{
parent_container = GATE_UI_GTK_GET_CTRL_CONTAINER(parent);
if (!parent_container && GTK_IS_CONTAINER(GATE_UI_GTK_GET_CTRL_WIDGET(parent)))
{
parent_container = GTK_CONTAINER(GATE_UI_GTK_GET_CTRL_WIDGET(parent));
}
}
if (parent_container != NULL)
{
/* moving a control in a container: */
if (position != NULL)
{
//gtk_fixed_move(GTK_FIXED(container), widget, position->x, position->y);
if (GTK_IS_LAYOUT(parent_container))
{
ptr_layout = GTK_LAYOUT(parent_container);
gtk_layout_move(ptr_layout, widget, position->x, position->y);
}
}
if (size != NULL)
{
gtk_widget_set_size_request(widget, width, height);
if (GTK_IS_LAYOUT(widget))
{
gtk_layout_set_size(GTK_LAYOUT(widget), width, height);
}
if (GTK_IS_CONTAINER(widget))
{
gtk_container_check_resize(GTK_CONTAINER(widget));
}
}
gate_ui_ctrl_refresh(ctrl);
ret = GATE_RESULT_OK;
break;
}
/* moving a window */
if (position != NULL)
{
if (GTK_IS_WINDOW(widget))
{
ptr_window = GTK_WINDOW(widget);
gtk_window_move(ptr_window, position->x, position->y);
}
}
if (size != NULL)
{
gtk_widget_set_size_request(widget, width, height);
if (GTK_IS_CONTAINER(widget))
{
gtk_container_check_resize(GTK_CONTAINER(widget));
}
}
gate_ui_ctrl_refresh(ctrl);
ret = GATE_RESULT_OK;
} while (0);
return ret;
}
gate_result_t gate_ui_gtk_ctrl_get_size(gate_ui_ctrl_t* ctrl, gate_ui_size_t* sz)
{
gate_ui_position_t pos;
gate_result_t ret = gate_ui_gtk_ctrl_get_position(ctrl, &pos);
if (GATE_SUCCEEDED(ret))
{
if (sz != NULL)
{
sz->width = pos.size.width;
sz->height = pos.size.height;
}
}
return ret;
}
gate_result_t gate_ui_gtk_ctrl_refresh(gate_ui_ctrl_t* ctrl)
{
gate_result_t ret;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
if (widget != NULL)
{
if (widget != GATE_UI_GTK_NO_WIDGET)
{
gtk_widget_queue_draw(widget);
}
ret = GATE_RESULT_OK;
}
else
{
ret = GATE_RESULT_INVALIDSTATE;
}
return ret;
}
gate_result_t gate_ui_gtk_ctrl_apply_layout(gate_ui_ctrl_t* ctrl, gate_ui_layout_t const* layout, gate_uint32_t lineheight)
{
gate_result_t ret = GATE_RESULT_OK;
gate_ui_host_t* host;<--- The scope of the variable 'host' can be reduced. [+]The scope of the variable 'host' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
GtkWidget* widget;<--- The scope of the variable 'widget' can be reduced. [+]The scope of the variable 'widget' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
GtkContainer* subcontainer = GATE_UI_GTK_GET_CTRL_CONTAINER(ctrl);
gate_ui_position_t position;
GtkAllocation widget_alloc;
if ((layout != NULL) && (ctrl != NULL))
{
host = GATE_UI_GTK_GET_CTRL_HOST(ctrl);
subcontainer = GATE_UI_GTK_GET_CTRL_CONTAINER(ctrl);
if (subcontainer != NULL)
{
widget = GTK_WIDGET(subcontainer);
}
else
{
widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
}
if (lineheight == 0)
{
lineheight = gate_ui_host_default_control_height(host, 1);
}
gtk_widget_get_allocation(widget, &widget_alloc);
position.pos.x = 0;
position.pos.y = 0;
position.size.width = widget_alloc.width;
position.size.height = widget_alloc.height;
ret = gate_ui_layout_apply(layout, lineheight, &position);
}
return ret;
}
static gboolean gate_ui_ctrl_refresh_idle_callback(gpointer user_data)
{
gate_ui_ctrl_t* ctrl = (gate_ui_ctrl_t*)user_data;
gate_result_t res = gate_ui_ctrl_refresh(ctrl);
return FALSE; /*FALSE means: do not call me again (until next g_idle_add() is invoked) */
}
static gboolean gate_ui_ctrl_event_configure(GtkWidget* widget, GdkEvent* event, gpointer user_data)
{
gate_ui_ctrl_t* ctrl = (gate_ui_ctrl_t*)user_data;
g_idle_add(&gate_ui_ctrl_refresh_idle_callback, ctrl);
return FALSE;
}
static void gate_ui_ctrl_event_size_allocate(GtkWidget* widget, GdkRectangle* allocation, gpointer user_data)
{
gate_ui_ctrl_t* ctrl = (gate_ui_ctrl_t*)user_data;
g_idle_add(&gate_ui_ctrl_refresh_idle_callback, ctrl);
}
gate_result_t gate_ui_gtk_ctrl_init(gate_ui_ctrl_t* ctrl, GtkWidget* widget, gate_ui_host_t* host, void* userparam,
gate_ui_ctrl_t* parent, gate_ui_gtk_dispatcher_t* dispatcher,
bool attach_subcontainer, bool refresh_on_resize,
gate_ui_position_t const* position, gate_uint32_t const* flags)
{
GtkContainer* parentContainer;<--- The scope of the variable 'parentContainer' can be reduced. [+]The scope of the variable 'parentContainer' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
GtkWidget* subcontainer = NULL;
gate_bool_t parent_missing = !(!flags || !GATE_FLAG_ENABLED(*flags, GATE_UI_FLAG_NOPARENT));
if (attach_subcontainer)
{
subcontainer = gtk_layout_new(NULL, NULL);
if (subcontainer == NULL)
{
return GATE_RESULT_OUTOFRESOURCES;
}
}
if (parent != NULL)
{
if ((flags == NULL) || (!GATE_FLAG_ENABLED(*flags, GATE_UI_FLAG_NOPARENT)))
{
parentContainer = GATE_UI_GTK_GET_CTRL_CONTAINER(parent);
if (parentContainer == NULL)
{
if (GTK_IS_CONTAINER(GATE_UI_GTK_GET_CTRL_WIDGET(parent)))
{
parentContainer = GTK_CONTAINER(GATE_UI_GTK_GET_CTRL_WIDGET(parent));
}
}
if (position != NULL)
{
if (parentContainer)
{
if (GTK_IS_LAYOUT(parentContainer))
{
gtk_layout_put(GTK_LAYOUT(parentContainer), widget, position->pos.x, position->pos.y);
//g_object_ref(widget);
}
else
{
parent_missing = true;
}
}
else
{
parent_missing = true;
}
gtk_widget_set_size_request(widget, position->size.width, position->size.height);
}
else
{
if (parentContainer)
{
if (GTK_IS_LAYOUT(parentContainer))
{
gtk_layout_put(GTK_LAYOUT(parentContainer), widget, 0, 0);
//g_object_ref(widget);
}
else
{
parent_missing = true;
}
}
else
{
parent_missing = true;
}
}
}
}
else
{
if (position != NULL)
{
if (GTK_IS_WINDOW(widget))
{
gtk_window_set_default_size(GTK_WINDOW(widget), position->size.width, position->size.height);
}
}
}
GATE_UI_GTK_SET_CTRL_WIDGET(ctrl, widget);
GATE_UI_GTK_SET_CTRL_HOST(ctrl, host);
GATE_UI_GTK_SET_CTRL_USER_PARAM(ctrl, userparam);
GATE_UI_GTK_SET_CTRL_PARENT(ctrl, parent);
GATE_UI_GTK_SET_CTRL_DISP(ctrl, dispatcher);
if (subcontainer != NULL)
{
GATE_UI_GTK_SET_CTRL_CONTAINER(ctrl, subcontainer);
gtk_container_add(GTK_CONTAINER(widget), subcontainer);
if (!parent_missing)
{
gtk_widget_realize(widget);
gtk_widget_realize(subcontainer);
}
gtk_widget_set_redraw_on_allocate(widget, TRUE);
gtk_widget_set_redraw_on_allocate(subcontainer, TRUE);
gtk_widget_show(subcontainer);
}
else
{
if (!parent_missing)
{
gtk_widget_realize(widget);
}
gtk_widget_set_redraw_on_allocate(widget, TRUE);
}
if (refresh_on_resize)
{
g_signal_connect(G_OBJECT(widget), "configure-event", G_CALLBACK(gate_ui_ctrl_event_configure), (gpointer)ctrl);
g_signal_connect(G_OBJECT(widget), "size-allocate", G_CALLBACK(gate_ui_ctrl_event_size_allocate), (gpointer)ctrl);
}
if (flags != NULL)
{
gate_ui_gtk_ctrl_set_visible(ctrl, GATE_FLAG_ENABLED(*flags, GATE_UI_FLAG_VISIBLE));
gate_ui_gtk_ctrl_set_enabled(ctrl, GATE_FLAG_ENABLED(*flags, GATE_UI_FLAG_ENABLED));
}
g_object_set_data(G_OBJECT(widget), gate_ui_gtk_widget_attachment_key, (gpointer)ctrl);
return GATE_RESULT_OK;
}
gate_ui_ctrl_t* gate_ui_gtk_ctrl_resolve(GtkWidget* widget)
{
gpointer ptr = g_object_get_data(G_OBJECT(widget), gate_ui_gtk_widget_attachment_key);
if (ptr)
{
return (gate_ui_ctrl_t*)ptr;
}
return NULL;
}
gate_result_t gate_ui_gtk_ctrl_get_children(gate_ui_ctrl_t* ctrl, gate_array_t* children)
{
gate_result_t ret = GATE_RESULT_INVALIDSTATE;
GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
GtkContainer* cont;<--- The scope of the variable 'cont' can be reduced. [+]The scope of the variable 'cont' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
GtkWidget* child_widget;<--- The scope of the variable 'child_widget' can be reduced. [+]The scope of the variable 'child_widget' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_ui_ctrl_t* child_ctrl;
GList* list = NULL;
GList* entry;<--- The scope of the variable 'entry' can be reduced. [+]The scope of the variable 'entry' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_arraylist_t childrenlist = NULL;
gate_array_create_empty(children);
if (widget)
{
do
{
childrenlist = gate_arraylist_create(sizeof(gate_ui_ctrl_t*), NULL, 0, NULL, NULL);
if (childrenlist == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
if (widget == GATE_UI_GTK_NO_WIDGET)
{
/* not a regular widget, so it has no children */
ret = GATE_RESULT_OK;
break;
}
if (GTK_IS_CONTAINER(widget))
{
cont = GTK_CONTAINER(widget);
list = gtk_container_get_children(cont);
for (entry = g_list_first(list); entry != NULL; entry = g_list_next(entry))
{
child_widget = (GtkWidget*)entry->data;
if (GTK_IS_WIDGET(child_widget))
{
child_ctrl = gate_ui_gtk_ctrl_resolve(child_widget);
if (child_ctrl)
{
gate_arraylist_add(childrenlist, &child_ctrl);
}
}
}
}
cont = GATE_UI_GTK_GET_CTRL_CONTAINER(ctrl);
if (cont)
{
list = gtk_container_get_children(cont);
for (entry = g_list_first(list); entry != NULL; entry = g_list_next(entry))
{
child_widget = (GtkWidget*)entry->data;
if (GTK_IS_WIDGET(child_widget))
{
child_ctrl = gate_ui_gtk_ctrl_resolve(child_widget);
if (child_ctrl)
{
gate_arraylist_add(childrenlist, &child_ctrl);
}
}
}
}
if (NULL == gate_array_create(children, childrenlist))
{
ret = GATE_RESULT_OUTOFMEMORY;
}
else
{
ret = GATE_RESULT_OK;
}
} while (0);
}
if (list != NULL)
{
g_list_free(list);
}
if (childrenlist != NULL)
{
gate_arraylist_release(childrenlist);
}
return ret;
}
gate_result_t gate_ui_gtk_ctrl_set_font(GtkWidget* widget, gate_ui_font_t const* font)
{
gate_result_t ret = GATE_RESULT_OK;
char font_family[128] = GATE_INIT_EMPTY;
PangoStyle font_style = font->italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL;
PangoVariant font_variant = PANGO_VARIANT_NORMAL;
PangoWeight font_weight = font->bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL;
PangoStretch font_stretch = PANGO_STRETCH_NORMAL;
gint font_size = font->size;
gate_int32_t orientation = font->orientation;
PangoGravity font_gravity = PANGO_GRAVITY_SOUTH;
PangoFontDescription* font_descr = pango_font_description_new();
while (orientation < 0)
{
orientation += 360;
}
while (orientation >= 360)
{
orientation -= 360;
}
if (font->orientation > 315)
{
font_gravity = PANGO_GRAVITY_SOUTH;
}
else if (font->orientation > 225)
{
font_gravity = PANGO_GRAVITY_WEST;
}
else if (font->orientation > 135)
{
font_gravity = PANGO_GRAVITY_NORTH;
}
else if (font->orientation > 45)
{
font_gravity = PANGO_GRAVITY_EAST;
}
else
{
font_gravity = PANGO_GRAVITY_SOUTH;
}
gate_str_print_text(font_family, sizeof(font_family), font->font_name, gate_str_length(font->font_name));
pango_font_description_set_family(font_descr, font_family);
pango_font_description_set_style(font_descr, font_style);
pango_font_description_set_weight(font_descr, font_weight);
pango_font_description_set_variant(font_descr, font_variant);
pango_font_description_set_stretch(font_descr, font_stretch);
pango_font_description_set_gravity(font_descr, font_gravity);
pango_font_description_set_size(font_descr, font->size * PANGO_SCALE);
gtk_widget_override_font(widget, font_descr);
return ret;
}
typedef struct gate_ui_gtk_key_mapping_class
{
gate_uint32_t gtk_key;
gate_input_keycode_t gate_key;
} gate_ui_gtk_key_mapping_t;
static gate_ui_gtk_key_mapping_t key_mapping[] = {
{ GDK_KEY_BackSpace, GATE_KBD_KEY_BACKSPACE },
{ GDK_KEY_Tab, GATE_KBD_KEY_TAB },
{ GDK_KEY_Clear, GATE_KBD_KEY_CLEAR },
{ GDK_KEY_Return, GATE_KBD_KEY_RETURN },
{ GDK_KEY_Pause, GATE_KBD_KEY_PAUSE },
{ GDK_KEY_Scroll_Lock, GATE_KBD_KEY_SCROLLLOCK },
{ GDK_KEY_Sys_Req, GATE_KBD_KEY_PRINTSCREEN },
{ GDK_KEY_Escape, GATE_KBD_KEY_ESCAPE },
{ GDK_KEY_Delete, GATE_KBD_KEY_DELETE },
{ GDK_KEY_Home, GATE_KBD_KEY_HOME },
{ GDK_KEY_Left, GATE_KBD_KEY_LEFT },
{ GDK_KEY_Up, GATE_KBD_KEY_UP },
{ GDK_KEY_Right, GATE_KBD_KEY_RIGHT },
{ GDK_KEY_Down, GATE_KBD_KEY_DOWN },
{ GDK_KEY_Page_Up, GATE_KBD_KEY_PGUP },
{ GDK_KEY_Page_Down, GATE_KBD_KEY_PGDOWN},
{ GDK_KEY_End, GATE_KBD_KEY_END},
{ GDK_KEY_Select, GATE_KBD_KEY_SELECT },
{ GDK_KEY_Print, GATE_KBD_KEY_PRINT },
{ GDK_KEY_Execute, GATE_KBD_KEY_EXECUTE },
{ GDK_KEY_Insert, GATE_KBD_KEY_INSERT },
{ GDK_KEY_Menu, GATE_KBD_KEY_MENU },
/*{ GDK_KEY_Cancel, GATE_KBD_KEY_CANCEL },*/
{ GDK_KEY_Help, GATE_KBD_KEY_HELP },
{ GDK_KEY_Num_Lock, GATE_KBD_KEY_NUMLOCK },
{ GDK_KEY_KP_Space, GATE_KBD_KEY_SPACEBAR },
{ GDK_KEY_KP_Tab, GATE_KBD_KEY_TAB},
{ GDK_KEY_KP_Enter, GATE_KBD_KEY_RETURN },
{ GDK_KEY_KP_Home, GATE_KBD_KEY_HOME },
{ GDK_KEY_KP_Left, GATE_KBD_KEY_LEFT },
{ GDK_KEY_KP_Up, GATE_KBD_KEY_UP },
{ GDK_KEY_KP_Right, GATE_KBD_KEY_RIGHT },
{ GDK_KEY_KP_Down, GATE_KBD_KEY_DOWN },
{ GDK_KEY_KP_Page_Up, GATE_KBD_KEY_PGUP },
{ GDK_KEY_KP_Page_Down, GATE_KBD_KEY_PGDOWN },
{ GDK_KEY_KP_End, GATE_KBD_KEY_END },
{ GDK_KEY_KP_Insert, GATE_KBD_KEY_INSERT },
{ GDK_KEY_KP_Delete, GATE_KBD_KEY_DELETE },
{ GDK_KEY_KP_Equal, GATE_KBD_KEY_EQUAL },
{ GDK_KEY_KP_Multiply, GATE_KBD_KEY_MULTIPLY },
{ GDK_KEY_KP_Add, GATE_KBD_KEY_ADD },
{ GDK_KEY_KP_Separator, GATE_KBD_KEY_SEPARATOR },
{ GDK_KEY_KP_Subtract, GATE_KBD_KEY_SUBTRACT },
{ GDK_KEY_KP_Decimal, GATE_KBD_KEY_DECIMAL },
{ GDK_KEY_KP_Divide, GATE_KBD_KEY_DEVIDE },
{ GDK_KEY_KP_0, GATE_KBD_KEY_NUM0 },
{ GDK_KEY_KP_1, GATE_KBD_KEY_NUM1 },
{ GDK_KEY_KP_2, GATE_KBD_KEY_NUM2 },
{ GDK_KEY_KP_3, GATE_KBD_KEY_NUM3 },
{ GDK_KEY_KP_4, GATE_KBD_KEY_NUM4 },
{ GDK_KEY_KP_5, GATE_KBD_KEY_NUM5 },
{ GDK_KEY_KP_6, GATE_KBD_KEY_NUM6 },
{ GDK_KEY_KP_7, GATE_KBD_KEY_NUM7 },
{ GDK_KEY_KP_8, GATE_KBD_KEY_NUM8 },
{ GDK_KEY_KP_9, GATE_KBD_KEY_NUM9 },
{ GDK_KEY_F1, GATE_KBD_KEY_F1},
{ GDK_KEY_F2, GATE_KBD_KEY_F2},
{ GDK_KEY_F3, GATE_KBD_KEY_F3 },
{ GDK_KEY_F4, GATE_KBD_KEY_F4 },
{ GDK_KEY_F5, GATE_KBD_KEY_F5 },
{ GDK_KEY_F6, GATE_KBD_KEY_F6 },
{ GDK_KEY_F7, GATE_KBD_KEY_F7 },
{ GDK_KEY_F8, GATE_KBD_KEY_F8 },
{ GDK_KEY_F9, GATE_KBD_KEY_F9 },
{ GDK_KEY_F10, GATE_KBD_KEY_F10 },
{ GDK_KEY_F11, GATE_KBD_KEY_F11 },
{ GDK_KEY_F12, GATE_KBD_KEY_F12 },
{ GDK_KEY_F13, GATE_KBD_KEY_F13 },
{ GDK_KEY_F14, GATE_KBD_KEY_F14 },
{ GDK_KEY_F15, GATE_KBD_KEY_F15 },
{ GDK_KEY_F16, GATE_KBD_KEY_F16 },
{ GDK_KEY_F17, GATE_KBD_KEY_F17 },
{ GDK_KEY_F18, GATE_KBD_KEY_F18 },
{ GDK_KEY_F19, GATE_KBD_KEY_F19 },
{ GDK_KEY_F20, GATE_KBD_KEY_F20 },
{ GDK_KEY_F21, GATE_KBD_KEY_F21 },
{ GDK_KEY_F22, GATE_KBD_KEY_F22 },
{ GDK_KEY_F23, GATE_KBD_KEY_F23 },
{ GDK_KEY_F24, GATE_KBD_KEY_F24 },
{ GDK_KEY_Shift_L, GATE_KBD_KEY_SHIFT_LEFT },
{ GDK_KEY_Shift_R, GATE_KBD_KEY_SHIFT_RIGHT },
{ GDK_KEY_Control_L, GATE_KBD_KEY_CTRL_LEFT },
{ GDK_KEY_Control_R, GATE_KBD_KEY_CTRL_RIGHT },
{ GDK_KEY_Caps_Lock, GATE_KBD_KEY_CAPITAL },
{ GDK_KEY_Meta_L, GATE_KBD_KEY_MENU_LEFT },
{ GDK_KEY_Meta_R, GATE_KBD_KEY_MENU_RIGHT },
{ GDK_KEY_Alt_L, GATE_KBD_KEY_MENU_LEFT },
{ GDK_KEY_Alt_R, GATE_KBD_KEY_MENU_RIGHT },
{ GDK_KEY_space, GATE_KBD_KEY_SPACEBAR },
{ GDK_KEY_plus, GATE_KBD_KEY_ADD },
{ GDK_KEY_comma, GATE_KBD_KEY_SEPARATOR },
{ GDK_KEY_minus, GATE_KBD_KEY_SUBTRACT },
{ GDK_KEY_period, GATE_KBD_KEY_DECIMAL },
{ GDK_KEY_0, GATE_KBD_KEY_0 },
{ GDK_KEY_1, GATE_KBD_KEY_1 },
{ GDK_KEY_2, GATE_KBD_KEY_2 },
{ GDK_KEY_3, GATE_KBD_KEY_3 },
{ GDK_KEY_4, GATE_KBD_KEY_4 },
{ GDK_KEY_5, GATE_KBD_KEY_5 },
{ GDK_KEY_6, GATE_KBD_KEY_6 },
{ GDK_KEY_7, GATE_KBD_KEY_7 },
{ GDK_KEY_8, GATE_KBD_KEY_8 },
{ GDK_KEY_9, GATE_KBD_KEY_9 },
/*
{ GDK_KEY_colon, GATE_KBD_KEY_ },
{ GDK_KEY_semicolon, GATE_KBD_KEY_ },
{ GDK_KEY_less, GATE_KBD_KEY_ },
*/
{ GDK_KEY_equal, GATE_KBD_KEY_EQUAL },
/*
{ GDK_KEY_greater, GATE_KBD_KEY_ },
{ GDK_KEY_question, GATE_KBD_KEY_ },
*/
{ GDK_KEY_A, GATE_KBD_KEY_A },
{ GDK_KEY_B, GATE_KBD_KEY_B },
{ GDK_KEY_C, GATE_KBD_KEY_C },
{ GDK_KEY_D, GATE_KBD_KEY_D },
{ GDK_KEY_E, GATE_KBD_KEY_E },
{ GDK_KEY_F, GATE_KBD_KEY_F },
{ GDK_KEY_G, GATE_KBD_KEY_G },
{ GDK_KEY_H, GATE_KBD_KEY_H },
{ GDK_KEY_I, GATE_KBD_KEY_I },
{ GDK_KEY_J, GATE_KBD_KEY_J },
{ GDK_KEY_K, GATE_KBD_KEY_K },
{ GDK_KEY_L, GATE_KBD_KEY_L },
{ GDK_KEY_M, GATE_KBD_KEY_M },
{ GDK_KEY_N, GATE_KBD_KEY_N },
{ GDK_KEY_O, GATE_KBD_KEY_O },
{ GDK_KEY_P, GATE_KBD_KEY_P },
{ GDK_KEY_Q, GATE_KBD_KEY_Q },
{ GDK_KEY_R, GATE_KBD_KEY_R },
{ GDK_KEY_S, GATE_KBD_KEY_S },
{ GDK_KEY_T, GATE_KBD_KEY_T },
{ GDK_KEY_U, GATE_KBD_KEY_U },
{ GDK_KEY_V, GATE_KBD_KEY_V },
{ GDK_KEY_W, GATE_KBD_KEY_W },
{ GDK_KEY_X, GATE_KBD_KEY_X },
{ GDK_KEY_Y, GATE_KBD_KEY_Y },
{ GDK_KEY_Z, GATE_KBD_KEY_Z },
{ GDK_KEY_bracketleft, GATE_KBD_KEY_BRACE_LEFT },
{ GDK_KEY_backslash, GATE_KBD_KEY_BACKSLASH },
{ GDK_KEY_bracketright, GATE_KBD_KEY_BRACE_RIGHT },
/*
{ GDK_KEY_asciicircum, GATE_KBD_KEY_ },
{ GDK_KEY_underscore, GATE_KBD_KEY_ },
{ GDK_KEY_grave, GATE_KBD_KEY_ },
{ GDK_KEY_quoteleft, GATE_KBD_KEY_ },
*/
{ GDK_KEY_a, GATE_KBD_KEY_A },
{ GDK_KEY_b, GATE_KBD_KEY_B },
{ GDK_KEY_c, GATE_KBD_KEY_C },
{ GDK_KEY_d, GATE_KBD_KEY_D },
{ GDK_KEY_e, GATE_KBD_KEY_E },
{ GDK_KEY_f, GATE_KBD_KEY_F },
{ GDK_KEY_g, GATE_KBD_KEY_G },
{ GDK_KEY_h, GATE_KBD_KEY_H },
{ GDK_KEY_i, GATE_KBD_KEY_I },
{ GDK_KEY_j, GATE_KBD_KEY_J },
{ GDK_KEY_k, GATE_KBD_KEY_K },
{ GDK_KEY_l, GATE_KBD_KEY_L },
{ GDK_KEY_m, GATE_KBD_KEY_M },
{ GDK_KEY_n, GATE_KBD_KEY_N },
{ GDK_KEY_o, GATE_KBD_KEY_O },
{ GDK_KEY_p, GATE_KBD_KEY_P },
{ GDK_KEY_q, GATE_KBD_KEY_Q },
{ GDK_KEY_r, GATE_KBD_KEY_R },
{ GDK_KEY_s, GATE_KBD_KEY_S },
{ GDK_KEY_t, GATE_KBD_KEY_T },
{ GDK_KEY_u, GATE_KBD_KEY_U },
{ GDK_KEY_v, GATE_KBD_KEY_V },
{ GDK_KEY_w, GATE_KBD_KEY_W },
{ GDK_KEY_x, GATE_KBD_KEY_X },
{ GDK_KEY_y, GATE_KBD_KEY_Y },
{ GDK_KEY_z, GATE_KBD_KEY_Z },
{ GDK_KEY_braceleft, GATE_KBD_KEY_BRACE_LEFT },
{ GDK_KEY_bar, GATE_KBD_KEY_SUBTRACT },
{ GDK_KEY_braceright, GATE_KBD_KEY_BRACE_RIGHT },
{ GDK_KEY_asciitilde, GATE_KBD_KEY_TILDE },
/*
{ GDK_KEY_AudioLowerVolume 0x1008ff11
{ GDK_KEY_AudioMute 0x1008ff12
{ GDK_KEY_AudioRaiseVolume 0x1008ff13
{ GDK_KEY_AudioPlay 0x1008ff14
{ GDK_KEY_AudioStop 0x1008ff15
{ GDK_KEY_AudioPrev 0x1008ff16
{ GDK_KEY_AudioNext 0x1008ff17
{ GDK_KEY_HomePage 0x1008ff18
{ GDK_KEY_Mail 0x1008ff19
{ GDK_KEY_Start 0x1008ff1a
{ GDK_KEY_Search 0x1008ff1b
{ GDK_KEY_AudioRecord 0x1008ff1c
{ GDK_KEY_Calculator 0x1008ff1d
{ GDK_KEY_Memo 0x1008ff1e
{ GDK_KEY_ToDoList 0x1008ff1f
{ GDK_KEY_Calendar 0x1008ff20
{ GDK_KEY_PowerDown 0x1008ff21
{ GDK_KEY_Back 0x1008ff26
{ GDK_KEY_Forward 0x1008ff27
{ GDK_KEY_Stop 0x1008ff28
{ GDK_KEY_Refresh 0x1008ff29
{ GDK_KEY_PowerOff 0x1008ff2a
{ GDK_KEY_WakeUp 0x1008ff2b
{ GDK_KEY_Eject 0x1008ff2c
{ GDK_KEY_WWW 0x1008ff2e
{ GDK_KEY_Sleep 0x1008ff2f
{ GDK_KEY_Favorites 0x1008ff30
{ GDK_KEY_AudioPause 0x1008ff31
{ GDK_KEY_AudioMedia 0x1008ff32
{ GDK_KEY_AudioRewind 0x1008ff3e
{ GDK_KEY_BackForward 0x1008ff3f
{ GDK_KEY_ApplicationLeft 0x1008ff50
{ GDK_KEY_ApplicationRight 0x1008ff51
{ GDK_KEY_Book 0x1008ff52
{ GDK_KEY_CD 0x1008ff53
{ GDK_KEY_WindowClear 0x1008ff55
{ GDK_KEY_Close 0x1008ff56
{ GDK_KEY_Copy 0x1008ff57
{ GDK_KEY_Cut 0x1008ff58
{ GDK_KEY_Suspend 0x1008ffa7
{ GDK_KEY_Hibernate 0x1008ffa8
*/
{ 0, GATE_KBD_KEY_UNKNOWN }
};
gate_bool_t gate_ui_gtk_convert_keyval(gate_uint32_t gtk_key, gate_input_keycode_t* gate_key)
{
gate_size_t len = sizeof(key_mapping) / sizeof(key_mapping[0]);
gate_size_t ndx;
for (ndx = 0; ndx != len; ++ndx)
{
if (gtk_key == key_mapping[ndx].gtk_key)
{
*gate_key = key_mapping[ndx].gate_key;
return true;
}
}
return false;
}
gate_bool_t gate_ui_gtk_convert_gate_key(gate_input_keycode_t gate_key, gate_uint32_t* gtk_key)
{
gate_size_t len = sizeof(key_mapping) / sizeof(key_mapping[0]);
gate_size_t ndx;
for (ndx = 0; ndx != len; ++ndx)
{
if (gate_key == key_mapping[ndx].gate_key)
{
*gtk_key = key_mapping[ndx].gtk_key;
return true;
}
}
return false;
}
#endif /* GATE_UI_GTK */
|