menu_window.cpp
来自「ncbi源码」· C++ 代码 · 共 1,678 行 · 第 1/4 页
CPP
1,678 行
if(x_IsMenuBar()) { // root menu is MenuBar if(sm_Popups.size() > 2) { // close last menu x_CloseItemSubmenu(); } else { // select previos item in MenuBar sm_CurrPopup = this; x_SelectPrevItem(); } } else { // root menu is Popup x_CloseItemSubmenu(); }}void CPopupMenu1::x_OnRightPressed(void){ if(sm_CurrPopup->m_Selected && ! sm_CurrPopup->m_Selected->IsSubmenuEmpty() && m_PushedItem) { sm_State = eExpand; } else { if(x_IsMenuBar()) {// && sm_Popups.size() > 1 && sm_CurrPopup == sm_Popups[1]) { // if current popup is MenuBar's submenu sm_CurrPopup = this; // MenuBar x_SelectNextItem(); // select next item (to the right) in MenuBar } }}// selects next item in the current menu (Popup or MenuBar)void CPopupMenu1::x_SelectNextItem(void){ _ASSERT(sm_CurrPopup); CMenuItem& root = *sm_CurrPopup->m_RootItem; if(! root.IsSubmenuEmpty()) { // find selected ot first if nothing is selected CMenuItem::TChildItem_I it = sm_CurrPopup->m_Selected ? root.FindSubItem(*sm_CurrPopup->m_Selected) : --root.SubItemsEnd(); _ASSERT(it != root.SubItemsEnd()); CMenuItem* item = NULL; do { ++it; if(it == root.SubItemsEnd()) { it = root.SubItemsBegin(); } item = (*it)->GetValue(); } while(item->IsSeparator() && item != sm_CurrPopup->m_Selected); sm_CurrItem = item; sm_State = (sm_CurrPopup->x_IsMenuBar() && ! sm_CurrItem->IsSubmenuEmpty() && m_PushedItem) ? eTrackSubmenus : eTrack; }}// selects next item in the current menu (Popup or MenuBar)void CPopupMenu1::x_SelectPrevItem(void){ _ASSERT(sm_CurrPopup); CMenuItem& root = *sm_CurrPopup->m_RootItem; if(! root.IsSubmenuEmpty()) { // find selected ot fisrt if nothing is selected CMenuItem::TChildItem_I it = sm_CurrPopup->m_Selected ? root.FindSubItem(*sm_CurrPopup->m_Selected) : root.SubItemsBegin(); _ASSERT(it != root.SubItemsEnd()); CMenuItem* item = NULL; do { if(it == root.SubItemsBegin()) { it = root.SubItemsEnd(); } --it; item = (*it)->GetValue(); } while(item->IsSeparator() && item != sm_CurrPopup->m_Selected); sm_CurrItem = item; //sm_State = sm_CurrPopup->x_IsMenuBar() ? eTrackSubmenus : eTrack; sm_State = (sm_CurrPopup->x_IsMenuBar() && ! sm_CurrItem->IsSubmenuEmpty() && m_PushedItem) ? eTrackSubmenus : eTrack; }}void CPopupMenu1::x_SelectTopItem(void){ if(! sm_CurrPopup->x_IsMenuBar()) { for(size_t i = 0; i < sm_CurrPopup->m_Entries.size(); i++ ) { SItemEntry& entry = sm_CurrPopup->m_Entries[i]; CMenuItem* item = entry.m_Item; if(! item->IsSeparator() && item->IsEnabled()) { sm_CurrItem = item; sm_State = eTrack; // select return; } } }}void CPopupMenu1::x_SelectBottomItem(void){ if(! sm_CurrPopup->x_IsMenuBar()) { for(int i = sm_CurrPopup->m_Entries.size() - 1; i >= 0; i-- ) { SItemEntry& entry = sm_CurrPopup->m_Entries[i]; CMenuItem* item = entry.m_Item; if(! item->IsSeparator() && item->IsEnabled()) { sm_CurrItem = item; sm_State = eTrack; // select return; } } }}void CPopupMenu1::x_CloseItemSubmenu(void){ if(sm_Popups.size() > 1) { CPopupMenu1* menu = sm_Popups.back(); if(sm_CurrPopup == menu) { // select previous one sm_CurrPopup = sm_Popups[sm_Popups.size() - 2]; sm_CurrItem = sm_CurrPopup->m_Selected; } sm_State = eHide; }}/// sets command that will be executed on exitvoid CPopupMenu1::x_SetExecuteCommand(const CMenuItem& item){ if(item.IsEnabled() && item.IsItem()) { m_Cmd = item.GetCommand(); } else { m_Cmd = eCmdInvalid; } }/// depending on selected item generates text string and supplies it to the /// listenervoid CPopupMenu1::x_UpdateListener(){ if(m_Listener) { string hint; if(sm_CurrItem && sm_CurrItem->IsItem()) { hint = GetCmdHint(sm_CurrItem->GetCommand()); } m_Listener->OnHint(hint); }}void CPopupMenu1::show(void){ x_AdjustRectangle(); x_UpdateItems(); TPopupMenuParent::show();}void CPopupMenu1::draw(void){ if(m_MenuBar) { // delegate draing to hosting MenuBar m_MenuBar->x_Draw(0, 0); } else { CMenu::x_Draw(0, 0); }}Fl_Widget& CPopupMenu1::GetWidget(void){ return *static_cast<Fl_Widget*>(this);}const Fl_Widget& CPopupMenu1::GetWidget(void) const{ return *static_cast<const Fl_Widget*>(this);}bool CPopupMenu1::x_IsHorizontal(void) const{ return m_MenuBar != NULL;}void CPopupMenu1::OnTimeout(int timer_id){ m_Timer.Stop(); if(timer_id == 1) { if(sm_CurrPopup && sm_CurrItem) { x_CollapseToCurrent(); // close expanded submenus x_ShowCurrItemSubmenu(false); //expand new submenu } }}////////////////////////////////////////////////////////////////////////////////// CMenuBar1CMenuBar1::CMenuBar1(int x, int y, int w, int h): Fl_Widget(x, y, w, h), m_TooltipItem(NULL){ m_Border = 2; m_Tooltip.SetMode(CTooltip::eStayOnMove); m_Tooltip.EnableActiveMode(this);}void CMenuBar1::EnableTooltips(bool b_en){ m_Tooltip.Enable(b_en);}void CMenuBar1::Activate(){ if(m_RootItem && ! m_RootItem->IsSubmenuEmpty()) { CMenuItem* item = (* m_RootItem->SubItemsBegin())->GetValue(); SetSelected(item); m_PushedItem = NULL; // not pushed by default m_bKeyActivated = true; x_ShowSubMenu(); m_bKeyActivated = false; }}void CMenuBar1::draw(void){ Fl_Widget& wid = GetWidget(); x_Draw(wid.x(), wid.y());}void CMenuBar1::x_Draw(int x, int y){ CMenu::x_Draw(x, y); Fl_Widget& wid = GetWidget(); fl_draw_box(FL_THIN_UP_FRAME, x, y, wid.w(), wid.h(), sm_Props.GetColor(eBack));}Fl_Widget& CMenuBar1::GetWidget(void){ return *static_cast<Fl_Widget*>(this);}const Fl_Widget& CMenuBar1::GetWidget() const{ return *static_cast<const Fl_Widget*>(this);}CRect CMenuBar1::GetPopupRect(void) const{ int w = GetWidget().w(); int h = GetWidget().h(); return CRect(x(), y(), x() + w -1, y() + h - 1);}int CMenuBar1::handle(int event){ int res = x_Handle(event); return res ? res : Fl_Widget::handle(event);}int CMenuBar1::x_Handle(int event){ m_Tooltip.Handle(event); switch(event) { case FL_ENTER: case FL_MOVE: case FL_DRAG: case FL_PUSH: case FL_LEAVE: { CMenuItem* item = x_HitTest(Fl::event_x(), Fl::event_y()); if(item && item->IsSeparator()) { item = NULL; } SetSelected(item); switch(event) { case FL_PUSH: { m_PushedItem = item; if(m_Selected && m_Selected->IsEnabledSubmenu()) { // FL_RELEASE for this FL_PUSH will be handled inside popup menu stub x_ShowSubMenu(); // when we return from x_ShowSubMenu() it should not be considered pressed m_PushedItem = NULL; } GetWidget().redraw(); }; break; case FL_ENTER: case FL_LEAVE: { redraw(); }; break; }; return 1; }; case FL_RELEASE: { CMenuItem* item = x_HitTest(Fl::event_x(), Fl::event_y()); if(item && item == m_PushedItem //&& Fl::event_is_click() && x_IsItemPushed()) { x_ExecuteCommand(*m_PushedItem); } m_PushedItem = NULL; GetWidget().redraw(); return 1; } }; return 0; }int CMenuBar1::x_HandleShortcuts(void){ char ch = tolower(Fl::event_key()); TAccessKeyToItemMap::const_iterator it = m_AccessKeyToItem.find(ch); if(it != m_AccessKeyToItem.end()) { SetSelected(it->second); x_ShowSubMenu(); return 1; } return 0;}void CMenuBar1::x_ShowSubMenu(void){ if(m_Selected->IsEnabled()) { if(m_Selected->IsSubmenu() && ! m_Selected->IsSubmenuEmpty()) { // show popup menu CPopupMenu1 popup(*this, m_RootItem, m_Selected, GetHintListener()); popup.SetCmdTarget(GetCmdTarget()); popup.Popup(); SetSelected(NULL); } } }bool CMenuBar1::x_IsHorizontal(void) const{ return true;}bool CMenuBar1::TC_NeedTooltip(int x, int y){ m_TooltipItem = x_HitTest(x, y); bool en = m_TooltipItem && ! m_TooltipItem->IsSeparator(); return en;}string CMenuBar1::TC_GetTooltip(int& x, int& y, int& w, int& h){ _ASSERT(m_TooltipItem); CRect rc = x_GetPopupItemRect(*m_TooltipItem); x = rc.Left(); y = rc.Top(); w = rc.Width(); h = rc.Height(); return x_GetTooltip(*m_TooltipItem);}string CMenuBar1::x_GetTooltip(const CMenuItem& item) const{ // find accelerator label if any string s_accel; ITERATE(TEntries, it, m_Entries) { if(it->m_Item == &item) { s_accel = it->m_AccelLabel; break; } } string label = item.GetLabel(); string::size_type pos = label.find(kAccessKeyMarker); // find accelerator marker if(pos != string::npos && pos + 1 < label.size()) { // there is an accelerator label.erase(pos, 1); } if(s_accel.size()) { label += " ("; label += s_accel + ")"; } return label;}END_NCBI_SCOPE/* * =========================================================================== * $Log: menu_window.cpp,v $ * Revision 1000.0 2004/06/01 21:29:21 gouriano * PRODUCTION: IMPORTED [GCC34_MSVC7] Dev-tree R1.7 * * Revision 1.7 2004/05/21 22:27:53 gorelenk * Added PCH ncbi_pch.hpp * * Revision 1.6 2004/05/13 17:29:39 yazhuk * Added support for accelerators, keyboard activated menus * * Revision 1.5 2004/05/10 20:58:11 ucko * +<math.h> for ceil * * Revision 1.4 2004/05/10 16:25:40 yazhuk * Addressed GCC warnings * * Revision 1.3 2004/05/07 14:19:29 yazhuk * Added UpdateItems(), fixed focus handling * * Revision 1.2 2004/05/03 19:47:36 yazhuk * Refactoring; support for command updates * * Revision 1.1 2004/04/22 16:56:35 yazhuk * Initial revision * * =========================================================================== */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?