📄 dropdown.cpp
字号:
{ if (mScrollArea == NULL || mScrollArea->getContent() == NULL) { throw GCN_EXCEPTION("ScrollArea or ListBox is NULL."); } if (selected >= 0) { mListBox->setSelected(selected); } } bool DropDown::keyPress(const Key& key) { if (mScrollArea == NULL || mScrollArea->getContent() == NULL) { throw GCN_EXCEPTION("ScrollArea or ListBox is NULL."); } if ((key.getValue() == Key::ENTER || key.getValue() == Key::SPACE) && !mDroppedDown) { dropDown(); return true; } return false; } void DropDown::mousePress(int x, int y, int button) { if (button == MouseInput::LEFT && hasMouse() && !mDroppedDown) { mPushed = true; dropDown(); } // Fold up the listbox if the upper part is clicked after fold down else if (button == MouseInput::LEFT && hasMouse() && mDroppedDown && y < mOldH) { foldUp(); } else if (!hasMouse()) { foldUp(); } } void DropDown::mouseRelease(int x, int y, int button) { if (button == MouseInput::LEFT) { mPushed = false; } } void DropDown::setListModel(ListModel *listModel) { if (mScrollArea == NULL || mScrollArea->getContent() == NULL) { throw GCN_EXCEPTION("ScrollArea or ListBox is NULL."); } mListBox->setListModel(listModel); if (mListBox->getSelected() < 0) { mListBox->setSelected(0); } adjustHeight(); } ListModel *DropDown::getListModel() { if (mScrollArea == NULL || mScrollArea->getContent() == NULL) { throw GCN_EXCEPTION("ScrollArea or ListBox is NULL."); } return mListBox->getListModel(); } void DropDown::setScrollArea(ScrollArea *scrollArea) { mScrollArea->_setFocusHandler(NULL); mScrollArea->_setParent(NULL); mScrollArea = scrollArea; mScrollArea->_setFocusHandler(&mFocusHandler); mScrollArea->setContent(mListBox); mScrollArea->_setParent(this); adjustHeight(); } ScrollArea *DropDown::getScrollArea() { return mScrollArea; } void DropDown::setListBox(ListBox *listBox) { listBox->setSelected(mListBox->getSelected()); listBox->setListModel(mListBox->getListModel()); listBox->addActionListener(this); if (mScrollArea->getContent() != NULL) { mListBox->removeActionListener(this); } mListBox = listBox; mScrollArea->setContent(mListBox); if (mListBox->getSelected() < 0) { mListBox->setSelected(0); } } ListBox *DropDown::getListBox() { return mListBox; } void DropDown::adjustHeight() { if (mScrollArea == NULL || mScrollArea->getContent() == NULL) { throw GCN_EXCEPTION("ScrollArea or ListBox is NULL."); } int listBoxHeight = mListBox->getHeight(); int h2 = mOldH ? mOldH : getFont()->getHeight(); setHeight(h2); // The addition/subtraction of 2 compensates for the seperation lines // seperating the selected element view and the scroll area. if (mDroppedDown && getParent()) { int h = getParent()->getHeight() - getY(); if (listBoxHeight > h - h2 - 2) { mScrollArea->setHeight(h - h2 - 2); setHeight(h); } else { setHeight(listBoxHeight + h2 + 2); mScrollArea->setHeight(listBoxHeight); } } mScrollArea->setWidth(getWidth()); mScrollArea->setPosition(0, h2 + 2); } void DropDown::dropDown() { if (!mDroppedDown) { mDroppedDown = true; mOldH = getHeight(); adjustHeight(); if (getParent()) { getParent()->moveToTop(this); } } mFocusHandler.requestFocus(mScrollArea->getContent()); } void DropDown::foldUp() { if (mDroppedDown) { mDroppedDown = false; mFocusHandler.focusNone(); adjustHeight(); } } bool DropDown::_keyInputMessage(const KeyInput& keyInput) { if (mDroppedDown) { if (mScrollArea == NULL || mScrollArea->getContent() == NULL) { throw GCN_EXCEPTION("ScrollArea or ListBox is NULL."); } if (mFocusHandler.getFocused() != NULL) { return mFocusHandler.getFocused()->_keyInputMessage(keyInput); } else { return false; } } else { return BasicContainer::_keyInputMessage(keyInput); } } void DropDown::_mouseInputMessage(const MouseInput &mouseInput) { BasicContainer::_mouseInputMessage(mouseInput); if (mDroppedDown) { if (mScrollArea == NULL || mScrollArea->getContent() == NULL) { throw GCN_EXCEPTION("ScrollArea or ListBox is NULL."); } if (mouseInput.y >= mOldH) { MouseInput mi = mouseInput; mi.y -= mScrollArea->getY(); mScrollArea->_mouseInputMessage(mi); if (mListBox->hasFocus()) { mi.y -= mListBox->getY(); mListBox->_mouseInputMessage(mi); } } } } void DropDown::lostFocus() { foldUp(); } void DropDown::moveToTop(Widget* widget) { if (getParent()) { getParent()->moveToTop(this); } } void DropDown::moveToBottom(Widget* widget) { if (getParent()) { getParent()->moveToBottom(this); } } void DropDown::_announceDeath(Widget* widget) { if (widget == mScrollArea) { mScrollArea = NULL; } else { throw GCN_EXCEPTION("Death announced for unknown widget.."); } } void DropDown::action(const std::string& eventId) { foldUp(); generateAction(); } void DropDown::getDrawSize(int& width, int& height, Widget* widget) { if (widget == mScrollArea) { if (mDroppedDown) { height = getHeight() - mOldH; width = getWidth(); } else { width = height = 0; } } else { throw GCN_EXCEPTION("DropDown::getDrawSize. widget is not the ScrollArea (wieeerd...)"); } } void DropDown::setBaseColor(const Color& color) { if (mDefaultScrollArea == mScrollArea && mScrollArea != NULL) { mScrollArea->setBaseColor(color); } if (mDefaultListBox == mListBox && mListBox != NULL) { mListBox->setBaseColor(color); } Widget::setBaseColor(color); } void DropDown::setBackgroundColor(const Color& color) { if (mDefaultScrollArea == mScrollArea && mScrollArea != NULL) { mScrollArea->setBackgroundColor(color); } if (mDefaultListBox == mListBox && mListBox != NULL) { mListBox->setBackgroundColor(color); } Widget::setBackgroundColor(color); } void DropDown::setForegroundColor(const Color& color) { if (mDefaultScrollArea == mScrollArea && mScrollArea != NULL) { mScrollArea->setForegroundColor(color); } if (mDefaultListBox == mListBox && mListBox != NULL) { mListBox->setForegroundColor(color); } Widget::setForegroundColor(color); } void DropDown::setFont(Font *font) { Widget::setFont(font); mListBox->setFont(font); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -