Merged from trunk with revision range 36835-37865,
[blender.git] / source / blender / editors / space_text / text_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  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_text/text_draw.c
30  *  \ingroup sptext
31  */
32
33
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/stat.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "BLF_api.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_utildefines.h"
45
46 #include "DNA_text_types.h"
47 #include "DNA_space_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_userdef_types.h"
50
51 #include "BKE_context.h"
52 #include "BKE_suggestions.h"
53 #include "BKE_text.h"
54
55
56 #include "BIF_gl.h"
57
58 #include "ED_datafiles.h"
59 #include "UI_interface.h"
60 #include "UI_resources.h"
61
62 #include "text_intern.h"
63
64 /******************** text font drawing ******************/
65 // XXX, fixme
66 #define mono blf_mono_font
67
68 static void text_font_begin(SpaceText *st)
69 {
70         BLF_size(mono, st->lheight, 72);
71 }
72
73 static void text_font_end(SpaceText *UNUSED(st))
74 {
75 }
76
77 static int text_font_draw(SpaceText *UNUSED(st), int x, int y, char *str)
78 {
79         BLF_position(mono, x, y, 0);
80         BLF_draw(mono, str, 65535); /* XXX, use real length */
81
82         return BLF_width(mono, str);
83 }
84
85 static int text_font_draw_character(SpaceText *st, int x, int y, char c)
86 {
87         char str[2];
88         str[0]= c;
89         str[1]= '\0';
90
91         BLF_position(mono, x, y, 0);
92         BLF_draw(mono, str, 1);
93
94         return st->cwidth;
95 }
96
97 int text_font_width(SpaceText *UNUSED(st), const char *str)
98 {
99         return BLF_width(mono, str);
100 }
101
102 /****************** flatten string **********************/
103
104 static void flatten_string_append(FlattenString *fs, char c, int accum) 
105 {
106         if(fs->pos>=fs->len && fs->pos>=sizeof(fs->fixedbuf)-1) {
107                 char *nbuf; int *naccum;
108                 if(fs->len) fs->len*= 2;
109                 else fs->len= sizeof(fs->fixedbuf) * 2;
110
111                 nbuf= MEM_callocN(sizeof(*fs->buf)*fs->len, "fs->buf");
112                 naccum= MEM_callocN(sizeof(*fs->accum)*fs->len, "fs->accum");
113
114                 memcpy(nbuf, fs->buf, fs->pos * sizeof(*fs->buf));
115                 memcpy(naccum, fs->accum, fs->pos * sizeof(*fs->accum));
116                 
117                 if(fs->buf != fs->fixedbuf) {
118                         MEM_freeN(fs->buf);
119                         MEM_freeN(fs->accum);
120                 }
121                 
122                 fs->buf= nbuf;
123                 fs->accum= naccum;
124         }
125         
126         fs->buf[fs->pos]= c;    
127         fs->accum[fs->pos]= accum;
128         
129         fs->pos++;
130 }
131
132 int flatten_string(SpaceText *st, FlattenString *fs, const char *in)
133 {
134         int r = 0, i = 0;
135
136         memset(fs, 0, sizeof(FlattenString));
137         fs->buf= fs->fixedbuf;
138         fs->accum= fs->fixedaccum;
139         
140         for(r=0, i=0; *in; r++, in++) {
141                 if(*in=='\t') {
142                         if(fs->pos && *(in-1)=='\t')
143                                 i= st->tabnumber;
144                         else if(st->tabnumber > 0)
145                                 i= st->tabnumber - (fs->pos%st->tabnumber);
146
147                         while(i--)
148                                 flatten_string_append(fs, ' ', r);
149                 }
150                 else
151                         flatten_string_append(fs, *in, r);
152         }
153
154         return fs->pos;
155 }
156
157 void flatten_string_free(FlattenString *fs)
158 {
159         if(fs->buf != fs->fixedbuf)
160                 MEM_freeN(fs->buf);
161         if(fs->accum != fs->fixedaccum)
162                 MEM_freeN(fs->accum);
163 }
164
165 /* Checks the specified source string for a Python built-in function name. This
166  name must start at the beginning of the source string and must be followed by
167  a non-identifier (see text_check_identifier(char)) or null character.
168  
169  If a built-in function is found, the length of the matching name is returned.
170  Otherwise, -1 is returned. */
171
172 static int find_builtinfunc(char *string)
173 {
174         int a, i;
175         char builtinfuncs[][9] = {"and", "as", "assert", "break", "class", "continue", "def",
176                                                                 "del", "elif", "else", "except", "exec", "finally",
177                                                                 "for", "from", "global", "if", "import", "in",
178                                                                 "is", "lambda", "not", "or", "pass", "print",
179                                                                 "raise", "return", "try", "while", "yield", "with"};
180
181         for(a=0; a < sizeof(builtinfuncs)/sizeof(builtinfuncs[0]); a++) {
182                 i = 0;
183                 while(1) {
184                         /* If we hit the end of a keyword... (eg. "def") */
185                         if(builtinfuncs[a][i]=='\0') {
186                                 /* If we still have identifier chars in the source (eg. "definate") */
187                                 if(text_check_identifier(string[i]))
188                                         i = -1; /* No match */
189                                 break; /* Next keyword if no match, otherwise we're done */
190                                 
191                         /* If chars mismatch, move on to next keyword */
192                         }
193                         else if(string[i]!=builtinfuncs[a][i]) {
194                                 i = -1;
195                                 break; /* Break inner loop, start next keyword */
196                         }
197                         i++;
198                 }
199                 if(i>0) break; /* If we have a match, we're done */
200         }
201         return i;
202 }
203
204 /* Checks the specified source string for a Python special name. This name must
205  start at the beginning of the source string and must be followed by a non-
206  identifier (see text_check_identifier(char)) or null character.
207  
208  If a special name is found, the length of the matching name is returned.
209  Otherwise, -1 is returned. */
210
211 static int find_specialvar(char *string) 
212 {
213         int i = 0;
214         /* Check for "def" */
215         if(string[0]=='d' && string[1]=='e' && string[2]=='f')
216                 i = 3;
217         /* Check for "class" */
218         else if(string[0]=='c' && string[1]=='l' && string[2]=='a' && string[3]=='s' && string[4]=='s')
219                 i = 5;
220         /* If next source char is an identifier (eg. 'i' in "definate") no match */
221         if(i==0 || text_check_identifier(string[i]))
222                 return -1;
223         return i;
224 }
225
226 static int find_decorator(char *string) 
227 {
228         if(string[0] == '@') {
229                 int i = 1;
230                 while(text_check_identifier(string[i])) {
231                         i++;
232                 }
233                 return i;
234         }
235         return -1;
236 }
237
238 static int find_bool(char *string) 
239 {
240         int i = 0;
241         /* Check for "False" */
242         if(string[0]=='F' && string[1]=='a' && string[2]=='l' && string[3]=='s' && string[4]=='e')
243                 i = 5;
244         /* Check for "True" */
245         else if(string[0]=='T' && string[1]=='r' && string[2]=='u' && string[3]=='e')
246                 i = 4;
247         /* Check for "None" */
248         else if(string[0]=='N' && string[1]=='o' && string[2]=='n' && string[3]=='e')
249                 i = 4;
250         /* If next source char is an identifier (eg. 'i' in "definate") no match */
251         if(i==0 || text_check_identifier(string[i]))
252                 return -1;
253         return i;
254 }
255
256 /* Ensures the format string for the given line is long enough, reallocating
257  as needed. Allocation is done here, alone, to ensure consistency. */
258 static int text_check_format_len(TextLine *line, unsigned int len)
259 {
260         if(line->format) {
261                 if(strlen(line->format) < len) {
262                         MEM_freeN(line->format);
263                         line->format = MEM_mallocN(len+2, "SyntaxFormat");
264                         if(!line->format) return 0;
265                 }
266         }
267         else {
268                 line->format = MEM_mallocN(len+2, "SyntaxFormat");
269                 if(!line->format) return 0;
270         }
271
272         return 1;
273 }
274
275 /* Formats the specified line. If do_next is set, the process will move on to
276  the succeeding line if it is affected (eg. multiline strings). Format strings
277  may contain any of the following characters:
278          '_'            Whitespace
279          '#'            Comment text
280          '!'            Punctuation and other symbols
281          'n'            Numerals
282          'l'            String letters
283          'v'            Special variables (class, def)
284          'b'            Built-in names (print, for, etc.)
285          'q'            Other text (identifiers, etc.)
286  It is terminated with a null-terminator '\0' followed by a continuation
287  flag indicating whether the line is part of a multi-line string. */
288
289 static void txt_format_line(SpaceText *st, TextLine *line, int do_next)
290 {
291         FlattenString fs;
292         char *str, *fmt, orig, cont, find, prev = ' ';
293         int len, i;
294
295         /* Get continuation from previous line */
296         if(line->prev && line->prev->format != NULL) {
297                 fmt= line->prev->format;
298                 cont = fmt[strlen(fmt)+1]; /* Just after the null-terminator */
299         }
300         else cont = 0;
301
302         /* Get original continuation from this line */
303         if(line->format != NULL) {
304                 fmt= line->format;
305                 orig = fmt[strlen(fmt)+1]; /* Just after the null-terminator */
306         }
307         else orig = 0xFF;
308
309         flatten_string(st, &fs, line->line);
310         str = fs.buf;
311         len = strlen(str);
312         if(!text_check_format_len(line, len)) {
313                 flatten_string_free(&fs);
314                 return;
315         }
316         fmt = line->format;
317
318         while(*str) {
319                 /* Handle escape sequences by skipping both \ and next char */
320                 if(*str == '\\') {
321                         *fmt = prev; fmt++; str++;
322                         if(*str == '\0') break;
323                         *fmt = prev; fmt++; str++;
324                         continue;
325                 }
326                 /* Handle continuations */
327                 else if(cont) {
328                         /* Triple strings ("""...""" or '''...''') */
329                         if(cont & TXT_TRISTR) {
330                                 find = (cont & TXT_DBLQUOTSTR) ? '"' : '\'';
331                                 if(*str==find && *(str+1)==find && *(str+2)==find) {
332                                         *fmt = 'l'; fmt++; str++;
333                                         *fmt = 'l'; fmt++; str++;
334                                         cont = 0;
335                                 }
336                         /* Handle other strings */
337                         }
338                         else {
339                                 find = (cont & TXT_DBLQUOTSTR) ? '"' : '\'';
340                                 if(*str == find) cont = 0;
341                         }
342
343                         *fmt = 'l';
344                 }
345                 /* Not in a string... */
346                 else {
347                         /* Deal with comments first */
348                         if(prev == '#' || *str == '#')
349                                 *fmt = '#';
350                         /* Strings */
351                         else if(*str == '"' || *str == '\'') {
352                                 find = *str;
353                                 cont = (*str== '"') ? TXT_DBLQUOTSTR : TXT_SNGQUOTSTR;
354                                 if(*(str+1) == find && *(str+2) == find) {
355                                         *fmt = 'l'; fmt++; str++;
356                                         *fmt = 'l'; fmt++; str++;
357                                         cont |= TXT_TRISTR;
358                                 }
359                                 *fmt = 'l';
360                         }
361                         /* Whitespace (all ws. has been converted to spaces) */
362                         else if(*str == ' ')
363                                 *fmt = '_';
364                         /* Numbers (digits not part of an identifier and periods followed by digits) */
365                         else if((prev != 'q' && text_check_digit(*str)) || (*str == '.' && text_check_digit(*(str+1))))
366                                 *fmt = 'n';
367                         /* Booleans */
368                         else if(prev != 'q' && (i=find_bool(str)) != -1)
369                                 if(i>0) {
370                                         while(i>1) {
371                                                 *fmt = 'n'; fmt++; str++;
372                                                 i--;
373                                         }
374                                         *fmt = 'n';
375                                 }
376                                 else
377                                         *fmt = 'q';
378                         /* Punctuation */
379                         else if(text_check_delim(*str))
380                                 *fmt = '!';
381                         /* Identifiers and other text (no previous ws. or delims. so text continues) */
382                         else if(prev == 'q')
383                                 *fmt = 'q';
384                         /* Not ws, a digit, punct, or continuing text. Must be new, check for special words */
385                         else {
386                                 /* Special vars(v) or built-in keywords(b) */
387                                 if((i=find_specialvar(str)) != -1)
388                                         prev = 'v';
389                                 else if((i=find_builtinfunc(str)) != -1)
390                                         prev = 'b';
391                                 else if((i=find_decorator(str)) != -1)
392                                         prev = 'v'; /* could have a new color for this */
393                                 if(i>0) {
394                                         while(i>1) {
395                                                 *fmt = prev; fmt++; str++;
396                                                 i--;
397                                         }
398                                         *fmt = prev;
399                                 }
400                                 else
401                                         *fmt = 'q';
402                         }
403                 }
404                 prev = *fmt;
405                 fmt++;
406                 str++;
407         }
408
409         /* Terminate and add continuation char */
410         *fmt = '\0'; fmt++;
411         *fmt = cont;
412
413         /* Debugging */
414         //print_format(st, line);
415
416         /* If continuation has changed and we're allowed, process the next line */
417         if(cont!=orig && do_next && line->next) {
418                 txt_format_line(st, line->next, do_next);
419         }
420
421         flatten_string_free(&fs);
422 }
423
424 #if 0
425 /* Formats every line of the current text */
426 static void txt_format_text(SpaceText *st) 
427 {
428         TextLine *linep;
429
430         if(!st->text) return;
431
432         for(linep=st->text->lines.first; linep; linep=linep->next)
433                 txt_format_line(st, linep, 0);
434 }
435 #endif
436
437 /* Sets the current drawing color based on the format character specified */
438 static void format_draw_color(char formatchar)
439 {
440         switch (formatchar) {
441                 case '_': /* Whitespace */
442                         break;
443                 case '!': /* Symbols */
444                         UI_ThemeColorBlend(TH_TEXT, TH_BACK, 0.5f);
445                         break;
446                 case '#': /* Comments */
447                         UI_ThemeColor(TH_SYNTAX_C);
448                         break;
449                 case 'n': /* Numerals */
450                         UI_ThemeColor(TH_SYNTAX_N);
451                         break;
452                 case 'l': /* Strings */
453                         UI_ThemeColor(TH_SYNTAX_L);
454                         break;
455                 case 'v': /* Specials: class, def */
456                         UI_ThemeColor(TH_SYNTAX_V);
457                         break;
458                 case 'b': /* Keywords: for, print, etc. */
459                         UI_ThemeColor(TH_SYNTAX_B);
460                         break;
461                 case 'q': /* Other text (identifiers) */
462                 default:
463                         UI_ThemeColor(TH_TEXT);
464                         break;
465         }
466 }
467
468 /************************** draw text *****************************/
469
470 /***********************/ /*
471
472 Notes on word-wrap
473 --
474 All word-wrap functions follow the algorithm below to maintain consistency.
475         line            The line to wrap (tabs converted to spaces)
476         view_width      The maximum number of characters displayable in the region
477                                 This equals region_width/font_width for the region
478         wrap_chars      Characters that allow wrapping. This equals [' ', '\t', '-']
479
480 def wrap(line, view_width, wrap_chars):
481         draw_start = 0
482         draw_end = view_width
483         pos = 0
484         for c in line:
485                 if pos-draw_start >= view_width:
486                         print line[draw_start:draw_end]
487                         draw_start = draw_end
488                         draw_end += view_width
489                 elif c in wrap_chars:
490                         draw_end = pos+1
491                 pos += 1
492         print line[draw_start:]
493
494 */ /***********************/
495
496 int wrap_width(SpaceText *st, ARegion *ar)
497 {
498         int winx= ar->winx - TXT_SCROLL_WIDTH;
499         int x, max;
500         
501         x= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;
502         max= st->cwidth ? (winx-x)/st->cwidth : 0;
503         return max>8 ? max : 8;
504 }
505
506 /* Sets (offl, offc) for transforming (line, curs) to its wrapped position */
507 void wrap_offset(SpaceText *st, ARegion *ar, TextLine *linein, int cursin, int *offl, int *offc)
508 {
509         Text *text;
510         TextLine *linep;
511         int i, j, start, end, chars, max, chop;
512         char ch;
513
514         *offl= *offc= 0;
515
516         if(!st->text) return;
517         if(!st->wordwrap) return;
518
519         text= st->text;
520
521         /* Move pointer to first visible line (top) */
522         linep= text->lines.first;
523         i= st->top;
524         while(i>0 && linep) {
525                 int lines= text_get_visible_lines(st, ar, linep->line);
526
527                 /* Line before top */
528                 if(linep == linein) {
529                         if(lines <= i)
530                                 /* no visible part of line */
531                                 return;
532                 }
533
534                 if (i-lines<0) {
535                         break;
536                 } else {
537                         linep= linep->next;
538                         (*offl)+= lines-1;
539                         i-= lines;
540                 }
541         }
542
543         max= wrap_width(st, ar);
544
545         while(linep) {
546                 start= 0;
547                 end= max;
548                 chop= 1;
549                 chars= 0;
550                 *offc= 0;
551                 for(i=0, j=0; linep->line[j]!='\0'; j++) {
552
553                         /* Mimic replacement of tabs */
554                         ch= linep->line[j];
555                         if(ch=='\t') {
556                                 chars= st->tabnumber-i%st->tabnumber;
557                                 if(linep==linein && i<cursin) cursin += chars-1;
558                                 ch= ' ';
559                         }
560                         else
561                                 chars= 1;
562
563                         while(chars--) {
564                                 if(i-start>=max) {
565                                         if(chop && linep==linein && i >= cursin) {
566                                                 if (i==cursin) {
567                                                         (*offl)++;
568                                                         *offc -= end-start;
569                                                 }
570
571                                                 return;
572                                         }
573
574                                         (*offl)++;
575                                         *offc -= end-start;
576
577                                         start= end;
578                                         end += max;
579                                         chop= 1;
580                                 }
581                                 else if(ch==' ' || ch=='-') {
582                                         end = i+1;
583                                         chop= 0;
584                                         if(linep==linein && i >= cursin)
585                                                 return;
586                                 }
587                                 i++;
588                         }
589                 }
590                 if(linep==linein) break;
591                 linep= linep->next;
592         }
593 }
594
595 void wrap_offset_in_line(SpaceText *st, ARegion *ar, TextLine *linein, int cursin, int *offl, int *offc)
596 {
597         int i, j, start, end, chars, max, chop;
598         char ch;
599
600         *offl= *offc= 0;
601
602         if(!st->text) return;
603         if(!st->wordwrap) return;
604
605         max= wrap_width(st, ar);
606
607         start= 0;
608         end= max;
609         chop= 1;
610         *offc= 0;
611
612         for(i=0, j=0; linein->line[j]!='\0'; j++) {
613
614                 /* Mimic replacement of tabs */
615                 ch= linein->line[j];
616                 if(ch=='\t') {
617                         chars= st->tabnumber-i%st->tabnumber;
618                         if(i<cursin) cursin += chars-1;
619                         ch= ' ';
620                 }
621                 else
622                         chars= 1;
623
624                 while(chars--) {
625                         if(i-start>=max) {
626                                 if(chop && i >= cursin) {
627                                         if (i==cursin) {
628                                                 (*offl)++;
629                                                 *offc -= end-start;
630                                         }
631
632                                         return;
633                                 }
634
635                                 (*offl)++;
636                                 *offc -= end-start;
637
638                                 start= end;
639                                 end += max;
640                                 chop= 1;
641                         }
642                         else if(ch==' ' || ch=='-') {
643                                 end = i+1;
644                                 chop= 0;
645                                 if(i >= cursin)
646                                         return;
647                         }
648                         i++;
649                 }
650         }
651 }
652
653 int text_get_char_pos(SpaceText *st, const char *line, int cur)
654 {
655         int a=0, i;
656         
657         for(i=0; i<cur && line[i]; i++) {
658                 if(line[i]=='\t')
659                         a += st->tabnumber-a%st->tabnumber;
660                 else
661                         a++;
662         }
663         return a;
664 }
665
666 static int text_draw_wrapped(SpaceText *st, char *str, int x, int y, int w, char *format, int skip)
667 {
668         FlattenString fs;
669         int basex, i, a, len, start, end, max, lines;
670         
671         len= flatten_string(st, &fs, str);
672         str= fs.buf;
673         max= w/st->cwidth;
674         if(max<8) max= 8;
675         basex= x;
676
677         lines= 1;
678         start= 0;
679         end= max;
680         for(i=0; i<len; i++) {
681                 if(i-start >= max) {
682                         /* skip hidden part of line */
683                         if(skip) {
684                                 skip--;
685                                 start= end;
686                                 end += max;
687                                 continue;
688                         }
689
690                         /* Draw the visible portion of text on the overshot line */
691                         for(a=start; a<end; a++) {
692                                 if(st->showsyntax && format) format_draw_color(format[a]);
693                                 x += text_font_draw_character(st, x, y, str[a]);
694                         }
695                         y -= st->lheight;
696                         x= basex;
697                         lines++;
698                         start= end;
699                         end += max;
700
701                         if(y<=0) break;
702                 }
703                 else if(str[i]==' ' || str[i]=='-') {
704                         end = i+1;
705                 }
706         }
707
708         /* Draw the remaining text */
709         for(a=start; a<len && y > 0; a++) {
710                 if(st->showsyntax && format)
711                         format_draw_color(format[a]);
712
713                 x += text_font_draw_character(st, x, y, str[a]);
714         }
715
716         flatten_string_free(&fs);
717
718         return lines;
719 }
720
721 static int text_draw(SpaceText *st, char *str, int cshift, int maxwidth, int draw, int x, int y, char *format)
722 {
723         FlattenString fs;
724         int r=0, w= 0, amount;
725         int *acc;
726         char *in;
727
728         w= flatten_string(st, &fs, str);
729         if(w < cshift) {
730                 flatten_string_free(&fs);
731                 return 0; /* String is shorter than shift */
732         }
733         
734         in= fs.buf+cshift;
735         acc= fs.accum+cshift;
736         w= w-cshift;
737
738         if(draw) {
739                 if(st->showsyntax && format) {
740                         int a;
741                         format = format+cshift;
742                 
743                         amount = strlen(in);
744                         if(maxwidth)
745                                 amount= MIN2(amount, maxwidth);
746                         
747                         for(a = 0; a < amount; a++) {
748                                 format_draw_color(format[a]);
749                                 x += text_font_draw_character(st, x, y, in[a]);
750                         }
751                 }
752                 else {
753                         amount = strlen(in);
754                         if(maxwidth)
755                                 amount= MIN2(amount, maxwidth);
756
757                         in[amount]= 0;
758                         text_font_draw(st, x, y, in);
759                 }
760         }
761         else {
762                 while(w-- && *acc++ < maxwidth)
763                         r+= st->cwidth;
764         }
765
766         flatten_string_free(&fs);
767
768         if(cshift && r==0)
769                 return 0;
770         else if(st->showlinenrs)
771                 return r+TXT_OFFSET+TEXTXLOC;
772         else
773                 return r+TXT_OFFSET;
774 }
775
776 /************************ cache utilities *****************************/
777
778 typedef struct DrawCache {
779         int *line_height;
780         int total_lines, nlines;
781
782         /* this is needed to check cache relevance */
783         int winx, wordwrap, showlinenrs, tabnumber;
784         short lheight;
785         char cwidth;
786         char text_id[MAX_ID_NAME];
787
788         /* for partial lines recalculation */
789         short update_flag;
790         int valid_head, valid_tail; /* amount of unchanged lines */
791 } DrawCache;
792
793 static void text_drawcache_init(SpaceText *st)
794 {
795         DrawCache *drawcache= MEM_callocN(sizeof (DrawCache), "text draw cache");
796
797         drawcache->winx= -1;
798         drawcache->nlines= BLI_countlist(&st->text->lines);
799         drawcache->text_id[0]= '\0';
800
801         st->drawcache= drawcache;
802 }
803
804 static void text_update_drawcache(SpaceText *st, ARegion *ar)
805 {
806         DrawCache *drawcache;
807         int full_update= 0, nlines= 0;
808         Text *txt= st->text;
809
810         if(!st->drawcache) text_drawcache_init(st);
811
812         text_update_character_width(st);
813
814         drawcache= (DrawCache *)st->drawcache;
815         nlines= drawcache->nlines;
816
817         /* check if full cache update is needed */
818         full_update|= drawcache->winx != ar->winx;                 /* area was resized */
819         full_update|= drawcache->wordwrap != st->wordwrap;         /* word-wrapping option was toggled */
820         full_update|= drawcache->showlinenrs != st->showlinenrs; /* word-wrapping option was toggled */
821         full_update|= drawcache->tabnumber != st->tabnumber;  /* word-wrapping option was toggled */
822         full_update|= drawcache->lheight != st->lheight;      /* word-wrapping option was toggled */
823         full_update|= drawcache->cwidth != st->cwidth;        /* word-wrapping option was toggled */
824         full_update|= strncmp(drawcache->text_id, txt->id.name, MAX_ID_NAME); /* text datablock was changed */
825
826         if(st->wordwrap) {
827                 /* update line heights */
828                 if(full_update || !drawcache->line_height) {
829                         drawcache->valid_head  = 0;
830                         drawcache->valid_tail  = 0;
831                         drawcache->update_flag = 1;
832                 }
833
834                 if(drawcache->update_flag) {
835                         TextLine *line= st->text->lines.first;
836                         int lineno= 0, size, lines_count;
837                         int *fp= drawcache->line_height, *new_tail, *old_tail;
838
839                         nlines= BLI_countlist(&txt->lines);
840                         size= sizeof(int)*nlines;
841
842                         if(fp) fp= MEM_reallocN(fp, size);
843                         else fp= MEM_callocN(size, "text drawcache line_height");
844
845                         drawcache->valid_tail= drawcache->valid_head= 0;
846                         old_tail= fp + drawcache->nlines - drawcache->valid_tail;
847                         new_tail= fp + nlines - drawcache->valid_tail;
848                         memmove(new_tail, old_tail, drawcache->valid_tail);
849
850                         drawcache->total_lines= 0;
851
852                         if(st->showlinenrs)
853                                 st->linenrs_tot= (int)floor(log10((float)nlines)) + 1;
854
855                         while(line) {
856                                 if(drawcache->valid_head) { /* we're inside valid head lines */
857                                         lines_count= fp[lineno];
858                                         drawcache->valid_head--;
859                                 } else if (lineno > new_tail - fp) {  /* we-re inside valid tail lines */
860                                         lines_count= fp[lineno];
861                                 } else {
862                                         lines_count= text_get_visible_lines(st, ar, line->line);
863                                 }
864
865                                 fp[lineno]= lines_count;
866
867                                 line= line->next;
868                                 lineno++;
869                                 drawcache->total_lines+= lines_count;
870                         }
871
872                         drawcache->line_height= fp;
873                 }
874         } else {
875                 if(drawcache->line_height) {
876                         MEM_freeN(drawcache->line_height);
877                         drawcache->line_height= NULL;
878                 }
879
880                 if(full_update || drawcache->update_flag) {
881                         nlines= BLI_countlist(&txt->lines);
882
883                         if(st->showlinenrs)
884                                 st->linenrs_tot= (int)floor(log10((float)nlines)) + 1;
885                 }
886
887                 drawcache->total_lines= nlines;
888         }
889
890         drawcache->nlines= nlines;
891
892         /* store settings */
893         drawcache->winx        = ar->winx;
894         drawcache->wordwrap    = st->wordwrap;
895         drawcache->lheight     = st->lheight;
896         drawcache->cwidth      = st->cwidth;
897         drawcache->showlinenrs = st->showlinenrs;
898         drawcache->tabnumber   = st->tabnumber;
899
900         strncpy(drawcache->text_id, txt->id.name, MAX_ID_NAME);
901
902         /* clear update flag */
903         drawcache->update_flag = 0;
904         drawcache->valid_head  = 0;
905         drawcache->valid_tail  = 0;
906 }
907
908 void text_drawcache_tag_update(SpaceText *st, int full)
909 {
910         DrawCache *drawcache= (DrawCache *)st->drawcache;
911
912         if(drawcache) {
913                 Text *txt= st->text;
914
915                 if(drawcache->update_flag) {
916                         /* happens when tagging update from space listener */
917                         /* should do nothing to prevent locally tagged cache be fully recalculated */
918                         return;
919                 }
920
921                 if(!full) {
922                         int sellno= BLI_findindex(&txt->lines, txt->sell);
923                         int curlno= BLI_findindex(&txt->lines, txt->curl);
924
925                         if(curlno < sellno) {
926                                 drawcache->valid_head= curlno;
927                                 drawcache->valid_tail= drawcache->nlines - sellno - 1;
928                         } else {
929                                 drawcache->valid_head= sellno;
930                                 drawcache->valid_tail= drawcache->nlines - curlno - 1;
931                         }
932
933                         /* quick cache recalculation is also used in delete operator,
934                            which could merge lines which are adjusent to current selection lines
935                            expand recalculate area to this lines */
936                         if(drawcache->valid_head>0) drawcache->valid_head--;
937                         if(drawcache->valid_tail>0) drawcache->valid_tail--;
938                 } else {
939                         drawcache->valid_head= 0;
940                         drawcache->valid_tail= 0;
941                 }
942
943                 drawcache->update_flag= 1;
944         }
945 }
946
947 void text_free_caches(SpaceText *st)
948 {
949         DrawCache *drawcache= (DrawCache *)st->drawcache;
950
951         if(drawcache) {
952                 if(drawcache->line_height)
953                         MEM_freeN(drawcache->line_height);
954
955                 MEM_freeN(drawcache);
956         }
957 }
958
959 /************************ word-wrap utilities *****************************/
960
961 /* cache should be updated in caller */
962 static int text_get_visible_lines_no(SpaceText *st, int lineno)
963 {
964         DrawCache *drawcache= (DrawCache *)st->drawcache;
965
966         return drawcache->line_height[lineno];
967 }
968
969 int text_get_visible_lines(SpaceText *st, ARegion *ar, const char *str)
970 {
971         int i, j, start, end, max, lines, chars;
972         char ch;
973
974         max= wrap_width(st, ar);
975         lines= 1;
976         start= 0;
977         end= max;
978         for(i= 0, j= 0; str[j] != '\0'; j++) {
979                 /* Mimic replacement of tabs */
980                 ch= str[j];
981                 if(ch=='\t') {
982                         chars= st->tabnumber-i%st->tabnumber;
983                         ch= ' ';
984                 }
985                 else chars= 1;
986
987                 while(chars--) {
988                         if(i-start >= max) {
989                                 lines++;
990                                 start= end;
991                                 end += max;
992                         }
993                         else if(ch==' ' || ch=='-') {
994                                 end= i+1;
995                         }
996
997                         i++;
998                 }
999         }
1000
1001         return lines;
1002 }
1003
1004 int text_get_span_wrap(SpaceText *st, ARegion *ar, TextLine *from, TextLine *to)
1005 {
1006         if(st->wordwrap) {
1007                 int ret=0;
1008                 TextLine *tmp= from;
1009
1010                 /* Look forwards */
1011                 while (tmp) {
1012                         if (tmp == to) return ret;
1013                         ret+= text_get_visible_lines(st, ar, tmp->line);
1014                         tmp= tmp->next;
1015                 }
1016
1017                 return ret;
1018         } else return txt_get_span(from, to);
1019 }
1020
1021 int text_get_total_lines(SpaceText *st, ARegion *ar)
1022 {
1023         DrawCache *drawcache;
1024
1025         text_update_drawcache(st, ar);
1026         drawcache= (DrawCache *)st->drawcache;
1027
1028         return drawcache->total_lines;
1029 }
1030
1031 /* Move pointer to first visible line (top) */
1032 static TextLine *first_visible_line(SpaceText *st, ARegion *ar, int *wrap_top)
1033 {
1034         Text *text= st->text;
1035         TextLine* pline= text->lines.first;
1036         int i= st->top, lineno= 0;
1037
1038         text_update_drawcache(st, ar);
1039
1040         if(wrap_top) *wrap_top= 0;
1041
1042         if(st->wordwrap) {
1043                 while(i>0 && pline) {
1044                         int lines= text_get_visible_lines_no(st, lineno);
1045
1046                         if (i-lines<0) {
1047                                 if(wrap_top) *wrap_top= i;
1048                                 break;
1049                         } else {
1050                                 pline= pline->next;
1051                                 i-= lines;
1052                                 lineno++;
1053                         }
1054                 }
1055         } else {
1056                 for(i=st->top; pline->next && i>0; i--)
1057                         pline= pline->next;
1058         }
1059
1060         return pline;
1061 }
1062
1063 /************************ draw scrollbar *****************************/
1064
1065 static void calc_text_rcts(SpaceText *st, ARegion *ar, rcti *scroll, rcti *back)
1066 {
1067         int lhlstart, lhlend, ltexth, sell_off, curl_off;
1068         short barheight, barstart, hlstart, hlend, blank_lines;
1069         short pix_available, pix_top_margin, pix_bottom_margin, pix_bardiff;
1070
1071         pix_top_margin = 8;
1072         pix_bottom_margin = 4;
1073         pix_available = ar->winy - pix_top_margin - pix_bottom_margin;
1074         ltexth= text_get_total_lines(st, ar);
1075         blank_lines = st->viewlines / 2;
1076         
1077         /* nicer code: use scroll rect for entire bar */
1078         back->xmin= ar->winx -18;
1079         back->xmax= ar->winx;
1080         back->ymin= 0;
1081         back->ymax= ar->winy;
1082         
1083         scroll->xmin= ar->winx - 17;
1084         scroll->xmax= ar->winx - 5;
1085         scroll->ymin= 4;
1086         scroll->ymax= 4+pix_available;
1087         
1088         /* when resizing a vieport with the bar at the bottom to a greater height more blank lines will be added */
1089         if(ltexth + blank_lines < st->top + st->viewlines) {
1090                 blank_lines = st->top + st->viewlines - ltexth;
1091         }
1092         
1093         ltexth += blank_lines;
1094
1095         barheight = (ltexth > 0)? (st->viewlines*pix_available)/ltexth: 0;
1096         pix_bardiff = 0;
1097         if(barheight < 20) {
1098                 pix_bardiff = 20 - barheight; /* take into account the now non-linear sizing of the bar */      
1099                 barheight = 20;
1100         }
1101         barstart = (ltexth > 0)? ((pix_available - pix_bardiff) * st->top)/ltexth: 0;
1102
1103         st->txtbar= *scroll;
1104         st->txtbar.ymax -= barstart;
1105         st->txtbar.ymin = st->txtbar.ymax - barheight;
1106
1107         CLAMP(st->txtbar.ymin, pix_bottom_margin, ar->winy - pix_top_margin);
1108         CLAMP(st->txtbar.ymax, pix_bottom_margin, ar->winy - pix_top_margin);
1109
1110         st->pix_per_line= (pix_available > 0)? (float) ltexth/pix_available: 0;
1111         if(st->pix_per_line < 0.1f) st->pix_per_line=0.1f;
1112
1113         curl_off= text_get_span_wrap(st, ar, st->text->lines.first, st->text->curl);
1114         sell_off= text_get_span_wrap(st, ar, st->text->lines.first, st->text->sell);
1115         lhlstart = MIN2(curl_off, sell_off);
1116         lhlend = MAX2(curl_off, sell_off);
1117
1118         if(ltexth > 0) {
1119                 hlstart = (lhlstart * pix_available)/ltexth;
1120                 hlend = (lhlend * pix_available)/ltexth;
1121
1122                 /* the scrollbar is non-linear sized */
1123                 if(pix_bardiff > 0) {
1124                         /* the start of the highlight is in the current viewport */
1125                         if(ltexth && st->viewlines && lhlstart >= st->top && lhlstart <= st->top + st->viewlines) { 
1126                                 /* speed the progresion of the start of the highlight through the scrollbar */
1127                                 hlstart = ( ( (pix_available - pix_bardiff) * lhlstart) / ltexth) + (pix_bardiff * (lhlstart - st->top) / st->viewlines);       
1128                         }
1129                         else if(lhlstart > st->top + st->viewlines && hlstart < barstart + barheight && hlstart > barstart) {
1130                                 /* push hl start down */
1131                                 hlstart = barstart + barheight;
1132                         }
1133                         else if(lhlend > st->top  && lhlstart < st->top && hlstart > barstart) {
1134                                 /*fill out start */
1135                                 hlstart = barstart;
1136                         }
1137
1138                         if(hlend <= hlstart) { 
1139                                 hlend = hlstart + 2;
1140                         }
1141
1142                         /* the end of the highlight is in the current viewport */
1143                         if(ltexth && st->viewlines && lhlend >= st->top && lhlend <= st->top + st->viewlines) { 
1144                                 /* speed the progresion of the end of the highlight through the scrollbar */
1145                                 hlend = (((pix_available - pix_bardiff )*lhlend)/ltexth) + (pix_bardiff * (lhlend - st->top)/st->viewlines);    
1146                         }
1147                         else if(lhlend < st->top && hlend >= barstart - 2 && hlend < barstart + barheight) {
1148                                 /* push hl end up */
1149                                 hlend = barstart;
1150                         }                                       
1151                         else if(lhlend > st->top + st->viewlines && lhlstart < st->top + st->viewlines && hlend < barstart + barheight) {
1152                                 /* fill out end */
1153                                 hlend = barstart + barheight;
1154                         }
1155
1156                         if(hlend <= hlstart) { 
1157                                 hlstart = hlend - 2;
1158                         }       
1159                 }       
1160         }
1161         else {
1162                 hlstart = 0;
1163                 hlend = 0;
1164         }
1165
1166         if(hlend - hlstart < 2) { 
1167                 hlend = hlstart + 2;
1168         }
1169         
1170         st->txtscroll= *scroll;
1171         st->txtscroll.ymax= ar->winy - pix_top_margin - hlstart;
1172         st->txtscroll.ymin= ar->winy - pix_top_margin - hlend;
1173
1174         CLAMP(st->txtscroll.ymin, pix_bottom_margin, ar->winy - pix_top_margin);
1175         CLAMP(st->txtscroll.ymax, pix_bottom_margin, ar->winy - pix_top_margin);
1176 }
1177
1178 static void draw_textscroll(SpaceText *st, rcti *scroll, rcti *back)
1179 {
1180         bTheme *btheme= U.themes.first;
1181         uiWidgetColors wcol= btheme->tui.wcol_scroll;
1182         unsigned char col[4];
1183         float rad;
1184         
1185         UI_ThemeColor(TH_BACK);
1186         glRecti(back->xmin, back->ymin, back->xmax, back->ymax);
1187
1188         uiWidgetScrollDraw(&wcol, scroll, &st->txtbar, (st->flags & ST_SCROLL_SELECT)?UI_SCROLL_PRESSED:0);
1189
1190         uiSetRoundBox(15);
1191         rad= 0.4f*MIN2(st->txtscroll.xmax - st->txtscroll.xmin, st->txtscroll.ymax - st->txtscroll.ymin);
1192         UI_GetThemeColor3ubv(TH_HILITE, col);
1193         col[3]= 48;
1194         glColor4ubv(col);
1195         glEnable(GL_BLEND);
1196         uiRoundBox(st->txtscroll.xmin+1, st->txtscroll.ymin, st->txtscroll.xmax-1, st->txtscroll.ymax, rad);
1197         glDisable(GL_BLEND);
1198 }
1199
1200 /************************** draw markers **************************/
1201
1202 static void draw_markers(SpaceText *st, ARegion *ar)
1203 {
1204         Text *text= st->text;
1205         TextMarker *marker, *next;
1206         TextLine *top, *line;
1207         int offl, offc, i, x1, x2, y1, y2, x, y;
1208         int topi, topy;
1209
1210         /* Move pointer to first visible line (top) */
1211         top= first_visible_line(st, ar, NULL);
1212         topi= BLI_findindex(&text->lines, top);
1213
1214         topy= txt_get_span(text->lines.first, top);
1215
1216         for(marker= text->markers.first; marker; marker= next) {
1217                 next= marker->next;
1218
1219                 /* invisible line (before top) */
1220                 if(marker->lineno<topi) continue;
1221
1222                 line= BLI_findlink(&text->lines, marker->lineno);
1223
1224                 /* Remove broken markers */
1225                 if(marker->end>line->len || marker->start>marker->end) {
1226                         BLI_freelinkN(&text->markers, marker);
1227                         continue;
1228                 }
1229
1230                 wrap_offset(st, ar, line, marker->start, &offl, &offc);
1231                 y1 = txt_get_span(top, line) - st->top + offl + topy;
1232                 x1 = text_get_char_pos(st, line->line, marker->start) - st->left + offc;
1233
1234                 wrap_offset(st, ar, line, marker->end, &offl, &offc);
1235                 y2 = txt_get_span(top, line) - st->top + offl + topy;
1236                 x2 = text_get_char_pos(st, line->line, marker->end) - st->left + offc;
1237
1238                 /* invisible part of line (before top, after last visible line) */
1239                 if(y2 < 0 || y1 > st->top+st->viewlines) continue;
1240
1241                 glColor3ubv(marker->color);
1242                 x= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;
1243                 y= ar->winy-3;
1244
1245                 if(y1==y2) {
1246                         y -= y1*st->lheight;
1247                         glBegin(GL_LINE_LOOP);
1248                         glVertex2i(x+x2*st->cwidth+1, y);
1249                         glVertex2i(x+x1*st->cwidth-2, y);
1250                         glVertex2i(x+x1*st->cwidth-2, y-st->lheight);
1251                         glVertex2i(x+x2*st->cwidth+1, y-st->lheight);
1252                         glEnd();
1253                 }
1254                 else {
1255                         y -= y1*st->lheight;
1256                         glBegin(GL_LINE_STRIP);
1257                         glVertex2i(ar->winx, y);
1258                         glVertex2i(x+x1*st->cwidth-2, y);
1259                         glVertex2i(x+x1*st->cwidth-2, y-st->lheight);
1260                         glVertex2i(ar->winx, y-st->lheight);
1261                         glEnd();
1262                         y-=st->lheight;
1263
1264                         for(i=y1+1; i<y2; i++) {
1265                                 glBegin(GL_LINES);
1266                                 glVertex2i(x, y);
1267                                 glVertex2i(ar->winx, y);
1268                                 glVertex2i(x, y-st->lheight);
1269                                 glVertex2i(ar->winx, y-st->lheight);
1270                                 glEnd();
1271                                 y-=st->lheight;
1272                         }
1273
1274                         glBegin(GL_LINE_STRIP);
1275                         glVertex2i(x, y);
1276                         glVertex2i(x+x2*st->cwidth+1, y);
1277                         glVertex2i(x+x2*st->cwidth+1, y-st->lheight);
1278                         glVertex2i(x, y-st->lheight);
1279                         glEnd();
1280                 }
1281         }
1282 }
1283
1284 /*********************** draw documentation *******************************/
1285
1286 static void draw_documentation(SpaceText *st, ARegion *ar)
1287 {
1288         TextLine *tmp;
1289         char *docs, buf[DOC_WIDTH+1], *p;
1290         int i, br, lines;
1291         int boxw, boxh, l, x, y, top;
1292         
1293         if(!st || !st->text) return;
1294         if(!texttool_text_is_active(st->text)) return;
1295         
1296         docs = texttool_docs_get();
1297
1298         if(!docs) return;
1299
1300         /* Count the visible lines to the cursor */
1301         for(tmp=st->text->curl, l=-st->top; tmp; tmp=tmp->prev, l++);
1302         if(l<0) return;
1303         
1304         if(st->showlinenrs) {
1305                 x= st->cwidth*(st->text->curc-st->left) + TXT_OFFSET + TEXTXLOC - 4;
1306         }
1307         else {
1308                 x= st->cwidth*(st->text->curc-st->left) + TXT_OFFSET - 4;
1309         }
1310         if(texttool_suggest_first()) {
1311                 x += SUGG_LIST_WIDTH*st->cwidth + 50;
1312         }
1313
1314         top= y= ar->winy - st->lheight*l - 2;
1315         boxw= DOC_WIDTH*st->cwidth + 20;
1316         boxh= (DOC_HEIGHT+1)*st->lheight;
1317
1318         /* Draw panel */
1319         UI_ThemeColor(TH_BACK);
1320         glRecti(x, y, x+boxw, y-boxh);
1321         UI_ThemeColor(TH_SHADE1);
1322         glBegin(GL_LINE_LOOP);
1323         glVertex2i(x, y);
1324         glVertex2i(x+boxw, y);
1325         glVertex2i(x+boxw, y-boxh);
1326         glVertex2i(x, y-boxh);
1327         glEnd();
1328         glBegin(GL_LINE_LOOP);
1329         glVertex2i(x+boxw-10, y-7);
1330         glVertex2i(x+boxw-4, y-7);
1331         glVertex2i(x+boxw-7, y-2);
1332         glEnd();
1333         glBegin(GL_LINE_LOOP);
1334         glVertex2i(x+boxw-10, y-boxh+7);
1335         glVertex2i(x+boxw-4, y-boxh+7);
1336         glVertex2i(x+boxw-7, y-boxh+2);
1337         glEnd();
1338         UI_ThemeColor(TH_TEXT);
1339
1340         i= 0; br= DOC_WIDTH; lines= 0; // XXX -doc_scroll;
1341         for(p=docs; *p; p++) {
1342                 if(*p == '\r' && *(++p) != '\n') *(--p)= '\n'; /* Fix line endings */
1343                 if(*p == ' ' || *p == '\t')
1344                         br= i;
1345                 else if(*p == '\n') {
1346                         buf[i]= '\0';
1347                         if(lines>=0) {
1348                                 y -= st->lheight;
1349                                 text_draw(st, buf, 0, 0, 1, x+4, y-3, NULL);
1350                         }
1351                         i= 0; br= DOC_WIDTH; lines++;
1352                 }
1353                 buf[i++]= *p;
1354                 if(i == DOC_WIDTH) { /* Reached the width, go to last break and wrap there */
1355                         buf[br]= '\0';
1356                         if(lines>=0) {
1357                                 y -= st->lheight;
1358                                 text_draw(st, buf, 0, 0, 1, x+4, y-3, NULL);
1359                         }
1360                         p -= i-br-1; /* Rewind pointer to last break */
1361                         i= 0; br= DOC_WIDTH; lines++;
1362                 }
1363                 if(lines >= DOC_HEIGHT) break;
1364         }
1365
1366         if(0 /* XXX doc_scroll*/ > 0 && lines < DOC_HEIGHT) {
1367                 // XXX doc_scroll--;
1368                 draw_documentation(st, ar);
1369         }
1370 }
1371
1372 /*********************** draw suggestion list *******************************/
1373
1374 static void draw_suggestion_list(SpaceText *st, ARegion *ar)
1375 {
1376         SuggItem *item, *first, *last, *sel;
1377         TextLine *tmp;
1378         char str[SUGG_LIST_WIDTH+1];
1379         int w, boxw=0, boxh, i, l, x, y, b, *top;
1380         
1381         if(!st || !st->text) return;
1382         if(!texttool_text_is_active(st->text)) return;
1383
1384         first = texttool_suggest_first();
1385         last = texttool_suggest_last();
1386
1387         if(!first || !last) return;
1388
1389         text_pop_suggest_list();
1390         sel = texttool_suggest_selected();
1391         top = texttool_suggest_top();
1392
1393         /* Count the visible lines to the cursor */
1394         for(tmp=st->text->curl, l=-st->top; tmp; tmp=tmp->prev, l++);
1395         if(l<0) return;
1396         
1397         if(st->showlinenrs) {
1398                 x = st->cwidth*(st->text->curc-st->left) + TXT_OFFSET + TEXTXLOC - 4;
1399         }
1400         else {
1401                 x = st->cwidth*(st->text->curc-st->left) + TXT_OFFSET - 4;
1402         }
1403         y = ar->winy - st->lheight*l - 2;
1404
1405         boxw = SUGG_LIST_WIDTH*st->cwidth + 20;
1406         boxh = SUGG_LIST_SIZE*st->lheight + 8;
1407         
1408         UI_ThemeColor(TH_SHADE1);
1409         glRecti(x-1, y+1, x+boxw+1, y-boxh-1);
1410         UI_ThemeColor(TH_BACK);
1411         glRecti(x, y, x+boxw, y-boxh);
1412
1413         /* Set the top 'item' of the visible list */
1414         for(i=0, item=first; i<*top && item->next; i++, item=item->next);
1415
1416         for(i=0; i<SUGG_LIST_SIZE && item; i++, item=item->next) {
1417
1418                 y -= st->lheight;
1419
1420                 strncpy(str, item->name, SUGG_LIST_WIDTH);
1421                 str[SUGG_LIST_WIDTH] = '\0';
1422
1423                 w = text_font_width(st, str);
1424                 
1425                 if(item == sel) {
1426                         UI_ThemeColor(TH_SHADE2);
1427                         glRecti(x+16, y-3, x+16+w, y+st->lheight-3);
1428                 }
1429                 b=1; /* b=1 color block, text is default. b=0 no block, color text */
1430                 switch (item->type) {
1431                         case 'k': UI_ThemeColor(TH_SYNTAX_B); b=0; break;
1432                         case 'm': UI_ThemeColor(TH_TEXT); break;
1433                         case 'f': UI_ThemeColor(TH_SYNTAX_L); break;
1434                         case 'v': UI_ThemeColor(TH_SYNTAX_N); break;
1435                         case '?': UI_ThemeColor(TH_TEXT); b=0; break;
1436                 }
1437                 if(b) {
1438                         glRecti(x+8, y+2, x+11, y+5);
1439                         UI_ThemeColor(TH_TEXT);
1440                 }
1441                 text_draw(st, str, 0, 0, 1, x+16, y-1, NULL);
1442
1443                 if(item == last) break;
1444         }
1445 }
1446
1447 /*********************** draw cursor ************************/
1448
1449 static void draw_cursor(SpaceText *st, ARegion *ar)
1450 {
1451         Text *text= st->text;
1452         int vcurl, vcurc, vsell, vselc, hidden=0;
1453         int x, y, w, i;
1454
1455         /* Draw the selection */
1456         if(text->curl!=text->sell || text->curc!=text->selc) {
1457                 int offl, offc;
1458                 /* Convert all to view space character coordinates */
1459                 wrap_offset(st, ar, text->curl, text->curc, &offl, &offc);
1460                 vcurl = txt_get_span(text->lines.first, text->curl) - st->top + offl;
1461                 vcurc = text_get_char_pos(st, text->curl->line, text->curc) - st->left + offc;
1462                 wrap_offset(st, ar, text->sell, text->selc, &offl, &offc);
1463                 vsell = txt_get_span(text->lines.first, text->sell) - st->top + offl;
1464                 vselc = text_get_char_pos(st, text->sell->line, text->selc) - st->left + offc;
1465
1466                 if(vcurc<0) vcurc=0;
1467                 if(vselc<0) vselc=0, hidden=1;
1468                 
1469                 UI_ThemeColor(TH_SHADE2);
1470                 x= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;
1471                 y= ar->winy-2;
1472
1473                 if(vcurl==vsell) {
1474                         y -= vcurl*st->lheight;
1475                         if(vcurc < vselc)
1476                                 glRecti(x+vcurc*st->cwidth-1, y, x+vselc*st->cwidth, y-st->lheight);
1477                         else
1478                                 glRecti(x+vselc*st->cwidth-1, y, x+vcurc*st->cwidth, y-st->lheight);
1479                 }
1480                 else {
1481                         int froml, fromc, tol, toc;
1482
1483                         if(vcurl < vsell) {
1484                                 froml= vcurl; tol= vsell;
1485                                 fromc= vcurc; toc= vselc;
1486                         }
1487                         else {
1488                                 froml= vsell; tol= vcurl;
1489                                 fromc= vselc; toc= vcurc;
1490                         }
1491
1492                         y -= froml*st->lheight;
1493                         glRecti(x+fromc*st->cwidth-1, y, ar->winx, y-st->lheight); y-=st->lheight;
1494                         for(i=froml+1; i<tol; i++)
1495                                 glRecti(x-4, y, ar->winx, y-st->lheight),  y-=st->lheight;
1496
1497                         glRecti(x-4, y, x+toc*st->cwidth, y-st->lheight);  y-=st->lheight;
1498
1499                         (void)y;
1500                 }
1501         }
1502         else {
1503                 int offl, offc;
1504                 wrap_offset(st, ar, text->sell, text->selc, &offl, &offc);
1505                 vsell = txt_get_span(text->lines.first, text->sell) - st->top + offl;
1506                 vselc = text_get_char_pos(st, text->sell->line, text->selc) - st->left + offc;
1507
1508                 if(vselc<0) {
1509                         vselc= 0;
1510                         hidden= 1;
1511                 }
1512         }
1513
1514         if(st->line_hlight) {
1515                 int x1, x2, y1, y2;
1516
1517                 if(st->wordwrap) {
1518                         int visible_lines = text_get_visible_lines(st, ar, text->sell->line);
1519                         int offl, offc;
1520
1521                         wrap_offset_in_line(st, ar, text->sell, text->selc, &offl, &offc);
1522
1523                         y1= ar->winy-2 - (vsell-offl)*st->lheight;
1524                         y2= y1-st->lheight*visible_lines+1;
1525                 } else {
1526                         y1= ar->winy-2 - vsell*st->lheight;
1527                         y2= y1-st->lheight+1;
1528                 }
1529
1530                 if(!(y1<0 || y2 > ar->winy)) { /* check we need to draw */
1531                         x1= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;
1532                         x2= x1 + ar->winx;
1533
1534                         glColor4ub(255, 255, 255, 32);
1535                         
1536                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1537                         glEnable(GL_BLEND);
1538                         glRecti(x1-4, y1, x2, y2);
1539                         glDisable(GL_BLEND);
1540                 }
1541         }
1542         
1543         if(!hidden) {
1544                 /* Draw the cursor itself (we draw the sel. cursor as this is the leading edge) */
1545                 x= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;
1546                 x += vselc*st->cwidth;
1547                 y= ar->winy-2 - vsell*st->lheight;
1548                 
1549                 if(st->overwrite) {
1550                         char ch= text->sell->line[text->selc];
1551                         
1552                         w= st->cwidth;
1553                         if(ch=='\t')  w*= st->tabnumber-(vselc+st->left)%st->tabnumber;
1554                         
1555                         UI_ThemeColor(TH_HILITE);
1556                         glRecti(x, y-st->lheight-1, x+w, y-st->lheight+1);
1557                 }
1558                 else {
1559                         UI_ThemeColor(TH_HILITE);
1560                         glRecti(x-1, y, x+1, y-st->lheight);
1561                 }
1562         }
1563 }
1564
1565 /******************* draw matching brackets *********************/
1566
1567 static void draw_brackets(SpaceText *st, ARegion *ar)
1568 {
1569         TextLine *startl, *endl, *linep;
1570         Text *text = st->text;
1571         int b, c, startc, endc, find, stack;
1572         int viewc, viewl, offl, offc, x, y;
1573         char ch;
1574
1575         // showsyntax must be on or else the format string will be null
1576         if(!text->curl || !st->showsyntax) return;
1577
1578         startl= text->curl;
1579         startc= text->curc;
1580         b= text_check_bracket(startl->line[startc]);
1581         if(b==0 && startc>0) b = text_check_bracket(startl->line[--startc]);
1582         if(b==0) return;
1583         
1584         linep= startl;
1585         c= startc;
1586         endl= NULL;
1587         endc= -1;
1588         find= -b;
1589         stack= 0;
1590         
1591         /* Dont highlight backets if syntax HL is off or bracket in string or comment. */
1592         if(!linep->format || linep->format[c] == 'l' || linep->format[c] == '#')
1593                 return;
1594
1595         if(b>0) {
1596                 /* opening bracket, search forward for close */
1597                 c++;
1598                 while(linep) {
1599                         while(c<linep->len) {
1600                                 if(linep->format && linep->format[c] != 'l' && linep->format[c] != '#') {
1601                                         b= text_check_bracket(linep->line[c]);
1602                                         if(b==find) {
1603                                                 if(stack==0) {
1604                                                         endl= linep;
1605                                                         endc= c;
1606                                                         break;
1607                                                 }
1608                                                 stack--;
1609                                         }
1610                                         else if(b==-find) {
1611                                                 stack++;
1612                                         }
1613                                 }
1614                                 c++;
1615                         }
1616                         if(endl) break;
1617                         linep= linep->next;
1618                         c= 0;
1619                 }
1620         }
1621         else {
1622                 /* closing bracket, search backward for open */
1623                 c--;
1624                 while(linep) {
1625                         while(c>=0) {
1626                                 if(linep->format && linep->format[c] != 'l' && linep->format[c] != '#') {
1627                                         b= text_check_bracket(linep->line[c]);
1628                                         if(b==find) {
1629                                                 if(stack==0) {
1630                                                         endl= linep;
1631                                                         endc= c;
1632                                                         break;
1633                                                 }
1634                                                 stack--;
1635                                         }
1636                                         else if(b==-find) {
1637                                                 stack++;
1638                                         }
1639                                 }
1640                                 c--;
1641                         }
1642                         if(endl) break;
1643                         linep= linep->prev;
1644                         if(linep) c= linep->len-1;
1645                 }
1646         }
1647
1648         if(!endl || endc==-1)
1649                 return;
1650
1651         UI_ThemeColor(TH_HILITE);       
1652         x= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET;
1653         y= ar->winy - st->lheight;
1654
1655         /* draw opening bracket */
1656         ch= startl->line[startc];
1657         wrap_offset(st, ar, startl, startc, &offl, &offc);
1658         viewc= text_get_char_pos(st, startl->line, startc) - st->left + offc;
1659
1660         if(viewc >= 0){
1661                 viewl= txt_get_span(text->lines.first, startl) - st->top + offl;
1662
1663                 text_font_draw_character(st, x+viewc*st->cwidth, y-viewl*st->lheight, ch);
1664                 text_font_draw_character(st, x+viewc*st->cwidth+1, y-viewl*st->lheight, ch);
1665         }
1666
1667         /* draw closing bracket */
1668         ch= endl->line[endc];
1669         wrap_offset(st, ar, endl, endc, &offl, &offc);
1670         viewc= text_get_char_pos(st, endl->line, endc) - st->left + offc;
1671
1672         if(viewc >= 0) {
1673                 viewl= txt_get_span(text->lines.first, endl) - st->top + offl;
1674
1675                 text_font_draw_character(st, x+viewc*st->cwidth, y-viewl*st->lheight, ch);
1676                 text_font_draw_character(st, x+viewc*st->cwidth+1, y-viewl*st->lheight, ch);
1677         }
1678 }
1679
1680 /*********************** main area drawing *************************/
1681
1682 void draw_text_main(SpaceText *st, ARegion *ar)
1683 {
1684         Text *text= st->text;
1685         TextLine *tmp;
1686         rcti scroll, back;
1687         char linenr[12];
1688         int i, x, y, winx, linecount= 0, lineno= 0;
1689         int wraplinecount= 0, wrap_skip= 0;
1690
1691         /* if no text, nothing to do */
1692         if(!text)
1693                 return;
1694         
1695         text_update_drawcache(st, ar);
1696
1697         /* make sure all the positional pointers exist */
1698         if(!text->curl || !text->sell || !text->lines.first || !text->lines.last)
1699                 txt_clean_text(text);
1700         
1701         if(st->lheight) st->viewlines= (int)ar->winy/st->lheight;
1702         else st->viewlines= 0;
1703         
1704         /* update rects for scroll */
1705         calc_text_rcts(st, ar, &scroll, &back); /* scroll will hold the entire bar size */
1706
1707         /* update syntax formatting if needed */
1708         tmp= text->lines.first;
1709         lineno= 0;
1710         for(i= 0; i<st->top && tmp; i++) {
1711                 if(st->showsyntax && !tmp->format)
1712                         txt_format_line(st, tmp, 0);
1713
1714                 if(st->wordwrap) {
1715                         int lines= text_get_visible_lines_no(st, lineno);
1716
1717                         if (wraplinecount+lines>st->top) {
1718                                 wrap_skip= st->top-wraplinecount;
1719                                 break;
1720                         } else {
1721                                 wraplinecount+= lines;
1722                                 tmp= tmp->next;
1723                                 linecount++;
1724                         }
1725                 } else {
1726                         tmp= tmp->next;
1727                         linecount++;
1728                 }
1729
1730                 lineno++;
1731         }
1732
1733         text_font_begin(st);
1734         st->cwidth= BLF_fixed_width(mono);
1735         st->cwidth= MAX2(st->cwidth, 1);
1736
1737         /* draw line numbers background */
1738         if(st->showlinenrs) {
1739                 x= TXT_OFFSET + TEXTXLOC;
1740
1741                 UI_ThemeColor(TH_GRID);
1742                 glRecti((TXT_OFFSET-12), 0, (TXT_OFFSET-5) + TEXTXLOC, ar->winy - 2);
1743         }
1744         else {
1745                 st->linenrs_tot= 0; /* not used */
1746                 x= TXT_OFFSET;
1747         }
1748         y= ar->winy-st->lheight;
1749         winx= ar->winx - TXT_SCROLL_WIDTH;
1750         
1751         /* draw cursor */
1752         draw_cursor(st, ar);
1753
1754         /* draw the text */
1755         UI_ThemeColor(TH_TEXT);
1756
1757         for(i=0; y>0 && i<st->viewlines && tmp; i++, tmp= tmp->next) {
1758                 if(st->showsyntax && !tmp->format)
1759                         txt_format_line(st, tmp, 0);
1760
1761                 if(st->showlinenrs && !wrap_skip) {
1762                         /* draw line number */
1763                         if(tmp == text->curl)
1764                                 UI_ThemeColor(TH_HILITE);
1765                         else
1766                                 UI_ThemeColor(TH_TEXT);
1767
1768                         sprintf(linenr, "%*d", st->linenrs_tot, i + linecount + 1);
1769                         /* itoa(i + linecount + 1, linenr, 10); */ /* not ansi-c :/ */
1770                         text_font_draw(st, TXT_OFFSET - 7, y, linenr);
1771
1772                         UI_ThemeColor(TH_TEXT);
1773                 }
1774
1775                 if(st->wordwrap) {
1776                         /* draw word wrapped text */
1777                         int lines = text_draw_wrapped(st, tmp->line, x, y, winx-x, tmp->format, wrap_skip);
1778                         y -= lines*st->lheight;
1779                 }
1780                 else {
1781                         /* draw unwrapped text */
1782                         text_draw(st, tmp->line, st->left, ar->winx/st->cwidth, 1, x, y, tmp->format);
1783                         y -= st->lheight;
1784                 }
1785
1786                 wrap_skip= 0;
1787         }
1788         
1789         if(st->flags&ST_SHOW_MARGIN) {
1790                 UI_ThemeColor(TH_HILITE);
1791
1792                 glBegin(GL_LINES);
1793                 glVertex2i(x+st->cwidth*st->margin_column, 0);
1794                 glVertex2i(x+st->cwidth*st->margin_column, ar->winy - 2);
1795                 glEnd();
1796         }
1797
1798         /* draw other stuff */
1799         draw_brackets(st, ar);
1800         draw_markers(st, ar);
1801         glTranslatef(0.375f, 0.375f, 0.0f); /* XXX scroll requires exact pixel space */
1802         draw_textscroll(st, &scroll, &back);
1803         draw_documentation(st, ar);
1804         draw_suggestion_list(st, ar);
1805         
1806         text_font_end(st);
1807 }
1808
1809 /************************** update ***************************/
1810
1811 void text_update_character_width(SpaceText *st)
1812 {
1813         text_font_begin(st);
1814         st->cwidth= BLF_fixed_width(mono);
1815         st->cwidth= MAX2(st->cwidth, 1);
1816         text_font_end(st);
1817 }
1818
1819 /* Moves the view to the cursor location,
1820   also used to make sure the view isnt outside the file */
1821 void text_update_cursor_moved(bContext *C)
1822 {
1823         ScrArea *sa= CTX_wm_area(C);
1824         SpaceText *st= CTX_wm_space_text(C);
1825         Text *text;
1826         ARegion *ar;
1827         int i, x, winx= 0;
1828
1829         if(ELEM3(NULL, st, st->text, st->text->curl)) return;
1830
1831         text= st->text;
1832
1833         for(ar=sa->regionbase.first; ar; ar= ar->next)
1834                 if(ar->regiontype==RGN_TYPE_WINDOW)
1835                         winx= ar->winx;
1836         
1837         winx -= TXT_SCROLL_WIDTH;
1838
1839         text_update_character_width(st);
1840
1841         i= txt_get_span(text->lines.first, text->sell);
1842         if(st->wordwrap) {
1843                 int offl, offc;
1844                 wrap_offset(st, CTX_wm_region(C), text->sell, text->selc, &offl, &offc);
1845                 i+= offl;
1846         }
1847
1848         if(st->top+st->viewlines <= i || st->top > i)
1849                 st->top= i - st->viewlines/2;
1850         
1851         if(st->wordwrap) {
1852                 st->left= 0;
1853         }
1854         else {
1855                 x= text_draw(st, text->sell->line, st->left, text->selc, 0, 0, 0, NULL);
1856
1857                 if(x==0 || x>winx)
1858                         st->left= text->curc-0.5*winx/st->cwidth;
1859         }
1860
1861         if(st->top < 0) st->top= 0;
1862         if(st->left <0) st->left= 0;
1863 }
1864