5 // Created by Jesse MacFadyen on 10-02-03.
8 // Originally this code was developed my Michael Nachbaur
9 // Formerly -> PhoneGap :: UIControls.h
10 // Created by Michael Nachbaur on 13/04/09.
11 // Copyright 2009 Decaf Ninja Software. All rights reserved.
13 #import "NativeControls.h"
15 #import <QuartzCore/QuartzCore.h>
17 @implementation NativeControls
22 -(PGPlugin*) initWithWebView:(UIWebView*)theWebView
24 self = (NativeControls*)[super initWithWebView:theWebView];
27 tabBarItems = [[NSMutableDictionary alloc] initWithCapacity:5];
28 originalWebViewBounds = theWebView.bounds;
40 [toolBarTitle release];
41 [toolBarItems release];
52 * Create a native tab bar at either the top or the bottom of the display.
53 * @brief creates a tab bar
54 * @param arguments unused
55 * @param options unused
57 - (void)createTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
59 tabBar = [UITabBar new];
61 tabBar.delegate = self;
62 tabBar.multipleTouchEnabled = NO;
63 tabBar.autoresizesSubviews = YES;
65 tabBar.userInteractionEnabled = YES;
68 self.webView.superview.autoresizesSubviews = YES;
70 [ self.webView.superview addSubview:tabBar];
74 * Show the tab bar after its been created.
75 * @brief show the tab bar
76 * @param arguments unused
77 * @param options used to indicate options for where and how the tab bar should be placed
78 * - \c height integer indicating the height of the tab bar (default: \c 49)
79 * - \c position specifies whether the tab bar will be placed at the \c top or \c bottom of the screen (default: \c bottom)
81 - (void)showTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
84 [self createTabBar:nil withDict:nil];
86 // if we are calling this again when its shown, reset
91 CGFloat height = 0.0f;
94 // CGRect offsetRect = [ [UIApplication sharedApplication] statusBarFrame];
98 height = [[options objectForKey:@"height"] floatValue];
99 atBottom = [[options objectForKey:@"position"] isEqualToString:@"bottom"];
108 CGRect webViewBounds = originalWebViewBounds;
111 NSNotification* notif = [NSNotification notificationWithName:@"PGLayoutSubviewAdded" object:tabBar];
112 [[NSNotificationQueue defaultQueue] enqueueNotification:notif postingStyle: NSPostASAP];
116 tabBarBounds = CGRectMake(
117 webViewBounds.origin.x,
118 webViewBounds.origin.y + webViewBounds.size.height - height,
119 webViewBounds.size.width,
122 webViewBounds = CGRectMake(
123 webViewBounds.origin.x,
124 webViewBounds.origin.y,
125 webViewBounds.size.width,
126 webViewBounds.size.height - height
131 tabBarBounds = CGRectMake(
132 webViewBounds.origin.x,
133 webViewBounds.origin.y,
134 webViewBounds.size.width,
137 webViewBounds = CGRectMake(
138 webViewBounds.origin.x,
139 webViewBounds.origin.y + height,
140 webViewBounds.size.width,
141 webViewBounds.size.height - height
145 [tabBar setFrame:tabBarBounds];
148 [self.webView setFrame:webViewBounds];
153 * @brief hide the tab bar
154 * @param arguments unused
155 * @param options unused
157 - (void)hideTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
160 [self createTabBar:nil withDict:nil];
163 NSNotification* notif = [NSNotification notificationWithName:@"PGLayoutSubviewRemoved" object:tabBar];
164 [[NSNotificationQueue defaultQueue] enqueueNotification:notif postingStyle: NSPostASAP];
167 [self.webView setFrame:originalWebViewBounds];
171 * Create a new tab bar item for use on a previously created tab bar. Use ::showTabBarItems to show the new item on the tab bar.
173 * If the supplied image name is one of the labels listed below, then this method will construct a tab button
174 * using the standard system buttons. Note that if you use one of the system images, that the \c title you supply will be ignored.
175 * - <b>Tab Buttons</b>
177 * - tabButton:Favorites
178 * - tabButton:Featured
179 * - tabButton:TopRated
180 * - tabButton:Recents
181 * - tabButton:Contacts
182 * - tabButton:History
183 * - tabButton:Bookmarks
185 * - tabButton:Downloads
186 * - tabButton:MostRecent
187 * - tabButton:MostViewed
188 * @brief create a tab bar item
189 * @param arguments Parameters used to create the tab bar
190 * -# \c name internal name to refer to this tab by
191 * -# \c title title text to show on the tab, or null if no text should be shown
192 * -# \c image image filename or internal identifier to show, or null if now image should be shown
193 * -# \c tag unique number to be used as an internal reference to this button
194 * @param options Options for customizing the individual tab item
195 * - \c badge value to display in the optional circular badge on the item; if nil or unspecified, the badge will be hidden
197 - (void)createTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
200 [self createTabBar:nil withDict:nil];
202 NSString *name = [arguments objectAtIndex:0];
203 NSString *title = [arguments objectAtIndex:1];
204 NSString *imageName = [arguments objectAtIndex:2];
205 int tag = [[arguments objectAtIndex:3] intValue];
207 UITabBarItem *item = nil;
208 if ([imageName length] > 0) {
209 UIBarButtonSystemItem systemItem = -1;
210 if ([imageName isEqualToString:@"tabButton:More"]) systemItem = UITabBarSystemItemMore;
211 if ([imageName isEqualToString:@"tabButton:Favorites"]) systemItem = UITabBarSystemItemFavorites;
212 if ([imageName isEqualToString:@"tabButton:Featured"]) systemItem = UITabBarSystemItemFeatured;
213 if ([imageName isEqualToString:@"tabButton:TopRated"]) systemItem = UITabBarSystemItemTopRated;
214 if ([imageName isEqualToString:@"tabButton:Recents"]) systemItem = UITabBarSystemItemRecents;
215 if ([imageName isEqualToString:@"tabButton:Contacts"]) systemItem = UITabBarSystemItemContacts;
216 if ([imageName isEqualToString:@"tabButton:History"]) systemItem = UITabBarSystemItemHistory;
217 if ([imageName isEqualToString:@"tabButton:Bookmarks"]) systemItem = UITabBarSystemItemBookmarks;
218 if ([imageName isEqualToString:@"tabButton:Search"]) systemItem = UITabBarSystemItemSearch;
219 if ([imageName isEqualToString:@"tabButton:Downloads"]) systemItem = UITabBarSystemItemDownloads;
220 if ([imageName isEqualToString:@"tabButton:MostRecent"]) systemItem = UITabBarSystemItemMostRecent;
221 if ([imageName isEqualToString:@"tabButton:MostViewed"]) systemItem = UITabBarSystemItemMostViewed;
222 if (systemItem != -1)
223 item = [[UITabBarItem alloc] initWithTabBarSystemItem:systemItem tag:tag];
227 item = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imageName] tag:tag];
230 if ([options objectForKey:@"badge"])
231 item.badgeValue = [options objectForKey:@"badge"];
233 [tabBarItems setObject:item forKey:name];
239 * Update an existing tab bar item to change its badge value.
240 * @brief update the badge value on an existing tab bar item
241 * @param arguments Parameters used to identify the tab bar item to update
242 * -# \c name internal name used to represent this item when it was created
243 * @param options Options for customizing the individual tab item
244 * - \c badge value to display in the optional circular badge on the item; if nil or unspecified, the badge will be hidden
246 - (void)updateTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
249 [self createTabBar:nil withDict:nil];
251 NSString *name = [arguments objectAtIndex:0];
252 UITabBarItem *item = [tabBarItems objectForKey:name];
254 item.badgeValue = [options objectForKey:@"bad ge"];
259 * Show previously created items on the tab bar
260 * @brief show a list of tab bar items
261 * @param arguments the item names to be shown
262 * @param options dictionary of options, notable options including:
263 * - \c animate indicates that the items should animate onto the tab bar
264 * @see createTabBarItem
267 - (void)showTabBarItems:(NSArray*)arguments withDict:(NSDictionary*)options
270 [self createTabBar:nil withDict:nil];
272 int i, count = [arguments count];
273 NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:count];
274 for (i = 0; i < count; i++) {
275 NSString *itemName = [arguments objectAtIndex:i];
276 UITabBarItem *item = [tabBarItems objectForKey:itemName];
278 [items addObject:item];
281 BOOL animateItems = NO;
282 if ([options objectForKey:@"animate"])
283 animateItems = [(NSString*)[options objectForKey:@"animate"] boolValue];
284 [tabBar setItems:items animated:animateItems];
289 * Manually select an individual tab bar item, or nil for deselecting a currently selected tab bar item.
290 * @brief manually select a tab bar item
291 * @param arguments the name of the tab bar item to select
292 * @see createTabBarItem
293 * @see showTabBarItems
295 - (void)selectTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
298 [self createTabBar:nil withDict:nil];
300 NSString *itemName = [arguments objectAtIndex:0];
301 UITabBarItem *item = [tabBarItems objectForKey:itemName];
303 tabBar.selectedItem = item;
305 tabBar.selectedItem = nil;
309 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
311 NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls.tabBarItemSelected(%d);", item.tag];
312 [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
319 /*********************************************************************************/
320 - (void)createToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
322 CGFloat height = 45.0f;
324 UIBarStyle style = UIBarStyleBlackOpaque;
326 NSDictionary* toolBarSettings = options;//[settings objectForKey:@"ToolBarSettings"];
329 if ([toolBarSettings objectForKey:@"height"])
330 height = [[toolBarSettings objectForKey:@"height"] floatValue];
332 if ([toolBarSettings objectForKey:@"position"])
333 atTop = [[toolBarSettings objectForKey:@"position"] isEqualToString:@"top"];
335 #pragma unused(atTop)
337 NSString *styleStr = [toolBarSettings objectForKey:@"style"];
338 if ([styleStr isEqualToString:@"Default"])
339 style = UIBarStyleDefault;
340 else if ([styleStr isEqualToString:@"BlackOpaque"])
341 style = UIBarStyleBlackOpaque;
342 else if ([styleStr isEqualToString:@"BlackTranslucent"])
343 style = UIBarStyleBlackTranslucent;
346 CGRect webViewBounds = self.webView.bounds;
347 CGRect toolBarBounds = CGRectMake(
348 webViewBounds.origin.x,
349 webViewBounds.origin.y - 1.0f,
350 webViewBounds.size.width,
353 webViewBounds = CGRectMake(
354 webViewBounds.origin.x,
355 webViewBounds.origin.y + height,
356 webViewBounds.size.width,
357 webViewBounds.size.height - height
359 toolBar = [[UIToolbar alloc] initWithFrame:toolBarBounds];
362 toolBar.multipleTouchEnabled = NO;
363 toolBar.autoresizesSubviews = YES;
364 toolBar.userInteractionEnabled = YES;
365 toolBar.barStyle = style;
368 [toolBar setFrame:toolBarBounds];
369 [self.webView setFrame:webViewBounds];
371 [self.webView.superview addSubview:toolBar];
374 - (void)resetToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
376 NSLog(@"about to reset toolBarItems");
381 [toolBarItems release];
388 * @brief hide the tool bar
389 * @param arguments unused
390 * @param options unused
392 - (void)hideToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
395 [self createToolBar:nil withDict:nil];
396 toolBar.hidden = YES;
398 NSNotification* notif = [NSNotification notificationWithName:@"PGLayoutSubviewRemoved" object:toolBar];
399 [[NSNotificationQueue defaultQueue] enqueueNotification:notif postingStyle: NSPostASAP];
402 [self.webView setFrame:originalWebViewBounds];
406 - (void)setToolBarTitle:(NSArray*)arguments withDict:(NSDictionary*)options
409 [self createToolBar:nil withDict:nil];
411 NSString *title = [arguments objectAtIndex:0];
415 NSLog(@"not : %@", title);
416 toolBarTitle = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(toolBarTitleClicked)];
418 NSLog(@"is: %@", title);
419 toolBarTitle.title = title;
424 * Create a new tool bar button item for use on a previously created tool bar. Use ::showToolBar to show the new item on the tool bar.
426 * If the supplied image name is one of the labels listed below, then this method will construct a button
427 * using the standard system buttons. Note that if you use one of the system images, that the title you supply will be ignored.
429 * <b>Tool Bar Buttons</b>
430 * UIBarButtonSystemItemDone
431 * UIBarButtonSystemItemCancel
432 * UIBarButtonSystemItemEdit
433 * UIBarButtonSystemItemSave
434 * UIBarButtonSystemItemAdd
435 * UIBarButtonSystemItemFlexibleSpace
436 * UIBarButtonSystemItemFixedSpace
437 * UIBarButtonSystemItemCompose
438 * UIBarButtonSystemItemReply
439 * UIBarButtonSystemItemAction
440 * UIBarButtonSystemItemOrganize
441 * UIBarButtonSystemItemBookmarks
442 * UIBarButtonSystemItemSearch
443 * UIBarButtonSystemItemRefresh
444 * UIBarButtonSystemItemStop
445 * UIBarButtonSystemItemCamera
446 * UIBarButtonSystemItemTrash
447 * UIBarButtonSystemItemPlay
448 * UIBarButtonSystemItemPause
449 * UIBarButtonSystemItemRewind
450 * UIBarButtonSystemItemFastForward
451 * UIBarButtonSystemItemUndo, // iOS 3.0 and later
452 * UIBarButtonSystemItemRedo, // iOS 3.0 and later
453 * UIBarButtonSystemItemPageCurl, // iOS 4.0 and later
454 * @param {String} name internal name to refer to this tab by
455 * @param {String} [title] title text to show on the button, or null if no text should be shown
456 * @param {String} [image] image filename or internal identifier to show, or null if now image should be shown
457 * @param {Object} [options] Options for customizing the individual tab item [no option available at this time - this is for future proofing]
460 - (void)createToolBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
464 [self createToolBar:nil withDict:nil];
469 toolBarItems = [[NSMutableArray alloc] initWithCapacity:1];
472 NSString *tagId = [arguments objectAtIndex:0];
473 NSString *title = [arguments objectAtIndex:1];
475 if (arguments.count >= 2)
477 imageName = [arguments objectAtIndex:2];
482 if (arguments.count >= 4)
484 style = [arguments objectAtIndex:3];
488 style = @"UIBarButtonItemStylePlain";
492 UIBarButtonItemStyle useStyle;
494 if ([style isEqualToString:@"UIBarButtonItemStyleBordered"])
496 useStyle = UIBarButtonItemStyleBordered;
498 else if ([style isEqualToString:@"UIBarButtonItemStyleDone"])
500 useStyle = UIBarButtonItemStyleDone;
504 useStyle = UIBarButtonItemStylePlain;
507 UIBarButtonItem *item = nil;
508 if ([imageName length] > 0)
510 UIBarButtonSystemItem systemItem;
511 if ([imageName isEqualToString:@"UIBarButtonSystemItemDone"])
513 systemItem = UIBarButtonSystemItemDone;
515 else if ([imageName isEqualToString:@"UIBarButtonSystemItemCancel"])
517 systemItem = UIBarButtonSystemItemCancel;
519 else if ([imageName isEqualToString:@"UIBarButtonSystemItemEdit"])
521 systemItem = UIBarButtonSystemItemEdit;
523 else if ([imageName isEqualToString:@"UIBarButtonSystemItemSave"])
525 systemItem = UIBarButtonSystemItemSave;
527 else if ([imageName isEqualToString:@"UIBarButtonSystemItemAdd"])
529 systemItem = UIBarButtonSystemItemAdd;
531 else if ([imageName isEqualToString:@"UIBarButtonSystemItemFlexibleSpace"])
533 systemItem = UIBarButtonSystemItemFlexibleSpace;
535 else if ([imageName isEqualToString:@"UIBarButtonSystemItemFixedSpace"])
537 systemItem = UIBarButtonSystemItemFixedSpace;
539 else if ([imageName isEqualToString:@"UIBarButtonSystemItemCompose"])
541 systemItem = UIBarButtonSystemItemCompose;
543 else if ([imageName isEqualToString:@"UIBarButtonSystemItemReply"])
545 systemItem = UIBarButtonSystemItemReply;
547 else if ([imageName isEqualToString:@"UIBarButtonSystemItemAction"])
549 systemItem = UIBarButtonSystemItemAction;
551 else if ([imageName isEqualToString:@"UIBarButtonSystemItemOrganize"])
553 systemItem = UIBarButtonSystemItemOrganize;
555 else if ([imageName isEqualToString:@"UIBarButtonSystemItemBookmarks"])
557 systemItem = UIBarButtonSystemItemBookmarks;
559 else if ([imageName isEqualToString:@"UIBarButtonSystemItemSearch"])
561 systemItem = UIBarButtonSystemItemSearch;
563 else if ([imageName isEqualToString:@"UIBarButtonSystemItemRefresh"])
565 systemItem = UIBarButtonSystemItemRefresh;
567 else if ([imageName isEqualToString:@"UIBarButtonSystemItemStop"])
569 systemItem = UIBarButtonSystemItemStop;
571 else if ([imageName isEqualToString:@"UIBarButtonSystemItemCamera"])
573 systemItem = UIBarButtonSystemItemCamera;
575 else if ([imageName isEqualToString:@"UIBarButtonSystemItemTrash"])
577 systemItem = UIBarButtonSystemItemTrash;
579 else if ([imageName isEqualToString:@"UIBarButtonSystemItemPlay"])
581 systemItem = UIBarButtonSystemItemPlay;
583 else if ([imageName isEqualToString:@"UIBarButtonSystemItemPause"])
585 systemItem = UIBarButtonSystemItemPause;
587 else if ([imageName isEqualToString:@"UIBarButtonSystemItemRewind"])
589 systemItem = UIBarButtonSystemItemRewind;
591 else if ([imageName isEqualToString:@"UIBarButtonSystemItemFastForward"])
593 systemItem = UIBarButtonSystemItemFastForward;
595 else if ([imageName isEqualToString:@"UIBarButtonSystemItemUndo"])
597 systemItem = UIBarButtonSystemItemUndo;
599 else if ([imageName isEqualToString:@"UIBarButtonSystemItemRedo"])
601 systemItem = UIBarButtonSystemItemRedo;
603 else if ([imageName isEqualToString:@"UIBarButtonSystemItemPageCurl"])
605 systemItem = UIBarButtonSystemItemPageCurl;
610 item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:self action:@selector(toolBarButtonTapped:)];
611 if ([imageName isEqualToString:@"UIBarButtonSystemItemFixedSpace"])
618 item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName] style:useStyle target:self action:@selector(toolBarButtonTapped:)];
623 item = [[UIBarButtonItem alloc] initWithTitle:title style:useStyle target:self action:@selector(toolBarButtonTapped:)];
627 [toolBarItems insertObject:item atIndex:[tagId intValue]];
631 - (void)showToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
635 [self createToolBar:nil withDict:nil];
638 [toolBar setItems:toolBarItems animated:NO];
641 - (void) toolBarButtonTapped:(UIBarButtonItem *)button
645 for (UIBarButtonItem* currentButton in toolBarItems)
647 if (currentButton == button) {
648 NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls.toolBarButtonTapped(%d);", count];
649 [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
658 #pragma mark ActionSheet
660 - (void)createActionSheet:(NSArray*)arguments withDict:(NSDictionary*)options
663 NSString* title = [options objectForKey:@"title"];
666 UIActionSheet* actionSheet = [ [UIActionSheet alloc ]
669 cancelButtonTitle:nil
670 destructiveButtonTitle:nil
671 otherButtonTitles:nil
674 int count = [arguments count];
675 for(int n = 0; n < count; n++)
677 [ actionSheet addButtonWithTitle:[arguments objectAtIndex:n]];
680 if([options objectForKey:@"cancelButtonIndex"])
682 actionSheet.cancelButtonIndex = [[options objectForKey:@"cancelButtonIndex"] intValue];
684 if([options objectForKey:@"destructiveButtonIndex"])
686 actionSheet.destructiveButtonIndex = [[options objectForKey:@"destructiveButtonIndex"] intValue];
689 actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;//UIActionSheetStyleBlackOpaque;
690 [actionSheet showInView:self.webView.superview];
691 [actionSheet release];
696 - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
698 NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls._onActionSheetDismissed(%d);", buttonIndex];
699 [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];