Merge branch '4524_cleanup'
[midnight-commander.git] / lib / widget / hline.c
blob0f77a84fbf84f60fa2afcf09633fc35219644f36
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2024
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-2022
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 hline.c
32 * \brief Source: WHLine widget (horizontal line)
35 #include <config.h>
37 #include <stdarg.h>
38 #include <stdlib.h>
40 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/tty/color.h"
43 #include "lib/skin.h"
44 #include "lib/strutil.h"
45 #include "lib/widget.h"
47 /*** global variables ****************************************************************************/
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** forward declarations (file scope functions) *************************************************/
55 /*** file scope variables ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
61 static void
62 hline_adjust_cols (WHLine * l)
64 if (l->auto_adjust_cols)
66 Widget *wl = WIDGET (l);
67 const Widget *o = CONST_WIDGET (wl->owner);
68 WRect *w = &wl->rect;
69 const WRect *wo = &o->rect;
71 if (CONST_DIALOG (o)->compact)
73 w->x = wo->x;
74 w->cols = wo->cols;
76 else
78 w->x = wo->x + 1;
79 w->cols = wo->cols - 2;
84 /* --------------------------------------------------------------------------------------------- */
86 static cb_ret_t
87 hline_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
89 WHLine *l = HLINE (w);
91 switch (msg)
93 case MSG_INIT:
94 hline_adjust_cols (l);
95 return MSG_HANDLED;
97 case MSG_RESIZE:
98 hline_adjust_cols (l);
99 w->rect.y = RECT (data)->y;
100 return MSG_HANDLED;
102 case MSG_DRAW:
103 if (l->transparent)
104 tty_setcolor (DEFAULT_COLOR);
105 else
107 const int *colors;
109 colors = widget_get_colors (w);
110 tty_setcolor (colors[DLG_COLOR_NORMAL]);
113 tty_draw_hline (w->rect.y, w->rect.x + 1, ACS_HLINE, w->rect.cols - 2);
115 if (l->auto_adjust_cols)
117 widget_gotoyx (w, 0, 0);
118 tty_print_alt_char (ACS_LTEE, FALSE);
119 widget_gotoyx (w, 0, w->rect.cols - 1);
120 tty_print_alt_char (ACS_RTEE, FALSE);
123 if (l->text != NULL)
125 int text_width;
127 text_width = str_term_width1 (l->text);
128 widget_gotoyx (w, 0, (w->rect.cols - text_width) / 2);
129 tty_print_string (l->text);
131 return MSG_HANDLED;
133 case MSG_DESTROY:
134 g_free (l->text);
135 return MSG_HANDLED;
137 default:
138 return widget_default_callback (w, sender, msg, parm, data);
142 /* --------------------------------------------------------------------------------------------- */
143 /*** public functions ****************************************************************************/
144 /* --------------------------------------------------------------------------------------------- */
146 WHLine *
147 hline_new (int y, int x, int width)
149 WRect r = { y, x, 1, width };
150 WHLine *l;
151 Widget *w;
153 l = g_new (WHLine, 1);
154 w = WIDGET (l);
155 r.cols = width < 0 ? 1 : width;
156 widget_init (w, &r, hline_callback, NULL);
157 l->text = NULL;
158 l->auto_adjust_cols = (width < 0);
159 l->transparent = FALSE;
161 return l;
164 /* --------------------------------------------------------------------------------------------- */
166 void
167 hline_set_text (WHLine * l, const char *text)
169 g_free (l->text);
171 if (text == NULL || *text == '\0')
172 l->text = NULL;
173 else
174 l->text = g_strdup (text);
176 widget_draw (WIDGET (l));
179 /* --------------------------------------------------------------------------------------------- */
181 void
182 hline_set_textv (WHLine * l, const char *format, ...)
184 va_list args;
185 char buf[BUF_1K]; /* FIXME: is it enough? */
187 va_start (args, format);
188 g_vsnprintf (buf, sizeof (buf), format, args);
189 va_end (args);
191 hline_set_text (l, buf);
194 /* --------------------------------------------------------------------------------------------- */