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