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