Merge branch '4524_cleanup'
[midnight-commander.git] / lib / widget / frame.c
blob127649e45291cc399bf2b8bda3fd076e541ce448
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 2020-2024
5 The Free Software Foundation, Inc.
7 Authors:
8 Andrew Borodin <aborodin@vmail.ru>, 2020-2022
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file frame.c
27 * \brief Source: WFrame widget (frame of dialogs)
30 #include <config.h>
32 #include <stdlib.h>
34 #include "lib/global.h"
35 #include "lib/tty/tty.h"
36 #include "lib/tty/color.h"
37 #include "lib/skin.h"
38 #include "lib/strutil.h"
39 #include "lib/util.h" /* MC_PTR_FREE */
40 #include "lib/widget.h"
42 /*** global variables ****************************************************************************/
44 /*** file scope macro definitions ****************************************************************/
46 /*** file scope type declarations ****************************************************************/
48 /*** forward declarations (file scope functions) *************************************************/
50 /*** file scope variables ************************************************************************/
52 /* --------------------------------------------------------------------------------------------- */
53 /*** file scope functions ************************************************************************/
54 /* --------------------------------------------------------------------------------------------- */
56 static void
57 frame_adjust (WFrame * f)
59 Widget *w = WIDGET (f);
61 w->rect = WIDGET (w->owner)->rect;
62 w->pos_flags |= WPOS_KEEP_ALL;
65 /* --------------------------------------------------------------------------------------------- */
67 static void
68 frame_draw (const WFrame * f)
70 const Widget *wf = CONST_WIDGET (f);
71 const WRect *w = &wf->rect;
72 int d = f->compact ? 0 : 1;
73 const int *colors;
75 colors = widget_get_colors (wf);
77 if (mc_global.tty.shadows)
78 tty_draw_box_shadow (w->y, w->x, w->lines, w->cols, SHADOW_COLOR);
80 tty_setcolor (colors[FRAME_COLOR_NORMAL]);
81 tty_fill_region (w->y, w->x, w->lines, w->cols, ' ');
82 tty_draw_box (w->y + d, w->x + d, w->lines - 2 * d, w->cols - 2 * d, f->single);
84 if (f->title != NULL)
86 /* TODO: truncate long title */
87 tty_setcolor (colors[FRAME_COLOR_TITLE]);
88 widget_gotoyx (f, d, (w->cols - str_term_width1 (f->title)) / 2);
89 tty_print_string (f->title);
93 /* --------------------------------------------------------------------------------------------- */
94 /*** public functions ****************************************************************************/
95 /* --------------------------------------------------------------------------------------------- */
97 WFrame *
98 frame_new (int y, int x, int lines, int cols, const char *title, gboolean single, gboolean compact)
100 WRect r = { y, x, lines, cols };
101 WFrame *f;
102 Widget *w;
104 f = g_new (WFrame, 1);
105 w = WIDGET (f);
106 widget_init (w, &r, frame_callback, NULL);
108 f->single = single;
109 f->compact = compact;
111 f->title = NULL;
112 frame_set_title (f, title);
114 return f;
117 /* --------------------------------------------------------------------------------------------- */
119 cb_ret_t
120 frame_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
122 WFrame *f = FRAME (w);
124 switch (msg)
126 case MSG_INIT:
127 frame_adjust (f);
128 return MSG_HANDLED;
130 case MSG_DRAW:
131 frame_draw (f);
132 return MSG_HANDLED;
134 case MSG_DESTROY:
135 g_free (f->title);
136 return MSG_HANDLED;
138 default:
139 return widget_default_callback (w, sender, msg, parm, data);
143 /* --------------------------------------------------------------------------------------------- */
145 void
146 frame_set_title (WFrame * f, const char *title)
148 MC_PTR_FREE (f->title);
150 /* Strip existing spaces, add one space before and after the title */
151 if (title != NULL && *title != '\0')
153 char *t;
155 t = g_strstrip (g_strdup (title));
156 if (*t != '\0')
157 f->title = g_strdup_printf (" %s ", t);
158 g_free (t);
161 widget_draw (WIDGET (f));
164 /* --------------------------------------------------------------------------------------------- */