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

#if defined(GATE_UI_WINAPI)

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

static BOOL SetWindowPosIfRequired(HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags)
{
    RECT rect;
    POINT point;
    HWND hWndParent;
    BOOL ret = TRUE;

#if !defined(GATE_SYS_WIN16)
    ret =
#endif
        GetWindowRect(hWnd, &rect);
    if (ret)
    {
        hWndParent = GetParent(hWnd);

        point.x = rect.left;
        point.y = rect.top;

        if (hWndParent != NULL)
        {
            ScreenToClient(hWndParent, &point);
        }
        if ((X != point.x) || (Y != point.y) ||
            (cx != rect.right - rect.left) || (cy != rect.bottom - rect.top)
            )
        {
            ret = SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags);
        }
        else
        {
            ret = FALSE;
        }
    }
    return ret;
}


static void gate_ui_splitter_resize(gate_ui_splitter_t* splitter, HWND hwndFirst, HWND hwndSecond)
{
    gate_ui_size_t sz;
    gate_result_t ret;
    gate_ui_host_t* host = GATE_UI_WINAPI_GET_HOST(&splitter->ctrl);
    gate_uint32_t lineheight = gate_ui_host_default_line_height(host);
    gate_uint32_t length;
    gate_int32_t separator;
    UINT showflags = SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_DRAWFRAME;
    do
    {
        ret = gate_ui_winapi_get_size(&splitter->ctrl, &sz);
        if (GATE_FAILED(ret)) break;

        separator = lineheight / 5;
        if (separator < 1) separator = 1;

        if (splitter->vertical)
        {
            length = sz.height;
        }
        else
        {
            length = sz.width;
        }
        if (length < splitter->minimum * 3 / 2)
        {
            ret = GATE_RESULT_FAILED;
            break;
        }


        if (splitter->value > length - splitter->minimum)
        {
            splitter->value = length - splitter->minimum;
        }

        if (splitter->value < splitter->minimum)
        {
            splitter->value = splitter->minimum;
        }

        if (splitter->vertical)
        {
            if (hwndFirst != NULL)
            {
                SetWindowPosIfRequired(hwndFirst, NULL, 0, 0, sz.width, splitter->value - separator, showflags);
            }
            if (hwndSecond != NULL)
            {
                SetWindowPosIfRequired(hwndSecond, NULL, 0, splitter->value + separator, sz.width, sz.height - splitter->value - separator, showflags);
            }
        }
        else
        {
            if (hwndFirst != NULL)
            {
                SetWindowPosIfRequired(hwndFirst, NULL, 0, 0, splitter->value - separator, sz.height, showflags);
            }
            if (hwndSecond != NULL)
            {
                SetWindowPosIfRequired(hwndSecond, NULL, splitter->value + separator, 0, sz.width - splitter->value - separator, sz.height, showflags);
            }
        }
    } while (0);

}

static gate_bool_t gate_ui_splitter_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;
    gate_ui_splitter_t* splitter;
    POINT pnt;
    HWND hwnd_parent;
    RECT rect;
    HDC hdc;
    HBRUSH hbr_bk_gnd = NULL;
    gate_ui_host_t* host = NULL;
    static HCURSOR hcursor = NULL;
#if defined(GATE_UI_WINAPI_DARKMODE_SUPPORT)
    gate_ui_color_t col;
#endif

    if (ctrl != NULL)
    {
        hwnd_ctrl = GATE_UI_WINAPI_GET_HWND(ctrl);
        if (hwnd == hwnd_ctrl)
        {
            if (hcursor == NULL)
            {
                hcursor = LoadCursor(NULL, IDC_SIZEWE);
            }

            splitter = (gate_ui_splitter_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), msg, wParam, lParam);
                return true;
                break;
            }
#endif /* GATE_SYS_WIN16 */
            case WM_ERASEBKGND:
            {
                hwnd_parent = GetParent(hwnd_ctrl);
                hdc = (HDC)wParam;

#if defined(GATE_UI_WINAPI_DARKMODE_SUPPORT)
                if (gate_ui_winapi_is_darkmode_applied(hwnd_ctrl))
                {
                    host = GATE_UI_WINAPI_GET_HOST(ctrl);

                    gate_ui_host_default_color(host, GATE_UI_COLOR_CONTROL, &col);
                    hbr_bk_gnd = (HBRUSH)gate_ui_winapi_host_get_brush(host, &col);
                }
                else
#endif /* GATE_UI_WINAPI_DARKMODE_SUPPORT */
                {
                    hbr_bk_gnd = (HBRUSH)gate_ui_winapi_get_syscolor_brush(
                        gate_ui_winapi_get_syscolor_index(GATE_UI_COLOR_CONTROL));
                }

                if (hbr_bk_gnd)
                {
                    GetClientRect(hwnd_ctrl, &rect);
                    FillRect(hdc, &rect, hbr_bk_gnd);
                    *lresult = 1;
                    return true;
                }
                break;
            }
            case WM_SIZE:
            {
                gate_ui_splitter_update(splitter);
                break;
            }
            case WM_SETCURSOR:
            {
                GetCursorPos(&pnt);
                if (hwnd == WindowFromPoint(pnt))
                {
                    SetCursor(hcursor);
                    *lresult = TRUE;
                    return true;
                }
                break;
            }
            case WM_LBUTTONDOWN:
            {
                splitter->drag_mode = true;
                splitter->click_point.x = GET_X_LPARAM(lParam);
                splitter->click_point.y = GET_Y_LPARAM(lParam);
                SetCapture(hwnd_ctrl);
                break;
            }
            case WM_MOUSEMOVE:
            {
                if (splitter->drag_mode)
                {
                    if ((splitter->click_point.x != GET_X_LPARAM(lParam))
                        || (splitter->click_point.y != GET_Y_LPARAM(lParam))
                        )
                    {
                        if (splitter->vertical)
                        {
                            splitter->click_point.y = GET_Y_LPARAM(lParam);
                            if (splitter->click_point.y < 0) splitter->click_point.y = 0;
                            splitter->value = splitter->click_point.y;
                        }
                        else
                        {
                            splitter->click_point.x = GET_X_LPARAM(lParam);
                            if (splitter->click_point.x < 0) splitter->click_point.x = 0;
                            splitter->value = splitter->click_point.x;
                        }
                        gate_ui_splitter_update(splitter);
                    }
                }
                break;
            }
            case WM_LBUTTONUP:
            {
                splitter->drag_mode = false;
                ReleaseCapture();
                break;
            }
            }
        }
    }
    return false;
}

gate_result_t gate_ui_splitter_create(
    gate_ui_splitter_t* splitter, gate_ui_ctrl_t* parent,
    gate_ui_position_t const* position,
    gate_bool_t vertical,
    gate_uint32_t flags,
    void* userparam
)
{
    gate_result_t ret;
    gate_uint32_t styles = 0;
    gate_uint32_t exstyles = 0;
    gate_ui_host_t* host;
    gate_ui_size_t clientsize;
    HWND hwnd;
    HWND hwndParent;

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

        gate_mem_clear(splitter, sizeof(gate_ui_splitter_t));
        splitter->minimum = 32;
        splitter->vertical = vertical;
#if !defined(GATE_SYS_WINCE) && !defined(GATE_SYS_WIN16)
        exstyles = WS_EX_CONTROLPARENT;
#endif
        styles = WS_CHILD | WS_CLIPCHILDREN;

        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(&splitter->ctrl, host, hwndParent, NULL, position, styles, exstyles, NULL, userparam, false);
        if (GATE_SUCCEEDED(ret))
        {
            hwnd = GATE_UI_WINAPI_GET_HWND(&splitter->ctrl);
            gate_ui_winapi_get_size(&splitter->ctrl, &clientsize);
            splitter->value = (vertical ? clientsize.height : clientsize.width) / 2;

            gate_ui_winapi_register_event(host, hwnd, WM_SIZE, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_SETCURSOR, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_LBUTTONDOWN, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_LBUTTONUP, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_MOUSEMOVE, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_ERASEBKGND, &gate_ui_splitter_events, &splitter->ctrl);

#if !defined(GATE_SYS_WIN16)
            gate_ui_winapi_register_event(host, hwnd, WM_CTLCOLORDLG, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_CTLCOLORSTATIC, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_CTLCOLOREDIT, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_CTLCOLORBTN, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_CTLCOLORLISTBOX, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_CTLCOLORMSGBOX, &gate_ui_splitter_events, &splitter->ctrl);
            gate_ui_winapi_register_event(host, hwnd, WM_CTLCOLORSCROLLBAR, &gate_ui_splitter_events, &splitter->ctrl);
#endif

        }
    } while (0);

    return ret;
}

gate_result_t gate_ui_splitter_set(gate_ui_splitter_t* splitter, gate_uint32_t const* minimum, gate_uint32_t const* value)
{
    gate_result_t ret = GATE_RESULT_OK;
    HWND hwnd = GATE_UI_WINAPI_GET_HWND(&splitter->ctrl);

    if (minimum != NULL)
    {
        splitter->minimum = *minimum;
    }
    if (value != NULL)
    {
        splitter->value = *value;
    }

    if (hwnd != NULL)
    {
        ret = gate_ui_splitter_update(splitter);
    }
    return ret;
}

struct gate_ui_splitter_enum_child_windows
{
    HWND	parent;
    HWND	first;
    HWND	second;
};

static BOOL CALLBACK gate_ui_splitter_enum_children(HWND hwnd, LPARAM lParam)
{
    struct gate_ui_splitter_enum_child_windows* param = (struct gate_ui_splitter_enum_child_windows*)(gate_intptr_t)lParam;
    if (GetParent(hwnd) == param->parent)
    {
        if (param->first == NULL)
        {
            param->first = hwnd;
        }
        else if (param->second == NULL)
        {
            param->second = hwnd;
        }
    }
    return TRUE;
}


gate_result_t gate_ui_splitter_update(gate_ui_splitter_t* splitter)
{
    gate_result_t ret = GATE_RESULT_INVALIDARG;
    HWND hwnd = GATE_UI_WINAPI_GET_HWND(&splitter->ctrl);
    struct gate_ui_splitter_enum_child_windows children;

    if (hwnd != NULL)
    {
        children.parent = hwnd;
        children.first = NULL;
        children.second = NULL;
#if defined(GATE_SYS_WINCE)
        children.first = GetWindow(hwnd, GW_CHILD);
        ret = GATE_RESULT_FAILED;<--- Variable 'ret' is reassigned a value before the old one has been used.
        if (children.first != NULL)
        {
            children.second = GetWindow(children.first, GW_HWNDNEXT);
        }
#else
        EnumChildWindows(hwnd, &gate_ui_splitter_enum_children, (LPARAM)(gate_intptr_t)&children);
#endif
        if ((children.first != NULL) || (children.second != NULL))
        {
            gate_ui_splitter_resize(splitter, children.first, children.second);
        }
        ret = GATE_RESULT_OK;<--- Variable 'ret' is reassigned a value before the old one has been used.
    }
    return ret;
}

gate_result_t gate_ui_splitter_set_first(gate_ui_splitter_t* splitter, gate_ui_ctrl_t* ctrl)
{
    GATE_UNUSED_ARG(splitter);
    GATE_UNUSED_ARG(ctrl);
    return GATE_RESULT_OK;
}
gate_result_t gate_ui_splitter_set_second(gate_ui_splitter_t* splitter, gate_ui_ctrl_t* ctrl)
{
    GATE_UNUSED_ARG(splitter);
    GATE_UNUSED_ARG(ctrl);
    return GATE_RESULT_OK;
}



#endif	/* GATE_UI_WINAPI */


#if defined(GATE_UI_GTK)

#include "gate/ui/gateui_gtk.h"


