1/* 2 * Copyright (C) 2014 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#import "config.h" 27#import "WKFormSelectControl.h" 28 29#if PLATFORM(IOS) 30 31#import "WKContentView.h" 32#import "WKContentViewInteraction.h" 33#import "WKFormPopover.h" 34#import "WebPageProxy.h" 35#import <CoreFoundation/CFUniChar.h> 36#import <UIKit/UIApplication_Private.h> 37#import <UIKit/UIDevice_Private.h> 38#import <UIKit/UIKeyboard_Private.h> 39#import <UIKit/UIPickerView.h> 40#import <UIKit/UIPickerView_Private.h> 41#import <UIKit/UIStringDrawing_Private.h> 42#import <UIKit/UIWebFormAccessory.h> 43#import <wtf/RetainPtr.h> 44 45using namespace WebKit; 46 47static const CGFloat minimumOptionFontSize = 12; 48 49CGFloat adjustedFontSize(CGFloat textWidth, UIFont *font, CGFloat initialFontSize, const Vector<OptionItem>& items) 50{ 51 CGFloat adjustedSize = initialFontSize; 52 for (size_t i = 0; i < items.size(); ++i) { 53 const OptionItem& item = items[i]; 54 if (item.text.isEmpty()) 55 continue; 56 57 CGFloat actualFontSize = initialFontSize; 58 [(NSString *)item.text _legacy_sizeWithFont:font minFontSize:minimumOptionFontSize actualFontSize:&actualFontSize forWidth:textWidth lineBreakMode:NSLineBreakByWordWrapping]; 59 60 if (actualFontSize > 0 && actualFontSize < adjustedSize) 61 adjustedSize = actualFontSize; 62 } 63 return adjustedSize; 64} 65 66@implementation WKFormSelectControl { 67 RetainPtr<id<WKFormControl>> _control; 68} 69 70- (instancetype)initWithView:(WKContentView *)view 71{ 72 if (!(self = [super init])) 73 return nil; 74 75 bool hasGroups = false; 76 for (size_t i = 0; i < view.assistedNodeInformation.selectOptions.size(); ++i) { 77 if (view.assistedNodeInformation.selectOptions[i].isGroup) { 78 hasGroups = true; 79 break; 80 } 81 } 82 83 if (UICurrentUserInterfaceIdiomIsPad()) 84 _control = adoptNS([[WKSelectPopover alloc] initWithView:view hasGroups:hasGroups]); 85 else if (view.assistedNodeInformation.isMultiSelect || hasGroups) 86 _control = adoptNS([[WKMultipleSelectPicker alloc] initWithView:view]); 87 else 88 _control = adoptNS([[WKSelectSinglePicker alloc] initWithView:view]); 89 90 return self; 91} 92 93+ (WKFormSelectControl *)createPeripheralWithView:(WKContentView *)view 94{ 95 return [[WKFormSelectControl alloc] initWithView:view]; 96} 97 98- (UIView *)assistantView 99{ 100 return [_control controlView]; 101} 102 103- (void)beginEditing 104{ 105 [_control controlBeginEditing]; 106} 107 108- (void)endEditing 109{ 110 [_control controlEndEditing]; 111} 112 113@end 114 115#endif // PLATFORM(IOS) 116