fix for compiling with the c90 standard, support for non-static variable initializers...
[blender.git] / source / blender / editors / space_console / console_draw.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  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <limits.h>
35
36
37 #include "BLF_api.h"
38
39 #include "BLI_blenlib.h"
40
41 #include "DNA_space_types.h"
42 #include "DNA_screen_types.h"
43
44 // #include "BKE_suggestions.h"
45 #include "BKE_report.h"
46 #include "BKE_utildefines.h"
47
48 #include "MEM_guardedalloc.h"
49
50 #include "BIF_gl.h"
51 #include "BIF_glutil.h"
52
53 #include "ED_datafiles.h"
54 #include "ED_types.h"
55
56 #include "UI_resources.h"
57
58 #include "console_intern.h"
59
60
61 static int mono= -1; // XXX needs proper storage and change all the BLF_* here!
62
63 static void console_font_begin(SpaceConsole *sc)
64 {
65         if(mono == -1)
66                 mono= BLF_load_mem("monospace", (unsigned char*)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
67
68         BLF_aspect(mono, 1.0);
69         BLF_size(mono, sc->lheight-2, 72);
70 }
71
72 static void console_line_color(unsigned char *fg, int type)
73 {
74         switch(type) {
75         case CONSOLE_LINE_OUTPUT:
76                 UI_GetThemeColor3ubv(TH_CONSOLE_OUTPUT, (char *)fg);
77                 break;
78         case CONSOLE_LINE_INPUT:
79                 UI_GetThemeColor3ubv(TH_CONSOLE_INPUT, (char *)fg);
80                 break;
81         case CONSOLE_LINE_INFO:
82                 UI_GetThemeColor3ubv(TH_CONSOLE_INFO, (char *)fg);
83                 break;
84         case CONSOLE_LINE_ERROR:
85                 UI_GetThemeColor3ubv(TH_CONSOLE_ERROR, (char *)fg);
86                 break;
87         }
88 }
89
90 static void console_report_color(unsigned char *fg, unsigned char *bg, Report *report, int bool)
91 {
92         /*
93         if              (type & RPT_ERROR_ALL)          { fg[0]=220; fg[1]=0; fg[2]=0; }
94         else if (type & RPT_WARNING_ALL)        { fg[0]=220; fg[1]=96; fg[2]=96; }
95         else if (type & RPT_OPERATOR_ALL)       { fg[0]=96; fg[1]=128; fg[2]=255; }
96         else if (type & RPT_INFO_ALL)           { fg[0]=0; fg[1]=170; fg[2]=0; }
97         else if (type & RPT_DEBUG_ALL)          { fg[0]=196; fg[1]=196; fg[2]=196; }
98         else                                                            { fg[0]=196; fg[1]=196; fg[2]=196; }
99         */
100         if(report->flag & SELECT) {
101                 fg[0]=255; fg[1]=255; fg[2]=255;
102                 if(bool) {
103                         bg[0]=96; bg[1]=128; bg[2]=255;
104                 }
105                 else {
106                         bg[0]=90; bg[1]=122; bg[2]=249;
107                 }
108         }
109
110         else {
111                 fg[0]=0; fg[1]=0; fg[2]=0;
112
113                 if(bool) {
114                         bg[0]=120; bg[1]=120; bg[2]=120;
115                 }
116                 else {
117                         bg[0]=114; bg[1]=114; bg[2]=114;
118                 }
119
120         }
121 }
122
123 typedef struct ConsoleDrawContext {
124         int cwidth;
125         int lheight;
126         int console_width;
127         int winx;
128         int ymin, ymax;
129         int *xy; // [2]
130         int *sel; // [2]
131         int *pos_pick; // bottom of view == 0, top of file == combine chars, end of line is lower then start. 
132         int *mval; // [2]
133         int draw;
134 } ConsoleDrawContext;
135
136 static void console_draw_sel(int sel[2], int xy[2], int str_len_draw, int cwidth, int lheight)
137 {
138         if(sel[0] <= str_len_draw && sel[1] >= 0) {
139                 int sta = MAX2(sel[0], 0);
140                 int end = MIN2(sel[1], str_len_draw);
141
142                 glEnable(GL_POLYGON_STIPPLE);
143                 glPolygonStipple(stipple_halftone);
144                 glEnable( GL_BLEND );
145                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
146                 glColor4ub(255, 255, 255, 96);
147
148                 glRecti(xy[0]+(cwidth*sta), xy[1]-2 + lheight, xy[0]+(cwidth*end), xy[1]-2);
149
150                 glDisable(GL_POLYGON_STIPPLE);
151                 glDisable( GL_BLEND );
152         }
153 }
154
155
156 /* return 0 if the last line is off the screen
157  * should be able to use this for any string type */
158
159 static int console_draw_string(ConsoleDrawContext *cdc, char *str, int str_len, unsigned char *fg, unsigned char *bg)
160 {
161 #define STEP_SEL(value) cdc->sel[0] += (value); cdc->sel[1] += (value)
162         int rct_ofs= cdc->lheight/4;
163         int tot_lines = (str_len/cdc->console_width)+1; /* total number of lines for wrapping */
164         int y_next = (str_len > cdc->console_width) ? cdc->xy[1]+cdc->lheight*tot_lines : cdc->xy[1]+cdc->lheight;
165
166         /* just advance the height */
167         if(cdc->draw==0) {
168                 if(cdc->pos_pick && (cdc->mval[1] != INT_MAX)) {
169                         if(cdc->xy[1] <= cdc->mval[1]) {
170                                 if((y_next >= cdc->mval[1])) {
171                                         int ofs = (int)floor(((float)cdc->mval[0] / (float)cdc->cwidth));
172
173                                         /* wrap */
174                                         if(str_len > cdc->console_width)
175                                                 ofs += (cdc->console_width * ((int)((((float)(y_next - cdc->mval[1]) / (float)(y_next-cdc->xy[1])) * tot_lines))));
176         
177                                         CLAMP(ofs, 0, str_len);
178                                         *cdc->pos_pick += str_len - ofs;
179                                 } else
180                                         *cdc->pos_pick += str_len + 1;
181                         }
182                 }
183
184                 cdc->xy[1]= y_next;
185                 return 1;
186         }
187         else if (y_next-cdc->lheight < cdc->ymin) {
188                 /* have not reached the drawable area so don't break */
189                 cdc->xy[1]= y_next;
190
191                 /* adjust selection even if not drawing */
192                 if(cdc->sel[0] != cdc->sel[1]) {
193                         STEP_SEL(-(str_len + 1));
194                 }
195
196                 return 1;
197         }
198
199         if(str_len > cdc->console_width) { /* wrap? */
200                 const int initial_offset= ((tot_lines-1) * cdc->console_width);
201                 char *line_stride= str + initial_offset;        /* advance to the last line and draw it first */
202                 char eol;                                                                                                       /* baclup the end of wrapping */
203                 
204                 int sel_orig[2];
205                 VECCOPY2D(sel_orig, cdc->sel);
206
207                 /* invert and swap for wrapping */
208                 cdc->sel[0] = str_len - sel_orig[1];
209                 cdc->sel[1] = str_len - sel_orig[0];
210                 
211                 if(bg) {
212                         glColor3ubv(bg);
213                         glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, (cdc->xy[1]+(cdc->lheight*tot_lines))+rct_ofs);
214                 }
215
216                 glColor3ubv(fg);
217
218                 /* last part needs no clipping */
219                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
220                 BLF_draw(mono, line_stride);
221
222                 if(cdc->sel[0] != cdc->sel[1]) {
223                         STEP_SEL(-initial_offset);
224                         // glColor4ub(255, 0, 0, 96); // debug
225                         console_draw_sel(cdc->sel, cdc->xy, str_len % cdc->console_width, cdc->cwidth, cdc->lheight);
226                         STEP_SEL(cdc->console_width);
227                         glColor3ubv(fg);
228                 }
229
230                 cdc->xy[1] += cdc->lheight;
231
232                 line_stride -= cdc->console_width;
233                 
234                 for(; line_stride >= str; line_stride -= cdc->console_width) {
235                         eol = line_stride[cdc->console_width];
236                         line_stride[cdc->console_width]= '\0';
237                         
238                         BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
239                         BLF_draw(mono, line_stride);
240                         
241                         if(cdc->sel[0] != cdc->sel[1]) {
242                                 // glColor4ub(0, 255, 0, 96); // debug
243                                 console_draw_sel(cdc->sel, cdc->xy, cdc->console_width, cdc->cwidth, cdc->lheight);
244                                 STEP_SEL(cdc->console_width);
245                                 glColor3ubv(fg);
246                         }
247
248                         cdc->xy[1] += cdc->lheight;
249
250                         line_stride[cdc->console_width] = eol; /* restore */
251                         
252                         /* check if were out of view bounds */
253                         if(cdc->xy[1] > cdc->ymax)
254                                 return 0;
255                 }
256
257                 VECCOPY2D(cdc->sel, sel_orig);
258                 STEP_SEL(-(str_len + 1));
259         }
260         else { /* simple, no wrap */
261
262                 if(bg) {
263                         glColor3ubv(bg);
264                         glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, cdc->xy[1]+cdc->lheight-rct_ofs);
265                 }
266
267                 glColor3ubv(fg);
268
269                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
270                 BLF_draw(mono, str);
271                 
272                 if(cdc->sel[0] != cdc->sel[1]) {
273                         int isel[2];
274
275                         isel[0]= str_len - cdc->sel[1];
276                         isel[1]= str_len - cdc->sel[0];
277
278                         // glColor4ub(255, 255, 0, 96); // debug
279                         console_draw_sel(isel, cdc->xy, str_len, cdc->cwidth, cdc->lheight);
280                         STEP_SEL(-(str_len + 1));
281                 }
282
283                 cdc->xy[1] += cdc->lheight;
284
285                 if(cdc->xy[1] > cdc->ymax)
286                         return 0;
287         }
288
289         return 1;
290 #undef STEP_SEL
291 }
292
293 void console_scrollback_prompt_begin(struct SpaceConsole *sc, ConsoleLine *cl_dummy)
294 {
295         /* fake the edit line being in the scroll buffer */
296         ConsoleLine *cl= sc->history.last;
297         cl_dummy->type= CONSOLE_LINE_INPUT;
298         cl_dummy->len= cl_dummy->len_alloc= strlen(sc->prompt) + cl->len;
299         cl_dummy->len_alloc= cl_dummy->len + 1;
300         cl_dummy->line= MEM_mallocN(cl_dummy->len_alloc, "cl_dummy");
301         memcpy(cl_dummy->line, sc->prompt, (cl_dummy->len_alloc - cl->len));
302         memcpy(cl_dummy->line + ((cl_dummy->len_alloc - cl->len)) - 1, cl->line, cl->len + 1);
303         BLI_addtail(&sc->scrollback, cl_dummy);
304 }
305 void console_scrollback_prompt_end(struct SpaceConsole *sc, ConsoleLine *cl_dummy) 
306 {
307         MEM_freeN(cl_dummy->line);
308         BLI_remlink(&sc->scrollback, cl_dummy);
309 }
310
311 #define CONSOLE_DRAW_MARGIN 4
312 #define CONSOLE_DRAW_SCROLL 16
313
314 static int console_text_main__internal(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int draw, int mval[2], void **mouse_pick, int *pos_pick)
315 {
316         View2D *v2d= &ar->v2d;
317
318         ConsoleLine *cl= sc->history.last;
319         ConsoleDrawContext cdc;
320         
321         int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
322         int xy[2], y_prev;
323         int cwidth;
324         int console_width; /* number of characters that fit into the width of the console (fixed width) */
325         int sel[2]= {-1, -1}; /* defaults disabled */
326         unsigned char fg[3];
327
328         console_font_begin(sc);
329         cwidth = BLF_fixed_width(mono);
330         
331         console_width= (ar->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) )/cwidth;
332         if (console_width < 8) console_width= 8;
333         
334         xy[0]= x_orig; xy[1]= y_orig;
335         
336         if(mval[1] != INT_MAX)
337                 mval[1] += (v2d->cur.ymin + CONSOLE_DRAW_MARGIN);
338
339         if(pos_pick)
340                 *pos_pick = 0;
341
342         /* constants for the sequencer context */
343         cdc.cwidth= cwidth;
344         cdc.lheight= sc->lheight;
345         cdc.console_width= console_width;
346         cdc.winx= ar->winx-(CONSOLE_DRAW_MARGIN+CONSOLE_DRAW_SCROLL);
347         cdc.ymin= v2d->cur.ymin;
348         cdc.ymax= v2d->cur.ymax;
349         cdc.xy= xy;
350         cdc.sel= sel;
351         cdc.pos_pick= pos_pick;
352         cdc.mval= mval;
353         cdc.draw= draw;
354
355         if(sc->type==CONSOLE_TYPE_PYTHON) {
356                 ConsoleLine cl_dummy= {0};
357                 int prompt_len= strlen(sc->prompt);
358                 
359                 if(sc->sel_start != sc->sel_end) {
360                         sel[0]= sc->sel_start;
361                         sel[1]= sc->sel_end;
362                 }
363                 
364                 /* text */
365                 if(draw) {
366                         /* cursor */
367                         UI_GetThemeColor3ubv(TH_CONSOLE_CURSOR, (char *)fg);
368                         glColor3ubv(fg);
369                         glRecti(xy[0]+(cwidth*(cl->cursor+prompt_len)) -1, xy[1]-2, xy[0]+(cwidth*(cl->cursor+prompt_len)) +1, xy[1]+sc->lheight-2);
370
371                         xy[0]= x_orig; /* remove prompt offset */
372                 }
373
374                 console_scrollback_prompt_begin(sc, &cl_dummy);
375
376                 for(cl= sc->scrollback.last; cl; cl= cl->prev) {
377                         y_prev= xy[1];
378
379                         if(draw)
380                                 console_line_color(fg, cl->type);
381
382                         if(!console_draw_string(&cdc, cl->line, cl->len, fg, NULL)) {
383                                 /* when drawing, if we pass v2d->cur.ymax, then quit */
384                                 if(draw) {
385                                         break; /* past the y limits */
386                                 }
387                         }
388
389                         if((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
390                                 *mouse_pick= (void *)cl;
391                                 break;
392                         }
393                 }
394
395                 console_scrollback_prompt_end(sc, &cl_dummy);
396         }
397         else { 
398                 Report *report;
399                 int report_mask= 0;
400                 int bool= 0;
401                 unsigned char bg[3];
402
403                 if(draw) {
404                         glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
405                         glClear(GL_COLOR_BUFFER_BIT);
406                 }
407
408                 /* convert our display toggles into a flag compatible with BKE_report flags */
409                 report_mask= console_report_mask(sc);
410                 
411                 for(report=reports->list.last; report; report=report->prev) {
412                         
413                         if(report->type & report_mask) {
414                                 y_prev= xy[1];
415
416                                 if(draw)
417                                         console_report_color(fg, bg, report, bool);
418
419                                 if(!console_draw_string(&cdc, report->message, report->len, fg, bg)) {
420                                         /* when drawing, if we pass v2d->cur.ymax, then quit */
421                                         if(draw) {
422                                                 break; /* past the y limits */
423                                         }
424                                 }
425                                 if((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
426                                         *mouse_pick= (void *)report;
427                                         break;
428                                 }
429
430                                 bool = !(bool);
431                         }
432                 }
433         }
434         xy[1] += sc->lheight*2;
435
436         
437         return xy[1]-y_orig;
438 }
439
440 void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
441 {
442         int mval[2] = {INT_MAX, INT_MAX};
443         console_text_main__internal(sc, ar, reports, 1,  mval, NULL, NULL);
444 }
445
446 int console_text_height(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
447 {
448         int mval[2] = {INT_MAX, INT_MAX};
449         return console_text_main__internal(sc, ar, reports, 0,  mval, NULL, NULL);
450 }
451
452 void *console_text_pick(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int mouse_y)
453 {
454         void *mouse_pick= NULL;
455         int mval[2];
456
457         mval[0]= 0;
458         mval[1]= mouse_y;
459
460         console_text_main__internal(sc, ar, reports, 0, mval, &mouse_pick, NULL);
461         return (void *)mouse_pick;
462 }
463
464 int console_char_pick(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int mval[2])
465 {
466         int pos_pick= 0;
467         void *mouse_pick= NULL;
468         int mval_clamp[2];
469
470         mval_clamp[0]= CLAMPIS(mval[0], CONSOLE_DRAW_MARGIN, ar->winx-(CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN));
471         mval_clamp[1]= CLAMPIS(mval[1], CONSOLE_DRAW_MARGIN, ar->winy-CONSOLE_DRAW_MARGIN);
472
473         console_text_main__internal(sc, ar, reports, 0, mval_clamp, &mouse_pick, &pos_pick);
474         return pos_pick;
475 }