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" |
| 4 | #include "SkApplication.h" |
| 5 | #include "SkCGUtils.h" |
| 6 | #include "SkData.h" |
| 7 | @implementation SkUIDetailViewController |
| 8 | @synthesize fNavigationBar, fPrintButton, fCycleButton, fPopOverController; |
| 9 | |
| 10 | //Overwritten from UIViewController |
| 11 | - (void)viewDidLoad { |
| 12 | [super viewDidLoad]; |
| 13 | |
| 14 | fSkUIView = (SkUIView*)self.view; |
| 15 | fWind = (SampleWindow*)fSkUIView.fWind; |
| 16 | fSkUIView.fTitleItem = fNavigationBar.topItem; |
| 17 | |
| 18 | [NSTimer scheduledTimerWithTimeInterval:0.001 target:self |
| 19 | selector:@selector(redraw) userInfo:nil |
| 20 | repeats:YES]; |
| 21 | [self createButtons]; |
| 22 | } |
| 23 | |
| 24 | - (void)createButtons { |
| 25 | UIToolbar* toolbar = [[UIToolbar alloc] |
| 26 | initWithFrame:CGRectMake(0, 0, 150, 45)]; |
| 27 | [toolbar setBarStyle: UIBarStyleBlackOpaque]; |
| 28 | |
| 29 | UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] |
| 30 | initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace |
| 31 | target:nil |
| 32 | action:nil]; |
| 33 | |
| 34 | fCycleButton = [[UIBarButtonItem alloc] |
| 35 | initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh |
| 36 | target:self |
| 37 | action:@selector(cycleDeviceType)]; |
| 38 | fCycleButton.style = UIBarButtonItemStylePlain; |
| 39 | |
| 40 | UIBarButtonItem* fixedSpace = [[UIBarButtonItem alloc] |
| 41 | initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace |
| 42 | target:nil |
| 43 | action:nil]; |
| 44 | fixedSpace.width = 10; |
| 45 | |
| 46 | fPrintButton = [[UIBarButtonItem alloc] |
| 47 | initWithBarButtonSystemItem:UIBarButtonSystemItemAction |
| 48 | target:self |
| 49 | action:@selector(printContent)]; |
| 50 | fPrintButton.style = UIBarButtonItemStylePlain; |
| 51 | |
| 52 | [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, fCycleButton, fixedSpace, fPrintButton, nil] |
| 53 | animated:NO]; |
| 54 | |
| 55 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] |
| 56 | initWithCustomView:toolbar]; |
| 57 | [flexibleSpace release]; |
| 58 | [fixedSpace release]; |
| 59 | [toolbar release]; |
| 60 | } |
| 61 | |
| 62 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
| 63 | return YES; // Overriden to allow auto rotation for any direction |
| 64 | } |
| 65 | |
| 66 | - (void)dealloc { |
| 67 | [fNavigationBar release]; |
| 68 | [fPrintButton release]; |
| 69 | [fCycleButton release]; |
| 70 | [fPopOverController release]; |
| 71 | application_term(); |
| 72 | [super dealloc]; |
| 73 | } |
| 74 | |
| 75 | //Instance Methods |
| 76 | - (void)redraw { |
| 77 | [self.view setNeedsDisplay]; |
| 78 | } |
| 79 | |
| 80 | - (void)populateRoot:(SkUIRootViewController*)rootVC { |
| 81 | for (int i = 0; i < fWind->sampleCount(); ++i) { |
| 82 | [rootVC addItem:[NSString stringWithUTF8String:fWind->getSampleTitle(i).c_str()]]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | - (void)goToItem:(NSUInteger)index { |
| 87 | fWind->goToSample(index); |
| 88 | } |
| 89 | |
| 90 | //UI actions |
| 91 | - (IBAction)usePipe:(id)sender { |
| 92 | //fWind->togglePipe(); |
| 93 | } |
| 94 | |
| 95 | - (void)printContent { |
| 96 | UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController]; |
| 97 | UIPrintInfo *printInfo = [UIPrintInfo printInfo]; |
| 98 | printInfo.jobName = @"Skia iOS SampleApp"; |
| 99 | printInfo.duplex = UIPrintInfoDuplexLongEdge; |
| 100 | printInfo.outputType = UIPrintInfoOutputGeneral; |
| 101 | fWind->saveToPdf(); |
| 102 | [self.view drawRect:self.view.bounds]; |
| 103 | fData = fWind->getPDFData(); |
| 104 | NSData* data = [NSData dataWithBytesNoCopy:(void*)fData->data() length:fData->size()]; |
| 105 | controller.printInfo = printInfo; |
| 106 | controller.printingItem = data; |
| 107 | //Add ref because data pointer retains a pointer to data |
| 108 | fData->ref(); |
| 109 | |
| 110 | void (^SkCompletionHandler)(UIPrintInteractionController *, BOOL, NSError *) = |
| 111 | ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) { |
| 112 | fData->unref(); |
| 113 | if (!completed && error) |
| 114 | NSLog(@"FAILED! due to error in domain %@ with error code %u", |
| 115 | error.domain, error.code); |
| 116 | }; |
| 117 | |
| 118 | if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { |
| 119 | [controller presentFromBarButtonItem:fPrintButton animated:YES |
| 120 | completionHandler:SkCompletionHandler]; |
| 121 | } else { |
| 122 | [controller presentAnimated:YES completionHandler:SkCompletionHandler]; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | - (IBAction)enterServerIP:(id)sender { |
| 127 | SkAlertPrompt *prompt = [[SkAlertPrompt alloc] initWithTitle:@"Enter Server IP:" |
| 128 | message:@"\n" |
| 129 | delegate:self |
| 130 | cancelButtonTitle:@"Cancel" |
| 131 | otherButtonTitles:@"Enter", nil]; |
| 132 | // show the dialog box |
| 133 | [prompt show]; |
| 134 | [prompt release]; |
| 135 | } |
| 136 | |
| 137 | - (void)cycleDeviceType { |
| 138 | fWind->toggleRendering(); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | - (void)presentActions { |
| 143 | if (!fPopOverController) { |
| 144 | SkOptionsTableViewController* controller = [[SkOptionsTableViewController alloc] |
| 145 | initWithStyle:UITableViewStyleGrouped]; |
| 146 | fPopOverController = [[UIPopoverController alloc] initWithContentViewController:controller]; |
| 147 | fPopOverController.popoverContentSize = CGSizeMake(500, 400); |
| 148 | [controller release]; |
| 149 | } |
| 150 | |
| 151 | if (fPopOverController.isPopoverVisible) |
| 152 | [fPopOverController dismissPopoverAnimated:YES]; |
| 153 | else |
| 154 | [fPopOverController presentPopoverFromBarButtonItem:fPrintButton |
| 155 | permittedArrowDirections:UIPopoverArrowDirectionAny |
| 156 | animated:YES]; |
| 157 | |
| 158 | } |
| 159 | */ |
| 160 | |
| 161 | // manage popup |
| 162 | - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex |
| 163 | { |
| 164 | if (buttonIndex != [alertView cancelButtonIndex]) |
| 165 | { |
| 166 | NSString *entered = [(SkAlertPrompt*)alertView enteredText]; |
| 167 | //fWind->setServerIP([entered UTF8String]); |
| 168 | } |
| 169 | } |
| 170 | //Popover Management |
| 171 | - (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem { |
| 172 | [fNavigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO]; |
| 173 | } |
| 174 | |
| 175 | - (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem { |
| 176 | [fNavigationBar.topItem setLeftBarButtonItem:nil animated:NO]; |
| 177 | } |
| 178 | |
| 179 | @end |