| 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_LABEL_H
|
|---|
| 30 | #define UI_LABEL_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/label.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "window.h"
|
|---|
| 46 |
|
|---|
| 47 | typedef struct sLabel Label;
|
|---|
| 48 | struct sLabel
|
|---|
| 49 | {
|
|---|
| 50 | char* name;
|
|---|
| 51 | char* text;
|
|---|
| 52 |
|
|---|
| 53 | size_t max_length;
|
|---|
| 54 |
|
|---|
| 55 | ui_fixed_t *fixed;
|
|---|
| 56 | ui_label_t *label;
|
|---|
| 57 |
|
|---|
| 58 | gfx_rect_t rect;
|
|---|
| 59 |
|
|---|
| 60 | void (*set_text)(Label* w, char* text);
|
|---|
| 61 | char* (*get_text)(Label* w);
|
|---|
| 62 |
|
|---|
| 63 | void (*update_rect)(Label* w);
|
|---|
| 64 | void (*paint)(Label* w);
|
|---|
| 65 |
|
|---|
| 66 | void (*set_horizontal_align)(Label* w, gfx_halign_t a);
|
|---|
| 67 | void (*set_vertical_align)(Label* w, gfx_halign_t a);
|
|---|
| 68 | void (*destroy)(Label* w);
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | int init_label(Label* w, const char * name, Window* window);
|
|---|
| 72 | void label_set_text(Label* w, char* text);
|
|---|
| 73 | char* label_get_text(Label* w);
|
|---|
| 74 | void label_update_rect(Label* w);
|
|---|
| 75 | void label_paint(Label* w);
|
|---|
| 76 | void label_set_horizontal_align(Label* w, gfx_halign_t a);
|
|---|
| 77 | void label_set_vertical_align(Label* w, gfx_halign_t a);
|
|---|
| 78 | void label_destroy(Label* w);
|
|---|
| 79 |
|
|---|
| 80 | void label_set_text(Label* w, char* text)
|
|---|
| 81 | {
|
|---|
| 82 | //free(&w->text);
|
|---|
| 83 | size_t szt = str_size(text) + 1;
|
|---|
| 84 | if ( szt > w->max_length )
|
|---|
| 85 | szt = w->max_length;
|
|---|
| 86 | w->text = malloc(sizeof(char) * szt);
|
|---|
| 87 | str_cpy(w->text, szt, text);
|
|---|
| 88 | ui_label_set_text(w->label, w->text);
|
|---|
| 89 | if(true)
|
|---|
| 90 | ui_label_paint(w->label);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | char* label_get_text(Label* w)
|
|---|
| 94 | {
|
|---|
| 95 | return w->text;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | void label_update_rect(Label* w)
|
|---|
| 99 | {
|
|---|
| 100 | ui_label_set_rect(w->label, &w->rect);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void label_paint(Label* w)
|
|---|
| 104 | {
|
|---|
| 105 | ui_label_paint(w->label);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | void label_set_horizontal_align(Label* w, gfx_halign_t a)
|
|---|
| 109 | {
|
|---|
| 110 | ui_label_set_halign(w->label, a);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | void label_set_vertical_align(Label* w, gfx_halign_t a)
|
|---|
| 114 | {
|
|---|
| 115 | ui_label_set_valign(w->label, a);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | void label_destroy(Label* w)
|
|---|
| 119 | {
|
|---|
| 120 | ui_label_destroy(w->label);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | int init_label(Label* w, const char * name, Window* window)
|
|---|
| 124 | {
|
|---|
| 125 | w->max_length = 50;
|
|---|
| 126 | w->text = malloc(sizeof(char) * 6);
|
|---|
| 127 | str_cpy(w->text, 6, (char*) "Label1");
|
|---|
| 128 |
|
|---|
| 129 | errno_t rc = ui_label_create(window->ui_res, w->text, &w->label);
|
|---|
| 130 | if (rc != EOK) {
|
|---|
| 131 | printf("Error creating label.\n");
|
|---|
| 132 | return rc;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /*w->rect.p0.x = 0;
|
|---|
| 136 | w->rect.p0.y = 0;
|
|---|
| 137 | w->rect.p1.x = 0;
|
|---|
| 138 | w->rect.p1.y = 0;*/
|
|---|
| 139 | ui_label_set_rect(w->label, &w->rect);
|
|---|
| 140 | ui_label_set_halign(w->label, gfx_halign_left);
|
|---|
| 141 |
|
|---|
| 142 | rc = ui_fixed_add(window->fixed, ui_label_ctl(w->label));
|
|---|
| 143 | if (rc != EOK) {
|
|---|
| 144 | printf("Error adding control to layout.\n");
|
|---|
| 145 | return rc;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | w->set_text = label_set_text;
|
|---|
| 149 | w->get_text = label_get_text;
|
|---|
| 150 | w->update_rect = label_update_rect;
|
|---|
| 151 | w->paint = label_paint;
|
|---|
| 152 |
|
|---|
| 153 | w->set_horizontal_align = label_set_horizontal_align;
|
|---|
| 154 | w->set_vertical_align = label_set_vertical_align;
|
|---|
| 155 |
|
|---|
| 156 | w->destroy = label_destroy;
|
|---|
| 157 |
|
|---|
| 158 | return EOK;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | #endif
|
|---|