📄 wg_dropdown.cpp
字号:
// wg_dropdown.cpp//// CDropDown class implementation////// Copyright (c) 2002 Rob Wiskow// rob-dev@boxedchaos.com//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//#include "wgui_include_config.h"#include "wg_dropdown.h"#include "wg_message_server.h"#include "wg_resources.h"namespace wGui{CDropDown::CDropDown(const CRect& WindowRect, CWindow* pParent, bool bAllowEdit, unsigned int iItemHeight, CFontEngine* pFontEngine) : CWindow(WindowRect, pParent), m_bAllowEdit(bAllowEdit){ m_sClassName = "CDropDown"; m_pEditBox = new CEditBox(CRect(WindowRect.TopLeft(), CPoint(WindowRect.Right() - WindowRect.Height(), WindowRect.Bottom())), this, pFontEngine); if (!m_bAllowEdit) { m_pEditBox->SetReadOnly(true); // Override the normal read-only BG color m_pEditBox->SetBGColor(COLOR_WHITE); } // We create the listbox with the root window (probably a CView object) as it's parent, so that it gets the OnMouseButtonDown/Up properly m_pListBox = new CListBox(CRect(WindowRect.BottomLeft(), CPoint(WindowRect.Right(), WindowRect.Bottom() + iItemHeight * 5 + 1)), GetAncestor(ROOT), true, iItemHeight, pFontEngine); m_pListBox->SetVisible(false); m_pListBox->SetDropDown(this); m_pDropButton = new CPictureButton( CRect(CPoint(WindowRect.Right() - WindowRect.Height() + 1, WindowRect.Top()), WindowRect.BottomRight()), this, CwgBitmapResourceHandle(WGRES_DOWN_ARROW_BITMAP)); CMessageServer::Instance().RegisterMessageClient(this, CMessage::MOUSE_BUTTONDOWN); CMessageServer::Instance().RegisterMessageClient(this, CMessage::CTRL_LCLICK); CMessageServer::Instance().RegisterMessageClient(this, CMessage::CTRL_VALUECHANGE);}CDropDown::~CDropDown(void){ // The listbox is parented by the root window, so we need to delete it by hand delete m_pListBox;}void CDropDown::SetWindowText(std::string sWindowText){ m_pEditBox->SetWindowText(sWindowText);}bool CDropDown::HandleMessage(CMessage* pMessage){ bool bHandled = false; CRect SubRect(m_WindowRect); SubRect.Grow(-3); if (pMessage) { switch(pMessage->MessageType()) { case CMessage::MOUSE_BUTTONDOWN: { CMouseMessage* pMouseMessage = dynamic_cast<CMouseMessage*>(pMessage); if (pMouseMessage->Button == CMouseMessage::LEFT) { if (m_pListBox->IsVisible() && m_pDropButton->GetWindowRect().HitTest(pMouseMessage->Point) != CRect::RELPOS_INSIDE && m_pListBox->GetWindowRect().HitTest(pMouseMessage->Point) != CRect::RELPOS_INSIDE) { HideListBox(); } } break; } case CMessage::CTRL_LCLICK: { if (pMessage->Destination() == this) { if (pMessage->Source() == m_pDropButton) { if (m_pListBox->IsVisible()) { HideListBox(); } else { ShowListBox(); } bHandled = true; } } break; } case CMessage::CTRL_VALUECHANGE: { TIntMessage* pCtrlMessage = dynamic_cast<TIntMessage*>(pMessage); if (pCtrlMessage && pMessage->Destination() == this) { if (pCtrlMessage->Source() == m_pListBox) { const SListItem& ListItem = m_pListBox->GetItem(pCtrlMessage->Value()); SetWindowText(ListItem.sItemText); HideListBox(); CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, 0)); bHandled = true; } else if (pCtrlMessage->Source() == m_pEditBox) { m_pListBox->SetAllSelections(false); HideListBox(); CMessageServer::Instance().QueueMessage(new TIntMessage(CMessage::CTRL_VALUECHANGE, m_pParentWindow, this, 0)); bHandled = true; } } break; } default : bHandled = CWindow::HandleMessage(pMessage); break; } } return bHandled;}void CDropDown::ShowListBox(void){ if (!m_pListBox->IsVisible()) { // This makes sure the ListBox is at the top of the order so it gets first shot at the OnMouseButtonDown/Up m_pListBox->SetNewParent(m_pListBox->GetAncestor(PARENT)); m_pListBox->SetVisible(true); m_pListBox->StartDrawProc(); }}void CDropDown::HideListBox(void){ if (m_pListBox->IsVisible()) { m_pListBox->SetVisible(false); CMessageServer::Instance().QueueMessage(new CMessage(CMessage::APP_PAINT, 0, this)); }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -