gauge.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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
  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
  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. /* Currently width is hardcoded here for text mode */
  39. #define gauge_len 47
  40. /*** file scope type declarations ****************************************************************/
  41. /*** file scope variables ************************************************************************/
  42. /*** file scope functions ************************************************************************/
  43. static cb_ret_t
  44. gauge_callback (Widget * w, widget_msg_t msg, int parm)
  45. {
  46. WGauge *g = (WGauge *) w;
  47. Dlg_head *h = g->widget.owner;
  48. if (msg == WIDGET_INIT)
  49. return MSG_HANDLED;
  50. /* We don't want to get the focus */
  51. if (msg == WIDGET_FOCUS)
  52. return MSG_NOT_HANDLED;
  53. if (msg == WIDGET_DRAW)
  54. {
  55. widget_move (&g->widget, 0, 0);
  56. tty_setcolor (h->color[DLG_COLOR_NORMAL]);
  57. if (!g->shown)
  58. tty_printf ("%*s", gauge_len, "");
  59. else
  60. {
  61. int percentage, columns;
  62. long total = g->max;
  63. long 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. percentage = (200 * done / total + 1) / 2;
  77. columns = (2 * (gauge_len - 7) * done / total + 1) / 2;
  78. tty_print_char ('[');
  79. if (g->from_left_to_right)
  80. {
  81. tty_setcolor (GAUGE_COLOR);
  82. tty_printf ("%*s", (int) columns, "");
  83. tty_setcolor (h->color[DLG_COLOR_NORMAL]);
  84. tty_printf ("%*s] %3d%%", (int) (gauge_len - 7 - columns), "", (int) percentage);
  85. }
  86. else
  87. {
  88. tty_setcolor (h->color[DLG_COLOR_NORMAL]);
  89. tty_printf ("%*s", gauge_len - columns - 7, "");
  90. tty_setcolor (GAUGE_COLOR);
  91. tty_printf ("%*s", columns, "");
  92. tty_setcolor (h->color[DLG_COLOR_NORMAL]);
  93. tty_printf ("] %3d%%", 100 * columns / (gauge_len - 7), percentage);
  94. }
  95. }
  96. return MSG_HANDLED;
  97. }
  98. return default_proc (msg, parm);
  99. }
  100. /* --------------------------------------------------------------------------------------------- */
  101. /*** public functions ****************************************************************************/
  102. /* --------------------------------------------------------------------------------------------- */
  103. WGauge *
  104. gauge_new (int y, int x, gboolean shown, int max, int current)
  105. {
  106. WGauge *g;
  107. g = g_new (WGauge, 1);
  108. init_widget (&g->widget, y, x, 1, gauge_len, 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. widget_want_cursor (g->widget, FALSE);
  116. widget_want_hotkey (g->widget, FALSE);
  117. return g;
  118. }
  119. /* --------------------------------------------------------------------------------------------- */
  120. void
  121. gauge_set_value (WGauge * g, int max, int current)
  122. {
  123. if (g->current == current && g->max == max)
  124. return; /* Do not flicker */
  125. if (max == 0)
  126. max = 1; /* I do not like division by zero :) */
  127. g->current = current;
  128. g->max = max;
  129. gauge_callback ((Widget *) g, WIDGET_DRAW, 0);
  130. }
  131. /* --------------------------------------------------------------------------------------------- */
  132. void
  133. gauge_show (WGauge * g, gboolean shown)
  134. {
  135. if (g->shown != shown)
  136. {
  137. g->shown = shown;
  138. gauge_callback ((Widget *) g, WIDGET_DRAW, 0);
  139. }
  140. }
  141. /* --------------------------------------------------------------------------------------------- */