]> git.tdb.fi Git - libs/gui.git/blob - source/graphics/cocoa/cocoaevent.m
Basic OS X support
[libs/gui.git] / source / graphics / cocoa / cocoaevent.m
1 #import <AppKit/NSEvent.h>
2 #import <AppKit/NSWindow.h>
3 #import <Foundation/NSString.h>
4 #include "cocoaevent.h"
5
6 NSWindow *get_native_window(CocoaWindow *);
7 CocoaWindow *lookup_window(NSWindow *);
8
9 CocoaWindow *last_window = 0;
10
11 void convert_event(NSEvent *ev, CocoaEvent *cev)
12 {
13         NSEventType type = [ev type];
14         cev->any.type = (CocoaEventType)type;
15
16         NSWindow *window = [ev window];
17         if(window)
18         {
19                 cev->any.window = lookup_window([ev window]);
20                 last_window = cev->any.window;
21         }
22
23         if(type==NSLeftMouseDown || type==NSLeftMouseUp)
24         {
25                 cev->button.button = 1;
26                 cev->button.state = (type==NSLeftMouseDown);
27         }
28         else if(type==NSRightMouseDown || type==NSRightMouseUp)
29         {
30                 cev->button.button = 3;
31                 cev->button.state = (type==NSRightMouseDown);
32         }
33         else if(type==NSOtherMouseDown || type==NSOtherMouseUp)
34         {
35                 cev->button.button = [ev buttonNumber];
36                 cev->button.state = (type==NSOtherMouseDown);
37         }
38         else if(type==NSMouseMoved)
39         {
40                 NSPoint pt = [ev locationInWindow];
41                 if(!window && last_window)
42                 {
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;
48                 }
49                 cev->motion.x = pt.x;
50                 cev->motion.y = pt.y;
51         }
52         else if(type==NSKeyDown)
53         {
54                 NSString *chars = [[ev charactersIgnoringModifiers] uppercaseString];
55                 if([chars length]==1)
56                         cev->key.key = [chars characterAtIndex:0];
57
58                 chars = [ev characters];
59                 if([chars length]==1)
60                         cev->key.character = [chars characterAtIndex:0];
61         }
62 }