Blender  V3.3
console_draw.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <string.h>
8 
9 #include "BLI_blenlib.h"
10 #include "BLI_utildefines.h"
11 
12 #include "DNA_screen_types.h"
13 #include "DNA_space_types.h"
14 
15 #include "MEM_guardedalloc.h"
16 
17 #include "GPU_immediate.h"
18 
19 #include "UI_interface.h"
20 #include "UI_resources.h"
21 #include "UI_view2d.h"
22 
23 #include "console_intern.h"
24 
25 #include "../space_info/textview.h"
26 
28  uchar fg[4],
29  uchar UNUSED(bg[4]),
30  int *UNUSED(icon),
31  uchar UNUSED(icon_fg[4]),
32  uchar UNUSED(icon_bg[4]))
33 {
34  const ConsoleLine *cl_iter = tvc->iter;
35  int fg_id = TH_TEXT;
36 
37  switch (cl_iter->type) {
39  fg_id = TH_CONSOLE_OUTPUT;
40  break;
41  case CONSOLE_LINE_INPUT:
42  fg_id = TH_CONSOLE_INPUT;
43  break;
44  case CONSOLE_LINE_INFO:
45  fg_id = TH_CONSOLE_INFO;
46  break;
47  case CONSOLE_LINE_ERROR:
48  fg_id = TH_CONSOLE_ERROR;
49  break;
50  }
51 
52  UI_GetThemeColor4ubv(fg_id, fg);
53  return TVC_LINE_FG;
54 }
55 
57 {
58  /* fake the edit line being in the scroll buffer */
59  ConsoleLine *cl = sc->history.last;
60  int prompt_len = strlen(sc->prompt);
61 
62  cl_dummy->type = CONSOLE_LINE_INPUT;
63  cl_dummy->len = prompt_len + cl->len;
64  cl_dummy->len_alloc = cl_dummy->len + 1;
65  cl_dummy->line = MEM_mallocN(cl_dummy->len_alloc, "cl_dummy");
66  memcpy(cl_dummy->line, sc->prompt, prompt_len);
67  memcpy(cl_dummy->line + prompt_len, cl->line, cl->len + 1);
68  BLI_addtail(&sc->scrollback, cl_dummy);
69 }
71 {
72  MEM_freeN(cl_dummy->line);
73  BLI_remlink(&sc->scrollback, cl_dummy);
74 }
75 
76 /* console textview callbacks */
78 {
79  SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
80  tvc->sel_start = sc->sel_start;
81  tvc->sel_end = sc->sel_end;
82 
83  /* iterator */
84  tvc->iter = sc->scrollback.last;
85 
86  return (tvc->iter != NULL);
87 }
88 
90 {
91  SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
92  (void)sc;
93 }
94 
96 {
97  return ((tvc->iter = (void *)((Link *)tvc->iter)->prev) != NULL);
98 }
99 
100 static void console_textview_line_get(TextViewContext *tvc, const char **r_line, int *r_len)
101 {
102  const ConsoleLine *cl = tvc->iter;
103  *r_line = cl->line;
104  *r_len = cl->len;
105  // printf("'%s' %d\n", *line, cl->len);
106  BLI_assert(cl->line[cl->len] == '\0' && (cl->len == 0 || cl->line[cl->len - 1] != '\0'));
107 }
108 
110  const char *str, int width, int *row, int *column, const char *end)
111 {
112  int col;
113 
114  for (; *str; str += BLI_str_utf8_size_safe(str)) {
116 
117  if (*column + col > width) {
118  (*row)++;
119  *column = 0;
120  }
121 
122  if (end && str >= end) {
123  break;
124  }
125 
126  *column += col;
127  }
128 }
129 
130 static void console_textview_draw_cursor(TextViewContext *tvc, int cwidth, int columns)
131 {
132  int pen[2];
133  {
134  const SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
135  const ConsoleLine *cl = (ConsoleLine *)sc->history.last;
136  int offl = 0, offc = 0;
137 
138  console_cursor_wrap_offset(sc->prompt, columns, &offl, &offc, NULL);
139  console_cursor_wrap_offset(cl->line, columns, &offl, &offc, cl->line + cl->cursor);
140  pen[0] = cwidth * offc;
141  pen[1] = -tvc->lheight * offl;
142 
143  console_cursor_wrap_offset(cl->line + cl->cursor, columns, &offl, &offc, NULL);
144  pen[1] += tvc->lheight * offl;
145 
146  pen[0] += tvc->draw_rect.xmin;
147  pen[1] += tvc->draw_rect.ymin;
148  }
149 
150  /* cursor */
155 
156  immRectf(pos, pen[0] - U.pixelsize, pen[1], pen[0] + U.pixelsize, pen[1] + tvc->lheight);
157 
159 }
160 
162 {
164 }
165 
166 static void console_textview_draw_rect_calc(const ARegion *region,
167  rcti *r_draw_rect,
168  rcti *r_draw_rect_outer)
169 {
170  const int margin = 4 * UI_DPI_FAC;
171  r_draw_rect->xmin = margin;
172  r_draw_rect->xmax = region->winx - V2D_SCROLL_WIDTH;
173  r_draw_rect->ymin = margin;
174  /* No margin at the top (allow text to scroll off the window). */
175  r_draw_rect->ymax = region->winy;
176 
177  r_draw_rect_outer->xmin = 0;
178  r_draw_rect_outer->xmax = region->winx;
179  r_draw_rect_outer->ymin = 0;
180  r_draw_rect_outer->ymax = region->winy;
181 }
182 
184  const ARegion *region,
185  const bool do_draw,
186  const int mval[2],
187  void **r_mval_pick_item,
188  int *r_mval_pick_offset)
189 {
190  ConsoleLine cl_dummy = {NULL};
191  int ret = 0;
192 
193  const View2D *v2d = &region->v2d;
194 
195  TextViewContext tvc = {0};
196 
199 
205 
206  tvc.arg1 = sc;
207  tvc.arg2 = NULL;
208 
209  /* view */
210  tvc.sel_start = sc->sel_start;
211  tvc.sel_end = sc->sel_end;
212  tvc.lheight = sc->lheight * UI_DPI_FAC;
213  tvc.scroll_ymin = v2d->cur.ymin;
214  tvc.scroll_ymax = v2d->cur.ymax;
215 
217 
218  console_scrollback_prompt_begin(sc, &cl_dummy);
219  ret = textview_draw(&tvc, do_draw, mval, r_mval_pick_item, r_mval_pick_offset);
220  console_scrollback_prompt_end(sc, &cl_dummy);
221 
222  return ret;
223 }
224 
226 {
227  const int mval[2] = {INT_MAX, INT_MAX};
228  console_textview_main__internal(sc, region, true, mval, NULL, NULL);
229 }
230 
232 {
233  const int mval[2] = {INT_MAX, INT_MAX};
234  return console_textview_main__internal(sc, region, false, mval, NULL, NULL);
235 }
236 
237 int console_char_pick(SpaceConsole *sc, const ARegion *region, const int mval[2])
238 {
239  int r_mval_pick_offset = 0;
240  void *mval_pick_item = NULL;
241 
242  console_textview_main__internal(sc, region, false, mval, &mval_pick_item, &r_mval_pick_offset);
243  return r_mval_pick_offset;
244 }
#define BLI_assert(a)
Definition: BLI_assert.h:46
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:100
int BLI_str_utf8_char_width_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: string_utf8.c:388
int BLI_str_utf8_size_safe(const char *p) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition: string_utf8.c:466
unsigned char uchar
Definition: BLI_sys_types.h:70
unsigned int uint
Definition: BLI_sys_types.h:67
#define UNUSED(x)
@ CONSOLE_LINE_INFO
@ CONSOLE_LINE_ERROR
@ CONSOLE_LINE_INPUT
@ CONSOLE_LINE_OUTPUT
void immUnbindProgram(void)
void immUniformThemeColor(int color_id)
void immBindBuiltinProgram(eGPUBuiltinShader shader_id)
GPUVertFormat * immVertexFormat(void)
void immRectf(uint pos, float x1, float y1, float x2, float 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:201
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
Read Guarded memory(de)allocation.
#define UI_DPI_FAC
Definition: UI_interface.h:305
@ TH_CONSOLE_SELECT
Definition: UI_resources.h:185
@ TH_CONSOLE_ERROR
Definition: UI_resources.h:183
@ TH_CONSOLE_CURSOR
Definition: UI_resources.h:184
@ TH_CONSOLE_INFO
Definition: UI_resources.h:182
@ TH_CONSOLE_OUTPUT
Definition: UI_resources.h:180
@ TH_CONSOLE_INPUT
Definition: UI_resources.h:181
@ TH_TEXT
Definition: UI_resources.h:42
void UI_GetThemeColor4ubv(int colorid, unsigned char col[4])
Definition: resources.c:1352
#define V2D_SCROLL_WIDTH
Definition: UI_view2d.h:55
return(oflags[bm->toolflag_index].f &oflag) !=0
unsigned int U
Definition: btGjkEpa3.h:78
void console_textview_main(SpaceConsole *sc, const ARegion *region)
Definition: console_draw.c:225
void console_scrollback_prompt_end(SpaceConsole *sc, ConsoleLine *cl_dummy)
Definition: console_draw.c:70
int console_textview_height(SpaceConsole *sc, const ARegion *region)
Definition: console_draw.c:231
void console_scrollback_prompt_begin(SpaceConsole *sc, ConsoleLine *cl_dummy)
Definition: console_draw.c:56
static void console_textview_const_colors(TextViewContext *UNUSED(tvc), uchar bg_sel[4])
Definition: console_draw.c:161
static int console_textview_step(TextViewContext *tvc)
Definition: console_draw.c:95
static enum eTextViewContext_LineFlag console_line_data(TextViewContext *tvc, uchar fg[4], uchar UNUSED(bg[4]), int *UNUSED(icon), uchar UNUSED(icon_fg[4]), uchar UNUSED(icon_bg[4]))
Definition: console_draw.c:27
static int console_textview_begin(TextViewContext *tvc)
Definition: console_draw.c:77
static void console_textview_line_get(TextViewContext *tvc, const char **r_line, int *r_len)
Definition: console_draw.c:100
static void console_textview_draw_rect_calc(const ARegion *region, rcti *r_draw_rect, rcti *r_draw_rect_outer)
Definition: console_draw.c:166
static void console_textview_end(TextViewContext *tvc)
Definition: console_draw.c:89
int console_char_pick(SpaceConsole *sc, const ARegion *region, const int mval[2])
Definition: console_draw.c:237
static void console_textview_draw_cursor(TextViewContext *tvc, int cwidth, int columns)
Definition: console_draw.c:130
static int console_textview_main__internal(SpaceConsole *sc, const ARegion *region, const bool do_draw, const int mval[2], void **r_mval_pick_item, int *r_mval_pick_offset)
Definition: console_draw.c:183
static void console_cursor_wrap_offset(const char *str, int width, int *row, int *column, const char *end)
Definition: console_draw.c:109
#define str(s)
uint pos
uint col
format
Definition: logImageCore.h:38
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
return ret
void * last
Definition: DNA_listBase.h:31
char prompt[256]
ListBase scrollback
const void * arg2
Definition: textview.h:37
rcti draw_rect
Definition: textview.h:26
const void * arg1
Definition: textview.h:36
void(* line_get)(struct TextViewContext *tvc, const char **r_line, int *r_len)
Definition: textview.h:41
const void * iter
Definition: textview.h:51
int(* begin)(struct TextViewContext *tvc)
Definition: textview.h:34
void(* const_colors)(struct TextViewContext *tvc, unsigned char bg_sel[4])
Definition: textview.h:50
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:42
void(* end)(struct TextViewContext *tvc)
Definition: textview.h:35
int(* step)(struct TextViewContext *tvc)
Definition: textview.h:40
void(* draw_cursor)(struct TextViewContext *tvc, int cwidth, int columns)
Definition: textview.h:48
rcti draw_rect_outer
Definition: textview.h:28
float ymax
Definition: DNA_vec_types.h:70
float ymin
Definition: DNA_vec_types.h:70
int ymin
Definition: DNA_vec_types.h:64
int ymax
Definition: DNA_vec_types.h:64
int xmin
Definition: DNA_vec_types.h:63
int xmax
Definition: DNA_vec_types.h:63
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:293
eTextViewContext_LineFlag
Definition: textview.h:9
@ TVC_LINE_FG
Definition: textview.h:10