2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "sfx2/sidebar/SidebarToolBox.hxx"
20 #include "ToolBoxBackground.hxx"
21 #include "sfx2/sidebar/ControllerFactory.hxx"
22 #include "sfx2/sidebar/Theme.hxx"
23 #include "sfx2/sidebar/Tools.hxx"
25 #include <vcl/gradient.hxx>
26 #include <toolkit/helper/vclunohelper.hxx>
27 #include <svtools/miscopt.hxx>
28 #include <framework/imageproducer.hxx>
29 #include <com/sun/star/frame/XSubToolbarController.hpp>
32 using namespace ::com::sun::star
;
33 using namespace ::com::sun::star::uno
;
34 using ::rtl::OUString
;
37 namespace sfx2
{ namespace sidebar
{
40 SidebarToolBox::SidebarToolBox (
41 Window
* pParentWindow
,
43 const cssu::Reference
<css::frame::XFrame
>& rxFrame
)
44 : ToolBox(pParentWindow
, rResId
),
45 mbParentIsBorder(false),
46 maItemSeparator(Theme::GetImage(Theme::Image_ToolBoxItemSeparator
)),
48 mbAreHandlersRegistered(false)
50 SetBackground(Wallpaper());
51 SetPaintTransparent(true);
52 SetToolboxButtonSize( TOOLBOX_BUTTONSIZE_SMALL
);
56 const sal_uInt16
nItemCount (GetItemCount());
59 // When there is only one item then make that as wide as
61 CreateController(GetItemId(0), rxFrame
, GetSizePixel().Width());
64 for (sal_uInt16 nItemIndex
=0; nItemIndex
<nItemCount
; ++nItemIndex
)
65 CreateController(GetItemId(nItemIndex
), rxFrame
, 0);
68 SetSizePixel(CalcWindowSizePixel());
74 SetText(A2S("SidebarToolBox"));
81 SidebarToolBox::SidebarToolBox (Window
* pParentWindow
)
82 : ToolBox(pParentWindow
, 0),
83 mbParentIsBorder(false),
84 maItemSeparator(Theme::GetImage(Theme::Image_ToolBoxItemSeparator
)),
86 mbAreHandlersRegistered(false)
88 SetBackground(Wallpaper());
89 SetPaintTransparent(true);
90 SetToolboxButtonSize( TOOLBOX_BUTTONSIZE_SMALL
);
93 SetText(A2S("SidebarToolBox"));
97 extern "C" SAL_DLLPUBLIC_EXPORT Window
* SAL_CALL
makeSidebarToolBox(Window
*pParent
)
99 return new SidebarToolBox(pParent
);
102 SidebarToolBox::~SidebarToolBox (void)
104 ControllerContainer aControllers
;
105 aControllers
.swap(maControllers
);
106 for (ControllerContainer::iterator
iController(aControllers
.begin()), iEnd(aControllers
.end());
110 Reference
<lang::XComponent
> xComponent (iController
->second
.mxController
, UNO_QUERY
);
112 xComponent
->dispose();
115 if (mbAreHandlersRegistered
)
117 SetDropdownClickHdl(Link());
119 SetDoubleClickHdl(Link());
120 SetSelectHdl(Link());
121 SetActivateHdl(Link());
122 SetDeactivateHdl(Link());
126 void SidebarToolBox::InsertItem(const OUString
& rCommand
,
127 const com::sun::star::uno::Reference
<com::sun::star::frame::XFrame
>& rFrame
,
128 ToolBoxItemBits nBits
, sal_uInt16 nPos
)
130 ToolBox::InsertItem(rCommand
, rFrame
, nBits
, nPos
);
132 CreateController(GetItemId(rCommand
), rFrame
, 0);
136 void SidebarToolBox::SetBorderWindow (const Window
* pBorderWindow
)
138 if (pBorderWindow
!= GetParent())
140 OSL_ASSERT("SetBorderWindow can only handle parent as border window");
144 if ( ! mbParentIsBorder
)
146 mbParentIsBorder
= true;
151 GetSizePixel().Width(),
152 GetSizePixel().Height(),
160 void SidebarToolBox::Paint (const Rectangle
& rRect
)
162 ToolBox::Paint(rRect
);
164 if (Theme::GetBoolean(Theme::Bool_UseToolBoxItemSeparator
))
166 const sal_Int32
nSeparatorY ((GetSizePixel().Height() - maItemSeparator
.GetSizePixel().Height())/2);
167 const sal_uInt16
nItemCount (GetItemCount());
169 for (sal_uInt16 nIndex
=0; nIndex
<nItemCount
; ++nIndex
)
171 const Rectangle
aItemBoundingBox (GetItemPosRect(nIndex
));
174 const int nSeparatorX ((nLastRight
+ aItemBoundingBox
.Left() - 1) / 2);
175 DrawImage(Point(nSeparatorX
,nSeparatorY
), maItemSeparator
);
178 nLastRight
= aItemBoundingBox
.Right();
186 Point
SidebarToolBox::GetPosPixel (void) const
188 if (mbParentIsBorder
)
190 const Point
aParentPoint (GetParent()->GetPosPixel());
191 const Point
aChildPoint (ToolBox::GetPosPixel());
193 aParentPoint
.X() + aChildPoint
.X(),
194 aParentPoint
.Y() + aChildPoint
.Y());
197 return ToolBox::GetPosPixel();
203 void SidebarToolBox::setPosSizePixel (
210 if (mbParentIsBorder
)
212 const Point
aRelativePosition (static_cast<ToolBoxBackground
*>(GetParent())->SetToolBoxChild(
219 ToolBox::setPosSizePixel(
220 aRelativePosition
.X(),
221 aRelativePosition
.Y(),
227 ToolBox::setPosSizePixel(nX
, nY
, nWidth
, nHeight
, nFlags
);
233 long SidebarToolBox::Notify (NotifyEvent
& rEvent
)
235 if (rEvent
.GetType() == EVENT_KEYINPUT
)
237 if (rEvent
.GetKeyEvent()->GetKeyCode().GetCode() == KEY_TAB
)
239 // Special handling for transferring handling of KEY_TAB
240 // that becomes necessary because of our parent that is
241 // not the dialog but a background control.
242 return DockingWindow::Notify(rEvent
);
245 return ToolBox::Notify(rEvent
);
251 void SidebarToolBox::CreateController (
252 const sal_uInt16 nItemId
,
253 const cssu::Reference
<css::frame::XFrame
>& rxFrame
,
254 const sal_Int32 nItemWidth
)
256 ItemDescriptor aDescriptor
;
258 const OUString
sCommandName (GetItemCommand(nItemId
));
260 aDescriptor
.mxController
= sfx2::sidebar::ControllerFactory::CreateToolBoxController(
265 VCLUnoHelper::GetInterface(this),
267 if (aDescriptor
.mxController
.is())
269 aDescriptor
.maURL
= sfx2::sidebar::Tools::GetURL(sCommandName
);
270 aDescriptor
.msCurrentCommand
= sCommandName
;
272 maControllers
.insert(::std::make_pair(nItemId
, aDescriptor
));
279 Reference
<frame::XToolbarController
> SidebarToolBox::GetControllerForItemId (const sal_uInt16 nItemId
) const
281 ControllerContainer::const_iterator
iController (maControllers
.find(nItemId
));
282 if (iController
!= maControllers
.end())
283 return iController
->second
.mxController
;
291 void SidebarToolBox::SetController(
292 const sal_uInt16 nItemId
,
293 const cssu::Reference
<css::frame::XToolbarController
>& rxController
,
294 const ::rtl::OUString
& rsCommandName
)
296 ItemDescriptor aDescriptor
;
297 aDescriptor
.mxController
= rxController
;
298 aDescriptor
.maURL
= sfx2::sidebar::Tools::GetURL(rsCommandName
);
299 aDescriptor
.msCurrentCommand
= rsCommandName
;
301 ControllerContainer::iterator
iController (maControllers
.find(nItemId
));
302 if (iController
!= maControllers
.end())
304 Reference
<lang::XComponent
> xComponent (iController
->second
.mxController
, UNO_QUERY
);
306 xComponent
->dispose();
308 iController
->second
= aDescriptor
;
312 maControllers
[nItemId
] = aDescriptor
;
315 if (rxController
.is())
322 void SidebarToolBox::UpdateIcons (const Reference
<frame::XFrame
>& rxFrame
)
324 for (ControllerContainer::iterator
iController(maControllers
.begin()), iEnd(maControllers
.end());
328 const ::rtl::OUString
sCommandURL (iController
->second
.msCurrentCommand
);
329 Image
aImage (framework::GetImageFromURL(rxFrame
, sCommandURL
, false));
330 SetItemImage(iController
->first
, aImage
);
337 sal_uInt16
SidebarToolBox::GetItemIdForSubToolbarName (const OUString
& rsSubToolbarName
) const
339 for (ControllerContainer::const_iterator
iController(maControllers
.begin()), iEnd(maControllers
.end());
343 Reference
<frame::XToolbarController
> xController (iController
->second
.mxController
);
344 Reference
<frame::XSubToolbarController
> xSubToolbarController (xController
, UNO_QUERY
);
345 if (xSubToolbarController
.is())
347 const OUString
sName (xSubToolbarController
->getSubToolbarName());
348 if (sName
.equals(rsSubToolbarName
))
349 return iController
->first
;
358 void SidebarToolBox::RegisterHandlers (void)
360 if ( ! mbAreHandlersRegistered
)
362 mbAreHandlersRegistered
= true;
363 SetDropdownClickHdl(LINK(this, SidebarToolBox
, DropDownClickHandler
));
364 SetClickHdl(LINK(this, SidebarToolBox
, ClickHandler
));
365 SetDoubleClickHdl(LINK(this, SidebarToolBox
, DoubleClickHandler
));
366 SetSelectHdl(LINK(this, SidebarToolBox
, SelectHandler
));
367 SetActivateHdl(LINK(this, SidebarToolBox
, ActivateToolBox
));
368 SetDeactivateHdl(LINK(this, SidebarToolBox
, DeactivateToolBox
));
375 IMPL_LINK(SidebarToolBox
, DropDownClickHandler
, ToolBox
*, pToolBox
)
377 if (pToolBox
!= NULL
)
379 Reference
<frame::XToolbarController
> xController (GetControllerForItemId(pToolBox
->GetCurItemId()));
380 if (xController
.is())
382 Reference
<awt::XWindow
> xWindow
= xController
->createPopupWindow();
393 IMPL_LINK(SidebarToolBox
, ClickHandler
, ToolBox
*, pToolBox
)
395 if (pToolBox
== NULL
)
398 Reference
<frame::XToolbarController
> xController (GetControllerForItemId(pToolBox
->GetCurItemId()));
399 if (xController
.is())
400 xController
->click();
408 IMPL_LINK(SidebarToolBox
, DoubleClickHandler
, ToolBox
*, pToolBox
)
410 if (pToolBox
== NULL
)
413 Reference
<frame::XToolbarController
> xController (GetControllerForItemId(pToolBox
->GetCurItemId()));
414 if (xController
.is())
415 xController
->doubleClick();
423 IMPL_LINK(SidebarToolBox
, SelectHandler
, ToolBox
*, pToolBox
)
425 if (pToolBox
== NULL
)
428 Reference
<frame::XToolbarController
> xController (GetControllerForItemId(pToolBox
->GetCurItemId()));
429 if (xController
.is())
430 xController
->execute((sal_Int16
)pToolBox
->GetModifier());
438 IMPL_LINK(SidebarToolBox
, ActivateToolBox
, ToolBox
*, EMPTYARG
)
446 IMPL_LINK(SidebarToolBox
, DeactivateToolBox
, ToolBox
*, EMPTYARG
)
453 } } // end of namespace sfx2::sidebar