Skip to content

Commit

Permalink
Improved handling of differences between OS versions in text input.
Browse files Browse the repository at this point in the history
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10843 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
  • Loading branch information
Manolo Gouy authored and Manolo Gouy committed Aug 27, 2015
1 parent be81fa0 commit a3adc0c
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions src/Fl_cocoa.mm
Expand Up @@ -79,6 +79,7 @@
static void clipboard_check(void);
static unsigned make_current_counts = 0; // if > 0, then Fl_Window::make_current() can be called only once
static NSBitmapImageRep* rect_to_NSBitmapImageRep(Fl_Window *win, int x, int y, int w, int h);
static SEL inputContext_SEL = @selector(inputContext);

int fl_mac_os_version = Fl_X::calc_mac_os_version(); // the version number of the running Mac OS X (e.g., 100604 for 10.6.4)

Expand Down Expand Up @@ -2034,14 +2035,13 @@ static void q_set_window_title(NSWindow *nsw, const char * name, const char *mi
that implements the NSTextInputClient protocol to properly handle text input. It also implements the old NSTextInput
protocol to run with OS <= 10.4. The few NSTextInput protocol methods that differ in signature from the NSTextInputClient
protocol transmit the received message to the corresponding NSTextInputClient method.
If OS < 10.6, the FLView class implements differently its fl_handle_keydown_event method.
Keyboard input sends keyDown: and performKeyEquivalent: messages to myview. The latter occurs for keys such as
ForwardDelete, arrows and F1, and when the Ctrl or Cmd modifiers are used. Other key presses send keyDown: messages.
The keyDown: method calls [myview fl_handle_keydown_event:theEvent] (equivalent to [[myview inputContext] handleEvent:theEvent])
that triggers system processing of keyboard events.
The keyDown: method calls [[myview performSelector:inputContext_SEL] handleEvent:theEvent] that is equivalent to
[[myview inputContext] handleEvent:theEvent], and triggers system processing of keyboard events.
The performKeyEquivalent: method directly calls Fl::handle(FL_KEYBOARD, focus-window)
when the Ctrl or Cmd modifiers are used. If not, it also calls [myview fl_handle_keydown_event:theEvent].
when the Ctrl or Cmd modifiers are used. If not, it also calls [[myview inputContext] handleEvent:theEvent].
The performKeyEquivalent: method returns YES when the keystroke has been handled and NO otherwise, which allows
shortcuts of the system menu to be processed. Three sorts of messages are then sent back by the system to myview:
doCommandBySelector:, setMarkedText: and insertText:. All 3 messages eventually produce Fl::handle(FL_KEYBOARD, win) calls.
Expand Down Expand Up @@ -2073,9 +2073,8 @@ The notion of selected text (!= marked text) is monitored by the selectedRange f
selectedRange = NSMakeRange(100, 0); to indicate no text is selected. The setMarkedText: method does
selectedRange = NSMakeRange(100, newSelection.length); to indicate that this length of text is selected.
With OS <= 10.5, the fl_handle_keydown_event: method is implemented differently because neither the
inputContext nor the handleEvent: methods are available.
It becomes [[FLTextInputContext singleInstance] handleEvent:event].
With OS <= 10.5, the NSView class does not implement the inputContext message. The inputContext_SEL selector is
assigned inputContextBefore10_6 which returns [FLTextInputContext singleInstance].
Method +[FLTextInputContext singleInstance] returns an instance of class FLTextInputContext that possesses
a handleEvent: method. The class FLTextView implements the so-called view's "field editor". This editor is an instance
of the FLTextView class allocated by the -(id)[FLWindowDelegate windowWillReturnFieldEditor: toObject:] method.
Expand Down Expand Up @@ -2163,8 +2162,10 @@ @interface FLView : NSView <NSTextInput
NSInteger identifier;
NSRange selectedRange;
}
+ (void)initialize;
+ (void)prepareEtext:(NSString*)aString;
+ (void)concatEtext:(NSString*)aString;
- (id)inputContextBefore10_6;
- (id)initWithFrame:(NSRect)frameRect;
- (void)drawRect:(NSRect)rect;
- (BOOL)acceptsFirstResponder;
Expand All @@ -2191,7 +2192,6 @@ - (NSDragOperation)draggingUpdated:(id < NSDraggingInfo >)sender;
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender;
- (void)draggingExited:(id < NSDraggingInfo >)sender;
- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal;
- (BOOL)fl_handle_keydown_event:(NSEvent*)event;
#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
- (void)insertText:(id)aString replacementRange:(NSRange)replacementRange;
- (void)setMarkedText:(id)aString selectedRange:(NSRange)newSelection replacementRange:(NSRange)replacementRange;
Expand All @@ -2202,6 +2202,14 @@ - (NSInteger)windowLevel;
@end

@implementation FLView
+ (void)initialize
{
if (fl_mac_os_version < 100600) inputContext_SEL = @selector(inputContextBefore10_6);
}
- (id)inputContextBefore10_6
{
return [FLTextInputContext singleInstance];
}
- (id)initWithFrame:(NSRect)frameRect
{
static NSInteger counter = 0;
Expand Down Expand Up @@ -2249,7 +2257,7 @@ - (BOOL)performKeyEquivalent:(NSEvent*)theEvent
else {
in_key_event = YES;
need_handle = NO;
handled = [self fl_handle_keydown_event:theEvent];
handled = [[self performSelector:inputContext_SEL] handleEvent:theEvent];
if (need_handle) handled = Fl::handle(FL_KEYBOARD, w);
in_key_event = NO;
}
Expand Down Expand Up @@ -2322,7 +2330,7 @@ - (void)keyDown:(NSEvent *)theEvent {
[FLView prepareEtext:[theEvent characters]];
} else {
need_handle = NO;
[self fl_handle_keydown_event:theEvent];
[[self performSelector:inputContext_SEL] handleEvent:theEvent];
}
if (need_handle) Fl::handle(FL_KEYBOARD, window);
in_key_event = NO;
Expand Down Expand Up @@ -2451,13 +2459,6 @@ - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
return NSDragOperationGeneric;
}

- (BOOL)fl_handle_keydown_event:(NSEvent*)event {
return fl_mac_os_version >= 100600 ?
[[self performSelector:@selector(inputContext)] handleEvent:event]
:
[[FLTextInputContext singleInstance] handleEvent:event];
}

+ (void)prepareEtext:(NSString*)aString {
// fills Fl::e_text with UTF-8 encoded aString using an adequate memory allocation
static char *received_utf8 = NULL;
Expand Down

0 comments on commit a3adc0c

Please sign in to comment.