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
/* 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/spinners.h"
#include "gate/results.h"
#include "gate/debugging.h"


#if defined(GATE_UI_WINAPI)

#if !defined(_WIN32_IE)
#	define _WIN32_IE 0x0501
#endif

#include "gate/ui/gateui_winapi.h"
#include "gate/platforms.h"

#if !defined(GATE_SYS_WIN16)
#include <commctrl.h>
#else
#	define UPDOWN_CLASS	"msctls_updown32"

#	define UDN_FIRST               (0U-721U)        // updown
#	define UDN_LAST                (0U-729U)
#	define UDN_DELTAPOS            (UDN_FIRST - 1)

#	define UDM_SETRANGE            (WM_USER+101)
#	define UDM_GETRANGE            (WM_USER+102)
#	define UDM_SETPOS              (WM_USER+103)
#	define UDM_GETPOS              (WM_USER+104)
#	define UDM_SETBUDDY            (WM_USER+105)
#	define UDM_GETBUDDY            (WM_USER+106)
#	define UDM_SETACCEL            (WM_USER+107)
#	define UDM_GETACCEL            (WM_USER+108)
#	define UDM_SETBASE             (WM_USER+109)
#	define UDM_GETBASE             (WM_USER+110)
#	define UDM_SETRANGE32          (WM_USER+111)
#	define UDM_GETRANGE32          (WM_USER+112) // wParam & lParam are LPINT
#	define UDM_SETPOS32            (WM_USER+113)
#	define UDM_GETPOS32            (WM_USER+114)

#	define UDS_WRAP                0x0001
#	define UDS_SETBUDDYINT         0x0002
#	define UDS_ALIGNRIGHT          0x0004
#	define UDS_ALIGNLEFT           0x0008
#	define UDS_AUTOBUDDY           0x0010
#	define UDS_ARROWKEYS           0x0020
#	define UDS_HORZ                0x0040
#	define UDS_NOTHOUSANDS         0x0080
#	define UDS_HOTTRACK            0x0100
#endif

static HWND gate_ui_spinbox_get_text(HWND container)
{
    return GetWindow(container, GW_CHILD);
}
static HWND gate_ui_spinbox_get_updown(HWND container)
{
    HWND hwnd_text = gate_ui_spinbox_get_text(container);
    return GetWindow(hwnd_text, GW_HWNDNEXT);
}

gate_result_t gate_ui_spinner_get_state(gate_ui_ctrl_t* ctrl, gate_int32_t* state)
{
    gate_int64_t tmp = 0;
    gate_result_t result = gate_ui_spinbox_get_value((gate_ui_spinbox_t*)ctrl, &tmp);
    if (state) *state = (gate_int32_t)tmp;
    return result;
}

gate_result_t gate_ui_spinner_set_state(gate_ui_ctrl_t* ctrl, gate_int32_t state)
{
    return gate_ui_spinbox_set_value((gate_ui_spinbox_t*)ctrl, (gate_int64_t)state);
}

static gate_result_t gate_ui_spinner_refresh(gate_ui_ctrl_t* ctrl)
{
    gate_result_t ret;
    HWND hwnd_ctrl = GATE_UI_WINAPI_GET_HWND(ctrl);
    HWND hwnd_text = gate_ui_spinbox_get_text(hwnd_ctrl);
    HWND hwnd_updown = gate_ui_spinbox_get_updown(hwnd_ctrl);

#if defined(GATE_UI_WINAPI_DARKMODE_SUPPORT)
    BOOL dark_mode;
    if (gate_ui_winapi_darkmode_supported())
    {
        dark_mode = gate_ui_winapi_darkmode_enabled() ? TRUE : FALSE;
        gate_ui_winapi_apply_darkmode(hwnd_text, dark_mode);
        gate_ui_winapi_apply_darkmode(hwnd_updown, dark_mode);
    }
#endif /* GATE_UI_WINAPI_DARKMODE_SUPPORT */

    ret = gate_ui_winapi_refresh(ctrl);
    gate_ui_winapi_refresh_hwnd(hwnd_text);
    gate_ui_winapi_refresh_hwnd(hwnd_updown);

    return ret;
}

static gate_ui_winapi_dispatchers_t global_win32_spinner_dispatchers =
{
    &gate_ui_winapi_destroy_with_font,
    &gate_ui_winapi_is_enabled,
    &gate_ui_winapi_is_visible,
    &gate_ui_winapi_is_focused,
    &gate_ui_winapi_get_position,
    &gate_ui_winapi_get_size,
    &gate_ui_winapi_get_children,
    &gate_ui_winapi_get_text_length,
    &gate_ui_winapi_get_text,
    &gate_ui_spinner_get_state,
    &gate_ui_winapi_set_enabled,
    &gate_ui_winapi_set_visible,
    &gate_ui_winapi_set_focus,
    &gate_ui_winapi_set_position,
    &gate_ui_winapi_set_text,
    &gate_ui_spinner_set_state,
    &gate_ui_spinner_refresh
};


static gate_bool_t gate_ui_spinbox_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 hwnd_ctrl;
    HWND hwnd_text;
    HWND hwnd_updown;
    RECT rect;
    gate_ui_spinbox_t* spinbox;
    int updown_width = 24;
    LRESULT pos;

    if ((ctrl != NULL) && (hwnd != NULL))
    {
        hwnd_ctrl = GATE_UI_WINAPI_GET_HWND(ctrl);
        if (hwnd_ctrl)
        {
            spinbox = (gate_ui_spinbox_t*)ctrl;
            switch (msg)
            {
#if !defined(GATE_SYS_WIN16)
            case WM_CTLCOLOREDIT:
            case WM_CTLCOLORBTN:
            case WM_CTLCOLORLISTBOX:
            case WM_CTLCOLORMSGBOX:
            case WM_CTLCOLORSCROLLBAR:
            case WM_CTLCOLORDLG:
            case WM_CTLCOLORSTATIC:
            {
                *lresult = SendMessage(GetParent((HWND)hwnd_ctrl), msg, wParam, lParam);
                return true;
                break;
            }
#endif /* GATE_SYS_WIN16 */
            case UDN_DELTAPOS:
            {
#if defined(UDM_GETPOS32)
                pos = SendMessage(hwnd_ctrl, UDM_GETPOS32, 0, 0);
#else
                pos = (LRESULT)LOWORD(SendMessage(hwnd, UDM_GETPOS, 0, 0));
#endif
                if (spinbox->on_change)
                {
                    spinbox->on_change(ctrl, (gate_int64_t)pos);
                }
                *lresult = 0;
                return true;
            }
            case WM_SIZE:
            {
                hwnd_text = gate_ui_spinbox_get_text(hwnd_ctrl);
                hwnd_updown = gate_ui_spinbox_get_updown(hwnd_ctrl);
                GetClientRect(hwnd_ctrl, &rect);
                SetWindowPos(hwnd_text, NULL, 0, 0, rect.right - updown_width, rect.bottom, SWP_NOZORDER);
                SetWindowPos(hwnd_updown, NULL, rect.right - updown_width, 0, updown_width, rect.bottom, SWP_NOZORDER);
                *lresult = 0;
                return true;
            }
            case WM_ENABLE:
            {
                hwnd_text = gate_ui_spinbox_get_text(hwnd_ctrl);
                hwnd_updown = gate_ui_spinbox_get_updown(hwnd_ctrl);
                EnableWindow(hwnd_text, (BOOL)wParam);
                EnableWindow(hwnd_updown, (BOOL)wParam);
                return false;
            }
            default:
            {
                break;
            }
            }
        }
    }
    return false;
}

gate_result_t gate_ui_spinbox_create(
    gate_ui_spinbox_t* spinbox, gate_ui_ctrl_t* parent,
    gate_ui_position_t const* position,
    gate_uint32_t flags,
    void* userparam
)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_uint32_t styles = 0, exstyles = 0;
    gate_ui_host_t* host = NULL;
    HWND hwnd_parent;
    HWND hwnd_container;
    HWND hwnd_text;
    HWND hwnd_updown;
    HINSTANCE hinst;
    HFONT hfont;

    do
    {
        host = gate_ui_ctrl_get_host(parent);
        if (host == NULL)
        {
            ret = GATE_RESULT_INVALIDSTATE;
            break;
        }
        hwnd_parent = GATE_UI_WINAPI_GET_HWND(parent);
        if (hwnd_parent == NULL)
        {
            ret = GATE_RESULT_INVALIDSTATE;
            break;
        }

        gate_mem_clear(spinbox, sizeof(gate_ui_spinbox_t));

        {
            /* create a container windows which contains the text and up-down elements */

            exstyles = 0;
#if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WIN16)
            exstyles |= WS_EX_CONTROLPARENT;
#endif
            styles = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_TABSTOP | WS_VISIBLE;
            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(&spinbox->ctrl, host, hwnd_parent, NULL, position, styles, exstyles, NULL, userparam, false);

            if (GATE_FAILED(ret))
            {
                break;
            }
            hwnd_container = GATE_UI_WINAPI_GET_HWND(&spinbox->ctrl);
            hinst = (HINSTANCE)GATE_UI_WINAPI_GET_HOST_APPHANDLE(host);
        }

        {
            /* create the text-element as child window */
            styles = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_TABSTOP | WS_VISIBLE | ES_AUTOHSCROLL;
#if !defined(GATE_SYS_WIN16)
            styles |= ES_NUMBER;
            exstyles = WS_EX_CLIENTEDGE;
#endif
            hwnd_text = CreateWindowEx(exstyles, _T("EDIT"), NULL, styles, 0, 0, 10, 10, hwnd_container, NULL, hinst, NULL);
            if (NULL == hwnd_text)
            {
                gate_ui_ctrl_destroy(&spinbox->ctrl);
                ret = GATE_RESULT_OUTOFRESOURCES;
                break;
            }
            hfont = (HFONT)GATE_UI_WINAPI_GET_HOST_FONT(host);
            if (hfont != NULL)
            {
                SendMessage(hwnd_text, WM_SETFONT, (WPARAM)(gate_uintptr_t)hfont, TRUE);
            }
#if defined(GATE_UI_WINAPI_DARKMODE_SUPPORT)
            if (gate_ui_winapi_darkmode_enabled())
            {
                gate_ui_winapi_apply_darkmode(hwnd_text, TRUE);
            }
#endif
        }

        {
            /* create up-down control as child window */
            exstyles = 0;
            styles = WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_TABSTOP | WS_VISIBLE;
            styles |= (UDS_ARROWKEYS | UDS_SETBUDDYINT | UDS_NOTHOUSANDS);

            hwnd_updown = CreateWindowEx(exstyles, UPDOWN_CLASS, NULL, styles, 0, 0, 10, 10, hwnd_container, NULL, hinst, NULL);
            if (NULL == hwnd_text)
            {
                DestroyWindow(hwnd_text);
                gate_ui_ctrl_destroy(&spinbox->ctrl);
                ret = GATE_RESULT_OUTOFRESOURCES;
                break;
            }

#if defined(GATE_UI_WINAPI_DARKMODE_SUPPORT)
            if (gate_ui_winapi_darkmode_enabled())
            {
                gate_ui_winapi_apply_darkmode(hwnd_updown, TRUE);
            }
#endif

            SendMessage(hwnd_updown, UDM_SETBUDDY, (WPARAM)hwnd_text, 0);
            SendMessage(hwnd_updown, UDM_SETRANGE32, (WPARAM)LONG_MIN, (LPARAM)LONG_MAX);
            SendMessage(hwnd_updown, UDM_SETPOS, 0, MAKELONG(0, 0));
        }

        GATE_UI_WINAPI_SET_DISPATCHER(&spinbox->ctrl, &global_win32_spinner_dispatchers);

        gate_ui_winapi_register_event(host, hwnd_container, WM_SIZE, &gate_ui_spinbox_events, &spinbox->ctrl);
        gate_ui_winapi_register_event(host, hwnd_container, WM_ENABLE, &gate_ui_spinbox_events, &spinbox->ctrl);
        gate_ui_winapi_register_event(host, hwnd_container, UDN_DELTAPOS, &gate_ui_spinbox_events, &spinbox->ctrl);
#if !defined(GATE_SYS_WIN16)
        gate_ui_winapi_register_event(host, hwnd_container, WM_CTLCOLORDLG, &gate_ui_spinbox_events, &spinbox->ctrl);
        gate_ui_winapi_register_event(host, hwnd_container, WM_CTLCOLORSTATIC, &gate_ui_spinbox_events, &spinbox->ctrl);
        gate_ui_winapi_register_event(host, hwnd_container, WM_CTLCOLOREDIT, &gate_ui_spinbox_events, &spinbox->ctrl);
        gate_ui_winapi_register_event(host, hwnd_container, WM_CTLCOLORBTN, &gate_ui_spinbox_events, &spinbox->ctrl);
        gate_ui_winapi_register_event(host, hwnd_container, WM_CTLCOLORLISTBOX, &gate_ui_spinbox_events, &spinbox->ctrl);
        gate_ui_winapi_register_event(host, hwnd_container, WM_CTLCOLORMSGBOX, &gate_ui_spinbox_events, &spinbox->ctrl);
        gate_ui_winapi_register_event(host, hwnd_container, WM_CTLCOLORSCROLLBAR, &gate_ui_spinbox_events, &spinbox->ctrl);
#endif

        ret = GATE_RESULT_OK;

    } while (0);

    return ret;
}

