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 .
20 #ifndef INCLUDED_VCL_MENU_HXX
21 #define INCLUDED_VCL_MENU_HXX
23 #include <tools/solar.h>
24 #include <vcl/dllapi.h>
25 #include <tools/rc.hxx>
26 #include <tools/resid.hxx>
27 #include <rsc/rsc-vcl-shared-types.hxx>
28 #include <vcl/bitmapex.hxx>
29 #include <tools/color.hxx>
30 #include <vcl/vclevent.hxx>
31 #include <com/sun/star/uno/Reference.hxx>
45 class MenuFloatingWindow
;
48 struct SystemMenuData
;
53 namespace accessibility
{
57 namespace vcl
{ struct MenuLayoutData
; }
63 #define MENU_APPEND ((sal_uInt16)0xFFFF)
64 #define MENU_ITEM_NOTFOUND ((sal_uInt16)0xFFFF)
66 #define POPUPMENU_EXECUTE_DOWN ((sal_uInt16)0x0001)
67 #define POPUPMENU_EXECUTE_UP ((sal_uInt16)0x0002)
68 #define POPUPMENU_EXECUTE_LEFT ((sal_uInt16)0x0004)
69 #define POPUPMENU_EXECUTE_RIGHT ((sal_uInt16)0x0008)
70 #define POPUPMENU_NOMOUSEUPCLOSE ((sal_uInt16)0x0010)
71 //If there isn't enough space to put the menu where it wants
72 //to go, then they will be autoplaced. Toggle this bit
73 //on to force menus to be placed either above or below
74 //the starting rectangle and shrunk to fit and then scroll rather than place
75 //the menu beside that rectangle
76 #define POPUPMENU_NOHORZ_PLACEMENT ((sal_uInt16)0x0020)
78 #define MENU_FLAG_NOAUTOMNEMONICS 0x0001
79 #define MENU_FLAG_HIDEDISABLEDENTRIES 0x0002
81 // overrides default hiding of disabled entries in popup menus
82 #define MENU_FLAG_ALWAYSSHOWDISABLEDENTRIES 0x0004
84 struct ImplMenuDelData
86 ImplMenuDelData
* mpNext
;
89 explicit ImplMenuDelData( const Menu
* );
92 bool isDeleted() const { return mpMenu
== 0; }
106 class VCL_DLLPUBLIC Menu
: public Resource
108 friend class MenuBar
;
109 friend class MenuBarWindow
;
110 friend class MenuButton
;
111 friend class MenuFloatingWindow
;
112 friend class PopupMenu
;
113 friend class SystemWindow
;
114 friend struct ImplMenuDelData
;
116 ImplMenuDelData
* mpFirstDel
;
117 MenuItemList
* pItemList
; // Liste mit den MenuItems
122 Link aActivateHdl
; // Active-Handler
123 Link aDeactivateHdl
; // Deactivate-Handler
124 Link aHighlightHdl
; // Highlight-Handler
125 Link aSelectHdl
; // Highlight-Handler
127 VclEventListeners maEventListeners
;
128 VclEventListeners maChildEventListeners
;
130 OUString aTitleText
; // PopupMenu text
132 ImplSVEvent
* nEventId
;
133 sal_uInt16 mnHighlightedItemPos
; // for native menus: keeps track of the highlighted item
134 sal_uInt16 nMenuFlags
;
135 sal_uInt16 nDefaultItem
; // Id of default item
136 sal_uInt16 nSelectedId
;
138 sal_uInt16 nHighlightedItem
;
141 sal_uInt16 nImgOrChkPos
;
144 bool bIsMenuBar
: 1, // Is this a menubar?
145 bCanceled
: 1, // Terminated during a callback
146 bInCallback
: 1, // In Activate/Deactivate
147 bKilled
: 1; // Killed...
149 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessible
> mxAccessible
;
150 mutable vcl::MenuLayoutData
* mpLayoutData
;
154 SAL_DLLPRIVATE
void ImplInit();
155 SAL_DLLPRIVATE
void ImplLoadRes( const ResId
& rResId
);
156 SAL_DLLPRIVATE Menu
* ImplGetStartMenu();
157 SAL_DLLPRIVATE Menu
* ImplFindSelectMenu();
158 SAL_DLLPRIVATE Menu
* ImplFindMenu( sal_uInt16 nId
);
159 SAL_DLLPRIVATE Size
ImplCalcSize( const Window
* pWin
);
160 SAL_DLLPRIVATE
bool ImplIsVisible( sal_uInt16 nPos
) const;
161 SAL_DLLPRIVATE
bool ImplIsSelectable( sal_uInt16 nPos
) const;
162 SAL_DLLPRIVATE sal_uInt16
ImplGetVisibleItemCount() const;
163 SAL_DLLPRIVATE sal_uInt16
ImplGetFirstVisible() const;
164 SAL_DLLPRIVATE sal_uInt16
ImplGetPrevVisible( sal_uInt16 nPos
) const;
165 SAL_DLLPRIVATE sal_uInt16
ImplGetNextVisible( sal_uInt16 nPos
) const;
166 SAL_DLLPRIVATE
void ImplPaint( Window
* pWin
, sal_uInt16 nBorder
, long nOffY
= 0, MenuItemData
* pThisDataOnly
= 0, bool bHighlighted
= false, bool bLayout
= false, bool bRollover
= false ) const;
167 SAL_DLLPRIVATE
void ImplSelect();
168 SAL_DLLPRIVATE
void ImplCallHighlight( sal_uInt16 nHighlightItem
);
169 SAL_DLLPRIVATE
void ImplCallEventListeners( sal_uLong nEvent
, sal_uInt16 nPos
);
170 DECL_DLLPRIVATE_LINK( ImplCallSelect
, void* );
172 SAL_DLLPRIVATE
void ImplFillLayoutData() const;
173 SAL_DLLPRIVATE SalMenu
* ImplGetSalMenu() { return mpSalMenu
; }
174 SAL_DLLPRIVATE
void ImplSetSalMenu( SalMenu
*pMenu
);
175 SAL_DLLPRIVATE OUString
ImplGetHelpText( sal_uInt16 nItemId
) const;
177 // returns native check and option menu symbol height in rCheckHeight and rRadioHeight
178 // return value is maximum width and height of checkboxes and radiobuttons
179 SAL_DLLPRIVATE Size
ImplGetNativeCheckAndRadioSize( const Window
*, long& rCheckHeight
, long& rRadioHeight
) const;
181 // returns native submenu arrow size and spacing from right border
182 // return value is whether it's supported natively
183 SAL_DLLPRIVATE
bool ImplGetNativeSubmenuArrowSize( Window
* pWin
, Size
& rArrowSize
, long& rArrowSpacing
) const;
185 SAL_DLLPRIVATE
void ImplAddDel( ImplMenuDelData
&rDel
);
186 SAL_DLLPRIVATE
void ImplRemoveDel( ImplMenuDelData
&rDel
);
188 SAL_DLLPRIVATE
void ImplKillLayoutData() const;
189 SAL_DLLPRIVATE Menu
* ImplGetStartedFrom() const { return pStartedFrom
; }
192 explicit Menu( bool bMenuBar
);
193 SAL_DLLPRIVATE Window
* ImplGetWindow() const { return pWindow
; }
194 void ImplSelectWithStart( Menu
* pStartMenu
= NULL
);
199 virtual void Activate();
200 virtual void Deactivate();
201 virtual void Highlight();
202 virtual void Select();
203 virtual void RequestHelp( const HelpEvent
& rHEvt
);
205 void InsertItem( sal_uInt16 nItemId
, const OUString
& rStr
,
206 MenuItemBits nItemBits
= 0,
207 const OString
&rIdent
= OString(),
208 sal_uInt16 nPos
= MENU_APPEND
);
209 void InsertItem( sal_uInt16 nItemId
, const Image
& rImage
,
210 MenuItemBits nItemBits
= 0,
211 const OString
&rIdent
= OString(),
212 sal_uInt16 nPos
= MENU_APPEND
);
213 void InsertItem( sal_uInt16 nItemId
,
214 const OUString
& rString
, const Image
& rImage
,
215 MenuItemBits nItemBits
= 0,
216 const OString
&rIdent
= OString(),
217 sal_uInt16 nPos
= MENU_APPEND
);
218 void InsertItem( const ResId
& rResId
, sal_uInt16 nPos
= MENU_APPEND
);
219 void InsertSeparator( const OString
&rIdent
= OString(),
220 sal_uInt16 nPos
= MENU_APPEND
);
221 void RemoveItem( sal_uInt16 nPos
);
222 void CopyItem( const Menu
& rMenu
, sal_uInt16 nPos
,
223 sal_uInt16 nNewPos
= MENU_APPEND
);
226 void CreateAutoMnemonics();
228 void SetMenuFlags( sal_uInt16 nFlags
) { nMenuFlags
= nFlags
; }
229 sal_uInt16
GetMenuFlags() const { return nMenuFlags
; }
231 sal_uInt16
GetItemCount() const;
232 sal_uInt16
GetItemId(sal_uInt16 nPos
) const;
233 sal_uInt16
GetItemId(const OString
&rIdent
) const;
234 sal_uInt16
GetItemPos( sal_uInt16 nItemId
) const;
235 OString
GetItemIdent(sal_uInt16 nItemId
) const;
236 MenuItemType
GetItemType( sal_uInt16 nPos
) const;
237 sal_uInt16
GetCurItemId() const;
238 OString
GetCurItemIdent() const;
240 void SetHighlightItem(sal_uInt16 nHighlightedItem
);
242 void SetDefaultItem( sal_uInt16 nItemId
) { nDefaultItem
= nItemId
; }
243 sal_uInt16
GetDefaultItem() const { return nDefaultItem
; }
245 void SetItemBits( sal_uInt16 nItemId
, MenuItemBits nBits
);
246 MenuItemBits
GetItemBits( sal_uInt16 nItemId
) const;
248 void SetUserValue( sal_uInt16 nItemId
, sal_uLong nValue
);
249 sal_uLong
GetUserValue( sal_uInt16 nItemId
) const;
251 void SetPopupMenu( sal_uInt16 nItemId
, PopupMenu
* pMenu
);
252 PopupMenu
* GetPopupMenu( sal_uInt16 nItemId
) const;
254 void SetAccelKey( sal_uInt16 nItemId
, const KeyCode
& rKeyCode
);
255 KeyCode
GetAccelKey( sal_uInt16 nItemId
) const;
257 void CheckItem( sal_uInt16 nItemId
, bool bCheck
= true );
258 bool IsItemChecked( sal_uInt16 nItemId
) const;
260 void SelectItem( sal_uInt16 nItemId
);
261 void DeSelect() { SelectItem( 0xFFFF ); } // MENUITEMPOS_INVALID
263 void EnableItem( sal_uInt16 nItemId
, bool bEnable
= true );
264 void EnableItem(const OString
&rIdent
, bool bEnable
= true)
266 EnableItem(GetItemId(rIdent
), bEnable
);
268 bool IsItemEnabled( sal_uInt16 nItemId
) const;
270 void ShowItem( sal_uInt16 nItemId
, bool bVisible
= true );
271 void HideItem( sal_uInt16 nItemId
) { ShowItem( nItemId
, false ); }
273 bool IsItemPosVisible( sal_uInt16 nItemPos
) const;
274 bool IsMenuVisible() const;
275 bool IsMenuBar() const { return bIsMenuBar
; }
277 void RemoveDisabledEntries( bool bCheckPopups
= true, bool bRemoveEmptyPopups
= false );
278 bool HasValidEntries( bool bCheckPopups
= true );
280 void SetItemText( sal_uInt16 nItemId
, const OUString
& rStr
);
281 OUString
GetItemText( sal_uInt16 nItemId
) const;
283 void SetItemImage( sal_uInt16 nItemId
, const Image
& rImage
);
284 Image
GetItemImage( sal_uInt16 nItemId
) const;
285 void SetItemImageAngle( sal_uInt16 nItemId
, long nAngle10
);
286 void SetItemImageMirrorMode( sal_uInt16 nItemId
, bool bMirror
);
288 void SetItemCommand( sal_uInt16 nItemId
, const OUString
& rCommand
);
289 OUString
GetItemCommand( sal_uInt16 nItemId
) const;
291 void SetHelpText( sal_uInt16 nItemId
, const OUString
& rString
);
292 OUString
GetHelpText( sal_uInt16 nItemId
) const;
294 void SetTipHelpText( sal_uInt16 nItemId
, const OUString
& rString
);
295 OUString
GetTipHelpText( sal_uInt16 nItemId
) const;
297 void SetHelpCommand( sal_uInt16 nItemId
, const OUString
& rString
);
298 OUString
GetHelpCommand( sal_uInt16 nItemId
) const;
300 void SetHelpId( sal_uInt16 nItemId
, const OString
& rHelpId
);
301 OString
GetHelpId( sal_uInt16 nItemId
) const;
303 void SetActivateHdl( const Link
& rLink
) { aActivateHdl
= rLink
; }
304 const Link
& GetActivateHdl() const { return aActivateHdl
; }
306 void SetDeactivateHdl( const Link
& rLink
) { aDeactivateHdl
= rLink
; }
307 const Link
& GetDeactivateHdl() const { return aDeactivateHdl
; }
309 void SetHighlightHdl( const Link
& rLink
) { aHighlightHdl
= rLink
; }
310 const Link
& GetHighlightHdl() const { return aHighlightHdl
; }
312 void SetSelectHdl( const Link
& rLink
) { aSelectHdl
= rLink
; }
313 const Link
& GetSelectHdl() const { return aSelectHdl
; }
315 bool HasLogo() const { return pLogo
? true : false; }
317 void AddEventListener( const Link
& rEventListener
);
318 void RemoveEventListener( const Link
& rEventListener
);
319 void AddChildEventListener( const Link
& rEventListener
);
320 void RemoveChildEventListener( const Link
& rEventListener
);
322 Menu
& operator =( const Menu
& rMenu
);
324 // Fuer Menu-'Funktionen'
325 MenuItemList
* GetItemList() const { return pItemList
; }
327 // returns the system's menu handle if native menus are supported
328 // pData must point to a SystemMenuData structure
329 bool GetSystemMenuData( SystemMenuData
* pData
) const;
331 // accessibility helpers
333 // returns the bounding box for the character at index nIndex
334 // where nIndex is relative to the starting index of the item
335 // with id nItemId (in coordinates of the displaying window)
336 Rectangle
GetCharacterBounds( sal_uInt16 nItemId
, long nIndex
) const;
337 // -1 is returned if no character is at that point
338 // if an index is found the corresponding item id is filled in (else 0)
339 long GetIndexForPoint( const Point
& rPoint
, sal_uInt16
& rItemID
) const;
340 // returns the bounding rectangle for an item at pos nItemPos
341 Rectangle
GetBoundingRectangle( sal_uInt16 nItemPos
) const;
343 ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessible
> GetAccessible();
344 void SetAccessible( const ::com::sun::star::uno::Reference
< ::com::sun::star::accessibility::XAccessible
>& rxAccessible
);
346 // gets the activation key of the specified item
347 KeyEvent
GetActivationKey( sal_uInt16 nItemId
) const;
349 Window
* GetWindow() const { return pWindow
; }
351 void SetAccessibleName( sal_uInt16 nItemId
, const OUString
& rStr
);
352 OUString
GetAccessibleName( sal_uInt16 nItemId
) const;
354 // returns whether the item a position nItemPos is highlighted or not.
355 bool IsHighlighted( sal_uInt16 nItemPos
) const;
357 void HighlightItem( sal_uInt16 nItemPos
);
358 void DeHighlight() { HighlightItem( 0xFFFF ); } // MENUITEMPOS_INVALID
365 class VCL_DLLPUBLIC MenuBar
: public Menu
370 bool mbCloserVisible
;
371 bool mbFloatBtnVisible
;
372 bool mbHideBtnVisible
;
375 friend class Application
;
377 friend class MenuBarWindow
;
378 friend class MenuFloatingWindow
;
379 friend class SystemWindow
;
381 SAL_DLLPRIVATE
static Window
* ImplCreate( Window
* pParent
, Window
* pWindow
, MenuBar
* pMenu
);
382 SAL_DLLPRIVATE
static void ImplDestroy( MenuBar
* pMenu
, bool bDelete
);
383 SAL_DLLPRIVATE
bool ImplHandleKeyEvent( const KeyEvent
& rKEvent
, bool bFromMenu
= true );
387 MenuBar( const MenuBar
& rMenu
);
390 MenuBar
& operator =( const MenuBar
& rMenu
);
392 void ShowCloser( bool bShow
= true );
393 bool HasCloser() const { return mbCloserVisible
; }
394 bool HasFloatButton() const { return mbFloatBtnVisible
; }
395 bool HasHideButton() const { return mbHideBtnVisible
; }
396 void ShowButtons( bool bClose
, bool bFloat
, bool bHide
);
398 void SelectEntry( sal_uInt16 nId
);
399 bool HandleMenuActivateEvent( Menu
*pMenu
) const;
400 bool HandleMenuDeActivateEvent( Menu
*pMenu
) const;
401 bool HandleMenuHighlightEvent( Menu
*pMenu
, sal_uInt16 nEventId
) const;
402 bool HandleMenuCommandEvent( Menu
*pMenu
, sal_uInt16 nEventId
) const;
403 bool HandleMenuButtonEvent( Menu
*pMenu
, sal_uInt16 nEventId
) const;
405 void SetCloserHdl( const Link
& rLink
) { maCloserHdl
= rLink
; }
406 const Link
& GetCloserHdl() const { return maCloserHdl
; }
407 void SetFloatButtonClickHdl( const Link
& rLink
) { maFloatHdl
= rLink
; }
408 const Link
& GetFloatButtonClickHdl() const { return maFloatHdl
; }
409 void SetHideButtonClickHdl( const Link
& rLink
) { maHideHdl
= rLink
; }
410 const Link
& GetHideButtonClickHdl() const { return maHideHdl
; }
412 // - by default a menubar is displayable
413 // - if a menubar is not displayable, its MenuBarWindow will never be shown
414 // and it will be hidden if it was visible before
415 // - note: if a menubar is diplayable, this does not necessarily mean that it is currently visible
416 void SetDisplayable( bool bDisplayable
);
417 bool IsDisplayable() const { return mbDisplayable
; }
419 struct MenuBarButtonCallbackArg
421 sal_uInt16 nId
; // Id of the button
422 bool bHighlight
; // highlight on/off
423 MenuBar
* pMenuBar
; // menubar the button belongs to
425 // add an arbitrary button to the menubar (will appear next to closer)
426 // passed link will be call with a MenuBarButtonCallbackArg on press
427 // passed string will be set as tooltip
428 sal_uInt16
AddMenuBarButton( const Image
&, const Link
&, const OUString
&, sal_uInt16 nPos
= 0 );
429 // set the highlight link for additional button with ID nId
430 // highlight link will be called with a MenuBarButtonHighlightArg
431 // the bHighlight member of that struct shall contain the new state
432 void SetMenuBarButtonHighlightHdl( sal_uInt16 nId
, const Link
& );
433 // returns the rectangle occupied by the additional button named nId
434 // coordinates are relative to the systemwindiow the menubar is attached to
435 // if the menubar is unattached an empty rectangle is returned
436 Rectangle
GetMenuBarButtonRectPixel( sal_uInt16 nId
);
437 void RemoveMenuBarButton( sal_uInt16 nId
);
440 inline MenuBar
& MenuBar::operator =( const MenuBar
& rMenu
)
442 Menu::operator =( rMenu
);
451 class VCL_DLLPUBLIC PopupMenu
: public Menu
454 friend class MenuFloatingWindow
;
455 friend class MenuBarWindow
;
456 friend struct MenuItemData
;
459 Menu
** pRefAutoSubMenu
; // keeps track if a pointer to this Menu is stored in the MenuItemData
461 SAL_DLLPRIVATE MenuFloatingWindow
* ImplGetFloatingWindow() const { return (MenuFloatingWindow
*)Menu::ImplGetWindow(); }
464 SAL_DLLPRIVATE sal_uInt16
ImplExecute( Window
* pWindow
, const Rectangle
& rRect
, sal_uLong nPopupFlags
, Menu
* pStaredFrom
, bool bPreSelectFirst
);
465 SAL_DLLPRIVATE
long ImplCalcHeight( sal_uInt16 nEntries
) const;
466 SAL_DLLPRIVATE sal_uInt16
ImplCalcVisEntries( long nMaxHeight
, sal_uInt16 nStartEntry
= 0, sal_uInt16
* pLastVisible
= NULL
) const;
470 PopupMenu( const PopupMenu
& rMenu
);
471 explicit PopupMenu( const ResId
& );
472 virtual ~PopupMenu();
474 void SetText( const OUString
& rTitle
) { aTitleText
= rTitle
; }
475 const OUString
& GetText() const { return aTitleText
; }
477 sal_uInt16
Execute( Window
* pWindow
, const Point
& rPopupPos
);
478 sal_uInt16
Execute( Window
* pWindow
, const Rectangle
& rRect
, sal_uInt16 nFlags
= 0 );
481 void EndExecute( sal_uInt16 nSelect
= 0 );
482 void SelectEntry( sal_uInt16 nId
);
483 void SetSelectedEntry( sal_uInt16 nId
); // for use by native submenu only
485 static bool IsInExecute();
486 static PopupMenu
* GetActivePopupMenu();
488 PopupMenu
& operator =( const PopupMenu
& rMenu
);
491 inline PopupMenu
& PopupMenu::operator =( const PopupMenu
& rMenu
)
493 Menu::operator =( rMenu
);
497 #endif // INCLUDED_VCL_MENU_HXX
499 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */