Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/ui/ui-drv/cocoa/FractalView.m
blob: 6a3b4587aee04402334965b642b89d349ead181e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 *     XaoS, a fast portable realtime fractal zoomer 
 *                  Copyright (C) 1996 by
 *
 *      Jan Hubicka          (hubicka@paru.cas.cz)
 *      Thomas Marsh         (tmarsh@austin.ibm.com)
 *
 *    Cocoa Driver by J.B. Langston III (jb-langston@austin.rr.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#import "FractalView.h"
#ifdef VIDEATOR_SUPPORT
#import "VideatorProxy.h"
#endif

@interface NSObject(AppDelegateStuff)

- (void)keyPressed:(NSString *)key;

@end

@implementation FractalView

#pragma mark Initialization

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
#ifdef VIDEATOR_SUPPORT
        videatorProxy = [[VideatorProxy alloc] init];
#endif
    }
    return self;
}

- (void)dealloc {
#ifdef VIDEATOR_SUPPORT
    [videatorProxy release];
#endif
    [super dealloc];
}

#pragma mark Drawing

- (BOOL)isOpaque {
    return YES;
}

- (void)drawRect:(NSRect)rect {
    if (imageRep[currentBuffer]) {
        [imageRep[currentBuffer] drawInRect:[self bounds]];
    }
    
    if (messageText) {
        [messageText drawAtPoint:messageLocation withAttributes:[self textAttributes]];
        [messageText release];
        messageText = nil;
    }
    
#ifdef VIDEATOR_SUPPORT
    [videatorProxy sendImageRep:imageRep[currentBuffer]];
#endif    
}

#pragma mark Resize Handling

- (void)viewDidEndLiveResize {
    ui_resize();
}

#pragma mark Mouse Event Handling

- (void)calculateMouseLocationFromEvent:(NSEvent *)theEvent {
    NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    mouseX = mouseLoc.x;
    mouseY = [self bounds].size.height - mouseLoc.y;
}

- (void)mouseDown:(NSEvent *)theEvent {
    [self calculateMouseLocationFromEvent:theEvent];
    
    // Emulate 3 buttons based on modifier keys
    mouseScrollWheel = 0;
    if ([theEvent modifierFlags] & NSControlKeyMask) {
        mouseButton = BUTTON3;
    } else if ([theEvent modifierFlags] & NSShiftKeyMask) {
        mouseButton = BUTTON2;
    } else {
        mouseButton = BUTTON1;
    }    
}

- (void)mouseUp:(NSEvent *)theEvent {
    mouseButton = 0;
}

- (void)mouseDragged:(NSEvent *)theEvent {
    [self calculateMouseLocationFromEvent:theEvent];
}

- (void)rightMouseDown:(NSEvent *)theEvent {
    [self calculateMouseLocationFromEvent:theEvent];
    mouseScrollWheel = 0;
    rightMouseButton = BUTTON3;
}

- (void)rightMouseUp:(NSEvent *)theEvent {
    rightMouseButton = 0;
}

- (void)rightMouseDragged:(NSEvent *)theEvent {
    [self calculateMouseLocationFromEvent:theEvent];
}

- (void)otherMouseDown:(NSEvent *)theEvent {
    [self calculateMouseLocationFromEvent:theEvent];
    mouseScrollWheel = 0;
    otherMouseButton = BUTTON2;
}

- (void)otherMouseUp:(NSEvent *)theEvent {
    otherMouseButton = 0;
}

- (void)otherMouseDragged:(NSEvent *)theEvent {
    [self calculateMouseLocationFromEvent:theEvent];
}

- (void)scrollWheel:(NSEvent *)theEvent {
    // Only scroll if no mouse buttons are held
    if ((mouseButton | rightMouseButton | otherMouseButton) == 0) {
        mouseScrollWheel = BUTTON2;
        mouseX += [theEvent deltaX];
        mouseY += [theEvent deltaY];
    }
}

#pragma mark Keyboard Event Handling

- (void)flagsChanged:(NSEvent *)theEvent {
    // Emulate 3 buttons based on modifier keys 
    if (mouseButton) {
        if ([theEvent modifierFlags] & NSControlKeyMask) {
            mouseButton = BUTTON3;
        } else if ([theEvent modifierFlags] & NSShiftKeyMask) {
            mouseButton = BUTTON2;
        } else {
            mouseButton = BUTTON1;
        }
    }
}

- (void)keyDown:(NSEvent *)e {
    NSString *characters = [e characters];
    if ([characters length] == 0) return;
    
    unichar keyChar = [characters characterAtIndex:0];
    switch(keyChar) {
      case NSLeftArrowFunctionKey:
          keysDown |= 1;
          ui_key(UIKEY_LEFT);
          break;
      case NSRightArrowFunctionKey:
          keysDown |= 2;
          ui_key(UIKEY_RIGHT);
          break;
      case NSUpArrowFunctionKey:
          keysDown |= 4;
          ui_key(UIKEY_UP);
          break;
      case NSDownArrowFunctionKey:
          keysDown |= 8;
          ui_key(UIKEY_DOWN);
          break;
      case NSBackspaceCharacter:
          ui_key(UIKEY_BACKSPACE);
          break;
      case NSEndFunctionKey:
          ui_key(UIKEY_END);
          break;
      case '\033': // Escape
          ui_key(UIKEY_ESC);
          break;
      case NSHomeFunctionKey:
          ui_key(UIKEY_HOME);
          break;
      case NSPageDownFunctionKey:
          ui_key(UIKEY_PGDOWN);
          break;
      case NSPageUpFunctionKey:
          ui_key(UIKEY_PGUP);
          break;
      case NSTabCharacter:
          ui_key(UIKEY_TAB);
          break;
      default:
          ui_key(keyChar);
    }
}

- (void)keyUp:(NSEvent *)e {
    NSString *characters = [e characters];
    if ([characters length] == 0) return;
    
    unichar keyChar = [characters characterAtIndex:0];
    switch(keyChar)	{
      case NSLeftArrowFunctionKey:
          keysDown &= ~1;
          break;
      case NSRightArrowFunctionKey:
          keysDown &= ~2;
          break;
      case NSUpArrowFunctionKey:
          keysDown &= ~4;
          break;
      case NSDownArrowFunctionKey:
          keysDown &= ~8;
          break;
    }
}

#pragma mark Accessors

#ifdef VIDEATOR_SUPPORT
- (VideatorProxy *)videatorProxy {
    return videatorProxy;
}
#endif

- (void)getWidth:(int *)w height:(int *)h {
    NSRect bounds = [self bounds];
    *w = bounds.size.width;
    *h = bounds.size.height;
}

- (void)getMouseX:(int *)mx mouseY:(int *)my mouseButton:(int *)mb {
    *mx = mouseX;
    *my = mouseY;
    *mb = mouseButton | rightMouseButton | otherMouseButton | mouseScrollWheel;
}

- (void)getMouseX:(int *)mx mouseY:(int *)my mouseButton:(int *)mb keys:(int *)k
{
    [self getMouseX:mx mouseY:my mouseButton:mb];
    *k = keysDown;
}

#pragma mark Cursor

- (void)setCursorType:(int)type {
    cursorType = type;
    [[self window] invalidateCursorRectsForView:self];
}

- (void)resetCursorRects {
    /* BUG - cursor changes back to arrow when mouse is clicked 
     if (cursorType == VJMOUSE) {
     NSCursor *cursor = [[NSCursor alloc] initWithImage:[NSImage imageNamed:@"performanceCursor"] hotSpot:NSMakePoint(1.0,1.0)];
     [cursor setOnMouseEntered:YES];
     [self addCursorRect:[self bounds] cursor:cursor];
     [cursor release];
     } */
}


#pragma mark Text

- (NSDictionary *)textAttributes {
	
	NSMutableDictionary *attrsDictionary = [NSMutableDictionary dictionaryWithObject:[NSColor whiteColor] 
																			  forKey:NSForegroundColorAttributeName];
	
	NSShadow *dockStyleTextShadow = [[NSShadow alloc] init];
	[dockStyleTextShadow setShadowOffset:NSMakeSize(2, -2)];
	[dockStyleTextShadow setShadowBlurRadius:1];
	[dockStyleTextShadow setShadowColor:[NSColor blackColor]];
	[attrsDictionary setValue:[NSFont boldSystemFontOfSize:12.0] forKey:NSFontAttributeName];
	[attrsDictionary setValue:dockStyleTextShadow forKey:NSShadowAttributeName];
	[dockStyleTextShadow autorelease];
	
	return attrsDictionary;
}


- (void)printText:(CONST char *)text atX:(int)x y:(int)y {
    messageText = [[NSString stringWithUTF8String:text] retain];
    messageLocation = NSMakePoint(x + 15, [self bounds].size.height - y);
    [self setNeedsDisplay:YES];
}

#pragma mark Buffers

- (int)allocBuffer1:(char **)b1 buffer2:(char **)b2 {
    currentBuffer = 0;
    NSRect bounds = [self bounds];
    imageRep[0] = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                                          pixelsWide:bounds.size.width
                                                          pixelsHigh:bounds.size.height
                                                       bitsPerSample:8
                                                     samplesPerPixel:3
                                                            hasAlpha:NO
                                                            isPlanar:NO
                                                      colorSpaceName:NSDeviceRGBColorSpace
                                                         bytesPerRow:0
                                                        bitsPerPixel:32];
    
    *b1 = (char *)[imageRep[0] bitmapData];
    
    imageRep[1] = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
                                                          pixelsWide:bounds.size.width
                                                          pixelsHigh:bounds.size.height
                                                       bitsPerSample:8
                                                     samplesPerPixel:3
                                                            hasAlpha:NO
                                                            isPlanar:NO
                                                      colorSpaceName:NSDeviceRGBColorSpace
                                                         bytesPerRow:0
                                                        bitsPerPixel:32];
    
    *b2 = (char *)[imageRep[1] bitmapData];
    NSLog(@"bytesPerRow = %d", [imageRep[0] bytesPerRow]);
    return [imageRep[0] bytesPerRow];
}

- (void)freeBuffers {
    [imageRep[0] release];
    [imageRep[1] release];
}

- (void)flipBuffers {
    currentBuffer ^= 1;
}

@end