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 <controls/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 <svl/svlresid.hxx>
27 #include <svl/svl.hrc>
28 #include <comphelper/diagnose_ex.hxx>
29 #include <tools/urlobj.hxx>
30 #include <vcl/toolkit/edit.hxx>
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::lang
;
34 using namespace ::com::sun::star::ui
;
37 FileControl::FileControl( vcl::Window
* pParent
, WinBits nStyle
) :
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( SvlResId(STR_FILECTRL_BUTTONTEXT
) ),
42 mnInternalFlags( FileControlMode_Internal::ORIGINALBUTTONTEXT
)
44 maButton
->SetClickHdl( LINK( this, FileControl
, ButtonHdl
) );
49 SetCompoundControl( true );
51 SetStyle( ImplInitStyle( GetStyle() ) );
55 WinBits
FileControl::ImplInitStyle( WinBits nStyle
)
57 if ( !( nStyle
& WB_NOTABSTOP
) )
59 maEdit
->SetStyle( (maEdit
->GetStyle()|WB_TABSTOP
)&(~WB_NOTABSTOP
) );
60 maButton
->SetStyle( (maButton
->GetStyle()|WB_TABSTOP
)&(~WB_NOTABSTOP
) );
64 maEdit
->SetStyle( (maEdit
->GetStyle()|WB_NOTABSTOP
)&(~WB_TABSTOP
) );
65 maButton
->SetStyle( (maButton
->GetStyle()|WB_NOTABSTOP
)&(~WB_TABSTOP
) );
68 const WinBits nAlignmentStyle
= ( WB_TOP
| WB_VCENTER
| WB_BOTTOM
);
69 maEdit
->SetStyle( ( maEdit
->GetStyle() & ~nAlignmentStyle
) | ( nStyle
& nAlignmentStyle
) );
71 if ( !(nStyle
& WB_NOGROUP
) )
74 if ( !(nStyle
& WB_NOBORDER
) )
77 nStyle
&= ~WB_TABSTOP
;
83 FileControl::~FileControl()
88 void FileControl::dispose()
90 maEdit
.disposeAndClear();
91 maButton
.disposeAndClear();
95 void FileControl::SetText( const OUString
& rStr
)
97 maEdit
->SetText( rStr
);
101 OUString
FileControl::GetText() const
103 return maEdit
->GetText();
107 void FileControl::StateChanged( StateChangedType nType
)
109 if ( nType
== StateChangedType::Enable
)
111 maEdit
->Enable( IsEnabled() );
112 maButton
->Enable( IsEnabled() );
114 else if ( nType
== StateChangedType::Zoom
)
116 GetEdit().SetZoom( GetZoom() );
117 GetButton().SetZoom( GetZoom() );
119 else if ( nType
== StateChangedType::Style
)
121 SetStyle( ImplInitStyle( GetStyle() ) );
123 else if ( nType
== StateChangedType::ControlFont
)
125 GetEdit().SetControlFont( GetControlFont() );
126 // Only use height of the button, as in HTML
127 // always Courier is used
128 vcl::Font aFont
= GetButton().GetControlFont();
129 aFont
.SetFontSize( GetControlFont().GetFontSize() );
130 GetButton().SetControlFont( aFont
);
132 else if ( nType
== StateChangedType::ControlForeground
)
134 GetEdit().SetControlForeground( GetControlForeground() );
135 GetButton().SetControlForeground( GetControlForeground() );
137 else if ( nType
== StateChangedType::ControlBackground
)
139 GetEdit().SetControlBackground( GetControlBackground() );
140 GetButton().SetControlBackground( GetControlBackground() );
142 Window::StateChanged( nType
);
146 void FileControl::Resize()
148 static const tools::Long ButtonBorder
= 10;
150 if( mnInternalFlags
& FileControlMode_Internal::INRESIZE
)
152 mnInternalFlags
|= FileControlMode_Internal::INRESIZE
;//InResize = sal_True
154 Size aOutSz
= GetOutputSizePixel();
155 tools::Long nButtonTextWidth
= maButton
->GetTextWidth( maButtonText
);
156 if ( !(mnInternalFlags
& FileControlMode_Internal::ORIGINALBUTTONTEXT
) ||
157 ( nButtonTextWidth
< aOutSz
.Width()/3 ) )
159 maButton
->SetText( maButtonText
);
163 OUString
aSmallText( "..." );
164 maButton
->SetText( aSmallText
);
165 nButtonTextWidth
= maButton
->GetTextWidth( aSmallText
);
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() );
172 mnInternalFlags
&= ~FileControlMode_Internal::INRESIZE
; //InResize = sal_False
176 void FileControl::GetFocus()
178 if (!maEdit
|| maEdit
->isDisposed())
183 void FileControl::SetEditModifyHdl( const Link
<Edit
&,void>& rLink
)
185 if (!maEdit
|| maEdit
->isDisposed())
187 maEdit
->SetModifyHdl(rLink
);
190 void FileControl::Draw( OutputDevice
* pDev
, const Point
& rPos
, SystemTextColorFlags nFlags
)
192 WinBits nOldEditStyle
= GetEdit().GetStyle();
193 if ( GetStyle() & WB_BORDER
)
194 GetEdit().SetStyle( nOldEditStyle
|WB_BORDER
);
195 Size
aOrigSize(GetEdit().GetSizePixel());
196 GetEdit().SetSizePixel(GetSizePixel());
197 GetEdit().Draw( pDev
, rPos
, nFlags
);
198 GetEdit().SetSizePixel(aOrigSize
);
199 if ( GetStyle() & WB_BORDER
)
200 GetEdit().SetStyle( nOldEditStyle
);
203 IMPL_LINK_NOARG(FileControl
, ButtonHdl
, Button
*, void)
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...
215 //#90430# Check if URL is really a file URL
217 if ( osl_getSystemPathFromFileURL( sFileURL
.pData
, &aTmp
.pData
) == osl_File_E_None
)
219 // initially set this directory
220 xFilePicker
->setDisplayDirectory( sFileURL
);
223 if ( xFilePicker
->execute() )
225 Sequence
< OUString
> aPathSeq
= xFilePicker
->getSelectedFiles();
227 if ( aPathSeq
.hasElements() )
229 OUString aNewText
= aPathSeq
[0];
230 INetURLObject
aObj( aNewText
);
231 if ( aObj
.GetProtocol() == INetProtocol::File
)
232 aNewText
= aObj
.PathToFileName();
234 maEdit
->GetModifyHdl().Call( *maEdit
);
238 catch( const Exception
& )
240 TOOLS_WARN_EXCEPTION( "toolkit", "FileControl::ImplBrowseFile: caught an exception while executing the file picker!" );
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */