text_field.cpp 13.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
#include <escher/text_field.h>
#include <escher/clipboard.h>
#include <assert.h>

/* TextField::ContentView */

TextField::ContentView::ContentView(char * textBuffer, char * draftTextBuffer, size_t textBufferSize, KDText::FontSize size, float horizontalAlignment, float verticalAlignment, KDColor textColor, KDColor backgroundColor) :
  View(),
  m_isEditing(false),
  m_textBuffer(textBuffer),
  m_draftTextBuffer(draftTextBuffer),
  m_currentDraftTextLength(0),
  m_currentCursorLocation(0),
  m_textBufferSize(textBufferSize),
  m_horizontalAlignment(horizontalAlignment),
  m_verticalAlignment(verticalAlignment),
  m_textColor(textColor),
  m_backgroundColor(backgroundColor),
  m_fontSize(size)
{
  assert(m_textBufferSize <= k_maxBufferSize);
}

void TextField::ContentView::setDraftTextBuffer(char * draftTextBuffer) {
  m_draftTextBuffer = draftTextBuffer;
}

void TextField::ContentView::drawRect(KDContext * ctx, KDRect rect) const {
  KDColor bckCol = m_backgroundColor;
  if (m_isEditing) {
    bckCol = KDColorWhite;
  }
  ctx->fillRect(rect, bckCol);
  ctx->drawString(text(), textOrigin(), m_fontSize, m_textColor, bckCol);
}

void TextField::ContentView::reload() {
  KDSize textSize = KDText::stringSize(text(), m_fontSize);
  KDSize textAndCursorSize = KDSize(textSize.width()+ m_cursorView.minimalSizeForOptimalDisplay().width(), textSize.height());
  KDRect dirtyZone(textOrigin(), textAndCursorSize);
  markRectAsDirty(dirtyZone);
}

const char * TextField::ContentView::text() const {
  if (m_isEditing) {
    return const_cast<const char *>(m_draftTextBuffer);
  }
  return const_cast<const char *>(m_textBuffer);
}

int TextField::ContentView::draftTextLength() const {
  assert(isEditing());
  assert(strlen(text()) == m_currentDraftTextLength);
  return m_currentDraftTextLength;
}

void TextField::ContentView::setText(const char * text) {
  reload();
  if (m_isEditing) {
    strlcpy(m_draftTextBuffer, text, m_textBufferSize);
    int textLength = strlen(text) >= m_textBufferSize ? m_textBufferSize-1 : strlen(text);
    m_currentDraftTextLength = textLength;
  } else {
    strlcpy(m_textBuffer, text, m_textBufferSize);
  }
  reload();
}

void TextField::ContentView::setBackgroundColor(KDColor backgroundColor) {
  m_backgroundColor = backgroundColor;
  markRectAsDirty(bounds());
}

void TextField::ContentView::setTextColor(KDColor textColor) {
  m_textColor = textColor;
  markRectAsDirty(bounds());
}

void TextField::ContentView::setAlignment(float horizontalAlignment, float verticalAlignment) {
  m_horizontalAlignment = horizontalAlignment;
  m_verticalAlignment = verticalAlignment;
  markRectAsDirty(bounds());
}

void TextField::ContentView::setEditing(bool isEditing, bool reinitDrafBuffer) {
  if (m_isEditing == isEditing && !reinitDrafBuffer) {
    return;
  }
  if (reinitDrafBuffer) {
    reinitDraftTextBuffer();
  }
  m_isEditing = isEditing;
  markRectAsDirty(bounds());
  layoutSubviews();
}

void TextField::ContentView::reinitDraftTextBuffer() {
  setCursorLocation(0);
  m_draftTextBuffer[0] = 0;
  m_currentDraftTextLength = 0;
}

void TextField::ContentView::setCursorLocation(int location) {
  int adjustedLocation = location < 0 ? 0 : location;
  adjustedLocation = adjustedLocation > (signed int)m_currentDraftTextLength ? (signed int)m_currentDraftTextLength : adjustedLocation;
  m_currentCursorLocation = adjustedLocation;
  layoutSubviews();
}

