xref: /trunk/main/vcl/aqua/source/window/salframe.cxx (revision c60bfbc060a01af3eab9e58ea07b1ecd178874bc) !
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 
27 #include <string>
28 #include <cstdlib>
29 
30 #include "rtl/ustrbuf.hxx"
31 
32 #include "osl/file.h"
33 
34 #include "vcl/svapp.hxx"
35 #include "vcl/window.hxx"
36 #include "vcl/timer.hxx"
37 
38 #include "aqua/saldata.hxx"
39 #include "aqua/salgdi.h"
40 #include "aqua/salframe.h"
41 #include "aqua/salmenu.h"
42 #include "aqua/saltimer.h"
43 #include "aqua/salinst.h"
44 #include "aqua/salframeview.h"
45 #include "aqua/aqua11yfactory.h"
46 
47 #include "salwtype.hxx"
48 
49 #include "premac.h"
50 // needed for theming
51 // FIXME: move theming code to salnativewidgets.cxx
52 #include <Carbon/Carbon.h>
53 #include "postmac.h"
54 
55 
56 using namespace std;
57 
58 // =======================================================================
59 
60 AquaSalFrame* AquaSalFrame::s_pCaptureFrame = NULL;
61 
62 // =======================================================================
63 
AquaSalFrame(SalFrame * pParent,sal_uLong salFrameStyle)64 AquaSalFrame::AquaSalFrame( SalFrame* pParent, sal_uLong salFrameStyle ) :
65     mpNSWindow(nil),
66     mpNSView(nil),
67     mpDockMenuEntry(nil),
68     mpGraphics(NULL),
69     mpParent(NULL),
70     mnMinWidth(0),
71     mnMinHeight(0),
72     mnMaxWidth(0),
73     mnMaxHeight(0),
74     mbGraphics(false),
75     mbFullScreen( false ),
76     mbShown(false),
77     mbInitShow(true),
78     mbPositioned(false),
79     mbSized(false),
80     mbPresentation( false ),
81     mnStyle( salFrameStyle ),
82     mnStyleMask( 0 ),
83     mnLastEventTime( 0 ),
84     mnLastModifierFlags( 0 ),
85     mpMenu( NULL ),
86     mnExtStyle( 0 ),
87     mePointerStyle( POINTER_ARROW ),
88     mnTrackingRectTag( 0 ),
89     mfBackingScale( 1.0f ),
90     mrClippingPath( 0 ),
91     mnICOptions( 0 )
92 {
93     maSysData.nSize     = sizeof( SystemEnvData );
94 
95     mpParent = dynamic_cast<AquaSalFrame*>(pParent);
96 
97     initWindowAndView();
98 
99     SalData* pSalData = GetSalData();
100     pSalData->maFrames.push_front( this );
101     pSalData->maFrameCheck.insert( this );
102 }
103 
104 // -----------------------------------------------------------------------
105 
~AquaSalFrame()106 AquaSalFrame::~AquaSalFrame()
107 {
108     // if the frame is destroyed and has the current menubar
109     // set the default menubar
110     if( mpMenu && mpMenu->mbMenuBar && AquaSalMenu::pCurrentMenuBar == mpMenu )
111         AquaSalMenu::setDefaultMenu();
112 
113     // cleanup clipping stuff
114     ResetClipRegion();
115 
116     [SalFrameView unsetMouseFrame: this];
117 
118     SalData* pSalData = GetSalData();
119     pSalData->maFrames.remove( this );
120     pSalData->maFrameCheck.erase( this );
121     pSalData->maPresentationFrames.remove( this );
122 
123     DBG_ASSERT( this != s_pCaptureFrame, "capture frame destroyed" );
124     if( this == s_pCaptureFrame )
125         s_pCaptureFrame = NULL;
126 
127     if ( mpGraphics )
128         delete mpGraphics;
129 
130     if( mpDockMenuEntry )
131         // life cycle comment: the menu has ownership of the item, so no release
132         [AquaSalInstance::GetDynamicDockMenu() removeItem: mpDockMenuEntry];
133     if ( mpNSView ) {
134         [AquaA11yFactory revokeView: mpNSView];
135         [mpNSView release];
136     }
137     if ( mpNSWindow )
138         [mpNSWindow release];
139 }
140 
141 // -----------------------------------------------------------------------
142 
initWindowAndView()143 void AquaSalFrame::initWindowAndView()
144 {
145     // initialize mirroring parameters
146     // FIXME: screens changing
147     NSScreen* pNSScreen = [mpNSWindow screen];
148     if( pNSScreen == nil )
149         pNSScreen = [NSScreen mainScreen];
150     maScreenRect = [pNSScreen frame];
151 
152     // calculate some default geometry
153     NSRect aVisibleRect = [pNSScreen visibleFrame];
154     CocoaToVCL( aVisibleRect );
155 
156     maGeometry.nX = static_cast<int>(aVisibleRect.origin.x + aVisibleRect.size.width / 10);
157     maGeometry.nY = static_cast<int>(aVisibleRect.origin.y + aVisibleRect.size.height / 10);
158     maGeometry.nWidth = static_cast<unsigned int>(aVisibleRect.size.width * 0.8);
159     maGeometry.nHeight = static_cast<unsigned int>(aVisibleRect.size.height * 0.8);
160 
161     // calculate style mask
162     if( (mnStyle & SAL_FRAME_STYLE_FLOAT) ||
163         (mnStyle & SAL_FRAME_STYLE_OWNERDRAWDECORATION) )
164         mnStyleMask = NSBorderlessWindowMask;
165     else if( mnStyle & SAL_FRAME_STYLE_DEFAULT )
166     {
167         mnStyleMask = NSTitledWindowMask            |
168                       NSMiniaturizableWindowMask    |
169                       NSResizableWindowMask         |
170                       NSClosableWindowMask;
171         // make default window "maximized"
172         maGeometry.nX = static_cast<int>(aVisibleRect.origin.x);
173         maGeometry.nY = static_cast<int>(aVisibleRect.origin.y);
174         maGeometry.nWidth = static_cast<int>(aVisibleRect.size.width);
175         maGeometry.nHeight = static_cast<int>(aVisibleRect.size.height);
176         mbPositioned = mbSized = true;
177     }
178     else
179     {
180         if( (mnStyle & SAL_FRAME_STYLE_MOVEABLE) )
181         {
182             mnStyleMask |= NSTitledWindowMask;
183             if( mpParent == NULL )
184                 mnStyleMask |= NSMiniaturizableWindowMask;
185         }
186         if( (mnStyle & SAL_FRAME_STYLE_SIZEABLE) )
187             mnStyleMask |= NSResizableWindowMask;
188         if( (mnStyle & SAL_FRAME_STYLE_CLOSEABLE) )
189             mnStyleMask |= NSClosableWindowMask;
190         // documentation says anything other than NSBorderlessWindowMask (=0)
191         // should also include NSTitledWindowMask;
192         if( mnStyleMask != 0 )
193             mnStyleMask |= NSTitledWindowMask;
194     }
195 
196     // #i91990# support GUI-less (daemon) execution
197     @try
198     {
199         mpNSWindow = [[SalFrameWindow alloc] initWithSalFrame: this];
200         mpNSView = [[SalFrameView alloc] initWithSalFrame: this];
201     }
202     @catch ( id exception )
203     {
204         return;
205     }
206 
207     if( (mnStyle & SAL_FRAME_STYLE_TOOLTIP) )
208         [mpNSWindow setIgnoresMouseEvents: YES];
209     else
210         [mpNSWindow setAcceptsMouseMovedEvents: YES];
211     [mpNSWindow setHasShadow: YES];
212     [mpNSWindow setDelegate: static_cast<id<NSWindowDelegate> >(mpNSWindow)];
213     UpdateBackingScale();
214 
215     const NSRect aRect = { NSZeroPoint, NSMakeSize( maGeometry.nWidth, maGeometry.nHeight )};
216     mnTrackingRectTag = [mpNSView addTrackingRect: aRect owner: mpNSView userData: nil assumeInside: NO];
217 
218     maSysData.mpNSView = mpNSView;
219 
220     UpdateFrameGeometry();
221 
222     [mpNSWindow setContentView: mpNSView];
223 }
224 
225 // -----------------------------------------------------------------------
226 
CocoaToVCL(NSRect & io_rRect,bool bRelativeToScreen)227 void AquaSalFrame::CocoaToVCL( NSRect& io_rRect, bool bRelativeToScreen )
228 {
229     if( bRelativeToScreen )
230         io_rRect.origin.y = maScreenRect.size.height - (io_rRect.origin.y+io_rRect.size.height);
231     else
232         io_rRect.origin.y = maGeometry.nHeight - (io_rRect.origin.y+io_rRect.size.height);
233 }
234 
VCLToCocoa(NSRect & io_rRect,bool bRelativeToScreen)235 void AquaSalFrame::VCLToCocoa( NSRect& io_rRect, bool bRelativeToScreen )
236 {
237     if( bRelativeToScreen )
238         io_rRect.origin.y = maScreenRect.size.height - (io_rRect.origin.y+io_rRect.size.height);
239     else
240         io_rRect.origin.y = maGeometry.nHeight - (io_rRect.origin.y+io_rRect.size.height);
241 }
242 
CocoaToVCL(NSPoint & io_rPoint,bool bRelativeToScreen)243 void AquaSalFrame::CocoaToVCL( NSPoint& io_rPoint, bool bRelativeToScreen )
244 {
245     if( bRelativeToScreen )
246         io_rPoint.y = maScreenRect.size.height - io_rPoint.y;
247     else
248         io_rPoint.y = maGeometry.nHeight - io_rPoint.y;
249 }
250 
VCLToCocoa(NSPoint & io_rPoint,bool bRelativeToScreen)251 void AquaSalFrame::VCLToCocoa( NSPoint& io_rPoint, bool bRelativeToScreen )
252 {
253     if( bRelativeToScreen )
254         io_rPoint.y = maScreenRect.size.height - io_rPoint.y;
255     else
256         io_rPoint.y = maGeometry.nHeight - io_rPoint.y;
257 }
258 
259 // -----------------------------------------------------------------------
260 
261 // SAL_AQUA_BACKING_SCALE=1 on a Retina screen (or =2 on a 1x one) tests the other scale
262 // whole numbers only: CGLayer rounds a fractional pixel size up
getBackingScaleOverride()263 static float getBackingScaleOverride()
264 {
265     const char* pEnvStr = getenv( "SAL_AQUA_BACKING_SCALE" );
266     if( !pEnvStr )
267         return 0;
268     char* pEnd = NULL;
269     const long nValue = strtol( pEnvStr, &pEnd, 10 );
270     if( pEnd == pEnvStr || *pEnd || nValue < 1 || nValue > 4 )
271         return 0;
272     return static_cast<float>(nValue);
273 }
274 
UpdateBackingScale()275 bool AquaSalFrame::UpdateBackingScale()
276 {
277     static const float fOverride = getBackingScaleOverride();
278     float fScale = mpNSWindow ? [mpNSWindow backingScaleFactor] : 1.0f;
279     if( fOverride > 0 )
280         fScale = fOverride;
281     if( fScale == mfBackingScale )
282         return false;
283     mfBackingScale = fScale;
284     return true;
285 }
286 
287 // -----------------------------------------------------------------------
288 
screenParametersChanged()289 void AquaSalFrame::screenParametersChanged()
290 {
291     UpdateFrameGeometry();
292 
293     if( mpGraphics )
294         mpGraphics->updateResolution();
295     CallCallback( SALEVENT_DISPLAYCHANGED, 0 );
296 }
297 
298 // -----------------------------------------------------------------------
299 
GetGraphics()300 SalGraphics* AquaSalFrame::GetGraphics()
301 {
302     if ( mbGraphics )
303         return NULL;
304 
305     if ( !mpGraphics )
306     {
307         mpGraphics = new AquaSalGraphics;
308         mpGraphics->SetWindowGraphics( this );
309     }
310 
311     mbGraphics = TRUE;
312     return mpGraphics;
313 }
314 
315 // -----------------------------------------------------------------------
316 
ReleaseGraphics(SalGraphics * pGraphics)317 void AquaSalFrame::ReleaseGraphics( SalGraphics *pGraphics )
318 {
319     (void)pGraphics;
320     DBG_ASSERT( pGraphics == mpGraphics, "graphics released on wrong frame" );
321     mbGraphics = FALSE;
322 }
323 
324 // -----------------------------------------------------------------------
325 
PostEvent(void * pData)326 sal_Bool AquaSalFrame::PostEvent( void *pData )
327 {
328     GetSalData()->mpFirstInstance->PostUserEvent( this, SALEVENT_USEREVENT, pData );
329     return TRUE;
330 }
331 
332 // -----------------------------------------------------------------------
SetTitle(const XubString & rTitle)333 void AquaSalFrame::SetTitle(const XubString& rTitle)
334 {
335     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetTitle, this, boost::cref( rTitle ) ) ) )
336         return;
337 
338     if ( !mpNSWindow )
339         return;
340 
341     // #i113170# may not be the main thread if called from UNO API
342     SalData::ensureThreadAutoreleasePool();
343 
344     NSString* pTitle = CreateNSString( rTitle );
345     [mpNSWindow setTitle: pTitle];
346 
347     // create an entry in the dock menu
348     const sal_uLong nAppWindowStyle = (SAL_FRAME_STYLE_CLOSEABLE | SAL_FRAME_STYLE_MOVEABLE);
349     if( mpParent == NULL &&
350         (mnStyle & nAppWindowStyle) == nAppWindowStyle )
351     {
352         if( mpDockMenuEntry == NULL )
353         {
354             NSMenu* pDock = AquaSalInstance::GetDynamicDockMenu();
355             mpDockMenuEntry = [pDock insertItemWithTitle: pTitle
356                                      action: @selector(dockMenuItemTriggered:)
357                                      keyEquivalent: @""
358                                      atIndex: 0];
359             [mpDockMenuEntry setTarget: mpNSWindow];
360 
361             // TODO: image (either the generic window image or an icon
362             // check mark (for "main" window ?)
363         }
364         else
365             [mpDockMenuEntry setTitle: pTitle];
366     }
367 
368     if (pTitle)
369         [pTitle release];
370 }
371 
372 // -----------------------------------------------------------------------
373 
SetIcon(sal_uInt16)374 void AquaSalFrame::SetIcon( sal_uInt16 )
375 {
376 }
377 
378 // -----------------------------------------------------------------------
379 
SetRepresentedURL(const rtl::OUString & i_rDocURL)380 void AquaSalFrame::SetRepresentedURL( const rtl::OUString& i_rDocURL )
381 {
382     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetRepresentedURL, this, boost::cref( i_rDocURL ) ) ) )
383         return;
384 
385     // #i113170# may not be the main thread if called from UNO API
386     SalData::ensureThreadAutoreleasePool();
387 
388     if( i_rDocURL.indexOfAsciiL( "file:", 5 ) == 0 )
389     {
390         rtl::OUString aSysPath;
391         osl_getSystemPathFromFileURL( i_rDocURL.pData, &aSysPath.pData );
392         NSString* pStr = CreateNSString( aSysPath );
393         if( pStr )
394         {
395             [pStr autorelease];
396             [mpNSWindow setRepresentedFilename: pStr];
397         }
398     }
399 }
400 
401 // -----------------------------------------------------------------------
402 
initShow()403 void AquaSalFrame::initShow()
404 {
405     mbInitShow = false;
406     if( ! mbPositioned && ! mbFullScreen )
407     {
408         Rectangle aScreenRect;
409         GetWorkArea( aScreenRect );
410         if( mpParent ) // center relative to parent
411         {
412             // center on parent
413             long nNewX = mpParent->maGeometry.nX + ((long)mpParent->maGeometry.nWidth - (long)maGeometry.nWidth)/2;
414             if( nNewX < aScreenRect.Left() )
415                 nNewX = aScreenRect.Left();
416             if( long(nNewX + maGeometry.nWidth) > aScreenRect.Right() )
417                 nNewX = aScreenRect.Right() - maGeometry.nWidth-1;
418             long nNewY = mpParent->maGeometry.nY + ((long)mpParent->maGeometry.nHeight - (long)maGeometry.nHeight)/2;
419             if( nNewY < aScreenRect.Top() )
420                 nNewY = aScreenRect.Top();
421             if( nNewY > aScreenRect.Bottom() )
422                 nNewY = aScreenRect.Bottom() - maGeometry.nHeight-1;
423             SetPosSize( nNewX - mpParent->maGeometry.nX,
424                         nNewY - mpParent->maGeometry.nY,
425                         0, 0,  SAL_FRAME_POSSIZE_X | SAL_FRAME_POSSIZE_Y );
426         }
427         else if( ! (mnStyle & SAL_FRAME_STYLE_SIZEABLE) )
428         {
429             // center on screen
430             long nNewX = (aScreenRect.GetWidth() - maGeometry.nWidth)/2;
431             long nNewY = (aScreenRect.GetHeight() - maGeometry.nHeight)/2;
432             SetPosSize( nNewX, nNewY, 0, 0,  SAL_FRAME_POSSIZE_X | SAL_FRAME_POSSIZE_Y );
433         }
434     }
435 
436     // make sure the view is present in the wrapper list before any children receive focus
437     [AquaA11yFactory registerView: mpNSView];
438 }
439 
SendPaintEvent(const Rectangle * pRect)440 void AquaSalFrame::SendPaintEvent( const Rectangle* pRect )
441 {
442     SalPaintEvent aPaintEvt( 0, 0, maGeometry.nWidth, maGeometry.nHeight, true );
443     if( pRect )
444     {
445         aPaintEvt.mnBoundX      = pRect->Left();
446         aPaintEvt.mnBoundY      = pRect->Top();
447         aPaintEvt.mnBoundWidth  = pRect->GetWidth();
448         aPaintEvt.mnBoundHeight = pRect->GetHeight();
449     }
450 
451     CallCallback(SALEVENT_PAINT, &aPaintEvt);
452 }
453 
454 // -----------------------------------------------------------------------
455 
Show(sal_Bool bVisible,sal_Bool bNoActivate)456 void AquaSalFrame::Show(sal_Bool bVisible, sal_Bool bNoActivate)
457 {
458     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::Show, this, bVisible, bNoActivate ) ) )
459         return;
460 
461     if ( !mpNSWindow )
462         return;
463 
464     // #i113170# may not be the main thread if called from UNO API
465     SalData::ensureThreadAutoreleasePool();
466 
467     mbShown = bVisible;
468     if(bVisible)
469     {
470         if( mbInitShow )
471             initShow();
472 
473         CallCallback(SALEVENT_RESIZE, 0);
474         // trigger filling our backbuffer
475         SendPaintEvent();
476 
477         if( bNoActivate || [mpNSWindow canBecomeKeyWindow] == NO )
478             [mpNSWindow orderFront: NSApp];
479         else
480             [mpNSWindow makeKeyAndOrderFront: NSApp];
481 
482         // a window first shown on another screen need not report a backing change
483         if( UpdateBackingScale() )
484             SendPaintEvent();
485 
486         if( mpParent )
487         {
488             /* #i92674# #i96433# we do not want an invisible parent to show up (which adding a visible
489                child implicitly does). However we also do not want a parentless toolbar.
490 
491                HACK: try to decide when we should not insert a child to its parent
492                floaters and ownerdraw windows have not yet shown up in cases where
493                we don't want the parent to become visible
494             */
495             if( mpParent->mbShown || (mnStyle & (SAL_FRAME_STYLE_OWNERDRAWDECORATION | SAL_FRAME_STYLE_FLOAT) ) )
496             {
497                 [mpParent->mpNSWindow addChildWindow: mpNSWindow ordered: NSWindowAbove];
498             }
499         }
500 
501         if( mbPresentation )
502             [mpNSWindow makeMainWindow];
503     }
504     else
505     {
506         // if the frame holding the current menubar gets hidden
507         // show the default menubar
508         if( mpMenu && mpMenu->mbMenuBar && AquaSalMenu::pCurrentMenuBar == mpMenu )
509             AquaSalMenu::setDefaultMenu();
510 
511         // #i90440# #i94443# work around the focus going back to some other window
512         // if a child gets hidden for a parent window
513         if( mpParent && mpParent->mbShown && [mpNSWindow isKeyWindow] )
514             [mpParent->mpNSWindow makeKeyAndOrderFront: NSApp];
515 
516         [SalFrameView unsetMouseFrame: this];
517         if( mpParent && [mpNSWindow parentWindow] == mpParent->mpNSWindow )
518             [mpParent->mpNSWindow removeChildWindow: mpNSWindow];
519 
520         [mpNSWindow orderOut: NSApp];
521     }
522 }
523 
524 // -----------------------------------------------------------------------
525 
Enable(sal_Bool)526 void AquaSalFrame::Enable( sal_Bool )
527 {
528 }
529 
530 // -----------------------------------------------------------------------
531 
SetMinClientSize(long nWidth,long nHeight)532 void AquaSalFrame::SetMinClientSize( long nWidth, long nHeight )
533 {
534     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetMinClientSize, this, nWidth, nHeight ) ) )
535         return;
536 
537     // #i113170# may not be the main thread if called from UNO API
538     SalData::ensureThreadAutoreleasePool();
539 
540     mnMinWidth = nWidth;
541     mnMinHeight = nHeight;
542 
543     if( mpNSWindow )
544     {
545         // Always add the decoration as the dimension concerns only
546         // the content rectangle
547         nWidth += maGeometry.nLeftDecoration + maGeometry.nRightDecoration;
548         nHeight += maGeometry.nTopDecoration + maGeometry.nBottomDecoration;
549 
550         const NSSize aSize = NSMakeSize( nWidth, nHeight);
551 
552         // Size of full window (content+structure) although we only
553         // have the client size in arguments
554         [mpNSWindow setMinSize: aSize];
555     }
556 }
557 
558 // -----------------------------------------------------------------------
559 
SetMaxClientSize(long nWidth,long nHeight)560 void AquaSalFrame::SetMaxClientSize( long nWidth, long nHeight )
561 {
562     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetMaxClientSize, this, nWidth, nHeight ) ) )
563         return;
564 
565     // #i113170# may not be the main thread if called from UNO API
566     SalData::ensureThreadAutoreleasePool();
567 
568     mnMaxWidth = nWidth;
569     mnMaxHeight = nHeight;
570 
571     if( mpNSWindow )
572     {
573         // Always add the decoration as the dimension concerns only
574         // the content rectangle
575         nWidth += maGeometry.nLeftDecoration + maGeometry.nRightDecoration;
576         nHeight += maGeometry.nTopDecoration + maGeometry.nBottomDecoration;
577 
578         // Carbon windows can't have a size greater than 32767x32767
579         if (nWidth>32767) nWidth=32767;
580         if (nHeight>32767) nHeight=32767;
581 
582         const NSSize aSize = NSMakeSize( nWidth, nHeight);
583 
584         // Size of full window (content+structure) although we only
585         // have the client size in arguments
586         [mpNSWindow setMaxSize: aSize];
587     }
588 }
589 
590 // -----------------------------------------------------------------------
591 
SetClientSize(long nWidth,long nHeight)592 void AquaSalFrame::SetClientSize( long nWidth, long nHeight )
593 {
594     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetClientSize, this, nWidth, nHeight ) ) )
595         return;
596 
597     // #i113170# may not be the main thread if called from UNO API
598     SalData::ensureThreadAutoreleasePool();
599 
600     if( mpNSWindow )
601     {
602         const NSSize aSize = NSMakeSize( nWidth, nHeight);
603 
604         [mpNSWindow setContentSize: aSize];
605         UpdateFrameGeometry();
606         if( mbShown )
607             // trigger filling our backbuffer
608             SendPaintEvent();
609     }
610 }
611 
612 // -----------------------------------------------------------------------
613 
GetClientSize(long & rWidth,long & rHeight)614 void AquaSalFrame::GetClientSize( long& rWidth, long& rHeight )
615 {
616     if( mbShown || mbInitShow )
617     {
618         rWidth  = maGeometry.nWidth;
619         rHeight = maGeometry.nHeight;
620     }
621     else
622     {
623         rWidth  = 0;
624         rHeight = 0;
625     }
626 }
627 
628 // -----------------------------------------------------------------------
629 
SetWindowState(const SalFrameState * pState)630 void AquaSalFrame::SetWindowState( const SalFrameState* pState )
631 {
632     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetWindowState, this, pState ) ) )
633         return;
634 
635     // #i113170# may not be the main thread if called from UNO API
636     SalData::ensureThreadAutoreleasePool();
637 
638     if ( mpNSWindow )
639     {
640     // set normal state
641     NSRect aStateRect = [mpNSWindow frame];
642     aStateRect = [NSWindow contentRectForFrameRect: aStateRect styleMask: mnStyleMask];
643     CocoaToVCL( aStateRect );
644     if( pState->mnMask & SAL_FRAMESTATE_MASK_X )
645         aStateRect.origin.x = float(pState->mnX);
646     if( pState->mnMask & SAL_FRAMESTATE_MASK_Y )
647         aStateRect.origin.y = float(pState->mnY);
648     if( pState->mnMask & SAL_FRAMESTATE_MASK_WIDTH )
649         aStateRect.size.width = float(pState->mnWidth);
650     if( pState->mnMask & SAL_FRAMESTATE_MASK_HEIGHT )
651         aStateRect.size.height = float(pState->mnHeight);
652     VCLToCocoa( aStateRect );
653     aStateRect = [NSWindow frameRectForContentRect: aStateRect styleMask: mnStyleMask];
654 
655     [mpNSWindow setFrame: aStateRect display: NO];
656     if( pState->mnState == SAL_FRAMESTATE_MINIMIZED )
657         [mpNSWindow miniaturize: NSApp];
658     else if( [mpNSWindow isMiniaturized] )
659         [mpNSWindow deminiaturize: NSApp];
660 
661 
662     /* ZOOMED is not really maximized (actually it toggles between a user set size and
663        the program specified one), but comes closest since the default behavior is
664        "maximized" if the user did not intervene
665     */
666     if( pState->mnState == SAL_FRAMESTATE_MAXIMIZED )
667     {
668         if(! [mpNSWindow isZoomed])
669             [mpNSWindow zoom: NSApp];
670     }
671     else
672     {
673         if( [mpNSWindow isZoomed] )
674             [mpNSWindow zoom: NSApp];
675     }
676     }
677 
678     // get new geometry
679     UpdateFrameGeometry();
680 
681     sal_uInt16 nEvent = 0;
682     if( pState->mnMask & (SAL_FRAMESTATE_MASK_X | SAL_FRAMESTATE_MASK_X) )
683     {
684         mbPositioned = true;
685         nEvent = SALEVENT_MOVE;
686     }
687 
688     if( pState->mnMask & (SAL_FRAMESTATE_MASK_WIDTH | SAL_FRAMESTATE_MASK_HEIGHT) )
689     {
690         mbSized = true;
691         nEvent = (nEvent == SALEVENT_MOVE) ? SALEVENT_MOVERESIZE : SALEVENT_RESIZE;
692     }
693     // send event that we were moved/sized
694     if( nEvent )
695         CallCallback( nEvent, NULL );
696 
697     if( mbShown && mpNSWindow )
698     {
699         // trigger filling our backbuffer
700         SendPaintEvent();
701 
702         // tell the system the views need to be updated
703         [mpNSWindow display];
704     }
705 }
706 
707 // -----------------------------------------------------------------------
708 
GetWindowState(SalFrameState * pState)709 sal_Bool AquaSalFrame::GetWindowState( SalFrameState* pState )
710 {
711     AquaSalInstance* pInst = GetSalData()->mpFirstInstance;
712     if( ! pInst->isNSAppThread() )
713         return pInst->callOnMainThread< sal_Bool >( boost::bind( &AquaSalFrame::GetWindowState, this, pState ) );
714 
715     if ( !mpNSWindow )
716         return FALSE;
717 
718     // #i113170# may not be the main thread if called from UNO API
719     SalData::ensureThreadAutoreleasePool();
720 
721     pState->mnMask = SAL_FRAMESTATE_MASK_X                 |
722                      SAL_FRAMESTATE_MASK_Y                 |
723                      SAL_FRAMESTATE_MASK_WIDTH             |
724                      SAL_FRAMESTATE_MASK_HEIGHT            |
725                      #if 0
726                      SAL_FRAMESTATE_MASK_MAXIMIZED_X       |
727                      SAL_FRAMESTATE_MASK_MAXIMIZED_Y       |
728                      SAL_FRAMESTATE_MASK_MAXIMIZED_WIDTH   |
729                      SAL_FRAMESTATE_MASK_MAXIMIZED_HEIGHT  |
730                      #endif
731                      SAL_FRAMESTATE_MASK_STATE;
732 
733     NSRect aStateRect = [mpNSWindow frame];
734     aStateRect = [NSWindow contentRectForFrameRect: aStateRect styleMask: mnStyleMask];
735     CocoaToVCL( aStateRect );
736     pState->mnX         = long(aStateRect.origin.x);
737     pState->mnY         = long(aStateRect.origin.y);
738     pState->mnWidth     = long(aStateRect.size.width);
739     pState->mnHeight    = long(aStateRect.size.height);
740 
741     if( [mpNSWindow isMiniaturized] )
742         pState->mnState = SAL_FRAMESTATE_MINIMIZED;
743     else if( ! [mpNSWindow isZoomed] )
744         pState->mnState = SAL_FRAMESTATE_NORMAL;
745     else
746         pState->mnState = SAL_FRAMESTATE_MAXIMIZED;
747 
748     return TRUE;
749 }
750 
751 // -----------------------------------------------------------------------
752 
SetScreenNumber(unsigned int nScreen)753 void AquaSalFrame::SetScreenNumber(unsigned int nScreen)
754 {
755     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetScreenNumber, this, nScreen ) ) )
756         return;
757 
758     if ( !mpNSWindow )
759         return;
760 
761     // #i113170# may not be the main thread if called from UNO API
762     SalData::ensureThreadAutoreleasePool();
763 
764     NSArray* pScreens = [NSScreen screens];
765     Rectangle aRet;
766     NSScreen* pScreen = nil;
767     if( pScreens && nScreen < [pScreens count] )
768     {
769         // get new screen frame
770         pScreen = [pScreens objectAtIndex: nScreen];
771         NSRect aNewScreen = [pScreen frame];
772 
773         // get current screen frame
774         pScreen = [mpNSWindow screen];
775         if( pScreen )
776         {
777             NSRect aCurScreen = [pScreen frame];
778             if( aCurScreen.origin.x != aNewScreen.origin.x ||
779                 aCurScreen.origin.y != aNewScreen.origin.y )
780             {
781                 NSRect aFrameRect = [mpNSWindow frame];
782                 aFrameRect.origin.x += aNewScreen.origin.x - aCurScreen.origin.x;
783                 aFrameRect.origin.y += aNewScreen.origin.y - aCurScreen.origin.y;
784                 [mpNSWindow setFrame: aFrameRect display: NO];
785                 UpdateFrameGeometry();
786             }
787         }
788     }
789 }
790 
791 // -----------------------------------------------------------------------
792 
ShowFullScreen(sal_Bool bFullScreen,sal_Int32 nDisplay)793 void AquaSalFrame::ShowFullScreen( sal_Bool bFullScreen, sal_Int32 nDisplay )
794 {
795     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::ShowFullScreen, this, bFullScreen, nDisplay ) ) )
796         return;
797 
798     if ( !mpNSWindow )
799         return;
800 
801     // #i113170# may not be the main thread if called from UNO API
802     SalData::ensureThreadAutoreleasePool();
803 
804     if( mbFullScreen == bFullScreen )
805         return;
806 
807     mbFullScreen = bFullScreen;
808     if( bFullScreen )
809     {
810         // hide the dock and the menubar if we are on the menu screen
811         // which is always on index 0 according to documentation
812         bool bHideMenu = (nDisplay == 0);
813 
814         NSRect aNewContentRect = NSZeroRect;
815         // get correct screen
816         NSScreen* pScreen = nil;
817         NSArray* pScreens = [NSScreen screens];
818         if( pScreens )
819         {
820             if( nDisplay >= 0 && (unsigned int)nDisplay < [pScreens count] )
821                 pScreen = [pScreens objectAtIndex: nDisplay];
822             else
823             {
824                 // this means span all screens
825                 bHideMenu = true;
826                 NSEnumerator* pEnum = [pScreens objectEnumerator];
827                 while( (pScreen = [pEnum nextObject]) != nil )
828                 {
829                     NSRect aScreenRect = [pScreen frame];
830                     if( aScreenRect.origin.x < aNewContentRect.origin.x )
831                     {
832                         aNewContentRect.size.width += aNewContentRect.origin.x - aScreenRect.origin.x;
833                         aNewContentRect.origin.x = aScreenRect.origin.x;
834                     }
835                     if( aScreenRect.origin.y < aNewContentRect.origin.y )
836                     {
837                         aNewContentRect.size.height += aNewContentRect.origin.y - aScreenRect.origin.y;
838                         aNewContentRect.origin.y = aScreenRect.origin.y;
839                     }
840                     if( aScreenRect.origin.x + aScreenRect.size.width > aNewContentRect.origin.x + aNewContentRect.size.width )
841                         aNewContentRect.size.width = aScreenRect.origin.x + aScreenRect.size.width - aNewContentRect.origin.x;
842                     if( aScreenRect.origin.y + aScreenRect.size.height > aNewContentRect.origin.y + aNewContentRect.size.height )
843                         aNewContentRect.size.height = aScreenRect.origin.y + aScreenRect.size.height - aNewContentRect.origin.y;
844                 }
845             }
846         }
847         if( aNewContentRect.size.width == 0 && aNewContentRect.size.height == 0 )
848         {
849             if( pScreen == nil )
850                 pScreen = [mpNSWindow screen];
851             if( pScreen == nil )
852                 pScreen = [NSScreen mainScreen];
853 
854             aNewContentRect = [pScreen frame];
855         }
856 
857         if( bHideMenu )
858             [NSMenu setMenuBarVisible:NO];
859 
860         maFullScreenRect = [mpNSWindow frame];
861         {
862             [mpNSWindow setFrame: [NSWindow frameRectForContentRect: aNewContentRect styleMask: mnStyleMask] display: mbShown ? YES : NO];
863         }
864 
865         UpdateFrameGeometry();
866 
867         if( mbShown )
868             CallCallback( SALEVENT_MOVERESIZE, NULL );
869     }
870     else
871     {
872         {
873             [mpNSWindow setFrame: maFullScreenRect display: mbShown ? YES : NO];
874         }
875         UpdateFrameGeometry();
876 
877         if( mbShown )
878             CallCallback( SALEVENT_MOVERESIZE, NULL );
879 
880         // show the dock and the menubar
881         [NSMenu setMenuBarVisible:YES];
882     }
883     if( mbShown )
884         // trigger filling our backbuffer
885         SendPaintEvent();
886 }
887 
888 // -----------------------------------------------------------------------
889 
890 class PreventSleepTimer : public AutoTimer
891 {
892 public:
PreventSleepTimer()893     PreventSleepTimer()
894     {
895         SetTimeout( 30000 );
896         Start();
897     }
898 
~PreventSleepTimer()899     virtual ~PreventSleepTimer()
900     {
901     }
902 
Timeout()903     virtual void Timeout()
904     {
905         UpdateSystemActivity(OverallAct);
906     }
907 };
908 
StartPresentation(sal_Bool bStart)909 void AquaSalFrame::StartPresentation( sal_Bool bStart )
910 {
911     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::StartPresentation, this, bStart ) ) )
912         return;
913 
914     if ( !mpNSWindow )
915         return;
916 
917     // #i113170# may not be the main thread if called from UNO API
918     SalData::ensureThreadAutoreleasePool();
919 
920     if( bStart )
921     {
922         GetSalData()->maPresentationFrames.push_back( this );
923         mpActivityTimer.reset( new PreventSleepTimer() );
924         [mpNSWindow setLevel: NSPopUpMenuWindowLevel];
925         if( mbShown )
926             [mpNSWindow makeMainWindow];
927     }
928     else
929     {
930         GetSalData()->maPresentationFrames.remove( this );
931         mpActivityTimer.reset();
932         [mpNSWindow setLevel: NSNormalWindowLevel];
933     }
934 }
935 
936 // -----------------------------------------------------------------------
937 
SetAlwaysOnTop(sal_Bool)938 void AquaSalFrame::SetAlwaysOnTop( sal_Bool )
939 {
940 }
941 
942 // -----------------------------------------------------------------------
943 
ToTop(sal_uInt16 nFlags)944 void AquaSalFrame::ToTop(sal_uInt16 nFlags)
945 {
946     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::ToTop, this, nFlags ) ) )
947         return;
948 
949     if ( !mpNSWindow )
950         return;
951 
952     // #i113170# may not be the main thread if called from UNO API
953     SalData::ensureThreadAutoreleasePool();
954 
955     // #i113170# may not be the main thread if called from UNO API
956     SalData::ensureThreadAutoreleasePool();
957 
958     if( ! (nFlags & SAL_FRAME_TOTOP_RESTOREWHENMIN) )
959     {
960         if( ! [mpNSWindow isVisible] || [mpNSWindow isMiniaturized] )
961             return;
962     }
963     if( nFlags & SAL_FRAME_TOTOP_GRABFOCUS )
964         [mpNSWindow makeKeyAndOrderFront: NSApp];
965     else
966         [mpNSWindow orderFront: NSApp];
967 }
968 
969 // -----------------------------------------------------------------------
970 
getCurrentCursor() const971 NSCursor* AquaSalFrame::getCurrentCursor() const
972 {
973     NSCursor* pCursor = nil;
974     switch( mePointerStyle )
975     {
976     case POINTER_TEXT:      pCursor = [NSCursor IBeamCursor];           break;
977     case POINTER_CROSS:     pCursor = [NSCursor crosshairCursor];       break;
978     case POINTER_HAND:
979     case POINTER_MOVE:      pCursor = [NSCursor openHandCursor];        break;
980     case POINTER_NSIZE:     pCursor = [NSCursor resizeUpCursor];        break;
981     case POINTER_SSIZE:     pCursor = [NSCursor resizeDownCursor];      break;
982     case POINTER_ESIZE:     pCursor = [NSCursor resizeRightCursor];      break;
983     case POINTER_WSIZE:     pCursor = [NSCursor resizeLeftCursor];     break;
984     case POINTER_ARROW:     pCursor = [NSCursor arrowCursor];           break;
985     case POINTER_VSPLIT:
986     case POINTER_VSIZEBAR:
987     case POINTER_WINDOW_NSIZE:
988     case POINTER_WINDOW_SSIZE:
989                             pCursor = [NSCursor resizeUpDownCursor];    break;
990     case POINTER_HSPLIT:
991     case POINTER_HSIZEBAR:
992     case POINTER_WINDOW_ESIZE:
993     case POINTER_WINDOW_WSIZE:
994                             pCursor = [NSCursor resizeLeftRightCursor]; break;
995     case POINTER_REFHAND:   pCursor = [NSCursor pointingHandCursor];    break;
996 
997     default:
998         pCursor = GetSalData()->getCursor( mePointerStyle );
999         if( pCursor == nil )
1000         {
1001             DBG_ERROR( "unmapped cursor" );
1002             pCursor = [NSCursor arrowCursor];
1003         }
1004         break;
1005     }
1006     return pCursor;
1007 }
1008 
SetPointer(PointerStyle ePointerStyle)1009 void AquaSalFrame::SetPointer( PointerStyle ePointerStyle )
1010 {
1011     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetPointer, this, ePointerStyle ) ) )
1012         return;
1013 
1014     if ( !mpNSWindow )
1015         return;
1016 
1017     // #i113170# may not be the main thread if called from UNO API
1018     SalData::ensureThreadAutoreleasePool();
1019 
1020     if( ePointerStyle >= POINTER_COUNT || ePointerStyle == mePointerStyle )
1021         return;
1022     mePointerStyle = ePointerStyle;
1023 
1024     [mpNSWindow invalidateCursorRectsForView: mpNSView];
1025 }
1026 
1027 // -----------------------------------------------------------------------
1028 
SetPointerPos(long nX,long nY)1029 void AquaSalFrame::SetPointerPos( long nX, long nY )
1030 {
1031     // FIXME: use Cocoa functions
1032 
1033     // FIXME: multiscreen support
1034     const CGPoint aPoint = CGPointMake( nX + maGeometry.nX, nY + maGeometry.nY);
1035     CGDirectDisplayID mainDisplayID = CGMainDisplayID();
1036     CGDisplayMoveCursorToPoint( mainDisplayID, aPoint );
1037 }
1038 
1039 // -----------------------------------------------------------------------
1040 
Flush(void)1041 void AquaSalFrame::Flush( void )
1042 {
1043     if( !(mbGraphics && mpGraphics && mpNSView && mbShown) )
1044         return;
1045 
1046     // #i113170# may not be the main thread if called from UNO API
1047     SalData::ensureThreadAutoreleasePool();
1048 
1049 
1050     [mpNSView setNeedsDisplay: YES];
1051 
1052     // outside of the application's event loop (e.g. IntroWindow)
1053     // nothing would trigger paint event handling
1054     // => fall back to synchronous painting
1055     if( ImplGetSVData()->maAppData.mnDispatchLevel <= 0 )
1056     {
1057         [mpNSView display];
1058     }
1059 }
1060 
1061 // -----------------------------------------------------------------------
1062 
Flush(const Rectangle & rRect)1063 void AquaSalFrame::Flush( const Rectangle& rRect )
1064 {
1065     if( !(mbGraphics && mpGraphics && mpNSView && mbShown) )
1066         return;
1067 
1068     // #i113170# may not be the main thread if called from UNO API
1069     SalData::ensureThreadAutoreleasePool();
1070 
1071     NSRect aNSRect = NSMakeRect( rRect.Left(), rRect.Top(), rRect.GetWidth(), rRect.GetHeight());
1072     VCLToCocoa( aNSRect, false );
1073     [mpNSView setNeedsDisplayInRect: aNSRect];
1074 
1075     // outside of the application's event loop (e.g. IntroWindow)
1076     // nothing would trigger paint event handling
1077     // => fall back to synchronous painting
1078     if( ImplGetSVData()->maAppData.mnDispatchLevel <= 0 )
1079     {
1080         [mpNSView display];
1081     }
1082 }
1083 
1084 // -----------------------------------------------------------------------
1085 
Sync()1086 void AquaSalFrame::Sync()
1087 {
1088     if( mbGraphics && mpGraphics && mpNSView && mbShown )
1089     {
1090         // #i113170# may not be the main thread if called from UNO API
1091         SalData::ensureThreadAutoreleasePool();
1092 
1093         [mpNSView setNeedsDisplay: YES];
1094         [mpNSView display];
1095     }
1096 }
1097 
1098 // -----------------------------------------------------------------------
1099 
SetInputContext(SalInputContext * pContext)1100 void AquaSalFrame::SetInputContext( SalInputContext* pContext )
1101 {
1102     if (!pContext)
1103     {
1104         mnICOptions = 0;
1105         return;
1106     }
1107 
1108     mnICOptions = pContext->mnOptions;
1109 
1110     if(!(pContext->mnOptions & SAL_INPUTCONTEXT_TEXT))
1111         return;
1112 }
1113 
1114 // -----------------------------------------------------------------------
1115 
EndExtTextInput(sal_uInt16)1116 void AquaSalFrame::EndExtTextInput( sal_uInt16 )
1117 {
1118 }
1119 
1120 // -----------------------------------------------------------------------
1121 
GetKeyName(sal_uInt16 nKeyCode)1122 XubString AquaSalFrame::GetKeyName( sal_uInt16 nKeyCode )
1123 {
1124     static std::map< sal_uInt16, rtl::OUString > aKeyMap;
1125     if( aKeyMap.empty() )
1126     {
1127         sal_uInt16 i;
1128         for( i = KEY_A; i <= KEY_Z; i++ )
1129             aKeyMap[ i ] = rtl::OUString( sal_Unicode( 'A' + (i - KEY_A) ) );
1130         for( i = KEY_0; i <= KEY_9; i++ )
1131             aKeyMap[ i ] = rtl::OUString( sal_Unicode( '0' + (i - KEY_0) ) );
1132         for( i = KEY_F1; i <= KEY_F26; i++ )
1133         {
1134             rtl::OUStringBuffer aKey( 3 );
1135             aKey.append( sal_Unicode( 'F' ) );
1136             aKey.append( sal_Int32( i - KEY_F1 + 1 ) );
1137             aKeyMap[ i ] = aKey.makeStringAndClear();
1138         }
1139 
1140         aKeyMap[ KEY_DOWN ]     = rtl::OUString( sal_Unicode( 0x21e3 ) );
1141         aKeyMap[ KEY_UP ]       = rtl::OUString( sal_Unicode( 0x21e1 ) );
1142         aKeyMap[ KEY_LEFT ]     = rtl::OUString( sal_Unicode( 0x21e0 ) );
1143         aKeyMap[ KEY_RIGHT ]    = rtl::OUString( sal_Unicode( 0x21e2 ) );
1144         aKeyMap[ KEY_HOME ]     = rtl::OUString( sal_Unicode( 0x2196 ) );
1145         aKeyMap[ KEY_END ]      = rtl::OUString( sal_Unicode( 0x2198 ) );
1146         aKeyMap[ KEY_PAGEUP ]   = rtl::OUString( sal_Unicode( 0x21de ) );
1147         aKeyMap[ KEY_PAGEDOWN ] = rtl::OUString( sal_Unicode( 0x21df ) );
1148         aKeyMap[ KEY_RETURN ]   = rtl::OUString( sal_Unicode( 0x21a9 ) );
1149         aKeyMap[ KEY_ESCAPE ]   = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "esc" ) );
1150         aKeyMap[ KEY_TAB ]      = rtl::OUString( sal_Unicode( 0x21e5 ) );
1151         aKeyMap[ KEY_BACKSPACE ]= rtl::OUString( sal_Unicode( 0x232b ) );
1152         aKeyMap[ KEY_SPACE ]    = rtl::OUString( sal_Unicode( 0x2423 ) );
1153         aKeyMap[ KEY_DELETE ]   = rtl::OUString( sal_Unicode( 0x2326 ) );
1154         aKeyMap[ KEY_ADD ]      = rtl::OUString( sal_Unicode( '+' ) );
1155         aKeyMap[ KEY_SUBTRACT ] = rtl::OUString( sal_Unicode( '-' ) );
1156         aKeyMap[ KEY_DIVIDE ]   = rtl::OUString( sal_Unicode( '/' ) );
1157         aKeyMap[ KEY_MULTIPLY ] = rtl::OUString( sal_Unicode( '*' ) );
1158         aKeyMap[ KEY_POINT ]    = rtl::OUString( sal_Unicode( '.' ) );
1159         aKeyMap[ KEY_COMMA ]    = rtl::OUString( sal_Unicode( ',' ) );
1160         aKeyMap[ KEY_LESS ]     = rtl::OUString( sal_Unicode( '<' ) );
1161         aKeyMap[ KEY_GREATER ]  = rtl::OUString( sal_Unicode( '>' ) );
1162         aKeyMap[ KEY_EQUAL ]    = rtl::OUString( sal_Unicode( '=' ) );
1163         aKeyMap[ KEY_OPEN ]     = rtl::OUString( sal_Unicode( 0x23cf ) );
1164 
1165         /* yet unmapped KEYCODES:
1166         aKeyMap[ KEY_INSERT ]   = rtl::OUString( sal_Unicode( ) );
1167         aKeyMap[ KEY_CUT ]      = rtl::OUString( sal_Unicode( ) );
1168         aKeyMap[ KEY_COPY ]     = rtl::OUString( sal_Unicode( ) );
1169         aKeyMap[ KEY_PASTE ]    = rtl::OUString( sal_Unicode( ) );
1170         aKeyMap[ KEY_UNDO ]     = rtl::OUString( sal_Unicode( ) );
1171         aKeyMap[ KEY_REPEAT ]   = rtl::OUString( sal_Unicode( ) );
1172         aKeyMap[ KEY_FIND ]     = rtl::OUString( sal_Unicode( ) );
1173         aKeyMap[ KEY_PROPERTIES ]     = rtl::OUString( sal_Unicode( ) );
1174         aKeyMap[ KEY_FRONT ]    = rtl::OUString( sal_Unicode( ) );
1175         aKeyMap[ KEY_CONTEXTMENU ]    = rtl::OUString( sal_Unicode( ) );
1176         aKeyMap[ KEY_MENU ]     = rtl::OUString( sal_Unicode( ) );
1177         aKeyMap[ KEY_HELP ]     = rtl::OUString( sal_Unicode( ) );
1178         aKeyMap[ KEY_HANGUL_HANJA ]   = rtl::OUString( sal_Unicode( ) );
1179         aKeyMap[ KEY_DECIMAL ]  = rtl::OUString( sal_Unicode( ) );
1180         aKeyMap[ KEY_TILDE ]    = rtl::OUString( sal_Unicode( ) );
1181         aKeyMap[ KEY_QUOTELEFT ]= rtl::OUString( sal_Unicode( ) );
1182         */
1183 
1184     }
1185 
1186     rtl::OUStringBuffer aResult( 16 );
1187 
1188     sal_uInt16 nUnmodifiedCode = (nKeyCode & KEY_CODE);
1189     std::map< sal_uInt16, rtl::OUString >::const_iterator it = aKeyMap.find( nUnmodifiedCode );
1190     if( it != aKeyMap.end() )
1191     {
1192         if( (nKeyCode & KEY_SHIFT) != 0 )
1193             aResult.append( sal_Unicode( 0x21e7 ) );
1194         if( (nKeyCode & KEY_MOD1) != 0 )
1195             aResult.append( sal_Unicode( 0x2318 ) );
1196         // we do not really handle Alt (see below)
1197         // we map it to MOD3, whichis actually Command
1198         if( (nKeyCode & (KEY_MOD2|KEY_MOD3)) != 0 )
1199             aResult.append( sal_Unicode( 0x2303 ) );
1200 
1201         aResult.append( it->second );
1202     }
1203 
1204     return aResult.makeStringAndClear();
1205 }
1206 
1207 // -----------------------------------------------------------------------
1208 
GetSymbolKeyName(const XubString &,sal_uInt16 nKeyCode)1209 XubString AquaSalFrame::GetSymbolKeyName( const XubString&, sal_uInt16 nKeyCode )
1210 {
1211     return GetKeyName( nKeyCode );
1212 }
1213 
1214 // -----------------------------------------------------------------------
1215 
getAppleScrollBarVariant(void)1216 static void getAppleScrollBarVariant(void)
1217 {
1218     bool bIsScrollbarDoubleMax = true; // default is DoubleMax
1219 
1220     CFStringRef AppleScrollBarType = CFSTR("AppleScrollBarVariant");
1221     if( AppleScrollBarType )
1222     {
1223         CFStringRef ScrollBarVariant = ((CFStringRef)CFPreferencesCopyAppValue( AppleScrollBarType, kCFPreferencesCurrentApplication ));
1224         if( ScrollBarVariant )
1225         {
1226             if( CFGetTypeID( ScrollBarVariant ) == CFStringGetTypeID() )
1227             {
1228                 // TODO: check for the less important variants "DoubleMin" and "DoubleBoth" too
1229                 CFStringRef DoubleMax = CFSTR("DoubleMax");
1230                 if (DoubleMax)
1231                 {
1232                     if ( !CFStringCompare(ScrollBarVariant, DoubleMax, kCFCompareCaseInsensitive) )
1233                         bIsScrollbarDoubleMax = true;
1234                     else
1235                         bIsScrollbarDoubleMax = false;
1236                     CFRelease(DoubleMax);
1237                 }
1238             }
1239             CFRelease( ScrollBarVariant );
1240         }
1241         CFRelease(AppleScrollBarType);
1242     }
1243 
1244     GetSalData()->mbIsScrollbarDoubleMax = bIsScrollbarDoubleMax;
1245 
1246     CFStringRef jumpScroll = CFSTR("AppleScrollerPagingBehavior");
1247     if( jumpScroll )
1248     {
1249         CFBooleanRef jumpStr = ((CFBooleanRef)CFPreferencesCopyAppValue( jumpScroll, kCFPreferencesCurrentApplication ));
1250         if( jumpStr )
1251         {
1252             if( CFGetTypeID( jumpStr ) == CFBooleanGetTypeID() )
1253                 ImplGetSVData()->maNWFData.mbScrollbarJumpPage = (jumpStr == kCFBooleanTrue);
1254             CFRelease( jumpStr );
1255         }
1256         CFRelease( jumpScroll );
1257     }
1258 }
1259 
getColor(NSColor * pSysColor,const Color & rDefault,NSWindow * pWin)1260 static Color getColor( NSColor* pSysColor, const Color& rDefault, NSWindow* pWin )
1261 {
1262     Color aRet( rDefault );
1263     if( pSysColor )
1264     {
1265         // transform to RGB
1266         NSColor* pRBGColor = [pSysColor colorUsingColorSpaceName: NSDeviceRGBColorSpace device: [pWin deviceDescription]];
1267         if( pRBGColor )
1268         {
1269             CGFloat r = 0, g = 0, b = 0, a = 0;
1270             [pRBGColor getRed: &r green: &g blue: &b alpha: &a];
1271             aRet = Color( int(r*255.999), int(g*255.999), int(b*255.999) );
1272             /*
1273             do not release here; leads to duplicate free in yield
1274             it seems the converted color comes out autoreleased, although this
1275             is not documented
1276             [pRBGColor release];
1277             */
1278         }
1279     }
1280     return aRet;
1281 }
1282 
getFont(NSFont * pFont,long nDPIY,const Font & rDefault)1283 static Font getFont( NSFont* pFont, long nDPIY, const Font& rDefault )
1284 {
1285     Font aResult( rDefault );
1286     if( pFont )
1287     {
1288         aResult.SetName( GetOUString( [pFont familyName] ) );
1289         aResult.SetHeight( static_cast<int>(([pFont pointSize] * 72.0 / (float)nDPIY)+0.5) );
1290         aResult.SetItalic( ([pFont italicAngle] != 0.0) ? ITALIC_NORMAL : ITALIC_NONE );
1291         // FIMXE: bold ?
1292     }
1293 
1294     return aResult;
1295 }
1296 
getResolution(sal_Int32 & o_rDPIX,sal_Int32 & o_rDPIY)1297 void AquaSalFrame::getResolution( sal_Int32& o_rDPIX, sal_Int32& o_rDPIY )
1298 {
1299     if( ! mpGraphics )
1300     {
1301         GetGraphics();
1302         ReleaseGraphics( mpGraphics );
1303     }
1304     mpGraphics->GetResolution( o_rDPIX, o_rDPIY );
1305 }
1306 
1307 // on OSX-Aqua the style settings are independent of the frame, so it does
1308 // not really belong here. Since the connection to the Appearance_Manager
1309 // is currently done in salnativewidgets.cxx this would be a good place.
1310 // On the other hand VCL's platform independent code currently only asks
1311 // SalFrames for system settings anyway, so moving the code somewhere else
1312 // doesn't make the anything cleaner for now
UpdateSettings(AllSettings & rSettings)1313 void AquaSalFrame::UpdateSettings( AllSettings& rSettings )
1314 {
1315     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::UpdateSettings, this, boost::ref( rSettings ) ) ) )
1316         return;
1317 
1318     if ( !mpNSWindow )
1319         return;
1320 
1321     // #i113170# may not be the main thread if called from UNO API
1322     SalData::ensureThreadAutoreleasePool();
1323 
1324     [mpNSView lockFocus];
1325 
1326     StyleSettings aStyleSettings = rSettings.GetStyleSettings();
1327 
1328     // Background Color
1329     Color aBackgroundColor = Color( 0xEC, 0xEC, 0xEC );
1330     aStyleSettings.Set3DColors( aBackgroundColor );
1331     aStyleSettings.SetFaceColor( aBackgroundColor );
1332     Color aInactiveTabColor( aBackgroundColor );
1333     aInactiveTabColor.DecreaseLuminance( 32 );
1334     aStyleSettings.SetInactiveTabColor( aInactiveTabColor );
1335 
1336     aStyleSettings.SetDialogColor( aBackgroundColor );
1337     aStyleSettings.SetLightBorderColor( aBackgroundColor );
1338     Color aShadowColor( aStyleSettings.GetShadowColor() );
1339     aStyleSettings.SetDarkShadowColor( aShadowColor );
1340     aShadowColor.IncreaseLuminance( 32 );
1341     aStyleSettings.SetShadowColor( aShadowColor );
1342 
1343     // get the system font settings
1344     Font aAppFont = aStyleSettings.GetAppFont();
1345     sal_Int32 nDPIX = 72, nDPIY = 72;
1346     getResolution( nDPIX, nDPIY );
1347     aAppFont = getFont( [NSFont systemFontOfSize: 0], nDPIY, aAppFont );
1348 
1349     // TODO: better mapping of aqua<->ooo font settings
1350     aStyleSettings.SetAppFont( aAppFont );
1351     aStyleSettings.SetHelpFont( aAppFont );
1352     aStyleSettings.SetPushButtonFont( aAppFont );
1353 
1354     Font aTitleFont( getFont( [NSFont titleBarFontOfSize: 0], nDPIY, aAppFont ) );
1355     aStyleSettings.SetTitleFont( aTitleFont );
1356     aStyleSettings.SetFloatTitleFont( aTitleFont );
1357 
1358     Font aMenuFont( getFont( [NSFont menuFontOfSize: 0], nDPIY, aAppFont ) );
1359     aStyleSettings.SetMenuFont( aMenuFont );
1360 
1361     aStyleSettings.SetToolFont( aAppFont );
1362 
1363     Font aLabelFont( getFont( [NSFont labelFontOfSize: 0], nDPIY, aAppFont ) );
1364     aStyleSettings.SetLabelFont( aLabelFont );
1365     aStyleSettings.SetInfoFont( aLabelFont );
1366     aStyleSettings.SetRadioCheckFont( aLabelFont );
1367     aStyleSettings.SetFieldFont( aLabelFont );
1368     aStyleSettings.SetGroupFont( aLabelFont );
1369     aStyleSettings.SetIconFont( aLabelFont );
1370 
1371     Color aHighlightColor( getColor( [NSColor selectedTextBackgroundColor],
1372                                       aStyleSettings.GetHighlightColor(), mpNSWindow ) );
1373     aStyleSettings.SetHighlightColor( aHighlightColor );
1374     Color aHighlightTextColor( getColor( [NSColor selectedTextColor],
1375                                          aStyleSettings.GetHighlightTextColor(), mpNSWindow ) );
1376     aStyleSettings.SetHighlightTextColor( aHighlightTextColor );
1377 
1378     Color aMenuHighlightColor( getColor( [NSColor selectedMenuItemColor],
1379                                          aStyleSettings.GetMenuHighlightColor(), mpNSWindow ) );
1380     aStyleSettings.SetMenuHighlightColor( aMenuHighlightColor );
1381     Color aMenuHighlightTextColor( getColor( [NSColor selectedMenuItemTextColor],
1382                                              aStyleSettings.GetMenuHighlightTextColor(), mpNSWindow ) );
1383     aStyleSettings.SetMenuHighlightTextColor( aMenuHighlightTextColor );
1384 
1385     aStyleSettings.SetMenuColor( aBackgroundColor );
1386     Color aMenuTextColor( getColor( [NSColor textColor],
1387                                     aStyleSettings.GetMenuTextColor(), mpNSWindow ) );
1388     aStyleSettings.SetMenuTextColor( aMenuTextColor );
1389     aStyleSettings.SetMenuBarTextColor( aMenuTextColor );
1390 
1391     aStyleSettings.SetCursorBlinkTime( 500 );
1392 
1393     // no mnemonics on aqua
1394     aStyleSettings.SetOptions( aStyleSettings.GetOptions() | STYLE_OPTION_NOMNEMONICS );
1395 
1396     getAppleScrollBarVariant();
1397 
1398     // set scrollbar size
1399     aStyleSettings.SetScrollBarSize( static_cast<long int>([NSScroller scrollerWidth]) );
1400 
1401     // images in menus false for MacOSX
1402     aStyleSettings.SetUseImagesInMenus( false );
1403 
1404     rSettings.SetStyleSettings( aStyleSettings );
1405 
1406     [mpNSView unlockFocus];
1407 }
1408 
1409 // -----------------------------------------------------------------------
1410 
GetSystemData() const1411 const SystemEnvData* AquaSalFrame::GetSystemData() const
1412 {
1413     return &maSysData;
1414 }
1415 
1416 // -----------------------------------------------------------------------
1417 
Beep(SoundType eSoundType)1418 void AquaSalFrame::Beep( SoundType eSoundType )
1419 {
1420     switch( eSoundType )
1421     {
1422     case SOUND_DISABLE:
1423         // don't beep
1424         break;
1425     default:
1426         NSBeep();
1427         break;
1428     }
1429 }
1430 
1431 // -----------------------------------------------------------------------
1432 
SetPosSize(long nX,long nY,long nWidth,long nHeight,sal_uInt16 nFlags)1433 void AquaSalFrame::SetPosSize(long nX, long nY, long nWidth, long nHeight, sal_uInt16 nFlags)
1434 {
1435     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetPosSize, this, nX, nY, nWidth, nHeight, nFlags ) ) )
1436         return;
1437 
1438     if ( !mpNSWindow )
1439         return;
1440 
1441     // #i113170# may not be the main thread if called from UNO API
1442     SalData::ensureThreadAutoreleasePool();
1443 
1444     sal_uInt16 nEvent = 0;
1445 
1446     if( [mpNSWindow isMiniaturized] )
1447         [mpNSWindow deminiaturize: NSApp]; // expand the window
1448 
1449     if (nFlags & (SAL_FRAME_POSSIZE_X | SAL_FRAME_POSSIZE_Y))
1450     {
1451         mbPositioned = true;
1452         nEvent = SALEVENT_MOVE;
1453     }
1454 
1455     if (nFlags & (SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT))
1456     {
1457         mbSized = true;
1458         nEvent = (nEvent == SALEVENT_MOVE) ? SALEVENT_MOVERESIZE : SALEVENT_RESIZE;
1459     }
1460 
1461     NSRect aFrameRect = [mpNSWindow frame];
1462     NSRect aContentRect = [NSWindow contentRectForFrameRect: aFrameRect styleMask: mnStyleMask];
1463 
1464     // position is always relative to parent frame
1465     NSRect aParentContentRect;
1466 
1467     if( mpParent )
1468     {
1469         if( Application::GetSettings().GetLayoutRTL() )
1470         {
1471             if( (nFlags & SAL_FRAME_POSSIZE_WIDTH) != 0 )
1472                 nX = mpParent->maGeometry.nWidth - nWidth-1 - nX;
1473             else
1474                 nX = mpParent->maGeometry.nWidth - static_cast<long int>( aContentRect.size.width-1) - nX;
1475         }
1476         NSRect aParentFrameRect = [mpParent->mpNSWindow frame];
1477         aParentContentRect = [NSWindow contentRectForFrameRect: aParentFrameRect styleMask: mpParent->mnStyleMask];
1478     }
1479     else
1480         aParentContentRect = maScreenRect; // use screen if no parent
1481 
1482     CocoaToVCL( aContentRect );
1483     CocoaToVCL( aParentContentRect );
1484 
1485     bool bPaint = false;
1486     if( (nFlags & (SAL_FRAME_POSSIZE_WIDTH | SAL_FRAME_POSSIZE_HEIGHT)) != 0 )
1487     {
1488         if( nWidth != aContentRect.size.width || nHeight != aContentRect.size.height )
1489             bPaint = true;
1490     }
1491 
1492     // use old window pos if no new pos requested
1493     if( (nFlags & SAL_FRAME_POSSIZE_X) != 0 )
1494         aContentRect.origin.x = nX + aParentContentRect.origin.x;
1495     if( (nFlags & SAL_FRAME_POSSIZE_Y) != 0)
1496         aContentRect.origin.y = nY + aParentContentRect.origin.y;
1497 
1498     // use old size if no new size requested
1499     if( (nFlags & SAL_FRAME_POSSIZE_WIDTH) != 0 )
1500         aContentRect.size.width = nWidth;
1501     if( (nFlags & SAL_FRAME_POSSIZE_HEIGHT) != 0)
1502         aContentRect.size.height = nHeight;
1503 
1504     VCLToCocoa( aContentRect );
1505 
1506     // do not display yet, we need to update our backbuffer
1507     {
1508         [mpNSWindow setFrame: [NSWindow frameRectForContentRect: aContentRect styleMask: mnStyleMask] display: NO];
1509     }
1510 
1511     UpdateFrameGeometry();
1512 
1513     if (nEvent)
1514         CallCallback(nEvent, NULL);
1515 
1516     if( mbShown && bPaint )
1517     {
1518         // trigger filling our backbuffer
1519         SendPaintEvent();
1520 
1521         // now inform the system that the views need to be drawn
1522         [mpNSWindow display];
1523     }
1524 }
1525 
GetWorkArea(Rectangle & rRect)1526 void AquaSalFrame::GetWorkArea( Rectangle& rRect )
1527 {
1528     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::GetWorkArea, this, boost::ref( rRect ) ) ) )
1529         return;
1530 
1531     if ( !mpNSWindow )
1532         return;
1533 
1534     // #i113170# may not be the main thread if called from UNO API
1535     SalData::ensureThreadAutoreleasePool();
1536 
1537     NSScreen* pScreen = [mpNSWindow screen];
1538     if( pScreen ==  nil )
1539         pScreen = [NSScreen mainScreen];
1540     NSRect aRect = [pScreen visibleFrame];
1541     CocoaToVCL( aRect );
1542     rRect.nLeft     = static_cast<long>(aRect.origin.x);
1543     rRect.nTop      = static_cast<long>(aRect.origin.y);
1544     rRect.nRight    = static_cast<long>(aRect.origin.x + aRect.size.width - 1);
1545     rRect.nBottom   = static_cast<long>(aRect.origin.y + aRect.size.height - 1);
1546 }
1547 
GetPointerState()1548 SalPointerState AquaSalFrame::GetPointerState()
1549 {
1550     AquaSalInstance* pInst = GetSalData()->mpFirstInstance;
1551     if( ! pInst->isNSAppThread() )
1552         return pInst->callOnMainThread< SalPointerState >( boost::bind( &AquaSalFrame::GetPointerState, this ) );
1553 
1554     // #i113170# may not be the main thread if called from UNO API
1555     SalData::ensureThreadAutoreleasePool();
1556 
1557     SalPointerState state;
1558     state.mnState = 0;
1559 
1560     // get position
1561     NSPoint aPt = [mpNSWindow mouseLocationOutsideOfEventStream];
1562     CocoaToVCL( aPt, false );
1563     state.maPos = Point(static_cast<long>(aPt.x), static_cast<long>(aPt.y));
1564 
1565     NSEvent* pCur = [NSApp currentEvent];
1566     bool bMouseEvent = false;
1567     if( pCur )
1568     {
1569         bMouseEvent = true;
1570         switch( [pCur type] )
1571         {
1572         case NSLeftMouseDown:       state.mnState |= MOUSE_LEFT; break;
1573         case NSLeftMouseUp:         break;
1574         case NSRightMouseDown:      state.mnState |= MOUSE_RIGHT; break;
1575         case NSRightMouseUp:        break;
1576         case NSOtherMouseDown:      state.mnState |= ([pCur buttonNumber] == 2) ? MOUSE_MIDDLE : 0; break;
1577         case NSOtherMouseUp:        break;
1578         case NSMouseMoved:          break;
1579         case NSLeftMouseDragged:    state.mnState |= MOUSE_LEFT; break;
1580         case NSRightMouseDragged:   state.mnState |= MOUSE_RIGHT; break;
1581         case NSOtherMouseDragged:   state.mnState |= ([pCur buttonNumber] == 2) ? MOUSE_MIDDLE : 0; break;
1582             break;
1583         default:
1584             bMouseEvent = false;
1585             break;
1586         }
1587     }
1588     if( bMouseEvent )
1589     {
1590         unsigned int nMask = (unsigned int)[pCur modifierFlags];
1591         if( (nMask & NSShiftKeyMask) != 0 )
1592             state.mnState |= KEY_SHIFT;
1593         if( (nMask & NSControlKeyMask) != 0 )
1594             state.mnState |= KEY_MOD3;
1595         if( (nMask & NSAlternateKeyMask) != 0 )
1596             state.mnState |= KEY_MOD2;
1597         if( (nMask & NSCommandKeyMask) != 0 )
1598             state.mnState |= KEY_MOD1;
1599 
1600     }
1601     else
1602     {
1603         // FIXME: replace Carbon by Cocoa
1604         // Cocoa does not have an equivalent for GetCurrentEventButtonState
1605         // and GetCurrentEventKeyModifiers.
1606         // we could try to get away with tracking all events for modifierKeys
1607         // and all mouse events for button state in VCL_NSApllication::sendEvent,
1608         // but it is unclear whether this will get us the same result.
1609         // leave in GetCurrentEventButtonState and GetCurrentEventKeyModifiers for now
1610 
1611         // fill in button state
1612         UInt32 nState = GetCurrentEventButtonState();
1613         state.mnState = 0;
1614         if( nState & 1 )
1615             state.mnState |= MOUSE_LEFT;    // primary button
1616         if( nState & 2 )
1617             state.mnState |= MOUSE_RIGHT;   // secondary button
1618         if( nState & 4 )
1619             state.mnState |= MOUSE_MIDDLE;  // tertiary button
1620 
1621         // fill in modifier state
1622         nState = GetCurrentEventKeyModifiers();
1623         if( nState & shiftKey )
1624             state.mnState |= KEY_SHIFT;
1625         if( nState & controlKey )
1626             state.mnState |= KEY_MOD3;
1627         if( nState & optionKey )
1628             state.mnState |= KEY_MOD2;
1629         if( nState & cmdKey )
1630             state.mnState |= KEY_MOD1;
1631     }
1632 
1633 
1634     return state;
1635 }
1636 
SetPluginParent(SystemParentData *)1637 bool AquaSalFrame::SetPluginParent( SystemParentData* )
1638 {
1639     // plugin parent may be killed unexpectedly by
1640     // plugging process;
1641 
1642     //TODO: implement
1643     return sal_False;
1644 }
1645 
MapUnicodeToKeyCode(sal_Unicode,LanguageType,KeyCode &)1646 sal_Bool AquaSalFrame::MapUnicodeToKeyCode( sal_Unicode , LanguageType , KeyCode& )
1647 {
1648     // not supported yet
1649     return FALSE;
1650 }
1651 
GetInputLanguage()1652 LanguageType AquaSalFrame::GetInputLanguage()
1653 {
1654     //TODO: implement
1655     return LANGUAGE_DONTKNOW;
1656 }
1657 
DrawMenuBar()1658 void AquaSalFrame::DrawMenuBar()
1659 {
1660 }
1661 
SetMenu(SalMenu * pSalMenu)1662 void AquaSalFrame::SetMenu( SalMenu* pSalMenu )
1663 {
1664     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetMenu, this, pSalMenu ) ) )
1665         return;
1666 
1667     // #i113170# may not be the main thread if called from UNO API
1668     SalData::ensureThreadAutoreleasePool();
1669 
1670     AquaSalMenu* pMenu = static_cast<AquaSalMenu*>(pSalMenu);
1671     DBG_ASSERT( ! pMenu || pMenu->mbMenuBar, "setting non menubar on frame" );
1672     mpMenu = pMenu;
1673     if( mpMenu  )
1674         mpMenu->setMainMenu();
1675 }
1676 
SetExtendedFrameStyle(SalExtStyle nStyle)1677 void AquaSalFrame::SetExtendedFrameStyle( SalExtStyle nStyle )
1678 {
1679     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::SetExtendedFrameStyle, this, nStyle ) ) )
1680         return;
1681 
1682     if ( mpNSWindow )
1683     {
1684     // #i113170# may not be the main thread if called from UNO API
1685     SalData::ensureThreadAutoreleasePool();
1686 
1687     if( (mnExtStyle & SAL_FRAME_EXT_STYLE_DOCMODIFIED) != (nStyle & SAL_FRAME_EXT_STYLE_DOCMODIFIED) )
1688         [mpNSWindow setDocumentEdited: (nStyle & SAL_FRAME_EXT_STYLE_DOCMODIFIED) ? YES : NO];
1689     }
1690 
1691     mnExtStyle = nStyle;
1692 }
1693 
SetBackgroundBitmap(SalBitmap *)1694 void AquaSalFrame::SetBackgroundBitmap( SalBitmap* )
1695 {
1696     //TODO: implement
1697 }
1698 
SnapShot()1699 SalBitmap* AquaSalFrame::SnapShot()
1700 {
1701     return mpGraphics ? mpGraphics->getBitmap( 0, 0, maGeometry.nWidth, maGeometry.nHeight ) : NULL;
1702 }
1703 
GetParent() const1704 SalFrame* AquaSalFrame::GetParent() const
1705 {
1706     return mpParent;
1707 }
1708 
SetParent(SalFrame * pNewParent)1709 void AquaSalFrame::SetParent( SalFrame* pNewParent )
1710 {
1711     bool bShown = mbShown;
1712     // remove from child list
1713     Show( FALSE );
1714     mpParent = (AquaSalFrame*)pNewParent;
1715     // insert to correct parent and paint
1716     Show( bShown );
1717 }
1718 
UpdateFrameGeometry()1719 void AquaSalFrame::UpdateFrameGeometry()
1720 {
1721     if ( !mpNSWindow )
1722     {
1723         return;
1724     }
1725 
1726     // keep in mind that view and window coordinates are lower left
1727     // whereas vcl's are upper left
1728 
1729     // update screen rect
1730     NSScreen * pScreen = [mpNSWindow screen];
1731     if( pScreen )
1732     {
1733         maScreenRect = [pScreen frame];
1734         NSArray* pScreens = [NSScreen screens];
1735         if( pScreens )
1736             maGeometry.nScreenNumber = [pScreens indexOfObject: pScreen];
1737     }
1738 
1739     NSRect aFrameRect = [mpNSWindow frame];
1740     NSRect aContentRect = [NSWindow contentRectForFrameRect: aFrameRect styleMask: mnStyleMask];
1741 
1742     // release old track rect
1743     [mpNSView removeTrackingRect: mnTrackingRectTag];
1744     // install the new track rect
1745     NSRect aTrackRect = { NSZeroPoint, aContentRect.size };
1746     mnTrackingRectTag = [mpNSView addTrackingRect: aTrackRect owner: mpNSView userData: nil assumeInside: NO];
1747 
1748     // convert to vcl convention
1749     CocoaToVCL( aFrameRect );
1750     CocoaToVCL( aContentRect );
1751 
1752     maGeometry.nX = static_cast<int>(aContentRect.origin.x);
1753     maGeometry.nY = static_cast<int>(aContentRect.origin.y);
1754 
1755     maGeometry.nLeftDecoration = static_cast<unsigned int>(aContentRect.origin.x - aFrameRect.origin.x);
1756     maGeometry.nRightDecoration = static_cast<unsigned int>((aFrameRect.origin.x + aFrameRect.size.width) -
1757                                   (aContentRect.origin.x + aContentRect.size.width));
1758 
1759     maGeometry.nTopDecoration = static_cast<unsigned int>(aContentRect.origin.y - aFrameRect.origin.y);
1760     maGeometry.nBottomDecoration = static_cast<unsigned int>((aFrameRect.origin.y + aFrameRect.size.height) -
1761                                    (aContentRect.origin.y + aContentRect.size.height));
1762 
1763     maGeometry.nWidth = static_cast<unsigned int>(aContentRect.size.width);
1764     maGeometry.nHeight = static_cast<unsigned int>(aContentRect.size.height);
1765 }
1766 
1767 // -----------------------------------------------------------------------
1768 
CaptureMouse(sal_Bool bCapture)1769 void AquaSalFrame::CaptureMouse( sal_Bool bCapture )
1770 {
1771     /* Remark:
1772        we'll try to use a pidgin version of capture mouse
1773        on MacOSX (neither carbon nor cocoa) there is a
1774        CaptureMouse equivalent (in Carbon there is TrackMouseLocation
1775        but this is useless to use since it is blocking)
1776 
1777        However on cocoa the active frame seems to get mouse events
1778        also outside the window, so we'll try to forward mouse events
1779        to the capture frame in the hope that one of our frames
1780        gets a mouse event.
1781 
1782        This will break as soon as the user activates another app, but
1783        a mouse click will normally lead to a release of the mouse anyway.
1784 
1785        Let's see how far we get this way. Alternatively we could use one
1786        large overlay window like we did for the carbon implementation,
1787        however that is resource intensive.
1788     */
1789 
1790     if( bCapture )
1791         s_pCaptureFrame = this;
1792     else if( ! bCapture && s_pCaptureFrame == this )
1793         s_pCaptureFrame = NULL;
1794 }
1795 
ResetClipRegion()1796 void AquaSalFrame::ResetClipRegion()
1797 {
1798     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::ResetClipRegion, this ) ) )
1799         return;
1800 
1801     if ( !mpNSWindow )
1802     {
1803         return;
1804     }
1805 
1806     // #i113170# may not be the main thread if called from UNO API
1807     SalData::ensureThreadAutoreleasePool();
1808 
1809     // release old path and indicate no clipping
1810     CGPathRelease( mrClippingPath );
1811     mrClippingPath = NULL;
1812 
1813     if( mpNSView && mbShown )
1814         [mpNSView setNeedsDisplay: YES];
1815     if( mpNSWindow )
1816     {
1817         [mpNSWindow setOpaque: YES];
1818         [mpNSWindow invalidateShadow];
1819     }
1820 }
1821 
BeginSetClipRegion(sal_uLong nRects)1822 void AquaSalFrame::BeginSetClipRegion( sal_uLong nRects )
1823 {
1824     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::BeginSetClipRegion, this, nRects ) ) )
1825         return;
1826 
1827     if ( !mpNSWindow )
1828     {
1829         return;
1830     }
1831 
1832     // #i113170# may not be the main thread if called from UNO API
1833     SalData::ensureThreadAutoreleasePool();
1834 
1835     // release old path
1836     if( mrClippingPath )
1837     {
1838         CGPathRelease( mrClippingPath );
1839         mrClippingPath = NULL;
1840     }
1841 
1842     if( maClippingRects.size() > SAL_CLIPRECT_COUNT && nRects < maClippingRects.size() )
1843     {
1844         std::vector<CGRect> aEmptyVec;
1845         maClippingRects.swap( aEmptyVec );
1846     }
1847     maClippingRects.clear();
1848     maClippingRects.reserve( nRects );
1849 }
1850 
UnionClipRegion(long nX,long nY,long nWidth,long nHeight)1851 void AquaSalFrame::UnionClipRegion( long nX, long nY, long nWidth, long nHeight )
1852 {
1853     // #i113170# may not be the main thread if called from UNO API
1854     SalData::ensureThreadAutoreleasePool();
1855 
1856     if( nWidth && nHeight )
1857     {
1858         NSRect aRect = NSMakeRect( nX, nY, nWidth, nHeight);
1859         VCLToCocoa( aRect, false );
1860         maClippingRects.push_back( CGRectMake(aRect.origin.x, aRect.origin.y, aRect.size.width, aRect.size.height) );
1861     }
1862 }
1863 
EndSetClipRegion()1864 void AquaSalFrame::EndSetClipRegion()
1865 {
1866     if( GetSalData()->mpFirstInstance->forwardToMainThread( boost::bind( &AquaSalFrame::EndSetClipRegion, this ) ) )
1867         return;
1868 
1869     if ( !mpNSWindow )
1870     {
1871         return;
1872     }
1873 
1874     // #i113170# may not be the main thread if called from UNO API
1875     SalData::ensureThreadAutoreleasePool();
1876 
1877     if( ! maClippingRects.empty() )
1878     {
1879         mrClippingPath = CGPathCreateMutable();
1880         CGPathAddRects( mrClippingPath, NULL, &maClippingRects[0], maClippingRects.size() );
1881     }
1882     if( mpNSView && mbShown )
1883         [mpNSView setNeedsDisplay: YES];
1884     if( mpNSWindow )
1885     {
1886         [mpNSWindow setOpaque: (mrClippingPath != NULL) ? NO : YES];
1887         [mpNSWindow setBackgroundColor: [NSColor clearColor]];
1888         // shadow is invalidated when view gets drawn again
1889     }
1890 }
1891