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