- Scrollbars for the console (use View2D functions)
[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, int winy,
113                                                                         int *x, int *y, int draw)
114 {       
115         int rct_ofs= lheight/4;
116         int tot_lines = (str_len/console_width)+1;                                                      /* total number of lines for wrapping */
117
118         /* just advance the height */
119         if(draw==0) {
120                 if(str_len > console_width)     (*y) += tot_lines * lheight;
121                 else                                            (*y) += lheight;
122
123                 return 1;
124         }
125
126         if(str_len > console_width) { /* wrap? */
127                 char *line_stride= str + ((tot_lines-1) * console_width);       /* advance to the last line and draw it first */
128                 char eol;                                                                                                                       /* baclup the end of wrapping */
129                 
130                 if(bg) {
131                         glColor3ub(bg[0], bg[1], bg[2]);
132                         glRecti(0, *y-rct_ofs, winx, (*y+(lheight*tot_lines))+rct_ofs);
133                 }
134
135                 glColor3ub(fg[0], fg[1], fg[2]);
136
137                 /* last part needs no clipping */
138                 BLF_position(*x, *y, 0); (*y) += lheight;
139                 BLF_draw(line_stride);
140                 line_stride -= console_width;
141                 
142                 for(; line_stride >= str; line_stride -= console_width) {
143                         eol = line_stride[console_width];
144                         line_stride[console_width]= '\0';
145                         
146                         BLF_position(*x, *y, 0); (*y) += lheight;
147                         BLF_draw(line_stride);
148                         
149                         line_stride[console_width] = eol; /* restore */
150                         
151                         /* check if were out of view bounds */
152                         if(*y > winy)
153                                 return 0;
154                 }
155         }
156         else { /* simple, no wrap */
157
158                 if(bg) {
159                         glColor3ub(bg[0], bg[1], bg[2]);
160                         glRecti(0, *y-rct_ofs, winx, *y+lheight-rct_ofs);
161                 }
162
163                 glColor3ub(fg[0], fg[1], fg[2]);
164
165                 BLF_position(*x, *y, 0); (*y) += lheight;
166                 BLF_draw(str);
167                 
168                 if(*y > winy)
169                         return 0;
170         }
171
172         return 1;
173 }
174
175 #define CONSOLE_DRAW_MARGIN 4
176 #define CONSOLE_DRAW_SCROLL 16
177
178 static int console_text_main__internal(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int draw)
179 {
180         View2D *v2d= &ar->v2d;
181
182         ConsoleLine *cl= sc->history.last;
183         
184         int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
185         int x,y;
186         int cwidth;
187         int console_width; /* number of characters that fit into the width of the console (fixed width) */
188         unsigned char fg[3];
189         
190         console_font_begin(sc);
191         cwidth = BLF_fixed_width();
192         
193         console_width= (ar->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) )/cwidth;
194         if (console_width < 8) console_width= 8;
195         
196         x= x_orig; y= y_orig;
197         
198         if(sc->type==CONSOLE_TYPE_PYTHON) {
199                 int prompt_len;
200                 
201                 /* text */
202                 if(draw) {
203                         prompt_len= strlen(sc->prompt);
204                         console_line_color(fg, CONSOLE_LINE_INPUT);
205                         glColor3ub(fg[0], fg[1], fg[2]);
206
207                         /* command line */
208                         if(prompt_len) {
209                                 BLF_position(x, y, 0); x += cwidth * prompt_len;
210                                 BLF_draw(sc->prompt);
211                         }
212                         BLF_position(x, y, 0);
213                         BLF_draw(cl->line);
214
215                         /* cursor */
216                         console_line_color(fg, CONSOLE_LINE_ERROR); /* lazy */
217                         glColor3ub(fg[0], fg[1], fg[2]);
218                         glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
219
220                         x= x_orig; /* remove prompt offset */
221                 }
222                 
223                 y += sc->lheight;
224                 
225                 for(cl= sc->scrollback.last; cl; cl= cl->prev) {
226
227                         if(draw)
228                                 console_line_color(fg, cl->type);
229
230                         if(!console_draw_string(        cl->line, cl->len,
231                                                                                 console_width, sc->lheight,
232                                                                                 fg, NULL,
233                                                                                 ar->winx-CONSOLE_DRAW_MARGIN, v2d->cur.ymax,
234                                                                                 &x, &y, draw))
235                         {
236                                 /* when drawing, if we pass v2d->cur.ymax, then quit */
237                                 if(draw) {
238                                         break; /* past the y limits */
239                                 }
240                         }
241                 }
242         }
243         else { 
244                 Report *report;
245                 int report_mask= 0;
246                 int bool= 0;
247                 unsigned char bg[3] = {114, 114, 114};
248                 
249                 if(draw) {
250                         glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
251                         glClear(GL_COLOR_BUFFER_BIT);
252                 }
253
254                 /* convert our display toggles into a flag compatible with BKE_report flags */
255                 if(sc->rpt_mask & CONSOLE_RPT_DEBUG)    report_mask |= RPT_DEBUG_ALL;
256                 if(sc->rpt_mask & CONSOLE_RPT_INFO)             report_mask |= RPT_INFO_ALL;
257                 if(sc->rpt_mask & CONSOLE_RPT_OP)               report_mask |= RPT_OPERATOR_ALL;
258                 if(sc->rpt_mask & CONSOLE_RPT_WARN)             report_mask |= RPT_WARNING_ALL;
259                 if(sc->rpt_mask & CONSOLE_RPT_ERR)              report_mask |= RPT_ERROR_ALL;
260                 
261                 for(report=reports->list.last; report; report=report->prev) {
262                         
263                         if(report->type & report_mask) {
264
265                                 if(draw)
266                                         console_report_color(fg, report->type);
267
268                                 if(!console_draw_string(        report->message, report->len,
269                                                                                         console_width, sc->lheight,
270                                                                                         fg, bool?bg:NULL,
271                                                                                         ar->winx-CONSOLE_DRAW_MARGIN, v2d->cur.ymax,
272                                                                                         &x, &y, draw))
273                                 {
274                                         /* when drawing, if we pass v2d->cur.ymax, then quit */
275                                         if(draw) {
276                                                 break; /* past the y limits */
277                                         }
278                                 }
279
280                                 bool = !(bool);
281                         }
282                         
283                 }
284         }
285         y += sc->lheight*2;
286
287         
288         return y-y_orig;
289 }
290
291 void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
292 {
293         console_text_main__internal(sc, ar, reports, 1);
294 }
295
296 int console_text_height(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
297 {
298         return console_text_main__internal(sc, ar, reports, 0);
299 }