Update ooo320-m1
[ooovba.git] / svx / source / cui / hlmarkwn.cxx
blobe65fbb228d3d6199394a5ee92a09d927a6e6f461
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: hlmarkwn.cxx,v $
10 * $Revision: 1.19.216.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #ifdef SVX_DLLIMPLEMENTATION
35 #undef SVX_DLLIMPLEMENTATION
36 #endif
37 #include <vcl/wrkwin.hxx>
38 #include <svx/dialmgr.hxx>
39 #include <sfx2/docfile.hxx>
40 #include <vcl/svapp.hxx>
41 #include <vcl/settings.hxx>
43 // UNO-Stuff
44 #include <comphelper/processfactory.hxx>
45 #include <com/sun/star/awt/XBitmap.hpp>
46 #include <com/sun/star/frame/XDesktop.hpp>
47 #include <com/sun/star/frame/XDesktop.hpp>
48 #include <com/sun/star/frame/XComponentLoader.hpp>
49 #include <com/sun/star/beans/PropertyValue.hpp>
50 #include <com/sun/star/document/XLinkTargetSupplier.hpp>
51 #include <com/sun/star/beans/XPropertySet.hpp>
53 #include <toolkit/unohlp.hxx>
55 #include <svx/dialogs.hrc>
56 #include "hlmarkwn.hrc"
57 #include "hlmarkwn.hxx"
58 #include "hltpbase.hxx"
60 using namespace ::com::sun::star;
61 using namespace ::rtl;
63 /*************************************************************************
65 |* Userdata-struct for tree-entries
67 |************************************************************************/
69 struct TargetData
71 OUString aUStrLinkname;
72 BOOL bIsTarget;
74 TargetData ( OUString aUStrLName, BOOL bTarget )
75 : bIsTarget ( bTarget )
77 if ( bIsTarget )
78 aUStrLinkname = aUStrLName;
83 //########################################################################
84 //# #
85 //# Tree-Window #
86 //# #
87 //########################################################################
89 SvxHlmarkTreeLBox::SvxHlmarkTreeLBox( Window* pParent, const ResId& rResId )
90 : SvTreeListBox ( pParent, rResId ),
91 mpParentWnd ( (SvxHlinkDlgMarkWnd*) pParent )
93 SetNodeDefaultImages();
96 void SvxHlmarkTreeLBox::Paint( const Rectangle& rRect )
98 if( mpParentWnd->mnError == LERR_NOERROR )
100 SvTreeListBox::Paint(rRect);
102 else
104 Erase();
106 Rectangle aDrawRect( Point( 0, 0 ), GetSizePixel() );
108 String aStrMessage;
110 switch( mpParentWnd->mnError )
112 case LERR_NOENTRIES :
113 aStrMessage = SVX_RESSTR( RID_SVXSTR_HYPDLG_ERR_LERR_NOENTRIES );
114 break;
115 case LERR_DOCNOTOPEN :
116 aStrMessage = SVX_RESSTR( RID_SVXSTR_HYPDLG_ERR_LERR_DOCNOTOPEN );
117 break;
120 DrawText( aDrawRect, aStrMessage, TEXT_DRAW_LEFT | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
125 //########################################################################
126 //# #
127 //# Window-Class #
128 //# #
129 //########################################################################
131 /*************************************************************************
133 |* Contructor / Destructor
135 |************************************************************************/
137 SvxHlinkDlgMarkWnd::SvxHlinkDlgMarkWnd( SvxHyperlinkTabPageBase *pParent )
138 : ModalDialog( (Window*)pParent, SVX_RES ( RID_SVXFLOAT_HYPERLINK_MARKWND ) ),
139 maBtApply( this, SVX_RES (BT_APPLY) ),
140 maBtClose( this, SVX_RES (BT_CLOSE) ),
141 maLbTree ( this, SVX_RES (TLB_MARK) ),
142 mbUserMoved ( FALSE ),
143 mbFirst ( TRUE ),
144 mpParent ( pParent ),
145 mnError ( LERR_NOERROR )
147 FreeResource();
149 maBtApply.SetClickHdl ( LINK ( this, SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl ) );
150 maBtClose.SetClickHdl ( LINK ( this, SvxHlinkDlgMarkWnd, ClickCloseHdl_Impl ) );
151 maLbTree.SetDoubleClickHdl ( LINK ( this, SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl ) );
153 // Tree-ListBox mit Linien versehen
154 maLbTree.SetWindowBits( WinBits( WB_TABSTOP | WB_BORDER | WB_HASLINES |
155 WB_HASBUTTONS | //WB_HASLINESATROOT |
156 WB_HSCROLL | WB_HASBUTTONSATROOT ) );
159 SvxHlinkDlgMarkWnd::~SvxHlinkDlgMarkWnd()
161 ClearTree();
164 /*************************************************************************
166 |* Set an errorstatus
168 |************************************************************************/
170 USHORT SvxHlinkDlgMarkWnd::SetError( USHORT nError)
172 USHORT nOldError = mnError;
173 mnError = nError;
175 if( mnError != LERR_NOERROR )
176 ClearTree();
178 maLbTree.Invalidate();
180 return nOldError;
183 /*************************************************************************
185 |* Move window
187 |************************************************************************/
189 BOOL SvxHlinkDlgMarkWnd::MoveTo ( Point aNewPos )
191 if ( !mbUserMoved )
193 BOOL bOldStatus = mbUserMoved;
194 SetPosPixel ( aNewPos );
195 mbUserMoved = bOldStatus;
198 return mbUserMoved;
201 void SvxHlinkDlgMarkWnd::Move ()
203 Window::Move();
205 if ( IsReallyVisible() )
206 mbUserMoved = TRUE;
209 BOOL SvxHlinkDlgMarkWnd::ConnectToDialog( BOOL bDoit )
211 BOOL bOldStatus = mbUserMoved;
213 mbUserMoved = !bDoit;
215 return bOldStatus;
218 /*************************************************************************
220 |* Interface to refresh tree
222 |************************************************************************/
224 void SvxHlinkDlgMarkWnd::RefreshTree ( String aStrURL )
226 String aEmptyStr;
227 OUString aUStrURL;
229 EnterWait();
231 ClearTree();
233 xub_StrLen nPos = aStrURL.Search ( sal_Unicode('#') );
235 if( nPos != 0 )
236 aUStrURL = ::rtl::OUString( aStrURL );
238 if( !RefreshFromDoc ( aUStrURL ) )
239 maLbTree.Invalidate();
241 if ( nPos != STRING_NOTFOUND )
243 String aStrMark = aStrURL.Copy ( nPos+1 );
244 SelectEntry ( aStrMark );
247 LeaveWait();
249 maStrLastURL = aStrURL;
252 /*************************************************************************
254 |* get links from document
256 |************************************************************************/
258 BOOL SvxHlinkDlgMarkWnd::RefreshFromDoc( OUString aURL )
260 mnError = LERR_NOERROR;
262 uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
263 if( xFactory.is() )
265 uno::Reference< frame::XDesktop > xDesktop( xFactory->createInstance( OUString::createFromAscii( "com.sun.star.frame.Desktop" ) ),
266 uno::UNO_QUERY );
267 if( xDesktop.is() )
269 uno::Reference< lang::XComponent > xComp;
271 if( aURL.getLength() )
273 // load from url
274 uno::Reference< frame::XComponentLoader > xLoader( xDesktop, uno::UNO_QUERY );
275 if( xLoader.is() )
279 uno::Sequence< beans::PropertyValue > aArg(1);
280 aArg.getArray()[0].Name = OUString::createFromAscii( "Hidden" );
281 aArg.getArray()[0].Value <<= (sal_Bool) TRUE;
282 xComp = xLoader->loadComponentFromURL( aURL, OUString::createFromAscii( "_blank" ), 0, aArg );
284 catch( const io::IOException& )
288 catch( const lang::IllegalArgumentException& )
294 else
296 // the component with user focus ( current document )
297 xComp = xDesktop->getCurrentComponent();
300 if( xComp.is() )
302 uno::Reference< document::XLinkTargetSupplier > xLTS( xComp, uno::UNO_QUERY );
304 if( xLTS.is() )
306 if( FillTree( xLTS->getLinks() ) == 0 )
307 mnError = LERR_NOENTRIES;
309 else
310 mnError = LERR_DOCNOTOPEN;
312 if ( aURL.getLength() )
313 xComp->dispose();
315 else
317 if( aURL.getLength() )
318 mnError=LERR_DOCNOTOPEN;
322 return (mnError==0);
325 void SvxHlinkDlgMarkWnd::Error(int nNr)
327 switch(nNr)
329 case 0:
331 Rectangle aDrawRect( Point( 0, 0 ), maLbTree.GetSizePixel() );
332 //maLbTree.SetTextColor( Color(COL_BLACK) );
333 //maLbTree.DrawText( aDrawRect, "Keine Ziele im Dokument vorhanden.", TEXT_DRAW_LEFT);// | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
334 maLbTree.DrawText( Point(0,0), "Keine Ziele im Dokument vorhanden.");
335 maLbTree.DrawLine(aDrawRect.TopLeft(), aDrawRect.BottomRight() );
337 break;
338 case 1:
339 Rectangle aDrawRect( Point( 0, 0 ), maLbTree.GetSizePixel() );
340 maLbTree.DrawText( aDrawRect, "Das Dokument konnte nicht ge�ffnet werden.", TEXT_DRAW_LEFT | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
341 break;
345 /*************************************************************************
347 |* Fill Tree-Control
349 |************************************************************************/
351 int SvxHlinkDlgMarkWnd::FillTree( uno::Reference< container::XNameAccess > xLinks, SvLBoxEntry* pParentEntry )
353 int nEntries=0;
354 const uno::Sequence< OUString > aNames( xLinks->getElementNames() );
355 const ULONG nLinks = aNames.getLength();
356 const OUString* pNames = aNames.getConstArray();
358 Color aMaskColor( COL_LIGHTMAGENTA );
359 const OUString aProp_LinkDisplayName( RTL_CONSTASCII_USTRINGPARAM( "LinkDisplayName" ) );
360 const OUString aProp_LinkTarget( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.LinkTarget" ) );
361 const OUString aProp_LinkDisplayBitmap( RTL_CONSTASCII_USTRINGPARAM( "LinkDisplayBitmap" ) );
362 for( ULONG i = 0; i < nLinks; i++ )
364 uno::Any aAny;
365 OUString aLink( *pNames++ );
367 BOOL bError = FALSE;
370 aAny = xLinks->getByName( aLink );
372 catch(const uno::Exception&)
374 // if the name of the target was invalid (like empty headings)
375 // no object can be provided
376 bError = TRUE;
378 if(bError)
379 continue;
381 uno::Reference< beans::XPropertySet > xTarget;
383 if( aAny >>= xTarget )
387 // get name to display
388 aAny = xTarget->getPropertyValue( aProp_LinkDisplayName );
389 OUString aDisplayName;
390 aAny >>= aDisplayName;
391 String aStrDisplayname ( aDisplayName );
393 // is it a target ?
394 uno::Reference< lang::XServiceInfo > xSI( xTarget, uno::UNO_QUERY );
395 BOOL bIsTarget = xSI->supportsService( aProp_LinkTarget );
397 // create userdata
398 TargetData *pData = new TargetData ( aLink, bIsTarget );
400 SvLBoxEntry* pEntry;
404 // get bitmap for the tree-entry
405 uno::Reference< awt::XBitmap > aXBitmap( xTarget->getPropertyValue( aProp_LinkDisplayBitmap ), uno::UNO_QUERY );
406 if( aXBitmap.is() )
408 Image aBmp( VCLUnoHelper::GetBitmap( aXBitmap ).GetBitmap(), aMaskColor );
409 // insert Displayname into treelist with bitmaps
410 pEntry = maLbTree.InsertEntry ( aStrDisplayname,
411 aBmp, aBmp,
412 pParentEntry,
413 FALSE, LIST_APPEND,
414 (void*)pData );
415 maLbTree.SetExpandedEntryBmp( pEntry, aBmp, BMP_COLOR_HIGHCONTRAST );
416 maLbTree.SetCollapsedEntryBmp( pEntry, aBmp, BMP_COLOR_HIGHCONTRAST );
417 nEntries++;
419 else
421 // insert Displayname into treelist without bitmaps
422 pEntry = maLbTree.InsertEntry ( aStrDisplayname,
423 pParentEntry,
424 FALSE, LIST_APPEND,
425 (void*)pData );
426 nEntries++;
429 catch(const com::sun::star::uno::Exception&)
431 // insert Displayname into treelist without bitmaps
432 pEntry = maLbTree.InsertEntry ( aStrDisplayname,
433 pParentEntry,
434 FALSE, LIST_APPEND,
435 (void*)pData );
436 nEntries++;
439 uno::Reference< document::XLinkTargetSupplier > xLTS( xTarget, uno::UNO_QUERY );
440 if( xLTS.is() )
441 nEntries += FillTree( xLTS->getLinks(), pEntry );
443 catch(const com::sun::star::uno::Exception&)
449 return nEntries;
452 /*************************************************************************
454 |* Clear Tree
456 |************************************************************************/
458 void SvxHlinkDlgMarkWnd::ClearTree()
460 SvLBoxEntry* pEntry = maLbTree.First();
462 while ( pEntry )
464 TargetData* pUserData = ( TargetData * ) pEntry->GetUserData();
465 delete pUserData;
467 pEntry = maLbTree.Next( pEntry );
470 maLbTree.Clear();
473 /*************************************************************************
475 |* Find Entry for Strng
477 |************************************************************************/
479 SvLBoxEntry* SvxHlinkDlgMarkWnd::FindEntry ( String aStrName )
481 BOOL bFound=FALSE;
482 SvLBoxEntry* pEntry = maLbTree.First();
484 while ( pEntry && !bFound )
486 TargetData* pUserData = ( TargetData * ) pEntry->GetUserData ();
487 if ( aStrName == String( pUserData->aUStrLinkname ) )
488 bFound = TRUE;
489 else
490 pEntry = maLbTree.Next( pEntry );
493 return pEntry;
496 /*************************************************************************
498 |* Select Entry
500 |************************************************************************/
502 void SvxHlinkDlgMarkWnd::SelectEntry ( String aStrMark )
504 SvLBoxEntry* pEntry = FindEntry ( aStrMark );
505 if ( pEntry )
507 maLbTree.Select ( pEntry );
508 maLbTree.MakeVisible ( pEntry );
512 /*************************************************************************
514 |* Click on Apply-Button / Doubleclick on item in tree
516 |************************************************************************/
518 IMPL_LINK ( SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl, void *, EMPTYARG )
520 SvLBoxEntry* pEntry = maLbTree.GetCurEntry();
522 if ( pEntry )
524 TargetData *pData = ( TargetData * )pEntry->GetUserData();
526 if ( pData->bIsTarget )
528 String aStrMark ( pData->aUStrLinkname );
529 mpParent->SetMarkStr ( aStrMark );
533 return( 0L );
536 /*************************************************************************
538 |* Click on Close-Button
540 |************************************************************************/
542 IMPL_LINK ( SvxHlinkDlgMarkWnd, ClickCloseHdl_Impl, void *, EMPTYARG )
544 Close();
546 return( 0L );