- Scrollbars for the console (use View2D functions)
[blender-staging.git] / source / blender / editors / space_console / console_draw.c
index 0a725d0c69ffe1525492039e747abf9571b216c6..96641fd8fbdb1de6ca297f4c7117d50c0d58ed01 100644 (file)
@@ -68,7 +68,7 @@ static void console_font_begin(SpaceConsole *sc)
        BLF_set(mono);
        BLF_aspect(1.0);
 
-       BLF_size(sc->lheight, 72);
+       BLF_size(sc->lheight-2, 72);
 }
 
 static void console_line_color(unsigned char *fg, int type)
@@ -106,12 +106,24 @@ static void console_report_color(unsigned char *fg, int type)
 
 /* return 0 if the last line is off the screen
  * should be able to use this for any string type */
-static int console_draw_string(char *str, int str_len, int console_width, int lheight, unsigned char *fg, unsigned char *bg, int winx, int winy, int *x, int *y)
+static int console_draw_string(        char *str, int str_len,
+                                                                       int console_width, int lheight,
+                                                                       unsigned char *fg, unsigned char *bg,
+                                                                       int winx, int winy,
+                                                                       int *x, int *y, int draw)
 {      
        int rct_ofs= lheight/4;
+       int tot_lines = (str_len/console_width)+1;                                                      /* total number of lines for wrapping */
+
+       /* just advance the height */
+       if(draw==0) {
+               if(str_len > console_width)     (*y) += tot_lines * lheight;
+               else                                            (*y) += lheight;
+
+               return 1;
+       }
 
        if(str_len > console_width) { /* wrap? */
-               int tot_lines = (str_len/console_width)+1;                                                      /* total number of lines for wrapping */
                char *line_stride= str + ((tot_lines-1) * console_width);       /* advance to the last line and draw it first */
                char eol;                                                                                                                       /* baclup the end of wrapping */
                
@@ -160,11 +172,13 @@ static int console_draw_string(char *str, int str_len, int console_width, int lh
        return 1;
 }
 
-#define CONSOLE_DRAW_MARGIN 8
-#define CONSOLE_LINE_MARGIN 6
+#define CONSOLE_DRAW_MARGIN 4
+#define CONSOLE_DRAW_SCROLL 16
 
-void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
+static int console_text_main__internal(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports, int draw)
 {
+       View2D *v2d= &ar->v2d;
+
        ConsoleLine *cl= sc->history.last;
        
        int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN;
@@ -176,41 +190,54 @@ void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *
        console_font_begin(sc);
        cwidth = BLF_fixed_width();
        
-       console_width= (ar->winx - CONSOLE_DRAW_MARGIN*2)/cwidth;
+       console_width= (ar->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) )/cwidth;
        if (console_width < 8) console_width= 8;
        
        x= x_orig; y= y_orig;
        
        if(sc->type==CONSOLE_TYPE_PYTHON) {
-               int prompt_len= strlen(sc->prompt);
+               int prompt_len;
                
                /* text */
-               console_line_color(fg, CONSOLE_LINE_INPUT);
-               glColor3ub(fg[0], fg[1], fg[2]);
-               
-               /* command line */
-               if(prompt_len) {
-                       BLF_position(x, y, 0); x += cwidth * prompt_len;
-                       BLF_draw(sc->prompt);   
+               if(draw) {
+                       prompt_len= strlen(sc->prompt);
+                       console_line_color(fg, CONSOLE_LINE_INPUT);
+                       glColor3ub(fg[0], fg[1], fg[2]);
+
+                       /* command line */
+                       if(prompt_len) {
+                               BLF_position(x, y, 0); x += cwidth * prompt_len;
+                               BLF_draw(sc->prompt);
+                       }
+                       BLF_position(x, y, 0);
+                       BLF_draw(cl->line);
+
+                       /* cursor */
+                       console_line_color(fg, CONSOLE_LINE_ERROR); /* lazy */
+                       glColor3ub(fg[0], fg[1], fg[2]);
+                       glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
+
+                       x= x_orig; /* remove prompt offset */
                }
-               BLF_position(x, y, 0);
-               BLF_draw(cl->line);     
-               
-               /* cursor */
-               console_line_color(fg, CONSOLE_LINE_ERROR); /* lazy */
-               glColor3ub(fg[0], fg[1], fg[2]);
-               glRecti(x+(cwidth*cl->cursor) -1, y-2, x+(cwidth*cl->cursor) +1, y+sc->lheight-2);
-               
-               x= x_orig; /* remove prompt offset */
                
                y += sc->lheight;
                
                for(cl= sc->scrollback.last; cl; cl= cl->prev) {
-                       console_line_color(fg, cl->type);
 
-                       if(!console_draw_string(cl->line, cl->len, console_width, sc->lheight+CONSOLE_LINE_MARGIN, fg, NULL, ar->winx, ar->winy, &x, &y))
-                               break; /* past the y limits */
-                       
+                       if(draw)
+                               console_line_color(fg, cl->type);
+
+                       if(!console_draw_string(        cl->line, cl->len,
+                                                                               console_width, sc->lheight,
+                                                                               fg, NULL,
+                                                                               ar->winx-CONSOLE_DRAW_MARGIN, v2d->cur.ymax,
+                                                                               &x, &y, draw))
+                       {
+                               /* when drawing, if we pass v2d->cur.ymax, then quit */
+                               if(draw) {
+                                       break; /* past the y limits */
+                               }
+                       }
                }
        }
        else { 
@@ -219,8 +246,10 @@ void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *
                int bool= 0;
                unsigned char bg[3] = {114, 114, 114};
                
-               glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
-               glClear(GL_COLOR_BUFFER_BIT);
+               if(draw) {
+                       glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
+                       glClear(GL_COLOR_BUFFER_BIT);
+               }
 
                /* convert our display toggles into a flag compatible with BKE_report flags */
                if(sc->rpt_mask & CONSOLE_RPT_DEBUG)    report_mask |= RPT_DEBUG_ALL;
@@ -232,15 +261,39 @@ void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *
                for(report=reports->list.last; report; report=report->prev) {
                        
                        if(report->type & report_mask) {
-                               console_report_color(fg, report->type);
-                               if(!console_draw_string(report->message, strlen(report->message), console_width, sc->lheight+CONSOLE_LINE_MARGIN, fg, bool?bg:NULL, ar->winx, ar->winy, &x, &y))
-                                       break; /* past the y limits */
 
-                               y+=CONSOLE_LINE_MARGIN;
+                               if(draw)
+                                       console_report_color(fg, report->type);
+
+                               if(!console_draw_string(        report->message, report->len,
+                                                                                       console_width, sc->lheight,
+                                                                                       fg, bool?bg:NULL,
+                                                                                       ar->winx-CONSOLE_DRAW_MARGIN, v2d->cur.ymax,
+                                                                                       &x, &y, draw))
+                               {
+                                       /* when drawing, if we pass v2d->cur.ymax, then quit */
+                                       if(draw) {
+                                               break; /* past the y limits */
+                                       }
+                               }
+
                                bool = !(bool);
                        }
                        
                }
        }
+       y += sc->lheight*2;
+
        
+       return y-y_orig;
+}
+
+void console_text_main(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
+{
+       console_text_main__internal(sc, ar, reports, 1);
+}
+
+int console_text_height(struct SpaceConsole *sc, struct ARegion *ar, ReportList *reports)
+{
+       return console_text_main__internal(sc, ar, reports, 0);
 }