private Rectangle computeCaretRect(int selRow, ST selType) {
    Rectangle rect;
    FontMetrics fm = painter.getFontMetrics(painter.getFont());
    Insets i = painter.getInsets();
    int gw = fm.getMaxAdvance(), gh = fm.getHeight();
    if (selType == ST.RECT) {
      rect = new Rectangle(1 + i.left + col * gw, // Position of the selection
          i.top + Math.min(row, selRow) * gh, // Top of the selection
          insert? 1 : gw, // Width of the glyph in overwrite mode, 1 in insert mode
          (Math.abs(row - selRow) + 1) * gh); // Height of selection
    } else {
      int off = insert? 1 : 0;
      rect = new Rectangle(off + i.left + joshText.line_wid_at(row, col), // Position of glyph, plus offset
          i.top + row * gh, // Top of the glyph
          insert? 1 : gw, // Width of the glyph in overwrite mode, 1 in insert mode
          gh); // Height of the glyph
    }
    return rect;
  }