blob: 95aa88e6cd3861c70b67015f9b1a8c582a6ceb1a [file] [log] [blame]
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00001#import "SkAlertPrompt.h"
2#import "SkUIDetailViewController.h"
3#include "SampleApp.h"
4#include "SkApplication.h"
5#include "SkCGUtils.h"
6#include "SkData.h"
yangsu@google.com59870452011-08-02 13:20:22 +00007#include "SkOSMenu.h"
yangsu@google.comc5aeccd2011-07-17 14:42:08 +00008@implementation SkUIDetailViewController
yangsu@google.com59870452011-08-02 13:20:22 +00009@synthesize fPrintButton, fOptionsButton, fPopOverController, fOptionsController;
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000010
11//Overwritten from UIViewController
12- (void)viewDidLoad {
13 [super viewDidLoad];
14
15 fSkUIView = (SkUIView*)self.view;
yangsu@google.com59870452011-08-02 13:20:22 +000016
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000017 fWind = (SampleWindow*)fSkUIView.fWind;
yangsu@google.com59870452011-08-02 13:20:22 +000018 fSkUIView.fTitleItem = self.navigationItem;
19
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000020 [self createButtons];
yangsu@google.com59870452011-08-02 13:20:22 +000021
22 fOptionsController = [[SkOptionsTableViewController alloc]
23 initWithStyle:UITableViewStyleGrouped];
24 fSkUIView.fOptionsDelegate = fOptionsController;
25 [fOptionsController registerMenus:fWind->getMenus()];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000026}
27
28- (void)createButtons {
29 UIToolbar* toolbar = [[UIToolbar alloc]
yangsu@google.com59870452011-08-02 13:20:22 +000030 initWithFrame:CGRectMake(0, 0, 125, 45)];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000031 [toolbar setBarStyle: UIBarStyleBlackOpaque];
32
33 UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]
34 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
35 target:nil
36 action:nil];
37
yangsu@google.com59870452011-08-02 13:20:22 +000038 fOptionsButton = [[UIBarButtonItem alloc]
39 initWithTitle:@"Options"
40 style:UIBarButtonItemStylePlain
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000041 target:self
yangsu@google.com59870452011-08-02 13:20:22 +000042 action:@selector(presentOptions)];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000043 UIBarButtonItem* fixedSpace = [[UIBarButtonItem alloc]
44 initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
45 target:nil
46 action:nil];
47 fixedSpace.width = 10;
48
49 fPrintButton = [[UIBarButtonItem alloc]
50 initWithBarButtonSystemItem:UIBarButtonSystemItemAction
51 target:self
52 action:@selector(printContent)];
53 fPrintButton.style = UIBarButtonItemStylePlain;
54
yangsu@google.com59870452011-08-02 13:20:22 +000055 [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, fOptionsButton, fixedSpace, fPrintButton, nil]
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000056 animated:NO];
57
58 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
59 initWithCustomView:toolbar];
60 [flexibleSpace release];
61 [fixedSpace release];
62 [toolbar release];
63}
64
65- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
66 return YES; // Overriden to allow auto rotation for any direction
67}
68
69- (void)dealloc {
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000070 [fPrintButton release];
yangsu@google.com59870452011-08-02 13:20:22 +000071 [fOptionsButton release];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000072 [fPopOverController release];
yangsu@google.com59870452011-08-02 13:20:22 +000073 [fOptionsController release];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000074 application_term();
75 [super dealloc];
76}
77
78//Instance Methods
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000079- (void)populateRoot:(SkUIRootViewController*)rootVC {
80 for (int i = 0; i < fWind->sampleCount(); ++i) {
81 [rootVC addItem:[NSString stringWithUTF8String:fWind->getSampleTitle(i).c_str()]];
82 }
83}
84
85- (void)goToItem:(NSUInteger)index {
86 fWind->goToSample(index);
87}
88
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000089- (void)printContent {
90 UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
91 UIPrintInfo *printInfo = [UIPrintInfo printInfo];
92 printInfo.jobName = @"Skia iOS SampleApp";
93 printInfo.duplex = UIPrintInfoDuplexLongEdge;
94 printInfo.outputType = UIPrintInfoOutputGeneral;
95 fWind->saveToPdf();
yangsu@google.com2ba30c02011-07-19 15:17:44 +000096 [fSkUIView forceRedraw];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +000097 fData = fWind->getPDFData();
98 NSData* data = [NSData dataWithBytesNoCopy:(void*)fData->data() length:fData->size()];
99 controller.printInfo = printInfo;
100 controller.printingItem = data;
101 //Add ref because data pointer retains a pointer to data
102 fData->ref();
103
104 void (^SkCompletionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
105 ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
106 fData->unref();
107 if (!completed && error)
108 NSLog(@"FAILED! due to error in domain %@ with error code %u",
109 error.domain, error.code);
110 };
111
112 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
113 [controller presentFromBarButtonItem:fPrintButton animated:YES
114 completionHandler:SkCompletionHandler];
115 } else {
116 [controller presentAnimated:YES completionHandler:SkCompletionHandler];
117 }
118}
119
yangsu@google.com59870452011-08-02 13:20:22 +0000120- (void)presentOptions {
121 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
122 if (nil == fPopOverController) {
123 UINavigationController* navigation = [[UINavigationController alloc]
124 initWithRootViewController:fOptionsController];
125 navigation.navigationBar.topItem.title = @"Options";
126 fPopOverController = [[UIPopoverController alloc] initWithContentViewController:navigation];
127 [navigation release];
128 }
129
130 if (fPopOverController.isPopoverVisible)
131 [fPopOverController dismissPopoverAnimated:YES];
132 else
133 [fPopOverController presentPopoverFromBarButtonItem:fOptionsButton
134 permittedArrowDirections:UIPopoverArrowDirectionAny
135 animated:YES];
136
137 } else {
138 UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
139 style:UIBarButtonItemStyleBordered
140 target:nil
141 action:nil];
142 self.navigationItem.backBarButtonItem = backButton;
143 [backButton release];
144 [self.navigationController pushViewController:fOptionsController animated:YES];
145 self.navigationController.navigationBar.topItem.title = @"Options";
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000146 }
147}
yangsu@google.com59870452011-08-02 13:20:22 +0000148
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000149//Popover Management
150- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
yangsu@google.com59870452011-08-02 13:20:22 +0000151 [self.navigationItem setLeftBarButtonItem:barButtonItem animated:NO];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000152}
153
154- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
yangsu@google.com59870452011-08-02 13:20:22 +0000155 [self.navigationItem setLeftBarButtonItem:nil animated:NO];
yangsu@google.comc5aeccd2011-07-17 14:42:08 +0000156}
157
158@end