OSDN Git Service

3673c69473e4b20d9d3b8dc870fb1f49378a545a
[roast/roast_ex_windows.git] / roast_ex / include / roast / windows / menu.hpp
1 //      Roast+ License
2 /*
3
4 */
5 #ifndef __SFJP_ROAST_EX__windows__menu_HPP__
6 #define __SFJP_ROAST_EX__windows__menu_HPP__
7
8 #include <windows.h>
9 #include <string>
10
11 namespace roast
12 {
13         namespace windows
14         {
15         /*      typedef ::LPCTSTR lpctstr_t;
16                 class windows_exception : public ::std::string
17                 {
18                 public:
19                         windows_exception(const char* s) : ::std::string(s) {}
20                         windows_exception(const ::std::string& s) : ::std::string(s) {}
21                 };*/
22                 
23                 class windows_menu_exception : public windows_exception
24                 {
25                 public:
26                         windows_menu_exception(const char* s) : windows_exception(s) {}
27                         windows_menu_exception(const ::std::string& s) : windows_exception(s) {}
28                 };
29
30                 /////////////////////////////////////////////////
31
32                 class menuitem
33                 {
34                 private:
35                         bool _SetMenuItemInfo(UINT uItem, BOOL fByPosition, LPMENUITEMINFO lpmii){
36                                 return ( SetMenuItemInfo(m_hMenu, uItem, fByPosition, lpmii) == TRUE );
37                         }
38                 protected:
39                         const ::HMENU m_hMenu;
40                         const UINT uItem;
41                         const BOOL fByPosition;
42                 public:
43                         menuitem(HMENU hMenu, UINT uItem_in, BOOL fByPosition_in)
44                                 : m_hMenu(hMenu), uItem(uItem_in), fByPosition(fByPosition_in) {}
45
46                 };
47
48                 class menu
49                 {
50                 private:
51                         bool _InsertMenu(UINT uPosition, UINT uFlags, UINT_PTR uIDNewItem, lpctstr_t lpNewItem){
52                                 return ( InsertMenu(m_hMenu, uPosition, uFlags, uIDNewItem, lpNewItem) == TRUE );
53                         }
54                         bool _InsertMenuItem(UINT uItem, BOOL fByPosition, LPCMENUITEMINFO lpmii){
55                                 return ( InsertMenuItem(m_hMenu, uItem, fByPosition, lpmii) == TRUE );
56                         }
57                         /*bool _SetMenuItemInfo(UINT uItem, BOOL fByPosition, LPMENUITEMINFO lpmii){
58                                 return ( SetMenuItemInfo(m_hMenu, uItem, fByPosition, lpmii) == TRUE );
59                         }*/
60                         void destroy_menu()
61                         {
62                                 if ( !m_bAttachedMenu )
63                                         if ( ::DestroyMenu(m_hMenu) == FALSE )
64                                                 windows_menu_exception("::DestroyMenu() Returned Error.");
65                         }
66                 protected:
67                         ::HMENU m_hMenu;
68                         bool m_bAttachedMenu;
69                 public:
70                         menu(){
71                                 m_bAttachedMenu = false;
72                                 m_hMenu = ::CreateMenu();
73                                 if ( m_hMenu == NULL )
74                                         windows_menu_exception("::CreateMenu() Returned Error.");
75                         }
76                         menu(HMENU hMenuAttach){
77                                 m_bAttachedMenu = true;
78                                 m_hMenu = hMenuAttach;
79                         }
80                         virtual ~menu(){ destroy_menu(); }
81                         
82                         //
83                         void attach(HMENU hMenuAttach){
84                                 destroy_menu();
85                                 
86                                 m_bAttachedMenu = true;
87                                 m_hMenu = hMenuAttach;
88                         }
89                         void attach(const menu& menu_in){
90                                 destroy_menu();
91                                 
92                                 m_bAttachedMenu = true;
93                                 m_hMenu = menu_in.m_hMenu;
94                         }
95                         
96                         //////
97                         
98                         bool insert(unsigned int index, lpctstr_t szText, UINT itemId=0){ return _InsertMenu(index, MF_BYPOSITION, itemId, szText); }
99                         bool insert_separater(unsigned int index){ return _InsertMenu(index, MF_BYPOSITION | MF_SEPARATOR, NULL, NULL); }
100
101                         bool insert_byid(unsigned int before_id, lpctstr_t szText, UINT itemId=0){ return _InsertMenu(before_id, MF_BYCOMMAND, itemId, szText); }
102                         bool insert_separater_byid(unsigned int before_id){ return _InsertMenu(before_id, MF_BYCOMMAND | MF_SEPARATOR, NULL, NULL); }
103
104                         bool add(lpctstr_t szText, UINT itemId=0){ return insert(-1, szText, itemId); }
105                         bool add_separater(){ return insert_separater(-1); }
106                         bool add_sep(){ return add_separater(); }
107
108                         bool attach_submenu(unsigned int index, HMENU hSubMenu){}
109                         bool attach_submenu_byid(unsigned int itemid, HMENU hSubMenu){}
110                         
111                         ///////////////////////////////////////////////
112
113                         menuitem get(unsigned int index){ return menuitem(m_hMenu, index, TRUE); }
114                         menuitem get_byid(UINT itemId){ return menuitem(m_hMenu, itemId, FALSE); }
115                         menuitem operator [](unsigned int index){ return get(index); }
116
117                         //////
118
119                         HMENU get_hmenu(){ return m_hMenu; }
120                         operator HMENU (){ return get_hmenu(); }
121                 };
122                 
123                 ////////////////////////////////////////////////////////////////
124         }
125 }
126
127 #endif//__SFJP_ROAST_EX__windows__menu_HPP__