gate_result_t gate_ui_spinbox_set_limits(gate_ui_spinbox_t* spinbox, gate_int64_t const* minimum, gate_int64_t const* maximum)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HWND hwnd_container = GATE_UI_WINAPI_GET_HWND(&spinbox->ctrl);
    HWND hwnd_updown;
    INT lower_limit = 0;
    INT upper_limit = 0;
    LRESULT result;
    if (hwnd_container)
    {
        hwnd_updown = gate_ui_spinbox_get_updown(hwnd_container);
        if (hwnd_updown)
        {
#if defined(UDM_SETRANGE32) && defined(UDM_GETRANGE32)
            result = SendMessage(hwnd_updown, UDM_GETRANGE32, (WPARAM)&lower_limit, (LPARAM)&upper_limit);<--- Variable 'result' is reassigned a value before the old one has been used.
            if (minimum)
            {
                lower_limit = (INT)*minimum;
            }
            if (maximum)
            {
                upper_limit = (INT)*maximum;
            }
            result = SendMessage(hwnd_updown, UDM_SETRANGE32, (WPARAM)lower_limit, (LPARAM)upper_limit);<--- Variable 'result' is reassigned a value before the old one has been used.
#else
            result = SendMessage(hwnd_updown, UDM_GETRANGE, 0, 0);
            lower_limit = (INT)(short)HIWORD(result);
            upper_limit = (INT)(short)LOWORD(result);
            if (minimum)
            {
                lower_limit = (INT)*minimum;
            }
            if (maximum)
            {
                upper_limit = (INT)*maximum;
            }
            result = SendMessage(hwnd_updown, UDM_SETRANGE, 0, MAKELPARAM(upper_limit, lower_limit));
#endif
            ret = GATE_RESULT_OK;
        }
    }
    return ret;

}
gate_result_t gate_ui_spinbox_get_limits(gate_ui_spinbox_t* spinbox, gate_int64_t* minimum, gate_int64_t* maximum)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HWND hwnd_container = GATE_UI_WINAPI_GET_HWND(&spinbox->ctrl);
    HWND hwnd_updown;
    INT lower_limit = 0;
    INT upper_limit = 0;
    LRESULT result;
    if (hwnd_container)
    {
        hwnd_updown = gate_ui_spinbox_get_updown(hwnd_container);
        if (hwnd_updown)
        {
#if defined(UDM_GETRANGE32)
            result = SendMessage(hwnd_updown, UDM_GETRANGE32, (WPARAM)&lower_limit, (LPARAM)&upper_limit);
#else
            result = SendMessage(hwnd_updown, UDM_GETRANGE, 0, 0);
            lower_limit = (INT)(short)HIWORD(result);
            upper_limit = (INT)(short)LOWORD(result);
#endif
            if (minimum)
            {
                *minimum = (gate_int64_t)lower_limit;
            }
            if (maximum)
            {
                *maximum = (gate_int64_t)upper_limit;
            }
            ret = GATE_RESULT_OK;
        }
    }
    return ret;
}

