Updated Russian translation.
[midnight-commander.git] / lib / widget / label.c
blob1e05f51b60ee8de831627d15c053379e2882e53d
1 /* Widgets for the Midnight Commander
3 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
6 Authors: 1994, 1995 Radek Doulik
7 1994, 1995 Miguel de Icaza
8 1995 Jakub Jelinek
9 1996 Andrej Borsenkow
10 1997 Norbert Warmuth
11 2009, 2010 Andrew Borodin
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 /** \file label.c
30 * \brief Source: WLabel widget
33 #include <config.h>
35 #include <stdlib.h>
36 #include <string.h>
38 #include "lib/global.h"
40 #include "lib/tty/tty.h"
41 #include "lib/tty/color.h"
42 #include "lib/skin.h"
43 #include "lib/strutil.h"
44 #include "lib/widget.h"
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 /*** file scope functions ************************************************************************/
56 static cb_ret_t
57 label_callback (Widget * w, widget_msg_t msg, int parm)
59 WLabel *l = (WLabel *) w;
60 Dlg_head *h = l->widget.owner;
62 switch (msg)
64 case WIDGET_INIT:
65 return MSG_HANDLED;
67 /* We don't want to get the focus */
68 case WIDGET_FOCUS:
69 return MSG_NOT_HANDLED;
71 case WIDGET_DRAW:
73 char *p = l->text;
74 int y = 0;
75 gboolean disabled = (w->options & W_DISABLED) != 0;
77 if (l->text == NULL)
78 return MSG_HANDLED;
80 if (l->transparent)
81 tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
82 else
83 tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]);
85 while (TRUE)
87 char *q;
88 char c = '\0';
90 q = strchr (p, '\n');
91 if (q != NULL)
93 c = q[0];
94 q[0] = '\0';
97 widget_move (&l->widget, y, 0);
98 tty_print_string (str_fit_to_term (p, l->widget.cols, J_LEFT));
100 if (q == NULL)
101 break;
103 q[0] = c;
104 p = q + 1;
105 y++;
107 return MSG_HANDLED;
110 case WIDGET_DESTROY:
111 g_free (l->text);
112 return MSG_HANDLED;
114 default:
115 return default_proc (msg, parm);
119 /* --------------------------------------------------------------------------------------------- */
120 /*** public functions ****************************************************************************/
121 /* --------------------------------------------------------------------------------------------- */
123 WLabel *
124 label_new (int y, int x, const char *text)
126 WLabel *l;
127 int cols = 1;
128 int lines = 1;
130 if (text != NULL)
131 str_msg_term_size (text, &lines, &cols);
133 l = g_new (WLabel, 1);
134 init_widget (&l->widget, y, x, lines, cols, label_callback, NULL);
135 l->text = g_strdup (text);
136 l->auto_adjust_cols = TRUE;
137 l->transparent = FALSE;
138 widget_want_cursor (l->widget, FALSE);
139 widget_want_hotkey (l->widget, FALSE);
141 return l;
144 /* --------------------------------------------------------------------------------------------- */
146 void
147 label_set_text (WLabel * label, const char *text)
149 int newcols = label->widget.cols;
150 int newlines;
152 if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
153 return; /* Flickering is not nice */
155 g_free (label->text);
157 if (text == NULL)
158 label->text = NULL;
159 else
161 label->text = g_strdup (text);
162 if (label->auto_adjust_cols)
164 str_msg_term_size (text, &newlines, &newcols);
165 if (newcols > label->widget.cols)
166 label->widget.cols = newcols;
167 if (newlines > label->widget.lines)
168 label->widget.lines = newlines;
172 if (label->widget.owner != NULL)
173 label_callback ((Widget *) label, WIDGET_DRAW, 0);
175 if (newcols < label->widget.cols)
176 label->widget.cols = newcols;
179 /* --------------------------------------------------------------------------------------------- */