blob: 7c2aef4d4fe74e0e49837dd5f76507731ef240ee [file] [log] [blame]
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00001#import "SkUIDetailViewController.h"
2#include "SampleApp.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00003#include "SkCGUtils.h"
4#include "SkData.h"
yangsu@google.com59870452011-08-02 13:20:22 +00005#include "SkOSMenu.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00006@implementation SkUIDetailViewController
yangsu@google.com59870452011-08-02 13:20:22 +00007@synthesize fPrintButton, fOptionsButton, fPopOverController, fOptionsController;
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00008
9//Overwritten from UIViewController
10- (void)viewDidLoad {
11 [super viewDidLoad];
12
13 fSkUIView = (SkUIView*)self.view;
yangsu@google.com59870452011-08-02 13:20:22 +000014
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000015 fWind = (SampleWindow*)fSkUIView.fWind;
yangsu@google.com59870452011-08-02 13:20:22 +000016 fSkUIView.fTitleItem = self.navigationItem;
17
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000018 [self createButtons];
yangsu@google.com59870452011-08-02 13:20:22 +000019
yangsu@google.comf3493f02011-08-08 15:12:05 +000020 UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc]
21 initWithTarget:self
22 action:@selector(handleSwipe:)];
23 [self.navigationController.navigationBar addGestureRecognizer:swipe];
24 [swipe release];
25 swipe = [[UISwipeGestureRecognizer alloc]
26 initWithTarget:self
27 action:@selector(handleSwipe:)];
28 swipe.direction = UISwipeGestureRecognizerDirectionLeft;
29 [self.navigationController.navigationBar addGestureRecognizer:swipe];
30 [swipe release];
31
yangsu@google.com59870452011-08-02 13:20:22 +000032 fOptionsController = [[SkOptionsTableViewController alloc]
33 initWithStyle:UITableViewStyleGrouped];
34 fSkUIView.fOptionsDelegate = fOptionsController;
35 [fOptionsController registerMenus:fWind->getMenus()];
yangsu@google.comf3493f02011-08-08 15:12:05 +000036
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000037}
38
39- (void)createButtons {
40 UIToolbar* toolbar = [[UIToolbar alloc]
yangsu@google.com59870452011-08-02 13:20:22 +000041 initWithFrame:CGRectMake(0, 0, 125, 45)];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000042 [toolbar setBarStyle: UIBarStyleBlackOpaque];
43
44 UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]
45 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
46 target:nil
47 action:nil];
48
yangsu@google.com59870452011-08-02 13:20:22 +000049 fOptionsButton = [[UIBarButtonItem alloc]
50 initWithTitle:@"Options"
51 style:UIBarButtonItemStylePlain
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000052 target:self
yangsu@google.com59870452011-08-02 13:20:22 +000053 action:@selector(presentOptions)];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000054 UIBarButtonItem* fixedSpace = [[UIBarButtonItem alloc]
55 initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
56 target:nil
57 action:nil];
58 fixedSpace.width = 10;
59
60 fPrintButton = [[UIBarButtonItem alloc]
61 initWithBarButtonSystemItem:UIBarButtonSystemItemAction
62 target:self
63 action:@selector(printContent)];
64 fPrintButton.style = UIBarButtonItemStylePlain;
65
yangsu@google.com59870452011-08-02 13:20:22 +000066 [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, fOptionsButton, fixedSpace, fPrintButton, nil]
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000067 animated:NO];
68
69 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
70 initWithCustomView:toolbar];
71 [flexibleSpace release];
72 [fixedSpace release];
73 [toolbar release];
74}
75
yangsu@google.comf3493f02011-08-08 15:12:05 +000076- (void)handleSwipe:(UISwipeGestureRecognizer *)sender {
77 if (UISwipeGestureRecognizerDirectionRight == sender.direction)
78 fWind->previousSample();
79 else
80 fWind->nextSample();
81}
82
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000083- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
84 return YES; // Overriden to allow auto rotation for any direction
85}
86
87- (void)dealloc {
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000088 [fPrintButton release];
yangsu@google.com59870452011-08-02 13:20:22 +000089 [fOptionsButton release];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000090 [fPopOverController release];
yangsu@google.com59870452011-08-02 13:20:22 +000091 [fOptionsController release];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000092 [super dealloc];
93}
94
95//Instance Methods
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000096- (void)populateRoot:(SkUIRootViewController*)rootVC {
97 for (int i = 0; i < fWind->sampleCount(); ++i) {
98 [rootVC addItem:[NSString stringWithUTF8String:fWind->getSampleTitle(i).c_str()]];
99 }
100}
101
102- (void)goToItem:(NSUInteger)index {
103 fWind->goToSample(index);
104}
105
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000106- (void)printContent {
107 UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
108 UIPrintInfo *printInfo = [UIPrintInfo printInfo];
109 printInfo.jobName = @"Skia iOS SampleApp";
110 printInfo.duplex = UIPrintInfoDuplexLongEdge;
111 printInfo.outputType = UIPrintInfoOutputGeneral;
112 fWind->saveToPdf();
yangsu@google.com2ba30c02011-07-19 15:17:44 +0000113 [fSkUIView forceRedraw];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000114 fData = fWind->getPDFData();
115 NSData* data = [NSData dataWithBytesNoCopy:(void*)fData->data() length:fData->size()];
116 controller.printInfo = printInfo;
117 controller.printingItem = data;
118 //Add ref because data pointer retains a pointer to data
119 fData->ref();
120
121 void (^SkCompletionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
122 ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
123 fData->unref();
124 if (!completed && error)
125 NSLog(@"FAILED! due to error in domain %@ with error code %u",
126 error.domain, error.code);
127 };
128
129 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
130 [controller presentFromBarButtonItem:fPrintButton animated:YES
131 completionHandler:SkCompletionHandler];
132 } else {
133 [controller presentAnimated:YES completionHandler:SkCompletionHandler];
134 }
135}
136
yangsu@google.com59870452011-08-02 13:20:22 +0000137- (void)presentOptions {
138 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
139 if (nil == fPopOverController) {
140 UINavigationController* navigation = [[UINavigationController alloc]
141 initWithRootViewController:fOptionsController];
142 navigation.navigationBar.topItem.title = @"Options";
143 fPopOverController = [[UIPopoverController alloc] initWithContentViewController:navigation];
144 [navigation release];
145 }
146
147 if (fPopOverController.isPopoverVisible)
148 [fPopOverController dismissPopoverAnimated:YES];
149 else
150 [fPopOverController presentPopoverFromBarButtonItem:fOptionsButton
151 permittedArrowDirections:UIPopoverArrowDirectionAny
152 animated:YES];
153
154 } else {
155 UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
156 style:UIBarButtonItemStyleBordered
157 target:nil
158 action:nil];
159 self.navigationItem.backBarButtonItem = backButton;
160 [backButton release];
161 [self.navigationController pushViewController:fOptionsController animated:YES];
162 self.navigationController.navigationBar.topItem.title = @"Options";
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000163 }
164}
yangsu@google.com59870452011-08-02 13:20:22 +0000165
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000166//Popover Management
167- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
yangsu@google.com59870452011-08-02 13:20:22 +0000168 [self.navigationItem setLeftBarButtonItem:barButtonItem animated:NO];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000169}
170
171- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
yangsu@google.com59870452011-08-02 13:20:22 +0000172 [self.navigationItem setLeftBarButtonItem:nil animated:NO];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000173}
174
175@end