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