2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Widgets for the Midnight Commander
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 1994-2024
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
Authors:
|
|
|
|
Radek Doulik, 1994, 1995
|
|
|
|
Miguel de Icaza, 1994, 1995
|
|
|
|
Jakub Jelinek, 1995
|
|
|
|
Andrej Borsenkow, 1996
|
|
|
|
Norbert Warmuth, 1997
|
2022-05-01 10:43:33 +03:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009-2022
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander 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 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2010-11-12 11:03:57 +03:00
|
|
|
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
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-11-12 11:03:57 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file label.c
|
|
|
|
* \brief Source: WLabel widget
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2013-08-07 10:24:30 +04:00
|
|
|
#include <stdarg.h>
|
2010-11-12 11:03:57 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
|
|
|
|
#include "lib/tty/tty.h"
|
|
|
|
#include "lib/tty/color.h"
|
|
|
|
#include "lib/skin.h"
|
|
|
|
#include "lib/strutil.h"
|
|
|
|
#include "lib/widget.h"
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-12 11:03:57 +03:00
|
|
|
/*** file scope functions ************************************************************************/
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
static cb_ret_t
|
2024-06-01 21:12:14 +03:00
|
|
|
label_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2012-10-11 12:25:07 +04:00
|
|
|
WLabel *l = LABEL (w);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
switch (msg)
|
|
|
|
{
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_DRAW:
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
|
|
|
char *p = l->text;
|
|
|
|
int y = 0;
|
2012-09-26 15:19:33 +04:00
|
|
|
gboolean disabled;
|
|
|
|
align_crt_t align;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
if (l->text == NULL)
|
|
|
|
return MSG_HANDLED;
|
|
|
|
|
2016-04-10 12:26:48 +03:00
|
|
|
disabled = widget_get_state (w, WST_DISABLED);
|
2012-09-26 15:19:33 +04:00
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
if (l->transparent)
|
|
|
|
tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
|
|
|
|
else
|
2019-11-17 17:58:52 +03:00
|
|
|
{
|
|
|
|
const int *colors;
|
|
|
|
|
|
|
|
colors = widget_get_colors (w);
|
|
|
|
tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL]);
|
|
|
|
}
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2012-09-26 15:19:33 +04:00
|
|
|
align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
|
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
char *q;
|
2010-12-01 15:16:54 +03:00
|
|
|
char c = '\0';
|
2010-11-12 11:03:57 +03:00
|
|
|
|
2012-09-26 15:19:33 +04:00
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
q = strchr (p, '\n');
|
|
|
|
if (q != NULL)
|
|
|
|
{
|
|
|
|
c = q[0];
|
|
|
|
q[0] = '\0';
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:07:24 +03:00
|
|
|
widget_gotoyx (w, y, 0);
|
2022-05-01 10:43:33 +03:00
|
|
|
tty_print_string (str_fit_to_term (p, w->rect.cols, align));
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
if (q == NULL)
|
|
|
|
break;
|
2010-12-01 15:16:54 +03:00
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
q[0] = c;
|
|
|
|
p = q + 1;
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
return MSG_HANDLED;
|
|
|
|
}
|
|
|
|
|
2012-09-28 15:05:43 +04:00
|
|
|
case MSG_DESTROY:
|
2010-11-12 11:03:57 +03:00
|
|
|
g_free (l->text);
|
|
|
|
return MSG_HANDLED;
|
|
|
|
|
|
|
|
default:
|
2012-09-28 15:05:43 +04:00
|
|
|
return widget_default_callback (w, sender, msg, parm, data);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
WLabel *
|
|
|
|
label_new (int y, int x, const char *text)
|
|
|
|
{
|
2022-05-21 16:11:06 +03:00
|
|
|
WRect r = { y, x, 1, 1 };
|
2010-11-12 11:03:57 +03:00
|
|
|
WLabel *l;
|
2012-06-20 15:09:44 +04:00
|
|
|
Widget *w;
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
if (text != NULL)
|
2022-05-21 16:11:06 +03:00
|
|
|
str_msg_term_size (text, &r.lines, &r.cols);
|
2010-11-12 11:03:57 +03:00
|
|
|
|
|
|
|
l = g_new (WLabel, 1);
|
2012-06-20 15:09:44 +04:00
|
|
|
w = WIDGET (l);
|
2022-05-21 16:11:06 +03:00
|
|
|
widget_init (w, &r, label_callback, NULL);
|
2012-06-20 15:09:44 +04:00
|
|
|
|
2010-11-12 11:03:57 +03:00
|
|
|
l->text = g_strdup (text);
|
|
|
|
l->auto_adjust_cols = TRUE;
|
|
|
|
l->transparent = FALSE;
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
label_set_text (WLabel *label, const char *text)
|
2010-11-12 11:03:57 +03:00
|
|
|
{
|
2012-06-20 15:09:44 +04:00
|
|
|
Widget *w = WIDGET (label);
|
2022-05-01 10:43:33 +03:00
|
|
|
int newcols = w->rect.cols;
|
2010-11-12 11:03:57 +03:00
|
|
|
int newlines;
|
|
|
|
|
|
|
|
if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
|
|
|
|
return; /* Flickering is not nice */
|
|
|
|
|
|
|
|
g_free (label->text);
|
|
|
|
|
|
|
|
if (text == NULL)
|
|
|
|
label->text = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
label->text = g_strdup (text);
|
|
|
|
if (label->auto_adjust_cols)
|
|
|
|
{
|
|
|
|
str_msg_term_size (text, &newlines, &newcols);
|
2022-05-01 10:43:33 +03:00
|
|
|
w->rect.cols = MAX (newcols, w->rect.cols);
|
|
|
|
w->rect.lines = MAX (newlines, w->rect.lines);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:44:57 +03:00
|
|
|
widget_draw (w);
|
2013-04-05 11:26:48 +04:00
|
|
|
|
2022-05-01 10:43:33 +03:00
|
|
|
w->rect.cols = MIN (newcols, w->rect.cols);
|
2010-11-12 11:03:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2013-08-07 10:24:30 +04:00
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
label_set_textv (WLabel *label, const char *format, ...)
|
2013-08-07 10:24:30 +04:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char buf[BUF_1K]; /* FIXME: is it enough? */
|
|
|
|
|
|
|
|
va_start (args, format);
|
|
|
|
g_vsnprintf (buf, sizeof (buf), format, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
label_set_text (label, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|