1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/cocoa/ComplexTextInputPanel.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,194 @@ 1.4 +/* 1.5 + * Copyright (C) 2009 Apple Inc. All Rights Reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1. Redistributions of source code must retain the above copyright 1.11 + * notice, this list of conditions and the following disclaimer. 1.12 + * 2. Redistributions in binary form must reproduce the above copyright 1.13 + * notice, this list of conditions and the following disclaimer in the 1.14 + * documentation and/or other materials provided with the distribution. 1.15 + * 1.16 + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 1.17 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.18 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.19 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 1.20 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.21 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.22 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.23 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 1.24 + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.25 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.26 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.27 + * 1.28 + * Modified by Josh Aas of Mozilla Corporation. 1.29 + */ 1.30 + 1.31 +#import "ComplexTextInputPanel.h" 1.32 + 1.33 +#include <algorithm> 1.34 +#include "mozilla/Preferences.h" 1.35 +#include "nsChildView.h" 1.36 +#include "nsCocoaFeatures.h" 1.37 + 1.38 +using namespace mozilla; 1.39 + 1.40 +#define kInputWindowHeight 20 1.41 + 1.42 +@implementation ComplexTextInputPanel 1.43 + 1.44 ++ (ComplexTextInputPanel*)sharedComplexTextInputPanel 1.45 +{ 1.46 + static ComplexTextInputPanel *sComplexTextInputPanel; 1.47 + if (!sComplexTextInputPanel) 1.48 + sComplexTextInputPanel = [[ComplexTextInputPanel alloc] init]; 1.49 + return sComplexTextInputPanel; 1.50 +} 1.51 + 1.52 +- (id)init 1.53 +{ 1.54 + // In the original Apple code the style mask is given by a function which is not open source. 1.55 + // What could possibly be worth hiding in that function, I do not know. 1.56 + // Courtesy of gdb: stylemask: 011000011111, 0x61f 1.57 + self = [super initWithContentRect:NSZeroRect styleMask:0x61f backing:NSBackingStoreBuffered defer:YES]; 1.58 + if (!self) 1.59 + return nil; 1.60 + 1.61 + // Set the frame size. 1.62 + NSRect visibleFrame = [[NSScreen mainScreen] visibleFrame]; 1.63 + NSRect frame = NSMakeRect(visibleFrame.origin.x, visibleFrame.origin.y, visibleFrame.size.width, kInputWindowHeight); 1.64 + 1.65 + [self setFrame:frame display:NO]; 1.66 + 1.67 + mInputTextView = [[NSTextView alloc] initWithFrame:[self.contentView frame]]; 1.68 + mInputTextView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable | NSViewMaxXMargin | NSViewMinXMargin | NSViewMaxYMargin | NSViewMinYMargin; 1.69 + 1.70 + NSScrollView* scrollView = [[NSScrollView alloc] initWithFrame:[self.contentView frame]]; 1.71 + scrollView.documentView = mInputTextView; 1.72 + self.contentView = scrollView; 1.73 + [scrollView release]; 1.74 + 1.75 + [self setFloatingPanel:YES]; 1.76 + 1.77 + [[NSNotificationCenter defaultCenter] addObserver:self 1.78 + selector:@selector(keyboardInputSourceChanged:) 1.79 + name:NSTextInputContextKeyboardSelectionDidChangeNotification 1.80 + object:nil]; 1.81 + 1.82 + return self; 1.83 +} 1.84 + 1.85 +- (void)dealloc 1.86 +{ 1.87 + [[NSNotificationCenter defaultCenter] removeObserver:self]; 1.88 + 1.89 + [mInputTextView release]; 1.90 + 1.91 + [super dealloc]; 1.92 +} 1.93 + 1.94 +- (void)keyboardInputSourceChanged:(NSNotification *)notification 1.95 +{ 1.96 + static int8_t sDoCancel = -1; 1.97 + if (!sDoCancel || ![self inComposition]) { 1.98 + return; 1.99 + } 1.100 + if (sDoCancel < 0) { 1.101 + bool cancelComposition = false; 1.102 + static const char* kPrefName = 1.103 + "ui.plugin.cancel_composition_at_input_source_changed"; 1.104 + nsresult rv = Preferences::GetBool(kPrefName, &cancelComposition); 1.105 + NS_ENSURE_SUCCESS(rv, ); 1.106 + sDoCancel = cancelComposition ? 1 : 0; 1.107 + } 1.108 + if (sDoCancel) { 1.109 + [self cancelComposition]; 1.110 + } 1.111 +} 1.112 + 1.113 +- (BOOL)interpretKeyEvent:(NSEvent*)event string:(NSString**)string 1.114 +{ 1.115 + BOOL hadMarkedText = [mInputTextView hasMarkedText]; 1.116 + 1.117 + *string = nil; 1.118 + 1.119 + if (![[mInputTextView inputContext] handleEvent:event]) 1.120 + return NO; 1.121 + 1.122 + if ([mInputTextView hasMarkedText]) { 1.123 + // Don't show the input method window for dead keys 1.124 + if ([[event characters] length] > 0) 1.125 + [self orderFront:nil]; 1.126 + 1.127 + return YES; 1.128 + } else { 1.129 + [self orderOut:nil]; 1.130 + 1.131 + NSString *text = [[mInputTextView textStorage] string]; 1.132 + if ([text length] > 0) 1.133 + *string = [[text copy] autorelease]; 1.134 + } 1.135 + 1.136 + [mInputTextView setString:@""]; 1.137 + return hadMarkedText; 1.138 +} 1.139 + 1.140 +- (NSTextInputContext*)inputContext 1.141 +{ 1.142 + return [mInputTextView inputContext]; 1.143 +} 1.144 + 1.145 +- (void)cancelComposition 1.146 +{ 1.147 + [mInputTextView setString:@""]; 1.148 + [self orderOut:nil]; 1.149 +} 1.150 + 1.151 +- (BOOL)inComposition 1.152 +{ 1.153 + return [mInputTextView hasMarkedText]; 1.154 +} 1.155 + 1.156 +- (void)adjustTo:(NSView*)view 1.157 +{ 1.158 + NSRect viewRect = [view frame]; 1.159 + viewRect.origin.x = 0; 1.160 + viewRect.origin.y = 0; 1.161 + viewRect = [view convertRect:viewRect toView:nil]; 1.162 + if (nsCocoaFeatures::OnLionOrLater()) { 1.163 + viewRect = [[view window] convertRectToScreen:viewRect]; 1.164 + } else { 1.165 + viewRect.origin = [[view window] convertBaseToScreen:viewRect.origin]; 1.166 + } 1.167 + NSRect selfRect = [self frame]; 1.168 + CGFloat minWidth = static_cast<CGFloat>( 1.169 + Preferences::GetUint("ui.plugin.panel.min-width", 500)); 1.170 + NSRect rect = NSMakeRect(viewRect.origin.x, 1.171 + viewRect.origin.y - selfRect.size.height, 1.172 + std::max(viewRect.size.width, minWidth), 1.173 + selfRect.size.height); 1.174 + 1.175 + // Adjust to screen. 1.176 + NSRect screenRect = [[NSScreen mainScreen] visibleFrame]; 1.177 + if (rect.origin.x < screenRect.origin.x) { 1.178 + rect.origin.x = screenRect.origin.x; 1.179 + } 1.180 + if (rect.origin.y < screenRect.origin.y) { 1.181 + rect.origin.y = screenRect.origin.y; 1.182 + } 1.183 + CGFloat xMostOfScreen = screenRect.origin.x + screenRect.size.width; 1.184 + CGFloat yMostOfScreen = screenRect.origin.y + screenRect.size.height; 1.185 + CGFloat xMost = rect.origin.x + rect.size.width; 1.186 + CGFloat yMost = rect.origin.y + rect.size.height; 1.187 + if (xMostOfScreen < xMost) { 1.188 + rect.origin.x -= xMost - xMostOfScreen; 1.189 + } 1.190 + if (yMostOfScreen < yMost) { 1.191 + rect.origin.y -= yMost - yMostOfScreen; 1.192 + } 1.193 + 1.194 + [self setFrame:rect display:[self isVisible]]; 1.195 +} 1.196 + 1.197 +@end