aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/iOSSampleApp/Shared/SkUIDetailViewController.mm
blob: 76ef7e13fd268a0a1cc13e4be79722dd8f4ba301 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#import "SkUIDetailViewController.h"
#include "SampleApp.h"
#include "SkCGUtils.h"
#include "SkData.h"
#include "SkOSMenu.h"
@implementation SkUIDetailViewController
@synthesize fPrintButton, fOptionsButton, fPopOverController, fOptionsController;

//Overwritten from UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    fSkUIView = (SkUIView*)self.view;

    fWind = (SampleWindow*)fSkUIView.fWind;
    fSkUIView.fTitleItem = self.navigationItem;

    [self createButtons];

    UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(handleSwipe:)];
    [self.navigationController.navigationBar addGestureRecognizer:swipe];
    [swipe release];
    swipe = [[UISwipeGestureRecognizer alloc]
             initWithTarget:self
             action:@selector(handleSwipe:)];
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.navigationController.navigationBar addGestureRecognizer:swipe];
    [swipe release];

    fOptionsController = [[SkOptionsTableViewController alloc]
                          initWithStyle:UITableViewStyleGrouped];
    fSkUIView.fOptionsDelegate = fOptionsController;
    [fOptionsController registerMenus:fWind->getMenus()];

}

- (void)createButtons {
    UIToolbar* toolbar = [[UIToolbar alloc]
                          initWithFrame:CGRectMake(0, 0, 125, 45)];
    [toolbar setBarStyle: UIBarStyleBlackOpaque];

    UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                       target:nil
                                       action:nil];

    fOptionsButton = [[UIBarButtonItem alloc]
                    initWithTitle:@"Options"
                    style:UIBarButtonItemStylePlain
                    target:self
                    action:@selector(presentOptions)];
    UIBarButtonItem* fixedSpace = [[UIBarButtonItem alloc]
                                    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                    target:nil
                                    action:nil];
    fixedSpace.width = 10;

    fPrintButton = [[UIBarButtonItem alloc]
                    initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                    target:self
                    action:@selector(printContent)];
    fPrintButton.style = UIBarButtonItemStylePlain;

    [toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, fOptionsButton, fixedSpace, fPrintButton, nil]
             animated:NO];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                              initWithCustomView:toolbar];
    [flexibleSpace release];
    [fixedSpace release];
    [toolbar release];
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)sender {
    if (UISwipeGestureRecognizerDirectionRight == sender.direction)
        fWind->previousSample();
    else
        fWind->nextSample();
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES; // Overriden to allow auto rotation for any direction
}

- (void)dealloc {
    [fPrintButton release];
    [fOptionsButton release];
    [fPopOverController release];
    [fOptionsController release];
    [super dealloc];
}

//Instance Methods
- (void)populateRoot:(SkUIRootViewController*)rootVC {
    for (int i = 0; i < fWind->sampleCount(); ++i) {
        [rootVC addItem:[NSString stringWithUTF8String:fWind->getSampleTitle(i).c_str()]];
    }
}

- (void)goToItem:(NSUInteger)index {
    fWind->goToSample(index);
}

- (void)printContent {
    /* comment out until we rev. this to use SkDocument

    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.jobName = @"Skia iOS SampleApp";
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    printInfo.outputType = UIPrintInfoOutputGeneral;
    fWind->saveToPdf();
    [fSkUIView forceRedraw];
    fData = fWind->getPDFData();
    NSData* data = [NSData dataWithBytesNoCopy:(void*)fData->data() length:fData->size()];
    controller.printInfo = printInfo;
    controller.printingItem = data;
    //Add ref because data pointer retains a pointer to data
    fData->ref();

    void (^SkCompletionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
        fData->unref();
        if (!completed && error)
            NSLog(@"FAILED! due to error in domain %@ with error code %u",
                  error.domain, error.code);
    };

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [controller presentFromBarButtonItem:fPrintButton animated:YES
                        completionHandler:SkCompletionHandler];
    } else {
        [controller presentAnimated:YES completionHandler:SkCompletionHandler];
    }
     */
}

- (void)presentOptions {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if (nil == fPopOverController) {
            UINavigationController* navigation = [[UINavigationController alloc]
                                                  initWithRootViewController:fOptionsController];
            navigation.navigationBar.topItem.title = @"Options";
            fPopOverController = [[UIPopoverController alloc] initWithContentViewController:navigation];
            [navigation release];
        }

        if (fPopOverController.isPopoverVisible)
            [fPopOverController dismissPopoverAnimated:YES];
        else
            [fPopOverController presentPopoverFromBarButtonItem:fOptionsButton
                                       permittedArrowDirections:UIPopoverArrowDirectionAny
                                                       animated:YES];

    } else {
        UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                       style:UIBarButtonItemStyleBordered
                                                                      target:nil
                                                                      action:nil];
        self.navigationItem.backBarButtonItem = backButton;
        [backButton release];
        [self.navigationController pushViewController:fOptionsController animated:YES];
        self.navigationController.navigationBar.topItem.title = @"Options";
    }
}

//Popover Management
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    [self.navigationItem setLeftBarButtonItem:barButtonItem animated:NO];
}

- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    [self.navigationItem setLeftBarButtonItem:nil animated:NO];
}

@end