bool TextField::ContentView::insertTextAtLocation(const char * text, int location) {
  int textSize = strlen(text);
  if (m_currentDraftTextLength + textSize >= m_textBufferSize || textSize == 0) {
    return false;
  }
  for (int k = m_currentDraftTextLength; k >= location && k >= 0; k--) {
    m_draftTextBuffer[k+textSize] = m_draftTextBuffer[k];
  }
  strlcpy(&m_draftTextBuffer[location], text, textSize);
  if (location+textSize > 0) {
    m_draftTextBuffer[location+textSize-1] = text[textSize-1];
  }
  m_currentDraftTextLength += textSize;
  reload();
  return true;
}

KDSize TextField::ContentView::minimalSizeForOptimalDisplay() const {
  if (m_isEditing) {
    return KDSize(textWidth()+m_cursorView.minimalSizeForOptimalDisplay().width(), textHeight());
  }
  return KDSize(textWidth(), textHeight());
}

KDCoordinate TextField::ContentView::textHeight() const {
  KDSize textSize = KDText::charSize(m_fontSize);
  return textSize.height();
}

KDCoordinate TextField::ContentView::charWidth() const {
  KDSize textSize = KDText::charSize(m_fontSize);
  return textSize.width();
}

KDCoordinate TextField::ContentView::textWidth() const {
  if (m_isEditing) {
    return (strlen(text())+1)*charWidth();
  }
  return strlen(text())*charWidth();
}

void TextField::ContentView::deleteCharPrecedingCursor() {
  if (cursorLocation() <= 0) {
    return;
  }
  reload();
  m_currentDraftTextLength--;
  setCursorLocation(m_currentCursorLocation-1);
  for (int k = m_currentCursorLocation; k < (signed char)m_currentDraftTextLength; k ++) {
    m_draftTextBuffer[k] = m_draftTextBuffer[k+1];
  }
  m_draftTextBuffer[m_currentDraftTextLength] = 0;
  layoutSubviews();
}

bool TextField::ContentView::deleteEndOfLine() {
  if (m_currentDraftTextLength == m_currentCursorLocation) {
    return false;
  }
  reload();
  m_currentDraftTextLength = m_currentCursorLocation;
  m_draftTextBuffer[m_currentCursorLocation] = 0;
  layoutSubviews();
  return true;
}

KDRect TextField::ContentView::cursorRect() {
  return KDRect(m_currentCursorLocation * charWidth(), 0, m_cursorView.minimalSizeForOptimalDisplay());
}

void TextField::ContentView::layoutSubviews() {
  if (!m_isEditing) {
    m_cursorView.setFrame(KDRectZero);
    return;
  }
  KDSize textSize = KDText::stringSize(text(), m_fontSize);
  KDCoordinate cursorWidth = m_cursorView.minimalSizeForOptimalDisplay().width();
  KDRect frame(m_horizontalAlignment*(m_frame.width() - textSize.width()-cursorWidth)+ m_currentCursorLocation * charWidth(), m_verticalAlignment*(m_frame.height() - textSize.height()), cursorWidth, textSize.height());
  m_cursorView.setFrame(frame);
}

TextField::TextField(Responder * parentResponder, char * textBuffer, char * draftTextBuffer,
    size_t textBufferSize, TextFieldDelegate * delegate, bool hasTwoBuffers, KDText::FontSize size,
    float horizontalAlignment, float verticalAlignment, KDColor textColor, KDColor backgroundColor) :
  ScrollableView(parentResponder, &m_contentView, this),
  m_contentView(textBuffer, draftTextBuffer, textBufferSize, size,horizontalAlignment, verticalAlignment, textColor, backgroundColor),
  m_hasTwoBuffers(hasTwoBuffers),
  m_delegate(delegate)
{
}

KDPoint TextField::ContentView::textOrigin() const {
  KDSize textSize = KDText::stringSize(text(), m_fontSize);
  return KDPoint(m_horizontalAlignment*(m_frame.width() - textSize.width()-m_cursorView.minimalSizeForOptimalDisplay().width()), m_verticalAlignment*(m_frame.height() - textSize.height()));
}

void TextField::setDelegate(TextFieldDelegate * delegate) {
  m_delegate = delegate;
}

void TextField::setDraftTextBuffer(char * draftTextBuffer) {
  m_contentView.setDraftTextBuffer(draftTextBuffer);
}

Toolbox * TextField::toolbox() {
  if (m_delegate) {
    return m_delegate->toolboxForTextField(this);
  }
  return nullptr;
}

bool TextField::isEditing() const {
  return m_contentView.isEditing();
}

const char * TextField::text() const {
  return m_contentView.text();
}

int TextField::draftTextLength() const {
  assert(isEditing());
  return m_contentView.draftTextLength();
}

int TextField::cursorLocation() const{
  return m_contentView.cursorLocation();
}

void TextField::setCursorLocation(int location) {
  m_contentView.setCursorLocation(location);
  scrollToCursor();
  layoutSubviews();
}

void TextField::setText(const char * text) {
  reloadScroll();
  m_contentView.setText(text);
  if (isEditing()) {
    setCursorLocation(draftTextLength());
  }
}

void TextField::setBackgroundColor(KDColor backgroundColor) {
  m_contentView.setBackgroundColor(backgroundColor);
}

KDColor TextField::backgroundColor() const {
  return m_contentView.backgroundColor();
}

void TextField::setTextColor(KDColor textColor) {
  m_contentView.setTextColor(textColor);
}

void TextField::setAlignment(float horizontalAlignment, float verticalAlignment) {
  m_contentView.setAlignment(horizontalAlignment, verticalAlignment);
}

void TextField::setEditing(bool isEditing, bool reinitDrafBuffer) {
  m_contentView.setEditing(isEditing, reinitDrafBuffer);
  if (reinitDrafBuffer) {
    reloadScroll();
    layoutSubviews();
  }
}

bool TextField::insertTextAtLocation(const char * text, int location) {
  if (m_contentView.insertTextAtLocation(text, location)) {
    layoutSubviews();
    return true;
  }
  return false;
}

