update credits
[LibreOffice.git] / cui / source / dialogs / hlmarkwn.cxx
blob343eb9cbbbe1fd9beae8727d84364e6ed9ea3f71
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 <vcl/wrkwin.hxx>
21 #include <dialmgr.hxx>
22 #include <sfx2/docfile.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/settings.hxx>
26 // UNO-Stuff
27 #include <comphelper/processfactory.hxx>
28 #include <com/sun/star/awt/XBitmap.hpp>
29 #include <com/sun/star/frame/Desktop.hpp>
30 #include <com/sun/star/frame/XComponentLoader.hpp>
31 #include <com/sun/star/beans/PropertyValue.hpp>
32 #include <com/sun/star/document/XLinkTargetSupplier.hpp>
33 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <toolkit/unohlp.hxx>
36 #include "svtools/treelistentry.hxx"
38 #include <cuires.hrc>
39 #include "hlmarkwn.hrc"
40 #include "hlmarkwn.hxx"
41 #include "hltpbase.hxx"
43 using namespace ::com::sun::star;
44 using namespace ::rtl;
46 /*************************************************************************
48 |* Userdata-struct for tree-entries
50 |************************************************************************/
52 struct TargetData
54 OUString aUStrLinkname;
55 sal_Bool bIsTarget;
57 TargetData ( OUString aUStrLName, sal_Bool bTarget )
58 : bIsTarget ( bTarget )
60 if ( bIsTarget )
61 aUStrLinkname = aUStrLName;
66 //########################################################################
67 //# #
68 //# Tree-Window #
69 //# #
70 //########################################################################
72 SvxHlmarkTreeLBox::SvxHlmarkTreeLBox( Window* pParent, const ResId& rResId )
73 : SvTreeListBox ( pParent, rResId ),
74 mpParentWnd ( (SvxHlinkDlgMarkWnd*) pParent )
76 SetNodeDefaultImages();
79 void SvxHlmarkTreeLBox::Paint( const Rectangle& rRect )
81 if( mpParentWnd->mnError == LERR_NOERROR )
83 SvTreeListBox::Paint(rRect);
85 else
87 Erase();
89 Rectangle aDrawRect( Point( 0, 0 ), GetSizePixel() );
91 String aStrMessage;
93 switch( mpParentWnd->mnError )
95 case LERR_NOENTRIES :
96 aStrMessage = CUI_RESSTR( RID_SVXSTR_HYPDLG_ERR_LERR_NOENTRIES );
97 break;
98 case LERR_DOCNOTOPEN :
99 aStrMessage = CUI_RESSTR( RID_SVXSTR_HYPDLG_ERR_LERR_DOCNOTOPEN );
100 break;
103 DrawText( aDrawRect, aStrMessage, TEXT_DRAW_LEFT | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
108 //########################################################################
109 //# #
110 //# Window-Class #
111 //# #
112 //########################################################################
114 /*************************************************************************
116 |* Contructor / Destructor
118 |************************************************************************/
120 SvxHlinkDlgMarkWnd::SvxHlinkDlgMarkWnd( SvxHyperlinkTabPageBase *pParent )
121 : ModalDialog( (Window*)pParent, CUI_RES ( RID_SVXFLOAT_HYPERLINK_MARKWND ) ),
122 maBtApply( this, CUI_RES (BT_APPLY) ),
123 maBtClose( this, CUI_RES (BT_CLOSE) ),
124 maLbTree ( this, CUI_RES (TLB_MARK) ),
125 mbUserMoved ( sal_False ),
126 mpParent ( pParent ),
127 mnError ( LERR_NOERROR )
129 FreeResource();
131 maBtApply.SetClickHdl ( LINK ( this, SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl ) );
132 maBtClose.SetClickHdl ( LINK ( this, SvxHlinkDlgMarkWnd, ClickCloseHdl_Impl ) );
133 maLbTree.SetDoubleClickHdl ( LINK ( this, SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl ) );
135 // add lines to the Tree-ListBox
136 maLbTree.SetStyle( maLbTree.GetStyle() | WB_TABSTOP | WB_BORDER | WB_HASLINES |
137 WB_HASBUTTONS | //WB_HASLINESATROOT |
138 WB_HSCROLL | WB_HASBUTTONSATROOT );
140 maLbTree.SetAccessibleName(String(CUI_RES(STR_MARK_TREE)));
144 SvxHlinkDlgMarkWnd::~SvxHlinkDlgMarkWnd()
146 ClearTree();
149 /*************************************************************************
151 |* Set an errorstatus
153 |************************************************************************/
155 sal_uInt16 SvxHlinkDlgMarkWnd::SetError( sal_uInt16 nError)
157 sal_uInt16 nOldError = mnError;
158 mnError = nError;
160 if( mnError != LERR_NOERROR )
161 ClearTree();
163 maLbTree.Invalidate();
165 return nOldError;
168 /*************************************************************************
170 |* Move window
172 |************************************************************************/
174 sal_Bool SvxHlinkDlgMarkWnd::MoveTo ( Point aNewPos )
176 if ( !mbUserMoved )
178 sal_Bool bOldStatus = mbUserMoved;
179 SetPosPixel ( aNewPos );
180 mbUserMoved = bOldStatus;
183 return mbUserMoved;
186 void SvxHlinkDlgMarkWnd::Move ()
188 Window::Move();
190 if ( IsReallyVisible() )
191 mbUserMoved = sal_True;
194 sal_Bool SvxHlinkDlgMarkWnd::ConnectToDialog( sal_Bool bDoit )
196 sal_Bool bOldStatus = mbUserMoved;
198 mbUserMoved = !bDoit;
200 return bOldStatus;
203 /*************************************************************************
205 |* Interface to refresh tree
207 |************************************************************************/
209 void SvxHlinkDlgMarkWnd::RefreshTree ( String aStrURL )
211 OUString aUStrURL;
213 EnterWait();
215 ClearTree();
217 xub_StrLen nPos = aStrURL.Search ( sal_Unicode('#') );
219 if( nPos != 0 )
220 aUStrURL = OUString( aStrURL );
222 if( !RefreshFromDoc ( aUStrURL ) )
223 maLbTree.Invalidate();
225 if ( nPos != STRING_NOTFOUND )
227 String aStrMark = aStrURL.Copy ( nPos+1 );
228 SelectEntry ( aStrMark );
231 LeaveWait();
233 maStrLastURL = aStrURL;
236 /*************************************************************************
238 |* get links from document
240 |************************************************************************/
242 sal_Bool SvxHlinkDlgMarkWnd::RefreshFromDoc( OUString aURL )
244 mnError = LERR_NOERROR;
246 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create( ::comphelper::getProcessComponentContext() );
247 uno::Reference< lang::XComponent > xComp;
249 if( !aURL.isEmpty() )
251 // load from url
252 uno::Reference< frame::XComponentLoader > xLoader( xDesktop, uno::UNO_QUERY );
253 if( xLoader.is() )
257 uno::Sequence< beans::PropertyValue > aArg(1);
258 aArg.getArray()[0].Name = OUString( "Hidden" );
259 aArg.getArray()[0].Value <<= (sal_Bool) sal_True;
260 xComp = xLoader->loadComponentFromURL( aURL, OUString( "_blank" ), 0, aArg );
262 catch( const io::IOException& )
266 catch( const lang::IllegalArgumentException& )
272 else
274 // the component with user focus ( current document )
275 xComp = xDesktop->getCurrentComponent();
278 if( xComp.is() )
280 uno::Reference< document::XLinkTargetSupplier > xLTS( xComp, uno::UNO_QUERY );
282 if( xLTS.is() )
284 if( FillTree( xLTS->getLinks() ) == 0 )
285 mnError = LERR_NOENTRIES;
287 else
288 mnError = LERR_DOCNOTOPEN;
290 if ( !aURL.isEmpty() )
291 xComp->dispose();
293 else
295 if( !aURL.isEmpty() )
296 mnError=LERR_DOCNOTOPEN;
298 return (mnError==0);
300 /*************************************************************************
302 |* Fill Tree-Control
304 |************************************************************************/
306 int SvxHlinkDlgMarkWnd::FillTree( uno::Reference< container::XNameAccess > xLinks, SvTreeListEntry* pParentEntry )
308 int nEntries=0;
309 const uno::Sequence< OUString > aNames( xLinks->getElementNames() );
310 const sal_uLong nLinks = aNames.getLength();
311 const OUString* pNames = aNames.getConstArray();
313 Color aMaskColor( COL_LIGHTMAGENTA );
314 const OUString aProp_LinkDisplayName( "LinkDisplayName" );
315 const OUString aProp_LinkTarget( "com.sun.star.document.LinkTarget" );
316 const OUString aProp_LinkDisplayBitmap( "LinkDisplayBitmap" );
317 for( sal_uLong i = 0; i < nLinks; i++ )
319 uno::Any aAny;
320 OUString aLink( *pNames++ );
322 sal_Bool bError = sal_False;
325 aAny = xLinks->getByName( aLink );
327 catch(const uno::Exception&)
329 // if the name of the target was invalid (like empty headings)
330 // no object can be provided
331 bError = sal_True;
333 if(bError)
334 continue;
336 uno::Reference< beans::XPropertySet > xTarget;
338 if( aAny >>= xTarget )
342 // get name to display
343 aAny = xTarget->getPropertyValue( aProp_LinkDisplayName );
344 OUString aDisplayName;
345 aAny >>= aDisplayName;
346 String aStrDisplayname ( aDisplayName );
348 // is it a target ?
349 uno::Reference< lang::XServiceInfo > xSI( xTarget, uno::UNO_QUERY );
350 sal_Bool bIsTarget = xSI->supportsService( aProp_LinkTarget );
352 // create userdata
353 TargetData *pData = new TargetData ( aLink, bIsTarget );
355 SvTreeListEntry* pEntry;
359 // get bitmap for the tree-entry
360 uno::Reference< awt::XBitmap > aXBitmap( xTarget->getPropertyValue( aProp_LinkDisplayBitmap ), uno::UNO_QUERY );
361 if( aXBitmap.is() )
363 Image aBmp( VCLUnoHelper::GetBitmap( aXBitmap ).GetBitmap(), aMaskColor );
364 // insert Displayname into treelist with bitmaps
365 pEntry = maLbTree.InsertEntry ( aStrDisplayname,
366 aBmp, aBmp,
367 pParentEntry,
368 sal_False, LIST_APPEND,
369 (void*)pData );
370 nEntries++;
372 else
374 // insert Displayname into treelist without bitmaps
375 pEntry = maLbTree.InsertEntry ( aStrDisplayname,
376 pParentEntry,
377 sal_False, LIST_APPEND,
378 (void*)pData );
379 nEntries++;
382 catch(const com::sun::star::uno::Exception&)
384 // insert Displayname into treelist without bitmaps
385 pEntry = maLbTree.InsertEntry ( aStrDisplayname,
386 pParentEntry,
387 sal_False, LIST_APPEND,
388 (void*)pData );
389 nEntries++;
392 uno::Reference< document::XLinkTargetSupplier > xLTS( xTarget, uno::UNO_QUERY );
393 if( xLTS.is() )
394 nEntries += FillTree( xLTS->getLinks(), pEntry );
396 catch(const com::sun::star::uno::Exception&)
402 return nEntries;
405 /*************************************************************************
407 |* Clear Tree
409 |************************************************************************/
411 void SvxHlinkDlgMarkWnd::ClearTree()
413 SvTreeListEntry* pEntry = maLbTree.First();
415 while ( pEntry )
417 TargetData* pUserData = ( TargetData * ) pEntry->GetUserData();
418 delete pUserData;
420 pEntry = maLbTree.Next( pEntry );
423 maLbTree.Clear();
426 /*************************************************************************
428 |* Find Entry for Strng
430 |************************************************************************/
432 SvTreeListEntry* SvxHlinkDlgMarkWnd::FindEntry ( String aStrName )
434 sal_Bool bFound=sal_False;
435 SvTreeListEntry* pEntry = maLbTree.First();
437 while ( pEntry && !bFound )
439 TargetData* pUserData = ( TargetData * ) pEntry->GetUserData ();
440 if ( aStrName == String( pUserData->aUStrLinkname ) )
441 bFound = sal_True;
442 else
443 pEntry = maLbTree.Next( pEntry );
446 return pEntry;
449 /*************************************************************************
451 |* Select Entry
453 |************************************************************************/
455 void SvxHlinkDlgMarkWnd::SelectEntry ( String aStrMark )
457 SvTreeListEntry* pEntry = FindEntry ( aStrMark );
458 if ( pEntry )
460 maLbTree.Select ( pEntry );
461 maLbTree.MakeVisible ( pEntry );
465 /*************************************************************************
467 |* Click on Apply-Button / Doubleclick on item in tree
469 |************************************************************************/
471 IMPL_LINK_NOARG(SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl)
473 SvTreeListEntry* pEntry = maLbTree.GetCurEntry();
475 if ( pEntry )
477 TargetData *pData = ( TargetData * )pEntry->GetUserData();
479 if ( pData->bIsTarget )
481 String aStrMark ( pData->aUStrLinkname );
482 mpParent->SetMarkStr ( aStrMark );
486 return( 0L );
489 /*************************************************************************
491 |* Click on Close-Button
493 |************************************************************************/
495 IMPL_LINK_NOARG(SvxHlinkDlgMarkWnd, ClickCloseHdl_Impl)
497 Close();
499 return( 0L );
503 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */