Blender  V2.93
textview.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "MEM_guardedalloc.h"
22 
23 #include "BLF_api.h"
24 
25 #include "BLI_math.h"
26 #include "BLI_string_utf8.h"
27 #include "BLI_utildefines.h"
28 
29 #include "GPU_immediate.h"
30 #include "GPU_state.h"
31 
32 #include "DNA_userdef_types.h" /* For 'U.dpi_fac' */
33 
34 #include "UI_interface.h"
35 #include "UI_interface_icons.h"
36 
37 #include "textview.h"
38 
39 static void textview_font_begin(const int font_id, const int lheight)
40 {
41  /* Font size in relation to line height. */
42  BLF_size(font_id, 0.8f * lheight, 72);
43 }
44 
45 typedef struct TextViewDrawState {
46  int font_id;
47  int cwidth;
48  int lheight;
50  int lofs;
53  int columns;
55  const rcti *draw_rect;
59  int *xy; // [2]
60  int *sel; // [2]
61  /* Bottom of view == 0, top of file == combine chars, end of line is lower than start. */
63  const int *mval; // [2]
64  bool do_draw;
66 
68 {
69  tds->sel[0] += step;
70  tds->sel[1] += step;
71 }
72 
73 static void textview_draw_sel(const char *str,
74  const int xy[2],
75  const int str_len_draw,
76  TextViewDrawState *tds,
77  const uchar bg_sel[4])
78 {
79  const int sel[2] = {tds->sel[0], tds->sel[1]};
80  const int cwidth = tds->cwidth;
81  const int lheight = tds->lheight;
82 
83  if (sel[0] <= str_len_draw && sel[1] >= 0) {
84  const int sta = BLI_str_utf8_offset_to_column(str, max_ii(sel[0], 0));
85  const int end = BLI_str_utf8_offset_to_column(str, min_ii(sel[1], str_len_draw));
86 
88 
92 
93  immUniformColor4ubv(bg_sel);
94  immRecti(pos, xy[0] + (cwidth * sta), xy[1] + lheight, xy[0] + (cwidth * end), xy[1]);
95 
97 
99  }
100 }
101 
107  const char *str, int len, int width, int *r_lines, int **r_offsets)
108 {
109  int i, end; /* Offset as unicode code-point. */
110  int j; /* Offset as bytes. */
111 
112  *r_lines = 1;
113 
114  *r_offsets = MEM_callocN(
115  sizeof(**r_offsets) *
116  (len * BLI_UTF8_WIDTH_MAX / MAX2(1, width - (BLI_UTF8_WIDTH_MAX - 1)) + 1),
117  __func__);
118  (*r_offsets)[0] = 0;
119 
120  for (i = 0, end = width, j = 0; j < len && str[j]; j += BLI_str_utf8_size_safe(str + j)) {
121  int columns = BLI_str_utf8_char_width_safe(str + j);
122 
123  if (i + columns > end) {
124  (*r_offsets)[*r_lines] = j;
125  (*r_lines)++;
126 
127  end = i + width;
128  }
129  i += columns;
130  }
131  return j;
132 }
133 
139  const char *str,
140  int str_len,
141  const uchar fg[4],
142  const uchar bg[4],
143  int icon,
144  const uchar icon_fg[4],
145  const uchar icon_bg[4],
146  const uchar bg_sel[4])
147 {
148  int tot_lines; /* Total number of lines for wrapping. */
149  int *offsets; /* Offsets of line beginnings for wrapping. */
150 
151  str_len = textview_wrap_offsets(str, str_len, tds->columns, &tot_lines, &offsets);
152 
153  int line_height = (tot_lines * tds->lheight) + (tds->row_vpadding * 2);
154  int line_bottom = tds->xy[1];
155  int line_top = line_bottom + line_height;
156 
157  int y_next = line_top;
158 
159  /* Just advance the height. */
160  if (tds->do_draw == false) {
161  if (tds->mval_pick_offset && tds->mval[1] != INT_MAX && line_bottom <= tds->mval[1]) {
162  if (y_next >= tds->mval[1]) {
163  int ofs = 0;
164 
165  /* Wrap. */
166  if (tot_lines > 1) {
167  int iofs = (int)((float)(y_next - tds->mval[1]) / tds->lheight);
168  ofs += offsets[MIN2(iofs, tot_lines - 1)];
169  }
170 
171  /* Last part. */
173  (int)floor((float)tds->mval[0] / tds->cwidth));
174 
175  CLAMP(ofs, 0, str_len);
176  *tds->mval_pick_offset += str_len - ofs;
177  }
178  else {
179  *tds->mval_pick_offset += str_len + 1;
180  }
181  }
182 
183  tds->xy[1] = y_next;
184  MEM_freeN(offsets);
185  return true;
186  }
187  if (y_next < tds->scroll_ymin) {
188  /* Have not reached the drawable area so don't break. */
189  tds->xy[1] = y_next;
190 
191  /* Adjust selection even if not drawing. */
192  if (tds->sel[0] != tds->sel[1]) {
193  textview_step_sel(tds, -(str_len + 1));
194  }
195 
196  MEM_freeN(offsets);
197  return true;
198  }
199 
200  size_t len;
201  const char *s;
202  int i;
203 
204  int sel_orig[2];
205  copy_v2_v2_int(sel_orig, tds->sel);
206 
207  /* Invert and swap for wrapping. */
208  tds->sel[0] = str_len - sel_orig[1];
209  tds->sel[1] = str_len - sel_orig[0];
210 
211  if (bg) {
216  immRecti(pos, tds->draw_rect_outer->xmin, line_bottom, tds->draw_rect_outer->xmax, line_top);
218  }
219 
220  if (icon_bg) {
221  float col[4];
222  int bg_size = UI_DPI_ICON_SIZE * 1.2;
223  float vpadding = (tds->lheight + (tds->row_vpadding * 2) - bg_size) / 2;
224  float hpadding = tds->draw_rect->xmin - (bg_size * 1.2f);
225 
226  rgba_uchar_to_float(col, icon_bg);
229  &(const rctf){
230  .xmin = hpadding,
231  .xmax = bg_size + hpadding,
232  .ymin = line_top - bg_size - vpadding,
233  .ymax = line_top - vpadding,
234  },
235  true,
236  4 * UI_DPI_FAC,
237  col);
238  }
239 
240  if (icon) {
241  int vpadding = (tds->lheight + (tds->row_vpadding * 2) - UI_DPI_ICON_SIZE) / 2;
242  int hpadding = tds->draw_rect->xmin - (UI_DPI_ICON_SIZE * 1.3f);
243 
245  UI_icon_draw_ex(hpadding,
246  line_top - UI_DPI_ICON_SIZE - vpadding,
247  icon,
248  (16 / UI_DPI_ICON_SIZE),
249  1.0f,
250  0.0f,
251  icon_fg,
252  false);
254  }
255 
256  tds->xy[1] += tds->row_vpadding;
257 
258  /* Last part needs no clipping. */
259  const int final_offset = offsets[tot_lines - 1];
260  len = str_len - final_offset;
261  s = str + final_offset;
262  BLF_position(tds->font_id, tds->xy[0], tds->lofs + line_bottom + tds->row_vpadding, 0);
263  BLF_color4ubv(tds->font_id, fg);
264  BLF_draw_mono(tds->font_id, s, len, tds->cwidth);
265 
266  if (tds->sel[0] != tds->sel[1]) {
267  textview_step_sel(tds, -final_offset);
268  const int pos[2] = {tds->xy[0], line_bottom};
269  textview_draw_sel(s, pos, len, tds, bg_sel);
270  }
271 
272  tds->xy[1] += tds->lheight;
273 
274  BLF_color4ubv(tds->font_id, fg);
275 
276  for (i = tot_lines - 1; i > 0; i--) {
277  len = offsets[i] - offsets[i - 1];
278  s = str + offsets[i - 1];
279 
280  BLF_position(tds->font_id, tds->xy[0], tds->lofs + tds->xy[1], 0);
281  BLF_draw_mono(tds->font_id, s, len, tds->cwidth);
282 
283  if (tds->sel[0] != tds->sel[1]) {
284  textview_step_sel(tds, len);
285  textview_draw_sel(s, tds->xy, len, tds, bg_sel);
286  }
287 
288  tds->xy[1] += tds->lheight;
289 
290  /* Check if we're out of view bounds. */
291  if (tds->xy[1] > tds->scroll_ymax) {
292  MEM_freeN(offsets);
293  return false;
294  }
295  }
296 
297  tds->xy[1] = y_next;
298 
299  copy_v2_v2_int(tds->sel, sel_orig);
300  textview_step_sel(tds, -(str_len + 1));
301 
302  MEM_freeN(offsets);
303  return true;
304 }
305 
314  const bool do_draw,
315  const int mval_init[2],
316  void **r_mval_pick_item,
317  int *r_mval_pick_offset)
318 {
319  TextViewDrawState tds = {0};
320 
321  const int x_orig = tvc->draw_rect.xmin, y_orig = tvc->draw_rect.ymin;
322  int xy[2];
323  /* Disable selection by. */
324  int sel[2] = {-1, -1};
325  uchar fg[4], bg[4], icon_fg[4], icon_bg[4];
326  int icon = 0;
327  const int font_id = blf_mono_font;
328 
329  textview_font_begin(font_id, tvc->lheight);
330 
331  xy[0] = x_orig;
332  xy[1] = y_orig;
333 
334  /* Offset and clamp the results,
335  * clamping so moving the cursor out of the bounds doesn't wrap onto the other lines. */
336  const int mval[2] = {
337  (mval_init[0] == INT_MAX) ?
338  INT_MAX :
339  CLAMPIS(mval_init[0], tvc->draw_rect.xmin, tvc->draw_rect.xmax) - tvc->draw_rect.xmin,
340  (mval_init[1] == INT_MAX) ?
341  INT_MAX :
342  CLAMPIS(mval_init[1], tvc->draw_rect.ymin, tvc->draw_rect.ymax) + tvc->scroll_ymin,
343  };
344 
345  if (r_mval_pick_offset != NULL) {
346  *r_mval_pick_offset = 0;
347  }
348 
349  /* Constants for the text-view context. */
350  tds.font_id = font_id;
351  tds.cwidth = (int)BLF_fixed_width(font_id);
352  BLI_assert(tds.cwidth > 0);
353  tds.lheight = tvc->lheight;
354  tds.row_vpadding = tvc->row_vpadding;
355  tds.lofs = -BLF_descender(font_id);
356  /* Note, scroll bar must be already subtracted. */
357  tds.columns = (tvc->draw_rect.xmax - tvc->draw_rect.xmin) / tds.cwidth;
358  /* Avoid divide by zero on small windows. */
359  if (tds.columns < 1) {
360  tds.columns = 1;
361  }
362  tds.draw_rect = &tvc->draw_rect;
363  tds.draw_rect_outer = &tvc->draw_rect_outer;
364  tds.scroll_ymin = tvc->scroll_ymin;
365  tds.scroll_ymax = tvc->scroll_ymax;
366  tds.xy = xy;
367  tds.sel = sel;
368  tds.mval_pick_offset = r_mval_pick_offset;
369  tds.mval = mval;
370  tds.do_draw = do_draw;
371 
372  if (tvc->sel_start != tvc->sel_end) {
373  sel[0] = tvc->sel_start;
374  sel[1] = tvc->sel_end;
375  }
376 
377  if (tvc->begin(tvc)) {
378  uchar bg_sel[4] = {0};
379 
380  if (do_draw && tvc->const_colors) {
381  tvc->const_colors(tvc, bg_sel);
382  }
383 
384  int iter_index = 0;
385  do {
386  const char *ext_line;
387  int ext_len;
388  int data_flag = 0;
389 
390  const int y_prev = xy[1];
391 
392  if (do_draw) {
393  data_flag = tvc->line_data(tvc, fg, bg, &icon, icon_fg, icon_bg);
394  }
395 
396  tvc->line_get(tvc, &ext_line, &ext_len);
397 
398  const bool is_out_of_view_y = !textview_draw_string(
399  &tds,
400  ext_line,
401  ext_len,
402  (data_flag & TVC_LINE_FG) ? fg : NULL,
403  (data_flag & TVC_LINE_BG) ? bg : NULL,
404  (data_flag & TVC_LINE_ICON) ? icon : 0,
405  (data_flag & TVC_LINE_ICON_FG) ? icon_fg : NULL,
406  (data_flag & TVC_LINE_ICON_BG) ? icon_bg : NULL,
407  bg_sel);
408 
409  if (do_draw) {
410  /* We always want the cursor to draw. */
411  if (tvc->draw_cursor && iter_index == 0) {
412  tvc->draw_cursor(tvc, tds.cwidth, tds.columns);
413  }
414 
415  /* When drawing, if we pass v2d->cur.ymax, then quit. */
416  if (is_out_of_view_y) {
417  break;
418  }
419  }
420 
421  if ((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
422  *r_mval_pick_item = (void *)tvc->iter;
423  break;
424  }
425 
426  iter_index++;
427 
428  } while (tvc->step(tvc));
429  }
430 
431  tvc->end(tvc);
432 
433  /* Sanity checks (bugs here can be tricky to track down). */
434  BLI_assert(tds.lheight == tvc->lheight);
435  BLI_assert(tds.row_vpadding == tvc->row_vpadding);
436  BLI_assert(tds.do_draw == do_draw);
437 
438  xy[1] += tvc->lheight * 2;
439 
440  return xy[1] - y_orig;
441 }
float BLF_fixed_width(int fontid) ATTR_WARN_UNUSED_RESULT
Definition: blf.c:728
int BLF_draw_mono(int fontid, const char *str, size_t len, int cwidth) ATTR_NONNULL(2)
Definition: blf.c:582
float BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT
Definition: blf.c:779
int blf_mono_font
Definition: blf.c:70
void BLF_size(int fontid, int size, int dpi)
Definition: blf.c:367
void BLF_color4ubv(int fontid, const unsigned char rgba[4])
Definition: blf.c:387
void BLF_position(int fontid, float x, float y, float z)
Definition: blf.c:312
#define BLI_assert(a)
Definition: BLI_assert.h:58
#define BLI_INLINE
MINLINE int min_ii(int a, int b)
MINLINE int max_ii(int a, int b)
void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
Definition: math_color.c:414
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
int BLI_str_utf8_char_width_safe(const char *p) ATTR_NONNULL()
Definition: string_utf8.c:429
int BLI_str_utf8_size_safe(const char *p) ATTR_NONNULL()
Definition: string_utf8.c:508
#define BLI_UTF8_WIDTH_MAX
int BLI_str_utf8_offset_from_column(const char *str, int column)
Definition: string_utf8.c:943
int BLI_str_utf8_offset_to_column(const char *str, int offset)
Definition: string_utf8.c:933
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
#define CLAMPIS(a, b, c)
#define MAX2(a, b)
#define MIN2(a, b)
void immUniformColor4ubv(const unsigned char rgba[4])
void immUnbindProgram(void)
void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
GPUVertFormat * immVertexFormat(void)
void immRecti(uint pos, int x1, int y1, int x2, int y2)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
@ GPU_SHADER_2D_UNIFORM_COLOR
Definition: GPU_shader.h:171
@ GPU_BLEND_NONE
Definition: GPU_state.h:55
@ GPU_BLEND_ALPHA
Definition: GPU_state.h:57
void GPU_blend(eGPUBlend blend)
Definition: gpu_state.cc:55
@ GPU_FETCH_INT_TO_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_I32
Read Guarded memory(de)allocation.
Group RGB to Bright Vector Camera CLAMP
@ UI_CNR_ALL
void UI_draw_roundbox_4fv(const struct rctf *rect, bool filled, float rad, const float col[4])
void UI_draw_roundbox_corner_set(int type)
#define UI_DPI_ICON_SIZE
Definition: UI_interface.h:311
#define UI_DPI_FAC
Definition: UI_interface.h:309
void UI_icon_draw_ex(float x, float y, int icon_id, float aspect, float alpha, float desaturate, const uchar mono_color[4], const bool mono_border)
#define str(s)
uint pos
uint col
format
Definition: logImageCore.h:47
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
rcti draw_rect
Definition: textview.h:40
void(* line_get)(struct TextViewContext *tvc, const char **r_line, int *r_len)
Definition: textview.h:55
const void * iter
Definition: textview.h:65
int row_vpadding
Definition: textview.h:37
int(* begin)(struct TextViewContext *tvc)
Definition: textview.h:48
void(* const_colors)(struct TextViewContext *tvc, unsigned char bg_sel[4])
Definition: textview.h:64
enum eTextViewContext_LineFlag(* line_data)(struct TextViewContext *tvc, uchar fg[4], uchar bg[4], int *r_icon, uchar r_icon_fg[4], uchar r_icon_bg[4])
Definition: textview.h:56
void(* end)(struct TextViewContext *tvc)
Definition: textview.h:49
int(* step)(struct TextViewContext *tvc)
Definition: textview.h:54
void(* draw_cursor)(struct TextViewContext *tvc, int cwidth, int columns)
Definition: textview.h:62
rcti draw_rect_outer
Definition: textview.h:42
const rcti * draw_rect
Definition: textview.c:55
const rcti * draw_rect_outer
Definition: textview.c:57
const int * mval
Definition: textview.c:63
int * mval_pick_offset
Definition: textview.c:62
int ymin
Definition: DNA_vec_types.h:80
int ymax
Definition: DNA_vec_types.h:80
int xmin
Definition: DNA_vec_types.h:79
int xmax
Definition: DNA_vec_types.h:79
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.c:138
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.c:73
static void textview_font_begin(const int font_id, const int lheight)
Definition: textview.c:39
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.c:313
BLI_INLINE void textview_step_sel(TextViewDrawState *tds, const int step)
Definition: textview.c:67
static int textview_wrap_offsets(const char *str, int len, int width, int *r_lines, int **r_offsets)
Definition: textview.c:106
struct TextViewDrawState TextViewDrawState
@ TVC_LINE_ICON
Definition: textview.h:26
@ TVC_LINE_ICON_FG
Definition: textview.h:27
@ TVC_LINE_ICON_BG
Definition: textview.h:28
@ TVC_LINE_BG
Definition: textview.h:25
@ TVC_LINE_FG
Definition: textview.h:24
ccl_device_inline float2 floor(const float2 &a)
uint len