build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / control / fixedhyper.cxx
blob8cfff962035806a200b1a6ec8159fb1b7616a75e
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 <vcl/fixedhyper.hxx>
21 #include <vcl/svapp.hxx>
22 #include <vcl/layout.hxx>
23 #include <comphelper/anytostring.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <cppuhelper/exc_hlp.hxx>
27 #include <com/sun/star/system/XSystemShellExecute.hpp>
28 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
29 #include <com/sun/star/system/SystemShellExecute.hpp>
31 using namespace css;
33 FixedHyperlink::FixedHyperlink(vcl::Window* pParent, WinBits nWinStyle)
34 : FixedText(pParent, nWinStyle)
35 , m_nTextLen(0)
37 Initialize();
40 void FixedHyperlink::Initialize()
42 // saves the old pointer
43 m_aOldPointer = GetPointer();
44 // changes the font
45 vcl::Font aFont = GetControlFont( );
46 // to underline
47 aFont.SetUnderline( LINESTYLE_SINGLE );
48 SetControlFont( aFont );
49 // changes the color to link color
50 SetControlForeground( Application::GetSettings().GetStyleSettings().GetLinkColor() );
51 // calculates text len
52 m_nTextLen = GetCtrlTextWidth( GetText() );
54 SetClickHdl(LINK(this, FixedHyperlink, HandleClick));
57 bool FixedHyperlink::ImplIsOverText(Point aPosition)
59 Size aSize = GetOutputSizePixel();
61 bool bIsOver = false;
63 if (GetStyle() & WB_RIGHT)
65 return aPosition.X() > (aSize.Width() - m_nTextLen);
67 else if (GetStyle() & WB_CENTER)
69 bIsOver = aPosition.X() > (aSize.Width() / 2 - m_nTextLen / 2) &&
70 aPosition.X() < (aSize.Width() / 2 + m_nTextLen / 2);
72 else
74 bIsOver = aPosition.X() < m_nTextLen;
77 return bIsOver;
80 void FixedHyperlink::MouseMove( const MouseEvent& rMEvt )
82 // changes the pointer if the control is enabled and the mouse is over the text.
83 if ( !rMEvt.IsLeaveWindow() && IsEnabled() && ImplIsOverText(GetPointerPosPixel()) )
84 SetPointer( PointerStyle::RefHand );
85 else
86 SetPointer( m_aOldPointer );
89 void FixedHyperlink::MouseButtonUp( const MouseEvent& )
91 // calls the link if the control is enabled and the mouse is over the text.
92 if ( IsEnabled() && ImplIsOverText(GetPointerPosPixel()) )
93 ImplCallEventListenersAndHandler( VCLEVENT_BUTTON_CLICK, [this] () { m_aClickHdl.Call(*this); } );
96 void FixedHyperlink::RequestHelp( const HelpEvent& rHEvt )
98 if ( IsEnabled() && ImplIsOverText(GetPointerPosPixel()) )
99 FixedText::RequestHelp( rHEvt );
102 void FixedHyperlink::GetFocus()
104 SetTextColor( Color( COL_LIGHTRED ) );
105 Invalidate(Rectangle(Point(), GetSizePixel()));
106 ShowFocus( Rectangle( Point( 1, 1 ), Size( m_nTextLen + 4, GetSizePixel().Height() - 2 ) ) );
109 void FixedHyperlink::LoseFocus()
111 SetTextColor( GetControlForeground() );
112 Invalidate(Rectangle(Point(), GetSizePixel()));
113 HideFocus();
116 void FixedHyperlink::KeyInput( const KeyEvent& rKEvt )
118 switch ( rKEvt.GetKeyCode().GetCode() )
120 case KEY_SPACE:
121 case KEY_RETURN:
122 m_aClickHdl.Call( *this );
123 break;
125 default:
126 FixedText::KeyInput( rKEvt );
130 void FixedHyperlink::SetURL( const OUString& rNewURL )
132 m_sURL = rNewURL;
133 SetQuickHelpText( m_sURL );
137 void FixedHyperlink::SetText(const OUString& rNewDescription)
139 FixedText::SetText(rNewDescription);
140 m_nTextLen = GetCtrlTextWidth(GetText());
143 bool FixedHyperlink::set_property(const OString &rKey, const OString &rValue)
145 if (rKey == "uri")
146 SetURL(OStringToOUString(rValue, RTL_TEXTENCODING_UTF8));
147 else
148 return FixedText::set_property(rKey, rValue);
149 return true;
152 IMPL_STATIC_LINK(FixedHyperlink, HandleClick, FixedHyperlink&, rHyperlink, void)
154 if ( rHyperlink.m_sURL.isEmpty() ) // Nothing to do, when the URL is empty
155 return;
159 uno::Reference< system::XSystemShellExecute > xSystemShellExecute(
160 system::SystemShellExecute::create(comphelper::getProcessComponentContext()));
161 //throws css::lang::IllegalArgumentException, css::system::SystemShellExecuteException
162 xSystemShellExecute->execute( rHyperlink.m_sURL, OUString(), system::SystemShellExecuteFlags::URIS_ONLY );
164 catch ( const uno::Exception& )
166 uno::Any exc(cppu::getCaughtException());
167 OUString msg(comphelper::anyToString(exc));
168 const SolarMutexGuard guard;
169 ScopedVclPtrInstance< MessageDialog > aErrorBox(nullptr, msg);
170 aErrorBox->SetText( rHyperlink.GetText() );
171 aErrorBox->Execute();
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */