1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 #define _SV_FIELCTRL_CXX
32 #include <tools/urlobj.hxx>
33 #include <svtools/svtdata.hxx>
34 #include <svtools/filectrl.hxx>
35 #include <filectrl.hrc>
36 
37 // =======================================================================
38 
39 FileControl::FileControl( Window* pParent, WinBits nStyle, FileControlMode nFlags ) :
40 	Window( pParent, nStyle|WB_DIALOGCONTROL ),
41 	maEdit( this, (nStyle&(~WB_BORDER))|WB_NOTABSTOP ),
42 	maButton( this, (nStyle&(~WB_BORDER))|WB_NOLIGHTBORDER|WB_NOPOINTERFOCUS|WB_NOTABSTOP ),
43 	maButtonText( SvtResId( STR_FILECTRL_BUTTONTEXT ) ),
44 	mnFlags( nFlags ),
45 	mnInternalFlags( FILECTRL_ORIGINALBUTTONTEXT )
46 {
47 	maButton.SetClickHdl( LINK( this, FileControl, ButtonHdl ) );
48 	mbOpenDlg = sal_True;
49 
50 	maButton.Show();
51 	maEdit.Show();
52 
53 	SetCompoundControl( sal_True );
54 
55 	SetStyle( ImplInitStyle( GetStyle() ) );
56 }
57 
58 // -----------------------------------------------------------------------
59 
60 WinBits FileControl::ImplInitStyle( WinBits nStyle )
61 {
62 	if ( !( nStyle & WB_NOTABSTOP ) )
63 	{
64 		maEdit.SetStyle( (maEdit.GetStyle()|WB_TABSTOP)&(~WB_NOTABSTOP) );
65 		maButton.SetStyle( (maButton.GetStyle()|WB_TABSTOP)&(~WB_NOTABSTOP) );
66 	}
67 	else
68 	{
69 		maEdit.SetStyle( (maEdit.GetStyle()|WB_NOTABSTOP)&(~WB_TABSTOP) );
70 		maButton.SetStyle( (maButton.GetStyle()|WB_NOTABSTOP)&(~WB_TABSTOP) );
71 	}
72 
73     const WinBits nAlignmentStyle = ( WB_TOP | WB_VCENTER | WB_BOTTOM );
74     maEdit.SetStyle( ( maEdit.GetStyle() & ~nAlignmentStyle ) | ( nStyle & nAlignmentStyle ) );
75 
76 	if ( !(nStyle & WB_NOGROUP) )
77 		nStyle |= WB_GROUP;
78 
79 	if ( !(nStyle & WB_NOBORDER ) )
80 		nStyle |= WB_BORDER;
81 
82 	nStyle &= ~WB_TABSTOP;
83 
84 	return nStyle;
85 }
86 
87 // -----------------------------------------------------------------------
88 
89 FileControl::~FileControl()
90 {
91 }
92 
93 // -----------------------------------------------------------------------
94 
95 void FileControl::SetText( const XubString& rStr )
96 {
97 	maEdit.SetText( rStr );
98 	if ( mnFlags & FILECTRL_RESIZEBUTTONBYPATHLEN )
99 		Resize();
100 }
101 
102 // -----------------------------------------------------------------------
103 
104 XubString FileControl::GetText() const
105 {
106 	return maEdit.GetText();
107 }
108 
109 // -----------------------------------------------------------------------
110 
111 void FileControl::StateChanged( StateChangedType nType )
112 {
113 	if ( nType == STATE_CHANGE_ENABLE )
114 	{
115 		maEdit.Enable( IsEnabled() );
116 		maButton.Enable( IsEnabled() );
117 	}
118 	else if ( nType == STATE_CHANGE_ZOOM )
119 	{
120 		GetEdit().SetZoom( GetZoom() );
121 		GetButton().SetZoom( GetZoom() );
122 	}
123 	else if ( nType == STATE_CHANGE_STYLE )
124 	{
125 		SetStyle( ImplInitStyle( GetStyle() ) );
126 	}
127 	else if ( nType == STATE_CHANGE_CONTROLFONT )
128 	{
129 		GetEdit().SetControlFont( GetControlFont() );
130 		// Fuer den Button nur die Hoehe uebernehmen, weil in
131 		// HTML immer Courier eingestellt wird.
132 		Font aFont = GetButton().GetControlFont();
133 		aFont.SetSize( GetControlFont().GetSize() );
134 		GetButton().SetControlFont( aFont );
135 	}
136 	else if ( nType == STATE_CHANGE_CONTROLFOREGROUND )
137 	{
138 		GetEdit().SetControlForeground( GetControlForeground() );
139 		GetButton().SetControlForeground( GetControlForeground() );
140 	}
141 	else if ( nType == STATE_CHANGE_CONTROLBACKGROUND )
142 	{
143 		GetEdit().SetControlBackground( GetControlBackground() );
144 		GetButton().SetControlBackground( GetControlBackground() );
145 	}
146 	Window::StateChanged( nType );
147 }
148 
149 // -----------------------------------------------------------------------
150 
151 void FileControl::Resize()
152 {
153 	static long ButtonBorder = 10;
154 
155 	if( mnInternalFlags & FILECTRL_INRESIZE )
156 		return;
157 	mnInternalFlags |= FILECTRL_INRESIZE;//InResize = sal_True
158 
159 	Size aOutSz = GetOutputSizePixel();
160 	long nButtonTextWidth = maButton.GetTextWidth( maButtonText );
161 	if ( ((mnInternalFlags & FILECTRL_ORIGINALBUTTONTEXT) == 0) ||
162 		( nButtonTextWidth < aOutSz.Width()/3 &&
163 		( mnFlags & FILECTRL_RESIZEBUTTONBYPATHLEN
164 		? ( maEdit.GetTextWidth( maEdit.GetText() )
165 			<= aOutSz.Width() - nButtonTextWidth - ButtonBorder )
166 		: sal_True ) )
167 	   )
168 	{
169 		maButton.SetText( maButtonText );
170 	}
171 	else
172 	{
173 		XubString aSmallText( RTL_CONSTASCII_USTRINGPARAM( "..." ) );
174 		maButton.SetText( aSmallText );
175 		nButtonTextWidth = maButton.GetTextWidth( aSmallText );
176 	}
177 
178 	long nButtonWidth = nButtonTextWidth+ButtonBorder;
179 	maEdit.SetPosSizePixel( 0, 0, aOutSz.Width()-nButtonWidth, aOutSz.Height() );
180 	maButton.SetPosSizePixel( aOutSz.Width()-nButtonWidth, 0, nButtonWidth, aOutSz.Height() );
181 
182 	mnInternalFlags &= ~FILECTRL_INRESIZE; //InResize = sal_False
183 }
184 
185 // -----------------------------------------------------------------------
186 
187 IMPL_LINK( FileControl, ButtonHdl, PushButton*, EMPTYARG )
188 {
189 	ImplBrowseFile( );
190 
191 	return 0;
192 }
193 
194 // -----------------------------------------------------------------------
195 
196 void FileControl::GetFocus()
197 {
198 	maEdit.GrabFocus();
199 }
200 
201 // -----------------------------------------------------------------------
202 
203 void FileControl::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, sal_uLong nFlags )
204 {
205 	WinBits nOldEditStyle = GetEdit().GetStyle();
206 	if ( GetStyle() & WB_BORDER )
207 		GetEdit().SetStyle( nOldEditStyle|WB_BORDER );
208 	GetEdit().Draw( pDev, rPos, rSize, nFlags );
209 	if ( GetStyle() & WB_BORDER )
210 		GetEdit().SetStyle( nOldEditStyle );
211 }
212 
213 // -----------------------------------------------------------------------
214 
215 void FileControl::SetButtonText( const XubString& rStr )
216 {
217 	mnInternalFlags &= ~FILECTRL_ORIGINALBUTTONTEXT;
218 	maButtonText = rStr;
219 	Resize();
220 }
221 
222 // -----------------------------------------------------------------------
223 
224 void FileControl::ResetButtonText()
225 {
226 	mnInternalFlags |= FILECTRL_ORIGINALBUTTONTEXT;
227 	maButtonText = XubString( SvtResId( STR_FILECTRL_BUTTONTEXT ) );
228 	Resize();
229 }
230 
231 
232