1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <vcl/event.hxx>
21 #include <vcl/toolkit/fixedhyper.hxx>
22 #include <vcl/settings.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/weld.hxx>
25 #include <vcl/ptrstyle.hxx>
26 #include <comphelper/anytostring.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <cppuhelper/exc_hlp.hxx>
30 #include <com/sun/star/system/XSystemShellExecute.hpp>
31 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
32 #include <com/sun/star/system/SystemShellExecute.hpp>
36 FixedHyperlink::FixedHyperlink(vcl::Window
* pParent
, WinBits nWinStyle
)
37 : FixedText(pParent
, nWinStyle
)
39 , m_aOldPointer(PointerStyle::Arrow
)
44 void FixedHyperlink::Initialize()
46 // saves the old pointer
47 m_aOldPointer
= GetPointer();
49 vcl::Font aFont
= GetControlFont( );
51 aFont
.SetUnderline( LINESTYLE_SINGLE
);
52 SetControlFont( aFont
);
53 // changes the color to link color
54 SetControlForeground( Application::GetSettings().GetStyleSettings().GetLinkColor() );
55 // calculates text len
56 m_nTextLen
= GetOutDev()->GetCtrlTextWidth( GetText() );
58 SetClickHdl(LINK(this, FixedHyperlink
, HandleClick
));
61 bool FixedHyperlink::ImplIsOverText(Point aPosition
) const
63 Size aSize
= GetOutputSizePixel();
67 if (GetStyle() & WB_RIGHT
)
69 return aPosition
.X() > (aSize
.Width() - m_nTextLen
);
71 else if (GetStyle() & WB_CENTER
)
73 bIsOver
= aPosition
.X() > (aSize
.Width() / 2 - m_nTextLen
/ 2) &&
74 aPosition
.X() < (aSize
.Width() / 2 + m_nTextLen
/ 2);
78 bIsOver
= aPosition
.X() < m_nTextLen
;
84 void FixedHyperlink::MouseMove( const MouseEvent
& rMEvt
)
86 // changes the pointer if the control is enabled and the mouse is over the text.
87 if ( !rMEvt
.IsLeaveWindow() && IsEnabled() && ImplIsOverText(GetPointerPosPixel()) )
88 SetPointer( PointerStyle::RefHand
);
90 SetPointer( m_aOldPointer
);
93 void FixedHyperlink::MouseButtonUp( const MouseEvent
& )
95 // calls the link if the control is enabled and the mouse is over the text.
96 if ( IsEnabled() && ImplIsOverText(GetPointerPosPixel()) )
97 ImplCallEventListenersAndHandler( VclEventId::ButtonClick
, [this] () { m_aClickHdl
.Call(*this); } );
100 void FixedHyperlink::RequestHelp( const HelpEvent
& rHEvt
)
102 if ( IsEnabled() && ImplIsOverText(GetPointerPosPixel()) )
103 FixedText::RequestHelp( rHEvt
);
106 void FixedHyperlink::GetFocus()
108 Size aSize
= GetSizePixel();
109 tools::Rectangle
aFocusRect(Point(1, 1), Size(m_nTextLen
+ 4, aSize
.Height() - 2));
110 if (GetStyle() & WB_RIGHT
)
111 aFocusRect
.Move(aSize
.Width() - aFocusRect
.getOpenWidth(), 0);
112 else if (GetStyle() & WB_CENTER
)
113 aFocusRect
.Move((aSize
.Width() - aFocusRect
.getOpenWidth()) / 2, 0);
115 Invalidate(aFocusRect
);
116 ShowFocus(aFocusRect
);
119 void FixedHyperlink::LoseFocus()
121 SetTextColor( GetControlForeground() );
122 Invalidate(tools::Rectangle(Point(), GetSizePixel()));
126 void FixedHyperlink::KeyInput( const KeyEvent
& rKEvt
)
128 switch ( rKEvt
.GetKeyCode().GetCode() )
132 m_aClickHdl
.Call( *this );
136 FixedText::KeyInput( rKEvt
);
140 void FixedHyperlink::SetURL( const OUString
& rNewURL
)
143 SetQuickHelpText( m_sURL
);
147 void FixedHyperlink::SetText(const OUString
& rNewDescription
)
149 FixedText::SetText(rNewDescription
);
150 m_nTextLen
= GetOutDev()->GetCtrlTextWidth(GetText());
153 bool FixedHyperlink::set_property(const OUString
&rKey
, const OUString
&rValue
)
158 return FixedText::set_property(rKey
, rValue
);
162 IMPL_LINK(FixedHyperlink
, HandleClick
, FixedHyperlink
&, rHyperlink
, void)
164 if ( rHyperlink
.m_sURL
.isEmpty() ) // Nothing to do, when the URL is empty
169 uno::Reference
< system::XSystemShellExecute
> xSystemShellExecute(
170 system::SystemShellExecute::create(comphelper::getProcessComponentContext()));
171 //throws css::lang::IllegalArgumentException, css::system::SystemShellExecuteException
172 xSystemShellExecute
->execute( rHyperlink
.m_sURL
, OUString(), system::SystemShellExecuteFlags::URIS_ONLY
);
174 catch ( const uno::Exception
& )
176 uno::Any
exc(cppu::getCaughtException());
177 OUString
msg(comphelper::anyToString(exc
));
179 std::shared_ptr
<weld::MessageDialog
> xErrorBox(
180 Application::CreateMessageDialog(GetFrameWeld(), VclMessageType::Error
, VclButtonsType::Ok
, msg
));
181 xErrorBox
->set_title(rHyperlink
.GetText());
182 xErrorBox
->runAsync(xErrorBox
, [](sal_Int32
){});
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */