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 *****
36 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
42 #include "DNA_space_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_userdef_types.h"
46 #include "BKE_global.h"
48 // #include "BKE_suggestions.h"
50 #include "BKE_report.h"
51 #include "BKE_utildefines.h"
54 #include "BIF_glutil.h"
56 #include "ED_datafiles.h"
59 #include "UI_interface.h"
60 #include "UI_resources.h"
62 #include "console_intern.h"
64 static void console_font_begin(SpaceConsole *sc)
66 static int mono= -1; // XXX needs proper storage
69 mono= BLF_load_mem("monospace", (unsigned char*)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
74 BLF_size(sc->lheight-2, 72);
77 static void console_line_color(unsigned char *fg, int type)
80 case CONSOLE_LINE_OUTPUT:
81 fg[0]=96; fg[1]=128; fg[2]=255;
83 case CONSOLE_LINE_INPUT:
84 fg[0]=255; fg[1]=255; fg[2]=255;
86 case CONSOLE_LINE_INFO:
87 fg[0]=0; fg[1]=170; fg[2]=0;
89 case CONSOLE_LINE_ERROR:
90 fg[0]=220; fg[1]=96; fg[2]=96;
95 static void console_report_color(unsigned char *fg, unsigned char *bg, Report *report, int bool)
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; }
105 if(report->flag & SELECT) {
106 fg[0]=255; fg[1]=255; fg[2]=255;
108 bg[0]=96; bg[1]=128; bg[2]=255;
111 bg[0]=90; bg[1]=122; bg[2]=249;
116 fg[0]=0; fg[1]=0; fg[2]=0;
119 bg[0]=120; bg[1]=120; bg[2]=120;
122 bg[0]=114; bg[1]=114; bg[2]=114;
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,
139 int *x, int *y, int draw)
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;
145 /* just advance the height */
150 else if (y_next-lheight < ymin) {
151 /* have not reached the drawable area so don't break */
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 */
161 glColor3ub(bg[0], bg[1], bg[2]);
162 glRecti(0, *y-rct_ofs, winx, (*y+(lheight*tot_lines))+rct_ofs);
165 glColor3ub(fg[0], fg[1], fg[2]);
167 /* last part needs no clipping */
168 BLF_position(*x, *y, 0); (*y) += lheight;
169 BLF_draw(line_stride);
170 line_stride -= console_width;
172 for(; line_stride >= str; line_stride -= console_width) {
173 eol = line_stride[console_width];
174 line_stride[console_width]= '\0';
176 BLF_position(*x, *y, 0); (*y) += lheight;
177 BLF_draw(line_stride);
179 line_stride[console_width] = eol; /* restore */
181 /* check if were out of view bounds */
186 else { /* simple, no wrap */
189 glColor3ub(bg[0], bg[1], bg[2]);
190 glRecti(0, *y-rct_ofs, winx, *y+lheight-rct_ofs);
193 glColor3ub(fg[0], fg[1], fg[2]);
195 BLF_position(*x, *y, 0); (*y) += lheight;
205 #define CONSOLE_DRAW_MARGIN 4
206 #define CONSOLE_DRAW_SCROLL 16
208 static int console_text_main__internal(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int draw, int mouse_y, void **mouse_pick)
210 View2D *v2d= &ar->v2d;
212 ConsoleLine *cl= sc->history.last;
214 int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
217 int console_width; /* number of characters that fit into the width of the console (fixed width) */
220 console_font_begin(sc);
221 cwidth = BLF_fixed_width();
223 console_width= (ar->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) )/cwidth;
224 if (console_width < 8) console_width= 8;
226 x= x_orig; y= y_orig;
228 if(mouse_y != INT_MAX)
229 mouse_y += (v2d->cur.ymin+CONSOLE_DRAW_MARGIN);
232 if(sc->type==CONSOLE_TYPE_PYTHON) {
237 prompt_len= strlen(sc->prompt);
238 console_line_color(fg, CONSOLE_LINE_INPUT);
239 glColor3ub(fg[0], fg[1], fg[2]);
243 BLF_position(x, y, 0); x += cwidth * prompt_len;
244 BLF_draw(sc->prompt);
246 BLF_position(x, y, 0);
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);
254 x= x_orig; /* remove prompt offset */
259 for(cl= sc->scrollback.last; cl; cl= cl->prev) {
263 console_line_color(fg, cl->type);
265 if(!console_draw_string( cl->line, cl->len,
266 console_width, sc->lheight,
268 ar->winx-(CONSOLE_DRAW_MARGIN+CONSOLE_DRAW_SCROLL),
269 v2d->cur.ymin, v2d->cur.ymax,
272 /* when drawing, if we pass v2d->cur.ymax, then quit */
274 break; /* past the y limits */
278 if((mouse_y != INT_MAX) && (mouse_y >= y_prev && mouse_y <= y)) {
279 *mouse_pick= (void *)cl;
291 glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
292 glClear(GL_COLOR_BUFFER_BIT);
295 /* convert our display toggles into a flag compatible with BKE_report flags */
296 report_mask= console_report_mask(sc);
298 for(report=reports->list.last; report; report=report->prev) {
300 if(report->type & report_mask) {
304 console_report_color(fg, bg, report, bool);
306 if(!console_draw_string( report->message, report->len,
307 console_width, sc->lheight,
309 ar->winx-(CONSOLE_DRAW_MARGIN+CONSOLE_DRAW_SCROLL),
310 v2d->cur.ymin, v2d->cur.ymax,
313 /* when drawing, if we pass v2d->cur.ymax, then quit */
315 break; /* past the y limits */
318 if((mouse_y != INT_MAX) && (mouse_y >= y_prev && mouse_y <= y)) {
319 *mouse_pick= (void *)report;
333 void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
335 console_text_main__internal(sc, ar, reports, 1, INT_MAX, NULL);
338 int console_text_height(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
340 return console_text_main__internal(sc, ar, reports, 0, INT_MAX, NULL);
343 void *console_text_pick(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int mouse_y)
345 void *mouse_pick= NULL;
346 console_text_main__internal(sc, ar, reports, 0, mouse_y, &mouse_pick);
347 return (void *)mouse_pick;