UTMenu.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 // Copyright (C) 2010  Brian Tietz    sdbtietz at yahoo dot com
00003 //
00004 // This program is free software; you can redistribute it and/or modify it under the terms of the
00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the
00006 // License.
00007 //
00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010 // General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License along with this program; if
00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00014 // 02110-1301, USA.
00015 //
00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com)
00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that
00018 // license as applied to the UT library in exchange for either substantial contributions to the
00019 // development of the UT library or other forms of compensation.  Any such waiver must be
00020 // established in writing between the copyright holder and the commercial entity obtaining such a
00021 // waiver.
00022 //==================================================================================================
00023 
00024 
00025 #ifndef _UT_MENU_H_
00026 #define _UT_MENU_H_
00027 
00030 
00031 
00032 //==================================================================================================
00033 //=== Project headers
00034 //==================================================================================================
00035 #include "UTgui.h"
00036 #include "UTLists.h"
00037 #include "UTMessageDestination.h"
00038 #include "UTGUIPoolAllocator.h"
00039 
00040 
00041 //==================================================================================================
00042 //=== Forward name declarations
00043 //==================================================================================================
00044 class Resource_t;
00045 class IOStream_t;
00046 class Menu_t;
00047 class MenuItem_t;
00048 class MenuBreak_t;
00049 class MenuMerge_t;
00050 
00051 
00052 //==================================================================================================
00053 //=== Predefined menu IDs
00054 //==================================================================================================
00055 
00057 const int eRCMENU_exit                  = 1;
00058 
00060 const int eRCMENU_prefs                 = 2;
00061 
00063 const int eRCMENU_file                  = 3;
00064 
00066 const int eRCMENU_edit                  = 4;
00067 
00069 const int eRCMENU_view                  = 5;
00070 
00072 const int eRCMENU_project               = 6;
00073 
00075 const int eRCMENU_tools                 = 7;
00076 
00078 const int eRCMENU_windows               = 8;
00079 
00081 const int eRCMENU_help                  = 9;
00082 
00085 const int eRCMENU_about                 = 10;
00086 
00088 const int eRCMENU_cascade               = 12;
00089 
00091 const int eRCMENU_tile_horizontal       = 13;
00092 
00094 const int eRCMENU_tile_vertical         = 14;
00095 
00097 const int eRCMENU_close_all             = 15;
00098 
00100 const int eRCMENU_window_list_break     = 16;
00101 
00103 const int eRCMENU_window_list_break_end = 17;
00104 
00106 const int eRCMENU_more_windows          = 18;
00107 
00109 const int eRCMENU_min_reserved          = 19;
00110 
00112 const int eRCMENU_max_reserved          = 99;
00113 
00114 
00115 //==================================================================================================
00116 class UTGUI_EXPORT MenuNode_t
00117 //==================================================================================================
00118 : public BTreeNode_t< MenuNode_t, uint >    // By token
00124 {
00125     //----------------------------------------------------------------------------------------------
00126     public:
00127     //----------------------------------------------------------------------------------------------
00128     inline void*                        operator new(size_t size);
00131 
00132     inline void                         operator delete(void* block);
00135 
00136     inline int                          MenuID() const;
00140 
00141     //----------------------------------------------------------------------------------------------
00142     private:
00143     //----------------------------------------------------------------------------------------------
00144                                         MenuNode_t(int menu_id);
00145     virtual                             ~MenuNode_t();
00146 
00147     inline                              operator int() const;
00148     inline int                          Compare(int other_menu_id) const;
00149 
00150     inline                              operator uint() const;
00151     inline int                          Compare(uint other_token) const;
00152 
00153     //----------------------------------------------------------------------------------------------
00154     private:
00155     //----------------------------------------------------------------------------------------------
00156     int                                 m_menu_id;
00157     uint                                m_token;    // Cannot be in reserved range
00158     #if WINDOWS
00159                                             #define c_reserved_menu_token_min 0xf000
00160                                             #define c_reserved_menu_token_max 0xf120
00161     #endif
00162     Menu_t*                             m_parent_menu;
00163 
00164     static BTree_t< MenuNode_t, uint >  ms_valid_menu_nodes;
00165     static uint                         ms_next_token;
00166 
00167     friend class Menu_t;
00168     friend class MenuItem_t;
00169     friend class MenuBreak_t;
00170     friend class MenuMerge_t;
00171     friend class MenuBar_t;
00172     friend class MergedMenuNode_t;
00173     friend class OwnedList_t<MenuNode_t>;
00174     friend class SortedList_t< MenuNode_t, int >;
00175     friend class BTree_t< MenuNode_t, uint >;
00176     friend class BTreeNode_t< MenuNode_t, uint >;
00177     friend class AppPlatformInfo_t;
00178 };
00179 
00180 
00181 //==================================================================================================
00182 class UTGUI_EXPORT Menu_t
00183 //==================================================================================================
00184 : public MenuNode_t
00198 {
00199     //----------------------------------------------------------------------------------------------
00200     public:
00201     //----------------------------------------------------------------------------------------------
00202                         Menu_t();
00204 
00205                         Menu_t( int resource_id, Resource_t* resources = NULL );
00211 
00212                         Menu_t( int menu_id, takes String_t* submenu_name );
00215 
00216     virtual             ~Menu_t();
00218 
00219     inline String_t*    Label() const;
00221 
00222     inline int          MenuID() const;
00225 
00226     void                AddSubmenu( takes Menu_t* menu, int index = -1 );
00235 
00236     Menu_t*             FindSubmenu(int menu_id);
00239 
00240     gives Menu_t*       RemoveSubmenu(Menu_t* menu);
00244 
00245     gives Menu_t*       RemoveSubmenu(int menu_id);
00250 
00251     void                AddItem( takes MenuItem_t* item, int index = -1 );
00254 
00255     MenuItem_t*         FindItem(int menu_id);
00258 
00259     gives MenuItem_t*   RemoveItem(MenuItem_t* item);
00263 
00264     gives MenuItem_t*   RemoveItem(int menu_id);
00269 
00270     void                AddBreak( takes MenuBreak_t* item, int index = -1 );
00276 
00277     MenuBreak_t*        FindBreak(int menu_id);
00280 
00281     gives MenuBreak_t*  RemoveBreak(MenuBreak_t* item);
00285 
00286     gives MenuBreak_t*  RemoveBreak(int menu_id);
00291 
00292     void                AddWindowMenuMerge( takes MenuMerge_t* merge, int index = -1 );
00304 
00305     MenuMerge_t*        FindWindowMenuMerge(int menu_id);
00308 
00309     gives MenuMerge_t*  RemoveWindowMenuMerge(MenuMerge_t* merge);
00314 
00315     gives MenuMerge_t*  RemoveWindowMenuMerge(int menu_id);
00320 
00321     inline int          CountNodes() const;
00323 
00324     inline MenuNode_t*  NodeAt(int index) const;
00326 
00327     //----------------------------------------------------------------------------------------------
00328     private:
00329     //----------------------------------------------------------------------------------------------
00330     static void         LoadResourceMenu(   Menu_t* load_to,
00331                                             Resource_t* resources,
00332                                             int resource_id );
00333     static void         ReadMenuContents(   IOStream_t* stream,
00334                                             Menu_t* reading_to,
00335                                             bool is_submenu,
00336                                             OwnedList_t<Resource_t>* named_resources );
00337     static void         ReadMenuItem(   String_t* line,
00338                                         IOStream_t* stream,
00339                                         Menu_t* reading_to,
00340                                         OwnedList_t<Resource_t>* named_resources );
00341     static void         ReadMenu(   String_t* line,
00342                                     IOStream_t* stream,
00343                                     Menu_t* reading_to,
00344                                     OwnedList_t<Resource_t>* named_resources );
00345     static void         ReadBreak( String_t* line, IOStream_t* stream, Menu_t* reading_to );
00346     static void         ReadWindowMenuMerge(    String_t* line,
00347                                                 IOStream_t* stream,
00348                                                 Menu_t* reading_to );
00349     static void         AddExitMenuItem(    Menu_t* load_to,
00350                                             OwnedList_t<Resource_t>* named_resources );
00351     static void         AddPrefsMenuItem(   Menu_t* load_to,
00352                                             OwnedList_t<Resource_t>* named_resources );
00353     static void         AddWindowsMenu( Menu_t* load_to,
00354                                         OwnedList_t<Resource_t>* named_resources );
00355     static void         AddAboutMenuItem(   Menu_t* load_to,
00356                                             OwnedList_t<Resource_t>* named_resources );
00357     static Resource_t*  ModuleResource( OwnedList_t<Resource_t>* named_resources,
00358                                         stringliteral* name );
00359 
00360     #if DEBUG
00361     void                AssertParentOrGrandparentOf(MenuNode_t* node);
00362     #endif
00363     void                SetRootMenu(Menu_t* root_menu);
00364     void                AddSubmenuToKeyLookup( Menu_t* menu, bool sort );
00365     void                RemoveSubmenuFromKeyLookup(Menu_t* menu);
00366     void                AddMenuNodeToKeyLookup(MenuNode_t* item);
00367     void                RemoveMenuNodeFromKeyLookup(MenuNode_t* item);
00368 
00369     void                AdjustPlatformPlacements();
00370 
00371     //----------------------------------------------------------------------------------------------
00372     private:
00373     //----------------------------------------------------------------------------------------------
00374     String_t*                           m_label;
00375     Menu_t*                             m_root_menu;
00376     OwnedList_t<MenuNode_t>             m_items;
00377     SortedList_t< MenuNode_t, int >*    m_key_lookup;
00378 
00379     friend class App_t;
00380     friend class MenuBar_t;
00381 };
00382 
00383 
00384 //==================================================================================================
00385 class UTGUI_EXPORT MenuItem_t
00386 //==================================================================================================
00387 : public MenuNode_t
00393 {
00394     //----------------------------------------------------------------------------------------------
00395     public:
00396     //----------------------------------------------------------------------------------------------
00397                                     MenuItem_t( int menu_id,
00398                                                 takes String_t* label,
00399                                                 bool checkable = false,
00400                                                 bool checked = false );
00404 
00405     virtual                         ~MenuItem_t();
00407 
00408     inline String_t*                Label() const;
00410 
00411     inline int                      MenuID() const;
00415 
00416     void                            SetAction(  takes Message_t* message_to_send,
00417                                                 const AbstractMessageDestination_t& destination );
00420 
00421     void                            Disable();
00425 
00426     inline bool                     IsEnabled() const;
00429 
00430     inline const Message_t*         ActionMessage() const;
00433 
00434     inline bool                     IsCheckable() const;
00436 
00437     inline bool                     IsChecked() const;
00441 
00442     void                            SetChecked(bool checked);
00445 
00446     //----------------------------------------------------------------------------------------------
00447     private:
00448     //----------------------------------------------------------------------------------------------
00449     void                            Activated();
00450                                     MenuItem_t(int menu_id);
00451 
00452     //----------------------------------------------------------------------------------------------
00453     private:
00454     //----------------------------------------------------------------------------------------------
00455     String_t*                       m_label;
00456     Message_t*                      m_action_message;
00457     AbstractMessageDestination_t    m_action_destination;
00458     bool                            m_checkable;
00459     bool                            m_checked;
00460 
00461     friend class Menu_t;
00462     friend class MenuBar_t;
00463     friend class AppPlatformInfo_t;
00464 };
00465 
00466 
00467 //==================================================================================================
00468 class UTGUI_EXPORT MenuBreak_t
00469 //==================================================================================================
00470 : public MenuNode_t
00476 {
00477     //----------------------------------------------------------------------------------------------
00478     public:
00479     //----------------------------------------------------------------------------------------------
00480                 MenuBreak_t(int menu_id);
00483 
00484     virtual     ~MenuBreak_t();
00486 
00487     inline int  MenuID() const;
00490 
00491 };
00492 
00493 
00494 //==================================================================================================
00495 class UTGUI_EXPORT MenuMerge_t
00496 //==================================================================================================
00497 : public MenuNode_t
00504 {
00505     //----------------------------------------------------------------------------------------------
00506     public:
00507     //----------------------------------------------------------------------------------------------
00508                 MenuMerge_t(int menu_id);
00511 
00512     virtual     ~MenuMerge_t();
00514 
00515     inline int  MenuID() const;
00518 };
00519 
00520 
00521 
00522 
00523 //==================================================================================================
00524 //==================================================================================================
00525 //===
00526 //=== Inline function implementations
00527 //===
00528 //==================================================================================================
00529 //==================================================================================================
00530 inline void*
00531 MenuNode_t::operator new(size_t size)
00532 {
00533     return g_gui_pool_allocator.Allocate(size);
00534 }
00535 
00536 
00537 inline void
00538 MenuNode_t::operator delete(void* block)
00539 {
00540     g_gui_pool_allocator.Release(block);
00541 }
00542 
00543 
00544 inline int
00545 MenuNode_t::MenuID() const
00546 {
00547     return m_menu_id;
00548 }
00549 
00550 
00551 inline
00552 MenuNode_t::operator int() const
00553 {
00554     assert(m_menu_id);
00555     return m_menu_id;
00556 }
00557 
00558 
00559 inline int
00560 MenuNode_t::Compare(int other_menu_id) const
00561 {
00562     assert(m_menu_id);
00563     return m_menu_id - other_menu_id;
00564 }
00565 
00566 
00567 inline
00568 MenuNode_t::operator uint() const
00569 {
00570     assert(m_token);
00571     return m_token;
00572 }
00573 
00574 
00575 inline int
00576 MenuNode_t::Compare(uint other_token) const
00577 {
00578     assert(m_token);
00579     return m_token - other_token;
00580 }
00581 
00582 
00583 inline String_t*
00584 Menu_t::Label() const
00585 {
00586     return m_label;
00587 }
00588 
00589 
00590 inline int
00591 Menu_t::MenuID() const
00592 {
00593     return m_menu_id;
00594 }
00595 
00596 
00597 inline int
00598 Menu_t::CountNodes() const
00599 {
00600     return m_items.CountItems();
00601 }
00602 
00603 
00604 inline MenuNode_t*
00605 Menu_t::NodeAt(int index) const
00606 {
00607     return m_items.ItemAt(index);
00608 }
00609 
00610 
00611 inline String_t*
00612 MenuItem_t::Label() const
00613 {
00614     return m_label;
00615 }
00616 
00617 
00618 inline int
00619 MenuItem_t::MenuID() const
00620 {
00621     return m_menu_id;
00622 }
00623 
00624 
00625 inline bool
00626 MenuItem_t::IsEnabled() const
00627 {
00628     return m_action_destination.IsValid();
00629 }
00630 
00631 
00632 inline const Message_t*
00633 MenuItem_t::ActionMessage() const
00634 {
00635     return m_action_message;
00636 }
00637 
00638 
00639 inline bool
00640 MenuItem_t::IsCheckable() const
00641 {
00642     return m_checkable;
00643 }
00644 
00645 
00646 inline bool
00647 MenuItem_t::IsChecked() const
00648 {
00649     assert(m_checkable);
00650     return m_checked;
00651 }
00652 
00653 
00654 inline int
00655 MenuBreak_t::MenuID() const
00656 {
00657     return m_menu_id;
00658 }
00659 
00660 
00661 inline int
00662 MenuMerge_t::MenuID() const
00663 {
00664     return m_menu_id;
00665 }
00666 
00667 
00668 #endif // _UT_MENU_H_

Generated on Tue Dec 14 22:35:05 2010 for UT library by  doxygen 1.6.1