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>
21 #include <vcl/fixedhyper.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 
37  : FixedText(pParent, nWinStyle)
38  , m_nTextLen(0)
39  , m_aOldPointer(PointerStyle::Arrow)
40 {
41  Initialize();
42 }
43 
45 {
46  // saves the old pointer
48  // changes the font
49  vcl::Font aFont = GetControlFont( );
50  // to underline
52  SetControlFont( aFont );
53  // changes the color to link color
54  SetControlForeground( Application::GetSettings().GetStyleSettings().GetLinkColor() );
55  // calculates text len
57 
58  SetClickHdl(LINK(this, FixedHyperlink, HandleClick));
59 }
60 
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 
85 {
86  // changes the pointer if the control is enabled and the mouse is over the text.
87  if ( !rMEvt.IsLeaveWindow() && IsEnabled() && ImplIsOverText(GetPointerPosPixel()) )
89  else
91 }
92 
94 {
95  // calls the link if the control is enabled and the mouse is over the text.
98 }
99 
101 {
103  FixedText::RequestHelp( rHEvt );
104 }
105 
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 
120 {
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;
144 }
145 
146 
147 void FixedHyperlink::SetText(const OUString& rNewDescription)
148 {
149  FixedText::SetText(rNewDescription);
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: */
long Width() const
bool ImplCallEventListenersAndHandler(VclEventId nEvent, std::function< void()> const &callHandler)
this calls both our event listeners, and a specified handler
Definition: ctrl.cxx:305
Point GetPointerPosPixel()
Definition: mouse.cxx:548
void HideFocus()
Definition: window2.cxx:91
IMPL_LINK(FixedHyperlink, HandleClick, FixedHyperlink &, rHyperlink, void)
Definition: fixedhyper.cxx:162
#define KEY_SPACE
Definition: keycodes.hxx:123
long Height() const
long getWidth() const
static const AllSettings & GetSettings()
Gets the application's settings.
Definition: svapp.cxx:705
virtual Size GetSizePixel() const
Definition: window.cxx:2364
WinBits const WB_RIGHT
sal_uInt16 GetCode() const
Definition: keycod.hxx:53
void SetQuickHelpText(const OUString &rHelpText)
Definition: window2.cxx:1220
void Move(long nHorzMoveDelta, long nVertMoveDelta)
sal_Int64 WinBits
void SetControlFont()
Definition: window2.cxx:409
Any SAL_CALL getCaughtException()
weld::Window * GetFrameWeld(SfxFrame *pFrame)
virtual OUString GetText() const
Definition: window.cxx:3051
void SetUnderline(FontLineStyle)
Definition: font/font.cxx:244
const Color & GetControlForeground() const
Definition: window2.cxx:1066
PointerStyle GetPointer() const
Definition: window2.cxx:1241
bool IsLeaveWindow() const
Definition: event.hxx:124
virtual void SetText(const OUString &rStr) override
Definition: ctrl.cxx:95
void SetControlForeground()
Definition: window2.cxx:457
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
Definition: paint.cxx:1153
LINESTYLE_SINGLE
void SetTextColor(const Color &rColor)
Definition: text.cxx:662
Size GetOutputSizePixel() const
Definition: outdev.hxx:441
virtual void ShowFocus(const tools::Rectangle &rRect)
Definition: window2.cxx:49
long X() const
virtual void KeyInput(const KeyEvent &rKEvt)
Definition: window.cxx:1767
#define KEY_RETURN
Definition: keycodes.hxx:119
long GetCtrlTextWidth(const OUString &rStr, const SalLayoutGlyphs *pLayoutCache=nullptr) const
Definition: text.cxx:2196
void SetPointer(PointerStyle)
Definition: mouse.cxx:473
const vcl::KeyCode & GetKeyCode() const
Definition: event.hxx:53
Reference< XComponentContext > getProcessComponentContext()
PointerStyle
Definition: ptrstyle.hxx:25
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage)
Definition: builder.cxx:154
WinBits const WB_CENTER
bool IsEnabled() const
Definition: window2.cxx:1116
WinBits GetStyle() const
Definition: window2.cxx:947
vcl::Font GetControlFont() const
Definition: window2.cxx:438
virtual bool set_property(const OString &rKey, const OUString &rValue) override
Definition: fixed.cxx:377
virtual void RequestHelp(const HelpEvent &rHEvt)
Definition: window.cxx:1832
OUString anyToString(uno::Any const &value)