/*
SRAppDelegate.m

Author: Makoto Kinoshita

Copyright 2004 The Shiira Project. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted 
provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions 
  and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of 
  conditions and the following disclaimer in the documentation and/or other materials provided 
  with the distribution.

THIS SOFTWARE IS PROVIDED BY THE SHIIRA PROJECT ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE SHIIRA PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/

#import "SRAppDelegate.h"
#import "SRTabExpose.h"

#import "SRApplication.h"

#import "AppKitEx.h"

@implementation SRApplication : NSApplication

#if 0
// For stack trace printing
#import <ExceptionHandling/NSExceptionHandler.h>
- (void)run
{
    // Enable exception
    [[NSExceptionHandler defaultExceptionHandler] 
            setExceptionHandlingMask:NSLogAndHandleEveryExceptionMask];
    
    [super run];
}
#endif

//--------------------------------------------------------------//
#pragma mark -- Event handling --
//--------------------------------------------------------------//

- (void)sendEvent:(NSEvent*)event
{
    // Check expose mode
    if (![SRTabExposeController isExposing]) {
        [super sendEvent:event];
        return;
    }
    
    // Ignore key event except deexpose request
    NSEventType type;
    type = [event type];
    if (type == NSKeyDown || type == NSKeyUp) {
        // Get expose character
        NSString*   keyEquivalent;
        unichar     c;
        keyEquivalent = [[[[[NSApp mainMenu] itemWithTag:SRWindowTag] submenu] itemWithTag:SRShowAllTabsTag] 
                keyEquivalent];
        c = [keyEquivalent characterAtIndex:0];
        
        // Deexpose
        if ([[event characters] characterAtIndex:0] == c) {
            [super sendEvent:event];
            return;
        }
        
        // Ignore key event
        return;
    }
    
    [super sendEvent:event];
}

- (BOOL)_handleKeyEquivalent:(NSEvent*)event
{
    BOOL    result;
    result = [super _handleKeyEquivalent:event];
    if (!result) {
        // Send to application delegate
        id  delegate;
        delegate = [self delegate];
        if (delegate && [delegate respondsToSelector:@selector(performKeyEquivalent:)]) {
            result = [delegate performKeyEquivalent:event];
        }
    }
    
    return result;
}

//--------------------------------------------------------------//
#pragma mark -- About panel --
//--------------------------------------------------------------//

- (void)orderFrontStandardAboutPanel:(id)sender
{
    if ([[self delegate] respondsToSelector:@selector(orderFrontStandardAboutPanel:)]) {
        [[self delegate] orderFrontStandardAboutPanel:sender];
    }
}

@end