gate_result_t gate_ui_spinbox_set_value(gate_ui_spinbox_t* spinbox, gate_int64_t value)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HWND hwnd_container = GATE_UI_WINAPI_GET_HWND(&spinbox->ctrl);
    HWND hwnd_updown;
    if (hwnd_container)
    {
        hwnd_updown = gate_ui_spinbox_get_updown(hwnd_container);
        if (hwnd_updown)
        {
#if defined(UDM_SETPOS32)
            SendMessage(hwnd_updown, UDM_SETPOS32, 0, (LPARAM)value);
#else
            SendMessage(hwnd_updown, UDM_SETPOS, 0, (LPARAM)value);
#endif
            ret = GATE_RESULT_OK;
        }
    }
    return ret;
}
gate_result_t gate_ui_spinbox_get_value(gate_ui_spinbox_t* spinbox, gate_int64_t* value)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    HWND hwnd_container = GATE_UI_WINAPI_GET_HWND(&spinbox->ctrl);
    HWND hwnd_updown;
    BOOL failed;
    LRESULT spinvalue = 0;
    if (hwnd_container)
    {
        hwnd_updown = gate_ui_spinbox_get_updown(hwnd_container);
        if (hwnd_updown)
        {
#if defined(UDM_GETPOS32)
            failed = FALSE;
            spinvalue = SendMessage(hwnd_updown, UDM_GETPOS32, 0, (LPARAM)&failed);
#else
            failed = FALSE;
            spinvalue = (LRESULT)(gate_int16_t)LOWORD(SendMessage(hwnd_updown, UDM_GETPOS, 0, 0));
#endif
            if (!failed)
            {
                *value = (gate_int64_t)spinvalue;
                ret = GATE_RESULT_OK;
            }
        }
    }
    return ret;
}

#endif


#if defined(GATE_UI_GTK)

#include "gate/ui/gateui_gtk.h"

static gate_uint32_t gate_ui_gtk_spinbox_get_text_length(gate_ui_ctrl_t* ctrl)
{
    gint char_count = 0;
    gchar const* ptr_text = NULL;
    GtkButton* button = GTK_BUTTON(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl));
    if (button)
    {
        ptr_text = gtk_button_get_label(button);
        if (ptr_text)
        {
            char_count = gate_str_length(ptr_text);
        }
    }
    return (gate_uint32_t)char_count;
}
static gate_result_t gate_ui_gtk_spinbox_get_text(gate_ui_ctrl_t* ctrl, gate_string_t* text)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gchar const* ptr_text = NULL;
    GtkButton* button = GTK_BUTTON(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl));
    if (button)
    {
        ptr_text = gtk_button_get_label(button);
        if (ptr_text)
        {
            if (NULL == gate_string_create(text, ptr_text, gate_str_length(ptr_text)))
            {
                ret = GATE_RESULT_OUTOFMEMORY;
            }
            else
            {
                ret = GATE_RESULT_OK;
            }
        }
        else
        {
            gate_string_create_empty(text);
            ret = GATE_RESULT_OK;
        }
    }
    return ret;

}
static gate_result_t gate_ui_gtk_spinbox_set_text(gate_ui_ctrl_t* ctrl, gate_string_t const* text)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gchar label[4096];
    GtkButton* button = GTK_BUTTON(GATE_UI_GTK_GET_CTRL_WIDGET(ctrl));
    if (button)
    {
        gate_str_print_text(label, sizeof(label), text->str, text->length);
        gtk_button_set_label(button, label);
        ret = GATE_RESULT_OK;
    }
    return ret;
}
static gate_result_t gate_ui_gtk_spinbox_get_state(gate_ui_ctrl_t* ctrl, gate_int32_t* value)
{
    return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t gate_ui_gtk_spinbox_set_state(gate_ui_ctrl_t* ctrl, gate_int32_t value)
{
    return GATE_RESULT_NOTIMPLEMENTED;
}

static gate_ui_gtk_dispatcher_t gate_ui_gtk_spinbox_dispatcher =
{
    &gate_ui_gtk_spinbox_get_text_length,
    &gate_ui_gtk_spinbox_get_text,
    &gate_ui_gtk_spinbox_set_text,
    &gate_ui_gtk_spinbox_get_state,
    &gate_ui_gtk_spinbox_set_state,
    NULL
};

static void gate_ui_spinbox_changed_cb(GtkSpinButton* spin_button, GtkScrollType  scroll, gpointer user_data)
{
    gate_ui_spinbox_t* spinbox_ctrl = (gate_ui_spinbox_t*)user_data;
    gate_int64_t value = 0;
    if (spinbox_ctrl)
    {
        if (spinbox_ctrl->on_change != NULL)
        {
            gate_ui_spinbox_get_value(spinbox_ctrl, &value);
            spinbox_ctrl->on_change(&spinbox_ctrl->ctrl, value);
        }
    }
}


gate_result_t gate_ui_spinbox_create(gate_ui_spinbox_t* spinbox, gate_ui_ctrl_t* parent,
    gate_ui_position_t const* position, gate_uint32_t flags, void* userparam)
{
    gate_result_t ret = GATE_RESULT_OK;
    GtkWidget* widget;
    gate_ui_host_t* host = GATE_UI_GTK_GET_CTRL_HOST(parent);

    gate_mem_clear(spinbox, sizeof(gate_ui_spinbox_t));
    widget = gtk_spin_button_new(NULL, 1.0, 0);
    if (widget == NULL)
    {
        return GATE_RESULT_FAILED;
    }

    ret = gate_ui_gtk_ctrl_init(&spinbox->ctrl, widget, host, userparam, parent,
        &gate_ui_gtk_spinbox_dispatcher, false, false, position, &flags);

    if (GATE_SUCCEEDED(ret))
    {
        gtk_spin_button_set_range(GTK_SPIN_BUTTON(widget), (gdouble)LONG_MIN, (gdouble)LONG_MAX);
        gtk_spin_button_set_increments(GTK_SPIN_BUTTON(widget), 1.0, 100.0);
        g_signal_connect(G_OBJECT(widget), "change-value", G_CALLBACK(gate_ui_spinbox_changed_cb), (gpointer)spinbox);
    }

    return ret;
}

gate_result_t gate_ui_spinbox_set_limits(gate_ui_spinbox_t* spinbox, gate_int64_t const* minimum, gate_int64_t const* maximum)
{
    gate_result_t ret = GATE_RESULT_OK;
    GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&spinbox->ctrl);
    GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(widget);
    gdouble min_value = 0.0;
    gdouble max_value = 0.0;
    gtk_spin_button_get_range(spinbutton, &min_value, &max_value);
    if (minimum)
    {
        min_value = (gdouble)*minimum;
    }
    if (maximum)
    {
        max_value = (gdouble)*maximum;
    }
    gtk_spin_button_set_range(spinbutton, min_value, max_value);
    return ret;
}

