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 : maEdit( this, (nStyle&(~WB_BORDER))|WB_NOTABSTOP ),
40 : maButton( this, (nStyle&(~WB_BORDER))|WB_NOLIGHTBORDER|WB_NOPOINTERFOCUS|WB_NOTABSTOP ),
41 : maButtonText( SVT_RESSTR(STR_FILECTRL_BUTTONTEXT) ),
42 : mnFlags( nFlags ),
43 0 : mnInternalFlags( FILECTRL_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 : }
90 :
91 :
92 :
93 0 : void FileControl::SetText( const OUString& rStr )
94 : {
95 0 : maEdit.SetText( rStr );
96 0 : if ( mnFlags & FILECTRL_RESIZEBUTTONBYPATHLEN )
97 0 : Resize();
98 0 : }
99 :
100 :
101 :
102 0 : OUString FileControl::GetText() const
103 : {
104 0 : return maEdit.GetText();
105 : }
106 :
107 :
108 :
109 0 : void FileControl::StateChanged( StateChangedType nType )
110 : {
111 0 : if ( nType == StateChangedType::ENABLE )
112 : {
113 0 : maEdit.Enable( IsEnabled() );
114 0 : maButton.Enable( IsEnabled() );
115 : }
116 0 : else if ( nType == StateChangedType::ZOOM )
117 : {
118 0 : GetEdit().SetZoom( GetZoom() );
119 0 : GetButton().SetZoom( GetZoom() );
120 : }
121 0 : else if ( nType == StateChangedType::STYLE )
122 : {
123 0 : SetStyle( ImplInitStyle( GetStyle() ) );
124 : }
125 0 : else if ( nType == StateChangedType::CONTROLFONT )
126 : {
127 0 : GetEdit().SetControlFont( GetControlFont() );
128 : // Only use height of the button, as in HTML
129 : // always Courier is used
130 0 : vcl::Font aFont = GetButton().GetControlFont();
131 0 : aFont.SetSize( GetControlFont().GetSize() );
132 0 : GetButton().SetControlFont( aFont );
133 : }
134 0 : else if ( nType == StateChangedType::CONTROLFOREGROUND )
135 : {
136 0 : GetEdit().SetControlForeground( GetControlForeground() );
137 0 : GetButton().SetControlForeground( GetControlForeground() );
138 : }
139 0 : else if ( nType == StateChangedType::CONTROLBACKGROUND )
140 : {
141 0 : GetEdit().SetControlBackground( GetControlBackground() );
142 0 : GetButton().SetControlBackground( GetControlBackground() );
143 : }
144 0 : Window::StateChanged( nType );
145 0 : }
146 :
147 :
148 :
149 0 : void FileControl::Resize()
150 : {
151 : static long ButtonBorder = 10;
152 :
153 0 : if( mnInternalFlags & FILECTRL_INRESIZE )
154 0 : return;
155 0 : mnInternalFlags |= FILECTRL_INRESIZE;//InResize = sal_True
156 :
157 0 : Size aOutSz = GetOutputSizePixel();
158 0 : long nButtonTextWidth = maButton.GetTextWidth( maButtonText );
159 0 : if ( ((mnInternalFlags & FILECTRL_ORIGINALBUTTONTEXT) == 0) ||
160 0 : ( nButtonTextWidth < aOutSz.Width()/3 &&
161 0 : ( mnFlags & FILECTRL_RESIZEBUTTONBYPATHLEN
162 0 : ? ( maEdit.GetTextWidth( maEdit.GetText() )
163 0 : <= aOutSz.Width() - nButtonTextWidth - ButtonBorder )
164 0 : : sal_True ) )
165 : )
166 : {
167 0 : maButton.SetText( maButtonText );
168 : }
169 : else
170 : {
171 0 : OUString aSmallText( "..." );
172 0 : maButton.SetText( aSmallText );
173 0 : nButtonTextWidth = maButton.GetTextWidth( aSmallText );
174 : }
175 :
176 0 : long nButtonWidth = nButtonTextWidth+ButtonBorder;
177 0 : maEdit.setPosSizePixel( 0, 0, aOutSz.Width()-nButtonWidth, aOutSz.Height() );
178 0 : maButton.setPosSizePixel( aOutSz.Width()-nButtonWidth, 0, nButtonWidth, aOutSz.Height() );
179 :
180 0 : mnInternalFlags &= ~FILECTRL_INRESIZE; //InResize = sal_False
181 : }
182 :
183 :
184 :
185 0 : IMPL_LINK_NOARG(FileControl, ButtonHdl)
186 : {
187 0 : ImplBrowseFile( );
188 :
189 0 : return 0;
190 : }
191 :
192 :
193 :
194 0 : void FileControl::GetFocus()
195 : {
196 0 : maEdit.GrabFocus();
197 0 : }
198 :
199 :
200 :
201 0 : void FileControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, sal_uLong nFlags )
202 : {
203 0 : WinBits nOldEditStyle = GetEdit().GetStyle();
204 0 : if ( GetStyle() & WB_BORDER )
205 0 : GetEdit().SetStyle( nOldEditStyle|WB_BORDER );
206 0 : GetEdit().Draw( pDev, rPos, rSize, nFlags );
207 0 : if ( GetStyle() & WB_BORDER )
208 0 : GetEdit().SetStyle( nOldEditStyle );
209 0 : }
210 :
211 0 : void FileControl::ImplBrowseFile( )
212 : {
213 : try
214 : {
215 0 : Reference< XComponentContext > xContext = comphelper::getProcessComponentContext();
216 0 : Reference < dialogs::XFilePicker3 > xFilePicker = dialogs::FilePicker::createWithMode( xContext, dialogs::TemplateDescription::FILEOPEN_SIMPLE );
217 : // transform the system notation text into a file URL
218 0 : OUString sSystemNotation = GetText(), sFileURL;
219 0 : oslFileError nError = osl_getFileURLFromSystemPath( sSystemNotation.pData, &sFileURL.pData );
220 0 : if ( nError == osl_File_E_INVAL )
221 0 : sFileURL = GetText(); // #97709# Maybe URL is already a file URL...
222 :
223 : //#90430# Check if URL is really a file URL
224 0 : OUString aTmp;
225 0 : if ( osl_getSystemPathFromFileURL( sFileURL.pData, &aTmp.pData ) == osl_File_E_None )
226 : {
227 : // initially set this directory
228 0 : xFilePicker->setDisplayDirectory( sFileURL );
229 : }
230 :
231 0 : if ( xFilePicker->execute() )
232 : {
233 0 : Sequence < OUString > aPathSeq = xFilePicker->getFiles();
234 :
235 0 : if ( aPathSeq.getLength() )
236 : {
237 0 : OUString aNewText = aPathSeq[0];
238 0 : INetURLObject aObj( aNewText );
239 0 : if ( aObj.GetProtocol() == INET_PROT_FILE )
240 0 : aNewText = aObj.PathToFileName();
241 0 : SetText( aNewText );
242 0 : maEdit.GetModifyHdl().Call( &maEdit );
243 0 : }
244 0 : }
245 : }
246 0 : catch( const Exception& )
247 : {
248 : OSL_FAIL( "FileControl::ImplBrowseFile: caught an exception while executing the file picker!" );
249 : }
250 1227 : }
251 :
252 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|