SVN maintenance.
[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, 72);
72 }
73
74 static void console_line_color(int type)
75 {
76         switch(type){
77         case CONSOLE_LINE_OUTPUT:
78                 glColor4ub(96, 128, 255, 255);
79                 break;
80         case CONSOLE_LINE_INPUT:
81                 glColor4ub(255, 255, 255, 255);
82                 break;
83         case CONSOLE_LINE_INFO:
84                 glColor4ub(0, 170, 0, 255);
85                 break;
86         case CONSOLE_LINE_ERROR:
87                 glColor4ub(220, 96, 96, 255);
88                 break;
89         }
90 }
91
92 static void console_report_color(int type)
93 {
94         if(type & RPT_ERROR_ALL)        return glColor4ub(220, 0, 0, 255);
95         if(type & RPT_WARNING_ALL)      return glColor4ub(220, 96, 96, 255);
96         if(type & RPT_OPERATOR_ALL)     return glColor4ub(96, 128, 255, 255);
97         if(type & RPT_INFO_ALL)         return glColor4ub(0, 170, 0, 255); 
98         if(type & RPT_DEBUG_ALL)        return glColor4ub(196, 196, 196, 255);
99         return glColor4ub(196, 196, 196, 255); /* unknown */
100 }
101
102
103 /* return 0 if the last line is off the screen
104  * should be able to use this for any string type */
105 static int console_draw_string(char *str, int str_len, int console_width, int lheight, int ymax, int *x, int *y)
106 {       
107         if(str_len > console_width) { /* wrap? */
108                 int tot_lines = (str_len/console_width)+1;                                                      /* total number of lines for wrapping */
109                 char *line_stride= str + ((tot_lines-1) * console_width);       /* advance to the last line and draw it first */
110                 char eol;                                                                                                                       /* baclup the end of wrapping */
111                 
112                 /* last part needs no clipping */
113                 BLF_position(*x, *y, 0); (*y) += lheight;
114                 BLF_draw(line_stride);
115                 line_stride -= console_width;
116                 
117                 for(; line_stride >= str; line_stride -= console_width) {
118                         eol = line_stride[console_width];
119                         line_stride[console_width]= '\0';
120                         
121                         BLF_position(*x, *y, 0); (*y) += lheight;
122                         BLF_draw(line_stride);
123                         
124                         line_stride[console_width] = eol; /* restore */
125                         
126                         /* check if were out of view bounds */
127                         if(*y > ymax)
128                                 return 0;
129                 }
130         }
131         else { /* simple, no wrap */
132                 BLF_position(*x, *y, 0); (*y) += lheight;
133                 BLF_draw(str);
134                 
135                 if(*y > ymax)
136                         return 0;
137         }
138
139         return 1;
140 }
141
142 #define CONSOLE_DRAW_MARGIN 8
143
144 void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
145 {
146         ConsoleLine *cl= sc->history.last;
147         
148         int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
149         int x,y;
150         int cwidth;
151         int console_width; /* number of characters that fit into the width of the console (fixed width) */
152         
153         
154         console_font_begin(sc);
155         cwidth = BLF_fixed_width();
156         
157         console_width= (ar->winx - CONSOLE_DRAW_MARGIN*2)/cwidth;
158         if (console_width < 8) console_width= 8;
159         
160         x= x_orig; y= y_orig;
161         
162         if(sc->type==CONSOLE_TYPE_PYTHON) {
163                 int prompt_len= strlen(sc->prompt);
164                 
165                 /* text */
166                 console_line_color(CONSOLE_LINE_INPUT);
167                 
168                 /* command line */
169                 if(prompt_len) {
170                         BLF_position(x, y, 0); x += cwidth * prompt_len;
171                         BLF_draw(sc->prompt);   
172                 }
173                 BLF_position(x, y, 0);
174                 BLF_draw(cl->line);     
175                 
176                 /* cursor */
177                 console_line_color(CONSOLE_LINE_ERROR); /* lazy */
178                 glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
179                 
180                 x= x_orig; /* remove prompt offset */
181                 
182                 y += sc->lheight;
183                 
184                 for(cl= sc->scrollback.last; cl; cl= cl->prev) {
185                         console_line_color(cl->type);
186
187                         if(!console_draw_string(cl->line, cl->len, console_width, sc->lheight, ar->winy + sc->lheight, &x, &y))
188                                 break; /* past the y limits */
189                         
190                 }
191         }
192         else { 
193                 Report *report;
194                 int report_mask= 0;
195                 
196                 /* convert our display toggles into a flag compatible with BKE_report flags */
197                 if(sc->rpt_mask & CONSOLE_RPT_DEBUG)    report_mask |= RPT_DEBUG_ALL;
198                 if(sc->rpt_mask & CONSOLE_RPT_INFO)     report_mask |= RPT_INFO_ALL;
199                 if(sc->rpt_mask & CONSOLE_RPT_OP)               report_mask |= RPT_OPERATOR_ALL;
200                 if(sc->rpt_mask & CONSOLE_RPT_WARN)     report_mask |= RPT_WARNING_ALL;
201                 if(sc->rpt_mask & CONSOLE_RPT_ERR)              report_mask |= RPT_ERROR_ALL;
202                 
203                 for(report=reports->list.last; report; report=report->prev) {
204                         
205                         if(report->type & report_mask) {
206                                 console_report_color(report->type);
207                                 if(!console_draw_string(report->message, strlen(report->message), console_width, sc->lheight, ar->winy + sc->lheight, &x, &y))
208                                         break; /* past the y limits */
209                         }
210                         
211                 }
212         }
213         
214 }