yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 1 | #import "SkUIDetailViewController.h" |
| 2 | #include "SampleApp.h" |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 3 | #include "SkCGUtils.h" |
| 4 | #include "SkData.h" |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 5 | #include "SkOSMenu.h" |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 6 | @implementation SkUIDetailViewController |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 7 | @synthesize fPrintButton, fOptionsButton, fPopOverController, fOptionsController; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 8 | |
| 9 | //Overwritten from UIViewController |
| 10 | - (void)viewDidLoad { |
| 11 | [super viewDidLoad]; |
| 12 | |
| 13 | fSkUIView = (SkUIView*)self.view; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 14 | |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 15 | fWind = (SampleWindow*)fSkUIView.fWind; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 16 | fSkUIView.fTitleItem = self.navigationItem; |
| 17 | |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 18 | [self createButtons]; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 19 | |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 20 | 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.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 32 | fOptionsController = [[SkOptionsTableViewController alloc] |
| 33 | initWithStyle:UITableViewStyleGrouped]; |
| 34 | fSkUIView.fOptionsDelegate = fOptionsController; |
| 35 | [fOptionsController registerMenus:fWind->getMenus()]; |
yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 36 | |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | - (void)createButtons { |
| 40 | UIToolbar* toolbar = [[UIToolbar alloc] |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 41 | initWithFrame:CGRectMake(0, 0, 125, 45)]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 42 | [toolbar setBarStyle: UIBarStyleBlackOpaque]; |
| 43 | |
| 44 | UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] |
| 45 | initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace |
| 46 | target:nil |
| 47 | action:nil]; |
| 48 | |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 49 | fOptionsButton = [[UIBarButtonItem alloc] |
| 50 | initWithTitle:@"Options" |
| 51 | style:UIBarButtonItemStylePlain |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 52 | target:self |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 53 | action:@selector(presentOptions)]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 54 | 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.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 66 | [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, fOptionsButton, fixedSpace, fPrintButton, nil] |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 67 | 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.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame] | 76 | - (void)handleSwipe:(UISwipeGestureRecognizer *)sender { |
| 77 | if (UISwipeGestureRecognizerDirectionRight == sender.direction) |
| 78 | fWind->previousSample(); |
| 79 | else |
| 80 | fWind->nextSample(); |
| 81 | } |
| 82 | |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 83 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
| 84 | return YES; // Overriden to allow auto rotation for any direction |
| 85 | } |
| 86 | |
| 87 | - (void)dealloc { |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 88 | [fPrintButton release]; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 89 | [fOptionsButton release]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 90 | [fPopOverController release]; |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 91 | [fOptionsController release]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 92 | [super dealloc]; |
| 93 | } |
| 94 | |
| 95 | //Instance Methods |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 96 | - (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.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 106 | - (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.com | 2ba30c0 | 2011-07-19 15:17:44 +0000 | [diff] [blame] | 113 | [fSkUIView forceRedraw]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 114 | 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.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 137 | - (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.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 165 | |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 166 | //Popover Management |
| 167 | - (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem { |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 168 | [self.navigationItem setLeftBarButtonItem:barButtonItem animated:NO]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | - (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem { |
yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 172 | [self.navigationItem setLeftBarButtonItem:nil animated:NO]; |
yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @end |