Big i18n commit: add "reports" from bmesh/readfile/tracking/dynapaint (and a few...
[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 "BLF_api.h"
35
36 #include "BLI_math.h"
37 #include "BLI_utildefines.h"
38
39 #include "BIF_gl.h"
40 #include "BIF_glutil.h"
41
42 #include "ED_datafiles.h"
43
44 #include "textview.h"
45
46 static void console_font_begin(TextViewContext *sc)
47 {
48         BLF_size(blf_mono_font, sc->lheight - 2, 72);
49 }
50
51 typedef struct ConsoleDrawContext {
52         int cwidth;
53         int lheight;
54         int console_width; /* number of characters that fit into the width of the console (fixed width) */
55         int winx;
56         int ymin, ymax;
57         int *xy; // [2]
58         int *sel; // [2]
59         int *pos_pick; // bottom of view == 0, top of file == combine chars, end of line is lower then start. 
60         int *mval; // [2]
61         int draw;
62 } ConsoleDrawContext;
63
64 static void console_draw_sel(int sel[2], int xy[2], int str_len_draw, int cwidth, int lheight)
65 {
66         if (sel[0] <= str_len_draw && sel[1] >= 0) {
67                 int sta = max_ii(sel[0], 0);
68                 int end = min_ii(sel[1], str_len_draw);
69
70                 glEnable(GL_POLYGON_STIPPLE);
71                 glPolygonStipple(stipple_halftone);
72                 glEnable(GL_BLEND);
73                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
74                 glColor4ub(255, 255, 255, 96);
75
76                 glRecti(xy[0] + (cwidth * sta), xy[1] - 2 + lheight, xy[0] + (cwidth * end), xy[1] - 2);
77
78                 glDisable(GL_POLYGON_STIPPLE);
79                 glDisable(GL_BLEND);
80         }
81 }
82
83
84 /* return 0 if the last line is off the screen
85  * should be able to use this for any string type */
86
87 static int console_draw_string(ConsoleDrawContext *cdc, const char *str, int str_len, unsigned char *fg, unsigned char *bg)
88 {
89 #define STEP_SEL(value) cdc->sel[0] += (value); cdc->sel[1] += (value)
90         int rct_ofs = cdc->lheight / 4;
91         int tot_lines = (str_len / cdc->console_width) + 1; /* total number of lines for wrapping */
92         int y_next = (str_len > cdc->console_width) ? cdc->xy[1] + cdc->lheight * tot_lines : cdc->xy[1] + cdc->lheight;
93         const int mono = blf_mono_font;
94
95         /* just advance the height */
96         if (cdc->draw == 0) {
97                 if (cdc->pos_pick && (cdc->mval[1] != INT_MAX)) {
98                         if (cdc->xy[1] <= cdc->mval[1]) {
99                                 if ((y_next >= cdc->mval[1])) {
100                                         int ofs = (int)floor(((float)cdc->mval[0] / (float)cdc->cwidth));
101
102                                         /* wrap */
103                                         if (str_len > cdc->console_width)
104                                                 ofs += (cdc->console_width * ((int)((((float)(y_next - cdc->mval[1]) / (float)(y_next - cdc->xy[1])) * tot_lines))));
105         
106                                         CLAMP(ofs, 0, str_len);
107                                         *cdc->pos_pick += str_len - ofs;
108                                 }
109                                 else
110                                         *cdc->pos_pick += str_len + 1;
111                         }
112                 }
113
114                 cdc->xy[1] = y_next;
115                 return 1;
116         }
117         else if (y_next - cdc->lheight < cdc->ymin) {
118                 /* have not reached the drawable area so don't break */
119                 cdc->xy[1] = y_next;
120
121                 /* adjust selection even if not drawing */
122                 if (cdc->sel[0] != cdc->sel[1]) {
123                         STEP_SEL(-(str_len + 1));
124                 }
125
126                 return 1;
127         }
128
129         if (tot_lines > 1) { /* wrap? */
130                 const int initial_offset = ((tot_lines - 1) * cdc->console_width);
131                 const char *line_stride = str + initial_offset;  /* advance to the last line and draw it first */
132                 
133                 int sel_orig[2];
134                 copy_v2_v2_int(sel_orig, cdc->sel);
135
136                 /* invert and swap for wrapping */
137                 cdc->sel[0] = str_len - sel_orig[1];
138                 cdc->sel[1] = str_len - sel_orig[0];
139                 
140                 if (bg) {
141                         glColor3ubv(bg);
142                         glRecti(0, cdc->xy[1] - rct_ofs, cdc->winx, (cdc->xy[1] + (cdc->lheight * tot_lines)) + rct_ofs);
143                 }
144
145                 glColor3ubv(fg);
146
147                 /* last part needs no clipping */
148                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
149                 BLF_draw(mono, line_stride, str_len - initial_offset);
150
151                 if (cdc->sel[0] != cdc->sel[1]) {
152                         STEP_SEL(-initial_offset);
153                         // glColor4ub(255, 0, 0, 96); // debug
154                         console_draw_sel(cdc->sel, cdc->xy, str_len % cdc->console_width, cdc->cwidth, cdc->lheight);
155                         STEP_SEL(cdc->console_width);
156                         glColor3ubv(fg);
157                 }
158
159                 cdc->xy[1] += cdc->lheight;
160
161                 line_stride -= cdc->console_width;
162                 
163                 for (; line_stride >= str; line_stride -= cdc->console_width) {
164                         BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
165                         BLF_draw(mono, line_stride, cdc->console_width);
166                         
167                         if (cdc->sel[0] != cdc->sel[1]) {
168                                 // glColor4ub(0, 255, 0, 96); // debug
169                                 console_draw_sel(cdc->sel, cdc->xy, cdc->console_width, cdc->cwidth, cdc->lheight);
170                                 STEP_SEL(cdc->console_width);
171                                 glColor3ubv(fg);
172                         }
173
174                         cdc->xy[1] += cdc->lheight;
175                         
176                         /* check if were out of view bounds */
177                         if (cdc->xy[1] > cdc->ymax)
178                                 return 0;
179                 }
180
181                 copy_v2_v2_int(cdc->sel, sel_orig);
182                 STEP_SEL(-(str_len + 1));
183         }
184         else { /* simple, no wrap */
185
186                 if (bg) {
187                         glColor3ubv(bg);
188                         glRecti(0, cdc->xy[1] - rct_ofs, cdc->winx, cdc->xy[1] + cdc->lheight - rct_ofs);
189                 }
190
191                 glColor3ubv(fg);
192
193                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
194                 BLF_draw(mono, str, str_len);
195                 
196                 if (cdc->sel[0] != cdc->sel[1]) {
197                         int isel[2];
198
199                         isel[0] = str_len - cdc->sel[1];
200                         isel[1] = str_len - cdc->sel[0];
201
202                         // glColor4ub(255, 255, 0, 96); // debug
203                         console_draw_sel(isel, cdc->xy, str_len, cdc->cwidth, cdc->lheight);
204                         STEP_SEL(-(str_len + 1));
205                 }
206
207                 cdc->xy[1] += cdc->lheight;
208
209                 if (cdc->xy[1] > cdc->ymax)
210                         return 0;
211         }
212
213         return 1;
214 #undef STEP_SEL
215 }
216
217 #define CONSOLE_DRAW_MARGIN 4
218 #define CONSOLE_DRAW_SCROLL 16
219
220 int textview_draw(TextViewContext *tvc, int draw, int mval[2], void **mouse_pick, int *pos_pick)
221 {
222         ConsoleDrawContext cdc = {0};
223
224         int x_orig = CONSOLE_DRAW_MARGIN, y_orig = CONSOLE_DRAW_MARGIN + tvc->lheight / 6;
225         int xy[2], y_prev;
226         int sel[2] = {-1, -1}; /* defaults disabled */
227         unsigned char fg[3], bg[3];
228         const int mono = blf_mono_font;
229
230         console_font_begin(tvc);
231
232         xy[0] = x_orig; xy[1] = y_orig;
233
234         if (mval[1] != INT_MAX)
235                 mval[1] += (tvc->ymin + CONSOLE_DRAW_MARGIN);
236
237         if (pos_pick)
238                 *pos_pick = 0;
239
240         /* constants for the sequencer context */
241         cdc.cwidth = (int)BLF_fixed_width(mono);
242         assert(cdc.cwidth > 0);
243         cdc.lheight = tvc->lheight;
244         cdc.console_width = (tvc->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN * 2) ) / cdc.cwidth;
245         CLAMP(cdc.console_width, 1, INT_MAX); /* avoid divide by zero on small windows */
246         cdc.winx = tvc->winx - (CONSOLE_DRAW_MARGIN + CONSOLE_DRAW_SCROLL);
247         cdc.ymin = tvc->ymin;
248         cdc.ymax = tvc->ymax;
249         cdc.xy = xy;
250         cdc.sel = sel;
251         cdc.pos_pick = pos_pick;
252         cdc.mval = mval;
253         cdc.draw = draw;
254
255         /* shouldnt be needed */
256         tvc->cwidth = cdc.cwidth;
257         tvc->console_width = cdc.console_width;
258         tvc->iter_index = 0;
259
260         if (tvc->sel_start != tvc->sel_end) {
261                 sel[0] = tvc->sel_start;
262                 sel[1] = tvc->sel_end;
263         }
264
265         if (tvc->begin(tvc)) {
266
267                 do {
268                         const char *ext_line;
269                         int ext_len;
270                         int color_flag = 0;
271
272                         y_prev = xy[1];
273
274                         if (draw)
275                                 color_flag = tvc->line_color(tvc, fg, bg);
276
277                         tvc->line_get(tvc, &ext_line, &ext_len);
278
279                         if (!console_draw_string(&cdc, ext_line, ext_len, (color_flag & TVC_LINE_FG) ? fg : NULL, (color_flag & TVC_LINE_BG) ? bg : NULL)) {
280                                 /* when drawing, if we pass v2d->cur.ymax, then quit */
281                                 if (draw) {
282                                         break; /* past the y limits */
283                                 }
284                         }
285
286                         if ((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
287                                 *mouse_pick = (void *)tvc->iter;
288                                 break;
289                         }
290
291                         tvc->iter_index++;
292
293                 } while (tvc->step(tvc));
294         }
295
296         tvc->end(tvc);
297
298         xy[1] += tvc->lheight * 2;
299
300         return xy[1] - y_orig;
301 }