ios version
[wl-mobile.git] / Plugins / NativeControls / NativeControls.m
1 //
2 //  NativeControls.h
3 //  
4 //
5 //  Created by Jesse MacFadyen on 10-02-03.
6 //  MIT Licensed
7
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.
12
13 #import "NativeControls.h"
14
15 #import <QuartzCore/QuartzCore.h>
16
17 @implementation NativeControls
18 #ifndef __IPHONE_3_0
19 @synthesize webView;
20 #endif
21
22 -(PGPlugin*) initWithWebView:(UIWebView*)theWebView
23 {
24     self = (NativeControls*)[super initWithWebView:theWebView];
25     if (self) 
26         {
27         tabBarItems = [[NSMutableDictionary alloc] initWithCapacity:5];
28                 originalWebViewBounds = theWebView.bounds;
29     }
30     return self;
31 }
32
33 - (void)dealloc
34 {       
35     if (tabBar)
36         [tabBar release];
37         
38         if (toolBar)
39         {
40                 [toolBarTitle release];
41                 [toolBarItems release];
42                 [toolBar release];
43         }
44         
45     [super dealloc];
46 }
47
48 #pragma mark -
49 #pragma mark TabBar
50
51 /**
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
56  */
57 - (void)createTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
58 {
59     tabBar = [UITabBar new];
60     [tabBar sizeToFit];
61     tabBar.delegate = self;
62     tabBar.multipleTouchEnabled   = NO;
63     tabBar.autoresizesSubviews    = YES;
64     tabBar.hidden                 = YES;
65     tabBar.userInteractionEnabled = YES;
66         tabBar.opaque = YES;
67         
68         self.webView.superview.autoresizesSubviews = YES;
69         
70         [ self.webView.superview addSubview:tabBar];    
71 }
72
73 /**
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)
80  */
81 - (void)showTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
82 {
83     if (!tabBar)
84         [self createTabBar:nil withDict:nil];
85         
86         // if we are calling this again when its shown, reset
87         if (!tabBar.hidden) {
88                 return;
89         }
90     
91     CGFloat height = 0.0f;
92     BOOL atBottom = YES;
93         
94     //  CGRect offsetRect = [ [UIApplication sharedApplication] statusBarFrame];
95     
96     if (options) 
97         {
98         height   = [[options objectForKey:@"height"] floatValue];
99         atBottom = [[options objectForKey:@"position"] isEqualToString:@"bottom"];
100     }
101         if(height == 0)
102         {
103                 height = 49.0f;
104                 atBottom = YES;
105         }
106         
107     tabBar.hidden = NO;
108     CGRect webViewBounds = originalWebViewBounds;
109     CGRect tabBarBounds;
110         
111         NSNotification* notif = [NSNotification notificationWithName:@"PGLayoutSubviewAdded" object:tabBar];
112         [[NSNotificationQueue defaultQueue] enqueueNotification:notif postingStyle: NSPostASAP];
113         
114     if (atBottom) 
115     {
116         tabBarBounds = CGRectMake(
117                                   webViewBounds.origin.x,
118                                   webViewBounds.origin.y + webViewBounds.size.height - height,
119                                   webViewBounds.size.width,
120                                   height
121                                   );
122         webViewBounds = CGRectMake(
123                                    webViewBounds.origin.x,
124                                    webViewBounds.origin.y,
125                                    webViewBounds.size.width,
126                                    webViewBounds.size.height - height
127                                    );
128     } 
129     else 
130     {
131         tabBarBounds = CGRectMake(
132                                   webViewBounds.origin.x,
133                                   webViewBounds.origin.y,
134                                   webViewBounds.size.width,
135                                   height
136                                   );
137         webViewBounds = CGRectMake(
138                                    webViewBounds.origin.x,
139                                    webViewBounds.origin.y + height,
140                                    webViewBounds.size.width,
141                                    webViewBounds.size.height - height
142                                    );
143     }
144     
145     [tabBar setFrame:tabBarBounds];
146         
147         
148     [self.webView setFrame:webViewBounds];
149 }
150
151 /**
152  * Hide the tab bar
153  * @brief hide the tab bar
154  * @param arguments unused
155  * @param options unused
156  */
157 - (void)hideTabBar:(NSArray*)arguments withDict:(NSDictionary*)options
158 {
159     if (!tabBar)
160         [self createTabBar:nil withDict:nil];
161     tabBar.hidden = YES;
162         
163         NSNotification* notif = [NSNotification notificationWithName:@"PGLayoutSubviewRemoved" object:tabBar];
164         [[NSNotificationQueue defaultQueue] enqueueNotification:notif postingStyle: NSPostASAP];
165         
166         
167         [self.webView setFrame:originalWebViewBounds];
168 }
169
170 /**
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.
172  *
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>
176  *   - tabButton:More
177  *   - tabButton:Favorites
178  *   - tabButton:Featured
179  *   - tabButton:TopRated
180  *   - tabButton:Recents
181  *   - tabButton:Contacts
182  *   - tabButton:History
183  *   - tabButton:Bookmarks
184  *   - tabButton:Search
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
196  */
197 - (void)createTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
198 {
199     if (!tabBar)
200         [self createTabBar:nil withDict:nil];
201     
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];
206     
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];
224     }
225     
226     if (item == nil) {
227         item = [[UITabBarItem alloc] initWithTitle:title image:[UIImage imageNamed:imageName] tag:tag];
228     }
229     
230     if ([options objectForKey:@"badge"])
231         item.badgeValue = [options objectForKey:@"badge"];
232     
233     [tabBarItems setObject:item forKey:name];
234         [item release];
235 }
236
237
238 /**
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
245  */
246 - (void)updateTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
247 {
248     if (!tabBar)
249         [self createTabBar:nil withDict:nil];
250     
251     NSString  *name = [arguments objectAtIndex:0];
252     UITabBarItem *item = [tabBarItems objectForKey:name];
253     if (item)
254         item.badgeValue = [options objectForKey:@"bad   ge"];
255 }
256
257
258 /**
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
265  * @see createTabBar
266  */
267 - (void)showTabBarItems:(NSArray*)arguments withDict:(NSDictionary*)options
268 {
269     if (!tabBar)
270         [self createTabBar:nil withDict:nil];
271     
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];
277         if (item)
278             [items addObject:item];
279     }
280     
281     BOOL animateItems = NO;
282     if ([options objectForKey:@"animate"])
283         animateItems = [(NSString*)[options objectForKey:@"animate"] boolValue];
284     [tabBar setItems:items animated:animateItems];
285         [items release];
286 }
287
288 /**
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
294  */
295 - (void)selectTabBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
296 {
297     if (!tabBar)
298         [self createTabBar:nil withDict:nil];
299     
300     NSString *itemName = [arguments objectAtIndex:0];
301     UITabBarItem *item = [tabBarItems objectForKey:itemName];
302     if (item)
303         tabBar.selectedItem = item;
304     else
305         tabBar.selectedItem = nil;
306 }
307
308
309 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
310 {
311     NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls.tabBarItemSelected(%d);", item.tag];    
312     [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
313 }
314
315 #pragma mark -
316 #pragma mark ToolBar
317
318
319 /*********************************************************************************/
320 - (void)createToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
321 {
322     CGFloat height   = 45.0f;
323     BOOL atTop       = YES;
324     UIBarStyle style = UIBarStyleBlackOpaque;
325     
326     NSDictionary* toolBarSettings = options;//[settings objectForKey:@"ToolBarSettings"];
327     if (toolBarSettings) 
328         {
329         if ([toolBarSettings objectForKey:@"height"])
330             height = [[toolBarSettings objectForKey:@"height"] floatValue];
331                 
332         if ([toolBarSettings objectForKey:@"position"])
333             atTop  = [[toolBarSettings objectForKey:@"position"] isEqualToString:@"top"];
334         
335 #pragma unused(atTop)
336                 
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;
344     }
345     
346     CGRect webViewBounds = self.webView.bounds;
347     CGRect toolBarBounds = CGRectMake(
348                                       webViewBounds.origin.x,
349                                       webViewBounds.origin.y - 1.0f,
350                                       webViewBounds.size.width,
351                                       height
352                                       );
353     webViewBounds = CGRectMake(
354                                webViewBounds.origin.x,
355                                webViewBounds.origin.y + height,
356                                webViewBounds.size.width,
357                                webViewBounds.size.height - height
358                                );
359     toolBar = [[UIToolbar alloc] initWithFrame:toolBarBounds];
360     [toolBar sizeToFit];
361     toolBar.hidden                 = NO;
362     toolBar.multipleTouchEnabled   = NO;
363     toolBar.autoresizesSubviews    = YES;
364     toolBar.userInteractionEnabled = YES;
365     toolBar.barStyle               = style;
366         
367     
368     [toolBar setFrame:toolBarBounds];
369     [self.webView setFrame:webViewBounds];
370     
371     [self.webView.superview addSubview:toolBar];
372 }
373
374 - (void)resetToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
375 {
376         NSLog(@"about to reset toolBarItems");
377         toolBarItems = nil;
378         /*
379      if (toolBarItems)
380      {
381      [toolBarItems release];
382      }
383          */
384 }
385
386 /**
387  * Hide the tool bar
388  * @brief hide the tool bar
389  * @param arguments unused
390  * @param options unused
391  */
392 - (void)hideToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
393 {
394     if (!toolBar)
395         [self createToolBar:nil withDict:nil];
396     toolBar.hidden = YES;
397         
398         NSNotification* notif = [NSNotification notificationWithName:@"PGLayoutSubviewRemoved" object:toolBar];
399         [[NSNotificationQueue defaultQueue] enqueueNotification:notif postingStyle: NSPostASAP];
400         
401         
402         [self.webView setFrame:originalWebViewBounds];
403 }
404
405
406 - (void)setToolBarTitle:(NSArray*)arguments withDict:(NSDictionary*)options
407 {
408     if (!toolBar)
409         [self createToolBar:nil withDict:nil];
410     
411     NSString *title = [arguments objectAtIndex:0];
412     
413        
414     if (!toolBarTitle) {
415          NSLog(@"not : %@", title);
416         toolBarTitle = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(toolBarTitleClicked)];
417     } else {
418          NSLog(@"is: %@", title);
419         toolBarTitle.title = title;
420     }
421 }
422
423 /**
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.
425  *
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.
428  *
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]
458  *  
459  */
460 - (void)createToolBarItem:(NSArray*)arguments withDict:(NSDictionary*)options
461 {
462     if (!toolBar)
463         {
464         [self createToolBar:nil withDict:nil];
465         }
466         
467         if (!toolBarItems)
468         {
469                 toolBarItems = [[NSMutableArray alloc] initWithCapacity:1];
470         }
471     
472     NSString  *tagId      = [arguments objectAtIndex:0];
473     NSString  *title     = [arguments objectAtIndex:1];
474         NSString  *imageName;
475         if (arguments.count >= 2)
476         {
477                 imageName = [arguments objectAtIndex:2];
478         }
479         
480         NSString  *style;
481         
482         if (arguments.count >= 4)
483         {
484                 style    = [arguments objectAtIndex:3];
485         }
486         else 
487         {
488                 style = @"UIBarButtonItemStylePlain";
489         }
490     
491         
492         UIBarButtonItemStyle useStyle;
493         
494         if ([style isEqualToString:@"UIBarButtonItemStyleBordered"])
495         {
496                 useStyle = UIBarButtonItemStyleBordered;
497         }
498         else if ([style isEqualToString:@"UIBarButtonItemStyleDone"])
499         {
500                 useStyle = UIBarButtonItemStyleDone;
501         }
502         else 
503         {
504                 useStyle = UIBarButtonItemStylePlain;
505         }
506     
507     UIBarButtonItem *item = nil;    
508     if ([imageName length] > 0) 
509         {
510         UIBarButtonSystemItem systemItem;
511         if ([imageName isEqualToString:@"UIBarButtonSystemItemDone"])
512                 {
513                         systemItem = UIBarButtonSystemItemDone;
514                 }
515         else if ([imageName isEqualToString:@"UIBarButtonSystemItemCancel"])
516                 {
517                         systemItem = UIBarButtonSystemItemCancel;
518                 }
519         else if ([imageName isEqualToString:@"UIBarButtonSystemItemEdit"])
520                 {
521                         systemItem = UIBarButtonSystemItemEdit;
522                 }
523         else if ([imageName isEqualToString:@"UIBarButtonSystemItemSave"])
524                 {
525                         systemItem = UIBarButtonSystemItemSave;
526                 }
527         else if ([imageName isEqualToString:@"UIBarButtonSystemItemAdd"])
528                 {
529                         systemItem = UIBarButtonSystemItemAdd;
530                 }
531         else if ([imageName isEqualToString:@"UIBarButtonSystemItemFlexibleSpace"])
532                 {
533                         systemItem = UIBarButtonSystemItemFlexibleSpace;
534                 }
535         else if ([imageName isEqualToString:@"UIBarButtonSystemItemFixedSpace"])
536                 {
537                         systemItem = UIBarButtonSystemItemFixedSpace;
538                 }
539         else if ([imageName isEqualToString:@"UIBarButtonSystemItemCompose"])
540                 {
541                         systemItem = UIBarButtonSystemItemCompose;
542                 }
543         else if ([imageName isEqualToString:@"UIBarButtonSystemItemReply"])
544                 {
545                         systemItem = UIBarButtonSystemItemReply;
546                 }
547                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemAction"])
548                 {
549                         systemItem = UIBarButtonSystemItemAction;
550                 }
551         else if ([imageName isEqualToString:@"UIBarButtonSystemItemOrganize"])
552                 {
553                         systemItem = UIBarButtonSystemItemOrganize;
554                 }
555         else if ([imageName isEqualToString:@"UIBarButtonSystemItemBookmarks"])
556                 {
557                         systemItem = UIBarButtonSystemItemBookmarks;
558                 }
559                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemSearch"])
560                 {
561                         systemItem = UIBarButtonSystemItemSearch;
562                 }
563                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemRefresh"])
564                 {
565                         systemItem = UIBarButtonSystemItemRefresh;
566                 }
567                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemStop"])
568                 {
569                         systemItem = UIBarButtonSystemItemStop;
570                 }
571                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemCamera"])
572                 {
573                         systemItem = UIBarButtonSystemItemCamera;
574                 }
575                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemTrash"])
576                 {
577                         systemItem = UIBarButtonSystemItemTrash;
578                 }
579                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemPlay"])
580                 {
581                         systemItem = UIBarButtonSystemItemPlay;
582                 }
583                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemPause"])
584                 {
585                         systemItem = UIBarButtonSystemItemPause;
586                 }
587                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemRewind"])
588                 {
589                         systemItem = UIBarButtonSystemItemRewind;
590                 }
591                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemFastForward"])
592                 {
593                         systemItem = UIBarButtonSystemItemFastForward;
594                 }
595                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemUndo"])
596                 {
597                         systemItem = UIBarButtonSystemItemUndo;
598                 }
599                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemRedo"])
600                 {
601                         systemItem = UIBarButtonSystemItemRedo;
602                 }
603                 else if ([imageName isEqualToString:@"UIBarButtonSystemItemPageCurl"])
604                 {
605                         systemItem = UIBarButtonSystemItemPageCurl;
606                 }
607         
608                 if (systemItem)
609                 {
610                         item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:self action:@selector(toolBarButtonTapped:)];
611                         if ([imageName isEqualToString:@"UIBarButtonSystemItemFixedSpace"])
612                         {
613                                 item.width = 14;
614                         }
615                 }
616                 else
617                 {
618                         item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName] style:useStyle target:self action:@selector(toolBarButtonTapped:)];
619                 }
620     }
621         else 
622         {
623                 item = [[UIBarButtonItem alloc] initWithTitle:title style:useStyle target:self action:@selector(toolBarButtonTapped:)];
624         }
625     
626         
627     [toolBarItems insertObject:item atIndex:[tagId intValue]];
628         [item release];
629 }
630
631 - (void)showToolBar:(NSArray*)arguments withDict:(NSDictionary*)options
632 {
633     if (!toolBar)
634         {
635         [self createToolBar:nil withDict:nil];
636         }       
637         
638         [toolBar setItems:toolBarItems animated:NO];
639 }
640
641 - (void) toolBarButtonTapped:(UIBarButtonItem *)button
642 {
643         int count = 0;
644     
645         for (UIBarButtonItem* currentButton in toolBarItems)
646         {
647                 if (currentButton == button) {
648                         NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls.toolBarButtonTapped(%d);", count];    
649                         [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
650                         return;
651                 }
652                 
653                 count++;
654         }
655 }
656
657 #pragma mark -
658 #pragma mark ActionSheet
659
660 - (void)createActionSheet:(NSArray*)arguments withDict:(NSDictionary*)options
661 {
662     
663         NSString* title = [options objectForKey:@"title"];
664     
665         
666         UIActionSheet* actionSheet = [ [UIActionSheet alloc ] 
667                                   initWithTitle:title 
668                                   delegate:self 
669                                   cancelButtonTitle:nil 
670                                   destructiveButtonTitle:nil
671                                   otherButtonTitles:nil
672                                   ];
673         
674         int count = [arguments count];
675         for(int n = 0; n < count; n++)
676         {
677                 [ actionSheet addButtonWithTitle:[arguments objectAtIndex:n]];
678         }
679         
680         if([options objectForKey:@"cancelButtonIndex"])
681         {
682                 actionSheet.cancelButtonIndex = [[options objectForKey:@"cancelButtonIndex"] intValue];
683         }
684         if([options objectForKey:@"destructiveButtonIndex"])
685         {
686                 actionSheet.destructiveButtonIndex = [[options objectForKey:@"destructiveButtonIndex"] intValue];
687         }
688         
689         actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;//UIActionSheetStyleBlackOpaque;
690     [actionSheet showInView:self.webView.superview];
691     [actionSheet release];
692         
693 }
694
695
696 - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
697 {
698         NSString * jsCallBack = [NSString stringWithFormat:@"window.plugins.nativeControls._onActionSheetDismissed(%d);", buttonIndex];    
699     [self.webView stringByEvaluatingJavaScriptFromString:jsCallBack];
700 }
701
702
703
704 @end