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::ui
;
36 FileControl::FileControl( vcl::Window
* pParent
, WinBits nStyle
) :
37 Window( pParent
, nStyle
|WB_DIALOGCONTROL
),
38 maEdit( VclPtr
<Edit
>::Create(this, (nStyle
&(~WB_BORDER
))|WB_NOTABSTOP
) ),
39 maButton( VclPtr
<PushButton
>::Create( this, (nStyle
&(~WB_BORDER
))|WB_NOLIGHTBORDER
|WB_NOPOINTERFOCUS
|WB_NOTABSTOP
) ),
40 maButtonText( SvlResId(STR_FILECTRL_BUTTONTEXT
) ),
41 mnInternalFlags( FileControlMode_Internal::ORIGINALBUTTONTEXT
)
43 maButton
->SetClickHdl( LINK( this, FileControl
, ButtonHdl
) );
48 SetCompoundControl( true );
50 SetStyle( ImplInitStyle( GetStyle() ) );
54 WinBits
FileControl::ImplInitStyle( WinBits nStyle
)
56 if ( !( nStyle
& WB_NOTABSTOP
) )
58 maEdit
->SetStyle( (maEdit
->GetStyle()|WB_TABSTOP
)&(~WB_NOTABSTOP
) );
59 maButton
->SetStyle( (maButton
->GetStyle()|WB_TABSTOP
)&(~WB_NOTABSTOP
) );
63 maEdit
->SetStyle( (maEdit
->GetStyle()|WB_NOTABSTOP
)&(~WB_TABSTOP
) );
64 maButton
->SetStyle( (maButton
->GetStyle()|WB_NOTABSTOP
)&(~WB_TABSTOP
) );
67 const WinBits nAlignmentStyle
= ( WB_TOP
| WB_VCENTER
| WB_BOTTOM
);
68 maEdit
->SetStyle( ( maEdit
->GetStyle() & ~nAlignmentStyle
) | ( nStyle
& nAlignmentStyle
) );
70 if ( !(nStyle
& WB_NOGROUP
) )
73 if ( !(nStyle
& WB_NOBORDER
) )
76 nStyle
&= ~WB_TABSTOP
;
82 FileControl::~FileControl()
87 void FileControl::dispose()
89 maEdit
.disposeAndClear();
90 maButton
.disposeAndClear();
94 void FileControl::SetText( const OUString
& rStr
)
96 maEdit
->SetText( rStr
);
100 OUString
FileControl::GetText() const
102 return maEdit
->GetText();
106 void FileControl::StateChanged( StateChangedType nType
)
108 if ( nType
== StateChangedType::Enable
)
110 maEdit
->Enable( IsEnabled() );
111 maButton
->Enable( IsEnabled() );
113 else if ( nType
== StateChangedType::Zoom
)
115 GetEdit().SetZoom( GetZoom() );
116 GetButton().SetZoom( GetZoom() );
118 else if ( nType
== StateChangedType::Style
)
120 SetStyle( ImplInitStyle( GetStyle() ) );
122 else if ( nType
== StateChangedType::ControlFont
)
124 GetEdit().SetControlFont( GetControlFont() );
125 // Only use height of the button, as in HTML
126 // always Courier is used
127 vcl::Font aFont
= GetButton().GetControlFont();
128 aFont
.SetFontSize( GetControlFont().GetFontSize() );
129 GetButton().SetControlFont( aFont
);
131 else if ( nType
== StateChangedType::ControlForeground
)
133 GetEdit().SetControlForeground( GetControlForeground() );
134 GetButton().SetControlForeground( GetControlForeground() );
136 else if ( nType
== StateChangedType::ControlBackground
)
138 GetEdit().SetControlBackground( GetControlBackground() );
139 GetButton().SetControlBackground( GetControlBackground() );
141 Window::StateChanged( nType
);
145 void FileControl::Resize()
147 static const tools::Long ButtonBorder
= 10;
149 if( mnInternalFlags
& FileControlMode_Internal::INRESIZE
)
151 mnInternalFlags
|= FileControlMode_Internal::INRESIZE
;//InResize = sal_True
153 Size aOutSz
= GetOutputSizePixel();
154 tools::Long nButtonTextWidth
= maButton
->GetTextWidth( maButtonText
);
155 if ( !(mnInternalFlags
& FileControlMode_Internal::ORIGINALBUTTONTEXT
) ||
156 ( nButtonTextWidth
< aOutSz
.Width()/3 ) )
158 maButton
->SetText( maButtonText
);
162 OUString
aSmallText( u
"..."_ustr
);
163 maButton
->SetText( aSmallText
);
164 nButtonTextWidth
= maButton
->GetTextWidth( aSmallText
);
167 tools::Long nButtonWidth
= nButtonTextWidth
+ButtonBorder
;
168 maEdit
->setPosSizePixel( 0, 0, aOutSz
.Width()-nButtonWidth
, aOutSz
.Height() );
169 maButton
->setPosSizePixel( aOutSz
.Width()-nButtonWidth
, 0, nButtonWidth
, aOutSz
.Height() );
171 mnInternalFlags
&= ~FileControlMode_Internal::INRESIZE
; //InResize = sal_False
175 void FileControl::GetFocus()
177 if (!maEdit
|| maEdit
->isDisposed())
182 void FileControl::SetEditModifyHdl( const Link
<Edit
&,void>& rLink
)
184 if (!maEdit
|| maEdit
->isDisposed())
186 maEdit
->SetModifyHdl(rLink
);
189 void FileControl::Draw( OutputDevice
* pDev
, const Point
& rPos
, SystemTextColorFlags nFlags
)
191 WinBits nOldEditStyle
= GetEdit().GetStyle();
192 if ( GetStyle() & WB_BORDER
)
193 GetEdit().SetStyle( nOldEditStyle
|WB_BORDER
);
194 Size
aOrigSize(GetEdit().GetSizePixel());
195 GetEdit().SetSizePixel(GetSizePixel());
196 GetEdit().Draw( pDev
, rPos
, nFlags
);
197 GetEdit().SetSizePixel(aOrigSize
);
198 if ( GetStyle() & WB_BORDER
)
199 GetEdit().SetStyle( nOldEditStyle
);
202 IMPL_LINK_NOARG(FileControl
, ButtonHdl
, Button
*, void)
206 const Reference
< XComponentContext
>& xContext
= comphelper::getProcessComponentContext();
207 Reference
< dialogs::XFilePicker3
> xFilePicker
= dialogs::FilePicker::createWithMode( xContext
, dialogs::TemplateDescription::FILEOPEN_SIMPLE
);
208 // transform the system notation text into a file URL
209 OUString sSystemNotation
= GetText(), sFileURL
;
210 oslFileError nError
= osl_getFileURLFromSystemPath( sSystemNotation
.pData
, &sFileURL
.pData
);
211 if ( nError
== osl_File_E_INVAL
)
212 sFileURL
= GetText(); // #97709# Maybe URL is already a file URL...
214 //#90430# Check if URL is really a file URL
216 if ( osl_getSystemPathFromFileURL( sFileURL
.pData
, &aTmp
.pData
) == osl_File_E_None
)
218 // initially set this directory
219 xFilePicker
->setDisplayDirectory( sFileURL
);
222 if ( xFilePicker
->execute() )
224 Sequence
< OUString
> aPathSeq
= xFilePicker
->getSelectedFiles();
226 if ( aPathSeq
.hasElements() )
228 OUString aNewText
= aPathSeq
[0];
229 INetURLObject
aObj( aNewText
);
230 if ( aObj
.GetProtocol() == INetProtocol::File
)
231 aNewText
= aObj
.PathToFileName();
233 maEdit
->GetModifyHdl().Call( *maEdit
);
237 catch( const Exception
& )
239 TOOLS_WARN_EXCEPTION( "toolkit", "FileControl::ImplBrowseFile: caught an exception while executing the file picker!" );
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */