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
|
//
// Copyright(C) 2005-2014 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
#include <stdlib.h>
#include <string.h>
#include "txt_label.h"
#include "txt_gui.h"
#include "txt_io.h"
#include "txt_main.h"
#include "txt_utf8.h"
#include "txt_window.h"
static void TXT_LabelSizeCalc(TXT_UNCAST_ARG(label))
{
TXT_CAST_ARG(txt_label_t, label);
label->widget.w = label->w;
label->widget.h = label->h;
}
static void TXT_LabelDrawer(TXT_UNCAST_ARG(label))
{
TXT_CAST_ARG(txt_label_t, label);
unsigned int x, y;
int origin_x, origin_y;
unsigned int align_indent = 0;
unsigned int w, sw;
w = label->widget.w;
if (label->bgcolor >= 0)
{
TXT_BGColor(label->bgcolor, 0);
}
if (label->fgcolor >= 0)
{
TXT_FGColor(label->fgcolor);
}
TXT_GetXY(&origin_x, &origin_y);
for (y=0; y<label->h; ++y)
{
// Calculate the amount to indent this line due to the align
// setting
sw = TXT_UTF8_Strlen(label->lines[y]);
switch (label->widget.align)
{
case TXT_HORIZ_LEFT:
align_indent = 0;
break;
case TXT_HORIZ_CENTER:
align_indent = (label->w - sw) / 2;
break;
case TXT_HORIZ_RIGHT:
align_indent = label->w - sw;
break;
}
// Draw this line
TXT_GotoXY(origin_x, origin_y + y);
// Gap at the start
for (x=0; x<align_indent; ++x)
{
TXT_DrawString(" ");
}
// The string itself
TXT_DrawString(label->lines[y]);
x += sw;
// Gap at the end
for (; x<w; ++x)
{
TXT_DrawString(" ");
}
}
}
static void TXT_LabelDestructor(TXT_UNCAST_ARG(label))
{
TXT_CAST_ARG(txt_label_t, label);
free(label->label);
free(label->lines);
}
txt_widget_class_t txt_label_class =
{
TXT_NeverSelectable,
TXT_LabelSizeCalc,
TXT_LabelDrawer,
NULL,
TXT_LabelDestructor,
NULL,
NULL,
};
void TXT_SetLabel(txt_label_t *label, const char *value)
{
char *p;
unsigned int y;
// Free back the old label
free(label->label);
free(label->lines);
// Set the new value
label->label = strdup(value);
// Work out how many lines in this label
label->h = 1;
for (p = label->label; *p != '\0'; ++p)
{
if (*p == '\n')
{
++label->h;
}
}
// Split into lines
label->lines = malloc(sizeof(char *) * label->h);
label->lines[0] = label->label;
y = 1;
for (p = label->label; *p != '\0'; ++p)
{
if (*p == '\n')
{
label->lines[y] = p + 1;
*p = '\0';
++y;
}
}
label->w = 0;
for (y=0; y<label->h; ++y)
{
unsigned int line_len;
line_len = TXT_UTF8_Strlen(label->lines[y]);
if (line_len > label->w)
label->w = line_len;
}
}
txt_label_t *TXT_NewLabel(const char *text)
{
txt_label_t *label;
label = malloc(sizeof(txt_label_t));
TXT_InitWidget(label, &txt_label_class);
label->label = NULL;
label->lines = NULL;
// Default colors
label->bgcolor = -1;
label->fgcolor = -1;
TXT_SetLabel(label, text);
return label;
}
void TXT_SetFGColor(txt_label_t *label, txt_color_t color)
{
label->fgcolor = color;
}
void TXT_SetBGColor(txt_label_t *label, txt_color_t color)
{
label->bgcolor = color;
}
|