summaryrefslogtreecommitdiff
path: root/src/scrollpad.cpp
blob: 58d918e99a24b67ede0052458bcebe5cefa81bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/***************************************************************************
 *   Copyright (C) 2008 by Andrzej Rybczak   *
 *   electricityispower@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "scrollpad.h"

Scrollpad::Scrollpad(const Scrollpad &s) : Window(s)
{
	itsContent = s.itsContent;
	itsRawContent = s.itsRawContent;
	itsBeginning = s.itsBeginning;
	itsRealHeight = s.itsRealHeight;
	itsXPos = s.itsXPos;
}

void Scrollpad::Add(string str)
{
	if (itsXPos > 0 && (str[0] != ' ' || str[0] != '\n'))
		str = " " + str;
	
	itsRawContent += str;
	
#	ifdef UTF8_ENABLED
	wstring s = ToWString(str);
	wstring tmp;
#	else
	string &s = str;
	string tmp;
#	endif
	
	int x_pos = 0;
	int space_pos = 0;
	int tab_size;
	bool collect = 0;
	
	for (size_t i = 0; i < s.length(); i++)
	{
		tab_size = 8-itsXPos%8;
		
		if (s[i] != '\t')
		{
#			ifdef UTF8_ENABLED
			itsXPos += wcwidth(s[i]);
#			else
			itsXPos++;
#			endif
		}
		else
			itsXPos += tab_size;
		
		if (BBEnabled)
		{
			if (s[i] == '[' && (s[i+1] == '.' || s[i+1] == '/'))
				collect = 1;
			
			if (collect)
			{
				if (s[i] != '[')
				{
					tmp += s[i];
					if (tmp.length() > 10) // the longest bbcode is 10 chars long
						collect = 0;
				}
				else
					tmp = s[i];
			}
			
			if (s[i] == ']')
				collect = 0;
			
			if (!collect && !tmp.empty())
			{
				if (IsValidColor(TO_STRING(tmp)))
					itsXPos -= tmp.length();
				tmp.clear();
			}
		}
		
		if (s[i] == ' ') // if space, remember its position;
		{
			space_pos = i;
			x_pos = itsXPos;
		}
		
		if (!collect && itsXPos >= itsWidth)
		{
			// 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
			if (space_pos > 0 && (s[i] != ' ' || s[i+1] != ' '))
			{
				i = space_pos;
				itsXPos = x_pos;
				s[i] = '\n';
			}
		}
		
		if (!collect && (itsXPos >= itsWidth || s[i] == '\n'))
		{
			itsRealHeight++;
			itsXPos = 0;
			space_pos = 0;
		}
	}
	itsContent += TO_STRING(s);
	Recreate();
}

void Scrollpad::Recreate()
{
	delwin(itsWindow);
	itsWindow = newpad(itsRealHeight, itsWidth);
	SetTimeout(itsWindowTimeout);
	SetColor(itsBaseColor, itsBgColor);
	Write(itsContent.c_str());
}

void Scrollpad::Refresh(bool)
{
	prefresh(itsWindow,itsBeginning,0,itsStartY,itsStartX,itsStartY+itsHeight-1,itsStartX+itsWidth);
}

void Scrollpad::Resize(int width, int height)
{
	if (itsBorder != brNone)
	{
		delwin(itsWinBorder);
		itsWinBorder = newpad(height, width);
		wattron(itsWinBorder,COLOR_PAIR(itsBorder));
		box(itsWinBorder,0,0);
		width -= 2;
		height -= 2;
	}
	if (!itsTitle.empty())
		width -= 2;
	
	if (height > 0 && width > 0)
	{
		itsHeight = height;
		itsWidth = width;
		
		itsBeginning = 0;
		itsRealHeight = 1;
		itsXPos = 0;
		itsContent.clear();
		string tmp = itsRawContent;
		itsRawContent.clear();
		Add(tmp);
		Recreate();
	}
}

void Scrollpad::Go(Where where)
{
	int MaxBeginning = itsContent.size() < itsHeight ? 0 : itsRealHeight-itsHeight;
	
	switch (where)
	{
		case wUp:
		{
			if (itsBeginning > 0) itsBeginning--; // for scrolling
			break;
		}
		case wDown:
		{
			if (itsBeginning < MaxBeginning) itsBeginning++; // scroll
			break;
		}
		case wPageUp:
		{
			itsBeginning -= itsHeight;
			if (itsBeginning < 0) itsBeginning = 0;
			break;
		}
		case wPageDown:
		{
			itsBeginning += itsHeight;
			if (itsBeginning > MaxBeginning) itsBeginning = MaxBeginning;
			break;
		}
		case wHome:
		{
			itsBeginning = 0;
			break;
		}
		case wEnd:
		{
			itsBeginning = MaxBeginning;
			break;
		}
	}
}

void Scrollpad::Clear(bool clear_screen)
{
	itsBeginning = 0;
	itsRealHeight = 1;
	itsXPos = 0;
	itsContent.clear();
	itsRawContent.clear();
	wclear(itsWindow);
	delwin(itsWindow);
	itsWindow = newpad(itsHeight, itsWidth);
	SetColor(itsColor, itsBgColor);
	SetTimeout(itsWindowTimeout);
	if (clear_screen)
		Window::Clear();
}

Window * Scrollpad::EmptyClone() const
{
	return new Scrollpad(GetStartX(),GetStartY(),GetWidth(),GetHeight(),itsTitle,itsBaseColor,itsBorder);
}