xref: /aoo41x/main/vcl/aqua/source/app/vclnsapp.mm (revision cdf0e10c)
1/*************************************************************************
2 *
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
6 *
7 * OpenOffice.org - a multi-platform office productivity suite
8 *
9 * This file is part of OpenOffice.org.
10 *
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org.  If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
25 *
26 ************************************************************************/
27
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29#include "precompiled_vcl.hxx"
30
31#include "rtl/ustrbuf.hxx"
32
33#include "vcl/window.hxx"
34#include "vcl/svapp.hxx"
35#include "vcl/cmdevt.hxx"
36
37#include "aqua/vclnsapp.h"
38#include "aqua/salinst.h"
39#include "aqua/saldata.hxx"
40#include "aqua/salframe.h"
41#include "aqua/salframeview.h"
42
43#include "impimagetree.hxx"
44
45#include "premac.h"
46#import "Carbon/Carbon.h"
47#import "apple_remote/RemoteControl.h"
48#include "postmac.h"
49
50
51@implementation CocoaThreadEnabler
52-(void)enableCocoaThreads:(id)param
53{
54    // do nothing, this is just to start an NSThread and therefore put
55    // Cocoa into multithread mode
56    (void)param;
57}
58@end
59
60@implementation VCL_NSApplication
61-(void)sendEvent:(NSEvent*)pEvent
62{
63    NSEventType eType = [pEvent type];
64    if( eType == NSApplicationDefined )
65        GetSalData()->mpFirstInstance->handleAppDefinedEvent( pEvent );
66    else if( eType == NSKeyDown && ([pEvent modifierFlags] & NSCommandKeyMask) != 0 )
67    {
68        NSWindow* pKeyWin = [NSApp keyWindow];
69        if( pKeyWin && [pKeyWin isKindOfClass: [SalFrameWindow class]] )
70        {
71            AquaSalFrame* pFrame = [(SalFrameWindow*)pKeyWin getSalFrame];
72            // handle Cmd-W
73            // FIXME: the correct solution would be to handle this in framework
74            // in the menu code
75            // however that is currently being revised, so let's use a preliminary solution here
76            // this hack is based on assumption
77            // a) Cmd-W is the same in all languages in OOo's menu conig
78            // b) Cmd-W is the same in all languages in on MacOS
79            // for now this seems to be true
80            unsigned int nModMask = ([pEvent modifierFlags] & (NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask));
81            if( (pFrame->mnStyleMask & NSClosableWindowMask) != 0 )
82            {
83                if( nModMask == NSCommandKeyMask
84                    && [[pEvent charactersIgnoringModifiers] isEqualToString: @"w"] )
85                {
86                    [pFrame->getWindow() windowShouldClose: nil];
87                    return;
88                }
89            }
90
91            /*
92             * #i98949# - Cmd-M miniaturize window, Cmd-Option-M miniaturize all windows
93             */
94            if( [[pEvent charactersIgnoringModifiers] isEqualToString: @"m"] )
95            {
96                if ( nModMask == NSCommandKeyMask && ([pFrame->getWindow() styleMask] & NSMiniaturizableWindowMask) )
97                {
98                    [pFrame->getWindow() performMiniaturize: nil];
99                    return;
100                }
101
102                if ( nModMask == ( NSCommandKeyMask | NSAlternateKeyMask ) )
103                {
104                    [NSApp miniaturizeAll: nil];
105                    return;
106                }
107            }
108
109            // #i90083# handle frame switching
110            // FIXME: lousy workaround
111            if( (nModMask & (NSControlKeyMask|NSAlternateKeyMask)) == 0 )
112            {
113                if( [[pEvent characters] isEqualToString: @"<"] ||
114                    [[pEvent characters] isEqualToString: @"~"] )
115                {
116                    [self cycleFrameForward: pFrame];
117                    return;
118                }
119                else if( [[pEvent characters] isEqualToString: @">"] ||
120                         [[pEvent characters] isEqualToString: @"`"] )
121                {
122                    [self cycleFrameBackward: pFrame];
123                    return;
124                }
125            }
126
127            // get information whether the event was handled; keyDown returns nothing
128            GetSalData()->maKeyEventAnswer[ pEvent ] = false;
129            bool bHandled = false;
130
131            // dispatch to view directly to avoid the key event being consumed by the menubar
132            // popup windows do not get the focus, so they don't get these either
133            // simplest would be dispatch this to the key window always if it is without parent
134            // however e.g. in document we want the menu shortcut if e.g. the stylist has focus
135            if( pFrame->mpParent && (pFrame->mnStyle & SAL_FRAME_STYLE_FLOAT) == 0 )
136            {
137                [[pKeyWin contentView] keyDown: pEvent];
138                bHandled = GetSalData()->maKeyEventAnswer[ pEvent ];
139            }
140
141            // see whether the main menu consumes this event
142            // if not, we want to dispatch it ourselves. Unless we do this "trick"
143            // the main menu just beeps for an unknown or disabled key equivalent
144            // and swallows the event wholesale
145            NSMenu* pMainMenu = [NSApp mainMenu];
146            if( ! bHandled && (pMainMenu == 0 || ! [pMainMenu performKeyEquivalent: pEvent]) )
147            {
148                [[pKeyWin contentView] keyDown: pEvent];
149                bHandled = GetSalData()->maKeyEventAnswer[ pEvent ];
150            }
151            else
152                bHandled = true;  // event handled already or main menu just handled it
153
154            GetSalData()->maKeyEventAnswer.erase( pEvent );
155            if( bHandled )
156                return;
157        }
158        else if( pKeyWin )
159        {
160            // #i94601# a window not of vcl's making has the focus.
161            // Since our menus do not invoke the usual commands
162            // try to play nice with native windows like the file dialog
163            // and emulate them
164            // precondition: this ONLY works because CMD-V (paste), CMD-C (copy) and CMD-X (cut) are
165            // NOT localized, that is the same in all locales. Should this be
166            // different in any locale, this hack will fail.
167            unsigned int nModMask = ([pEvent modifierFlags] & (NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask));
168            if( nModMask == NSCommandKeyMask )
169            {
170
171                if( [[pEvent charactersIgnoringModifiers] isEqualToString: @"v"] )
172                {
173                    if( [NSApp sendAction: @selector(paste:) to: nil from: nil] )
174                        return;
175                }
176                else if( [[pEvent charactersIgnoringModifiers] isEqualToString: @"c"] )
177                {
178                    if( [NSApp sendAction: @selector(copy:) to: nil from: nil] )
179                        return;
180                }
181                else if( [[pEvent charactersIgnoringModifiers] isEqualToString: @"x"] )
182                {
183                    if( [NSApp sendAction: @selector(cut:) to: nil from: nil] )
184                        return;
185                }
186            }
187        }
188    }
189    else if( eType == NSScrollWheel && ( GetSalData()->mnSystemVersion < VER_LEOPARD /* fixed in Leopard and above */ ) )
190    {
191
192        NSWindow* pWin = [pEvent window];
193        // on Tiger wheel events do not reach non key windows
194        // which probably should be considered a bug
195        if( [pWin isKindOfClass: [SalFrameWindow class]] && [pWin canBecomeKeyWindow] == NO )
196        {
197            [[pWin contentView] scrollWheel: pEvent];
198            return;
199        }
200    }
201    [super sendEvent: pEvent];
202}
203
204-(void)sendSuperEvent:(NSEvent*)pEvent
205{
206    [super sendEvent: pEvent];
207}
208
209-(void)cycleFrameForward: (AquaSalFrame*)pCurFrame
210{
211    // find current frame in list
212    std::list< AquaSalFrame* >& rFrames( GetSalData()->maFrames );
213    std::list< AquaSalFrame* >::iterator it = rFrames.begin();
214    for( ; it != rFrames.end() && *it != pCurFrame; ++it )
215        ;
216    if( it != rFrames.end() )
217    {
218        // now find the next frame (or end)
219        do
220        {
221            ++it;
222            if( it != rFrames.end() )
223            {
224                if( (*it)->mpDockMenuEntry != NULL &&
225                    (*it)->mbShown )
226                {
227                    [(*it)->getWindow() makeKeyAndOrderFront: NSApp];
228                    return;
229                }
230            }
231        } while( it != rFrames.end() );
232        // cycle around, find the next up to pCurFrame
233        it = rFrames.begin();
234        while( *it != pCurFrame )
235        {
236            if( (*it)->mpDockMenuEntry != NULL &&
237                (*it)->mbShown )
238            {
239                [(*it)->getWindow() makeKeyAndOrderFront: NSApp];
240                return;
241            }
242            ++it;
243        }
244    }
245}
246
247-(void)cycleFrameBackward: (AquaSalFrame*)pCurFrame
248{
249    // do the same as cycleFrameForward only with a reverse iterator
250
251    // find current frame in list
252    std::list< AquaSalFrame* >& rFrames( GetSalData()->maFrames );
253    std::list< AquaSalFrame* >::reverse_iterator it = rFrames.rbegin();
254    for( ; it != rFrames.rend() && *it != pCurFrame; ++it )
255        ;
256    if( it != rFrames.rend() )
257    {
258        // now find the next frame (or end)
259        do
260        {
261            ++it;
262            if( it != rFrames.rend() )
263            {
264                if( (*it)->mpDockMenuEntry != NULL &&
265                    (*it)->mbShown )
266                {
267                    [(*it)->getWindow() makeKeyAndOrderFront: NSApp];
268                    return;
269                }
270            }
271        } while( it != rFrames.rend() );
272        // cycle around, find the next up to pCurFrame
273        it = rFrames.rbegin();
274        while( *it != pCurFrame )
275        {
276            if( (*it)->mpDockMenuEntry != NULL &&
277                (*it)->mbShown )
278            {
279                [(*it)->getWindow() makeKeyAndOrderFront: NSApp];
280                return;
281            }
282            ++it;
283        }
284    }
285}
286
287-(NSMenu*)applicationDockMenu:(NSApplication *)sender
288{
289    (void)sender;
290    return AquaSalInstance::GetDynamicDockMenu();
291}
292
293-(BOOL)application: (NSApplication*)app openFile: (NSString*)pFile
294{
295    (void)app;
296    const rtl::OUString aFile( GetOUString( pFile ) );
297    if( ! AquaSalInstance::isOnCommandLine( aFile ) )
298    {
299        const ApplicationEvent* pAppEvent = new ApplicationEvent( String(), ApplicationAddress(),
300                                                    APPEVENT_OPEN_STRING, aFile );
301        AquaSalInstance::aAppEventList.push_back( pAppEvent );
302    }
303    return YES;
304}
305
306-(void)application: (NSApplication*) app openFiles: (NSArray*)files
307{
308    (void)app;
309    rtl::OUStringBuffer aFileList( 256 );
310
311    NSEnumerator* it = [files objectEnumerator];
312    NSString* pFile = nil;
313
314    while( (pFile = [it nextObject]) != nil )
315    {
316        const rtl::OUString aFile( GetOUString( pFile ) );
317        if( ! AquaSalInstance::isOnCommandLine( aFile ) )
318        {
319            if( aFileList.getLength() > 0 )
320                aFileList.append( sal_Unicode( APPEVENT_PARAM_DELIMITER ) );
321            aFileList.append( aFile );
322        }
323    }
324
325    if( aFileList.getLength() )
326    {
327        // we have no back channel here, we have to assume success, in which case
328        // replyToOpenOrPrint does not need to be called according to documentation
329        // [app replyToOpenOrPrint: NSApplicationDelegateReplySuccess];
330        const ApplicationEvent* pAppEvent = new ApplicationEvent( String(), ApplicationAddress(),
331                                                    APPEVENT_OPEN_STRING, aFileList.makeStringAndClear() );
332        AquaSalInstance::aAppEventList.push_back( pAppEvent );
333    }
334}
335
336-(BOOL)application: (NSApplication*)app printFile: (NSString*)pFile
337{
338    (void)app;
339    const rtl::OUString aFile( GetOUString( pFile ) );
340	const ApplicationEvent* pAppEvent = new ApplicationEvent( String(), ApplicationAddress(),
341                                                APPEVENT_PRINT_STRING, aFile );
342	AquaSalInstance::aAppEventList.push_back( pAppEvent );
343    return YES;
344}
345-(NSApplicationPrintReply)application: (NSApplication *) app printFiles:(NSArray *)files withSettings: (NSDictionary *)printSettings showPrintPanels:(BOOL)bShowPrintPanels
346{
347    (void)app;
348    (void)printSettings;
349    (void)bShowPrintPanels;
350    // currently ignores print settings an bShowPrintPanels
351    rtl::OUStringBuffer aFileList( 256 );
352
353    NSEnumerator* it = [files objectEnumerator];
354    NSString* pFile = nil;
355
356    while( (pFile = [it nextObject]) != nil )
357    {
358        if( aFileList.getLength() > 0 )
359            aFileList.append( sal_Unicode( APPEVENT_PARAM_DELIMITER ) );
360        aFileList.append( GetOUString( pFile ) );
361    }
362	const ApplicationEvent* pAppEvent = new ApplicationEvent( String(), ApplicationAddress(),
363                                                APPEVENT_PRINT_STRING, aFileList.makeStringAndClear() );
364	AquaSalInstance::aAppEventList.push_back( pAppEvent );
365    // we have no back channel here, we have to assume success
366    // correct handling would be NSPrintingReplyLater and then send [app replyToOpenOrPrint]
367    return NSPrintingSuccess;
368}
369
370-(NSApplicationTerminateReply)applicationShouldTerminate: (NSApplication *) app
371{
372    (void)app;
373    NSApplicationTerminateReply aReply = NSTerminateNow;
374    {
375        YIELD_GUARD;
376
377        SalData* pSalData = GetSalData();
378        if( ! pSalData->maFrames.empty() )
379        {
380            // the following QueryExit will likely present a message box, activate application
381            [NSApp activateIgnoringOtherApps: YES];
382            aReply = pSalData->maFrames.front()->CallCallback( SALEVENT_SHUTDOWN, NULL ) ? NSTerminateCancel : NSTerminateNow;
383        }
384
385        if( aReply == NSTerminateNow )
386        {
387            ApplicationEvent aEv( String(), ApplicationAddress(), ByteString( "PRIVATE:DOSHUTDOWN" ), String() );
388            GetpApp()->AppEvent( aEv );
389            ImplImageTreeSingletonRef()->shutDown();
390            // DeInitVCL should be called in ImplSVMain - unless someon _exits first which
391            // can occur in Desktop::doShutdown for example
392        }
393    }
394
395    return aReply;
396}
397
398-(void)systemColorsChanged: (NSNotification*) pNotification
399{
400    (void)pNotification;
401    YIELD_GUARD;
402
403    const SalData* pSalData = GetSalData();
404	if( !pSalData->maFrames.empty() )
405		pSalData->maFrames.front()->CallCallback( SALEVENT_SETTINGSCHANGED, NULL );
406}
407
408-(void)screenParametersChanged: (NSNotification*) pNotification
409{
410    (void)pNotification;
411    YIELD_GUARD;
412
413    SalData* pSalData = GetSalData();
414    std::list< AquaSalFrame* >::iterator it;
415    for( it = pSalData->maFrames.begin(); it != pSalData->maFrames.end(); ++it )
416    {
417        (*it)->screenParametersChanged();
418    }
419}
420
421-(void)scrollbarVariantChanged: (NSNotification*) pNotification
422{
423    (void)pNotification;
424    GetSalData()->mpFirstInstance->delayedSettingsChanged( true );
425}
426
427-(void)scrollbarSettingsChanged: (NSNotification*) pNotification
428{
429    (void)pNotification;
430    GetSalData()->mpFirstInstance->delayedSettingsChanged( false );
431}
432
433-(void)addFallbackMenuItem: (NSMenuItem*)pNewItem
434{
435    AquaSalMenu::addFallbackMenuItem( pNewItem );
436}
437
438-(void)removeFallbackMenuItem: (NSMenuItem*)pItem
439{
440    AquaSalMenu::removeFallbackMenuItem( pItem );
441}
442
443-(void)addDockMenuItem: (NSMenuItem*)pNewItem
444{
445    NSMenu* pDock = AquaSalInstance::GetDynamicDockMenu();
446    [pDock insertItem: pNewItem atIndex: [pDock numberOfItems]];
447}
448
449// for Apple Remote implementation
450
451#pragma mark -
452#pragma mark NSApplication Delegates
453- (void)applicationWillBecomeActive:(NSNotification *)pNotification
454{
455    (void)pNotification;
456    if (GetSalData()->mpMainController->remoteControl) {
457
458        // [remoteControl startListening: self];
459        // does crash because the right thing to do is
460        // [GetSalData()->mpMainController->remoteControl startListening: self];
461        // but the instance variable 'remoteControl' is declared protected
462        // workaround : declare remoteControl instance variable as public in RemoteMainController.m
463
464        [GetSalData()->mpMainController->remoteControl startListening: self];
465#ifdef DEBUG
466        NSLog(@"Apple Remote will become active - Using remote controls");
467#endif
468    }
469}
470
471- (void)applicationWillResignActive:(NSNotification *)pNotification
472{
473    (void)pNotification;
474    if (GetSalData()->mpMainController->remoteControl) {
475
476        // [remoteControl stopListening: self];
477        // does crash because the right thing to do is
478        // [GetSalData()->mpMainController->remoteControl stopListening: self];
479        // but the instance variable 'remoteControl' is declared protected
480        // workaround : declare remoteControl instance variable as public in RemoteMainController.m
481
482        [GetSalData()->mpMainController->remoteControl stopListening: self];
483#ifdef DEBUG
484        NSLog(@"Apple Remote will resign active - Releasing remote controls");
485#endif
486    }
487}
488
489- (BOOL)applicationShouldHandleReopen: (NSApplication*)pApp hasVisibleWindows: (BOOL) bWinVisible
490{
491    (void)pApp;
492    (void)bWinVisible;
493    NSObject* pHdl = GetSalData()->mpDockIconClickHandler;
494    if( pHdl && [pHdl respondsToSelector: @selector(dockIconClicked:)] )
495    {
496        [pHdl performSelector:@selector(dockIconClicked:) withObject: self];
497    }
498    return YES;
499}
500
501-(void)setDockIconClickHandler: (NSObject*)pHandler
502{
503    GetSalData()->mpDockIconClickHandler = pHandler;
504}
505
506
507@end
508
509