File: TextView.cpp

package info (click to toggle)
codelite 14.0+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (227 lines) | stat: -rw-r--r-- 6,530 bytes parent folder | download
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
#include "TextView.h"
#include <wx/sizer.h>
#include "wxTerminalOptions.h"
#include <wx/wupdlock.h>

TextView::TextView(wxWindow* parent, wxWindowID winid)
    : wxWindow(parent, winid)
{
    SetSizer(new wxBoxSizer(wxVERTICAL));
#if USE_STC
    m_ctrl = new wxStyledTextCtrl(this, wxID_ANY);
    m_ctrl->SetCaretStyle(wxSTC_CARETSTYLE_BLOCK);
    m_ctrl->SetYCaretPolicy(wxSTC_CARET_STRICT | wxSTC_CARET_SLOP, 4);
    m_ctrl->SetLexer(wxSTC_LEX_CONTAINER);
#if wxCHECK_VERSION(3, 1, 1)
    m_ctrl->StartStyling(0);
#else
    m_ctrl->StartStyling(0, 0x1f);
#endif
    m_ctrl->SetWrapMode(wxSTC_WRAP_CHAR);
#else
    m_ctrl = new TextCtrl_t(this, wxID_ANY, "", wxDefaultPosition, wxDefaultSize,
                            wxTE_MULTILINE | wxTE_RICH | wxTE_PROCESS_ENTER | wxTE_NOHIDESEL | wxTE_PROCESS_TAB);
#endif

#if defined(__WXOSX__) && !USE_STC
    m_ctrl->OSXDisableAllSmartSubstitutions();
#endif

    GetSizer()->Add(m_ctrl, 1, wxEXPAND);
    GetSizer()->Fit(this);
    ReloadSettings();
    m_colourHandler.SetCtrl(this);
}

TextView::~TextView() {}

void TextView::AppendText(const wxString& buffer)
{
    m_ctrl->AppendText(buffer);
#if USE_STC
    m_ctrl->SetStyling(buffer.Length(), GetCurrentStyle());
#endif
}

long TextView::GetLastPosition() const { return m_ctrl->GetLastPosition(); }

wxString TextView::GetRange(int from, int to) const { return m_ctrl->GetRange(from, to); }

bool TextView::PositionToXY(long pos, long* x, long* y) const { return m_ctrl->PositionToXY(pos, x, y); }

long TextView::XYToPosition(long x, long y) const { return m_ctrl->XYToPosition(x, y); }

void TextView::Remove(long from, long to) { return m_ctrl->Remove(from, to); }

void TextView::SetInsertionPoint(long pos) { m_ctrl->SetInsertionPoint(pos); }

void TextView::SelectNone() { m_ctrl->SelectNone(); }

void TextView::SetInsertionPointEnd() { m_ctrl->SetInsertionPointEnd(); }

int TextView::GetNumberOfLines() const { return m_ctrl->GetNumberOfLines(); }

void TextView::SetDefaultStyle(const wxTextAttr& attr)
{
#if USE_STC
    m_defaultAttr = attr;
#if wxCHECK_VERSION(3, 1, 1)
    m_ctrl->StartStyling(m_ctrl->GetLastPosition());
#else
    m_ctrl->StartStyling(m_ctrl->GetLastPosition(), 0x1f);
#endif
#else
    m_ctrl->SetDefaultStyle(attr);
#endif
}

wxTextAttr TextView::GetDefaultStyle() const
{
#if USE_STC
    return m_defaultAttr;
#else
    return m_ctrl->GetDefaultStyle();
#endif
}

bool TextView::IsEditable() const { return m_ctrl->IsEditable(); }

long TextView::GetInsertionPoint() const { return m_ctrl->GetInsertionPoint(); }

void TextView::Replace(long from, long to, const wxString& replaceWith) { m_ctrl->Replace(from, to, replaceWith); }

wxString TextView::GetLineText(int lineNumber) const { return m_ctrl->GetLineText(lineNumber); }

void TextView::SetEditable(bool b) { m_ctrl->SetEditable(b); }

