1*5900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5900e8ecSAndrew Rist  * distributed with this work for additional information
6*5900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
9*5900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5900e8ecSAndrew Rist  *
11*5900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5900e8ecSAndrew Rist  *
13*5900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5900e8ecSAndrew Rist  * software distributed under the License is distributed on an
15*5900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
17*5900e8ecSAndrew Rist  * specific language governing permissions and limitations
18*5900e8ecSAndrew Rist  * under the License.
19*5900e8ecSAndrew Rist  *
20*5900e8ecSAndrew Rist  *************************************************************/
21*5900e8ecSAndrew Rist 
22*5900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #define _SV_FIELCTRL_CXX
28cdf0e10cSrcweir #include <tools/urlobj.hxx>
29cdf0e10cSrcweir #include <svtools/svtdata.hxx>
30cdf0e10cSrcweir #include <svtools/filectrl.hxx>
31cdf0e10cSrcweir #include <filectrl.hrc>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir // =======================================================================
34cdf0e10cSrcweir 
FileControl(Window * pParent,WinBits nStyle,FileControlMode nFlags)35cdf0e10cSrcweir FileControl::FileControl( Window* pParent, WinBits nStyle, FileControlMode nFlags ) :
36cdf0e10cSrcweir 	Window( pParent, nStyle|WB_DIALOGCONTROL ),
37cdf0e10cSrcweir 	maEdit( this, (nStyle&(~WB_BORDER))|WB_NOTABSTOP ),
38cdf0e10cSrcweir 	maButton( this, (nStyle&(~WB_BORDER))|WB_NOLIGHTBORDER|WB_NOPOINTERFOCUS|WB_NOTABSTOP ),
39cdf0e10cSrcweir 	maButtonText( SvtResId( STR_FILECTRL_BUTTONTEXT ) ),
40cdf0e10cSrcweir 	mnFlags( nFlags ),
41cdf0e10cSrcweir 	mnInternalFlags( FILECTRL_ORIGINALBUTTONTEXT )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir 	maButton.SetClickHdl( LINK( this, FileControl, ButtonHdl ) );
44cdf0e10cSrcweir 	mbOpenDlg = sal_True;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 	maButton.Show();
47cdf0e10cSrcweir 	maEdit.Show();
48cdf0e10cSrcweir 
49cdf0e10cSrcweir 	SetCompoundControl( sal_True );
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	SetStyle( ImplInitStyle( GetStyle() ) );
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // -----------------------------------------------------------------------
55cdf0e10cSrcweir 
ImplInitStyle(WinBits nStyle)56cdf0e10cSrcweir WinBits FileControl::ImplInitStyle( WinBits nStyle )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir 	if ( !( nStyle & WB_NOTABSTOP ) )
59cdf0e10cSrcweir 	{
60cdf0e10cSrcweir 		maEdit.SetStyle( (maEdit.GetStyle()|WB_TABSTOP)&(~WB_NOTABSTOP) );
61cdf0e10cSrcweir 		maButton.SetStyle( (maButton.GetStyle()|WB_TABSTOP)&(~WB_NOTABSTOP) );
62cdf0e10cSrcweir 	}
63cdf0e10cSrcweir 	else
64cdf0e10cSrcweir 	{
65cdf0e10cSrcweir 		maEdit.SetStyle( (maEdit.GetStyle()|WB_NOTABSTOP)&(~WB_TABSTOP) );
66cdf0e10cSrcweir 		maButton.SetStyle( (maButton.GetStyle()|WB_NOTABSTOP)&(~WB_TABSTOP) );
67cdf0e10cSrcweir 	}
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     const WinBits nAlignmentStyle = ( WB_TOP | WB_VCENTER | WB_BOTTOM );
70cdf0e10cSrcweir     maEdit.SetStyle( ( maEdit.GetStyle() & ~nAlignmentStyle ) | ( nStyle & nAlignmentStyle ) );
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	if ( !(nStyle & WB_NOGROUP) )
73cdf0e10cSrcweir 		nStyle |= WB_GROUP;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	if ( !(nStyle & WB_NOBORDER ) )
76cdf0e10cSrcweir 		nStyle |= WB_BORDER;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	nStyle &= ~WB_TABSTOP;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	return nStyle;
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
83cdf0e10cSrcweir // -----------------------------------------------------------------------
84cdf0e10cSrcweir 
~FileControl()85cdf0e10cSrcweir FileControl::~FileControl()
86cdf0e10cSrcweir {
87cdf0e10cSrcweir }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir // -----------------------------------------------------------------------
90cdf0e10cSrcweir 
SetText(const XubString & rStr)91cdf0e10cSrcweir void FileControl::SetText( const XubString& rStr )
92cdf0e10cSrcweir {
93cdf0e10cSrcweir 	maEdit.SetText( rStr );
94cdf0e10cSrcweir 	if ( mnFlags & FILECTRL_RESIZEBUTTONBYPATHLEN )
95cdf0e10cSrcweir 		Resize();
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir // -----------------------------------------------------------------------
99cdf0e10cSrcweir 
GetText() const100cdf0e10cSrcweir XubString FileControl::GetText() const
101cdf0e10cSrcweir {
102cdf0e10cSrcweir 	return maEdit.GetText();
103cdf0e10cSrcweir }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir // -----------------------------------------------------------------------
106cdf0e10cSrcweir 
StateChanged(StateChangedType nType)107cdf0e10cSrcweir void FileControl::StateChanged( StateChangedType nType )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	if ( nType == STATE_CHANGE_ENABLE )
110cdf0e10cSrcweir 	{
111cdf0e10cSrcweir 		maEdit.Enable( IsEnabled() );
112cdf0e10cSrcweir 		maButton.Enable( IsEnabled() );
113cdf0e10cSrcweir 	}
114cdf0e10cSrcweir 	else if ( nType == STATE_CHANGE_ZOOM )
115cdf0e10cSrcweir 	{
116cdf0e10cSrcweir 		GetEdit().SetZoom( GetZoom() );
117cdf0e10cSrcweir 		GetButton().SetZoom( GetZoom() );
118cdf0e10cSrcweir 	}
119cdf0e10cSrcweir 	else if ( nType == STATE_CHANGE_STYLE )
120cdf0e10cSrcweir 	{
121cdf0e10cSrcweir 		SetStyle( ImplInitStyle( GetStyle() ) );
122cdf0e10cSrcweir 	}
123cdf0e10cSrcweir 	else if ( nType == STATE_CHANGE_CONTROLFONT )
124cdf0e10cSrcweir 	{
125cdf0e10cSrcweir 		GetEdit().SetControlFont( GetControlFont() );
126cdf0e10cSrcweir 		// Fuer den Button nur die Hoehe uebernehmen, weil in
127cdf0e10cSrcweir 		// HTML immer Courier eingestellt wird.
128cdf0e10cSrcweir 		Font aFont = GetButton().GetControlFont();
129cdf0e10cSrcweir 		aFont.SetSize( GetControlFont().GetSize() );
130cdf0e10cSrcweir 		GetButton().SetControlFont( aFont );
131cdf0e10cSrcweir 	}
132cdf0e10cSrcweir 	else if ( nType == STATE_CHANGE_CONTROLFOREGROUND )
133cdf0e10cSrcweir 	{
134cdf0e10cSrcweir 		GetEdit().SetControlForeground( GetControlForeground() );
135cdf0e10cSrcweir 		GetButton().SetControlForeground( GetControlForeground() );
136cdf0e10cSrcweir 	}
137cdf0e10cSrcweir 	else if ( nType == STATE_CHANGE_CONTROLBACKGROUND )
138cdf0e10cSrcweir 	{
139cdf0e10cSrcweir 		GetEdit().SetControlBackground( GetControlBackground() );
140cdf0e10cSrcweir 		GetButton().SetControlBackground( GetControlBackground() );
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir 	Window::StateChanged( nType );
143cdf0e10cSrcweir }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir // -----------------------------------------------------------------------
146cdf0e10cSrcweir 
Resize()147cdf0e10cSrcweir void FileControl::Resize()
148cdf0e10cSrcweir {
149cdf0e10cSrcweir 	static long ButtonBorder = 10;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	if( mnInternalFlags & FILECTRL_INRESIZE )
152cdf0e10cSrcweir 		return;
153cdf0e10cSrcweir 	mnInternalFlags |= FILECTRL_INRESIZE;//InResize = sal_True
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	Size aOutSz = GetOutputSizePixel();
156cdf0e10cSrcweir 	long nButtonTextWidth = maButton.GetTextWidth( maButtonText );
157cdf0e10cSrcweir 	if ( ((mnInternalFlags & FILECTRL_ORIGINALBUTTONTEXT) == 0) ||
158cdf0e10cSrcweir 		( nButtonTextWidth < aOutSz.Width()/3 &&
159cdf0e10cSrcweir 		( mnFlags & FILECTRL_RESIZEBUTTONBYPATHLEN
160cdf0e10cSrcweir 		? ( maEdit.GetTextWidth( maEdit.GetText() )
161cdf0e10cSrcweir 			<= aOutSz.Width() - nButtonTextWidth - ButtonBorder )
162cdf0e10cSrcweir 		: sal_True ) )
163cdf0e10cSrcweir 	   )
164cdf0e10cSrcweir 	{
165cdf0e10cSrcweir 		maButton.SetText( maButtonText );
166cdf0e10cSrcweir 	}
167cdf0e10cSrcweir 	else
168cdf0e10cSrcweir 	{
169cdf0e10cSrcweir 		XubString aSmallText( RTL_CONSTASCII_USTRINGPARAM( "..." ) );
170cdf0e10cSrcweir 		maButton.SetText( aSmallText );
171cdf0e10cSrcweir 		nButtonTextWidth = maButton.GetTextWidth( aSmallText );
172cdf0e10cSrcweir 	}
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	long nButtonWidth = nButtonTextWidth+ButtonBorder;
175cdf0e10cSrcweir 	maEdit.SetPosSizePixel( 0, 0, aOutSz.Width()-nButtonWidth, aOutSz.Height() );
176cdf0e10cSrcweir 	maButton.SetPosSizePixel( aOutSz.Width()-nButtonWidth, 0, nButtonWidth, aOutSz.Height() );
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 	mnInternalFlags &= ~FILECTRL_INRESIZE; //InResize = sal_False
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir // -----------------------------------------------------------------------
182cdf0e10cSrcweir 
IMPL_LINK(FileControl,ButtonHdl,PushButton *,EMPTYARG)183cdf0e10cSrcweir IMPL_LINK( FileControl, ButtonHdl, PushButton*, EMPTYARG )
184cdf0e10cSrcweir {
185cdf0e10cSrcweir 	ImplBrowseFile( );
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	return 0;
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir // -----------------------------------------------------------------------
191cdf0e10cSrcweir 
GetFocus()192cdf0e10cSrcweir void FileControl::GetFocus()
193cdf0e10cSrcweir {
194cdf0e10cSrcweir 	maEdit.GrabFocus();
195cdf0e10cSrcweir }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir // -----------------------------------------------------------------------
198cdf0e10cSrcweir 
Draw(OutputDevice * pDev,const Point & rPos,const Size & rSize,sal_uLong nFlags)199cdf0e10cSrcweir void FileControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, sal_uLong nFlags )
200cdf0e10cSrcweir {
201cdf0e10cSrcweir 	WinBits nOldEditStyle = GetEdit().GetStyle();
202cdf0e10cSrcweir 	if ( GetStyle() & WB_BORDER )
203cdf0e10cSrcweir 		GetEdit().SetStyle( nOldEditStyle|WB_BORDER );
204cdf0e10cSrcweir 	GetEdit().Draw( pDev, rPos, rSize, nFlags );
205cdf0e10cSrcweir 	if ( GetStyle() & WB_BORDER )
206cdf0e10cSrcweir 		GetEdit().SetStyle( nOldEditStyle );
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir // -----------------------------------------------------------------------
210cdf0e10cSrcweir 
SetButtonText(const XubString & rStr)211cdf0e10cSrcweir void FileControl::SetButtonText( const XubString& rStr )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	mnInternalFlags &= ~FILECTRL_ORIGINALBUTTONTEXT;
214cdf0e10cSrcweir 	maButtonText = rStr;
215cdf0e10cSrcweir 	Resize();
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir // -----------------------------------------------------------------------
219cdf0e10cSrcweir 
ResetButtonText()220cdf0e10cSrcweir void FileControl::ResetButtonText()
221cdf0e10cSrcweir {
222cdf0e10cSrcweir 	mnInternalFlags |= FILECTRL_ORIGINALBUTTONTEXT;
223cdf0e10cSrcweir 	maButtonText = XubString( SvtResId( STR_FILECTRL_BUTTONTEXT ) );
224cdf0e10cSrcweir 	Resize();
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 
228