more PascalCase to camelCase conversions
[ncmpcpp.git] / src / scrollpad.cpp
blobd05f8b0628abd9d2c68e91e6690fffec8e490c65
1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <cassert>
23 #include "scrollpad.h"
24 #include "utility/wide_string.h"
26 namespace NC {//
28 Scrollpad::Scrollpad(size_t startx,
29 size_t starty,
30 size_t width,
31 size_t height,
32 const std::string &title,
33 Color color,
34 Border border)
35 : Window(startx, starty, width, height, title, color, border),
36 m_beginning(0),
37 m_found_value_begin(-1),
38 m_found_value_end(-1),
39 m_real_height(height)
43 void Scrollpad::flush()
45 m_real_height = 1;
47 std::wstring s = m_buffer.str();
49 size_t x = 0;
50 int x_pos = 0;
51 int space_pos = 0;
53 for (size_t i = 0; i < s.length(); ++i)
55 x += s[i] != '\t' ? wcwidth(s[i]) : 8-x%8; // tab size
57 if (s[i] == ' ') // if space, remember its position;
59 space_pos = i;
60 x_pos = x;
63 if (x >= m_width)
65 // if line is over, there was at least one space in this line and we are in the middle of the word, restore position to last known space and make it EOL
66 if (space_pos > 0 && (s[i] != ' ' || s[i+1] != ' '))
68 i = space_pos;
69 x = x_pos;
70 s[i] = '\n';
74 if (x >= m_width || s[i] == '\n')
76 m_real_height++;
77 x = 0;
78 space_pos = 0;
81 m_real_height = std::max(m_height, m_real_height);
82 recreate(m_width, m_real_height);
83 // print our modified string
84 std::swap(s, m_buffer.m_string);
85 static_cast<Window &>(*this) << m_buffer;
86 // restore original one
87 std::swap(s, m_buffer.m_string);
90 bool Scrollpad::setFormatting(short val_b, const std::wstring &s, short val_e, bool case_sensitive, bool for_each)
92 bool result = m_buffer.setFormatting(val_b, s, val_e, case_sensitive, for_each);
93 if (result)
95 m_found_for_each = for_each;
96 m_found_case_sensitive = case_sensitive;
97 m_found_value_begin = val_b;
98 m_found_value_end = val_e;
99 m_found_pattern = s;
101 else
102 forgetFormatting();
103 return result;
106 void Scrollpad::forgetFormatting()
108 m_found_value_begin = -1;
109 m_found_value_end = -1;
110 m_found_pattern.clear();
113 void Scrollpad::removeFormatting()
115 if (m_found_value_begin >= 0 && m_found_value_end >= 0)
116 m_buffer.removeFormatting(m_found_value_begin, m_found_pattern, m_found_value_end, m_found_case_sensitive, m_found_for_each);
119 void Scrollpad::refresh()
121 assert(m_real_height >= m_real_height);
122 size_t max_beginning = m_real_height - m_height;
123 m_beginning = std::min(m_beginning, max_beginning);
124 prefresh(m_window, m_beginning, 0, m_start_y, m_start_x, m_start_y+m_height-1, m_start_x+m_width-1);
127 void Scrollpad::resize(size_t new_width, size_t new_height)
129 adjustDimensions(new_width, new_height);
130 flush();
133 void Scrollpad::scroll(Where where)
135 assert(m_real_height >= m_height);
136 size_t max_beginning = m_real_height - m_height;
137 switch (where)
139 case wUp:
141 if (m_beginning > 0)
142 --m_beginning;
143 break;
145 case wDown:
147 if (m_beginning < max_beginning)
148 ++m_beginning;
149 break;
151 case wPageUp:
153 if (m_beginning > m_height)
154 m_beginning -= m_height;
155 else
156 m_beginning = 0;
157 break;
159 case wPageDown:
161 m_beginning = std::min(m_beginning + m_height, max_beginning);
162 break;
164 case wHome:
166 m_beginning = 0;
167 break;
169 case wEnd:
171 m_beginning = max_beginning;
172 break;
177 void Scrollpad::clear()
179 m_real_height = m_height;
180 m_buffer.clear();
181 wclear(m_window);
182 delwin(m_window);
183 m_window = newpad(m_height, m_width);
184 setTimeout(m_window_timeout);
185 setColor(m_color, m_bg_color);
186 forgetFormatting();
187 keypad(m_window, 1);
190 void Scrollpad::reset()
192 m_beginning = 0;
195 Scrollpad &Scrollpad::operator<<(const std::string &s)
197 m_buffer << ToWString(s);
198 return *this;