source: gs2-extensions/iOs-3.x/trunk/Classes/GreenstoneApplication.mm@ 22603

Last change on this file since 22603 was 22603, checked in by davidb, 14 years ago

Initial cut as iOs 3.x version of Greenstone2.app

File size: 7.7 KB
Line 
1#import <Foundation/NSDictionary.h>
2#import <UIKit/CDStructures.h>
3#import <UIKit/UITable.h>
4#import <UIKit/UIHardware.h>
5
6#import "GreenstoneApplication.h"
7#import "GreenstoneButtonBar.h"
8#import "SimpleWebView.h"
9
10#import "GreenstoneAPI.h"
11
12static GreenstoneApplication* _toplevelApp = nil;
13static UITransitionView* _transView = nil;
14static SimpleWebView* _webView = nil;
15static GreenstoneAPI* _greenstoneAPI = nil;
16
17static NSString* _inCollection = nil;
18static NSMutableDictionary* _collectionToRowId = nil;
19
20@implementation GreenstoneApplication
21
22+(GreenstoneApplication*) toplevelApp {
23 return (_toplevelApp);
24}
25
26+(GreenstoneAPI*) greenstoneAPI {
27 return (_greenstoneAPI);
28}
29
30+(CGRect)workAreaRect
31{
32 CGRect screen_rect = [UIHardware fullScreenApplicationContentRect];
33
34 int wa_x_dim = screen_rect.size.width;
35 int wa_y_dim = screen_rect.size.height - BOT_NAVBAR_HEIGHT;
36
37 CGRect wa_rect = CGRectMake(0.0f,0.0f,wa_x_dim,wa_y_dim);
38
39 return wa_rect;
40}
41
42
43+(void) setInCollection:(NSString*)collection
44{
45 NSLog(@"Setting in collection = %@",collection);
46 if (_inCollection != nil) {
47 // [_inCollection release]; // **** memory leak????
48 }
49 _inCollection = nil;
50
51 if (collection != nil) {
52 _inCollection = [[NSString alloc] initWithString:collection];
53 }
54}
55
56+(int) inCollectionRowId
57{
58 if (_inCollection == nil) {
59 return -1;
60 }
61
62 NSNumber* rowid_num = [_collectionToRowId objectForKey:_inCollection];
63 int rowid = [rowid_num intValue];
64
65 return rowid;
66}
67
68-(NSString*) languagePref
69{
70 int rowid = [GreenstoneApplication inCollectionRowId];
71
72 if (rowid>=0) {
73
74 PrefView* prefview = (PrefView*)[_prefCascadeView directEntry:rowid];
75 NSString* lang = [prefview languagePref];
76
77 return lang; // lang is allocated, make sure calling method does release
78 }
79 else {
80 return nil;
81 }
82}
83
84
85
86-(void) applicationDidFinishLaunching:(NSNotification *)aNotification
87{
88 _toplevelApp = self;
89 _greenstoneAPI = new GreenstoneAPI(self);
90
91 _inCollection = nil;
92 _collectionToRowId = nil;
93
94 if (!_greenstoneAPI->sanityCheck()) {
95 return;
96 }
97
98 CGRect screen_rect = [UIHardware fullScreenApplicationContentRect];
99 screen_rect.origin.x = screen_rect.origin.y = 0.0f;
100
101 _window = [[UIWindow alloc] initWithContentRect: screen_rect];
102
103 CGRect workarea_rect = [GreenstoneApplication workAreaRect];
104
105 // Set up top-level browsing and search menu CascadeViews
106 _browseCascadeView = [BrowseCascadeView alloc];
107 _browseCascadeView = [_browseCascadeView initWithFrame:workarea_rect withTitle: @"Browse collection:"];
108
109 _queryCascadeView = [QueryCascadeView alloc];
110 _queryCascadeView = [_queryCascadeView initWithFrame:workarea_rect withTitle: @"Search collection:"];
111
112 _prefCascadeView = [PrefCascadeView alloc];
113 _prefCascadeView = [_prefCascadeView initWithFrame:workarea_rect withTitle: @"Collection preferences:"];
114
115 NSMutableArray* collist = _greenstoneAPI->collectionList();
116
117 int collist_len = [collist count];
118 _collectionToRowId = [[NSMutableDictionary alloc] initWithCapacity:collist_len];
119
120
121 NSEnumerator* collist_enumerator = [collist objectEnumerator];
122 int i = 0;
123 NSString* colnamedir;
124 while (colnamedir = [collist_enumerator nextObject]) {
125 ColInfoResponse_t& colinfo = _greenstoneAPI->collectInfo(colnamedir);
126 NSString* colname = _greenstoneAPI->collectionName(colinfo);
127
128 [_browseCascadeView appendMenuItem:self withItem:colname withGID:colnamedir];
129 [_queryCascadeView appendMenuItem:self withItem:colname withGID:colnamedir];
130
131 [_prefCascadeView appendMenuItem:self withItem:colname withGID:colnamedir];
132
133 NSNumber* inum = [[NSNumber alloc] initWithInt: i];
134 [_collectionToRowId setObject:inum forKey:colnamedir];
135 [inum release]; // **** should probably add this in
136
137 i++;
138 }
139 [collist release];
140
141
142
143 if (_webView == nil) {
144 // setup Web browser view
145 _webView = [[SimpleWebView alloc] initWithFrame:workarea_rect];
146 }
147
148
149 if (_transView == nil) {
150 // Set up transition view
151 _transView = [[UITransitionView alloc] initWithFrame: workarea_rect];
152 }
153
154 // Set up button bar
155 int butbar_x_org = 0.0f;
156 int butbar_y_org = workarea_rect.size.height+1;
157 int butbar_x_dim = screen_rect.size.width;
158 int butbar_y_dim = BOT_NAVBAR_HEIGHT;
159
160 CGRect butbarrect = CGRectMake(butbar_x_org,butbar_y_org,butbar_x_dim,butbar_y_dim);
161 _butbar = [[GreenstoneButtonBar alloc] initInView:_window withFrame:butbarrect];
162
163 // Usual standard window stuff
164 [_window makeKeyAndVisible];
165
166 [_window addSubview: _transView ];
167 [_window addSubview: _butbar];
168
169 [_transView transition:0 toView:_browseCascadeView];
170}
171
172
173enum compassDirection { itNorth=0, itEast=1, itSouth=2, itWest=3};
174
175static int compass_degrees[4] = { 0, 90, 180, 270 };
176
177static int current_orientation = kOrientationVertical;
178static int current_compass_dir = itNorth;
179static int current_compass_degs = 0;
180
181-(void) deviceOrientationChanged:(struct __GSEvent *)event
182{
183 int new_orientation = [UIHardware deviceOrientation: YES];
184
185 int new_compass_dir = -1;
186
187 switch (new_orientation)
188 {
189 case kOrientationVertical:
190 new_compass_dir = itNorth;
191 break;
192
193 case kOrientationHorizontalLeft:
194 new_compass_dir = itEast;
195 break;
196
197 case kOrientationHorizontalRight:
198 new_compass_dir = itWest;
199 break;
200 case kOrientationVerticalUpsideDown:
201 new_compass_dir = itSouth;
202 break;
203 }
204
205 if (new_compass_dir>=0) {
206 int new_compass_degs = compass_degrees[new_compass_dir];
207 int degs_delta = current_compass_degs - new_compass_degs;
208
209 if (degs_delta != 0) {
210
211 current_orientation = new_orientation;
212 current_compass_degs = new_compass_degs;
213 current_compass_dir = new_compass_dir;
214
215 [_webView setTransform:CGAffineTransformIdentity];
216 [_webView setRotationBy:new_compass_degs];
217
218 }
219 }
220
221 [super deviceOrientationChanged:event];
222}
223
224
225
226-(void) buttonBarClicked:(id)sender
227{
228 int button = [sender tag];
229 int transStyle = UITransitionNextShiftUp;
230
231 int rowid = [GreenstoneApplication inCollectionRowId];
232
233 switch (button)
234 {
235 case GSBBBrowse:
236 if (rowid>=0) {
237 [_transView transition:transStyle toView:[_browseCascadeView directEntry:rowid]];
238 }
239 else {
240 [_transView transition:transStyle toView:_browseCascadeView];
241 }
242 break;
243
244 case GSBBQuery:
245 if (rowid>=0) {
246 [_transView transition:transStyle toView:[_queryCascadeView directEntry:rowid]];
247 }
248 else {
249 [_transView transition:transStyle toView:_queryCascadeView];
250 }
251 break;
252
253 case GSBBPref:
254 if (rowid>=0) {
255 [_transView transition:transStyle toView:[_prefCascadeView directEntry:rowid]];
256 }
257 else {
258 [_transView transition:transStyle toView:_prefCascadeView];
259 }
260 break;
261
262 default:
263 NSLog(@"Unrecognised button bar click: %d",button);
264 break;
265 }
266}
267
268
269+(void) transitionView:(int)eventNum fromView:(UIView*)from toView:(UIView*)to
270{
271 [_transView transition:eventNum fromView:from toView:to];
272}
273
274+(void) startWebViewBusy
275{
276 // cerr << "**** start busy" << endl;
277 // stub for providing "busy" cue (e.g. busy spinner, or HUD)
278}
279
280+(void) stopWebViewBusy
281{
282 // cerr << "**** end busy" << endl;
283 // stub for providing "busy" cue (e.g. busy spinner, or HUD)
284}
285
286+(void) loadURL:(NSURL*)url
287{
288 [_webView loadURL:url];
289}
290
291+(void) showWebView:(int)eventNum fromView:(UIView*)from withURL:(NSURL*)url
292{
293 [_webView loadURL:url];
294 [_webView setParent:from];
295 [_transView transition:eventNum fromView:from toView:_webView];
296}
297
298
299
300-(void) alertSheet:(UIActionSheet*)sheet buttonClicked:(int)button
301{
302 [sheet dismissAnimated:YES];
303 [sheet release];
304}
305
306
307-(void) dealloc
308{
309 [_browseCascadeView release];
310 [_queryCascadeView release];
311 [_prefCascadeView release];
312
313 [_window release];
314 [_butbar release];
315
316 [super dealloc];
317}
318
319
320@end
321
322
323
Note: See TracBrowser for help on using the repository browser.