doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / editors / space_console / console_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  * Contributor(s): Campbell Barton
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include <math.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <limits.h>
30
31 #include "BLF_api.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_utildefines.h"
35
36 #include "DNA_space_types.h"
37 #include "DNA_screen_types.h"
38
39 #include "BKE_report.h"
40
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BIF_gl.h"
45 #include "BIF_glutil.h"
46
47 #include "ED_datafiles.h"
48 #include "ED_types.h"
49
50 #include "UI_resources.h"
51
52 #include "console_intern.h"
53
54
55 #include "../space_info/textview.h"
56
57 static void console_line_color(unsigned char fg[3], int type)
58 {
59         switch(type) {
60         case CONSOLE_LINE_OUTPUT:
61                 UI_GetThemeColor3ubv(TH_CONSOLE_OUTPUT, fg);
62                 break;
63         case CONSOLE_LINE_INPUT:
64                 UI_GetThemeColor3ubv(TH_CONSOLE_INPUT, fg);
65                 break;
66         case CONSOLE_LINE_INFO:
67                 UI_GetThemeColor3ubv(TH_CONSOLE_INFO, fg);
68                 break;
69         case CONSOLE_LINE_ERROR:
70                 UI_GetThemeColor3ubv(TH_CONSOLE_ERROR, fg);
71                 break;
72         }
73 }
74
75 typedef struct ConsoleDrawContext {
76         int cwidth;
77         int lheight;
78         int console_width;
79         int winx;
80         int ymin, ymax;
81         int *xy; // [2]
82         int *sel; // [2]
83         int *pos_pick; // bottom of view == 0, top of file == combine chars, end of line is lower then start. 
84         int *mval; // [2]
85         int draw;
86 } ConsoleDrawContext;
87
88 void console_scrollback_prompt_begin(struct SpaceConsole *sc, ConsoleLine *cl_dummy)
89 {
90         /* fake the edit line being in the scroll buffer */
91         ConsoleLine *cl= sc->history.last;
92         cl_dummy->type= CONSOLE_LINE_INPUT;
93         cl_dummy->len= cl_dummy->len_alloc= strlen(sc->prompt) + cl->len;
94         cl_dummy->len_alloc= cl_dummy->len + 1;
95         cl_dummy->line= MEM_mallocN(cl_dummy->len_alloc, "cl_dummy");
96         memcpy(cl_dummy->line, sc->prompt, (cl_dummy->len_alloc - cl->len));
97         memcpy(cl_dummy->line + ((cl_dummy->len_alloc - cl->len)) - 1, cl->line, cl->len + 1);
98         BLI_addtail(&sc->scrollback, cl_dummy);
99 }
100 void console_scrollback_prompt_end(struct SpaceConsole *sc, ConsoleLine *cl_dummy) 
101 {
102         MEM_freeN(cl_dummy->line);
103         BLI_remlink(&sc->scrollback, cl_dummy);
104 }
105
106 #define CONSOLE_DRAW_MARGIN 4
107 #define CONSOLE_DRAW_SCROLL 16
108
109
110
111 /* console textview callbacks */
112 static int console_textview_begin(TextViewContext *tvc)
113 {
114         SpaceConsole *sc= (SpaceConsole *)tvc->arg1;
115         tvc->lheight= sc->lheight;
116         tvc->sel_start= sc->sel_start;
117         tvc->sel_end= sc->sel_end;
118         
119         /* iterator */
120         tvc->iter= sc->scrollback.last;
121         
122         return (tvc->iter != NULL);
123 }
124
125 static void console_textview_end(TextViewContext *tvc)
126 {
127         SpaceConsole *sc= (SpaceConsole *)tvc->arg1;
128         (void)sc;
129         
130 }
131
132 static int console_textview_step(TextViewContext *tvc)
133 {
134         return ((tvc->iter= (void *)((Link *)tvc->iter)->prev) != NULL);
135 }
136
137 static int console_textview_line_get(struct TextViewContext *tvc, const char **line, int *len)
138 {
139         ConsoleLine *cl= (ConsoleLine *)tvc->iter;
140         *line= cl->line;
141         *len= cl->len;
142
143         return 1;
144 }
145
146 static int console_textview_line_color(struct TextViewContext *tvc, unsigned char fg[3], unsigned char UNUSED(bg[3]))
147 {
148         ConsoleLine *cl_iter= (ConsoleLine *)tvc->iter;
149
150         /* annoying hack, to draw the prompt */
151         if(tvc->iter_index == 0) {
152                 const SpaceConsole *sc= (SpaceConsole *)tvc->arg1;
153                 const ConsoleLine *cl= (ConsoleLine *)sc->history.last;
154                 const int prompt_len= strlen(sc->prompt);
155                 const int cursor_loc= cl->cursor + prompt_len;
156                 int xy[2] = {CONSOLE_DRAW_MARGIN, CONSOLE_DRAW_MARGIN};
157                 int pen[2];
158                 xy[1] += tvc->lheight/6;
159
160                 /* account for wrapping */
161                 if(cl->len < tvc->console_width) {
162                         /* simple case, no wrapping */
163                         pen[0]= tvc->cwidth * cursor_loc;
164                         pen[1]= -2;
165                 }
166                 else {
167                         /* wrap */
168                         pen[0]= tvc->cwidth * (cursor_loc % tvc->console_width);
169                         pen[1]= -2 + (((cl->len / tvc->console_width) - (cursor_loc / tvc->console_width)) * tvc->lheight);
170                 }
171
172                 /* cursor */
173                 UI_GetThemeColor3ubv(TH_CONSOLE_CURSOR, fg);
174                 glColor3ubv(fg);
175
176                 glRecti(        (xy[0] + pen[0]) - 1,
177                                         (xy[1] + pen[1]),
178                                         (xy[0] + pen[0]) + 1,
179                                         (xy[1] + pen[1] + tvc->lheight)
180                 );
181         }
182
183         console_line_color(fg, cl_iter->type);
184
185         return TVC_LINE_FG;
186 }
187
188
189 static int console_textview_main__internal(struct SpaceConsole *sc, struct ARegion *ar, int draw, int mval[2], void **mouse_pick, int *pos_pick)
190 {
191         ConsoleLine cl_dummy= {NULL};
192         int ret= 0;
193         
194         View2D *v2d= &ar->v2d;
195
196         TextViewContext tvc= {0};
197
198         tvc.begin= console_textview_begin;
199         tvc.end= console_textview_end;
200
201         tvc.step= console_textview_step;
202         tvc.line_get= console_textview_line_get;
203         tvc.line_color= console_textview_line_color;
204
205         tvc.arg1= sc;
206         tvc.arg2= NULL;
207
208         /* view */
209         tvc.sel_start= sc->sel_start;
210         tvc.sel_end= sc->sel_end;
211         tvc.lheight= sc->lheight;
212         tvc.ymin= v2d->cur.ymin;
213         tvc.ymax= v2d->cur.ymax;
214         tvc.winx= ar->winx;
215
216         console_scrollback_prompt_begin(sc, &cl_dummy);
217         ret= textview_draw(&tvc, draw, mval, mouse_pick, pos_pick);
218         console_scrollback_prompt_end(sc, &cl_dummy);
219
220         return ret;
221 }
222
223
224 void console_textview_main(struct SpaceConsole *sc, struct ARegion *ar)
225 {
226         int mval[2] = {INT_MAX, INT_MAX};
227         console_textview_main__internal(sc, ar, 1,  mval, NULL, NULL);
228 }
229
230 int console_textview_height(struct SpaceConsole *sc, struct ARegion *ar)
231 {
232         int mval[2] = {INT_MAX, INT_MAX};
233         return console_textview_main__internal(sc, ar, 0,  mval, NULL, NULL);
234 }
235
236 int console_char_pick(struct SpaceConsole *sc, struct ARegion *ar, int mval[2])
237 {
238         int pos_pick= 0;
239         void *mouse_pick= NULL;
240         int mval_clamp[2];
241
242         mval_clamp[0]= CLAMPIS(mval[0], CONSOLE_DRAW_MARGIN, ar->winx-(CONSOLE_DRAW_SCROLL + CONSOLE_DRAW_MARGIN));
243         mval_clamp[1]= CLAMPIS(mval[1], CONSOLE_DRAW_MARGIN, ar->winy-CONSOLE_DRAW_MARGIN);
244
245         console_textview_main__internal(sc, ar, 0, mval_clamp, &mouse_pick, &pos_pick);
246         return pos_pick;
247 }