/** 根据像素坐标设置文本光标的行列坐标 */ int TextLayer_SetCaretPosByPixelPos( LCUI_TextLayer* layer, int x, int y ) { TextRowData* p_row; int i, pixel_pos, ins_x, ins_y; for( pixel_pos=0, i=0; i<layer->row_list.rows; ++i ) { p_row = layer->row_list.rowdata[i]; pixel_pos += p_row->top_spacing; pixel_pos += p_row->max_height; pixel_pos += p_row->bottom_spacing; if( pixel_pos >= y ) { ins_y = i; break; } } if( i >= layer->row_list.rows ) { if( layer->row_list.rows > 0 ) { ins_y = layer->row_list.rows-1; } else { layer->insert_x = 0; layer->insert_y = 0; return -1; } } p_row = layer->row_list.rowdata[ins_y]; ins_x = p_row->string_len; /* 根据文本对齐方式,确定当前行文本的起始坐标 */ switch( layer->text_align ) { case TEXT_ALIGN_CENTER: pixel_pos = (layer->max_width - p_row->max_width)/2; break; case TEXT_ALIGN_RIGHT: pixel_pos = layer->max_width - p_row->max_width; break; case TEXT_ALIGN_LEFT: default: pixel_pos = 0; break; } for( i=0; i<p_row->string_len; ++i ) { if( !p_row->string[i]->bitmap ) { continue; } pixel_pos += p_row->string[i]->bitmap->advance.x; /* 如果在当前字中心点的前面 */ if( x <= pixel_pos - p_row->string[i]->bitmap->advance.x/2 ) { ins_x = i; break; } } TextLayer_SetCaretPos( layer, ins_y, ins_x ); return 0; }
/** 退格删除文本,即删除文本光标的当前坐标左边的文本 */ int TextLayer_Backspace( LCUI_TextLayer* layer, int n_char ) { int n_del; int char_x, char_y; TextRowData* p_row; /* 先获取当前字的位置 */ char_x = layer->insert_x; char_y = layer->insert_y; /* 再计算删除 n_char 个字后的位置 */ for( n_del=n_char; char_y>=0; --char_y ) { p_row = layer->row_list.rowdata[char_y]; /* 如果不是当前行,则重定位至行尾 */ if( char_y < layer->insert_y ) { char_x = p_row->string_len; if( p_row->eol == EOL_NONE ) { --char_x; } } if( char_x >= n_del ) { char_x = char_x - n_del; n_del = 0; break; } n_del = n_del - char_x - 1; } if( char_y < 0 && n_del == n_char ) { return -1; } /* 若能够被删除的字不够 n_char 个,则调整需删除的字数 */ if( n_del > 0 ) { n_char -= n_del; } /* 开始删除文本 */ TextLayer_DeleteText( layer, char_y, char_x, n_char ); /* 若最后一行被完全移除,则移动输入点至上一行的行尾处 */ if( char_x == 0 && layer->row_list.rows > 0 && char_y >= layer->row_list.rows ) { char_y = layer->row_list.rows-1; char_x = layer->row_list.rowdata[char_y]->string_len; } /* 更新文本光标的位置 */ TextLayer_SetCaretPos( layer, char_y, char_x ); return 0; }
/** 退格删除文本,即删除文本光标的当前坐标左边的文本 */ int TextLayer_TextBackspace( LCUI_TextLayer layer, int n_char ) { int n_del; int char_x, char_y; TextRow txtrow; /* 先获取当前字的位置 */ char_x = layer->insert_x; char_y = layer->insert_y; /* 再计算删除 n_char 个字后的位置 */ for( n_del = n_char; char_y >= 0; --char_y ) { txtrow = layer->rowlist.rows[char_y]; /* 如果不是当前行,则重定位至行尾 */ if( char_y < layer->insert_y ) { char_x = txtrow->length; if( txtrow->eol == EOL_NONE ) { --char_x; } } if( char_x >= n_del ) { char_x = char_x - n_del; n_del = 0; break; } n_del = n_del - char_x - 1; } if( char_y < 0 || n_del == n_char ) { return -1; } /* 若能够被删除的字不够 n_char 个,则调整需删除的字数 */ if( n_del > 0 ) { n_char -= n_del; } /* 开始删除文本 */ TextLayer_TextDeleteEx( layer, char_y, char_x, n_char ); /* 若最后一行被完全移除,则移动输入点至上一行的行尾处 */ if( char_x == 0 && layer->rowlist.length > 0 && char_y >= layer->rowlist.length ) { char_y = layer->rowlist.length - 1; char_x = layer->rowlist.rows[char_y]->length; } /* 更新文本光标的位置 */ TextLayer_SetCaretPos( layer, char_y, char_x ); return 0; }
/** 根据像素坐标设置文本光标的行列坐标 */ int TextLayer_SetCaretPosByPixelPos( LCUI_TextLayer layer, int x, int y ) { TextRow txtrow; int i, pixel_pos, ins_x, ins_y; for( pixel_pos = 0, i = 0; i < layer->rowlist.length; ++i ) { pixel_pos += layer->rowlist.rows[i]->height;; if( pixel_pos >= y ) { ins_y = i; break; } } if( i >= layer->rowlist.length ) { if( layer->rowlist.length > 0 ) { ins_y = layer->rowlist.length - 1; } else { layer->insert_x = 0; layer->insert_y = 0; return -1; } } txtrow = layer->rowlist.rows[ins_y]; ins_x = txtrow->length; pixel_pos = TextLayer_GetRowStartX( layer, txtrow ); for( i = 0; i < txtrow->length; ++i ) { TextChar txtchar; txtchar = txtrow->string[i]; if( !txtchar->bitmap ) { continue; } pixel_pos += txtchar->bitmap->advance.x; /* 如果在当前字中心点的前面 */ if( x <= pixel_pos - txtchar->bitmap->advance.x / 2 ) { ins_x = i; break; } } TextLayer_SetCaretPos( layer, ins_y, ins_x ); return 0; }