1 /*
2 Widgets for the Midnight Commander
3
4 Copyright (C) 1994-2025
5 Free Software Foundation, Inc.
6
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
14
15 This file is part of the Midnight Commander.
16
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.
21
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.
26
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <https://www.gnu.org/licenses/>.
29 */
30
31 /** \file hline.c
32 * \brief Source: WHLine widget (horizontal line)
33 */
34
35 #include <config.h>
36
37 #include <stdarg.h>
38 #include <stdlib.h>
39
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"
46
47 /*** global variables ****************************************************************************/
48
49 /*** file scope macro definitions ****************************************************************/
50
51 /*** file scope type declarations ****************************************************************/
52
53 /*** forward declarations (file scope functions) *************************************************/
54
55 /*** file scope variables ************************************************************************/
56
57 /* --------------------------------------------------------------------------------------------- */
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
60
61 static void
62 hline_adjust_cols (WHLine *l)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
63 {
64 if (l->auto_adjust_cols)
65 {
66 Widget *wl = WIDGET (l);
67 const Widget *o = CONST_WIDGET (wl->owner);
68 WRect *w = &wl->rect;
69 const WRect *wo = &o->rect;
70
71 if (CONST_DIALOG (o)->compact)
72 {
73 w->x = wo->x;
74 w->cols = wo->cols;
75 }
76 else
77 {
78 w->x = wo->x + 1;
79 w->cols = wo->cols - 2;
80 }
81 }
82 }
83
84 /* --------------------------------------------------------------------------------------------- */
85
86 static cb_ret_t
87 hline_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
88 {
89 WHLine *l = HLINE (w);
90
91 switch (msg)
92 {
93 case MSG_INIT:
94 hline_adjust_cols (l);
95 return MSG_HANDLED;
96
97 case MSG_RESIZE:
98 hline_adjust_cols (l);
99 w->rect.y = RECT (data)->y;
100 return MSG_HANDLED;
101
102 case MSG_DRAW:
103 if (l->transparent)
104 tty_setcolor (DEFAULT_COLOR);
105 else
106 {
107 const int *colors;
108
109 colors = widget_get_colors (w);
110 tty_setcolor (colors[DLG_COLOR_NORMAL]);
111 }
112
113 tty_draw_hline (w->rect.y, w->rect.x + 1, ACS_HLINE, w->rect.cols - 2);
114
115 if (l->auto_adjust_cols)
116 {
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);
121 }
122
123 if (l->text != NULL)
124 {
125 int text_width;
126
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);
130 }
131 return MSG_HANDLED;
132
133 case MSG_DESTROY:
134 g_free (l->text);
135 return MSG_HANDLED;
136
137 default:
138 return widget_default_callback (w, sender, msg, parm, data);
139 }
140 }
141
142 /* --------------------------------------------------------------------------------------------- */
143 /*** public functions ****************************************************************************/
144 /* --------------------------------------------------------------------------------------------- */
145
146 WHLine *
147 hline_new (int y, int x, int width)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
148 {
149 WRect r = { y, x, 1, width };
150 WHLine *l;
151 Widget *w;
152
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;
160
161 return l;
162 }
163
164 /* --------------------------------------------------------------------------------------------- */
165
166 void
167 hline_set_text (WHLine *l, const char *text)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
168 {
169 g_free (l->text);
170
171 if (text == NULL || *text == '\0')
172 l->text = NULL;
173 else
174 l->text = g_strdup (text);
175
176 widget_draw (WIDGET (l));
177 }
178
179 /* --------------------------------------------------------------------------------------------- */
180
181 void
182 hline_set_textv (WHLine *l, const char *format, ...)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
183 {
184 va_list args;
185 char buf[BUF_1K]; // FIXME: is it enough?
186
187 va_start (args, format);
188 g_vsnprintf (buf, sizeof (buf), format, args);
189 va_end (args);
190
191 hline_set_text (l, buf);
192 }
193
194 /* --------------------------------------------------------------------------------------------- */