bump product version to 6.3.0.0.beta1
[LibreOffice.git] / vcl / osx / salobj.cxx
blobfb1105b6adb7606682ebf691d42a12dce9bfe697
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.nSize = sizeof( maSysData );
47 maSysData.mpNSView = nullptr;
48 maSysData.mbOpenGL = false;
50 NSRect aInitFrame = { NSZeroPoint, { 20, 20 } };
51 mpClipView = [[NSClipView alloc] initWithFrame: aInitFrame ];
52 if( mpClipView )
54 [mpFrame->getNSView() addSubview: mpClipView];
55 [mpClipView setHidden: YES];
57 if (pWindowData && pWindowData->bOpenGL)
59 maSysData.mbOpenGL = true;
60 SAL_WNODEPRECATED_DECLARATIONS_PUSH
61 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please use
62 // Metal or MetalKit."
63 NSOpenGLPixelFormat* pixFormat = nullptr;
64 SAL_WNODEPRECATED_DECLARATIONS_POP
66 if (pWindowData->bLegacy)
68 SAL_WNODEPRECATED_DECLARATIONS_PUSH
69 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS 10.14"
70 NSOpenGLPixelFormatAttribute const aAttributes[] =
71 SAL_WNODEPRECATED_DECLARATIONS_POP
73 SAL_WNODEPRECATED_DECLARATIONS_PUSH
74 // "'NSOpenGLPFADoubleBuffer' is deprecated: first deprecated in macOS 10.14",
75 // "'NSOpenGLPFAAlphaSize' is deprecated: first deprecated in macOS 10.14",
76 // "'NSOpenGLPFAColorSize' is deprecated: first deprecated in macOS 10.14",
77 // "'NSOpenGLPFADepthSize' is deprecated: first deprecated in macOS 10.14",
78 // "'NSOpenGLPFAMultisample' is deprecated: first deprecated in macOS 10.14",
79 // "'NSOpenGLPFASampleBuffers' is deprecated: first deprecated in macOS 10.14",
80 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS
81 // 10.14",
82 // "'NSOpenGLPFASamples' is deprecated: first deprecated in macOS 10.14"
83 NSOpenGLPFADoubleBuffer,
84 NSOpenGLPFAAlphaSize, 8,
85 NSOpenGLPFAColorSize, 24,
86 NSOpenGLPFADepthSize, 24,
87 NSOpenGLPFAMultisample,
88 NSOpenGLPFASampleBuffers, NSOpenGLPixelFormatAttribute(1),
89 NSOpenGLPFASamples, NSOpenGLPixelFormatAttribute(4),
90 SAL_WNODEPRECATED_DECLARATIONS_POP
93 SAL_WNODEPRECATED_DECLARATIONS_PUSH
94 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please
95 // use Metal or MetalKit."
96 pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:aAttributes];
97 SAL_WNODEPRECATED_DECLARATIONS_POP
99 else
101 SAL_WNODEPRECATED_DECLARATIONS_PUSH
102 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS 10.14"
103 NSOpenGLPixelFormatAttribute const aAttributes[] =
104 SAL_WNODEPRECATED_DECLARATIONS_POP
106 SAL_WNODEPRECATED_DECLARATIONS_PUSH
107 // "'NSOpenGLPFAOpenGLProfile' is deprecated: first deprecated in macOS 10.14",
108 // "'NSOpenGLProfileVersion3_2Core' is deprecated: first deprecated in macOS
109 // 10.14",
110 // "'NSOpenGLPFADoubleBuffer' is deprecated: first deprecated in macOS 10.14",
111 // "'NSOpenGLPFAAlphaSize' is deprecated: first deprecated in macOS 10.14",
112 // "'NSOpenGLPFAColorSize' is deprecated: first deprecated in macOS 10.14",
113 // "'NSOpenGLPFADepthSize' is deprecated: first deprecated in macOS 10.14",
114 // "'NSOpenGLPFAMultisample' is deprecated: first deprecated in macOS 10.14",
115 // "'NSOpenGLPFASampleBuffers' is deprecated: first deprecated in macOS 10.14",
116 // "'NSOpenGLPixelFormatAttribute' is deprecated: first deprecated in macOS
117 // 10.14",
118 // "'NSOpenGLPFASamples' is deprecated: first deprecated in macOS 10.14"
119 NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
120 NSOpenGLPFADoubleBuffer,
121 NSOpenGLPFAAlphaSize, 8,
122 NSOpenGLPFAColorSize, 24,
123 NSOpenGLPFADepthSize, 24,
124 NSOpenGLPFAMultisample,
125 NSOpenGLPFASampleBuffers, NSOpenGLPixelFormatAttribute(1),
126 NSOpenGLPFASamples, NSOpenGLPixelFormatAttribute(4),
127 SAL_WNODEPRECATED_DECLARATIONS_POP
130 SAL_WNODEPRECATED_DECLARATIONS_PUSH
131 // "'NSOpenGLPixelFormat' is deprecated: first deprecated in macOS 10.14 - Please
132 // use Metal or MetalKit."
133 pixFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:aAttributes];
134 SAL_WNODEPRECATED_DECLARATIONS_POP
137 SAL_WNODEPRECATED_DECLARATIONS_PUSH
138 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
139 // instead."
140 maSysData.mpNSView = [[NSOpenGLView alloc] initWithFrame: aInitFrame pixelFormat:pixFormat];
141 SAL_WNODEPRECATED_DECLARATIONS_POP
143 else
145 maSysData.mpNSView = [[NSView alloc] initWithFrame: aInitFrame];
148 if( maSysData.mpNSView )
150 if( mpClipView )
151 [mpClipView setDocumentView: maSysData.mpNSView];
155 AquaSalObject::~AquaSalObject()
157 assert( GetSalData()->mpInstance->IsMainThread() );
159 if( maSysData.mpNSView )
161 NSView *pView = maSysData.mpNSView;
162 [pView removeFromSuperview];
163 [pView release];
165 if( mpClipView )
167 [mpClipView removeFromSuperview];
168 [mpClipView release];
172 // Please note that the talk about QTMovieView below presumably refers
173 // to stuff in the QuickTime avmedia thingie, and that QuickTime is
174 // deprecated, not available for 64-bit code, and won't thus be used
175 // in a "modern" build of LO anyway. So the relevance of the comment
176 // is unclear.
179 sadly there seems to be no way to impose clipping on a child view,
180 especially a QTMovieView which seems to ignore the current context
181 completely. Also there is no real way to shape a window; on Aqua a
182 similar effect to non-rectangular windows is achieved by using a
183 non-opaque window and not painting where one wants the background
184 to shine through.
186 With respect to SalObject this leaves us to having an NSClipView
187 containing the child view. Even a QTMovieView respects the boundaries of
188 that, which gives us a clip "region" consisting of one rectangle.
189 This is gives us an 80% solution only, though.
192 void AquaSalObject::ResetClipRegion()
194 mbClip = false;
195 setClippedPosSize();
198 void AquaSalObject::BeginSetClipRegion( sal_uInt32 )
200 mbClip = false;
203 void AquaSalObject::UnionClipRegion( long nX, long nY, long nWidth, long nHeight )
205 if( mbClip )
207 if( nX < mnClipX )
209 mnClipWidth += mnClipX - nX;
210 mnClipX = nX;
212 if( nX + nWidth > mnClipX + mnClipWidth )
213 mnClipWidth = nX + nWidth - mnClipX;
214 if( nY < mnClipY )
216 mnClipHeight += mnClipY - nY;
217 mnClipY = nY;
219 if( nY + nHeight > mnClipY + mnClipHeight )
220 mnClipHeight = nY + nHeight - mnClipY;
222 else
224 mnClipX = nX;
225 mnClipY = nY;
226 mnClipWidth = nWidth;
227 mnClipHeight = nHeight;
228 mbClip = true;
232 void AquaSalObject::EndSetClipRegion()
234 setClippedPosSize();
237 void AquaSalObject::SetPosSize( long nX, long nY, long nWidth, long nHeight )
239 mnX = nX;
240 mnY = nY;
241 mnWidth = nWidth;
242 mnHeight = nHeight;
243 setClippedPosSize();
246 void AquaSalObject::setClippedPosSize()
248 OSX_SALDATA_RUNINMAIN( setClippedPosSize() )
250 NSRect aViewRect = { NSZeroPoint, NSMakeSize( mnWidth, mnHeight) };
251 if( maSysData.mpNSView )
253 NSView* pNSView = maSysData.mpNSView;
254 [pNSView setFrame: aViewRect];
257 NSRect aClipViewRect = NSMakeRect( mnX, mnY, mnWidth, mnHeight);
258 NSPoint aClipPt = NSZeroPoint;
259 if( mbClip )
261 aClipViewRect.origin.x += mnClipX;
262 aClipViewRect.origin.y += mnClipY;
263 aClipViewRect.size.width = mnClipWidth;
264 aClipViewRect.size.height = mnClipHeight;
265 aClipPt.x = mnClipX;
266 if( mnClipY == 0 )
267 aClipPt.y = mnHeight - mnClipHeight;
270 mpFrame->VCLToCocoa( aClipViewRect, false );
271 [mpClipView setFrame: aClipViewRect];
273 [mpClipView scrollToPoint: aClipPt];
276 void AquaSalObject::Show( bool bVisible )
278 if( !mpClipView )
279 return;
281 OSX_SALDATA_RUNINMAIN( Show( bVisible ) )
283 [mpClipView setHidden: (bVisible ? NO : YES)];
286 const SystemEnvData* AquaSalObject::GetSystemData() const
288 return &maSysData;
291 class AquaOpenGLContext : public OpenGLContext
293 public:
294 virtual void initWindow() override;
296 private:
297 GLWindow m_aGLWin;
299 virtual const GLWindow& getOpenGLWindow() const override { return m_aGLWin; }
300 virtual GLWindow& getModifiableOpenGLWindow() override { return m_aGLWin; }
301 SAL_WNODEPRECATED_DECLARATIONS_PUSH
302 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
303 // instead."
304 NSOpenGLView* getOpenGLView();
305 SAL_WNODEPRECATED_DECLARATIONS_POP
306 virtual bool ImplInit() override;
307 virtual SystemWindowData generateWinData(vcl::Window* pParent, bool bRequestLegacyContext) override;
308 virtual void makeCurrent() override;
309 virtual void destroyCurrentContext() override;
310 virtual void resetCurrent() override;
311 virtual void swapBuffers() override;
314 void AquaOpenGLContext::resetCurrent()
316 OSX_SALDATA_RUNINMAIN( resetCurrent() )
318 clearCurrent();
320 OpenGLZone aZone;
322 (void) this; // loplugin:staticmethods
323 SAL_WNODEPRECATED_DECLARATIONS_PUSH
324 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
325 // MetalKit."
326 [NSOpenGLContext clearCurrentContext];
327 SAL_WNODEPRECATED_DECLARATIONS_POP
330 void AquaOpenGLContext::makeCurrent()
332 OSX_SALDATA_RUNINMAIN( makeCurrent() )
334 if (isCurrent())
335 return;
337 OpenGLZone aZone;
339 clearCurrent();
341 SAL_WNODEPRECATED_DECLARATIONS_PUSH
342 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
343 // instead."
344 NSOpenGLView* pView = getOpenGLView();
345 SAL_WNODEPRECATED_DECLARATIONS_POP
346 [[pView openGLContext] makeCurrentContext];
348 registerAsCurrent();
351 void AquaOpenGLContext::swapBuffers()
353 OSX_SALDATA_RUNINMAIN( swapBuffers() )
355 OpenGLZone aZone;
357 SAL_WNODEPRECATED_DECLARATIONS_PUSH
358 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
359 // instead."
360 NSOpenGLView* pView = getOpenGLView();
361 SAL_WNODEPRECATED_DECLARATIONS_POP
362 [[pView openGLContext] flushBuffer];
364 BuffersSwapped();
367 SystemWindowData AquaOpenGLContext::generateWinData(vcl::Window* /*pParent*/, bool bRequestLegacyContext)
369 SystemWindowData aWinData;
370 aWinData.bOpenGL = true;
371 aWinData.bLegacy = bRequestLegacyContext;
372 return aWinData;
375 void AquaOpenGLContext::destroyCurrentContext()
377 OSX_SALDATA_RUNINMAIN( destroyCurrentContext() )
378 SAL_WNODEPRECATED_DECLARATIONS_PUSH
379 // "'NSOpenGLContext' is deprecated: first deprecated in macOS 10.14 - Please use Metal or
380 // MetalKit."
381 [NSOpenGLContext clearCurrentContext];
382 SAL_WNODEPRECATED_DECLARATIONS_POP
385 void AquaOpenGLContext::initWindow()
387 OSX_SALDATA_RUNINMAIN( initWindow() )
389 if( !m_pChildWindow )
391 SystemWindowData winData = generateWinData(mpWindow, mbRequestLegacyContext);
392 m_pChildWindow = VclPtr<SystemChildWindow>::Create(mpWindow, 0, &winData, false);
395 if (m_pChildWindow)
397 InitChildWindow(m_pChildWindow.get());
401 bool AquaOpenGLContext::ImplInit()
403 OSX_SALDATA_RUNINMAIN_UNION( ImplInit(), boolean )
405 OpenGLZone aZone;
407 VCL_GL_INFO("OpenGLContext::ImplInit----start");
408 SAL_WNODEPRECATED_DECLARATIONS_PUSH
409 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
410 // instead."
411 NSOpenGLView* pView = getOpenGLView();
412 SAL_WNODEPRECATED_DECLARATIONS_POP
413 [[pView openGLContext] makeCurrentContext];
415 bool bRet = InitGL();
416 InitGLDebugging();
417 return bRet;
420 SAL_WNODEPRECATED_DECLARATIONS_PUSH
421 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
422 // instead."
423 NSOpenGLView* AquaOpenGLContext::getOpenGLView()
424 SAL_WNODEPRECATED_DECLARATIONS_POP
426 SAL_WNODEPRECATED_DECLARATIONS_PUSH
427 // "'NSOpenGLView' is deprecated: first deprecated in macOS 10.14 - Please use MTKView
428 // instead."
429 return reinterpret_cast<NSOpenGLView*>(m_pChildWindow->GetSystemData()->mpNSView);
430 SAL_WNODEPRECATED_DECLARATIONS_POP
433 OpenGLContext* AquaSalInstance::CreateOpenGLContext()
435 OSX_SALDATA_RUNINMAIN_POINTER( CreateOpenGLContext(), OpenGLContext* )
436 return new AquaOpenGLContext;
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */