LibreOffice Module sdext (master)  1
PresenterPaneContainer.cxx
Go to the documentation of this file.
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 
21 #include "PresenterPaneBase.hxx"
22 
23 using namespace ::com::sun::star;
24 using namespace ::com::sun::star::uno;
25 using namespace ::com::sun::star::drawing::framework;
26 
27 namespace sdext::presenter {
28 
30  const Reference<XComponentContext>& rxContext)
32 {
33  Reference<lang::XMultiComponentFactory> xFactory (rxContext->getServiceManager());
34  if (xFactory.is())
35  {
37  xFactory->createInstanceWithContext(
38  "com.sun.star.comp.Draw.PresenterHelper",
39  rxContext),
40  UNO_QUERY_THROW);
41  }
42 }
43 
45 {
46 }
47 
49  const Reference<XResourceId>& rxPaneId,
50  const OUString& rsViewURL,
51  const OUString& rsTitle,
52  const OUString& rsAccessibleTitle,
53  const bool bIsOpaque,
54  const ViewInitializationFunction& rViewInitialization)
55 {
56  if ( ! rxPaneId.is())
57  return;
58 
59  SharedPaneDescriptor pPane (FindPaneURL(rxPaneId->getResourceURL()));
60  if (pPane)
61  return;
62 
63  // No entry found for the given pane id. Create a new one.
64  SharedPaneDescriptor pDescriptor = std::make_shared<PaneDescriptor>();
65  pDescriptor->mxPaneId = rxPaneId;
66  pDescriptor->msViewURL = rsViewURL;
67  pDescriptor->mxPane = nullptr;
68  if (rsTitle.indexOf('%') < 0)
69  {
70  pDescriptor->msTitle = rsTitle;
71  pDescriptor->msTitleTemplate.clear();
72  }
73  else
74  {
75  pDescriptor->msTitleTemplate = rsTitle;
76  pDescriptor->msTitle.clear();
77  }
78  pDescriptor->msAccessibleTitleTemplate = rsAccessibleTitle;
79  pDescriptor->maViewInitialization = rViewInitialization;
80  pDescriptor->mbIsActive = true;
81  pDescriptor->mbIsOpaque = bIsOpaque;
82  pDescriptor->mbIsSprite = false;
83 
84  maPanes.push_back(pDescriptor);
85 }
86 
88 {
89  for (const auto& rxPane : maPanes)
90  if (rxPane->mxPaneId.is())
91  RemovePane(rxPane->mxPaneId);
92 }
93 
96 {
97  SharedPaneDescriptor pDescriptor;
98 
99  if (rxPane.is())
100  {
101  OUString sPaneURL;
102  Reference<XResourceId> xPaneId (rxPane->getResourceId());
103  if (xPaneId.is())
104  sPaneURL = xPaneId->getResourceURL();
105 
106  pDescriptor = FindPaneURL(sPaneURL);
107  if (!pDescriptor)
108  PreparePane(xPaneId, OUString(), OUString(), OUString(),
109  false, ViewInitializationFunction());
110  pDescriptor = FindPaneURL(sPaneURL);
111  if (pDescriptor)
112  {
113  Reference<awt::XWindow> xWindow (rxPane->getWindow());
114  pDescriptor->mxContentWindow = xWindow;
115  pDescriptor->mxPaneId = xPaneId;
116  pDescriptor->mxPane = rxPane;
117  pDescriptor->mxPane->SetTitle(pDescriptor->msTitle);
118 
119  if (xWindow.is())
120  xWindow->addEventListener(this);
121  }
122  }
123 
124  return pDescriptor;
125 }
126 
129  const Reference<XResourceId>& rxPaneId,
130  const Reference<awt::XWindow>& rxBorderWindow)
131 {
132  // The content window may not be present. Use the resource URL of the
133  // pane id as key.
134  OUString sPaneURL;
135  if (rxPaneId.is())
136  sPaneURL = rxPaneId->getResourceURL();
137 
138  SharedPaneDescriptor pDescriptor (FindPaneURL(sPaneURL));
139  if (pDescriptor)
140  {
141  pDescriptor->mxBorderWindow = rxBorderWindow;
142  return pDescriptor;
143  }
144  else
145  return SharedPaneDescriptor();
146 }
147 
150  const Reference<XView>& rxView)
151 {
152  SharedPaneDescriptor pDescriptor;
153 
154  if (rxView.is())
155  {
156  OUString sPaneURL;
157  Reference<XResourceId> xViewId (rxView->getResourceId());
158  if (xViewId.is())
159  {
160  Reference<XResourceId> xPaneId (xViewId->getAnchor());
161  if (xPaneId.is())
162  sPaneURL = xPaneId->getResourceURL();
163  }
164 
165  pDescriptor = FindPaneURL(sPaneURL);
166  if (pDescriptor)
167  {
168  pDescriptor->mxView = rxView;
169  try
170  {
171  if (pDescriptor->maViewInitialization)
172  pDescriptor->maViewInitialization(rxView);
173  }
174  catch (RuntimeException&)
175  {
176  OSL_ASSERT(false);
177  }
178  }
179  }
180 
181  return pDescriptor;
182 }
183 
185  PresenterPaneContainer::RemovePane (const Reference<XResourceId>& rxPaneId)
186 {
187  SharedPaneDescriptor pDescriptor (FindPaneId(rxPaneId));
188  if (pDescriptor)
189  {
190  if (pDescriptor->mxContentWindow.is())
191  pDescriptor->mxContentWindow->removeEventListener(this);
192  pDescriptor->mxContentWindow = nullptr;
193  pDescriptor->mxBorderWindow = nullptr;
194  pDescriptor->mxPane = nullptr;
195  pDescriptor->mxView = nullptr;
196  pDescriptor->mbIsActive = false;
197  }
198  return pDescriptor;
199 }
200 
202  PresenterPaneContainer::RemoveView (const Reference<XView>& rxView)
203 {
204  SharedPaneDescriptor pDescriptor;
205 
206  if (rxView.is())
207  {
208  OUString sPaneURL;
209  Reference<XResourceId> xViewId (rxView->getResourceId());
210  if (xViewId.is())
211  {
212  Reference<XResourceId> xPaneId (xViewId->getAnchor());
213  if (xPaneId.is())
214  sPaneURL = xPaneId->getResourceURL();
215  }
216 
217  pDescriptor = FindPaneURL(sPaneURL);
218  if (pDescriptor)
219  {
220  pDescriptor->mxView = nullptr;
221  }
222  }
223 
224  return pDescriptor;
225 }
226 
228  const Reference<awt::XWindow>& rxBorderWindow)
229 {
230  auto iPane = std::find_if(maPanes.begin(), maPanes.end(),
231  [&rxBorderWindow](const SharedPaneDescriptor& rxPane) { return rxPane->mxBorderWindow == rxBorderWindow; });
232  if (iPane != maPanes.end())
233  return *iPane;
234  return SharedPaneDescriptor();
235 }
236 
238  const Reference<awt::XWindow>& rxContentWindow)
239 {
240  auto iPane = std::find_if(maPanes.begin(), maPanes.end(),
241  [&rxContentWindow](const SharedPaneDescriptor& rxPane) { return rxPane->mxContentWindow == rxContentWindow; });
242  if (iPane != maPanes.end())
243  return *iPane;
244  return SharedPaneDescriptor();
245 }
246 
248  const OUString& rsPaneURL)
249 {
250  auto iPane = std::find_if(maPanes.begin(), maPanes.end(),
251  [&rsPaneURL](const SharedPaneDescriptor& rxPane) { return rxPane->mxPaneId->getResourceURL() == rsPaneURL; });
252  if (iPane != maPanes.end())
253  return *iPane;
254  return SharedPaneDescriptor();
255 }
256 
258  const Reference<XResourceId>& rxPaneId)
259 {
260  if ( ! rxPaneId.is())
261  return SharedPaneDescriptor();
262 
263  auto iPane = std::find_if(maPanes.begin(), maPanes.end(),
264  [&rxPaneId](const SharedPaneDescriptor& rxPane) { return rxPaneId->compareTo(rxPane->mxPaneId) == 0; });
265  if (iPane != maPanes.end())
266  return *iPane;
267  return SharedPaneDescriptor();
268 }
269 
271  const OUString& rsViewURL)
272 {
273  auto iPane = std::find_if(maPanes.begin(), maPanes.end(),
274  [&rsViewURL](const SharedPaneDescriptor& rxPane) { return rsViewURL == rxPane->msViewURL; });
275  if (iPane != maPanes.end())
276  return *iPane;
277  return SharedPaneDescriptor();
278 }
279 
280 OUString PresenterPaneContainer::GetPaneURLForViewURL (const OUString& rsViewURL)
281 {
282  SharedPaneDescriptor pDescriptor (FindViewURL(rsViewURL));
283  if (pDescriptor)
284  if (pDescriptor->mxPaneId.is())
285  return pDescriptor->mxPaneId->getResourceURL();
286  return OUString();
287 }
288 
290 {
291  if (!rpDescriptor)
292  return;
293 
294  // Find iterator for pDescriptor.
295  PaneList::iterator iEnd (maPanes.end());
296  auto iPane = std::find_if(maPanes.begin(), iEnd,
297  [&rpDescriptor](SharedPaneDescriptor& rxPane) { return rxPane.get() == rpDescriptor.get(); });
298  OSL_ASSERT(iPane!=iEnd);
299  if (iPane == iEnd)
300  return;
301 
302  if (mxPresenterHelper.is())
303  mxPresenterHelper->toTop(rpDescriptor->mxBorderWindow);
304 
305  maPanes.erase(iPane);
306  maPanes.push_back(rpDescriptor);
307 }
308 
309 //----- XEventListener --------------------------------------------------------
310 
312  const css::lang::EventObject& rEvent)
313 {
314  SharedPaneDescriptor pDescriptor (
315  FindContentWindow(Reference<awt::XWindow>(rEvent.Source, UNO_QUERY)));
316  if (pDescriptor)
317  {
318  RemovePane(pDescriptor->mxPaneId);
319  }
320 }
321 
322 //===== PresenterPaneContainer::PaneDescriptor ================================
323 
325 {
326  mbIsActive = bIsActive;
327 }
328 
329 } // end of namespace ::sdext::presenter
330 
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL disposing() override
SharedPaneDescriptor FindPaneId(const css::uno::Reference< css::drawing::framework::XResourceId > &rxPaneId)
Find the pane whose resource id is identical to the given one.
SharedPaneDescriptor StoreView(const css::uno::Reference< css::drawing::framework::XView > &rxView)
css::uno::Reference< css::drawing::XPresenterHelper > mxPresenterHelper
void ToTop(const SharedPaneDescriptor &rpDescriptor)
Reference< drawing::XPresenterHelper > mxPresenterHelper
SharedPaneDescriptor RemoveView(const css::uno::Reference< css::drawing::framework::XView > &rxView)
SharedPaneDescriptor FindContentWindow(const css::uno::Reference< css::awt::XWindow > &rxBorderWindow)
Find the pane whose border window is identical to the given content window.
std::mutex m_aMutex
SharedPaneDescriptor RemovePane(const css::uno::Reference< css::drawing::framework::XResourceId > &rxPaneId)
::std::function< void(const css::uno::Reference< css::drawing::framework::XView > &)> ViewInitializationFunction
SharedPaneDescriptor FindBorderWindow(const css::uno::Reference< css::awt::XWindow > &rxBorderWindow)
Find the pane whose border window is identical to the given border window.
SharedPaneDescriptor FindViewURL(const OUString &rsViewURL)
SharedPaneDescriptor FindPaneURL(const OUString &rsPaneURL)
Find the pane whose pane URL is identical to the given URL string.
SharedPaneDescriptor StorePane(const rtl::Reference< PresenterPaneBase > &rxPane)
SharedPaneDescriptor StoreBorderWindow(const css::uno::Reference< css::drawing::framework::XResourceId > &rxPaneId, const css::uno::Reference< css::awt::XWindow > &rxBorderWindow)
OUString GetPaneURLForViewURL(const OUString &rsViewURL)
std::shared_ptr< PaneDescriptor > SharedPaneDescriptor
::cppu::WeakComponentImplHelper< css::lang::XEventListener > PresenterPaneContainerInterfaceBase
PresenterPaneContainer(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
Reference< XSingleServiceFactory > xFactory
void PreparePane(const css::uno::Reference< css::drawing::framework::XResourceId > &rxPaneId, const OUString &rsViewURL, const OUString &rsTitle, const OUString &rsAccessibleTitle, const bool bIsOpaque, const ViewInitializationFunction &rViewInitialization)