behavior when key is pressed.
| 358 | |
| 359 | // behavior when key is pressed. |
| 360 | void UIEdit::OnKeyDown(int key) { |
| 361 | int ascii = grfont_KeyToAscii(m_FontHandle, key); |
| 362 | int len = strlen(m_TextBuf); |
| 363 | int i; |
| 364 | |
| 365 | if (!m_Active) { |
| 366 | UIEdit::Activate(); |
| 367 | } |
| 368 | |
| 369 | if (key == KEY_ESC || key == KEY_ENTER) { |
| 370 | UIEdit::Deactivate(); |
| 371 | if (key == KEY_ENTER) { |
| 372 | UIGadget::OnSelect(); // do this to enforce pressing enter ONLY causes OnSelect to work. |
| 373 | } |
| 374 | } else if (key == KEY_DELETE) { |
| 375 | for (i = m_CurPos; i < len; i++) |
| 376 | m_TextBuf[i] = m_TextBuf[i + 1]; |
| 377 | } else { |
| 378 | if (m_CurPos < m_BufSize) { |
| 379 | if (CHECK_FLAG(m_Flags, UIED_NUMBERS)) { |
| 380 | if (ascii < '0' || ascii > '9') { |
| 381 | goto skip_add_to_buffer; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (ascii >= 32 && ascii < 127 && len < m_BufSize) { |
| 386 | // insert character |
| 387 | for (i = len; i >= m_CurPos; i--) |
| 388 | m_TextBuf[i + 1] = m_TextBuf[i]; |
| 389 | // adjust scroll region if cursor will go out of visible region. |
| 390 | mprintf(0, "scroll=%d, curpos=%d\n", m_ScrollThresh, m_CurPos); |
| 391 | // if (m_ScrollThresh<=m_CurPos && m_ScrollThresh) |
| 392 | // m_StartPos++; |
| 393 | m_TextBuf[m_CurPos++] = (char)ascii; |
| 394 | m_UpdateCount = true; |
| 395 | if (m_CurPos == m_BufSize && CHECK_FLAG(m_Flags, UIED_AUTOSELECT)) { |
| 396 | UIEdit::Deactivate(); |
| 397 | UIGadget::OnSelect(); |
| 398 | return; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | skip_add_to_buffer: |
| 404 | if (m_CurPos < len) { |
| 405 | if (key == KEY_RIGHT) { |
| 406 | if (m_ScrollThresh <= m_CurPos && m_ScrollThresh) { |
| 407 | m_StartPos++; |
| 408 | } |
| 409 | // Update_count = true; |
| 410 | m_CurPos++; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (m_CurPos > 0) { |
| 415 | if (key == KEY_LEFT) { |
| 416 | m_CurPos--; |
| 417 | } else if (key == KEY_BACKSP) { |
nothing calls this directly
no test coverage detected