xref: /aoo41x/main/vcl/source/control/group.cxx (revision cdf0e10c)
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_vcl.hxx"
30 
31 #include <tools/rc.h>
32 
33 #include <vcl/event.hxx>
34 #include <vcl/group.hxx>
35 
36 #include <controldata.hxx>
37 
38 // =======================================================================
39 
40 #define GROUP_BORDER			12
41 #define GROUP_TEXT_BORDER		2
42 
43 #define GROUP_VIEW_STYLE		(WB_3DLOOK | WB_NOLABEL)
44 
45 // =======================================================================
46 
47 void GroupBox::ImplInit( Window* pParent, WinBits nStyle )
48 {
49 	nStyle = ImplInitStyle( nStyle );
50 	Control::ImplInit( pParent, nStyle, NULL );
51 	SetMouseTransparent( sal_True );
52 	ImplInitSettings( sal_True, sal_True, sal_True );
53 }
54 
55 // -----------------------------------------------------------------------
56 
57 WinBits GroupBox::ImplInitStyle( WinBits nStyle )
58 {
59 	if ( !(nStyle & WB_NOGROUP) )
60 		nStyle |= WB_GROUP;
61 	return nStyle;
62 }
63 
64 // -----------------------------------------------------------------
65 
66 const Font& GroupBox::GetCanonicalFont( const StyleSettings& _rStyle ) const
67 {
68     return _rStyle.GetGroupFont();
69 }
70 
71 // -----------------------------------------------------------------
72 const Color& GroupBox::GetCanonicalTextColor( const StyleSettings& _rStyle ) const
73 {
74     return _rStyle.GetGroupTextColor();
75 }
76 
77 // -----------------------------------------------------------------------
78 
79 void GroupBox::ImplInitSettings( sal_Bool bFont,
80 								 sal_Bool bForeground, sal_Bool bBackground )
81 {
82     Control::ImplInitSettings( bFont, bForeground );
83 
84 	if ( bBackground )
85 	{
86 		Window* pParent = GetParent();
87 		if ( (pParent->IsChildTransparentModeEnabled() ||
88 			  !(pParent->GetStyle() & WB_CLIPCHILDREN) ) &&
89 			 !IsControlBackground() )
90 		{
91 			EnableChildTransparentMode( sal_True );
92 			SetParentClipMode( PARENTCLIPMODE_NOCLIP );
93 			SetPaintTransparent( sal_True );
94 			SetBackground();
95 		}
96 		else
97 		{
98 			EnableChildTransparentMode( sal_False );
99 			SetParentClipMode( 0 );
100 			SetPaintTransparent( sal_False );
101 
102 			if ( IsControlBackground() )
103 				SetBackground( GetControlBackground() );
104 			else
105 				SetBackground( pParent->GetBackground() );
106 		}
107 	}
108 }
109 
110 // -----------------------------------------------------------------------
111 
112 GroupBox::GroupBox( Window* pParent, WinBits nStyle ) :
113 	Control( WINDOW_GROUPBOX )
114 {
115 	ImplInit( pParent, nStyle );
116 }
117 
118 // -----------------------------------------------------------------------
119 
120 GroupBox::GroupBox( Window* pParent, const ResId& rResId ) :
121 	Control( WINDOW_GROUPBOX )
122 {
123 	rResId.SetRT( RSC_GROUPBOX );
124 	WinBits nStyle = ImplInitRes( rResId );
125 	ImplInit( pParent, nStyle );
126 	ImplLoadRes( rResId );
127 
128 	if ( !(nStyle & WB_HIDE) )
129 		Show();
130 }
131 
132 // -----------------------------------------------------------------------
133 
134 void GroupBox::ImplDraw( OutputDevice* pDev, sal_uLong nDrawFlags,
135 						 const Point& rPos, const Size& rSize, bool bLayout )
136 {
137 	long					nTop;
138 	long					nTextOff;
139 	const StyleSettings&	rStyleSettings = GetSettings().GetStyleSettings();
140 	XubString				aText( GetText() );
141 	Rectangle				aRect( rPos, rSize );
142 	sal_uInt16					nTextStyle = TEXT_DRAW_LEFT | TEXT_DRAW_TOP | TEXT_DRAW_ENDELLIPSIS | TEXT_DRAW_MNEMONIC;
143 
144 	if ( GetStyle() & WB_NOLABEL )
145 		nTextStyle &= ~TEXT_DRAW_MNEMONIC;
146 	if ( nDrawFlags & WINDOW_DRAW_NOMNEMONIC )
147 	{
148 		if ( nTextStyle & TEXT_DRAW_MNEMONIC )
149 		{
150 			aText = GetNonMnemonicString( aText );
151 			nTextStyle &= ~TEXT_DRAW_MNEMONIC;
152 		}
153 	}
154 	if ( !(nDrawFlags & WINDOW_DRAW_NODISABLE) )
155 	{
156 		if ( !IsEnabled() )
157 			nTextStyle |= TEXT_DRAW_DISABLE;
158 	}
159 	if ( (nDrawFlags & WINDOW_DRAW_MONO) ||
160 		 (rStyleSettings.GetOptions() & STYLE_OPTION_MONO) )
161 	{
162 		nTextStyle |= TEXT_DRAW_MONO;
163 		nDrawFlags |= WINDOW_DRAW_MONO;
164 	}
165 
166 	if ( !aText.Len() )
167 	{
168 		nTop = rPos.Y();
169 		nTextOff = 0;
170 	}
171 	else
172 	{
173 		aRect.Left() += GROUP_BORDER;
174 		aRect.Right() -= GROUP_BORDER;
175 		aRect = pDev->GetTextRect( aRect, aText, nTextStyle );
176 		nTop = rPos.Y();
177 		nTop += aRect.GetHeight() / 2;
178 		nTextOff = GROUP_TEXT_BORDER;
179 	}
180 
181     if( ! bLayout )
182     {
183         if ( nDrawFlags & WINDOW_DRAW_MONO )
184             pDev->SetLineColor( Color( COL_BLACK ) );
185         else
186             pDev->SetLineColor( rStyleSettings.GetShadowColor() );
187 
188         if ( !aText.Len() )
189             pDev->DrawLine( Point( rPos.X(), nTop ), Point( rPos.X()+rSize.Width()-2, nTop ) );
190         else
191         {
192             pDev->DrawLine( Point( rPos.X(), nTop ), Point( aRect.Left()-nTextOff, nTop ) );
193             pDev->DrawLine( Point( aRect.Right()+nTextOff, nTop ), Point( rPos.X()+rSize.Width()-2, nTop ) );
194         }
195         pDev->DrawLine( Point( rPos.X(), nTop ), Point( rPos.X(), rPos.Y()+rSize.Height()-2 ) );
196         pDev->DrawLine( Point( rPos.X(), rPos.Y()+rSize.Height()-2 ), Point( rPos.X()+rSize.Width()-2, rPos.Y()+rSize.Height()-2 ) );
197         pDev->DrawLine( Point( rPos.X()+rSize.Width()-2, rPos.Y()+rSize.Height()-2 ), Point( rPos.X()+rSize.Width()-2, nTop ) );
198 
199         bool bIsPrinter = OUTDEV_PRINTER == pDev->GetOutDevType();
200         // if we're drawing onto a printer, spare the 3D effect
201         // #i46986# / 2005-04-13 / frank.schoenheit@sun.com
202 
203         if ( !bIsPrinter && !(nDrawFlags & WINDOW_DRAW_MONO) )
204         {
205             pDev->SetLineColor( rStyleSettings.GetLightColor() );
206             if ( !aText.Len() )
207                 pDev->DrawLine( Point( rPos.X()+1, nTop+1 ), Point( rPos.X()+rSize.Width()-3, nTop+1 ) );
208             else
209             {
210                 pDev->DrawLine( Point( rPos.X()+1, nTop+1 ), Point( aRect.Left()-nTextOff, nTop+1 ) );
211                 pDev->DrawLine( Point( aRect.Right()+nTextOff, nTop+1 ), Point( rPos.X()+rSize.Width()-3, nTop+1 ) );
212             }
213             pDev->DrawLine( Point( rPos.X()+1, nTop+1 ), Point( rPos.X()+1, rPos.Y()+rSize.Height()-3 ) );
214             pDev->DrawLine( Point( rPos.X(), rPos.Y()+rSize.Height()-1 ), Point( rPos.X()+rSize.Width()-1, rPos.Y()+rSize.Height()-1 ) );
215             pDev->DrawLine( Point( rPos.X()+rSize.Width()-1, rPos.Y()+rSize.Height()-1 ), Point( rPos.X()+rSize.Width()-1, nTop ) );
216         }
217     }
218 
219     MetricVector* pVector = bLayout ? &mpControlData->mpLayoutData->m_aUnicodeBoundRects : NULL;
220     String* pDisplayText = bLayout ? &mpControlData->mpLayoutData->m_aDisplayText : NULL;
221     DrawControlText( *pDev, aRect, aText, nTextStyle, pVector, pDisplayText );
222 }
223 
224 // -----------------------------------------------------------------------
225 
226 void GroupBox::FillLayoutData() const
227 {
228     mpControlData->mpLayoutData = new vcl::ControlLayoutData();
229     const_cast<GroupBox*>(this)->	ImplDraw( const_cast<GroupBox*>(this), 0, Point(), GetOutputSizePixel(), true );
230 }
231 
232 // -----------------------------------------------------------------------
233 
234 void GroupBox::Paint( const Rectangle& )
235 {
236 	ImplDraw( this, 0, Point(), GetOutputSizePixel() );
237 }
238 
239 // -----------------------------------------------------------------------
240 
241 void GroupBox::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize,
242 					 sal_uLong nFlags )
243 {
244 	Point		aPos  = pDev->LogicToPixel( rPos );
245 	Size		aSize = pDev->LogicToPixel( rSize );
246 	Font		aFont = GetDrawPixelFont( pDev );
247 
248 	pDev->Push();
249 	pDev->SetMapMode();
250 	pDev->SetFont( aFont );
251 	if ( nFlags & WINDOW_DRAW_MONO )
252 		pDev->SetTextColor( Color( COL_BLACK ) );
253 	else
254 		pDev->SetTextColor( GetTextColor() );
255 	pDev->SetTextFillColor();
256 
257 	ImplDraw( pDev, nFlags, aPos, aSize );
258 	pDev->Pop();
259 }
260 
261 // -----------------------------------------------------------------------
262 
263 void GroupBox::Resize()
264 {
265     Control::Resize();
266 	Invalidate();
267 }
268 
269 // -----------------------------------------------------------------------
270 
271 void GroupBox::StateChanged( StateChangedType nType )
272 {
273 	Control::StateChanged( nType );
274 
275 	if ( (nType == STATE_CHANGE_ENABLE) ||
276 		 (nType == STATE_CHANGE_TEXT) ||
277 		 (nType == STATE_CHANGE_UPDATEMODE) )
278 	{
279 		if ( IsUpdateMode() )
280 			Invalidate();
281 	}
282 	else if ( nType == STATE_CHANGE_STYLE )
283 	{
284 		SetStyle( ImplInitStyle( GetStyle() ) );
285 		if ( (GetPrevStyle() & GROUP_VIEW_STYLE) !=
286 			 (GetStyle() & GROUP_VIEW_STYLE) )
287 			Invalidate();
288 	}
289 	else if ( (nType == STATE_CHANGE_ZOOM)	||
290 			  (nType == STATE_CHANGE_CONTROLFONT) )
291 	{
292 		ImplInitSettings( sal_True, sal_False, sal_False );
293 		Invalidate();
294 	}
295 	else if ( nType == STATE_CHANGE_CONTROLFOREGROUND )
296 	{
297 		ImplInitSettings( sal_False, sal_True, sal_False );
298 		Invalidate();
299 	}
300 	else if ( nType == STATE_CHANGE_CONTROLBACKGROUND )
301 	{
302 		ImplInitSettings( sal_False, sal_False, sal_True );
303 		Invalidate();
304 	}
305 }
306 
307 // -----------------------------------------------------------------------
308 
309 void GroupBox::DataChanged( const DataChangedEvent& rDCEvt )
310 {
311 	Control::DataChanged( rDCEvt );
312 
313 	if ( (rDCEvt.GetType() == DATACHANGED_FONTS) ||
314 		 (rDCEvt.GetType() == DATACHANGED_FONTSUBSTITUTION) ||
315 		 ((rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
316 		  (rDCEvt.GetFlags() & SETTINGS_STYLE)) )
317 	{
318 		ImplInitSettings( sal_True, sal_True, sal_True );
319 		Invalidate();
320 	}
321 }
322 
323