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