public int getContentWidth() {
   FontRenderer font = getFontRenderer();
   int width = 0;
   for (int i = 0; i < textLength; ++i) {
     width += font.getCharWidth(text[i]);
   }
   return width;
 }
  protected void findRenderStart() {

    caret = MathHelper.clampI(caret, 0, textLength);
    if (caret < renderStart) {
      renderStart = caret;
      return;
    }

    FontRenderer font = getFontRenderer();
    int endX = sizeX - 2;

    for (int i = renderStart, width = 0; i < caret; ++i) {
      width += font.getCharWidth(text[i]);
      while (width >= endX) {
        width -= font.getCharWidth(text[renderStart++]);
        if (renderStart >= textLength) {
          return;
        }
      }
    }
  }
 public int getVisibleWidth() {
   FontRenderer font = getFontRenderer();
   int width = 0, endX = sizeX - 1;
   for (int i = renderStart; i < textLength; ++i) {
     int charW = font.getCharWidth(text[i]);
     if (!enableStencil && (width + charW) > endX) {
       break;
     }
     width += charW;
     if (width >= endX) {
       width = Math.min(width, endX);
       break;
     }
   }
   return width;
 }
  @Override
  public boolean onMousePressed(int mouseX, int mouseY, int mouseButton) {

    selecting = mouseButton == 0;
    l:
    if (selecting) {
      if (textLength == 0) {
        selectionStart = selectionEnd = caret = 0;
        break l;
      }
      FontRenderer font = getFontRenderer();
      int pos = mouseX - posX - 1;
      for (int i = renderStart, width = 0; ; ) {
        int charW = font.getCharWidth(text[i]);
        if ((width += charW) > pos || ++i >= textLength) {
          selectionStart = selectionEnd = caret = i;
          break;
        }
      }
    }

    setFocused(true);
    return true;
  }
  @Override
  public void drawForeground(int mouseX, int mouseY) {

    if (enableStencil) {
      glEnable(GL_STENCIL_TEST);
      glClear(GL_STENCIL_BUFFER_BIT);
      drawStencil(posX + 1, posY + 1, posX + sizeX - 1, posY + sizeY - 1, 1);
    }

    FontRenderer font = getFontRenderer();
    char[] text = this.text;
    int startX = posX + paddingLeft,
        endX = sizeX - paddingRight,
        startY = posY + paddingTop,
        endY = startY + font.FONT_HEIGHT + paddingBottom;
    for (int i = renderStart, width = 0; i <= textLength; ++i) {
      boolean end = i == textLength;
      int charW = 2;
      if (!end) {
        charW = font.getCharWidth(text[i]);
        if (!enableStencil && (width + charW) > endX) {
          break;
        }
      }

      boolean drawCaret = i == caret && (caretCounter &= 31) < 16 && isFocused();
      if (drawCaret) {
        int caretEnd = width + 2;
        if (caretInsert) {
          caretEnd = width + charW;
        }
        drawModalRect(
            startX + width,
            startY - 1,
            startX + caretEnd,
            endY,
            (0xFF000000 & defaultCaretColor) | (~defaultCaretColor & 0xFFFFFF));
      }

      if (!end) {
        boolean selected = i >= selectionStart & i < selectionEnd;
        if (selected) {
          drawModalRect(startX + width, startY, startX + width + charW, endY, selectedLineColor);
        }
        font.drawStringWithShadow(
            String.valueOf(text[i]),
            startX + width,
            startY,
            selected ? selectedTextColor : textColor);
      }

      if (drawCaret) {
        int caretEnd = width + 2;
        if (caretInsert) {
          caretEnd = width + charW;
        }

        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_ONE_MINUS_DST_COLOR, GL11.GL_ZERO);
        gui.drawSizedModalRect(startX + width, startY - 1, startX + caretEnd, endY, -1);
        GL11.glDisable(GL11.GL_BLEND);
      }

      width += charW;
      if (width > endX) {
        break;
      }
    }

    if (enableStencil) {
      glDisable(GL_STENCIL_TEST);
    }
  }