2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
11 Andrej Borsenkow, 1996
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/>.
32 * \brief Source: WLabel widget
41 #include "lib/global.h"
43 #include "lib/tty/tty.h"
44 #include "lib/tty/color.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 ************************************************************************/
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
;
70 /* We don't want to get the focus */
72 return MSG_NOT_HANDLED
;
84 disabled
= (w
->options
& W_DISABLED
) != 0;
87 tty_setcolor (disabled
? DISABLED_COLOR
: DEFAULT_COLOR
);
89 tty_setcolor (disabled
? DISABLED_COLOR
: h
->color
[DLG_COLOR_NORMAL
]);
91 align
= (w
->pos_flags
& WPOS_CENTER_HORZ
) != 0 ? J_CENTER_LEFT
: J_LEFT
;
106 widget_move (w
, y
, 0);
107 tty_print_string (str_fit_to_term (p
, w
->cols
, align
));
124 return widget_default_callback (w
, sender
, msg
, parm
, data
);
128 /* --------------------------------------------------------------------------------------------- */
129 /*** public functions ****************************************************************************/
130 /* --------------------------------------------------------------------------------------------- */
133 label_new (int y
, int x
, const char *text
)
141 str_msg_term_size (text
, &lines
, &cols
);
143 l
= g_new (WLabel
, 1);
145 widget_init (w
, y
, x
, lines
, cols
, label_callback
, NULL
);
147 l
->text
= g_strdup (text
);
148 l
->auto_adjust_cols
= TRUE
;
149 l
->transparent
= FALSE
;
150 widget_want_cursor (w
, FALSE
);
151 widget_want_hotkey (w
, FALSE
);
156 /* --------------------------------------------------------------------------------------------- */
159 label_set_text (WLabel
* label
, const char *text
)
161 Widget
*w
= WIDGET (label
);
162 int newcols
= w
->cols
;
165 if (label
->text
!= NULL
&& text
!= NULL
&& strcmp (label
->text
, text
) == 0)
166 return; /* Flickering is not nice */
168 g_free (label
->text
);
174 label
->text
= g_strdup (text
);
175 if (label
->auto_adjust_cols
)
177 str_msg_term_size (text
, &newlines
, &newcols
);
178 if (newcols
> w
->cols
)
180 if (newlines
> w
->lines
)
187 if (newcols
< w
->cols
)
191 /* --------------------------------------------------------------------------------------------- */
194 label_set_textv (WLabel
* label
, const char *format
, ...)
197 char buf
[BUF_1K
]; /* FIXME: is it enough? */
199 va_start (args
, format
);
200 g_vsnprintf (buf
, sizeof (buf
), format
, args
);
203 label_set_text (label
, buf
);
206 /* --------------------------------------------------------------------------------------------- */