blob: 0ab71d9b1ef32599f6ae654f3ab0e0a1e7f349f9 [file] [log] [blame]
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00001#import "SkUIDetailViewController.h"
2#include "SampleApp.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00003#include "SkCGUtils.h"
4#include "SkData.h"
yangsu@google.com59870452011-08-02 13:20:22 +00005#include "SkOSMenu.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00006@implementation SkUIDetailViewController
yangsu@google.com59870452011-08-02 13:20:22 +00007@synthesize fPrintButton, fOptionsButton, fPopOverController, fOptionsController;
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00008
9//Overwritten from UIViewController
10- (void)viewDidLoad {
11 [super viewDidLoad];
12
13 fSkUIView = (SkUIView*)self.view;
yangsu@google.com59870452011-08-02 13:20:22 +000014
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000015 fWind = (SampleWindow*)fSkUIView.fWind;
yangsu@google.com59870452011-08-02 13:20:22 +000016 fSkUIView.fTitleItem = self.navigationItem;
17
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000018 [self createButtons];
yangsu@google.com59870452011-08-02 13:20:22 +000019
yangsu@google.comf3493f02011-08-08 15:12:05 +000020 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.com59870452011-08-02 13:20:22 +000032 fOptionsController = [[SkOptionsTableViewController alloc]
33 initWithStyle:UITableViewStyleGrouped];
34 fSkUIView.fOptionsDelegate = fOptionsController;
35 [fOptionsController registerMenus:fWind->getMenus()];
yangsu@google.comf3493f02011-08-08 15:12:05 +000036
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000037}
38
39- (void)createButtons {
40 UIToolbar* toolbar = [[UIToolbar alloc]
yangsu@google.com59870452011-08-02 13:20:22 +000041 initWithFrame:CGRectMake(0, 0, 125, 45)];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000042 [toolbar setBarStyle: UIBarStyleBlackOpaque];
43
44 UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]
45 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
46 target:nil
47 action:nil];
48
yangsu@google.com59870452011-08-02 13:20:22 +000049 fOptionsButton = [[UIBarButtonItem alloc]
50 initWithTitle:@"Options"
51 style:UIBarButtonItemStylePlain
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000052 target:self
yangsu@google.com59870452011-08-02 13:20:22 +000053 action:@selector(presentOptions)];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000054 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.com59870452011-08-02 13:20:22 +000066 [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, fOptionsButton, fixedSpace, fPrintButton, nil]
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000067 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.comf3493f02011-08-08 15:12:05 +000076- (void)handleSwipe:(UISwipeGestureRecognizer *)sender {
77 if (UISwipeGestureRecognizerDirectionRight == sender.direction)
78 fWind->previousSample();
79 else
80 fWind->nextSample();
81}
82
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000083- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
84 return YES; // Overriden to allow auto rotation for any direction
85}
86
87- (void)dealloc {
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000088 [fPrintButton release];
yangsu@google.com59870452011-08-02 13:20:22 +000089 [fOptionsButton release];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000090 [fPopOverController release];
yangsu@google.com59870452011-08-02 13:20:22 +000091 [fOptionsController release];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000092 [super dealloc];
93}
94
95//Instance Methods
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000096- (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.comc5aeccd2011-07-17 14:42:08 +0000106- (void)printContent {
reedf0b17102014-10-22 13:06:00 -0700107 /* comment out until we rev. this to use SkDocument
108
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000109 UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
110 UIPrintInfo *printInfo = [UIPrintInfo printInfo];
111 printInfo.jobName = @"Skia iOS SampleApp";
112 printInfo.duplex = UIPrintInfoDuplexLongEdge;
113 printInfo.outputType = UIPrintInfoOutputGeneral;
114 fWind->saveToPdf();
yangsu@google.com2ba30c02011-07-19 15:17:44 +0000115 [fSkUIView forceRedraw];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000116 fData = fWind->getPDFData();
117 NSData* data = [NSData dataWithBytesNoCopy:(void*)fData->data() length:fData->size()];
118 controller.printInfo = printInfo;
119 controller.printingItem = data;
120 //Add ref because data pointer retains a pointer to data
121 fData->ref();
122
123 void (^SkCompletionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
124 ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
125 fData->unref();
126 if (!completed && error)
127 NSLog(@"FAILED! due to error in domain %@ with error code %u",
128 error.domain, error.code);
129 };
130
131 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
132 [controller presentFromBarButtonItem:fPrintButton animated:YES
133 completionHandler:SkCompletionHandler];
134 } else {
135 [controller presentAnimated:YES completionHandler:SkCompletionHandler];
136 }
reedf0b17102014-10-22 13:06:00 -0700137 */
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000138}
139
yangsu@google.com59870452011-08-02 13:20:22 +0000140- (void)presentOptions {
141 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
142 if (nil == fPopOverController) {
143 UINavigationController* navigation = [[UINavigationController alloc]
144 initWithRootViewController:fOptionsController];
145 navigation.navigationBar.topItem.title = @"Options";
146 fPopOverController = [[UIPopoverController alloc] initWithContentViewController:navigation];
147 [navigation release];
148 }
149
150 if (fPopOverController.isPopoverVisible)
151 [fPopOverController dismissPopoverAnimated:YES];
152 else
153 [fPopOverController presentPopoverFromBarButtonItem:fOptionsButton
154 permittedArrowDirections:UIPopoverArrowDirectionAny
155 animated:YES];
156
157 } else {
158 UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
159 style:UIBarButtonItemStyleBordered
160 target:nil
161 action:nil];
162 self.navigationItem.backBarButtonItem = backButton;
163 [backButton release];
164 [self.navigationController pushViewController:fOptionsController animated:YES];
165 self.navigationController.navigationBar.topItem.title = @"Options";
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000166 }
167}
yangsu@google.com59870452011-08-02 13:20:22 +0000168
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000169//Popover Management
170- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
yangsu@google.com59870452011-08-02 13:20:22 +0000171 [self.navigationItem setLeftBarButtonItem:barButtonItem animated:NO];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000172}
173
174- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
yangsu@google.com59870452011-08-02 13:20:22 +0000175 [self.navigationItem setLeftBarButtonItem:nil animated:NO];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000176}
177
178@end