fixes for errors on startup and compiler errors and draw speedup.
[blender-staging.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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
35 #include "MEM_guardedalloc.h"
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 #include "DNA_userdef_types.h"
44
45 #include "BKE_global.h"
46 #include "BKE_main.h"
47 // #include "BKE_suggestions.h"
48 #include "BKE_text.h"
49 #include "BKE_report.h"
50 #include "BKE_utildefines.h"
51
52 #include "BIF_gl.h"
53 #include "BIF_glutil.h"
54
55 #include "ED_datafiles.h"
56 #include "UI_interface.h"
57 #include "UI_resources.h"
58
59 //#include "console_intern.h"
60
61 static void console_font_begin(SpaceConsole *sc)
62 {
63         static int mono= -1; // XXX needs proper storage
64
65         if(mono == -1)
66                 mono= BLF_load_mem("monospace", (unsigned char*)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
67
68         BLF_set(mono);
69         BLF_aspect(1.0);
70
71         BLF_size(sc->lheight-2, 72);
72 }
73
74 static void console_line_color(unsigned char *fg, int type)
75 {
76         switch(type) {
77         case CONSOLE_LINE_OUTPUT:
78                 fg[0]=96; fg[1]=128; fg[2]=255;
79                 break;
80         case CONSOLE_LINE_INPUT:
81                 fg[0]=255; fg[1]=255; fg[2]=255;
82                 break;
83         case CONSOLE_LINE_INFO:
84                 fg[0]=0; fg[1]=170; fg[2]=0;
85                 break;
86         case CONSOLE_LINE_ERROR:
87                 fg[0]=220; fg[1]=96; fg[2]=96;
88                 break;
89         }
90 }
91
92 static void console_report_color(unsigned char *fg, int type)
93 {
94         /*
95         if              (type & RPT_ERROR_ALL)          { fg[0]=220; fg[1]=0; fg[2]=0; }
96         else if (type & RPT_WARNING_ALL)        { fg[0]=220; fg[1]=96; fg[2]=96; }
97         else if (type & RPT_OPERATOR_ALL)       { fg[0]=96; fg[1]=128; fg[2]=255; }
98         else if (type & RPT_INFO_ALL)           { fg[0]=0; fg[1]=170; fg[2]=0; }
99         else if (type & RPT_DEBUG_ALL)          { fg[0]=196; fg[1]=196; fg[2]=196; }
100         else                                                            { fg[0]=196; fg[1]=196; fg[2]=196; }
101         */
102
103         fg[0]=0; fg[1]=0; fg[2]=0;
104 }
105
106
107 /* return 0 if the last line is off the screen
108  * should be able to use this for any string type */
109 static int console_draw_string( char *str, int str_len,
110                                                                         int console_width, int lheight,
111                                                                         unsigned char *fg, unsigned char *bg,
112                                                                         int winx,
113                                                                         int ymin, int ymax,
114                                                                         int *x, int *y, int draw)
115 {       
116         int rct_ofs= lheight/4;
117         int tot_lines = (str_len/console_width)+1; /* total number of lines for wrapping */
118         int y_next = (str_len > console_width) ? (*y)+lheight*tot_lines : (*y)+lheight;
119
120         /* just advance the height */
121         if(draw==0) {
122                 *y= y_next;
123                 return 1;
124         }
125         else if (y_next-lheight < ymin) {
126                 /* have not reached the drawable area so don't break */
127                 *y= y_next;
128                 return 1;
129         }
130
131         if(str_len > console_width) { /* wrap? */
132                 char *line_stride= str + ((tot_lines-1) * console_width);       /* advance to the last line and draw it first */
133                 char eol;                                                                                                       /* baclup the end of wrapping */
134                 
135                 if(bg) {
136                         glColor3ub(bg[0], bg[1], bg[2]);
137                         glRecti(0, *y-rct_ofs, winx, (*y+(lheight*tot_lines))+rct_ofs);
138                 }
139
140                 glColor3ub(fg[0], fg[1], fg[2]);
141
142                 /* last part needs no clipping */
143                 BLF_position(*x, *y, 0); (*y) += lheight;
144                 BLF_draw(line_stride);
145                 line_stride -= console_width;
146                 
147                 for(; line_stride >= str; line_stride -= console_width) {
148                         eol = line_stride[console_width];
149                         line_stride[console_width]= '\0';
150                         
151                         BLF_position(*x, *y, 0); (*y) += lheight;
152                         BLF_draw(line_stride);
153                         
154                         line_stride[console_width] = eol; /* restore */
155                         
156                         /* check if were out of view bounds */
157                         if(*y > ymax)
158                                 return 0;
159                 }
160         }
161         else { /* simple, no wrap */
162
163                 if(bg) {
164                         glColor3ub(bg[0], bg[1], bg[2]);
165                         glRecti(0, *y-rct_ofs, winx, *y+lheight-rct_ofs);
166                 }
167
168                 glColor3ub(fg[0], fg[1], fg[2]);
169
170                 BLF_position(*x, *y, 0); (*y) += lheight;
171                 BLF_draw(str);
172                 
173                 if(*y > ymax)
174                         return 0;
175         }
176
177         return 1;
178 }
179
180 #define CONSOLE_DRAW_MARGIN 4
181 #define CONSOLE_DRAW_SCROLL 16
182
183 static int console_text_main__internal(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int draw)
184 {
185         View2D *v2d= &ar->v2d;
186
187         ConsoleLine *cl= sc->history.last;
188         
189         int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
190         int x,y;
191         int cwidth;
192         int console_width; /* number of characters that fit into the width of the console (fixed width) */
193         unsigned char fg[3];
194         
195         console_font_begin(sc);
196         cwidth = BLF_fixed_width();
197         
198         console_width= (ar->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) )/cwidth;
199         if (console_width < 8) console_width= 8;
200         
201         x= x_orig; y= y_orig;
202         
203         if(sc->type==CONSOLE_TYPE_PYTHON) {
204                 int prompt_len;
205                 
206                 /* text */
207                 if(draw) {
208                         prompt_len= strlen(sc->prompt);
209                         console_line_color(fg, CONSOLE_LINE_INPUT);
210                         glColor3ub(fg[0], fg[1], fg[2]);
211
212                         /* command line */
213                         if(prompt_len) {
214                                 BLF_position(x, y, 0); x += cwidth * prompt_len;
215                                 BLF_draw(sc->prompt);
216                         }
217                         BLF_position(x, y, 0);
218                         BLF_draw(cl->line);
219
220                         /* cursor */
221                         console_line_color(fg, CONSOLE_LINE_ERROR); /* lazy */
222                         glColor3ub(fg[0], fg[1], fg[2]);
223                         glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
224
225                         x= x_orig; /* remove prompt offset */
226                 }
227                 
228                 y += sc->lheight;
229                 
230                 for(cl= sc->scrollback.last; cl; cl= cl->prev) {
231
232                         if(draw)
233                                 console_line_color(fg, cl->type);
234
235                         if(!console_draw_string(        cl->line, cl->len,
236                                                                                 console_width, sc->lheight,
237                                                                                 fg, NULL,
238                                                                                 ar->winx-CONSOLE_DRAW_MARGIN,
239                                                                                 v2d->cur.ymin, v2d->cur.ymax,
240                                                                                 &x, &y, draw))
241                         {
242                                 /* when drawing, if we pass v2d->cur.ymax, then quit */
243                                 if(draw) {
244                                         break; /* past the y limits */
245                                 }
246                         }
247                 }
248         }
249         else { 
250                 Report *report;
251                 int report_mask= 0;
252                 int bool= 0;
253                 unsigned char bg[3] = {114, 114, 114};
254                 
255                 if(draw) {
256                         glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
257                         glClear(GL_COLOR_BUFFER_BIT);
258                 }
259
260                 /* convert our display toggles into a flag compatible with BKE_report flags */
261                 if(sc->rpt_mask & CONSOLE_RPT_DEBUG)    report_mask |= RPT_DEBUG_ALL;
262                 if(sc->rpt_mask & CONSOLE_RPT_INFO)             report_mask |= RPT_INFO_ALL;
263                 if(sc->rpt_mask & CONSOLE_RPT_OP)               report_mask |= RPT_OPERATOR_ALL;
264                 if(sc->rpt_mask & CONSOLE_RPT_WARN)             report_mask |= RPT_WARNING_ALL;
265                 if(sc->rpt_mask & CONSOLE_RPT_ERR)              report_mask |= RPT_ERROR_ALL;
266                 
267                 for(report=reports->list.last; report; report=report->prev) {
268                         
269                         if(report->type & report_mask) {
270
271                                 if(draw)
272                                         console_report_color(fg, report->type);
273
274                                 if(!console_draw_string(        report->message, report->len,
275                                                                                         console_width, sc->lheight,
276                                                                                         fg, bool?bg:NULL,
277                                                                                         ar->winx-CONSOLE_DRAW_MARGIN,
278                                                                                         v2d->cur.ymin, v2d->cur.ymax,
279                                                                                         &x, &y, draw))
280                                 {
281                                         /* when drawing, if we pass v2d->cur.ymax, then quit */
282                                         if(draw) {
283                                                 break; /* past the y limits */
284                                         }
285                                 }
286
287                                 bool = !(bool);
288                         }
289                 }
290         }
291         y += sc->lheight*2;
292
293         
294         return y-y_orig;
295 }
296
297 void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
298 {
299         console_text_main__internal(sc, ar, reports, 1);
300 }
301
302 int console_text_height(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
303 {
304         return console_text_main__internal(sc, ar, reports, 0);
305 }