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
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
/* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright (c) 2018-2026, 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/graphics.h"
#include "gate/results.h"

#if defined(GATE_UI_WINAPI)

#include "gate/ui/gateui_winapi.h"
#include "gate/platforms.h"
#include "gate/debugging.h"
#include <shellapi.h>

#if !defined(GATE_SYS_WIN16)
#include <commctrl.h>
#endif

#define GATE_UI_GRAPHICS_HDC(graph)     ((HDC)(graph)->resources[0])
#define GATE_UI_GRAPHICS_BACKUP(graph)  ((HBITMAP)(graph)->resources[1])
#define GATE_UI_GRAPHICS_WINDOW(graph)  ((HWND)(graph)->resources[2])
#define GATE_UI_GRAPHICS_BYTEBUFFER(graph)  ((void*)(graph)->resources[3])

gate_result_t gate_ui_graphics_create_virtual(gate_ui_graphics_t* graph, gate_ui_host_t* host, gate_uint32_t width, gate_uint32_t height)
{
    HWND desk = GetDesktopWindow();
    HDC deskdc = GetDC(desk);
    HDC dc;
    HBITMAP bmp;
    gate_mem_clear(graph, sizeof(gate_ui_graphics_t));
    dc = CreateCompatibleDC(deskdc);
    if (dc == NULL)
    {
        ReleaseDC(desk, deskdc);
        return GATE_RESULT_FAILED;
    }
    bmp = CreateCompatibleBitmap(deskdc, (int)width, (int)height);
    ReleaseDC(desk, deskdc);
    if (bmp == NULL)
    {
        DeleteDC(dc);
        return GATE_RESULT_OUTOFRESOURCES;
    }
    graph->host = host;
    graph->width = width;
    graph->height = height;
    graph->resources[0] = dc;
    graph->resources[1] = SelectObject(dc, (HGDIOBJ)bmp);
    return GATE_RESULT_OK;
}

typedef struct gate_ui_bitmap_header_class
{
    BITMAPINFO  info;
    RGBQUAD     palette[256];
}  gate_ui_bitmap_header_t;

static gate_size_t resolve_color_index(gate_uint8_t r, gate_uint8_t g, gate_uint8_t b)
{
    const gate_size_t nr = ((gate_size_t)r + 26u) / 51u;
    const gate_size_t ng = ((gate_size_t)g + 26u) / 51u;
    const gate_size_t nb = ((gate_size_t)b + 26u) / 51u;

    const gate_size_t ndx = nr * 36u + ng * 6u + nb;
    return ndx;
}

static DWORD create_palette_8(RGBQUAD* ptr_quad)
{
    unsigned short r, g, b;
    for (r = 0; r < 6; ++r)
    {
        for (g = 0; g < 6; ++g)
        {
            for (b = 0; b < 6; ++b)
            {
                ptr_quad->rgbBlue = (unsigned char)(b * 51);
                ptr_quad->rgbGreen = (unsigned char)(g * 51);
                ptr_quad->rgbRed = (unsigned char)(r * 51);
                ptr_quad->rgbReserved = 0;
                ++ptr_quad;
            }
        }
    }
    return 216;
}

gate_result_t gate_ui_graphics_create_image(gate_ui_graphics_t* graph, gate_ui_host_t* host, gate_uint32_t width, gate_uint32_t height, gate_uint32_t depth)
{
    gate_result_t ret;
    HWND desk = GetDesktopWindow();
    HDC deskdc = GetDC(desk);
    HDC dc = NULL;
    HBITMAP hbitmap;
    HBITMAP hbackup;
    gate_ui_bitmap_header_t bitmapinfo;
    gate_uint8_t r;
    gate_size_t linelen;
    gate_size_t bufferlen;
    void* buffer;
    RGBQUAD* ptr_quad;

    do
    {
        gate_mem_clear(graph, sizeof(gate_ui_graphics_t));

        if (deskdc == NULL)
        {
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }
        dc = CreateCompatibleDC(deskdc);
        if (dc == NULL)
        {
            ret = GATE_RESULT_OUTOFRESOURCES;
            break;
        }


        bitmapinfo.info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        bitmapinfo.info.bmiHeader.biWidth = width;
        bitmapinfo.info.bmiHeader.biHeight = height;
        bitmapinfo.info.bmiHeader.biPlanes = 1;
        bitmapinfo.info.bmiHeader.biBitCount = depth;
        bitmapinfo.info.bmiHeader.biCompression = BI_RGB;
        bitmapinfo.info.bmiHeader.biSizeImage = 0;
        bitmapinfo.info.bmiHeader.biXPelsPerMeter = 0;
        bitmapinfo.info.bmiHeader.biYPelsPerMeter = 0;
        bitmapinfo.info.bmiHeader.biClrImportant = 0;

        if (depth == 1)
        {
            bitmapinfo.info.bmiHeader.biClrUsed = 2;

            /* black */
            bitmapinfo.info.bmiColors[0].rgbBlue = 0;
            bitmapinfo.info.bmiColors[0].rgbGreen = 0;
            bitmapinfo.info.bmiColors[0].rgbRed = 0;
            bitmapinfo.info.bmiColors[0].rgbReserved = 0;

            /* white */
            bitmapinfo.info.bmiColors[1].rgbBlue = 255;
            bitmapinfo.info.bmiColors[1].rgbGreen = 255;
            bitmapinfo.info.bmiColors[1].rgbRed = 255;
            bitmapinfo.info.bmiColors[1].rgbReserved = 0;
        }
        else if (depth < 8)
        {
            static gate_color_rgb_t vga_pal[16];
            static gate_bool_t vga_pal_init = false;
            if (!vga_pal_init)
            {
                gate_color_palette_rgb4_create(vga_pal);
                vga_pal_init = true;
            }

            bitmapinfo.info.bmiHeader.biClrUsed = 16;
            ptr_quad = &bitmapinfo.info.bmiColors[0];
            for (r = 0; r < 16; ++r)
            {
                ptr_quad->rgbBlue = vga_pal[r].b;
                ptr_quad->rgbGreen = vga_pal[r].g;
                ptr_quad->rgbRed = vga_pal[r].r;
                ptr_quad->rgbReserved = 0;
                ++ptr_quad;
            }
        }
        else if (depth < 16)
        {
            ptr_quad = &bitmapinfo.info.bmiColors[0];
            bitmapinfo.info.bmiHeader.biClrUsed = create_palette_8(ptr_quad);
        }
        else
        {
            bitmapinfo.info.bmiHeader.biClrUsed = 0;
        }

        linelen = (bitmapinfo.info.bmiHeader.biWidth * (LONG)bitmapinfo.info.bmiHeader.biBitCount + 7) / 8;
        if (linelen % 4 != 0)
        {
            linelen += (4 - (linelen % 4));
        }
        bufferlen = linelen * bitmapinfo.info.bmiHeader.biHeight;
#if defined(GATE_SYS_WIN16)
        buffer = gate_mem_alloc(bufferlen);
        gate_mem_clear(buffer, bufferlen);
        hbitmap = CreateDIBitmap(deskdc, &bitmapinfo.info.bmiHeader, CBM_INIT, buffer, &bitmapinfo.info, DIB_RGB_COLORS);
#else
        buffer = NULL;
        hbitmap = CreateDIBSection(deskdc, &bitmapinfo.info, DIB_RGB_COLORS, &buffer, NULL, 0);
#endif
        if (hbitmap == NULL)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }
        hbackup = SelectObject(dc, hbitmap);

        graph->width = width;
        graph->height = height;
        graph->host = host;
        graph->resources[0] = dc;
        graph->resources[1] = hbackup;
        graph->resources[2] = NULL;
        graph->resources[3] = buffer;
        ret = GATE_RESULT_OK;

    } while (0);

    if (deskdc != NULL)
    {
        ReleaseDC(desk, deskdc);
    }

    return ret;
}

gate_result_t gate_ui_graphics_create_image_from(gate_ui_graphics_t* graph, gate_ui_host_t* host, gate_rasterimage_t const* rasterimage)
{
    
    gate_uint32_t x, y;
    gate_result_t ret;
#if defined(GATE_WIN32_ANSI)

#   if 0
    /* use 8-bit DIB */
    ret = gate_ui_graphics_create_image(graph, host, rasterimage->width, rasterimage->height, 8);
    if (GATE_SUCCEEDED(ret))
    {
        gate_uint8_t* ptrbytes = (gate_uint8_t*)GATE_UI_GRAPHICS_BYTEBUFFER(graph);
        unsigned line_patch = (unsigned)(rasterimage->width % 4);
        if (line_patch != 0)
        {
            line_patch = 4 - line_patch;
        }
        for (y = 0; y != rasterimage->height; ++y)
        {
            gate_color_t const* ptrpixels = (gate_color_t const*)gate_rasterimage_get_line_ptr(rasterimage, (rasterimage->height - 1 - y));
            for (x = 0; x != rasterimage->width; ++x)
            {
                gate_uint16_t col16;
                col16 = (((gate_uint16_t)ptrpixels->r + 25) / 51) * 36;
                col16 += (((gate_uint16_t)ptrpixels->g + 25) / 51) * 6;
                col16 += (((gate_uint16_t)ptrpixels->b + 25) / 51);

                *(ptrbytes++) = (gate_uint8_t)(col16 & 0xff);
                ++ptrpixels;
            }
            ptrbytes += line_patch;
        }
    }
#   else
    /* use 16-bit DIB */
    ret = gate_ui_graphics_create_image(graph, host, rasterimage->width, rasterimage->height, 16);
    if (GATE_SUCCEEDED(ret))
    {
        gate_uint8_t* ptrbytes = (gate_uint8_t*)GATE_UI_GRAPHICS_BYTEBUFFER(graph);
        unsigned line_patch = (unsigned)(rasterimage->width % 2) * 2;
        for (y = 0; y != rasterimage->height; ++y)
        {
            gate_color_t const* ptrpixels = (gate_color_t const*)gate_rasterimage_get_line_ptr(rasterimage, (rasterimage->height - 1 - y));
            for (x = 0; x != rasterimage->width; ++x)
            {
                gate_uint16_t col16
                    = ((gate_uint16_t)(ptrpixels->b & 0xF8) >> 3)
                    | ((gate_uint16_t)(ptrpixels->g & 0xF8) << 2)
                    | ((gate_uint16_t)(ptrpixels->r & 0xF8) << 7);

                *(ptrbytes++) = (gate_uint8_t)(col16 & 0xff);
                *(ptrbytes++) = (gate_uint8_t)(col16 >> 8);
                ++ptrpixels;
            }
            ptrbytes += line_patch;
        }
    }
#   endif

#else
    ret = gate_ui_graphics_create_image(graph, host, rasterimage->width, rasterimage->height, 32);
    if (GATE_SUCCEEDED(ret))
    {
        gate_uint8_t* ptrbytes = (gate_uint8_t*)GATE_UI_GRAPHICS_BYTEBUFFER(graph);
        for (y = 0; y != rasterimage->height; ++y)
        {
            if ((rasterimage->pixel_format == GATE_IMAGE_PIXELFORMAT_DEFAULT) 
             || (rasterimage->pixel_format == GATE_IMAGE_PIXELFORMAT_RGBA)
             || (rasterimage->pixel_format == GATE_IMAGE_PIXELFORMAT_RGB32)
            )
            {
                gate_color_t const* ptrpixels = (gate_color_t const*)gate_rasterimage_get_line_ptr(rasterimage, (rasterimage->height - 1 - y));
                for (x = 0; x != rasterimage->width; ++x)
                {
                    *(ptrbytes++) = ptrpixels->b;
                    *(ptrbytes++) = ptrpixels->g;
                    *(ptrbytes++) = ptrpixels->r;
                    *(ptrbytes++) = 0;
                    ++ptrpixels;
                }
            }
            else
            {
                unsigned const line = rasterimage->height - 1 - y;
                for (x = 0; x != rasterimage->width; ++x)
                {
                    gate_color_t pixel;
                    gate_rasterimage_get_pixel(rasterimage, x, line, &pixel);
                    *(ptrbytes++) = pixel.b;
                    *(ptrbytes++) = pixel.g;
                    *(ptrbytes++) = pixel.r;
                    *(ptrbytes++) = 0;
                }
            }
        }
    }
#endif
    return ret;
}


gate_result_t gate_ui_graphics_create_ctrl(gate_ui_graphics_t* graph, gate_ui_ctrl_t* ctrl)
{
    HWND hwnd = (HWND)GATE_UI_WINAPI_GET_HWND(ctrl);
    RECT rect;

    gate_mem_clear(graph, sizeof(gate_ui_graphics_t));

    GetClientRect(hwnd, &rect);

    graph->host = GATE_UI_WINAPI_GET_HOST(ctrl);
    graph->width = rect.right - rect.left;
    graph->height = rect.bottom - rect.top;
    graph->resources[0] = GetDC(hwnd);
    graph->resources[2] = hwnd;
    return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_create_native(gate_ui_graphics_t* graph, gate_ui_host_t* host, void* graphics, void* param, gate_int32_t width, gate_int32_t height)
{
    GATE_UNUSED_ARG(param);
    gate_mem_clear(graph, sizeof(gate_ui_graphics_t));
    graph->host = host;
    graph->resources[0] = graphics;
    graph->resources[3] = (void*)(gate_intptr_t)(-1);   ///< no-release flag
    graph->width = width;
    graph->height = height;
    return GATE_RESULT_OK;
}
gate_result_t gate_ui_graphics_destroy(gate_ui_graphics_t* graph)
{
    gate_bool_t success = true;
    HDC hdc = GATE_UI_GRAPHICS_HDC(graph);
    HBITMAP hbmp = GATE_UI_GRAPHICS_BACKUP(graph);
    HWND hwnd = GATE_UI_GRAPHICS_WINDOW(graph);
    void* buffer_ptr = GATE_UI_GRAPHICS_BYTEBUFFER(graph);

    if (-1 == (gate_intptr_t)buffer_ptr)
    {
        // no-release flag is active
    }
    else
    {
        if (hbmp != NULL)
        {
            success &= (FALSE != DeleteObject(SelectObject(hdc, (HGDIOBJ)hbmp)));
        }

        if (hwnd != NULL)
        {
            success &= (0 != ReleaseDC(hwnd, hdc));
        }
        else
        {
            success &= (FALSE != DeleteDC(hdc));
        }

        if (buffer_ptr != NULL)
        {
#if defined(GATE_SYS_WIN16)
            gate_mem_dealloc(buffer_ptr);
#else
            /* win32 dib section will release the buffer bits automatically */
#endif
        }

    }

    return success ? GATE_RESULT_OK : GATE_RESULT_FAILED;
}

gate_int32_t gate_ui_graphics_width(gate_ui_graphics_t* graph)
{
    return (graph != NULL) ? graph->width : 0;
}
gate_int32_t gate_ui_graphics_height(gate_ui_graphics_t* graph)
{
    return (graph != NULL) ? graph->height : 0;
}

#ifndef CLR_INVALID
#define CLR_INVALID 0xffffffff
#endif

gate_result_t gate_ui_graphics_set_pixel(gate_ui_graphics_t* graph, gate_ui_point_t pos, gate_ui_color_t col)
{
    HDC hdc = GATE_UI_GRAPHICS_HDC(graph);
    COLORREF colref = RGB(col.r, col.g, col.b);
    COLORREF result = SetPixel(hdc, (int)pos.x, (int)pos.y, colref);
    if (result == CLR_INVALID)
    {
        return GATE_RESULT_FAILED;
    }
    else
    {
        return GATE_RESULT_OK;
    }
}
gate_result_t gate_ui_graphics_get_pixel(gate_ui_graphics_t* graph, gate_ui_point_t pos, gate_ui_color_t* col)
{
    HDC hdc = GATE_UI_GRAPHICS_HDC(graph);
    COLORREF colref = GetPixel(hdc, (int)pos.x, (int)pos.y);
    if (colref == CLR_INVALID)
    {
        return GATE_RESULT_FAILED;
    }
    else
    {
        col->r = GetRValue(colref);
        col->g = GetGValue(colref);
        col->b = GetBValue(colref);
        col->a = 255;
        return GATE_RESULT_OK;
    }
}
gate_result_t gate_ui_graphics_draw(gate_ui_graphics_t* graph, gate_ui_graphics_t* srcgraph,
    gate_ui_point_t const* dst, gate_ui_size_t const* size, gate_ui_point_t const* srcpos)
{
    HDC hdc_dst = GATE_UI_GRAPHICS_HDC(graph);
    HDC hdc_src = GATE_UI_GRAPHICS_HDC(srcgraph);
    int dst_x = dst ? dst->x : 0;
    int dst_y = dst ? dst->y : 0;
    int width = size ? size->width : -1;
    int height = size ? size->height : -1;
    int src_x = srcpos ? srcpos->x : 0;
    int src_y = srcpos ? srcpos->y : 0;

    if (width < 0)
    {
        width += gate_ui_graphics_width(srcgraph) + 1;
    }
    if (height < 0)
    {
        height += gate_ui_graphics_height(srcgraph) + 1;
    }

    if (FALSE == BitBlt(hdc_dst, dst_x, dst_y, width, height, hdc_src, src_x, src_y, SRCCOPY))
    {
        return GATE_RESULT_FAILED;
    }
    else
    {
        return GATE_RESULT_OK;
    }
}

gate_result_t gate_ui_graphics_draw_ex(gate_ui_graphics_t* graph, gate_ui_graphics_t* src_graph,
    gate_ui_position_t const* dest_rect, gate_ui_position_t const* src_rect)
{
    HDC hdc_dst = GATE_UI_GRAPHICS_HDC(graph);
    HDC hdc_src = GATE_UI_GRAPHICS_HDC(src_graph);
    int dst_x = dest_rect ? dest_rect->pos.x : 0;
    int dst_y = dest_rect ? dest_rect->pos.y : 0;
    int dst_width = dest_rect ? dest_rect->size.width : -1;
    int dst_height = dest_rect ? dest_rect->size.height : -1;
    int src_x = src_rect ? src_rect->pos.x : 0;
    int src_y = src_rect ? src_rect->pos.y : 0;
    int src_width = src_rect ? src_rect->size.width : -1;
    int src_height = src_rect ? src_rect->size.height : -1;

    if (dst_width < 0)
    {
        dst_width += gate_ui_graphics_width(graph) + 1;
    }
    if (dst_height < 0)
    {
        dst_height += gate_ui_graphics_height(graph) + 1;
    }

    if (src_width < 0)
    {
        src_width += gate_ui_graphics_width(src_graph) + 1;
    }
    if (src_height < 0)
    {
        src_height += gate_ui_graphics_height(src_graph) + 1;
    }
#if defined(COLORONCOLOR) && !defined(__MINGW32CE__)
    SetStretchBltMode(hdc_dst, COLORONCOLOR);
#endif

    if (FALSE == StretchBlt(hdc_dst, dst_x, dst_y, dst_width, dst_height, hdc_src,
        src_x, src_y, src_width, src_height, SRCCOPY))
    {
        return GATE_RESULT_FAILED;
    }
    else
    {
        return GATE_RESULT_OK;
    }
}

gate_result_t gate_ui_graphics_draw_image(gate_ui_graphics_t* graph, gate_rasterimage_t const* srcimage,
    gate_ui_point_t const* dst, gate_ui_size_t const* size, gate_ui_point_t const* srcpos)
{
    gate_ui_graphics_t src_graph;
    gate_result_t ret = gate_ui_graphics_create_image_from(&src_graph, graph->host, srcimage);
    if (GATE_SUCCEEDED(ret))
    {
        ret = gate_ui_graphics_draw(graph, &src_graph, dst, size, srcpos);
        gate_ui_graphics_destroy(&src_graph);
    }
    return ret;
}


gate_result_t gate_ui_graphics_line(gate_ui_graphics_t* graph, gate_ui_point_t from, gate_ui_point_t to, gate_ui_color_t col, gate_uint32_t linewidth)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HDC hdc = GATE_UI_GRAPHICS_HDC(graph);
    COLORREF colref = RGB(col.r, col.g, col.b);
    HPEN hpen = CreatePen(PS_SOLID, linewidth, colref);
    HGDIOBJ hselpen = SelectObject(hdc, (HGDIOBJ)hpen);
    if (MoveToEx(hdc, (int)from.x, (int)from.y, NULL))
    {
        if (LineTo(hdc, (int)to.x, (int)to.y))
        {
            SetPixel(hdc, (int)to.x, (int)to.y, colref);
            ret = GATE_RESULT_OK;
        }
    }
    DeleteObject(SelectObject(hdc, hselpen));
    return ret;
}
gate_result_t gate_ui_graphics_rect(gate_ui_graphics_t* graph, gate_ui_position_t rect, gate_ui_color_t const* colline, gate_uint32_t linewidth, gate_ui_color_t const* colfill)
{
    gate_result_t success = true;
    HDC hdc = GATE_UI_GRAPHICS_HDC(graph);
    RECT r;
    HGDIOBJ hbackup;
    HBRUSH hbrush;
    HPEN hpen;
    COLORREF colref;

    if (colfill != NULL)
    {
        colref = RGB(colfill->r, colfill->g, colfill->b);
        hbrush = CreateSolidBrush(colref);
        r.left = rect.pos.x;
        r.top = rect.pos.y;
        r.right = rect.pos.x + rect.size.width + 1;
        r.bottom = rect.pos.y + rect.size.height + 1;
        success &= (0 != FillRect(hdc, &r, hbrush));
        DeleteObject(hbrush);
    }
    if (colline != NULL)
    {
        colref = RGB(colline->r, colline->g, colline->b);
        hpen = CreatePen(PS_SOLID, (int)linewidth, colref);
        hbackup = SelectObject(hdc, hpen);
        success &= (FALSE != MoveToEx(hdc, rect.pos.x, rect.pos.y, NULL));
        success &= (FALSE != LineTo(hdc, rect.pos.x + rect.size.width, rect.pos.y));
        success &= (FALSE != LineTo(hdc, rect.pos.x + rect.size.width, rect.pos.y + rect.size.height));
        success &= (FALSE != LineTo(hdc, rect.pos.x, rect.pos.y + rect.size.height));
        success &= (FALSE != LineTo(hdc, rect.pos.x, rect.pos.y));
        DeleteObject(SelectObject(hdc, hbackup));
    }
    return success ? GATE_RESULT_OK : GATE_RESULT_FAILED;
}
gate_result_t gate_ui_graphics_polygon(gate_ui_graphics_t* graph, gate_ui_point_t const* points, gate_size_t pointcount, gate_ui_color_t const* colline, gate_uint32_t linewidth, gate_ui_color_t const* colfill)
{
    gate_result_t success = true;
    HDC hdc = GATE_UI_GRAPHICS_HDC(graph);
    HGDIOBJ hbackup;
    HGDIOBJ hbackup2;
    HPEN hpen;
    HBRUSH hbrush;
    COLORREF colref;
    gate_size_t ndx;
    POINT pnts[1024];

    if (colfill != NULL)
    {
        colref = RGB(colfill->r, colfill->g, colfill->b);
        if (pointcount > sizeof(pnts) / sizeof(pnts[0]))
        {
            pointcount = sizeof(pnts) / sizeof(pnts[0]);
        }
        for (ndx = 0; ndx < pointcount; ++ndx)
        {
            pnts[ndx].x = (int)points[ndx].x;
            pnts[ndx].y = (int)points[ndx].y;
        }
        hpen = CreatePen(PS_NULL, 0, 0);
        hbackup = SelectObject(hdc, (HGDIOBJ)hpen);
        hbrush = CreateSolidBrush(colref);
        hbackup2 = SelectObject(hdc, (HGDIOBJ)hbrush);

        success &= (FALSE != Polygon(hdc, pnts, (int)pointcount));

        DeleteObject(SelectObject(hdc, hbackup2));
        DeleteObject(SelectObject(hdc, hbackup));
    }

    if (colline != NULL)
    {
        colref = RGB(colline->r, colline->g, colline->b);
        hpen = CreatePen(PS_SOLID, linewidth, colref);
        hbackup = SelectObject(hdc, (HGDIOBJ)hpen);
        success &= (FALSE != MoveToEx(hdc, (int)points[0].x, (int)points[0].y, NULL));
        for (ndx = 1; ndx != pointcount; ++ndx)
        {
            success &= (FALSE != LineTo(hdc, (int)points[ndx].x, (int)points[ndx].y));
        }
        success &= (FALSE != LineTo(hdc, (int)points[0].x, (int)points[0].y));
        DeleteObject(SelectObject(hdc, hbackup));
    }
    return success ? GATE_RESULT_OK : GATE_RESULT_FAILED;
}

