diff options
author | Simon Howard | 2006-05-20 21:36:28 +0000 |
---|---|---|
committer | Simon Howard | 2006-05-20 21:36:28 +0000 |
commit | 27307fa2d7cca6081fa16ecc01e0c3977b58c088 (patch) | |
tree | 9a04c2a1bb1221b98de48a600c3795189491dae5 | |
parent | 56912a4dcca6f35f9a75fff2bccc59949acd2197 (diff) | |
download | chocolate-doom-27307fa2d7cca6081fa16ecc01e0c3977b58c088.tar.gz chocolate-doom-27307fa2d7cca6081fa16ecc01e0c3977b58c088.tar.bz2 chocolate-doom-27307fa2d7cca6081fa16ecc01e0c3977b58c088.zip |
Add label class.
Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 494
-rw-r--r-- | textscreen/Makefile.am | 1 | ||||
-rw-r--r-- | textscreen/txt_label.c | 110 | ||||
-rw-r--r-- | textscreen/txt_label.h | 44 |
3 files changed, 155 insertions, 0 deletions
diff --git a/textscreen/Makefile.am b/textscreen/Makefile.am index 90949e5c..14a6b076 100644 --- a/textscreen/Makefile.am +++ b/textscreen/Makefile.am @@ -11,6 +11,7 @@ libtextscreen_a_SOURCES = \ txt_io.c txt_io.h \ txt_main.c txt_main.h \ txt_button.c txt_button.h \ + txt_label.c txt_label.h \ txt_separator.c txt_separator.h\ txt_table.c txt_table.h \ txt_widget.c txt_widget.h \ diff --git a/textscreen/txt_label.c b/textscreen/txt_label.c new file mode 100644 index 00000000..0534f10a --- /dev/null +++ b/textscreen/txt_label.c @@ -0,0 +1,110 @@ + +#include <string.h> + +#include "txt_label.h" +#include "txt_io.h" +#include "txt_main.h" +#include "txt_widget.h" +#include "txt_window.h" + +static void TXT_LabelSizeCalc(txt_widget_t *widget, int *w, int *h) +{ + txt_label_t *label = (txt_label_t *) widget; + + *w = label->w; + *h = label->h; +} + +static void TXT_LabelDrawer(txt_widget_t *widget, int w, int selected) +{ + txt_label_t *label = (txt_label_t *) widget; + int i; + int origin_x, origin_y; + + TXT_BGColor(TXT_COLOR_BLUE, 0); + TXT_FGColor(TXT_COLOR_BRIGHT_WHITE); + + TXT_GetXY(&origin_x, &origin_y); + + for (i=0; i<label->h; ++i) + { + TXT_GotoXY(origin_x, origin_y + i); + TXT_DrawString(label->lines[i]); + } +} + +static void TXT_LabelDestructor(txt_widget_t *widget) +{ + txt_label_t *label = (txt_label_t *) widget; + + free(label->label); + free(label->lines); + free(label); +} + +txt_widget_class_t txt_label_class = +{ + TXT_LabelSizeCalc, + TXT_LabelDrawer, + NULL, + TXT_LabelDestructor, +}; + +static void TXT_SplitLabel(txt_label_t *label) +{ + char *p; + int y; + + // 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) + { + if (strlen(label->lines[y]) > label->w) + label->w = strlen(label->lines[y]); + } +} + +txt_label_t *TXT_NewLabel(char *text) +{ + txt_label_t *label; + + label = malloc(sizeof(txt_label_t)); + + label->widget.widget_class = &txt_label_class; + label->widget.selectable = 0; + label->widget.visible = 1; + label->label = strdup(text); + + TXT_SplitLabel(label); + + return label; +} + diff --git a/textscreen/txt_label.h b/textscreen/txt_label.h new file mode 100644 index 00000000..80be506a --- /dev/null +++ b/textscreen/txt_label.h @@ -0,0 +1,44 @@ +// Emacs style mode select -*- C++ -*- +//----------------------------------------------------------------------------- +// +// $Id$ +// +// Copyright(C) 1993-1996 Id Software, Inc. +// Copyright(C) 2006 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. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. +// + +#ifndef TXT_LABEL_H +#define TXT_LABEL_H + +typedef struct txt_label_s txt_label_t; + +#include "txt_widget.h" + +struct txt_label_s +{ + txt_widget_t widget; + char *label; + char **lines; + int w, h; +}; + +txt_label_t *TXT_NewLabel(char *label); + +#endif /* #ifndef TXT_LABEL_H */ + + |