1 #import <AppKit/NSEvent.h>
2 #import <AppKit/NSWindow.h>
3 #import <Foundation/NSString.h>
4 #include "cocoaevent.h"
6 NSWindow *get_native_window(CocoaWindow *);
7 CocoaWindow *lookup_window(NSWindow *);
9 CocoaWindow *last_window = 0;
11 void convert_event(NSEvent *ev, CocoaEvent *cev)
13 NSEventType type = [ev type];
14 cev->any.type = (CocoaEventType)type;
16 NSWindow *window = [ev window];
19 cev->any.window = lookup_window([ev window]);
20 last_window = cev->any.window;
23 if(type==NSLeftMouseDown || type==NSLeftMouseUp)
25 cev->button.button = 1;
26 cev->button.state = (type==NSLeftMouseDown);
28 else if(type==NSRightMouseDown || type==NSRightMouseUp)
30 cev->button.button = 3;
31 cev->button.state = (type==NSRightMouseDown);
33 else if(type==NSOtherMouseDown || type==NSOtherMouseUp)
35 cev->button.button = [ev buttonNumber];
36 cev->button.state = (type==NSOtherMouseDown);
38 else if(type==NSMouseMoved)
40 NSPoint pt = [ev locationInWindow];
41 if(!window && last_window)
43 /* If the pointer leaves the window, any subsequent MouseMoved events
44 are reported to a null window until the window is clicked. */
45 window = get_native_window(last_window);
46 pt = [window convertScreenToBase:pt];
47 cev->any.window = last_window;
52 else if(type==NSKeyDown)
54 NSString *chars = [[ev charactersIgnoringModifiers] uppercaseString];
56 cev->key.key = [chars characterAtIndex:0];
58 chars = [ev characters];
60 cev->key.character = [chars characterAtIndex:0];