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