-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathCTextReader.cpp
More file actions
122 lines (97 loc) · 2.43 KB
/
CTextReader.cpp
File metadata and controls
122 lines (97 loc) · 2.43 KB
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
#include "CTextReader.h"
#include "CText.h"
#include "CVScrollBar.h"
#include <string.h>
#include "CSurfWindow.h"
#include "CApplication.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include<boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
CTextReader::CTextReader(int x, int y, int w, int h) : CForm(x, y, w, h)
{
using namespace std::placeholders;
int panelW = mPanel->Width();
mTextScroll = new CTextScroll();
mTextScroll->SetPosition(0, 0);
mTextScroll->AddTextPositionHandler(std::bind(&CTextReader::OnTextPositionChange, this, _1));
mPanel->AddWindow(mTextScroll);
mScrollBar = new CVScrollBar();
mScrollBar->AddOnScrollHandler(std::bind(&CTextReader::OnScroll, this, _1));
mScrollBar->SetPosition(mPanel->Width() - mScrollBar->Width(), 0);
mScrollBar->SetHeight(mPanel->GetHeight());
mPanel->AddWindow(mScrollBar);
mTextScroll->SetSize(Width() - mScrollBar->Width(), mPanel->GetHeight());
}
CTextReader::~CTextReader()
{
RemoveWindow(mScrollBar);
mPanel->RemoveWindow(mTextScroll);
delete(mScrollBar);
delete(mTextScroll);
}
void CTextReader::OnScroll(int position)
{
if (position < 0)
position = 0;
if (position != mTextScroll->mPosition)
{
mTextScroll->SetScroll(position);
}
}
void CTextReader::OnTextPositionChange(CTextScroll *scroll)
{
if (scroll->mPosition != mScrollBar->mPosition)
{
mScrollBar->SetPos(scroll->mPosition, false);
}
}
void CTextReader::AddLine(string text)
{
mTextScroll->AddLine(text);
mScrollBar->SetRange(0, mTextScroll->mText.size()*TTF_FontHeight(CText::sFontSmall) - mTextScroll->mParent->GetHeight());
}
void CTextReader::SetText(std::string text, std::string caption)
{
char* line;
const char c[2] = "\n";
line = strtok((char*)text.c_str(), c);
mTitleBar.SetTitle(caption);
while (line != NULL)
{
AddLine(line);
line = strtok(NULL, c);
}
mTitleBar.SetTitle(caption);
}
void CTextReader::SetTtile(std::string title)
{
mTitleBar.SetTitle(title);
}
void CTextReader::LoadFile(std::string filename)
{
mTextScroll->mText.clear();
ifstream file(filename.c_str(), ios::in);
if (!file.is_open())
{
printf("Can't find file.\n");
return;
}
string line;
int i = 1;
while (getline(file, line))
{
boost::algorithm::replace_all(line, "\r", "");
boost::algorithm::replace_all(line, "\n", "");
if (line == "")
{
line = " ";
}
AddLine(line);
i++;
}
file.close();
mTextScroll->SetScroll(0);
}