bool TextField::privateHandleEvent(Ion::Events::Event event) {
    assert(m_delegate != nullptr);
  if (m_delegate->textFieldDidReceiveEvent(this, event)) {
    return true;
  }
  if (Responder::handleEvent(event)) {
    /* The only event Responder handles is 'Toolbox' displaying. In that case,
     * the text field is forced into editing mode. */
    if (!isEditing()) {
      setEditing(true);
    }
    return true;
  }
  if (event == Ion::Events::Left && isEditing() && cursorLocation() > 0) {
    setCursorLocation(cursorLocation()-1);
    return true;
  }
  if (event == Ion::Events::ShiftLeft && isEditing()) {
    setCursorLocation(0);
    return true;
  }
  if (event == Ion::Events::Right && isEditing() && cursorLocation() < draftTextLength()) {
    setCursorLocation(cursorLocation()+1);
    return true;
  }
  if (event == Ion::Events::ShiftRight && isEditing()) {
    setCursorLocation(draftTextLength());
    return true;
  }
  if (isEditing() && textFieldShouldFinishEditing(event)) {
    char bufferText[ContentView::k_maxBufferSize];
    strlcpy(bufferText, m_contentView.textBuffer(), ContentView::k_maxBufferSize);
    strlcpy(m_contentView.textBuffer(), m_contentView.draftTextBuffer(), m_contentView.bufferSize());
    int cursorLoc = cursorLocation();
    setEditing(false, m_hasTwoBuffers);
    if (m_delegate->textFieldDidFinishEditing(this, text(), event)) {
      reloadScroll();
      return true;
    }
    /* if the text was refused (textFieldDidFinishEditing returned false, we
     * reset the textfield in the same state as before */
    char bufferDraft[ContentView::k_maxBufferSize];
    strlcpy(bufferDraft, m_contentView.textBuffer(), ContentView::k_maxBufferSize);
    setText(bufferText);
    setEditing(true);
    setText(bufferDraft);
    setCursorLocation(cursorLoc);
    return true;
  }
  if ((event == Ion::Events::OK || event == Ion::Events::EXE) && !isEditing()) {
    setEditing(true);
    /* If the text could not be inserted (buffer is full), we set the cursor
     * at the end of the text. */
    int nextCursorLocation = draftTextLength();
    if (insertTextAtLocation(m_contentView.textBuffer(), cursorLocation())) {
      nextCursorLocation = strlen(m_contentView.draftTextBuffer());
    }
    setCursorLocation(nextCursorLocation);
    return true;
  }
  if (event == Ion::Events::Backspace && isEditing()) {
    deleteCharPrecedingCursor();
    return true;
  }
  if (event.hasText()) {
    if (!isEditing()) {
      setEditing(true);
    }
    int nextCursorLocation = draftTextLength();
    if (insertTextAtLocation(event.text(), cursorLocation())) {
      /* All events whose text is longer than 2 have parenthesis. In these cases,
       * we want to position the cursor before the last parenthesis */
      int cursorDelta = strlen(event.text()) > 2 ? -1 : 0;
      nextCursorLocation = cursorLocation() + strlen(event.text()) + cursorDelta;
    }
    setCursorLocation(nextCursorLocation);
    return true;
  }
  if (event == Ion::Events::Back && isEditing()) {
    setEditing(false);
    reloadScroll();
    m_delegate->textFieldDidAbortEditing(this, text());
    return true;
  }
  if (event == Ion::Events::Clear && isEditing()) {
    if (!deleteEndOfLine()) {
      setEditing(true, true);
    }
    return true;
  }
  if (event == Ion::Events::Copy && !isEditing()) {
    Clipboard::sharedClipboard()->store(text());
    return true;
  }
  if (event == Ion::Events::Cut && !isEditing()) {
    Clipboard::sharedClipboard()->store(text());
    setEditing(true, true);
    return true;
  }
  if (event == Ion::Events::Paste) {
    if (!isEditing()) {
      setEditing(true);
    }
    int nextCursorLocation = draftTextLength();
    if (insertTextAtLocation(Clipboard::sharedClipboard()->storedText(), cursorLocation())) {
      nextCursorLocation = cursorLocation() + strlen(Clipboard::sharedClipboard()->storedText());
    }
    setCursorLocation(nextCursorLocation);
    return true;
  }

  return false;
}

void TextField::deleteCharPrecedingCursor() {
  m_contentView.deleteCharPrecedingCursor();
  scrollToCursor();
  layoutSubviews();
}

bool TextField::deleteEndOfLine() {
  if (m_contentView.deleteEndOfLine()) {
    scrollToCursor();
    layoutSubviews();
    return true;
  }
  return false;
}

KDSize TextField::minimalSizeForOptimalDisplay() const {
  return m_contentView.minimalSizeForOptimalDisplay();
}

bool TextField::textFieldShouldFinishEditing(Ion::Events::Event event) {
  return m_delegate->textFieldShouldFinishEditing(this, event);
}

bool TextField::handleEvent(Ion::Events::Event event) {
  assert(m_delegate != nullptr);
  size_t previousTextLength = strlen(text());
  bool didHandleEvent = privateHandleEvent(event);
  return m_delegate->textFieldDidHandleEvent(this, event, didHandleEvent, strlen(text()) != previousTextLength);
}

void TextField::scrollToCursor() {
  if (!isEditing()) {
    return;
  }
  scrollToContentRect(m_contentView.cursorRect());
}