1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/wince/menuce.cpp
3 // Purpose: Smartphone menus implementation
4 // Author: Wlodzimierz ABX Skiba
8 // Copyright: (c) Wlodzimierz Skiba
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/toplevel.h"
37 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
43 #include "wx/msw/wince/missing.h"
45 #include "wx/msw/wince/resources.h"
47 #include "wx/stockitem.h"
49 wxTopLevelWindowMSW::ButtonMenu::ButtonMenu()
52 m_label
= wxEmptyString
;
57 wxTopLevelWindowMSW::ButtonMenu::~ButtonMenu()
66 void wxTopLevelWindowMSW::SetLeftMenu(int id
, const wxString
& label
, wxMenu
*subMenu
)
68 m_LeftButton
.SetButton(id
, label
, subMenu
);
72 void wxTopLevelWindowMSW::SetRightMenu(int id
, const wxString
& label
, wxMenu
*subMenu
)
74 m_RightButton
.SetButton(id
, label
, subMenu
);
78 void wxTopLevelWindowMSW::ButtonMenu::SetButton(int id
, const wxString
& label
, wxMenu
*subMenu
)
82 if(label
.empty() && wxIsStockID(id
))
83 m_label
= wxGetStockLabel(id
, false);
89 wxMenu
*wxTopLevelWindowMSW::ButtonMenu::DuplicateMenu(wxMenu
*menu
)
91 // This is required in case of converting wxMenuBar to wxMenu in wxFrame::SetMenuBar.
92 // All submenus has to be recreated because of new owner.
94 wxMenu
*duplication
= new wxMenu
;
98 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
101 wxMenuItem
*item
= node
->GetData();
104 wxMenu
*submenu
= NULL
;
106 if(item
->IsSubMenu())
107 submenu
= DuplicateMenu( item
->GetSubMenu() );
111 wxMenuItem
*new_item
= wxMenuItem::New(duplication
, item
->GetId(), item
->GetLabel(), item
->GetHelp(), item
->GetKind(), submenu
);
113 if( item
->IsCheckable() )
114 new_item
->Check(item
->IsChecked());
116 new_item
->Enable( item
->IsEnabled() );
118 duplication
->Append(new_item
);
120 node
= node
->GetNext();
128 void wxMenuToHMenu(wxMenu
* in
, HMENU hMenu
)
134 wxMenuItemList::compatibility_iterator node
= in
->GetMenuItems().GetFirst();
137 wxMenuItem
*item
= node
->GetData();
143 if( item
->IsSeparator() )
145 uFlags
|= MF_SEPARATOR
;
146 uIDNewItem
= (unsigned)wxID_ANY
;
153 wxStrcpy(buf
, item
->GetLabel().c_str());
157 uFlags
|= ( item
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
);
160 uFlags
|= ( item
->IsChecked() ? MF_CHECKED
: MF_UNCHECKED
);
162 if( item
->IsSubMenu() )
165 HMENU hSubMenu
= CreatePopupMenu();
166 wxMenuToHMenu(item
->GetSubMenu(), hSubMenu
);
167 uIDNewItem
= (UINT
) hSubMenu
;
171 uIDNewItem
= item
->GetId();
175 AppendMenu(hMenu
, uFlags
, uIDNewItem
, lpNewItem
);
177 node
= node
->GetNext();
181 void wxTopLevelWindowMSW::ReloadButton(ButtonMenu
& button
, UINT menuID
)
183 TBBUTTONINFO button_info
;
187 memset (&button_info
, 0, sizeof (TBBUTTONINFO
));
188 button_info
.cbSize
= sizeof(TBBUTTONINFO
);
189 button_info
.dwMask
= TBIF_TEXT
| TBIF_STATE
;
190 button_info
.fsState
= TBSTATE_ENABLED
;
191 wxStrcpy(buf
, button
.GetLabel().c_str());
192 button_info
.pszText
= buf
;
193 ::SendMessage(m_MenuBarHWND
, TB_SETBUTTONINFO
, menuID
, (LPARAM
) &button_info
);
197 HMENU hPopupMenu
= (HMENU
) ::SendMessage(m_MenuBarHWND
, SHCMBM_GETSUBMENU
, 0, menuID
);
198 RemoveMenu(hPopupMenu
, 0, MF_BYPOSITION
);
199 wxMenuToHMenu(button
.GetMenu(), hPopupMenu
);
203 void wxTopLevelWindowMSW::ReloadAllButtons()
205 // first reaload only after initialization of both buttons
206 // it should is done at the end of Create() of wxTLW
207 if(!m_LeftButton
.IsAssigned() || !m_RightButton
.IsAssigned())
210 SHMENUBARINFO menu_bar
;
213 memset (&menu_bar
, 0, sizeof (SHMENUBARINFO
));
214 menu_bar
.cbSize
= sizeof (SHMENUBARINFO
);
215 menu_bar
.hwndParent
= (HWND
) GetHWND();
217 if(m_LeftButton
.IsMenu() && m_RightButton
.IsMenu())
218 menu_bar
.nToolBarId
= IDR_MENUBAR_BOTH_MENUS
;
219 else if(m_LeftButton
.IsMenu())
220 menu_bar
.nToolBarId
= IDR_MENUBAR_LEFT_MENU
;
221 else if(m_RightButton
.IsMenu())
222 menu_bar
.nToolBarId
= IDR_MENUBAR_RIGHT_MENU
;
224 menu_bar
.nToolBarId
= IDR_MENUBAR_ONE_BUTTON
;
226 menu_bar
.hInstRes
= wxGetInstance();
228 if (!SHCreateMenuBar(&menu_bar
))
230 wxFAIL_MSG( _T("SHCreateMenuBar failed") );
234 HWND prev_MenuBar
= m_MenuBarHWND
;
235 m_MenuBarHWND
= menu_bar
.hwndMB
;
237 ReloadButton(m_LeftButton
, IDM_LEFT
);
238 ReloadButton(m_RightButton
, IDM_RIGHT
);
240 // hide previous and show new menubar
242 ::ShowWindow( prev_MenuBar
, SW_HIDE
);
243 ::ShowWindow( m_MenuBarHWND
, SW_SHOW
);
247 bool wxTopLevelWindowMSW::HandleCommand(WXWORD id
, WXWORD
WXUNUSED(cmd
), WXHWND
WXUNUSED(control
))
249 // handle here commands from Smartphone menu bar
250 if ( id
== IDM_LEFT
|| id
== IDM_RIGHT
)
252 int menuId
= id
== IDM_LEFT
? m_LeftButton
.GetId() : m_RightButton
.GetId() ;
253 wxCommandEvent
commandEvent(wxEVT_COMMAND_MENU_SELECTED
, menuId
);
254 commandEvent
.SetEventObject(this);
255 GetEventHandler()->ProcessEvent(commandEvent
);
261 #endif // __SMARTPHONE__ && __WXWINCE__