int main(int argc, char*argv[]) { FILE *fp; char buff[256]; LCUI_Widget *window, *textbox; LCUI_Init(argc, argv); window = Create_Widget("window"); textbox = Create_Widget("text_box"); Set_Window_Title_Text(window, "测试文本框部件"); window->resize( window, Size(320, 240) ); Window_Client_Area_Add( window, textbox ); /* 启用多行文本显示 */ TextBox_Multiline( textbox, TRUE ); textbox->set_align( textbox, ALIGN_MIDDLE_CENTER, Pos(0,0) ); /* 打开README.md文件,并将内容读取至文本框上显示 */ fp = fopen("../README.md", "r"); if( fp ) { while(fgets( buff, sizeof(buff), fp )) { TextBox_Text_Append( textbox, buff ); } fclose( fp ); } Set_Widget_Dock( textbox, DOCK_TYPE_FILL ); textbox->show(textbox); window->show(window); return LCUI_Main(); }
static void TextBox_Init( LCUI_Widget *widget ) /* 初始化文本框相关数据 */ { LCUI_TextBox *textbox; widget->valid_state = WIDGET_STATE_ACTIVE | WIDGET_STATE_OVERLAY; widget->valid_state |= (WIDGET_STATE_NORMAL | WIDGET_STATE_DISABLE); textbox = WidgetPrivData_New(widget, sizeof(LCUI_TextBox)); textbox->text = Widget_New( "label" ); textbox->cursor = Widget_New( NULL ); textbox->scrollbar[0] = Widget_New( "scrollbar" ); textbox->scrollbar[1] = Widget_New( "scrollbar" ); /* 不可获得焦点 */ textbox->text->focus = FALSE; textbox->cursor->focus = FALSE; textbox->scrollbar[0]->focus = FALSE; textbox->scrollbar[1]->focus = FALSE; textbox->limit_mode = 0; textbox->block_size = 256; textbox->show_placeholder = FALSE; LCUIWString_Init( &textbox->placeholder ); TextStyle_Init( &textbox->placeholder_style ); TextStyle_FontColor( &textbox->placeholder_style, RGB(100,100,100) ); Label_AutoSize( textbox->text, FALSE, 0 ); Widget_SetSize( textbox->text, "100%", "100%" ); /* 添加至相应的容器 */ Widget_Container_Add( textbox->text, textbox->cursor ); Widget_Container_Add( widget, textbox->text ); Widget_Container_Add( widget, textbox->scrollbar[0] ); Widget_Container_Add( widget, textbox->scrollbar[1] ); /* 设置滚动条的尺寸 */ Widget_SetSize( textbox->scrollbar[0], "10px", NULL ); Widget_SetSize( textbox->scrollbar[1], NULL, "10px" ); Widget_SetAlign( textbox->scrollbar[0], ALIGN_TOP_RIGHT, Pos(0,0) ); Widget_SetAlign( textbox->scrollbar[1], ALIGN_BOTTOM_LEFT, Pos(0,0) ); /* 滚动条设为横向 */ ScrollBar_SetDirection( textbox->scrollbar[1], 1 ); /* 将回调函数与滚动条连接 */ ScrollBar_Connect( textbox->scrollbar[0], TextBox_TextLayer_VertScroll, widget ); ScrollBar_Connect( textbox->scrollbar[1], TextBox_HoriScroll_TextLayer, widget ); Widget_Show( textbox->text ); Queue_Init( &textbox->text_block_buff, sizeof(LCUI_TextBlock), destroy_textblock ); TextLayer_UsingStyleTags( Label_GetTextLayer(textbox->text), FALSE ); Widget_SetPadding( widget, Padding(2,2,2,2) ); Widget_SetBackgroundColor( textbox->cursor, RGB(0,0,0) ); Widget_SetBackgroundTransparent( textbox->cursor, FALSE ); Widget_SetBackgroundTransparent( widget, FALSE ); Widget_Resize( textbox->cursor, Size(1, 14) ); /* 设置可点击区域的alpha值要满足的条件 */ Set_Widget_ClickableAlpha( textbox->cursor, 0, 1 ); Set_Widget_ClickableAlpha( textbox->text, 0, 1 ); /* 设定定时器,每1秒闪烁一次 */ if( __timer_id == -1 ) { __timer_id = set_timer( 500, blink_cursor, TRUE ); } Widget_Event_Connect( widget, EVENT_DRAG, TextBox_TextLayer_Click ); /* 关联 FOCUS_OUT 和 FOCUS_IN 事件 */ Widget_Event_Connect( widget, EVENT_FOCUSOUT, _putout_textbox_cursor ); Widget_Event_Connect( widget, EVENT_FOCUSIN, _putin_textbox_cursor ); /* 关联按键输入事件 */ Widget_Event_Connect( widget, EVENT_KEYBOARD, TextBox_Input ); /* 默认不启用多行文本模式 */ TextBox_Multiline( widget, FALSE ); }
static void TextBox_Init( LCUI_Widget *widget ) /* 初始化文本框相关数据 */ { LCUI_TextBox *textbox; textbox = Widget_Create_PrivData(widget, sizeof(LCUI_TextBox)); textbox->text = Create_Widget( "label" ); textbox->cursor = Create_Widget( NULL ); textbox->scrollbar[0] = Create_Widget( "scrollbar" ); textbox->scrollbar[1] = Create_Widget( "scrollbar" ); /* 不可获得焦点 */ textbox->text->focus = FALSE; textbox->cursor->focus = FALSE; textbox->scrollbar[0]->focus = FALSE; textbox->scrollbar[1]->focus = FALSE; textbox->limit_mode = 0; textbox->block_size = 256; Label_AutoSize( textbox->text, FALSE, 0 ); Set_Widget_Size( textbox->text, "100%", "100%" ); /* 添加至相应的容器 */ Widget_Container_Add( textbox->text, textbox->cursor ); Widget_Container_Add( widget, textbox->text ); Widget_Container_Add( widget, textbox->scrollbar[0] ); Widget_Container_Add( widget, textbox->scrollbar[1] ); /* 设置滚动条的尺寸 */ Set_Widget_Size( textbox->scrollbar[0], "10px", NULL ); Set_Widget_Size( textbox->scrollbar[1], NULL, "10px" ); Set_Widget_Align( textbox->scrollbar[0], ALIGN_TOP_RIGHT, Pos(0,0) ); Set_Widget_Align( textbox->scrollbar[1], ALIGN_BOTTOM_LEFT, Pos(0,0) ); /* 滚动条设为横向 */ ScrollBar_Set_Direction( textbox->scrollbar[1], 1 ); /* 将回调函数与滚动条连接 */ ScrollBar_Connect( textbox->scrollbar[0], TextBox_VertScroll_TextLayer, widget ); ScrollBar_Connect( textbox->scrollbar[1], TextBox_HoriScroll_TextLayer, widget ); Show_Widget( textbox->text ); Queue_Init( &textbox->text_block_buff, sizeof(LCUI_TextBlock), destroy_textblock ); TextLayer_Using_StyleTags( Label_Get_TextLayer(textbox->text), FALSE ); Set_Widget_Padding( widget, Padding(2,2,2,2) ); Set_Widget_Backcolor( textbox->cursor, RGB(0,0,0) ); Set_Widget_BG_Mode( textbox->cursor, BG_MODE_FILL_BACKCOLOR ); Resize_Widget( textbox->cursor, Size(1, 14) ); /* 设置可点击区域的alpha值要满足的条件 */ Set_Widget_ClickableAlpha( textbox->cursor, 0, 1 ); Set_Widget_ClickableAlpha( textbox->text, 0, 1 ); /* 设定定时器,每1秒闪烁一次 */ if( __timer_id == -1 ) { __timer_id = set_timer( 500, blink_cursor, TRUE ); } Widget_Drag_Event_Connect( widget, TextBox_TextLayer_Click ); /* 关联 FOCUS_OUT 和 FOCUS_IN 事件 */ Widget_FocusOut_Event_Connect( widget, hide_textbox_cursor, NULL ); Widget_FocusIn_Event_Connect( widget, _put_textbox_cursor, NULL ); /* 关联按键输入事件 */ Widget_Keyboard_Event_Connect( widget, TextBox_Input ); /* 默认不启用多行文本模式 */ TextBox_Multiline( widget, FALSE ); }