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