tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / vcl / headless / svpframe.cxx
blob00672935d256857a5b73007359223c6d92820e33
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <comphelper/lok.hxx>
21 #include <sal/log.hxx>
23 #include <headless/svpframe.hxx>
24 #include <headless/svpinst.hxx>
25 #ifndef IOS
26 #include <headless/svpgdi.hxx>
27 #endif
28 #include <salsys.hxx>
30 #include <basegfx/vector/b2ivector.hxx>
32 #ifndef IOS
33 #include <cairo.h>
34 #endif
36 SvpSalFrame* SvpSalFrame::s_pFocusFrame = nullptr;
38 #ifdef IOS
39 #define SvpSalGraphics AquaSalGraphics
40 #endif
42 SvpSalFrame::SvpSalFrame( SvpSalInstance* pInstance,
43 SalFrame* pParent,
44 SalFrameStyleFlags nSalFrameStyle ) :
45 m_pInstance( pInstance ),
46 m_pParent( static_cast<SvpSalFrame*>(pParent) ),
47 m_nStyle( nSalFrameStyle ),
48 m_bVisible( false ),
49 #ifndef IOS
50 m_pSurface( nullptr ),
51 #endif
52 m_nMinWidth( 0 ),
53 m_nMinHeight( 0 ),
54 m_nMaxWidth( 0 ),
55 m_nMaxHeight( 0 )
57 #ifdef IOS
58 // Nothing
59 #elif defined ANDROID
60 // Nothing
61 #else
62 m_aSystemChildData.pSalFrame = this;
63 #endif
65 if( m_pParent )
66 m_pParent->m_aChildren.push_back( this );
68 if( m_pInstance )
69 m_pInstance->registerFrame( this );
71 SetPosSize( 0, 0, 800, 600, SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT );
74 SvpSalFrame::~SvpSalFrame()
76 if( m_pInstance )
77 m_pInstance->deregisterFrame( this );
79 std::vector<SvpSalFrame*> Children = m_aChildren;
80 for( auto& rChild : Children )
81 rChild->SetParent( m_pParent );
82 if( m_pParent )
83 std::erase(m_pParent->m_aChildren, this);
85 if( s_pFocusFrame == this )
87 s_pFocusFrame = nullptr;
88 // call directly here, else an event for a destroyed frame would be dispatched
89 CallCallback( SalEvent::LoseFocus, nullptr );
90 // if the handler has not set a new focus frame
91 // pass focus to another frame, preferably a document style window
92 if( s_pFocusFrame == nullptr )
94 for (auto pSalFrame : m_pInstance->getFrames() )
96 SvpSalFrame* pFrame = static_cast<SvpSalFrame*>( pSalFrame );
97 if( pFrame->m_bVisible &&
98 pFrame->m_pParent == nullptr &&
99 (pFrame->m_nStyle & (SalFrameStyleFlags::MOVEABLE |
100 SalFrameStyleFlags::SIZEABLE |
101 SalFrameStyleFlags::CLOSEABLE) )
104 pFrame->GetFocus();
105 break;
110 #ifndef IOS
111 if (m_pSurface)
112 cairo_surface_destroy(m_pSurface);
113 #endif
116 void SvpSalFrame::GetFocus()
118 if (m_nStyle == SalFrameStyleFlags::NONE)
119 return;
120 if( s_pFocusFrame == this )
121 return;
122 // FIXME: return if !m_bVisible
123 // That's IMHO why CppunitTest_sd_tiledrendering crashes non-headless
125 if( (m_nStyle & (SalFrameStyleFlags::OWNERDRAWDECORATION | SalFrameStyleFlags::FLOAT)) == SalFrameStyleFlags::NONE )
127 if( s_pFocusFrame )
128 s_pFocusFrame->LoseFocus();
129 s_pFocusFrame = this;
130 m_pInstance->PostEvent( this, nullptr, SalEvent::GetFocus );
134 void SvpSalFrame::LoseFocus()
136 if( s_pFocusFrame == this )
138 m_pInstance->PostEvent( this, nullptr, SalEvent::LoseFocus );
139 s_pFocusFrame = nullptr;
143 basegfx::B2IVector SvpSalFrame::GetSurfaceFrameSize() const
145 basegfx::B2IVector aFrameSize( maGeometry.width(), maGeometry.height() );
146 if( aFrameSize.getX() == 0 )
147 aFrameSize.setX( 1 );
148 if( aFrameSize.getY() == 0 )
149 aFrameSize.setY( 1 );
150 // Creating backing surfaces for invisible windows costs a big chunk of RAM.
151 if (Application::IsHeadlessModeEnabled())
152 aFrameSize = basegfx::B2IVector( 1, 1 );
153 return aFrameSize;
156 SalGraphics* SvpSalFrame::AcquireGraphics()
158 SvpSalGraphics* pGraphics = new SvpSalGraphics();
159 #ifndef IOS
160 pGraphics->setSurface(m_pSurface, GetSurfaceFrameSize());
161 #endif
162 m_aGraphics.push_back( pGraphics );
163 return pGraphics;
166 void SvpSalFrame::ReleaseGraphics( SalGraphics* pGraphics )
168 SvpSalGraphics* pSvpGraphics = dynamic_cast<SvpSalGraphics*>(pGraphics);
169 std::erase(m_aGraphics, pSvpGraphics);
170 delete pSvpGraphics;
173 bool SvpSalFrame::PostEvent(std::unique_ptr<ImplSVEvent> pData)
175 m_pInstance->PostEvent( this, pData.release(), SalEvent::UserEvent );
176 return true;
179 void SvpSalFrame::PostPaint() const
181 if( m_bVisible )
183 SalPaintEvent aPEvt(0, 0, maGeometry.width(), maGeometry.height());
184 aPEvt.mbImmediateUpdate = false;
185 CallCallback( SalEvent::Paint, &aPEvt );
189 void SvpSalFrame::SetTitle(const OUString& sTitle)
191 m_sTitle = sTitle;
194 void SvpSalFrame::SetIcon( sal_uInt16 )
198 void SvpSalFrame::SetMenu( SalMenu* )
202 void SvpSalFrame::SetExtendedFrameStyle( SalExtStyle )
206 void SvpSalFrame::Show( bool bVisible, bool bNoActivate )
208 if (m_nStyle == SalFrameStyleFlags::NONE)
209 return;
210 if (bVisible == m_bVisible)
212 if (m_bVisible && !bNoActivate)
213 GetFocus();
214 return;
217 if (bVisible)
219 m_bVisible = true;
220 m_pInstance->PostEvent( this, nullptr, SalEvent::Resize );
221 if( ! bNoActivate )
222 GetFocus();
224 else
226 m_bVisible = false;
227 LoseFocus();
231 void SvpSalFrame::SetMinClientSize( tools::Long nWidth, tools::Long nHeight )
233 m_nMinWidth = nWidth;
234 m_nMinHeight = nHeight;
237 void SvpSalFrame::SetMaxClientSize( tools::Long nWidth, tools::Long nHeight )
239 m_nMaxWidth = nWidth;
240 m_nMaxHeight = nHeight;
243 void SvpSalFrame::SetPosSize( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, sal_uInt16 nFlags )
245 if( (nFlags & SAL_FRAME_POSSIZE_X) != 0 )
246 maGeometry.setX(nX);
247 if( (nFlags & SAL_FRAME_POSSIZE_Y) != 0 )
248 maGeometry.setY(nY);
249 if( (nFlags & SAL_FRAME_POSSIZE_WIDTH) != 0 )
251 maGeometry.setWidth(nWidth);
252 if (m_nMaxWidth > 0 && maGeometry.width() > m_nMaxWidth)
253 maGeometry.setWidth(m_nMaxWidth);
254 if (m_nMinWidth > 0 && maGeometry.width() < m_nMinWidth)
255 maGeometry.setWidth(m_nMinWidth);
257 if( (nFlags & SAL_FRAME_POSSIZE_HEIGHT) != 0 )
259 maGeometry.setHeight(nHeight);
260 if (m_nMaxHeight > 0 && maGeometry.height() > m_nMaxHeight)
261 maGeometry.setHeight(m_nMaxHeight);
262 if (m_nMinHeight > 0 && maGeometry.height() < m_nMinHeight)
263 maGeometry.setHeight(m_nMinHeight);
265 #ifndef IOS
266 basegfx::B2IVector aFrameSize = GetSurfaceFrameSize();
267 if (!m_pSurface || cairo_image_surface_get_width(m_pSurface) != aFrameSize.getX() ||
268 cairo_image_surface_get_height(m_pSurface) != aFrameSize.getY() )
270 if (m_pSurface)
271 cairo_surface_destroy(m_pSurface);
273 m_pSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
274 aFrameSize.getX(),
275 aFrameSize.getY());
277 // update device in existing graphics
278 for (auto const& graphic : m_aGraphics)
280 graphic->setSurface(m_pSurface, aFrameSize);
283 if( m_bVisible )
284 m_pInstance->PostEvent( this, nullptr, SalEvent::Resize );
285 #endif
288 void SvpSalFrame::GetClientSize( tools::Long& rWidth, tools::Long& rHeight )
290 rWidth = maGeometry.width();
291 rHeight = maGeometry.height();
294 void SvpSalFrame::GetWorkArea( AbsoluteScreenPixelRectangle& rRect )
296 rRect = AbsoluteScreenPixelRectangle( AbsoluteScreenPixelPoint( 0, 0 ),
297 AbsoluteScreenPixelSize( VIRTUAL_DESKTOP_WIDTH, VIRTUAL_DESKTOP_HEIGHT ) );
300 SalFrame* SvpSalFrame::GetParent() const
302 return m_pParent;
305 void SvpSalFrame::SetWindowState(const vcl::WindowData *pState)
307 if (pState == nullptr)
308 return;
310 // Request for position or size change
311 if (!(pState->mask() & vcl::WindowDataMask::PosSize))
312 return;
314 tools::Long nX = maGeometry.x();
315 tools::Long nY = maGeometry.y();
316 tools::Long nWidth = maGeometry.width();
317 tools::Long nHeight = maGeometry.height();
319 // change requested properties
320 if (pState->mask() & vcl::WindowDataMask::X)
321 nX = pState->x();
322 if (pState->mask() & vcl::WindowDataMask::Y)
323 nY = pState->y();
324 if (pState->mask() & vcl::WindowDataMask::Width)
325 nWidth = pState->width();
326 if (pState->mask() & vcl::WindowDataMask::Height)
327 nHeight = pState->height();
329 SetPosSize( nX, nY, nWidth, nHeight,
330 SAL_FRAME_POSSIZE_X | SAL_FRAME_POSSIZE_Y |
331 SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT );
334 bool SvpSalFrame::GetWindowState(vcl::WindowData* pState)
336 pState->setPosSize(maGeometry.posSize());
337 pState->setState(vcl::WindowState::Normal);
338 pState->setMask(vcl::WindowDataMask::PosSizeState);
339 return true;
342 void SvpSalFrame::ShowFullScreen( bool, sal_Int32 )
344 SetPosSize( 0, 0, VIRTUAL_DESKTOP_WIDTH, VIRTUAL_DESKTOP_HEIGHT,
345 SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT );
348 void SvpSalFrame::StartPresentation( bool )
352 void SvpSalFrame::SetAlwaysOnTop( bool )
356 void SvpSalFrame::ToTop(SalFrameToTop nFlags)
358 if (m_nStyle == SalFrameStyleFlags::NONE)
359 return;
360 if (nFlags & SalFrameToTop::RestoreWhenMin)
361 Show(true, false);
362 else
363 GetFocus();
366 void SvpSalFrame::SetPointer( PointerStyle )
370 void SvpSalFrame::CaptureMouse( bool )
374 void SvpSalFrame::SetPointerPos( tools::Long, tools::Long )
378 void SvpSalFrame::Flush()
382 void SvpSalFrame::SetInputContext( SalInputContext* )
386 void SvpSalFrame::EndExtTextInput( EndExtTextInputFlags )
390 OUString SvpSalFrame::GetKeyName( sal_uInt16 )
392 return OUString();
395 bool SvpSalFrame::MapUnicodeToKeyCode( sal_Unicode, LanguageType, vcl::KeyCode& )
397 return false;
400 LanguageType SvpSalFrame::GetInputLanguage()
402 return LANGUAGE_DONTKNOW;
405 void SvpSalFrame::UpdateSettings( AllSettings& rSettings )
407 StyleSettings aStyleSettings = rSettings.GetStyleSettings();
409 Color aBackgroundColor( 0xef, 0xef, 0xef );
410 aStyleSettings.BatchSetBackgrounds( aBackgroundColor, false );
411 aStyleSettings.SetMenuColor( aBackgroundColor );
412 aStyleSettings.SetMenuBarColor( aBackgroundColor );
414 if (comphelper::LibreOfficeKit::isActive()) // TODO: remove this.
416 vcl::Font aStdFont( FAMILY_SWISS, Size( 0, 14 ) );
417 aStdFont.SetCharSet( osl_getThreadTextEncoding() );
418 aStdFont.SetWeight( WEIGHT_NORMAL );
419 aStdFont.SetFamilyName( u"Liberation Sans"_ustr );
420 aStyleSettings.BatchSetFonts( aStdFont, aStdFont );
422 aStdFont.SetFontSize(Size(0, 12));
423 aStyleSettings.SetMenuFont(aStdFont);
425 SvpSalGraphics* pGraphics = m_aGraphics.empty() ? nullptr : m_aGraphics.back();
426 bool bFreeGraphics = false;
427 if (!pGraphics)
429 pGraphics = dynamic_cast<SvpSalGraphics*>(AcquireGraphics());
430 if (!pGraphics)
432 SAL_WARN("vcl.gtk3", "Could not get graphics - unable to update settings");
433 return;
435 bFreeGraphics = true;
437 rSettings.SetStyleSettings(aStyleSettings);
438 #ifndef IOS // For now...
439 pGraphics->UpdateSettings(rSettings);
440 #endif
441 if (bFreeGraphics)
442 ReleaseGraphics(pGraphics);
444 else
445 rSettings.SetStyleSettings(aStyleSettings);
448 void SvpSalFrame::Beep()
452 const SystemEnvData& SvpSalFrame::GetSystemData() const
454 return m_aSystemChildData;
457 SalFrame::SalPointerState SvpSalFrame::GetPointerState()
459 SalPointerState aState;
460 aState.mnState = 0;
461 return aState;
464 KeyIndicatorState SvpSalFrame::GetIndicatorState()
466 return KeyIndicatorState::NONE;
469 void SvpSalFrame::SimulateKeyPress( sal_uInt16 /*nKeyCode*/ )
473 void SvpSalFrame::SetParent( SalFrame* pNewParent )
475 if( m_pParent )
476 std::erase(m_pParent->m_aChildren, this);
477 m_pParent = static_cast<SvpSalFrame*>(pNewParent);
480 void SvpSalFrame::SetPluginParent( SystemParentData* )
484 void SvpSalFrame::ResetClipRegion()
488 void SvpSalFrame::BeginSetClipRegion( sal_uInt32 )
492 void SvpSalFrame::UnionClipRegion( tools::Long, tools::Long, tools::Long, tools::Long )
496 void SvpSalFrame::EndSetClipRegion()
500 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */