Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_console / console_draw.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Campbell Barton
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/editors/space_console/console_draw.c
24  *  \ingroup spconsole
25  */
26
27 #include <math.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <limits.h>
32
33
34 #include "BLI_blenlib.h"
35 #include "BLI_utildefines.h"
36
37 #include "DNA_space_types.h"
38 #include "DNA_screen_types.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "BIF_gl.h"
43 #include "GPU_immediate.h"
44
45 #include "UI_interface.h"
46 #include "UI_resources.h"
47 #include "UI_view2d.h"
48
49 #include "console_intern.h"
50
51
52 #include "../space_info/textview.h"
53
54 static void console_line_color(unsigned char fg[3], int type)
55 {
56         switch (type) {
57                 case CONSOLE_LINE_OUTPUT:
58                         UI_GetThemeColor3ubv(TH_CONSOLE_OUTPUT, fg);
59                         break;
60                 case CONSOLE_LINE_INPUT:
61                         UI_GetThemeColor3ubv(TH_CONSOLE_INPUT, fg);
62                         break;
63                 case CONSOLE_LINE_INFO:
64                         UI_GetThemeColor3ubv(TH_CONSOLE_INFO, fg);
65                         break;
66                 case CONSOLE_LINE_ERROR:
67                         UI_GetThemeColor3ubv(TH_CONSOLE_ERROR, fg);
68                         break;
69         }
70 }
71
72 void console_scrollback_prompt_begin(struct SpaceConsole *sc, ConsoleLine *cl_dummy)
73 {
74         /* fake the edit line being in the scroll buffer */
75         ConsoleLine *cl = sc->history.last;
76         int prompt_len = strlen(sc->prompt);
77
78         cl_dummy->type = CONSOLE_LINE_INPUT;
79         cl_dummy->len = prompt_len + cl->len;
80         cl_dummy->len_alloc = cl_dummy->len + 1;
81         cl_dummy->line = MEM_mallocN(cl_dummy->len_alloc, "cl_dummy");
82         memcpy(cl_dummy->line, sc->prompt, prompt_len);
83         memcpy(cl_dummy->line + prompt_len, cl->line, cl->len + 1);
84         BLI_addtail(&sc->scrollback, cl_dummy);
85 }
86 void console_scrollback_prompt_end(struct SpaceConsole *sc, ConsoleLine *cl_dummy)
87 {
88         MEM_freeN(cl_dummy->line);
89         BLI_remlink(&sc->scrollback, cl_dummy);
90 }
91
92 #define CONSOLE_DRAW_MARGIN 4
93
94 /* console textview callbacks */
95 static int console_textview_begin(TextViewContext *tvc)
96 {
97         SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
98         tvc->lheight = sc->lheight * UI_DPI_FAC;
99         tvc->sel_start = sc->sel_start;
100         tvc->sel_end = sc->sel_end;
101
102         /* iterator */
103         tvc->iter = sc->scrollback.last;
104
105         return (tvc->iter != NULL);
106 }
107
108 static void console_textview_end(TextViewContext *tvc)
109 {
110         SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
111         (void)sc;
112
113 }
114
115 static int console_textview_step(TextViewContext *tvc)
116 {
117         return ((tvc->iter = (void *)((Link *)tvc->iter)->prev) != NULL);
118 }
119
120 static int console_textview_line_get(struct TextViewContext *tvc, const char **line, int *len)
121 {
122         ConsoleLine *cl = (ConsoleLine *)tvc->iter;
123         *line = cl->line;
124         *len = cl->len;
125         // printf("'%s' %d\n", *line, cl->len);
126         BLI_assert(cl->line[cl->len] == '\0' && (cl->len == 0 || cl->line[cl->len - 1] != '\0'));
127         return 1;
128 }
129
130 static void console_cursor_wrap_offset(const char *str, int width, int *row, int *column, const char *end)
131 {
132         int col;
133
134         for (; *str; str += BLI_str_utf8_size_safe(str)) {
135                 col = BLI_str_utf8_char_width_safe(str);
136
137                 if (*column + col > width) {
138                         (*row)++;
139                         *column = 0;
140                 }
141
142                 if (end && str >= end)
143                         break;
144
145                 *column += col;
146         }
147         return;
148 }
149
150 static int console_textview_line_color(struct TextViewContext *tvc, unsigned char fg[3], unsigned char UNUSED(bg[3]))
151 {
152         ConsoleLine *cl_iter = (ConsoleLine *)tvc->iter;
153
154         /* annoying hack, to draw the prompt */
155         if (tvc->iter_index == 0) {
156                 const SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
157                 const ConsoleLine *cl = (ConsoleLine *)sc->history.last;
158                 int offl = 0, offc = 0;
159                 int xy[2] = {CONSOLE_DRAW_MARGIN, CONSOLE_DRAW_MARGIN};
160                 int pen[2];
161                 Gwn_VertFormat *format = immVertexFormat();
162                 unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
163                 xy[1] += tvc->lheight / 6;
164
165                 console_cursor_wrap_offset(sc->prompt, tvc->console_width, &offl, &offc, NULL);
166                 console_cursor_wrap_offset(cl->line, tvc->console_width, &offl, &offc, cl->line + cl->cursor);
167                 pen[0] = tvc->cwidth * offc;
168                 pen[1] = -2 - tvc->lheight * offl;
169
170                 console_cursor_wrap_offset(cl->line + cl->cursor, tvc->console_width, &offl, &offc, NULL);
171                 pen[1] += tvc->lheight * offl;
172
173                 /* cursor */
174                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
175                 immUniformThemeColor(TH_CONSOLE_CURSOR);
176
177                 immRectf(pos,
178                          (xy[0] + pen[0]) - 1,
179                          (xy[1] + pen[1]),
180                          (xy[0] + pen[0]) + 1,
181                          (xy[1] + pen[1] + tvc->lheight)
182                          );
183
184                 immUnbindProgram();
185         }
186
187         console_line_color(fg, cl_iter->type);
188
189         return TVC_LINE_FG;
190 }
191
192 static void console_textview_const_colors(TextViewContext *UNUSED(tvc), unsigned char bg_sel[4])
193 {
194         UI_GetThemeColor4ubv(TH_CONSOLE_SELECT, bg_sel);
195 }
196
197 static int console_textview_main__internal(struct SpaceConsole *sc, ARegion *ar, int draw,
198                                            int mval[2], void **mouse_pick, int *pos_pick)
199 {
200         ConsoleLine cl_dummy = {NULL};
201         int ret = 0;
202
203         View2D *v2d = &ar->v2d;
204
205         TextViewContext tvc = {0};
206
207         tvc.begin = console_textview_begin;
208         tvc.end = console_textview_end;
209
210         tvc.step = console_textview_step;
211         tvc.line_get = console_textview_line_get;
212         tvc.line_color = console_textview_line_color;
213         tvc.const_colors = console_textview_const_colors;
214
215         tvc.arg1 = sc;
216         tvc.arg2 = NULL;
217
218         /* view */
219         tvc.sel_start = sc->sel_start;
220         tvc.sel_end = sc->sel_end;
221         tvc.lheight = sc->lheight * UI_DPI_FAC;
222         tvc.ymin = v2d->cur.ymin;
223         tvc.ymax = v2d->cur.ymax;
224         tvc.winx = ar->winx - V2D_SCROLL_WIDTH;
225
226         console_scrollback_prompt_begin(sc, &cl_dummy);
227         ret = textview_draw(&tvc, draw, mval, mouse_pick, pos_pick);
228         console_scrollback_prompt_end(sc, &cl_dummy);
229
230         return ret;
231 }
232
233
234 void console_textview_main(struct SpaceConsole *sc, ARegion *ar)
235 {
236         int mval[2] = {INT_MAX, INT_MAX};
237         console_textview_main__internal(sc, ar, 1,  mval, NULL, NULL);
238 }
239
240 int console_textview_height(struct SpaceConsole *sc, ARegion *ar)
241 {
242         int mval[2] = {INT_MAX, INT_MAX};
243         return console_textview_main__internal(sc, ar, 0,  mval, NULL, NULL);
244 }
245
246 int console_char_pick(struct SpaceConsole *sc, ARegion *ar, const int mval[2])
247 {
248         int pos_pick = 0;
249         void *mouse_pick = NULL;
250         int mval_clamp[2];
251
252         mval_clamp[0] = CLAMPIS(mval[0], CONSOLE_DRAW_MARGIN, ar->winx - CONSOLE_DRAW_MARGIN);
253         mval_clamp[1] = CLAMPIS(mval[1], CONSOLE_DRAW_MARGIN, ar->winy - CONSOLE_DRAW_MARGIN);
254
255         console_textview_main__internal(sc, ar, 0, mval_clamp, &mouse_pick, &pos_pick);
256         return pos_pick;
257 }