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

#if defined(GATE_UI_WINAPI)

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

#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL	0x020A
#endif

#ifndef GET_WHEEL_DELTA_WPARAM
#define GET_WHEEL_DELTA_WPARAM(wParam)  ((short)HIWORD(wParam))
#endif 

#ifndef WHEEL_DELTA
#define WHEEL_DELTA                     120
#endif 

static gate_bool_t gate_ui_paintsurface_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_result_t result;
    gate_ui_paintsurface_t* surface;
    gate_ui_host_t* host;
    gate_ui_size_t sz;
    gate_ui_point_t coords;
    gate_uint32_t btn;
    gate_int32_t delta;
    RECT rect, client_rect;
    PAINTSTRUCT ps;
    gate_ui_graphics_t graph;
    gate_ui_position_t position;
    HDC hdc;
    gate_input_keycode_t kbd_key;
    gate_input_keystates_t kbd_states;

    if (ctrl != NULL)
    {
        hwndCtrl = GATE_UI_WINAPI_GET_HWND(ctrl);
        host = GATE_UI_WINAPI_GET_HOST(ctrl);
        if (hwnd == hwndCtrl)
        {
            surface = (gate_ui_paintsurface_t*)ctrl;
            switch (msg)
            {
            case WM_PAINT:
            {
                if (GetUpdateRect(hwndCtrl, &rect, FALSE))
                {
                    gate_mem_clear(&client_rect, sizeof(client_rect));
                    GetClientRect((HWND)hwnd, &client_rect);
                    hdc = BeginPaint((HWND)hwnd, &ps);
                    if (hdc)
                    {
                        if (surface->on_paint)
                        {
                            position.pos.x = rect.left;
                            position.pos.y = rect.top;
                            position.size.width = rect.right - rect.left + 1;
                            position.size.height = rect.bottom - rect.top + 1;
                            result = gate_ui_graphics_create_native(&graph, host, (void*)hdc, NULL, client_rect.right, client_rect.bottom);
                            surface->on_paint(ctrl, &graph, &position);
                        }
                        EndPaint(hwndCtrl, &ps);
                        *lresult = 0;
                        return true;
                    }
                }
                break;
            }
            case WM_SIZE:
            {
                sz.width = (int)(short)LOWORD(lParam);
                sz.height = (int)(short)HIWORD(lParam);
                if (surface->on_resize != NULL)
                {
                    surface->on_resize(ctrl, &sz);
                }
                break;
            }
            case WM_LBUTTONDOWN:
            case WM_RBUTTONDOWN:
            case WM_MBUTTONDOWN:
            {
                switch (msg)
                {
                case WM_LBUTTONDOWN:	btn = GATE_UI_MOUSE_LEFT; break;
                case WM_RBUTTONDOWN:	btn = GATE_UI_MOUSE_RIGHT; break;
                case WM_MBUTTONDOWN:	btn = GATE_UI_MOUSE_MIDDLE; break;
                }
                coords.x = GET_X_LPARAM(lParam);
                coords.y = GET_Y_LPARAM(lParam);
                if (surface->on_mouse_down != NULL)
                {
                    surface->on_mouse_down(&surface->ctrl, &coords, btn);
                }
                break;
            }
            case WM_LBUTTONUP:
            case WM_RBUTTONUP:
            case WM_MBUTTONUP:
            {
                switch (msg)
                {
                case WM_LBUTTONUP:	btn = GATE_UI_MOUSE_LEFT; break;
                case WM_RBUTTONUP:	btn = GATE_UI_MOUSE_RIGHT; break;
                case WM_MBUTTONUP:	btn = GATE_UI_MOUSE_MIDDLE; break;
                }
                coords.x = GET_X_LPARAM(lParam);
                coords.y = GET_Y_LPARAM(lParam);
                if (surface->on_mouse_up != NULL)
                {
                    surface->on_mouse_up(&surface->ctrl, &coords, btn);
                }
                break;
            }
            case WM_LBUTTONDBLCLK:
            case WM_RBUTTONDBLCLK:
            case WM_MBUTTONDBLCLK:
            {
                switch (msg)
                {
                case WM_LBUTTONDBLCLK:	btn = GATE_UI_MOUSE_LEFT; break;
                case WM_RBUTTONDBLCLK:	btn = GATE_UI_MOUSE_RIGHT; break;
                case WM_MBUTTONDBLCLK:	btn = GATE_UI_MOUSE_MIDDLE; break;
                }
                coords.x = GET_X_LPARAM(lParam);
                coords.y = GET_Y_LPARAM(lParam);
                if (surface->on_mouse_dblclick != NULL)
                {
                    surface->on_mouse_dblclick(&surface->ctrl, &coords, btn);
                }
                break;
            }
            case WM_MOUSEMOVE:
            {
                btn = 0;
                if (GATE_FLAG_ENABLED(LOWORD(wParam), MK_LBUTTON)) btn |= GATE_UI_MOUSE_LEFT;
                if (GATE_FLAG_ENABLED(LOWORD(wParam), MK_RBUTTON)) btn |= GATE_UI_MOUSE_RIGHT;
                if (GATE_FLAG_ENABLED(LOWORD(wParam), MK_MBUTTON)) btn |= GATE_UI_MOUSE_MIDDLE;
                coords.x = GET_X_LPARAM(lParam);
                coords.y = GET_Y_LPARAM(lParam);
                if (surface->on_mouse_move != NULL)
                {
                    surface->on_mouse_move(&surface->ctrl, &coords, btn);
                }
                break;
            }
            case WM_MOUSEWHEEL:
            {
                btn = 0;
                if (GATE_FLAG_ENABLED(LOWORD(wParam), MK_LBUTTON)) btn |= GATE_UI_MOUSE_LEFT;
                if (GATE_FLAG_ENABLED(LOWORD(wParam), MK_RBUTTON)) btn |= GATE_UI_MOUSE_RIGHT;
                if (GATE_FLAG_ENABLED(LOWORD(wParam), MK_MBUTTON)) btn |= GATE_UI_MOUSE_MIDDLE;
                delta = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
                coords.x = GET_X_LPARAM(lParam);
                coords.y = GET_Y_LPARAM(lParam);
                if (surface->on_mouse_scroll != NULL)
                {
                    surface->on_mouse_scroll(&surface->ctrl, &coords, btn, delta);
                }
                break;
            }
            case WM_KEYDOWN:
            {
                if (surface->on_key_down != NULL)
                {
                    kbd_states = 0;
                    kbd_key = 0;
                    gate_ui_winapi_get_keystates(&kbd_states);
                    gate_keyboard_parse_native_key((gate_input_nativekeycode_t)wParam, &kbd_key, NULL);
                    surface->on_key_down(&surface->ctrl, kbd_key, kbd_states);
                }
                break;
            }
            case WM_KEYUP:
            {
                if (surface->on_key_up != NULL)
                {
                    kbd_states = 0;
                    kbd_key = 0;
                    gate_ui_winapi_get_keystates(&kbd_states);
                    gate_keyboard_parse_native_key((gate_input_nativekeycode_t)wParam, &kbd_key, NULL);
                    surface->on_key_up(&surface->ctrl, kbd_key, kbd_states);
                }
                break;
            }
            case WM_CHAR:
            {
                if (surface->on_key_char != NULL)
                {
                    kbd_states = 0;
                    gate_ui_winapi_get_keystates(&kbd_states);
                    surface->on_key_char(&surface->ctrl, (gate_char32_t)wParam, kbd_states);
                }
                break;
            }
            }
        }
    }
    return false;
}

