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