Blender  V2.59
info_draw.c
Go to the documentation of this file.
00001 /*
00002  * $Id: info_draw.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  * The Original Code is Copyright (C) 2010 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * Contributor(s): Blender Foundation
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00034 #include <math.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <sys/stat.h>
00038 #include <limits.h>
00039 
00040 #include "BLF_api.h"
00041 
00042 #include "BLI_blenlib.h"
00043 #include "BLI_utildefines.h"
00044 
00045 #include "DNA_space_types.h"
00046 #include "DNA_screen_types.h"
00047 
00048 // #include "BKE_suggestions.h"
00049 #include "BKE_report.h"
00050 
00051 
00052 #include "MEM_guardedalloc.h"
00053 
00054 #include "BIF_gl.h"
00055 #include "BIF_glutil.h"
00056 
00057 #include "ED_datafiles.h"
00058 #include "ED_types.h"
00059 
00060 #include "UI_resources.h"
00061 
00062 #include "info_intern.h"
00063 #include "../space_info/textview.h"
00064 
00065 /* complicates things a bit, so leaving in old simple code */
00066 #define USE_INFO_NEWLINE
00067 
00068 static void info_report_color(unsigned char *fg, unsigned char *bg, Report *report, int bool)
00069 {
00070         if(report->flag & SELECT) {
00071                 fg[0]=255; fg[1]=255; fg[2]=255;
00072                 if(bool) {
00073                         bg[0]=96; bg[1]=128; bg[2]=255;
00074                 }
00075                 else {
00076                         bg[0]=90; bg[1]=122; bg[2]=249;
00077                 }
00078         }
00079         else {
00080                 fg[0]=0; fg[1]=0; fg[2]=0;
00081                 
00082                 if (report->type & RPT_ERROR_ALL) {
00083                         if (bool) { bg[0]=220; bg[1]=0;   bg[2]=0;   }
00084                         else      { bg[0]=214; bg[1]=0;   bg[2]=0;   }
00085                 }
00086                 else if (report->type & RPT_WARNING_ALL) {
00087                         if (bool) { bg[0]=220; bg[1]=128; bg[2]=96;  }
00088                         else      { bg[0]=214; bg[1]=122; bg[2]=90;  }
00089                 }
00090 #if 0 // XXX: this looks like the selected colour, so don't use this
00091                 else if (report->type & RPT_OPERATOR_ALL) {
00092                         if (bool) { bg[0]=96;  bg[1]=128; bg[2]=255; }
00093                         else      { bg[0]=90;  bg[1]=122; bg[2]=249; }
00094                 }
00095 #endif
00096                 else if (report->type & RPT_INFO_ALL) {
00097                         if (bool) { bg[0]=0;   bg[1]=170; bg[2]=0;   }
00098                         else      { bg[0]=0;   bg[1]=164; bg[2]=0;   }
00099                 }
00100                 else if (report->type & RPT_DEBUG_ALL) {
00101                         if (bool) { bg[0]=196; bg[1]=196; bg[2]=196; }
00102                         else      { bg[0]=190; bg[1]=190; bg[2]=190; }
00103                 }
00104                 else {
00105                         if (bool) { bg[0]=120; bg[1]=120; bg[2]=120; }
00106                         else      { bg[0]=114; bg[1]=114; bg[2]=114; }
00107                 }
00108         }
00109 }
00110 
00111 /* reports! */
00112 #ifdef USE_INFO_NEWLINE
00113 static void report_textview_init__internal(TextViewContext *tvc)
00114 {
00115         Report *report= (Report *)tvc->iter;
00116         const char *str= report->message;
00117         const char *next_str= strchr(str + tvc->iter_char, '\n');
00118 
00119         if(next_str) {
00120                 tvc->iter_char_next= (int)(next_str - str);
00121         }
00122         else {
00123                 tvc->iter_char_next= report->len;
00124         }
00125 }
00126 
00127 static int report_textview_skip__internal(TextViewContext *tvc)
00128 {
00129         SpaceInfo *sinfo= (SpaceInfo *)tvc->arg1;
00130         const int report_mask= info_report_mask(sinfo);
00131         while (tvc->iter && (((Report *)tvc->iter)->type & report_mask)==0) {
00132                 tvc->iter= (void *)((Link *)tvc->iter)->prev;
00133         }
00134         return (tvc->iter != NULL);
00135 }
00136 
00137 #endif // USE_INFO_NEWLINE
00138 
00139 static int report_textview_begin(TextViewContext *tvc)
00140 {
00141         // SpaceConsole *sc= (SpaceConsole *)tvc->arg1;
00142         ReportList *reports= (ReportList *)tvc->arg2;
00143 
00144         tvc->lheight= 14; //sc->lheight;
00145         tvc->sel_start= 0;
00146         tvc->sel_end= 0;
00147 
00148         /* iterator */
00149         tvc->iter= reports->list.last;
00150 
00151         glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
00152         glClear(GL_COLOR_BUFFER_BIT);
00153 
00154 #ifdef USE_INFO_NEWLINE
00155         tvc->iter_tmp= 0;
00156         if(tvc->iter && report_textview_skip__internal(tvc)) {
00157                 /* init the newline iterator */
00158                 tvc->iter_char= 0;
00159                 report_textview_init__internal(tvc);
00160 
00161                 return TRUE;
00162         }
00163         else {
00164                 return FALSE;
00165         }
00166 #else
00167         return (tvc->iter != NULL);
00168 #endif
00169 }
00170 
00171 static void report_textview_end(TextViewContext *UNUSED(tvc))
00172 {
00173         /* pass */
00174 }
00175 
00176 #ifdef USE_INFO_NEWLINE
00177 static int report_textview_step(TextViewContext *tvc)
00178 {
00179         /* simple case, but no newline support */
00180         Report *report= (Report *)tvc->iter;
00181 
00182         if(report->len <= tvc->iter_char_next) {
00183                 tvc->iter= (void *)((Link *)tvc->iter)->prev;
00184                 if(tvc->iter && report_textview_skip__internal(tvc)) {
00185                         tvc->iter_tmp++;
00186 
00187                         tvc->iter_char= 0; /* reset start */
00188                         report_textview_init__internal(tvc);
00189 
00190                         return TRUE;
00191                 }
00192                 else {
00193                         return FALSE;
00194                 }
00195         }
00196         else {
00197                 /* step to the next newline */
00198                 tvc->iter_char= tvc->iter_char_next + 1;
00199                 report_textview_init__internal(tvc);
00200 
00201                 return TRUE;
00202         }
00203 }
00204 
00205 static int report_textview_line_get(struct TextViewContext *tvc, const char **line, int *len)
00206 {
00207         Report *report= (Report *)tvc->iter;
00208         *line= report->message + tvc->iter_char;
00209         *len= tvc->iter_char_next - tvc->iter_char;
00210         return 1;
00211 }
00212 
00213 static int report_textview_line_color(struct TextViewContext *tvc, unsigned char fg[3], unsigned char bg[3])
00214 {
00215         Report *report= (Report *)tvc->iter;
00216         info_report_color(fg, bg, report, tvc->iter_tmp % 2);
00217         return TVC_LINE_FG | TVC_LINE_BG;
00218 }
00219 
00220 
00221 #else // USE_INFO_NEWLINE
00222 
00223 static int report_textview_step(TextViewContext *tvc)
00224 {
00225         SpaceInfo *sinfo= (SpaceInfo *)tvc->arg1;
00226         const int report_mask= info_report_mask(sinfo);
00227         do {
00228                 tvc->iter= (void *)((Link *)tvc->iter)->prev;
00229         } while (tvc->iter && (((Report *)tvc->iter)->type & report_mask)==0);
00230 
00231         return (tvc->iter != NULL);
00232 }
00233 
00234 static int report_textview_line_get(struct TextViewContext *tvc, const char **line, int *len)
00235 {
00236         Report *report= (Report *)tvc->iter;
00237         *line= report->message;
00238         *len= report->len;
00239 
00240         return 1;
00241 }
00242 
00243 static int report_textview_line_color(struct TextViewContext *tvc, unsigned char fg[3], unsigned char bg[3])
00244 {
00245         Report *report= (Report *)tvc->iter;
00246         info_report_color(fg, bg, report, tvc->iter_tmp % 2);
00247         return TVC_LINE_FG | TVC_LINE_BG;
00248 }
00249 
00250 #endif // USE_INFO_NEWLINE
00251 
00252 #undef USE_INFO_NEWLINE
00253 
00254 static int info_textview_main__internal(struct SpaceInfo *sinfo, struct ARegion *ar, ReportList *reports, int draw, int mval[2], void **mouse_pick, int *pos_pick)
00255 {
00256         int ret= 0;
00257         
00258         View2D *v2d= &ar->v2d;
00259 
00260         TextViewContext tvc= {0};
00261         tvc.begin= report_textview_begin;
00262         tvc.end= report_textview_end;
00263 
00264         tvc.step= report_textview_step;
00265         tvc.line_get= report_textview_line_get;
00266         tvc.line_color= report_textview_line_color;
00267 
00268         tvc.arg1= sinfo;
00269         tvc.arg2= reports;
00270 
00271         /* view */
00272         tvc.sel_start= 0;
00273         tvc.sel_end= 0;
00274         tvc.lheight= 14; //sc->lheight;
00275         tvc.ymin= v2d->cur.ymin;
00276         tvc.ymax= v2d->cur.ymax;
00277         tvc.winx= ar->winx;
00278 
00279         ret= textview_draw(&tvc, draw, mval, mouse_pick, pos_pick);
00280         
00281         return ret;
00282 }
00283 
00284 void *info_text_pick(struct SpaceInfo *sinfo, struct ARegion *ar, ReportList *reports, int mouse_y)
00285 {
00286         void *mouse_pick= NULL;
00287         int mval[2];
00288 
00289         mval[0]= 0;
00290         mval[1]= mouse_y;
00291 
00292         info_textview_main__internal(sinfo, ar, reports, 0, mval, &mouse_pick, NULL);
00293         return (void *)mouse_pick;
00294 }
00295 
00296 
00297 int info_textview_height(struct SpaceInfo *sinfo, struct ARegion *ar, ReportList *reports)
00298 {
00299         int mval[2] = {INT_MAX, INT_MAX};
00300         return info_textview_main__internal(sinfo, ar, reports, 0,  mval, NULL, NULL);
00301 }
00302 
00303 void info_textview_main(struct SpaceInfo *sinfo, struct ARegion *ar, ReportList *reports)
00304 {
00305         int mval[2] = {INT_MAX, INT_MAX};
00306         info_textview_main__internal(sinfo, ar, reports, 1,  mval, NULL, NULL);
00307 }