blob: fdaa2153da58d3db6e749a9c7f322add5ba9cd70 [file] [log] [blame]
yangsu@google.com2eff7e22011-06-24 15:57:30 +00001#import "SkUIDetailViewController.h"
2#import "SkApplication.h"
3#import "SkWindow.h"
4#import "SkCGUtils.h"
5
6@implementation SkUIDetailViewController
7@synthesize fNavigationBar, fPrintButton;
8
9- (void)viewDidLoad {
10 [super viewDidLoad];
11
12 fSkUIView = (SkUIView_shell*)self.view;
13 fSkUIView.fTitle = fNavigationBar.topItem;
14
15 application_init();
16 fWind = (SampleWindow*)create_sk_window(self.view);
17 CGSize s = self.view.bounds.size;
18 fWind->resize(s.width, s.height);
19 [fSkUIView setSkWindow:(SkOSWindow*)fWind];
20
21 [self initGestureRecognizers];
22 [NSTimer scheduledTimerWithTimeInterval:0.001 target:self
23 selector:@selector(redraw) userInfo:nil
24 repeats:YES];
25}
26
27- (void)dealloc {
28 [fNavigationBar release];
29 [fPrintButton release];
30 application_term();
31 delete fWind;
32 [super dealloc];
33}
34
35- (void)redraw {
36 [self.view setNeedsDisplay];
37}
38
39- (void)populateRoot:(SkUIRootViewController*)rootVC {
40 for (int i = 0; i < fWind->sampleCount(); ++i) {
41 [rootVC addItem:[NSString stringWithUTF8String:fWind->getSampleTitle(i).c_str()]];
42 }
43}
44
45- (void)goToItem:(NSUInteger)index {
46 fWind->goToSample(index);
47}
48
49- (IBAction)printContent:(id)sender {
50 UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
51 UIPrintInfo *printInfo = [UIPrintInfo printInfo];
52 printInfo.jobName = @"Skia iOS SampleApp";
53 printInfo.duplex = UIPrintInfoDuplexLongEdge;
54
55 printInfo.outputType = UIPrintInfoOutputGeneral;
56 fWind->saveToPdf();
57 [self.view drawRect:self.view.bounds];
58 fData = fWind->getPDFData();
59 NSData* data = [NSData dataWithBytesNoCopy:(void*)fData->data() length:fData->size()];
60 controller.showsPageRange = YES;
61 controller.printInfo = printInfo;
62 controller.printingItem = data;
63 //Add ref because data pointer retains a pointer to data
64 fData->ref();
65
66 void (^SkCompletionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
67 ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
68 fData->unref();
69 if (!completed && error)
70 NSLog(@"FAILED! due to error in domain %@ with error code %u",
71 error.domain, error.code);
72 };
73
74 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
75 [controller presentFromBarButtonItem:fPrintButton animated:YES
76 completionHandler:SkCompletionHandler];
77 } else {
78 [controller presentAnimated:YES completionHandler:SkCompletionHandler];
79 }
80}
81
82#pragma mark -
83#pragma mark Rotation support
84
85- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
86 return YES; // Overriden to allow auto rotation for any direction
87}
88
89#pragma mark -
90#pragma mark Managing the popover
91
92- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
93 // Add the popover button to the left navigation item.
94 [fNavigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
95}
96
97
98- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
99 // Remove the popover button.
100 [fNavigationBar.topItem setLeftBarButtonItem:nil animated:NO];
101}
102
103// Gestures
104- (void)initGestureRecognizers {
105 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]
106 initWithTarget:self
107 action:@selector(handleDoubleTapGesture:)];
108 doubleTap.numberOfTapsRequired = 2;
109 [self.view addGestureRecognizer:doubleTap];
110 [doubleTap release];
111
112 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]
113 initWithTarget:self
114 action:@selector(handlePanGesture:)];
115 [self.view addGestureRecognizer:pan];
116 [pan release];
117
118 UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]
119 initWithTarget:self
120 action:@selector(handlePinchGesture:)];
121 [self.view addGestureRecognizer:pinch];
122 [pinch release];
123
124 UISwipeGestureRecognizer *lswipe = [[UISwipeGestureRecognizer alloc]
125 initWithTarget:self
126 action:@selector(handleSwipeGesture:)];
127 lswipe.direction = UISwipeGestureRecognizerDirectionLeft;
128 [self.view addGestureRecognizer:lswipe];
129 [lswipe release];
130
131 UISwipeGestureRecognizer *rswipe = [[UISwipeGestureRecognizer alloc]
132 initWithTarget:self
133 action:@selector(handleSwipeGesture:)];
134 //Swipe direction default to right
135 [self.view addGestureRecognizer:rswipe];
136 [rswipe release];
137
138 UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]
139 initWithTarget:self
140 action:@selector(handleRotationGesture:)];
141 [self.view addGestureRecognizer:rotation];
142 [rotation release];
143}
144
145- (void)handleDoubleTapGesture:(UIGestureRecognizer *)sender {
146 [fSkUIView resetTransformations];
147}
148
149- (void)handlePanGesture:(UIPanGestureRecognizer *)sender {
150 CGPoint translate = [sender translationInView:self.view];
151 switch (sender.state) {
152 case UIGestureRecognizerStateBegan:
153 fInitialOffset = fSkUIView.fOffset;
154 fInitialCenter = fSkUIView.fCenter;
155 break;
156
157 case UIGestureRecognizerStateChanged:
158 fSkUIView.fOffset = CGPointMake(fInitialOffset.x + translate.x,
159 fInitialOffset.y + translate.y);
160 fSkUIView.fCenter = CGPointMake(fInitialCenter.x - translate.x,
161 fInitialCenter.y - translate.y);
162 break;
163 case UIGestureRecognizerStateEnded:
164 case UIGestureRecognizerStateCancelled:
165 case UIGestureRecognizerStateFailed:
166 break;
167 default:
168 break;
169 }
170}
171
172- (void)handlePinchGesture:(UIPinchGestureRecognizer *)sender {
173 switch (sender.state) {
174 case UIGestureRecognizerStateBegan:
175 fInitialScale = fSkUIView.fScale;
176 break;
177 case UIGestureRecognizerStateChanged:
178 fSkUIView.fScale = fInitialScale * [sender scale];
179 break;
180 case UIGestureRecognizerStateEnded:
181 case UIGestureRecognizerStateCancelled:
182 case UIGestureRecognizerStateFailed:
183 break;
184 default:
185 break;
186 }
187}
188
189- (void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender {
190 if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
191 fWind->previousSample();
192 }
193 else {
194 fWind->nextSample();
195 }
196}
197
198- (void)handleRotationGesture:(UIRotationGestureRecognizer *)sender {
199 switch (sender.state) {
200 case UIGestureRecognizerStateBegan:
201 fInitialRotation = fSkUIView.fRotation;
202 break;
203 case UIGestureRecognizerStateChanged:
204 fSkUIView.fRotation = fInitialRotation + [sender rotation] * 50.0;
205 break;
206 case UIGestureRecognizerStateEnded:
207 case UIGestureRecognizerStateCancelled:
208 case UIGestureRecognizerStateFailed:
209 break;
210 default:
211 break;
212 }
213}
214
215@end