0a725d0c69ffe1525492039e747abf9571b216c6
[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(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, int console_width, int lheight, unsigned char *fg, unsigned char *bg, int winx, int winy, int *x, int *y)
110 {       
111         int rct_ofs= lheight/4;
112
113         if(str_len > console_width) { /* wrap? */
114                 int tot_lines = (str_len/console_width)+1;                                                      /* total number of lines for wrapping */
115                 char *line_stride= str + ((tot_lines-1) * console_width);       /* advance to the last line and draw it first */
116                 char eol;                                                                                                                       /* baclup the end of wrapping */
117                 
118                 if(bg) {
119                         glColor3ub(bg[0], bg[1], bg[2]);
120                         glRecti(0, *y-rct_ofs, winx, (*y+(lheight*tot_lines))+rct_ofs);
121                 }
122
123                 glColor3ub(fg[0], fg[1], fg[2]);
124
125                 /* last part needs no clipping */
126                 BLF_position(*x, *y, 0); (*y) += lheight;
127                 BLF_draw(line_stride);
128                 line_stride -= console_width;
129                 
130                 for(; line_stride >= str; line_stride -= console_width) {
131                         eol = line_stride[console_width];
132                         line_stride[console_width]= '\0';
133                         
134                         BLF_position(*x, *y, 0); (*y) += lheight;
135                         BLF_draw(line_stride);
136                         
137                         line_stride[console_width] = eol; /* restore */
138                         
139                         /* check if were out of view bounds */
140                         if(*y > winy)
141                                 return 0;
142                 }
143         }
144         else { /* simple, no wrap */
145
146                 if(bg) {
147                         glColor3ub(bg[0], bg[1], bg[2]);
148                         glRecti(0, *y-rct_ofs, winx, *y+lheight-rct_ofs);
149                 }
150
151                 glColor3ub(fg[0], fg[1], fg[2]);
152
153                 BLF_position(*x, *y, 0); (*y) += lheight;
154                 BLF_draw(str);
155                 
156                 if(*y > winy)
157                         return 0;
158         }
159
160         return 1;
161 }
162
163 #define CONSOLE_DRAW_MARGIN 8
164 #define CONSOLE_LINE_MARGIN 6
165
166 void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
167 {
168         ConsoleLine *cl= sc->history.last;
169         
170         int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
171         int x,y;
172         int cwidth;
173         int console_width; /* number of characters that fit into the width of the console (fixed width) */
174         unsigned char fg[3];
175         
176         console_font_begin(sc);
177         cwidth = BLF_fixed_width();
178         
179         console_width= (ar->winx - CONSOLE_DRAW_MARGIN*2)/cwidth;
180         if (console_width < 8) console_width= 8;
181         
182         x= x_orig; y= y_orig;
183         
184         if(sc->type==CONSOLE_TYPE_PYTHON) {
185                 int prompt_len= strlen(sc->prompt);
186                 
187                 /* text */
188                 console_line_color(fg, CONSOLE_LINE_INPUT);
189                 glColor3ub(fg[0], fg[1], fg[2]);
190                 
191                 /* command line */
192                 if(prompt_len) {
193                         BLF_position(x, y, 0); x += cwidth * prompt_len;
194                         BLF_draw(sc->prompt);   
195                 }
196                 BLF_position(x, y, 0);
197                 BLF_draw(cl->line);     
198                 
199                 /* cursor */
200                 console_line_color(fg, CONSOLE_LINE_ERROR); /* lazy */
201                 glColor3ub(fg[0], fg[1], fg[2]);
202                 glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
203                 
204                 x= x_orig; /* remove prompt offset */
205                 
206                 y += sc->lheight;
207                 
208                 for(cl= sc->scrollback.last; cl; cl= cl->prev) {
209                         console_line_color(fg, cl->type);
210
211                         if(!console_draw_string(cl->line, cl->len, console_width, sc->lheight+CONSOLE_LINE_MARGIN, fg, NULL, ar->winx, ar->winy, &x, &y))
212                                 break; /* past the y limits */
213                         
214                 }
215         }
216         else { 
217                 Report *report;
218                 int report_mask= 0;
219                 int bool= 0;
220                 unsigned char bg[3] = {114, 114, 114};
221                 
222                 glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
223                 glClear(GL_COLOR_BUFFER_BIT);
224
225                 /* convert our display toggles into a flag compatible with BKE_report flags */
226                 if(sc->rpt_mask & CONSOLE_RPT_DEBUG)    report_mask |= RPT_DEBUG_ALL;
227                 if(sc->rpt_mask & CONSOLE_RPT_INFO)             report_mask |= RPT_INFO_ALL;
228                 if(sc->rpt_mask & CONSOLE_RPT_OP)               report_mask |= RPT_OPERATOR_ALL;
229                 if(sc->rpt_mask & CONSOLE_RPT_WARN)             report_mask |= RPT_WARNING_ALL;
230                 if(sc->rpt_mask & CONSOLE_RPT_ERR)              report_mask |= RPT_ERROR_ALL;
231                 
232                 for(report=reports->list.last; report; report=report->prev) {
233                         
234                         if(report->type & report_mask) {
235                                 console_report_color(fg, report->type);
236                                 if(!console_draw_string(report->message, strlen(report->message), console_width, sc->lheight+CONSOLE_LINE_MARGIN, fg, bool?bg:NULL, ar->winx, ar->winy, &x, &y))
237                                         break; /* past the y limits */
238
239                                 y+=CONSOLE_LINE_MARGIN;
240                                 bool = !(bool);
241                         }
242                         
243                 }
244         }
245         
246 }