1 /***************************************************************************
2 * Copyright (C) 2008-2012 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
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. *
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. *
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 ***************************************************************************/
23 #include "scrollpad.h"
24 #include "utility/wide_string.h"
28 Scrollpad::Scrollpad(size_t startx
,
32 const std::string
&title
,
35 : Window(startx
, starty
, width
, height
, title
, color
, border
),
37 m_found_value_begin(-1),
38 m_found_value_end(-1),
43 void Scrollpad::flush()
47 std::wstring s
= m_buffer
.str();
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;
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] != ' '))
74 if (x
>= m_width
|| s
[i
] == '\n')
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
);
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
;
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
);
133 void Scrollpad::scroll(Where where
)
135 assert(m_real_height
>= m_height
);
136 size_t max_beginning
= m_real_height
- m_height
;
147 if (m_beginning
< max_beginning
)
153 if (m_beginning
> m_height
)
154 m_beginning
-= m_height
;
161 m_beginning
= std::min(m_beginning
+ m_height
, max_beginning
);
171 m_beginning
= max_beginning
;
177 void Scrollpad::clear()
179 m_real_height
= m_height
;
183 m_window
= newpad(m_height
, m_width
);
184 setTimeout(m_window_timeout
);
185 setColor(m_color
, m_bg_color
);
190 void Scrollpad::reset()
195 Scrollpad
&Scrollpad::operator<<(const std::string
&s
)
197 m_buffer
<< ToWString(s
);