| 1 | /*
|
|---|
| 2 | * Copyright (c) 2023 SimonJRiddix
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #ifndef UI_BUTTON_H
|
|---|
| 30 | #define UI_BUTTON_H
|
|---|
| 31 |
|
|---|
| 32 | #include <errno.h>
|
|---|
| 33 | #include <gfx/coord.h>
|
|---|
| 34 | #include <stdbool.h>
|
|---|
| 35 | #include <stdio.h>
|
|---|
| 36 | #include <stdlib.h>
|
|---|
| 37 | #include <str.h>
|
|---|
| 38 | #include <str_error.h>
|
|---|
| 39 | #include <task.h>
|
|---|
| 40 | #include <ui/fixed.h>
|
|---|
| 41 | #include <ui/resource.h>
|
|---|
| 42 | #include <ui/ui.h>
|
|---|
| 43 | #include <ui/pbutton.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "window.h"
|
|---|
| 46 |
|
|---|
| 47 | typedef struct sButton Button;
|
|---|
| 48 | struct sButton
|
|---|
| 49 | {
|
|---|
| 50 | char* name;
|
|---|
| 51 | char* text;
|
|---|
| 52 |
|
|---|
| 53 | size_t max_length;
|
|---|
| 54 |
|
|---|
| 55 | ui_fixed_t *fixed;
|
|---|
| 56 | ui_pbutton_t *button;
|
|---|
| 57 | ui_pbutton_flags_t flags;
|
|---|
| 58 |
|
|---|
| 59 | gfx_rect_t rect;
|
|---|
| 60 |
|
|---|
| 61 | void (*set_text)(Button* w, char* text);
|
|---|
| 62 | char* (*get_text)(Button* w);
|
|---|
| 63 |
|
|---|
| 64 | ui_pbutton_cb_t pbutton_cb;
|
|---|
| 65 | void (*set_callback)(Button* w, void (*pb_clicked)(ui_pbutton_t *, void *), void* extra);
|
|---|
| 66 |
|
|---|
| 67 | void (*update_rect)(Button* w);
|
|---|
| 68 | void (*set_flags)(Button* w, ui_pbutton_flags_t flags);
|
|---|
| 69 | void (*update_flags)(Button* w);
|
|---|
| 70 | void (*paint)(Button* w);
|
|---|
| 71 |
|
|---|
| 72 | void (*set_horizontal_align)(Button* w, gfx_halign_t a);
|
|---|
| 73 | void (*set_vertical_align)(Button* w, gfx_halign_t a);
|
|---|
| 74 | void (*destroy)(Button* w);
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | int init_button(Button* w, const char * name, Window* window);
|
|---|
| 78 | void button_set_callback(Button* w, void (*pb_clicked)(ui_pbutton_t *, void *), void* extra);
|
|---|
| 79 | void button_set_text(Button* w, char* text);
|
|---|
| 80 | char* button_get_text(Button* w);
|
|---|
| 81 | void button_update_rect(Button* w);
|
|---|
| 82 | void button_set_flags(Button* w, ui_pbutton_flags_t flags);
|
|---|
| 83 | void button_update_flags(Button* w);
|
|---|
| 84 | void button_paint(Button* w);
|
|---|
| 85 | void button_set_horizontal_align(Button* w, gfx_halign_t a);
|
|---|
| 86 | void button_set_vertical_align(Button* w, gfx_halign_t a);
|
|---|
| 87 | void button_destroy(Button* w);
|
|---|
| 88 |
|
|---|
| 89 | void button_set_text(Button* w, char* text)
|
|---|
| 90 | {
|
|---|
| 91 | //free(w->text);
|
|---|
| 92 | size_t szt = str_size(text) + 1;
|
|---|
| 93 | if ( szt > w->max_length )
|
|---|
| 94 | szt = w->max_length;
|
|---|
| 95 | w->text = malloc(sizeof(char) * szt);
|
|---|
| 96 | str_cpy(w->text, szt, text);
|
|---|
| 97 | ui_pbutton_set_caption(w->button, w->text);
|
|---|
| 98 | ui_pbutton_paint(w->button);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | char* button_get_text(Button* w)
|
|---|
| 102 | {
|
|---|
| 103 | return w->text;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void button_update_rect(Button* w)
|
|---|
| 107 | {
|
|---|
| 108 | ui_pbutton_set_rect(w->button, &w->rect);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | void button_paint(Button* w)
|
|---|
| 112 | {
|
|---|
| 113 | ui_pbutton_paint(w->button);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | void button_set_horizontal_align(Button* w, gfx_halign_t a)
|
|---|
| 117 | {
|
|---|
| 118 | //ui_pbutton_set_halign(w->button, a);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void button_set_vertical_align(Button* w, gfx_halign_t a)
|
|---|
| 122 | {
|
|---|
| 123 | //ui_pbutton_set_valign(w->button, a);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | void button_set_callback(Button* w, void (*pb_clicked2)(ui_pbutton_t *, void *), void* extra)
|
|---|
| 127 | {
|
|---|
| 128 | ui_pbutton_cb_t pbutton_cb = {
|
|---|
| 129 | .clicked = pb_clicked2
|
|---|
| 130 | };
|
|---|
| 131 | w->pbutton_cb = pbutton_cb;
|
|---|
| 132 | ui_pbutton_set_cb(w->button, &w->pbutton_cb, extra);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | void button_set_flags(Button* w, ui_pbutton_flags_t flags)
|
|---|
| 136 | {
|
|---|
| 137 | w->flags = flags;
|
|---|
| 138 | ui_pbutton_set_flags(w->button, w->flags);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | void button_update_flags(Button* w)
|
|---|
| 142 | {
|
|---|
| 143 | ui_pbutton_set_flags(w->button, w->flags);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | void button_destroy(Button* w)
|
|---|
| 147 | {
|
|---|
| 148 | ui_pbutton_destroy(w->button);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | int init_button(Button* w, const char * name, Window* window)
|
|---|
| 152 | {
|
|---|
| 153 | w->max_length = 50;
|
|---|
| 154 | w->text = malloc(sizeof(char) * 7);
|
|---|
| 155 | str_cpy(w->text, 7, (char*) "Button1");
|
|---|
| 156 |
|
|---|
| 157 | errno_t rc = ui_pbutton_create(window->ui_res, w->text, &w->button);
|
|---|
| 158 | if (rc != EOK) {
|
|---|
| 159 | printf("Error creating button.\n");
|
|---|
| 160 | return rc;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /*w->rect.p0.x = 15;
|
|---|
| 164 | w->rect.p0.y = 130;
|
|---|
| 165 | w->rect.p1.x = 190;
|
|---|
| 166 | w->rect.p1.y = w->rect.p0.y + 28;*/
|
|---|
| 167 | ui_pbutton_set_rect(w->button, &w->rect);
|
|---|
| 168 |
|
|---|
| 169 | rc = ui_fixed_add(window->fixed, ui_pbutton_ctl(w->button));
|
|---|
| 170 | if (rc != EOK) {
|
|---|
| 171 | printf("Error adding control to layout.\n");
|
|---|
| 172 | return rc;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | w->set_text = button_set_text;
|
|---|
| 176 | w->get_text = button_get_text;
|
|---|
| 177 | w->update_rect = button_update_rect;
|
|---|
| 178 | w->set_flags = button_set_flags;
|
|---|
| 179 | w->update_flags = button_update_flags;
|
|---|
| 180 | w->paint = button_paint;
|
|---|
| 181 |
|
|---|
| 182 | w->set_horizontal_align = button_set_horizontal_align;
|
|---|
| 183 | w->set_vertical_align = button_set_vertical_align;
|
|---|
| 184 |
|
|---|
| 185 | w->set_callback = button_set_callback;
|
|---|
| 186 |
|
|---|
| 187 | w->destroy = button_destroy;
|
|---|
| 188 |
|
|---|
| 189 | return EOK;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | #endif
|
|---|