bump product version to 6.4.0.3
[LibreOffice.git] / vcl / osx / salobj.cxx
blob34679dcdbaf7c5d66d3d9e57ec58b918721c5a20
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 <string.h>
21 #include <tools/debug.hxx>
22 #include <vcl/opengl/OpenGLContext.hxx>
23 #include <vcl/opengl/OpenGLHelper.hxx>
24 #include <opengl/zone.hxx>
26 #include <osx/saldata.hxx>
27 #include <osx/salframe.h>
28 #include <osx/salinst.h>
29 #include <osx/salobj.h>
30 #include <osx/runinmain.hxx>
32 #include <AppKit/NSOpenGLView.h>
34 AquaSalObject::AquaSalObject( AquaSalFrame* pFrame, SystemWindowData const * pWindowData ) :
35 mpFrame( pFrame ),
36 mnClipX( -1 ),
37 mnClipY( -1 ),
38 mnClipWidth( -1 ),
39 mnClipHeight( -1 ),
40 mbClip( false ),
41 mnX( 0 ),
42 mnY( 0 ),
43 mnWidth( 20 ),
44 mnHeight( 20 )
46 maSysData.mpNSView = nullptr;
47 maSysData.mbOpenGL = false;
49 NSRect aInitFrame = { NSZeroPoint, { 20, 20 } };
50 mpClipView = [[NSClipView alloc] initWithFrame: aInitFrame ];
51 if( mpClipView )
53 [mpFrame->getNSView() addSubview: mpClipView];
54 [mpClipView setHidden: YES];
56 if (pWindowData && pWindowData->bOpenGL)
58 maSysData.mbOpenGL = true;
59 SAL_WNODEPRECATED_DECLARATIONS_PUSH
60 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please use
61 // Metal or MetalKit."
62 NSOpenGLPixelFormat* pixFormat = nullptr;
63 SAL_WNODEPRECATED_DECLARATIONS_POP
65 if (pWindowData->bLegacy)
67 SAL_WNODEPRECATED_DECLARATIONS_PUSH
68 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS 10.14"
69 NSOpenGLPixelFormatAttribute const aAttributes[] =
70 SAL_WNODEPRECATED_DECLARATIONS_POP
72 SAL_WNODEPRECATED_DECLARATIONS_PUSH
73 // "'NSOpenGLPFADoubleBuffer' is deprecated: first deprecated in macOS 10.14",
74 // "'NSOpenGLPFAAlphaSize' is deprecated: first deprecated in macOS 10.14",
75 // "'NSOpenGLPFAColorSize' is deprecated: first deprecated in macOS 10.14",
76 // "'NSOpenGLPFADepthSize' is deprecated: first deprecated in macOS 10.14",
77 // "'NSOpenGLPFAMultisample' is deprecated: first deprecated in macOS 10.14",
78 // "'NSOpenGLPFASampleBuffers' is deprecated: first deprecated in macOS 10.14",
79 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS
80 // 10.14",
81 // "'NSOpenGLPFASamples' is deprecated: first deprecated in macOS 10.14"
82 NSOpenGLPFADoubleBuffer,
83 NSOpenGLPFAAlphaSize, 8,
84 NSOpenGLPFAColorSize, 24,
85 NSOpenGLPFADepthSize, 24,
86 NSOpenGLPFAMultisample,
87 NSOpenGLPFASampleBuffers, NSOpenGLPixelFormatAttribute(1),
88 NSOpenGLPFASamples, NSOpenGLPixelFormatAttribute(4),
89 SAL_WNODEPRECATED_DECLARATIONS_POP
92 SAL_WNODEPRECATED_DECLARATIONS_PUSH
93 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please
94 // use Metal or MetalKit."
95 pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:aAttributes];
96 SAL_WNODEPRECATED_DECLARATIONS_POP
98 else
100 SAL_WNODEPRECATED_DECLARATIONS_PUSH
101 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS 10.14"
102 NSOpenGLPixelFormatAttribute const aAttributes[] =
103 SAL_WNODEPRECATED_DECLARATIONS_POP
105 SAL_WNODEPRECATED_DECLARATIONS_PUSH
106 // "'NSOpenGLPFAOpenGLProfile' is deprecated: first deprecated in macOS 10.14",
107 // "'NSOpenGLProfileVersion3_2Core' is deprecated: first deprecated in macOS
108 // 10.14",
109 // "'NSOpenGLPFADoubleBuffer' is deprecated: first deprecated in macOS 10.14",
110 // "'NSOpenGLPFAAlphaSize' is deprecated: first deprecated in macOS 10.14",
111 // "'NSOpenGLPFAColorSize' is deprecated: first deprecated in macOS 10.14",
112 // "'NSOpenGLPFADepthSize' is deprecated: first deprecated in macOS 10.14",
113 // "'NSOpenGLPFAMultisample' is deprecated: first deprecated in macOS 10.14",
114 // "'NSOpenGLPFASampleBuffers' is deprecated: first deprecated in macOS 10.14",
115 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS
116 // 10.14",
117 // "'NSOpenGLPFASamples' is deprecated: first deprecated in macOS 10.14"
118 NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
119 NSOpenGLPFADoubleBuffer,
120 NSOpenGLPFAAlphaSize, 8,
121 NSOpenGLPFAColorSize, 24,
122 NSOpenGLPFADepthSize, 24,
123 NSOpenGLPFAMultisample,
124 NSOpenGLPFASampleBuffers, NSOpenGLPixelFormatAttribute(1),
125 NSOpenGLPFASamples, NSOpenGLPixelFormatAttribute(4),
126 SAL_WNODEPRECATED_DECLARATIONS_POP
129 SAL_WNODEPRECATED_DECLARATIONS_PUSH
130 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please
131 // use Metal or MetalKit."
132 pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:aAttributes];
133 SAL_WNODEPRECATED_DECLARATIONS_POP
136 SAL_WNODEPRECATED_DECLARATIONS_PUSH
137 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
138 // instead."
139 maSysData.mpNSView = [[NSOpenGLView alloc] initWithFrame: aInitFrame pixelFormat:pixFormat];
140 SAL_WNODEPRECATED_DECLARATIONS_POP
142 else
144 maSysData.mpNSView = [[NSView alloc] initWithFrame: aInitFrame];
147 if( maSysData.mpNSView )
149 if( mpClipView )
150 [mpClipView setDocumentView: maSysData.mpNSView];
154 AquaSalObject::~AquaSalObject()
156 assert( GetSalData()->mpInstance->IsMainThread() );
158 if( maSysData.mpNSView )
160 NSView *pView = maSysData.mpNSView;
161 [pView removeFromSuperview];
162 [pView release];
164 if( mpClipView )
166 [mpClipView removeFromSuperview];
167 [mpClipView release];
171 // Please note that the talk about QTMovieView below presumably refers
172 // to stuff in the QuickTime avmedia thingie, and that QuickTime is
173 // deprecated, not available for 64-bit code, and won't thus be used
174 // in a "modern" build of LO anyway. So the relevance of the comment
175 // is unclear.
178 sadly there seems to be no way to impose clipping on a child view,
179 especially a QTMovieView which seems to ignore the current context
180 completely. Also there is no real way to shape a window; on Aqua a
181 similar effect to non-rectangular windows is achieved by using a
182 non-opaque window and not painting where one wants the background
183 to shine through.
185 With respect to SalObject this leaves us to having an NSClipView
186 containing the child view. Even a QTMovieView respects the boundaries of
187 that, which gives us a clip "region" consisting of one rectangle.
188 This is gives us an 80% solution only, though.
191 void AquaSalObject::ResetClipRegion()
193 mbClip = false;
194 setClippedPosSize();
197 void AquaSalObject::BeginSetClipRegion( sal_uInt32 )
199 mbClip = false;
202 void AquaSalObject::UnionClipRegion( long nX, long nY, long nWidth, long nHeight )
204 if( mbClip )
206 if( nX < mnClipX )
208 mnClipWidth += mnClipX - nX;
209 mnClipX = nX;
211 if( nX + nWidth > mnClipX + mnClipWidth )
212 mnClipWidth = nX + nWidth - mnClipX;
213 if( nY < mnClipY )
215 mnClipHeight += mnClipY - nY;
216 mnClipY = nY;
218 if( nY + nHeight > mnClipY + mnClipHeight )
219 mnClipHeight = nY + nHeight - mnClipY;
221 else
223 mnClipX = nX;
224 mnClipY = nY;
225 mnClipWidth = nWidth;
226 mnClipHeight = nHeight;
227 mbClip = true;
231 void AquaSalObject::EndSetClipRegion()
233 setClippedPosSize();
236 void AquaSalObject::SetPosSize( long nX, long nY, long nWidth, long nHeight )
238 mnX = nX;
239 mnY = nY;
240 mnWidth = nWidth;
241 mnHeight = nHeight;
242 setClippedPosSize();
245 void AquaSalObject::setClippedPosSize()
247 OSX_SALDATA_RUNINMAIN( setClippedPosSize() )
249 NSRect aViewRect = { NSZeroPoint, NSMakeSize( mnWidth, mnHeight) };
250 if( maSysData.mpNSView )
252 NSView* pNSView = maSysData.mpNSView;
253 [pNSView setFrame: aViewRect];
256 NSRect aClipViewRect = NSMakeRect( mnX, mnY, mnWidth, mnHeight);
257 NSPoint aClipPt = NSZeroPoint;
258 if( mbClip )
260 aClipViewRect.origin.x += mnClipX;
261 aClipViewRect.origin.y += mnClipY;
262 aClipViewRect.size.width = mnClipWidth;
263 aClipViewRect.size.height = mnClipHeight;
264 aClipPt.x = mnClipX;
265 if( mnClipY == 0 )
266 aClipPt.y = mnHeight - mnClipHeight;
269 mpFrame->VCLToCocoa( aClipViewRect, false );
270 [mpClipView setFrame: aClipViewRect];
272 [mpClipView scrollToPoint: aClipPt];
275 void AquaSalObject::Show( bool bVisible )
277 if( !mpClipView )
278 return;
280 OSX_SALDATA_RUNINMAIN( Show( bVisible ) )
282 [mpClipView setHidden: (bVisible ? NO : YES)];
285 const SystemEnvData* AquaSalObject::GetSystemData() const
287 return &maSysData;
290 class AquaOpenGLContext : public OpenGLContext
292 public:
293 virtual void initWindow() override;
295 private:
296 GLWindow m_aGLWin;
298 virtual const GLWindow& getOpenGLWindow() const override { return m_aGLWin; }
299 virtual GLWindow& getModifiableOpenGLWindow() override { return m_aGLWin; }
300 SAL_WNODEPRECATED_DECLARATIONS_PUSH
301 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
302 // instead."
303 NSOpenGLView* getOpenGLView();
304 SAL_WNODEPRECATED_DECLARATIONS_POP
305 virtual bool ImplInit() override;
306 virtual SystemWindowData generateWinData(vcl::Window* pParent, bool bRequestLegacyContext) override;
307 virtual void makeCurrent() override;
308 virtual void destroyCurrentContext() override;
309 virtual void resetCurrent() override;
310 virtual void swapBuffers() override;
313 void AquaOpenGLContext::resetCurrent()
315 OSX_SALDATA_RUNINMAIN( resetCurrent() )
317 clearCurrent();
319 OpenGLZone aZone;
321 (void) this; // loplugin:staticmethods
322 SAL_WNODEPRECATED_DECLARATIONS_PUSH
323 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
324 // MetalKit."
325 [NSOpenGLContext clearCurrentContext];
326 SAL_WNODEPRECATED_DECLARATIONS_POP
329 void AquaOpenGLContext::makeCurrent()
331 OSX_SALDATA_RUNINMAIN( makeCurrent() )
333 if (isCurrent())
334 return;
336 OpenGLZone aZone;
338 clearCurrent();
340 SAL_WNODEPRECATED_DECLARATIONS_PUSH
341 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
342 // instead."
343 NSOpenGLView* pView = getOpenGLView();
344 SAL_WNODEPRECATED_DECLARATIONS_POP
345 [[pView openGLContext] makeCurrentContext];
347 registerAsCurrent();
350 void AquaOpenGLContext::swapBuffers()
352 OSX_SALDATA_RUNINMAIN( swapBuffers() )
354 OpenGLZone aZone;
356 SAL_WNODEPRECATED_DECLARATIONS_PUSH
357 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
358 // instead."
359 NSOpenGLView* pView = getOpenGLView();
360 SAL_WNODEPRECATED_DECLARATIONS_POP
361 [[pView openGLContext] flushBuffer];
363 BuffersSwapped();
366 SystemWindowData AquaOpenGLContext::generateWinData(vcl::Window* /*pParent*/, bool bRequestLegacyContext)
368 SystemWindowData aWinData;
369 aWinData.bOpenGL = true;
370 aWinData.bLegacy = bRequestLegacyContext;
371 return aWinData;
374 void AquaOpenGLContext::destroyCurrentContext()
376 OSX_SALDATA_RUNINMAIN( destroyCurrentContext() )
377 SAL_WNODEPRECATED_DECLARATIONS_PUSH
378 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
379 // MetalKit."
380 [NSOpenGLContext clearCurrentContext];
381 SAL_WNODEPRECATED_DECLARATIONS_POP
384 void AquaOpenGLContext::initWindow()
386 OSX_SALDATA_RUNINMAIN( initWindow() )
388 if( !m_pChildWindow )
390 SystemWindowData winData = generateWinData(mpWindow, mbRequestLegacyContext);
391 m_pChildWindow = VclPtr<SystemChildWindow>::Create(mpWindow, 0, &winData, false);
394 if (m_pChildWindow)
396 InitChildWindow(m_pChildWindow.get());
400 bool AquaOpenGLContext::ImplInit()
402 OSX_SALDATA_RUNINMAIN_UNION( ImplInit(), boolean )
404 OpenGLZone aZone;
406 VCL_GL_INFO("OpenGLContext::ImplInit----start");
407 SAL_WNODEPRECATED_DECLARATIONS_PUSH
408 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
409 // instead."
410 NSOpenGLView* pView = getOpenGLView();
411 SAL_WNODEPRECATED_DECLARATIONS_POP
412 [[pView openGLContext] makeCurrentContext];
414 bool bRet = InitGL();
415 InitGLDebugging();
416 return bRet;
419 SAL_WNODEPRECATED_DECLARATIONS_PUSH
420 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
421 // instead."
422 NSOpenGLView* AquaOpenGLContext::getOpenGLView()
423 SAL_WNODEPRECATED_DECLARATIONS_POP
425 SAL_WNODEPRECATED_DECLARATIONS_PUSH
426 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
427 // instead."
428 return reinterpret_cast<NSOpenGLView*>(m_pChildWindow->GetSystemData()->mpNSView);
429 SAL_WNODEPRECATED_DECLARATIONS_POP
432 OpenGLContext* AquaSalInstance::CreateOpenGLContext()
434 OSX_SALDATA_RUNINMAIN_POINTER( CreateOpenGLContext(), OpenGLContext* )
435 return new AquaOpenGLContext;
438 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */