• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#import "config.h"
21#import "PopupMenu.h"
22
23#import "Chrome.h"
24#import "ChromeClient.h"
25#import "EventHandler.h"
26#import "Frame.h"
27#import "FrameView.h"
28#import "HTMLNames.h"
29#import "HTMLOptGroupElement.h"
30#import "HTMLOptionElement.h"
31#import "HTMLSelectElement.h"
32#import "Page.h"
33#import "SimpleFontData.h"
34#import "WebCoreSystemInterface.h"
35
36namespace WebCore {
37
38using namespace HTMLNames;
39
40PopupMenu::PopupMenu(PopupMenuClient* client)
41    : m_popupClient(client)
42{
43}
44
45PopupMenu::~PopupMenu()
46{
47    if (m_popup)
48        [m_popup.get() setControlView:nil];
49}
50
51void PopupMenu::clear()
52{
53    if (m_popup)
54        [m_popup.get() removeAllItems];
55}
56
57void PopupMenu::populate()
58{
59    if (m_popup)
60        clear();
61    else {
62        m_popup = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:!client()->shouldPopOver()];
63        [m_popup.get() release]; // release here since the RetainPtr has retained the object already
64        [m_popup.get() setUsesItemFromMenu:NO];
65        [m_popup.get() setAutoenablesItems:NO];
66    }
67
68    BOOL messagesEnabled = [[m_popup.get() menu] menuChangedMessagesEnabled];
69    [[m_popup.get() menu] setMenuChangedMessagesEnabled:NO];
70
71    // For pullDown menus the first item is hidden.
72    if (!client()->shouldPopOver())
73        [m_popup.get() addItemWithTitle:@""];
74
75    ASSERT(client());
76    int size = client()->listSize();
77
78    for (int i = 0; i < size; i++) {
79        if (client()->itemIsSeparator(i))
80            [[m_popup.get() menu] addItem:[NSMenuItem separatorItem]];
81        else {
82            PopupMenuStyle style = client()->itemStyle(i);
83            NSMutableDictionary* attributes = [[NSMutableDictionary alloc] init];
84            if (style.font() != Font()) {
85                NSFont *font = style.font().primaryFont()->getNSFont();
86                if (!font) {
87                    CGFloat size = style.font().primaryFont()->platformData().size();
88                    font = style.font().weight() < FontWeightBold ? [NSFont systemFontOfSize:size] : [NSFont boldSystemFontOfSize:size];
89                }
90                [attributes setObject:font forKey:NSFontAttributeName];
91            }
92            // FIXME: Add support for styling the foreground and background colors.
93            // FIXME: Find a way to customize text color when an item is highlighted.
94            NSAttributedString* string = [[NSAttributedString alloc] initWithString:client()->itemText(i) attributes:attributes];
95            [attributes release];
96
97            [m_popup.get() addItemWithTitle:@""];
98            NSMenuItem* menuItem = [m_popup.get() lastItem];
99            [menuItem setAttributedTitle:string];
100            [menuItem setEnabled:client()->itemIsEnabled(i)];
101            [menuItem setToolTip:client()->itemToolTip(i)];
102            [string release];
103        }
104    }
105
106    [[m_popup.get() menu] setMenuChangedMessagesEnabled:messagesEnabled];
107}
108
109#if !ENABLE(EXPERIMENTAL_SINGLE_VIEW_MODE)
110
111void PopupMenu::show(const IntRect& r, FrameView* v, int index)
112{
113    populate();
114    int numItems = [m_popup.get() numberOfItems];
115    if (numItems <= 0) {
116        if (client())
117            client()->popupDidHide();
118        return;
119    }
120    ASSERT(numItems > index);
121
122    // Workaround for crazy bug where a selected index of -1 for a menu with only 1 item will cause a blank menu.
123    if (index == -1 && numItems == 2 && !client()->shouldPopOver() && ![[m_popup.get() itemAtIndex:1] isEnabled])
124        index = 0;
125
126    NSView* view = v->documentView();
127
128    [m_popup.get() attachPopUpWithFrame:r inView:view];
129    [m_popup.get() selectItemAtIndex:index];
130
131    NSMenu* menu = [m_popup.get() menu];
132
133    NSPoint location;
134    NSFont* font = client()->menuStyle().font().primaryFont()->getNSFont();
135
136    // These values were borrowed from AppKit to match their placement of the menu.
137    const int popOverHorizontalAdjust = -10;
138    const int popUnderHorizontalAdjust = 6;
139    const int popUnderVerticalAdjust = 6;
140    if (client()->shouldPopOver()) {
141        NSRect titleFrame = [m_popup.get() titleRectForBounds:r];
142        if (titleFrame.size.width <= 0 || titleFrame.size.height <= 0)
143            titleFrame = r;
144        float vertOffset = roundf((NSMaxY(r) - NSMaxY(titleFrame)) + NSHeight(titleFrame));
145        // Adjust for fonts other than the system font.
146        NSFont* defaultFont = [NSFont systemFontOfSize:[font pointSize]];
147        vertOffset += [font descender] - [defaultFont descender];
148        vertOffset = fminf(NSHeight(r), vertOffset);
149
150        location = NSMakePoint(NSMinX(r) + popOverHorizontalAdjust, NSMaxY(r) - vertOffset);
151    } else
152        location = NSMakePoint(NSMinX(r) + popUnderHorizontalAdjust, NSMaxY(r) + popUnderVerticalAdjust);
153
154    // Save the current event that triggered the popup, so we can clean up our event
155    // state after the NSMenu goes away.
156    RefPtr<Frame> frame = v->frame();
157    NSEvent* event = [frame->eventHandler()->currentNSEvent() retain];
158
159    RefPtr<PopupMenu> protector(this);
160
161    RetainPtr<NSView> dummyView(AdoptNS, [[NSView alloc] initWithFrame:r]);
162    [view addSubview:dummyView.get()];
163    location = [dummyView.get() convertPoint:location fromView:view];
164
165    if (Page* page = frame->page())
166        page->chrome()->client()->willPopUpMenu(menu);
167    wkPopupMenu(menu, location, roundf(NSWidth(r)), dummyView.get(), index, font);
168
169    [m_popup.get() dismissPopUp];
170    [dummyView.get() removeFromSuperview];
171
172    if (client()) {
173        int newIndex = [m_popup.get() indexOfSelectedItem];
174        client()->popupDidHide();
175
176        // Adjust newIndex for hidden first item.
177        if (!client()->shouldPopOver())
178            newIndex--;
179
180        if (index != newIndex && newIndex >= 0)
181            client()->valueChanged(newIndex);
182
183        // Give the frame a chance to fix up its event state, since the popup eats all the
184        // events during tracking.
185        frame->eventHandler()->sendFakeEventsAfterWidgetTracking(event);
186    }
187
188    [event release];
189}
190
191#else
192
193void PopupMenu::show(const IntRect&, FrameView*, int)
194{
195}
196
197#endif
198
199void PopupMenu::hide()
200{
201    [m_popup.get() dismissPopUp];
202}
203
204void PopupMenu::updateFromElement()
205{
206}
207
208bool PopupMenu::itemWritingDirectionIsNatural()
209{
210    return true;
211}
212
213}
214