doxygen: blender/editors tagged.
[blender.git] / source / blender / editors / space_info / info_draw.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2010 Blender Foundation.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_info/info_draw.c
29  *  \ingroup spinfo
30  */
31
32
33
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/stat.h>
38 #include <limits.h>
39
40 #include "BLF_api.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_utildefines.h"
44
45 #include "DNA_space_types.h"
46 #include "DNA_screen_types.h"
47
48 // #include "BKE_suggestions.h"
49 #include "BKE_report.h"
50
51
52 #include "MEM_guardedalloc.h"
53
54 #include "BIF_gl.h"
55 #include "BIF_glutil.h"
56
57 #include "ED_datafiles.h"
58 #include "ED_types.h"
59
60 #include "UI_resources.h"
61
62 #include "info_intern.h"
63 #include "../space_info/textview.h"
64
65 /* complicates things a bit, so leaving in old simple code */
66 #define USE_INFO_NEWLINE
67
68 static void info_report_color(unsigned char *fg, unsigned char *bg, Report *report, int bool)
69 {
70         if(report->flag & SELECT) {
71                 fg[0]=255; fg[1]=255; fg[2]=255;
72                 if(bool) {
73                         bg[0]=96; bg[1]=128; bg[2]=255;
74                 }
75                 else {
76                         bg[0]=90; bg[1]=122; bg[2]=249;
77                 }
78         }
79         else {
80                 fg[0]=0; fg[1]=0; fg[2]=0;
81                 
82                 if (report->type & RPT_ERROR_ALL) {
83                         if (bool) { bg[0]=220; bg[1]=0;   bg[2]=0;   }
84                         else      { bg[0]=214; bg[1]=0;   bg[2]=0;   }
85                 }
86                 else if (report->type & RPT_WARNING_ALL) {
87                         if (bool) { bg[0]=220; bg[1]=128; bg[2]=96;  }
88                         else      { bg[0]=214; bg[1]=122; bg[2]=90;  }
89                 }
90 #if 0 // XXX: this looks like the selected colour, so don't use this
91                 else if (report->type & RPT_OPERATOR_ALL) {
92                         if (bool) { bg[0]=96;  bg[1]=128; bg[2]=255; }
93                         else      { bg[0]=90;  bg[1]=122; bg[2]=249; }
94                 }
95 #endif
96                 else if (report->type & RPT_INFO_ALL) {
97                         if (bool) { bg[0]=0;   bg[1]=170; bg[2]=0;   }
98                         else      { bg[0]=0;   bg[1]=164; bg[2]=0;   }
99                 }
100                 else if (report->type & RPT_DEBUG_ALL) {
101                         if (bool) { bg[0]=196; bg[1]=196; bg[2]=196; }
102                         else      { bg[0]=190; bg[1]=190; bg[2]=190; }
103                 }
104                 else {
105                         if (bool) { bg[0]=120; bg[1]=120; bg[2]=120; }
106                         else      { bg[0]=114; bg[1]=114; bg[2]=114; }
107                 }
108         }
109 }
110
111 /* reports! */
112 #ifdef USE_INFO_NEWLINE
113 static void report_textview_init__internal(TextViewContext *tvc)
114 {
115         Report *report= (Report *)tvc->iter;
116         const char *str= report->message;
117         const char *next_str= strchr(str + tvc->iter_char, '\n');
118
119         if(next_str) {
120                 tvc->iter_char_next= (int)(next_str - str);
121         }
122         else {
123                 tvc->iter_char_next= report->len;
124         }
125 }
126
127 static int report_textview_skip__internal(TextViewContext *tvc)
128 {
129         SpaceInfo *sinfo= (SpaceInfo *)tvc->arg1;
130         const int report_mask= info_report_mask(sinfo);
131         while (tvc->iter && (((Report *)tvc->iter)->type & report_mask)==0) {
132                 tvc->iter= (void *)((Link *)tvc->iter)->prev;
133         }
134         return (tvc->iter != NULL);
135 }
136
137 #endif // USE_INFO_NEWLINE
138
139 static int report_textview_begin(TextViewContext *tvc)
140 {
141         // SpaceConsole *sc= (SpaceConsole *)tvc->arg1;
142         ReportList *reports= (ReportList *)tvc->arg2;
143
144         tvc->lheight= 14; //sc->lheight;
145         tvc->sel_start= 0;
146         tvc->sel_end= 0;
147
148         /* iterator */
149         tvc->iter= reports->list.last;
150
151         glClearColor(120.0/255.0, 120.0/255.0, 120.0/255.0, 1.0);
152         glClear(GL_COLOR_BUFFER_BIT);
153
154 #ifdef USE_INFO_NEWLINE
155         tvc->iter_tmp= 0;
156         if(tvc->iter && report_textview_skip__internal(tvc)) {
157                 /* init the newline iterator */
158                 tvc->iter_char= 0;
159                 report_textview_init__internal(tvc);
160
161                 return TRUE;
162         }
163         else {
164                 return FALSE;
165         }
166 #else
167         return (tvc->iter != NULL);
168 #endif
169 }
170
171 static void report_textview_end(TextViewContext *UNUSED(tvc))
172 {
173         /* pass */
174 }
175
176 #ifdef USE_INFO_NEWLINE
177 static int report_textview_step(TextViewContext *tvc)
178 {
179         /* simple case, but no newline support */
180         Report *report= (Report *)tvc->iter;
181
182         if(report->len <= tvc->iter_char_next) {
183                 tvc->iter= (void *)((Link *)tvc->iter)->prev;
184                 if(tvc->iter && report_textview_skip__internal(tvc)) {
185                         tvc->iter_tmp++;
186
187                         tvc->iter_char= 0; /* reset start */
188                         report_textview_init__internal(tvc);
189
190                         return TRUE;
191                 }
192                 else {
193                         return FALSE;
194                 }
195         }
196         else {
197                 /* step to the next newline */
198                 tvc->iter_char= tvc->iter_char_next + 1;
199                 report_textview_init__internal(tvc);
200
201                 return TRUE;
202         }
203 }
204
205 static int report_textview_line_get(struct TextViewContext *tvc, const char **line, int *len)
206 {
207         Report *report= (Report *)tvc->iter;
208         *line= report->message + tvc->iter_char;
209         *len= tvc->iter_char_next - tvc->iter_char;
210         return 1;
211 }
212
213 static int report_textview_line_color(struct TextViewContext *tvc, unsigned char fg[3], unsigned char bg[3])
214 {
215         Report *report= (Report *)tvc->iter;
216         info_report_color(fg, bg, report, tvc->iter_tmp % 2);
217         return TVC_LINE_FG | TVC_LINE_BG;
218 }
219
220
221 #else // USE_INFO_NEWLINE
222
223 static int report_textview_step(TextViewContext *tvc)
224 {
225         SpaceInfo *sinfo= (SpaceInfo *)tvc->arg1;
226         const int report_mask= info_report_mask(sinfo);
227         do {
228                 tvc->iter= (void *)((Link *)tvc->iter)->prev;
229         } while (tvc->iter && (((Report *)tvc->iter)->type & report_mask)==0);
230
231         return (tvc->iter != NULL);
232 }
233
234 static int report_textview_line_get(struct TextViewContext *tvc, const char **line, int *len)
235 {
236         Report *report= (Report *)tvc->iter;
237         *line= report->message;
238         *len= report->len;
239
240         return 1;
241 }
242
243 static int report_textview_line_color(struct TextViewContext *tvc, unsigned char fg[3], unsigned char bg[3])
244 {
245         Report *report= (Report *)tvc->iter;
246         info_report_color(fg, bg, report, tvc->iter_tmp % 2);
247         return TVC_LINE_FG | TVC_LINE_BG;
248 }
249
250 #endif // USE_INFO_NEWLINE
251
252 #undef USE_INFO_NEWLINE
253
254 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)
255 {
256         int ret= 0;
257         
258         View2D *v2d= &ar->v2d;
259
260         TextViewContext tvc= {0};
261         tvc.begin= report_textview_begin;
262         tvc.end= report_textview_end;
263
264         tvc.step= report_textview_step;
265         tvc.line_get= report_textview_line_get;
266         tvc.line_color= report_textview_line_color;
267
268         tvc.arg1= sinfo;
269         tvc.arg2= reports;
270
271         /* view */
272         tvc.sel_start= 0;
273         tvc.sel_end= 0;
274         tvc.lheight= 14; //sc->lheight;
275         tvc.ymin= v2d->cur.ymin;
276         tvc.ymax= v2d->cur.ymax;
277         tvc.winx= ar->winx;
278
279         ret= textview_draw(&tvc, draw, mval, mouse_pick, pos_pick);
280         
281         return ret;
282 }
283
284 void *info_text_pick(struct SpaceInfo *sinfo, struct ARegion *ar, ReportList *reports, int mouse_y)
285 {
286         void *mouse_pick= NULL;
287         int mval[2];
288
289         mval[0]= 0;
290         mval[1]= mouse_y;
291
292         info_textview_main__internal(sinfo, ar, reports, 0, mval, &mouse_pick, NULL);
293         return (void *)mouse_pick;
294 }
295
296
297 int info_textview_height(struct SpaceInfo *sinfo, struct ARegion *ar, ReportList *reports)
298 {
299         int mval[2] = {INT_MAX, INT_MAX};
300         return info_textview_main__internal(sinfo, ar, reports, 0,  mval, NULL, NULL);
301 }
302
303 void info_textview_main(struct SpaceInfo *sinfo, struct ARegion *ar, ReportList *reports)
304 {
305         int mval[2] = {INT_MAX, INT_MAX};
306         info_textview_main__internal(sinfo, ar, reports, 1,  mval, NULL, NULL);
307 }