shithub: choc

Download patch

ref: 27307fa2d7cca6081fa16ecc01e0c3977b58c088
parent: 56912a4dcca6f35f9a75fff2bccc59949acd2197
author: Simon Howard <[email protected]>
date: Sat May 20 17:36:28 EDT 2006

Add label class.

Subversion-branch: /trunk/chocolate-doom
Subversion-revision: 494

--- a/textscreen/Makefile.am
+++ b/textscreen/Makefile.am
@@ -11,6 +11,7 @@
 	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     \
--- /dev/null
+++ b/textscreen/txt_label.c
@@ -1,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;
+}
+
--- /dev/null
+++ b/textscreen/txt_label.h
@@ -1,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 */
+
+