#if defined(GATE_SYS_WIN16)

static BOOL GetTextExtentExPoint(HDC hdc, LPCSTR lpszString, int cchString, int nMaxExtent, LPINT lpnFit, LPINT lpnDx, LPSIZE lpSize)
{
    SIZE sz;
    SIZE szchr;
    GATE_UNUSED_ARG(nMaxExtent);
    GATE_UNUSED_ARG(lpnFit);
    sz.cx = 0;
    sz.cy = 0;
    while (cchString-- > 0)
    {
        GetTextExtentPoint(hdc, lpszString, 1, &szchr);
        sz.cx += szchr.cx;
        *lpnDx = szchr.cx;

        ++lpszString;
        ++lpnDx;
    }
    lpSize->cx = sz.cx;
    lpSize->cy = sz.cy;
    return TRUE;
}

#define GetTextExtentPoint32 GetTextExtentPoint

#endif

gate_result_t gate_ui_graphics_get_char_size(gate_ui_graphics_t* graph, gate_ui_font_t const* font,
    gate_char32_t const* chars, gate_size_t charcount, gate_int32_t* charlens)
{
    /* NOTICE: this function only supports up to 2048 characters! */
    gate_result_t ret = GATE_RESULT_FAILED;
    HDC hdc = GATE_UI_GRAPHICS_HDC(graph);
    TCHAR buffer[2048];
    int lengths[2048];
    gate_size_t bufferused = gate_win32_utf32_2_winstr(chars, charcount, buffer, sizeof(buffer) / sizeof(buffer[0]));
    HGDIOBJ hbackup;
    HFONT hfont = (HFONT)gate_ui_winapi_create_font(font);
    SIZE sz = { 0, 0 };
    gate_size_t index;

    hbackup = SelectObject(hdc, (HGDIOBJ)hfont);

    if (FALSE == GetTextExtentExPoint(hdc, buffer, (int)bufferused, 0, NULL, lengths, &sz))
    {
        gate_win32_print_lasterror(&ret, NULL, 0);
    }
    else
    {
        for (index = 0; index != bufferused; ++index)
        {
            charlens[index] = lengths[index];
        }
        ret = GATE_RESULT_OK;
    }

    DeleteObject(SelectObject(hdc, hbackup));
    return ret;
}
gate_result_t gate_ui_graphics_get_text_size(gate_ui_graphics_t* graph, gate_ui_font_t const* font,
    gate_string_t const* txt, gate_int32_t* width, gate_int32_t* height)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HDC hdc = GATE_UI_GRAPHICS_HDC(graph);
    HGDIOBJ hbackup = NULL;
    TCHAR buffer[4096];
    gate_size_t bufferused = gate_win32_utf8_2_winstr(txt->str, txt->length, buffer, sizeof(buffer) / sizeof(buffer[0]));
    HFONT hfont = (HFONT)gate_ui_winapi_create_font(font);
    SIZE sz = { 0, 0 };

    hbackup = SelectObject(hdc, (HGDIOBJ)hfont);

    if (FALSE == GetTextExtentPoint32(hdc, buffer, (int)bufferused, &sz))
    {
        gate_win32_print_lasterror(&ret, NULL, 0);
    }
    else
    {
        if (width)
        {
            *width = (gate_int32_t)sz.cx;
        }
        if (height)
        {
            *height = (gate_int32_t)sz.cy;
        }
        ret = GATE_RESULT_OK;
    }

    DeleteObject(SelectObject(hdc, hbackup));
    return ret;
}
gate_result_t gate_ui_graphics_print(gate_ui_graphics_t* graph, gate_ui_font_t const* font,
    gate_string_t const* text, gate_ui_position_t const* pos)
{
    gate_bool_t success = true;
    HDC hdc = GATE_UI_GRAPHICS_HDC(graph);
    HGDIOBJ hbackup;
    HFONT hfont = (HFONT)gate_ui_winapi_create_font(font);
    TCHAR buffer[4096];
    RECT rect;
    UINT flags = DT_LEFT | DT_NOPREFIX | DT_NOCLIP;
    gate_size_t bufferused = gate_win32_utf8_2_winstr(text->str, text->length, buffer, sizeof(buffer) / sizeof(buffer[0]));
    rect.left = pos->pos.x;
    rect.top = pos->pos.y;
    if ((pos->size.width == 0) && (pos->size.height == 0))
    {
        rect.bottom = graph->width;
        rect.right = graph->height;
    }
    else
    {
        rect.bottom = pos->pos.x + pos->size.width + 1;
        rect.right = pos->pos.y + pos->size.height + 1;
        flags |= DT_VCENTER;
    }
    SetTextColor(hdc, RGB(font->color.r, font->color.g, font->color.b));
    SetBkMode(hdc, TRANSPARENT);
    hbackup = SelectObject(hdc, (HGDIOBJ)hfont);
    success &= ((0 != DrawText(hdc, buffer, (int)bufferused, &rect, flags)) ? true : false);
    DeleteObject(SelectObject(hdc, hbackup));
    return success ? GATE_RESULT_OK : GATE_RESULT_FAILED;
}

