Back to index

nux  3.0.0
MenuPage.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef MENUPAGE_H
00024 #define MENUPAGE_H
00025 
00026 #include "ActionItem.h"
00027 #include "FloatingWindow.h"
00028 
00029 namespace nux
00030 {
00031   class StaticText;
00032   class MenuPage;
00033   class VLayout;
00034   class MenuBar;
00035 
00036   class MenuItem: public View
00037   {
00038     NUX_DECLARE_OBJECT_TYPE(MenuItem, View);
00039   public:
00040     MenuItem(const char *label, int UserValue, NUX_FILE_LINE_PROTO);
00041     ~MenuItem();
00042 
00043     void DrawAsMenuItem(GraphicsEngine &graphics_engine, const Color &textcolor, bool is_highlighted, bool isFirstItem, bool isLastItem, bool draw_icone);
00044 
00045     //const ActionItem& GetItem() const {return m_ActionItem;}
00046     ActionItem *GetActionItem() const;
00047     //ActionItem* GetActionItem();
00048     
00049     int GetTextWidth();
00050     int GetTextHeight();
00051 
00052     StaticText * GetStaticText()
00053     {
00054       return _pango_static_text;
00055     }
00056 
00057   private:
00058     virtual void Draw(GraphicsEngine &graphics_engine, bool force_draw);
00059     virtual void DrawContent(GraphicsEngine &graphics_engine, bool force_draw) {};
00060     virtual void PostDraw(GraphicsEngine &graphics_engine, bool force_draw) {};
00061 
00062     void SetChildMenu(MenuPage *menu);
00063     MenuPage *GetChildMenu() const;
00064     void SetActionItem(ActionItem *menu);
00065 
00066     MenuPage    *_child_menu;
00067     ActionItem  *_action_item;
00068     StaticText  *_pango_static_text;
00069     friend class MenuPage;
00070   };
00071 
00072   class MenuSeparator: public View
00073   {
00074     NUX_DECLARE_OBJECT_TYPE(MenuSeparator, View);
00075   public:
00076     MenuSeparator(NUX_FILE_LINE_PROTO);
00077     ~MenuSeparator();
00078 
00079     virtual void Draw(GraphicsEngine &graphics_engine, bool force_draw);
00080     virtual void DrawContent(GraphicsEngine &graphics_engine, bool force_draw) {};
00081     virtual void PostDraw(GraphicsEngine &graphics_engine, bool force_draw) {};
00082 
00083   private:
00084     friend class MenuPage;
00085   };
00086 
00087   class MenuPage: public View
00088   {
00089     NUX_DECLARE_OBJECT_TYPE(MenuPage, View);
00090   public:
00091     MenuPage(const char *title = "", NUX_FILE_LINE_PROTO);
00092     ~MenuPage();
00093 
00094 //    void SetName(const char* name);
00095     const char *GetName() const;
00096 
00097     ActionItem *AddAction(const char *label = 0, int UserValue = 0);
00098     //void AddActionItem(ActionItem* actionItem);
00099     void AddSeparator();
00100     MenuPage *AddMenu(const char *label);
00101     ActionItem *AddSubMenu(const char *label, MenuPage *menu);
00102 
00103     void RemoveItem(ActionItem *item);
00104     void RemoveAllItem();
00105 
00106     bool CanClose() const;
00107     // emitters
00108     void EmitMouseMove(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags);
00109     void EmitMouseUp(int x, int y, unsigned long button_flags, unsigned long key_flags);
00110     void EmitMouseDown(int x, int y, unsigned long button_flags, unsigned long key_flags);
00111     void EmitMouseDrag(int x, int y, int dx, int dy, unsigned long button_flags, unsigned long key_flags);
00112     void RecvMouseLeave(int x, int y, unsigned long button_flags, unsigned long key_flags);
00113 
00114 //private:
00116     /*
00117         Start the MenuPage iteration and show it.
00118         When this function is called, the menu becomes visible and appear at position(MenuXPosition, MenuYPosition).
00119         The menu also assumes it has received a mouse down event at coordinates(x, y).
00120         \param MenuXPosition: the position of the menu.
00121         \param MenuYPosition: the position of the menu.
00122         \param x: the simulate position where the mouse down happened on the menu area.
00123         \param y: the simulate position where the mouse down happened on the menu area.
00124         \param TakeMousefocus: if true, the MenuPage area will take the mouse focus.
00125     */
00126     void StartMenu(int MenuXPosition, int MenuYPosition, int x = 0, int y = 0, bool OverrideCurrentMenuChain = true);
00127 
00129     /*
00130         Stop the MenuPage iteration and hide it.
00131         \param x: the simulate position where the mouse down happened on the menu area.
00132         \param y: the simulate position where the mouse down happened on the menu area.
00133 
00134     */
00135     void StopMenu(int x = 0, int y = 0);
00136 
00137     void SetFontName(char *font_name);
00138 
00140 
00148     void SetOnClosureContinueEventCycle(bool on_closure_continue_with_event);
00149 
00151 
00159     bool OnClosureContinueEventCycle() const;
00160 
00161   public:
00162     void StopActionSubMenu();
00163     void ExecuteActionItem(MenuItem *menuItem);
00164 
00165     void NotifyActionTriggeredToParent(MenuPage *, MenuItem *menuItem);
00166     void NotifyTerminateMenuCascade();
00167     void NotifyMouseDownOutsideMenuCascade(int x, int y);
00168 
00169     void SetParentMenu(MenuPage *);
00170     MenuPage *GetParentMenu();
00171 
00172     void setShowItemIcon(bool b)
00173     {
00174       m_show_item_icon = b;
00175     }
00176     bool ShowItemIcon()
00177     {
00178       return m_show_item_icon;
00179     }
00180     bool TestMouseDown();
00181     bool TestMouseUp(int x, int y, unsigned long button_flags, unsigned long key_flags, bool &hit_inside_a_menu);
00182 
00183     // Never call this function directly
00184     void Terminate(int x, int y, unsigned long button_flags, unsigned long key_flags);
00185 
00186     // signals
00187     sigc::signal<void, int> sigItemSelected;
00189     /*
00190         The MenuPage object sends this signal when an action is triggered.
00191         \param MenuPage the menu object sending the signal.
00192         \param ActionItem the action object that was triggered in the menu.
00193     */
00194     sigc::signal<void, MenuPage *, ActionItem * > sigActionTriggered;
00195 
00197     /*
00198         The MenuPage object send this signal to inform that it needs to be close.
00199         The receiving object must close the MenuPage by calling the member function StopMenu().
00200         Any object that controls a menu should intercept sigTerminateMenuCascade and sigMouseDownOutsideMenuCascade.
00201     */
00202     sigc::signal<void> sigTerminateMenuCascade;
00203 
00205     /*
00206         Notify that a mouse down event happened outside the menu cascade. This event is processed by the MenuPage bar. The menu bar
00207         checks if the mouse down happened on one of its menu bar item. If yes, it let the menu bar item process the event.
00208         if no, it will initiate the closure of the menu cascade.
00209         Any object that controls a menu should intercept sigTerminateMenuCascade and sigMouseDownOutsideMenuCascade.
00210     */
00211     sigc::signal<void, MenuPage *, int, int> sigMouseDownOutsideMenuCascade;
00212 
00213     sigc::signal<void, MenuPage *> sigOpeningMenu;
00214     sigc::signal<void, MenuPage *> sigClosingMenu;
00215 
00216     void SetActive(bool b)
00217     {
00218       m_IsActive = b;
00219 
00220       if (b)
00221         /*m_PopupArea.*/CaptureMouseDownAnyWhereElse(true);
00222       else
00223         /*m_PopupArea.*/CaptureMouseDownAnyWhereElse(false);
00224     }
00225 
00226     bool IsActive() const
00227     {
00228       return m_IsActive;
00229     }
00230 
00234     int GetNumItem() const
00235     {
00236       return m_numItem;
00237     }
00238     ActionItem *GetActionItem(int i) const;
00239 
00244     int GetActionItemIndex(ActionItem *action) const;
00245 
00247 
00254     virtual Geometry GetAbsoluteGeometry() const;
00255 
00257 
00261     virtual Geometry GetRootGeometry() const;
00262     
00263   protected:
00264     virtual Area* FindAreaUnderMouse(const Point& mouse_position, NuxEventType event_type);
00265     virtual void Draw(GraphicsEngine &graphics_engine, bool force_draw);
00266     virtual void DrawContent(GraphicsEngine &graphics_engine, bool force_draw);
00267     virtual void PostDraw(GraphicsEngine &graphics_engine, bool force_draw);
00268 
00269   private:
00270 
00271     bool on_closure_continue_with_event_;
00272     int m_numItem;
00273     int m_HighlightedItem;
00274     bool m_IsActive;
00275     VLayout* _vlayout;
00276     bool m_NextMouseUpMeanStop;
00277     MenuItem *m_SubMenuAction;
00278     NString m_Name;
00279 
00280     bool m_Action_Triggered;
00281     MenuPage *m_Parent;
00282 
00283     // Set to TRUE if one of the MenuItem processed the mouse event.
00284     bool m_MouseEventProcessed;
00285 
00286     int m_item_width;
00287     int m_item_height;
00288     bool m_show_item_icon;
00289     std::vector<MenuItem *> m_MenuItemVector;
00290     std::vector< MenuSeparator * > m_MenuSeparatorVector;
00291     BaseWindow *m_MenuWindow;
00292 
00294     bool m_IsTopOfMenuChain;
00295 
00296     char *_font_name;
00297 
00298   public:
00300     // AbstractInterfaceObject
00302 
00303     virtual long ComputeContentSize();
00304     virtual void SetGeometry(const Geometry &geo);
00305 
00306     friend class MenuBar;
00307     friend class WindowCompositor;
00308   };
00309 
00310 }
00311 
00312 #endif // MENUPAGE_H