1 /*
2 Widgets for the Midnight Commander
3
4 Copyright (C) 1994-2026
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 {
104 const int *colors = widget_get_colors (w);
105
106 tty_setcolor (colors[DLG_COLOR_FRAME]);
107
108 tty_draw_hline (w->rect.y, w->rect.x + 1, mc_tty_frm[MC_TTY_FRM_HORIZ], w->rect.cols - 2);
109
110 if (l->auto_adjust_cols)
111 {
112 widget_gotoyx (w, 0, 0);
113 tty_print_char (mc_tty_frm[MC_TTY_FRM_DLEFTMIDDLE]);
114 widget_gotoyx (w, 0, w->rect.cols - 1);
115 tty_print_char (mc_tty_frm[MC_TTY_FRM_DRIGHTMIDDLE]);
116 }
117
118 if (l->text != NULL)
119 {
120 int text_width;
121
122 text_width = str_term_width1 (l->text);
123 widget_gotoyx (w, 0, (w->rect.cols - text_width) / 2);
124 tty_print_string (l->text);
125 }
126 return MSG_HANDLED;
127 }
128
129 case MSG_DESTROY:
130 g_free (l->text);
131 return MSG_HANDLED;
132
133 default:
134 return widget_default_callback (w, sender, msg, parm, data);
135 }
136 }
137
138 /* --------------------------------------------------------------------------------------------- */
139 /*** public functions ****************************************************************************/
140 /* --------------------------------------------------------------------------------------------- */
141
142 WHLine *
143 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)
*/
144 {
145 WRect r = { y, x, 1, width };
146 WHLine *l;
147 Widget *w;
148
149 l = g_new (WHLine, 1);
150 w = WIDGET (l);
151 r.cols = width < 0 ? 1 : width;
152 widget_init (w, &r, hline_callback, NULL);
153 l->text = NULL;
154 l->auto_adjust_cols = (width < 0);
155
156 return l;
157 }
158
159 /* --------------------------------------------------------------------------------------------- */
160
161 void
162 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)
*/
163 {
164 g_free (l->text);
165
166 if (text == NULL || *text == '\0')
167 l->text = NULL;
168 else
169 l->text = g_strdup (text);
170
171 widget_draw (WIDGET (l));
172 }
173
174 /* --------------------------------------------------------------------------------------------- */
175
176 void
177 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)
*/
178 {
179 va_list args;
180 char buf[BUF_1K]; // FIXME: is it enough?
181
182 va_start (args, format);
183 g_vsnprintf (buf, sizeof (buf), format, args);
184 va_end (args);
185
186 hline_set_text (l, buf);
187 }
188
189 /* --------------------------------------------------------------------------------------------- */