Merge branch '3821_init_st_mtim'
[midnight-commander.git] / lib / widget / hline.c
blob4e7b93ef8f4f595a172453beb850eaf677cf6a73
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2017
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, 2010, 2013
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 <stdlib.h>
39 #include "lib/global.h"
40 #include "lib/tty/tty.h"
41 #include "lib/tty/color.h"
42 #include "lib/skin.h"
43 #include "lib/strutil.h"
44 #include "lib/widget.h"
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 /*** file scope functions ************************************************************************/
56 static cb_ret_t
57 hline_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
59 WHLine *l = HLINE (w);
60 WDialog *h = w->owner;
62 switch (msg)
64 case MSG_INIT:
65 case MSG_RESIZE:
66 if (l->auto_adjust_cols)
68 Widget *wo = WIDGET (h);
70 if (h->compact)
72 w->x = wo->x;
73 w->cols = wo->cols;
75 else
77 w->x = wo->x + 1;
78 w->cols = wo->cols - 2;
81 return MSG_HANDLED;
83 case MSG_DRAW:
84 if (l->transparent)
85 tty_setcolor (DEFAULT_COLOR);
86 else
87 tty_setcolor (h->color[DLG_COLOR_NORMAL]);
89 tty_draw_hline (w->y, w->x + 1, ACS_HLINE, w->cols - 2);
91 if (l->auto_adjust_cols)
93 widget_move (w, 0, 0);
94 tty_print_alt_char (ACS_LTEE, FALSE);
95 widget_move (w, 0, w->cols - 1);
96 tty_print_alt_char (ACS_RTEE, FALSE);
99 if (l->text != NULL)
101 int text_width;
103 text_width = str_term_width1 (l->text);
104 widget_move (w, 0, (w->cols - text_width) / 2);
105 tty_print_string (l->text);
107 return MSG_HANDLED;
109 case MSG_DESTROY:
110 g_free (l->text);
111 return MSG_HANDLED;
113 default:
114 return widget_default_callback (w, sender, msg, parm, data);
118 /* --------------------------------------------------------------------------------------------- */
119 /*** public functions ****************************************************************************/
120 /* --------------------------------------------------------------------------------------------- */
122 WHLine *
123 hline_new (int y, int x, int width)
125 WHLine *l;
126 Widget *w;
127 int lines = 1;
129 l = g_new (WHLine, 1);
130 w = WIDGET (l);
131 widget_init (w, y, x, lines, width < 0 ? 1 : width, hline_callback, NULL);
132 l->text = NULL;
133 l->auto_adjust_cols = (width < 0);
134 l->transparent = FALSE;
136 return l;
139 /* --------------------------------------------------------------------------------------------- */
141 void
142 hline_set_text (WHLine * l, const char *text)
144 g_free (l->text);
146 if (text == NULL || *text == '\0')
147 l->text = NULL;
148 else
149 l->text = g_strdup (text);
151 widget_redraw (WIDGET (l));
154 /* --------------------------------------------------------------------------------------------- */