4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): Campbell Barton
27 * ***** END GPL LICENSE BLOCK *****
35 #include "MEM_guardedalloc.h"
39 #include "BLI_blenlib.h"
41 #include "DNA_space_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_userdef_types.h"
45 #include "BKE_global.h"
47 // #include "BKE_suggestions.h"
49 #include "BKE_report.h"
50 #include "BKE_utildefines.h"
53 #include "BIF_glutil.h"
55 #include "ED_datafiles.h"
56 #include "UI_interface.h"
57 #include "UI_resources.h"
59 //#include "console_intern.h"
61 static void console_font_begin(SpaceConsole *sc)
63 static int mono= -1; // XXX needs proper storage
66 mono= BLF_load_mem("monospace", (unsigned char*)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
71 BLF_size(sc->lheight, 72);
74 static void console_line_color(unsigned char *fg, int type)
77 case CONSOLE_LINE_OUTPUT:
78 fg[0]=96; fg[1]=128; fg[2]=255;
80 case CONSOLE_LINE_INPUT:
81 fg[0]=255; fg[1]=255; fg[2]=255;
83 case CONSOLE_LINE_INFO:
84 fg[0]=0; fg[1]=170; fg[2]=0;
86 case CONSOLE_LINE_ERROR:
87 fg[0]=220; fg[1]=96; fg[2]=96;
92 static void console_report_color(unsigned char *fg, int type)
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; }
103 fg[0]=0; fg[1]=0; fg[2]=0;
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)
111 int rct_ofs= lheight/4;
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 */
119 glColor3ub(bg[0], bg[1], bg[2]);
120 glRecti(0, *y-rct_ofs, winx, (*y+(lheight*tot_lines))+rct_ofs);
123 glColor3ub(fg[0], fg[1], fg[2]);
125 /* last part needs no clipping */
126 BLF_position(*x, *y, 0); (*y) += lheight;
127 BLF_draw(line_stride);
128 line_stride -= console_width;
130 for(; line_stride >= str; line_stride -= console_width) {
131 eol = line_stride[console_width];
132 line_stride[console_width]= '\0';
134 BLF_position(*x, *y, 0); (*y) += lheight;
135 BLF_draw(line_stride);
137 line_stride[console_width] = eol; /* restore */
139 /* check if were out of view bounds */
144 else { /* simple, no wrap */
147 glColor3ub(bg[0], bg[1], bg[2]);
148 glRecti(0, *y-rct_ofs, winx, *y+lheight-rct_ofs);
151 glColor3ub(fg[0], fg[1], fg[2]);
153 BLF_position(*x, *y, 0); (*y) += lheight;
163 #define CONSOLE_DRAW_MARGIN 8
164 #define CONSOLE_LINE_MARGIN 6
166 void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
168 ConsoleLine *cl= sc->history.last;
170 int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
173 int console_width; /* number of characters that fit into the width of the console (fixed width) */
176 console_font_begin(sc);
177 cwidth = BLF_fixed_width();
179 console_width= (ar->winx - CONSOLE_DRAW_MARGIN*2)/cwidth;
180 if (console_width < 8) console_width= 8;
182 x= x_orig; y= y_orig;
184 if(sc->type==CONSOLE_TYPE_PYTHON) {
185 int prompt_len= strlen(sc->prompt);
188 console_line_color(fg, CONSOLE_LINE_INPUT);
189 glColor3ub(fg[0], fg[1], fg[2]);
193 BLF_position(x, y, 0); x += cwidth * prompt_len;
194 BLF_draw(sc->prompt);
196 BLF_position(x, y, 0);
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);
204 x= x_orig; /* remove prompt offset */
208 for(cl= sc->scrollback.last; cl; cl= cl->prev) {
209 console_line_color(fg, cl->type);
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 */
220 unsigned char bg[3] = {114, 114, 114};
222 glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
223 glClear(GL_COLOR_BUFFER_BIT);
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;
232 for(report=reports->list.last; report; report=report->prev) {
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 */
239 y+=CONSOLE_LINE_MARGIN;