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