gate_result_t gate_ui_paintsurface_create(
    gate_ui_paintsurface_t* paintsurface, gate_ui_ctrl_t* parent,
    gate_ui_position_t const* position, gate_uint32_t flags, void* userparam)
{
    HWND hwndparent = (HWND)GATE_UI_WINAPI_GET_HWND(parent);
    gate_ui_host_t* host = GATE_UI_WINAPI_GET_HOST(parent);
    gate_result_t ret;
    gate_uint32_t styles, exstyles;
    gate_mem_clear(paintsurface, sizeof(gate_ui_paintsurface_t));

    exstyles = 0;
    styles = WS_CLIPSIBLINGS | WS_CHILD;

    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(&paintsurface->ctrl, host, hwndparent, NULL, position, styles, exstyles, NULL, userparam, false);
    if (GATE_SUCCEEDED(ret))
    {
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_SIZE, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_PAINT, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_LBUTTONDOWN, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_RBUTTONDOWN, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_MBUTTONDOWN, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_LBUTTONUP, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_RBUTTONUP, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_MBUTTONUP, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_LBUTTONDBLCLK, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_RBUTTONDBLCLK, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_MBUTTONDBLCLK, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_MOUSEMOVE, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_MOUSEWHEEL, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_KEYDOWN, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_KEYUP, &gate_ui_paintsurface_events, &paintsurface->ctrl);
        gate_ui_winapi_register_event(host, GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl), WM_CHAR, &gate_ui_paintsurface_events, &paintsurface->ctrl);
    }
    return ret;
}

