menu_window.hpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:13k
- /*
- * ===========================================================================
- * PRODUCTION $Log: menu_window.hpp,v $
- * PRODUCTION Revision 1000.0 2004/06/01 19:56:37 gouriano
- * PRODUCTION PRODUCTION: IMPORTED [GCC34_MSVC7] Dev-tree R1.5
- * PRODUCTION
- * ===========================================================================
- */
- #ifndef GUI_WIDGETS_FL___MENU_WINDOW__HPP
- #define GUI_WIDGETS_FL___MENU_WINDOW__HPP
- /* $Id: menu_window.hpp,v 1000.0 2004/06/01 19:56:37 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Andrey Yazhuk
- *
- */
- /// @file menu.hpp
-
- #include <corelib/ncbistd.hpp>
- #include <corelib/ncbi_tree.hpp>
- #include <gui/utils/command.hpp>
- #include <gui/utils/gui_event.hpp>
- #include <gui/widgets/fl/rect.hpp>
- #include <gui/widgets/fl/menu_item.hpp>
- #include <gui/widgets/fl/resource_manager.hpp>
- #include <gui/widgets/fl/tooltip.hpp>
- #include <gui/widgets/fl/fltk_timer.hpp>
- #include <FL/Fl_Menu_.H>
- #include <FL/Fl_Menu_Item.H>
- #include <FL/Fl_Double_Window.H>
- #include <FL/Fl_Image.H>
- /** @addtogroup GUI_FltkWidgets
- *
- * @{
- */
- BEGIN_NCBI_SCOPE
- ///////////////////////////////////////////////////////////////////////////////
- ///
- class NCBI_GUIWIDGETS_FL_EXPORT IMenuHintListener
- {
- public:
- virtual ~IMenuHintListener() {};
- virtual void OnHint(const string& hint) = 0;
- };
- ///////////////////////////////////////////////////////////////////////////////
- /// class CMenu - base class for all menus
- ///
- /// Access key is underlined letter in the menu item label. When a menu is active,
- /// the user can select a menu item by pressing the key that corresponds to the
- /// item's underlined letter.
- /// Accelerator is a combination of key with a number of modifiers corresponding
- /// to a command. Menu shows accelerator labels next to corresponding command items.
- ///
- class NCBI_GUIWIDGETS_FL_EXPORT CMenu : public IMenu
- {
- public:
- enum EColorType {
- eBack = 0,
- eFocusedBack,
- eCheckedBack,
- ePushedBack,
- ePopupBack,
- eFrame,
- eText,
- eDisabledText,
- eFocusedDisabledText,
- ePushedText,
- eFocusedFrame,
- eIconArea,
- eBorder,
- eLast
- };
- /// CProperties holds common properties for all CMenu derived classes;
- class CProperties
- {
- public:
- CProperties(void);
- inline Fl_Color GetColor(EColorType type) const { return m_Colors[type]; }
- public:
- int m_FontType;
- int m_FontSize;
-
- Fl_Color m_Colors[eLast];
- };
- public:
- CMenu(void);
- virtual ~CMenu(void);
- static CRef<CResourceManager> GetResourceManager(void);
- static void SetResourceManager(CRef<CResourceManager> manager);
-
- static void SetCmdHint(TCmdID cmd, const string& hint);
- static string GetCmdHint(TCmdID cmd);
- /// @name IMenu* implementation
- /// @{
- virtual void SetCmdTarget(CCommandTarget* target);
- virtual CCommandTarget* GetCmdTarget(void);
- /// @}
- virtual void SetItemImageSize(int size);
- virtual void SetItems(const SMenuItemRec* items);
- virtual void SetItems(CMenuItem* root_item); // takes ownership of the given structure
- virtual void SetHintListener(IMenuHintListener* listener);
- virtual IMenuHintListener* GetHintListener();
-
- virtual CPoint GetPreferredSize(void) const;
- virtual void SetSelected(CMenuItem* item);
-
- CMenuItem* GetRootItem(void);
- int GetBorder(void) const;
- /// returns pointer to widget hosting CMenu instance
- virtual Fl_Widget& GetWidget(void) = 0;
- virtual const Fl_Widget& GetWidget(void) const = 0;
-
- bool x_IsSubMenuPushed() const { return m_PushedItem && m_PushedItem->IsSubmenu(); }
- bool x_IsItemPushed() const { return m_PushedItem && m_PushedItem->IsItem(); }
- protected:
- struct SItemEntry
- {
- CMenuItem* m_Item;
- CFLTKImageHandle m_hIcon;
- int m_Size;
- string m_AccelLabel; // symbolic representation of accelerator
- };
-
- protected:
- virtual bool x_IsKeyActivated() const;
- virtual void x_SetItems(CMenuItem* root_item, bool take_ownership);
- virtual void x_Clear(void);
- virtual void x_Layout(void);
- virtual void x_UpdateItems(void);
-
- virtual void x_Draw(int x, int y);
- virtual void x_DrawItems(int x, int y);
- virtual void x_ExecuteCommand(const CMenuItem& item);
-
- virtual bool x_IsHorizontal(void) const = 0;
-
- virtual void x_MeasureItem(const SItemEntry& entry, int& text_w) const;
- virtual void x_DrawItem(SItemEntry& entry, int x, int y, int w, int h);
- virtual void x_DrawItemText(const SItemEntry& entry, int x, int y, int w, int h);
- virtual void x_DrawItemIcon(SItemEntry& entry, int x, int y, Fl_Color back_color);
- CMenuItem* x_HitTest(int x, int y);
- // returns rectangle used to position submenu
- virtual CRect x_GetPopupItemRect(const CMenuItem& item);
-
- protected:
- typedef vector<SItemEntry> TEntries;
- typedef map<char, CMenuItem*> TAccessKeyToItemMap;
- typedef map<TCmdID, string> TCmdToHintMap;
-
- static CProperties sm_Props; // common properties for all menus
- static CRef<CResourceManager> sm_ResManager; // singleton Resource Manager
- static TCmdToHintMap sm_CmdToHint;
-
- CMenuItem* m_RootItem;
- bool m_bOwnItems; /// if "true" CMenu does not assume ownership of m_RootItem
-
- CCommandTarget* m_CmdTarget; /// root command target
- IMenuHintListener* m_Listener;
- TEntries m_Entries; /// menu item entires
- TAccessKeyToItemMap m_AccessKeyToItem; /// access key table
-
- int m_Width; // total width including borders
- int m_Height; // total height including borders
- int m_Border; // width(height) of the border around menu items
- int m_IconSize;
- int m_MaxLabelW;
- EColorType m_BackColor;
-
- CMenuItem* m_Selected; // selected (focused) item
- CMenuItem* m_PushedItem; // item pushed by mouse or keyboard
- bool m_bKeyActivated; // menu was activated using keyboard
-
- CFLTKImageHandle m_hCheckIcon;
- CFLTKImageHandle m_hRadioIcon;
- };
- ///////////////////////////////////////////////////////////////////////////////
- /// CMenuCmdUI
- class NCBI_GUIWIDGETS_FL_EXPORT CMenuCmdUI : public ICmdUI
- {
- public:
- CMenuCmdUI(CMenuItem& item);
- /// @name ICmdUI implementation
- /// @{
- virtual TCmdID GetCommand() const;
- virtual void Enable(bool en);
- virtual void SetCheck(bool set);
- virtual void SetRadio(bool set);
- virtual void SetLabel(const string& label);
- /// @}
- protected:
- CMenuItem& m_Item;
- };
- ///////////////////////////////////////////////////////////////////////////////
- /// CPopupMenu1
- typedef Fl_Double_Window TPopupMenuParent;
- class NCBI_GUIWIDGETS_FL_EXPORT CPopupMenu1 : public TPopupMenuParent,
- public CMenu,
- public ITimerListener
- {
- friend class CMenuBar1;
- public:
- CPopupMenu1(int x, int y, CMenuItem* root_item, CCommandTarget* target = NULL,
- IMenuHintListener* listener = NULL);
- CPopupMenu1(int x, int y, const SMenuItemRec* items, CCommandTarget* target = NULL,
- IMenuHintListener* listener = NULL);
- protected:
- /// Creates a child popup menu
- CPopupMenu1(int x, int y, int left, int top, CMenuItem* root_item,
- CCommandTarget* target, IMenuHintListener* listener);
-
- /// Creates root popup menu stub for a menubar
- CPopupMenu1(CMenuBar1& menubar, CMenuItem* root_item, CMenuItem* sel_item,
- IMenuHintListener* listener);
- public:
- virtual void Popup(void); /// shows popup menu
- /// @name FLTK overridables
- /// @{
- virtual void show(void);
- virtual void draw(void);
- virtual int handle(int event);
- /// @}
-
- virtual void OnTimeout(int timer_id);
- virtual Fl_Widget& GetWidget(void);
- virtual const Fl_Widget& GetWidget(void) const;
- protected:
- virtual void x_Init(int x, int y, int left, int top, CMenuItem* root_item,
- CCommandTarget* target, IMenuHintListener* listener, bool root_popup);
- virtual bool x_IsHorizontal() const;
- virtual void SetSelected(CMenuItem* item);
- virtual CRect x_GetPopupItemRect(const CMenuItem& item);
- void x_CollapseToCurrent(void);
- void x_ShowCurrItemSubmenu(bool b_select);
- void x_TogglePushItem();
-
- int x_HandleKeyboard(void);
- int x_HandleShortcuts(void);
- void x_OnLeftPressed(void);
- void x_OnRightPressed(void);
- void x_SelectNextItem(void);
- void x_SelectPrevItem(void);
- void x_SelectTopItem(void);
- void x_SelectBottomItem(void);
-
- void x_CloseItemSubmenu(void);
- void x_SetExecuteCommand(const CMenuItem& item);
- void x_UpdateListener();
-
- void x_AdjustRectangle(void); // adjust window size and position
- bool x_IsMenuBar() const { return m_MenuBar != NULL; }
- protected:
- int m_Left;
- int m_Top;
- CMenuBar1* m_MenuBar;
- bool m_bSkipFirstRelease;
- TCmdID m_Cmd; // choosen command
- CFLTKTimer m_Timer;
- CGUIEvent m_Event;
- protected:
- enum EState{
- eIdle,
- eTrack, // track item, do not expand submenus
- eTrackSubmenus, // track item, show/hide submenus
- eExpand, // selected item
- eHide, // hide the last popup
- eMenuPush, // push on submenu
- ePush, // push on command item
- eDone
- };
- typedef vector<CPopupMenu1*> TPopupVector;
- static TPopupVector sm_Popups;
- static CMenuItem* sm_CurrItem;
- static CPopupMenu1* sm_CurrPopup;
- static EState sm_State;
- static bool sm_bMenubar;
- };
- ///////////////////////////////////////////////////////////////////////////////
- /// CMenuBar1
- class NCBI_GUIWIDGETS_FL_EXPORT CMenuBar1 :
- public Fl_Widget,
- public CMenu,
- public ITooltipClient
- {
- friend class CPopupMenu1;
- public:
- CMenuBar1(int x, int y, int w, int h);
- virtual void EnableTooltips(bool b_en);
- // update items state
- virtual void UpdateItems() { x_UpdateItems(); }
- virtual void Activate();
- /// @name FLTK overridables
- /// @{
- virtual void draw(void);
- virtual int handle(int event);
- /// @}
-
- CRect GetPopupRect() const;
- /// @name ITooltipClient implementation
- /// @{
- virtual bool TC_NeedTooltip(int x, int y);
- virtual string TC_GetTooltip(int& x, int& y, int& w, int& h);
- /// @}
- virtual Fl_Widget& GetWidget(void);
- virtual const Fl_Widget& GetWidget(void) const;
- protected:
- virtual void x_Draw(int x, int y);
- virtual int x_Handle(int event);
-
- int x_HandleShortcuts(void);
- void x_ShowSubMenu(void);
- virtual bool x_IsHorizontal() const;
- virtual string x_GetTooltip(const CMenuItem& item) const;
- protected:
- CTooltip m_Tooltip;
- CMenuItem* m_TooltipItem;
- };
- END_NCBI_SCOPE
- /* @} */
- /*
- * ===========================================================================
- * $Log: menu_window.hpp,v $
- * Revision 1000.0 2004/06/01 19:56:37 gouriano
- * PRODUCTION: IMPORTED [GCC34_MSVC7] Dev-tree R1.5
- *
- * Revision 1.5 2004/05/13 17:29:30 yazhuk
- * Added support for accelerators, keyboard activated menus
- *
- * Revision 1.4 2004/05/11 18:55:14 dicuccio
- * Added doxygen modules info
- *
- * Revision 1.3 2004/05/07 14:19:04 yazhuk
- * Added UpdateItems()
- *
- * Revision 1.2 2004/05/03 19:47:27 yazhuk
- * Refactoring; support for command updates
- *
- * Revision 1.1 2004/04/22 16:53:18 yazhuk
- * Initial revision: menu_window.hpp
- *
- * ===========================================================================
- */
- #endif // GUI_WIDGETS_FL___MENU_WINDOW__HPP