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 <comphelper/processfactory.hxx>
21 :
22 : #include <tools/rc.h>
23 : #include <vcl/svapp.hxx>
24 : #include <vcl/event.hxx>
25 : #include <vcl/ctrl.hxx>
26 : #include <vcl/decoview.hxx>
27 : #include <vcl/salnativewidgets.hxx>
28 : #include <vcl/settings.hxx>
29 :
30 : #include <textlayout.hxx>
31 : #include <svdata.hxx>
32 : #include <controldata.hxx>
33 :
34 : using namespace vcl;
35 :
36 0 : void Control::ImplInitControlData()
37 : {
38 0 : mbHasControlFocus = false;
39 0 : mpControlData = new ImplControlData;
40 0 : }
41 :
42 0 : Control::Control( WindowType nType ) :
43 0 : Window( nType )
44 : {
45 0 : ImplInitControlData();
46 0 : }
47 :
48 0 : Control::Control( Window* pParent, WinBits nStyle ) :
49 0 : Window( WINDOW_CONTROL )
50 : {
51 0 : ImplInitControlData();
52 0 : ImplInit( pParent, nStyle, NULL );
53 0 : }
54 :
55 0 : Control::Control( Window* pParent, const ResId& rResId ) :
56 0 : Window( WINDOW_CONTROL )
57 : {
58 0 : ImplInitControlData();
59 0 : rResId.SetRT( RSC_CONTROL );
60 0 : WinBits nStyle = ImplInitRes( rResId );
61 0 : ImplInit( pParent, nStyle, NULL );
62 0 : ImplLoadRes( rResId );
63 :
64 0 : if ( !(nStyle & WB_HIDE) )
65 0 : Show();
66 0 : }
67 :
68 0 : Control::~Control()
69 : {
70 0 : delete mpControlData, mpControlData = NULL;
71 0 : }
72 :
73 0 : void Control::EnableRTL( bool bEnable )
74 : {
75 : // convenience: for controls also switch layout mode
76 : SetLayoutMode( bEnable ? TEXT_LAYOUT_BIDI_RTL | TEXT_LAYOUT_TEXTORIGIN_LEFT :
77 0 : TEXT_LAYOUT_BIDI_LTR | TEXT_LAYOUT_TEXTORIGIN_LEFT );
78 0 : StateChanged( STATE_CHANGE_MIRRORING );
79 0 : OutputDevice::EnableRTL(bEnable);
80 0 : }
81 :
82 0 : void Control::GetFocus()
83 : {
84 0 : Window::GetFocus();
85 0 : }
86 :
87 0 : void Control::LoseFocus()
88 : {
89 0 : Window::LoseFocus();
90 0 : }
91 :
92 0 : void Control::Resize()
93 : {
94 0 : ImplClearLayoutData();
95 0 : Window::Resize();
96 0 : }
97 :
98 0 : void Control::FillLayoutData() const
99 : {
100 0 : }
101 :
102 0 : void Control::CreateLayoutData() const
103 : {
104 : DBG_ASSERT( !mpControlData->mpLayoutData, "Control::CreateLayoutData: should be called with non-existent layout data only!" );
105 0 : mpControlData->mpLayoutData = new ::vcl::ControlLayoutData();
106 0 : }
107 :
108 0 : bool Control::HasLayoutData() const
109 : {
110 0 : return mpControlData->mpLayoutData != NULL;
111 : }
112 :
113 0 : ::vcl::ControlLayoutData* Control::GetLayoutData() const
114 : {
115 0 : return mpControlData->mpLayoutData;
116 : }
117 :
118 0 : void Control::SetText( const OUString& rStr )
119 : {
120 0 : ImplClearLayoutData();
121 0 : Window::SetText( rStr );
122 0 : }
123 :
124 0 : Rectangle ControlLayoutData::GetCharacterBounds( long nIndex ) const
125 : {
126 0 : return (nIndex >= 0 && nIndex < (long) m_aUnicodeBoundRects.size()) ? m_aUnicodeBoundRects[ nIndex ] : Rectangle();
127 : }
128 :
129 0 : Rectangle Control::GetCharacterBounds( long nIndex ) const
130 : {
131 0 : if( !HasLayoutData() )
132 0 : FillLayoutData();
133 0 : return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetCharacterBounds( nIndex ) : Rectangle();
134 : }
135 :
136 0 : long ControlLayoutData::GetIndexForPoint( const Point& rPoint ) const
137 : {
138 0 : long nIndex = -1;
139 0 : for( long i = m_aUnicodeBoundRects.size()-1; i >= 0; i-- )
140 : {
141 0 : Point aTopLeft = m_aUnicodeBoundRects[i].TopLeft();
142 0 : Point aBottomRight = m_aUnicodeBoundRects[i].BottomRight();
143 0 : if (rPoint.X() >= aTopLeft.X() && rPoint.Y() >= aTopLeft.Y() &&
144 0 : rPoint.X() <= aBottomRight.X() && rPoint.Y() <= aBottomRight.Y())
145 : {
146 0 : nIndex = i;
147 0 : break;
148 : }
149 : }
150 0 : return nIndex;
151 : }
152 :
153 0 : long Control::GetIndexForPoint( const Point& rPoint ) const
154 : {
155 0 : if( ! HasLayoutData() )
156 0 : FillLayoutData();
157 0 : return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetIndexForPoint( rPoint ) : -1;
158 : }
159 :
160 0 : long ControlLayoutData::GetLineCount() const
161 : {
162 0 : long nLines = m_aLineIndices.size();
163 0 : if( nLines == 0 && !m_aDisplayText.isEmpty() )
164 0 : nLines = 1;
165 0 : return nLines;
166 : }
167 :
168 0 : Pair ControlLayoutData::GetLineStartEnd( long nLine ) const
169 : {
170 0 : Pair aPair( -1, -1 );
171 :
172 0 : int nDisplayLines = m_aLineIndices.size();
173 0 : if( nLine >= 0 && nLine < nDisplayLines )
174 : {
175 0 : aPair.A() = m_aLineIndices[nLine];
176 0 : if( nLine+1 < nDisplayLines )
177 0 : aPair.B() = m_aLineIndices[nLine+1]-1;
178 : else
179 0 : aPair.B() = m_aDisplayText.getLength()-1;
180 : }
181 0 : else if( nLine == 0 && nDisplayLines == 0 && !m_aDisplayText.isEmpty() )
182 : {
183 : // special case for single line controls so the implementations
184 : // in that case do not have to fill in the line indices
185 0 : aPair.A() = 0;
186 0 : aPair.B() = m_aDisplayText.getLength()-1;
187 : }
188 0 : return aPair;
189 : }
190 :
191 0 : Pair Control::GetLineStartEnd( long nLine ) const
192 : {
193 0 : if( !HasLayoutData() )
194 0 : FillLayoutData();
195 0 : return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->GetLineStartEnd( nLine ) : Pair( -1, -1 );
196 : }
197 :
198 0 : long ControlLayoutData::ToRelativeLineIndex( long nIndex ) const
199 : {
200 : // is the index sensible at all ?
201 0 : if( nIndex >= 0 && nIndex < m_aDisplayText.getLength() )
202 : {
203 0 : int nDisplayLines = m_aLineIndices.size();
204 : // if only 1 line exists, then absolute and relative index are
205 : // identical -> do nothing
206 0 : if( nDisplayLines > 1 )
207 : {
208 : int nLine;
209 0 : for( nLine = nDisplayLines-1; nLine >= 0; nLine-- )
210 : {
211 0 : if( m_aLineIndices[nLine] <= nIndex )
212 : {
213 0 : nIndex -= m_aLineIndices[nLine];
214 0 : break;
215 : }
216 : }
217 0 : if( nLine < 0 )
218 : {
219 : DBG_ASSERT( nLine >= 0, "ToRelativeLineIndex failed" );
220 0 : nIndex = -1;
221 : }
222 : }
223 : }
224 : else
225 0 : nIndex = -1;
226 :
227 0 : return nIndex;
228 : }
229 :
230 0 : long Control::ToRelativeLineIndex( long nIndex ) const
231 : {
232 0 : if( !HasLayoutData() )
233 0 : FillLayoutData();
234 0 : return mpControlData->mpLayoutData ? mpControlData->mpLayoutData->ToRelativeLineIndex( nIndex ) : -1;
235 : }
236 :
237 0 : OUString Control::GetDisplayText() const
238 : {
239 0 : if( !HasLayoutData() )
240 0 : FillLayoutData();
241 0 : return mpControlData->mpLayoutData ? OUString(mpControlData->mpLayoutData->m_aDisplayText) : GetText();
242 : }
243 :
244 0 : bool Control::Notify( NotifyEvent& rNEvt )
245 : {
246 0 : if ( rNEvt.GetType() == EVENT_GETFOCUS )
247 : {
248 0 : if ( !mbHasControlFocus )
249 : {
250 0 : mbHasControlFocus = true;
251 0 : StateChanged( STATE_CHANGE_CONTROL_FOCUS );
252 0 : if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_GETFOCUS, maGetFocusHdl, this ) )
253 : // been destroyed within the handler
254 0 : return true;
255 : }
256 : }
257 : else
258 : {
259 0 : if ( rNEvt.GetType() == EVENT_LOSEFOCUS )
260 : {
261 0 : Window* pFocusWin = Application::GetFocusWindow();
262 0 : if ( !pFocusWin || !ImplIsWindowOrChild( pFocusWin ) )
263 : {
264 0 : mbHasControlFocus = false;
265 0 : StateChanged( STATE_CHANGE_CONTROL_FOCUS );
266 0 : if ( ImplCallEventListenersAndHandler( VCLEVENT_CONTROL_LOSEFOCUS, maLoseFocusHdl, this ) )
267 : // been destroyed within the handler
268 0 : return true;
269 : }
270 : }
271 : }
272 :
273 0 : return Window::Notify( rNEvt );
274 : }
275 :
276 0 : void Control::StateChanged( StateChangedType nStateChange )
277 : {
278 0 : if( nStateChange == STATE_CHANGE_INITSHOW ||
279 0 : nStateChange == STATE_CHANGE_VISIBLE ||
280 0 : nStateChange == STATE_CHANGE_ZOOM ||
281 0 : nStateChange == STATE_CHANGE_BORDER ||
282 : nStateChange == STATE_CHANGE_CONTROLFONT
283 : )
284 : {
285 0 : ImplClearLayoutData();
286 : }
287 0 : Window::StateChanged( nStateChange );
288 0 : }
289 :
290 0 : void Control::AppendLayoutData( const Control& rSubControl ) const
291 : {
292 0 : if( !rSubControl.HasLayoutData() )
293 0 : rSubControl.FillLayoutData();
294 0 : if( !rSubControl.HasLayoutData() || rSubControl.mpControlData->mpLayoutData->m_aDisplayText.isEmpty() )
295 0 : return;
296 :
297 0 : long nCurrentIndex = mpControlData->mpLayoutData->m_aDisplayText.getLength();
298 0 : mpControlData->mpLayoutData->m_aDisplayText += rSubControl.mpControlData->mpLayoutData->m_aDisplayText;
299 0 : int nLines = rSubControl.mpControlData->mpLayoutData->m_aLineIndices.size();
300 : int n;
301 0 : mpControlData->mpLayoutData->m_aLineIndices.push_back( nCurrentIndex );
302 0 : for( n = 1; n < nLines; n++ )
303 0 : mpControlData->mpLayoutData->m_aLineIndices.push_back( rSubControl.mpControlData->mpLayoutData->m_aLineIndices[n] + nCurrentIndex );
304 0 : int nRectangles = rSubControl.mpControlData->mpLayoutData->m_aUnicodeBoundRects.size();
305 0 : Rectangle aRel = const_cast<Control&>(rSubControl).GetWindowExtentsRelative( const_cast<Control*>(this) );
306 0 : for( n = 0; n < nRectangles; n++ )
307 : {
308 0 : Rectangle aRect = rSubControl.mpControlData->mpLayoutData->m_aUnicodeBoundRects[n];
309 0 : aRect.Move( aRel.Left(), aRel.Top() );
310 0 : mpControlData->mpLayoutData->m_aUnicodeBoundRects.push_back( aRect );
311 : }
312 : }
313 :
314 0 : bool Control::ImplCallEventListenersAndHandler( sal_uLong nEvent, const Link& rHandler, void* pCaller )
315 : {
316 0 : ImplDelData aCheckDelete;
317 0 : ImplAddDel( &aCheckDelete );
318 :
319 0 : ImplCallEventListeners( nEvent );
320 0 : if ( !aCheckDelete.IsDead() )
321 : {
322 0 : rHandler.Call( pCaller );
323 :
324 0 : if ( !aCheckDelete.IsDead() )
325 : {
326 0 : ImplRemoveDel( &aCheckDelete );
327 0 : return false;
328 : }
329 : }
330 0 : return true;
331 : }
332 :
333 0 : void Control::SetLayoutDataParent( const Control* pParent ) const
334 : {
335 0 : if( HasLayoutData() )
336 0 : mpControlData->mpLayoutData->m_pParent = pParent;
337 0 : }
338 :
339 0 : void Control::ImplClearLayoutData() const
340 : {
341 0 : delete mpControlData->mpLayoutData, mpControlData->mpLayoutData = NULL;
342 0 : }
343 :
344 0 : void Control::ImplDrawFrame( OutputDevice* pDev, Rectangle& rRect )
345 : {
346 : // use a deco view to draw the frame
347 : // However, since there happens a lot of magic there, we need to fake some (style) settings
348 : // on the device
349 0 : AllSettings aOriginalSettings( pDev->GetSettings() );
350 :
351 0 : AllSettings aNewSettings( aOriginalSettings );
352 0 : StyleSettings aStyle( aNewSettings.GetStyleSettings() );
353 :
354 : // The *only known* clients of the Draw methods of the various VCL-controls are form controls:
355 : // During print preview, and during printing, Draw is called. Thus, drawing always happens with a
356 : // mono (colored) border
357 0 : aStyle.SetOptions( aStyle.GetOptions() | STYLE_OPTION_MONO );
358 0 : aStyle.SetMonoColor( GetSettings().GetStyleSettings().GetMonoColor() );
359 :
360 0 : aNewSettings.SetStyleSettings( aStyle );
361 : // #i67023# do not call data changed listeners for this fake
362 : // since they may understandably invalidate on settings changed
363 0 : pDev->OutputDevice::SetSettings( aNewSettings );
364 :
365 0 : DecorationView aDecoView( pDev );
366 0 : rRect = aDecoView.DrawFrame( rRect, FRAME_DRAW_WINDOWBORDER );
367 :
368 0 : pDev->OutputDevice::SetSettings( aOriginalSettings );
369 0 : }
370 :
371 0 : void Control::DataChanged( const DataChangedEvent& rDCEvt)
372 : {
373 : // we don't want to loose some style settings for controls created with the
374 : // toolkit
375 0 : if ( IsCreatedWithToolkit() &&
376 0 : (rDCEvt.GetType() == DATACHANGED_SETTINGS) &&
377 0 : (rDCEvt.GetFlags() & SETTINGS_STYLE) )
378 : {
379 0 : const AllSettings* pOldSettings = rDCEvt.GetOldSettings();
380 :
381 0 : AllSettings aSettings = GetSettings();
382 0 : StyleSettings aStyleSettings = aSettings.GetStyleSettings();
383 0 : sal_uLong nNewOptions = aStyleSettings.GetOptions();
384 :
385 0 : if ( pOldSettings && !(nNewOptions & STYLE_OPTION_MONO) && ( pOldSettings->GetStyleSettings().GetOptions() & STYLE_OPTION_MONO ) )
386 : {
387 0 : nNewOptions |= STYLE_OPTION_MONO;
388 0 : aStyleSettings.SetOptions( nNewOptions );
389 0 : aStyleSettings.SetMonoColor( pOldSettings->GetStyleSettings().GetMonoColor() );
390 0 : aSettings.SetStyleSettings( aStyleSettings );
391 0 : SetSettings( aSettings );
392 0 : }
393 : }
394 0 : }
395 :
396 0 : ControlLayoutData::~ControlLayoutData()
397 : {
398 0 : if( m_pParent )
399 0 : m_pParent->ImplClearLayoutData();
400 0 : }
401 :
402 0 : Size Control::GetOptimalSize() const
403 : {
404 0 : return Size( GetTextWidth( GetText() ) + 2 * 12,
405 0 : GetTextHeight() + 2 * 6 );
406 : }
407 :
408 0 : void Control::SetReferenceDevice( OutputDevice* _referenceDevice )
409 : {
410 0 : if ( mpControlData->mpReferenceDevice == _referenceDevice )
411 0 : return;
412 :
413 0 : mpControlData->mpReferenceDevice = _referenceDevice;
414 0 : Invalidate();
415 : }
416 :
417 0 : OutputDevice* Control::GetReferenceDevice() const
418 : {
419 0 : return mpControlData->mpReferenceDevice;
420 : }
421 :
422 0 : const Font& Control::GetCanonicalFont( const StyleSettings& _rStyle ) const
423 : {
424 0 : return _rStyle.GetLabelFont();
425 : }
426 :
427 0 : const Color& Control::GetCanonicalTextColor( const StyleSettings& _rStyle ) const
428 : {
429 0 : return _rStyle.GetLabelTextColor();
430 : }
431 :
432 0 : void Control::ImplInitSettings( const bool _bFont, const bool _bForeground )
433 : {
434 0 : const StyleSettings& rStyleSettings = GetSettings().GetStyleSettings();
435 :
436 0 : if ( _bFont )
437 : {
438 0 : Font aFont( GetCanonicalFont( rStyleSettings ) );
439 0 : if ( IsControlFont() )
440 0 : aFont.Merge( GetControlFont() );
441 0 : SetZoomedPointFont( aFont );
442 : }
443 :
444 0 : if ( _bForeground || _bFont )
445 : {
446 0 : Color aColor;
447 0 : if ( IsControlForeground() )
448 0 : aColor = GetControlForeground();
449 : else
450 0 : aColor = GetCanonicalTextColor( rStyleSettings );
451 0 : SetTextColor( aColor );
452 0 : SetTextFillColor();
453 : }
454 0 : }
455 :
456 0 : void Control::DrawControlText( OutputDevice& _rTargetDevice, Rectangle& _io_rRect, const OUString& _rStr,
457 : sal_uInt16 _nStyle, MetricVector* _pVector, OUString* _pDisplayText ) const
458 : {
459 : #ifdef FS_DEBUG
460 : if ( !_pVector )
461 : {
462 : static MetricVector aCharRects;
463 : static OUString sDisplayText;
464 : aCharRects.clear();
465 : sDisplayText = OUString();
466 : _pVector = &aCharRects;
467 : _pDisplayText = &sDisplayText;
468 : }
469 : #endif
470 :
471 0 : if ( !mpControlData->mpReferenceDevice || ( mpControlData->mpReferenceDevice == &_rTargetDevice ) )
472 : {
473 0 : _io_rRect = _rTargetDevice.GetTextRect( _io_rRect, _rStr, _nStyle );
474 0 : _rTargetDevice.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
475 : }
476 : else
477 : {
478 0 : ControlTextRenderer aRenderer( *this, _rTargetDevice, *mpControlData->mpReferenceDevice );
479 0 : _io_rRect = aRenderer.DrawText( _io_rRect, _rStr, _nStyle, _pVector, _pDisplayText );
480 : }
481 :
482 : #ifdef FS_DEBUG
483 : _rTargetDevice.Push( PUSH_LINECOLOR | PUSH_FILLCOLOR );
484 : _rTargetDevice.SetLineColor( COL_LIGHTRED );
485 : _rTargetDevice.SetFillColor();
486 : for ( MetricVector::const_iterator cr = _pVector->begin();
487 : cr != _pVector->end();
488 : ++cr
489 : )
490 : {
491 : _rTargetDevice.DrawRect( *cr );
492 : }
493 : _rTargetDevice.Pop();
494 : #endif
495 0 : }
496 :
497 : Font
498 0 : Control::GetUnzoomedControlPointFont() const
499 : {
500 0 : Font aFont( GetCanonicalFont( GetSettings().GetStyleSettings() ) );
501 0 : if ( IsControlFont() )
502 0 : aFont.Merge( GetControlFont() );
503 0 : return aFont;
504 : }
505 :
506 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|