gate_result_t gate_ui_paintsurface_redraw(gate_ui_paintsurface_t* paintsurface)
{
    HWND hwnd = GATE_UI_WINAPI_GET_HWND(&paintsurface->ctrl);
    if (!RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_NOERASE))
    {
        return GATE_RESULT_FAILED;
    }
    else
    {
        return GATE_RESULT_OK;
    }
}

gate_result_t gate_ui_paintsurface_set_cursor(gate_ui_paintsurface_t* paintsurface, gate_ui_cursor_t const* cursor)
{
    return GATE_RESULT_NOTIMPLEMENTED;
}



#endif	/* GATE_UI_WINAPI */


#if defined(GATE_UI_GTK)

#include "gate/ui/gateui_gtk.h"

static gate_result_t gate_ui_gtk_paintsurface_refresh(gate_ui_ctrl_t* ctrl)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    return ret;
}
static gate_result_t gate_ui_gtk_paintsurface_destroy(gate_ui_ctrl_t* ctrl)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    return ret;
}

static gboolean gate_ui_gdk_paintsurface_button_press_event(GtkWidget* widget, GdkEvent* evt, gpointer user_data)
{
    gate_ui_paintsurface_t* paintsurface = (gate_ui_paintsurface_t*)user_data;
    gate_ui_point_t point;
    gate_uint32_t btn = 0;

    if (paintsurface->on_mouse_down)
    {
        switch (evt->button.button)
        {
        case 1:		btn = GATE_UI_MOUSE_LEFT; break;
        case 2:		btn = GATE_UI_MOUSE_MIDDLE; break;
        case 3:		btn = GATE_UI_MOUSE_LEFT; break;
        case 4:		btn = GATE_UI_MOUSE_SPECIAL1; break;
        case 5:		btn = GATE_UI_MOUSE_SPECIAL2; break;
        default:	btn = GATE_UI_MOUSE_UNKNOWN; break;
        }
        point.x = (gate_int32_t)evt->button.x;
        point.y = (gate_int32_t)evt->button.y;

        paintsurface->on_mouse_down(&paintsurface->ctrl, &point, btn);
    }
    return FALSE;
}
static gboolean gate_ui_gdk_paintsurface_button_release_event(GtkWidget* widget, GdkEvent* evt, gpointer user_data)
{
    gate_ui_paintsurface_t* paintsurface = (gate_ui_paintsurface_t*)user_data;
    gate_ui_point_t point;
    gate_uint32_t btn = GATE_UI_MOUSE_UNKNOWN;

    if (paintsurface->on_mouse_up)
    {
        switch (evt->button.button)
        {
        case 1:		btn = GATE_UI_MOUSE_LEFT; break;
        case 2:		btn = GATE_UI_MOUSE_MIDDLE; break;
        case 3:		btn = GATE_UI_MOUSE_LEFT; break;
        case 4:		btn = GATE_UI_MOUSE_SPECIAL1; break;
        case 5:		btn = GATE_UI_MOUSE_SPECIAL2; break;
        }
        point.x = (gate_int32_t)evt->button.x;
        point.y = (gate_int32_t)evt->button.y;

        paintsurface->on_mouse_up(&paintsurface->ctrl, &point, btn);
    }
    return FALSE;
}
static gboolean gate_ui_gdk_paintsurface_enter_notify_event(GtkWidget* widget, GdkEvent* evt, gpointer user_data)
{
    gate_ui_paintsurface_t* paintsurface = (gate_ui_paintsurface_t*)user_data;
    (void)evt;
    if (paintsurface->on_mouse_enter)
    {
        paintsurface->on_mouse_enter(&paintsurface->ctrl);
    }
    return FALSE;
}
static gboolean gate_ui_gdk_paintsurface_leave_notify_event(GtkWidget* widget, GdkEvent* evt, gpointer user_data)
{
    gate_ui_paintsurface_t* paintsurface = (gate_ui_paintsurface_t*)user_data;
    (void)evt;
    if (paintsurface->on_mouse_leave)
    {
        paintsurface->on_mouse_leave(&paintsurface->ctrl);
    }
    return FALSE;
}

