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