diff options
Diffstat (limited to 'source/blender/editors/space_text/text_draw.c')
-rw-r--r-- | source/blender/editors/space_text/text_draw.c | 186 |
1 files changed, 103 insertions, 83 deletions
diff --git a/source/blender/editors/space_text/text_draw.c b/source/blender/editors/space_text/text_draw.c index 0dfe7c9e660..9721fbc2b9c 100644 --- a/source/blender/editors/space_text/text_draw.c +++ b/source/blender/editors/space_text/text_draw.c @@ -41,6 +41,7 @@ #include "DNA_text_types.h" #include "DNA_space_types.h" #include "DNA_screen_types.h" +#include "DNA_userdef_types.h" #include "BKE_global.h" #include "BKE_main.h" @@ -94,13 +95,7 @@ static int text_font_draw_character(SpaceText *st, int x, int y, char c) BLF_position(x, y, 0); BLF_draw(str); - return text_font_width_character(st); -} - -int text_font_width_character(SpaceText *st) -{ - // XXX need quick BLF function, or cache it somewhere - return (st->lheight == 12)? 7: 8; + return st->cwidth; } int text_font_width(SpaceText *st, char *str) @@ -114,22 +109,18 @@ static void flatten_string_append(FlattenString *fs, char c, int accum) { if(fs->pos>=fs->len && fs->pos>=sizeof(fs->fixedbuf)-1) { char *nbuf; int *naccum; - int olen= fs->len; - - if(olen) fs->len*= 2; - else fs->len= 256; - + if(fs->len) fs->len*= 2; + else fs->len= sizeof(fs->fixedbuf) * 2; + nbuf= MEM_callocN(sizeof(*fs->buf)*fs->len, "fs->buf"); naccum= MEM_callocN(sizeof(*fs->accum)*fs->len, "fs->accum"); + + memcpy(nbuf, fs->buf, fs->pos); + memcpy(naccum, fs->accum, fs->pos); - if(olen) { - memcpy(nbuf, fs->buf, olen); - memcpy(naccum, fs->accum, olen); - - if(fs->buf != fs->fixedbuf) { - MEM_freeN(fs->buf); - MEM_freeN(fs->accum); - } + if(fs->buf != fs->fixedbuf) { + MEM_freeN(fs->buf); + MEM_freeN(fs->accum); } fs->buf= nbuf; @@ -139,8 +130,7 @@ static void flatten_string_append(FlattenString *fs, char c, int accum) fs->buf[fs->pos]= c; fs->accum[fs->pos]= accum; - if(c==0) fs->pos= 0; - else fs->pos++; + fs->pos++; } int flatten_string(SpaceText *st, FlattenString *fs, char *in) @@ -522,7 +512,7 @@ int wrap_width(SpaceText *st, ARegion *ar) int x, max; x= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; - max= (ar->winx-x)/text_font_width_character(st); + max= (ar->winx-x)/st->cwidth; return max>8 ? max : 8; } @@ -614,7 +604,7 @@ static int text_draw_wrapped(SpaceText *st, char *str, int x, int y, int w, char len= flatten_string(st, &fs, str); str= fs.buf; - max= w/text_font_width_character(st); + max= w/st->cwidth; if(max<8) max= 8; basex= x; @@ -686,7 +676,7 @@ static int text_draw(SpaceText *st, char *str, int cshift, int maxwidth, int dra } else { while(w-- && *acc++ < maxwidth) - r+= text_font_width_character(st); + r+= st->cwidth; } flatten_string_free(&fs); @@ -701,7 +691,7 @@ static int text_draw(SpaceText *st, char *str, int cshift, int maxwidth, int dra /************************ draw scrollbar *****************************/ -static void calc_text_rcts(SpaceText *st, ARegion *ar) +static void calc_text_rcts(SpaceText *st, ARegion *ar, rcti *scroll) { int lhlstart, lhlend, ltexth; short barheight, barstart, hlstart, hlend, blank_lines; @@ -713,6 +703,12 @@ static void calc_text_rcts(SpaceText *st, ARegion *ar) ltexth= txt_get_span(st->text->lines.first, st->text->lines.last); blank_lines = st->viewlines / 2; + /* nicer code: use scroll rect for entire bar */ + scroll->xmin= 5; + scroll->xmax= 17; + scroll->ymin= 4; + scroll->ymax= 4+pix_available; + /* when resizing a vieport with the bar at the bottom to a greater height more blank lines will be added */ if(ltexth + blank_lines < st->top + st->viewlines) { blank_lines = st->top + st->viewlines - ltexth; @@ -728,9 +724,8 @@ static void calc_text_rcts(SpaceText *st, ARegion *ar) } barstart = (ltexth > 0)? ((pix_available - pix_bardiff) * st->top)/ltexth: 0; - st->txtbar.xmin = 5; - st->txtbar.xmax = 17; - st->txtbar.ymax = ar->winy - pix_top_margin - barstart; + st->txtbar= *scroll; + st->txtbar.ymax -= barstart; st->txtbar.ymin = st->txtbar.ymax - barheight; CLAMP(st->txtbar.ymin, pix_bottom_margin, ar->winy - pix_top_margin); @@ -796,8 +791,7 @@ static void calc_text_rcts(SpaceText *st, ARegion *ar) hlend = hlstart + 2; } - st->txtscroll.xmin= 5; - st->txtscroll.xmax= 17; + st->txtscroll= *scroll; st->txtscroll.ymax= ar->winy - pix_top_margin - hlstart; st->txtscroll.ymin= ar->winy - pix_top_margin - hlend; @@ -805,19 +799,31 @@ static void calc_text_rcts(SpaceText *st, ARegion *ar) CLAMP(st->txtscroll.ymax, pix_bottom_margin, ar->winy - pix_top_margin); } -static void draw_textscroll(SpaceText *st, ARegion *ar) +static void draw_textscroll(SpaceText *st, ARegion *ar, rcti *scroll) { - UI_ThemeColorShade(TH_SHADE1, -20); - glRecti(2, 2, 20, ar->winy-6); - uiEmboss(2, 2, 20, ar->winy-6, 1); - - UI_ThemeColor(TH_SHADE1); - glRecti(st->txtbar.xmin, st->txtbar.ymin, st->txtbar.xmax, st->txtbar.ymax); + bTheme *btheme= U.themes.first; + uiWidgetColors wcol= btheme->tui.wcol_scroll; + char col[3]; + float rad; + +// UI_ThemeColorShade(TH_SHADE1, -20); +// glRecti(2, 2, 20, ar->winy-6); +// uiEmboss(2, 2, 20, ar->winy-6, 1); - UI_ThemeColor(TH_SHADE2); - glRecti(st->txtscroll.xmin, st->txtscroll.ymin, st->txtscroll.xmax, st->txtscroll.ymax); +// UI_ThemeColor(TH_SHADE1); +// glRecti(st->txtbar.xmin, st->txtbar.ymin, st->txtbar.xmax, st->txtbar.ymax); - uiEmboss(st->txtbar.xmin, st->txtbar.ymin, st->txtbar.xmax, st->txtbar.ymax, st->flags & ST_SCROLL_SELECT); +// uiEmboss(st->txtbar.xmin, st->txtbar.ymin, st->txtbar.xmax, st->txtbar.ymax, st->flags & ST_SCROLL_SELECT); + + uiWidgetScrollDraw(&wcol, scroll, &st->txtbar, (st->flags & ST_SCROLL_SELECT)?UI_SCROLL_PRESSED:0); + + uiSetRoundBox(15); + rad= 0.4f*MIN2(st->txtscroll.xmax - st->txtscroll.xmin, st->txtscroll.ymax - st->txtscroll.ymin); + UI_GetThemeColor3ubv(TH_HILITE, col); + glColor4ub(col[0], col[1], col[2], 48); + glEnable(GL_BLEND); + uiRoundBox(st->txtscroll.xmin+1, st->txtscroll.ymin, st->txtscroll.xmax-1, st->txtscroll.ymax, rad); + glDisable(GL_BLEND); } /************************** draw markers **************************/ @@ -860,18 +866,18 @@ static void draw_markers(SpaceText *st, ARegion *ar) if(y1==y2) { y -= y1*st->lheight; glBegin(GL_LINE_LOOP); - glVertex2i(x+x2*text_font_width_character(st)+1, y); - glVertex2i(x+x1*text_font_width_character(st)-2, y); - glVertex2i(x+x1*text_font_width_character(st)-2, y-st->lheight); - glVertex2i(x+x2*text_font_width_character(st)+1, y-st->lheight); + glVertex2i(x+x2*st->cwidth+1, y); + glVertex2i(x+x1*st->cwidth-2, y); + glVertex2i(x+x1*st->cwidth-2, y-st->lheight); + glVertex2i(x+x2*st->cwidth+1, y-st->lheight); glEnd(); } else { y -= y1*st->lheight; glBegin(GL_LINE_STRIP); glVertex2i(ar->winx, y); - glVertex2i(x+x1*text_font_width_character(st)-2, y); - glVertex2i(x+x1*text_font_width_character(st)-2, y-st->lheight); + glVertex2i(x+x1*st->cwidth-2, y); + glVertex2i(x+x1*st->cwidth-2, y-st->lheight); glVertex2i(ar->winx, y-st->lheight); glEnd(); y-=st->lheight; @@ -888,8 +894,8 @@ static void draw_markers(SpaceText *st, ARegion *ar) glBegin(GL_LINE_STRIP); glVertex2i(x, y); - glVertex2i(x+x2*text_font_width_character(st)+1, y); - glVertex2i(x+x2*text_font_width_character(st)+1, y-st->lheight); + glVertex2i(x+x2*st->cwidth+1, y); + glVertex2i(x+x2*st->cwidth+1, y-st->lheight); glVertex2i(x, y-st->lheight); glEnd(); } @@ -923,18 +929,18 @@ static void draw_documentation(SpaceText *st, ARegion *ar) if(l<0) return; if(st->showlinenrs) { - x= text_font_width_character(st)*(st->text->curc-st->left) + TXT_OFFSET + TEXTXLOC - 4; + x= st->cwidth*(st->text->curc-st->left) + TXT_OFFSET + TEXTXLOC - 4; } else { - x= text_font_width_character(st)*(st->text->curc-st->left) + TXT_OFFSET - 4; + x= st->cwidth*(st->text->curc-st->left) + TXT_OFFSET - 4; } if(texttool_suggest_first()) { - x += SUGG_LIST_WIDTH*text_font_width_character(st) + 50; + x += SUGG_LIST_WIDTH*st->cwidth + 50; } top= y= ar->winy - st->lheight*l - 2; len= strlen(docs); - boxw= DOC_WIDTH*text_font_width_character(st) + 20; + boxw= DOC_WIDTH*st->cwidth + 20; boxh= (DOC_HEIGHT+1)*st->lheight; /* Draw panel */ @@ -1017,14 +1023,14 @@ static void draw_suggestion_list(SpaceText *st, ARegion *ar) if(l<0) return; if(st->showlinenrs) { - x = text_font_width_character(st)*(st->text->curc-st->left) + TXT_OFFSET + TEXTXLOC - 4; + x = st->cwidth*(st->text->curc-st->left) + TXT_OFFSET + TEXTXLOC - 4; } else { - x = text_font_width_character(st)*(st->text->curc-st->left) + TXT_OFFSET - 4; + x = st->cwidth*(st->text->curc-st->left) + TXT_OFFSET - 4; } y = ar->winy - st->lheight*l - 2; - boxw = SUGG_LIST_WIDTH*text_font_width_character(st) + 20; + boxw = SUGG_LIST_WIDTH*st->cwidth + 20; boxh = SUGG_LIST_SIZE*st->lheight + 8; UI_ThemeColor(TH_SHADE1); @@ -1094,9 +1100,9 @@ static void draw_cursor(SpaceText *st, ARegion *ar) if(vcurl==vsell) { y -= vcurl*st->lheight; if(vcurc < vselc) - glRecti(x+vcurc*text_font_width_character(st)-1, y, x+vselc*text_font_width_character(st), y-st->lheight); + glRecti(x+vcurc*st->cwidth-1, y, x+vselc*st->cwidth, y-st->lheight); else - glRecti(x+vselc*text_font_width_character(st)-1, y, x+vcurc*text_font_width_character(st), y-st->lheight); + glRecti(x+vselc*st->cwidth-1, y, x+vcurc*st->cwidth, y-st->lheight); } else { int froml, fromc, tol, toc; @@ -1111,11 +1117,11 @@ static void draw_cursor(SpaceText *st, ARegion *ar) } y -= froml*st->lheight; - glRecti(x+fromc*text_font_width_character(st)-1, y, ar->winx, y-st->lheight); y-=st->lheight; + glRecti(x+fromc*st->cwidth-1, y, ar->winx, y-st->lheight); y-=st->lheight; for(i=froml+1; i<tol; i++) glRecti(x-4, y, ar->winx, y-st->lheight), y-=st->lheight; - glRecti(x-4, y, x+toc*text_font_width_character(st), y-st->lheight); y-=st->lheight; + glRecti(x-4, y, x+toc*st->cwidth, y-st->lheight); y-=st->lheight; } } else { @@ -1132,13 +1138,13 @@ static void draw_cursor(SpaceText *st, ARegion *ar) if(!hidden) { /* Draw the cursor itself (we draw the sel. cursor as this is the leading edge) */ x= st->showlinenrs ? TXT_OFFSET + TEXTXLOC : TXT_OFFSET; - x += vselc*text_font_width_character(st); + x += vselc*st->cwidth; y= ar->winy-2 - vsell*st->lheight; if(st->overwrite) { char ch= text->sell->line[text->selc]; if(!ch) ch= ' '; - w= text_font_width_character(st); + w= st->cwidth; UI_ThemeColor(TH_HILITE); glRecti(x, y-st->lheight-1, x+w, y-st->lheight+1); } @@ -1238,8 +1244,8 @@ static void draw_brackets(SpaceText *st, ARegion *ar) if(viewc >= 0){ viewl= txt_get_span(text->lines.first, startl) - st->top + offl; - text_font_draw_character(st, x+viewc*text_font_width_character(st), y-viewl*st->lheight, ch); - text_font_draw_character(st, x+viewc*text_font_width_character(st)+1, y-viewl*st->lheight, ch); + text_font_draw_character(st, x+viewc*st->cwidth, y-viewl*st->lheight, ch); + text_font_draw_character(st, x+viewc*st->cwidth+1, y-viewl*st->lheight, ch); } /* draw closing bracket */ @@ -1250,8 +1256,8 @@ static void draw_brackets(SpaceText *st, ARegion *ar) if(viewc >= 0) { viewl= txt_get_span(text->lines.first, endl) - st->top + offl; - text_font_draw_character(st, x+viewc*text_font_width_character(st), y-viewl*st->lheight, ch); - text_font_draw_character(st, x+viewc*text_font_width_character(st)+1, y-viewl*st->lheight, ch); + text_font_draw_character(st, x+viewc*st->cwidth, y-viewl*st->lheight, ch); + text_font_draw_character(st, x+viewc*st->cwidth+1, y-viewl*st->lheight, ch); } } @@ -1261,6 +1267,7 @@ void draw_text_main(SpaceText *st, ARegion *ar) { Text *text= st->text; TextLine *tmp; + rcti scroll; char linenr[12]; int i, x, y, linecount= 0; @@ -1276,7 +1283,7 @@ void draw_text_main(SpaceText *st, ARegion *ar) else st->viewlines= 0; /* update rects for scroll */ - calc_text_rcts(st, ar); + calc_text_rcts(st, ar, &scroll); /* scroll will hold the entire bar size */ /* update syntax formatting if needed */ tmp= text->lines.first; @@ -1288,13 +1295,23 @@ void draw_text_main(SpaceText *st, ARegion *ar) linecount++; } + text_font_begin(st); + st->cwidth= BLF_fixed_width(); + st->cwidth= MAX2(st->cwidth, 1); + /* draw line numbers background */ if(st->showlinenrs) { + st->linenrs_tot = (int)floor(log10((float)(linecount + st->viewlines))) + 1; + x= TXT_OFFSET + TEXTXLOC; + UI_ThemeColor(TH_GRID); - glRecti(23, 0, (st->lheight==15)? 63: 59, ar->winy - 2); + glRecti((TXT_OFFSET-12), 0, (TXT_OFFSET-5) + TEXTXLOC, ar->winy - 2); } - - text_font_begin(st); + else { + st->linenrs_tot= 0; /* not used */ + x= TXT_OFFSET; + } + y= ar->winy-st->lheight; /* draw cursor */ draw_cursor(st, ar); @@ -1302,9 +1319,6 @@ void draw_text_main(SpaceText *st, ARegion *ar) /* draw the text */ UI_ThemeColor(TH_TEXT); - y= ar->winy-st->lheight; - x= (st->showlinenrs)? TXT_OFFSET + TEXTXLOC: TXT_OFFSET; - for(i=0; y>0 && i<st->viewlines && tmp; i++, tmp= tmp->next) { if(st->showsyntax && !tmp->format) txt_format_line(st, tmp, 0); @@ -1316,14 +1330,9 @@ void draw_text_main(SpaceText *st, ARegion *ar) else UI_ThemeColor(TH_TEXT); - if(((float)(i + linecount + 1)/10000.0) < 1.0) { - sprintf(linenr, "%4d", i + linecount + 1); - text_font_draw(st, TXT_OFFSET - 7, y, linenr); - } - else { - sprintf(linenr, "%5d", i + linecount + 1); - text_font_draw(st, TXT_OFFSET - 11, y, linenr); - } + sprintf(linenr, "%d", i + linecount + 1); + /* itoa(i + linecount + 1, linenr, 10); */ /* not ansi-c :/ */ + text_font_draw(st, TXT_OFFSET - 7, y, linenr); UI_ThemeColor(TH_TEXT); } @@ -1343,7 +1352,8 @@ void draw_text_main(SpaceText *st, ARegion *ar) /* draw other stuff */ draw_brackets(st, ar); draw_markers(st, ar); - draw_textscroll(st, ar); + glTranslatef(0.375f, 0.375f, 0.0f); /* XXX scroll requires exact pixel space */ + draw_textscroll(st, ar, &scroll); draw_documentation(st, ar); draw_suggestion_list(st, ar); @@ -1352,6 +1362,14 @@ void draw_text_main(SpaceText *st, ARegion *ar) /************************** update ***************************/ +void text_update_character_width(SpaceText *st) +{ + text_font_begin(st); + st->cwidth= BLF_fixed_width(); + st->cwidth= MAX2(st->cwidth, 1); + text_font_end(st); +} + /* Moves the view to the cursor location, also used to make sure the view isnt outside the file */ void text_update_cursor_moved(SpaceText *st, ARegion *ar) @@ -1361,6 +1379,8 @@ void text_update_cursor_moved(SpaceText *st, ARegion *ar) if(!text || !text->curl) return; + text_update_character_width(st); + i= txt_get_span(text->lines.first, text->sell); if(st->top+st->viewlines <= i || st->top > i) st->top= i - st->viewlines/2; @@ -1372,7 +1392,7 @@ void text_update_cursor_moved(SpaceText *st, ARegion *ar) x= text_draw(st, text->sell->line, st->left, text->selc, 0, 0, 0, NULL); if(x==0 || x>ar->winx) - st->left= text->curc-0.5*(ar->winx)/text_font_width_character(st); + st->left= text->curc-0.5*(ar->winx)/st->cwidth; } if(st->top < 0) st->top= 0; |