root/lib/widget/label.c

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

DEFINITIONS

This source file includes following definitions.
  1. label_callback
  2. label_new
  3. label_set_text
  4. label_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 label.c
  32  *  \brief Source: WLabel widget
  33  */
  34 
  35 #include <config.h>
  36 
  37 #include <stdarg.h>
  38 #include <stdlib.h>
  39 #include <string.h>
  40 
  41 #include "lib/global.h"
  42 
  43 #include "lib/tty/tty.h"
  44 #include "lib/tty/color.h"
  45 #include "lib/skin.h"
  46 #include "lib/strutil.h"
  47 #include "lib/widget.h"
  48 
  49 /*** global variables ****************************************************************************/
  50 
  51 /*** file scope macro definitions ****************************************************************/
  52 
  53 /*** file scope type declarations ****************************************************************/
  54 
  55 /*** file scope variables ************************************************************************/
  56 
  57 /*** file scope functions ************************************************************************/
  58 
  59 static cb_ret_t
  60 label_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  61 {
  62     WLabel *l = LABEL (w);
  63 
  64     switch (msg)
  65     {
  66     case MSG_DRAW:
  67         {
  68             char *p = l->text;
  69             int y = 0;
  70             gboolean disabled;
  71             align_crt_t align;
  72 
  73             if (l->text == NULL)
  74                 return MSG_HANDLED;
  75 
  76             disabled = widget_get_state (w, WST_DISABLED);
  77 
  78             if (l->transparent)
  79                 tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
  80             else
  81             {
  82                 const int *colors;
  83 
  84                 colors = widget_get_colors (w);
  85                 tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL]);
  86             }
  87 
  88             align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
  89 
  90             while (TRUE)
  91             {
  92                 char *q;
  93                 char c = '\0';
  94 
  95 
  96                 q = strchr (p, '\n');
  97                 if (q != NULL)
  98                 {
  99                     c = q[0];
 100                     q[0] = '\0';
 101                 }
 102 
 103                 widget_gotoyx (w, y, 0);
 104                 tty_print_string (str_fit_to_term (p, w->rect.cols, align));
 105 
 106                 if (q == NULL)
 107                     break;
 108 
 109                 q[0] = c;
 110                 p = q + 1;
 111                 y++;
 112             }
 113             return MSG_HANDLED;
 114         }
 115 
 116     case MSG_DESTROY:
 117         g_free (l->text);
 118         return MSG_HANDLED;
 119 
 120     default:
 121         return widget_default_callback (w, sender, msg, parm, data);
 122     }
 123 }
 124 
 125 /* --------------------------------------------------------------------------------------------- */
 126 /*** public functions ****************************************************************************/
 127 /* --------------------------------------------------------------------------------------------- */
 128 
 129 WLabel *
 130 label_new (int y, int x, const char *text)
     /* [previous][next][first][last][top][bottom][index][help]  */
 131 {
 132     WRect r = { y, x, 1, 1 };
 133     WLabel *l;
 134     Widget *w;
 135 
 136     if (text != NULL)
 137         str_msg_term_size (text, &r.lines, &r.cols);
 138 
 139     l = g_new (WLabel, 1);
 140     w = WIDGET (l);
 141     widget_init (w, &r, label_callback, NULL);
 142 
 143     l->text = g_strdup (text);
 144     l->auto_adjust_cols = TRUE;
 145     l->transparent = FALSE;
 146 
 147     return l;
 148 }
 149 
 150 /* --------------------------------------------------------------------------------------------- */
 151 
 152 void
 153 label_set_text (WLabel * label, const char *text)
     /* [previous][next][first][last][top][bottom][index][help]  */
 154 {
 155     Widget *w = WIDGET (label);
 156     int newcols = w->rect.cols;
 157     int newlines;
 158 
 159     if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
 160         return;                 /* Flickering is not nice */
 161 
 162     g_free (label->text);
 163 
 164     if (text == NULL)
 165         label->text = NULL;
 166     else
 167     {
 168         label->text = g_strdup (text);
 169         if (label->auto_adjust_cols)
 170         {
 171             str_msg_term_size (text, &newlines, &newcols);
 172             w->rect.cols = MAX (newcols, w->rect.cols);
 173             w->rect.lines = MAX (newlines, w->rect.lines);
 174         }
 175     }
 176 
 177     widget_draw (w);
 178 
 179     w->rect.cols = MIN (newcols, w->rect.cols);
 180 }
 181 
 182 /* --------------------------------------------------------------------------------------------- */
 183 
 184 void
 185 label_set_textv (WLabel * label, const char *format, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 186 {
 187     va_list args;
 188     char buf[BUF_1K];           /* FIXME: is it enough? */
 189 
 190     va_start (args, format);
 191     g_vsnprintf (buf, sizeof (buf), format, args);
 192     va_end (args);
 193 
 194     label_set_text (label, buf);
 195 }
 196 
 197 /* --------------------------------------------------------------------------------------------- */

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