+
+ // Draw Selection Background all at once here
+ if(selected || line == this.line || line == this.selLine)
+ {
+ int sx = XOFFSET + x, sy = y;
+ int tw, th;
+ int oh = 0;
+ char * buffer = line.buffer;
+ if(line != this.line && line != this.selLine)
+ {
+ if(style.freeCaret)
+ {
+ tw = clientSize.w - sx;
+ th = space.h;
+ }
+ else
+ {
+ surface.TextExtent2(buffer, line.count, &tw, &th, 0, null, &oh);
+ tw += countTabsExtraSpaces(buffer, tabSize, 0, line.count) * space.w;
+ }
+ }
+ else if(line == selStartLine)
+ {
+ int prevGlyph = 0;
+ int start = Min(line.count, selStartX);
+ int end = Min(line.count, selEndX);
+ surface.TextExtent2(buffer, start, &tw, &th, 0, &prevGlyph, null);
+ sx += tw;
+ sx += countTabsExtraSpaces(buffer, tabSize, 0, start) * space.w;
+ if(selStartX > start) sx += space.w * (selStartX - start);
+ if(style.freeCaret && line != selEndLine)
+ {
+ tw = clientSize.w - sx;
+ th = space.h;
+ }
+ else if(line != selEndLine)
+ {
+ surface.TextExtent2(buffer + start, line.count - start, &tw, &th, prevGlyph, null, &oh);
+ tw += countTabsExtraSpaces(buffer, tabSize, start, line.count) * space.w;
+ }
+ else
+ {
+ surface.TextExtent2(buffer + start, end - start, &tw, &th, prevGlyph, null, &oh);
+ tw += countTabsExtraSpaces(buffer, tabSize, start, end) * space.w;
+ end = Max(end, selStartX);
+ if(selEndX > end) { tw += space.w * (selEndX - end); th = space.h; }
+ }
+ }
+ else if(line == selEndLine)
+ {
+ int end = Min(line.count, selEndX);
+ surface.TextExtent2(buffer, end, &tw, &th, 0, null, &oh);
+ tw += countTabsExtraSpaces(buffer, tabSize, 0, end) * space.w;
+ if(selEndX - end) { tw += space.w * (selEndX - end); th = space.h; }
+ }
+ tw += oh;
+ surface.Area(sx, sy, sx + tw-1, sy + th-1);
+ }
+