android: Update app-specific/MIME type icons
[LibreOffice.git] / cui / source / dialogs / hltpbase.cxx
blob01776c80d25e391ad072f4067a93bb442dc0a40a
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 <memory>
21 #include <sal/config.h>
23 #include <comphelper/lok.hxx>
24 #include <osl/file.hxx>
25 #include <sfx2/app.hxx>
26 #include <sfx2/event.hxx>
27 #include <sfx2/frame.hxx>
28 #include <sfx2/viewfrm.hxx>
29 #include <sot/formats.hxx>
30 #include <sfx2/sfxsids.hrc>
31 #include <svl/macitem.hxx>
32 #include <ucbhelper/content.hxx>
33 #include <cuihyperdlg.hxx>
34 #include <hltpbase.hxx>
35 #include <macroass.hxx>
36 #include <svx/svxdlg.hxx>
37 #include <strings.hrc>
38 #include <dialmgr.hxx>
39 #include <bitmaps.hlst>
41 using namespace ::ucbhelper;
43 namespace {
45 OUString CreateUiNameFromURL( const OUString& aStrURL )
47 OUString aStrUiURL;
48 INetURLObject aURLObj( aStrURL );
50 switch(aURLObj.GetProtocol())
52 case INetProtocol::File:
53 osl::FileBase::getSystemPathFromFileURL(aURLObj.GetMainURL(INetURLObject::DecodeMechanism::NONE), aStrUiURL);
54 break;
55 case INetProtocol::Ftp :
57 //remove password from name
58 INetURLObject aTmpURL(aURLObj);
59 aTmpURL.SetPass(u"");
60 aStrUiURL = aTmpURL.GetMainURL( INetURLObject::DecodeMechanism::Unambiguous );
62 break;
63 default :
65 aStrUiURL = aURLObj.GetMainURL(INetURLObject::DecodeMechanism::Unambiguous);
68 if(aStrUiURL.isEmpty())
69 return aStrURL;
70 return aStrUiURL;
75 // ComboBox-Control for URL's with History and Autocompletion
76 SvxHyperURLBox::SvxHyperURLBox(std::unique_ptr<weld::ComboBox> xControl)
77 : SvtURLBox(std::move(xControl))
78 , DropTargetHelper(getWidget()->get_drop_target())
80 SetSmartProtocol(INetProtocol::Http);
83 sal_Int8 SvxHyperURLBox::AcceptDrop( const AcceptDropEvent& /* rEvt */ )
85 return IsDropFormatSupported( SotClipboardFormatId::STRING ) ? DND_ACTION_COPY : DND_ACTION_NONE;
88 sal_Int8 SvxHyperURLBox::ExecuteDrop( const ExecuteDropEvent& rEvt )
90 TransferableDataHelper aDataHelper( rEvt.maDropEvent.Transferable );
91 OUString aString;
92 sal_Int8 nRet = DND_ACTION_NONE;
94 if( aDataHelper.GetString( SotClipboardFormatId::STRING, aString ) )
96 set_entry_text(aString);
97 nRet = DND_ACTION_COPY;
100 return nRet;
103 //# Hyperlink-Dialog: Tabpages-Baseclass #
105 SvxHyperlinkTabPageBase::SvxHyperlinkTabPageBase(weld::Container* pParent,
106 SvxHpLinkDlg* pDlg,
107 const OUString& rUIXMLDescription,
108 const OUString& rID,
109 const SfxItemSet* pItemSet)
110 : IconChoicePage(pParent, rUIXMLDescription, rID, pItemSet)
111 , mxCbbFrame(xBuilder->weld_combo_box("frame"))
112 , mxLbForm(xBuilder->weld_combo_box("form"))
113 , mxEdIndication(xBuilder->weld_entry("indication"))
114 , mxEdText(xBuilder->weld_entry("name"))
115 , mxBtScript(xBuilder->weld_button("script"))
116 , mxFormLabel(xBuilder->weld_label("form_label"))
117 , mxFrameLabel(xBuilder->weld_label("frame_label"))
118 , mbIsCloseDisabled( false )
119 , mpDialog( pDlg )
120 , mbStdControlsInit( false )
121 , maTimer("cui SvxHyperlinkTabPageBase maTimer")
123 // create bookmark-window
126 SvxHyperlinkTabPageBase::~SvxHyperlinkTabPageBase()
128 maTimer.Stop();
130 HideMarkWnd();
133 bool SvxHyperlinkTabPageBase::QueryClose()
135 return !mbIsCloseDisabled;
138 void SvxHyperlinkTabPageBase::InitStdControls ()
140 if ( !mbStdControlsInit )
142 SfxDispatcher* pDispatch = GetDispatcher();
143 SfxViewFrame* pViewFrame = pDispatch ? pDispatch->GetFrame() : nullptr;
144 SfxFrame* pFrame = pViewFrame ? &pViewFrame->GetFrame() : nullptr;
145 if ( pFrame )
147 TargetList aList;
148 SfxFrame::GetDefaultTargetList(aList);
149 if( !aList.empty() )
151 size_t nCount = aList.size();
152 size_t i;
153 for ( i = 0; i < nCount; i++ )
155 mxCbbFrame->append_text( aList.at( i ) );
160 mxBtScript->set_from_icon_name(RID_SVXBMP_SCRIPT);
162 mxBtScript->connect_clicked ( LINK ( this, SvxHyperlinkTabPageBase, ClickScriptHdl_Impl ) );
165 mbStdControlsInit = true;
168 // Move Extra-Window
169 void SvxHyperlinkTabPageBase::MoveToExtraWnd( Point aNewPos )
171 mxMarkWnd->MoveTo(aNewPos);
174 // Show Extra-Window
175 void SvxHyperlinkTabPageBase::ShowMarkWnd()
177 if (mxMarkWnd)
179 mxMarkWnd->getDialog()->present();
180 return;
183 weld::Dialog* pDialog = mpDialog->getDialog();
185 mxMarkWnd = std::make_shared<SvxHlinkDlgMarkWnd>(pDialog, this);
187 // Size of dialog-window in screen pixels
188 Point aDlgPos(pDialog->get_position());
189 Size aDlgSize(pDialog->get_size());
191 // Absolute size of the screen
192 ::tools::Rectangle aScreen(pDialog->get_monitor_workarea());
194 // Size of Extrawindow
195 Size aExtraWndSize(mxMarkWnd->getDialog()->get_preferred_size());
197 // mxMarkWnd is a child of mpDialog, so coordinates for positioning must be relative to mpDialog
198 if( aDlgPos.X()+(1.05*aDlgSize.Width())+aExtraWndSize.Width() > aScreen.Right() )
200 if( aDlgPos.X() - ( 0.05*aDlgSize.Width() ) - aExtraWndSize.Width() < 0 )
202 // Pos Extrawindow anywhere
203 MoveToExtraWnd( Point(10,10) ); // very unlikely
205 else
207 // Pos Extrawindow on the left side of Dialog
208 MoveToExtraWnd( Point(0,0) - Point( tools::Long(0.05*aDlgSize.Width()), 0 ) - Point( aExtraWndSize.Width(), 0 ) );
211 else
213 // Pos Extrawindow on the right side of Dialog
214 MoveToExtraWnd ( Point( tools::Long(1.05*aDlgSize.getWidth()), 0 ) );
217 // Set size of Extra-Window
218 mxMarkWnd->getDialog()->set_size_request(aExtraWndSize.Width(), aDlgSize.Height());
220 weld::DialogController::runAsync(mxMarkWnd, [this](sal_Int32 /*nResult*/) { mxMarkWnd.reset(); } );
223 void SvxHyperlinkTabPageBase::HideMarkWnd()
225 if (mxMarkWnd)
227 mxMarkWnd->response(RET_CANCEL);
228 mxMarkWnd.reset();
232 // Fill Dialogfields
233 void SvxHyperlinkTabPageBase::FillStandardDlgFields ( const SvxHyperlinkItem* pHyperlinkItem )
235 if (!comphelper::LibreOfficeKit::isActive())
237 // Frame
238 sal_Int32 nPos = mxCbbFrame->find_text(pHyperlinkItem->GetTargetFrame());
239 if (nPos != -1)
240 mxCbbFrame->set_active(nPos);
242 // Form
243 OUString aStrFormText = CuiResId( RID_CUISTR_HYPERDLG_FROM_TEXT );
245 OUString aStrFormButton = CuiResId( RID_CUISTR_HYPERDLG_FORM_BUTTON );
247 if( pHyperlinkItem->GetInsertMode() & HLINK_HTMLMODE )
249 mxLbForm->clear();
250 mxLbForm->append_text( aStrFormText );
251 mxLbForm->set_active( 0 );
253 else
255 mxLbForm->clear();
256 mxLbForm->append_text( aStrFormText );
257 mxLbForm->append_text( aStrFormButton );
258 mxLbForm->set_active( pHyperlinkItem->GetInsertMode() == HLINK_BUTTON ? 1 : 0 );
261 else
263 mxCbbFrame->hide();
264 mxLbForm->hide();
265 mxFormLabel->hide();
266 mxFrameLabel->hide();
269 // URL
270 mxEdIndication->set_text( pHyperlinkItem->GetName() );
272 // Name
273 mxEdText->set_text( pHyperlinkItem->GetIntName() );
275 // Script-button
276 if (!comphelper::LibreOfficeKit::isActive())
278 if ( pHyperlinkItem->GetMacroEvents() == HyperDialogEvent::NONE )
279 mxBtScript->set_sensitive(false);
280 else
281 mxBtScript->set_sensitive(true);
283 else
285 mxBtScript->hide();
289 // Any action to do after apply-button is pressed
290 void SvxHyperlinkTabPageBase::DoApply ()
292 // default-implementation : do nothing
295 // This method would be called from bookmark-window to set new mark-string
296 void SvxHyperlinkTabPageBase::SetMarkStr ( const OUString& /*aStrMark*/ )
298 // default-implementation : do nothing
301 // Set initial focus
302 void SvxHyperlinkTabPageBase::SetInitFocus()
304 xContainer->grab_focus();
307 // retrieve dispatcher
308 SfxDispatcher* SvxHyperlinkTabPageBase::GetDispatcher() const
310 return mpDialog->GetDispatcher();
313 void SvxHyperlinkTabPageBase::DisableClose(bool _bDisable)
315 mbIsCloseDisabled = _bDisable;
316 if (mbIsCloseDisabled)
317 maBusy.incBusy(mpDialog->getDialog());
318 else
319 maBusy.decBusy();
322 // Click on imagebutton : Script
323 IMPL_LINK_NOARG(SvxHyperlinkTabPageBase, ClickScriptHdl_Impl, weld::Button&, void)
325 SvxHyperlinkItem *pHyperlinkItem = const_cast<SvxHyperlinkItem*>(
326 GetItemSet().GetItem (SID_HYPERLINK_GETLINK));
328 if (!pHyperlinkItem || pHyperlinkItem->GetMacroEvents() == HyperDialogEvent::NONE)
329 return;
331 // get macros from itemset
332 const SvxMacroTableDtor* pMacroTbl = pHyperlinkItem->GetMacroTable();
333 SvxMacroItem aItem ( SID_ATTR_MACROITEM );
334 if( pMacroTbl )
335 aItem.SetMacroTable( *pMacroTbl );
337 // create empty itemset for macro-dlg
338 SfxItemSetFixed<SID_ATTR_MACROITEM, SID_ATTR_MACROITEM> aItemSet( SfxGetpApp()->GetPool() );
339 aItemSet.Put ( aItem );
341 DisableClose( true );
343 SfxMacroAssignDlg aDlg(mpDialog->getDialog(), mxDocumentFrame, aItemSet);
345 // add events
346 SfxMacroTabPage *pMacroPage = aDlg.GetTabPage();
348 if ( pHyperlinkItem->GetMacroEvents() & HyperDialogEvent::MouseOverObject )
349 pMacroPage->AddEvent( CuiResId(RID_CUISTR_HYPDLG_MACROACT1),
350 SvMacroItemId::OnMouseOver );
351 if ( pHyperlinkItem->GetMacroEvents() & HyperDialogEvent::MouseClickObject )
352 pMacroPage->AddEvent( CuiResId(RID_CUISTR_HYPDLG_MACROACT2),
353 SvMacroItemId::OnClick);
354 if ( pHyperlinkItem->GetMacroEvents() & HyperDialogEvent::MouseOutObject )
355 pMacroPage->AddEvent( CuiResId(RID_CUISTR_HYPDLG_MACROACT3),
356 SvMacroItemId::OnMouseOut);
357 // execute dlg
358 short nRet = aDlg.run();
359 DisableClose( false );
360 if ( RET_OK == nRet )
362 const SfxItemSet* pOutSet = aDlg.GetOutputItemSet();
363 const SfxPoolItem* pItem;
364 if( SfxItemState::SET == pOutSet->GetItemState( SID_ATTR_MACROITEM, false, &pItem ))
366 pHyperlinkItem->SetMacroTable( static_cast<const SvxMacroItem*>(pItem)->GetMacroTable() );
371 // Get Macro-Infos
372 HyperDialogEvent SvxHyperlinkTabPageBase::GetMacroEvents() const
374 const SvxHyperlinkItem *pHyperlinkItem =
375 GetItemSet().GetItem (SID_HYPERLINK_GETLINK);
377 return pHyperlinkItem ? pHyperlinkItem->GetMacroEvents() : HyperDialogEvent();
380 SvxMacroTableDtor* SvxHyperlinkTabPageBase::GetMacroTable()
382 const SvxHyperlinkItem *pHyperlinkItem =
383 GetItemSet().GetItem (SID_HYPERLINK_GETLINK);
385 return const_cast<SvxMacroTableDtor*>(pHyperlinkItem->GetMacroTable());
388 // try to detect the current protocol that is used in rStrURL
389 OUString SvxHyperlinkTabPageBase::GetSchemeFromURL( const OUString& rStrURL )
391 OUString aStrScheme;
393 INetURLObject aURL( rStrURL );
394 INetProtocol aProtocol = aURL.GetProtocol();
396 // our new INetUrlObject now has the ability
397 // to detect if a Url is valid or not :-(
398 if ( aProtocol == INetProtocol::NotValid )
400 if ( rStrURL.startsWithIgnoreAsciiCase( INET_HTTP_SCHEME ) )
402 aStrScheme = INET_HTTP_SCHEME;
404 else if ( rStrURL.startsWithIgnoreAsciiCase( INET_HTTPS_SCHEME ) )
406 aStrScheme = INET_HTTPS_SCHEME;
408 else if ( rStrURL.startsWithIgnoreAsciiCase( INET_FTP_SCHEME ) )
410 aStrScheme = INET_FTP_SCHEME;
412 else if ( rStrURL.startsWithIgnoreAsciiCase( INET_MAILTO_SCHEME ) )
414 aStrScheme = INET_MAILTO_SCHEME;
417 else
418 aStrScheme = INetURLObject::GetScheme( aProtocol );
419 return aStrScheme;
422 void SvxHyperlinkTabPageBase::GetDataFromCommonFields( OUString& aStrName,
423 OUString& aStrIntName, OUString& aStrFrame,
424 SvxLinkInsertMode& eMode )
426 aStrIntName = mxEdText->get_text();
427 aStrName = mxEdIndication->get_text();
428 aStrFrame = mxCbbFrame->get_active_text();
430 sal_Int32 nPos = mxLbForm->get_active();
431 if (nPos == -1)
432 // This happens when FillStandardDlgFields() hides mpLbForm.
433 nPos = 0;
434 eMode = static_cast<SvxLinkInsertMode>(nPos + 1);
436 // Ask dialog whether the current doc is a HTML-doc
437 if (mpDialog->IsHTMLDoc())
438 eMode = static_cast<SvxLinkInsertMode>( sal_uInt16(eMode) | HLINK_HTMLMODE );
441 // reset dialog-fields
442 void SvxHyperlinkTabPageBase::Reset( const SfxItemSet& rItemSet)
445 // Set dialog-fields from create-itemset
446 maStrInitURL.clear();
448 const SvxHyperlinkItem *pHyperlinkItem =
449 rItemSet.GetItem (SID_HYPERLINK_GETLINK);
451 if ( pHyperlinkItem )
453 // set dialog-fields
454 FillStandardDlgFields (pHyperlinkItem);
456 // set all other fields
457 FillDlgFields ( pHyperlinkItem->GetURL() );
459 // Store initial URL
460 maStrInitURL = pHyperlinkItem->GetURL();
464 // Fill output-ItemSet
465 bool SvxHyperlinkTabPageBase::FillItemSet( SfxItemSet* rOut)
467 OUString aStrURL, aStrName, aStrIntName, aStrFrame;
468 SvxLinkInsertMode eMode;
470 GetCurrentItemData ( aStrURL, aStrName, aStrIntName, aStrFrame, eMode);
471 if ( aStrName.isEmpty() ) //automatically create a visible name if the link is created without name
472 aStrName = CreateUiNameFromURL(aStrURL);
474 HyperDialogEvent nEvents = GetMacroEvents();
475 SvxMacroTableDtor* pTable = GetMacroTable();
477 SvxHyperlinkItem aItem( SID_HYPERLINK_SETLINK, aStrName, aStrURL, aStrFrame,
478 aStrIntName, eMode, nEvents, pTable );
479 rOut->Put (aItem);
481 return true;
484 // Activate / Deactivate Tabpage
485 void SvxHyperlinkTabPageBase::ActivatePage( const SfxItemSet& rItemSet )
488 // Set dialog-fields from input-itemset
489 const SvxHyperlinkItem *pHyperlinkItem =
490 rItemSet.GetItem (SID_HYPERLINK_GETLINK);
492 if ( pHyperlinkItem )
494 // standard-fields
495 FillStandardDlgFields (pHyperlinkItem);
498 // show mark-window if it was open before
499 if ( ShouldOpenMarkWnd () )
500 ShowMarkWnd ();
503 DeactivateRC SvxHyperlinkTabPageBase::DeactivatePage( SfxItemSet* _pSet)
505 // hide mark-wnd
506 SetMarkWndShouldOpen( IsMarkWndVisible () );
507 HideMarkWnd ();
509 // retrieve data of dialog
510 OUString aStrURL, aStrName, aStrIntName, aStrFrame;
511 SvxLinkInsertMode eMode;
513 GetCurrentItemData ( aStrURL, aStrName, aStrIntName, aStrFrame, eMode);
515 HyperDialogEvent nEvents = GetMacroEvents();
516 SvxMacroTableDtor* pTable = GetMacroTable();
518 if( _pSet )
520 SvxHyperlinkItem aItem( SID_HYPERLINK_GETLINK, aStrName, aStrURL, aStrFrame,
521 aStrIntName, eMode, nEvents, pTable );
522 _pSet->Put( aItem );
525 return DeactivateRC::LeavePage;
528 bool SvxHyperlinkTabPageBase::ShouldOpenMarkWnd()
530 return false;
533 void SvxHyperlinkTabPageBase::SetMarkWndShouldOpen(bool)
537 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */