aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/iOSSampleApp/Shared/SkUIDetailViewController.mm
blob: 17e0ec2df928308960a124af2de7d5e0adda4589 (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
#import "SkAlertPrompt.h"
#import "SkUIDetailViewController.h"
#include "SampleApp.h"
#include "SkApplication.h"
#include "SkCGUtils.h"
#include "SkData.h"
@implementation SkUIDetailViewController
@synthesize fNavigationBar, fPrintButton, fCycleButton, fPopOverController;

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

    fSkUIView = (SkUIView*)self.view;
    fWind = (SampleWindow*)fSkUIView.fWind;
    fSkUIView.fTitleItem = fNavigationBar.topItem;

    [NSTimer scheduledTimerWithTimeInterval:0.001 target:self
                                   selector:@selector(redraw) userInfo:nil
                                    repeats:YES];
    [self createButtons];
}

- (void)createButtons {
    UIToolbar* toolbar = [[UIToolbar alloc]
                          initWithFrame:CGRectMake(0, 0, 150, 45)];
    [toolbar setBarStyle: UIBarStyleBlackOpaque];
    
    UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                       target:nil
                                       action:nil];
    
    fCycleButton = [[UIBarButtonItem alloc]
                    initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                    target:self
                    action:@selector(cycleDeviceType)];
    fCycleButton.style = UIBarButtonItemStylePlain;
    
    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, fCycleButton, fixedSpace, fPrintButton, nil]
             animated:NO];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
                                              initWithCustomView:toolbar];
    [flexibleSpace release];
    [fixedSpace release];
    [toolbar release];
}

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

- (void)dealloc {
    [fNavigationBar release];
    [fPrintButton release];
    [fCycleButton release];
    [fPopOverController release];
    application_term();
    [super dealloc];
}

//Instance Methods
- (void)redraw {
    [self.view setNeedsDisplay];
}

- (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);
}

//UI actions
- (IBAction)usePipe:(id)sender {
    //fWind->togglePipe();
}

- (void)printContent {
    UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.jobName = @"Skia iOS SampleApp";
    printInfo.duplex = UIPrintInfoDuplexLongEdge;
    printInfo.outputType = UIPrintInfoOutputGeneral;
    fWind->saveToPdf();
    [self.view drawRect:self.view.bounds];
    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];
    }
}

- (IBAction)enterServerIP:(id)sender {
    SkAlertPrompt *prompt = [[SkAlertPrompt alloc] initWithTitle:@"Enter Server IP:"
                                                         message:@"\n"
                                                        delegate:self
                                               cancelButtonTitle:@"Cancel"
                                               otherButtonTitles:@"Enter", nil];
    // show the dialog box
    [prompt show];
    [prompt release];
}

- (void)cycleDeviceType {
    fWind->toggleRendering();
}

/*
- (void)presentActions {
    if (!fPopOverController) {
        SkOptionsTableViewController* controller = [[SkOptionsTableViewController alloc] 
                                                    initWithStyle:UITableViewStyleGrouped];
        fPopOverController = [[UIPopoverController alloc] initWithContentViewController:controller];
        fPopOverController.popoverContentSize = CGSizeMake(500, 400);
        [controller release];
    }
    
    if (fPopOverController.isPopoverVisible)
        [fPopOverController dismissPopoverAnimated:YES];
    else
        [fPopOverController presentPopoverFromBarButtonItem:fPrintButton 
                                   permittedArrowDirections:UIPopoverArrowDirectionAny 
                                                   animated:YES];

}
 */

// manage popup
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        NSString *entered = [(SkAlertPrompt*)alertView enteredText];
        //fWind->setServerIP([entered UTF8String]);
    }
}
//Popover Management
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem {
    [fNavigationBar.topItem setLeftBarButtonItem:barButtonItem animated:NO];
}

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

@end