1 /*
2    Widgets for the Midnight Commander
3 
4    Copyright (C) 1994-2021
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, 2010, 2013
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 <http://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 /*** file scope variables ************************************************************************/
54 
55 /* --------------------------------------------------------------------------------------------- */
56 /*** file scope functions ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
58 
59 static void
hline_adjust_cols(WHLine * l)60 hline_adjust_cols (WHLine * l)
61 {
62     if (l->auto_adjust_cols)
63     {
64         Widget *w = WIDGET (l);
65         Widget *wo = WIDGET (w->owner);
66 
67         if (DIALOG (wo)->compact)
68         {
69             w->x = wo->x;
70             w->cols = wo->cols;
71         }
72         else
73         {
74             w->x = wo->x + 1;
75             w->cols = wo->cols - 2;
76         }
77     }
78 }
79 
80 /* --------------------------------------------------------------------------------------------- */
81 
82 static cb_ret_t
hline_callback(Widget * w,Widget * sender,widget_msg_t msg,int parm,void * data)83 hline_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
84 {
85     WHLine *l = HLINE (w);
86 
87     switch (msg)
88     {
89     case MSG_INIT:
90         hline_adjust_cols (l);
91         return MSG_HANDLED;
92 
93     case MSG_RESIZE:
94         hline_adjust_cols (l);
95         w->y = RECT (data)->y;
96         return MSG_HANDLED;
97 
98     case MSG_DRAW:
99         if (l->transparent)
100             tty_setcolor (DEFAULT_COLOR);
101         else
102         {
103             const int *colors;
104 
105             colors = widget_get_colors (w);
106             tty_setcolor (colors[DLG_COLOR_NORMAL]);
107         }
108 
109         tty_draw_hline (w->y, w->x + 1, ACS_HLINE, w->cols - 2);
110 
111         if (l->auto_adjust_cols)
112         {
113             widget_gotoyx (w, 0, 0);
114             tty_print_alt_char (ACS_LTEE, FALSE);
115             widget_gotoyx (w, 0, w->cols - 1);
116             tty_print_alt_char (ACS_RTEE, FALSE);
117         }
118 
119         if (l->text != NULL)
120         {
121             int text_width;
122 
123             text_width = str_term_width1 (l->text);
124             widget_gotoyx (w, 0, (w->cols - text_width) / 2);
125             tty_print_string (l->text);
126         }
127         return MSG_HANDLED;
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 *
hline_new(int y,int x,int width)143 hline_new (int y, int x, int width)
144 {
145     WHLine *l;
146     Widget *w;
147     int lines = 1;
148 
149     l = g_new (WHLine, 1);
150     w = WIDGET (l);
151     widget_init (w, y, x, lines, width < 0 ? 1 : width, hline_callback, NULL);
152     l->text = NULL;
153     l->auto_adjust_cols = (width < 0);
154     l->transparent = FALSE;
155 
156     return l;
157 }
158 
159 /* --------------------------------------------------------------------------------------------- */
160 
161 void
hline_set_text(WHLine * l,const char * text)162 hline_set_text (WHLine * l, const char *text)
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
hline_set_textv(WHLine * l,const char * format,...)177 hline_set_textv (WHLine * l, const char *format, ...)
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 /* --------------------------------------------------------------------------------------------- */
190