Blender V2.61 - r43446

console_draw.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 <stdlib.h>
00030 #include <string.h>
00031 #include <sys/stat.h>
00032 #include <limits.h>
00033 
00034 #include "BLF_api.h"
00035 
00036 #include "BLI_blenlib.h"
00037 #include "BLI_utildefines.h"
00038 
00039 #include "DNA_space_types.h"
00040 #include "DNA_screen_types.h"
00041 
00042 #include "BKE_report.h"
00043 
00044 
00045 #include "MEM_guardedalloc.h"
00046 
00047 #include "BIF_gl.h"
00048 #include "BIF_glutil.h"
00049 
00050 #include "ED_datafiles.h"
00051 #include "ED_types.h"
00052 
00053 #include "UI_resources.h"
00054 
00055 #include "console_intern.h"
00056 
00057 
00058 #include "../space_info/textview.h"
00059 
00060 static void console_line_color(unsigned char fg[3], int type)
00061 {
00062     switch(type) {
00063     case CONSOLE_LINE_OUTPUT:
00064         UI_GetThemeColor3ubv(TH_CONSOLE_OUTPUT, fg);
00065         break;
00066     case CONSOLE_LINE_INPUT:
00067         UI_GetThemeColor3ubv(TH_CONSOLE_INPUT, fg);
00068         break;
00069     case CONSOLE_LINE_INFO:
00070         UI_GetThemeColor3ubv(TH_CONSOLE_INFO, fg);
00071         break;
00072     case CONSOLE_LINE_ERROR:
00073         UI_GetThemeColor3ubv(TH_CONSOLE_ERROR, fg);
00074         break;
00075     }
00076 }
00077 
00078 typedef struct ConsoleDrawContext {
00079     int cwidth;
00080     int lheight;
00081     int console_width;
00082     int winx;
00083     int ymin, ymax;
00084 #if 0 /* used by textview, may use later */
00085     int *xy; // [2]
00086     int *sel; // [2]
00087     int *pos_pick; // bottom of view == 0, top of file == combine chars, end of line is lower then start. 
00088     int *mval; // [2]
00089     int draw;
00090 #endif
00091 } ConsoleDrawContext;
00092 
00093 void console_scrollback_prompt_begin(struct SpaceConsole *sc, ConsoleLine *cl_dummy)
00094 {
00095     /* fake the edit line being in the scroll buffer */
00096     ConsoleLine *cl= sc->history.last;
00097     cl_dummy->type= CONSOLE_LINE_INPUT;
00098     cl_dummy->len= cl_dummy->len_alloc= strlen(sc->prompt) + cl->len;
00099     cl_dummy->len_alloc= cl_dummy->len + 1;
00100     cl_dummy->line= MEM_mallocN(cl_dummy->len_alloc, "cl_dummy");
00101     memcpy(cl_dummy->line, sc->prompt, (cl_dummy->len_alloc - cl->len));
00102     memcpy(cl_dummy->line + ((cl_dummy->len_alloc - cl->len)) - 1, cl->line, cl->len + 1);
00103     BLI_addtail(&sc->scrollback, cl_dummy);
00104 }
00105 void console_scrollback_prompt_end(struct SpaceConsole *sc, ConsoleLine *cl_dummy) 
00106 {
00107     MEM_freeN(cl_dummy->line);
00108     BLI_remlink(&sc->scrollback, cl_dummy);
00109 }
00110 
00111 #define CONSOLE_DRAW_MARGIN 4
00112 #define CONSOLE_DRAW_SCROLL 16
00113 
00114 
00115 
00116 /* console textview callbacks */
00117 static int console_textview_begin(TextViewContext *tvc)
00118 {
00119     SpaceConsole *sc= (SpaceConsole *)tvc->arg1;
00120     tvc->lheight= sc->lheight;
00121     tvc->sel_start= sc->sel_start;
00122     tvc->sel_end= sc->sel_end;
00123     
00124     /* iterator */
00125     tvc->iter= sc->scrollback.last;
00126     
00127     return (tvc->iter != NULL);
00128 }
00129 
00130 static void console_textview_end(TextViewContext *tvc)
00131 {
00132     SpaceConsole *sc= (SpaceConsole *)tvc->arg1;
00133     (void)sc;
00134     
00135 }
00136 
00137 static int console_textview_step(TextViewContext *tvc)
00138 {
00139     return ((tvc->iter= (void *)((Link *)tvc->iter)->prev) != NULL);
00140 }
00141 
00142 static int console_textview_line_get(struct TextViewContext *tvc, const char **line, int *len)
00143 {
00144     ConsoleLine *cl= (ConsoleLine *)tvc->iter;
00145     *line= cl->line;
00146     *len= cl->len;
00147 
00148     return 1;
00149 }
00150 
00151 static int console_textview_line_color(struct TextViewContext *tvc, unsigned char fg[3], unsigned char UNUSED(bg[3]))
00152 {
00153     ConsoleLine *cl_iter= (ConsoleLine *)tvc->iter;
00154 
00155     /* annoying hack, to draw the prompt */
00156     if(tvc->iter_index == 0) {
00157         const SpaceConsole *sc= (SpaceConsole *)tvc->arg1;
00158         const ConsoleLine *cl= (ConsoleLine *)sc->history.last;
00159         const int prompt_len= strlen(sc->prompt);
00160         const int cursor_loc= cl->cursor + prompt_len;
00161         int xy[2] = {CONSOLE_DRAW_MARGIN, CONSOLE_DRAW_MARGIN};
00162         int pen[2];
00163         xy[1] += tvc->lheight/6;
00164 
00165         /* account for wrapping */
00166         if(cl->len < tvc->console_width) {
00167             /* simple case, no wrapping */
00168             pen[0]= tvc->cwidth * cursor_loc;
00169             pen[1]= -2;
00170         }
00171         else {
00172             /* wrap */
00173             pen[0]= tvc->cwidth * (cursor_loc % tvc->console_width);
00174             pen[1]= -2 + (((cl->len / tvc->console_width) - (cursor_loc / tvc->console_width)) * tvc->lheight);
00175         }
00176 
00177         /* cursor */
00178         UI_GetThemeColor3ubv(TH_CONSOLE_CURSOR, fg);
00179         glColor3ubv(fg);
00180 
00181         glRecti(    (xy[0] + pen[0]) - 1,
00182                     (xy[1] + pen[1]),
00183                     (xy[0] + pen[0]) + 1,
00184                     (xy[1] + pen[1] + tvc->lheight)
00185         );
00186     }
00187 
00188     console_line_color(fg, cl_iter->type);
00189 
00190     return TVC_LINE_FG;
00191 }
00192 
00193 
00194 static int console_textview_main__internal(struct SpaceConsole *sc, struct ARegion *ar, int draw, int mval[2], void **mouse_pick, int *pos_pick)
00195 {
00196     ConsoleLine cl_dummy= {NULL};
00197     int ret= 0;
00198     
00199     View2D *v2d= &ar->v2d;
00200 
00201     TextViewContext tvc= {0};
00202 
00203     tvc.begin= console_textview_begin;
00204     tvc.end= console_textview_end;
00205 
00206     tvc.step= console_textview_step;
00207     tvc.line_get= console_textview_line_get;
00208     tvc.line_color= console_textview_line_color;
00209 
00210     tvc.arg1= sc;
00211     tvc.arg2= NULL;
00212 
00213     /* view */
00214     tvc.sel_start= sc->sel_start;
00215     tvc.sel_end= sc->sel_end;
00216     tvc.lheight= sc->lheight;
00217     tvc.ymin= v2d->cur.ymin;
00218     tvc.ymax= v2d->cur.ymax;
00219     tvc.winx= ar->winx;
00220 
00221     console_scrollback_prompt_begin(sc, &cl_dummy);
00222     ret= textview_draw(&tvc, draw, mval, mouse_pick, pos_pick);
00223     console_scrollback_prompt_end(sc, &cl_dummy);
00224 
00225     return ret;
00226 }
00227 
00228 
00229 void console_textview_main(struct SpaceConsole *sc, struct ARegion *ar)
00230 {
00231     int mval[2] = {INT_MAX, INT_MAX};
00232     console_textview_main__internal(sc, ar, 1,  mval, NULL, NULL);
00233 }
00234 
00235 int console_textview_height(struct SpaceConsole *sc, struct ARegion *ar)
00236 {
00237     int mval[2] = {INT_MAX, INT_MAX};
00238     return console_textview_main__internal(sc, ar, 0,  mval, NULL, NULL);
00239 }
00240 
00241 int console_char_pick(struct SpaceConsole *sc, struct ARegion *ar, int mval[2])
00242 {
00243     int pos_pick= 0;
00244     void *mouse_pick= NULL;
00245     int mval_clamp[2];
00246 
00247     mval_clamp[0]= CLAMPIS(mval[0], CONSOLE_DRAW_MARGIN, ar->winx-(CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN));
00248     mval_clamp[1]= CLAMPIS(mval[1], CONSOLE_DRAW_MARGIN, ar->winy-CONSOLE_DRAW_MARGIN);
00249 
00250     console_textview_main__internal(sc, ar, 0, mval_clamp, &mouse_pick, &pos_pick);
00251     return pos_pick;
00252 }