e2895109b4874e24914fac5335566db366886ae7
[blender-staging.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 #include <math.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <limits.h>
35
36
37 #include "BLI_utildefines.h"
38
39 #include "DNA_space_types.h"
40 #include "DNA_screen_types.h"
41
42 // #include "BKE_suggestions.h"
43 #include "BKE_report.h"
44
45 #include "BIF_gl.h"
46
47
48 #include "UI_resources.h"
49 #include "UI_interface.h"
50 #include "UI_view2d.h"
51
52 #include "info_intern.h"
53 #include "../space_info/textview.h"
54
55 /* complicates things a bit, so leaving in old simple code */
56 #define USE_INFO_NEWLINE
57
58 static void info_report_color(unsigned char *fg, unsigned char *bg, Report *report, const short do_tint)
59 {
60         int bg_id = TH_BACK, fg_id = TH_TEXT;
61         int shade = do_tint ? 0 : -6;
62
63         if (report->flag & SELECT) {
64                 bg_id = TH_INFO_SELECTED;
65                 fg_id = TH_INFO_SELECTED_TEXT;
66         }
67         else if (report->type & RPT_ERROR_ALL) {
68                 bg_id = TH_INFO_ERROR;
69                 fg_id = TH_INFO_ERROR_TEXT;
70         }
71         else if (report->type & RPT_WARNING_ALL) {
72                 bg_id = TH_INFO_WARNING;
73                 fg_id = TH_INFO_WARNING_TEXT;
74         }
75         else if (report->type & RPT_INFO_ALL) {
76                 bg_id = TH_INFO_INFO;
77                 fg_id = TH_INFO_INFO_TEXT;
78         }
79         else if (report->type & RPT_DEBUG_ALL) {
80                 bg_id = TH_INFO_DEBUG;
81                 fg_id = TH_INFO_DEBUG_TEXT;
82         }
83         else {
84                 bg_id = TH_BACK;
85                 fg_id = TH_TEXT;
86         }
87
88         UI_GetThemeColorShade3ubv(bg_id, shade, bg);
89         UI_GetThemeColor3ubv(fg_id, fg);
90 }
91
92 /* reports! */
93 #ifdef USE_INFO_NEWLINE
94 static void report_textview_init__internal(TextViewContext *tvc)
95 {
96         Report *report = (Report *)tvc->iter;
97         const char *str = report->message;
98         const char *next_str = strchr(str + tvc->iter_char, '\n');
99
100         if (next_str) {
101                 tvc->iter_char_next = (int)(next_str - str);
102         }
103         else {
104                 tvc->iter_char_next = report->len;
105         }
106 }
107
108 static int report_textview_skip__internal(TextViewContext *tvc)
109 {
110         SpaceInfo *sinfo = (SpaceInfo *)tvc->arg1;
111         const int report_mask = info_report_mask(sinfo);
112         while (tvc->iter && (((Report *)tvc->iter)->type & report_mask) == 0) {
113                 tvc->iter = (void *)((Link *)tvc->iter)->prev;
114         }
115         return (tvc->iter != NULL);
116 }
117
118 #endif // USE_INFO_NEWLINE
119
120 static int report_textview_begin(TextViewContext *tvc)
121 {
122         // SpaceConsole *sc = (SpaceConsole *)tvc->arg1;
123         ReportList *reports = (ReportList *)tvc->arg2;
124
125         tvc->lheight = 14 * UI_DPI_FAC; //sc->lheight;
126         tvc->sel_start = 0;
127         tvc->sel_end = 0;
128
129         /* iterator */
130         tvc->iter = reports->list.last;
131
132         UI_ThemeClearColor(TH_BACK);
133         glClear(GL_COLOR_BUFFER_BIT);
134
135 #ifdef USE_INFO_NEWLINE
136         tvc->iter_tmp = 0;
137         if (tvc->iter && report_textview_skip__internal(tvc)) {
138                 /* init the newline iterator */
139                 tvc->iter_char = 0;
140                 report_textview_init__internal(tvc);
141
142                 return true;
143         }
144         else {
145                 return false;
146         }
147 #else
148         return (tvc->iter != NULL);
149 #endif
150 }
151
152 static void report_textview_end(TextViewContext *UNUSED(tvc))
153 {
154         /* pass */
155 }
156
157 #ifdef USE_INFO_NEWLINE
158 static int report_textview_step(TextViewContext *tvc)
159 {
160         /* simple case, but no newline support */
161         Report *report = (Report *)tvc->iter;
162
163         if (report->len <= tvc->iter_char_next) {
164                 tvc->iter = (void *)((Link *)tvc->iter)->prev;
165                 if (tvc->iter && report_textview_skip__internal(tvc)) {
166                         tvc->iter_tmp++;
167
168                         tvc->iter_char = 0; /* reset start */
169                         report_textview_init__internal(tvc);
170
171                         return true;
172                 }
173                 else {
174                         return false;
175                 }
176         }
177         else {
178                 /* step to the next newline */
179                 tvc->iter_char = tvc->iter_char_next + 1;
180                 report_textview_init__internal(tvc);
181
182                 return true;
183         }
184 }
185
186 static int report_textview_line_get(struct TextViewContext *tvc, const char **line, int *len)
187 {
188         Report *report = (Report *)tvc->iter;
189         *line = report->message + tvc->iter_char;
190         *len = tvc->iter_char_next - tvc->iter_char;
191         return 1;
192 }
193
194 static int report_textview_line_color(struct TextViewContext *tvc, unsigned char fg[3], unsigned char bg[3])
195 {
196         Report *report = (Report *)tvc->iter;
197         info_report_color(fg, bg, report, tvc->iter_tmp % 2);
198         return TVC_LINE_FG | TVC_LINE_BG;
199 }
200
201
202 #else // USE_INFO_NEWLINE
203
204 static int report_textview_step(TextViewContext *tvc)
205 {
206         SpaceInfo *sinfo = (SpaceInfo *)tvc->arg1;
207         const int report_mask = info_report_mask(sinfo);
208         do {
209                 tvc->iter = (void *)((Link *)tvc->iter)->prev;
210         } while (tvc->iter && (((Report *)tvc->iter)->type & report_mask) == 0);
211
212         return (tvc->iter != NULL);
213 }
214
215 static int report_textview_line_get(struct TextViewContext *tvc, const char **line, int *len)
216 {
217         Report *report = (Report *)tvc->iter;
218         *line = report->message;
219         *len = report->len;
220
221         return 1;
222 }
223
224 static int report_textview_line_color(struct TextViewContext *tvc, unsigned char fg[3], unsigned char bg[3])
225 {
226         Report *report = (Report *)tvc->iter;
227         info_report_color(fg, bg, report, tvc->iter_tmp % 2);
228         return TVC_LINE_FG | TVC_LINE_BG;
229 }
230
231 #endif // USE_INFO_NEWLINE
232
233 #undef USE_INFO_NEWLINE
234
235 static int info_textview_main__internal(struct SpaceInfo *sinfo, ARegion *ar, ReportList *reports,
236                                         int draw, int mval[2], void **mouse_pick, int *pos_pick)
237 {
238         int ret = 0;
239         
240         View2D *v2d = &ar->v2d;
241
242         TextViewContext tvc = {0};
243         tvc.begin = report_textview_begin;
244         tvc.end = report_textview_end;
245
246         tvc.step = report_textview_step;
247         tvc.line_get = report_textview_line_get;
248         tvc.line_color = report_textview_line_color;
249         tvc.const_colors = NULL;
250
251         tvc.arg1 = sinfo;
252         tvc.arg2 = reports;
253
254         /* view */
255         tvc.sel_start = 0;
256         tvc.sel_end = 0;
257         tvc.lheight = 14 * UI_DPI_FAC; //sc->lheight;
258         tvc.ymin = v2d->cur.ymin;
259         tvc.ymax = v2d->cur.ymax;
260         tvc.winx = ar->winx - V2D_SCROLL_WIDTH;
261
262         ret = textview_draw(&tvc, draw, mval, mouse_pick, pos_pick);
263         
264         return ret;
265 }
266
267 void *info_text_pick(struct SpaceInfo *sinfo, ARegion *ar, ReportList *reports, int mouse_y)
268 {
269         void *mouse_pick = NULL;
270         int mval[2];
271
272         mval[0] = 0;
273         mval[1] = mouse_y;
274
275         info_textview_main__internal(sinfo, ar, reports, 0, mval, &mouse_pick, NULL);
276         return (void *)mouse_pick;
277 }
278
279
280 int info_textview_height(struct SpaceInfo *sinfo, ARegion *ar, ReportList *reports)
281 {
282         int mval[2] = {INT_MAX, INT_MAX};
283         return info_textview_main__internal(sinfo, ar, reports, 0,  mval, NULL, NULL);
284 }
285
286 void info_textview_main(struct SpaceInfo *sinfo, ARegion *ar, ReportList *reports)
287 {
288         int mval[2] = {INT_MAX, INT_MAX};
289         info_textview_main__internal(sinfo, ar, reports, 1,  mval, NULL, NULL);
290 }