When launching a file from the finder, add the new file into the command line at...
[chocolate-doom.git] / textscreen / txt_label.c
blob7ae29c3d41422407679bedb0c1826711b18fd7e1
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 2006 Simon Howard
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 // 02111-1307, USA.
22 #include <stdlib.h>
23 #include <string.h>
25 #include "txt_label.h"
26 #include "txt_gui.h"
27 #include "txt_io.h"
28 #include "txt_main.h"
29 #include "txt_window.h"
31 static void TXT_LabelSizeCalc(TXT_UNCAST_ARG(label))
33 TXT_CAST_ARG(txt_label_t, label);
35 label->widget.w = label->w;
36 label->widget.h = label->h;
39 static void TXT_LabelDrawer(TXT_UNCAST_ARG(label), int selected)
41 TXT_CAST_ARG(txt_label_t, label);
42 unsigned int x, y;
43 int origin_x, origin_y;
44 unsigned int align_indent = 0;
45 unsigned int w;
47 w = label->widget.w;
49 TXT_BGColor(label->bgcolor, 0);
50 TXT_FGColor(label->fgcolor);
52 TXT_GetXY(&origin_x, &origin_y);
54 for (y=0; y<label->h; ++y)
56 // Calculate the amount to indent this line due to the align
57 // setting
59 switch (label->widget.align)
61 case TXT_HORIZ_LEFT:
62 align_indent = 0;
63 break;
64 case TXT_HORIZ_CENTER:
65 align_indent = (label->w - strlen(label->lines[y])) / 2;
66 break;
67 case TXT_HORIZ_RIGHT:
68 align_indent = label->w - strlen(label->lines[y]);
69 break;
72 // Draw this line
74 TXT_GotoXY(origin_x, origin_y + y);
76 // Gap at the start
78 for (x=0; x<align_indent; ++x)
80 TXT_DrawString(" ");
83 // The string itself
85 TXT_DrawString(label->lines[y]);
86 x += strlen(label->lines[y]);
88 // Gap at the end
90 for (; x<w; ++x)
92 TXT_DrawString(" ");
97 static void TXT_LabelDestructor(TXT_UNCAST_ARG(label))
99 TXT_CAST_ARG(txt_label_t, label);
101 free(label->label);
102 free(label->lines);
105 txt_widget_class_t txt_label_class =
107 TXT_LabelSizeCalc,
108 TXT_LabelDrawer,
109 NULL,
110 TXT_LabelDestructor,
111 NULL,
112 NULL,
115 void TXT_SetLabel(txt_label_t *label, char *value)
117 char *p;
118 unsigned int y;
120 // Free back the old label
122 free(label->label);
123 free(label->lines);
125 // Set the new value
127 label->label = strdup(value);
129 // Work out how many lines in this label
131 label->h = 1;
133 for (p = value; *p != '\0'; ++p)
135 if (*p == '\n')
137 ++label->h;
141 // Split into lines
143 label->lines = malloc(sizeof(char *) * label->h);
144 label->lines[0] = label->label;
145 y = 1;
147 for (p = label->label; *p != '\0'; ++p)
149 if (*p == '\n')
151 label->lines[y] = p + 1;
152 *p = '\0';
153 ++y;
157 label->w = 0;
159 for (y=0; y<label->h; ++y)
161 if (strlen(label->lines[y]) > label->w)
162 label->w = strlen(label->lines[y]);
166 txt_label_t *TXT_NewLabel(char *text)
168 txt_label_t *label;
170 label = malloc(sizeof(txt_label_t));
172 TXT_InitWidget(label, &txt_label_class);
173 label->widget.selectable = 0;
174 label->label = NULL;
175 label->lines = NULL;
177 // Default colors
179 label->bgcolor = TXT_COLOR_BLUE;
180 label->fgcolor = TXT_COLOR_BRIGHT_WHITE;
182 TXT_SetLabel(label, text);
184 return label;
187 void TXT_SetFGColor(txt_label_t *label, txt_color_t color)
189 label->fgcolor = color;
192 void TXT_SetBGColor(txt_label_t *label, txt_color_t color)
194 label->bgcolor = color;