1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <svtools/filectrl.hxx>
22 #include <com/sun/star/ui/dialogs/FilePicker.hpp>
23 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
24 #include <comphelper/processfactory.hxx>
26 #include <svtools/svtresid.hxx>
27 #include <tools/urlobj.hxx>
28 #include <vcl/stdtext.hxx>
29 #include <filectrl.hrc>
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::lang
;
33 using namespace ::com::sun::star::ui
;
37 FileControl::FileControl( vcl::Window
* pParent
, WinBits nStyle
, FileControlMode nFlags
) :
38 Window( pParent
, nStyle
|WB_DIALOGCONTROL
),
39 maEdit( VclPtr
<Edit
>::Create(this, (nStyle
&(~WB_BORDER
))|WB_NOTABSTOP
) ),
40 maButton( VclPtr
<PushButton
>::Create( this, (nStyle
&(~WB_BORDER
))|WB_NOLIGHTBORDER
|WB_NOPOINTERFOCUS
|WB_NOTABSTOP
) ),
41 maButtonText( SVT_RESSTR(STR_FILECTRL_BUTTONTEXT
) ),
43 mnInternalFlags( FileControlMode_Internal::ORIGINALBUTTONTEXT
)
45 maButton
->SetClickHdl( LINK( this, FileControl
, ButtonHdl
) );
51 SetCompoundControl( true );
53 SetStyle( ImplInitStyle( GetStyle() ) );
58 WinBits
FileControl::ImplInitStyle( WinBits nStyle
)
60 if ( !( nStyle
& WB_NOTABSTOP
) )
62 maEdit
->SetStyle( (maEdit
->GetStyle()|WB_TABSTOP
)&(~WB_NOTABSTOP
) );
63 maButton
->SetStyle( (maButton
->GetStyle()|WB_TABSTOP
)&(~WB_NOTABSTOP
) );
67 maEdit
->SetStyle( (maEdit
->GetStyle()|WB_NOTABSTOP
)&(~WB_TABSTOP
) );
68 maButton
->SetStyle( (maButton
->GetStyle()|WB_NOTABSTOP
)&(~WB_TABSTOP
) );
71 const WinBits nAlignmentStyle
= ( WB_TOP
| WB_VCENTER
| WB_BOTTOM
);
72 maEdit
->SetStyle( ( maEdit
->GetStyle() & ~nAlignmentStyle
) | ( nStyle
& nAlignmentStyle
) );
74 if ( !(nStyle
& WB_NOGROUP
) )
77 if ( !(nStyle
& WB_NOBORDER
) )
80 nStyle
&= ~WB_TABSTOP
;
87 FileControl::~FileControl()
92 void FileControl::dispose()
94 maEdit
.disposeAndClear();
95 maButton
.disposeAndClear();
99 void FileControl::SetText( const OUString
& rStr
)
101 maEdit
->SetText( rStr
);
102 if ( mnFlags
& FileControlMode::RESIZEBUTTONBYPATHLEN
)
108 OUString
FileControl::GetText() const
110 return maEdit
->GetText();
115 void FileControl::StateChanged( StateChangedType nType
)
117 if ( nType
== StateChangedType::Enable
)
119 maEdit
->Enable( IsEnabled() );
120 maButton
->Enable( IsEnabled() );
122 else if ( nType
== StateChangedType::Zoom
)
124 GetEdit().SetZoom( GetZoom() );
125 GetButton().SetZoom( GetZoom() );
127 else if ( nType
== StateChangedType::Style
)
129 SetStyle( ImplInitStyle( GetStyle() ) );
131 else if ( nType
== StateChangedType::ControlFont
)
133 GetEdit().SetControlFont( GetControlFont() );
134 // Only use height of the button, as in HTML
135 // always Courier is used
136 vcl::Font aFont
= GetButton().GetControlFont();
137 aFont
.SetSize( GetControlFont().GetSize() );
138 GetButton().SetControlFont( aFont
);
140 else if ( nType
== StateChangedType::ControlForeground
)
142 GetEdit().SetControlForeground( GetControlForeground() );
143 GetButton().SetControlForeground( GetControlForeground() );
145 else if ( nType
== StateChangedType::ControlBackground
)
147 GetEdit().SetControlBackground( GetControlBackground() );
148 GetButton().SetControlBackground( GetControlBackground() );
150 Window::StateChanged( nType
);
155 void FileControl::Resize()
157 static long ButtonBorder
= 10;
159 if( mnInternalFlags
& FileControlMode_Internal::INRESIZE
)
161 mnInternalFlags
|= FileControlMode_Internal::INRESIZE
;//InResize = sal_True
163 Size aOutSz
= GetOutputSizePixel();
164 long nButtonTextWidth
= maButton
->GetTextWidth( maButtonText
);
165 if ( !(mnInternalFlags
& FileControlMode_Internal::ORIGINALBUTTONTEXT
) ||
166 ( nButtonTextWidth
< aOutSz
.Width()/3 &&
167 ( !( mnFlags
& FileControlMode::RESIZEBUTTONBYPATHLEN
) ||
168 ( maEdit
->GetTextWidth( maEdit
->GetText() )
169 <= aOutSz
.Width() - nButtonTextWidth
- ButtonBorder
) ) ) )
171 maButton
->SetText( maButtonText
);
175 OUString
aSmallText( "..." );
176 maButton
->SetText( aSmallText
);
177 nButtonTextWidth
= maButton
->GetTextWidth( aSmallText
);
180 long nButtonWidth
= nButtonTextWidth
+ButtonBorder
;
181 maEdit
->setPosSizePixel( 0, 0, aOutSz
.Width()-nButtonWidth
, aOutSz
.Height() );
182 maButton
->setPosSizePixel( aOutSz
.Width()-nButtonWidth
, 0, nButtonWidth
, aOutSz
.Height() );
184 mnInternalFlags
&= ~FileControlMode_Internal::INRESIZE
; //InResize = sal_False
189 IMPL_LINK_NOARG(FileControl
, ButtonHdl
)
196 void FileControl::GetFocus()
201 void FileControl::SetEditModifyHdl( const Link
<>& rLink
)
203 if (!maEdit
|| maEdit
->IsDisposed())
205 maEdit
->SetModifyHdl(rLink
);
208 void FileControl::Draw( OutputDevice
* pDev
, const Point
& rPos
, const Size
& rSize
, sal_uLong nFlags
)
210 WinBits nOldEditStyle
= GetEdit().GetStyle();
211 if ( GetStyle() & WB_BORDER
)
212 GetEdit().SetStyle( nOldEditStyle
|WB_BORDER
);
213 GetEdit().Draw( pDev
, rPos
, rSize
, nFlags
);
214 if ( GetStyle() & WB_BORDER
)
215 GetEdit().SetStyle( nOldEditStyle
);
218 void FileControl::ImplBrowseFile( )
222 Reference
< XComponentContext
> xContext
= comphelper::getProcessComponentContext();
223 Reference
< dialogs::XFilePicker3
> xFilePicker
= dialogs::FilePicker::createWithMode( xContext
, dialogs::TemplateDescription::FILEOPEN_SIMPLE
);
224 // transform the system notation text into a file URL
225 OUString sSystemNotation
= GetText(), sFileURL
;
226 oslFileError nError
= osl_getFileURLFromSystemPath( sSystemNotation
.pData
, &sFileURL
.pData
);
227 if ( nError
== osl_File_E_INVAL
)
228 sFileURL
= GetText(); // #97709# Maybe URL is already a file URL...
230 //#90430# Check if URL is really a file URL
232 if ( osl_getSystemPathFromFileURL( sFileURL
.pData
, &aTmp
.pData
) == osl_File_E_None
)
234 // initially set this directory
235 xFilePicker
->setDisplayDirectory( sFileURL
);
238 if ( xFilePicker
->execute() )
240 Sequence
< OUString
> aPathSeq
= xFilePicker
->getFiles();
242 if ( aPathSeq
.getLength() )
244 OUString aNewText
= aPathSeq
[0];
245 INetURLObject
aObj( aNewText
);
246 if ( aObj
.GetProtocol() == INetProtocol::File
)
247 aNewText
= aObj
.PathToFileName();
249 maEdit
->GetModifyHdl().Call( maEdit
.get() );
253 catch( const Exception
& )
255 OSL_FAIL( "FileControl::ImplBrowseFile: caught an exception while executing the file picker!" );
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */