| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 1 | #import "SkAlertPrompt.h" |
| 2 | #import "SkUIDetailViewController.h" |
| 3 | #include "SampleApp.h" |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 4 | #include "SkCGUtils.h" |
| 5 | #include "SkData.h" |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 6 | #include "SkOSMenu.h" |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 7 | @implementation SkUIDetailViewController |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 8 | @synthesize fPrintButton, fOptionsButton, fPopOverController, fOptionsController; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 9 | |
| 10 | //Overwritten from UIViewController |
| 11 | - (void)viewDidLoad { |
| 12 | [super viewDidLoad]; |
| 13 | |
| 14 | fSkUIView = (SkUIView*)self.view; |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 15 | |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 16 | fWind = (SampleWindow*)fSkUIView.fWind; |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 17 | fSkUIView.fTitleItem = self.navigationItem; |
| 18 | |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 19 | [self createButtons]; |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 20 | |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 21 | UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] |
| 22 | initWithTarget:self |
| 23 | action:@selector(handleSwipe:)]; |
| 24 | [self.navigationController.navigationBar addGestureRecognizer:swipe]; |
| 25 | [swipe release]; |
| 26 | swipe = [[UISwipeGestureRecognizer alloc] |
| 27 | initWithTarget:self |
| 28 | action:@selector(handleSwipe:)]; |
| 29 | swipe.direction = UISwipeGestureRecognizerDirectionLeft; |
| 30 | [self.navigationController.navigationBar addGestureRecognizer:swipe]; |
| 31 | [swipe release]; |
| 32 | |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 33 | fOptionsController = [[SkOptionsTableViewController alloc] |
| 34 | initWithStyle:UITableViewStyleGrouped]; |
| 35 | fSkUIView.fOptionsDelegate = fOptionsController; |
| 36 | [fOptionsController registerMenus:fWind->getMenus()]; |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 37 | |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | - (void)createButtons { |
| 41 | UIToolbar* toolbar = [[UIToolbar alloc] |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 42 | initWithFrame:CGRectMake(0, 0, 125, 45)]; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 43 | [toolbar setBarStyle: UIBarStyleBlackOpaque]; |
| 44 | |
| 45 | UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] |
| 46 | initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace |
| 47 | target:nil |
| 48 | action:nil]; |
| 49 | |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 50 | fOptionsButton = [[UIBarButtonItem alloc] |
| 51 | initWithTitle:@"Options" |
| 52 | style:UIBarButtonItemStylePlain |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 53 | target:self |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 54 | action:@selector(presentOptions)]; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 55 | UIBarButtonItem* fixedSpace = [[UIBarButtonItem alloc] |
| 56 | initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace |
| 57 | target:nil |
| 58 | action:nil]; |
| 59 | fixedSpace.width = 10; |
| 60 | |
| 61 | fPrintButton = [[UIBarButtonItem alloc] |
| 62 | initWithBarButtonSystemItem:UIBarButtonSystemItemAction |
| 63 | target:self |
| 64 | action:@selector(printContent)]; |
| 65 | fPrintButton.style = UIBarButtonItemStylePlain; |
| 66 | |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 67 | [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, fOptionsButton, fixedSpace, fPrintButton, nil] |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 68 | animated:NO]; |
| 69 | |
| 70 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] |
| 71 | initWithCustomView:toolbar]; |
| 72 | [flexibleSpace release]; |
| 73 | [fixedSpace release]; |
| 74 | [toolbar release]; |
| 75 | } |
| 76 | |
| yangsu@google.com | f3493f0 | 2011-08-08 15:12:05 +0000 | [diff] [blame^] | 77 | - (void)handleSwipe:(UISwipeGestureRecognizer *)sender { |
| 78 | if (UISwipeGestureRecognizerDirectionRight == sender.direction) |
| 79 | fWind->previousSample(); |
| 80 | else |
| 81 | fWind->nextSample(); |
| 82 | } |
| 83 | |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 84 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
| 85 | return YES; // Overriden to allow auto rotation for any direction |
| 86 | } |
| 87 | |
| 88 | - (void)dealloc { |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 89 | [fPrintButton release]; |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 90 | [fOptionsButton release]; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 91 | [fPopOverController release]; |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 92 | [fOptionsController release]; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 93 | [super dealloc]; |
| 94 | } |
| 95 | |
| 96 | //Instance Methods |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 97 | - (void)populateRoot:(SkUIRootViewController*)rootVC { |
| 98 | for (int i = 0; i < fWind->sampleCount(); ++i) { |
| 99 | [rootVC addItem:[NSString stringWithUTF8String:fWind->getSampleTitle(i).c_str()]]; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | - (void)goToItem:(NSUInteger)index { |
| 104 | fWind->goToSample(index); |
| 105 | } |
| 106 | |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 107 | - (void)printContent { |
| 108 | UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController]; |
| 109 | UIPrintInfo *printInfo = [UIPrintInfo printInfo]; |
| 110 | printInfo.jobName = @"Skia iOS SampleApp"; |
| 111 | printInfo.duplex = UIPrintInfoDuplexLongEdge; |
| 112 | printInfo.outputType = UIPrintInfoOutputGeneral; |
| 113 | fWind->saveToPdf(); |
| yangsu@google.com | 2ba30c0 | 2011-07-19 15:17:44 +0000 | [diff] [blame] | 114 | [fSkUIView forceRedraw]; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 115 | fData = fWind->getPDFData(); |
| 116 | NSData* data = [NSData dataWithBytesNoCopy:(void*)fData->data() length:fData->size()]; |
| 117 | controller.printInfo = printInfo; |
| 118 | controller.printingItem = data; |
| 119 | //Add ref because data pointer retains a pointer to data |
| 120 | fData->ref(); |
| 121 | |
| 122 | void (^SkCompletionHandler)(UIPrintInteractionController *, BOOL, NSError *) = |
| 123 | ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) { |
| 124 | fData->unref(); |
| 125 | if (!completed && error) |
| 126 | NSLog(@"FAILED! due to error in domain %@ with error code %u", |
| 127 | error.domain, error.code); |
| 128 | }; |
| 129 | |
| 130 | if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
| 131 | [controller presentFromBarButtonItem:fPrintButton animated:YES |
| 132 | completionHandler:SkCompletionHandler]; |
| 133 | } else { |
| 134 | [controller presentAnimated:YES completionHandler:SkCompletionHandler]; |
| 135 | } |
| 136 | } |
| 137 | |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 138 | - (void)presentOptions { |
| 139 | if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
| 140 | if (nil == fPopOverController) { |
| 141 | UINavigationController* navigation = [[UINavigationController alloc] |
| 142 | initWithRootViewController:fOptionsController]; |
| 143 | navigation.navigationBar.topItem.title = @"Options"; |
| 144 | fPopOverController = [[UIPopoverController alloc] initWithContentViewController:navigation]; |
| 145 | [navigation release]; |
| 146 | } |
| 147 | |
| 148 | if (fPopOverController.isPopoverVisible) |
| 149 | [fPopOverController dismissPopoverAnimated:YES]; |
| 150 | else |
| 151 | [fPopOverController presentPopoverFromBarButtonItem:fOptionsButton |
| 152 | permittedArrowDirections:UIPopoverArrowDirectionAny |
| 153 | animated:YES]; |
| 154 | |
| 155 | } else { |
| 156 | UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" |
| 157 | style:UIBarButtonItemStyleBordered |
| 158 | target:nil |
| 159 | action:nil]; |
| 160 | self.navigationItem.backBarButtonItem = backButton; |
| 161 | [backButton release]; |
| 162 | [self.navigationController pushViewController:fOptionsController animated:YES]; |
| 163 | self.navigationController.navigationBar.topItem.title = @"Options"; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 166 | |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 167 | //Popover Management |
| 168 | - (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem { |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 169 | [self.navigationItem setLeftBarButtonItem:barButtonItem animated:NO]; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | - (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem { |
| yangsu@google.com | 5987045 | 2011-08-02 13:20:22 +0000 | [diff] [blame] | 173 | [self.navigationItem setLeftBarButtonItem:nil animated:NO]; |
| yangsu@google.com | c5aeccd | 2011-07-17 14:42:08 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @end |