1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/wince/menuce.cpp
3 // Purpose: Smartphone menus implementation
4 // Author: Wlodzimierz ABX Skiba
8 // Copyright: (c) Wlodzimierz Skiba
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
31 #include "wx/toplevel.h"
41 #include "wx/msw/wince/missing.h"
43 #include "wx/msw/wince/resources.h"
45 #include "wx/stockitem.h"
47 wxTopLevelWindowMSW::ButtonMenu::ButtonMenu()
50 m_label
= wxEmptyString
;
55 wxTopLevelWindowMSW::ButtonMenu::~ButtonMenu()
60 void wxTopLevelWindowMSW::SetLeftMenu(int id
, const wxString
& label
, wxMenu
*subMenu
)
62 m_LeftButton
.SetButton(id
, label
, subMenu
);
66 void wxTopLevelWindowMSW::SetRightMenu(int id
, const wxString
& label
, wxMenu
*subMenu
)
68 m_RightButton
.SetButton(id
, label
, subMenu
);
72 void wxTopLevelWindowMSW::ButtonMenu::SetButton(int id
, const wxString
& label
, wxMenu
*subMenu
)
76 if(label
.empty() && wxIsStockID(id
))
77 m_label
= wxGetStockLabel(id
, wxSTOCK_NOFLAGS
);
83 wxMenu
*wxTopLevelWindowMSW::ButtonMenu::DuplicateMenu(wxMenu
*menu
)
85 // This is required in case of converting wxMenuBar to wxMenu in wxFrame::SetMenuBar.
86 // All submenus has to be recreated because of new owner.
88 wxMenu
*duplication
= new wxMenu
;
92 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
95 wxMenuItem
*item
= node
->GetData();
98 wxMenu
*submenu
= NULL
;
100 if(item
->IsSubMenu())
101 submenu
= DuplicateMenu( item
->GetSubMenu() );
105 wxMenuItem
*new_item
= wxMenuItem::New(duplication
, item
->GetId(), item
->GetLabel(), item
->GetHelp(), item
->GetKind(), submenu
);
107 if( item
->IsCheckable() )
108 new_item
->Check(item
->IsChecked());
110 new_item
->Enable( item
->IsEnabled() );
112 duplication
->Append(new_item
);
114 node
= node
->GetNext();
122 void wxMenuToHMenu(wxMenu
* in
, HMENU hMenu
)
128 wxMenuItemList::compatibility_iterator node
= in
->GetMenuItems().GetFirst();
131 wxMenuItem
*item
= node
->GetData();
137 if( item
->IsSeparator() )
139 uFlags
|= MF_SEPARATOR
;
140 uIDNewItem
= (unsigned)wxID_ANY
;
147 wxStrcpy(buf
, item
->GetLabel().c_str());
151 uFlags
|= ( item
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
);
154 uFlags
|= ( item
->IsChecked() ? MF_CHECKED
: MF_UNCHECKED
);
156 if( item
->IsSubMenu() )
159 HMENU hSubMenu
= CreatePopupMenu();
160 wxMenuToHMenu(item
->GetSubMenu(), hSubMenu
);
161 uIDNewItem
= (UINT
) hSubMenu
;
165 uIDNewItem
= item
->GetId();
169 AppendMenu(hMenu
, uFlags
, uIDNewItem
, lpNewItem
);
171 node
= node
->GetNext();
175 void wxTopLevelWindowMSW::ReloadButton(ButtonMenu
& button
, UINT menuID
)
177 TBBUTTONINFO button_info
;
181 memset (&button_info
, 0, sizeof (TBBUTTONINFO
));
182 button_info
.cbSize
= sizeof(TBBUTTONINFO
);
183 button_info
.dwMask
= TBIF_TEXT
| TBIF_STATE
;
184 button_info
.fsState
= TBSTATE_ENABLED
;
185 wxStrcpy(buf
, button
.GetLabel().c_str());
186 button_info
.pszText
= buf
;
187 ::SendMessage(m_MenuBarHWND
, TB_SETBUTTONINFO
, menuID
, (LPARAM
) &button_info
);
191 HMENU hPopupMenu
= (HMENU
) ::SendMessage(m_MenuBarHWND
, SHCMBM_GETSUBMENU
, 0, menuID
);
192 RemoveMenu(hPopupMenu
, 0, MF_BYPOSITION
);
193 wxMenuToHMenu(button
.GetMenu(), hPopupMenu
);
197 void wxTopLevelWindowMSW::ReloadAllButtons()
199 // first reaload only after initialization of both buttons
200 // it should is done at the end of Create() of wxTLW
201 if(!m_LeftButton
.IsAssigned() || !m_RightButton
.IsAssigned())
204 SHMENUBARINFO menu_bar
;
207 memset (&menu_bar
, 0, sizeof (SHMENUBARINFO
));
208 menu_bar
.cbSize
= sizeof (SHMENUBARINFO
);
209 menu_bar
.hwndParent
= (HWND
) GetHWND();
211 if(m_LeftButton
.IsMenu() && m_RightButton
.IsMenu())
212 menu_bar
.nToolBarId
= IDR_MENUBAR_BOTH_MENUS
;
213 else if(m_LeftButton
.IsMenu())
214 menu_bar
.nToolBarId
= IDR_MENUBAR_LEFT_MENU
;
215 else if(m_RightButton
.IsMenu())
216 menu_bar
.nToolBarId
= IDR_MENUBAR_RIGHT_MENU
;
218 menu_bar
.nToolBarId
= IDR_MENUBAR_ONE_BUTTON
;
220 menu_bar
.hInstRes
= wxGetInstance();
222 if (!SHCreateMenuBar(&menu_bar
))
224 wxFAIL_MSG( wxT("SHCreateMenuBar failed") );
228 HWND prev_MenuBar
= m_MenuBarHWND
;
229 m_MenuBarHWND
= menu_bar
.hwndMB
;
231 ReloadButton(m_LeftButton
, IDM_LEFT
);
232 ReloadButton(m_RightButton
, IDM_RIGHT
);
234 // hide previous and show new menubar
236 ::ShowWindow( prev_MenuBar
, SW_HIDE
);
237 ::ShowWindow( m_MenuBarHWND
, SW_SHOW
);
239 // Setup backspace key handling
240 SendMessage(m_MenuBarHWND
, SHCMBM_OVERRIDEKEY
, VK_TBACK
,
241 MAKELPARAM( SHMBOF_NODEFAULT
| SHMBOF_NOTIFY
,
242 SHMBOF_NODEFAULT
| SHMBOF_NOTIFY
));
245 bool wxTopLevelWindowMSW::HandleCommand(WXWORD id
, WXWORD
WXUNUSED(cmd
), WXHWND
WXUNUSED(control
))
247 // handle here commands from Smartphone menu bar
248 if ( id
== IDM_LEFT
|| id
== IDM_RIGHT
)
250 int menuId
= id
== IDM_LEFT
? m_LeftButton
.GetId() : m_RightButton
.GetId() ;
251 wxCommandEvent
commandEvent(wxEVT_MENU
, menuId
);
252 commandEvent
.SetEventObject(this);
253 GetEventHandler()->ProcessEvent(commandEvent
);
259 bool wxTopLevelWindowMSW::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
261 MSG
*msg
= (MSG
*)pMsg
;
263 // Process back key to be like backspace.
264 if (msg
->message
== WM_HOTKEY
)
266 if (HIWORD(msg
->lParam
) == VK_TBACK
)
267 SHSendBackToFocusWindow(msg
->message
, msg
->wParam
, msg
->lParam
);
270 return wxTopLevelWindowBase::MSWShouldPreProcessMessage(pMsg
);
273 #endif // __SMARTPHONE__ && __WXWINCE__