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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
/*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/** \file
* \ingroup spinfo
*/
#include "MEM_guardedalloc.h"
#include "BLF_api.h"
#include "BLI_math.h"
#include "BLI_string_utf8.h"
#include "BLI_utildefines.h"
#include "GPU_immediate.h"
#include "GPU_state.h"
#include "DNA_userdef_types.h" /* For 'U.dpi_fac' */
#include "UI_interface.h"
#include "UI_interface_icons.h"
#include "textview.h"
static void textview_font_begin(const int font_id, const int lheight)
{
/* Font size in relation to line height. */
BLF_size(font_id, 0.8f * lheight, 72);
}
typedef struct TextViewDrawState {
int font_id;
int cwidth;
int lheight;
/** Text vertical offset per line. */
int lofs;
int row_vpadding;
/** Number of characters that fit into the width of the console (fixed width). */
int columns;
/** For drawing text. */
const rcti *draw_rect;
/** For drawing backgrounds colors which may extend beyond text. */
const rcti *draw_rect_outer;
int scroll_ymin, scroll_ymax;
int *xy; // [2]
int *sel; // [2]
/* Bottom of view == 0, top of file == combine chars, end of line is lower then start. */
int *mval_pick_offset;
const int *mval; // [2]
bool do_draw;
} TextViewDrawState;
BLI_INLINE void textview_step_sel(TextViewDrawState *tds, const int step)
{
tds->sel[0] += step;
tds->sel[1] += step;
}
static void textview_draw_sel(const char *str,
const int xy[2],
const int str_len_draw,
TextViewDrawState *tds,
const uchar bg_sel[4])
{
const int sel[2] = {tds->sel[0], tds->sel[1]};
const int cwidth = tds->cwidth;
const int lheight = tds->lheight;
if (sel[0] <= str_len_draw && sel[1] >= 0) {
const int sta = BLI_str_utf8_offset_to_column(str, max_ii(sel[0], 0));
const int end = BLI_str_utf8_offset_to_column(str, min_ii(sel[1], str_len_draw));
GPU_blend(true);
GPU_blend_set_func_separate(
GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
GPUVertFormat *format = immVertexFormat();
uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
immUniformColor4ubv(bg_sel);
immRecti(pos, xy[0] + (cwidth * sta), xy[1] + lheight, xy[0] + (cwidth * end), xy[1]);
immUnbindProgram();
GPU_blend(false);
}
}
/**
* \warning Allocated memory for 'r_offsets' must be freed by caller.
* \return The length in bytes.
*/
static int textview_wrap_offsets(
const char *str, int len, int width, int *r_lines, int **r_offsets)
{
int i, end; /* Offset as unicode code-point. */
int j; /* Offset as bytes. */
*r_lines = 1;
*r_offsets = MEM_callocN(
sizeof(**r_offsets) *
(len * BLI_UTF8_WIDTH_MAX / MAX2(1, width - (BLI_UTF8_WIDTH_MAX - 1)) + 1),
__func__);
(*r_offsets)[0] = 0;
for (i = 0, end = width, j = 0; j < len && str[j]; j += BLI_str_utf8_size_safe(str + j)) {
int columns = BLI_str_utf8_char_width_safe(str + j);
if (i + columns > end) {
(*r_offsets)[*r_lines] = j;
(*r_lines)++;
end = i + width;
}
i += columns;
}
return j;
}
/**
* return false if the last line is off the screen
* should be able to use this for any string type.
*/
static bool textview_draw_string(TextViewDrawState *tds,
const char *str,
int str_len,
const uchar fg[4],
const uchar bg[4],
int icon,
const uchar icon_fg[4],
const uchar icon_bg[4],
const uchar bg_sel[4])
{
int tot_lines; /* Total number of lines for wrapping. */
int *offsets; /* Offsets of line beginnings for wrapping. */
str_len = textview_wrap_offsets(str, str_len, tds->columns, &tot_lines, &offsets);
int line_height = (tot_lines * tds->lheight) + (tds->row_vpadding * 2);
int line_bottom = tds->xy[1];
int line_top = line_bottom + line_height;
int y_next = line_top;
/* Just advance the height. */
if (tds->do_draw == false) {
if (tds->mval_pick_offset && tds->mval[1] != INT_MAX && line_bottom <= tds->mval[1]) {
if (y_next >= tds->mval[1]) {
int ofs = 0;
/* Wrap. */
if (tot_lines > 1) {
int iofs = (int)((float)(y_next - tds->mval[1]) / tds->lheight);
ofs += offsets[MIN2(iofs, tot_lines - 1)];
}
/* Last part. */
ofs += BLI_str_utf8_offset_from_column(str + ofs,
(int)floor((float)tds->mval[0] / tds->cwidth));
CLAMP(ofs, 0, str_len);
*tds->mval_pick_offset += str_len - ofs;
}
else {
*tds->mval_pick_offset += str_len + 1;
}
}
tds->xy[1] = y_next;
MEM_freeN(offsets);
return true;
}
else if (y_next < tds->scroll_ymin) {
/* Have not reached the drawable area so don't break. */
tds->xy[1] = y_next;
/* Adjust selection even if not drawing. */
if (tds->sel[0] != tds->sel[1]) {
textview_step_sel(tds, -(str_len + 1));
}
MEM_freeN(offsets);
return true;
}
size_t len;
const char *s;
int i;
int sel_orig[2];
copy_v2_v2_int(sel_orig, tds->sel);
/* Invert and swap for wrapping. */
tds->sel[0] = str_len - sel_orig[1];
tds->sel[1] = str_len - sel_orig[0];
if (bg) {
GPUVertFormat *format = immVertexFormat();
uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
immUniformColor4ubv(bg);
immRecti(pos, tds->draw_rect_outer->xmin, line_bottom, tds->draw_rect_outer->xmax, line_top);
immUnbindProgram();
}
if (icon_bg) {
float col[4];
int bg_size = UI_DPI_ICON_SIZE * 1.2;
float vpadding = (tds->lheight + (tds->row_vpadding * 2) - bg_size) / 2;
float hpadding = tds->draw_rect->xmin - (bg_size * 1.2f);
rgba_uchar_to_float(col, icon_bg);
UI_draw_roundbox_corner_set(UI_CNR_ALL);
UI_draw_roundbox_aa(true,
hpadding,
line_top - bg_size - vpadding,
bg_size + hpadding,
line_top - vpadding,
4 * UI_DPI_FAC,
col);
}
if (icon) {
int vpadding = (tds->lheight + (tds->row_vpadding * 2) - UI_DPI_ICON_SIZE) / 2;
int hpadding = tds->draw_rect->xmin - (UI_DPI_ICON_SIZE * 1.3f);
GPU_blend(true);
UI_icon_draw_ex(hpadding,
line_top - UI_DPI_ICON_SIZE - vpadding,
icon,
(16 / UI_DPI_ICON_SIZE),
1.0f,
0.0f,
icon_fg,
false);
GPU_blend(false);
}
tds->xy[1] += tds->row_vpadding;
/* Last part needs no clipping. */
const int final_offset = offsets[tot_lines - 1];
len = str_len - final_offset;
s = str + final_offset;
BLF_position(tds->font_id, tds->xy[0], tds->lofs + line_bottom + tds->row_vpadding, 0);
BLF_color4ubv(tds->font_id, fg);
BLF_draw_mono(tds->font_id, s, len, tds->cwidth);
if (tds->sel[0] != tds->sel[1]) {
textview_step_sel(tds, -final_offset);
int pos[2] = {tds->xy[0], line_bottom};
textview_draw_sel(s, pos, len, tds, bg_sel);
}
tds->xy[1] += tds->lheight;
BLF_color4ubv(tds->font_id, fg);
for (i = tot_lines - 1; i > 0; i--) {
len = offsets[i] - offsets[i - 1];
s = str + offsets[i - 1];
BLF_position(tds->font_id, tds->xy[0], tds->lofs + tds->xy[1], 0);
BLF_draw_mono(tds->font_id, s, len, tds->cwidth);
if (tds->sel[0] != tds->sel[1]) {
textview_step_sel(tds, len);
textview_draw_sel(s, tds->xy, len, tds, bg_sel);
}
tds->xy[1] += tds->lheight;
/* Check if we're out of view bounds. */
if (tds->xy[1] > tds->scroll_ymax) {
MEM_freeN(offsets);
return false;
}
}
tds->xy[1] = y_next;
copy_v2_v2_int(tds->sel, sel_orig);
textview_step_sel(tds, -(str_len + 1));
MEM_freeN(offsets);
return true;
}
/**
* \param r_mval_pick_item: The resulting item clicked on using \a mval_init.
* Set from the void pointer which holds the current iterator.
* It's type depends on the data being iterated over.
* \param r_mval_pick_offset: The offset in bytes of the \a mval_init.
* Use for selection.
*/
int textview_draw(TextViewContext *tvc,
const bool do_draw,
const int mval_init[2],
void **r_mval_pick_item,
int *r_mval_pick_offset)
{
TextViewDrawState tds = {0};
const int x_orig = tvc->draw_rect.xmin, y_orig = tvc->draw_rect.ymin;
int xy[2];
/* Disable selection by. */
int sel[2] = {-1, -1};
uchar fg[4], bg[4], icon_fg[4], icon_bg[4];
int icon = 0;
const int font_id = blf_mono_font;
textview_font_begin(font_id, tvc->lheight);
xy[0] = x_orig;
xy[1] = y_orig;
/* Offset and clamp the results,
* clamping so moving the cursor out of the bounds doesn't wrap onto the other lines. */
const int mval[2] = {
(mval_init[0] == INT_MAX) ?
INT_MAX :
CLAMPIS(mval_init[0], tvc->draw_rect.xmin, tvc->draw_rect.xmax) - tvc->draw_rect.xmin,
(mval_init[1] == INT_MAX) ?
INT_MAX :
CLAMPIS(mval_init[1], tvc->draw_rect.ymin, tvc->draw_rect.ymax) + tvc->scroll_ymin,
};
if (r_mval_pick_offset != NULL) {
*r_mval_pick_offset = 0;
}
/* Constants for the text-view context. */
tds.font_id = font_id;
tds.cwidth = (int)BLF_fixed_width(font_id);
BLI_assert(tds.cwidth > 0);
tds.lheight = tvc->lheight;
tds.row_vpadding = tvc->row_vpadding;
tds.lofs = -BLF_descender(font_id);
/* Note, scroll bar must be already subtracted. */
tds.columns = (tvc->draw_rect.xmax - tvc->draw_rect.xmin) / tds.cwidth;
/* Avoid divide by zero on small windows. */
if (tds.columns < 1) {
tds.columns = 1;
}
tds.draw_rect = &tvc->draw_rect;
tds.draw_rect_outer = &tvc->draw_rect_outer;
tds.scroll_ymin = tvc->scroll_ymin;
tds.scroll_ymax = tvc->scroll_ymax;
tds.xy = xy;
tds.sel = sel;
tds.mval_pick_offset = r_mval_pick_offset;
tds.mval = mval;
tds.do_draw = do_draw;
if (tvc->sel_start != tvc->sel_end) {
sel[0] = tvc->sel_start;
sel[1] = tvc->sel_end;
}
if (tvc->begin(tvc)) {
uchar bg_sel[4] = {0};
if (do_draw && tvc->const_colors) {
tvc->const_colors(tvc, bg_sel);
}
int iter_index = 0;
do {
const char *ext_line;
int ext_len;
int data_flag = 0;
const int y_prev = xy[1];
if (do_draw) {
data_flag = tvc->line_data(tvc, fg, bg, &icon, icon_fg, icon_bg);
}
tvc->line_get(tvc, &ext_line, &ext_len);
const bool is_out_of_view_y = !textview_draw_string(
&tds,
ext_line,
ext_len,
(data_flag & TVC_LINE_FG) ? fg : NULL,
(data_flag & TVC_LINE_BG) ? bg : NULL,
(data_flag & TVC_LINE_ICON) ? icon : 0,
(data_flag & TVC_LINE_ICON_FG) ? icon_fg : NULL,
(data_flag & TVC_LINE_ICON_BG) ? icon_bg : NULL,
bg_sel);
if (do_draw) {
/* We always want the cursor to draw. */
if (tvc->draw_cursor && iter_index == 0) {
tvc->draw_cursor(tvc, tds.cwidth, tds.columns, tds.lofs);
}
/* When drawing, if we pass v2d->cur.ymax, then quit. */
if (is_out_of_view_y) {
break;
}
}
if ((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
*r_mval_pick_item = (void *)tvc->iter;
break;
}
iter_index++;
} while (tvc->step(tvc));
}
tvc->end(tvc);
/* Sanity checks (bugs here can be tricky to track down). */
BLI_assert(tds.lheight == tvc->lheight);
BLI_assert(tds.row_vpadding == tvc->row_vpadding);
BLI_assert(tds.do_draw == do_draw);
xy[1] += tvc->lheight * 2;
return xy[1] - y_orig;
}
|