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 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/ui/toolbars.h"
#include "gate/results.h"
#if defined(GATE_UI_WINAPI)
#ifndef _WIN32_IE
# if defined(GATE_COMPILER_TCC)
# define _WIN32_IE 0x0501
# else
# define _WIN32_IE 0x0400
# endif
#endif
#include "gate/ui/gateui_winapi.h"
#include "gate/ui/graphics.h"
#include "gate/platforms.h"
#if defined(GATE_SYS_WIN16)
typedef struct tagNMHDR
{
HWND hwndFrom;
UINT_PTR idFrom;
UINT code;
} NMHDR;
typedef struct tagNMMOUSE
{
NMHDR hdr;
DWORD dwItemSpec;
DWORD dwItemData;
POINT pt;
LPARAM dwHitInfo;
} NMMOUSE, * LPNMMOUSE;
typedef struct _TBBUTTON
{
int iBitmap;
int idCommand;
BYTE fsState;
BYTE fsStyle;
DWORD dwData;
INT_PTR iString;
} TBBUTTON, * LPTBBUTTON;
typedef struct
{
UINT cbSize;
DWORD dwMask;
int idCommand;
int iImage;<--- struct member 'TBBUTTONINFO::iImage' is never used.
BYTE fsState;
BYTE fsStyle;
WORD cx;
DWORD lParam;
LPSTR pszText;
int cchText;<--- struct member 'TBBUTTONINFO::cchText' is never used.
} TBBUTTONINFO, * LPTBBUTTONINFO;
#define NM_FIRST (0U- 0U)
#define NM_LAST (0U- 99U)
#define TBN_FIRST (0U-700U)
#define TBN_LAST (0U-720U)
#define WM_NOTIFY 0x004E
#define NM_CLICK (NM_FIRST-2)
#define NM_DBLCLK (NM_FIRST-3)
#define TB_GETBUTTONINFO (WM_USER + 65)
#define TB_SETBUTTONINFO (WM_USER + 66)
#define TB_AUTOSIZE (WM_USER + 33)
#define TBIF_IMAGE 0x00000001
#define TBIF_TEXT 0x00000002
#define TBIF_STATE 0x00000004
#define TBIF_STYLE 0x00000008
#define TBIF_LPARAM 0x00000010
#define TBIF_COMMAND 0x00000020
#define TBIF_SIZE 0x00000040
#define TBIF_BYINDEX 0x80000000
#define TBSTYLE_BUTTON 0x0000
#define TBSTYLE_SEP 0x0001
#define TBSTYLE_CHECK 0x0002
#define TBSTYLE_GROUP 0x0004
#define TBSTYLE_CHECKGROUP (TBSTYLE_GROUP | TBSTYLE_CHECK)
#define TBSTYLE_DROPDOWN 0x0008
#define TBSTYLE_AUTOSIZE 0x0010
#define TBSTYLE_NOPREFIX 0x0020
#define TBSTYLE_TOOLTIPS 0x0100
#define TBSTYLE_WRAPABLE 0x0200
#define TBSTYLE_ALTDRAG 0x0400
#define TBSTYLE_FLAT 0x0800
#define TBSTYLE_LIST 0x1000
#define TBSTYLE_CUSTOMERASE 0x2000
#define TBSTYLE_REGISTERDROP 0x4000
#define TBSTYLE_TRANSPARENT 0x8000
#define CCS_NORESIZE 0x00000004L
#define CCS_NOPARENTALIGN 0x00000008L
#define TBSTATE_CHECKED 0x01
#define TBSTATE_PRESSED 0x02
#define TBSTATE_ENABLED 0x04
#define TBSTATE_HIDDEN 0x08
#define TBSTATE_INDETERMINATE 0x10
#define TBSTATE_WRAP 0x20
#define TBSTATE_ELLIPSES 0x40
#define TBSTATE_MARKED 0x80
#define TB_ADDBUTTONS (WM_USER + 20)
#define TB_INSERTBUTTON (WM_USER + 21)
#define TB_DELETEBUTTON (WM_USER + 22)
#define TB_GETBUTTON (WM_USER + 23)
#define TB_BUTTONCOUNT (WM_USER + 24)
#define TB_COMMANDTOINDEX (WM_USER + 25)
#define TOOLBARCLASSNAME "ToolbarWindow"
#else
# include <commctrl.h>
#endif
#ifndef TBCDRF_USECDCOLORS
#define TBCDRF_USECDCOLORS 0x00800000
#endif
static gate_bool_t gate_ui_toolbar_events(void* hwnd, gate_ui_ctrl_t* ctrl, gate_uint32_t msg, gate_uintptr_t wParam, gate_intptr_t lParam, gate_intptr_t* lresult)
{
HWND hwndCtrl;
gate_ui_toolbar_t* toolbar;
gate_ui_host_t* host = NULL;
NMHDR* nmhdr;<--- The scope of the variable 'nmhdr' can be reduced. [+]The scope of the variable 'nmhdr' 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.
LPARAM lparam_button;
TBBUTTON tb_button;
int buttonindex;<--- The scope of the variable 'buttonindex' can be reduced. [+]The scope of the variable 'buttonindex' 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.
#if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WIN16)
HIMAGELIST himagelist;
#endif
#if defined(GATE_UI_WINAPI_DARKMODE_SUPPORT)
gate_ui_color_t color;
HBRUSH hbgbrush;
NMTBCUSTOMDRAW* tb_draw;
#endif
if ((ctrl != NULL) && (hwnd != NULL))
{
hwndCtrl = GATE_UI_WINAPI_GET_HWND(ctrl);
if (NULL != hwndCtrl)
{
host = GATE_UI_WINAPI_GET_HOST(ctrl);
toolbar = (gate_ui_toolbar_t*)ctrl;
switch (msg)
{
case WM_COMMAND:
{
if ((HWND)lParam == hwndCtrl)
{
/* toolbar button was clicked */
if (toolbar->on_click != NULL)
{
lparam_button = 0;
buttonindex = (int)SendMessage(hwndCtrl, TB_BUTTONCOUNT, 0, 0);
while (buttonindex > 0)
{
--buttonindex;
gate_mem_clear(&tb_button, sizeof(tb_button));
if (SendMessage(hwndCtrl, TB_GETBUTTON, (WPARAM)buttonindex, (LPARAM)&tb_button))
{
if (tb_button.idCommand == (WPARAM)LOWORD(wParam))
{
/* we found our clicked button */
toolbar->on_click(&toolbar->ctrl, (gate_ui_toolbar_button_t)(LOWORD(wParam)), (void*)tb_button.dwData);
break;
}
}
}
}
*lresult = 0;
return true;
}
break;
}
case WM_NOTIFY:
{
nmhdr = (NMHDR*)(lParam);
if (nmhdr->hwndFrom == hwndCtrl)
{
switch (nmhdr->code)
{
case NM_DBLCLK:
{
break;
}
#if defined(GATE_UI_WINAPI_DARKMODE_SUPPORT)
case NM_CUSTOMDRAW:
{
if (!gate_ui_winapi_darkmode_enabled())
{
break;
}
tb_draw = (NMTBCUSTOMDRAW*)lParam;
switch (tb_draw->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
{
gate_ui_host_default_color(host, GATE_UI_COLOR_MENUBACKGROUND, &color);
hbgbrush = (HBRUSH)gate_ui_winapi_host_get_brush(host, &color);
FillRect(tb_draw->nmcd.hdc, &tb_draw->nmcd.rc, hbgbrush);
*lresult = CDRF_NOTIFYITEMDRAW;
return true;
}
case CDDS_ITEMPREPAINT:
{
//gate_ui_host_default_color(host, GATE_UI_COLOR_MENUTEXT, &color);
//tb_draw->clrText = RGB(color.r, color.g, color.b);
//gate_ui_host_default_color(host, GATE_UI_COLOR_SELECTEDMENUTEXT, &color);
// tb_draw->clrTextHighlight = RGB(color.r, color.g, color.b);
if (tb_draw->nmcd.uItemState & CDIS_SELECTED)
{
gate_ui_host_default_color(host, GATE_UI_COLOR_SELECTEDMENUBACKGROUND, &color);
hbgbrush = (HBRUSH)gate_ui_winapi_host_get_brush(host, &color);
}
else if (tb_draw->nmcd.uItemState & CDIS_HOT)
{
//gate_ui_host_default_color(host, GATE_UI_COLOR_HOVERMENUBACKGROUND, &color);
//hbgbrush = (HBRUSH)gate_ui_winapi_host_get_brush(host, &color);
hbgbrush = NULL;
}
else
{
gate_ui_host_default_color(host, GATE_UI_COLOR_MENUBACKGROUND, &color);
hbgbrush = (HBRUSH)gate_ui_winapi_host_get_brush(host, &color);
}
if (NULL != hbgbrush)
{
FillRect(tb_draw->nmcd.hdc, &tb_draw->nmcd.rc, hbgbrush);
}
*lresult = CDRF_NEWFONT;
return true;
}
case (CDDS_SUBITEM | CDDS_ITEMPREPAINT):
{
//SelectObject(tb_draw->nmcd.hdc, CreateSolidBrush(RGB(0, 0, 0)));
//FillRect(tb_draw->nmcd.hdc, &tb_draw->nmcd.rc, tb_draw->clrBtnFace);
*lresult = CDRF_NEWFONT;
return true;
}
default:
{
*lresult = CDRF_DODEFAULT;
return true;
}
}
break;
}
#endif /* GATE_UI_WINAPI_DARKMODE_SUPPORT */
}
}
break;
}
case WM_DESTROY:
{
#if defined(GATE_SYS_WINCE) || defined(GATE_SYS_WIN16)
#else
himagelist = (HIMAGELIST)SendMessage(hwnd, TB_GETIMAGELIST, 0, 0);
if (himagelist)
{
SendMessage(hwnd, TB_SETIMAGELIST, 0, 0);
gate_ui_winapi_imagelist_destroy(himagelist);
}
#endif
gate_ui_winapi_unregister_window(host, hwnd);
break;
}
default:
{
break;
}
}
}
}
return false;
}
#if !defined(ILC_COLOR16) && defined(ILC_COLOR)
# define ILC_COLOR16 ILC_COLOR
#endif
gate_result_t gate_ui_toolbar_create(gate_ui_toolbar_t* toolbar, gate_ui_ctrl_t* parent,
gate_ui_position_t const* position, gate_uint32_t flags, void* userparam)
{
gate_result_t ret;
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.
HWND hwndParent;
HWND hwnd;
gate_uint32_t styles;
gate_uint32_t exstyles = 0;
#if defined(GATE_SYS_WINCE) || defined(GATE_SYS_WIN16)
HINSTANCE hinst;
#else
HIMAGELIST himagelist;
#endif
do
{
host = gate_ui_ctrl_get_host(parent);
if (host == NULL)
{
ret = GATE_RESULT_INVALIDSTATE;
break;
}
hwndParent = GATE_UI_WINAPI_GET_HWND(parent);
if (hwndParent == NULL)
{
ret = GATE_RESULT_INVALIDSTATE;
break;
}
gate_mem_clear(toolbar, sizeof(gate_ui_toolbar_t));
#if defined(GATE_SYS_WINCE)
hinst = (HINSTANCE)GATE_UI_WINAPI_GET_HOST_APPHANDLE(host);
styles = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_TABSTOP;
hwnd = CreateToolbarEx(hwndParent, styles,
0, 0, hinst, 0, NULL, 0, 0, 0, 0, 0, sizeof(TBBUTTON));
ret = gate_ui_winapi_setup_control(&toolbar->ctrl, host, hwnd, hwndParent, userparam);
if (GATE_FAILED(ret))
{
DestroyWindow(hwnd);
break;
}
#else /* GATE_SYS_WINCE */
styles = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_TABSTOP | TBSTYLE_TOOLTIPS | CCS_NOPARENTALIGN | CCS_NORESIZE;
if (!GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_ENABLED)) styles |= WS_DISABLED;
if (GATE_FLAG_ENABLED(flags, GATE_UI_FLAG_VISIBLE)) styles |= WS_VISIBLE;
ret = gate_ui_winapi_create(&toolbar->ctrl, host, hwndParent, TOOLBARCLASSNAME, position, styles, exstyles, NULL, userparam, true);
if (GATE_FAILED(ret))
{
break;
}
#if !defined(GATE_SYS_WIN16)
hwnd = GATE_UI_WINAPI_GET_HWND(&toolbar->ctrl);
SendMessage(hwnd, TB_SETBITMAPSIZE, 0, MAKELONG(16, 16));
SendMessage(hwnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);
himagelist = (HIMAGELIST)gate_ui_winapi_imagelist_create(16, 16, ILC_COLOR16 | ILC_MASK, 0, 0);<--- Skipping configuration 'GATE_UI_WINAPI;ILC_COLOR;ILC_COLOR16' since the value of 'ILC_COLOR16' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
if (!himagelist)
{
gate_ui_winapi_destroy(&toolbar->ctrl);
ret = GATE_RESULT_OUTOFRESOURCES;
break;
}
SendMessage(hwnd, TB_SETIMAGELIST, 0, (LPARAM)himagelist);
#endif
#endif /* GATE_SYS_WINCE */
gate_ui_winapi_register_event(host, hwndParent, WM_NOTIFY, &gate_ui_toolbar_events, &toolbar->ctrl);
gate_ui_winapi_register_event(host, hwndParent, WM_COMMAND, &gate_ui_toolbar_events, &toolbar->ctrl);
gate_ui_winapi_register_event(host, hwnd, WM_DESTROY, &gate_ui_toolbar_events, &toolbar->ctrl);
/*
gate_ui_winapi_register_event(host, hwndParent, WM_DRAWITEM, &gate_ui_label_events, &label->ctrl);
*/
} while (0);
return ret;
}
static int volatile global_gate_toolbar_id = 0;
#if !defined(I_IMAGENONE)
# define I_IMAGENONE (-2)
#endif
static gate_result_t gate_ui_toolbar_add_bitmap_id(gate_ui_toolbar_t* toolbar, int bitmap_id, void* button_param,
gate_ui_toolbar_button_t* button_id)
{
gate_result_t ret = GATE_RESULT_FAILED;
HWND hwnd = GATE_UI_WINAPI_GET_HWND(&toolbar->ctrl);
TBBUTTON button;
TBBUTTONINFO buttoninfo;
do
{
button.iBitmap = bitmap_id;
button.idCommand = ++global_gate_toolbar_id;
button.fsState = TBSTATE_ENABLED;
button.fsStyle = TBSTYLE_BUTTON;
button.dwData = (DWORD_PTR)button_param;
button.iString = 0;
if (FALSE == SendMessage(hwnd, TB_ADDBUTTONS, 1, (LPARAM)&button))
{
ret = GATE_RESULT_FAILED;
}
else
{
gate_mem_clear(&buttoninfo, sizeof(buttoninfo));
buttoninfo.cbSize = sizeof(buttoninfo);
buttoninfo.dwMask = TBIF_COMMAND | TBIF_LPARAM;
buttoninfo.idCommand = button.idCommand;
buttoninfo.lParam = (LPARAM)button_param;
SendMessage(hwnd, TB_SETBUTTONINFO, (WPARAM)button.idCommand, (LPARAM)&buttoninfo);
SendMessage(hwnd, TB_AUTOSIZE, 0, 0);
*button_id = button.idCommand;
ret = GATE_RESULT_OK;
}
} while (0);
return ret;
}
static gate_result_t gate_ui_toolbar_add_HICON(gate_ui_toolbar_t* toolbar, HICON hicon,
void* button_param, gate_ui_toolbar_button_t* button_id)
{
gate_result_t ret = GATE_RESULT_FAILED;
#if !defined(GATE_SYS_WIN16)
HWND hwnd = GATE_UI_WINAPI_GET_HWND(&toolbar->ctrl);
int bitmap_id;
HIMAGELIST himagelist = (HIMAGELIST)SendMessage(hwnd, TB_GETIMAGELIST, 0, 0);
#if !defined(GATE_SYS_WINCE)
if (himagelist == NULL)
{
/* NT3 or Win9x without IE does not support GETIMAGELIST */
ICONINFO iconinfo;
TBADDBITMAP bminfo;
GetIconInfo(hicon, &iconinfo);
bminfo.hInst = NULL;
bminfo.nID = (UINT_PTR)iconinfo.hbmColor;
bitmap_id = (int)SendMessage(hwnd, TB_ADDBITMAP, 1, (LRESULT)&bminfo);
}
else
#endif
{
bitmap_id = gate_ui_winapi_imagelist_replace_icon(himagelist, -1, hicon);
if (bitmap_id == -1)
{
#if defined(GATE_SYS_WINCE)
hicon = (HICON)LoadCursor(NULL, IDC_ICON);
#else
hicon = LoadIcon(NULL, IDI_WARNING);
#endif
bitmap_id = gate_ui_winapi_imagelist_replace_icon(himagelist, -1, hicon);
}
}
if (bitmap_id == -1)
{
/* Failed to add ICON bitmap to toolbar's image list */
ret = GATE_RESULT_FAILED;
}
else
{
ret = gate_ui_toolbar_add_bitmap_id(toolbar, bitmap_id, button_param, button_id);
}
#endif
return ret;
}
gate_result_t gate_ui_toolbar_add_icon_button(gate_ui_toolbar_t* toolbar, gate_ui_icon_t const* icon,
void* button_param, gate_ui_toolbar_button_t* button_id)
{
gate_result_t ret = GATE_RESULT_FAILED;
HICON hicon = (HICON)icon->resources[0];
gate_uint32_t stock_id;
int bitmap_id = 0xff;<--- The scope of the variable 'bitmap_id' can be reduced. [+]The scope of the variable 'bitmap_id' 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:<--- Variable 'bitmap_id' is assigned a value that is never used.
void f(int x)<--- Variable 'bitmap_id' is assigned a value that is never used.
{<--- Variable 'bitmap_id' is assigned a value that is never used.
int i = 0;<--- Variable 'bitmap_id' is assigned a value that is never used.
if (x) {<--- Variable 'bitmap_id' is assigned a value that is never used.
// it's safe to move 'int i = 0;' here<--- Variable 'bitmap_id' is assigned a value that is never used.
for (int n = 0; n < 10; ++n) {<--- Variable 'bitmap_id' is assigned a value that is never used.
// it is possible but not safe to move 'int i = 0;' here<--- Variable 'bitmap_id' is assigned a value that is never used.
do_something(&i);<--- Variable 'bitmap_id' is assigned a value that is never used.
}<--- Variable 'bitmap_id' is assigned a value that is never used.
}<--- Variable 'bitmap_id' is assigned a value that is never used.
}<--- Variable 'bitmap_id' is assigned a value that is never used.
When you see this message it is always safe to reduce the variable scope 1 level. <--- Variable 'bitmap_id' is assigned a value that is never used.
#if !defined(GATE_SYS_WIN16)
if (GATE_FLAG_ENABLED(icon->flags, GATE_UI_ICON_FLAG_STOCK))
{
stock_id = (gate_uint32_t)(gate_uintptr_t)icon->resources[1];
switch (stock_id)
{
case GATE_UI_ICON_STOCK_APP:
case GATE_UI_ICON_STOCK_DOCUMENT:
case GATE_UI_ICON_STOCK_FOLDER:
case GATE_UI_ICON_STOCK_FOLDEROPEN:
case GATE_UI_ICON_STOCK_NEWFILE: bitmap_id = STD_FILENEW; break;
case GATE_UI_ICON_STOCK_OPENFILE: bitmap_id = STD_FILEOPEN; break;
case GATE_UI_ICON_STOCK_SAVEFILE: bitmap_id = STD_FILESAVE; break;
case GATE_UI_ICON_STOCK_PRINTER: bitmap_id = STD_PRINT; break;
case GATE_UI_ICON_STOCK_CUT: bitmap_id = STD_CUT; break;
case GATE_UI_ICON_STOCK_COPY: bitmap_id = STD_COPY; break;
case GATE_UI_ICON_STOCK_PASTE: bitmap_id = STD_PASTE; break;
case GATE_UI_ICON_STOCK_DELETE: bitmap_id = STD_DELETE; break;
case GATE_UI_ICON_STOCK_FIND: bitmap_id = STD_FIND; break;
case GATE_UI_ICON_STOCK_HELP: bitmap_id = STD_HELP; break;
case GATE_UI_ICON_STOCK_STORAGE:
case GATE_UI_ICON_STOCK_COMPUTER:
case GATE_UI_ICON_STOCK_NETWORK:
case GATE_UI_ICON_STOCK_GLOBE:
case GATE_UI_ICON_STOCK_HOME:
case GATE_UI_ICON_STOCK_IMAGE:
case GATE_UI_ICON_STOCK_AUDIO:
case GATE_UI_ICON_STOCK_VIDEO:
case GATE_UI_ICON_STOCK_MAIL:
case GATE_UI_ICON_STOCK_SETTINGS: bitmap_id = STD_PROPERTIES; break;
default:
break;
}
ret = gate_ui_toolbar_add_bitmap_id(toolbar, bitmap_id, button_param, button_id);
}
else
{
ret = gate_ui_toolbar_add_HICON(toolbar, hicon, button_param, button_id);
}
#endif /* GATE_SYS_WIN16 */
return ret;
}
gate_result_t gate_ui_toolbar_add_image_button(gate_ui_toolbar_t* toolbar, gate_rasterimage_t const* raster,
void* button_param, gate_ui_toolbar_button_t* button_id)
{
gate_ui_host_t* host = GATE_UI_WINAPI_GET_HOST(&toolbar->ctrl);
gate_ui_icon_t icon;
gate_result_t ret;
HICON hicon;
ret = gate_ui_icon_create_image(&icon, host, raster);
hicon = (HICON)icon.resources[0];
if (GATE_SUCCEEDED(ret))
{
ret = gate_ui_toolbar_add_HICON(toolbar, hicon, button_param, button_id);
gate_ui_icon_destroy(&icon);
}
return ret;
}
gate_result_t gate_ui_toolbar_add_separator(gate_ui_toolbar_t* toolbar)
{
gate_result_t ret = GATE_RESULT_OK;
HWND hwnd = GATE_UI_WINAPI_GET_HWND(&toolbar->ctrl);
TBBUTTON button;
button.iBitmap = 0;
button.idCommand = 0;
button.fsState = TBSTATE_ENABLED;
button.fsStyle = TBSTYLE_SEP;
button.dwData = 0;
button.iString = 0;
if (FALSE == SendMessage(hwnd, TB_ADDBUTTONS, 1, (LPARAM)&button))
{
//ret = GATE_RESULT_FAILED;
}
else
{
SendMessage(hwnd, TB_AUTOSIZE, 0, 0);
}
return ret;
}
#endif /* GATE_UI_WINAPI */
#if defined(GATE_UI_GTK)
#include "gate/ui/gateui_gtk.h"
gate_result_t gate_ui_toolbar_create(gate_ui_toolbar_t* toolbar, gate_ui_ctrl_t* parent,
gate_ui_position_t const* position, gate_uint32_t flags, void* userparam)
{
gate_result_t ret;
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.
gate_ui_host_t* host = GATE_UI_GTK_GET_CTRL_HOST(parent);
GtkContainer* parentContainer = GATE_UI_GTK_GET_CTRL_CONTAINER(parent);
do
{
gate_mem_clear(toolbar, sizeof(gate_ui_toolbar_t));
widget = gtk_toolbar_new();
if (widget == NULL)
{
ret = GATE_RESULT_FAILED;
break;
}
ret = gate_ui_gtk_ctrl_init(&toolbar->ctrl, widget, host, userparam, parent,
NULL, false, false, position, &flags);
GATE_BREAK_IF_FAILED(ret);
gtk_toolbar_set_style(GTK_TOOLBAR(widget), GTK_TOOLBAR_ICONS);
} while (0);
return ret;
}
static void gate_ui_toolbar_button_clicked(GtkToolButton* toolbutton, gpointer user_data)
{
GtkWidget* toolbar_widget = gtk_widget_get_parent(GTK_WIDGET(toolbutton));
gate_ui_ctrl_t* toolbar_ctrl = NULL;
gate_ui_toolbar_t* toolbar = NULL;
if (NULL != toolbar_widget)
{
toolbar_ctrl = gate_ui_gtk_ctrl_resolve(toolbar_widget);
if (toolbar_ctrl)
{
toolbar = (gate_ui_toolbar_t*)toolbar_ctrl;
if (toolbar->on_click)
{
toolbar->on_click(&toolbar->ctrl, (gate_ui_toolbar_button_t)toolbutton, user_data);
}
}
}
}
gate_result_t gate_ui_toolbar_add_icon_button(gate_ui_toolbar_t* toolbar, gate_ui_icon_t const* icon,
void* button_param, gate_ui_toolbar_button_t* button_id)
{
gate_result_t ret = GATE_RESULT_OK;
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.
GtkToolItem* item;<--- The scope of the variable 'item' can be reduced. [+]The scope of the variable 'item' 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* icon_widget;<--- The scope of the variable 'icon_widget' can be reduced. [+]The scope of the variable 'icon_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.
do
{
widget = GATE_UI_GTK_GET_CTRL_WIDGET(&toolbar->ctrl);
icon_widget = gtk_image_new_from_pixbuf((GdkPixbuf*)icon->resources[0]);
item = gtk_tool_button_new(icon_widget, NULL);
if (NULL == item)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
gtk_toolbar_insert(GTK_TOOLBAR(widget), item, -1);
gtk_widget_show_all(widget);
g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(gate_ui_toolbar_button_clicked), (gpointer)button_param);
if (button_id)
{
*button_id = (gate_ui_toolbar_button_t)item;
}
} while (0);
return ret;
}
gate_result_t gate_ui_toolbar_add_image_button(gate_ui_toolbar_t* toolbar, gate_rasterimage_t const* raster,
void* button_param, gate_ui_toolbar_button_t* button_id)
{
gate_result_t ret = GATE_RESULT_OK;
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.
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.
GtkToolItem* item;<--- The scope of the variable 'item' can be reduced. [+]The scope of the variable 'item' 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_icon_t icon = GATE_INIT_EMPTY;
GtkWidget* icon_widget;<--- The scope of the variable 'icon_widget' can be reduced. [+]The scope of the variable 'icon_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.
do
{
widget = GATE_UI_GTK_GET_CTRL_WIDGET(&toolbar->ctrl);
host = GATE_UI_GTK_GET_CTRL_HOST(&toolbar->ctrl);
ret = gate_ui_icon_create_image(&icon, host, raster);
GATE_BREAK_IF_FAILED(ret);
icon_widget = gtk_image_new_from_pixbuf((GdkPixbuf*)icon.resources[0]);
if (NULL == icon_widget)
{
ret = GATE_RESULT_FAILED;
break;
}
item = gtk_tool_button_new(icon_widget, NULL);
if (NULL == item)
{
gtk_widget_destroy(icon_widget);
ret = GATE_RESULT_FAILED;
break;
}
gtk_toolbar_insert(GTK_TOOLBAR(widget), item, -1);
gtk_widget_show_all(widget);
g_signal_connect(G_OBJECT(item), "clicked", G_CALLBACK(gate_ui_toolbar_button_clicked), (gpointer)button_param);
if (button_id)
{
*button_id = (gate_ui_toolbar_button_t)item;
}
} while (0);
gate_ui_icon_destroy(&icon);
return ret;
}
gate_result_t gate_ui_toolbar_add_separator(gate_ui_toolbar_t* toolbar)
{
gate_result_t ret = GATE_RESULT_OK;
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.
GtkToolItem* item;<--- The scope of the variable 'item' can be reduced. [+]The scope of the variable 'item' 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.
do
{
widget = GATE_UI_GTK_GET_CTRL_WIDGET(&toolbar->ctrl);
item = gtk_tool_item_new();
if (NULL == item)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
gtk_toolbar_insert(GTK_TOOLBAR(widget), item, -1);
} while (0);
return ret;
}
#endif
#if defined(GATE_UI_MOTIF)
#include "gate/ui/gateui_motif.h"
#include "gate/debugging.h"
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
static gate_result_t motif_toolbar_destroy(gate_ui_ctrl_t* ctrl)
{
Widget w;
WidgetList wl = NULL;
Cardinal wcnt = 0;
Widget child;
Pixmap pm;
gate_ui_host_t* host = NULL;
Display* dpy = NULL;
GATE_DEBUG_ASSERT(ctrl != NULL);
w = GATE_UI_MOTIF_GET_CTRL_WIDGET(ctrl);
host = GATE_UI_MOTIF_GET_CTRL_HOST(ctrl);
if (w && host)
{
dpy = GATE_UI_MOTIF_GET_HOST_DISPLAY(host);
XtVaGetValues(w, XmNnumChildren, &wcnt, XmNchildren, &wl, NULL, NULL);
while (wcnt-- > 0)
{
child = wl[wcnt];
if (!child) continue;
pm = 0;
XtVaGetValues(child, XmNarmPixmap, &pm, NULL, NULL);
if (pm)
{
XFreePixmap(dpy, pm);
}
XtUnmanageChild(child);
XtDestroyWidget(child);
}
gate_ui_motif_widget_destroy_children(w, false);
}
return gate_ui_motif_ctrl_destroy_default(ctrl);
}
static gate_result_t motif_toolbar_get_text(gate_ui_ctrl_t* ctrl, gate_string_t* ptr_text)
{
GATE_UNUSED_ARG(ctrl);
GATE_UNUSED_ARG(ptr_text);
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t motif_toolbar_set_text(gate_ui_ctrl_t* ctrl, gate_string_t const* text)
{
GATE_UNUSED_ARG(ctrl);
GATE_UNUSED_ARG(text);
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t motif_toolbar_get_state(gate_ui_ctrl_t* ctrl, gate_int32_t* ptr_state)
{
GATE_UNUSED_ARG(ctrl);
GATE_UNUSED_ARG(ptr_state);
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t motif_toolbar_set_state(gate_ui_ctrl_t* ctrl, gate_int32_t state)
{
GATE_UNUSED_ARG(ctrl);
GATE_UNUSED_ARG(state);
return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t motif_toolbar_refresh(gate_ui_ctrl_t* ctrl)
{
return gate_ui_motif_ctrl_refresh(ctrl);
}
static gate_result_t motif_toolbar_get_children(gate_ui_ctrl_t* ctrl, void** ptr_to_widgetlist, gate_size_t* ptr_to_children_count)
{
GATE_UNUSED_ARG(ctrl);
if (ptr_to_widgetlist) *ptr_to_widgetlist = NULL;
if (ptr_to_children_count) *ptr_to_children_count = 0;
return GATE_RESULT_OK;
}
static gate_ui_motif_dispatcher_t tb_dispatcher =
{
&motif_toolbar_destroy,
&motif_toolbar_get_text,
&motif_toolbar_set_text,
&motif_toolbar_get_state,
&motif_toolbar_set_state,
&motif_toolbar_refresh,
&motif_toolbar_get_children
};
gate_result_t gate_ui_toolbar_create(gate_ui_toolbar_t* toolbar, gate_ui_ctrl_t* parent, gate_ui_position_t const* position,
gate_uint32_t flags, void* userparam)
{
gate_result_t ret = GATE_RESULT_FAILED;
Arg args[8];
unsigned args_count = 0;
do
{
if (!toolbar || !parent)
{
ret = GATE_RESULT_INVALIDARG;
break;
}
XtSetArg(args[args_count], XmNorientation, XmHORIZONTAL); ++args_count;
XtSetArg(args[args_count], XmNshadowThickness, 1); ++args_count;
XtSetArg(args[args_count], XmNhighlightThickness, 1); ++args_count;
ret = gate_ui_motif_ctrl_create(&toolbar->ctrl, xmRowColumnWidgetClass, userparam, NULL, parent,
position, &flags, false, &tb_dispatcher, args, args_count);
GATE_BREAK_IF_FAILED(ret);
} while (0);
return ret;
}
static void toolbar_button_pushed(Widget widget, XtPointer client_data, XtPointer call_data)
{
void* button_param = (void*)client_data;
Widget parent = XtParent(widget);
gate_ui_ctrl_t* ptr_ctrl = gate_ui_motif_widget_get_ctrl(parent);
gate_ui_toolbar_t* ptr_toolbar = (gate_ui_toolbar_t*)ptr_ctrl;
gate_ui_toolbar_button_t button_id = (gate_ui_toolbar_button_t)widget;
if (!ptr_toolbar || !ptr_toolbar->on_click)
{
return;
}
ptr_toolbar->on_click(ptr_ctrl, button_id, button_param);
}
gate_result_t gate_ui_toolbar_add_icon_button(gate_ui_toolbar_t* toolbar, gate_ui_icon_t const* icon, void* button_param,
gate_ui_toolbar_button_t* button_id)
{
return GATE_RESULT_NOTIMPLEMENTED;
}
gate_result_t gate_ui_toolbar_add_image_button(gate_ui_toolbar_t* toolbar, gate_rasterimage_t const* raster, void* button_param,
gate_ui_toolbar_button_t* button_id)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_ui_host_t* host = GATE_UI_MOTIF_GET_CTRL_HOST(&toolbar->ctrl);
Widget widget_tb;
Widget new_button = NULL;
gate_ui_graphics_t graph;
gate_ui_position_t pos = { 0, 0, 32, 32 };
gate_ui_color_t bg_col = { 192, 192, 192, 255 };
Pixmap pm = 0;
Arg args[8];
Cardinal args_count = 0;
GATE_DEBUG_ASSERT(toolbar != NULL);
do
{
widget_tb = GATE_UI_MOTIF_GET_CTRL_WIDGET(&toolbar->ctrl);
GATE_DEBUG_ASSERT(widget_tb != NULL);
ret = gate_ui_graphics_create_image(&graph, host, raster->width, raster->height, 0);
GATE_BREAK_IF_FAILED(ret);
gate_ui_graphics_rect(&graph, pos, &bg_col, 0, &bg_col);
ret = gate_ui_graphics_draw_image(&graph, raster, NULL, NULL, NULL);
GATE_BREAK_IF_FAILED(ret);
pm = (Pixmap)gate_ui_graphics_surface(&graph);
graph.resources[1] = NULL; /* remove pixmap resource from graphics object */
XtSetArg(args[args_count], XmNuserData, NULL); ++args_count;
XtSetArg(args[args_count], XmNlabelType, XmPIXMAP); ++args_count;
XtSetArg(args[args_count], XmNarmPixmap, pm); ++args_count;
XtSetArg(args[args_count], XmNshadowThickness, 1); ++args_count;
XtSetArg(args[args_count], XmNhighlightThickness, 1); ++args_count;
new_button = XtCreateWidget(NULL, xmPushButtonWidgetClass, widget_tb, args, args_count);
gate_ui_graphics_destroy(&graph);
if (!new_button)
{
ret = GATE_RESULT_FAILED;
break;
}
XtManageChild(new_button);
XtAddCallback(new_button, XmNactivateCallback, &toolbar_button_pushed, button_param);
*button_id = (gate_ui_toolbar_button_t)(gate_intptr_t)new_button;
ret = GATE_RESULT_OK;
} while (0);
if (GATE_FAILED(ret) && (new_button != NULL))
{
XtDestroyWidget(new_button);
}
return ret;
}
gate_result_t gate_ui_toolbar_add_separator(gate_ui_toolbar_t* toolbar)
{
return GATE_RESULT_OK;
}
#endif /* GATE_UI_MOTIF */
|