1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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"
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 |************************************************************************/
54 OUString aUStrLinkname
;
57 TargetData ( OUString aUStrLName
, sal_Bool bTarget
)
58 : bIsTarget ( bTarget
)
61 aUStrLinkname
= aUStrLName
;
66 //########################################################################
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
);
89 Rectangle
aDrawRect( Point( 0, 0 ), GetSizePixel() );
93 switch( mpParentWnd
->mnError
)
96 aStrMessage
= CUI_RESSTR( RID_SVXSTR_HYPDLG_ERR_LERR_NOENTRIES
);
98 case LERR_DOCNOTOPEN
:
99 aStrMessage
= CUI_RESSTR( RID_SVXSTR_HYPDLG_ERR_LERR_DOCNOTOPEN
);
103 DrawText( aDrawRect
, aStrMessage
, TEXT_DRAW_LEFT
| TEXT_DRAW_MULTILINE
| TEXT_DRAW_WORDBREAK
);
108 //########################################################################
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
)
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()
149 /*************************************************************************
151 |* Set an errorstatus
153 |************************************************************************/
155 sal_uInt16
SvxHlinkDlgMarkWnd::SetError( sal_uInt16 nError
)
157 sal_uInt16 nOldError
= mnError
;
160 if( mnError
!= LERR_NOERROR
)
163 maLbTree
.Invalidate();
168 /*************************************************************************
172 |************************************************************************/
174 sal_Bool
SvxHlinkDlgMarkWnd::MoveTo ( Point aNewPos
)
178 sal_Bool bOldStatus
= mbUserMoved
;
179 SetPosPixel ( aNewPos
);
180 mbUserMoved
= bOldStatus
;
186 void SvxHlinkDlgMarkWnd::Move ()
190 if ( IsReallyVisible() )
191 mbUserMoved
= sal_True
;
194 sal_Bool
SvxHlinkDlgMarkWnd::ConnectToDialog( sal_Bool bDoit
)
196 sal_Bool bOldStatus
= mbUserMoved
;
198 mbUserMoved
= !bDoit
;
203 /*************************************************************************
205 |* Interface to refresh tree
207 |************************************************************************/
209 void SvxHlinkDlgMarkWnd::RefreshTree ( String aStrURL
)
217 xub_StrLen nPos
= aStrURL
.Search ( sal_Unicode('#') );
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
);
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() )
252 uno::Reference
< frame::XComponentLoader
> xLoader( xDesktop
, uno::UNO_QUERY
);
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
& )
274 // the component with user focus ( current document )
275 xComp
= xDesktop
->getCurrentComponent();
280 uno::Reference
< document::XLinkTargetSupplier
> xLTS( xComp
, uno::UNO_QUERY
);
284 if( FillTree( xLTS
->getLinks() ) == 0 )
285 mnError
= LERR_NOENTRIES
;
288 mnError
= LERR_DOCNOTOPEN
;
290 if ( !aURL
.isEmpty() )
295 if( !aURL
.isEmpty() )
296 mnError
=LERR_DOCNOTOPEN
;
300 /*************************************************************************
304 |************************************************************************/
306 int SvxHlinkDlgMarkWnd::FillTree( uno::Reference
< container::XNameAccess
> xLinks
, SvTreeListEntry
* pParentEntry
)
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
++ )
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
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
);
349 uno::Reference
< lang::XServiceInfo
> xSI( xTarget
, uno::UNO_QUERY
);
350 sal_Bool bIsTarget
= xSI
->supportsService( aProp_LinkTarget
);
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
);
363 Image
aBmp( VCLUnoHelper::GetBitmap( aXBitmap
).GetBitmap(), aMaskColor
);
364 // insert Displayname into treelist with bitmaps
365 pEntry
= maLbTree
.InsertEntry ( aStrDisplayname
,
368 sal_False
, LIST_APPEND
,
374 // insert Displayname into treelist without bitmaps
375 pEntry
= maLbTree
.InsertEntry ( aStrDisplayname
,
377 sal_False
, LIST_APPEND
,
382 catch(const com::sun::star::uno::Exception
&)
384 // insert Displayname into treelist without bitmaps
385 pEntry
= maLbTree
.InsertEntry ( aStrDisplayname
,
387 sal_False
, LIST_APPEND
,
392 uno::Reference
< document::XLinkTargetSupplier
> xLTS( xTarget
, uno::UNO_QUERY
);
394 nEntries
+= FillTree( xLTS
->getLinks(), pEntry
);
396 catch(const com::sun::star::uno::Exception
&)
405 /*************************************************************************
409 |************************************************************************/
411 void SvxHlinkDlgMarkWnd::ClearTree()
413 SvTreeListEntry
* pEntry
= maLbTree
.First();
417 TargetData
* pUserData
= ( TargetData
* ) pEntry
->GetUserData();
420 pEntry
= maLbTree
.Next( pEntry
);
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
) )
443 pEntry
= maLbTree
.Next( pEntry
);
449 /*************************************************************************
453 |************************************************************************/
455 void SvxHlinkDlgMarkWnd::SelectEntry ( String aStrMark
)
457 SvTreeListEntry
* pEntry
= FindEntry ( aStrMark
);
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();
477 TargetData
*pData
= ( TargetData
* )pEntry
->GetUserData();
479 if ( pData
->bIsTarget
)
481 String
aStrMark ( pData
->aUStrLinkname
);
482 mpParent
->SetMarkStr ( aStrMark
);
489 /*************************************************************************
491 |* Click on Close-Button
493 |************************************************************************/
495 IMPL_LINK_NOARG(SvxHlinkDlgMarkWnd
, ClickCloseHdl_Impl
)
503 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */