Home | History | Annotate | Download | only in Hosted
      1 /*
      2  * Copyright (C) 2009 Apple Inc. All Rights Reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #if USE(PLUGIN_HOST_PROCESS)
     27 
     28 #import "WebTextInputWindowController.h"
     29 
     30 #import <WebKitSystemInterface.h>
     31 
     32 @interface WebTextInputPanel : NSPanel {
     33     NSTextView *_inputTextView;
     34 }
     35 
     36 - (NSTextInputContext *)_inputContext;
     37 - (BOOL)_interpretKeyEvent:(NSEvent *)event string:(NSString **)string;
     38 
     39 @end
     40 
     41 #define inputWindowHeight 20
     42 
     43 @implementation WebTextInputPanel
     44 
     45 - (void)dealloc
     46 {
     47     [[NSNotificationCenter defaultCenter] removeObserver:self];
     48 
     49     [_inputTextView release];
     50 
     51     [super dealloc];
     52 }
     53 
     54 - (id)init
     55 {
     56     self = [super initWithContentRect:NSZeroRect styleMask:WKGetInputPanelWindowStyle() backing:NSBackingStoreBuffered defer:YES];
     57     if (!self)
     58         return nil;
     59 
     60     // Set the frame size.
     61     NSRect visibleFrame = [[NSScreen mainScreen] visibleFrame];
     62     NSRect frame = NSMakeRect(visibleFrame.origin.x, visibleFrame.origin.y, visibleFrame.size.width, inputWindowHeight);
     63 
     64     [self setFrame:frame display:NO];
     65 
     66     _inputTextView = [[NSTextView alloc] initWithFrame:[self.contentView frame]];
     67     _inputTextView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable | NSViewMaxXMargin | NSViewMinXMargin | NSViewMaxYMargin | NSViewMinYMargin;
     68 
     69     NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:[self.contentView frame]];
     70     scrollView.documentView = _inputTextView;
     71     self.contentView = scrollView;
     72     [scrollView release];
     73 
     74     [self setFloatingPanel:YES];
     75 
     76     [[NSNotificationCenter defaultCenter] addObserver:self
     77                                              selector:@selector(_keyboardInputSourceChanged:)
     78                                                  name:NSTextInputContextKeyboardSelectionDidChangeNotification
     79                                                object:nil];
     80 
     81     return self;
     82 }
     83 
     84 - (void)_keyboardInputSourceChanged:(NSNotification *)notification
     85 {
     86     [_inputTextView setString:@""];
     87     [self orderOut:nil];
     88 }
     89 
     90 - (BOOL)_interpretKeyEvent:(NSEvent *)event string:(NSString **)string
     91 {
     92     BOOL hadMarkedText = [_inputTextView hasMarkedText];
     93 
     94     *string = nil;
     95 
     96     if (![[_inputTextView inputContext] handleEvent:event])
     97         return NO;
     98 
     99     if ([_inputTextView hasMarkedText]) {
    100         // Don't show the input method window for dead keys
    101         if ([[event characters] length] > 0)
    102             [self orderFront:nil];
    103 
    104         return YES;
    105     }
    106 
    107     if (hadMarkedText) {
    108         [self orderOut:nil];
    109 
    110         NSString *text = [[_inputTextView textStorage] string];
    111         if ([text length] > 0)
    112             *string = [[text copy] autorelease];
    113     }
    114 
    115     [_inputTextView setString:@""];
    116     return hadMarkedText;
    117 }
    118 
    119 - (NSTextInputContext *)_inputContext
    120 {
    121     return [_inputTextView inputContext];
    122 }
    123 
    124 @end
    125 
    126 @implementation WebTextInputWindowController
    127 
    128 + (WebTextInputWindowController *)sharedTextInputWindowController
    129 {
    130     static WebTextInputWindowController *textInputWindowController;
    131     if (!textInputWindowController)
    132         textInputWindowController = [[WebTextInputWindowController alloc] init];
    133 
    134     return textInputWindowController;
    135 }
    136 
    137 - (id)init
    138 {
    139     self = [super init];
    140     if (!self)
    141         return nil;
    142 
    143     _panel = [[WebTextInputPanel alloc] init];
    144 
    145     return self;
    146 }
    147 
    148 - (NSTextInputContext *)inputContext
    149 {
    150     return [_panel _inputContext];
    151 }
    152 
    153 - (BOOL)interpretKeyEvent:(NSEvent *)event string:(NSString **)string
    154 {
    155     return [_panel _interpretKeyEvent:event string:string];
    156 }
    157 
    158 @end
    159 
    160 #endif // USE(PLUGIN_HOST_PROCESS)
    161 
    162