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>
28 #include <cppuhelper/exc_hlp.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 
34 using namespace css;
35 
36 FixedHyperlink::FixedHyperlink(vcl::Window* pParent, WinBits nWinStyle)
37  : FixedText(pParent, nWinStyle)
38  , m_nTextLen(0)
39  , m_aOldPointer(PointerStyle::Arrow)
40 {
41  Initialize();
42 }
43 
44 void 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 = GetCtrlTextWidth( GetText() );
57 
58  SetClickHdl(LINK(this, FixedHyperlink, HandleClick));
59 }
60 
61 bool FixedHyperlink::ImplIsOverText(Point aPosition)
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 
84 void 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 
93 void 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 
100 void FixedHyperlink::RequestHelp( const HelpEvent& rHEvt )
101 {
102  if ( IsEnabled() && ImplIsOverText(GetPointerPosPixel()) )
103  FixedText::RequestHelp( rHEvt );
104 }
105 
106 void 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.getWidth(), 0);
112  else if (GetStyle() & WB_CENTER)
113  aFocusRect.Move((aSize.Width() - aFocusRect.getWidth()) / 2, 0);
114 
115  Invalidate(aFocusRect);
116  ShowFocus(aFocusRect);
117 }
118 
119 void FixedHyperlink::LoseFocus()
120 {
121  SetTextColor( GetControlForeground() );
122  Invalidate(tools::Rectangle(Point(), GetSizePixel()));
123  HideFocus();
124 }
125 
126 void 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 
140 void FixedHyperlink::SetURL( const OUString& rNewURL )
141 {
142  m_sURL = rNewURL;
143  SetQuickHelpText( m_sURL );
144 }
145 
146 
147 void FixedHyperlink::SetText(const OUString& rNewDescription)
148 {
149  FixedText::SetText(rNewDescription);
150  m_nTextLen = GetCtrlTextWidth(GetText());
151 }
152 
153 bool FixedHyperlink::set_property(const OString &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 
162 IMPL_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));
178  SolarMutexGuard g;
179  std::unique_ptr<weld::MessageDialog> xErrorBox(Application::CreateMessageDialog(GetFrameWeld(), VclMessageType::Error, VclButtonsType::Ok, msg));
180  xErrorBox->set_title(rHyperlink.GetText());
181  xErrorBox->run();
182  }
183 }
184 
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IMPL_LINK(FixedHyperlink, HandleClick, FixedHyperlink &, rHyperlink, void)
Definition: fixedhyper.cxx:162
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:707
sal_uInt16 GetCode() const
Definition: keycod.hxx:51
constexpr sal_uInt16 KEY_SPACE
Definition: keycodes.hxx:123
sal_Int64 WinBits
Any SAL_CALL getCaughtException()
void SetUnderline(FontLineStyle)
Definition: font/font.cxx:245
OUString m_sURL
bool IsLeaveWindow() const
Definition: event.hxx:137
virtual void SetText(const OUString &rStr) override
Definition: ctrl.cxx:95
tools::Long Width() const
constexpr sal_uInt16 KEY_RETURN
Definition: keycodes.hxx:119
virtual void KeyInput(const KeyEvent &rKEvt)
Definition: window.cxx:1804
const vcl::KeyCode & GetKeyCode() const
Definition: event.hxx:54
tools::Long Height() const
Reference< XComponentContext > getProcessComponentContext()
PointerStyle
Definition: ptrstyle.hxx:25
WinBits const WB_CENTER
virtual bool set_property(const OString &rKey, const OUString &rValue) override
Definition: fixed.cxx:377
virtual void RequestHelp(const HelpEvent &rHEvt)
Definition: window.cxx:1868
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
Definition: builder.cxx:205
OUString anyToString(uno::Any const &value)