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