2 * Copyright (C) 2006, 2008, 2010, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #import "PopupMenuMac.h"
23 #import "WebDelegateImplementationCaching.h"
24 #import "WebFrameInternal.h"
25 #import <WebCore/IntRect.h>
26 #import <WebCore/AXObjectCache.h>
27 #import <WebCore/BlockExceptions.h>
28 #import <WebCore/Chrome.h>
29 #import <WebCore/ChromeClient.h>
30 #import <WebCore/EventHandler.h>
31 #import <WebCore/Frame.h>
32 #import <WebCore/FrameView.h>
33 #import <WebCore/Page.h>
34 #import <WebCore/PopupMenuClient.h>
35 #import <WebCore/SimpleFontData.h>
36 #import <WebCore/WebCoreSystemInterface.h>
38 using namespace WebCore;
40 PopupMenuMac::PopupMenuMac(PopupMenuClient* client)
45 PopupMenuMac::~PopupMenuMac()
47 [m_popup setControlView:nil];
50 void PopupMenuMac::clear()
52 [m_popup removeAllItems];
55 void PopupMenuMac::populate()
60 m_popup = adoptNS([[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:!m_client->shouldPopOver()]);
61 [m_popup setUsesItemFromMenu:NO];
62 [m_popup setAutoenablesItems:NO];
65 BOOL messagesEnabled = [[m_popup menu] menuChangedMessagesEnabled];
66 [[m_popup menu] setMenuChangedMessagesEnabled:NO];
68 // For pullDown menus the first item is hidden.
69 if (!m_client->shouldPopOver())
70 [m_popup addItemWithTitle:@""];
72 TextDirection menuTextDirection = m_client->menuStyle().textDirection();
73 [m_popup setUserInterfaceLayoutDirection:menuTextDirection == LTR ? NSUserInterfaceLayoutDirectionLeftToRight : NSUserInterfaceLayoutDirectionRightToLeft];
76 int size = m_client->listSize();
78 for (int i = 0; i < size; i++) {
79 if (m_client->itemIsSeparator(i)) {
80 [[m_popup menu] addItem:[NSMenuItem separatorItem]];
84 PopupMenuStyle style = m_client->itemStyle(i);
85 RetainPtr<NSMutableDictionary> attributes = adoptNS([[NSMutableDictionary alloc] init]);
86 if (style.font() != Font()) {
87 NSFont *font = style.font().primaryFont()->getNSFont();
89 CGFloat size = style.font().primaryFont()->platformData().size();
90 font = style.font().weight() < FontWeightBold ? [NSFont systemFontOfSize:size] : [NSFont boldSystemFontOfSize:size];
92 [attributes setObject:font forKey:NSFontAttributeName];
95 RetainPtr<NSMutableParagraphStyle> paragraphStyle = adoptNS([[NSParagraphStyle defaultParagraphStyle] mutableCopy]);
96 [paragraphStyle setAlignment:menuTextDirection == LTR ? NSLeftTextAlignment : NSRightTextAlignment];
97 NSWritingDirection writingDirection = style.textDirection() == LTR ? NSWritingDirectionLeftToRight : NSWritingDirectionRightToLeft;
98 [paragraphStyle setBaseWritingDirection:writingDirection];
99 if (style.hasTextDirectionOverride()) {
100 RetainPtr<NSNumber> writingDirectionValue = adoptNS([[NSNumber alloc] initWithInteger:writingDirection + NSTextWritingDirectionOverride]);
101 RetainPtr<NSArray> writingDirectionArray = adoptNS([[NSArray alloc] initWithObjects:writingDirectionValue.get(), nil]);
102 [attributes setObject:writingDirectionArray.get() forKey:NSWritingDirectionAttributeName];
104 [attributes setObject:paragraphStyle.get() forKey:NSParagraphStyleAttributeName];
106 // FIXME: Add support for styling the foreground and background colors.
107 // FIXME: Find a way to customize text color when an item is highlighted.
108 RetainPtr<NSAttributedString> string = adoptNS([[NSAttributedString alloc] initWithString:m_client->itemText(i) attributes:attributes.get()]);
110 [m_popup addItemWithTitle:@""];
111 NSMenuItem *menuItem = [m_popup lastItem];
112 [menuItem setAttributedTitle:string.get()];
113 // We set the title as well as the attributed title here. The attributed title will be displayed in the menu,
114 // but typeahead will use the non-attributed string that doesn't contain any leading or trailing whitespace.
115 [menuItem setTitle:[[string string] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
116 [menuItem setEnabled:m_client->itemIsEnabled(i)];
117 [menuItem setToolTip:m_client->itemToolTip(i)];
119 #pragma clang diagnostic push
120 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
121 // Allow the accessible text of the item to be overriden if necessary.
122 if (AXObjectCache::accessibilityEnabled()) {
123 NSString *accessibilityOverride = m_client->itemAccessibilityText(i);
124 if ([accessibilityOverride length])
125 [menuItem accessibilitySetOverrideValue:accessibilityOverride forAttribute:NSAccessibilityDescriptionAttribute];
127 #pragma clang diagnostic pop
130 [[m_popup menu] setMenuChangedMessagesEnabled:messagesEnabled];
133 void PopupMenuMac::show(const IntRect& r, FrameView* v, int index)
136 int numItems = [m_popup numberOfItems];
139 m_client->popupDidHide();
142 ASSERT(numItems > index);
144 // Workaround for crazy bug where a selected index of -1 for a menu with only 1 item will cause a blank menu.
145 if (index == -1 && numItems == 2 && !m_client->shouldPopOver() && ![[m_popup itemAtIndex:1] isEnabled])
148 NSView* view = v->documentView();
150 [m_popup attachPopUpWithFrame:r inView:view];
151 [m_popup selectItemAtIndex:index];
153 NSMenu* menu = [m_popup menu];
156 NSFont* font = m_client->menuStyle().font().primaryFont()->getNSFont();
158 // These values were borrowed from AppKit to match their placement of the menu.
159 const int popOverHorizontalAdjust = -10;
160 const int popUnderHorizontalAdjust = 6;
161 const int popUnderVerticalAdjust = 6;
162 if (m_client->shouldPopOver()) {
163 NSRect titleFrame = [m_popup titleRectForBounds:r];
164 if (titleFrame.size.width <= 0 || titleFrame.size.height <= 0)
166 float vertOffset = roundf((NSMaxY(r) - NSMaxY(titleFrame)) + NSHeight(titleFrame));
167 // Adjust for fonts other than the system font.
168 NSFont* defaultFont = [NSFont systemFontOfSize:[font pointSize]];
169 vertOffset += [font descender] - [defaultFont descender];
170 vertOffset = fminf(NSHeight(r), vertOffset);
172 location = NSMakePoint(NSMinX(r) + popOverHorizontalAdjust, NSMaxY(r) - vertOffset);
174 location = NSMakePoint(NSMinX(r) + popUnderHorizontalAdjust, NSMaxY(r) + popUnderVerticalAdjust);
176 // Save the current event that triggered the popup, so we can clean up our event
177 // state after the NSMenu goes away.
178 Ref<Frame> frame(v->frame());
179 RetainPtr<NSEvent> event = frame->eventHandler().currentNSEvent();
181 Ref<PopupMenuMac> protector(*this);
183 RetainPtr<NSView> dummyView = adoptNS([[NSView alloc] initWithFrame:r]);
184 [view addSubview:dummyView.get()];
185 location = [dummyView convertPoint:location fromView:view];
187 if (Page* page = frame->page()) {
188 WebView* webView = kit(page);
189 BEGIN_BLOCK_OBJC_EXCEPTIONS;
190 CallUIDelegate(webView, @selector(webView:willPopupMenu:), menu);
191 END_BLOCK_OBJC_EXCEPTIONS;
194 NSControlSize controlSize;
195 switch (m_client->menuStyle().menuSize()) {
196 case PopupMenuStyle::PopupMenuSizeNormal:
197 controlSize = NSRegularControlSize;
199 case PopupMenuStyle::PopupMenuSizeSmall:
200 controlSize = NSSmallControlSize;
202 case PopupMenuStyle::PopupMenuSizeMini:
203 controlSize = NSMiniControlSize;
207 wkPopupMenu(menu, location, roundf(NSWidth(r)), dummyView.get(), index, font, controlSize, !m_client->menuStyle().hasDefaultAppearance());
209 [m_popup dismissPopUp];
210 [dummyView removeFromSuperview];
215 int newIndex = [m_popup indexOfSelectedItem];
216 m_client->popupDidHide();
218 // Adjust newIndex for hidden first item.
219 if (!m_client->shouldPopOver())
222 if (index != newIndex && newIndex >= 0)
223 m_client->valueChanged(newIndex);
225 // Give the frame a chance to fix up its event state, since the popup eats all the
226 // events during tracking.
227 frame->eventHandler().sendFakeEventsAfterWidgetTracking(event.get());
230 void PopupMenuMac::hide()
232 [m_popup dismissPopUp];
235 void PopupMenuMac::updateFromElement()
239 void PopupMenuMac::disconnectClient()