1 /* xscreensaver, Copyright (c) 2012-2014 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 @"iSaverRunner57t.png"]];
62 UIButton *button = [[UIButton alloc] init];
63 [button setFrame: CGRectMake(0, 0, img.size.width/2, img.size.height/2)];
64 [button setBackgroundImage:img forState:UIControlStateNormal];
65 [button addTarget:self
66 action:@selector(titleTapped:)
67 forControlEvents:UIControlEventTouchUpInside];
68 self.navigationItem.rightBarButtonItem =
69 [[UIBarButtonItem alloc] initWithCustomView: button];
74 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectZero];
75 UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectZero];
76 [label1 setText: line1];
77 [label2 setText: line2];
78 [label1 setBackgroundColor:[UIColor clearColor]];
79 [label2 setBackgroundColor:[UIColor clearColor]];
81 [label1 setFont: [UIFont boldSystemFontOfSize: 17]];
82 [label2 setFont: [UIFont systemFontOfSize: 12]];
86 CGRect r1 = [label1 frame];
87 CGRect r2 = [label2 frame];
90 CGRect win = [self view].frame;
91 if (win.size.width > 414 && win.size.height > 414) { // iPad
92 [label1 setTextAlignment: NSTextAlignmentLeft];
93 [label2 setTextAlignment: NSTextAlignmentRight];
94 label2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
95 r3.size.width = win.size.width;
102 r3.size.width = win.size.width; // force it to be flush-left
103 [label1 setTextAlignment: NSTextAlignmentLeft];
104 [label2 setTextAlignment: NSTextAlignmentLeft];
105 r1.origin.y = -1; // make it fit in landscape
106 r2.origin.y = r1.origin.y + r1.size.height - 2;
107 r3.size.height = r1.size.height + r2.size.height;
109 v.autoresizingMask = UIViewAutoresizingFlexibleWidth;
110 [label1 setFrame:r1];
111 [label2 setFrame:r2];
114 [v addSubview:label1];
115 [v addSubview:label2];
117 // Default opacity looks bad.
118 [v setBackgroundColor:[[v backgroundColor] colorWithAlphaComponent:1]];
120 self.navigationItem.titleView = v;
124 win.size.height = 44; // #### This cannot possibly be right.
125 UISearchBar *search = [[UISearchBar alloc] initWithFrame:win];
126 search.delegate = self;
127 search.placeholder = @"Search...";
128 self.tableView.tableHeaderView = search;
130 // Dismiss the search field's keyboard as soon as we scroll.
132 if ([self.tableView respondsToSelector:@selector(keyboardDismissMode)])
133 [self.tableView setKeyboardDismissMode:
134 UIScrollViewKeyboardDismissModeOnDrag];
139 - (id)initWithNames:(NSArray *)_names descriptions:(NSDictionary *)_descs;
142 if (! self) return 0;
143 [self reload:_names descriptions:_descs search:nil];
149 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tv
151 int n = countof(list_by_letter);
152 NSMutableArray *a = [NSMutableArray arrayWithCapacity: n];
153 for (int i = 0; i < n; i++) {
154 if ([list_by_letter[i] count] == 0) // Omit empty letter sections.
157 s[0] = (i == 'Z'-'A'+1 ? '#' : i+'A');
159 [a addObject: [NSString stringWithCString:s
160 encoding:NSASCIIStringEncoding]];
166 /* Called when text is typed into the top search bar.
168 - (void)searchBar:(UISearchBar *)bar textDidChange:(NSString *)txt
170 [self reload:names descriptions:descriptions search:txt];
174 - (void) reload:(NSArray *)_names descriptions:(NSDictionary *)_descs
177 if (names != _names) {
178 if (names) [names release];
179 names = [_names retain];
181 if (_descs != descriptions) {
182 if (descriptions) [descriptions release];
183 descriptions = [_descs retain];
186 int n = countof(list_by_letter);
187 for (int i = 0; i < n; i++) {
188 list_by_letter[i] = [[NSMutableArray alloc] init];
191 for (NSString *name in names) {
193 // If we're searching, omit any items that don't have a match in the
194 // title or description.
196 BOOL matchp = (!search || [search length] == 0);
198 matchp = ([name rangeOfString:search
199 options:NSCaseInsensitiveSearch].location
203 NSString *desc = [descriptions objectForKey:name];
204 matchp = ([desc rangeOfString:search
205 options:NSCaseInsensitiveSearch].location
211 int index = ([name cStringUsingEncoding: NSASCIIStringEncoding])[0];
212 if (index >= 'a' && index <= 'z')
214 if (index >= 'A' && index <= 'Z')
218 [list_by_letter[index] addObject: name];
221 active_section_count = 0;
222 letter_sections = [[[NSMutableArray alloc] init] retain];
223 section_titles = [[[NSMutableArray alloc] init] retain];
224 for (int i = 0; i < n; i++) {
225 if ([list_by_letter[i] count] > 0) {
226 active_section_count++;
227 [letter_sections addObject: list_by_letter[i]];
229 [section_titles addObject: [NSString stringWithFormat: @"%c", i+'A']];
231 [section_titles addObject: @"#"];
234 [self.tableView reloadData];
238 - (NSString *)tableView:(UITableView *)tv
239 titleForHeaderInSection:(NSInteger)section
241 return [section_titles objectAtIndex: section];
244 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tv
246 return active_section_count;
250 - (NSInteger)tableView:(UITableView *)tv
251 numberOfRowsInSection:(NSInteger)section
253 return [[letter_sections objectAtIndex: section] count];
256 - (NSInteger)tableView:(UITableView *)tv
257 sectionForSectionIndexTitle:(NSString *)title
258 atIndex:(NSInteger) index
261 for (NSString *sectionTitle in section_titles) {
262 if ([sectionTitle isEqualToString: title])
270 - (UITableViewCell *)tableView:(UITableView *)tv
271 cellForRowAtIndexPath:(NSIndexPath *)ip
274 [[letter_sections objectAtIndex: [ip indexAtPosition: 0]]
275 objectAtIndex: [ip indexAtPosition: 1]];
276 NSString *desc = [descriptions objectForKey:title];
278 NSString *id = @"Cell";
279 UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:id];
281 cell = [[[UITableViewCell alloc]
282 initWithStyle: UITableViewCellStyleSubtitle
286 cell.textLabel.text = title;
287 cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
288 cell.detailTextLabel.text = desc;
294 /* Selecting a row launches the saver.
296 - (void)tableView:(UITableView *)tv
297 didSelectRowAtIndexPath:(NSIndexPath *)ip
299 UITableViewCell *cell = [tv cellForRowAtIndexPath: ip];
301 (SaverRunner *) [[UIApplication sharedApplication] delegate];
304 // Dismiss the search field's keyboard before launching a saver.
305 [self.tableView.tableHeaderView resignFirstResponder];
307 NSAssert ([s isKindOfClass:[SaverRunner class]], @"not a SaverRunner");
308 [s loadSaver: cell.textLabel.text];
311 /* Selecting a row's Disclosure Button opens the preferences.
313 - (void)tableView:(UITableView *)tv
314 accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)ip
316 UITableViewCell *cell = [tv cellForRowAtIndexPath: ip];
318 (SaverRunner *) [[UIApplication sharedApplication] delegate];
320 NSAssert ([s isKindOfClass:[SaverRunner class]], @"not a SaverRunner");
321 [s openPreferences: cell.textLabel.text];
325 - (void) scrollTo: (NSString *) name
330 for (NSArray *a in letter_sections) {
332 for (NSString *n in a) {
333 ok = [n isEqualToString: name];
341 NSIndexPath *ip = [NSIndexPath indexPathForRow: j inSection: i];
342 [self.tableView selectRowAtIndexPath:ip
344 scrollPosition: UITableViewScrollPositionMiddle];
349 /* We need this to respond to "shake" gestures
351 - (BOOL)canBecomeFirstResponder
356 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
360 - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
365 /* Shake means load a random screen saver.
367 - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
369 if (motion != UIEventSubtypeMotionShake)
371 NSMutableArray *a = [NSMutableArray arrayWithCapacity: 200];
372 for (NSArray *sec in letter_sections)
373 for (NSString *s in sec)
377 NSString *which = [a objectAtIndex: (random() % n)];
380 (SaverRunner *) [[UIApplication sharedApplication] delegate];
382 NSAssert ([s isKindOfClass:[SaverRunner class]], @"not a SaverRunner");
383 [self scrollTo: which];
384 [s loadSaver: which];
390 for (int i = 0; i < countof(list_by_letter); i++)
391 [list_by_letter[i] release];
392 [letter_sections release];
393 [section_titles release];
394 [descriptions release];
401 #endif // USE_IPHONE -- whole file