static gboolean gate_ui_gtk_splitter_refresh_idle_callback(gpointer user_data)
{
    gate_ui_splitter_t* splitter = (gate_ui_splitter_t*)user_data;
    gate_ui_gtk_ctrl_refresh(&splitter->ctrl);
    return FALSE;	/* FALSE means: do not invoke this callback again (until next call to g_idle_add()) */
}


static gate_result_t gate_ui_gtk_splitter_refresh(gate_ui_ctrl_t* ctrl)
{
    gate_ui_splitter_t* splitter = (gate_ui_splitter_t*)ctrl;
    g_idle_add(&gate_ui_gtk_splitter_refresh_idle_callback, splitter);
    return gate_ui_gtk_ctrl_refresh(ctrl);
}

static gate_result_t gate_ui_gtk_splitter_destroy(gate_ui_ctrl_t* ctrl)
{
    return gate_ui_gtk_ctrl_destroy_native(ctrl);
}


static gate_ui_gtk_dispatcher_t gate_ui_gtk_splitter_dispatcher =
{
    NULL /*get_text_length*/,
    NULL /*get_text*/,
    NULL /*set_text*/,
    NULL /*get_state*/,
    NULL /*set_state*/,
    &gate_ui_gtk_splitter_refresh,
    &gate_ui_gtk_splitter_destroy
};


gate_result_t gate_ui_splitter_create(
    gate_ui_splitter_t* splitter, gate_ui_ctrl_t* parent,
    gate_ui_position_t const* position,
    gate_bool_t vertical,
    gate_uint32_t flags,
    void* userparam
)
{
    GtkWidget* widget;
    gate_ui_host_t* host = GATE_UI_GTK_GET_CTRL_HOST(parent);

    gate_mem_clear(splitter, sizeof(gate_ui_splitter_t));
    widget = gtk_paned_new(vertical ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
    if (widget == NULL)
    {
        return GATE_RESULT_FAILED;
    }

    gate_ui_gtk_ctrl_init(&splitter->ctrl, widget, host, userparam, parent,
        &gate_ui_gtk_splitter_dispatcher, false, true, position, &flags);

    return GATE_RESULT_OK;
}

gate_result_t gate_ui_splitter_set(gate_ui_splitter_t* splitter,
    gate_uint32_t const* minimum, gate_uint32_t const* value)
{
    gate_result_t ret = GATE_RESULT_OK;
    GtkWidget* container = GATE_UI_GTK_GET_CTRL_WIDGET(&splitter->ctrl);

    if (value)
    {
        gtk_paned_set_position(GTK_PANED(container), (gint)*value);
    }
    return ret;
}

gate_result_t gate_ui_splitter_update(gate_ui_splitter_t* splitter)
{
    gate_result_t ret = GATE_RESULT_OK;
    GtkWidget* container = GATE_UI_GTK_GET_CTRL_WIDGET(&splitter->ctrl);<--- Variable 'container' is assigned a value that is never used.

    return ret;
}

gate_result_t gate_ui_splitter_set_first(gate_ui_splitter_t* splitter, gate_ui_ctrl_t* ctrl)
{
    gate_result_t ret = GATE_RESULT_OK;
    GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&splitter->ctrl);
    GtkWidget* child = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
    GtkContainer* container = GTK_CONTAINER(widget);<--- Variable 'container' is assigned a value that is never used.

    gtk_paned_add1(GTK_PANED(widget), child);
    if (!gtk_widget_get_realized(child))
    {
        gtk_widget_realize(child);
    }

    return ret;
}
gate_result_t gate_ui_splitter_set_second(gate_ui_splitter_t* splitter, gate_ui_ctrl_t* ctrl)
{
    gate_result_t ret = GATE_RESULT_OK;
    GtkWidget* widget = GATE_UI_GTK_GET_CTRL_WIDGET(&splitter->ctrl);
    GtkWidget* child = GATE_UI_GTK_GET_CTRL_WIDGET(ctrl);
    GtkContainer* container = GTK_CONTAINER(widget);<--- Variable 'container' is assigned a value that is never used.

    gtk_paned_add2(GTK_PANED(widget), child);
    if (!gtk_widget_get_realized(child))
    {
        gtk_widget_realize(child);
    }

    return ret;
}

