LibreOffice Module toolkit (master) 1
filectrl.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 <controls/filectrl.hxx>
21
22#include <com/sun/star/ui/dialogs/FilePicker.hpp>
23#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
25#include <osl/file.h>
26#include <svl/svlresid.hxx>
27#include <svl/svl.hrc>
29#include <tools/urlobj.hxx>
30#include <vcl/toolkit/edit.hxx>
31
32using namespace ::com::sun::star::uno;
33using namespace ::com::sun::star::lang;
34using namespace ::com::sun::star::ui;
35
36
38 Window( pParent, nStyle|WB_DIALOGCONTROL ),
39 maEdit( VclPtr<Edit>::Create(this, (nStyle&(~WB_BORDER))|WB_NOTABSTOP) ),
41 maButtonText( SvlResId(STR_FILECTRL_BUTTONTEXT) ),
43{
44 maButton->SetClickHdl( LINK( this, FileControl, ButtonHdl ) );
45
46 maButton->Show();
47 maEdit->Show();
48
49 SetCompoundControl( true );
50
52}
53
54
56{
57 if ( !( nStyle & WB_NOTABSTOP ) )
58 {
59 maEdit->SetStyle( (maEdit->GetStyle()|WB_TABSTOP)&(~WB_NOTABSTOP) );
60 maButton->SetStyle( (maButton->GetStyle()|WB_TABSTOP)&(~WB_NOTABSTOP) );
61 }
62 else
63 {
64 maEdit->SetStyle( (maEdit->GetStyle()|WB_NOTABSTOP)&(~WB_TABSTOP) );
65 maButton->SetStyle( (maButton->GetStyle()|WB_NOTABSTOP)&(~WB_TABSTOP) );
66 }
67
68 const WinBits nAlignmentStyle = ( WB_TOP | WB_VCENTER | WB_BOTTOM );
69 maEdit->SetStyle( ( maEdit->GetStyle() & ~nAlignmentStyle ) | ( nStyle & nAlignmentStyle ) );
70
71 if ( !(nStyle & WB_NOGROUP) )
72 nStyle |= WB_GROUP;
73
74 if ( !(nStyle & WB_NOBORDER ) )
75 nStyle |= WB_BORDER;
76
77 nStyle &= ~WB_TABSTOP;
78
79 return nStyle;
80}
81
82
84{
86}
87
89{
92 Window::dispose();
93}
94
95void FileControl::SetText( const OUString& rStr )
96{
97 maEdit->SetText( rStr );
98}
99
100
101OUString FileControl::GetText() const
102{
103 return maEdit->GetText();
104}
105
106
108{
109 if ( nType == StateChangedType::Enable )
110 {
111 maEdit->Enable( IsEnabled() );
112 maButton->Enable( IsEnabled() );
113 }
114 else if ( nType == StateChangedType::Zoom )
115 {
116 GetEdit().SetZoom( GetZoom() );
118 }
119 else if ( nType == StateChangedType::Style )
120 {
122 }
123 else if ( nType == StateChangedType::ControlFont )
124 {
126 // Only use height of the button, as in HTML
127 // always Courier is used
130 GetButton().SetControlFont( aFont );
131 }
132 else if ( nType == StateChangedType::ControlForeground )
133 {
136 }
137 else if ( nType == StateChangedType::ControlBackground )
138 {
141 }
142 Window::StateChanged( nType );
143}
144
145
147{
148 static const tools::Long ButtonBorder = 10;
149
151 return;
153
154 Size aOutSz = GetOutputSizePixel();
155 tools::Long nButtonTextWidth = maButton->GetTextWidth( maButtonText );
157 ( nButtonTextWidth < aOutSz.Width()/3 ) )
158 {
159 maButton->SetText( maButtonText );
160 }
161 else
162 {
163 OUString aSmallText( "..." );
164 maButton->SetText( aSmallText );
165 nButtonTextWidth = maButton->GetTextWidth( aSmallText );
166 }
167
168 tools::Long nButtonWidth = nButtonTextWidth+ButtonBorder;
169 maEdit->setPosSizePixel( 0, 0, aOutSz.Width()-nButtonWidth, aOutSz.Height() );
170 maButton->setPosSizePixel( aOutSz.Width()-nButtonWidth, 0, nButtonWidth, aOutSz.Height() );
171
173}
174
175
177{
178 if (!maEdit || maEdit->isDisposed())
179 return;
180 maEdit->GrabFocus();
181}
182
184{
185 if (!maEdit || maEdit->isDisposed())
186 return;
187 maEdit->SetModifyHdl(rLink);
188}
189
191{
192 WinBits nOldEditStyle = GetEdit().GetStyle();
193 if ( GetStyle() & WB_BORDER )
194 GetEdit().SetStyle( nOldEditStyle|WB_BORDER );
195 Size aOrigSize(GetEdit().GetSizePixel());
197 GetEdit().Draw( pDev, rPos, nFlags );
198 GetEdit().SetSizePixel(aOrigSize);
199 if ( GetStyle() & WB_BORDER )
200 GetEdit().SetStyle( nOldEditStyle );
201}
202
204{
205 try
206 {
207 Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
208 Reference < dialogs::XFilePicker3 > xFilePicker = dialogs::FilePicker::createWithMode( xContext, dialogs::TemplateDescription::FILEOPEN_SIMPLE );
209 // transform the system notation text into a file URL
210 OUString sSystemNotation = GetText(), sFileURL;
211 oslFileError nError = osl_getFileURLFromSystemPath( sSystemNotation.pData, &sFileURL.pData );
212 if ( nError == osl_File_E_INVAL )
213 sFileURL = GetText(); // #97709# Maybe URL is already a file URL...
214
215 //#90430# Check if URL is really a file URL
216 OUString aTmp;
217 if ( osl_getSystemPathFromFileURL( sFileURL.pData, &aTmp.pData ) == osl_File_E_None )
218 {
219 // initially set this directory
220 xFilePicker->setDisplayDirectory( sFileURL );
221 }
222
223 if ( xFilePicker->execute() )
224 {
225 Sequence < OUString > aPathSeq = xFilePicker->getSelectedFiles();
226
227 if ( aPathSeq.hasElements() )
228 {
229 OUString aNewText = aPathSeq[0];
230 INetURLObject aObj( aNewText );
231 if ( aObj.GetProtocol() == INetProtocol::File )
232 aNewText = aObj.PathToFileName();
233 SetText( aNewText );
234 maEdit->GetModifyHdl().Call( *maEdit );
235 }
236 }
237 }
238 catch( const Exception& )
239 {
240 TOOLS_WARN_EXCEPTION( "toolkit", "FileControl::ImplBrowseFile: caught an exception while executing the file picker!" );
241 }
242}
243
244/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SystemTextColorFlags
virtual void Draw(OutputDevice *pDev, const Point &rPos, SystemTextColorFlags nFlags) override
void SetText(const OUString &rStr) override
Definition: filectrl.cxx:95
OUString maButtonText
Definition: filectrl.hxx:44
virtual void dispose() override
Definition: filectrl.cxx:88
FileControl(vcl::Window *pParent, WinBits nStyle)
Definition: filectrl.cxx:37
WinBits ImplInitStyle(WinBits nStyle)
Definition: filectrl.cxx:55
void StateChanged(StateChangedType nType) override
Definition: filectrl.cxx:107
PushButton & GetButton()
Definition: filectrl.hxx:59
OUString GetText() const override
Definition: filectrl.cxx:101
void SetEditModifyHdl(const Link< Edit &, void > &rLink)
Definition: filectrl.cxx:183
Edit & GetEdit()
Definition: filectrl.hxx:58
void Resize() override
Definition: filectrl.cxx:146
void Draw(OutputDevice *pDev, const Point &rPos, SystemTextColorFlags nFlags) override
Definition: filectrl.cxx:190
VclPtr< Edit > maEdit
Definition: filectrl.hxx:42
virtual ~FileControl() override
Definition: filectrl.cxx:83
FileControlMode_Internal mnInternalFlags
Definition: filectrl.hxx:45
VclPtr< PushButton > maButton
Definition: filectrl.hxx:43
void GetFocus() override
Definition: filectrl.cxx:176
OUString PathToFileName() const
INetProtocol GetProtocol() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
void disposeAndClear()
void SetFontSize(const Size &)
void SetStyle(WinBits nStyle)
void SetControlForeground()
void SetControlFont()
virtual void SetSizePixel(const Size &rNewSize)
const Color & GetControlForeground() const
void SetControlBackground()
WinBits GetStyle() const
const Fraction & GetZoom() const
vcl::Font GetControlFont() const
void SetZoom(const Fraction &rZoom)
virtual Size GetSizePixel() const
Size GetOutputSizePixel() const
const Color & GetControlBackground() const
bool IsEnabled() const
void SetCompoundControl(bool bCompound)
#define TOOLS_WARN_EXCEPTION(area, stream)
virtual void SetText(const OUString &rStr) override
IMPL_LINK_NOARG(FileControl, ButtonHdl, Button *, void)
Definition: filectrl.cxx:203
FileControlMode_Internal
Definition: filectrl.hxx:29
sal_uInt16 GetFontSize(sal_uInt16 nPos)
@ Exception
Reference< XComponentContext > getProcessComponentContext()
void Create(SwFormatVertOrient &rItem, SvStream &rStrm, sal_uInt16 nVersionAbusedAsSize)
long Long
QPRO_FUNC_TYPE nType
SVL_DLLPUBLIC OUString SvlResId(TranslateId sContextAndId)
const tools::Long nButtonWidth
StateChangedType
sal_Int64 WinBits
WinBits const WB_TOP
WinBits const WB_VCENTER
WinBits const WB_DIALOGCONTROL
WinBits const WB_NOTABSTOP
WinBits const WB_NOPOINTERFOCUS
WinBits const WB_GROUP
WinBits const WB_BORDER
WinBits const WB_NOGROUP
WinBits const WB_NOLIGHTBORDER
WinBits const WB_TABSTOP
WinBits const WB_BOTTOM
WinBits const WB_NOBORDER