static gboolean gate_ui_gdk_paintsurface_focus_in_event(GtkWidget* widget, GdkEvent* evt, gpointer user_data)
{
    gate_ui_paintsurface_t* paintsurface = (gate_ui_paintsurface_t*)user_data;
    (void)evt;
    if (paintsurface->on_focus_changed)
    {
        paintsurface->on_focus_changed(&paintsurface->ctrl, true);
    }
    return FALSE;
}
static gboolean gate_ui_gdk_paintsurface_focus_out_event(GtkWidget* widget, GdkEvent* evt, gpointer user_data)
{
    gate_ui_paintsurface_t* paintsurface = (gate_ui_paintsurface_t*)user_data;
    if (paintsurface->on_focus_changed)
    {
        paintsurface->on_focus_changed(&paintsurface->ctrl, false);
    }
    return FALSE;
}

static gboolean gate_ui_gdk_paintsurface_key_press_event(GtkWidget* widget, GdkEvent* evt, gpointer user_data)
{
    gate_ui_paintsurface_t* paintsurface = (gate_ui_paintsurface_t*)user_data;
    gate_input_keycode_t key_code = 0;
    gate_input_keystates_t ctrl_keys = 0;
    gate_char32_t chr32 = 0;

    if (GATE_FLAG_ENABLED(evt->key.state, GDK_SHIFT_MASK))	ctrl_keys |= GATE_UI_KEY_SHIFT;
    if (GATE_FLAG_ENABLED(evt->key.state, GDK_CONTROL_MASK))	ctrl_keys |= GATE_UI_KEY_CTRL;
    if (GATE_FLAG_ENABLED(evt->key.state, GDK_MOD1_MASK))	ctrl_keys |= GATE_UI_KEY_ALT;

    if (paintsurface->on_key_down)
    {
        gate_ui_gtk_convert_keyval(evt->key.keyval, &key_code);
        paintsurface->on_key_down(&paintsurface->ctrl, key_code, ctrl_keys);
    }
    if (paintsurface->on_key_char)
    {
        chr32 = gdk_keyval_to_unicode(evt->key.keyval);
        paintsurface->on_key_char(&paintsurface->ctrl, chr32, ctrl_keys);
    }
    return FALSE;
}
static gboolean gate_ui_gdk_paintsurface_key_release_event(GtkWidget* widget, GdkEvent* evt, gpointer user_data)
{
    gate_ui_paintsurface_t* paintsurface = (gate_ui_paintsurface_t*)user_data;
    gate_uint32_t key_code = 0;
    gate_uint32_t ctrl_keys = 0;
    gate_char32_t chr32 = 0;

    if (GATE_FLAG_ENABLED(evt->key.state, GDK_SHIFT_MASK))	ctrl_keys |= GATE_UI_KEY_SHIFT;
    if (GATE_FLAG_ENABLED(evt->key.state, GDK_CONTROL_MASK))	ctrl_keys |= GATE_UI_KEY_CTRL;
    if (GATE_FLAG_ENABLED(evt->key.state, GDK_MOD1_MASK))	ctrl_keys |= GATE_UI_KEY_ALT;

    if (paintsurface->on_key_up)
    {
        gate_ui_gtk_convert_keyval(evt->key.keyval, &key_code);
        paintsurface->on_key_up(&paintsurface->ctrl, key_code, ctrl_keys);
    }
    return FALSE;
}
static gboolean gate_ui_gdk_paintsurface_scroll_event(GtkWidget* widget, GdkEvent* evt, gpointer   user_data)
{
    return FALSE;
}
static void gate_ui_gdk_paintsurface_destroy(GtkWidget* object, gpointer user_data)
{

}

