Ticket 1551: Update GPL version from 2 to 3
[midnight-commander.git] / lib / widget / label.c
blobf26a7050aec6071023a5a1649360de405b98fe12
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2006, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Authors:
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1996
13 Norbert Warmuth, 1997
14 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
16 This file is part of the Midnight Commander.
18 The Midnight Commander is free software: you can redistribute it
19 and/or modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation, either version 3 of the License,
21 or (at your option) any later version.
23 The Midnight Commander is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 /** \file label.c
33 * \brief Source: WLabel widget
36 #include <config.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include "lib/global.h"
43 #include "lib/tty/tty.h"
44 #include "lib/tty/color.h"
45 #include "lib/skin.h"
46 #include "lib/strutil.h"
47 #include "lib/widget.h"
49 /*** global variables ****************************************************************************/
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
59 static cb_ret_t
60 label_callback (Widget * w, widget_msg_t msg, int parm)
62 WLabel *l = (WLabel *) w;
63 Dlg_head *h = l->widget.owner;
65 switch (msg)
67 case WIDGET_INIT:
68 return MSG_HANDLED;
70 /* We don't want to get the focus */
71 case WIDGET_FOCUS:
72 return MSG_NOT_HANDLED;
74 case WIDGET_DRAW:
76 char *p = l->text;
77 int y = 0;
78 gboolean disabled = (w->options & W_DISABLED) != 0;
80 if (l->text == NULL)
81 return MSG_HANDLED;
83 if (l->transparent)
84 tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
85 else
86 tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]);
88 while (TRUE)
90 char *q;
91 char c = '\0';
93 q = strchr (p, '\n');
94 if (q != NULL)
96 c = q[0];
97 q[0] = '\0';
100 widget_move (&l->widget, y, 0);
101 tty_print_string (str_fit_to_term (p, l->widget.cols, J_LEFT));
103 if (q == NULL)
104 break;
106 q[0] = c;
107 p = q + 1;
108 y++;
110 return MSG_HANDLED;
113 case WIDGET_DESTROY:
114 g_free (l->text);
115 return MSG_HANDLED;
117 default:
118 return default_proc (msg, parm);
122 /* --------------------------------------------------------------------------------------------- */
123 /*** public functions ****************************************************************************/
124 /* --------------------------------------------------------------------------------------------- */
126 WLabel *
127 label_new (int y, int x, const char *text)
129 WLabel *l;
130 int cols = 1;
131 int lines = 1;
133 if (text != NULL)
134 str_msg_term_size (text, &lines, &cols);
136 l = g_new (WLabel, 1);
137 init_widget (&l->widget, y, x, lines, cols, label_callback, NULL);
138 l->text = g_strdup (text);
139 l->auto_adjust_cols = TRUE;
140 l->transparent = FALSE;
141 widget_want_cursor (l->widget, FALSE);
142 widget_want_hotkey (l->widget, FALSE);
144 return l;
147 /* --------------------------------------------------------------------------------------------- */
149 void
150 label_set_text (WLabel * label, const char *text)
152 int newcols = label->widget.cols;
153 int newlines;
155 if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
156 return; /* Flickering is not nice */
158 g_free (label->text);
160 if (text == NULL)
161 label->text = NULL;
162 else
164 label->text = g_strdup (text);
165 if (label->auto_adjust_cols)
167 str_msg_term_size (text, &newlines, &newcols);
168 if (newcols > label->widget.cols)
169 label->widget.cols = newcols;
170 if (newlines > label->widget.lines)
171 label->widget.lines = newlines;
175 if (label->widget.owner != NULL)
176 label_callback ((Widget *) label, WIDGET_DRAW, 0);
178 if (newcols < label->widget.cols)
179 label->widget.cols = newcols;
182 /* --------------------------------------------------------------------------------------------- */