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-2, 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,
110 int console_width, int lheight,
111 unsigned char *fg, unsigned char *bg,
114 int *x, int *y, int draw)
116 int rct_ofs= lheight/4;
117 int tot_lines = (str_len/console_width)+1; /* total number of lines for wrapping */
118 int y_next = (str_len > console_width) ? (*y)+lheight*tot_lines : (*y)+lheight;
120 /* just advance the height */
125 else if (y_next-lheight < ymin) {
126 /* have not reached the drawable area so don't break */
131 if(str_len > console_width) { /* wrap? */
132 char *line_stride= str + ((tot_lines-1) * console_width); /* advance to the last line and draw it first */
133 char eol; /* baclup the end of wrapping */
136 glColor3ub(bg[0], bg[1], bg[2]);
137 glRecti(0, *y-rct_ofs, winx, (*y+(lheight*tot_lines))+rct_ofs);
140 glColor3ub(fg[0], fg[1], fg[2]);
142 /* last part needs no clipping */
143 BLF_position(*x, *y, 0); (*y) += lheight;
144 BLF_draw(line_stride);
145 line_stride -= console_width;
147 for(; line_stride >= str; line_stride -= console_width) {
148 eol = line_stride[console_width];
149 line_stride[console_width]= '\0';
151 BLF_position(*x, *y, 0); (*y) += lheight;
152 BLF_draw(line_stride);
154 line_stride[console_width] = eol; /* restore */
156 /* check if were out of view bounds */
161 else { /* simple, no wrap */
164 glColor3ub(bg[0], bg[1], bg[2]);
165 glRecti(0, *y-rct_ofs, winx, *y+lheight-rct_ofs);
168 glColor3ub(fg[0], fg[1], fg[2]);
170 BLF_position(*x, *y, 0); (*y) += lheight;
180 #define CONSOLE_DRAW_MARGIN 4
181 #define CONSOLE_DRAW_SCROLL 16
183 static int console_text_main__internal(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int draw)
185 View2D *v2d= &ar->v2d;
187 ConsoleLine *cl= sc->history.last;
189 int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
192 int console_width; /* number of characters that fit into the width of the console (fixed width) */
195 console_font_begin(sc);
196 cwidth = BLF_fixed_width();
198 console_width= (ar->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) )/cwidth;
199 if (console_width < 8) console_width= 8;
201 x= x_orig; y= y_orig;
203 if(sc->type==CONSOLE_TYPE_PYTHON) {
208 prompt_len= strlen(sc->prompt);
209 console_line_color(fg, CONSOLE_LINE_INPUT);
210 glColor3ub(fg[0], fg[1], fg[2]);
214 BLF_position(x, y, 0); x += cwidth * prompt_len;
215 BLF_draw(sc->prompt);
217 BLF_position(x, y, 0);
221 console_line_color(fg, CONSOLE_LINE_ERROR); /* lazy */
222 glColor3ub(fg[0], fg[1], fg[2]);
223 glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
225 x= x_orig; /* remove prompt offset */
230 for(cl= sc->scrollback.last; cl; cl= cl->prev) {
233 console_line_color(fg, cl->type);
235 if(!console_draw_string( cl->line, cl->len,
236 console_width, sc->lheight,
238 ar->winx-CONSOLE_DRAW_MARGIN,
239 v2d->cur.ymin, v2d->cur.ymax,
242 /* when drawing, if we pass v2d->cur.ymax, then quit */
244 break; /* past the y limits */
253 unsigned char bg[3] = {114, 114, 114};
256 glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
257 glClear(GL_COLOR_BUFFER_BIT);
260 /* convert our display toggles into a flag compatible with BKE_report flags */
261 if(sc->rpt_mask & CONSOLE_RPT_DEBUG) report_mask |= RPT_DEBUG_ALL;
262 if(sc->rpt_mask & CONSOLE_RPT_INFO) report_mask |= RPT_INFO_ALL;
263 if(sc->rpt_mask & CONSOLE_RPT_OP) report_mask |= RPT_OPERATOR_ALL;
264 if(sc->rpt_mask & CONSOLE_RPT_WARN) report_mask |= RPT_WARNING_ALL;
265 if(sc->rpt_mask & CONSOLE_RPT_ERR) report_mask |= RPT_ERROR_ALL;
267 for(report=reports->list.last; report; report=report->prev) {
269 if(report->type & report_mask) {
272 console_report_color(fg, report->type);
274 if(!console_draw_string( report->message, report->len,
275 console_width, sc->lheight,
277 ar->winx-CONSOLE_DRAW_MARGIN,
278 v2d->cur.ymin, v2d->cur.ymax,
281 /* when drawing, if we pass v2d->cur.ymax, then quit */
283 break; /* past the y limits */
297 void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
299 console_text_main__internal(sc, ar, reports, 1);
302 int console_text_height(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
304 return console_text_main__internal(sc, ar, reports, 0);