static gboolean gate_ui_gdk_paintsurface_draw(GtkWidget* widget, cairo_t* cr, gpointer user_data)
{
    gate_ui_paintsurface_t* paintsurface = (gate_ui_paintsurface_t*)user_data;
    gate_ui_graphics_t graph = GATE_INIT_EMPTY;
    gate_ui_position_t position;
    gate_result_t result;
    gate_ui_host_t* host = NULL;
    int width = 0, height = 0;

    if (paintsurface && paintsurface->on_paint)
    {
        host = GATE_UI_GTK_GET_CTRL_HOST(&paintsurface->ctrl);
        widget = GATE_UI_GTK_GET_CTRL_WIDGET(&paintsurface->ctrl);
        width = gtk_widget_get_allocated_width(widget);
        height = gtk_widget_get_allocated_height(widget);
        result = gate_ui_graphics_create_native(&graph, host, cr, NULL, width, height);
        if (GATE_SUCCEEDED(result))
        {
            paintsurface->on_paint(&paintsurface->ctrl, &graph, &position);
        }
    }
    return FALSE;
}



static gate_ui_gtk_dispatcher_t gate_ui_gtk_paintsurface_dispatcher =
{
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    &gate_ui_gtk_paintsurface_refresh,
    &gate_ui_gtk_paintsurface_destroy
};

#include "gtk/gtkdrawingarea.h"

gate_result_t gate_ui_paintsurface_create(
    gate_ui_paintsurface_t* paintsurface, 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(paintsurface, sizeof(gate_ui_paintsurface_t));
    widget = gtk_drawing_area_new();
    if (widget == NULL)
    {
        return GATE_RESULT_FAILED;
    }

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

    if (GATE_SUCCEEDED(ret))
    {
        gint desired_events = GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK | GDK_BUTTON_MOTION_MASK
            | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
            | GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK
            | GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK
            | GDK_FOCUS_CHANGE_MASK | GDK_SCROLL_MASK;

        gtk_widget_add_events(widget, desired_events);

        g_signal_connect(G_OBJECT(widget), "button-press-event", G_CALLBACK(gate_ui_gdk_paintsurface_button_press_event), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "button-release-event", G_CALLBACK(gate_ui_gdk_paintsurface_button_release_event), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "scroll-event", G_CALLBACK(gate_ui_gdk_paintsurface_scroll_event), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "key-press-event", G_CALLBACK(gate_ui_gdk_paintsurface_key_press_event), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "key-release-event", G_CALLBACK(gate_ui_gdk_paintsurface_key_release_event), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "enter-notify-event", G_CALLBACK(gate_ui_gdk_paintsurface_enter_notify_event), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "leave-notify-event", G_CALLBACK(gate_ui_gdk_paintsurface_leave_notify_event), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "focus-in-event", G_CALLBACK(gate_ui_gdk_paintsurface_focus_in_event), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "focus-out-event", G_CALLBACK(gate_ui_gdk_paintsurface_focus_out_event), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "draw", G_CALLBACK(gate_ui_gdk_paintsurface_draw), (gpointer)paintsurface);
        g_signal_connect(G_OBJECT(widget), "destroy", G_CALLBACK(gate_ui_gdk_paintsurface_destroy), (gpointer)paintsurface);
    }

    return ret;
}

