1 /* xscreensaver, Copyright (c) 2012-2013 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
11 * This implements the top-level screen-saver selection list in the iOS app.
14 #ifdef USE_IPHONE // whole file
17 #import "SaverListController.h"
18 #import "SaverRunner.h"
23 #define countof(x) (sizeof((x))/sizeof((*x)))
26 @implementation SaverListController
28 - (void) titleTapped:(id) sender
30 [[UIApplication sharedApplication]
31 openURL:[NSURL URLWithString:@"http://www.jwz.org/xscreensaver/"]];
37 // Extract the version number and release date from the version string.
38 // Here's an area where I kind of wish I had "Two Problems".
39 // I guess I could add custom key to the Info.plist for this.
41 NSArray *a = [[NSString stringWithCString: screensaver_id
42 encoding:NSASCIIStringEncoding]
43 componentsSeparatedByCharactersInSet:
45 characterSetWithCharactersInString:@" ()-"]];
46 NSString *vers = [a objectAtIndex: 3];
47 NSString *year = [a objectAtIndex: 7];
49 NSString *line1 = [@"XScreenSaver " stringByAppendingString: vers];
50 NSString *line2 = [@"\u00A9 " stringByAppendingString:
51 [year stringByAppendingString:
52 @" Jamie Zawinski <jwz@jwz.org>"]];
54 UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
56 // The "go to web page" button on the right
58 UIImage *img = [UIImage imageWithContentsOfFile:
59 [[[NSBundle mainBundle] bundlePath]
60 stringByAppendingPathComponent:
61 @"iSaverRunner29t.png"]];
62 UIBarButtonItem *button = [[[UIBarButtonItem alloc]
64 style: UIBarButtonItemStylePlain
66 action: @selector(titleTapped:)]
68 button.width = img.size.width;
69 self.navigationItem.rightBarButtonItem = button;
73 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectZero];
74 UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectZero];
75 [label1 setText: line1];
76 [label2 setText: line2];
77 [label1 setBackgroundColor:[UIColor clearColor]];
78 [label2 setBackgroundColor:[UIColor clearColor]];
80 [label1 setFont: [UIFont boldSystemFontOfSize: 17]];
81 [label2 setFont: [UIFont systemFontOfSize: 12]];
85 CGRect r1 = [label1 frame];
86 CGRect r2 = [label2 frame];
89 CGRect win = [self view].frame;
90 if (win.size.width > 320) { // iPad
91 [label1 setTextAlignment: UITextAlignmentLeft];
92 [label2 setTextAlignment: UITextAlignmentRight];
93 label2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
94 r3.size.width = win.size.width;
101 r3.size.width = 320; // force it to be flush-left
102 [label1 setTextAlignment: UITextAlignmentLeft];
103 [label2 setTextAlignment: UITextAlignmentLeft];
104 r1.origin.y = -1; // make it fit in landscape
105 r2.origin.y = r1.origin.y + r1.size.height - 2;
106 r3.size.height = r1.size.height + r2.size.height;
108 v.autoresizingMask = UIViewAutoresizingFlexibleWidth;
109 [label1 setFrame:r1];
110 [label2 setFrame:r2];
113 [v addSubview:label1];
114 [v addSubview:label2];
116 self.navigationItem.titleView = v;
120 - (id)initWithNames:(NSArray *)names descriptions:(NSDictionary *)descs;
123 if (! self) return 0;
124 [self reload:names descriptions:descs];
130 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tv
132 int n = countof(list_by_letter);
133 NSMutableArray *a = [NSMutableArray arrayWithCapacity: n];
134 for (int i = 0; i < n; i++) {
136 s[0] = (i == 'Z'-'A'+1 ? '#' : i+'A');
138 [a addObject: [NSString stringWithCString:s
139 encoding:NSASCIIStringEncoding]];
145 - (void) reload:(NSArray *)names descriptions:(NSDictionary *)descs
148 [descriptions release];
149 descriptions = [descs retain];
151 int n = countof(list_by_letter);
152 for (int i = 0; i < n; i++) {
153 list_by_letter[i] = [[NSMutableArray alloc] init];
156 for (NSString *name in names) {
157 int index = ([name cStringUsingEncoding: NSASCIIStringEncoding])[0];
158 if (index >= 'a' && index <= 'z')
160 if (index >= 'A' && index <= 'Z')
164 [list_by_letter[index] addObject: name];
167 active_section_count = 0;
168 letter_sections = [[[NSMutableArray alloc] init] retain];
169 section_titles = [[[NSMutableArray alloc] init] retain];
170 for (int i = 0; i < n; i++) {
171 if ([list_by_letter[i] count] > 0) {
172 active_section_count++;
173 [letter_sections addObject: list_by_letter[i]];
175 [section_titles addObject: [NSString stringWithFormat: @"%c", i+'A']];
177 [section_titles addObject: @"#"];
180 [self.tableView reloadData];
184 - (NSString *)tableView:(UITableView *)tv
185 titleForHeaderInSection:(NSInteger)section
187 return [section_titles objectAtIndex: section];
190 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tv
192 return active_section_count;
196 - (NSInteger)tableView:(UITableView *)tv
197 numberOfRowsInSection:(NSInteger)section
199 return [[letter_sections objectAtIndex: section] count];
202 - (NSInteger)tableView:(UITableView *)tv
203 sectionForSectionIndexTitle:(NSString *)title
204 atIndex:(NSInteger) index
207 for (NSString *sectionTitle in section_titles) {
208 if ([sectionTitle isEqualToString: title])
216 - (UITableViewCell *)tableView:(UITableView *)tv
217 cellForRowAtIndexPath:(NSIndexPath *)ip
220 [[letter_sections objectAtIndex: [ip indexAtPosition: 0]]
221 objectAtIndex: [ip indexAtPosition: 1]];
222 NSString *desc = [descriptions objectForKey:id];
224 UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier: id];
226 cell = [[[UITableViewCell alloc]
228 ? UITableViewCellStyleSubtitle
229 : UITableViewCellStyleDefault)
232 cell.textLabel.text = id;
234 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
236 cell.detailTextLabel.text = desc;
242 /* Selecting a row launches the saver.
244 - (void)tableView:(UITableView *)tv
245 didSelectRowAtIndexPath:(NSIndexPath *)ip
247 UITableViewCell *cell = [tv cellForRowAtIndexPath: ip];
249 (SaverRunner *) [[UIApplication sharedApplication] delegate];
251 NSAssert ([s isKindOfClass:[SaverRunner class]], @"not a SaverRunner");
252 [s loadSaver: cell.textLabel.text];
255 /* Selecting a row's Disclosure Button opens the preferences.
257 - (void)tableView:(UITableView *)tv
258 accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)ip
260 UITableViewCell *cell = [tv cellForRowAtIndexPath: ip];
262 (SaverRunner *) [[UIApplication sharedApplication] delegate];
264 NSAssert ([s isKindOfClass:[SaverRunner class]], @"not a SaverRunner");
265 [s openPreferences: cell.textLabel.text];
269 - (void) scrollTo: (NSString *) name
274 for (NSArray *a in letter_sections) {
276 for (NSString *n in a) {
277 ok = [n isEqualToString: name];
285 NSIndexPath *ip = [NSIndexPath indexPathForRow: j inSection: i];
286 [self.tableView selectRowAtIndexPath:ip
288 scrollPosition: UITableViewScrollPositionMiddle];
293 - (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)o
299 /* We need this to respond to "shake" gestures
301 - (BOOL)canBecomeFirstResponder
306 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
310 - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
315 /* Shake means load a random screen saver.
317 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
319 if (motion != UIEventSubtypeMotionShake)
321 NSMutableArray *a = [NSMutableArray arrayWithCapacity: 200];
322 for (NSArray *sec in letter_sections)
323 for (NSString *s in sec)
327 NSString *which = [a objectAtIndex: (random() % n)];
330 (SaverRunner *) [[UIApplication sharedApplication] delegate];
332 NSAssert ([s isKindOfClass:[SaverRunner class]], @"not a SaverRunner");
333 [self scrollTo: which];
334 [s loadSaver: which];
340 for (int i = 0; i < countof(list_by_letter); i++)
341 [list_by_letter[i] release];
342 [letter_sections release];
343 [section_titles release];
344 [descriptions release];
351 #endif // USE_IPHONE -- whole file