Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / space_info / textview.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_info/textview.c
24  *  \ingroup spinfo
25  */
26
27
28 #include <math.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <limits.h>
32 #include <assert.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLF_api.h"
37
38 #include "BLI_math.h"
39 #include "BLI_utildefines.h"
40 #include "BLI_string_utf8.h"
41
42 #include "GPU_immediate.h"
43
44 #include "BIF_gl.h"
45
46 #include "BKE_text.h"
47
48 #include "textview.h"
49
50 static void console_font_begin(const int font_id, const int lheight)
51 {
52         /* 0.875 is based on: 16 pixels lines get 14 pixel text */
53         BLF_size(font_id, 0.875 * lheight, 72);
54 }
55
56 typedef struct ConsoleDrawContext {
57         int font_id;
58         int cwidth;
59         int lheight;
60         int lofs; /* text vertical offset */
61         int console_width; /* number of characters that fit into the width of the console (fixed width) */
62         int winx;
63         int ymin, ymax;
64         int *xy; // [2]
65         int *sel; // [2]
66         int *pos_pick; // bottom of view == 0, top of file == combine chars, end of line is lower then start.
67         const int *mval; // [2]
68         int draw;
69 } ConsoleDrawContext;
70
71 BLI_INLINE void console_step_sel(ConsoleDrawContext *cdc, const int step)
72 {
73         cdc->sel[0] += step;
74         cdc->sel[1] += step;
75 }
76
77 static void console_draw_sel(const char *str, const int sel[2], const int xy[2], const int str_len_draw,
78                              int cwidth, int lheight, const unsigned char bg_sel[4])
79 {
80         if (sel[0] <= str_len_draw && sel[1] >= 0) {
81                 const int sta = txt_utf8_offset_to_column(str, max_ii(sel[0], 0));
82                 const int end = txt_utf8_offset_to_column(str, min_ii(sel[1], str_len_draw));
83
84                 glEnable(GL_BLEND);
85                 glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
86
87                 Gwn_VertFormat *format = immVertexFormat();
88                 unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
89                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
90
91                 immUniformColor4ubv(bg_sel);
92                 immRecti(pos, xy[0] + (cwidth * sta), xy[1] - 2 + lheight, xy[0] + (cwidth * end), xy[1] - 2);
93
94                 immUnbindProgram();
95
96                 glDisable(GL_BLEND);
97         }
98 }
99
100 /* warning: allocated memory for 'offsets' must be freed by caller */
101 static int console_wrap_offsets(const char *str, int len, int width, int *lines, int **offsets)
102 {
103         int i, end;  /* column */
104         int j;       /* mem */
105
106         *lines = 1;
107
108         *offsets = MEM_callocN(sizeof(**offsets) * (len * BLI_UTF8_WIDTH_MAX / MAX2(1, width - (BLI_UTF8_WIDTH_MAX - 1)) + 1),
109                                "console_wrap_offsets");
110         (*offsets)[0] = 0;
111
112         for (i = 0, end = width, j = 0; j < len && str[j]; j += BLI_str_utf8_size_safe(str + j)) {
113                 int columns = BLI_str_utf8_char_width_safe(str + j);
114
115                 if (i + columns > end) {
116                         (*offsets)[*lines] = j;
117                         (*lines)++;
118
119                         end = i + width;
120                 }
121                 i += columns;
122         }
123         return j; /* return actual length */
124 }
125
126 /* return 0 if the last line is off the screen
127  * should be able to use this for any string type */
128
129 static int console_draw_string(ConsoleDrawContext *cdc, const char *str, int str_len,
130                                const unsigned char fg[3], const unsigned char bg[3], const unsigned char bg_sel[4])
131 {
132         int tot_lines;            /* total number of lines for wrapping */
133         int *offsets;             /* offsets of line beginnings for wrapping */
134         int y_next;
135
136         str_len = console_wrap_offsets(str, str_len, cdc->console_width, &tot_lines, &offsets);
137         y_next = cdc->xy[1] + cdc->lheight * tot_lines;
138
139         /* just advance the height */
140         if (cdc->draw == 0) {
141                 if (cdc->pos_pick && cdc->mval[1] != INT_MAX && cdc->xy[1] <= cdc->mval[1]) {
142                         if (y_next >= cdc->mval[1]) {
143                                 int ofs = 0;
144
145                                 /* wrap */
146                                 if (tot_lines > 1) {
147                                         int iofs = (int)((float)(y_next - cdc->mval[1]) / cdc->lheight);
148                                         ofs += offsets[MIN2(iofs, tot_lines - 1)];
149                                 }
150
151                                 /* last part */
152                                 ofs += txt_utf8_column_to_offset(str + ofs,
153                                                                  (int)floor((float)cdc->mval[0] / cdc->cwidth));
154
155                                 CLAMP(ofs, 0, str_len);
156                                 *cdc->pos_pick += str_len - ofs;
157                         }
158                         else
159                                 *cdc->pos_pick += str_len + 1;
160                 }
161
162                 cdc->xy[1] = y_next;
163                 MEM_freeN(offsets);
164                 return 1;
165         }
166         else if (y_next < cdc->ymin) {
167                 /* have not reached the drawable area so don't break */
168                 cdc->xy[1] = y_next;
169
170                 /* adjust selection even if not drawing */
171                 if (cdc->sel[0] != cdc->sel[1]) {
172                         console_step_sel(cdc, -(str_len + 1));
173                 }
174
175                 MEM_freeN(offsets);
176                 return 1;
177         }
178
179         if (tot_lines > 1) { /* wrap? */
180                 const int initial_offset = offsets[tot_lines - 1];
181                 size_t len = str_len - initial_offset;
182                 const char *s = str + initial_offset;
183                 int i;
184
185                 int sel_orig[2];
186                 copy_v2_v2_int(sel_orig, cdc->sel);
187
188                 /* invert and swap for wrapping */
189                 cdc->sel[0] = str_len - sel_orig[1];
190                 cdc->sel[1] = str_len - sel_orig[0];
191
192                 if (bg) {
193                         Gwn_VertFormat *format = immVertexFormat();
194                         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
195                         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
196
197                         immUniformColor3ubv(bg);
198                         immRecti(pos, 0, cdc->xy[1], cdc->winx, (cdc->xy[1] + (cdc->lheight * tot_lines)));
199
200                         immUnbindProgram();
201                 }
202
203                 /* last part needs no clipping */
204                 BLF_position(cdc->font_id, cdc->xy[0], cdc->lofs + cdc->xy[1], 0);
205                 BLF_color3ubv(cdc->font_id, fg);
206                 BLF_draw_mono(cdc->font_id, s, len, cdc->cwidth);
207
208                 if (cdc->sel[0] != cdc->sel[1]) {
209                         console_step_sel(cdc, -initial_offset);
210                         /* BLF_color3ub(cdc->font_id, 255, 0, 0); // debug */
211                         console_draw_sel(s, cdc->sel, cdc->xy, len, cdc->cwidth, cdc->lheight, bg_sel);
212                 }
213
214                 cdc->xy[1] += cdc->lheight;
215
216                 for (i = tot_lines - 1; i > 0; i--) {
217                         len = offsets[i] - offsets[i - 1];
218                         s = str + offsets[i - 1];
219
220                         BLF_position(cdc->font_id, cdc->xy[0], cdc->lofs + cdc->xy[1], 0);
221                         BLF_draw_mono(cdc->font_id, s, len, cdc->cwidth);
222
223                         if (cdc->sel[0] != cdc->sel[1]) {
224                                 console_step_sel(cdc, len);
225                                 /* BLF_color3ub(cdc->font_id, 0, 255, 0); // debug */
226                                 console_draw_sel(s, cdc->sel, cdc->xy, len, cdc->cwidth, cdc->lheight, bg_sel);
227                         }
228
229                         cdc->xy[1] += cdc->lheight;
230
231                         /* check if were out of view bounds */
232                         if (cdc->xy[1] > cdc->ymax) {
233                                 MEM_freeN(offsets);
234                                 return 0;
235                         }
236                 }
237
238                 copy_v2_v2_int(cdc->sel, sel_orig);
239                 console_step_sel(cdc, -(str_len + 1));
240         }
241         else { /* simple, no wrap */
242
243                 if (bg) {
244                         Gwn_VertFormat *format = immVertexFormat();
245                         unsigned int pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_I32, 2, GWN_FETCH_INT_TO_FLOAT);
246                         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
247
248                         immUniformColor3ubv(bg);
249                         immRecti(pos, 0, cdc->xy[1], cdc->winx, cdc->xy[1] + cdc->lheight);
250
251                         immUnbindProgram();
252                 }
253
254                 BLF_color3ubv(cdc->font_id, fg);
255                 BLF_position(cdc->font_id, cdc->xy[0], cdc->lofs + cdc->xy[1], 0);
256                 BLF_draw_mono(cdc->font_id, str, str_len, cdc->cwidth);
257
258                 if (cdc->sel[0] != cdc->sel[1]) {
259                         int isel[2];
260
261                         isel[0] = str_len - cdc->sel[1];
262                         isel[1] = str_len - cdc->sel[0];
263
264                         /* BLF_color3ub(cdc->font_id, 255, 255, 0); // debug */
265                         console_draw_sel(str, isel, cdc->xy, str_len, cdc->cwidth, cdc->lheight, bg_sel);
266                         console_step_sel(cdc, -(str_len + 1));
267                 }
268
269                 cdc->xy[1] += cdc->lheight;
270
271                 if (cdc->xy[1] > cdc->ymax) {
272                         MEM_freeN(offsets);
273                         return 0;
274                 }
275         }
276
277         MEM_freeN(offsets);
278         return 1;
279 }
280
281 #define CONSOLE_DRAW_MARGIN 4
282
283 int textview_draw(TextViewContext *tvc, const int draw, int mval[2], void **mouse_pick, int *pos_pick)
284 {
285         ConsoleDrawContext cdc = {0};
286
287         int x_orig = CONSOLE_DRAW_MARGIN, y_orig = CONSOLE_DRAW_MARGIN + tvc->lheight / 6;
288         int xy[2], y_prev;
289         int sel[2] = {-1, -1}; /* defaults disabled */
290         unsigned char fg[3], bg[3];
291         const int font_id = blf_mono_font;
292
293         console_font_begin(font_id, tvc->lheight);
294
295         xy[0] = x_orig; xy[1] = y_orig;
296
297         if (mval[1] != INT_MAX)
298                 mval[1] += (tvc->ymin + CONSOLE_DRAW_MARGIN);
299
300         if (pos_pick)
301                 *pos_pick = 0;
302
303         /* constants for the sequencer context */
304         cdc.font_id = font_id;
305         cdc.cwidth = (int)BLF_fixed_width(font_id);
306         assert(cdc.cwidth > 0);
307         cdc.lheight = tvc->lheight;
308         cdc.lofs = -BLF_descender(font_id);
309         /* note, scroll bar must be already subtracted () */
310         cdc.console_width = (tvc->winx - (CONSOLE_DRAW_MARGIN * 2)) / cdc.cwidth;
311         /* avoid divide by zero on small windows */
312         if (cdc.console_width < 1)
313                 cdc.console_width = 1;
314         cdc.winx = tvc->winx - CONSOLE_DRAW_MARGIN;
315         cdc.ymin = tvc->ymin;
316         cdc.ymax = tvc->ymax;
317         cdc.xy = xy;
318         cdc.sel = sel;
319         cdc.pos_pick = pos_pick;
320         cdc.mval = mval;
321         cdc.draw = draw;
322
323         /* shouldnt be needed */
324         tvc->cwidth = cdc.cwidth;
325         tvc->console_width = cdc.console_width;
326         tvc->iter_index = 0;
327
328         if (tvc->sel_start != tvc->sel_end) {
329                 sel[0] = tvc->sel_start;
330                 sel[1] = tvc->sel_end;
331         }
332
333         if (tvc->begin(tvc)) {
334                 unsigned char bg_sel[4] = {0};
335
336                 if (draw && tvc->const_colors) {
337                         tvc->const_colors(tvc, bg_sel);
338                 }
339
340                 do {
341                         const char *ext_line;
342                         int ext_len;
343                         int color_flag = 0;
344
345                         y_prev = xy[1];
346
347                         if (draw)
348                                 color_flag = tvc->line_color(tvc, fg, bg);
349
350                         tvc->line_get(tvc, &ext_line, &ext_len);
351
352                         if (!console_draw_string(&cdc, ext_line, ext_len,
353                                                  (color_flag & TVC_LINE_FG) ? fg : NULL,
354                                                  (color_flag & TVC_LINE_BG) ? bg : NULL,
355                                                  bg_sel))
356                         {
357                                 /* when drawing, if we pass v2d->cur.ymax, then quit */
358                                 if (draw) {
359                                         break; /* past the y limits */
360                                 }
361                         }
362
363                         if ((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
364                                 *mouse_pick = (void *)tvc->iter;
365                                 break;
366                         }
367
368                         tvc->iter_index++;
369
370                 } while (tvc->step(tvc));
371         }
372
373         tvc->end(tvc);
374
375         xy[1] += tvc->lheight * 2;
376
377         return xy[1] - y_orig;
378 }