gate_result_t gate_ui_paintsurface_redraw(gate_ui_paintsurface_t* paintsurface)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    do
    {
        ret = gate_ui_gtk_ctrl_refresh(&paintsurface->ctrl);

    } while (0);
    return ret;
}

gate_result_t gate_ui_paintsurface_set_cursor(gate_ui_paintsurface_t* paintsurface, gate_ui_cursor_t const* cursor)
{
    return GATE_RESULT_NOTIMPLEMENTED;
}

#endif	/* GATE_UI_GTK */



#if defined(GATE_UI_MOTIF)

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

static void motif_paintsurface_on_expose(gate_ui_paintsurface_t* surface, gate_ui_position_t const* pos)
{
    gate_result_t result;
    gate_ui_graphics_t graph = GATE_INIT_EMPTY;

    result = gate_ui_graphics_create_ctrl(&graph, &surface->ctrl);
    if (GATE_SUCCEEDED(result))
    {
        surface->on_paint(&surface->ctrl, &graph, pos);
        gate_ui_graphics_destroy(&graph);
    }
}

static void drawing_area_callback(Widget widget, XtPointer client_data, XtPointer call_data)
{
    XmDrawingAreaCallbackStruct* cbs = (XmDrawingAreaCallbackStruct*)call_data;
    gate_ui_paintsurface_t* surface = (gate_ui_paintsurface_t*)client_data;
    XEvent* xevt;
    Display* dpy;
    gate_ui_graphics_t graph = GATE_INIT_EMPTY;
    gate_ui_position_t pos = GATE_INIT_EMPTY;
    Widget w = NULL;
    gate_result_t result;
    gate_enumint_t mousebtn;

    if (!cbs || !surface)
    {
        return;
    }

    xevt = cbs->event;
    if (!xevt)
    {
        return;
    }
    dpy = xevt->xany.display;
    if (dpy)
    {
        return;
    }
    w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&surface->ctrl);

    switch (cbs->reason)
    {
    case XmCR_INPUT:
    {
        switch (xevt->xany.type)
        {
        case ButtonPress:
        {
            pos.pos.x = xevt->xbutton.x;
            pos.pos.y = xevt->xbutton.y;
            mousebtn = GATE_UI_MOUSE_UNKNOWN;
            if (xevt->xbutton.button & Button1Mask) mousebtn = GATE_UI_MOUSE_LEFT;
            if (xevt->xbutton.button & Button2Mask) mousebtn = GATE_UI_MOUSE_MIDDLE;
            if (xevt->xbutton.button & Button3Mask) mousebtn = GATE_UI_MOUSE_RIGHT;
            surface->on_mouse_down(&surface->ctrl, &pos.pos, mousebtn);
            break;
        }
        case ButtonRelease:
        {
            pos.pos.x = xevt->xbutton.x;
            pos.pos.y = xevt->xbutton.y;
            mousebtn = GATE_UI_MOUSE_UNKNOWN;
            if (xevt->xbutton.button & Button1Mask) mousebtn = GATE_UI_MOUSE_LEFT;
            if (xevt->xbutton.button & Button2Mask) mousebtn = GATE_UI_MOUSE_MIDDLE;
            if (xevt->xbutton.button & Button3Mask) mousebtn = GATE_UI_MOUSE_RIGHT;
            surface->on_mouse_up(&surface->ctrl, &pos.pos, mousebtn);
            break;
        }
        case MotionNotify:
        {
            pos.pos.x = xevt->xmotion.x;
            pos.pos.y = xevt->xmotion.y;
            mousebtn = 0;
            if (xevt->xmotion.state & Button1Mask) mousebtn |= GATE_UI_MOUSE_LEFT;
            if (xevt->xmotion.state & Button2Mask) mousebtn |= GATE_UI_MOUSE_MIDDLE;
            if (xevt->xmotion.state & Button3Mask) mousebtn |= GATE_UI_MOUSE_RIGHT;
            surface->on_mouse_move(&surface->ctrl, &pos.pos, mousebtn);
            break;
        }
        case EnterNotify:
        {
            surface->on_mouse_enter(&surface->ctrl);
            break;
        }
        case LeaveNotify:
        {
            surface->on_mouse_leave(&surface->ctrl);
            break;
        }
        case KeyPress:
        {
            surface->on_key_down(&surface->ctrl, xevt->xkey.keycode, 0);
            break;
        }
        case KeyRelease:
        {
            surface->on_key_up(&surface->ctrl, xevt->xkey.keycode, 0);
            break;
        }
        }
        break;
    }
    case XmCR_EXPOSE:
    {
        pos.pos.x = xevt->xexpose.x;
        pos.pos.y = xevt->xexpose.y;
        pos.size.width = xevt->xexpose.width;
        pos.size.height = xevt->xexpose.height;
        motif_paintsurface_on_expose(surface, &pos);
        break;
    }
    case XmCR_ACTIVATE:
    {
        break;
    }
    }
}


gate_result_t gate_ui_paintsurface_create(gate_ui_paintsurface_t* paintsurface, 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[8];
    unsigned args_count = 0;

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

        XtSetArg(args[args_count], XmNrecomputeSize, 0); ++args_count;
        XtSetArg(args[args_count], XmNshadowThickness, 1); ++args_count;
        XtSetArg(args[args_count], XmNhighlightThickness, 1); ++args_count;

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

        w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&paintsurface->ctrl);
        XtAddCallback(w, XmNinputCallback, &drawing_area_callback, paintsurface);
        XtAddCallback(w, XmNresizeCallback, &drawing_area_callback, paintsurface);
        XtAddCallback(w, XmNexposeCallback, &drawing_area_callback, paintsurface);
    } while (0);

    return ret;
}

gate_result_t gate_ui_paintsurface_redraw(gate_ui_paintsurface_t* paintsurface)
{
    Widget w;
    XExposeEvent xevt;
    gate_ui_host_t* host = NULL;
    gate_ui_position_t pos;
    Display* dpy;

    GATE_DEBUG_ASSERT(paintsurface != NULL);
    w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&paintsurface->ctrl);
    GATE_DEBUG_ASSERT(w != NULL);
    host = GATE_UI_MOTIF_GET_CTRL_HOST(&paintsurface->ctrl);
    GATE_DEBUG_ASSERT(host != NULL);
    dpy = GATE_UI_MOTIF_GET_HOST_DISPLAY(host);
    GATE_DEBUG_ASSERT(dpy != NULL);

    gate_ui_ctrl_get_position(&paintsurface->ctrl, &pos);
    motif_paintsurface_on_expose(paintsurface, &pos);

    /*
    xevt.type = Expose;
    xevt.display = dpy;
    xevt.window = XtWindow(w);
    xevt.x = 0;
    xevt.y = 0;
    xevt.width = pos.size.width;
    xevt.height = pos.size.height;
    xevt.count = 0;

    XSendEvent(dpy, xevt.window, False, ExposureMask, (XEvent*)&xevt);
    XFlush(dpy);
    */
    return GATE_RESULT_OK;
}

gate_result_t gate_ui_paintsurface_set_cursor(gate_ui_paintsurface_t* paintsurface, gate_ui_cursor_t const* cursor)
{
    /* TODO */
    return GATE_RESULT_OK;
}

#endif /* GATE_UI_MOTIF */