summaryrefslogtreecommitdiff
path: root/plugingui/textedit.cc
blob: abfb3ab3e5baf782cf055702f9c2dcd0aa69e747 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            lineedit.cc
 *
 *  Tue Oct 21 11:25:26 CEST 2014
 *  Copyright 2014 Jonas Suhr Christensen
 *  jsc@umbraculum.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "textedit.h"

#include "window.h"

#include <assert.h>
#include <hugin.hpp>
#include <list>
#include <stdio.h>

#define BORDER 10

namespace GUI
{

TextEdit::TextEdit(Widget* parent) : Widget(parent), scroll(this)
{
	setReadOnly(true);

	scroll.move(width() - 23, 1);
	scroll.resize(16, 100);
	CONNECT(&scroll, valueChangeNotifier, this, &TextEdit::scrolled);
}

TextEdit::~TextEdit()
{
}

void TextEdit::resize(std::size_t width, std::size_t height)
{
	Widget::resize(width, height);
	scroll.resize(scroll.width(), height - 14);
	scroll.move(width - 23, 7);
}

void TextEdit::setReadOnly(bool readonly)
{
	this->readonly = readonly;
}

bool TextEdit::readOnly()
{
	return readonly;
}

void TextEdit::setText(const std::string& text)
{
	_text = text;

	preprocessText();
	redraw();
	textChangedNotifier();
}

std::string TextEdit::text()
{
	return _text;
}

void TextEdit::preprocessText()
{
	preprocessedtext.clear();
	std::string text = _text;

	{ // Handle tab characters
		for(size_t i = 0; i < text.length(); ++i)
		{
			char ch = text.at(i);
			if(ch == '\t')
			{
				text.erase(i, 1);
				text.insert(i, 4, ' ');
			}
		}
	}

	std::list<std::string> lines;
	{ // Handle new line characters
		size_t pos = 0;
		do
		{
			pos = text.find("\n");
			lines.push_back(text.substr(0, pos));
			text = text.substr(pos + 1);
		} while(pos != std::string::npos);
	}

	{ // Wrap long lines
		std::list<std::string>::iterator it;
		for(it = lines.begin(); it != lines.end(); ++it)
		{
			std::string line = *it;

			for(size_t i = 0; i < line.length(); ++i)
			{
				size_t linewidth = font.textWidth(line.substr(0, i));
				if(linewidth >= width() - BORDER - 20 - scroll.width())
				{
					preprocessedtext.push_back(line.substr(0, i));
					line = line.substr(i);
					i = 0;
				}
			}
			preprocessedtext.push_back(line);
		}
	}
}

void TextEdit::repaintEvent(RepaintEvent* repaintEvent)
{
	Painter p(*this);
	p.clear();

	// update values of scroll bar
	scroll.setRange(height() / font.textHeight());
	scroll.setMaximum(preprocessedtext.size());

	int w = width();
	int h = height();
	if((w == 0) || (h == 0))
	{
		return;
	}

	box.setSize(w, h);
	p.drawImage(0, 0, box);

	p.setColour(Colour(183.0 / 255.0, 219.0 / 255.0, 255.0 / 255.0, 1));

	int skip = scroll.value();

	int ypos = font.textHeight() + 5 + 1 + 1 + 1;
	std::list<std::string>::iterator it;
	it = preprocessedtext.begin();

	int c = 0;
	for(; c < skip; c++)
	{
		++it;
	}

	c = 0;
	for(; it != preprocessedtext.end(); it++)
	{
		if((c * font.textHeight()) >= (height() - 8 - font.textHeight()))
		{
			break;
		}

		std::string line = *it;
		p.drawText(BORDER - 4 + 3, ypos, font, line);
		ypos += font.textHeight();
		++c;
	}
}

void TextEdit::scrollEvent(ScrollEvent* scrollEvent)
{
	scroll.setValue(scroll.value() + scrollEvent->delta);
}

void TextEdit::scrolled(int value)
{
	(void)value;
	redraw();
}

} // GUI::