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