gauge.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2021
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Radek Doulik, 1994, 1995
  7. Miguel de Icaza, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1996
  10. Norbert Warmuth, 1997
  11. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. 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.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file gauge.c
  25. * \brief Source: WGauge widget (progress indicator)
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "lib/global.h"
  31. #include "lib/tty/tty.h"
  32. #include "lib/tty/color.h"
  33. #include "lib/skin.h"
  34. #include "lib/widget.h"
  35. /*** global variables ****************************************************************************/
  36. /*** file scope macro definitions ****************************************************************/
  37. /*** file scope type declarations ****************************************************************/
  38. /*** file scope variables ************************************************************************/
  39. /*** file scope functions ************************************************************************/
  40. static cb_ret_t
  41. gauge_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  42. {
  43. WGauge *g = GAUGE (w);
  44. const int *colors;
  45. switch (msg)
  46. {
  47. case MSG_DRAW:
  48. colors = widget_get_colors (w);
  49. widget_gotoyx (w, 0, 0);
  50. if (!g->shown)
  51. {
  52. tty_setcolor (colors[DLG_COLOR_NORMAL]);
  53. tty_printf ("%*s", w->cols, "");
  54. }
  55. else
  56. {
  57. int gauge_len;
  58. int percentage, columns;
  59. int total = g->max;
  60. int done = g->current;
  61. if (total <= 0 || done < 0)
  62. {
  63. done = 0;
  64. total = 100;
  65. }
  66. if (done > total)
  67. done = total;
  68. while (total > 65535)
  69. {
  70. total /= 256;
  71. done /= 256;
  72. }
  73. gauge_len = w->cols - 7; /* 7 positions for percentage */
  74. percentage = (200 * done / total + 1) / 2;
  75. columns = (2 * gauge_len * done / total + 1) / 2;
  76. tty_print_char ('[');
  77. if (g->from_left_to_right)
  78. {
  79. tty_setcolor (GAUGE_COLOR);
  80. tty_printf ("%*s", columns, "");
  81. tty_setcolor (colors[DLG_COLOR_NORMAL]);
  82. tty_printf ("%*s] %3d%%", gauge_len - columns, "", percentage);
  83. }
  84. else
  85. {
  86. tty_setcolor (colors[DLG_COLOR_NORMAL]);
  87. tty_printf ("%*s", gauge_len - columns, "");
  88. tty_setcolor (GAUGE_COLOR);
  89. tty_printf ("%*s", columns, "");
  90. tty_setcolor (colors[DLG_COLOR_NORMAL]);
  91. tty_printf ("] %3d%%", percentage);
  92. }
  93. }
  94. return MSG_HANDLED;
  95. default:
  96. return widget_default_callback (w, sender, msg, parm, data);
  97. }
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. /*** public functions ****************************************************************************/
  101. /* --------------------------------------------------------------------------------------------- */
  102. WGauge *
  103. gauge_new (int y, int x, int cols, gboolean shown, int max, int current)
  104. {
  105. WGauge *g;
  106. Widget *w;
  107. g = g_new (WGauge, 1);
  108. w = WIDGET (g);
  109. widget_init (w, y, x, 1, cols, gauge_callback, NULL);
  110. g->shown = shown;
  111. if (max == 0)
  112. max = 1; /* I do not like division by zero :) */
  113. g->max = max;
  114. g->current = current;
  115. g->from_left_to_right = TRUE;
  116. return g;
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. void
  120. gauge_set_value (WGauge * g, int max, int current)
  121. {
  122. if (g->current == current && g->max == max)
  123. return; /* Do not flicker */
  124. if (max == 0)
  125. max = 1; /* I do not like division by zero :) */
  126. g->current = current;
  127. g->max = max;
  128. widget_draw (WIDGET (g));
  129. }
  130. /* --------------------------------------------------------------------------------------------- */
  131. void
  132. gauge_show (WGauge * g, gboolean shown)
  133. {
  134. if (g->shown != shown)
  135. {
  136. g->shown = shown;
  137. widget_draw (WIDGET (g));
  138. }
  139. }
  140. /* --------------------------------------------------------------------------------------------- */