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