gauge.c 5.3 KB

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