1#import "SkUIView.h"
2#include "SkCanvas.h"
3#include "SkCGUtils.h"
4@implementation SkUIView
5
6@synthesize fWind, fTitleItem, fOptionsDelegate;
7
8- (id)initWithDefaults {
9    fWind = NULL;
10    return self;
11}
12
13- (id)initWithCoder:(NSCoder*)coder {
14    if ((self = [super initWithCoder:coder])) {
15        self = [self initWithDefaults];
16        [self setUpWindow];
17    }
18    return self;
19}
20
21- (id)initWithFrame:(CGRect)frame {
22    if (self = [super initWithFrame:frame]) {
23        self = [self initWithDefaults];
24        [self setUpWindow];
25    }
26    return self;
27}
28
29- (void)setUpWindow {
30    if (NULL != fWind) {
31        fWind->setVisibleP(true);
32        fWind->resize(self.frame.size.width, self.frame.size.height,
33                      kN32_SkColorType);
34    }
35}
36
37- (void)dealloc {
38    delete fWind;
39    [fTitleItem release];
40    [super dealloc];
41}
42
43- (void)forceRedraw {
44    [self drawInRaster];
45}
46
47- (void)drawInRaster {
48    SkCanvas canvas(fWind->getBitmap());
49    fWind->draw(&canvas);
50    CGImageRef cgimage = SkCreateCGImageRef(fWind->getBitmap());
51    self.layer.contents = (id)cgimage;
52    CGImageRelease(cgimage);
53}
54
55//Gesture Handlers
56- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
57    for (UITouch *touch in touches) {
58        CGPoint loc = [touch locationInView:self];
59        fWind->handleClick(loc.x, loc.y, SkView::Click::kDown_State, touch);
60    }
61}
62
63- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
64    for (UITouch *touch in touches) {
65        CGPoint loc = [touch locationInView:self];
66        fWind->handleClick(loc.x, loc.y, SkView::Click::kMoved_State, touch);
67    }
68}
69
70- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
71    for (UITouch *touch in touches) {
72        CGPoint loc = [touch locationInView:self];
73        fWind->handleClick(loc.x, loc.y, SkView::Click::kUp_State, touch);
74    }
75}
76
77- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
78    for (UITouch *touch in touches) {
79        CGPoint loc = [touch locationInView:self];
80        fWind->handleClick(loc.x, loc.y, SkView::Click::kUp_State, touch);
81    }
82}
83
84///////////////////////////////////////////////////////////////////////////////
85
86- (void)setSkTitle:(const char *)title {
87    if (fTitleItem) {
88        fTitleItem.title = [NSString stringWithUTF8String:title];
89    }
90}
91
92- (BOOL)onHandleEvent:(const SkEvent&)evt {
93    return false;
94}
95
96- (void)getAttachmentInfo:(SkOSWindow::AttachmentInfo*)info {
97    // we don't have a GL context.
98    info->fSampleCount = 0;
99    info->fStencilBits = 0;
100}
101
102#include "SkOSMenu.h"
103- (void)onAddMenu:(const SkOSMenu*)menu {
104    [self.fOptionsDelegate view:self didAddMenu:menu];
105}
106- (void)onUpdateMenu:(SkOSMenu*)menu {
107    [self.fOptionsDelegate view:self didUpdateMenu:menu];
108}
109
110- (void)postInvalWithRect:(const SkIRect*)r {
111    [self performSelector:@selector(drawInRaster) withObject:nil afterDelay:0];
112    [self setNeedsDisplay];
113}
114
115@end
116