int QUimTextUtil::acquireSelectionTextInQ3TextEdit( enum UTextOrigin origin, int former_req_len, int latter_req_len, char **former, char **latter ) { Q3TextEdit *edit = static_cast<Q3TextEdit *>( mWidget ); QString text; int len, offset, newline; int start_para, start_index, end_para, end_index; int para, index; bool cursor_at_beginning = false; Qt::TextFormat format; if ( ! edit->hasSelectedText() ) return -1; format = edit->textFormat(); edit->setTextFormat( Qt::PlainText ); edit->getCursorPosition( ¶, &index ); edit->getSelection(&start_para, &start_index, &end_para, &end_index, 0 ); if ( para == start_para && index == start_index ) cursor_at_beginning = true; text = edit->selectedText(); len = text.length(); if ( origin == UTextOrigin_Beginning || ( origin == UTextOrigin_Cursor && cursor_at_beginning ) ) { *former = 0; offset = 0; if ( latter_req_len >= 0 ) { if ( len > latter_req_len ) offset = len - latter_req_len; } else { if (! ( ~latter_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) { edit->setTextFormat( format ); return -1; } if ( latter_req_len == UTextExtent_Line && ( ( newline = text.indexOf( '\n' ) ) != -1 ) ) offset = len - newline; } *latter = strdup( text.left( len - offset ).toUtf8().data() ); } else if ( origin == UTextOrigin_End || ( origin == UTextOrigin_Cursor && !cursor_at_beginning ) ) { offset = 0; if ( former_req_len >= 0 ) { if ( len > former_req_len ) offset = len - former_req_len; } else { if (! ( ~former_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) { edit->setTextFormat( format ); return -1; } if ( former_req_len == UTextExtent_Line && ( ( newline = text.lastIndexOf( '\n' ) ) != -1 ) ) offset = newline + 1; } *former = strdup( text.mid( offset, len - offset ).toUtf8().data() ); *latter = 0; } else { edit->setTextFormat( format ); return -1; } edit->setTextFormat( format ); return 0; }
int QUimTextUtil::deleteSelectionTextInQ3TextEdit( enum UTextOrigin origin, int former_req_len, int latter_req_len ) { Q3TextEdit *edit = static_cast<Q3TextEdit *>( mWidget ); QString text; int len, newline; int para, index; int sel_para_from, sel_index_from, sel_para_to, sel_index_to; int start_para, start_index, end_para, end_index; bool cursor_at_beginning = false; if ( ! edit->hasSelectedText() ) return -1; edit->getCursorPosition( ¶, &index ); edit->getSelection( &sel_para_from, &sel_index_from, &sel_para_to, &sel_index_to, 0 ); if ( para == sel_para_from && index == sel_index_from ) cursor_at_beginning = true; text = edit->selectedText(); len = text.length(); start_para = sel_para_from; start_index = sel_index_from; end_para = sel_para_to; end_index = sel_index_to; if ( origin == UTextOrigin_Beginning || ( origin == UTextOrigin_Cursor && cursor_at_beginning ) ) { edit->setCursorPosition( sel_para_from, sel_index_from ); if ( latter_req_len >= 0 ) { if ( len > latter_req_len ) { end_para = sel_para_from; end_index = sel_index_from; for ( int i = 0; i < latter_req_len; i++) Q3TextEditPositionForward( &end_para, &end_index ); } } else { if (! ( ~latter_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; if ( latter_req_len == UTextExtent_Line && ( ( newline = text.indexOf('\n') ) != -1 ) ) { end_para = sel_para_from; end_index = sel_index_from + newline; } } } else if ( origin == UTextOrigin_End || ( origin == UTextOrigin_Cursor && !cursor_at_beginning ) ) { if ( former_req_len >= 0 ) { if ( len > former_req_len ) { start_para = sel_para_to; start_index = sel_index_to; for ( int i = 0; i < former_req_len; i++) Q3TextEditPositionBackward( &start_para, &start_index ); } } else { if (! ( ~former_req_len & ( ~UTextExtent_Line | ~UTextExtent_Full ) ) ) return -1; if ( former_req_len == UTextExtent_Line && ( ( newline = text.lastIndexOf( '\n' ) ) != -1 ) ) { start_para = sel_para_to; start_index = 0; } } } else { return -1; } edit->setSelection( start_para, start_index, end_para, end_index, 1 ); edit->removeSelectedText( 1 ); return 0; }