root/lib/widget/gauge.c

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

DEFINITIONS

This source file includes following definitions.
  1. gauge_callback
  2. gauge_new
  3. gauge_set_value
  4. gauge_show

   1 /*
   2    Widgets for the Midnight Commander
   3 
   4    Copyright (C) 1994-2020
   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 gauge.c
  32  *  \brief Source: WGauge widget (progress indicator)
  33  */
  34 
  35 #include <config.h>
  36 
  37 #include <stdlib.h>
  38 #include <string.h>
  39 
  40 #include "lib/global.h"
  41 
  42 #include "lib/tty/tty.h"
  43 #include "lib/tty/color.h"
  44 #include "lib/skin.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 /*** file scope functions ************************************************************************/
  56 
  57 static cb_ret_t
  58 gauge_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  59 {
  60     WGauge *g = GAUGE (w);
  61     const int *colors;
  62 
  63     switch (msg)
  64     {
  65     case MSG_DRAW:
  66         colors = widget_get_colors (w);
  67         widget_gotoyx (w, 0, 0);
  68         if (!g->shown)
  69         {
  70             tty_setcolor (colors[DLG_COLOR_NORMAL]);
  71             tty_printf ("%*s", w->cols, "");
  72         }
  73         else
  74         {
  75             int gauge_len;
  76             int percentage, columns;
  77             int total = g->max;
  78             int done = g->current;
  79 
  80             if (total <= 0 || done < 0)
  81             {
  82                 done = 0;
  83                 total = 100;
  84             }
  85             if (done > total)
  86                 done = total;
  87             while (total > 65535)
  88             {
  89                 total /= 256;
  90                 done /= 256;
  91             }
  92 
  93             gauge_len = w->cols - 7;    /* 7 positions for percentage */
  94 
  95             percentage = (200 * done / total + 1) / 2;
  96             columns = (2 * gauge_len * done / total + 1) / 2;
  97             tty_print_char ('[');
  98             if (g->from_left_to_right)
  99             {
 100                 tty_setcolor (GAUGE_COLOR);
 101                 tty_printf ("%*s", columns, "");
 102                 tty_setcolor (colors[DLG_COLOR_NORMAL]);
 103                 tty_printf ("%*s] %3d%%", gauge_len - columns, "", percentage);
 104             }
 105             else
 106             {
 107                 tty_setcolor (colors[DLG_COLOR_NORMAL]);
 108                 tty_printf ("%*s", gauge_len - columns, "");
 109                 tty_setcolor (GAUGE_COLOR);
 110                 tty_printf ("%*s", columns, "");
 111                 tty_setcolor (colors[DLG_COLOR_NORMAL]);
 112                 tty_printf ("] %3d%%", percentage);
 113             }
 114         }
 115         return MSG_HANDLED;
 116 
 117     default:
 118         return widget_default_callback (w, sender, msg, parm, data);
 119     }
 120 }
 121 
 122 /* --------------------------------------------------------------------------------------------- */
 123 /*** public functions ****************************************************************************/
 124 /* --------------------------------------------------------------------------------------------- */
 125 
 126 WGauge *
 127 gauge_new (int y, int x, int cols, gboolean shown, int max, int current)
     /* [previous][next][first][last][top][bottom][index][help]  */
 128 {
 129     WGauge *g;
 130     Widget *w;
 131 
 132     g = g_new (WGauge, 1);
 133     w = WIDGET (g);
 134     widget_init (w, y, x, 1, cols, gauge_callback, NULL);
 135 
 136     g->shown = shown;
 137     if (max == 0)
 138         max = 1;                /* I do not like division by zero :) */
 139     g->max = max;
 140     g->current = current;
 141     g->from_left_to_right = TRUE;
 142 
 143     return g;
 144 }
 145 
 146 /* --------------------------------------------------------------------------------------------- */
 147 
 148 void
 149 gauge_set_value (WGauge * g, int max, int current)
     /* [previous][next][first][last][top][bottom][index][help]  */
 150 {
 151     if (g->current == current && g->max == max)
 152         return;                 /* Do not flicker */
 153 
 154     if (max == 0)
 155         max = 1;                /* I do not like division by zero :) */
 156     g->current = current;
 157     g->max = max;
 158     widget_draw (WIDGET (g));
 159 }
 160 
 161 /* --------------------------------------------------------------------------------------------- */
 162 
 163 void
 164 gauge_show (WGauge * g, gboolean shown)
     /* [previous][next][first][last][top][bottom][index][help]  */
 165 {
 166     if (g->shown != shown)
 167     {
 168         g->shown = shown;
 169         widget_draw (WIDGET (g));
 170     }
 171 }
 172 
 173 /* --------------------------------------------------------------------------------------------- */

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