void* gate_ui_graphics_handle(gate_ui_graphics_t* graph)
{
    return (void*)GATE_UI_GRAPHICS_HDC(graph);
}
void* gate_ui_graphics_surface(gate_ui_graphics_t* graph)
{
    return (void*)GATE_UI_GRAPHICS_WINDOW(graph);
}


/*****************************
 * icon WIN32 implementation *
 *****************************/

gate_result_t gate_ui_icon_create_native(gate_ui_icon_t* icon, gate_ui_host_t* host, void* handle, gate_uint32_t flags)
{
    icon->host = host;
    icon->resources[0] = handle;
    icon->flags = flags;
    return GATE_RESULT_OK;
}

typedef UINT(WINAPI* ExtractIconEx_funcptr_t)(LPCTSTR lpszFile, int nIconIndex, HICON* phiconLarge, HICON* phiconSmall, UINT nIcons);
static ExtractIconEx_funcptr_t ExtractIconEx_function = NULL;

static HICON gate_ui_extract_icon(LPCTSTR file, int icon_index, gate_uint32_t flags)
{
    gate_bool_t is_small = GATE_FLAG_ENABLED(flags, GATE_UI_ICON_FLAG_SMALL);
    HICON hicon = NULL;
    HMODULE hmod;
    UINT extracted;
    do
    {
        if (ExtractIconEx_function == NULL)
        {
            hmod = gate_win32_get_shell_module();
            if (hmod != NULL)
            {
                gate_win32_get_proc_address(hmod, GATE_WINAPI_DUAL_NAME("ExtractIconEx"), &ExtractIconEx_function);
            }
        }
        if (ExtractIconEx_function != NULL)
        {
            extracted = ExtractIconEx_function(file, icon_index, is_small ? NULL : &hicon, is_small ? &hicon : NULL, 1);
            if (extracted == 0)
            {
                hicon = NULL;
            }
        }
    } while (0);
    return hicon;
}

gate_result_t gate_ui_icon_create_stock(gate_ui_icon_t* icon, gate_ui_host_t* host, gate_uint32_t stock_id, gate_uint32_t flags)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HICON hicon = NULL;

#if defined(GATE_SYS_WINCE)
    hicon = LoadCursor(NULL, IDC_APPSTARTING);
#else
    LPCTSTR icon_source_file = _T("shell32.dll");
    int icon_source_index = -1;
    int icon_source_index_alt = -1;

    switch (stock_id)
    {
    case GATE_UI_ICON_STOCK_APP:            icon_source_index = 2; break;
    case GATE_UI_ICON_STOCK_DOCUMENT:       icon_source_index = 1; break;
    case GATE_UI_ICON_STOCK_FOLDER:         icon_source_index = 3; break;
    case GATE_UI_ICON_STOCK_FOLDEROPEN:     icon_source_index = 4; break;
    case GATE_UI_ICON_STOCK_NEWFILE:        icon_source_index = 70; icon_source_index_alt = 1; break;
    case GATE_UI_ICON_STOCK_OPENFILE:       icon_source_index = 126; icon_source_index_alt = 85; break;
    case GATE_UI_ICON_STOCK_SAVEFILE:       icon_source_index = 258; icon_source_index_alt = 6; break;
    case GATE_UI_ICON_STOCK_PRINTER:        icon_source_index = 16; break;
    case GATE_UI_ICON_STOCK_CUT:            icon_source_index = 259; icon_source_index_alt = 65; break;
    case GATE_UI_ICON_STOCK_COPY:           icon_source_index = 134; icon_source_index_alt = 54; break;
    case GATE_UI_ICON_STOCK_PASTE:          icon_source_index = 260; icon_source_index_alt = 66; break;
    case GATE_UI_ICON_STOCK_DELETE:         icon_source_index = 131; icon_source_index_alt = 105; break;
    case GATE_UI_ICON_STOCK_FIND:           icon_source_index = 22; break;
    case GATE_UI_ICON_STOCK_HELP:           icon_source_index = 23; break;

    case GATE_UI_ICON_STOCK_STORAGE:        icon_source_index = 8; icon_source_index_alt = 8; break;
    case GATE_UI_ICON_STOCK_COMPUTER:       icon_source_index = 15; icon_source_index_alt = 15; break;
    case GATE_UI_ICON_STOCK_NETWORK:        icon_source_index = 18; icon_source_index_alt = 18; break;
    case GATE_UI_ICON_STOCK_GLOBE:          icon_source_index = 13; icon_source_index_alt = 13; break;
    case GATE_UI_ICON_STOCK_SETTINGS:       icon_source_index = 90; icon_source_index_alt = 21; break;
    case GATE_UI_ICON_STOCK_HOME:           icon_source_index = 150; icon_source_index_alt = 39; break;
    case GATE_UI_ICON_STOCK_IMAGE:          icon_source_index = 117; icon_source_index_alt = 34; break;
    case GATE_UI_ICON_STOCK_AUDIO:          icon_source_index = 116; icon_source_index_alt = 40; break;
    case GATE_UI_ICON_STOCK_VIDEO:          icon_source_index = 115; icon_source_index_alt = 79; break;
    case GATE_UI_ICON_STOCK_MAIL:           icon_source_index = 156; icon_source_index_alt = 91; break;

    default:                                break;
    }

    if (icon_source_index < 0)
    {
        return GATE_RESULT_NOMATCH;
    }

    hicon = gate_ui_extract_icon(icon_source_file, icon_source_index, flags);
    if ((hicon == NULL) && (icon_source_index_alt >= 0))
    {
        hicon = gate_ui_extract_icon(icon_source_file, icon_source_index_alt, flags);
    }
#endif

    if (hicon == NULL)
    {
        return GATE_RESULT_NOTAVAILABLE;
    }

    ret = gate_ui_icon_create_native(icon, host, (void*)hicon, (flags & GATE_UI_ICON_FLAG_SMALL) | GATE_UI_ICON_FLAG_STOCK);
    if (GATE_FAILED(ret))
    {
        DestroyIcon(hicon);
    }
    else
    {
        icon->resources[1] = (void*)(gate_uintptr_t)stock_id;
    }
    return ret;
}

#if defined(GATE_SYS_WIN16)

static HICON gate_ui_icon_create_from_image(gate_ui_host_t* host, gate_rasterimage_t const* image, gate_uint32_t hx, gate_uint32_t hy)
{
    /* TODO */
    return CreateIcon(NULL, 32, 32, 2, 2, NULL, NULL);
}

#else /* GATE_SYS_WIN16 */

static HICON gate_ui_icon_create_from_image(gate_ui_host_t* host, gate_rasterimage_t const* image, gate_uint32_t hx, gate_uint32_t hy)
{
    HICON ret = NULL;

    do
    {
        gate_ui_position_t imagerect;
        gate_ui_color_t maskcolor = { 255, 255, 255, 255 };
        gate_ui_point_t pos;
        gate_uint32_t icon_flags = 0;
        static COLORREF color_black = RGB(0, 0, 0);
        static COLORREF color_white = RGB(255, 255, 255);
        COLORREF color_bg = GetSysColor(COLOR_3DFACE);

        ICONINFO iconinfo;

        HBITMAP hbmptmp1 = NULL;
        HBITMAP hbmptmp2 = NULL;
        HBITMAP bmpmask = NULL;
        HBITMAP bmpimage = NULL;
        HDC desk = NULL;
        HDC hmask = NULL;
        HDC himage = NULL;
        gate_color_t const* ptrpixels;

        iconinfo.fIcon = TRUE;
        iconinfo.xHotspot = hx;
        iconinfo.yHotspot = hy;
        iconinfo.hbmMask = NULL;
        iconinfo.hbmColor = NULL;

        imagerect.pos.x = 0;
        imagerect.pos.y = 0;
        imagerect.size.width = image->width;
        imagerect.size.height = image->height;

        desk = GetDC(NULL);
        hmask = CreateCompatibleDC(desk);
        himage = CreateCompatibleDC(desk);

        bmpmask = CreateBitmap(image->width, image->height, 1, 1, NULL);
        bmpimage = CreateCompatibleBitmap(desk, image->width, image->height);

        hbmptmp1 = (HBITMAP)SelectObject(hmask, bmpmask);
        hbmptmp2 = (HBITMAP)SelectObject(himage, bmpimage);

        ptrpixels = (gate_color_t const*)gate_rasterimage_get_line_ptr(image, 0);
        for (pos.y = 0; pos.y != image->height; ++pos.y)
        {
            for (pos.x = 0; pos.x != image->width; ++pos.x)
            {
                SetPixel(hmask, pos.x, pos.y, (ptrpixels->a > 127) ? color_black : color_white);
                SetPixel(himage, pos.x, pos.y, (ptrpixels->a > 127) ? RGB(ptrpixels->r, ptrpixels->g, ptrpixels->b) : color_black);
                ++ptrpixels;
            }
        }
        SelectObject(hmask, hbmptmp1);
        SelectObject(himage, hbmptmp2);
        DeleteDC(hmask);
        DeleteDC(himage);
        ReleaseDC(NULL, desk);

        if (image->height < 24)
        {
            icon_flags |= GATE_UI_ICON_FLAG_SMALL;
        }
        if (image->height > 40)
        {
            icon_flags |= GATE_UI_ICON_FLAG_LARGE;
        }

        iconinfo.hbmMask = bmpmask;
        iconinfo.hbmColor = bmpimage;

        ret = CreateIconIndirect(&iconinfo);
        GATE_DEBUG_ASSERT(ret != NULL);

        DeleteObject(bmpmask);
        DeleteObject(bmpimage);
    } while (0);

    return ret;
}

#endif /* GATE_SYS_WIN16 */

gate_result_t gate_ui_icon_create_image(gate_ui_icon_t* icon, gate_ui_host_t* host, gate_rasterimage_t const* image)
{
    gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
    HICON hicon = gate_ui_icon_create_from_image(host, image, 0, 0);
    if (hicon == NULL)
    {
        ret = GATE_RESULT_FAILED;
    }
    else
    {
        icon->resources[0] = hicon;
        icon->resources[1] = NULL;
        icon->host = host;
        if (image->height < 28)
        {
            icon->flags = GATE_UI_ICON_FLAG_SMALL;
        }
        else if (image->height > 36)
        {
            icon->flags = GATE_UI_ICON_FLAG_LARGE;
        }

        ret = GATE_RESULT_OK;
    }
    return ret;
}
gate_result_t gate_ui_icon_create_file(gate_ui_icon_t* icon, gate_ui_host_t* host, gate_string_t const* filepath)
{
    gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED;
    /* TODO */
    return ret;
}
gate_result_t gate_ui_icon_copy(gate_ui_icon_t* dsticon, gate_ui_icon_t const* srcicon)
{
    gate_result_t ret = GATE_RESULT_OK;
    dsticon->host = srcicon->host;

#if !defined(GATE_SYS_WINCE)
    if (!GATE_FLAG_ENABLED(srcicon->flags, GATE_UI_ICON_FLAG_STOCK))
    {
        /* managed icon */
        dsticon->flags = srcicon->flags;
#if defined(GATE_SYS_WIN16)
        dsticon->resources[0] = (void*)CopyIcon(NULL, (HICON)srcicon->resources[0]);
#else
        dsticon->resources[0] = (void*)CopyIcon((HICON)srcicon->resources[0]);
#endif
        if (dsticon->resources[0] == NULL)
        {
            ret = GATE_RESULT_FAILED;
        }
        dsticon->resources[1] = NULL;
    }
    else
#endif
    {
        dsticon->resources[0] = srcicon->resources[0];
        dsticon->resources[1] = srcicon->resources[1];
    }
    return ret;
}

gate_result_t gate_ui_icon_destroy(gate_ui_icon_t* icon)
{
    gate_result_t ret = GATE_RESULT_OK;
    if (!GATE_FLAG_ENABLED(icon->flags, GATE_UI_ICON_FLAG_STOCK))
    {
        /* managed icon */
        if (!DestroyIcon((HICON)icon->resources[0]))
        {
            ret = GATE_RESULT_FAILED;
        }
    }
    return ret;
}



/******************
 * iconlist WIN32 *
 ******************/
#if !defined(ILC_COLOR16) && defined(ILC_COLOR)
#   define ILC_COLOR16 ILC_COLOR
#endif

gate_result_t gate_ui_iconlist_create(gate_ui_iconlist_t* ilst, gate_ui_host_t* host, gate_int32_t width, gate_int32_t height)
{
#if defined(GATE_SYS_WIN16)
    return GATE_RESULT_NOTIMPLEMENTED;
#else
    int const x = (int)width;
    int const y = (int)height;
    static UINT const flags = ILC_COLOR16 | ILC_MASK;<--- 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.
    HIMAGELIST hlst = (HIMAGELIST)gate_ui_winapi_imagelist_create(x, y, flags, 1, 1);
    if (!hlst)
    {
        return GATE_RESULT_FAILED;
    }
    gate_ui_winapi_imagelist_set_bkcolor(hlst, NULL);
    gate_mem_clear(ilst, sizeof(gate_ui_iconlist_t));
    ilst->host = host;
    ilst->flags = 0;
    ilst->width = width;
    ilst->height = height;
    ilst->resources[0] = hlst;
    return GATE_RESULT_OK;
#endif
}

gate_result_t gate_ui_iconlist_destroy(gate_ui_iconlist_t* ilst)
{
#if defined(GATE_SYS_WIN16)
    return GATE_RESULT_NOTIMPLEMENTED;
#else
    if (ilst->resources[0] != NULL)
    {
        gate_ui_winapi_imagelist_destroy((HIMAGELIST)ilst->resources[0]);
    }
    gate_mem_clear(ilst, sizeof(gate_ui_iconlist_t));
    return GATE_RESULT_OK;
#endif
}
gate_size_t gate_ui_iconlist_count(gate_ui_iconlist_t* ilst)
{
    return 0;
}
gate_result_t gate_ui_iconlist_add_icon(gate_ui_iconlist_t* ilst, gate_ui_icon_t const* icon, gate_intptr_t* icon_key)
{
#if defined(GATE_SYS_WIN16)
    return GATE_RESULT_NOTIMPLEMENTED;
#else
    HIMAGELIST hlst = (HIMAGELIST)ilst->resources[0];
    int index;

    if (!hlst)
    {
        return GATE_RESULT_INVALIDSTATE;
    }

    index = gate_ui_winapi_imagelist_replace_icon(hlst, -1, (HICON)icon->resources[0]);
    if (index < 0)
    {
        return GATE_RESULT_FAILED;
    }
    else
    {
        if (icon_key)
        {
            *icon_key = index;
        }
        return GATE_RESULT_OK;
    }
#endif
}

gate_result_t gate_ui_iconlist_add_image(gate_ui_iconlist_t* ilst, gate_rasterimage_t const* image, gate_intptr_t* icon_key)
{
#if defined(GATE_SYS_WIN16)
    return GATE_RESULT_NOTIMPLEMENTED;
#else
    gate_ui_icon_t icon = GATE_INIT_EMPTY;
    gate_result_t ret = gate_ui_icon_create_image(&icon, ilst->host, image);
    if (GATE_SUCCEEDED(ret))
    {
        ret = gate_ui_iconlist_add_icon(ilst, &icon, icon_key);
        gate_ui_icon_destroy(&icon);
    }
    return ret;
#endif
}

gate_result_t gate_ui_iconlist_get_icon(gate_ui_iconlist_t* ilst, gate_intptr_t icon_key, gate_ui_icon_t* icon)
{
#if defined(GATE_SYS_WIN16)
    return GATE_RESULT_NOTIMPLEMENTED;
#else
    UINT flags = ILD_TRANSPARENT;
    gate_uint32_t icon_flags;
    HICON hicon = (HICON)gate_ui_winapi_imagelist_get_icon((HIMAGELIST)ilst->resources[0], (int)icon_key, flags);
    if (!hicon)
    {
        return GATE_RESULT_FAILED;
    }
    else
    {
        icon_flags = GATE_UI_ICON_FLAG_STOCK;
        if (ilst->height < 24)
        {
            icon_flags |= GATE_UI_ICON_FLAG_SMALL;
        }
        if (ilst->height > 40)
        {
            icon_flags |= GATE_UI_ICON_FLAG_LARGE;
        }
        return gate_ui_icon_create_native(icon, ilst->host, hicon, icon_flags);
    }
#endif
}

void* gate_ui_iconlist_get_handle(gate_ui_iconlist_t* ilst, gate_intptr_t icon_key)
{
#if defined(GATE_SYS_WIN16)
    return NULL;
#else
    UINT flags = ILD_TRANSPARENT;
    HICON hicon = (HICON)gate_ui_winapi_imagelist_get_icon((HIMAGELIST)ilst->resources[0], (int)icon_key, flags);
    return (void*)hicon;
#endif
}





/****************
 * cursor WIN32 *
 ****************/

#ifndef IDC_APPSTARTING
#define IDC_APPSTARTING IDC_WAIT
#endif

#ifndef IDC_NO
#define IDC_NO IDC_ICON
#endif

gate_result_t gate_ui_cursor_create_stock(gate_ui_cursor_t* cursor, gate_ui_host_t* host, gate_enumint_t stock_id)
{
    LPCTSTR resourceId = NULL;
    HCURSOR hcursor = NULL;

    GATE_DEBUG_ASSERT(cursor != NULL);
    GATE_DEBUG_ASSERT(host != NULL);

    gate_mem_clear(cursor, sizeof(gate_ui_cursor_t));

    switch (stock_id)
    {
    case GATE_UI_CURSOR_STOCK_POINTER:              resourceId = IDC_ARROW; break;
    case GATE_UI_CURSOR_STOCK_BUSY:                 resourceId = IDC_WAIT; break;
    case GATE_UI_CURSOR_STOCK_STARTING:             resourceId = IDC_APPSTARTING; break;
    case GATE_UI_CURSOR_STOCK_HAND:                 resourceId = /*IDC_HAND*/ IDC_UPARROW; break;
    case GATE_UI_CURSOR_STOCK_TEXT:                 resourceId = IDC_IBEAM; break;
    case GATE_UI_CURSOR_STOCK_REJECTED:             resourceId = IDC_NO; break;
    case GATE_UI_CURSOR_STOCK_SIZE_ALL:             resourceId = IDC_SIZE; break;
    case GATE_UI_CURSOR_STOCK_SIZE_LEFTRIGHT:       resourceId = IDC_SIZEWE; break;
    case GATE_UI_CURSOR_STOCK_SIZE_UPDOWN:          resourceId = IDC_SIZENS; break;
    case GATE_UI_CURSOR_STOCK_SIZE_LEFTUPRIGHTDOWN: resourceId = IDC_SIZENWSE; break;
    case GATE_UI_CURSOR_STOCK_SIZE_RIGHTUPKEFTDOWN: resourceId = IDC_SIZENESW; break;
    default:    return GATE_RESULT_INVALIDARG;
    }

    hcursor = LoadCursor(NULL, resourceId);
    if (NULL == hcursor)
    {
        return GATE_RESULT_FAILED;
    }
    cursor->host = host;
    cursor->resources[0] = (void*)hcursor;
    cursor->resources[1] = NULL;
    cursor->flags = GATE_UI_CURSOR_FLAG_STOCK;
    return GATE_RESULT_OK;
}
gate_result_t gate_ui_cursor_create_native(gate_ui_cursor_t* cursor, gate_ui_host_t* host, void* handle, gate_enumint_t flags)
{
    cursor->host = host;
    cursor->resources[0] = handle;
    cursor->resources[1] = NULL;
    cursor->flags = flags;
    return GATE_RESULT_OK;
}
gate_result_t gate_ui_cursor_create_image(gate_ui_cursor_t* cursor, gate_ui_host_t* host, gate_rasterimage_t const* image, gate_uint16_t x, gate_uint16_t y)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HCURSOR hcursor = (HCURSOR)gate_ui_icon_create_from_image(host, image, x, y);
    if (hcursor)
    {
        cursor->resources[0] = (void*)hcursor;
        cursor->resources[1] = NULL;
        cursor->host = host;
        cursor->flags = 0;
        ret = GATE_RESULT_OK;
    }
    return ret;
}
gate_result_t gate_ui_cursor_create_file(gate_ui_cursor_t* cursor, gate_ui_host_t* host, gate_string_t const* filepath)
{
    return GATE_RESULT_NOTIMPLEMENTED;
}

gate_result_t gate_ui_cursor_copy(gate_ui_cursor_t* dsticon, gate_ui_cursor_t const* srcicon)
{
    gate_result_t ret = GATE_RESULT_FAILED;
#if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WIN16)
    if (!GATE_FLAG_ENABLED(srcicon->flags, GATE_UI_CURSOR_FLAG_STOCK))
    {
        dsticon->resources[0] = (void*)CopyImage((HANDLE)srcicon->resources[0], IMAGE_CURSOR, 0, 0, 0);
        if (dsticon->resources[0] != NULL)
        {
            dsticon->resources[1] = NULL;
            dsticon->host = srcicon->host;
            dsticon->flags = 0;
            ret = GATE_RESULT_OK;
        }
    }
    else
#endif /* ! GATE_SYS_WINCE */
    {
        gate_mem_copy(dsticon, srcicon, sizeof(gate_ui_cursor_t));
        ret = GATE_RESULT_OK;
    }
    return ret;
}
gate_result_t gate_ui_cursor_destroy(gate_ui_cursor_t* cursor)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    if (cursor)
    {
        ret = GATE_RESULT_OK;
        if (!GATE_FLAG_ENABLED(cursor->flags, GATE_UI_CURSOR_FLAG_STOCK))
        {
#if !defined(GATE_SYS_WINCE)
            if (!DestroyCursor((HCURSOR)cursor->resources[0]))
            {
                ret = GATE_RESULT_FAILED;
            }
#endif
        }
    }
    return ret;
}

#endif  /* GATE_UI_WINAPI */