xref: /aoo41x/main/cui/source/dialogs/hlmarkwn.cxx (revision 2ee96f1c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_cui.hxx"
26 
27 #include <vcl/wrkwin.hxx>
28 #include <dialmgr.hxx>
29 #include <sfx2/docfile.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/settings.hxx>
32 
33 // UNO-Stuff
34 #include <comphelper/processfactory.hxx>
35 #include <com/sun/star/awt/XBitmap.hpp>
36 #include <com/sun/star/frame/XDesktop.hpp>
37 #include <com/sun/star/frame/XDesktop.hpp>
38 #include <com/sun/star/frame/XComponentLoader.hpp>
39 #include <com/sun/star/beans/PropertyValue.hpp>
40 #include <com/sun/star/document/XLinkTargetSupplier.hpp>
41 #include <com/sun/star/beans/XPropertySet.hpp>
42 
43 #include <toolkit/unohlp.hxx>
44 
45 #include <cuires.hrc>
46 #include "hlmarkwn.hrc"
47 #include "hlmarkwn.hxx"
48 #include "hltpbase.hxx"
49 
50 using namespace ::com::sun::star;
51 using namespace ::rtl;
52 
53 /*************************************************************************
54 |*
55 |* Userdata-struct for tree-entries
56 |*
57 |************************************************************************/
58 
59 struct TargetData
60 {
61 	OUString		aUStrLinkname;
62 	sal_Bool		bIsTarget;
63 
TargetDataTargetData64 	TargetData ( OUString aUStrLName, sal_Bool bTarget )
65 		:	bIsTarget ( bTarget )
66 	{
67 		if ( bIsTarget )
68 			aUStrLinkname = aUStrLName;
69 	}
70 };
71 
72 
73 //########################################################################
74 //#                                                                      #
75 //# Tree-Window 														 #
76 //#                                                                      #
77 //########################################################################
78 
SvxHlmarkTreeLBox(Window * pParent,const ResId & rResId)79 SvxHlmarkTreeLBox::SvxHlmarkTreeLBox( Window* pParent, const ResId& rResId )
80 : SvTreeListBox	( pParent, rResId ),
81   mpParentWnd	( (SvxHlinkDlgMarkWnd*) pParent )
82 {
83 	SetNodeDefaultImages();
84 }
85 
Paint(const Rectangle & rRect)86 void SvxHlmarkTreeLBox::Paint( const Rectangle& rRect )
87 {
88 	if( mpParentWnd->mnError == LERR_NOERROR )
89 	{
90 		SvTreeListBox::Paint(rRect);
91 	}
92 	else
93 	{
94 		Erase();
95 
96 		Rectangle aDrawRect( Point( 0, 0 ), GetSizePixel() );
97 
98 		String aStrMessage;
99 
100 		switch( mpParentWnd->mnError )
101 		{
102 		case LERR_NOENTRIES :
103 			aStrMessage = CUI_RESSTR( RID_SVXSTR_HYPDLG_ERR_LERR_NOENTRIES );
104 			break;
105 		case LERR_DOCNOTOPEN :
106 			aStrMessage = CUI_RESSTR( RID_SVXSTR_HYPDLG_ERR_LERR_DOCNOTOPEN );
107 			break;
108 		}
109 
110 		DrawText( aDrawRect, aStrMessage, TEXT_DRAW_LEFT | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
111 	}
112 
113 }
114 
115 //########################################################################
116 //#                                                                      #
117 //# Window-Class														 #
118 //#                                                                      #
119 //########################################################################
120 
121 /*************************************************************************
122 |*
123 |* Contructor / Destructor
124 |*
125 |************************************************************************/
126 
SvxHlinkDlgMarkWnd(SvxHyperlinkTabPageBase * pParent)127 SvxHlinkDlgMarkWnd::SvxHlinkDlgMarkWnd( SvxHyperlinkTabPageBase *pParent )
128 :	ModalDialog( (Window*)pParent, CUI_RES ( RID_SVXFLOAT_HYPERLINK_MARKWND ) ),
129 	maBtApply( this, CUI_RES (BT_APPLY) ),
130 	maBtClose( this, CUI_RES (BT_CLOSE) ),
131 	maLbTree ( this, CUI_RES (TLB_MARK) ),
132 	mbUserMoved ( sal_False ),
133 	mbFirst	    ( sal_True ),
134 	mpParent	( pParent ),
135 	mnError		( LERR_NOERROR )
136 {
137 	FreeResource();
138 
139 	maBtApply.SetClickHdl		( LINK ( this, SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl ) );
140 	maBtClose.SetClickHdl		( LINK ( this, SvxHlinkDlgMarkWnd, ClickCloseHdl_Impl ) );
141 	maLbTree.SetDoubleClickHdl	( LINK ( this, SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl ) );
142 
143 	// Tree-ListBox mit Linien versehen
144 	maLbTree.SetStyle( maLbTree.GetStyle() | WB_TABSTOP | WB_BORDER | WB_HASLINES |
145 							WB_HASBUTTONS |  //WB_HASLINESATROOT |
146 							WB_HSCROLL | WB_HASBUTTONSATROOT );
147 
148     maLbTree.SetAccessibleName(String(CUI_RES(STR_MARK_TREE)));
149 
150 }
151 
~SvxHlinkDlgMarkWnd()152 SvxHlinkDlgMarkWnd::~SvxHlinkDlgMarkWnd()
153 {
154 	ClearTree();
155 }
156 
157 /*************************************************************************
158 |*
159 |* Set an errorstatus
160 |*
161 |************************************************************************/
162 
SetError(sal_uInt16 nError)163 sal_uInt16 SvxHlinkDlgMarkWnd::SetError( sal_uInt16 nError)
164 {
165 	sal_uInt16 nOldError = mnError;
166 	mnError = nError;
167 
168 	if( mnError != LERR_NOERROR )
169 		ClearTree();
170 
171 	maLbTree.Invalidate();
172 
173 	return nOldError;
174 }
175 
176 /*************************************************************************
177 |*
178 |* Move window
179 |*
180 |************************************************************************/
181 
MoveTo(Point aNewPos)182 sal_Bool SvxHlinkDlgMarkWnd::MoveTo ( Point aNewPos )
183 {
184 	if ( !mbUserMoved )
185 	{
186 		sal_Bool bOldStatus = mbUserMoved;
187 		SetPosPixel ( aNewPos );
188 		mbUserMoved = bOldStatus;
189 	}
190 
191 	return mbUserMoved;
192 }
193 
Move()194 void SvxHlinkDlgMarkWnd::Move ()
195 {
196 	Window::Move();
197 
198 	if ( IsReallyVisible() )
199 		mbUserMoved = sal_True;
200 }
201 
ConnectToDialog(sal_Bool bDoit)202 sal_Bool SvxHlinkDlgMarkWnd::ConnectToDialog( sal_Bool bDoit )
203 {
204 	sal_Bool bOldStatus = mbUserMoved;
205 
206 	mbUserMoved = !bDoit;
207 
208 	return bOldStatus;
209 }
210 
211 /*************************************************************************
212 |*
213 |* Interface to refresh tree
214 |*
215 |************************************************************************/
216 
RefreshTree(String aStrURL)217 void SvxHlinkDlgMarkWnd::RefreshTree ( String aStrURL )
218 {
219     String aEmptyStr;
220 	OUString aUStrURL;
221 
222 	EnterWait();
223 
224 	ClearTree();
225 
226 	xub_StrLen nPos = aStrURL.Search ( sal_Unicode('#') );
227 
228 	if( nPos != 0 )
229 		aUStrURL = ::rtl::OUString( aStrURL );
230 
231 	if( !RefreshFromDoc ( aUStrURL ) )
232 		maLbTree.Invalidate();
233 
234 	if ( nPos != STRING_NOTFOUND )
235 	{
236 		String aStrMark = aStrURL.Copy ( nPos+1 );
237 		SelectEntry ( aStrMark );
238 	}
239 
240 	LeaveWait();
241 
242 	maStrLastURL = aStrURL;
243 }
244 
245 /*************************************************************************
246 |*
247 |* get links from document
248 |*
249 |************************************************************************/
250 
RefreshFromDoc(OUString aURL)251 sal_Bool SvxHlinkDlgMarkWnd::RefreshFromDoc( OUString aURL )
252 {
253 	mnError = LERR_NOERROR;
254 
255 	uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
256 	if( xFactory.is() )
257 	{
258 		uno::Reference< frame::XDesktop > xDesktop( xFactory->createInstance( OUString::createFromAscii( "com.sun.star.frame.Desktop" ) ),
259 					uno::UNO_QUERY );
260 		if( xDesktop.is() )
261 		{
262 			uno::Reference< lang::XComponent > xComp;
263 
264 			if( aURL.getLength() )
265 			{
266 				// load from url
267 				uno::Reference< frame::XComponentLoader > xLoader( xDesktop, uno::UNO_QUERY );
268 				if( xLoader.is() )
269 				{
270 					try
271 					{
272 						uno::Sequence< beans::PropertyValue > aArg(1);
273 						aArg.getArray()[0].Name = OUString::createFromAscii( "Hidden" );
274 						aArg.getArray()[0].Value <<= (sal_Bool) sal_True;
275 						xComp = xLoader->loadComponentFromURL( aURL, OUString::createFromAscii( "_blank" ), 0, aArg );
276 					}
277 					catch( const io::IOException& )
278 					{
279 
280 					}
281 					catch( const lang::IllegalArgumentException& )
282 					{
283 
284 					}
285 				}
286 			}
287 			else
288 			{
289 				// the component with user focus ( current document )
290 				xComp = xDesktop->getCurrentComponent();
291 			}
292 
293 			if( xComp.is() )
294 			{
295 				uno::Reference< document::XLinkTargetSupplier > xLTS( xComp, uno::UNO_QUERY );
296 
297 				if( xLTS.is() )
298 				{
299 					if( FillTree( xLTS->getLinks() ) == 0 )
300 						mnError = LERR_NOENTRIES;
301 				}
302 				else
303 					mnError = LERR_DOCNOTOPEN;
304 
305 				if ( aURL.getLength() )
306 					xComp->dispose();
307 			}
308 			else
309 			{
310 				if( aURL.getLength() )
311 					mnError=LERR_DOCNOTOPEN;
312 			}
313 		}
314 	}
315 	return (mnError==0);
316 }
317 /*
318 void SvxHlinkDlgMarkWnd::Error(int nNr)
319 {
320 	switch(nNr)
321 	{
322 		case 0:
323 		{
324 			Rectangle aDrawRect( Point( 0, 0 ), maLbTree.GetSizePixel() );
325 			//maLbTree.SetTextColor( Color(COL_BLACK) );
326 			//maLbTree.DrawText( aDrawRect, "Keine Ziele im Dokument vorhanden.", TEXT_DRAW_LEFT);// | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
327 			maLbTree.DrawText( Point(0,0), "Keine Ziele im Dokument vorhanden.");
328 			maLbTree.DrawLine(aDrawRect.TopLeft(), aDrawRect.BottomRight() );
329 		}
330 		break;
331 		case 1:
332 			Rectangle aDrawRect( Point( 0, 0 ), maLbTree.GetSizePixel() );
333 			maLbTree.DrawText( aDrawRect, "Das Dokument konnte nicht ge�ffnet werden.", TEXT_DRAW_LEFT | TEXT_DRAW_MULTILINE | TEXT_DRAW_WORDBREAK );
334 			break;
335 	}
336 }
337 */
338 /*************************************************************************
339 |*
340 |* Fill Tree-Control
341 |*
342 |************************************************************************/
343 
FillTree(uno::Reference<container::XNameAccess> xLinks,SvLBoxEntry * pParentEntry)344 int SvxHlinkDlgMarkWnd::FillTree( uno::Reference< container::XNameAccess > xLinks, SvLBoxEntry* pParentEntry )
345 {
346 	int nEntries=0;
347 	const uno::Sequence< OUString > aNames( xLinks->getElementNames() );
348 	const sal_uLong nLinks = aNames.getLength();
349 	const OUString* pNames = aNames.getConstArray();
350 
351     Color aMaskColor( COL_LIGHTMAGENTA );
352 	const OUString aProp_LinkDisplayName( RTL_CONSTASCII_USTRINGPARAM( "LinkDisplayName" ) );
353 	const OUString aProp_LinkTarget( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.LinkTarget" ) );
354 	const OUString aProp_LinkDisplayBitmap( RTL_CONSTASCII_USTRINGPARAM( "LinkDisplayBitmap" ) );
355 	for( sal_uLong i = 0; i < nLinks; i++ )
356 	{
357 		uno::Any aAny;
358 		OUString aLink( *pNames++ );
359 
360 		sal_Bool bError = sal_False;
361 		try
362 		{
363 			aAny = xLinks->getByName( aLink );
364 		}
365 		catch(const uno::Exception&)
366 		{
367 			// if the name of the target was invalid (like empty headings)
368 			// no object can be provided
369 			bError = sal_True;
370 		}
371 		if(bError)
372 			continue;
373 
374 		uno::Reference< beans::XPropertySet > xTarget;
375 
376 		if( aAny >>= xTarget )
377 		{
378 			try
379 			{
380 				// get name to display
381 				aAny = xTarget->getPropertyValue( aProp_LinkDisplayName );
382 				OUString aDisplayName;
383 				aAny >>= aDisplayName;
384 				String aStrDisplayname ( aDisplayName );
385 
386 				// is it a target ?
387 				uno::Reference< lang::XServiceInfo > xSI( xTarget, uno::UNO_QUERY );
388 				sal_Bool bIsTarget = xSI->supportsService( aProp_LinkTarget );
389 
390 				// create userdata
391 				TargetData *pData = new TargetData ( aLink, bIsTarget );
392 
393 				SvLBoxEntry* pEntry;
394 
395 				try
396 				{
397 					// get bitmap for the tree-entry
398 					uno::Reference< awt::XBitmap > aXBitmap( xTarget->getPropertyValue( aProp_LinkDisplayBitmap ), uno::UNO_QUERY );
399 					if( aXBitmap.is() )
400 					{
401                         Image aBmp( VCLUnoHelper::GetBitmap( aXBitmap ).GetBitmap(), aMaskColor );
402                         // insert Displayname into treelist with bitmaps
403 						pEntry = maLbTree.InsertEntry ( aStrDisplayname,
404                                                         aBmp, aBmp,
405 														pParentEntry,
406 														sal_False, LIST_APPEND,
407 														(void*)pData );
408                         maLbTree.SetExpandedEntryBmp( pEntry, aBmp, BMP_COLOR_HIGHCONTRAST );
409                         maLbTree.SetCollapsedEntryBmp( pEntry, aBmp, BMP_COLOR_HIGHCONTRAST );
410                         nEntries++;
411 					}
412 					else
413 					{
414 						// insert Displayname into treelist without bitmaps
415 						pEntry = maLbTree.InsertEntry ( aStrDisplayname,
416 														pParentEntry,
417 														sal_False, LIST_APPEND,
418 														(void*)pData );
419 						nEntries++;
420 					}
421 				}
422 				catch(const com::sun::star::uno::Exception&)
423 				{
424 					// insert Displayname into treelist without bitmaps
425 					pEntry = maLbTree.InsertEntry ( aStrDisplayname,
426 													pParentEntry,
427 													sal_False, LIST_APPEND,
428 													(void*)pData );
429 					nEntries++;
430 				}
431 
432 				uno::Reference< document::XLinkTargetSupplier > xLTS( xTarget, uno::UNO_QUERY );
433 				if( xLTS.is() )
434 					nEntries += FillTree( xLTS->getLinks(), pEntry );
435 			}
436 			catch(const com::sun::star::uno::Exception&)
437 			{
438 			}
439 		}
440 	}
441 
442 	return nEntries;
443 }
444 
445 /*************************************************************************
446 |*
447 |* Clear Tree
448 |*
449 |************************************************************************/
450 
ClearTree()451 void SvxHlinkDlgMarkWnd::ClearTree()
452 {
453 	SvLBoxEntry* pEntry = maLbTree.First();
454 
455 	while ( pEntry )
456 	{
457 		TargetData* pUserData = ( TargetData * ) pEntry->GetUserData();
458 		delete pUserData;
459 
460 		pEntry = maLbTree.Next( pEntry );
461 	}
462 
463 	maLbTree.Clear();
464 }
465 
466 /*************************************************************************
467 |*
468 |* Find Entry for Strng
469 |*
470 |************************************************************************/
471 
FindEntry(String aStrName)472 SvLBoxEntry* SvxHlinkDlgMarkWnd::FindEntry ( String aStrName )
473 {
474 	sal_Bool bFound=sal_False;
475 	SvLBoxEntry* pEntry = maLbTree.First();
476 
477 	while ( pEntry && !bFound )
478 	{
479 		TargetData* pUserData = ( TargetData * ) pEntry->GetUserData ();
480 		if ( aStrName == String( pUserData->aUStrLinkname ) )
481 			bFound = sal_True;
482 		else
483 			pEntry = maLbTree.Next( pEntry );
484 	}
485 
486 	return pEntry;
487 }
488 
489 /*************************************************************************
490 |*
491 |* Select Entry
492 |*
493 |************************************************************************/
494 
SelectEntry(String aStrMark)495 void SvxHlinkDlgMarkWnd::SelectEntry ( String aStrMark )
496 {
497 	SvLBoxEntry* pEntry = FindEntry ( aStrMark );
498 	if ( pEntry )
499 	{
500 		maLbTree.Select ( pEntry );
501 		maLbTree.MakeVisible ( pEntry );
502 	}
503 }
504 
505 /*************************************************************************
506 |*
507 |* Click on Apply-Button / Doubleclick on item in tree
508 |*
509 |************************************************************************/
510 
IMPL_LINK(SvxHlinkDlgMarkWnd,ClickApplyHdl_Impl,void *,EMPTYARG)511 IMPL_LINK ( SvxHlinkDlgMarkWnd, ClickApplyHdl_Impl, void *, EMPTYARG )
512 {
513 	SvLBoxEntry* pEntry = maLbTree.GetCurEntry();
514 
515 	if ( pEntry )
516 	{
517 		TargetData *pData = ( TargetData * )pEntry->GetUserData();
518 
519 		if ( pData->bIsTarget )
520 		{
521 			String aStrMark ( pData->aUStrLinkname );
522 			mpParent->SetMarkStr ( aStrMark );
523 		}
524 	}
525 
526 	return( 0L );
527 }
528 
529 /*************************************************************************
530 |*
531 |* Click on Close-Button
532 |*
533 |************************************************************************/
534 
IMPL_LINK(SvxHlinkDlgMarkWnd,ClickCloseHdl_Impl,void *,EMPTYARG)535 IMPL_LINK ( SvxHlinkDlgMarkWnd, ClickCloseHdl_Impl, void *, EMPTYARG )
536 {
537     Close();
538 
539 	return( 0L );
540 }
541 
542 
543