blob: f4976b56c43749cee86f6a99206c8ff718420b28 [file] [log] [blame]
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00001#import "SkAlertPrompt.h"
2#import "SkUIDetailViewController.h"
3#include "SampleApp.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00004#include "SkCGUtils.h"
5#include "SkData.h"
yangsu@google.com59870452011-08-02 13:20:22 +00006#include "SkOSMenu.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00007@implementation SkUIDetailViewController
yangsu@google.com59870452011-08-02 13:20:22 +00008@synthesize fPrintButton, fOptionsButton, fPopOverController, fOptionsController;
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00009
10//Overwritten from UIViewController
11- (void)viewDidLoad {
12 [super viewDidLoad];
13
14 fSkUIView = (SkUIView*)self.view;
yangsu@google.com59870452011-08-02 13:20:22 +000015
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000016 fWind = (SampleWindow*)fSkUIView.fWind;
yangsu@google.com59870452011-08-02 13:20:22 +000017 fSkUIView.fTitleItem = self.navigationItem;
18
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000019 [self createButtons];
yangsu@google.com59870452011-08-02 13:20:22 +000020
yangsu@google.comf3493f02011-08-08 15:12:05 +000021 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.com59870452011-08-02 13:20:22 +000033 fOptionsController = [[SkOptionsTableViewController alloc]
34 initWithStyle:UITableViewStyleGrouped];
35 fSkUIView.fOptionsDelegate = fOptionsController;
36 [fOptionsController registerMenus:fWind->getMenus()];
yangsu@google.comf3493f02011-08-08 15:12:05 +000037
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000038}
39
40- (void)createButtons {
41 UIToolbar* toolbar = [[UIToolbar alloc]
yangsu@google.com59870452011-08-02 13:20:22 +000042 initWithFrame:CGRectMake(0, 0, 125, 45)];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000043 [toolbar setBarStyle: UIBarStyleBlackOpaque];
44
45 UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]
46 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
47 target:nil
48 action:nil];
49
yangsu@google.com59870452011-08-02 13:20:22 +000050 fOptionsButton = [[UIBarButtonItem alloc]
51 initWithTitle:@"Options"
52 style:UIBarButtonItemStylePlain
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000053 target:self
yangsu@google.com59870452011-08-02 13:20:22 +000054 action:@selector(presentOptions)];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000055 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.com59870452011-08-02 13:20:22 +000067 [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, fOptionsButton, fixedSpace, fPrintButton, nil]
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000068 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.comf3493f02011-08-08 15:12:05 +000077- (void)handleSwipe:(UISwipeGestureRecognizer *)sender {
78 if (UISwipeGestureRecognizerDirectionRight == sender.direction)
79 fWind->previousSample();
80 else
81 fWind->nextSample();
82}
83
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000084- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
85 return YES; // Overriden to allow auto rotation for any direction
86}
87
88- (void)dealloc {
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000089 [fPrintButton release];
yangsu@google.com59870452011-08-02 13:20:22 +000090 [fOptionsButton release];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000091 [fPopOverController release];
yangsu@google.com59870452011-08-02 13:20:22 +000092 [fOptionsController release];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000093 [super dealloc];
94}
95
96//Instance Methods
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000097- (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.comc5aeccd2011-07-17 14:42:08 +0000107- (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.com2ba30c02011-07-19 15:17:44 +0000114 [fSkUIView forceRedraw];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000115 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.com59870452011-08-02 13:20:22 +0000138- (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.comc5aeccd2011-07-17 14:42:08 +0000164 }
165}
yangsu@google.com59870452011-08-02 13:20:22 +0000166
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000167//Popover Management
168- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
yangsu@google.com59870452011-08-02 13:20:22 +0000169 [self.navigationItem setLeftBarButtonItem:barButtonItem animated:NO];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000170}
171
172- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
yangsu@google.com59870452011-08-02 13:20:22 +0000173 [self.navigationItem setLeftBarButtonItem:nil animated:NO];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000174}
175
176@end