source: gs2-extensions/iOS-1.x/trunk/SimpleWebView.mm@ 22548

Last change on this file since 22548 was 22548, checked in by davidb, 12 years ago

Original version of Greenstone2 app for iPhone/iPod-touch

File size: 4.4 KB
Line 
1/* BSD License: Based on SimpleWebView by Sean Heber <sean@spiffytech.com>, J. Zdziarski */
2
3#include <sys/select.h>
4
5#import <UIKit/UIView-Geometry.h>
6#import <UIKit/UIView-Rendering.h>
7
8#import "GreenstoneApplication.h"
9#import "SimpleWebView.h"
10
11// ****** change name away from SimpleWebView
12
13
14@implementation MyWebView
15- (void) gestureEnded:(struct __GSEvent *)event
16{
17 [ super gestureEnded: event ];
18 [ _delegate gestureEnded: event ];
19}
20
21- (void) doubleTap:(struct __GSEvent *)event
22{
23 [ super doubleTap: event ];
24 [ _delegate doubleTap: event ];
25}
26
27
28@end
29
30
31@implementation SimpleWebView
32
33-(void)view: (UIView*)v didSetFrame:(CGRect)f
34{
35 if (v == webView) {
36 [ scroller setContentSize: f.size ];
37 }
38}
39
40-(void)view:(id)v didDrawInRect:(CGRect)f duration:(float)d
41{
42 if (v == webView) {
43 size = [ webView bounds ].size;
44 if (size.height != lastSize.height
45 || size.width != lastSize.width)
46 {
47 lastSize = size;
48 [ scroller setContentSize: size ];
49 }
50 }
51}
52
53- (void)gestureEnded:(struct __GSEvent *)event {
54 [ webView redrawScaledDocument ];
55 [ webView setNeedsDisplay ];
56 [ scroller setContentSize: [ webView bounds ].size ];
57}
58
59- (void)doubleTap:(struct __GSEvent *)event {
60 struct timeval tv;
61 tv.tv_sec = 2;
62 tv.tv_usec = 0;
63 select(NULL, NULL, NULL, NULL, &tv);
64 [ webView redrawScaledDocument ];
65 [ webView setNeedsDisplay ];
66 [ scroller setContentSize: [ webView bounds ].size ];
67}
68
69
70-(void)dealloc
71{
72 [ urlRequest release ];
73 [ webView release ];
74 [ scroller release ];
75
76 [ super dealloc ];
77}
78
79-(id)initWithFrame: (CGRect)frame
80{
81 return [self initWithFrame:frame withParent:nil];
82}
83
84
85-(id)initWithFrame: (CGRect)frame withParent:(UIView*)parent
86{
87 [ super initWithFrame: frame ];
88
89 _parent = parent;
90 urlRequest = nil;
91
92 int frame_x_dim = frame.size.width;
93 int frame_y_dim = frame.size.height;
94
95 int nav_x_org = 0.0f;
96 int nav_y_org = 0.0f;
97 int nav_x_dim = frame_x_dim;
98 int nav_y_dim = TOP_NAVBAR_HEIGHT;
99
100 CGRect navrect = CGRectMake(nav_x_org,nav_y_org,nav_x_dim,nav_y_dim);
101 UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame: navrect];
102
103 [navbar showButtonsWithLeftTitle:@"Back" rightTitle:nil leftBack:YES];
104 [navbar enableAnimation];
105 [navbar setBarStyle: 2]; // Dark, transparent // ****
106 [navbar setDelegate: self];
107
108 int scroll_x_org = 0.0f;
109 int scroll_y_org = 0.0f;
110 int scroll_x_dim = frame_x_dim;
111 int scroll_y_dim = frame_y_dim;
112
113 CGRect scrollrect = CGRectMake(scroll_x_org,scroll_y_org,scroll_x_dim,scroll_y_dim);
114
115 scroller = [ [ UIScroller alloc ] initWithFrame: scrollrect ];
116 [ scroller setScrollingEnabled: YES ];
117 [ scroller setAdjustForContentSizeChange: YES ];
118 [ scroller setClipsSubviews: NO ];
119 [ scroller setAllowsRubberBanding: YES ];
120 [ scroller setDelegate: self ];
121
122 webView = [ [ MyWebView alloc ] initWithFrame: [ scroller bounds ] ];
123 [ webView setTilingEnabled: YES ];
124 [ webView setTileSize: scrollrect.size ];
125 [ webView setAutoresizes: YES];
126 [ webView setDelegate: self];
127 [ webView setEnabledGestures: 0xFF ];
128 [ webView setSmoothsFonts: YES ];
129
130
131 [ self addSubview: scroller ];
132 [ scroller addSubview: webView ];
133
134 [self addSubview: navbar];
135
136 return self;
137}
138
139
140-(void) setParent:(UIView*)parent
141{
142 _parent = parent;
143}
144
145
146-(id) loadURL:(NSURL*)url
147{
148 CGPoint zero;
149 zero.x = zero.y = 0;
150 [scroller scrollPointVisibleAtTopLeft: zero];
151
152 if (urlRequest != nil) { // *****
153 [urlRequest release];
154 urlRequest = nil;
155 }
156
157 urlRequest = [[NSURLRequest requestWithURL:url] retain];
158 [webView loadRequest:urlRequest];
159}
160
161
162//***********
163//* Delegates
164//***********
165
166
167
168-(void) navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button
169{
170 switch (button)
171 {
172 case 1: // left
173 if (_parent != nil) {
174 // only do transition if there is a parent view to go back to
175 int transStyle = UITransitionBothShiftRight;
176 [GreenstoneApplication transitionView:transStyle fromView:self toView:_parent];
177 }
178
179 // need to think about deallocating self? // *****
180 break;
181
182 default:
183 NSLog(@"Unrecognised button = %d",button);
184 break;
185 }
186}
187
188@end
189
190
191
192
193
194
Note: See TracBrowser for help on using the repository browser.