Blender  V2.93
console_draw.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 <string.h>
22 
23 #include "BLI_blenlib.h"
24 #include "BLI_utildefines.h"
25 
26 #include "DNA_screen_types.h"
27 #include "DNA_space_types.h"
28 
29 #include "MEM_guardedalloc.h"
30 
31 #include "GPU_immediate.h"
32 
33 #include "UI_interface.h"
34 #include "UI_resources.h"
35 #include "UI_view2d.h"
36 
37 #include "console_intern.h"
38 
39 #include "../space_info/textview.h"
40 
42  uchar fg[4],
43  uchar UNUSED(bg[4]),
44  int *UNUSED(icon),
45  uchar UNUSED(icon_fg[4]),
46  uchar UNUSED(icon_bg[4]))
47 {
48  const ConsoleLine *cl_iter = tvc->iter;
49  int fg_id = TH_TEXT;
50 
51  switch (cl_iter->type) {
53  fg_id = TH_CONSOLE_OUTPUT;
54  break;
55  case CONSOLE_LINE_INPUT:
56  fg_id = TH_CONSOLE_INPUT;
57  break;
58  case CONSOLE_LINE_INFO:
59  fg_id = TH_CONSOLE_INFO;
60  break;
61  case CONSOLE_LINE_ERROR:
62  fg_id = TH_CONSOLE_ERROR;
63  break;
64  }
65 
66  UI_GetThemeColor4ubv(fg_id, fg);
67  return TVC_LINE_FG;
68 }
69 
71 {
72  /* fake the edit line being in the scroll buffer */
73  ConsoleLine *cl = sc->history.last;
74  int prompt_len = strlen(sc->prompt);
75 
76  cl_dummy->type = CONSOLE_LINE_INPUT;
77  cl_dummy->len = prompt_len + cl->len;
78  cl_dummy->len_alloc = cl_dummy->len + 1;
79  cl_dummy->line = MEM_mallocN(cl_dummy->len_alloc, "cl_dummy");
80  memcpy(cl_dummy->line, sc->prompt, prompt_len);
81  memcpy(cl_dummy->line + prompt_len, cl->line, cl->len + 1);
82  BLI_addtail(&sc->scrollback, cl_dummy);
83 }
85 {
86  MEM_freeN(cl_dummy->line);
87  BLI_remlink(&sc->scrollback, cl_dummy);
88 }
89 
90 /* console textview callbacks */
92 {
93  SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
94  tvc->sel_start = sc->sel_start;
95  tvc->sel_end = sc->sel_end;
96 
97  /* iterator */
98  tvc->iter = sc->scrollback.last;
99 
100  return (tvc->iter != NULL);
101 }
102 
104 {
105  SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
106  (void)sc;
107 }
108 
110 {
111  return ((tvc->iter = (void *)((Link *)tvc->iter)->prev) != NULL);
112 }
113 
114 static void console_textview_line_get(TextViewContext *tvc, const char **r_line, int *r_len)
115 {
116  const ConsoleLine *cl = tvc->iter;
117  *r_line = cl->line;
118  *r_len = cl->len;
119  // printf("'%s' %d\n", *line, cl->len);
120  BLI_assert(cl->line[cl->len] == '\0' && (cl->len == 0 || cl->line[cl->len - 1] != '\0'));
121 }
122 
124  const char *str, int width, int *row, int *column, const char *end)
125 {
126  int col;
127 
128  for (; *str; str += BLI_str_utf8_size_safe(str)) {
130 
131  if (*column + col > width) {
132  (*row)++;
133  *column = 0;
134  }
135 
136  if (end && str >= end) {
137  break;
138  }
139 
140  *column += col;
141  }
142 }
143 
144 static void console_textview_draw_cursor(TextViewContext *tvc, int cwidth, int columns)
145 {
146  int pen[2];
147  {
148  const SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
149  const ConsoleLine *cl = (ConsoleLine *)sc->history.last;
150  int offl = 0, offc = 0;
151 
152  console_cursor_wrap_offset(sc->prompt, columns, &offl, &offc, NULL);
153  console_cursor_wrap_offset(cl->line, columns, &offl, &offc, cl->line + cl->cursor);
154  pen[0] = cwidth * offc;
155  pen[1] = -tvc->lheight * offl;
156 
157  console_cursor_wrap_offset(cl->line + cl->cursor, columns, &offl, &offc, NULL);
158  pen[1] += tvc->lheight * offl;
159 
160  pen[0] += tvc->draw_rect.xmin;
161  pen[1] += tvc->draw_rect.ymin;
162  }
163 
164  /* cursor */
169 
170  immRectf(pos, pen[0] - U.pixelsize, pen[1], pen[0] + U.pixelsize, pen[1] + tvc->lheight);
171 
173 }
174 
176 {
178 }
179 
180 static void console_textview_draw_rect_calc(const ARegion *region,
181  rcti *r_draw_rect,
182  rcti *r_draw_rect_outer)
183 {
184  const int margin = 4 * UI_DPI_FAC;
185  r_draw_rect->xmin = margin;
186  r_draw_rect->xmax = region->winx - V2D_SCROLL_WIDTH;
187  r_draw_rect->ymin = margin;
188  /* No margin at the top (allow text to scroll off the window). */
189  r_draw_rect->ymax = region->winy;
190 
191  r_draw_rect_outer->xmin = 0;
192  r_draw_rect_outer->xmax = region->winx;
193  r_draw_rect_outer->ymin = 0;
194  r_draw_rect_outer->ymax = region->winy;
195 }
196 
198  const ARegion *region,
199  const bool do_draw,
200  const int mval[2],
201  void **r_mval_pick_item,
202  int *r_mval_pick_offset)
203 {
204  ConsoleLine cl_dummy = {NULL};
205  int ret = 0;
206 
207  const View2D *v2d = &region->v2d;
208 
209  TextViewContext tvc = {0};
210 
213 
219 
220  tvc.arg1 = sc;
221  tvc.arg2 = NULL;
222 
223  /* view */
224  tvc.sel_start = sc->sel_start;
225  tvc.sel_end = sc->sel_end;
226  tvc.lheight = sc->lheight * UI_DPI_FAC;
227  tvc.scroll_ymin = v2d->cur.ymin;
228  tvc.scroll_ymax = v2d->cur.ymax;
229 
231 
232  console_scrollback_prompt_begin(sc, &cl_dummy);
233  ret = textview_draw(&tvc, do_draw, mval, r_mval_pick_item, r_mval_pick_offset);
234  console_scrollback_prompt_end(sc, &cl_dummy);
235 
236  return ret;
237 }
238 
240 {
241  const int mval[2] = {INT_MAX, INT_MAX};
242  console_textview_main__internal(sc, region, true, mval, NULL, NULL);
243 }
244 
246 {
247  const int mval[2] = {INT_MAX, INT_MAX};
248  return console_textview_main__internal(sc, region, false, mval, NULL, NULL);
249 }
250 
251 int console_char_pick(SpaceConsole *sc, const ARegion *region, const int mval[2])
252 {
253  int r_mval_pick_offset = 0;
254  void *mval_pick_item = NULL;
255 
256  console_textview_main__internal(sc, region, false, mval, &mval_pick_item, &r_mval_pick_offset);
257  return r_mval_pick_offset;
258 }
#define BLI_assert(a)
Definition: BLI_assert.h:58
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
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
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
#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:171
@ 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:309
@ TH_CONSOLE_SELECT
Definition: UI_resources.h:201
@ TH_CONSOLE_ERROR
Definition: UI_resources.h:199
@ TH_CONSOLE_CURSOR
Definition: UI_resources.h:200
@ TH_CONSOLE_INFO
Definition: UI_resources.h:198
@ TH_CONSOLE_OUTPUT
Definition: UI_resources.h:196
@ TH_CONSOLE_INPUT
Definition: UI_resources.h:197
@ TH_TEXT
Definition: UI_resources.h:58
void UI_GetThemeColor4ubv(int colorid, unsigned char col[4])
Definition: resources.c:1381
#define V2D_SCROLL_WIDTH
Definition: UI_view2d.h:68
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:239
void console_scrollback_prompt_end(SpaceConsole *sc, ConsoleLine *cl_dummy)
Definition: console_draw.c:84
int console_textview_height(SpaceConsole *sc, const ARegion *region)
Definition: console_draw.c:245
void console_scrollback_prompt_begin(SpaceConsole *sc, ConsoleLine *cl_dummy)
Definition: console_draw.c:70
static void console_textview_const_colors(TextViewContext *UNUSED(tvc), uchar bg_sel[4])
Definition: console_draw.c:175
static int console_textview_step(TextViewContext *tvc)
Definition: console_draw.c:109
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:41
static int console_textview_begin(TextViewContext *tvc)
Definition: console_draw.c:91
static void console_textview_line_get(TextViewContext *tvc, const char **r_line, int *r_len)
Definition: console_draw.c:114
static void console_textview_draw_rect_calc(const ARegion *region, rcti *r_draw_rect, rcti *r_draw_rect_outer)
Definition: console_draw.c:180
static void console_textview_end(TextViewContext *tvc)
Definition: console_draw.c:103
int console_char_pick(SpaceConsole *sc, const ARegion *region, const int mval[2])
Definition: console_draw.c:251
static void console_textview_draw_cursor(TextViewContext *tvc, int cwidth, int columns)
Definition: console_draw.c:144
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:197
static void console_cursor_wrap_offset(const char *str, int width, int *row, int *column, const char *end)
Definition: console_draw.c:123
#define str(s)
uint pos
uint col
format
Definition: logImageCore.h:47
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
return ret
void * last
Definition: DNA_listBase.h:47
char prompt[256]
ListBase scrollback
const void * arg2
Definition: textview.h:51
rcti draw_rect
Definition: textview.h:40
const void * arg1
Definition: textview.h:50
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(* 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
float ymax
Definition: DNA_vec_types.h:86
float ymin
Definition: DNA_vec_types.h:86
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
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
eTextViewContext_LineFlag
Definition: textview.h:23
@ TVC_LINE_FG
Definition: textview.h:24