BLF_draw functions take an extra length argument, so the console drawing doenst need...
[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
43 static int mono= -1; // XXX needs proper storage and change all the BLF_* here!
44
45
46 static void console_font_begin(TextViewContext *sc)
47 {
48         if(mono == -1) {
49                 mono= BLF_load_mem("monospace", (unsigned char*)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
50         }
51
52         BLF_aspect(mono, 1.0);
53         BLF_size(mono, sc->lheight-2, 72);
54 }
55
56 typedef struct ConsoleDrawContext {
57         int cwidth;
58         int lheight;
59         int console_width; /* number of characters that fit into the width of the console (fixed width) */
60         int winx;
61         int ymin, ymax;
62         int *xy; // [2]
63         int *sel; // [2]
64         int *pos_pick; // bottom of view == 0, top of file == combine chars, end of line is lower then start. 
65         int *mval; // [2]
66         int draw;
67 } ConsoleDrawContext;
68
69 static void console_draw_sel(int sel[2], int xy[2], int str_len_draw, int cwidth, int lheight)
70 {
71         if(sel[0] <= str_len_draw && sel[1] >= 0) {
72                 int sta = MAX2(sel[0], 0);
73                 int end = MIN2(sel[1], str_len_draw);
74
75                 glEnable(GL_POLYGON_STIPPLE);
76                 glPolygonStipple(stipple_halftone);
77                 glEnable( GL_BLEND );
78                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
79                 glColor4ub(255, 255, 255, 96);
80
81                 glRecti(xy[0]+(cwidth*sta), xy[1]-2 + lheight, xy[0]+(cwidth*end), xy[1]-2);
82
83                 glDisable(GL_POLYGON_STIPPLE);
84                 glDisable( GL_BLEND );
85         }
86 }
87
88
89 /* return 0 if the last line is off the screen
90  * should be able to use this for any string type */
91
92 static int console_draw_string(ConsoleDrawContext *cdc, const char *str, int str_len, unsigned char *fg, unsigned char *bg)
93 {
94 #define STEP_SEL(value) cdc->sel[0] += (value); cdc->sel[1] += (value)
95         int rct_ofs= cdc->lheight/4;
96         int tot_lines = (str_len/cdc->console_width)+1; /* total number of lines for wrapping */
97         int y_next = (str_len > cdc->console_width) ? cdc->xy[1]+cdc->lheight*tot_lines : cdc->xy[1]+cdc->lheight;
98
99         /* just advance the height */
100         if(cdc->draw==0) {
101                 if(cdc->pos_pick && (cdc->mval[1] != INT_MAX)) {
102                         if(cdc->xy[1] <= cdc->mval[1]) {
103                                 if((y_next >= cdc->mval[1])) {
104                                         int ofs = (int)floor(((float)cdc->mval[0] / (float)cdc->cwidth));
105
106                                         /* wrap */
107                                         if(str_len > cdc->console_width)
108                                                 ofs += (cdc->console_width * ((int)((((float)(y_next - cdc->mval[1]) / (float)(y_next-cdc->xy[1])) * tot_lines))));
109         
110                                         CLAMP(ofs, 0, str_len);
111                                         *cdc->pos_pick += str_len - ofs;
112                                 } else
113                                         *cdc->pos_pick += str_len + 1;
114                         }
115                 }
116
117                 cdc->xy[1]= y_next;
118                 return 1;
119         }
120         else if (y_next-cdc->lheight < cdc->ymin) {
121                 /* have not reached the drawable area so don't break */
122                 cdc->xy[1]= y_next;
123
124                 /* adjust selection even if not drawing */
125                 if(cdc->sel[0] != cdc->sel[1]) {
126                         STEP_SEL(-(str_len + 1));
127                 }
128
129                 return 1;
130         }
131
132         if(str_len > cdc->console_width) { /* wrap? */
133                 const int initial_offset= ((tot_lines-1) * cdc->console_width);
134                 const char *line_stride= str + initial_offset;  /* advance to the last line and draw it first */
135                 
136                 int sel_orig[2];
137                 VECCOPY2D(sel_orig, cdc->sel);
138
139                 /* invert and swap for wrapping */
140                 cdc->sel[0] = str_len - sel_orig[1];
141                 cdc->sel[1] = str_len - sel_orig[0];
142                 
143                 if(bg) {
144                         glColor3ubv(bg);
145                         glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, (cdc->xy[1]+(cdc->lheight*tot_lines))+rct_ofs);
146                 }
147
148                 glColor3ubv(fg);
149
150                 /* last part needs no clipping */
151                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
152                 BLF_draw(mono, line_stride, str_len - initial_offset);
153
154                 if(cdc->sel[0] != cdc->sel[1]) {
155                         STEP_SEL(-initial_offset);
156                         // glColor4ub(255, 0, 0, 96); // debug
157                         console_draw_sel(cdc->sel, cdc->xy, str_len % cdc->console_width, cdc->cwidth, cdc->lheight);
158                         STEP_SEL(cdc->console_width);
159                         glColor3ubv(fg);
160                 }
161
162                 cdc->xy[1] += cdc->lheight;
163
164                 line_stride -= cdc->console_width;
165                 
166                 for(; line_stride >= str; line_stride -= cdc->console_width) {
167                         BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
168                         BLF_draw(mono, line_stride, cdc->console_width);
169                         
170                         if(cdc->sel[0] != cdc->sel[1]) {
171                                 // glColor4ub(0, 255, 0, 96); // debug
172                                 console_draw_sel(cdc->sel, cdc->xy, cdc->console_width, cdc->cwidth, cdc->lheight);
173                                 STEP_SEL(cdc->console_width);
174                                 glColor3ubv(fg);
175                         }
176
177                         cdc->xy[1] += cdc->lheight;
178                         
179                         /* check if were out of view bounds */
180                         if(cdc->xy[1] > cdc->ymax)
181                                 return 0;
182                 }
183
184                 VECCOPY2D(cdc->sel, sel_orig);
185                 STEP_SEL(-(str_len + 1));
186         }
187         else { /* simple, no wrap */
188
189                 if(bg) {
190                         glColor3ubv(bg);
191                         glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, cdc->xy[1]+cdc->lheight-rct_ofs);
192                 }
193
194                 glColor3ubv(fg);
195
196                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
197                 BLF_draw(mono, str, str_len);
198                 
199                 if(cdc->sel[0] != cdc->sel[1]) {
200                         int isel[2];
201
202                         isel[0]= str_len - cdc->sel[1];
203                         isel[1]= str_len - cdc->sel[0];
204
205                         // glColor4ub(255, 255, 0, 96); // debug
206                         console_draw_sel(isel, cdc->xy, str_len, cdc->cwidth, cdc->lheight);
207                         STEP_SEL(-(str_len + 1));
208                 }
209
210                 cdc->xy[1] += cdc->lheight;
211
212                 if(cdc->xy[1] > cdc->ymax)
213                         return 0;
214         }
215
216         return 1;
217 #undef STEP_SEL
218 }
219
220 #define CONSOLE_DRAW_MARGIN 4
221 #define CONSOLE_DRAW_SCROLL 16
222
223 int textview_draw(TextViewContext *tvc, int draw, int mval[2], void **mouse_pick, int *pos_pick)
224 {
225         ConsoleDrawContext cdc= {0};
226
227         int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
228         int xy[2], y_prev;
229         int sel[2]= {-1, -1}; /* defaults disabled */
230         unsigned char fg[3], bg[3];
231
232         console_font_begin(tvc);
233
234         xy[0]= x_orig; xy[1]= y_orig;
235
236         if(mval[1] != INT_MAX)
237                 mval[1] += (tvc->ymin + CONSOLE_DRAW_MARGIN);
238
239         if(pos_pick)
240                 *pos_pick = 0;
241
242         /* constants for the sequencer context */
243         cdc.cwidth= (int)BLF_fixed_width(mono);
244         assert(cdc.cwidth > 0);
245         cdc.lheight= tvc->lheight;
246         cdc.console_width= (tvc->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) ) / cdc.cwidth;
247         cdc.winx= tvc->winx-(CONSOLE_DRAW_MARGIN+CONSOLE_DRAW_SCROLL);
248         cdc.ymin= tvc->ymin;
249         cdc.ymax= tvc->ymax;
250         cdc.xy= xy;
251         cdc.sel= sel;
252         cdc.pos_pick= pos_pick;
253         cdc.mval= mval;
254         cdc.draw= draw;
255
256         /* shouldnt be needed */
257         tvc->cwidth= cdc.cwidth;
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 }