Branch libreoffice-5-0-4
[LibreOffice.git] / sfx2 / source / sidebar / SidebarToolBox.cxx
blob64df989c154e99f9c99a7012bbcfb3671e597858
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <sfx2/sidebar/SidebarToolBox.hxx>
21 #include <sfx2/sidebar/ControllerFactory.hxx>
22 #include <sfx2/sidebar/Theme.hxx>
23 #include <sfx2/sidebar/Tools.hxx>
25 #include <vcl/builderfactory.hxx>
26 #include <vcl/gradient.hxx>
27 #include <vcl/settings.hxx>
28 #include <toolkit/helper/vclunohelper.hxx>
29 #include <svtools/miscopt.hxx>
30 #include <framework/imageproducer.hxx>
31 #include <com/sun/star/frame/XSubToolbarController.hpp>
33 using namespace css;
34 using namespace css::uno;
36 namespace {
37 void lcl_RTLizeCommandURL( OUString& rCommandURL )
39 if (rCommandURL == ".uno:ParaLeftToRight")
40 rCommandURL = ".uno:ParaRightToLeft";
41 else if (rCommandURL == ".uno:ParaRightToLeft")
42 rCommandURL = ".uno:ParaLeftToRight";
43 else if (rCommandURL == ".uno:LeftPara")
44 rCommandURL = ".uno:RightPara";
45 else if (rCommandURL == ".uno:RightPara")
46 rCommandURL = ".uno:LeftPara";
47 else if (rCommandURL == ".uno:AlignLeft")
48 rCommandURL = ".uno:AlignRight";
49 else if (rCommandURL == ".uno:AlignRight")
50 rCommandURL = ".uno:AlignLeft";
54 namespace sfx2 { namespace sidebar {
56 SidebarToolBox::SidebarToolBox (vcl::Window* pParentWindow)
57 : ToolBox(pParentWindow, 0),
58 maItemSeparator(Theme::GetImage(Theme::Image_ToolBoxItemSeparator)),
59 maControllers(),
60 mbAreHandlersRegistered(false)
62 SetBackground(Wallpaper());
63 SetPaintTransparent(true);
64 SetToolboxButtonSize( TOOLBOX_BUTTONSIZE_SMALL );
66 #ifdef DEBUG
67 SetText(OUString("SidebarToolBox"));
68 #endif
71 VCL_BUILDER_FACTORY(SidebarToolBox)
73 SidebarToolBox::~SidebarToolBox()
75 disposeOnce();
78 void SidebarToolBox::dispose()
80 ControllerContainer aControllers;
81 aControllers.swap(maControllers);
82 for (ControllerContainer::iterator iController(aControllers.begin()), iEnd(aControllers.end());
83 iController!=iEnd;
84 ++iController)
86 Reference<lang::XComponent> xComponent (iController->second.mxController, UNO_QUERY);
87 if (xComponent.is())
88 xComponent->dispose();
91 if (mbAreHandlersRegistered)
93 SetDropdownClickHdl(Link<ToolBox *, void>());
94 SetClickHdl(Link<ToolBox *, void>());
95 SetDoubleClickHdl(Link<ToolBox *, void>());
96 SetSelectHdl(Link<ToolBox *, void>());
97 SetActivateHdl(Link<ToolBox *, void>());
98 SetDeactivateHdl(Link<ToolBox *, void>());
99 mbAreHandlersRegistered = false;
102 ToolBox::dispose();
105 void SidebarToolBox::InsertItem(const OUString& rCommand,
106 const css::uno::Reference<css::frame::XFrame>& rFrame,
107 ToolBoxItemBits nBits, const Size& rRequestedSize, sal_uInt16 nPos)
109 OUString aCommand( rCommand );
111 if( AllSettings::GetLayoutRTL() )
113 lcl_RTLizeCommandURL( aCommand );
116 ToolBox::InsertItem(aCommand, rFrame, nBits, rRequestedSize, nPos);
118 CreateController(GetItemId(aCommand), rFrame, std::max(rRequestedSize.Width(), 0L));
119 RegisterHandlers();
122 void SidebarToolBox::Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect)
124 ToolBox::Paint(rRenderContext, rRect);
126 if (Theme::GetBoolean(Theme::Bool_UseToolBoxItemSeparator))
128 const sal_Int32 nSeparatorY((GetSizePixel().Height() - maItemSeparator.GetSizePixel().Height()) / 2);
129 const sal_uInt16 nItemCount(GetItemCount());
130 int nLastRight(-1);
131 for (sal_uInt16 nIndex = 0; nIndex < nItemCount; ++nIndex)
133 const Rectangle aItemBoundingBox (GetItemPosRect(nIndex));
134 if (nLastRight >= 0)
136 const int nSeparatorX((nLastRight + aItemBoundingBox.Left() - 1) / 2);
137 rRenderContext.DrawImage(Point(nSeparatorX, nSeparatorY), maItemSeparator);
139 nLastRight = aItemBoundingBox.Right();
144 bool SidebarToolBox::Notify (NotifyEvent& rEvent)
146 if (rEvent.GetType() == MouseNotifyEvent::KEYINPUT)
148 if (rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_TAB)
150 // Special handling for transferring handling of KEY_TAB
151 // that becomes necessary because of our parent that is
152 // not the dialog but a background control.
153 return DockingWindow::Notify(rEvent);
156 return ToolBox::Notify(rEvent);
159 void SidebarToolBox::CreateController (
160 const sal_uInt16 nItemId,
161 const css::uno::Reference<css::frame::XFrame>& rxFrame,
162 const sal_Int32 nItemWidth)
164 ItemDescriptor aDescriptor;
166 const OUString sCommandName (GetItemCommand(nItemId));
168 aDescriptor.mxController = sfx2::sidebar::ControllerFactory::CreateToolBoxController(
169 this, nItemId, sCommandName, rxFrame,
170 VCLUnoHelper::GetInterface(this), nItemWidth);
171 if (aDescriptor.mxController.is())
173 aDescriptor.maURL = sfx2::sidebar::Tools::GetURL(sCommandName);
174 aDescriptor.msCurrentCommand = sCommandName;
176 maControllers.insert(std::make_pair(nItemId, aDescriptor));
180 Reference<frame::XToolbarController> SidebarToolBox::GetControllerForItemId (const sal_uInt16 nItemId) const
182 ControllerContainer::const_iterator iController (maControllers.find(nItemId));
183 if (iController != maControllers.end())
184 return iController->second.mxController;
185 else
186 return NULL;
189 void SidebarToolBox::SetController(const sal_uInt16 nItemId,
190 const css::uno::Reference<css::frame::XToolbarController>& rxController,
191 const OUString& rsCommandName)
193 ItemDescriptor aDescriptor;
194 aDescriptor.mxController = rxController;
195 aDescriptor.maURL = sfx2::sidebar::Tools::GetURL(rsCommandName);
196 aDescriptor.msCurrentCommand = rsCommandName;
198 ControllerContainer::iterator iController (maControllers.find(nItemId));
199 if (iController != maControllers.end())
201 Reference<lang::XComponent> xComponent (iController->second.mxController, UNO_QUERY);
202 if (xComponent.is())
203 xComponent->dispose();
205 iController->second = aDescriptor;
207 else
209 maControllers[nItemId] = aDescriptor;
212 if (rxController.is())
213 RegisterHandlers();
216 sal_uInt16 SidebarToolBox::GetItemIdForSubToolbarName (const OUString& rsSubToolbarName) const
218 for (ControllerContainer::const_iterator iController(maControllers.begin()), iEnd(maControllers.end());
219 iController!=iEnd;
220 ++iController)
222 Reference<frame::XToolbarController> xController (iController->second.mxController);
223 Reference<frame::XSubToolbarController> xSubToolbarController (xController, UNO_QUERY);
224 if (xSubToolbarController.is())
226 const OUString sName (xSubToolbarController->getSubToolbarName());
227 if (sName.equals(rsSubToolbarName))
228 return iController->first;
231 return 0;
234 void SidebarToolBox::RegisterHandlers()
236 if ( ! mbAreHandlersRegistered)
238 mbAreHandlersRegistered = true;
239 SetDropdownClickHdl(LINK(this, SidebarToolBox, DropDownClickHandler));
240 SetClickHdl(LINK(this, SidebarToolBox, ClickHandler));
241 SetDoubleClickHdl(LINK(this, SidebarToolBox, DoubleClickHandler));
242 SetSelectHdl(LINK(this, SidebarToolBox, SelectHandler));
243 SetActivateHdl(LINK(this, SidebarToolBox, ActivateToolBox));
244 SetDeactivateHdl(LINK(this, SidebarToolBox, DeactivateToolBox));
248 IMPL_LINK_TYPED(SidebarToolBox, DropDownClickHandler, ToolBox*, pToolBox, void)
250 if (pToolBox != NULL)
252 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
253 if (xController.is())
255 Reference<awt::XWindow> xWindow = xController->createPopupWindow();
256 if (xWindow.is() )
257 xWindow->setFocus();
262 IMPL_LINK_TYPED(SidebarToolBox, ClickHandler, ToolBox*, pToolBox, void)
264 if (pToolBox == NULL)
265 return;
267 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
268 if (xController.is())
269 xController->click();
272 IMPL_LINK_TYPED(SidebarToolBox, DoubleClickHandler, ToolBox*, pToolBox, void)
274 if (pToolBox == NULL)
275 return;
277 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
278 if (xController.is())
279 xController->doubleClick();
282 IMPL_LINK_TYPED(SidebarToolBox, SelectHandler, ToolBox*, pToolBox, void)
284 if (pToolBox == NULL)
285 return;
287 Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
288 if (xController.is())
289 xController->execute((sal_Int16)pToolBox->GetModifier());
292 IMPL_STATIC_LINK_NOARG_TYPED(
293 SidebarToolBox, ActivateToolBox, ToolBox*, void)
296 IMPL_STATIC_LINK_NOARG_TYPED(
297 SidebarToolBox, DeactivateToolBox, ToolBox*, void)
300 } } // end of namespace sfx2::sidebar
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */