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

#if defined(GATE_UI_WINAPI)


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

static gate_bool_t gate_ui_button_events(void* hwnd, gate_ui_ctrl_t* ctrl, gate_uint32_t msg, gate_uintptr_t wParam, gate_intptr_t lParam, gate_intptr_t* lresult)
{
    HWND hwndCtrl;
    gate_ui_button_t* button;
    if ((ctrl != NULL) && (hwnd != NULL))
    {
        hwndCtrl = GATE_UI_WINAPI_GET_HWND(ctrl);
        if (hwndCtrl)
        {
            switch (msg)
            {
            case WM_COMMAND:
            {
                if (hwndCtrl == (HWND)lParam)
                {
                    button = (gate_ui_button_t*)ctrl;
                    switch (HIWORD(wParam))
                    {
                    case BN_CLICKED:
                    {
                        if (button->on_click != NULL)
                        {
                            button->on_click(ctrl);
                        }
                        *lresult = 0;
                        return true;
                    }
                    default:
                    {
                        break;
                    }
                    }
                }
                break;
            }
            default:
            {
                break;
            }
            }
        }
    }
    return false;
}

gate_result_t gate_ui_button_create(
    gate_ui_button_t* button, gate_ui_ctrl_t* parent,
    gate_ui_position_t const* position,
    gate_string_t const* caption,
    gate_uint32_t flags,
    void* userparam
)
{
    gate_result_t ret;
    gate_uint32_t styles, exstyles;
    gate_ui_host_t* host;
    HWND hwndParent;

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

        gate_mem_clear(button, sizeof(gate_ui_button_t));

        exstyles = 0;
        styles = WS_CHILD | WS_CLIPSIBLINGS | WS_TABSTOP | BS_PUSHBUTTON;
#if !defined(GATE_SYS_WIN16)
        styles |= (BS_MULTILINE | BS_VCENTER);
#endif
        /*styles |= BS_FLAT;*/
        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(&button->ctrl, host, (void*)hwndParent,
            _T("BUTTON"), position, styles, exstyles, caption, userparam, true);
        if (GATE_SUCCEEDED(ret))
        {
            gate_ui_winapi_register_event(host, (void*)hwndParent, WM_COMMAND, &gate_ui_button_events, &button->ctrl);
        }
    } while (0);

    return ret;
}

#endif /*GATE_UI_WINAPI*/


#if defined(GATE_UI_GTK)

#include "gate/ui/gateui_gtk.h"

static gate_uint32_t gate_ui_gtk_button_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_button_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_button_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_button_get_state(gate_ui_ctrl_t* ctrl, gate_int32_t* value)
{
    return GATE_RESULT_NOTIMPLEMENTED;
}
static gate_result_t gate_ui_gtk_button_set_state(gate_ui_ctrl_t* ctrl, gate_int32_t value)
{
    return GATE_RESULT_NOTIMPLEMENTED;
}

static gate_ui_gtk_dispatcher_t gate_ui_gtk_button_dispatcher =
{
    gate_ui_gtk_button_get_text_length,
    gate_ui_gtk_button_get_text,
    gate_ui_gtk_button_set_text,
    NULL,
    NULL,
    NULL
};


static void gate_ui_button_clicked(GtkButton* button, gpointer user_data)
{
    gate_ui_button_t* button_ctrl = (gate_ui_button_t*)user_data;
    if (button_ctrl)
    {
        if (button_ctrl->on_click != NULL)
        {
            button_ctrl->on_click(&button_ctrl->ctrl);
        }
    }
}


gate_result_t gate_ui_button_create(gate_ui_button_t* button, gate_ui_ctrl_t* parent,
    gate_ui_position_t const* position, gate_string_t const* caption,
    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(button, sizeof(gate_ui_button_t));
    widget = gtk_button_new();
    if (widget == NULL)
    {
        return GATE_RESULT_FAILED;
    }

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

    if (GATE_SUCCEEDED(ret))
    {
        if (gate_string_length(caption) > 0)
        {
            gate_ui_ctrl_set_text(&button->ctrl, caption);
        }

        g_signal_connect(G_OBJECT(widget), "clicked", G_CALLBACK(gate_ui_button_clicked), (gpointer)button);
    }

    return ret;
}


#endif	/* GATE_UI_GTK */

#if defined(GATE_UI_MOTIF)

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

static void button_pushed(Widget widget, XtPointer client_data, XtPointer call_data)
{
    gate_ui_button_t* ptr_button = (gate_ui_button_t*)client_data;
    if (!ptr_button || !ptr_button->on_click)
    {
        return;
    }
    ptr_button->on_click(&ptr_button->ctrl);
}

gate_result_t gate_ui_button_create(gate_ui_button_t* button, gate_ui_ctrl_t* parent,
    gate_ui_position_t const* position, gate_string_t const* caption,
    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 (!button || !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(&button->ctrl, xmPushButtonWidgetClass, userparam, NULL, parent,
            position, &flags, false, NULL, args, args_count);
        GATE_BREAK_IF_FAILED(ret);

        w = GATE_UI_MOTIF_GET_CTRL_WIDGET(&button->ctrl);
        XtAddCallback(w, XmNactivateCallback, &button_pushed, button);
        ret = gate_ui_motif_widget_set_label(w, caption);
    } while (0);

    return ret;
}

#endif /* GATE_UI_MOTIF */


#if defined(GATE_UI_WASMHTML)

#include "gate/ui/gateui_wasmhtml.h"

static gate_string_t const html_class_button = GATE_STRING_INIT_STATIC("button");

gate_result_t gate_ui_button_create(gate_ui_button_t* button, gate_ui_ctrl_t* parent,
    gate_ui_position_t const* position, gate_string_t const* caption,
    gate_uint32_t flags, void* userparam)
{
    gate_result_t ret = GATE_RESULT_FAILED;
    gate_ui_host_t* host = gate_ui_ctrl_get_host(parent);
    gate_uint32_t parent_id = gate_ui_wasm_ctrl_get_container(parent);
    gate_uint32_t new_id = 0;

    do
    {
        gate_ui_wasm_new_ctrl_id(host, &new_id);
        ret = gate_ui_wasm_ctrl_create(host, new_id, parent_id, &html_class_button, userparam, &button->ctrl);
        GATE_BREAK_IF_FAILED(ret);

        if (position)
        {
            gate_ui_wasm_ctrl_set_position(&button->ctrl, &position->pos, &position->size);
        }
        gate_ui_wasm_ctrl_set_text(&button->ctrl, caption);
    } while (0);

    return ret;
}

#endif /* GATE_UI_WASMHTML */