xref: /AOO41X/main/svx/source/dialog/hyprlink.cxx (revision 479f0c2e472461a5ddf1db2522d0b551f4ce9b48)
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_svx.hxx"
26 #include <tools/urlobj.hxx>
27 #include <vcl/msgbox.hxx>
28 #include <unotools/configitem.hxx>
29 #include <unotools/cmdoptions.hxx>
30 #include <svtools/inetimg.hxx>
31 #include <svl/urlbmk.hxx>
32 #include <svl/eitem.hxx>
33 #include <svl/stritem.hxx>
34 #include <sfx2/viewfrm.hxx>
35 #include <sfx2/docfile.hxx>
36 #include <sfx2/imgmgr.hxx>
37 #include <sfx2/dispatch.hxx>
38 #include <svl/urihelper.hxx>
39 #include <sfx2/objsh.hxx>
40 #include <comphelper/processfactory.hxx>
41 
42 #include "svx/hlnkitem.hxx"
43 #include <svx/dialogs.hrc>
44 #include "hyprlink.hrc"
45 #include <svx/dialmgr.hxx>
46 #include "svx/hyprlink.hxx"
47 #include "hyprdlg.hxx"
48 
49 using namespace ::rtl;
50 using namespace ::com::sun::star;
51 
52 // -----------------------------------------------------------------------
53 
54 class SearchDefaultConfigItem_Impl : public ::utl::ConfigItem
55 {
56     OUString    sDefaultEngine;
57 public:
58     SearchDefaultConfigItem_Impl();
59     ~SearchDefaultConfigItem_Impl();
60 
GetDefaultSearchEngine()61     const OUString&    GetDefaultSearchEngine(){ return sDefaultEngine;}
62     virtual void            Notify( const com::sun::star::uno::Sequence< rtl::OUString >& aPropertyNames );
63     virtual void            Commit();
64 };
65 
66 /*-- 11.11.2003 14:20:59---------------------------------------------------
67 
68   -----------------------------------------------------------------------*/
SearchDefaultConfigItem_Impl()69 SearchDefaultConfigItem_Impl::SearchDefaultConfigItem_Impl() :
70         ConfigItem(OUString::createFromAscii("Inet/DefaultSearchEngine"))
71 {
72     uno::Sequence<OUString> aNames(1);
73     aNames.getArray()[0] = OUString::createFromAscii("Name");
74     uno::Sequence< uno::Any > aValues = GetProperties(aNames);
75     aValues.getConstArray()[0] >>= sDefaultEngine;
76 }
77 /*-- 11.11.2003 14:21:00---------------------------------------------------
78 
79   -----------------------------------------------------------------------*/
~SearchDefaultConfigItem_Impl()80 SearchDefaultConfigItem_Impl::~SearchDefaultConfigItem_Impl()
81 {
82 }
83 
Notify(const com::sun::star::uno::Sequence<rtl::OUString> &)84 void SearchDefaultConfigItem_Impl::Notify( const com::sun::star::uno::Sequence< rtl::OUString >& )
85 {
86 }
87 
Commit()88 void SearchDefaultConfigItem_Impl::Commit()
89 {
90 }
91 
92 /*************************************************************************
93 |*
94 |* Dialog zum Einf"ugen/"Andern eines Hyperlink
95 |*
96 \************************************************************************/
97 
SvxHyperlinkDlg(SfxBindings * _pBindings,Window * pParent)98 SvxHyperlinkDlg::SvxHyperlinkDlg( SfxBindings *_pBindings, Window* pParent) :
99 
100     ToolBox             ( pParent, SVX_RES( RID_SVXDLG_HYPERLINK ) ),
101     SfxControllerItem   ( SID_HYPERLINK_SETLINK, *_pBindings ),
102 
103     aForwarder          ( SID_HYPERLINK_GETLINK, *this ),
104     aHyperlinkDlgForward( SID_HYPERLINK_DIALOG , *this),
105 
106     aNameCB             ( this, SVX_RES( CB_NAME ) ),
107     aUrlFT              ( this, SVX_RES( FT_URL ) ),
108     aUrlCB              ( this, SVX_RES( CB_URL ) ),
109     aSearchConfig       (sal_True),
110     sAddress            ( SVX_RES( STR_ADDRESS ) ),
111     sExplorer           ( SVX_RES( STR_EXPLORER ) ),
112     sSearchTitle        ( SVX_RES( STR_BOOKMARK_SEARCH ) ),
113     aLinkPopup          ( SVX_RES( RID_SVXMN_HYPERLINK ) ),
114     pTargetMenu         ( NULL ),
115 
116     bNoDoc              ( sal_True ),
117     bHasOldName         ( sal_False ),
118     bHtmlMode           ( sal_False )
119 
120 {
121     FreeResource();
122 
123     mpManager = SfxImageManager::GetImageManager( 0 );
124     mpManager->RegisterToolBox( this );
125 
126     SetImages();
127 
128     // save initial size
129     nMaxWidth = GetSizePixel().Width();
130     nMaxHeight = GetSizePixel().Height();
131 
132 
133     InsertSeparator( 0 );
134     InsertWindow( CB_URL, &aUrlCB, 0, 0 );
135     // we need an item text for accessibility
136     String sItemText = aUrlCB.GetText();
137     if ( sItemText.Len() == 0 )
138         sItemText = aUrlCB.GetQuickHelpText();
139     if ( sItemText.Len() > 0 )
140     {
141         SetItemText( CB_URL, sItemText );
142         sItemText.Erase();
143     }
144     InsertWindow( FT_URL, &aUrlFT, 0, 0 );
145     InsertSeparator( 0 );
146     InsertWindow( CB_NAME, &aNameCB, 0, 0 );
147     sItemText = aNameCB.GetText();
148     if ( sItemText.Len() == 0 )
149         sItemText = aNameCB.GetQuickHelpText();
150     if ( sItemText.Len() > 0 )
151         SetItemText( CB_NAME, sItemText );
152 
153     SetSizePixel(CalcWindowSizePixel());    // Groesse initialisieren
154 
155     nMaxHeight = GetSizePixel().Height();   // Hoehe nochmal merken, da sie veraendert wurde
156                                             // SetSizePixel ruft Resize-Handler!
157 
158     Show();
159     Resize();
160 
161     long nUrlWidth = aUrlCB.GetResizeWidth();
162     long nNameWidth = aNameCB.GetResizeWidth();
163     long nSum = nUrlWidth + nNameWidth;
164 
165     aUrlCB.SetRatio((nUrlWidth * 100L) / nSum);
166     aNameCB.SetRatio((nNameWidth * 100L) / nSum);
167 
168     SetClickHdl( LINK( this, SvxHyperlinkDlg, TBClickHdl ) );
169     SetSelectHdl( LINK( this, SvxHyperlinkDlg, TBSelectHdl ) );
170     SetDropdownClickHdl( LINK( this, SvxHyperlinkDlg, DropdownClick ) );
171 
172     SetItemBits( BTN_TARGET, GetItemBits( BTN_TARGET ) | TIB_DROPDOWNONLY );
173     SetItemBits( BTN_INET_SEARCH, GetItemBits( BTN_INET_SEARCH ) | TIB_DROPDOWN );
174     SetItemBits( BTN_INET_SEARCH, GetItemBits( BTN_TARGET ) | TIB_DROPDOWNONLY );
175 
176     aLinkPopup.SetSelectHdl(LINK(this, SvxHyperlinkDlg, LinkPopupSelectHdl));
177 
178     Link aLk = LINK(this, SvxHyperlinkDlg, ComboSelectHdl);
179     aNameCB.SetSelectHdl(aLk);
180     aUrlCB.SetSelectHdl(aLk);
181 
182     aLk = LINK(this, SvxHyperlinkDlg, ComboModifyHdl);
183     aNameCB.SetModifyHdl(aLk);
184     aUrlCB.SetModifyHdl(aLk);
185 
186     // Accessibility: Set the quick help text as accessible name for the
187     // drop down lists.
188     aNameCB.SetAccessibleName (aNameCB.GetQuickHelpText());
189     aUrlCB.SetAccessibleName (aUrlCB.GetQuickHelpText());
190 
191     // Hide button according to config item.
192     SvtCommandOptions aCmdOpts;
193     if ( aCmdOpts.Lookup( SvtCommandOptions::CMDOPTION_DISABLED,
194                           rtl::OUString(
195                               RTL_CONSTASCII_USTRINGPARAM(
196                                   "InternetSearch" ) ) ) )
197         HideItem( BTN_INET_SEARCH );
198 }
199 
200 /*--------------------------------------------------------------------
201     Beschreibung:
202  --------------------------------------------------------------------*/
203 
~SvxHyperlinkDlg()204 SvxHyperlinkDlg::~SvxHyperlinkDlg()
205 {
206     SfxImageManager::GetImageManager( 0 )->ReleaseToolBox(this);
207 
208     if (pTargetMenu != NULL)
209         delete pTargetMenu;
210 }
211 
212 /*--------------------------------------------------------------------
213     Beschreibung: Leiste so resizen, dass die Comboboxen automatisch mit
214     verkleinert werden
215  --------------------------------------------------------------------*/
216 
Resize()217 void SvxHyperlinkDlg::Resize()
218 {
219     long nWidth = GetSizePixel().Width();
220 
221     ToolBox::Resize();
222 
223     if (nWidth) // nWidth ist manchmal 0
224     {
225         long nDeltaW = nMaxWidth - nWidth + aUrlCB.LogicToPixel(Size(3, 1)).Width() + 1;
226 
227         long nNewUrlWidth = aUrlCB.CalcResizeWidth(nDeltaW);
228         long nNewNameWidth = aNameCB.CalcResizeWidth(nDeltaW);
229 
230         if (nNewUrlWidth && nNewNameWidth)  // Flackern reduzieren
231         {
232             SetUpdateMode(sal_False);
233 
234             // Comboboxen resizen
235             aUrlCB.DoResize(nNewUrlWidth);
236             aNameCB.DoResize(nNewNameWidth);
237             RecalcItems();  // Alle Elemente neu anordnen
238 
239             SetUpdateMode(sal_True);
240         }
241     }
242 }
243 
244 /*--------------------------------------------------------------------
245     Beschreibung:
246  --------------------------------------------------------------------*/
247 
Resizing(Size & rSize)248 void SvxHyperlinkDlg::Resizing(Size& rSize)
249 {
250     if (rSize.Height() > nMaxHeight)
251         rSize.Height() = nMaxHeight;
252 
253     if (rSize.Width() > nMaxWidth)
254         rSize.Width() = nMaxWidth;
255 
256     ToolBox::Resizing(rSize);
257 }
258 
259 /*--------------------------------------------------------------------
260     Beschreibung: Im Dokument selektierten Hyperlink in Leiste anzeigen
261  --------------------------------------------------------------------*/
262 
StateChanged(sal_uInt16 nSID,SfxItemState eState,const SfxPoolItem * pState)263 void SvxHyperlinkDlg::StateChanged( sal_uInt16 nSID, SfxItemState eState,
264                                                     const SfxPoolItem* pState )
265 {
266     if ( nSID == SID_HYPERLINK_DIALOG )
267     {
268         if( eState != SFX_ITEM_DISABLED)
269         {
270             EnableItem( BTN_OPENDIALOG, sal_True );
271 
272             sal_Bool bItem = sal_False;
273             if ( pState && eState == SFX_ITEM_AVAILABLE )
274                     bItem = ((SfxBoolItem*)pState)->GetValue();
275             SetItemState ( BTN_OPENDIALOG, bItem ? STATE_CHECK : STATE_NOCHECK );
276         }
277         else
278         {
279             SetItemState ( BTN_OPENDIALOG, STATE_NOCHECK );
280             EnableItem( BTN_OPENDIALOG, sal_False );
281         }
282     }
283 
284     if (nSID == SID_HYPERLINK_SETLINK)
285     {
286         if (eState == SFX_ITEM_DISABLED)
287             bNoDoc = sal_True;
288         else
289             bNoDoc = sal_False;
290         EnableItem(BTN_TARGET, !bNoDoc);
291         EnableLink();
292     }
293 
294     if (nSID == SID_HYPERLINK_GETLINK)
295     {
296         if (eState == SFX_ITEM_AVAILABLE)
297         {
298             const SvxHyperlinkItem& rHLnkItem = *((const SvxHyperlinkItem*)pState);
299 
300             sal_uInt16 nNamePos = aNameCB.GetEntryPos(aNameCB.GetText());
301             sal_uInt16 nUrlPos = aUrlCB.GetEntryPos(aUrlCB.GetText());
302             sal_uInt16 nNotFound = COMBOBOX_ENTRY_NOTFOUND;
303 
304             if (!bHasOldName &&
305                 (nNamePos == nNotFound || nUrlPos == nNotFound))
306             {
307                 sOldName = aNameCB.GetText();
308                 bHasOldName = sal_True;
309             }
310             if (rHLnkItem.GetName().Len())
311             {
312                 aNameCB.SetText(rHLnkItem.GetName());
313                 ComboModifyHdl(&aNameCB);
314             }
315             if (rHLnkItem.GetURL().Len() || rHLnkItem.GetName().Len())
316             {
317                 String sUrl = INetURLObject(rHLnkItem.GetURL()).GetURLNoPass();
318                 aUrlCB.SetText(sUrl);
319             }
320             else if (aUrlCB.GetEntryCount())
321             {   // Letzten Eintrag wieder selektieren
322                 aNameCB.SetText(aNameCB.GetEntry(0));
323                 aUrlCB.SetText(aUrlCB.GetEntry(0));
324             }
325 
326             TargetMenu(rHLnkItem.GetTargetFrame(), sal_False);
327             bHtmlMode = (rHLnkItem.GetInsertMode() & HLINK_HTMLMODE) != 0;
328         }
329         else
330             return;
331 
332         ComboModifyHdl(&aUrlCB);
333     }
334 }
335 
336 /*--------------------------------------------------------------------
337     Beschreibung:
338  --------------------------------------------------------------------*/
339 
IMPL_LINK(SvxHyperlinkDlg,TBClickHdl,ToolBox *,pBox)340 IMPL_LINK( SvxHyperlinkDlg, TBClickHdl, ToolBox *, pBox )
341 {
342     switch (pBox->GetCurItemId())
343     {
344         case BTN_LINK:
345         {
346             SendToApp(HLINK_DEFAULT);
347         }
348         break;
349 
350         case BTN_OPENDIALOG:
351         {
352             GetBindings().GetDispatcher()->Execute( SID_HYPERLINK_DIALOG );
353         }
354         break;
355     }
356 
357     return sal_True;
358 }
359 
360 /*--------------------------------------------------------------------
361     Beschreibung:
362  --------------------------------------------------------------------*/
363 
IMPL_LINK(SvxHyperlinkDlg,TBSelectHdl,ToolBox *,pBox)364 IMPL_LINK( SvxHyperlinkDlg, TBSelectHdl, ToolBox *, pBox )
365 {
366     switch (pBox->GetCurItemId())
367     {
368         // Link als Bookmark im Explorer eintragen
369         // Soll erst im Loslassen der Maus gerufen werden, daher im Select-Hdl
370         case BTN_INSERT_BOOKMARK:
371         {
372             String sName = aNameCB.GetText();
373             if ( !sName.Len() )
374                 sName = aUrlCB.GetText();
375 
376             String aBase = GetBindings().GetDispatcher()->GetFrame()->GetObjectShell()->GetMedium()->GetBaseURL();
377             SfxStringItem aName( SID_BOOKMARK_TITLE, sName );
378             SfxStringItem aURL( SID_BOOKMARK_URL,
379                                 URIHelper::SmartRel2Abs( INetURLObject(aBase), aUrlCB.GetText(), URIHelper::GetMaybeFileHdl(), true, false,
380                                                           INetURLObject::WAS_ENCODED,
381                                                           INetURLObject::DECODE_UNAMBIGUOUS ) );
382             GetBindings().GetDispatcher()->Execute(
383                 SID_CREATELINK, SFX_CALLMODE_ASYNCHRON, &aName, &aURL, 0L );
384         }
385         break;
386     }
387 
388     return sal_True;
389 }
390 
IMPL_LINK(SvxHyperlinkDlg,DropdownClick,ToolBox *,pBox)391 IMPL_LINK( SvxHyperlinkDlg, DropdownClick, ToolBox *, pBox )
392 {
393     switch (pBox->GetCurItemId())
394     {
395         case BTN_LINK:
396         {
397             // Link-Popup anstossen
398             EndSelection(); // Vor dem Execute, damit Popup den Focus bekommt
399             aLinkPopup.EnableItem(MN_BUTTON, !bHtmlMode);
400             aLinkPopup.Execute( this, GetItemRect( BTN_LINK ), FLOATWIN_POPUPMODE_DOWN );
401         }
402         break;
403 
404         case BTN_INET_SEARCH:
405         {
406             // Search-Engines per Popup auswaehlen
407             PopupMenu *pMenu = new PopupMenu;
408             pMenu->SetSelectHdl(LINK(this, SvxHyperlinkDlg, SearchPopupSelectHdl));
409             SearchDefaultConfigItem_Impl aDefaultEngine;
410             String sDefault(aDefaultEngine.GetDefaultSearchEngine());
411             sDefault.ToLowerAscii();
412             const bool bHasDefault = sDefault.Len() > 0;
413 
414             sal_uInt16         nCount = aSearchConfig.Count();
415             String sFound;
416             for (sal_uInt16 i = 0; i < nCount; i++)
417             {
418                 const SvxSearchEngineData& rData = aSearchConfig.GetData(i);
419                 //check if it's the configured default search engine
420                 String sTest(rData.sEngineName);
421                 sTest.ToLowerAscii();
422                 bool bIsDefaultEngine = bHasDefault && STRING_NOTFOUND != sTest.Search( sDefault );
423                 //then put it at the top
424                 if(i && bIsDefaultEngine)
425                 {
426                     pMenu->InsertItem( i + 1, rData.sEngineName, 0, 0);
427                     pMenu->InsertSeparator(1);
428                 }
429                 else
430                 {
431                     if (i)
432                         pMenu->InsertSeparator();
433                     pMenu->InsertItem( i + 1, rData.sEngineName);
434                 }
435             }
436             pBox->SetItemDown(BTN_INET_SEARCH, sal_True, sal_True);
437             pMenu->Execute( this, GetItemRect( BTN_INET_SEARCH ), FLOATWIN_POPUPMODE_DOWN );
438             pBox->SetItemDown(BTN_INET_SEARCH, sal_False, sal_True);
439             EndSelection();
440             delete pMenu;
441         }
442         break;
443 
444         case BTN_TARGET:
445         {
446             // Target Frame einstellen
447             TargetMenu(GetSelTarget(), sal_True);
448             EndSelection();
449         }
450         break;
451     }
452 
453     return sal_True;
454 }
455 
456 /*--------------------------------------------------------------------
457     Beschreibung:
458  --------------------------------------------------------------------*/
459 
TargetMenu(const String & rSelEntry,sal_Bool bExecute)460 void SvxHyperlinkDlg::TargetMenu(const String& rSelEntry, sal_Bool bExecute)
461 {
462     if (pTargetMenu && !bExecute)
463     {
464         for (sal_uInt16 i = 1; i <= pTargetMenu->GetItemCount(); i++)
465         {
466             if (pTargetMenu->GetItemText(i) == rSelEntry)
467             {
468                 pTargetMenu->CheckItem(i);
469                 return;
470             }
471         }
472     }
473 
474     SfxViewFrame* pVwFrm = SfxViewFrame::Current();
475 
476     if (pVwFrm) // Alle moeglichen Target Frames zusammensammeln und anzeigen
477     {
478         TargetList aList;
479         pVwFrm->GetTopFrame().GetTargetList(aList);
480 
481         sal_uInt16 nCount = (sal_uInt16)aList.Count();
482         if( nCount )
483         {
484             sal_Bool bChecked = sal_False;
485 
486             if (pTargetMenu != NULL)
487                 delete pTargetMenu;
488 
489             pTargetMenu = new PopupMenu;
490             pTargetMenu->SetMenuFlags( pTargetMenu->GetMenuFlags() |
491                                        MENU_FLAG_NOAUTOMNEMONICS );
492             sal_uInt16 i;
493             for ( i = 0; i < nCount; i++ )
494             {
495                 String sEntry(*aList.GetObject(i));
496                 pTargetMenu->InsertItem(i + 1, sEntry, MIB_RADIOCHECK|MIB_AUTOCHECK);
497 
498                 if (sEntry == rSelEntry)
499                 {
500                     pTargetMenu->CheckItem(i + 1);
501                     bChecked = sal_True;
502                 }
503             }
504             for ( i = nCount; i; i-- )
505                 delete aList.GetObject( i - 1 );
506 
507             if (!bChecked)
508                 pTargetMenu->CheckItem(1);
509 
510             if (bExecute)
511             {
512                 sal_uInt16 nEntry = pTargetMenu->Execute(
513                     this, GetItemRect( BTN_TARGET ), FLOATWIN_POPUPMODE_DOWN );
514                 if ( nEntry )
515                     pTargetMenu->CheckItem( nEntry );
516             }
517         }
518     }
519 }
520 
521 /*--------------------------------------------------------------------
522     Beschreibung:
523  --------------------------------------------------------------------*/
524 
IMPL_LINK(SvxHyperlinkDlg,LinkPopupSelectHdl,Menu *,pMenu)525 IMPL_LINK( SvxHyperlinkDlg, LinkPopupSelectHdl, Menu *, pMenu )
526 {
527     switch (pMenu->GetCurItemId())
528     {
529         case MN_FIELD:  // URL als Hyperlink ins Dok einfuegen
530             SendToApp(HLINK_FIELD);
531             break;
532         case MN_BUTTON: // URL als Button ins Dok einfuegen
533             SendToApp(HLINK_BUTTON);
534             break;
535     }
536 
537     return sal_True;
538 }
539 
540 /*--------------------------------------------------------------------
541     Beschreibung:
542  --------------------------------------------------------------------*/
543 
IMPL_LINK(SvxHyperlinkDlg,SearchPopupSelectHdl,Menu *,pMenu)544 IMPL_LINK( SvxHyperlinkDlg, SearchPopupSelectHdl, Menu *, pMenu )
545 {
546     const SvxSearchEngineData&  rData = aSearchConfig.GetData(pMenu->GetCurItemId() - 1);
547     String sText(aNameCB.GetText());
548     sText.EraseLeadingChars().EraseTrailingChars();
549 
550     rtl::OUString   sPrefix;
551     rtl::OUString   sSuffix;
552     rtl::OUString   sSeparator;
553     sal_Int32       nCaseMatch;
554     sal_Unicode cToken = 0;
555 
556     if (sText.Search(' ') != STRING_NOTFOUND)
557     {
558         sPrefix = rData.sExactPrefix;
559         sSuffix = rData.sExactSuffix;
560         sSeparator = rData.sExactSeparator;
561         nCaseMatch = rData.nExactCaseMatch;
562         cToken = ' ';
563     }
564     else if (sText.Search('+') != STRING_NOTFOUND)
565     {
566         sPrefix = rData.sAndPrefix;
567         sSuffix = rData.sAndSuffix;
568         sSeparator = rData.sAndSeparator;
569         nCaseMatch = rData.nAndCaseMatch;
570         cToken = '+';
571     }
572     else if (sText.Search(',') != STRING_NOTFOUND)
573     {
574         sPrefix = rData.sOrPrefix;
575         sSuffix = rData.sOrSuffix;
576         sSeparator = rData.sOrSeparator;
577         nCaseMatch = rData.nOrCaseMatch;
578         cToken = ',';
579     }
580     else
581     {
582         sPrefix = rData.sExactPrefix;
583         sSuffix = rData.sExactSuffix;
584         nCaseMatch = rData.nExactCaseMatch;
585         cToken = 0;
586     }
587 
588     String sURL = sPrefix;
589     xub_StrLen nTok;
590 
591     if(1 == nCaseMatch)
592         sText.ToUpperAscii();
593     else if(2 == nCaseMatch)
594         sText.ToLowerAscii();
595 
596     if ((nTok = sText.GetTokenCount(cToken)) > 1)
597     {
598         for (sal_uInt16 i = 0; i < nTok; i++)
599         {
600             sURL += sText.GetToken(i, cToken);
601             if(i < nTok -1)
602                 sURL += (String)sSeparator;
603         }
604         sURL += (String)sSuffix;
605     }
606     else
607     {
608         sURL += sText;
609         sURL += (String)sSuffix;
610     }
611     sURL.EraseAllChars();   // remove all spaces
612     SfxViewFrame* pViewFrame = SfxViewFrame::Current();
613     if ( pViewFrame )
614         pViewFrame = pViewFrame->GetTopViewFrame();
615     OpenDoc( sURL, pViewFrame );
616 
617     return sal_True;
618 }
619 
620 /*--------------------------------------------------------------------
621     Beschreibung:
622  --------------------------------------------------------------------*/
623 
IMPL_LINK(SvxHyperlinkDlg,BookmarkFoundHdl,String *,pUrl)624 IMPL_LINK( SvxHyperlinkDlg, BookmarkFoundHdl, String *, pUrl )
625 {
626     if (pUrl && pUrl->Len())
627     {
628         aUrlCB.SetText(*pUrl);
629         ComboModifyHdl(&aUrlCB);
630     }
631 
632     return sal_True;
633 }
634 /*--------------------------------------------------------------------
635     Beschreibung: Link-Button enablen/disablen
636  --------------------------------------------------------------------*/
637 
EnableLink()638 void SvxHyperlinkDlg::EnableLink()
639 {
640     sal_Bool bEnable = aUrlCB.GetText().Len() != 0;
641 
642     EnableItem(BTN_LINK, (!bNoDoc) & bEnable);
643 }
644 
645 /*--------------------------------------------------------------------
646     Beschreibung: URL im Dokument einfuegen
647  --------------------------------------------------------------------*/
648 
SendToApp(sal_uInt16 nType)649 void SvxHyperlinkDlg::SendToApp(sal_uInt16 nType)
650 {
651     static bool bAlreadyWorking(false);
652 
653     if(bAlreadyWorking)
654     {
655         OSL_ENSURE(false, "SvxHyperlinkDlg::SendToApp called to potentially run recursively, please check (!)");
656         return;
657     }
658 
659     bAlreadyWorking = true;
660     sal_Bool bIsFile = sal_False;
661     String sURL( aUrlCB.GetText() );
662 
663     if ( !sURL.Len() )
664     {
665         bAlreadyWorking = false;
666         return;
667     }
668 
669     String aBase = GetBindings().GetDispatcher()->GetFrame()->GetObjectShell()->GetMedium()->GetBaseURL();
670     INetURLObject aObj( URIHelper::SmartRel2Abs( INetURLObject(aBase), sURL, URIHelper::GetMaybeFileHdl(), true, false,
671                                                   INetURLObject::WAS_ENCODED,
672                                                   INetURLObject::DECODE_UNAMBIGUOUS ) );
673     sURL = aObj.GetMainURL( INetURLObject::NO_DECODE );
674     if ( aObj.GetProtocol() == INET_PROT_FILE )
675         bIsFile = sal_True;
676 
677     if ( bIsFile )
678     {
679         EnterWait();
680         SfxMedium aMedium( sURL, STREAM_STD_READ, sal_True );
681         if ( aMedium.Exists( sal_False ) == sal_False )
682         {
683             LeaveWait();
684             QueryBox aBox( this, SVX_RES( RID_SVXQB_DONTEXIST ) );
685             if ( aBox.Execute() == RET_NO )
686             {
687                 bAlreadyWorking = false;
688                 return;
689             }
690         }
691         else
692             LeaveWait();
693     }
694 
695     SvxHyperlinkItem aItem( SID_HYPERLINK_SETLINK );
696 
697     if (aNameCB.GetText().Len())
698         aItem.SetName( aNameCB.GetText() );
699     else
700         aItem.SetName( sURL );
701 
702     sOldName = aNameCB.GetText();
703 
704     aItem.SetURL(sURL);
705     aItem.SetInsertMode( (SvxLinkInsertMode)nType );
706     aItem.SetTargetFrame( GetSelTarget() );
707 
708     GetBindings().GetDispatcher()->Execute(
709         SID_HYPERLINK_SETLINK, SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD, &aItem, 0L );
710 
711     AddToHistory( aNameCB.GetText(), sURL );
712 
713     if ( sURL != aUrlCB.GetText() )
714         aUrlCB.SetText( sURL );
715 
716     bAlreadyWorking = false;
717 }
718 
719 /*--------------------------------------------------------------------
720     Beschreibung: Selektierten Target Frame Eintrag im Popup ermitteln
721  --------------------------------------------------------------------*/
722 
GetSelTarget()723 String SvxHyperlinkDlg::GetSelTarget()
724 {
725     String sTarget;
726 
727     if (pTargetMenu != NULL)
728     {
729         for (sal_uInt16 i = 1; i <= pTargetMenu->GetItemCount(); i++)
730         {
731             if (pTargetMenu->IsItemChecked(i))
732             {
733                 sTarget = pTargetMenu->GetItemText(i);
734                 break;
735             }
736         }
737     }
738 
739     return sTarget;
740 }
741 
742 /*--------------------------------------------------------------------
743     Beschreibung: URL in History der Leiste aufnehmen
744  --------------------------------------------------------------------*/
745 
AddToHistory(const String & rName,const String & rURL)746 void SvxHyperlinkDlg::AddToHistory(const String& rName, const String& rURL)
747 {
748     String sName(rName);
749 
750     if (bHasOldName && sOldName.Len())
751     {
752         sName = sOldName;
753         bHasOldName = sal_False;
754     }
755 
756     if (!sName.Len())
757         sName = rURL;
758 
759     if (rURL.Len())
760     {
761         sal_uInt16 nNamePos = aNameCB.GetEntryPos(sName);
762         sal_uInt16 nUrlPos = aUrlCB.GetEntryPos(rURL);
763         sal_uInt16 nPos = COMBOBOX_ENTRY_NOTFOUND;
764 
765         if (nNamePos != COMBOBOX_ENTRY_NOTFOUND)
766             nPos = nNamePos;
767         else
768             nPos = nUrlPos;
769 
770         // Alten Eintrag durch neuen Eintrag ersetzen
771         if (nPos != COMBOBOX_ENTRY_NOTFOUND)
772         {
773             aNameCB.RemoveEntry(nPos);
774             aUrlCB.RemoveEntry(nPos);
775             aNameCB.SetText(rName);
776             aUrlCB.SetText(rURL);
777         }
778 
779         aNameCB.InsertEntry(sName, 0);
780         aUrlCB.InsertEntry(rURL, 0);
781     }
782 }
783 
784 /*--------------------------------------------------------------------
785     Beschreibung: Bookmark und Search-Button enablen/disablen
786  --------------------------------------------------------------------*/
787 
IMPL_LINK(SvxHyperlinkDlg,ComboSelectHdl,ComboBox *,pCombo)788 IMPL_LINK( SvxHyperlinkDlg, ComboSelectHdl, ComboBox *, pCombo )
789 {
790     sal_uInt16 nPos = pCombo->GetEntryPos(pCombo->GetText());
791 
792     if (nPos != COMBOBOX_ENTRY_NOTFOUND)
793     {
794         aNameCB.SetText(aNameCB.GetEntry(nPos));
795         aUrlCB.SetText(aUrlCB.GetEntry(nPos));
796 
797         EnableLink();
798         EnableItem(BTN_INSERT_BOOKMARK, sal_True);
799         EnableItem(BTN_INET_SEARCH, sal_True);
800     }
801     return sal_True;
802 }
803 
804 /*--------------------------------------------------------------------
805     Beschreibung:
806  --------------------------------------------------------------------*/
807 
IMPL_LINK(SvxHyperlinkDlg,ComboModifyHdl,ComboBox *,pCombo)808 IMPL_LINK( SvxHyperlinkDlg, ComboModifyHdl, ComboBox *, pCombo )
809 {
810     sal_Bool bEnable = sal_True;
811 
812     if (!pCombo->GetText().Len())
813         bEnable = sal_False;
814 
815     if (pCombo == &aNameCB)
816     {
817         if (IsItemEnabled(BTN_INET_SEARCH) != bEnable)
818             EnableItem(BTN_INET_SEARCH, bEnable);
819     }
820 
821     EnableLink();
822 
823     if (aUrlCB.GetText().Len())
824         bEnable = sal_True;
825     else
826         bEnable = sal_False;
827 
828     if (IsItemEnabled(BTN_INSERT_BOOKMARK) != bEnable)
829         EnableItem(BTN_INSERT_BOOKMARK, bEnable);
830 
831     return sal_True;
832 }
833 
834 // --------------------------------------------------------------------
835 
DataChanged(const DataChangedEvent & rDCEvt)836 void SvxHyperlinkDlg::DataChanged( const DataChangedEvent& rDCEvt )
837 {
838     ToolBox::DataChanged( rDCEvt );
839 
840     if ( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_STYLE))
841         SetImages();
842 }
843 
844 // --------------------------------------------------------------------
845 
SetImages()846 void SvxHyperlinkDlg::SetImages()
847 {
848     bool bHighContrast = GetSettings().GetStyleSettings().GetHighContrastMode();
849 
850     SetItemImage( BTN_LINK, mpManager->GetImage( BTN_LINK, bHighContrast ) );
851     SetItemImage( BTN_INSERT_BOOKMARK, mpManager->GetImage( BTN_INSERT_BOOKMARK, bHighContrast ) );
852     SetItemImage( BTN_INET_SEARCH, mpManager->GetImage( BTN_INET_SEARCH, bHighContrast ) );
853     SetItemImage( BTN_TARGET, mpManager->GetImage( BTN_TARGET, bHighContrast ) );
854     SetItemImage( BTN_OPENDIALOG, mpManager->GetImage( BTN_OPENDIALOG, bHighContrast ) );
855 }
856 
857 /*--------------------------------------------------------------------
858     Beschreibung: Comboboxen
859  --------------------------------------------------------------------*/
860 
HyperCombo(SvxHyperlinkDlg * pDialog,const ResId & rResId)861 HyperCombo::HyperCombo( SvxHyperlinkDlg* pDialog, const ResId& rResId ) :
862         ComboBox( (Window *)pDialog, rResId )
863 {
864     pDlg = pDialog;
865 
866     nMaxWidth = GetSizePixel().Width();
867     if (this == &pDlg->aUrlCB)
868         nMinWidth = GetTextWidth(String::CreateFromAscii("http://www.stardiv.dewww"));
869     else
870         nMinWidth = GetTextWidth(String::CreateFromAscii("StarDivision GmbHwww"));
871 }
872 
873 /*--------------------------------------------------------------------
874     Beschreibung: Comboboxen KeyInput
875  --------------------------------------------------------------------*/
876 
Notify(NotifyEvent & rNEvt)877 long HyperCombo::Notify( NotifyEvent& rNEvt )
878 {
879     long nHandled = 0;
880     static sal_Bool bLocked = sal_False;
881 
882     if (bLocked)    // Keine weiteren RETURNs annehmen (nicht Reentrant!)
883         return nHandled;
884 
885     bLocked = sal_True;
886 
887     if ( rNEvt.GetType() == EVENT_KEYINPUT )
888     {
889         const KeyEvent* pKEvt = rNEvt.GetKeyEvent();
890         const KeyCode aKeyCode = pKEvt->GetKeyCode();
891 
892         if (aKeyCode.GetCode() == KEY_RETURN)
893         {
894             pDlg->SendToApp(HLINK_DEFAULT);
895             nHandled = 1;
896         }
897     }
898     if (this == &pDlg->aNameCB)
899         pDlg->sOldName = GetText();
900 
901     if (!nHandled)
902         nHandled = ComboBox::Notify( rNEvt );
903 
904     bLocked = sal_False;
905 
906     return nHandled;
907 }
908 
909 /*--------------------------------------------------------------------
910     Beschreibung: Drag&Drop vor dem Combo-Edit abfangen und weiterleiten
911  --------------------------------------------------------------------*/
912 
PreNotify(NotifyEvent & rNEvt)913 long HyperCombo::PreNotify( NotifyEvent& rNEvt )
914 {
915     long nHandled = 0;
916 
917     nHandled = ComboBox::PreNotify( rNEvt );
918 
919     return nHandled;
920 }
921 
922 /*--------------------------------------------------------------------
923     Beschreibung:
924  --------------------------------------------------------------------*/
925 
CalcResizeWidth(long nW)926 long HyperCombo::CalcResizeWidth( long nW )
927 {
928     long nNewWidth = Max(nMaxWidth - nW * GetRatio() / 100L, nMinWidth);
929 
930     if (nNewWidth > nMaxWidth)
931         nNewWidth = nMaxWidth;
932 
933     if (nNewWidth != GetSizePixel().Width())
934         return nNewWidth;
935     else
936         return 0;   // Kein Resize notwendig
937 }
938 
939 /*--------------------------------------------------------------------
940     Beschreibung:
941  --------------------------------------------------------------------*/
942 
DoResize(long nNewWidth)943 void HyperCombo::DoResize( long nNewWidth )
944 {
945     SetSizePixel(Size(nNewWidth, GetSizePixel().Height()));
946 }
947 
948 /*--------------------------------------------------------------------
949     Beschreibung: FixedText
950  --------------------------------------------------------------------*/
951 
HyperFixedText(SvxHyperlinkDlg * pDialog,const ResId & rResId)952 HyperFixedText::HyperFixedText( SvxHyperlinkDlg* pDialog, const ResId& rResId ) :
953         FixedInfo( (Window *)pDialog, rResId )
954 {
955     pDlg = pDialog;
956 }
957 
958 /*--------------------------------------------------------------------
959     Beschreibung: Webseite der Search-Engine mit Suchergebnissen anzeigen
960  --------------------------------------------------------------------*/
OpenDoc(const String & rURL,SfxViewFrame * pViewFrame)961 void SvxHyperlinkDlg::OpenDoc( const String& rURL, SfxViewFrame* pViewFrame )
962 {
963     SfxStringItem aName( SID_FILE_NAME, rURL );
964     SfxStringItem aReferer( SID_REFERER, String::CreateFromAscii("private:user") );
965     SfxBoolItem aNewView( SID_OPEN_NEW_VIEW, sal_True );
966     SfxBoolItem aSilent( SID_SILENT, sal_True );
967     SfxBoolItem aReadOnly( SID_DOC_READONLY, sal_True );
968     SfxBoolItem aExternal( SID_BROWSE, sal_True );
969     SfxDispatcher* pDisp = SfxViewFrame::Current() ? SfxViewFrame::Current()->GetDispatcher() : NULL;
970 
971     if ( pViewFrame )
972     {
973         SfxFrameItem aView( SID_DOCFRAME, pViewFrame ? &pViewFrame->GetFrame() : NULL );
974         if ( pDisp )
975             pDisp->Execute( SID_OPENDOC, SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD,
976                             &aName, &aView, &aNewView, &aSilent, &aReadOnly, &aReferer, &aExternal, 0L );
977     }
978     else if ( pDisp )
979         pDisp->Execute( SID_OPENDOC, SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD,
980                         &aName, &aNewView, &aSilent, &aReadOnly, &aReferer, 0L );
981 }
982 // class SvxHyperlinkDialogWrapper ------------------------------------------
983 
SFX_IMPL_CHILDWINDOW(SvxHyperlinkDlgWrapper,SID_HYPERLINK_INSERT)984 SFX_IMPL_CHILDWINDOW(SvxHyperlinkDlgWrapper, SID_HYPERLINK_INSERT)
985 
986 /*--------------------------------------------------------------------
987     Beschreibung: Wrapper fuer Hyperlinkleiste
988  --------------------------------------------------------------------*/
989 
990 SvxHyperlinkDlgWrapper::SvxHyperlinkDlgWrapper( Window* _pParent, sal_uInt16 nId,
991                                                 SfxBindings* _pBindings, SfxChildWinInfo* /*pInfo*/ ) :
992 
993     SfxChildWindow( _pParent, nId )
994 
995 {
996     pWindow = new SvxHyperlinkDlg( _pBindings, _pParent );
997     eChildAlignment = SFX_ALIGN_TOP;
998 }
999 
1000 /*--------------------------------------------------------------------
1001     Beschreibung:
1002  --------------------------------------------------------------------*/
1003 
GetInfo() const1004 SfxChildWinInfo SvxHyperlinkDlgWrapper::GetInfo() const
1005 {
1006     SfxChildWinInfo aInfo = SfxChildWindow::GetInfo();
1007     return aInfo;
1008 }
1009 
1010