gauge.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2024
  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-2022
  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. /*** forward declarations (file scope functions) *************************************************/
  39. /*** file scope variables ************************************************************************/
  40. /* --------------------------------------------------------------------------------------------- */
  41. /*** file scope functions ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. static cb_ret_t
  44. gauge_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  45. {
  46. WGauge *g = GAUGE (w);
  47. const int *colors;
  48. switch (msg)
  49. {
  50. case MSG_DRAW:
  51. colors = widget_get_colors (w);
  52. widget_gotoyx (w, 0, 0);
  53. if (!g->shown)
  54. {
  55. tty_setcolor (colors[DLG_COLOR_NORMAL]);
  56. tty_printf ("%*s", w->rect.cols, "");
  57. }
  58. else
  59. {
  60. int gauge_len;
  61. int percentage, columns;
  62. int total = g->max;
  63. int done = g->current;
  64. if (total <= 0 || done < 0)
  65. {
  66. done = 0;
  67. total = 100;
  68. }
  69. if (done > total)
  70. done = total;
  71. while (total > 65535)
  72. {
  73. total /= 256;
  74. done /= 256;
  75. }
  76. gauge_len = w->rect.cols - 7; /* 7 positions for percentage */
  77. percentage = (200 * done / total + 1) / 2;
  78. columns = (2 * gauge_len * done / total + 1) / 2;
  79. tty_print_char ('[');
  80. if (g->from_left_to_right)
  81. {
  82. tty_setcolor (GAUGE_COLOR);
  83. tty_printf ("%*s", columns, "");
  84. tty_setcolor (colors[DLG_COLOR_NORMAL]);
  85. tty_printf ("%*s] %3d%%", gauge_len - columns, "", percentage);
  86. }
  87. else
  88. {
  89. tty_setcolor (colors[DLG_COLOR_NORMAL]);
  90. tty_printf ("%*s", gauge_len - columns, "");
  91. tty_setcolor (GAUGE_COLOR);
  92. tty_printf ("%*s", columns, "");
  93. tty_setcolor (colors[DLG_COLOR_NORMAL]);
  94. tty_printf ("] %3d%%", percentage);
  95. }
  96. }
  97. return MSG_HANDLED;
  98. default:
  99. return widget_default_callback (w, sender, msg, parm, data);
  100. }
  101. }
  102. /* --------------------------------------------------------------------------------------------- */
  103. /*** public functions ****************************************************************************/
  104. /* --------------------------------------------------------------------------------------------- */
  105. WGauge *
  106. gauge_new (int y, int x, int cols, gboolean shown, int max, int current)
  107. {
  108. WRect r = { y, x, 1, cols };
  109. WGauge *g;
  110. Widget *w;
  111. g = g_new (WGauge, 1);
  112. w = WIDGET (g);
  113. widget_init (w, &r, gauge_callback, NULL);
  114. g->shown = shown;
  115. if (max == 0)
  116. max = 1; /* I do not like division by zero :) */
  117. g->max = max;
  118. g->current = current;
  119. g->from_left_to_right = TRUE;
  120. return g;
  121. }
  122. /* --------------------------------------------------------------------------------------------- */
  123. void
  124. gauge_set_value (WGauge *g, int max, int current)
  125. {
  126. if (g->current == current && g->max == max)
  127. return; /* Do not flicker */
  128. if (max == 0)
  129. max = 1; /* I do not like division by zero :) */
  130. g->current = current;
  131. g->max = max;
  132. widget_draw (WIDGET (g));
  133. }
  134. /* --------------------------------------------------------------------------------------------- */
  135. void
  136. gauge_show (WGauge *g, gboolean shown)
  137. {
  138. if (g->shown != shown)
  139. {
  140. g->shown = shown;
  141. widget_draw (WIDGET (g));
  142. }
  143. }
  144. /* --------------------------------------------------------------------------------------------- */