void LineEditor::_receive( const Msg_p& pMsg ) { Widget::_receive(pMsg); MsgRouter_p pHandler = Base::msgRouter(); MsgType event = pMsg->type(); if( event == MsgType::Tick ) { if( _isSelectable() && m_state.isFocused() ) { m_text.incTime( TickMsg::cast(pMsg)->timediff() ); _requestRender(); //TODO: Should only render the cursor and selection! } return; } if( (event == MsgType::MousePress || event == MsgType::MouseDrag) && MouseButtonMsg::cast(pMsg)->button() == MouseButton::Left ) { MouseButtonMsg_p pButtonMsg = MouseButtonMsg::cast(pMsg); if( !m_state.isFocused() ) grabFocus(); if( m_state.isFocused() ) { if( _isSelectable() && (pButtonMsg->modKeys() & MODKEY_SHIFT) ) { m_text.setSelectionMode(true); } Coord ofs = pButtonMsg->pointerPos() - globalPos(); int x = ofs.x + m_viewOfs; int y = 0; if( m_bPasswordMode ) { TextAttr attr; m_text.getBaseAttr( attr ); Pen pen; pen.setAttributes( attr ); pen.setChar(m_pwGlyph); pen.advancePos(); int spacing = pen.getPosX(); int height = pen.getLineSpacing(); int line = y/height; int col = (x+spacing/2)/spacing; if(col < 0) { col = 0; line = 0; } m_text.gotoSoftPos(line,col); } else { m_text.cursorGotoCoord( Coord(x, 0), Rect(0,0,1000000,1000000) ); } if(_isSelectable() && event == MsgType::MousePress && !(pButtonMsg->modKeys() & MODKEY_SHIFT)) { m_text.clearSelection(); m_text.setSelectionMode(true); } } _adjustViewOfs(); } if( event == MsgType::MouseRelease ) { if( m_state.isFocused() && MouseButtonMsg::cast(pMsg)->button() == MouseButton::Left ) m_text.setSelectionMode(false); } if( event == MsgType::TextInput ) { String str = TextInputMsg::cast(pMsg)->text(); if( _isEditable() && m_state.isFocused() ) { if(m_text.hasSelection()) m_text.delSelection(); m_text.setSelectionMode(false); bool bModified = false; for( int i = 0 ; i < str.length() ; i++ ) { unsigned short ch = str.chars()[i].getGlyph(); if( ch >= 32 && ch != 127 ) { if( m_text.putChar( ch ) ) bModified = true; } } if( bModified ) { if( pHandler ) pHandler->post( new TextEditMsg(text.ptr(),false) ); _adjustViewOfs(); } } } if( event == MsgType::KeyRelease && m_state.isFocused() ) { Key key = KeyMsg::cast(pMsg)->translatedKeyCode(); switch( key ) { case Key::Shift: if(!Base::inputHandler()->isButtonPressed(MouseButton::Left)) m_text.setSelectionMode(false); break; } } if( (event == MsgType::KeyPress || event == MsgType::KeyRepeat) && _isEditable() && m_state.isFocused() ) { KeyMsg_p pKeyMsg = KeyMsg::cast(pMsg); Key key = pKeyMsg->translatedKeyCode(); switch( key ) { case Key::Left: if( pKeyMsg->modKeys() & MODKEY_SHIFT ) m_text.setSelectionMode(true); if( pKeyMsg->modKeys() & MODKEY_CTRL ) { if( m_bPasswordMode ) m_text.goBol(); else m_text.gotoPrevWord(); } else { m_text.goLeft(); } break; case Key::Right: if( pKeyMsg->modKeys() & MODKEY_SHIFT ) m_text.setSelectionMode(true); if( pKeyMsg->modKeys() & MODKEY_CTRL ) { if( m_bPasswordMode ) m_text.goEol(); else m_text.gotoNextWord(); } else { m_text.goRight(); } break; case Key::Backspace: { if(m_text.hasSelection()) m_text.delSelection(); else if( (pKeyMsg->modKeys() & MODKEY_CTRL) && !m_bPasswordMode) m_text.delPrevWord(); else m_text.delPrevChar(); if( pHandler ) pHandler->post( new TextEditMsg(text.ptr(),false) ); break; } case Key::Delete: { if(m_text.hasSelection()) m_text.delSelection(); else if( (pKeyMsg->modKeys() & MODKEY_CTRL) && !m_bPasswordMode) m_text.delNextWord(); else m_text.delNextChar(); if( pHandler ) pHandler->post( new TextEditMsg(text.ptr(),false) ); break; } case Key::Home: /* * I am not sure if this is the proper way to this, but in my opinion, the default * "actions" has to be separated from any modifier key action combination */ switch( pKeyMsg->modKeys() ) { case MODKEY_CTRL: break; default: // no modifier key was pressed if(pKeyMsg->modKeys() & MODKEY_SHIFT ) m_text.setSelectionMode(true); m_text.goBol(); break; } break; case Key::End: /* * I am not sure if this is the proper way to this, but in my opinion, the default * "actions" has to be separated from any modifier key action combination */ switch( pKeyMsg->modKeys() ) { case MODKEY_CTRL: break; default: // no modifier key was pressed if( pKeyMsg->modKeys() & MODKEY_SHIFT ) m_text.setSelectionMode(true); m_text.goEol(); break; } break; default: break; } _adjustViewOfs(); } // Swallow message depending on rules. if( pMsg->isMouseButtreceive() ) { if( MouseButtonMsg::cast(pMsg)->button() == MouseButton::Left ) pMsg->swallow(); } else if( pMsg->isKeyMsg() ) { Key key = KeyMsg::cast(pMsg)->translatedKeyCode(); if( KeyMsg::cast(pMsg)->isMovementKey() == true || key == Key::Delete || key == Key::Backspace ) pMsg->swallow(); //TODO: Would be good if we didn't forward any character-creating keys either... } }
void Button::_receive( const Msg_p& _pMsg ) { State state = m_state; MsgRouter_p pHandler = Base::msgRouter(); switch( _pMsg->type() ) { case MsgType::KeyPress: if( KeyPressMsg::cast(_pMsg)->translatedKeyCode() == Key::Return ) { m_bReturnPressed = true; _pMsg->swallow(); } break; case MsgType::KeyRepeat: if( KeyRepeatMsg::cast(_pMsg)->translatedKeyCode() == Key::Return ) _pMsg->swallow(); break; case MsgType::KeyRelease: if( KeyReleaseMsg::cast(_pMsg)->translatedKeyCode() == Key::Return ) { m_bReturnPressed = false; pHandler->post( new SelectMsg(this) ); _pMsg->swallow(); } break; case MsgType::MouseEnter: state.setHovered(true); break; case MsgType::MouseLeave: state.setHovered(false); break; case MsgType::MousePress: if( MousePressMsg::cast(_pMsg)->button() == MouseButton::Left ) { m_bPressed = true; _pMsg->swallow(); } break; case MsgType::MouseRelease: if( MouseReleaseMsg::cast(_pMsg)->button() == MouseButton::Left ) { m_bPressed = false; _pMsg->swallow(); } break; case MsgType::MouseClick: if( MouseClickMsg::cast(_pMsg)->button() == MouseButton::Left ) { pHandler->post( new SelectMsg(this) ); _pMsg->swallow(); } break; case MsgType::MouseDoubleClick: case MsgType::MouseRepeat: case MsgType::MouseDrag: if( MouseButtonMsg::cast(_pMsg)->button() == MouseButton::Left ) _pMsg->swallow(); break; case MsgType::FocusGained: state.setFocused(true); break; case MsgType::FocusLost: state.setFocused(false); m_bReturnPressed = false; m_bPressed = false; break; default: break; } if( m_bReturnPressed || (m_bPressed && (m_bDownOutside || state.isHovered() )) ) state.setPressed(true); else state.setPressed(false); if( state != m_state ) _setState(state); }