Blender V2.61 - r43446

textview.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Campbell Barton
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00028 #include <math.h>
00029 #include <string.h>
00030 #include <sys/stat.h>
00031 #include <limits.h>
00032 #include <assert.h>
00033 
00034 #include "BLF_api.h"
00035 
00036 #include "BLI_math.h"
00037 #include "BLI_utildefines.h"
00038 
00039 
00040 
00041 #include "BIF_gl.h"
00042 #include "BIF_glutil.h"
00043 
00044 #include "ED_datafiles.h"
00045 
00046 #include "textview.h"
00047 
00048 static void console_font_begin(TextViewContext *sc)
00049 {
00050     BLF_size(blf_mono_font, sc->lheight-2, 72);
00051 }
00052 
00053 typedef struct ConsoleDrawContext {
00054     int cwidth;
00055     int lheight;
00056     int console_width; /* number of characters that fit into the width of the console (fixed width) */
00057     int winx;
00058     int ymin, ymax;
00059     int *xy; // [2]
00060     int *sel; // [2]
00061     int *pos_pick; // bottom of view == 0, top of file == combine chars, end of line is lower then start. 
00062     int *mval; // [2]
00063     int draw;
00064 } ConsoleDrawContext;
00065 
00066 static void console_draw_sel(int sel[2], int xy[2], int str_len_draw, int cwidth, int lheight)
00067 {
00068     if(sel[0] <= str_len_draw && sel[1] >= 0) {
00069         int sta = MAX2(sel[0], 0);
00070         int end = MIN2(sel[1], str_len_draw);
00071 
00072         glEnable(GL_POLYGON_STIPPLE);
00073         glPolygonStipple(stipple_halftone);
00074         glEnable( GL_BLEND );
00075         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00076         glColor4ub(255, 255, 255, 96);
00077 
00078         glRecti(xy[0]+(cwidth*sta), xy[1]-2 + lheight, xy[0]+(cwidth*end), xy[1]-2);
00079 
00080         glDisable(GL_POLYGON_STIPPLE);
00081         glDisable( GL_BLEND );
00082     }
00083 }
00084 
00085 
00086 /* return 0 if the last line is off the screen
00087  * should be able to use this for any string type */
00088 
00089 static int console_draw_string(ConsoleDrawContext *cdc, const char *str, int str_len, unsigned char *fg, unsigned char *bg)
00090 {
00091 #define STEP_SEL(value) cdc->sel[0] += (value); cdc->sel[1] += (value)
00092     int rct_ofs= cdc->lheight/4;
00093     int tot_lines = (str_len/cdc->console_width)+1; /* total number of lines for wrapping */
00094     int y_next = (str_len > cdc->console_width) ? cdc->xy[1]+cdc->lheight*tot_lines : cdc->xy[1]+cdc->lheight;
00095     const int mono= blf_mono_font;
00096 
00097     /* just advance the height */
00098     if(cdc->draw==0) {
00099         if(cdc->pos_pick && (cdc->mval[1] != INT_MAX)) {
00100             if(cdc->xy[1] <= cdc->mval[1]) {
00101                 if((y_next >= cdc->mval[1])) {
00102                     int ofs = (int)floor(((float)cdc->mval[0] / (float)cdc->cwidth));
00103 
00104                     /* wrap */
00105                     if(str_len > cdc->console_width)
00106                         ofs += (cdc->console_width * ((int)((((float)(y_next - cdc->mval[1]) / (float)(y_next-cdc->xy[1])) * tot_lines))));
00107     
00108                     CLAMP(ofs, 0, str_len);
00109                     *cdc->pos_pick += str_len - ofs;
00110                 } else
00111                     *cdc->pos_pick += str_len + 1;
00112             }
00113         }
00114 
00115         cdc->xy[1]= y_next;
00116         return 1;
00117     }
00118     else if (y_next-cdc->lheight < cdc->ymin) {
00119         /* have not reached the drawable area so don't break */
00120         cdc->xy[1]= y_next;
00121 
00122         /* adjust selection even if not drawing */
00123         if(cdc->sel[0] != cdc->sel[1]) {
00124             STEP_SEL(-(str_len + 1));
00125         }
00126 
00127         return 1;
00128     }
00129 
00130     if(str_len > cdc->console_width) { /* wrap? */
00131         const int initial_offset= ((tot_lines-1) * cdc->console_width);
00132         const char *line_stride= str + initial_offset;  /* advance to the last line and draw it first */
00133         
00134         int sel_orig[2];
00135         copy_v2_v2_int(sel_orig, cdc->sel);
00136 
00137         /* invert and swap for wrapping */
00138         cdc->sel[0] = str_len - sel_orig[1];
00139         cdc->sel[1] = str_len - sel_orig[0];
00140         
00141         if(bg) {
00142             glColor3ubv(bg);
00143             glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, (cdc->xy[1]+(cdc->lheight*tot_lines))+rct_ofs);
00144         }
00145 
00146         glColor3ubv(fg);
00147 
00148         /* last part needs no clipping */
00149         BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
00150         BLF_draw(mono, line_stride, str_len - initial_offset);
00151 
00152         if(cdc->sel[0] != cdc->sel[1]) {
00153             STEP_SEL(-initial_offset);
00154             // glColor4ub(255, 0, 0, 96); // debug
00155             console_draw_sel(cdc->sel, cdc->xy, str_len % cdc->console_width, cdc->cwidth, cdc->lheight);
00156             STEP_SEL(cdc->console_width);
00157             glColor3ubv(fg);
00158         }
00159 
00160         cdc->xy[1] += cdc->lheight;
00161 
00162         line_stride -= cdc->console_width;
00163         
00164         for(; line_stride >= str; line_stride -= cdc->console_width) {
00165             BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
00166             BLF_draw(mono, line_stride, cdc->console_width);
00167             
00168             if(cdc->sel[0] != cdc->sel[1]) {
00169                 // glColor4ub(0, 255, 0, 96); // debug
00170                 console_draw_sel(cdc->sel, cdc->xy, cdc->console_width, cdc->cwidth, cdc->lheight);
00171                 STEP_SEL(cdc->console_width);
00172                 glColor3ubv(fg);
00173             }
00174 
00175             cdc->xy[1] += cdc->lheight;
00176             
00177             /* check if were out of view bounds */
00178             if(cdc->xy[1] > cdc->ymax)
00179                 return 0;
00180         }
00181 
00182         copy_v2_v2_int(cdc->sel, sel_orig);
00183         STEP_SEL(-(str_len + 1));
00184     }
00185     else { /* simple, no wrap */
00186 
00187         if(bg) {
00188             glColor3ubv(bg);
00189             glRecti(0, cdc->xy[1]-rct_ofs, cdc->winx, cdc->xy[1]+cdc->lheight-rct_ofs);
00190         }
00191 
00192         glColor3ubv(fg);
00193 
00194         BLF_position(mono, cdc->xy[0], cdc->xy[1], 0);
00195         BLF_draw(mono, str, str_len);
00196         
00197         if(cdc->sel[0] != cdc->sel[1]) {
00198             int isel[2];
00199 
00200             isel[0]= str_len - cdc->sel[1];
00201             isel[1]= str_len - cdc->sel[0];
00202 
00203             // glColor4ub(255, 255, 0, 96); // debug
00204             console_draw_sel(isel, cdc->xy, str_len, cdc->cwidth, cdc->lheight);
00205             STEP_SEL(-(str_len + 1));
00206         }
00207 
00208         cdc->xy[1] += cdc->lheight;
00209 
00210         if(cdc->xy[1] > cdc->ymax)
00211             return 0;
00212     }
00213 
00214     return 1;
00215 #undef STEP_SEL
00216 }
00217 
00218 #define CONSOLE_DRAW_MARGIN 4
00219 #define CONSOLE_DRAW_SCROLL 16
00220 
00221 int textview_draw(TextViewContext *tvc, int draw, int mval[2], void **mouse_pick, int *pos_pick)
00222 {
00223     ConsoleDrawContext cdc= {0};
00224 
00225     int x_orig=CONSOLE_DRAW_MARGIN, y_orig=CONSOLE_DRAW_MARGIN + tvc->lheight/6;
00226     int xy[2], y_prev;
00227     int sel[2]= {-1, -1}; /* defaults disabled */
00228     unsigned char fg[3], bg[3];
00229     const int mono= blf_mono_font;
00230 
00231     console_font_begin(tvc);
00232 
00233     xy[0]= x_orig; xy[1]= y_orig;
00234 
00235     if(mval[1] != INT_MAX)
00236         mval[1] += (tvc->ymin + CONSOLE_DRAW_MARGIN);
00237 
00238     if(pos_pick)
00239         *pos_pick = 0;
00240 
00241     /* constants for the sequencer context */
00242     cdc.cwidth= (int)BLF_fixed_width(mono);
00243     assert(cdc.cwidth > 0);
00244     cdc.lheight= tvc->lheight;
00245     cdc.console_width= (tvc->winx - (CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN*2) ) / cdc.cwidth;
00246     CLAMP(cdc.console_width, 1, INT_MAX); /* avoid divide by zero on small windows */
00247     cdc.winx= tvc->winx-(CONSOLE_DRAW_MARGIN+CONSOLE_DRAW_SCROLL);
00248     cdc.ymin= tvc->ymin;
00249     cdc.ymax= tvc->ymax;
00250     cdc.xy= xy;
00251     cdc.sel= sel;
00252     cdc.pos_pick= pos_pick;
00253     cdc.mval= mval;
00254     cdc.draw= draw;
00255 
00256     /* shouldnt be needed */
00257     tvc->cwidth= cdc.cwidth;
00258     tvc->console_width= cdc.console_width;
00259     tvc->iter_index= 0;
00260 
00261     if(tvc->sel_start != tvc->sel_end) {
00262         sel[0]= tvc->sel_start;
00263         sel[1]= tvc->sel_end;
00264     }
00265 
00266     if(tvc->begin(tvc)) {
00267 
00268         do {
00269             const char *ext_line;
00270             int ext_len;
00271             int color_flag= 0;
00272 
00273             y_prev= xy[1];
00274 
00275             if(draw)
00276                 color_flag= tvc->line_color(tvc, fg, bg);
00277 
00278             tvc->line_get(tvc, &ext_line, &ext_len);
00279 
00280             if(!console_draw_string(&cdc, ext_line, ext_len, (color_flag & TVC_LINE_FG) ? fg : NULL, (color_flag & TVC_LINE_BG) ? bg : NULL)) {
00281                 /* when drawing, if we pass v2d->cur.ymax, then quit */
00282                 if(draw) {
00283                     break; /* past the y limits */
00284                 }
00285             }
00286 
00287             if((mval[1] != INT_MAX) && (mval[1] >= y_prev && mval[1] <= xy[1])) {
00288                 *mouse_pick= (void *)tvc->iter;
00289                 break;
00290             }
00291 
00292             tvc->iter_index++;
00293 
00294         } while(tvc->step(tvc));
00295     }
00296 
00297     tvc->end(tvc);
00298 
00299     xy[1] += tvc->lheight * 2;
00300 
00301     return xy[1] - y_orig;
00302 }