combobox.cpp
来自「ncbi源码」· C++ 代码 · 共 622 行 · 第 1/2 页
CPP
622 行
// PRE : none// POST: the current value of the comboboxconst char* CComboBox::value(void) const{ return m_Input->value();}//-----------------------------------------------------------------------------// PRE : none// POST: the index of the current value in the entry list (or -1 if not found)int CComboBox::value_index(void) const{ CMap::const_iterator i = m_EntryMap->FindExact(m_Input->value(), (type() & eCaseSensitive) > 0); if (i == m_EntryMap->end()) { return -1; } else { return i->second; }}//-----------------------------------------------------------------------------// PRE : conditions ripe for potential callback// POST: callback executed, depending on WHEN maskvoid CComboBox::x_MaybeCallback(void){ if ((when() & FL_WHEN_NOT_CHANGED) || ((when() & FL_WHEN_CHANGED) && m_PrevValue != m_Input->value())) { if (when() & FL_WHEN_RELEASE_ALWAYS) { do_callback(); } else if (value_index() != -1) { do_callback(); } } m_PrevValue = m_Input->value();}//-----------------------------------------------------------------------------// PRE : down-arrow button clicked// POST: if browser was open, it's now closed; if it was closed, it's now openvoid CComboBox::x_ButtonClicked(void){ if (m_PopupWin && ((Fl_Widget*)m_PopupWin)->visible()) { x_CloseBrowser(); } else { x_OpenBrowser(); }}//-----------------------------------------------------------------------------// PRE : a key pressed in input box// POST: if something was entered at the end of the input box, looks for// matches in the entry list; if a match is found, the completion is// displayed in the input box (selected, so that the user can easily delete// it if desired)void CComboBox::x_Keypressed(void){ if (m_InKeypressed || m_Input->size() == 0 || (!(type() & eRestrictToEntries) && (m_Input->position() != m_Input->size() || Fl::event_key() == FL_Delete || Fl::event_key() == FL_BackSpace))) { return; } m_InKeypressed = true; // recursive call guard CMap::const_iterator matchI = m_EntryMap->FindInitial(m_Input->value(), (type() & eCaseSensitive) > 0); if (matchI != m_EntryMap->end()) { const char* match = m_Entries[matchI->second].c_str(); const char* input = m_Input->value(); if (!(type() & eCaseSensitive)) { for (; toupper(*input) == toupper(*match) && *match != '\0'; ++input, ++match); int len = match - m_Entries[matchI->second].c_str(); int pos = m_Input->position(); m_Input->replace(0,len, m_Entries[matchI->second].c_str(), len); m_Input->position(pos); } else { for (; *input == *match && *match != '\0'; ++input, ++match); } if (*match != '\0') { int pos = m_Input->position(); if (Fl::event_key() == FL_BackSpace) { --pos; } m_Input->insert(match); m_Input->position(pos, m_Input->size()); m_Browser->value(matchI->second+1); } } else if (type() & eRestrictToEntries) { //if restricting & no match.. m_Input->Undo(); } m_InKeypressed = false; // recursive call guard}//-----------------------------------------------------------------------------// PRE : user selected an item in the browser// POST: input box displays text of this itemvoid CComboBox::x_ItemSelected(void){ if (m_Browser->value() > 0) { m_Input->value(m_Entries[m_Browser->value()-1].c_str()); if (Fl::event() == FL_RELEASE && Fl::event_inside(m_Browser)) { x_CloseBrowser(); } }}//-----------------------------------------------------------------------------// PRE : none// POST: browser window open with ??? displayedvoid CComboBox::x_OpenBrowser(void){ Fl::belowmouse(this);//hack: FLTK won't change belowmouse when grabbing int boxY, browserY; //adjust m_PopupWin *BEFORE* adding controls if (y() + Fl::event_y_root()-Fl::event_y() + m_Box->h()+m_Browser->h() < Fl::h()) {//drop "down" boxY = 0; browserY = m_Box->h(); m_PopupWin->resize(x() + Fl::event_x_root()-Fl::event_x(), y() + Fl::event_y_root()-Fl::event_y(), w(), m_Box->h()+m_Browser->h()); } else { //drop "up" boxY = m_Browser->h(); browserY = 0; m_PopupWin->resize(x() + Fl::event_x_root()-Fl::event_x(), y() + Fl::event_y_root()-Fl::event_y() - m_Browser->h(), w(), m_Box->h()+m_Browser->h()); } m_PopupWin->add(m_Box); m_PopupWin->add(m_Input); m_PopupWin->add(m_Button); m_Box->position(0, boxY); ((Fl_Widget*) m_Input)->position(Fl::box_dx(FL_DOWN_FRAME), boxY+Fl::box_dy(FL_DOWN_FRAME)); m_Button->position(m_Input->x()+m_Input->w()+1, m_Input->y()); ((Fl_Widget*) m_Browser)->resize(0, browserY, w(), m_PopupWin->h()-m_Box->h()); m_PopupWin->show(); if (Fl::focus() != m_Input) { Fl::focus(m_Input); } Fl::grab(m_PopupWin);}//-----------------------------------------------------------------------------// PRE : browser window open// POST: browser window closedvoid CComboBox::x_CloseBrowser(void){ //order is crucial: move widget *before* hiding popup add(m_Box); add(m_Input); add(m_Button); m_Box->position(x(),y()); ((Fl_Widget*)m_Input)->position(x() + Fl::box_dx(FL_DOWN_FRAME), y() + Fl::box_dy(FL_DOWN_FRAME)); m_Button->position(m_Input->x() + m_Input->w()+1, m_Input->y()); m_ResizeBox->resize(m_Input->x(), m_Input->y(), m_Input->w(), 1); m_PopupWin->hide(); Fl::grab(0); x_MaybeCallback();}//-----------------------------------------------------------------------------// PRE : type flags -- binary or of ETypeFlags// POST: void CComboBox::type(unsigned char t){ if ((t & eRestrictToEntries) && value() != "" && value_index() == -1) { value(""); } if (!(t & eCaseSensitive) && (type() & eCaseSensitive)) { // Cycle through & upcase map CMap *newMap = new CMap; ITERATE(CMap, mapI, *m_EntryMap) { newMap->Add(mapI->first.c_str(), mapI->second, false); } m_EntryMap.reset(newMap); } Fl_Widget::type(t);}//-----------------------------------------------------------------------------// PRE : an entry & an (optional) abbreviation for entry// POST: if not already there, entry & abbreviation added to combobox list// of possible optionsvoid CComboBox::Add(const char* entry, const char* abbrev){ string s(entry); if (!(type() & eCaseSensitive)) { NStr::ToUpper(s); } CMap::iterator it = m_EntryMap->lower_bound(s); if (it == m_EntryMap->end() || it->first != s) { m_Browser->add(entry); m_Entries.push_back(entry); it = m_EntryMap->insert(it, CMap::value_type(s, m_Entries.size()-1)); } if (abbrev) { m_EntryMap->Add(abbrev, it->second, (type() & eCaseSensitive) > 0); }}//-----------------------------------------------------------------------------// PRE : none// POST: browser list & input box clearedvoid CComboBox::clear(void){ m_Browser->clear(); m_Entries.clear(); m_EntryMap->clear(); m_Input->value("");}END_NCBI_SCOPE/* * =========================================================================== * $Log: combobox.cpp,v $ * Revision 1000.3 2004/06/01 21:08:58 gouriano * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.16 * * Revision 1.16 2004/05/28 14:10:51 johnson * Enhanced performance of Add method * * Revision 1.15 2004/05/21 22:27:53 gorelenk * Added PCH ncbi_pch.hpp * * Revision 1.14 2004/05/13 17:06:29 johnson * fix to get drop up working on first click * * Revision 1.13 2004/05/12 20:43:57 johnson * drop "up" when near bottom of screen * * Revision 1.12 2004/02/09 16:49:58 johnson * added popup_height get/set methods * * Revision 1.11 2004/01/21 23:55:40 johnson * fix to get popup working correctly after resize * * Revision 1.10 2004/01/13 21:15:28 johnson * made popup a top-level window so it will appear even if combobox is at the * bottom of the parent window * * Revision 1.9 2003/11/19 22:15:09 johnson * don't swallow enter unnecessarily * * Revision 1.8 2003/11/05 20:35:11 johnson * fixed bug introduced in rev 1.7; '>' is of higher precedence than '&'. * * Revision 1.7 2003/10/30 15:58:26 ivanov * Get rid of compilation warnings * * Revision 1.6 2003/10/14 22:51:47 johnson * added method to clear browser; tweaked response to backspace * * Revision 1.5 2003/08/29 18:29:38 johnson * Numerous tweaks & adjustments: replaced Fl_Input with class that does *not* * automatically swallows ctrl key shortcuts; case in/sensitive matching; * multiple abbreviations for a given entry; smoothed internal access to map * * Revision 1.4 2003/08/26 15:53:58 johnson * adjusted value setter to select the correct browser element * * Revision 1.3 2003/08/26 13:20:16 johnson * added value set function; slight modification of up & down arrow behavior * * Revision 1.2 2003/08/21 12:24:13 dicuccio * Added to NCBI namespace * * Revision 1.1 2003/08/19 18:18:00 johnson * initial revision * * =========================================================================== */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?