在第二人生里,从登录的界面可以看到一个组合框,就是选择不同服务器的组件,它就使用到组合框了。其实组合框是由按钮、滚动列表框和编辑框组成的。它的继承关系如下:
class LLComboBox
:
public LLUICtrl, public LLCtrlListInterface
同样它也是一个控件类,继承了基类
LLUICtrl。从LLComboBox类的声明里,可以看到下面这段代码:
#001protected:
组合框里的按钮。
#002
LLButton* mButton;
组合框里的滚动列表。
#003
LLScrollListCtrl* mList;
#004
LLViewBorder* mBorder;
#005
EPreferredPositionmListPosition;
#006
LLPointer<LLImageGL> mArrowImage;
#007
#008private:
#009
S32 mButtonPadding;
组合框里的编辑框。
#010
LLLineEditor* mTextEntry;
#011
BOOL mAllowTextEntry;
#012
S32 mMaxChars;
#013
BOOL mTextEntryTentative;
由于组合框都是有现成的控件组成,所以它的显示代码是非常简单的,如下
:
#001void LLComboBox::draw()
#002{
判断是否可以可见。
#003
if( getVisible() )
#004
{
设置边框。
#005
mBorder->setKeyboardFocusHighlight(hasFocus());
#006
设置按钮是否起作用。
#007
mButton->setEnabled(getEnabled() /*&& !mList->isEmpty()*/);
#008
显示所有子窗口组件。
#009
// Draw children normally
#010
LLUICtrl::draw();
#011
}
#012}
上面子窗口显示的代码是显示那些组件的呢?下面从构造函数里看到它是下面的组件构成,如下:
#001LLComboBox::LLComboBox(
const LLString& name, const LLRect &rect, const LLString& label,
#002
void (*commit_callback)(LLUICtrl*,void*),
#003
void *callback_userdata
#004
)
#005:LLUICtrl(name, rect, TRUE, commit_callback, callback_userdata,
#006
FOLLOWS_LEFT | FOLLOWS_TOP),
#007
mTextEntry(NULL),
#008
mArrowImage(NULL),
#009
mAllowTextEntry(FALSE),
#010
mMaxChars(20),
#011
mTextEntryTentative(TRUE),
#012
mListPosition(BELOW),
#013
mPrearrangeCallback( NULL ),
#014
mTextEntryCallback( NULL )
#015{
创建按钮类。
#016
// Always use text box
#017
// Text label button
#018
mButton = new LLButton("comboxbox button",
#019
LLRect(), label, NULL, LLString::null,
#020
NULL, this);
#021
mButton->setImageUnselected("square_btn_32x128.tga");
#022
mButton->setImageSelected("square_btn_selected_32x128.tga");
#023
mButton->setImageDisabled("square_btn_32x128.tga");
#024
mButton->setImageDisabledSelected("square_btn_selected_32x128.tga");
#025
mButton->setScaleImage(TRUE);
#026
#027
mButton->setMouseDownCallback(onButtonDown);
#028
mButton->setFont(LLFontGL::sSansSerifSmall);
#029
mButton->setFollows(FOLLOWS_LEFT | FOLLOWS_BOTTOM | FOLLOWS_RIGHT);
#030
mButton->setHAlign( LLFontGL::LEFT );
#031
mButton->setRightHPad(2);
添加到子窗口列表。
#032
addChild(mButton);
#033
创建滚动列表框。
#034
// disallow multiple selection
#035
mList = new LLScrollListCtrl(
#036
"ComboBox", LLRect(),
#037
&LLComboBox::onItemSelected, this, FALSE);
#038
mList->setVisible(FALSE);
#039
mList->setBgWriteableColor( LLColor4(1,1,1,1) );
#040
mList->setCommitOnKeyboardMovement(FALSE);
添加到子窗口列表。
#041
addChild(mList);
#042
#043
LLRect border_rect(0, getRect().getHeight(), getRect().getWidth(), 0);
创建边框。
#044
mBorder = new LLViewBorder( "combo border", border_rect );
#045
addChild( mBorder );
#046
mBorder->setFollowsAll();
#047
#048
LLUUID arrow_image_id( LLUI::sAssetsGroup->getString("combobox_arrow.tga") );
#049
mArrowImage = LLUI::sImageProvider->getImageByID(arrow_image_id);
#050
mButton->setImageOverlay("combobox_arrow.tga", LLFontGL::RIGHT);
#051
在这个函数里添加编辑框到子窗口列表里。
#052
updateLayout();
#053}
通过上面的代码分析,看到一个组合框是怎么样构成,并且怎么样显示出来。