1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/wince/menuce.cpp
3 // Purpose: Smartphone menus implementation
4 // Author: Wlodzimierz ABX Skiba
7 // Copyright: (c) Wlodzimierz Skiba
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #if defined(__SMARTPHONE__) && defined(__WXWINCE__)
30 #include "wx/toplevel.h"
40 #include "wx/msw/wince/missing.h"
42 #include "wx/msw/wince/resources.h"
44 #include "wx/stockitem.h"
46 wxTopLevelWindowMSW::ButtonMenu::ButtonMenu()
49 m_label
= wxEmptyString
;
54 wxTopLevelWindowMSW::ButtonMenu::~ButtonMenu()
59 void wxTopLevelWindowMSW::SetLeftMenu(int id
, const wxString
& label
, wxMenu
*subMenu
)
61 m_LeftButton
.SetButton(id
, label
, subMenu
);
65 void wxTopLevelWindowMSW::SetRightMenu(int id
, const wxString
& label
, wxMenu
*subMenu
)
67 m_RightButton
.SetButton(id
, label
, subMenu
);
71 void wxTopLevelWindowMSW::ButtonMenu::SetButton(int id
, const wxString
& label
, wxMenu
*subMenu
)
75 if(label
.empty() && wxIsStockID(id
))
76 m_label
= wxGetStockLabel(id
, wxSTOCK_NOFLAGS
);
82 wxMenu
*wxTopLevelWindowMSW::ButtonMenu::DuplicateMenu(wxMenu
*menu
)
84 // This is required in case of converting wxMenuBar to wxMenu in wxFrame::SetMenuBar.
85 // All submenus has to be recreated because of new owner.
87 wxMenu
*duplication
= new wxMenu
;
91 wxMenuItemList::compatibility_iterator node
= menu
->GetMenuItems().GetFirst();
94 wxMenuItem
*item
= node
->GetData();
97 wxMenu
*submenu
= NULL
;
100 submenu
= DuplicateMenu( item
->GetSubMenu() );
104 wxMenuItem
*new_item
= wxMenuItem::New(duplication
, item
->GetId(), item
->GetLabel(), item
->GetHelp(), item
->GetKind(), submenu
);
106 if( item
->IsCheckable() )
107 new_item
->Check(item
->IsChecked());
109 new_item
->Enable( item
->IsEnabled() );
111 duplication
->Append(new_item
);
113 node
= node
->GetNext();
121 void wxMenuToHMenu(wxMenu
* in
, HMENU hMenu
)
127 wxMenuItemList::compatibility_iterator node
= in
->GetMenuItems().GetFirst();
130 wxMenuItem
*item
= node
->GetData();
136 if( item
->IsSeparator() )
138 uFlags
|= MF_SEPARATOR
;
139 uIDNewItem
= (unsigned)wxID_ANY
;
146 wxStrcpy(buf
, item
->GetLabel().c_str());
150 uFlags
|= ( item
->IsEnabled() ? MF_ENABLED
: MF_GRAYED
);
153 uFlags
|= ( item
->IsChecked() ? MF_CHECKED
: MF_UNCHECKED
);
155 if( item
->IsSubMenu() )
158 HMENU hSubMenu
= CreatePopupMenu();
159 wxMenuToHMenu(item
->GetSubMenu(), hSubMenu
);
160 uIDNewItem
= (UINT
) hSubMenu
;
164 uIDNewItem
= item
->GetId();
168 AppendMenu(hMenu
, uFlags
, uIDNewItem
, lpNewItem
);
170 node
= node
->GetNext();
174 void wxTopLevelWindowMSW::ReloadButton(ButtonMenu
& button
, UINT menuID
)
176 TBBUTTONINFO button_info
;
180 memset (&button_info
, 0, sizeof (TBBUTTONINFO
));
181 button_info
.cbSize
= sizeof(TBBUTTONINFO
);
182 button_info
.dwMask
= TBIF_TEXT
| TBIF_STATE
;
183 button_info
.fsState
= TBSTATE_ENABLED
;
184 wxStrcpy(buf
, button
.GetLabel().c_str());
185 button_info
.pszText
= buf
;
186 ::SendMessage(m_MenuBarHWND
, TB_SETBUTTONINFO
, menuID
, (LPARAM
) &button_info
);
190 HMENU hPopupMenu
= (HMENU
) ::SendMessage(m_MenuBarHWND
, SHCMBM_GETSUBMENU
, 0, menuID
);
191 RemoveMenu(hPopupMenu
, 0, MF_BYPOSITION
);
192 wxMenuToHMenu(button
.GetMenu(), hPopupMenu
);
196 void wxTopLevelWindowMSW::ReloadAllButtons()
198 // first reaload only after initialization of both buttons
199 // it should is done at the end of Create() of wxTLW
200 if(!m_LeftButton
.IsAssigned() || !m_RightButton
.IsAssigned())
203 SHMENUBARINFO menu_bar
;
206 memset (&menu_bar
, 0, sizeof (SHMENUBARINFO
));
207 menu_bar
.cbSize
= sizeof (SHMENUBARINFO
);
208 menu_bar
.hwndParent
= (HWND
) GetHWND();
210 if(m_LeftButton
.IsMenu() && m_RightButton
.IsMenu())
211 menu_bar
.nToolBarId
= IDR_MENUBAR_BOTH_MENUS
;
212 else if(m_LeftButton
.IsMenu())
213 menu_bar
.nToolBarId
= IDR_MENUBAR_LEFT_MENU
;
214 else if(m_RightButton
.IsMenu())
215 menu_bar
.nToolBarId
= IDR_MENUBAR_RIGHT_MENU
;
217 menu_bar
.nToolBarId
= IDR_MENUBAR_ONE_BUTTON
;
219 menu_bar
.hInstRes
= wxGetInstance();
221 if (!SHCreateMenuBar(&menu_bar
))
223 wxFAIL_MSG( wxT("SHCreateMenuBar failed") );
227 HWND prev_MenuBar
= m_MenuBarHWND
;
228 m_MenuBarHWND
= menu_bar
.hwndMB
;
230 ReloadButton(m_LeftButton
, IDM_LEFT
);
231 ReloadButton(m_RightButton
, IDM_RIGHT
);
233 // hide previous and show new menubar
235 ::ShowWindow( prev_MenuBar
, SW_HIDE
);
236 ::ShowWindow( m_MenuBarHWND
, SW_SHOW
);
238 // Setup backspace key handling
239 SendMessage(m_MenuBarHWND
, SHCMBM_OVERRIDEKEY
, VK_TBACK
,
240 MAKELPARAM( SHMBOF_NODEFAULT
| SHMBOF_NOTIFY
,
241 SHMBOF_NODEFAULT
| SHMBOF_NOTIFY
));
244 bool wxTopLevelWindowMSW::HandleCommand(WXWORD id
, WXWORD
WXUNUSED(cmd
), WXHWND
WXUNUSED(control
))
246 // handle here commands from Smartphone menu bar
247 if ( id
== IDM_LEFT
|| id
== IDM_RIGHT
)
249 int menuId
= id
== IDM_LEFT
? m_LeftButton
.GetId() : m_RightButton
.GetId() ;
250 wxCommandEvent
commandEvent(wxEVT_MENU
, menuId
);
251 commandEvent
.SetEventObject(this);
252 GetEventHandler()->ProcessEvent(commandEvent
);
258 bool wxTopLevelWindowMSW::MSWShouldPreProcessMessage(WXMSG
* pMsg
)
260 MSG
*msg
= (MSG
*)pMsg
;
262 // Process back key to be like backspace.
263 if (msg
->message
== WM_HOTKEY
)
265 if (HIWORD(msg
->lParam
) == VK_TBACK
)
266 SHSendBackToFocusWindow(msg
->message
, msg
->wParam
, msg
->lParam
);
269 return wxTopLevelWindowBase::MSWShouldPreProcessMessage(pMsg
);
272 #endif // __SMARTPHONE__ && __WXWINCE__