gate_result_t gate_ui_spinbox_get_limits(gate_ui_spinbox_t* spinbox, gate_int64_t* minimum, gate_int64_t* maximum)
{
    gate_result_t ret = GATE_RESULT_OK;
    GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&spinbox->ctrl);
    GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(widget);
    gdouble min_value = 0.0;
    gdouble max_value = 0.0;
    gtk_spin_button_get_range(spinbutton, &min_value, &max_value);
    if (minimum)
    {
        *minimum = (gate_int64_t)min_value;
    }
    if (maximum)
    {
        *maximum = (gate_int64_t)max_value;
    }
    return ret;
}

gate_result_t gate_ui_spinbox_set_value(gate_ui_spinbox_t* spinbox, gate_int64_t value)
{
    gate_result_t ret = GATE_RESULT_OK;
    GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&spinbox->ctrl);
    GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(widget);
    gtk_spin_button_set_value(spinbutton, (gdouble)value);
    return ret;
}

gate_result_t gate_ui_spinbox_get_value(gate_ui_spinbox_t* spinbox, gate_int64_t* value)
{
    gate_result_t ret = GATE_RESULT_OK;
    GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&spinbox->ctrl);
    GtkSpinButton* spinbutton = GTK_SPIN_BUTTON(widget);
    gdouble native_value = gtk_spin_button_get_value(spinbutton);
    if (value)
    {
        *value = (gate_int64_t)native_value;
    }
    return ret;
}

#endif /* GATE_UI_GTK */



#if defined(GATE_UI_MOTIF)

#include "gate/ui/gateui_motif.h"
#include <Xm/SpinB.h>
#include <Xm/SSpinB.h>


static void spinbox_value_changed(Widget widget, XtPointer client_data, XtPointer call_data)
{
    gate_ui_spinbox_t* ptr_spinbox = (gate_ui_spinbox_t*)client_data;
    if (!ptr_spinbox || !ptr_spinbox->on_change)
    {
        return;
    }
    ptr_spinbox->on_change(&ptr_spinbox->ctrl, 0);

}

