LibreOffice Module vcl (master) 1
fixedhyper.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <vcl/event.hxx>
22#include <vcl/settings.hxx>
23#include <vcl/svapp.hxx>
24#include <vcl/weld.hxx>
25#include <vcl/ptrstyle.hxx>
29
30#include <com/sun/star/system/XSystemShellExecute.hpp>
31#include <com/sun/star/system/SystemShellExecuteFlags.hpp>
32#include <com/sun/star/system/SystemShellExecute.hpp>
33
34using namespace css;
35
36FixedHyperlink::FixedHyperlink(vcl::Window* pParent, WinBits nWinStyle)
37 : FixedText(pParent, nWinStyle)
38 , m_nTextLen(0)
39 , m_aOldPointer(PointerStyle::Arrow)
40{
41 Initialize();
42}
43
44void FixedHyperlink::Initialize()
45{
46 // saves the old pointer
47 m_aOldPointer = GetPointer();
48 // changes the font
49 vcl::Font aFont = GetControlFont( );
50 // to underline
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() );
57
58 SetClickHdl(LINK(this, FixedHyperlink, HandleClick));
59}
60
61bool FixedHyperlink::ImplIsOverText(Point aPosition) const
62{
63 Size aSize = GetOutputSizePixel();
64
65 bool bIsOver = false;
66
67 if (GetStyle() & WB_RIGHT)
68 {
69 return aPosition.X() > (aSize.Width() - m_nTextLen);
70 }
71 else if (GetStyle() & WB_CENTER)
72 {
73 bIsOver = aPosition.X() > (aSize.Width() / 2 - m_nTextLen / 2) &&
74 aPosition.X() < (aSize.Width() / 2 + m_nTextLen / 2);
75 }
76 else
77 {
78 bIsOver = aPosition.X() < m_nTextLen;
79 }
80
81 return bIsOver;
82}
83
84void FixedHyperlink::MouseMove( const MouseEvent& rMEvt )
85{
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 );
89 else
90 SetPointer( m_aOldPointer );
91}
92
93void FixedHyperlink::MouseButtonUp( const MouseEvent& )
94{
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); } );
98}
99
100void FixedHyperlink::RequestHelp( const HelpEvent& rHEvt )
101{
102 if ( IsEnabled() && ImplIsOverText(GetPointerPosPixel()) )
103 FixedText::RequestHelp( rHEvt );
104}
105
106void FixedHyperlink::GetFocus()
107{
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);
114
115 Invalidate(aFocusRect);
116 ShowFocus(aFocusRect);
117}
118
119void FixedHyperlink::LoseFocus()
120{
121 SetTextColor( GetControlForeground() );
122 Invalidate(tools::Rectangle(Point(), GetSizePixel()));
123 HideFocus();
124}
125
126void FixedHyperlink::KeyInput( const KeyEvent& rKEvt )
127{
128 switch ( rKEvt.GetKeyCode().GetCode() )
129 {
130 case KEY_SPACE:
131 case KEY_RETURN:
132 m_aClickHdl.Call( *this );
133 break;
134
135 default:
136 FixedText::KeyInput( rKEvt );
137 }
138}
139
140void FixedHyperlink::SetURL( const OUString& rNewURL )
141{
142 m_sURL = rNewURL;
143 SetQuickHelpText( m_sURL );
144}
145
146
147void FixedHyperlink::SetText(const OUString& rNewDescription)
148{
149 FixedText::SetText(rNewDescription);
150 m_nTextLen = GetOutDev()->GetCtrlTextWidth(GetText());
151}
152
153bool FixedHyperlink::set_property(const OUString &rKey, const OUString &rValue)
154{
155 if (rKey == "uri")
156 SetURL(rValue);
157 else
158 return FixedText::set_property(rKey, rValue);
159 return true;
160}
161
162IMPL_LINK(FixedHyperlink, HandleClick, FixedHyperlink&, rHyperlink, void)
163{
164 if ( rHyperlink.m_sURL.isEmpty() ) // Nothing to do, when the URL is empty
165 return;
166
167 try
168 {
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 );
173 }
174 catch ( const uno::Exception& )
175 {
177 OUString msg(comphelper::anyToString(exc));
179 std::shared_ptr<weld::MessageDialog> xErrorBox(
181 xErrorBox->set_title(rHyperlink.GetText());
182 xErrorBox->runAsync(xErrorBox, [](sal_Int32){});
183 }
184}
185
186/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:638
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
Definition: builder.cxx:215
virtual void SetText(const OUString &rStr) override
Definition: ctrl.cxx:98
virtual bool set_property(const OUString &rKey, const OUString &rValue) override
Definition: fixed.cxx:376
const vcl::KeyCode & GetKeyCode() const
Definition: event.hxx:57
bool IsLeaveWindow() const
Definition: event.hxx:140
constexpr tools::Long X() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
void SetUnderline(FontLineStyle)
Definition: font/font.cxx:266
sal_uInt16 GetCode() const
Definition: keycod.hxx:49
virtual void RequestHelp(const HelpEvent &rHEvt)
Definition: window.cxx:1869
virtual void KeyInput(const KeyEvent &rKEvt)
Definition: window.cxx:1805
OUString m_sURL
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
IMPL_LINK(FixedHyperlink, HandleClick, FixedHyperlink &, rHyperlink, void)
Definition: fixedhyper.cxx:162
constexpr sal_uInt16 KEY_RETURN
Definition: keycodes.hxx:119
constexpr sal_uInt16 KEY_SPACE
Definition: keycodes.hxx:123
OUString anyToString(uno::Any const &value)
Reference< XComponentContext > getProcessComponentContext()
Any SAL_CALL getCaughtException()
PointerStyle
Definition: ptrstyle.hxx:26
sal_Int64 WinBits
Definition: wintypes.hxx:109
WinBits const WB_CENTER
Definition: wintypes.hxx:147
WinBits const WB_RIGHT
Definition: wintypes.hxx:148