bump product version to 4.1.6.2
[LibreOffice.git] / sfx2 / source / sidebar / SidebarToolBox.cxx
blob5039a1a675b03fd11a40e92867c13af67205eb08
1 /*
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,
42 const ResId& rResId,
43 const cssu::Reference<css::frame::XFrame>& rxFrame)
44 : ToolBox(pParentWindow, rResId),
45 mbParentIsBorder(false),
46 maItemSeparator(Theme::GetImage(Theme::Image_ToolBoxItemSeparator)),
47 maControllers(),
48 mbAreHandlersRegistered(false)
50 SetBackground(Wallpaper());
51 SetPaintTransparent(true);
52 SetToolboxButtonSize( TOOLBOX_BUTTONSIZE_SMALL );
54 if (rxFrame.is())
56 const sal_uInt16 nItemCount (GetItemCount());
57 if (nItemCount == 1)
59 // When there is only one item then make that as wide as
60 // the tool box.
61 CreateController(GetItemId(0), rxFrame, GetSizePixel().Width());
63 else
64 for (sal_uInt16 nItemIndex=0; nItemIndex<nItemCount; ++nItemIndex)
65 CreateController(GetItemId(nItemIndex), rxFrame, 0);
66 UpdateIcons(rxFrame);
68 SetSizePixel(CalcWindowSizePixel());
70 RegisterHandlers();
73 #ifdef DEBUG
74 SetText(A2S("SidebarToolBox"));
75 #endif
81 SidebarToolBox::SidebarToolBox (Window* pParentWindow)
82 : ToolBox(pParentWindow, 0),
83 mbParentIsBorder(false),
84 maItemSeparator(Theme::GetImage(Theme::Image_ToolBoxItemSeparator)),
85 maControllers(),
86 mbAreHandlersRegistered(false)
88 SetBackground(Wallpaper());
89 SetPaintTransparent(true);
90 SetToolboxButtonSize( TOOLBOX_BUTTONSIZE_SMALL );
92 #ifdef DEBUG
93 SetText(A2S("SidebarToolBox"));
94 #endif
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());
107 iController!=iEnd;
108 ++iController)
110 Reference<lang::XComponent> xComponent (iController->second.mxController, UNO_QUERY);
111 if (xComponent.is())
112 xComponent->dispose();
115 if (mbAreHandlersRegistered)
117 SetDropdownClickHdl(Link());
118 SetClickHdl(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);
133 RegisterHandlers();
136 void SidebarToolBox::SetBorderWindow (const Window* pBorderWindow)
138 if (pBorderWindow != GetParent())
140 OSL_ASSERT("SetBorderWindow can only handle parent as border window");
141 return;
144 if ( ! mbParentIsBorder)
146 mbParentIsBorder = true;
148 setPosSizePixel (
149 GetPosPixel().X(),
150 GetPosPixel().Y(),
151 GetSizePixel().Width(),
152 GetSizePixel().Height(),
153 WINDOW_POSSIZE_ALL);
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());
168 int nLastRight (-1);
169 for (sal_uInt16 nIndex=0; nIndex<nItemCount; ++nIndex)
171 const Rectangle aItemBoundingBox (GetItemPosRect(nIndex));
172 if (nLastRight >= 0)
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());
192 return Point(
193 aParentPoint.X() + aChildPoint.X(),
194 aParentPoint.Y() + aChildPoint.Y());
196 else
197 return ToolBox::GetPosPixel();
203 void SidebarToolBox::setPosSizePixel (
204 long nX,
205 long nY,
206 long nWidth,
207 long nHeight,
208 sal_uInt16 nFlags)
210 if (mbParentIsBorder)
212 const Point aRelativePosition (static_cast<ToolBoxBackground*>(GetParent())->SetToolBoxChild(
213 this,
216 nWidth,
217 nHeight,
218 nFlags));
219 ToolBox::setPosSizePixel(
220 aRelativePosition.X(),
221 aRelativePosition.Y(),
222 nWidth,
223 nHeight,
224 nFlags);
226 else
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(
261 this,
262 nItemId,
263 sCommandName,
264 rxFrame,
265 VCLUnoHelper::GetInterface(this),
266 nItemWidth);
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;
284 else
285 return NULL;
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);
305 if (xComponent.is())
306 xComponent->dispose();
308 iController->second = aDescriptor;
310 else
312 maControllers[nItemId] = aDescriptor;
315 if (rxController.is())
316 RegisterHandlers();
322 void SidebarToolBox::UpdateIcons (const Reference<frame::XFrame>& rxFrame)
324 for (ControllerContainer::iterator iController(maControllers.begin()), iEnd(maControllers.end());
325 iController!=iEnd;
326 ++iController)
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());
340 iController!=iEnd;
341 ++iController)
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;
352 return 0;
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();
383 if (xWindow.is() )
384 xWindow->setFocus();
387 return 1;
393 IMPL_LINK(SidebarToolBox, ClickHandler, ToolBox*, pToolBox)
395 if (pToolBox == NULL)
396 return 0;
398 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
399 if (xController.is())
400 xController->click();
402 return 1;
408 IMPL_LINK(SidebarToolBox, DoubleClickHandler, ToolBox*, pToolBox)
410 if (pToolBox == NULL)
411 return 0;
413 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
414 if (xController.is())
415 xController->doubleClick();
417 return 1;
423 IMPL_LINK(SidebarToolBox, SelectHandler, ToolBox*, pToolBox)
425 if (pToolBox == NULL)
426 return 0;
428 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
429 if (xController.is())
430 xController->execute((sal_Int16)pToolBox->GetModifier());
432 return 1;
438 IMPL_LINK(SidebarToolBox, ActivateToolBox, ToolBox*, EMPTYARG)
440 return 1;
446 IMPL_LINK(SidebarToolBox, DeactivateToolBox, ToolBox*, EMPTYARG)
448 return 1;
453 } } // end of namespace sfx2::sidebar