OSDN Git Service

menu.hpp: 101022_menu\3.5_menu_exception
[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 menu
33                 {
34                 private:
35                         bool _InsertMenuItem(UINT uItem, BOOL fByPosition, LPCMENUITEMINFO lpmii){
36                                 return ( InsertMenuItem(m_hMenu, uItem, fByPosition, lpmii) == TRUE );
37                         }
38                         bool _InsertMenu(UINT uPosition, UINT uFlags, UINT_PTR uIDNewItem, lpctstr_t lpNewItem){
39                                 return ( InsertMenu(m_hMenu, uPosition, uFlags, uIDNewItem, lpNewItem) == TRUE );
40                         }
41                         void destroy_menu()
42                         {
43                                 if ( !m_bAttachedMenu )
44                                         if ( ::DestroyMenu(m_hMenu) == FALSE )
45                                                 windows_menu_exception("::DestroyMenu() Returned Error.");
46                         }
47                 protected:
48                         ::HMENU m_hMenu;
49                         bool m_bAttachedMenu;
50                 public:
51                         menu(){
52                                 m_bAttachedMenu = false;
53                                 m_hMenu = ::CreateMenu();
54                                 if ( m_hMenu == NULL )
55                                         windows_menu_exception("::CreateMenu() Returned Error.");
56                         }
57                         menu(HMENU hMenuAttach){
58                                 m_bAttachedMenu = true;
59                                 m_hMenu = hMenuAttach;
60                         }
61                         virtual ~menu(){ destroy_menu(); }
62                         
63                         //
64                         void attach(HMENU hMenuAttach){
65                                 destroy_menu();
66                                 
67                                 m_bAttachedMenu = true;
68                                 m_hMenu = hMenuAttach;
69                         }
70                         void attach(const menu& menu_in){
71                                 destroy_menu();
72                                 
73                                 m_bAttachedMenu = true;
74                                 m_hMenu = menu_in.m_hMenu;
75                         }
76                         
77                         //////
78                         
79                         bool insert(unsigned int index, lpctstr_t szText, UINT itemId=0){ return _InsertMenu(index, MF_BYPOSITION, itemId, szText); }
80                         bool insert_separater(unsigned int index){ return _InsertMenu(index, MF_BYPOSITION | MF_SEPARATOR, NULL, NULL); }
81
82                         bool insert_byid(unsigned int before_id, lpctstr_t szText, UINT itemId=0){ return _InsertMenu(before_id, MF_BYCOMMAND, itemId, szText); }
83                         bool insert_separater_byid(unsigned int before_id){ return _InsertMenu(before_id, MF_BYCOMMAND | MF_SEPARATOR, NULL, NULL); }
84
85                         bool add(lpctstr_t szText, UINT itemId=0){ return insert(-1, szText, itemId); }
86                         bool add_separater(){ return insert_separater(-1); }
87                         bool add_sep(){ return add_separater(); }
88                         
89                         //////
90
91                         HMENU get_hmenu(){ return m_hMenu; }
92                         operator HMENU (){ return get_hmenu(); }
93                 };
94                 
95                 ////////////////////////////////////////////////////////////////
96         }
97 }
98
99 #endif//__SFJP_ROAST_EX__windows__menu_HPP__