LibreOffice Module cui (master)  1
hlmarkwn.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 
20 #include <dialmgr.hxx>
21 #include <unotools/viewoptions.hxx>
22 #include <vcl/graph.hxx>
23 
24 // UNO-Stuff
26 #include <comphelper/sequence.hxx>
27 #include <com/sun/star/awt/XBitmap.hpp>
28 #include <com/sun/star/frame/Desktop.hpp>
29 #include <com/sun/star/beans/NamedValue.hpp>
30 #include <com/sun/star/beans/PropertyValue.hpp>
31 #include <com/sun/star/document/XLinkTargetSupplier.hpp>
32 #include <com/sun/star/beans/XPropertySet.hpp>
33 #include <com/sun/star/io/IOException.hpp>
34 
36 
37 #include <strings.hrc>
38 #include <hlmarkwn.hxx>
39 #include <hltpbase.hxx>
40 #include <hlmarkwn_def.hxx>
41 
42 #include <stack>
43 
44 using namespace ::com::sun::star;
45 
46 namespace {
47 
48 // Userdata-struct for tree-entries
49 struct TargetData
50 {
51  OUString aUStrLinkname;
52  bool bIsTarget;
53 
54  TargetData (const OUString& aUStrLName, bool bTarget)
55  : bIsTarget(bTarget)
56  {
57  if (bIsTarget)
58  aUStrLinkname = aUStrLName;
59  }
60 };
61 
62 }
63 
64 //*** Window-Class ***
65 // Constructor / Destructor
67  : GenericDialogController(pParentDialog, "cui/ui/hyperlinkmarkdialog.ui", "HyperlinkMark")
68  , mpParent(pParentPage)
69  , mnError(LERR_NOERROR)
70  , mxBtApply(m_xBuilder->weld_button("ok"))
71  , mxBtClose(m_xBuilder->weld_button("close"))
72  , mxLbTree(m_xBuilder->weld_tree_view("TreeListBox"))
73  , mxError(m_xBuilder->weld_label("error"))
74 {
75  mxLbTree->set_size_request(mxLbTree->get_approximate_digit_width() * 25,
76  mxLbTree->get_height_rows(12));
77  mxBtApply->connect_clicked( LINK ( this, SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl ) );
78  mxBtClose->connect_clicked( LINK ( this, SvxHlinkDlgMarkWnd, ClickCloseHdl_Impl ) );
79  mxLbTree->connect_row_activated( LINK ( this, SvxHlinkDlgMarkWnd, DoubleClickApplyHdl_Impl ) );
80 }
81 
83 {
84  ClearTree();
85 }
86 
88 {
89  if (mnError == LERR_NOENTRIES)
90  {
91  OUString aStrMessage = CuiResId( RID_SVXSTR_HYPDLG_ERR_LERR_NOENTRIES );
92  mxError->set_label(aStrMessage);
93  mxError->show();
94  mxLbTree->hide();
95  }
96  else if (mnError == LERR_DOCNOTOPEN)
97  {
98  OUString aStrMessage = CuiResId( RID_SVXSTR_HYPDLG_ERR_LERR_DOCNOTOPEN );
99  mxError->set_label(aStrMessage);
100  mxError->show();
101  mxLbTree->hide();
102  }
103  else
104  {
105  mxLbTree->show();
106  mxError->hide();
107  }
108 }
109 
110 // Set an errorstatus
111 sal_uInt16 SvxHlinkDlgMarkWnd::SetError( sal_uInt16 nError)
112 {
113  sal_uInt16 nOldError = mnError;
114  mnError = nError;
115 
116  if( mnError != LERR_NOERROR )
117  ClearTree();
118 
119  ErrorChanged();
120 
121  return nOldError;
122 }
123 
124 // Move window
125 void SvxHlinkDlgMarkWnd::MoveTo(const Point& rNewPos)
126 {
127  m_xDialog->window_move(rNewPos.X(), rNewPos.Y());
128 }
129 
130 namespace
131 {
132  void SelectPath(weld::TreeIter* pEntry, weld::TreeView& rLbTree,
133  std::deque<OUString> &rLastSelectedPath)
134  {
135  OUString sTitle(rLastSelectedPath.front());
136  rLastSelectedPath.pop_front();
137  if (sTitle.isEmpty())
138  return;
139  while (pEntry)
140  {
141  if (sTitle == rLbTree.get_text(*pEntry))
142  {
143  rLbTree.select(*pEntry);
144  rLbTree.scroll_to_row(*pEntry);
145  if (!rLastSelectedPath.empty())
146  {
147  rLbTree.expand_row(*pEntry);
148  if (!rLbTree.iter_children(*pEntry))
149  pEntry = nullptr;
150  SelectPath(pEntry, rLbTree, rLastSelectedPath);
151  }
152  break;
153  }
154  if (!rLbTree.iter_next_sibling(*pEntry))
155  pEntry = nullptr;
156  }
157  }
158 }
159 
160 #define TG_SETTING_MANAGER "TargetInDocument"
161 #define TG_SETTING_LASTMARK "LastSelectedMark"
162 #define TG_SETTING_LASTPATH "LastSelectedPath"
163 
165 {
166  bool bSelectedEntry = false;
167 
168  OUString sLastSelectedMark;
169  std::deque<OUString> aLastSelectedPath;
170  SvtViewOptions aViewSettings( EViewType::Dialog, TG_SETTING_MANAGER );
171  if (aViewSettings.Exists())
172  {
173  //Maybe we might want to have some sort of mru list and keep a mapping
174  //per document, rather than the current reuse of "the last thing
175  //selected, regardless of the document"
176  aViewSettings.GetUserItem(TG_SETTING_LASTMARK) >>= sLastSelectedMark;
177  uno::Sequence<OUString> aTmp;
178  aViewSettings.GetUserItem(TG_SETTING_LASTPATH) >>= aTmp;
179  aLastSelectedPath = comphelper::sequenceToContainer< std::deque<OUString> >(aTmp);
180  }
181  //fallback to previous entry selected the last time we executed this dialog.
182  //First see if the exact mark exists and re-use that
183  if (!sLastSelectedMark.isEmpty())
184  bSelectedEntry = SelectEntry(sLastSelectedMark);
185  //Otherwise just select the closest path available
186  //now to what was available at dialog close time
187  if (!bSelectedEntry && !aLastSelectedPath.empty())
188  {
189  std::deque<OUString> aTmpSelectedPath(aLastSelectedPath);
190  std::unique_ptr<weld::TreeIter> xEntry(mxLbTree->make_iterator());
191  if (!mxLbTree->get_iter_first(*xEntry))
192  xEntry.reset();
193  SelectPath(xEntry.get(), *mxLbTree, aTmpSelectedPath);
194  }
195 }
196 
197 // Interface to refresh tree
198 void SvxHlinkDlgMarkWnd::RefreshTree (const OUString& aStrURL)
199 {
200  OUString aUStrURL;
201 
202  weld::WaitObject aWait(m_xDialog.get());
203 
204  ClearTree();
205 
206  sal_Int32 nPos = aStrURL.indexOf('#');
207 
208  if (nPos != 0)
209  aUStrURL = aStrURL;
210 
211  if (!RefreshFromDoc(aUStrURL))
212  ErrorChanged();
213 
214  bool bSelectedEntry = false;
215 
216  if ( nPos != -1 )
217  {
218  OUString aStrMark = aStrURL.copy(nPos+1);
219  bSelectedEntry = SelectEntry(aStrMark);
220  }
221 
222  if (!bSelectedEntry)
224 }
225 
226 // get links from document
227 bool SvxHlinkDlgMarkWnd::RefreshFromDoc(const OUString& aURL)
228 {
230 
231  uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( ::comphelper::getProcessComponentContext() );
232  uno::Reference< lang::XComponent > xComp;
233 
234  if( !aURL.isEmpty() )
235  {
236  // load from url
237  if( xDesktop.is() )
238  {
239  try
240  {
241  uno::Sequence< beans::PropertyValue > aArg(1);
242  aArg.getArray()[0].Name = "Hidden";
243  aArg.getArray()[0].Value <<= true;
244  xComp = xDesktop->loadComponentFromURL( aURL, "_blank", 0, aArg );
245  }
246  catch( const io::IOException& )
247  {
248 
249  }
250  catch( const lang::IllegalArgumentException& )
251  {
252 
253  }
254  }
255  }
256  else
257  {
258  // the component with user focus ( current document )
259  xComp = xDesktop->getCurrentComponent();
260  }
261 
262  if( xComp.is() )
263  {
264  uno::Reference< document::XLinkTargetSupplier > xLTS( xComp, uno::UNO_QUERY );
265 
266  if( xLTS.is() )
267  {
268  if( FillTree( xLTS->getLinks() ) == 0 )
270  }
271  else
273 
274  if ( !aURL.isEmpty() )
275  xComp->dispose();
276  }
277  else
278  {
279  if( !aURL.isEmpty() )
281  }
282  return (mnError==0);
283 }
284 
285 // Fill Tree-Control
286 int SvxHlinkDlgMarkWnd::FillTree( const uno::Reference< container::XNameAccess >& xLinks, const weld::TreeIter* pParentEntry )
287 {
288  // used to create the Headings outline parent children tree view relation
289  std::stack<std::pair<std::unique_ptr<weld::TreeIter>, const sal_Int32>> aHeadingsParentEntryStack;
290 
291  int nEntries=0;
292  const uno::Sequence< OUString > aNames( xLinks->getElementNames() );
293  const sal_Int32 nLinks = aNames.getLength();
294  const OUString* pNames = aNames.getConstArray();
295 
296  static const OUStringLiteral aProp_LinkDisplayName( u"LinkDisplayName" );
297  static const OUStringLiteral aProp_LinkTarget( u"com.sun.star.document.LinkTarget" );
298  static const OUStringLiteral aProp_LinkDisplayBitmap( u"LinkDisplayBitmap" );
299  for( sal_Int32 i = 0; i < nLinks; i++ )
300  {
301  uno::Any aAny;
302  OUString aLink( *pNames++ );
303 
304  bool bError = false;
305  try
306  {
307  aAny = xLinks->getByName( aLink );
308  }
309  catch(const uno::Exception&)
310  {
311  // if the name of the target was invalid (like empty headings)
312  // no object can be provided
313  bError = true;
314  }
315  if(bError)
316  continue;
317 
318  uno::Reference< beans::XPropertySet > xTarget;
319 
320  if( aAny >>= xTarget )
321  {
322  try
323  {
324  // get name to display
325  aAny = xTarget->getPropertyValue( aProp_LinkDisplayName );
326  OUString aDisplayName;
327  aAny >>= aDisplayName;
328  OUString aStrDisplayname ( aDisplayName );
329 
330  // is it a target ?
331  uno::Reference< lang::XServiceInfo > xSI( xTarget, uno::UNO_QUERY );
332  bool bIsTarget = xSI->supportsService( aProp_LinkTarget );
333 
334  // create userdata
335  TargetData *pData = new TargetData ( aLink, bIsTarget );
336  OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pData)));
337 
338  std::unique_ptr<weld::TreeIter> xEntry(mxLbTree->make_iterator());
339  if (pParentEntry)
340  {
341  OUString sContentType = mxLbTree->get_text(*pParentEntry);
342  if (sContentType == "Headings")
343  {
344  if (aHeadingsParentEntryStack.empty())
345  aHeadingsParentEntryStack.push(
346  std::pair(mxLbTree->make_iterator(pParentEntry), -1));
347 
348  // get the headings name to display
349  aAny = xTarget->getPropertyValue("ActualOutlineName");
350  OUString sActualOutlineName;
351  aAny >>= sActualOutlineName;
352 
353  // get the headings outline level
354  aAny = xTarget->getPropertyValue("OutlineLevel");
355  sal_Int32 nOutlineLevel;
356  aAny >>= nOutlineLevel;
357 
358  // pop until the top of stack entry has an outline level less than
359  // the to be inserted heading outline level
360  while (nOutlineLevel <= aHeadingsParentEntryStack.top().second)
361  aHeadingsParentEntryStack.pop();
362 
363  mxLbTree->insert(aHeadingsParentEntryStack.top().first.get(), -1,
364  &sActualOutlineName, &sId, nullptr, nullptr, false,
365  xEntry.get());
366 
367  // push if the inserted entry is a child
368  if (nOutlineLevel > aHeadingsParentEntryStack.top().second)
369  aHeadingsParentEntryStack.push(
370  std::pair(std::move(xEntry), nOutlineLevel));
371  }
372  else
373  {
374  mxLbTree->insert(pParentEntry, -1, &aStrDisplayname, &sId, nullptr,
375  nullptr, false, xEntry.get());
376  }
377  }
378  else
379  {
380  mxLbTree->insert(pParentEntry, -1, &aStrDisplayname, &sId, nullptr, nullptr,
381  false, xEntry.get());
382  }
383 
384  try
385  {
386  // get bitmap for the tree-entry
387  uno::Reference< awt::XBitmap >
388  aXBitmap( xTarget->getPropertyValue( aProp_LinkDisplayBitmap ), uno::UNO_QUERY );
389  if (aXBitmap.is())
390  {
391  Graphic aBmp(Graphic(VCLUnoHelper::GetBitmap(aXBitmap)));
392  // insert Displayname into treelist with bitmaps
393  mxLbTree->set_image(*xEntry, aBmp.GetXGraphic(), -1);
394  }
395  }
396  catch(const css::uno::Exception&)
397  {
398  }
399 
400  nEntries++;
401 
402  uno::Reference< document::XLinkTargetSupplier > xLTS( xTarget, uno::UNO_QUERY );
403  if( xLTS.is() )
404  nEntries += FillTree( xLTS->getLinks(), xEntry.get() );
405  }
406  catch(const css::uno::Exception&)
407  {
408  }
409  }
410  }
411 
412  return nEntries;
413 }
414 
415 // Clear Tree
417 {
418  std::unique_ptr<weld::TreeIter> xEntry = mxLbTree->make_iterator();
419  bool bEntry = mxLbTree->get_iter_first(*xEntry);
420 
421  while (bEntry)
422  {
423  TargetData* pUserData = reinterpret_cast<TargetData*>(mxLbTree->get_id(*xEntry).toInt64());
424  delete pUserData;
425 
426  bEntry = mxLbTree->iter_next(*xEntry);
427  }
428 
429  mxLbTree->clear();
430 }
431 
432 // Find Entry for String
433 std::unique_ptr<weld::TreeIter> SvxHlinkDlgMarkWnd::FindEntry (std::u16string_view aStrName)
434 {
435  bool bFound=false;
436  std::unique_ptr<weld::TreeIter> xEntry = mxLbTree->make_iterator();
437  bool bEntry = mxLbTree->get_iter_first(*xEntry);
438 
439  while (bEntry && !bFound)
440  {
441  TargetData* pUserData = reinterpret_cast<TargetData*>(mxLbTree->get_id(*xEntry).toInt64());
442  if (aStrName == pUserData->aUStrLinkname)
443  bFound = true;
444  else
445  bEntry = mxLbTree->iter_next(*xEntry);
446  }
447 
448  if (!bFound)
449  xEntry.reset();
450 
451  return xEntry;
452 }
453 
454 // Select Entry
455 bool SvxHlinkDlgMarkWnd::SelectEntry(std::u16string_view aStrMark)
456 {
457  std::unique_ptr<weld::TreeIter> xEntry = FindEntry(aStrMark);
458  if (!xEntry)
459  return false;
460  mxLbTree->select(*xEntry);
461  mxLbTree->scroll_to_row(*xEntry);
462  return true;
463 }
464 
465 // Click on Apply-Button / Double-click on item in tree
466 IMPL_LINK_NOARG(SvxHlinkDlgMarkWnd, DoubleClickApplyHdl_Impl, weld::TreeView&, bool)
467 {
468  ClickApplyHdl_Impl(*mxBtApply);
469  return true;
470 }
471 
472 IMPL_LINK_NOARG(SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl, weld::Button&, void)
473 {
474  std::unique_ptr<weld::TreeIter> xEntry(mxLbTree->make_iterator());
475  bool bEntry = mxLbTree->get_cursor(xEntry.get());
476  if (bEntry)
477  {
478  TargetData* pData = reinterpret_cast<TargetData*>(mxLbTree->get_id(*xEntry).toInt64());
479  if (pData->bIsTarget)
480  {
481  mpParent->SetMarkStr(pData->aUStrLinkname);
482  }
483  }
484 }
485 
486 // Click on Close-Button
487 IMPL_LINK_NOARG(SvxHlinkDlgMarkWnd, ClickCloseHdl_Impl, weld::Button&, void)
488 {
489  std::unique_ptr<weld::TreeIter> xEntry(mxLbTree->make_iterator());
490  bool bEntry = mxLbTree->get_cursor(xEntry.get());
491  if (bEntry)
492  {
493  TargetData* pUserData = reinterpret_cast<TargetData*>(mxLbTree->get_id(*xEntry).toInt64());
494  OUString sLastSelectedMark = pUserData->aUStrLinkname;
495 
496  std::deque<OUString> aLastSelectedPath;
497  //If the bottommost entry is expanded but nothing
498  //underneath it is selected leave a dummy entry
499  if (mxLbTree->get_row_expanded(*xEntry))
500  aLastSelectedPath.push_front(OUString());
501  while (bEntry)
502  {
503  aLastSelectedPath.push_front(mxLbTree->get_text(*xEntry));
504  bEntry = mxLbTree->iter_parent(*xEntry);
505  }
506 
507  uno::Sequence< beans::NamedValue > aSettings
508  {
509  { TG_SETTING_LASTMARK, css::uno::Any(sLastSelectedMark) },
510  { TG_SETTING_LASTPATH, css::uno::Any(comphelper::containerToSequence(aLastSelectedPath)) }
511  };
512 
513  // write
514  SvtViewOptions aViewSettings( EViewType::Dialog, TG_SETTING_MANAGER );
515  aViewSettings.SetUserData( aSettings );
516  }
517 
518  m_xDialog->response(RET_CANCEL);
519 }
520 
521 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< weld::Dialog > m_xDialog
std::unique_ptr< weld::Button > mxBtApply
Definition: hlmarkwn.hxx:37
std::unique_ptr< ContentProperties > pData
int FillTree(const css::uno::Reference< css::container::XNameAccess > &xLinks, const weld::TreeIter *pParentEntry=nullptr)
Definition: hlmarkwn.cxx:286
virtual ~SvxHlinkDlgMarkWnd() override
Definition: hlmarkwn.cxx:82
Reference< XInterface > xTarget
#define LERR_NOENTRIES
void MoveTo(const Point &rNewPos)
Definition: hlmarkwn.cxx:125
std::unique_ptr< weld::TreeIter > FindEntry(std::u16string_view aStrName)
Definition: hlmarkwn.cxx:433
RET_CANCEL
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
void RefreshTree(const OUString &aStrURL)
Definition: hlmarkwn.cxx:198
#define TG_SETTING_MANAGER
Definition: hlmarkwn.cxx:160
#define LERR_DOCNOTOPEN
static BitmapEx GetBitmap(const css::uno::Reference< css::awt::XBitmap > &rxBitmap)
void RestoreLastSelection()
Definition: hlmarkwn.cxx:164
#define LERR_NOERROR
sal_uInt16 SetError(sal_uInt16 nError)
Definition: hlmarkwn.cxx:111
IMPL_LINK_NOARG(SvxHlinkDlgMarkWnd, DoubleClickApplyHdl_Impl, weld::TreeView &, bool)
Definition: hlmarkwn.cxx:466
int i
bool RefreshFromDoc(const OUString &aURL)
Definition: hlmarkwn.cxx:227
void SetUserData(const css::uno::Sequence< css::beans::NamedValue > &lData)
float u
virtual OUString get_text(int row, int col=-1) const =0
virtual void expand_row(const TreeIter &rIter)=0
OUString CuiResId(const char *pKey)
Definition: cuiresmgr.cxx:23
virtual bool iter_children(TreeIter &rIter) const =0
#define TG_SETTING_LASTPATH
Definition: hlmarkwn.cxx:162
RegionData_Impl * mpParent
std::unique_ptr< weld::Label > mxError
Definition: hlmarkwn.hxx:40
virtual void select(int pos)=0
bool SelectEntry(std::u16string_view aStrMark)
Definition: hlmarkwn.cxx:455
sal_uInt16 mnError
Definition: hlmarkwn.hxx:35
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
#define TG_SETTING_LASTMARK
Definition: hlmarkwn.cxx:161
bool Exists() const
Reference< XExecutableDialog > m_xDialog
Reference< XComponentContext > getProcessComponentContext()
css::uno::Any GetUserItem(const OUString &sName) const
SvxHlinkDlgMarkWnd(weld::Window *pParentDialog, SvxHyperlinkTabPageBase *pParentPage)
Definition: hlmarkwn.cxx:66
std::unique_ptr< weld::Button > mxBtClose
Definition: hlmarkwn.hxx:38
OUString sId
virtual bool iter_next_sibling(TreeIter &rIter) const =0
Tabpage : Basisclass.
Definition: hltpbase.hxx:46
std::unique_ptr< weld::TreeView > mxLbTree
Definition: hlmarkwn.hxx:39
sal_uInt16 nPos
virtual void scroll_to_row(int row)=0