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-2019
   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 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     WDialog *h = w->owner;
  64 
  65     switch (msg)
  66     {
  67     case MSG_DRAW:
  68         {
  69             char *p = l->text;
  70             int y = 0;
  71             gboolean disabled;
  72             align_crt_t align;
  73 
  74             if (l->text == NULL)
  75                 return MSG_HANDLED;
  76 
  77             disabled = widget_get_state (w, WST_DISABLED);
  78 
  79             if (l->transparent)
  80                 tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
  81             else
  82                 tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]);
  83 
  84             align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
  85 
  86             while (TRUE)
  87             {
  88                 char *q;
  89                 char c = '\0';
  90 
  91 
  92                 q = strchr (p, '\n');
  93                 if (q != NULL)
  94                 {
  95                     c = q[0];
  96                     q[0] = '\0';
  97                 }
  98 
  99                 widget_move (w, y, 0);
 100                 tty_print_string (str_fit_to_term (p, w->cols, align));
 101 
 102                 if (q == NULL)
 103                     break;
 104 
 105                 q[0] = c;
 106                 p = q + 1;
 107                 y++;
 108             }
 109             return MSG_HANDLED;
 110         }
 111 
 112     case MSG_DESTROY:
 113         g_free (l->text);
 114         return MSG_HANDLED;
 115 
 116     default:
 117         return widget_default_callback (w, sender, msg, parm, data);
 118     }
 119 }
 120 
 121 /* --------------------------------------------------------------------------------------------- */
 122 /*** public functions ****************************************************************************/
 123 /* --------------------------------------------------------------------------------------------- */
 124 
 125 WLabel *
 126 label_new (int y, int x, const char *text)
     /* [previous][next][first][last][top][bottom][index][help]  */
 127 {
 128     WLabel *l;
 129     Widget *w;
 130     int cols = 1;
 131     int lines = 1;
 132 
 133     if (text != NULL)
 134         str_msg_term_size (text, &lines, &cols);
 135 
 136     l = g_new (WLabel, 1);
 137     w = WIDGET (l);
 138     widget_init (w, y, x, lines, cols, label_callback, NULL);
 139 
 140     l->text = g_strdup (text);
 141     l->auto_adjust_cols = TRUE;
 142     l->transparent = FALSE;
 143 
 144     return l;
 145 }
 146 
 147 /* --------------------------------------------------------------------------------------------- */
 148 
 149 void
 150 label_set_text (WLabel * label, const char *text)
     /* [previous][next][first][last][top][bottom][index][help]  */
 151 {
 152     Widget *w = WIDGET (label);
 153     int newcols = w->cols;
 154     int newlines;
 155 
 156     if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
 157         return;                 /* Flickering is not nice */
 158 
 159     g_free (label->text);
 160 
 161     if (text == NULL)
 162         label->text = NULL;
 163     else
 164     {
 165         label->text = g_strdup (text);
 166         if (label->auto_adjust_cols)
 167         {
 168             str_msg_term_size (text, &newlines, &newcols);
 169             if (newcols > w->cols)
 170                 w->cols = newcols;
 171             if (newlines > w->lines)
 172                 w->lines = newlines;
 173         }
 174     }
 175 
 176     widget_redraw (w);
 177 
 178     if (newcols < w->cols)
 179         w->cols = newcols;
 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]  */