-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinenumbers.cpp
More file actions
127 lines (97 loc) · 3.41 KB
/
Copy pathlinenumbers.cpp
File metadata and controls
127 lines (97 loc) · 3.41 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
123
124
125
126
127
#include "linenumbers.h"
#include "text_renderer.h"
#include "application.h"
LineNumbers::LineNumbers(Widget* parent) : Widget(parent) {
id = icu::UnicodeString::fromUTF8("LineNumbers");
}
void LineNumbers::render() {
// we do this here because we have to do this after the textedit has updated their scroll
t_h = toFollow->t_h; // there's a reason I promise. Prevents 1 frame lagging of size.
int number_of_lines = toFollow->lines.size();
int max_len = std::to_string(number_of_lines).length();
double text_scroll = toFollow->scrolled_to_vert; // messured in lines
int line_start = floor(text_scroll);
start_y = -fmod(text_scroll, 1) * TextRenderer::get_text_height();
lines_to_draw.clear();
lines_to_color.clear();
int num_lines = ceil((float)t_h/(float)TextRenderer::get_text_height()) + 1;
int cursor_line = toFollow->cursors[0].head_line+1;
for (int cur_line = line_start+1; cur_line < line_start+1+num_lines+1; cur_line++) {
if (cur_line > number_of_lines) {
break;
}
if (App::settings->getValue("use_rel_line_num", false)) {
int dst_to_crsr = abs(cursor_line-cur_line);
Color* fillcolor = App::theme.lesser_text_color;
std::string line_text = "";
if (dst_to_crsr == 0) {
dst_to_crsr = cursor_line;
line_text = std::to_string(dst_to_crsr);
fillcolor = App::theme.main_text_color;
}else{
line_text = std::to_string(dst_to_crsr);
int len = line_text.length();
int spaces = max_len-len;
for (int s = 0; s < spaces; s++) {
line_text = " " + line_text;
}
}
icu::UnicodeString text = icu::UnicodeString::fromUTF8(line_text);
lines_to_draw.push_back(text);
lines_to_color.push_back(fillcolor);
}else{
Color* fillcolor = App::theme.lesser_text_color;
std::string line_text = std::to_string(cur_line);
if (cur_line == cursor_line) {
fillcolor = App::theme.main_text_color;
}
int len = line_text.length();
int spaces = max_len-len;
for (int s = 0; s < spaces; s++) {
line_text = " " + line_text;
}
icu::UnicodeString text = icu::UnicodeString::fromUTF8(line_text);
lines_to_draw.push_back(text);
lines_to_color.push_back(fillcolor);
}
}
if (rounded) {
App::DrawRoundedRect(t_x, t_y, t_w, t_h, App::text_padding, App::theme.extras_background_color);
if (border) {
App::DrawRoundBorder(t_x, t_y, t_w, t_h, App::theme.border, 5, App::text_padding);
}
}else{
App::DrawRect(t_x, t_y, t_w, t_h, App::theme.extras_background_color);
if (border) {
App::DrawRect(t_x+t_w-1, t_y, 1, t_h, App::theme.border);
// App::DrawBorder(t_x, t_y, t_w, t_h, App::theme.border);
}
}
int curx = t_x+App::text_padding;
int cury = start_y+t_y+App::text_padding;
for (int ln_ren = 0; ln_ren < lines_to_draw.size(); ln_ren++) {
TextRenderer::draw_text(curx, cury, lines_to_draw[ln_ren], lines_to_color[ln_ren]);
cury += TextRenderer::get_text_height();
}
Widget::render();
}
void LineNumbers::position(int x, int y, int w, int h) {
t_x = x;
t_y = y;
t_h = h;
int number_of_lines = toFollow->lines.size();
int max_len = std::to_string(number_of_lines).length();
if (!toFollow) {
t_w = 0;
Widget::position(t_x, t_y, t_w, t_h);
return;
}else{
t_w = TextRenderer::get_text_width(max_len) + App::text_padding*2;
t_h = toFollow->t_h;
t_y = toFollow->t_y;
}
Widget::position(t_x, t_y, t_w, t_h);
}
void LineNumbers::setTextedit(TextEdit* edit) {
toFollow = edit;
}