console text underscore would draw outside the view for larger font sizes.
[blender.git] / source / blender / editors / space_info / textview.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <math.h>
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <limits.h>
29 #include <assert.h>
30
31 #include "BLF_api.h"
32
33 #include "BKE_utildefines.h"
34
35 #include "BIF_gl.h"
36 #include "BIF_glutil.h"
37
38 #include "ED_datafiles.h"
39
40 #include "textview.h"
41
42 static void console_font_begin(TextViewContext *sc)
43 {
44         BLF_aspect(blf_mono_font, 1.0);
45         BLF_size(blf_mono_font, sc->lheight-2, 72);
46 }
47
48 typedef struct ConsoleDrawContext {
49         int cwidth;
50         int lheight;
51         int console_width; /* number of characters that fit into the width of the console (fixed width) */
52         int winx;
53         int ymin, ymax;
54         int *xy; // [2]
55         int *sel; // [2]
56         int *pos_pick; // bottom of view == 0, top of file == combine chars, end of line is lower then start. 
57         int *mval; // [2]
58         int draw;
59 } ConsoleDrawContext;
60
61 static void console_draw_sel(int sel[2], int xy[2], int str_len_draw, int cwidth, int lheight)
62 {
63         if(sel[0] <= str_len_draw && sel[1] >= 0) {
64                 int sta = MAX2(sel[0], 0);
65                 int end = MIN2(sel[1], str_len_draw);
66
67                 glEnable(GL_POLYGON_STIPPLE);
68                 glPolygonStipple(stipple_halftone);
69                 glEnable( GL_BLEND );
70                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
71                 glColor4ub(255, 255, 255, 96);
72
73                 glRecti(xy[0]+(cwidth*sta), xy[1]-2 + lheight, xy[0]+(cwidth*end), xy[1]-2);
74
75                 glDisable(GL_POLYGON_STIPPLE);
76                 glDisable( GL_BLEND );
77         }
78 }
79
80
81 /* return 0 if the last line is off the screen
82  * should be able to use this for any string type */
83
84 static int console_draw_string(ConsoleDrawContext *cdc, const char *str, int str_len, unsigned char *fg, unsigned char *bg)
85 {
86 #define STEP_SEL(value) cdc->sel[0] += (value); cdc->sel[1] += (value)
87         int rct_ofs= cdc->lheight/4;
88         int tot_lines = (str_len/cdc->console_width)+1; /* total number of lines for wrapping */
89         int y_next = (str_len > cdc->console_width) ? cdc->xy[1]+cdc->lheight*tot_lines : cdc->xy[1]+cdc->lheight;
90         const int mono= blf_mono_font;
91
92         /* just advance the height */
93         if(cdc->draw==0) {
94                 if(cdc->pos_pick && (cdc->mval[1] != INT_MAX)) {
95                         if(cdc->xy[1] <= cdc->mval[1]) {
96                                 if((y_next >= cdc->mval[1])) {
97                                         int ofs = (int)floor(((float)cdc->mval[0] / (float)cdc->cwidth));
98
99                                         /* wrap */
100                                         if(str_len > cdc->console_width)
101                                                 ofs += (cdc->console_width * ((int)((((float)(y_next - cdc->mval[1]) / (float)(y_next-cdc->xy[1])) * tot_lines))));
102         
103                                         CLAMP(ofs, 0, str_len);
104                                         *cdc->pos_pick += str_len - ofs;
105                                 } else
106                                         *cdc->pos_pick += str_len + 1;
107                         }
108                 }
109
110                 cdc->xy[1]= y_next;
111                 return 1;
112         }
113         else if (y_next-cdc->lheight < cdc->ymin) {
114                 /* have not reached the drawable area so don't break */
115                 cdc->xy[1]= y_next;
116
117                 /* adjust selection even if not drawing */
118                 if(cdc->sel[0] != cdc->sel[1]) {
119                         STEP_SEL(-(str_len + 1));
120                 }
121
122                 return 1;
123         }
124
125         if(str_len > cdc->console_width) { /* wrap? */
126                 const int initial_offset= ((tot_lines-1) * cdc->console_width);
127                 const char *line_stride= str + initial_offset;  /* advance to the last line and draw it first */
128                 
129                 int sel_orig[2];
130                 VECCOPY2D(sel_orig, cdc->sel);
131
132                 /* invert and swap for wrapping */
133                 cdc->sel[0] = str_len - sel_orig[1];
134                 cdc->sel[1] = str_len - sel_orig[0];
135                 
136                 if(bg) {
137                         glColor3ubv(bg);
138                         glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, (cdc->xy[1]+(cdc->lheight*tot_lines))+rct_ofs);
139                 }
140
141                 glColor3ubv(fg);
142
143                 /* last part needs no clipping */
144                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
145                 BLF_draw(mono, line_stride, str_len - initial_offset);
146
147                 if(cdc->sel[0] != cdc->sel[1]) {
148                         STEP_SEL(-initial_offset);
149                         // glColor4ub(255, 0, 0, 96); // debug
150                         console_draw_sel(cdc->sel, cdc->xy, str_len % cdc->console_width, cdc->cwidth, cdc->lheight);
151                         STEP_SEL(cdc->console_width);
152                         glColor3ubv(fg);
153                 }
154
155                 cdc->xy[1] += cdc->lheight;
156
157                 line_stride -= cdc->console_width;
158                 
159                 for(; line_stride >= str; line_stride -= cdc->console_width) {
160                         BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
161                         BLF_draw(mono, line_stride, cdc->console_width);
162                         
163                         if(cdc->sel[0] != cdc->sel[1]) {
164                                 // glColor4ub(0, 255, 0, 96); // debug
165                                 console_draw_sel(cdc->sel, cdc->xy, cdc->console_width, cdc->cwidth, cdc->lheight);
166                                 STEP_SEL(cdc->console_width);
167                                 glColor3ubv(fg);
168                         }
169
170                         cdc->xy[1] += cdc->lheight;
171                         
172                         /* check if were out of view bounds */
173                         if(cdc->xy[1] > cdc->ymax)
174                                 return 0;
175                 }
176
177                 VECCOPY2D(cdc->sel, sel_orig);
178                 STEP_SEL(-(str_len + 1));
179         }
180         else { /* simple, no wrap */
181
182                 if(bg) {
183                         glColor3ubv(bg);
184                         glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, cdc->xy[1]+cdc->lheight-rct_ofs);
185                 }
186
187                 glColor3ubv(fg);
188
189                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
190                 BLF_draw(mono, str, str_len);
191                 
192                 if(cdc->sel[0] != cdc->sel[1]) {
193                         int isel[2];
194
195                         isel[0]= str_len - cdc->sel[1];
196                         isel[1]= str_len - cdc->sel[0];
197
198                         // glColor4ub(255, 255, 0, 96); // debug
199                         console_draw_sel(isel, cdc->xy, str_len, cdc->cwidth, cdc->lheight);
200                         STEP_SEL(-(str_len + 1));
201                 }
202
203                 cdc->xy[1] += cdc->lheight;
204
205                 if(cdc->xy[1] > cdc->ymax)
206                         return 0;
207         }
208
209         return 1;
210 #undef STEP_SEL
211 }
212
213 #define CONSOLE_DRAW_MARGIN 4
214 #define CONSOLE_DRAW_SCROLL 16
215
216 int textview_draw(TextViewContext *tvc, int draw, int mval[2], void **mouse_pick, int *pos_pick)
217 {
218         ConsoleDrawContext cdc= {0};
219
220         int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN + tvc->lheight/6;
221         int xy[2], y_prev;
222         int sel[2]= {-1, -1}; /* defaults disabled */
223         unsigned char fg[3], bg[3];
224         const int mono= blf_mono_font;
225
226         console_font_begin(tvc);
227
228         xy[0]= x_orig; xy[1]= y_orig;
229
230         if(mval[1] != INT_MAX)
231                 mval[1] += (tvc->ymin + CONSOLE_DRAW_MARGIN);
232
233         if(pos_pick)
234                 *pos_pick = 0;
235
236         /* constants for the sequencer context */
237         cdc.cwidth= (int)BLF_fixed_width(mono);
238         assert(cdc.cwidth > 0);
239         cdc.lheight= tvc->lheight;
240         cdc.console_width= (tvc->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) ) / cdc.cwidth;
241         CLAMP(cdc.console_width, 1, INT_MAX); /* avoid divide by zero on small windows */
242         cdc.winx= tvc->winx-(CONSOLE_DRAW_MARGIN+CONSOLE_DRAW_SCROLL);
243         cdc.ymin= tvc->ymin;
244         cdc.ymax= tvc->ymax;
245         cdc.xy= xy;
246         cdc.sel= sel;
247         cdc.pos_pick= pos_pick;
248         cdc.mval= mval;
249         cdc.draw= draw;
250
251         /* shouldnt be needed */
252         tvc->cwidth= cdc.cwidth;
253         tvc->iter_index= 0;
254
255         if(tvc->sel_start != tvc->sel_end) {
256                 sel[0]= tvc->sel_start;
257                 sel[1]= tvc->sel_end;
258         }
259
260         if(tvc->begin(tvc)) {
261
262                 do {
263                         const char *ext_line;
264                         int ext_len;
265                         int color_flag= 0;
266
267                         y_prev= xy[1];
268
269                         if(draw)
270                                 color_flag= tvc->line_color(tvc, fg, bg);
271
272                         tvc->line_get(tvc, &ext_line, &ext_len);
273
274                         if(!console_draw_string(&cdc, ext_line, ext_len, (color_flag & TVC_LINE_FG) ? fg : NULL, (color_flag & TVC_LINE_BG) ? bg : NULL)) {
275                                 /* when drawing, if we pass v2d->cur.ymax, then quit */
276                                 if(draw) {
277                                         break; /* past the y limits */
278                                 }
279                         }
280
281                         if((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
282                                 *mouse_pick= (void *)tvc->iter;
283                                 break;
284                         }
285
286                         tvc->iter_index++;
287
288                 } while(tvc->step(tvc));
289         }
290
291         tvc->end(tvc);
292
293         xy[1] += tvc->lheight * 2;
294
295         return xy[1] - y_orig;
296 }