comboboxcontrol.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 1,419 行 · 第 1/3 页

CPP
1,419
字号
	}	getContainer()->paintChildren( context );}void ComboBoxControl::onListModelContentsChanged( ListModelEvent* event ){	if ( NULL != event ){		switch ( event->getType() ){			case LIST_MODEL_CONTENTS_DELETED: {				if ( selectedItem_ ==  event->getListItem() ) {					selectedItem_ = NULL;					selectedIndex_ = 0;				}			}			break;			case LIST_MODEL_ITEM_CHANGED: {				ListItem* item = event->getListItem();				if ( NULL != item ){				}			}			break;		}	}}void ComboBoxControl::onItemAdded( ListModelEvent* event ){}void ComboBoxControl::onItemDeleted( ListModelEvent* event ){	if ( selectedItem_ ==  event->getListItem() ) {		selectedItem_ = NULL;		selectedIndex_ = 0;	}}void ComboBoxControl::closeDropDown( Event* event ){	if ( NULL != dropDown_ ) {		ComboBoxDropDown* dropdown = (ComboBoxDropDown*)dropDown_;		ListItem* selectedItem = dropdown->getSelectedItem();		EventHandler* lostFocusHandler = getEventHandler( "ComboBoxControl::onDropDownLostFocus" );		dropDown_->FrameActivation.removeHandler( lostFocusHandler );		dropDown_->setVisible( false );		this->repaint();		dropDown_->close();		if ( NULL != selectedItem  ) {			// not when we just lost the focus			if ( event->getType() != 888999 ) {				setDropDownSelected( true );				setSelectedItem( selectedItem );				setDropDownSelected( false );			}		}		if ( cbsDropDownWithEdit == comboBoxStyle_ ) {			ListItem* item = getSelectedItem();			if ( NULL != item ) {				setCurrentText( item->getCaption() );			}		}		//delete dropDown_;	}	dropDown_ = NULL;}void ComboBoxControl::onDropDownLostFocus( WindowEvent* event ){	if ( NULL != dropDown_ ) {		Frame* frame = (Frame*)event->getSource();		if ( false == frame->isActive() ) {			dropDown_->setVisible( false );			Event* closeEvent = new Event( dropDown_, 888999 );			EventHandler* ev = new GenericEventHandler<ComboBoxControl>( this, &ComboBoxControl::closeDropDown );			UIToolkit::postEvent( ev, closeEvent );		}	}	this->arrowPressed_ = false;}void ComboBoxControl::mouseDown( MouseEvent* event ){	CustomControl::mouseDown( event );	//this->arrowPressed_ = this->arrowRect_.containsPt( event->getPoint() );	this->keepMouseEvents();	Rect clientRect = getClientBounds();	this->arrowPressed_ = clientRect.containsPt( event->getPoint() );	if ( true == arrowPressed_ ){		if ( dropDown_ != NULL ) {			//drop down is still up, this will close it			return;		}		dropDown_ = NULL;		ListModel* model = getListModel();		if ( NULL != model ) {			if ( model->getCount() > 0 ) {				EventHandler* closeHandler = getEventHandler( "ComboBoxControl::closeDropDown" );				if ( NULL == closeHandler ) {					closeHandler = new GenericEventHandler<ComboBoxControl>( this, &ComboBoxControl::closeDropDown, "ComboBoxControl::closeDropDown" );				}				ComboBoxDropDown* dropDown = new ComboBoxDropDown( this, closeHandler );				EventHandler* lostFocusHandler = getEventHandler( "ComboBoxControl::onDropDownLostFocus" );				if ( NULL == lostFocusHandler ) {					lostFocusHandler = new WindowEventHandler<ComboBoxControl>( this,																				&ComboBoxControl::onDropDownLostFocus,																				"ComboBoxControl::onDropDownLostFocus" );				}				dropDown->FrameActivation.addHandler( lostFocusHandler );				dropDown_ = dropDown;				dropDown_->show();			}		}	}	this->repaint();}void ComboBoxControl::mouseMove( MouseEvent* event ){	CustomControl::mouseMove( event );	if ( event->hasLeftButton() ){		//this->arrowPressed_ = this->arrowRect_.containsPt( event->getPoint() );		Rect clientRect = getClientBounds();		this->arrowPressed_ = clientRect.containsPt( event->getPoint() );		this->repaint();	}}void ComboBoxControl::mouseUp( MouseEvent* event ){	CustomControl::mouseUp( event );	this->releaseMouseEvents();	arrowPressed_ = false;	this->repaint();}void ComboBoxControl::keyPressed( KeyboardEvent* event ){	// this case is called when the focus is on the DropDownListBox	//  control, as it happens when an item is selected;	// it is not called when the focus is on the edit_ control	switch ( event->getVirtualCode() ){		case vkUpArrow :{			ulong32 index = selectedIndex_ + 1;			if ( index >=	this->listModel_->getCount() ){				index = 0;			}			this->setSelectedItemIndex( index );			this->repaint();		}		break;		case vkDownArrow :{			ulong32 index = selectedIndex_ - 1;			/**			* this is done this way because we have an UNSIGNED long so we			*won't get negative numbers, anything over the count needs to be wrapped			*/			if ( index > this->listModel_->getCount() ){				index = this->listModel_->getCount() -1;			}			this->setSelectedItemIndex( index );			this->repaint();		}		break;		case vkEscape : {			if ( NULL != dropDown_ ) {				Event ev( this );				closeDropDown( &ev );			}		}		break;		case vkReturn : {			StringUtils::trace( "ComboBoxControl::keyPressed: vkReturn()\n" );			if ( NULL != dropDown_ ) {				ListItem* item = ((ComboBoxDropDown*)dropDown_)->getListBox()->getSelectedItem();				// we close the drodownbox here because if we do it after, 				// we would set back the current selected item, while				// the selected item is going to be changed by this code.				Event ev( this );				closeDropDown( &ev );				setSelectedItem( item );				if ( cbsDropDownWithEdit == comboBoxStyle_ ) {					item = getSelectedItem();					if ( NULL != item ) {						setCurrentText( item->getCaption() );					}				}			}		}		break;	}}ListItem* ComboBoxControl::getSelectedItem(){	return selectedItem_;}void ComboBoxControl::onPostSelect( ItemEvent* e ){	SelectionChanged.fireEvent( e );}void ComboBoxControl::setSelectedItem( ListItem* selectedItem ){	if ( NULL != selectedItem_ ) {		selectedItem_->setSelected( false );	}	selectedItem_ = selectedItem;	if ( NULL != selectedItem_ ) {		selectedItem_->setSelected( true );				if ( cbsDropDownWithEdit == comboBoxStyle_	) {			edit_->getTextModel()->setText( selectedItem_->getCaption() );		}		setFocused();		/**		JC		I changed this so that the source is the ComboBoxControl		instance as opposed to the selected item		*/ 		ItemEvent event( this, ITEM_EVENT_SELECTED );		event.setUserData( (void*)selectedItem_ );		SelectionChanged.fireEvent( &event );	}	repaint();}void ComboBoxControl::setSelectedItemIndex( const ulong32& selectedIndex ){	selectedIndex_ = selectedIndex;	if ( NULL != listModel_ ){		if ( listModel_->getCount() > 0 ){			ListItem* item = listModel_->getItemFromIndex( selectedIndex );			setSelectedItem( item );		}	}}void ComboBoxControl::setComboBoxStyle( const ComboBoxStyleType& comboBoxStyle ){	comboBoxStyle_ = comboBoxStyle;	edit_->setVisible( (comboBoxStyle_ == cbsDropDownWithEdit) );	edit_->setEnabled( (comboBoxStyle_ == cbsDropDownWithEdit) );	if ( cbsDropDownWithEdit == comboBoxStyle_ ) {		setTabStop( false );	}	else {		setTabStop( true );	}	updateEditBounds();}/*void ComboBoxControl::setBounds( Rect* rect, const bool& anchorDeltasNeedUpdating ){	CustomControl::setBounds( rect, anchorDeltasNeedUpdating );	if ( comboBoxStyle_ == cbsDropDownWithEdit ) {		Rect rectTmp( 0, 0, rect->getWidth() - rect->getHeight(), rect->getHeight() );		rectTmp.inflate( -1, -1 );		rectTmp.right_ += 2;		edit_->setBounds( &rectTmp );	}	else {		edit_->setBounds( &Rect(0,0,0,0) );	}}*/Rect ComboBoxControl::getEditBounds(){	Rect result;	result = getClientBounds();	//Size sz = 		//getUIMetricsManager()->getDefaultVerticalScrollButtonDimensions();	result.right_ -= UIToolkit::getUIMetricValue( UIMetricsManager::mtVerticalScrollbarThumbWidth );		result.right_ -= 3;	result.inflate( -2, -1 );	return result;}void ComboBoxControl::updateEditBounds(){	if ( comboBoxStyle_ == cbsDropDownWithEdit ) {		edit_->setBounds( &getEditBounds() );	}	else {		edit_->setBounds( &Rect(0,0,0,0) );	}}void ComboBoxControl::onEditKeyPressed( KeyboardEvent* event ){	//if (  vkReturn == event->getVirtualCode() ) {		KeyboardEvent* postedEvent = new KeyboardEvent( event->getSource(), event->getType(),														event->getRepeatCount(), event->getKeyMask(),														event->getKeyValue(), event->getVirtualCode() );		UIToolkit::postEvent(			new KeyboardEventHandler<ComboBoxControl>( this, &ComboBoxControl::onEditReturnKeyPressed ), postedEvent );	//}	//VirtualKeyCode key = event->getVirtualCode();	//StringUtils::trace( Format( "Key: %d\r\n" ) % key );}void ComboBoxControl::onEditReturnKeyPressed( KeyboardEvent* event ){	// we get here only if the focus is on the edit control;	// if we select an item, the focus will go to the DropDownListBox instead.	if ( vkReturn == event->getVirtualCode() ) {		if ( NULL != dropDown_ ) {			ListItem* item = ((ComboBoxDropDown*)dropDown_)->getListBox()->getSelectedItem();			// we close the drodownbox now because if we do it after, 			// we would set back the current selected item			Event ev( this );			closeDropDown( &ev );			setSelectedItem( item );			if ( cbsDropDownWithEdit == comboBoxStyle_ ) {				ListItem* item = getSelectedItem();				if ( NULL != item ) {					setCurrentText( item->getCaption() );				}			}		}		else {			ListItem* item = this->getSelectedItem();			if ( NULL != item ) {				// commented as we don't want to change the caption of the items in the list [ bugfix 1112867]				//item->setCaption( edit_->getTextModel()->getText() );				repaint();			}		}	}	else if ( vkEscape == event->getVirtualCode() ) {		Event ev( this );		closeDropDown( &ev );	}	else {		if ( NULL != dropDown_ ) {			// find the item and select it			if ( autoLookup_ ) {				bool hasSpecialKey = ( event->getKeyMask() != 0 );				if ( !hasSpecialKey ) {					String caption, editCaption;					editCaption = edit_->getTextModel()->getText();					ListItem* found = lookupItem( editCaption, autoLookupIgnoreCase_ );					if ( NULL != found ) {						this->selectItems( false );						((ComboBoxDropDown*)dropDown_)->ensureVisible( found, true );						found->setSelected( true );					}				}			}		}	}}ListItem* ComboBoxControl::lookupItem( const String& text, const bool& ignoreCase/*=false*/ ){	ListItem* found = NULL;	if ( text != "" ) {		String txt = text;		if ( ignoreCase ) {			txt = StringUtils::lowerCase( text );		}		String caption;		ListItem* similarItem = NULL;		Enumerator<ListItem*>* items = getListModel()->getItems();		while ( true == items->hasMoreElements() ) {			ListItem* item = items->nextElement();			caption = item->getCaption();			if ( ignoreCase ) {				caption = StringUtils::lowerCase( caption );			}			// this privilege the identical item, otherwise it accept the first more similar to the text in the (unsorted) list			if ( caption == txt ) {				similarItem = item;				break;			} else if ( NULL == similarItem ) {				if ( 0 == caption.find( txt ) ) {						similarItem = item;					}			}		}		found = similarItem;	}	return found;}void ComboBoxControl::setDropDownCount( const ulong32& dropDownCount ){	dropDownCount_ = dropDownCount;}void ComboBoxControl::setDropDownWidth( const ulong32& dropDownWidth ){	dropDownWidth_ = dropDownWidth;}void ComboBoxControl::setDropDownExtendFullScreen( const bool& dropDownExtendFullScreen ){	dropDownExtendFullScreen_ = dropDownExtendFullScreen;}void ComboBoxControl::setDiscreteScroll( const bool& discreteScroll ){	discreteScroll_ = discreteScroll;}void ComboBoxControl::setDropDownSelected( const bool& dropDownSelected )

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?