ecere:gui/gfx:drivers/cocoa: Partial native Cocoa drivers implementation for Mac...
[sdk] / ecere / src / gui / drivers / cocoa / EcereView.m
1 #import <Cocoa/Cocoa.h>
2 #import "CocoaEcereBridge.h"
3 #import "EcereView.h"
4
5 @implementation EcereView
6 - (EcereView*)initWithEcereWindow:(EcereWindowRef)ecereWindow;
7
8     self = [super initWithFrame:NSZeroRect];
9     
10     lockCount = 0;
11     
12     if (self != nil)
13     { 
14         NSOpenGLPixelFormatAttribute attributes[] =
15         { 
16             NSOpenGLPFAWindow,
17             //NSOpenGLPFAAccelerated,
18             NSOpenGLPFADoubleBuffer,
19             //NSOpenGLPFAColorSize, 24,
20             NSOpenGLPFAAlphaSize, 8,
21             NSOpenGLPFADepthSize, 24,
22             NSOpenGLPFAMinimumPolicy,
23             // select a pixelformat which meets or
24             // exceeds these requirements
25             0
26         };
27         
28         _pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes] ;
29     
30         if (_pixelformat == nil)
31         { 
32             NSLog( @"No valid OpenGL pixel format" );
33             NSLog( @"matching attributes specified" );
34             exit(1);
35         }
36         
37         [self setEcereWindow:ecereWindow];
38         
39         _context = [[NSOpenGLContext alloc]
40             initWithFormat:_pixelformat shareContext:nil] ;
41     
42         if (_context == nil)
43         {
44             // Failed initialization
45             self = nil;
46             NSLog( @"No valid OpenGL" ) ;
47             exit(1);
48         }
49         else 
50         {
51             // ensure we are pointing to ourself as the Drawable
52             [_context setView:self];
53         }
54     }
55     
56     if (_context == nil) {
57         self = nil;
58     }
59     
60     return self;
61 }
62
63 - (void)Dealloc
64 {
65     [_context release];
66 }
67
68 - (NSOpenGLContext*)openGLContext
69
70     return _context;
71 }
72
73 - (void)lockFocus
74
75     // ensure we are ready to draw
76     lockCount++;
77     
78     [_context setView:self];
79     [super lockFocus];
80     
81     [self makeCurrentContext];
82     
83     printf("- (void)lockFocus(%i)\n", lockCount);
84 }
85
86 - (void)unlockFocus
87
88     lockCount--;
89
90     [self flushOpenGLBuffer];
91     
92     [super unlockFocus];
93
94     printf("- (void)unlockFocus(%i)\n", lockCount);
95 }
96
97 - (void)makeCurrentContext
98 {
99     printf("- (void)makeCurrentContext\n");
100     
101     [_context makeCurrentContext];
102 }
103
104 - (void)flushOpenGLBuffer
105 {
106     printf("- (void)flushOpenGLBuffer\n");
107
108     [_context flushBuffer];
109 }
110
111 - (BOOL)isOpaque
112 {
113     return YES;
114 }
115
116 - drawRect:(NSRect)rect
117
118     // make us the current OpenGL context
119     //[_context makeCurrentContext];
120     printf("- drawRect:(NSRect)rect\n");
121     CocoaDispatch_Update([self ecereWindow]);
122 }
123
124 - (void)viewDidMoveToWindow
125 {
126     // Register with window for resize and move notification.
127     [[NSNotificationCenter defaultCenter]
128         addObserver:self
129         selector:@selector(windowResized:) 
130         name:NSWindowDidResizeNotification
131         object:[self window]];
132
133     [[NSNotificationCenter defaultCenter]
134         addObserver:self
135         selector:@selector(windowMoved:) 
136         name:NSWindowDidMoveNotification
137         object:[self window]];
138 }
139
140 - (void)windowResized:(NSNotification *)notification
141 {
142     [_context update];
143
144     NSRect rect = [[self window] frame];
145
146     CocoaDispatch_ExternalPosition([self ecereWindow], rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
147         
148     printf("- (void)windowResized:(NSNotification *)notification\n");
149 }
150
151 - (void)windowMoved:(NSNotification *)notification;
152 {
153     [_context update];
154
155     NSRect rect = [[self window] frame];
156
157     CocoaDispatch_ExternalPosition([self ecereWindow], rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
158             
159     printf("- (void)windowMoved:(NSNotification *)notification\n");
160 }
161
162 - (EcereWindowRef)ecereWindow
163 {
164     return _ecereWindow;
165 }
166
167 - setEcereWindow:(EcereWindowRef)ecereWindow
168 {
169     _ecereWindow = ecereWindow;    
170 }
171
172 - (void)mouseDown:(NSEvent *)event
173 {
174     NSPoint location = [NSEvent mouseLocation];
175     
176     CocoaKeyMod mod;
177     
178     CocoaDispatch_OnLeftButtonDown([self ecereWindow], location.x, location.y, mod);
179 }
180
181 - (CocoaColor)foreground
182 {
183     return _foreground;
184 }
185
186 - (void)setForeground:(CocoaColor)color
187 {
188     _foreground = color;
189 }
190
191 - (CocoaColor)background
192 {
193     return _background;
194 }
195
196 - (void)setBackground:(CocoaColor)color
197 {
198     _background = color;
199 }
200
201 @end
202
203