void TextView::ReloadSettings()
{
#if USE_STC
    wxFont font = wxTerminalOptions::Get().GetFont();
    wxColour textColour = wxTerminalOptions::Get().GetTextColour();
    wxColour bgColour = wxTerminalOptions::Get().GetBgColour();
    SetBackgroundColour(bgColour);
    SetForegroundColour(textColour);
    for(int i = 0; i < wxSTC_STYLE_MAX; ++i) {
        m_ctrl->StyleSetBackground(i, bgColour);
        m_ctrl->StyleSetForeground(i, textColour);
        m_ctrl->StyleSetFont(i, font);
    }
    m_ctrl->SetCaretForeground(textColour);
    m_ctrl->SetBackgroundColour(bgColour);
    m_ctrl->SetForegroundColour(textColour);
    wxTextAttr defaultAttr = wxTextAttr(textColour, bgColour, font);
    SetDefaultStyle(defaultAttr);
    m_colourHandler.SetDefaultStyle(defaultAttr);
    m_ctrl->Refresh();
#else
    wxFont font = wxTerminalOptions::Get().GetFont();
    wxColour textColour = wxTerminalOptions::Get().GetTextColour();
    wxColour bgColour = wxTerminalOptions::Get().GetBgColour();
    SetBackgroundColour(bgColour);
    SetForegroundColour(textColour);
    m_ctrl->SetBackgroundColour(bgColour);
    m_ctrl->SetForegroundColour(textColour);
    wxTextAttr defaultAttr = wxTextAttr(textColour, bgColour, font);
    SetDefaultStyle(defaultAttr);
    m_colourHandler.SetDefaultStyle(defaultAttr);
    m_ctrl->Refresh();
#endif
}

void TextView::StyleAndAppend(const wxString& buffer) { m_colourHandler << buffer; }

void TextView::Focus() { m_ctrl->SetFocus(); }

void TextView::ShowCommandLine()
{
#if USE_STC
    m_ctrl->SetSelection(m_ctrl->GetLastPosition(), m_ctrl->GetLastPosition());
    m_ctrl->EnsureCaretVisible();
#else
    m_ctrl->SetInsertionPointEnd();
    m_ctrl->ShowPosition(m_ctrl->GetLastPosition());
#endif
}

void TextView::SetCommand(long from, const wxString& command)
{
#if USE_STC
    m_ctrl->SetSelection(from, GetLastPosition());
    m_ctrl->ReplaceSelection(command);
#else
    m_ctrl->Replace(from, GetLastPosition(), command);
#endif
}

void TextView::SetCaretEnd()
{
#if USE_STC
    m_ctrl->SetSelection(GetLastPosition(), GetLastPosition());
    m_ctrl->SetCurrentPos(GetLastPosition());
    m_ctrl->SetFocus();
#else
    SelectNone();
    SetInsertionPointEnd();
    CallAfter(&TextView::Focus);
#endif
}

int TextView::Truncate()
{
    if(GetNumberOfLines() > 1000) {
        // Start removing lines from the top
        long linesToRemove = (GetNumberOfLines() - 1000);
        long startPos = 0;
        long endPos = XYToPosition(0, linesToRemove);
        this->Remove(startPos, endPos);
        return endPos - startPos;
    }
    return 0;
}

wxChar TextView::GetLastChar() const
{
#if USE_STC
    return m_ctrl->GetCharAt(m_ctrl->GetLastPosition());
#else
    wxString text = m_ctrl->GetRange(m_ctrl->GetLastPosition() - 1, m_ctrl->GetLastPosition());
    return text[0];
#endif
}

int TextView::GetCurrentStyle()
{
#if USE_STC
#if 0
    wxString key;
    wxFont f = m_defaultAttr.GetFont();
    const wxColour& bg = m_defaultAttr.GetBackgroundColour();
    const wxColour& textColour = m_defaultAttr.GetTextColour();
    key << bg.GetAsString() << ":" << textColour.GetAsString() << ":" << (f.GetWeight() == wxFONTWEIGHT_BOLD);
    if(m_styles.count(key)) { return m_styles[key]; }
    ++m_nextStyle;

    m_ctrl->StyleSetBackground(m_nextStyle, bg);
    m_ctrl->StyleSetForeground(m_nextStyle, textColour);
    m_ctrl->StyleSetFont(m_nextStyle, f);
    return m_nextStyle;
#endif
    return 0;
#else
    return wxNOT_FOUND;
#endif
}

void TextView::Clear()
{
#if USE_STC
    m_ctrl->ClearAll();
    m_ctrl->ClearDocumentStyle();
#if wxCHECK_VERSION(3, 1, 1)
    m_ctrl->StartStyling(0);
#else
    m_ctrl->StartStyling(0, 0x1f);
#endif
#endif
}