From ae400c2dbbd847160de48e0b882b1f91ca4c4e18 Mon Sep 17 00:00:00 2001 From: Bjorn Winckler Date: Sun, 9 Aug 2009 21:27:30 +0200 Subject: [PATCH] Add user default MMUseInlineIm Use inline IM (Vim draws marked text) when on, use old IM drawing code otherwise. --- src/MacVim/MMAppController.m | 3 +++ src/MacVim/MMAtsuiTextView.m | 6 ++--- src/MacVim/MMBackend.m | 6 ----- src/MacVim/MMTextView.m | 12 +++++----- src/MacVim/MMTextViewHelper.h | 1 + src/MacVim/MMTextViewHelper.m | 43 +++++++++++++++++++++++------------ src/MacVim/Miscellaneous.h | 6 +++++ src/MacVim/Miscellaneous.m | 3 +++ 8 files changed, 51 insertions(+), 29 deletions(-) diff --git a/src/MacVim/MMAppController.m b/src/MacVim/MMAppController.m index f85a925380..b60cfda560 100644 --- a/src/MacVim/MMAppController.m +++ b/src/MacVim/MMAppController.m @@ -209,6 +209,9 @@ fsEventCallback(ConstFSEventStreamRef streamRef, [NSNumber numberWithInt:0], MMPreloadCacheSizeKey, [NSNumber numberWithInt:0], MMLastWindowClosedBehaviorKey, [NSNumber numberWithBool:YES], MMLoadDefaultFontKey, +#ifdef INCLUDE_OLD_IM_CODE + [NSNumber numberWithBool:YES], MMUseInlineImKey, +#endif // INCLUDE_OLD_IM_CODE nil]; [[NSUserDefaults standardUserDefaults] registerDefaults:dict]; diff --git a/src/MacVim/MMAtsuiTextView.m b/src/MacVim/MMAtsuiTextView.m index 63e0b86b5e..bdc55ac1d4 100644 --- a/src/MacVim/MMAtsuiTextView.m +++ b/src/MacVim/MMAtsuiTextView.m @@ -518,8 +518,8 @@ defaultLineHeightForFont(NSFont *font) NSPoint pt = { insetSize.width, insetSize.height }; [contentImage compositeToPoint:pt operation:NSCompositeCopy]; -#ifdef USE_OLD_IM - if ([self hasMarkedText]) { +#ifdef INCLUDE_OLD_IM_CODE + if ([self hasMarkedText] && ![helper useInlineIm]) { int len = [[helper markedText] length]; int rows = 0; int cols = maxColumns - [helper preEditColumn]; @@ -575,7 +575,7 @@ defaultLineHeightForFont(NSFont *font) shape:MMInsertionPointVertical fraction:25]; } -#endif // USE_OLD_IM +#endif // INCLUDE_OLD_IM_CODE } - (BOOL) wantsDefaultClipping diff --git a/src/MacVim/MMBackend.m b/src/MacVim/MMBackend.m index c389ab71c2..dc849b68fe 100644 --- a/src/MacVim/MMBackend.m +++ b/src/MacVim/MMBackend.m @@ -56,11 +56,9 @@ vimmenu_T *menu_for_descriptor(NSArray *desc); static id evalExprCocoa(NSString * expr, NSString ** errstr); -#ifndef USE_OLD_IM extern void im_preedit_start_macvim(); extern void im_preedit_end_macvim(); extern void im_preedit_changed_macvim(char *preedit_string, int cursor_index); -#endif enum { MMBlinkStateNone = 0, @@ -2771,8 +2769,6 @@ static void netbeansReadCallback(CFSocketRef s, } -#ifndef USE_OLD_IM - - (void)handleMarkedText:(NSData *)data { const void *bytes = [data bytes]; @@ -2792,8 +2788,6 @@ static void netbeansReadCallback(CFSocketRef s, } } -#endif // USE_OLD_IM - @end // MMBackend (Private) diff --git a/src/MacVim/MMTextView.m b/src/MacVim/MMTextView.m index b950c4715e..cf2983da44 100644 --- a/src/MacVim/MMTextView.m +++ b/src/MacVim/MMTextView.m @@ -522,8 +522,8 @@ numInvertRects = 0; } -#ifdef USE_OLD_IM - if ([self hasMarkedText]) { +#ifdef INCLUDE_OLD_IM_CODE + if ([self hasMarkedText] && ![helper useInlineIm]) { shouldDrawInsertionPoint = YES; MMTextStorage *ts = (MMTextStorage*)[self textStorage]; NSSize inset = [self textContainerInset]; @@ -581,7 +581,7 @@ } } } -#endif +#endif // INCLUDE_OLD_IM_CODE if (shouldDrawInsertionPoint) { MMTextStorage *ts = (MMTextStorage*)[self textStorage]; @@ -591,9 +591,9 @@ ipRect.origin.x += [self textContainerOrigin].x; ipRect.origin.y += [self textContainerOrigin].y; -#ifdef USE_OLD_IM +#ifdef INCLUDE_OLD_IM_CODE // Draw insertion point inside marked text. - if ([self hasMarkedText]) { + if ([self hasMarkedText] && ![helper useInlineIm]) { NSFont *theFont = [[self markedTextAttributes] valueForKey:NSFontAttributeName]; if (theFont == [ts font]) @@ -605,7 +605,7 @@ ([helper imRange].location + [helper imRange].length); } -#endif +#endif // INCLUDE_OLD_IM_CODE if (MMInsertionPointHorizontal == insertionPointShape) { int frac = ([ts cellSize].height * insertionPointFraction + 99)/100; diff --git a/src/MacVim/MMTextViewHelper.h b/src/MacVim/MMTextViewHelper.h index 67b4847ac0..715dec6404 100644 --- a/src/MacVim/MMTextViewHelper.h +++ b/src/MacVim/MMTextViewHelper.h @@ -95,5 +95,6 @@ enum { - (NSRect)firstRectForCharacterRange:(NSRange)range; - (void)setImControl:(BOOL)enable; - (void)activateIm:(BOOL)enable; +- (BOOL)useInlineIm; @end diff --git a/src/MacVim/MMTextViewHelper.m b/src/MacVim/MMTextViewHelper.m index a4cefb3c64..0f8a32398e 100644 --- a/src/MacVim/MMTextViewHelper.m +++ b/src/MacVim/MMTextViewHelper.m @@ -185,9 +185,8 @@ KeyboardInputSourcesEqual(TISInputSourceRef a, TISInputSourceRef b) - (void)insertText:(id)string { if ([self hasMarkedText]) { - // Clear marked text [self sendMarkedText:nil position:0]; - + // NOTE: If this call is left out then the marked text isn't properly // erased when Return is used to accept the text. // The input manager only ever sets new marked text, it never actually @@ -627,7 +626,20 @@ KeyboardInputSourcesEqual(TISInputSourceRef a, TISInputSourceRef b) ASLogDebug(@"text='%@' range=%@", text, NSStringFromRange(range)); [self unmarkText]; -#ifdef USE_OLD_IM + if ([self useInlineIm]) { + if ([text isKindOfClass:[NSAttributedString class]]) + text = [text string]; + + if ([text length] > 0) { + markedRange = NSMakeRange(0, [text length]); + imRange = range; + } + + [self sendMarkedText:text position:range.location]; + return; + } + +#ifdef INCLUDE_OLD_IM_CODE if (!(text && [text length] > 0)) return; @@ -667,17 +679,7 @@ KeyboardInputSourcesEqual(TISInputSourceRef a, TISInputSourceRef b) } [textView setNeedsDisplay:YES]; -#else // USE_OLD_IM - if ([text isKindOfClass:[NSAttributedString class]]) - text = [text string]; - - if ([text length] > 0) { - markedRange = NSMakeRange(0, [text length]); - imRange = range; - } - - [self sendMarkedText:text position:range.location]; -#endif +#endif // INCLUDE_OLD_IM_CODE } - (void)unmarkText @@ -851,6 +853,16 @@ KeyboardInputSourcesEqual(TISInputSourceRef a, TISInputSourceRef b) #endif } +- (BOOL)useInlineIm +{ +#ifdef INCLUDE_OLD_IM_CODE + NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; + return [ud boolForKey:MMUseInlineImKey]; +#else + return YES; +#endif // INCLUDE_OLD_IM_CODE +} + @end // MMTextViewHelper @@ -1107,6 +1119,9 @@ KeyboardInputSourcesEqual(TISInputSourceRef a, TISInputSourceRef b) - (void)sendMarkedText:(NSString *)text position:(unsigned)pos { + if (![self useInlineIm]) + return; + NSMutableData *data = [NSMutableData data]; unsigned len = text == nil ? 0 : [text lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; diff --git a/src/MacVim/Miscellaneous.h b/src/MacVim/Miscellaneous.h index ffb6b20bc5..9b66ecdeb8 100644 --- a/src/MacVim/Miscellaneous.h +++ b/src/MacVim/Miscellaneous.h @@ -13,6 +13,9 @@ #import "MacVim.h" +// TODO: Remove this when the inline IM code has been tested +#define INCLUDE_OLD_IM_CODE + // NSUserDefaults keys extern NSString *MMTabMinWidthKey; @@ -47,6 +50,9 @@ extern NSString *MMVerticalSplitKey; extern NSString *MMPreloadCacheSizeKey; extern NSString *MMLastWindowClosedBehaviorKey; extern NSString *MMLoadDefaultFontKey; +#ifdef INCLUDE_OLD_IM_CODE +extern NSString *MMUseInlineImKey; +#endif // INCLUDE_OLD_IM_CODE // Enum for MMUntitledWindowKey diff --git a/src/MacVim/Miscellaneous.m b/src/MacVim/Miscellaneous.m index 22124d9d7b..5e60cf9b98 100644 --- a/src/MacVim/Miscellaneous.m +++ b/src/MacVim/Miscellaneous.m @@ -46,6 +46,9 @@ NSString *MMVerticalSplitKey = @"MMVerticalSplit"; NSString *MMPreloadCacheSizeKey = @"MMPreloadCacheSize"; NSString *MMLastWindowClosedBehaviorKey = @"MMLastWindowClosedBehavior"; NSString *MMLoadDefaultFontKey = @"MMLoadDefaultFont"; +#ifdef INCLUDE_OLD_IM_CODE +NSString *MMUseInlineImKey = @"MMUseInlineIm"; +#endif // INCLUDE_OLD_IM_CODE