1 /***************************************************************************
2 * Copyright (C) 2008-2009 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"
25 using namespace NCurses
;
27 Scrollpad::Scrollpad(size_t startx
,
31 const std::string
&title
,
34 : Window(startx
, starty
, width
, height
, title
, color
, border
),
37 itsFoundValueBegin(-1),
43 Scrollpad::Scrollpad(const Scrollpad
&s
) : Window(s
)
45 itsBuffer
<< s
.itsBuffer
;
46 itsBeginning
= s
.itsBeginning
;
47 itsRealHeight
= s
.itsRealHeight
;
50 void Scrollpad::Flush()
54 std::basic_string
<my_char_t
> s
= itsBuffer
.Str();
60 for (size_t i
= 0; i
< s
.length(); ++i
)
62 x
+= s
[i
] != '\t' ? wcwidth(s
[i
]) : 8-x
%8; // tab size
64 if (s
[i
] == ' ') // if space, remember its position;
72 // 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
73 if (space_pos
> 0 && (s
[i
] != ' ' || s
[i
+1] != ' '))
81 if (x
>= itsWidth
|| s
[i
] == '\n')
88 itsRealHeight
= std::max(itsHeight
, itsRealHeight
);
89 Recreate(itsWidth
, itsRealHeight
);
90 itsBuffer
.SetTemp(&s
);
91 static_cast<Window
&>(*this) << itsBuffer
;
95 bool Scrollpad::SetFormatting(short val_b
, const std::basic_string
<my_char_t
> &s
, short val_e
, bool for_each
)
97 bool result
= itsBuffer
.SetFormatting(val_b
, s
, val_e
, for_each
);
100 itsFoundForEach
= for_each
;
101 itsFoundValueBegin
= val_b
;
102 itsFoundValueEnd
= val_e
;
110 void Scrollpad::ForgetFormatting()
113 itsFoundValueBegin
= -1;
114 itsFoundValueEnd
= -1;
115 itsFoundPattern
.clear();
118 void Scrollpad::RemoveFormatting()
120 if (itsFoundValueBegin
>= 0 && itsFoundValueEnd
>= 0)
121 itsBuffer
.RemoveFormatting(itsFoundValueBegin
, itsFoundPattern
, itsFoundValueEnd
, itsFoundForEach
);
124 void Scrollpad::Refresh()
126 int MaxBeginning
= itsRealHeight
-itsHeight
;
127 assert(MaxBeginning
>= 0);
128 if (itsBeginning
> MaxBeginning
)
129 itsBeginning
= MaxBeginning
;
130 prefresh(itsWindow
, itsBeginning
, 0, itsStartY
, itsStartX
, itsStartY
+itsHeight
-1, itsStartX
+itsWidth
-1);
133 void Scrollpad::Resize(size_t new_width
, size_t new_height
)
135 AdjustDimensions(new_width
, new_height
);
139 void Scrollpad::Scroll(Where where
)
141 int MaxBeginning
= /*itsContent.size() < itsHeight ? 0 : */itsRealHeight
-itsHeight
;
147 if (itsBeginning
> 0)
153 if (itsBeginning
< MaxBeginning
)
159 itsBeginning
-= itsHeight
;
160 if (itsBeginning
< 0)
166 itsBeginning
+= itsHeight
;
167 if (itsBeginning
> MaxBeginning
)
168 itsBeginning
= MaxBeginning
;
178 itsBeginning
= MaxBeginning
;
184 void Scrollpad::Clear(bool clear_screen
)
186 itsRealHeight
= itsHeight
;
190 itsWindow
= newpad(itsHeight
, itsWidth
);
191 SetTimeout(itsWindowTimeout
);
192 SetColor(itsColor
, itsBgColor
);
194 keypad(itsWindow
, 1);
199 void Scrollpad::Reset()
205 Scrollpad
&Scrollpad::operator<<(const std::string
&s
)
207 itsBuffer
<< ToWString(s
);