1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <vcl/menu.hxx>
23 #include <vcl/sysdata.hxx>
24 #include <o3tl/char16_t2wchar_t.hxx>
26 #include <win/wincomp.hxx>
27 #include <win/saldata.hxx>
28 #include <win/salinst.h>
29 #include <win/salframe.h>
30 #include <win/salmenu.h>
36 bool SalData::IsKnownMenuHandle( HMENU hMenu
)
38 if( mhMenuSet
.find( hMenu
) == mhMenuSet
.end() )
44 // WinSalInst factory methods
46 std::unique_ptr
<SalMenu
> WinSalInstance::CreateMenu( bool bMenuBar
, Menu
* )
48 WinSalMenu
*pSalMenu
= new WinSalMenu();
50 pSalMenu
->mbMenuBar
= bMenuBar
;
51 pSalMenu
->mhWnd
= nullptr;
53 pSalMenu
->mhMenu
= ::CreateMenu();
55 pSalMenu
->mhMenu
= ::CreatePopupMenu();
57 if( pSalMenu
->mhMenu
)
58 GetSalData()->mhMenuSet
.insert( pSalMenu
->mhMenu
);
60 return std::unique_ptr
<SalMenu
>(pSalMenu
);
63 std::unique_ptr
<SalMenuItem
> WinSalInstance::CreateMenuItem( const SalItemParams
& rItemData
)
65 WinSalMenuItem
*pSalMenuItem
= new WinSalMenuItem();
66 memset( &pSalMenuItem
->mInfo
, 0, sizeof( MENUITEMINFOW
) );
67 pSalMenuItem
->mInfo
.cbSize
= sizeof( MENUITEMINFOW
);
69 if( rItemData
.eType
== MenuItemType::SEPARATOR
)
72 pSalMenuItem
->mInfo
.fMask
= MIIM_TYPE
;
73 pSalMenuItem
->mInfo
.fType
= MFT_SEPARATOR
;
78 pSalMenuItem
->mText
= rItemData
.aText
;
79 pSalMenuItem
->mpMenu
= rItemData
.pMenu
;
80 pSalMenuItem
->maBitmap
= !!rItemData
.aImage
? rItemData
.aImage
.GetBitmapEx().GetBitmap() : Bitmap();
81 pSalMenuItem
->mnId
= rItemData
.nId
;
83 // 'translate' mnemonics
84 pSalMenuItem
->mText
= pSalMenuItem
->mText
.replaceAll( "~", "&" );
86 pSalMenuItem
->mInfo
.fMask
= MIIM_TYPE
| MIIM_STATE
| MIIM_ID
| MIIM_DATA
;
87 pSalMenuItem
->mInfo
.fType
= MFT_STRING
;
88 pSalMenuItem
->mInfo
.dwTypeData
= o3tl::toW(const_cast<sal_Unicode
*>(pSalMenuItem
->mText
.getStr()));
89 pSalMenuItem
->mInfo
.cch
= pSalMenuItem
->mText
.getLength();
91 pSalMenuItem
->mInfo
.wID
= rItemData
.nId
;
92 pSalMenuItem
->mInfo
.dwItemData
= reinterpret_cast<ULONG_PTR
>(pSalMenuItem
); // user data
95 return std::unique_ptr
<SalMenuItem
>(pSalMenuItem
);
98 static void ImplDrawMenuBar( SalMenu
*pMenu
)
100 if( pMenu
->VisibleMenuBar() )
102 // redrawing the menubar all the time actually seems to be unnecessary (it just flickers)
104 WinSalMenu *pMenuBar = ImplFindMenuBar( pMenu );
105 if( pMenuBar && pMenuBar->mhWnd )
106 ::DrawMenuBar( pMenuBar->mhWnd );
115 WinSalMenu::WinSalMenu()
120 mpParentMenu
= nullptr;
123 WinSalMenu::~WinSalMenu()
125 // only required if not associated to a window...
126 GetSalData()->mhMenuSet
.erase( mhMenu
);
127 ::DestroyMenu( mhMenu
);
130 bool WinSalMenu::VisibleMenuBar()
132 // The Win32 implementation never shows a native
133 // menubar. Thus, native menus are only visible
134 // when the menu is merged with an OLE container.
135 // The reason are missing tooltips, ownerdraw
136 // issues and accessibility which are better supported
138 // Nevertheless, the native menus are always created
139 // and the application will properly react to all native
145 void WinSalMenu::SetFrame( const SalFrame
*pFrame
)
148 mhWnd
= static_cast<const WinSalFrame
*>(pFrame
)->mhWnd
;
153 void WinSalMenu::InsertItem( SalMenuItem
* pSalMenuItem
, unsigned nPos
)
157 WinSalMenuItem
* pWItem
= static_cast<WinSalMenuItem
*>(pSalMenuItem
);
158 if( nPos
== MENU_APPEND
)
160 nPos
= ::GetMenuItemCount( mhMenu
);
161 if( nPos
== static_cast<unsigned>( -1 ) )
165 if(!::InsertMenuItemW( mhMenu
, nPos
, TRUE
, &pWItem
->mInfo
))
166 myerr
= GetLastError();
169 pWItem
->mpSalMenu
= this;
170 ImplDrawMenuBar( this );
175 void WinSalMenu::RemoveItem( unsigned nPos
)
177 int num
= ::GetMenuItemCount( mhMenu
);
178 if( num
!= -1 && nPos
< static_cast<unsigned>(num
) )
180 WinSalMenuItem
*pSalMenuItem
= nullptr;
182 MENUITEMINFOW mi
= {};
183 mi
.cbSize
= sizeof( mi
);
184 mi
.fMask
= MIIM_DATA
;
185 if( !GetMenuItemInfoW( mhMenu
, nPos
, TRUE
, &mi
) )
186 myerr
= GetLastError();
188 pSalMenuItem
= reinterpret_cast<WinSalMenuItem
*>(mi
.dwItemData
);
190 if( !::RemoveMenu( mhMenu
, nPos
, MF_BYPOSITION
) )
191 myerr
= GetLastError();
195 pSalMenuItem
->mpSalMenu
= nullptr;
196 ImplDrawMenuBar( this );
201 static void ImplRemoveItemById( WinSalMenu
*pSalMenu
, unsigned nItemId
)
206 WinSalMenuItem
*pSalMenuItem
= nullptr;
208 MENUITEMINFOW mi
= {};
209 mi
.cbSize
= sizeof( mi
);
210 mi
.fMask
= MIIM_DATA
;
211 if( !GetMenuItemInfoW( pSalMenu
->mhMenu
, nItemId
, FALSE
, &mi
) )
212 myerr
= GetLastError();
214 pSalMenuItem
= reinterpret_cast<WinSalMenuItem
*>(mi
.dwItemData
);
216 if( !::RemoveMenu( pSalMenu
->mhMenu
, nItemId
, MF_BYCOMMAND
) )
217 myerr
= GetLastError();
221 pSalMenuItem
->mpSalMenu
= nullptr;
222 ImplDrawMenuBar( pSalMenu
);
226 void WinSalMenu::SetSubMenu( SalMenuItem
* pSalMenuItem
, SalMenu
* pSubMenu
, unsigned nPos
)
230 WinSalMenuItem
* pWMenuItem
= static_cast<WinSalMenuItem
*>(pSalMenuItem
);
231 WinSalMenu
* pWSubMenu
= static_cast<WinSalMenu
*>(pSubMenu
);
232 if( pWMenuItem
->mInfo
.hSubMenu
)
234 GetSalData()->mhMenuSet
.erase( pWMenuItem
->mInfo
.hSubMenu
);
235 ::DestroyMenu( pWMenuItem
->mInfo
.hSubMenu
);
238 pWMenuItem
->mInfo
.fMask
|= MIIM_SUBMENU
;
240 pWMenuItem
->mInfo
.hSubMenu
= nullptr;
243 pWMenuItem
->mInfo
.hSubMenu
= pWSubMenu
->mhMenu
;
244 pWSubMenu
->mpParentMenu
= this;
247 if(!::SetMenuItemInfoW( mhMenu
, nPos
, TRUE
, &pWMenuItem
->mInfo
) )
248 myerr
= GetLastError();
250 ImplDrawMenuBar( this );
254 void WinSalMenu::CheckItem( unsigned nPos
, bool bCheck
)
256 if( static_cast<unsigned>( -1 ) != ::CheckMenuItem( mhMenu
, nPos
, MF_BYPOSITION
|(bCheck
? MF_CHECKED
: MF_UNCHECKED
) ) )
257 ImplDrawMenuBar( this );
260 void WinSalMenu::EnableItem( unsigned nPos
, bool bEnable
)
262 if( -1 != ::EnableMenuItem( mhMenu
, nPos
, MF_BYPOSITION
|(bEnable
? MF_ENABLED
: (MF_DISABLED
|MF_GRAYED
) ) ) )
263 ImplDrawMenuBar( this );
266 void WinSalMenu::SetItemImage( unsigned /*nPos*/, SalMenuItem
* pSalMenuItem
, const Image
& rImage
)
270 WinSalMenuItem
* pWItem
= static_cast<WinSalMenuItem
*>(pSalMenuItem
);
272 pWItem
->maBitmap
= rImage
.GetBitmapEx().GetBitmap();
274 pWItem
->maBitmap
= Bitmap();
278 void WinSalMenu::SetItemText( unsigned nPos
, SalMenuItem
* pSalMenuItem
, const OUString
& rText
)
282 WinSalMenuItem
* pWItem
= static_cast<WinSalMenuItem
*>(pSalMenuItem
);
283 pWItem
->mText
= rText
;
284 // 'translate' mnemonics
285 pWItem
->mText
= pWItem
->mText
.replaceAll( "~", "&" );
286 pWItem
->mInfo
.fMask
= MIIM_TYPE
| MIIM_DATA
;
287 pWItem
->mInfo
.fType
= MFT_STRING
;
289 // combine text and accelerator text
290 OUString
aStr( pWItem
->mText
);
291 if( pWItem
->mAccelText
.getLength() )
293 aStr
+= "\t" + pWItem
->mAccelText
;
295 pWItem
->mInfo
.dwTypeData
= o3tl::toW(const_cast<sal_Unicode
*>(aStr
.getStr()));
296 pWItem
->mInfo
.cch
= aStr
.getLength();
298 if(!::SetMenuItemInfoW( mhMenu
, nPos
, TRUE
, &pWItem
->mInfo
))
299 myerr
= GetLastError();
301 ImplDrawMenuBar( this );
305 void WinSalMenu::SetAccelerator( unsigned nPos
, SalMenuItem
* pSalMenuItem
, const vcl::KeyCode
&, const OUString
& rKeyName
)
309 WinSalMenuItem
* pWItem
= static_cast<WinSalMenuItem
*>(pSalMenuItem
);
310 pWItem
->mAccelText
= rKeyName
;
311 pWItem
->mInfo
.fMask
= MIIM_TYPE
| MIIM_DATA
;
312 pWItem
->mInfo
.fType
= MFT_STRING
;
314 // combine text and accelerator text
315 OUString
aStr( pWItem
->mText
);
316 if( pWItem
->mAccelText
.getLength() )
318 aStr
+= "\t" + pWItem
->mAccelText
;
320 pWItem
->mInfo
.dwTypeData
= o3tl::toW(const_cast<sal_Unicode
*>(aStr
.getStr()));
321 pWItem
->mInfo
.cch
= aStr
.getLength();
323 if(!::SetMenuItemInfoW( mhMenu
, nPos
, TRUE
, &pWItem
->mInfo
))
324 myerr
= GetLastError();
326 ImplDrawMenuBar( this );
330 void WinSalMenu::GetSystemMenuData( SystemMenuData
* pData
)
333 pData
->hMenu
= mhMenu
;
340 WinSalMenuItem::WinSalMenuItem()
342 memset( &mInfo
, 0, sizeof( MENUITEMINFOW
) );
348 WinSalMenuItem::~WinSalMenuItem()
351 ImplRemoveItemById( mpSalMenu
, mnId
);
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */