Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sfx2/sidebar/SidebarToolBox.hxx>
21 : #include <sfx2/sidebar/ControllerFactory.hxx>
22 : #include <sfx2/sidebar/Theme.hxx>
23 : #include <sfx2/sidebar/Tools.hxx>
24 :
25 : #include <vcl/builderfactory.hxx>
26 : #include <vcl/gradient.hxx>
27 : #include <toolkit/helper/vclunohelper.hxx>
28 : #include <svtools/miscopt.hxx>
29 : #include <framework/imageproducer.hxx>
30 : #include <com/sun/star/frame/XSubToolbarController.hpp>
31 :
32 : using namespace css;
33 : using namespace css::uno;
34 :
35 : namespace sfx2 { namespace sidebar {
36 :
37 16146 : SidebarToolBox::SidebarToolBox (vcl::Window* pParentWindow)
38 : : ToolBox(pParentWindow, 0),
39 : maItemSeparator(Theme::GetImage(Theme::Image_ToolBoxItemSeparator)),
40 : maControllers(),
41 16146 : mbAreHandlersRegistered(false)
42 : {
43 16146 : SetBackground(Wallpaper());
44 16146 : SetPaintTransparent(true);
45 16146 : SetToolboxButtonSize( TOOLBOX_BUTTONSIZE_SMALL );
46 :
47 : #ifdef DEBUG
48 : SetText(OUString("SidebarToolBox"));
49 : #endif
50 16146 : }
51 :
52 8013 : VCL_BUILDER_FACTORY(SidebarToolBox)
53 :
54 46971 : SidebarToolBox::~SidebarToolBox()
55 : {
56 15657 : disposeOnce();
57 31314 : }
58 :
59 16146 : void SidebarToolBox::dispose()
60 : {
61 16146 : ControllerContainer aControllers;
62 16146 : aControllers.swap(maControllers);
63 34027 : for (ControllerContainer::iterator iController(aControllers.begin()), iEnd(aControllers.end());
64 : iController!=iEnd;
65 : ++iController)
66 : {
67 17881 : Reference<lang::XComponent> xComponent (iController->second.mxController, UNO_QUERY);
68 17881 : if (xComponent.is())
69 17881 : xComponent->dispose();
70 17881 : }
71 :
72 16146 : if (mbAreHandlersRegistered)
73 : {
74 9683 : SetDropdownClickHdl(Link<ToolBox *, void>());
75 9683 : SetClickHdl(Link<ToolBox *, void>());
76 9683 : SetDoubleClickHdl(Link<ToolBox *, void>());
77 9683 : SetSelectHdl(Link<ToolBox *, void>());
78 9683 : SetActivateHdl(Link<ToolBox *, void>());
79 9683 : SetDeactivateHdl(Link<ToolBox *, void>());
80 9683 : mbAreHandlersRegistered = false;
81 : }
82 :
83 16146 : ToolBox::dispose();
84 16146 : }
85 :
86 16261 : void SidebarToolBox::InsertItem(const OUString& rCommand,
87 : const css::uno::Reference<css::frame::XFrame>& rFrame,
88 : ToolBoxItemBits nBits, const Size& rRequestedSize, sal_uInt16 nPos)
89 : {
90 16261 : ToolBox::InsertItem(rCommand, rFrame, nBits, rRequestedSize, nPos);
91 :
92 16261 : CreateController(GetItemId(rCommand), rFrame, std::max(rRequestedSize.Width(), 0L));
93 16261 : RegisterHandlers();
94 16261 : }
95 :
96 150 : void SidebarToolBox::Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect)
97 : {
98 150 : ToolBox::Paint(rRenderContext, rRect);
99 :
100 150 : if (Theme::GetBoolean(Theme::Bool_UseToolBoxItemSeparator))
101 : {
102 0 : const sal_Int32 nSeparatorY((GetSizePixel().Height() - maItemSeparator.GetSizePixel().Height()) / 2);
103 0 : const sal_uInt16 nItemCount(GetItemCount());
104 0 : int nLastRight(-1);
105 0 : for (sal_uInt16 nIndex = 0; nIndex < nItemCount; ++nIndex)
106 : {
107 0 : const Rectangle aItemBoundingBox (GetItemPosRect(nIndex));
108 0 : if (nLastRight >= 0)
109 : {
110 0 : const int nSeparatorX((nLastRight + aItemBoundingBox.Left() - 1) / 2);
111 0 : rRenderContext.DrawImage(Point(nSeparatorX, nSeparatorY), maItemSeparator);
112 : }
113 0 : nLastRight = aItemBoundingBox.Right();
114 : }
115 : }
116 150 : }
117 :
118 3954 : bool SidebarToolBox::Notify (NotifyEvent& rEvent)
119 : {
120 3954 : if (rEvent.GetType() == MouseNotifyEvent::KEYINPUT)
121 : {
122 0 : if (rEvent.GetKeyEvent()->GetKeyCode().GetCode() == KEY_TAB)
123 : {
124 : // Special handling for transferring handling of KEY_TAB
125 : // that becomes necessary because of our parent that is
126 : // not the dialog but a background control.
127 0 : return DockingWindow::Notify(rEvent);
128 : }
129 : }
130 3954 : return ToolBox::Notify(rEvent);
131 : }
132 :
133 16261 : void SidebarToolBox::CreateController (
134 : const sal_uInt16 nItemId,
135 : const css::uno::Reference<css::frame::XFrame>& rxFrame,
136 : const sal_Int32 nItemWidth)
137 : {
138 16261 : ItemDescriptor aDescriptor;
139 :
140 32522 : const OUString sCommandName (GetItemCommand(nItemId));
141 :
142 32522 : aDescriptor.mxController = sfx2::sidebar::ControllerFactory::CreateToolBoxController(
143 : this, nItemId, sCommandName, rxFrame,
144 16261 : VCLUnoHelper::GetInterface(this), nItemWidth);
145 16261 : if (aDescriptor.mxController.is())
146 : {
147 16261 : aDescriptor.maURL = sfx2::sidebar::Tools::GetURL(sCommandName);
148 16261 : aDescriptor.msCurrentCommand = sCommandName;
149 :
150 16261 : maControllers.insert(std::make_pair(nItemId, aDescriptor));
151 16261 : }
152 16261 : }
153 :
154 0 : Reference<frame::XToolbarController> SidebarToolBox::GetControllerForItemId (const sal_uInt16 nItemId) const
155 : {
156 0 : ControllerContainer::const_iterator iController (maControllers.find(nItemId));
157 0 : if (iController != maControllers.end())
158 0 : return iController->second.mxController;
159 : else
160 0 : return NULL;
161 : }
162 :
163 1620 : void SidebarToolBox::SetController(const sal_uInt16 nItemId,
164 : const css::uno::Reference<css::frame::XToolbarController>& rxController,
165 : const OUString& rsCommandName)
166 : {
167 1620 : ItemDescriptor aDescriptor;
168 1620 : aDescriptor.mxController = rxController;
169 1620 : aDescriptor.maURL = sfx2::sidebar::Tools::GetURL(rsCommandName);
170 1620 : aDescriptor.msCurrentCommand = rsCommandName;
171 :
172 1620 : ControllerContainer::iterator iController (maControllers.find(nItemId));
173 1620 : if (iController != maControllers.end())
174 : {
175 0 : Reference<lang::XComponent> xComponent (iController->second.mxController, UNO_QUERY);
176 0 : if (xComponent.is())
177 0 : xComponent->dispose();
178 :
179 0 : iController->second = aDescriptor;
180 : }
181 : else
182 : {
183 1620 : maControllers[nItemId] = aDescriptor;
184 : }
185 :
186 1620 : if (rxController.is())
187 1620 : RegisterHandlers();
188 1620 : }
189 :
190 0 : sal_uInt16 SidebarToolBox::GetItemIdForSubToolbarName (const OUString& rsSubToolbarName) const
191 : {
192 0 : for (ControllerContainer::const_iterator iController(maControllers.begin()), iEnd(maControllers.end());
193 : iController!=iEnd;
194 : ++iController)
195 : {
196 0 : Reference<frame::XToolbarController> xController (iController->second.mxController);
197 0 : Reference<frame::XSubToolbarController> xSubToolbarController (xController, UNO_QUERY);
198 0 : if (xSubToolbarController.is())
199 : {
200 0 : const OUString sName (xSubToolbarController->getSubToolbarName());
201 0 : if (sName.equals(rsSubToolbarName))
202 0 : return iController->first;
203 : }
204 0 : }
205 0 : return 0;
206 : }
207 :
208 17881 : void SidebarToolBox::RegisterHandlers()
209 : {
210 17881 : if ( ! mbAreHandlersRegistered)
211 : {
212 9683 : mbAreHandlersRegistered = true;
213 9683 : SetDropdownClickHdl(LINK(this, SidebarToolBox, DropDownClickHandler));
214 9683 : SetClickHdl(LINK(this, SidebarToolBox, ClickHandler));
215 9683 : SetDoubleClickHdl(LINK(this, SidebarToolBox, DoubleClickHandler));
216 9683 : SetSelectHdl(LINK(this, SidebarToolBox, SelectHandler));
217 : }
218 17881 : }
219 :
220 0 : IMPL_LINK_TYPED(SidebarToolBox, DropDownClickHandler, ToolBox*, pToolBox, void)
221 : {
222 0 : if (pToolBox != NULL)
223 : {
224 0 : Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
225 0 : if (xController.is())
226 : {
227 0 : Reference<awt::XWindow> xWindow = xController->createPopupWindow();
228 0 : if (xWindow.is() )
229 0 : xWindow->setFocus();
230 0 : }
231 : }
232 0 : }
233 :
234 0 : IMPL_LINK_TYPED(SidebarToolBox, ClickHandler, ToolBox*, pToolBox, void)
235 : {
236 0 : if (pToolBox == NULL)
237 0 : return;
238 :
239 0 : Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
240 0 : if (xController.is())
241 0 : xController->click();
242 : }
243 :
244 0 : IMPL_LINK_TYPED(SidebarToolBox, DoubleClickHandler, ToolBox*, pToolBox, void)
245 : {
246 0 : if (pToolBox == NULL)
247 0 : return;
248 :
249 0 : Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
250 0 : if (xController.is())
251 0 : xController->doubleClick();
252 : }
253 :
254 0 : IMPL_LINK_TYPED(SidebarToolBox, SelectHandler, ToolBox*, pToolBox, void)
255 : {
256 0 : if (pToolBox == NULL)
257 0 : return;
258 :
259 0 : Reference<frame::XToolbarController> xController (GetControllerForItemId(pToolBox->GetCurItemId()));
260 0 : if (xController.is())
261 0 : xController->execute((sal_Int16)pToolBox->GetModifier());
262 : }
263 :
264 : } } // end of namespace sfx2::sidebar
265 :
266 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|