scrollbar.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (C) 2003 Mooffie <mooffie@typo.co.il>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  16. #include <config.h>
  17. #include "scrollbar.h"
  18. #include "themes.h"
  19. Scrollbar::Scrollbar()
  20. {
  21. create_window();
  22. scrollok(wnd, 0);
  23. total_size = 0;
  24. page_size = 0;
  25. page_pos = 0;
  26. dirty = true;
  27. }
  28. void Scrollbar::set_page_size(int sz)
  29. {
  30. if (sz != page_size) {
  31. page_size = sz;
  32. invalidate_view();
  33. }
  34. }
  35. void Scrollbar::set_total_size(int sz)
  36. {
  37. if (sz != total_size) {
  38. total_size = sz;
  39. invalidate_view();
  40. }
  41. }
  42. void Scrollbar::set_page_pos(int pos)
  43. {
  44. if (pos != page_pos) {
  45. page_pos = pos;
  46. invalidate_view();
  47. }
  48. }
  49. void Scrollbar::update()
  50. {
  51. if (!dirty)
  52. return;
  53. int thumb_pos = 0;
  54. if (page_pos > 0)
  55. thumb_pos = (int)((double(page_pos)/double(total_size))*window_height());
  56. int thumb_size = (int)((double)page_size/double(total_size)*window_height()+1);
  57. if (thumb_size > window_height())
  58. thumb_size = window_height();
  59. if (page_size >= total_size) // don't show if we don't have a reason
  60. thumb_size = 0;
  61. if (thumb_size == window_height())
  62. thumb_size--;
  63. if (page_pos + page_size >= total_size)
  64. thumb_pos = window_height() - thumb_size;
  65. werase(wnd);
  66. attribute_t base_attr = get_attr(SCROLLBAR_ATTR);
  67. attribute_t thumb_attr = get_attr(SCROLLBAR_THUMB_ATTR);
  68. for (int i = 0; i < window_height(); i++) {
  69. if (i >= thumb_pos && i < thumb_pos + thumb_size) {
  70. wattrset(wnd, thumb_attr);
  71. mvwhline(wnd, i, 0, ' ', 1);
  72. } else {
  73. wattrset(wnd, base_attr);
  74. mvwhline(wnd, i, 0, terminal::graphical_boxes ? ACS_VLINE : '|', 1);
  75. }
  76. }
  77. wnoutrefresh(wnd);
  78. dirty = false;
  79. }
  80. void Scrollbar::resize(int lines, int columns, int y, int x)
  81. {
  82. Widget::resize(lines, columns, y, x);
  83. invalidate_view();
  84. }