Don't focus widget that doesn't have the WOP_SELECTABLE option.
[midnight-commander.git] / lib / widget / label.c
blob769a9c3ab24b9289a60d505fb2fe1f3d6666e94a
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Authors:
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
10 Jakub Jelinek, 1995
11 Andrej Borsenkow, 1996
12 Norbert Warmuth, 1997
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** \file label.c
32 * \brief Source: WLabel widget
35 #include <config.h>
37 #include <stdarg.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 * sender, widget_msg_t msg, int parm, void *data)
62 WLabel *l = LABEL (w);
63 WDialog *h = w->owner;
65 switch (msg)
67 case MSG_INIT:
68 return MSG_HANDLED;
70 case MSG_DRAW:
72 char *p = l->text;
73 int y = 0;
74 gboolean disabled;
75 align_crt_t align;
77 if (l->text == NULL)
78 return MSG_HANDLED;
80 disabled = widget_get_state (w, WST_DISABLED);
82 if (l->transparent)
83 tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
84 else
85 tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]);
87 align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
89 while (TRUE)
91 char *q;
92 char c = '\0';
95 q = strchr (p, '\n');
96 if (q != NULL)
98 c = q[0];
99 q[0] = '\0';
102 widget_move (w, y, 0);
103 tty_print_string (str_fit_to_term (p, w->cols, align));
105 if (q == NULL)
106 break;
108 q[0] = c;
109 p = q + 1;
110 y++;
112 return MSG_HANDLED;
115 case MSG_DESTROY:
116 g_free (l->text);
117 return MSG_HANDLED;
119 default:
120 return widget_default_callback (w, sender, msg, parm, data);
124 /* --------------------------------------------------------------------------------------------- */
125 /*** public functions ****************************************************************************/
126 /* --------------------------------------------------------------------------------------------- */
128 WLabel *
129 label_new (int y, int x, const char *text)
131 WLabel *l;
132 Widget *w;
133 int cols = 1;
134 int lines = 1;
136 if (text != NULL)
137 str_msg_term_size (text, &lines, &cols);
139 l = g_new (WLabel, 1);
140 w = WIDGET (l);
141 widget_init (w, y, x, lines, cols, label_callback, NULL);
143 l->text = g_strdup (text);
144 l->auto_adjust_cols = TRUE;
145 l->transparent = FALSE;
147 return l;
150 /* --------------------------------------------------------------------------------------------- */
152 void
153 label_set_text (WLabel * label, const char *text)
155 Widget *w = WIDGET (label);
156 int newcols = w->cols;
157 int newlines;
159 if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
160 return; /* Flickering is not nice */
162 g_free (label->text);
164 if (text == NULL)
165 label->text = NULL;
166 else
168 label->text = g_strdup (text);
169 if (label->auto_adjust_cols)
171 str_msg_term_size (text, &newlines, &newcols);
172 if (newcols > w->cols)
173 w->cols = newcols;
174 if (newlines > w->lines)
175 w->lines = newlines;
179 widget_redraw (w);
181 if (newcols < w->cols)
182 w->cols = newcols;
185 /* --------------------------------------------------------------------------------------------- */
187 void
188 label_set_textv (WLabel * label, const char *format, ...)
190 va_list args;
191 char buf[BUF_1K]; /* FIXME: is it enough? */
193 va_start (args, format);
194 g_vsnprintf (buf, sizeof (buf), format, args);
195 va_end (args);
197 label_set_text (label, buf);
200 /* --------------------------------------------------------------------------------------------- */