[ba09d06] | 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 Label
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <gfx/context.h>
|
---|
| 38 | #include <gfx/render.h>
|
---|
| 39 | #include <gfx/text.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
| 41 | #include <str.h>
|
---|
| 42 | #include <ui/paint.h>
|
---|
| 43 | #include <ui/label.h>
|
---|
| 44 | #include "../private/label.h"
|
---|
| 45 | #include "../private/resource.h"
|
---|
| 46 |
|
---|
| 47 | /** Create new label.
|
---|
| 48 | *
|
---|
| 49 | * @param resource UI resource
|
---|
| 50 | * @param text Text
|
---|
| 51 | * @param rlabel Place to store pointer to new label
|
---|
| 52 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 53 | */
|
---|
| 54 | errno_t ui_label_create(ui_resource_t *resource, const char *text,
|
---|
| 55 | ui_label_t **rlabel)
|
---|
| 56 | {
|
---|
| 57 | ui_label_t *label;
|
---|
| 58 |
|
---|
| 59 | label = calloc(1, sizeof(ui_label_t));
|
---|
| 60 | if (label == NULL)
|
---|
| 61 | return ENOMEM;
|
---|
| 62 |
|
---|
| 63 | label->text = str_dup(text);
|
---|
| 64 | if (label->text == NULL) {
|
---|
| 65 | free(label);
|
---|
| 66 | return ENOMEM;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | label->res = resource;
|
---|
[58a67050] | 70 | label->halign = gfx_halign_left;
|
---|
[ba09d06] | 71 | *rlabel = label;
|
---|
| 72 | return EOK;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /** Destroy label.
|
---|
| 76 | *
|
---|
| 77 | * @param label Label or @c NULL
|
---|
| 78 | */
|
---|
| 79 | void ui_label_destroy(ui_label_t *label)
|
---|
| 80 | {
|
---|
| 81 | if (label == NULL)
|
---|
| 82 | return;
|
---|
| 83 |
|
---|
| 84 | free(label);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /** Set label rectangle.
|
---|
| 88 | *
|
---|
| 89 | * @param label Label
|
---|
| 90 | * @param rect New label rectangle
|
---|
| 91 | */
|
---|
| 92 | void ui_label_set_rect(ui_label_t *label, gfx_rect_t *rect)
|
---|
| 93 | {
|
---|
| 94 | label->rect = *rect;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[58a67050] | 97 | /** Set label horizontal text alignment.
|
---|
| 98 | *
|
---|
| 99 | * @param label Label
|
---|
| 100 | * @param halign Horizontal alignment
|
---|
| 101 | */
|
---|
| 102 | void ui_label_set_halign(ui_label_t *label, gfx_halign_t halign)
|
---|
| 103 | {
|
---|
| 104 | label->halign = halign;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[ba09d06] | 107 | /** Set label text.
|
---|
| 108 | *
|
---|
| 109 | * @param label Label
|
---|
| 110 | * @param text New label text
|
---|
| 111 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 112 | */
|
---|
| 113 | errno_t ui_label_set_text(ui_label_t *label, const char *text)
|
---|
| 114 | {
|
---|
| 115 | char *tcopy;
|
---|
| 116 |
|
---|
| 117 | tcopy = str_dup(text);
|
---|
| 118 | if (tcopy == NULL)
|
---|
| 119 | return ENOMEM;
|
---|
| 120 |
|
---|
| 121 | free(label->text);
|
---|
| 122 | label->text = tcopy;
|
---|
| 123 |
|
---|
| 124 | return EOK;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /** Paint label.
|
---|
| 128 | *
|
---|
| 129 | * @param label Label
|
---|
| 130 | * @return EOK on success or an error code
|
---|
| 131 | */
|
---|
| 132 | errno_t ui_label_paint(ui_label_t *label)
|
---|
| 133 | {
|
---|
| 134 | gfx_text_fmt_t fmt;
|
---|
| 135 | gfx_coord2_t pos;
|
---|
| 136 | errno_t rc;
|
---|
| 137 |
|
---|
| 138 | /* Paint label background */
|
---|
| 139 |
|
---|
| 140 | rc = gfx_set_color(label->res->gc, label->res->wnd_face_color);
|
---|
| 141 | if (rc != EOK)
|
---|
| 142 | goto error;
|
---|
| 143 |
|
---|
| 144 | rc = gfx_fill_rect(label->res->gc, &label->rect);
|
---|
| 145 | if (rc != EOK)
|
---|
| 146 | goto error;
|
---|
| 147 |
|
---|
[58a67050] | 148 | switch (label->halign) {
|
---|
| 149 | case gfx_halign_left:
|
---|
| 150 | case gfx_halign_justify:
|
---|
| 151 | pos.x = label->rect.p0.x;
|
---|
| 152 | break;
|
---|
| 153 | case gfx_halign_center:
|
---|
| 154 | pos.x = (label->rect.p0.x + label->rect.p1.x) / 2;
|
---|
| 155 | break;
|
---|
| 156 | case gfx_halign_right:
|
---|
| 157 | pos.y = label->rect.p1.x;
|
---|
| 158 | break;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | pos.y = label->rect.p0.y;
|
---|
[ba09d06] | 162 |
|
---|
| 163 | gfx_text_fmt_init(&fmt);
|
---|
[58a67050] | 164 | fmt.halign = label->halign;
|
---|
[ba09d06] | 165 | fmt.valign = gfx_valign_top;
|
---|
| 166 |
|
---|
| 167 | rc = gfx_set_color(label->res->gc, label->res->wnd_text_color);
|
---|
| 168 | if (rc != EOK)
|
---|
| 169 | goto error;
|
---|
| 170 |
|
---|
| 171 | rc = gfx_puttext(label->res->font, &pos, &fmt, label->text);
|
---|
| 172 | if (rc != EOK)
|
---|
| 173 | goto error;
|
---|
| 174 |
|
---|
| 175 | return EOK;
|
---|
| 176 | error:
|
---|
| 177 | return rc;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /** @}
|
---|
| 181 | */
|
---|