📄 wg_editbox.cpp
字号:
{ //If the cursor is within the control then check to see if we've already // set the cursor to the I Beam, if we have, don't do anything. If we // havent, set it to the ibeam. //Else if it's outside the control and the I Beam cursor is set, set it // back to a normal cursor. if (m_WindowRect.HitTest(pMouseMessage->Point) == CRect::RELPOS_INSIDE && !m_bLastMouseMoveInside) { m_bLastMouseMoveInside = true; CwgCursorResourceHandle IBeamHandle(WGRES_IBEAM_CURSOR); CApplication::Instance()->SetMouseCursor(&IBeamHandle); } else if (m_WindowRect.HitTest(pMouseMessage->Point) != CRect::RELPOS_INSIDE && m_bLastMouseMoveInside) { m_bLastMouseMoveInside= false; CApplication::Instance()->SetMouseCursor(); } if (m_bMouseDown) { CPoint Offset; std::vector<CRect> CharRects; m_pRenderedString->GetMetrics(0, &Offset, &CharRects); int xDelta = abs(pMouseMessage->Point.XPos() - (CharRects[0].Left() + Offset.XPos() + SubRect.Left() + m_ScrollOffset)); unsigned int CursorPos = 0; for (unsigned int i = 0; i < m_pRenderedString->GetLength(); ++i) { if (abs(pMouseMessage->Point.XPos() - (CharRects[i].Right() + Offset.XPos() + SubRect.Left() + m_ScrollOffset)) < xDelta) { xDelta = abs(pMouseMessage->Point.XPos() - (CharRects[i].Right() + Offset.XPos() + SubRect.Left() + m_ScrollOffset)); CursorPos = i + 1; } } if (CursorPos < m_DragStart) { m_SelLength = m_DragStart - CursorPos; m_SelStart = CursorPos; } else { m_SelStart = m_DragStart; m_SelLength = CursorPos - m_SelStart; } bHandled = true; StartDrawProc(); } } break; } case CMessage::KEYBOARD_KEYDOWN: if (m_bVisible) { CKeyboardMessage* pKeyboardMessage = dynamic_cast<CKeyboardMessage*>(pMessage); if (pKeyboardMessage && pMessage->Destination() == this && !m_bReadOnly) { std::string sBuffer = m_sWindowText; switch(pKeyboardMessage->Key) { case SDLK_BACKSPACE: if (m_SelStart > 0) { if (m_SelLength > 0) { SelDelete(&sBuffer); } else { sBuffer.erase(--m_SelStart, 1); } } break; case SDLK_DELETE: if (static_cast<unsigned int>(m_SelStart) < sBuffer.length()) { if (m_SelLength > 0) { SelDelete(&sBuffer); } else { sBuffer.erase(m_SelStart, 1); } } break; case SDLK_LEFT: if (pKeyboardMessage->Modifiers & KMOD_SHIFT) //Shift modifier { if (m_SelStart > 0) { if ((m_SelLength > 0) || ((m_SelStart - abs(m_SelLength)) > 0)) { if (pKeyboardMessage->Modifiers & KMOD_CTRL) { unsigned int pos = static_cast<unsigned int>(sBuffer.rfind(" ", (m_SelStart + m_SelLength) - 1)); if (pos != std::string::npos) { m_SelLength = pos - m_SelStart; } else { m_SelLength = m_SelStart * -1; } } else { m_SelLength--; } } } } else if (m_SelLength != 0) { if (m_SelLength < 0) { m_SelStart = m_SelStart + m_SelLength; } m_SelLength = 0; } else if (m_SelStart > 0) { if (pKeyboardMessage->Modifiers & KMOD_CTRL) { unsigned int pos = static_cast<unsigned int>(sBuffer.rfind(" ", m_SelStart - 1)); if (pos != std::string::npos) { m_SelStart = pos; } else { m_SelStart = 0; } } else { --m_SelStart; } m_SelLength = 0; } break; case SDLK_RIGHT: if (static_cast<unsigned int>(m_SelStart) <= sBuffer.length()) { if (pKeyboardMessage->Modifiers & KMOD_SHIFT) { if (pKeyboardMessage->Modifiers & KMOD_CTRL) { unsigned int pos = static_cast<unsigned int>(sBuffer.find(" ", m_SelStart + m_SelLength)); if (pos != std::string::npos) { m_SelLength = (pos - m_SelStart) + 1; } else { m_SelLength = static_cast<int>(sBuffer.length()) - m_SelStart; } } else if (m_SelStart + m_SelLength < sBuffer.length()) { m_SelLength++; //Selecting, one character at a time, increase the selection length by one. } } else if(m_SelLength == 0 && static_cast<unsigned int>(m_SelStart) < sBuffer.length()) { //With the ctrl modifier used, we look for the next instance of a space. // If we find one, we set the cursor position to that location. // If we can't find one, we set the cursor position to the end of the string. // If we don't have the ctrl modifier, then we just incriment the cursor position by one character if (pKeyboardMessage->Modifiers & KMOD_CTRL) { unsigned int pos = static_cast<unsigned int>(sBuffer.find(" ", m_SelStart + 1)); if (pos != std::string::npos) { m_SelStart = pos + 1; } else { m_SelStart = static_cast<unsigned int>(sBuffer.length()); } } else { ++m_SelStart; //We don't have anything selected, so we'll just incriment the start position } } else { if (m_SelLength > 0) { m_SelStart = m_SelStart + m_SelLength; //Reset cursor position to the end of the selection } m_SelLength = 0; //Set selection length to zero } } break; case SDLK_END: if (pKeyboardMessage->Modifiers & KMOD_SHIFT) { m_SelLength = static_cast<unsigned int>(sBuffer.length()) - m_SelStart; } else { m_SelLength = 0; m_SelStart = static_cast<unsigned int>(sBuffer.length()); } break; case SDLK_HOME: if (pKeyboardMessage->Modifiers & KMOD_SHIFT) { m_SelLength = m_SelStart; m_SelStart = 0; } else { m_SelLength = 0; m_SelStart = 0; } break; default: if (pKeyboardMessage->Unicode) { if ((pKeyboardMessage->Unicode & 0xFF80) == 0) { SelDelete(&sBuffer); sBuffer.insert(m_SelStart++, 1, static_cast<char>(pKeyboardMessage->Unicode & 0x7F)); } else { Trace("CEditBox::HandleMessage : CEditBox can't handle Unicode characters yet."); } } break; } if (m_sWindowText != sBuffer) { CMessageServer::Instance().QueueMessage(new TStringMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, sBuffer)); } m_sWindowText = sBuffer; CWindow::SetWindowText(sBuffer); std::auto_ptr<CRenderedString> pRenderedString(new CRenderedString( m_pFontEngine, sBuffer, CRenderedString::VALIGN_NORMAL, CRenderedString::HALIGN_LEFT)); m_pRenderedString = pRenderedString; StartDrawProc(); } break; } default : bHandled = CWindow::HandleMessage(pMessage); break; } } return bHandled;}void CEditBox::SelDelete(std::string* psString){ //This means we've selected something, therefore, should replace it if (m_SelLength > 0) { unsigned int SelStartNorm=0, SelLenNorm=0; if (m_SelLength < 0) { SelStartNorm = m_SelLength + m_SelStart; SelLenNorm = abs(m_SelLength); } else { SelStartNorm = m_SelStart; SelLenNorm = m_SelLength; } psString->erase(SelStartNorm, SelLenNorm); m_SelLength = 0; }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -