[f80690a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2020 Jiri Svoboda
|
---|
| 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 | /** @addtogroup libui
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file Push button
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
[47728678] | 37 | #include <gfx/color.h>
|
---|
| 38 | #include <gfx/context.h>
|
---|
| 39 | #include <gfx/render.h>
|
---|
| 40 | #include <gfx/text.h>
|
---|
[f80690a] | 41 | #include <stdlib.h>
|
---|
[47728678] | 42 | #include <str.h>
|
---|
[f80690a] | 43 | #include <ui/pbutton.h>
|
---|
| 44 | #include "../private/pbutton.h"
|
---|
[47728678] | 45 | #include "../private/resource.h"
|
---|
[f80690a] | 46 |
|
---|
| 47 | /** Create new push button.
|
---|
| 48 | *
|
---|
[47728678] | 49 | * @param resource UI resource
|
---|
[f80690a] | 50 | * @param caption Caption
|
---|
| 51 | * @param rpbutton Place to store pointer to new push button
|
---|
| 52 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 53 | */
|
---|
[47728678] | 54 | errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
|
---|
| 55 | ui_pbutton_t **rpbutton)
|
---|
[f80690a] | 56 | {
|
---|
| 57 | ui_pbutton_t *pbutton;
|
---|
| 58 |
|
---|
| 59 | pbutton = calloc(1, sizeof(ui_pbutton_t));
|
---|
| 60 | if (pbutton == NULL)
|
---|
| 61 | return ENOMEM;
|
---|
| 62 |
|
---|
[47728678] | 63 | pbutton->caption = str_dup(caption);
|
---|
| 64 | if (pbutton->caption == NULL) {
|
---|
| 65 | free(pbutton);
|
---|
| 66 | return ENOMEM;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | pbutton->res = resource;
|
---|
[f80690a] | 70 | *rpbutton = pbutton;
|
---|
| 71 | return EOK;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /** Destroy push button.
|
---|
| 75 | *
|
---|
| 76 | * @param pbutton Push button or @c NULL
|
---|
| 77 | */
|
---|
| 78 | void ui_pbutton_destroy(ui_pbutton_t *pbutton)
|
---|
| 79 | {
|
---|
| 80 | if (pbutton == NULL)
|
---|
| 81 | return;
|
---|
| 82 |
|
---|
| 83 | free(pbutton);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[47728678] | 86 | /** Set button rectangle.
|
---|
| 87 | *
|
---|
| 88 | * @param pbutton Button
|
---|
| 89 | * @param rect New button rectanle
|
---|
| 90 | */
|
---|
| 91 | void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
|
---|
| 92 | {
|
---|
| 93 | pbutton->rect = *rect;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /** Paint push button.
|
---|
| 97 | *
|
---|
| 98 | * @param pbutton Push button
|
---|
| 99 | * @return EOK on success or an error code
|
---|
| 100 | */
|
---|
| 101 | errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
|
---|
| 102 | {
|
---|
| 103 | gfx_color_t *color = NULL;
|
---|
| 104 | gfx_coord2_t pos;
|
---|
| 105 | gfx_text_fmt_t fmt;
|
---|
| 106 | errno_t rc;
|
---|
| 107 |
|
---|
| 108 | rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
|
---|
| 109 | if (rc != EOK)
|
---|
| 110 | goto error;
|
---|
| 111 |
|
---|
| 112 | rc = gfx_set_color(pbutton->res->gc, color);
|
---|
| 113 | if (rc != EOK)
|
---|
| 114 | goto error;
|
---|
| 115 |
|
---|
| 116 | rc = gfx_fill_rect(pbutton->res->gc, &pbutton->rect);
|
---|
| 117 | if (rc != EOK)
|
---|
| 118 | goto error;
|
---|
| 119 |
|
---|
| 120 | gfx_color_delete(color);
|
---|
| 121 |
|
---|
| 122 | rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
|
---|
| 123 | if (rc != EOK)
|
---|
| 124 | goto error;
|
---|
| 125 |
|
---|
| 126 | rc = gfx_set_color(pbutton->res->gc, color);
|
---|
| 127 | if (rc != EOK)
|
---|
| 128 | goto error;
|
---|
| 129 |
|
---|
| 130 | /* Center of button rectangle */
|
---|
| 131 | pos.x = (pbutton->rect.p0.x + pbutton->rect.p1.x) / 2;
|
---|
| 132 | pos.y = (pbutton->rect.p0.y + pbutton->rect.p1.y) / 2;
|
---|
| 133 |
|
---|
| 134 | gfx_text_fmt_init(&fmt);
|
---|
| 135 | fmt.halign = gfx_halign_center;
|
---|
| 136 | fmt.valign = gfx_valign_center;
|
---|
| 137 |
|
---|
| 138 | rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
|
---|
| 139 | if (rc != EOK)
|
---|
| 140 | goto error;
|
---|
| 141 |
|
---|
| 142 | gfx_color_delete(color);
|
---|
| 143 |
|
---|
| 144 | return EOK;
|
---|
| 145 | error:
|
---|
| 146 | if (color != NULL)
|
---|
| 147 | gfx_color_delete(color);
|
---|
| 148 | return rc;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[f80690a] | 151 | /** @}
|
---|
| 152 | */
|
---|