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    /* comment out until we rev. this to use SkDocument
108
109    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();
115    [fSkUIView forceRedraw];
116    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    }
137     */
138}
139
140- (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";
166    }
167}
168
169//Popover Management
170- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
171    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:NO];
172}
173
174- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
175    [self.navigationItem setLeftBarButtonItem:nil animated:NO];
176}
177
178@end