BugFix:
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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
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 "ED_types.h"
57
58 #include "UI_resources.h"
59
60 #include "console_intern.h"
61
62
63 static int mono= -1; // XXX needs proper storage and change all the BLF_* here!
64
65 static void console_font_begin(SpaceConsole *sc)
66 {
67         if(mono == -1)
68                 mono= BLF_load_mem("monospace", (unsigned char*)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
69
70         BLF_aspect(mono, 1.0);
71         BLF_size(mono, 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                 UI_GetThemeColor3ubv(TH_CONSOLE_OUTPUT, (char *)fg);
79                 break;
80         case CONSOLE_LINE_INPUT:
81                 UI_GetThemeColor3ubv(TH_CONSOLE_INPUT, (char *)fg);
82                 break;
83         case CONSOLE_LINE_INFO:
84                 UI_GetThemeColor3ubv(TH_CONSOLE_INFO, (char *)fg);
85                 break;
86         case CONSOLE_LINE_ERROR:
87                 UI_GetThemeColor3ubv(TH_CONSOLE_ERROR, (char *)fg);
88                 break;
89         }
90 }
91
92 static void console_report_color(unsigned char *fg, unsigned char *bg, Report *report, int bool)
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         if(report->flag & SELECT) {
103                 fg[0]=255; fg[1]=255; fg[2]=255;
104                 if(bool) {
105                         bg[0]=96; bg[1]=128; bg[2]=255;
106                 }
107                 else {
108                         bg[0]=90; bg[1]=122; bg[2]=249;
109                 }
110         }
111
112         else {
113                 fg[0]=0; fg[1]=0; fg[2]=0;
114
115                 if(bool) {
116                         bg[0]=120; bg[1]=120; bg[2]=120;
117                 }
118                 else {
119                         bg[0]=114; bg[1]=114; bg[2]=114;
120                 }
121
122         }
123 }
124
125 typedef struct ConsoleDrawContext {
126         int cwidth;
127         int lheight;
128         int console_width;
129         int winx;
130         int ymin, ymax;
131         int *xy; // [2]
132         int *sel; // [2]
133         int *pos_pick;
134         int *mval; // [2]
135         int draw;
136 } ConsoleDrawContext;
137
138 static void console_draw_sel(int sel[2], int xy[2], int str_len, int cwidth, int console_width, int lheight)
139 {
140         if(sel[0] <= str_len && sel[1] >= 0) {
141                 int sta = MAX2(sel[0], 0);
142                 int end = MIN2(sel[1], str_len);
143
144                 /* highly confusing but draws correctly */
145                 if(sel[0] < 0 || sel[1] > str_len) {
146                         if(sel[0] > 0) {
147                                 end= sta;
148                                 sta= 0;
149                         }
150                         if (sel[1] <= str_len) {
151                                 sta= end;
152                                 end= str_len;
153                         }
154                 }
155                 /* end confusement */
156
157                 {
158                         glEnable(GL_POLYGON_STIPPLE);
159                         glPolygonStipple(stipple_halftone);
160                         glEnable( GL_BLEND );
161                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
162                         glColor4ub(255, 255, 255, 96);
163                 }
164                 glRecti(xy[0]+(cwidth*sta), xy[1]-2 + lheight, xy[0]+(cwidth*end), xy[1]-2);
165                 {
166                         glDisable(GL_POLYGON_STIPPLE);
167                         glDisable( GL_BLEND );
168                 }
169         }
170
171         sel[0] -= str_len + 1;
172         sel[1] -= str_len + 1;
173 }
174
175
176 /* return 0 if the last line is off the screen
177  * should be able to use this for any string type */
178
179 static int console_draw_string(ConsoleDrawContext *cdc, char *str, int str_len, unsigned char *fg, unsigned char *bg)
180 {
181         int rct_ofs= cdc->lheight/4;
182         int tot_lines = (str_len/cdc->console_width)+1; /* total number of lines for wrapping */
183         int y_next = (str_len > cdc->console_width) ? cdc->xy[1]+cdc->lheight*tot_lines : cdc->xy[1]+cdc->lheight;
184
185         /* just advance the height */
186         if(cdc->draw==0) {
187                 if(cdc->pos_pick) {
188                         if((cdc->mval[1] != INT_MAX) && cdc->xy[1] <= cdc->mval[1]) {
189                                 if((cdc->xy[1]+cdc->lheight >= cdc->mval[1])) {
190                                         int ofs = (int)floor(((float)cdc->mval[0] / (float)cdc->cwidth));
191                                         *cdc->pos_pick += MIN2(ofs, str_len);
192                                 } else
193                                         *cdc->pos_pick += str_len + 1;
194                         }
195
196                 }
197
198                 cdc->xy[1]= y_next;
199                 return 1;
200         }
201         else if (y_next-cdc->lheight < cdc->ymin) {
202                 /* have not reached the drawable area so don't break */
203                 cdc->xy[1]= y_next;
204
205                 /* adjust selection even if not drawing */
206                 if(cdc->sel[0] != cdc->sel[1]) {
207                         cdc->sel[0] -= str_len + 1;
208                         cdc->sel[1] -= str_len + 1;
209                 }
210
211                 return 1;
212         }
213
214         if(str_len > cdc->console_width) { /* wrap? */
215                 char *line_stride= str + ((tot_lines-1) * cdc->console_width);  /* advance to the last line and draw it first */
216                 char eol;                                                                                                       /* baclup the end of wrapping */
217                 
218                 if(bg) {
219                         glColor3ub(bg[0], bg[1], bg[2]);
220                         glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, (cdc->xy[1]+(cdc->lheight*tot_lines))+rct_ofs);
221                 }
222
223                 glColor3ub(fg[0], fg[1], fg[2]);
224
225                 /* last part needs no clipping */
226                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
227                 BLF_draw(mono, line_stride);
228
229                 if(cdc->sel[0] != cdc->sel[1]) {
230                         cdc->sel[0] += str_len - (cdc->console_width % str_len);
231                         cdc->sel[1] += str_len - (cdc->console_width % str_len);
232                         console_draw_sel(cdc->sel, cdc->xy, cdc->console_width % str_len, cdc->cwidth, cdc->console_width, cdc->lheight);
233                         glColor3ub(fg[0], fg[1], fg[2]);
234                 }
235
236                 cdc->xy[1] += cdc->lheight;
237
238                 line_stride -= cdc->console_width;
239                 
240                 for(; line_stride >= str; line_stride -= cdc->console_width) {
241                         eol = line_stride[cdc->console_width];
242                         line_stride[cdc->console_width]= '\0';
243                         
244                         BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
245                         BLF_draw(mono, line_stride);
246                         
247                         if(cdc->sel[0] != cdc->sel[1]) {
248                                 console_draw_sel(cdc->sel, cdc->xy, cdc->console_width, cdc->cwidth, cdc->console_width, cdc->lheight);
249                                 glColor3ub(fg[0], fg[1], fg[2]);
250                         }
251
252                         cdc->xy[1] += cdc->lheight;
253
254                         line_stride[cdc->console_width] = eol; /* restore */
255                         
256                         /* check if were out of view bounds */
257                         if(cdc->xy[1] > cdc->ymax)
258                                 return 0;
259                 }
260         }
261         else { /* simple, no wrap */
262
263                 if(bg) {
264                         glColor3ub(bg[0], bg[1], bg[2]);
265                         glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, cdc->xy[1]+cdc->lheight-rct_ofs);
266                 }
267
268                 glColor3ub(fg[0], fg[1], fg[2]);
269
270                 BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
271                 BLF_draw(mono, str);
272                 
273                 if(cdc->sel[0] != cdc->sel[1])
274                         console_draw_sel(cdc->sel, cdc->xy, str_len, cdc->cwidth, cdc->console_width, cdc->lheight);
275
276                 cdc->xy[1] += cdc->lheight;
277
278                 if(cdc->xy[1] > cdc->ymax)
279                         return 0;
280         }
281
282         return 1;
283 }
284
285 #define CONSOLE_DRAW_MARGIN 4
286 #define CONSOLE_DRAW_SCROLL 16
287
288 static int console_text_main__internal(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int draw, int mval[2], void **mouse_pick, int *pos_pick)
289 {
290         View2D *v2d= &ar->v2d;
291
292         ConsoleLine *cl= sc->history.last;
293         ConsoleDrawContext cdc;
294         
295         int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
296         int xy[2], y_prev;
297         int cwidth;
298         int console_width; /* number of characters that fit into the width of the console (fixed width) */
299         int sel[2]= {-1, -1}; /* defaults disabled */
300         unsigned char fg[3];
301
302         console_font_begin(sc);
303         cwidth = BLF_fixed_width(mono);
304         
305         console_width= (ar->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) )/cwidth;
306         if (console_width < 8) console_width= 8;
307         
308         xy[0]= x_orig; xy[1]= y_orig;
309         
310         if(mval[1] != INT_MAX)
311                 mval[1] += (v2d->cur.ymin + CONSOLE_DRAW_MARGIN);
312
313         if(pos_pick)
314                 *pos_pick = 0;
315
316         /* constants for the sequencer context */
317         cdc.cwidth= cwidth;
318         cdc.lheight= sc->lheight;
319         cdc.console_width= console_width;
320         cdc.winx= ar->winx-(CONSOLE_DRAW_MARGIN+CONSOLE_DRAW_SCROLL);
321         cdc.ymin= v2d->cur.ymin;
322         cdc.ymax= v2d->cur.ymax;
323         cdc.xy= xy;
324         cdc.sel= sel;
325         cdc.pos_pick= pos_pick;
326         cdc.mval= mval;
327         cdc.draw= draw;
328
329         if(sc->type==CONSOLE_TYPE_PYTHON) {
330                 int prompt_len;
331
332                 if(sc->sel_start != sc->sel_end) {
333                         sel[0]= sc->sel_start;
334                         sel[1]= sc->sel_end;
335                         // printf("%d %d\n", sel[0], sel[1]);
336                 }
337                 
338                 /* text */
339                 if(draw) {
340                         prompt_len= strlen(sc->prompt);
341                         console_line_color(fg, CONSOLE_LINE_INPUT);
342                         glColor3ub(fg[0], fg[1], fg[2]);
343
344                         /* command line */
345                         if(prompt_len) {
346                                 BLF_position(mono, xy[0], xy[1], 0); xy[0] += cwidth * prompt_len;
347                                 BLF_draw(mono, sc->prompt);
348                         }
349                         BLF_position(mono, xy[0], xy[1], 0);
350                         BLF_draw(mono, cl->line);
351
352                         /* cursor */
353                         UI_GetThemeColor3ubv(TH_CONSOLE_CURSOR, (char *)fg);
354                         glColor3ub(fg[0], fg[1], fg[2]);
355                         glRecti(xy[0]+(cwidth*cl->cursor) -1, xy[1]-2, xy[0]+(cwidth*cl->cursor) +1, xy[1]+sc->lheight-2);
356
357                         xy[0]= x_orig; /* remove prompt offset */
358                 }
359                 
360                 xy[1] += sc->lheight;
361                 
362                 for(cl= sc->scrollback.last; cl; cl= cl->prev) {
363                         y_prev= xy[1];
364
365                         if(draw)
366                                 console_line_color(fg, cl->type);
367
368                         if(!console_draw_string(&cdc, cl->line, cl->len, fg, NULL)) {
369                                 /* when drawing, if we pass v2d->cur.ymax, then quit */
370                                 if(draw) {
371                                         break; /* past the y limits */
372                                 }
373                         }
374
375                         if((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
376                                 *mouse_pick= (void *)cl;
377                                 break;
378                         }
379                 }
380         }
381         else { 
382                 Report *report;
383                 int report_mask= 0;
384                 int bool= 0;
385                 unsigned char bg[3];
386
387                 if(draw) {
388                         glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
389                         glClear(GL_COLOR_BUFFER_BIT);
390                 }
391
392                 /* convert our display toggles into a flag compatible with BKE_report flags */
393                 report_mask= console_report_mask(sc);
394                 
395                 for(report=reports->list.last; report; report=report->prev) {
396                         
397                         if(report->type & report_mask) {
398                                 y_prev= xy[1];
399
400                                 if(draw)
401                                         console_report_color(fg, bg, report, bool);
402
403                                 if(!console_draw_string(&cdc, report->message, report->len, fg, bg)) {
404                                         /* when drawing, if we pass v2d->cur.ymax, then quit */
405                                         if(draw) {
406                                                 break; /* past the y limits */
407                                         }
408                                 }
409                                 if((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
410                                         *mouse_pick= (void *)report;
411                                         break;
412                                 }
413
414                                 bool = !(bool);
415                         }
416                 }
417         }
418         xy[1] += sc->lheight*2;
419
420         
421         return xy[1]-y_orig;
422 }
423
424 void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
425 {
426         int mval[2] = {INT_MAX, INT_MAX};
427         console_text_main__internal(sc, ar, reports, 1,  mval, NULL, NULL);
428 }
429
430 int console_text_height(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
431 {
432         int mval[2] = {INT_MAX, INT_MAX};
433         return console_text_main__internal(sc, ar, reports, 0,  mval, NULL, NULL);
434 }
435
436 void *console_text_pick(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int mouse_y)
437 {
438         int mval[2] = {0, mouse_y};
439         void *mouse_pick= NULL;
440         console_text_main__internal(sc, ar, reports, 0, mval, &mouse_pick, NULL);
441         return (void *)mouse_pick;
442 }
443
444 // XXX - breaks with line wrap
445 int console_char_pick(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int mval[2])
446 {
447         int pos_pick= 0;
448         void *mouse_pick= NULL;
449         console_text_main__internal(sc, ar, reports, 0, mval, &mouse_pick, &pos_pick);
450         return pos_pick;
451 }