gate_result_t gate_ui_spinbox_create(gate_ui_spinbox_t* spinbox, gate_ui_ctrl_t* parent, gate_ui_position_t const* position,
    gate_uint32_t flags, void* userparam)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    Widget w = NULL;
    Arg args[12];
    unsigned args_count = 0;

    do
    {
        if (!spinbox || !parent)
        {
            ret = GATE_RESULT_INVALIDARG;
            break;
        }

        XtSetArg(args[args_count], XmNarrowLayout, XmARROWS_SPLIT); ++args_count;
        XtSetArg(args[args_count], XmNminimumValue, 0); ++args_count;
        XtSetArg(args[args_count], XmNmaximumValue, 32767); ++args_count;
        XtSetArg(args[args_count], XmNposition, 1); ++args_count;
        XtSetArg(args[args_count], XmNincrementValue, 1); ++args_count;
        XtSetArg(args[args_count], XmNpositionType, XmPOSITION_VALUE); ++args_count;
        XtSetArg(args[args_count], XmNspinBoxChildType, XmNUMERIC); ++args_count;
        XtSetArg(args[args_count], XmNshadowThickness, 0); ++args_count;
        XtSetArg(args[args_count], XmNarrowSensitivity, XmARROWS_SENSITIVE); ++args_count;

        ret = gate_ui_motif_ctrl_create(&spinbox->ctrl, xmSimpleSpinBoxWidgetClass, userparam, NULL, parent,
            position, &flags, false, NULL, args, args_count);
        GATE_BREAK_IF_FAILED(ret);

        w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&spinbox->ctrl);
        XtAddCallback(w, XmNvalueChangedCallback, &spinbox_value_changed, spinbox);
    } while (0);

    return ret;
}

gate_result_t gate_ui_spinbox_set_limits(gate_ui_spinbox_t* spinbox, gate_int64_t const* minimum, gate_int64_t const* maximum)
{
    Widget w;
    GATE_DEBUG_ASSERT(spinbox != NULL);
    w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&spinbox->ctrl);
    GATE_DEBUG_ASSERT(w != NULL);

    if (minimum)
    {
        spinbox->min_limit = *minimum;
        XtVaSetValues(w, XmNminimumValue, (int)*minimum, NULL, NULL);
    }
    if (maximum)
    {
        spinbox->max_limit = *maximum;
        XtVaSetValues(w, XmNmaximumValue, (int)*maximum, NULL, NULL);
    }
    return GATE_RESULT_OK;
}

gate_result_t gate_ui_spinbox_get_limits(gate_ui_spinbox_t* spinbox, gate_int64_t* minimum, gate_int64_t* maximum)
{
    Widget w;
    int limit;
    GATE_DEBUG_ASSERT(spinbox != NULL);
    w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&spinbox->ctrl);
    GATE_DEBUG_ASSERT(w != NULL);

    if (minimum)
    {
        XtVaGetValues(w, XmNminimumValue, &limit, NULL, NULL);
        *minimum = limit;
    }
    if (maximum)
    {
        XtVaGetValues(w, XmNmaximumValue, &limit, NULL, NULL);
        *maximum = limit;
    }
    return GATE_RESULT_OK;
}

gate_result_t gate_ui_spinbox_set_value(gate_ui_spinbox_t* spinbox, gate_int64_t value)
{
    Widget w;
    int limit;
    GATE_DEBUG_ASSERT(spinbox != NULL);
    w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&spinbox->ctrl);
    GATE_DEBUG_ASSERT(w != NULL);

    XtVaGetValues(w, XmNminimumValue, &limit, NULL, NULL);
    if (value < spinbox->min_limit)
    {
        spinbox->value = spinbox->min_limit;
    }
    else if (value > spinbox->max_limit)
    {
        spinbox->value = spinbox->max_limit;
    }
    else
    {
        spinbox->value = value;
    }
    XtVaSetValues(w, XmNposition, (int)spinbox->value, NULL, NULL);
    return GATE_RESULT_OK;
}

gate_result_t gate_ui_spinbox_get_value(gate_ui_spinbox_t* spinbox, gate_int64_t* value)
{
    Widget w;
    int pos = 0;
    GATE_DEBUG_ASSERT(spinbox != NULL);
    w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&spinbox->ctrl);
    GATE_DEBUG_ASSERT(w != NULL);

    if (value)
    {
        XtVaGetValues(w, XmNposition, &pos, NULL, NULL);
        *value = spinbox->value;
    }
    return GATE_RESULT_OK;
}


#endif /* GATE_UI_MOTIF */