The Pedigree Project  0.1
Scrollable.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "pedigree/kernel/debugger/Scrollable.h"
21 
22 Scrollable::Scrollable()
23  : m_x(0), m_y(0), m_width(0), m_height(0), m_line(0), m_ScrollUp('j'),
24  m_ScrollDown('k')
25 {
26 }
27 Scrollable::~Scrollable() = default;
28 
29 void Scrollable::move(size_t x, size_t y)
30 {
31  m_x = x;
32  m_y = y;
33 }
34 
35 void Scrollable::resize(size_t width, size_t height)
36 {
37  m_width = width;
38  m_height = height;
39 }
40 
41 void Scrollable::scroll(ssize_t lines)
42 {
43  m_line += lines;
44 
45  if (m_line >
46  (static_cast<ssize_t>(getLineCount()) - static_cast<ssize_t>(m_height)))
47  m_line = static_cast<ssize_t>(getLineCount()) -
48  static_cast<ssize_t>(m_height);
49 
50  if (m_line < 0)
51  m_line = 0;
52 }
53 
54 void Scrollable::scrollTo(size_t absolute)
55 {
56  m_line = absolute;
57 
58  if (m_line >
59  (static_cast<ssize_t>(getLineCount()) - static_cast<ssize_t>(m_height)))
60  m_line = static_cast<ssize_t>(getLineCount()) -
61  static_cast<ssize_t>(m_height);
62 
63  if (m_line < 0)
64  m_line = 0;
65 }
66 
67 void Scrollable::refresh(DebuggerIO *pScreen)
68 {
69  pScreen->disableRefreshes();
70 
71  // TODO: Add a scroll bar on the right side of the window
72 
73  // Can we scroll up?
74  bool bCanScrollUp = (m_line > 0);
75  bool bCanScrollDown = ((m_line + m_height) < getLineCount());
76 
77  // For every available line.
78  ssize_t line = m_line;
79  for (size_t i = 0; i < m_height; i++)
80  {
81  pScreen->drawHorizontalLine(
82  ' ', m_y + i, m_x, m_x + m_width - 1, DebuggerIO::White,
83  DebuggerIO::Black);
84 
85  if (line >= 0 && line < static_cast<ssize_t>(getLineCount()))
86  {
87  DebuggerIO::Colour colour = DebuggerIO::White;
88  DebuggerIO::Colour bgColour = DebuggerIO::Black;
89  size_t colOffset;
90  const char *Line = getLine1(line, colour, bgColour);
91  if (Line)
92  pScreen->drawString(Line, m_y + i, m_x, colour, bgColour);
93 
94  colour = DebuggerIO::White;
95  bgColour = DebuggerIO::Black;
96  Line = getLine2(line, colOffset, colour, bgColour);
97  if (Line)
98  pScreen->drawString(
99  Line, m_y + i, m_x + colOffset, colour, bgColour);
100  }
101  line++;
102  }
103 
104  // Draw scroll marks.
105  char str[4] = {'^', ' ', m_ScrollUp, '\0'};
106  if (bCanScrollUp)
107  pScreen->drawString(
108  str, m_y, m_x + m_width - 3, DebuggerIO::White, DebuggerIO::Blue);
109  str[0] = 'v';
110  str[2] = m_ScrollDown;
111  if (bCanScrollDown)
112  pScreen->drawString(
113  str, m_y + m_height - 1, m_x + m_width - 3, DebuggerIO::White,
114  DebuggerIO::Blue);
115 
116  pScreen->enableRefreshes();
117 }
118 
119 void Scrollable::setScrollKeys(char up, char down)
120 {
121  m_ScrollUp = up;
122  m_ScrollDown = down;
123 }
124 
125 ssize_t Scrollable::getLine()
126 {
127  return m_line;
128 }
129 
130 void Scrollable::centreOn(size_t line)
131 {
132  if (line > getLineCount())
133  line = getLineCount();
134 
135  if (line < m_height)
136  {
137  scrollTo(0);
138  return;
139  }
140 
141  scrollTo(line - (m_height / 2));
142 }
143 
144 size_t Scrollable::height() const
145 {
146  return m_height;
147 }
148 size_t Scrollable::width() const
149 {
150  return m_width;
151 }
virtual void enableRefreshes()=0
virtual void drawString(const char *str, size_t row, size_t col, Colour foreColour, Colour backColour)=0
virtual void drawHorizontalLine(char c, size_t row, size_t colStart, size_t colEnd, Colour foreColour, Colour backColour)=0