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