gauge.c 5.3 KB

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