gauge.c 5.0 KB

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