#endif	/* GATE_UI_GTK */



#if defined(GATE_UI_MOTIF)

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

gate_result_t gate_ui_splitter_create(gate_ui_splitter_t* splitter, gate_ui_ctrl_t* parent, gate_ui_position_t const* position,
    gate_bool_t vertical, gate_uint32_t flags, void* userparam)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    Arg args[8];
    unsigned args_count = 0;
    Widget w = NULL;

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

        XtSetArg(args[args_count], XmNorientation, (vertical ? XmVERTICAL : XmHORIZONTAL)); ++args_count;

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

        splitter->vertical = vertical;
        splitter->minimum = 0;
        splitter->drag_mode = false;
        splitter->value = 0;

    } while (0);

    return ret;
}


static void apply_splitter_settings_to_children(gate_ui_splitter_t* splitter)
{
    Widget w = NULL;
    WidgetList children = NULL;
    int count = 0;
    int ndx;

    w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&splitter->ctrl);
    XmPanedGetPanes(w, &children, &count);
    if (count > 0)
    {
        if (splitter->value != 0)
        {
            XtVaSetValues(children[0],
                XmNpreferredPaneSize, (Cardinal)splitter->value,
                XmNresizeToPreferred, TRUE,
                NULL, NULL);
        }
        if (splitter->minimum != 0)
        {
            for (ndx = 0; ndx != count; ++ndx)
            {
                XtVaSetValues(children[ndx],
                    XmNpaneMinimum, (Cardinal)splitter->minimum,
                    NULL, NULL);
            }
        }
    }
}

gate_result_t gate_ui_splitter_set(gate_ui_splitter_t* splitter, gate_uint32_t const* minimum, gate_uint32_t const* value)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    Widget w = NULL;

    do
    {
        if (!gate_ui_ctrl_is_created(&splitter->ctrl))
        {
            ret = GATE_RESULT_INVALIDSTATE;
            break;
        }
        w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&splitter->ctrl);
        if (!w)
        {
            ret = GATE_RESULT_INVALIDSTATE;
            break;
        }

        if (minimum)
        {
            splitter->minimum = *minimum;
        }
        if (value)
        {
            splitter->value = *value;
        }

        apply_splitter_settings_to_children(splitter);

        ret = GATE_RESULT_OK;
    } while (0);
    return ret;
}

gate_result_t gate_ui_splitter_update(gate_ui_splitter_t* splitter)
{
    return GATE_RESULT_NOTIMPLEMENTED;
}

static gate_result_t gate_ui_splitter_add_child(gate_ui_splitter_t* splitter, gate_ui_ctrl_t* ctrl)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    Widget w = NULL;
    Widget c = NULL;
    gate_ui_host_t* ptr_host;
    XtAppContext app_context;

    do
    {
        if (!gate_ui_ctrl_is_created(&splitter->ctrl) || !gate_ui_ctrl_is_created(ctrl))
        {
            ret = GATE_RESULT_INVALIDSTATE;
            break;
        }
        w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&splitter->ctrl);
        c = GATE_UI_MOTIF_GET_CTRL_WIDGET(ctrl);
        if (!w || !c)
        {
            ret = GATE_RESULT_INVALIDSTATE;
            break;
        }

        apply_splitter_settings_to_children(splitter);

        ret = GATE_RESULT_OK;
    } while (0);
    return ret;
}

gate_result_t gate_ui_splitter_set_first(gate_ui_splitter_t* splitter, gate_ui_ctrl_t* ctrl)
{
    return gate_ui_splitter_add_child(splitter, ctrl);
}

gate_result_t gate_ui_splitter_set_second(gate_ui_splitter_t* splitter, gate_ui_ctrl_t* ctrl)
{
    return gate_ui_splitter_add_child(splitter, ctrl);
}

#endif /* GATE_UI_MOTIF */