Blender V5.3
textview.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
11#include "MEM_guardedalloc.h"
12
13#include "BLF_api.hh"
14
15#include "BLI_math_color_c.hh"
16#include "BLI_math_vector_c.hh"
17#include "BLI_string_utf8.hh"
18#include "BLI_utildefines.hh"
19
20#include "GPU_immediate.hh"
21#include "GPU_state.hh"
22
23#include "DNA_userdef_types.h" /* For 'UI_SCALE_FAC' */
24
25#include "UI_interface.hh"
26#include "UI_interface_icons.hh"
27
28#include "textview.hh"
29
30namespace blender {
31
32static void textview_font_begin(const int font_id, const int line_height)
33{
34 /* Font size in relation to line height. */
35 BLF_size(font_id, 0.8f * line_height);
36}
37
43 int lofs;
52 int *xy; // [2]
53 int *sel; // [2]
54 /* Bottom of view == 0, top of file == combine chars, end of line is lower than start. */
56 const int *mval; // [2]
57 bool do_draw;
58};
59
61{
62 tds->sel[0] += step;
63 tds->sel[1] += step;
64}
65
66static void textview_draw_sel(const char *str,
67 const int xy[2],
68 const int str_len_draw,
70 const uchar bg_sel[4])
71{
72 const int sel[2] = {tds->sel[0], tds->sel[1]};
73 const int char_width = tds->char_width;
74 const int line_height = tds->line_height;
75
76 if (sel[0] <= str_len_draw && sel[1] >= 0) {
78 str, str_len_draw, max_ii(sel[0], 0), TVC_TAB_COLUMNS);
80 str, str_len_draw, min_ii(sel[1], str_len_draw), TVC_TAB_COLUMNS);
81
83
85 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
87
88 immUniformColor4ubv(bg_sel);
90 pos, xy[0] + (char_width * sta), xy[1] + line_height, xy[0] + (char_width * end), xy[1]);
91
93
95 }
96}
97
99 const char *str, const int str_len, const int width, int *r_lines, int **r_offsets)
100{
101 int i, end; /* Offset as unicode code-point. */
102 int j; /* Offset as bytes. */
103 const int tab_columns = TVC_TAB_COLUMNS;
104 const int column_width_max = std::max(tab_columns, BLI_UTF8_WIDTH_MAX);
105
106 *r_lines = 1;
107
108 *r_offsets = MEM_new_array_zeroed<int>(
109 (str_len * column_width_max / std::max(1, width - (column_width_max - 1)) + 1), __func__);
110 (*r_offsets)[0] = 0;
111
112 for (i = 0, end = width, j = 0; j < str_len && str[j]; j += BLI_str_utf8_size_safe(str + j)) {
113 int columns = UNLIKELY(*(str + j) == '\t') ? (tab_columns - (i % tab_columns)) :
115
116 if (i + columns > end) {
117 (*r_offsets)[*r_lines] = j;
118 (*r_lines)++;
119
120 end = i + width;
121 }
122 i += columns;
123 }
124 return j;
125}
126
132 const char *str,
133 int str_len,
134 const uchar fg[4],
135 const uchar bg[4],
136 int icon,
137 const uchar icon_fg[4],
138 const uchar icon_bg[4],
139 const uchar bg_sel[4])
140{
141 int tot_lines; /* Total number of lines for wrapping. */
142 int *offsets; /* Offsets of line beginnings for wrapping. */
143
144 str_len = textview_wrap_offsets(str, str_len, tds->columns, &tot_lines, &offsets);
145
146 int line_height = (tot_lines * tds->line_height) + (tds->row_vpadding * 2);
147 int line_bottom = tds->xy[1];
148 int line_top = line_bottom + line_height;
149
150 int y_next = line_top;
151
152 /* Just advance the height. */
153 if (tds->do_draw == false) {
154 if (tds->mval_pick_offset && tds->mval[1] != INT_MAX && line_bottom <= tds->mval[1]) {
155 if (y_next >= tds->mval[1]) {
156 int ofs = 0;
157
158 /* Wrap. */
159 if (tot_lines > 1) {
160 int iofs = int(float(y_next - tds->mval[1]) / tds->line_height);
161 ofs += offsets[std::min(iofs, tot_lines - 1)];
162 }
163
164 /* Last part. */
166 str + ofs,
167 str_len - ofs,
168 int(floor(float(tds->mval[0]) / tds->char_width)),
170
171 CLAMP(ofs, 0, str_len);
172 *tds->mval_pick_offset += str_len - ofs;
173 }
174 else {
175 *tds->mval_pick_offset += str_len + 1;
176 }
177 }
178
179 tds->xy[1] = y_next;
180 MEM_delete(offsets);
181 return true;
182 }
183 if (y_next < tds->scroll_ymin) {
184 /* Have not reached the drawable area so don't break. */
185 tds->xy[1] = y_next;
186
187 /* Adjust selection even if not drawing. */
188 if (tds->sel[0] != tds->sel[1]) {
189 textview_step_sel(tds, -(str_len + 1));
190 }
191
192 MEM_delete(offsets);
193 return true;
194 }
195
196 size_t len;
197 const char *s;
198 int i;
199
200 int sel_orig[2];
201 copy_v2_v2_int(sel_orig, tds->sel);
202
203 /* Invert and swap for wrapping. */
204 tds->sel[0] = str_len - sel_orig[1];
205 tds->sel[1] = str_len - sel_orig[0];
206
207 if (bg) {
208 GPUVertFormat *format = immVertexFormat();
209 uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
212 immRectf(pos, tds->draw_rect_outer->xmin, line_bottom, tds->draw_rect_outer->xmax, line_top);
214 }
215
216 if (icon_bg) {
217 float col[4];
218 int bg_size = UI_ICON_SIZE * 1.2;
219 float vpadding = (tds->line_height + (tds->row_vpadding * 2) - bg_size) / 2;
220 float hpadding = tds->draw_rect->xmin - (bg_size * 1.2f);
221
222 rgba_uchar_to_float(col, icon_bg);
223 draw_roundbox_corner_set(ui::CNR_ALL);
224
225 rctf roundbox_rect;
226 roundbox_rect.xmin = hpadding;
227 roundbox_rect.xmax = bg_size + hpadding;
228 roundbox_rect.ymin = line_top - bg_size - vpadding;
229 roundbox_rect.ymax = line_top - vpadding;
230
231 ui::draw_roundbox_4fv(&roundbox_rect, true, 4 * UI_SCALE_FAC, col);
232 }
233
234 if (icon) {
235 int vpadding = (tds->line_height + (tds->row_vpadding * 2) - UI_ICON_SIZE) / 2;
236 int hpadding = tds->draw_rect->xmin - (UI_ICON_SIZE * 1.3f);
237
239 ui::icon_draw_ex(hpadding,
240 line_top - UI_ICON_SIZE - vpadding,
241 icon,
242 (16 / UI_ICON_SIZE),
243 1.0f,
244 0.0f,
245 icon_fg,
246 false,
249 }
250
251 tds->xy[1] += tds->row_vpadding;
252
253 /* Last part needs no clipping. */
254 const int final_offset = offsets[tot_lines - 1];
255 len = str_len - final_offset;
256 s = str + final_offset;
257
258 if (tds->sel[0] != tds->sel[1]) {
259 textview_step_sel(tds, -final_offset);
260 const int pos[2] = {tds->xy[0], line_bottom};
261 textview_draw_sel(s, pos, len, tds, bg_sel);
262 }
263
264 BLF_position(tds->font_id, tds->xy[0], tds->lofs + line_bottom + tds->row_vpadding, 0);
265 BLF_color4ubv(tds->font_id, fg);
267
268 tds->xy[1] += tds->line_height;
269
270 BLF_color4ubv(tds->font_id, fg);
271
272 for (i = tot_lines - 1; i > 0; i--) {
273 len = offsets[i] - offsets[i - 1];
274 s = str + offsets[i - 1];
275
276 if (tds->sel[0] != tds->sel[1]) {
278 textview_draw_sel(s, tds->xy, len, tds, bg_sel);
279 }
280
281 BLF_position(tds->font_id, tds->xy[0], tds->lofs + tds->xy[1], 0);
283
284 tds->xy[1] += tds->line_height;
285
286 /* Check if we're out of view bounds. */
287 if (tds->xy[1] > tds->scroll_ymax) {
288 MEM_delete(offsets);
289 return false;
290 }
291 }
292
293 tds->xy[1] = y_next;
294
295 copy_v2_v2_int(tds->sel, sel_orig);
296 textview_step_sel(tds, -(str_len + 1));
297
298 MEM_delete(offsets);
299 return true;
300}
301
303 const bool do_draw,
304 const int mval_init[2],
305 void **r_mval_pick_item,
306 int *r_mval_pick_offset)
307{
308 TextViewDrawState tds = {0};
309
310 const int x_orig = tvc->draw_rect.xmin, y_orig = tvc->draw_rect.ymin;
311 int xy[2];
312 /* Disable selection by. */
313 int sel[2] = {-1, -1};
314 uchar fg[4], bg[4], icon_fg[4], icon_bg[4];
315 int icon = 0;
316 const int font_id = blf_mono_font;
317
318 textview_font_begin(font_id, tvc->line_height);
319
320 xy[0] = x_orig;
321 xy[1] = y_orig;
322
323 /* Offset and clamp the results,
324 * clamping so moving the cursor out of the bounds doesn't wrap onto the other lines. */
325 const int mval[2] = {
326 (mval_init[0] == INT_MAX) ?
327 INT_MAX :
328 std::clamp(mval_init[0], tvc->draw_rect.xmin, tvc->draw_rect.xmax) - tvc->draw_rect.xmin,
329 (mval_init[1] == INT_MAX) ?
330 INT_MAX :
331 std::clamp(mval_init[1], tvc->draw_rect.ymin, tvc->draw_rect.ymax) + tvc->scroll_ymin,
332 };
333
334 if (r_mval_pick_offset != nullptr) {
335 *r_mval_pick_offset = 0;
336 }
337
338 /* Constants for the text-view context. */
339 tds.font_id = font_id;
340 tds.char_width = int(BLF_fixed_width(font_id));
341 BLI_assert(tds.char_width > 0);
342 tds.line_height = tvc->line_height;
343 tds.row_vpadding = tvc->row_vpadding;
344 tds.lofs = -BLF_descender(font_id);
345 /* NOTE: scroll bar must be already subtracted. */
346 tds.columns = (tvc->draw_rect.xmax - tvc->draw_rect.xmin) / tds.char_width;
347 /* Avoid divide by zero on small windows. */
348 tds.columns = std::max(tds.columns, 1);
349 tds.draw_rect = &tvc->draw_rect;
351 tds.scroll_ymin = tvc->scroll_ymin;
352 tds.scroll_ymax = tvc->scroll_ymax;
353 tds.xy = xy;
354 tds.sel = sel;
355 tds.mval_pick_offset = r_mval_pick_offset;
356 tds.mval = mval;
357 tds.do_draw = do_draw;
358
359 if (tvc->sel_start != tvc->sel_end) {
360 sel[0] = tvc->sel_start;
361 sel[1] = tvc->sel_end;
362 }
363
364 if (tvc->begin(tvc)) {
365 uchar bg_sel[4] = {0};
366
367 if (do_draw && tvc->const_colors) {
368 tvc->const_colors(tvc, bg_sel);
369 }
370
371 int iter_index = 0;
372 do {
373 const char *ext_line;
374 int ext_len;
375 int data_flag = 0;
376
377 const int y_prev = xy[1];
378
379 if (do_draw) {
380 data_flag = tvc->line_data(tvc, fg, bg, &icon, icon_fg, icon_bg);
381 }
382
383 tvc->line_get(tvc, &ext_line, &ext_len);
384
385 const bool is_out_of_view_y = !textview_draw_string(
386 &tds,
387 ext_line,
388 ext_len,
389 (data_flag & TVC_LINE_FG) ? fg : nullptr,
390 (data_flag & TVC_LINE_BG) ? bg : nullptr,
391 (data_flag & TVC_LINE_ICON) ? icon : 0,
392 (data_flag & TVC_LINE_ICON_FG) ? icon_fg : nullptr,
393 (data_flag & TVC_LINE_ICON_BG) ? icon_bg : nullptr,
394 bg_sel);
395
396 if (do_draw) {
397 /* We always want the cursor to draw. */
398 if (tvc->draw_cursor && iter_index == 0) {
399 tvc->draw_cursor(tvc, tds.char_width, tds.columns);
400 }
401
402 /* When drawing, if we pass v2d->cur.ymax, then quit. */
403 if (is_out_of_view_y) {
404 break;
405 }
406 }
407
408 if ((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
409 *r_mval_pick_item = const_cast<void *>(tvc->iter);
410 break;
411 }
412
413 iter_index++;
414
415 } while (tvc->step(tvc));
416 }
417
418 tvc->end(tvc);
419
420 /* Sanity checks (bugs here can be tricky to track down). */
423 BLI_assert(tds.do_draw == do_draw);
424
425 xy[1] += tvc->line_height * 2;
426
427 return xy[1] - y_orig;
428}
429
430} // namespace blender
#define BLI_UTF8_WIDTH_MAX
#define UI_SCALE_FAC
#define UI_ICON_SIZE
Read Blender memory allocation and freeing.
#define UI_NO_ICON_OVERLAY_TEXT
#define CLAMP(a, b, c)
#define BLI_INLINE
#define UNLIKELY(x)
#define str(s)
#define INT_MAX
#define floor
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void icon_draw_ex(float x, float y, int icon_id, float aspect, float alpha, float desaturate, const uchar mono_color[4], bool mono_border, const IconTextOverlay *text_overlay, const bool inverted=false, float scale=1.0f)
void draw_roundbox_4fv(const rctf *rect, bool filled, float rad, const float col[4])
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
void BLF_position(int fontid, float x, float y, float z)
Definition blf.cc:390
unsigned int uint
Definition BKE_text.h:55
int BLI_str_utf8_char_width_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
GPUVertFormat * immVertexFormat()
MINLINE int max_ii(int a, int b)
int textview_wrap_offsets(const char *str, const int str_len, const int width, int *r_lines, int **r_offsets)
Definition textview.cc:98
void immUnbindProgram()
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
int textview_draw(TextViewContext *tvc, const bool do_draw, const int mval_init[2], void **r_mval_pick_item, int *r_mval_pick_offset)
Definition textview.cc:302
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
void BLF_size(int fontid, float size)
Definition blf.cc:445
unsigned char uchar
Definition BKE_text.h:58
static void textview_draw_sel(const char *str, const int xy[2], const int str_len_draw, TextViewDrawState *tds, const uchar bg_sel[4])
Definition textview.cc:66
@ TVC_LINE_BG
Definition textview.hh:21
@ TVC_LINE_ICON_FG
Definition textview.hh:23
@ TVC_LINE_ICON_BG
Definition textview.hh:24
@ TVC_LINE_FG
Definition textview.hh:20
@ TVC_LINE_ICON
Definition textview.hh:22
int BLI_str_utf8_offset_to_column_with_tabs(const char *str, size_t str_len, int offset_target, int tab_width) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
float BLF_fixed_width(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:848
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void BLF_color4ubv(int fontid, const unsigned char rgba[4])
Definition blf.cc:454
int BLF_draw_mono(int fontid, const char *str, size_t str_len, int char_width, int tab_columns) ATTR_NONNULL(2)
Definition blf.cc:609
int BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT
Definition blf.cc:905
int blf_mono_font
Definition blf.cc:50
BLI_INLINE void textview_step_sel(TextViewDrawState *tds, const int step)
Definition textview.cc:60
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
void immUniformColor4ubv(const unsigned char rgba[4])
MINLINE int min_ii(int a, int b)
void immRectf(uint pos, float x1, float y1, float x2, float y2)
int BLI_str_utf8_offset_from_column_with_tabs(const char *str, size_t str_len, int column_target, int tab_width) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
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])
Definition textview.cc:131
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
static void textview_font_begin(const int font_id, const int line_height)
Definition textview.cc:32
void(*) const_colors(TextViewContext *tvc, unsigned char bg_sel[4])
Definition textview.hh:61
int(*) begin(TextViewContext *tvc)
Definition textview.hh:45
void(*) line_get(TextViewContext *tvc, const char **r_line, int *r_len)
Definition textview.hh:52
int(*) step(TextViewContext *tvc)
Definition textview.hh:51
void(*) draw_cursor(TextViewContext *tvc, int char_width, int columns)
Definition textview.hh:59
void(*) end(TextViewContext *tvc)
Definition textview.hh:46
enum eTextViewContext_LineFlag(*) line_data(TextViewContext *tvc, uchar fg[4], uchar bg[4], int *r_icon, uchar r_icon_fg[4], uchar r_icon_bg[4])
Definition textview.hh:53
const rcti * draw_rect_outer
Definition textview.cc:50
#define TVC_TAB_COLUMNS
Definition textview.hh:17