gauge.c 5.4 KB

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