root/lib/widget/hline.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. hline_adjust_cols
  2. hline_callback
  3. hline_new
  4. hline_set_text
  5. hline_set_textv

   1 /*
   2    Widgets for the Midnight Commander
   3 
   4    Copyright (C) 1994-2022
   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 <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
  60 hline_adjust_cols (WHLine * l)
     /* [previous][next][first][last][top][bottom][index][help]  */
  61 {
  62     if (l->auto_adjust_cols)
  63     {
  64         Widget *wl = WIDGET (l);
  65         const Widget *o = CONST_WIDGET (wl->owner);
  66         WRect *w = &wl->rect;
  67         const WRect *wo = &o->rect;
  68 
  69         if (CONST_DIALOG (o)->compact)
  70         {
  71             w->x = wo->x;
  72             w->cols = wo->cols;
  73         }
  74         else
  75         {
  76             w->x = wo->x + 1;
  77             w->cols = wo->cols - 2;
  78         }
  79     }
  80 }
  81 
  82 /* --------------------------------------------------------------------------------------------- */
  83 
  84 static cb_ret_t
  85 hline_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  86 {
  87     WHLine *l = HLINE (w);
  88 
  89     switch (msg)
  90     {
  91     case MSG_INIT:
  92         hline_adjust_cols (l);
  93         return MSG_HANDLED;
  94 
  95     case MSG_RESIZE:
  96         hline_adjust_cols (l);
  97         w->rect.y = RECT (data)->y;
  98         return MSG_HANDLED;
  99 
 100     case MSG_DRAW:
 101         if (l->transparent)
 102             tty_setcolor (DEFAULT_COLOR);
 103         else
 104         {
 105             const int *colors;
 106 
 107             colors = widget_get_colors (w);
 108             tty_setcolor (colors[DLG_COLOR_NORMAL]);
 109         }
 110 
 111         tty_draw_hline (w->rect.y, w->rect.x + 1, ACS_HLINE, w->rect.cols - 2);
 112 
 113         if (l->auto_adjust_cols)
 114         {
 115             widget_gotoyx (w, 0, 0);
 116             tty_print_alt_char (ACS_LTEE, FALSE);
 117             widget_gotoyx (w, 0, w->rect.cols - 1);
 118             tty_print_alt_char (ACS_RTEE, FALSE);
 119         }
 120 
 121         if (l->text != NULL)
 122         {
 123             int text_width;
 124 
 125             text_width = str_term_width1 (l->text);
 126             widget_gotoyx (w, 0, (w->rect.cols - text_width) / 2);
 127             tty_print_string (l->text);
 128         }
 129         return MSG_HANDLED;
 130 
 131     case MSG_DESTROY:
 132         g_free (l->text);
 133         return MSG_HANDLED;
 134 
 135     default:
 136         return widget_default_callback (w, sender, msg, parm, data);
 137     }
 138 }
 139 
 140 /* --------------------------------------------------------------------------------------------- */
 141 /*** public functions ****************************************************************************/
 142 /* --------------------------------------------------------------------------------------------- */
 143 
 144 WHLine *
 145 hline_new (int y, int x, int width)
     /* [previous][next][first][last][top][bottom][index][help]  */
 146 {
 147     WRect r = { y, x, 1, width };
 148     WHLine *l;
 149     Widget *w;
 150 
 151     l = g_new (WHLine, 1);
 152     w = WIDGET (l);
 153     r.cols = width < 0 ? 1 : width;
 154     widget_init (w, &r, hline_callback, NULL);
 155     l->text = NULL;
 156     l->auto_adjust_cols = (width < 0);
 157     l->transparent = FALSE;
 158 
 159     return l;
 160 }
 161 
 162 /* --------------------------------------------------------------------------------------------- */
 163 
 164 void
 165 hline_set_text (WHLine * l, const char *text)
     /* [previous][next][first][last][top][bottom][index][help]  */
 166 {
 167     g_free (l->text);
 168 
 169     if (text == NULL || *text == '\0')
 170         l->text = NULL;
 171     else
 172         l->text = g_strdup (text);
 173 
 174     widget_draw (WIDGET (l));
 175 }
 176 
 177 /* --------------------------------------------------------------------------------------------- */
 178 
 179 void
 180 hline_set_textv (WHLine * l, const char *format, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 181 {
 182     va_list args;
 183     char buf[BUF_1K];           /* FIXME: is it enough? */
 184 
 185     va_start (args, format);
 186     g_vsnprintf (buf, sizeof (buf), format, args);
 187     va_end (args);
 188 
 189     hline_set_text (l, buf);
 190 }
 191 
 192 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */