Line data Source code
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 <svtools/filectrl.hxx>
21 :
22 : #include <com/sun/star/ui/dialogs/FilePicker.hpp>
23 : #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
24 : #include <comphelper/processfactory.hxx>
25 : #include <osl/file.h>
26 : #include <svtools/svtresid.hxx>
27 : #include <tools/urlobj.hxx>
28 : #include <vcl/stdtext.hxx>
29 : #include <filectrl.hrc>
30 :
31 : using namespace ::com::sun::star::uno;
32 : using namespace ::com::sun::star::lang;
33 : using namespace ::com::sun::star::ui;
34 :
35 :
36 :
37 0 : FileControl::FileControl( vcl::Window* pParent, WinBits nStyle, FileControlMode nFlags ) :
38 : Window( pParent, nStyle|WB_DIALOGCONTROL ),
39 0 : maEdit( VclPtr<Edit>::Create(this, (nStyle&(~WB_BORDER))|WB_NOTABSTOP) ),
40 0 : maButton( VclPtr<PushButton>::Create( this, (nStyle&(~WB_BORDER))|WB_NOLIGHTBORDER|WB_NOPOINTERFOCUS|WB_NOTABSTOP ) ),
41 : maButtonText( SVT_RESSTR(STR_FILECTRL_BUTTONTEXT) ),
42 : mnFlags( nFlags ),
43 0 : mnInternalFlags( FileControlMode_Internal::ORIGINALBUTTONTEXT )
44 : {
45 0 : maButton->SetClickHdl( LINK( this, FileControl, ButtonHdl ) );
46 0 : mbOpenDlg = true;
47 :
48 0 : maButton->Show();
49 0 : maEdit->Show();
50 :
51 0 : SetCompoundControl( true );
52 :
53 0 : SetStyle( ImplInitStyle( GetStyle() ) );
54 0 : }
55 :
56 :
57 :
58 0 : WinBits FileControl::ImplInitStyle( WinBits nStyle )
59 : {
60 0 : if ( !( nStyle & WB_NOTABSTOP ) )
61 : {
62 0 : maEdit->SetStyle( (maEdit->GetStyle()|WB_TABSTOP)&(~WB_NOTABSTOP) );
63 0 : maButton->SetStyle( (maButton->GetStyle()|WB_TABSTOP)&(~WB_NOTABSTOP) );
64 : }
65 : else
66 : {
67 0 : maEdit->SetStyle( (maEdit->GetStyle()|WB_NOTABSTOP)&(~WB_TABSTOP) );
68 0 : maButton->SetStyle( (maButton->GetStyle()|WB_NOTABSTOP)&(~WB_TABSTOP) );
69 : }
70 :
71 0 : const WinBits nAlignmentStyle = ( WB_TOP | WB_VCENTER | WB_BOTTOM );
72 0 : maEdit->SetStyle( ( maEdit->GetStyle() & ~nAlignmentStyle ) | ( nStyle & nAlignmentStyle ) );
73 :
74 0 : if ( !(nStyle & WB_NOGROUP) )
75 0 : nStyle |= WB_GROUP;
76 :
77 0 : if ( !(nStyle & WB_NOBORDER ) )
78 0 : nStyle |= WB_BORDER;
79 :
80 0 : nStyle &= ~WB_TABSTOP;
81 :
82 0 : return nStyle;
83 : }
84 :
85 :
86 :
87 0 : FileControl::~FileControl()
88 : {
89 0 : disposeOnce();
90 0 : }
91 :
92 0 : void FileControl::dispose()
93 : {
94 0 : maEdit.disposeAndClear();
95 0 : maButton.disposeAndClear();
96 0 : Window::dispose();
97 0 : }
98 :
99 0 : void FileControl::SetText( const OUString& rStr )
100 : {
101 0 : maEdit->SetText( rStr );
102 0 : if ( mnFlags & FileControlMode::RESIZEBUTTONBYPATHLEN )
103 0 : Resize();
104 0 : }
105 :
106 :
107 :
108 0 : OUString FileControl::GetText() const
109 : {
110 0 : return maEdit->GetText();
111 : }
112 :
113 :
114 :
115 0 : void FileControl::StateChanged( StateChangedType nType )
116 : {
117 0 : if ( nType == StateChangedType::Enable )
118 : {
119 0 : maEdit->Enable( IsEnabled() );
120 0 : maButton->Enable( IsEnabled() );
121 : }
122 0 : else if ( nType == StateChangedType::Zoom )
123 : {
124 0 : GetEdit().SetZoom( GetZoom() );
125 0 : GetButton().SetZoom( GetZoom() );
126 : }
127 0 : else if ( nType == StateChangedType::Style )
128 : {
129 0 : SetStyle( ImplInitStyle( GetStyle() ) );
130 : }
131 0 : else if ( nType == StateChangedType::ControlFont )
132 : {
133 0 : GetEdit().SetControlFont( GetControlFont() );
134 : // Only use height of the button, as in HTML
135 : // always Courier is used
136 0 : vcl::Font aFont = GetButton().GetControlFont();
137 0 : aFont.SetSize( GetControlFont().GetSize() );
138 0 : GetButton().SetControlFont( aFont );
139 : }
140 0 : else if ( nType == StateChangedType::ControlForeground )
141 : {
142 0 : GetEdit().SetControlForeground( GetControlForeground() );
143 0 : GetButton().SetControlForeground( GetControlForeground() );
144 : }
145 0 : else if ( nType == StateChangedType::ControlBackground )
146 : {
147 0 : GetEdit().SetControlBackground( GetControlBackground() );
148 0 : GetButton().SetControlBackground( GetControlBackground() );
149 : }
150 0 : Window::StateChanged( nType );
151 0 : }
152 :
153 :
154 :
155 0 : void FileControl::Resize()
156 : {
157 : static long ButtonBorder = 10;
158 :
159 0 : if( mnInternalFlags & FileControlMode_Internal::INRESIZE )
160 0 : return;
161 0 : mnInternalFlags |= FileControlMode_Internal::INRESIZE;//InResize = sal_True
162 :
163 0 : Size aOutSz = GetOutputSizePixel();
164 0 : long nButtonTextWidth = maButton->GetTextWidth( maButtonText );
165 0 : if ( !(mnInternalFlags & FileControlMode_Internal::ORIGINALBUTTONTEXT) ||
166 0 : ( nButtonTextWidth < aOutSz.Width()/3 &&
167 0 : ( !( mnFlags & FileControlMode::RESIZEBUTTONBYPATHLEN ) ||
168 0 : ( maEdit->GetTextWidth( maEdit->GetText() )
169 0 : <= aOutSz.Width() - nButtonTextWidth - ButtonBorder ) ) ) )
170 : {
171 0 : maButton->SetText( maButtonText );
172 : }
173 : else
174 : {
175 0 : OUString aSmallText( "..." );
176 0 : maButton->SetText( aSmallText );
177 0 : nButtonTextWidth = maButton->GetTextWidth( aSmallText );
178 : }
179 :
180 0 : long nButtonWidth = nButtonTextWidth+ButtonBorder;
181 0 : maEdit->setPosSizePixel( 0, 0, aOutSz.Width()-nButtonWidth, aOutSz.Height() );
182 0 : maButton->setPosSizePixel( aOutSz.Width()-nButtonWidth, 0, nButtonWidth, aOutSz.Height() );
183 :
184 0 : mnInternalFlags &= ~FileControlMode_Internal::INRESIZE; //InResize = sal_False
185 : }
186 :
187 :
188 :
189 0 : IMPL_LINK_NOARG(FileControl, ButtonHdl)
190 : {
191 0 : ImplBrowseFile( );
192 :
193 0 : return 0;
194 : }
195 :
196 0 : void FileControl::GetFocus()
197 : {
198 0 : maEdit->GrabFocus();
199 0 : }
200 :
201 0 : void FileControl::SetEditModifyHdl( const Link<>& rLink )
202 : {
203 0 : if (!maEdit || maEdit->IsDisposed())
204 0 : return;
205 0 : maEdit->SetModifyHdl(rLink);
206 : }
207 :
208 0 : void FileControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags )
209 : {
210 0 : WinBits nOldEditStyle = GetEdit().GetStyle();
211 0 : if ( GetStyle() & WB_BORDER )
212 0 : GetEdit().SetStyle( nOldEditStyle|WB_BORDER );
213 0 : GetEdit().Draw( pDev, rPos, rSize, nFlags );
214 0 : if ( GetStyle() & WB_BORDER )
215 0 : GetEdit().SetStyle( nOldEditStyle );
216 0 : }
217 :
218 0 : void FileControl::ImplBrowseFile( )
219 : {
220 : try
221 : {
222 0 : Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
223 0 : Reference < dialogs::XFilePicker3 > xFilePicker = dialogs::FilePicker::createWithMode( xContext, dialogs::TemplateDescription::FILEOPEN_SIMPLE );
224 : // transform the system notation text into a file URL
225 0 : OUString sSystemNotation = GetText(), sFileURL;
226 0 : oslFileError nError = osl_getFileURLFromSystemPath( sSystemNotation.pData, &sFileURL.pData );
227 0 : if ( nError == osl_File_E_INVAL )
228 0 : sFileURL = GetText(); // #97709# Maybe URL is already a file URL...
229 :
230 : //#90430# Check if URL is really a file URL
231 0 : OUString aTmp;
232 0 : if ( osl_getSystemPathFromFileURL( sFileURL.pData, &aTmp.pData ) == osl_File_E_None )
233 : {
234 : // initially set this directory
235 0 : xFilePicker->setDisplayDirectory( sFileURL );
236 : }
237 :
238 0 : if ( xFilePicker->execute() )
239 : {
240 0 : Sequence < OUString > aPathSeq = xFilePicker->getFiles();
241 :
242 0 : if ( aPathSeq.getLength() )
243 : {
244 0 : OUString aNewText = aPathSeq[0];
245 0 : INetURLObject aObj( aNewText );
246 0 : if ( aObj.GetProtocol() == INetProtocol::File )
247 0 : aNewText = aObj.PathToFileName();
248 0 : SetText( aNewText );
249 0 : maEdit->GetModifyHdl().Call( maEdit.get() );
250 0 : }
251 0 : }
252 : }
253 0 : catch( const Exception& )
254 : {
255 : OSL_FAIL( "FileControl::ImplBrowseFile: caught an exception while executing the file picker!" );
256 : }
257 798 : }
258 :
259 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|