tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sfx2 / source / inet / inettbc.cxx
blobd424067ed37f107e96f506468bd720249a1be21d
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 .
21 #include <inettbc.hxx>
23 #include <com/sun/star/awt/XTopWindow.hpp>
24 #include <com/sun/star/frame/Desktop.hpp>
25 #include <com/sun/star/task/XInteractionHandler.hpp>
26 #include <com/sun/star/util/XURLTransformer.hpp>
28 #include <comphelper/propertyvalue.hxx>
29 #include <svl/stritem.hxx>
30 #include <unotools/historyoptions.hxx>
31 #include <vcl/toolbox.hxx>
32 #include <vcl/svapp.hxx>
33 #include <osl/file.hxx>
34 #include <rtl/ustring.hxx>
36 #include <svtools/inettbc.hxx>
38 #include <vcl/InterimItemWindow.hxx>
39 #include <sfx2/sfxsids.hrc>
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::beans;
43 using namespace ::com::sun::star::util;
44 using namespace ::com::sun::star::frame;
47 // SfxURLToolBoxControl_Impl
50 SFX_IMPL_TOOLBOX_CONTROL(SfxURLToolBoxControl_Impl,SfxStringItem)
52 SfxURLToolBoxControl_Impl::SfxURLToolBoxControl_Impl( sal_uInt16 nSlotId, ToolBoxItemId nId, ToolBox& rBox )
53 : SfxToolBoxControl( nSlotId, nId, rBox )
54 , m_bModified(false)
56 addStatusListener( u".uno:CurrentURL"_ustr);
59 SfxURLToolBoxControl_Impl::~SfxURLToolBoxControl_Impl()
63 class URLBoxItemWindow final : public InterimItemWindow
65 private:
66 std::unique_ptr<SvtURLBox> m_xWidget;
68 DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
69 public:
70 URLBoxItemWindow(vcl::Window* pParent)
71 : InterimItemWindow(pParent, u"sfx/ui/urlbox.ui"_ustr, u"URLBox"_ustr)
72 , m_xWidget(new SvtURLBox(m_xBuilder->weld_combo_box(u"urlbox"_ustr)))
74 InitControlBase(m_xWidget->getWidget());
76 m_xWidget->connect_key_press(LINK(this, URLBoxItemWindow, KeyInputHdl));
78 int nWidth = GetDesktopRectPixel().GetWidth() > 800 ? 300 : 225;
79 SetSizePixel(Size(nWidth, m_xWidget->get_preferred_size().Height()));
82 SvtURLBox* GetURLBox()
84 return m_xWidget.get();
87 virtual void dispose() override
89 m_xWidget.reset();
90 InterimItemWindow::dispose();
93 void set_sensitive(bool bSensitive)
95 Enable(bSensitive);
96 m_xWidget->set_sensitive(bSensitive);
99 virtual ~URLBoxItemWindow() override
101 disposeOnce();
105 IMPL_LINK(URLBoxItemWindow, KeyInputHdl, const KeyEvent&, rKEvt, bool)
107 return ChildKeyInput(rKEvt);
110 URLBoxItemWindow* SfxURLToolBoxControl_Impl::GetURLBoxItemWindow() const
112 return static_cast<URLBoxItemWindow*>(GetToolBox().GetItemWindow(GetId()));
115 SvtURLBox* SfxURLToolBoxControl_Impl::GetURLBox() const
117 return GetURLBoxItemWindow()->GetURLBox();
120 void SfxURLToolBoxControl_Impl::OpenURL( const OUString& rName ) const
122 OUString aName;
124 INetURLObject aObj( rName );
125 if ( aObj.GetProtocol() == INetProtocol::NotValid )
127 aName = SvtURLBox::ParseSmart( rName, u""_ustr );
129 else
130 aName = rName;
132 if ( aName.isEmpty() )
133 return;
135 Reference< XDispatchProvider > xDispatchProvider( getFrameInterface(), UNO_QUERY );
136 if ( !xDispatchProvider.is() )
137 return;
139 URL aTargetURL;
140 aTargetURL.Complete = aName;
142 getURLTransformer()->parseStrict( aTargetURL );
143 Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, u"_default"_ustr, 0 );
144 if ( !xDispatch.is() )
145 return;
147 SfxURLToolBoxControl_Impl::ExecuteInfo* pExecuteInfo = new SfxURLToolBoxControl_Impl::ExecuteInfo;
148 pExecuteInfo->xDispatch = std::move(xDispatch);
149 pExecuteInfo->aTargetURL = std::move(aTargetURL);
150 pExecuteInfo->aArgs = {
151 comphelper::makePropertyValue(u"Referer"_ustr, u"private:user"_ustr),
152 comphelper::makePropertyValue(u"FileName"_ustr, aName)
155 Application::PostUserEvent( LINK( nullptr, SfxURLToolBoxControl_Impl, ExecuteHdl_Impl), pExecuteInfo );
159 IMPL_STATIC_LINK( SfxURLToolBoxControl_Impl, ExecuteHdl_Impl, void*, p, void )
161 ExecuteInfo* pExecuteInfo = static_cast<ExecuteInfo*>(p);
164 // Asynchronous execution as this can lead to our own destruction!
165 // Framework can recycle our current frame and the layout manager disposes all user interface
166 // elements if a component gets detached from its frame!
167 pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
169 catch ( Exception& )
173 delete pExecuteInfo;
176 VclPtr<InterimItemWindow> SfxURLToolBoxControl_Impl::CreateItemWindow( vcl::Window* pParent )
178 VclPtrInstance<URLBoxItemWindow> xURLBox(pParent);
179 SvtURLBox* pURLBox = xURLBox->GetURLBox();
180 pURLBox->connect_changed(LINK(this, SfxURLToolBoxControl_Impl, SelectHdl));
181 pURLBox->connect_entry_activate(LINK(this, SfxURLToolBoxControl_Impl, OpenHdl));
182 xURLBox->Show();
183 return xURLBox;
186 IMPL_LINK(SfxURLToolBoxControl_Impl, SelectHdl, weld::ComboBox&, rComboBox, void)
188 m_bModified = true;
190 SvtURLBox* pURLBox = GetURLBox();
191 OUString aName( pURLBox->GetURL() );
193 if (rComboBox.changed_by_direct_pick() && !aName.isEmpty())
194 OpenURL( aName );
197 IMPL_LINK_NOARG(SfxURLToolBoxControl_Impl, OpenHdl, weld::ComboBox&, bool)
199 SvtURLBox* pURLBox = GetURLBox();
200 OpenURL( pURLBox->GetURL() );
202 Reference< XDesktop2 > xDesktop = Desktop::create( m_xContext );
203 Reference< XFrame > xFrame = xDesktop->getActiveFrame();
204 if (!xFrame.is())
205 return true;
207 auto xWin = xFrame->getContainerWindow();
208 if (!xWin)
209 return true;
210 xWin->setFocus();
211 Reference<css::awt::XTopWindow> xTop(xWin, UNO_QUERY);
212 if (!xTop)
213 return true;
214 xTop->toFront();
215 return true;
218 void SfxURLToolBoxControl_Impl::StateChangedAtToolBoxControl
220 sal_uInt16 nSID,
221 SfxItemState eState,
222 const SfxPoolItem* pState
225 if ( nSID == SID_OPENURL )
227 // Disable URL box if command is disabled
228 GetURLBoxItemWindow()->set_sensitive( SfxItemState::DISABLED != eState );
231 if ( !GetURLBoxItemWindow()->IsEnabled() )
232 return;
234 if( nSID == SID_FOCUSURLBOX )
236 if ( GetURLBoxItemWindow()->IsVisible() )
237 GetURLBoxItemWindow()->GrabFocus();
239 else if ( !m_bModified && SfxItemState::DEFAULT == eState )
241 SvtURLBox* pURLBox = GetURLBox();
242 pURLBox->clear();
244 const std::vector< SvtHistoryOptions::HistoryItem > lList = SvtHistoryOptions::GetList(EHistoryType::PickList);
245 for (const SvtHistoryOptions::HistoryItem& lProps : lList)
247 if (!lProps.sURL.isEmpty())
249 INetURLObject aURL ( lProps.sURL );
250 OUString sMainURL( aURL.GetMainURL( INetURLObject::DecodeMechanism::WithCharset ) );
251 OUString sFile;
253 if (osl::FileBase::getSystemPathFromFileURL(sMainURL, sFile) == osl::FileBase::E_None)
254 pURLBox->append_text(sFile);
255 else
256 pURLBox->append_text(sMainURL);
260 const SfxStringItem *pURL = dynamic_cast< const SfxStringItem* >(pState);
261 assert(pURL);
262 INetURLObject aURL( pURL->GetValue() );
263 INetProtocol eProt = aURL.GetProtocol();
264 if ( eProt == INetProtocol::File )
266 pURLBox->set_entry_text( aURL.PathToFileName() );
268 else
269 pURLBox->set_entry_text( aURL.GetURLNoPass() );
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */