1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
|
/*
* Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
* Copyright (C) 2007-2009 Torch Mobile Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "PopupMenu.h"
#include "BitmapInfo.h"
#include "Document.h"
#include "FloatRect.h"
#include "FontSelector.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "HTMLNames.h"
#include "Page.h"
#include "PlatformMouseEvent.h"
#include "PlatformScreen.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "Scrollbar.h"
#include "ScrollbarTheme.h"
#include "SimpleFontData.h"
#include <tchar.h>
#include <windows.h>
#if PLATFORM(WINCE)
#include <ResDefCE.h>
#define MAKEPOINTS(l) (*((POINTS FAR *)&(l)))
#endif
using std::min;
namespace WebCore {
using namespace HTMLNames;
// Default Window animation duration in milliseconds
static const int defaultAnimationDuration = 200;
// Maximum height of a popup window
static const int maxPopupHeight = 320;
const int optionSpacingMiddle = 1;
const int popupWindowBorderWidth = 1;
static LPCTSTR kPopupWindowClassName = _T("PopupWindowClass");
static ATOM registerPopup();
static LRESULT CALLBACK PopupWndProc(HWND, UINT, WPARAM, LPARAM);
// FIXME: Remove this as soon as practical.
static inline bool isASCIIPrintable(unsigned c)
{
return c >= 0x20 && c <= 0x7E;
}
PopupMenu::PopupMenu(PopupMenuClient* client)
: m_popupClient(client)
, m_scrollbar(0)
, m_popup(0)
, m_DC(0)
, m_bmp(0)
, m_wasClicked(false)
, m_itemHeight(0)
, m_scrollOffset(0)
, m_wheelDelta(0)
, m_focusedIndex(0)
, m_scrollbarCapturingMouse(false)
{
}
PopupMenu::~PopupMenu()
{
if (m_bmp)
::DeleteObject(m_bmp);
if (m_DC)
::DeleteDC(m_DC);
if (m_popup)
::DestroyWindow(m_popup);
}
LPCTSTR PopupMenu::popupClassName()
{
return kPopupWindowClassName;
}
void PopupMenu::show(const IntRect& r, FrameView* v, int index)
{
calculatePositionAndSize(r, v);
if (clientRect().isEmpty())
return;
if (!m_popup) {
registerPopup();
DWORD exStyle = WS_EX_LTRREADING;
// Even though we already know our size and location at this point, we pass (0,0,0,0) as our size/location here.
// We need to wait until after the call to ::SetWindowLongPtr to set our size so that in our WM_SIZE handler we can get access to the PopupMenu object
m_popup = ::CreateWindowEx(exStyle, kPopupWindowClassName, _T("PopupMenu"),
WS_POPUP | WS_BORDER,
0, 0, 0, 0,
v->hostWindow()->platformWindow(), 0, 0, 0);
if (!m_popup)
return;
#if PLATFORM(WINCE)
::SetWindowLong(m_popup, 0, (LONG)this);
#else
::SetWindowLongPtr(m_popup, 0, (LONG_PTR)this);
#endif
}
if (!m_scrollbar)
if (visibleItems() < client()->listSize()) {
// We need a scroll bar
m_scrollbar = client()->createScrollbar(this, VerticalScrollbar, SmallScrollbar);
}
::SetWindowPos(m_popup, HWND_TOP, m_windowRect.x(), m_windowRect.y(), m_windowRect.width(), m_windowRect.height(), 0);
// Determine whether we should animate our popups
// Note: Must use 'BOOL' and 'FALSE' instead of 'bool' and 'false' to avoid stack corruption with SystemParametersInfo
BOOL shouldAnimate = FALSE;
#if !PLATFORM(WINCE)
::SystemParametersInfo(SPI_GETCOMBOBOXANIMATION, 0, &shouldAnimate, 0);
if (shouldAnimate) {
RECT viewRect = {0};
::GetWindowRect(v->hostWindow()->platformWindow(), &viewRect);
if (!::IsRectEmpty(&viewRect)) {
// Popups should slide into view away from the <select> box
// NOTE: This may have to change for Vista
DWORD slideDirection = (m_windowRect.y() < viewRect.top + v->contentsToWindow(r.location()).y()) ? AW_VER_NEGATIVE : AW_VER_POSITIVE;
::AnimateWindow(m_popup, defaultAnimationDuration, AW_SLIDE | slideDirection | AW_ACTIVATE);
}
} else
#endif
::ShowWindow(m_popup, SW_SHOWNORMAL);
::SetCapture(m_popup);
if (client()) {
int index = client()->selectedIndex();
if (index >= 0)
setFocusedIndex(index);
}
}
void PopupMenu::hide()
{
::ShowWindow(m_popup, SW_HIDE);
}
const int endOfLinePadding = 2;
void PopupMenu::calculatePositionAndSize(const IntRect& r, FrameView* v)
{
// r is in absolute document coordinates, but we want to be in screen coordinates
// First, move to WebView coordinates
IntRect rScreenCoords(v->contentsToWindow(r.location()), r.size());
// Then, translate to screen coordinates
POINT location(rScreenCoords.location());
if (!::ClientToScreen(v->hostWindow()->platformWindow(), &location))
return;
rScreenCoords.setLocation(location);
// First, determine the popup's height
int itemCount = client()->listSize();
m_itemHeight = client()->menuStyle().font().height() + optionSpacingMiddle;
int naturalHeight = m_itemHeight * itemCount;
int popupHeight = min(maxPopupHeight, naturalHeight);
// The popup should show an integral number of items (i.e. no partial items should be visible)
popupHeight -= popupHeight % m_itemHeight;
// Next determine its width
int popupWidth = 0;
for (int i = 0; i < itemCount; ++i) {
String text = client()->itemText(i);
if (text.isEmpty())
continue;
Font itemFont = client()->menuStyle().font();
if (client()->itemIsLabel(i)) {
FontDescription d = itemFont.fontDescription();
d.setWeight(d.bolderWeight());
itemFont = Font(d, itemFont.letterSpacing(), itemFont.wordSpacing());
itemFont.update(m_popupClient->fontSelector());
}
popupWidth = max(popupWidth, itemFont.width(TextRun(text.characters(), text.length())));
}
if (naturalHeight > maxPopupHeight)
// We need room for a scrollbar
popupWidth += ScrollbarTheme::nativeTheme()->scrollbarThickness(SmallScrollbar);
// Add padding to align the popup text with the <select> text
// Note: We can't add paddingRight() because that value includes the width
// of the dropdown button, so we must use our own endOfLinePadding constant.
popupWidth += max(0, endOfLinePadding - client()->clientInsetRight()) + max(0, client()->clientPaddingLeft() - client()->clientInsetLeft());
// Leave room for the border
popupWidth += 2 * popupWindowBorderWidth;
popupHeight += 2 * popupWindowBorderWidth;
// The popup should be at least as wide as the control on the page
popupWidth = max(rScreenCoords.width() - client()->clientInsetLeft() - client()->clientInsetRight(), popupWidth);
// Always left-align items in the popup. This matches popup menus on the mac.
int popupX = rScreenCoords.x() + client()->clientInsetLeft();
IntRect popupRect(popupX, rScreenCoords.bottom(), popupWidth, popupHeight);
// The popup needs to stay within the bounds of the screen and not overlap any toolbars
FloatRect screen = screenAvailableRect(v);
// Check that we don't go off the screen vertically
if (popupRect.bottom() > screen.height()) {
// The popup will go off the screen, so try placing it above the client
if (rScreenCoords.y() - popupRect.height() < 0) {
// The popup won't fit above, either, so place it whereever's bigger and resize it to fit
if ((rScreenCoords.y() + rScreenCoords.height() / 2) < (screen.height() / 2)) {
// Below is bigger
popupRect.setHeight(screen.height() - popupRect.y());
} else {
// Above is bigger
popupRect.setY(0);
popupRect.setHeight(rScreenCoords.y());
}
} else {
// The popup fits above, so reposition it
popupRect.setY(rScreenCoords.y() - popupRect.height());
}
}
// Check that we don't go off the screen horizontally
if (popupRect.x() < screen.x()) {
popupRect.setWidth(popupRect.width() - (screen.x() - popupRect.x()));
popupRect.setX(screen.x());
}
m_windowRect = popupRect;
return;
}
bool PopupMenu::setFocusedIndex(int i, bool hotTracking)
{
if (i < 0 || i >= client()->listSize() || i == focusedIndex())
return false;
if (!client()->itemIsEnabled(i))
return false;
invalidateItem(focusedIndex());
invalidateItem(i);
m_focusedIndex = i;
if (!hotTracking)
client()->setTextFromItem(i);
if (!scrollToRevealSelection())
::UpdateWindow(m_popup);
return true;
}
int PopupMenu::visibleItems() const
{
return clientRect().height() / m_itemHeight;
}
int PopupMenu::listIndexAtPoint(const IntPoint& point) const
{
return m_scrollOffset + point.y() / m_itemHeight;
}
int PopupMenu::focusedIndex() const
{
return m_focusedIndex;
}
void PopupMenu::focusFirst()
{
if (!client())
return;
int size = client()->listSize();
for (int i = 0; i < size; ++i)
if (client()->itemIsEnabled(i)) {
setFocusedIndex(i);
break;
}
}
void PopupMenu::focusLast()
{
if (!client())
return;
int size = client()->listSize();
for (int i = size - 1; i > 0; --i)
if (client()->itemIsEnabled(i)) {
setFocusedIndex(i);
break;
}
}
bool PopupMenu::down(unsigned lines)
{
if (!client())
return false;
int size = client()->listSize();
int lastSelectableIndex, selectedListIndex;
lastSelectableIndex = selectedListIndex = focusedIndex();
for (int i = selectedListIndex + 1; i >= 0 && i < size; ++i)
if (client()->itemIsEnabled(i)) {
lastSelectableIndex = i;
if (i >= selectedListIndex + (int)lines)
break;
}
return setFocusedIndex(lastSelectableIndex);
}
bool PopupMenu::up(unsigned lines)
{
if (!client())
return false;
int size = client()->listSize();
int lastSelectableIndex, selectedListIndex;
lastSelectableIndex = selectedListIndex = focusedIndex();
for (int i = selectedListIndex - 1; i >= 0 && i < size; --i)
if (client()->itemIsEnabled(i)) {
lastSelectableIndex = i;
if (i <= selectedListIndex - (int)lines)
break;
}
return setFocusedIndex(lastSelectableIndex);
}
void PopupMenu::invalidateItem(int index)
{
if (!m_popup)
return;
IntRect damageRect(clientRect());
damageRect.setY(m_itemHeight * (index - m_scrollOffset));
damageRect.setHeight(m_itemHeight);
if (m_scrollbar)
damageRect.setWidth(damageRect.width() - m_scrollbar->frameRect().width());
RECT r = damageRect;
::InvalidateRect(m_popup, &r, TRUE);
}
IntRect PopupMenu::clientRect() const
{
IntRect clientRect = m_windowRect;
clientRect.inflate(-popupWindowBorderWidth);
clientRect.setLocation(IntPoint(0, 0));
return clientRect;
}
void PopupMenu::incrementWheelDelta(int delta)
{
m_wheelDelta += delta;
}
void PopupMenu::reduceWheelDelta(int delta)
{
ASSERT(delta >= 0);
ASSERT(delta <= abs(m_wheelDelta));
if (m_wheelDelta > 0)
m_wheelDelta -= delta;
else if (m_wheelDelta < 0)
m_wheelDelta += delta;
else
return;
}
bool PopupMenu::scrollToRevealSelection()
{
if (!m_scrollbar)
return false;
int index = focusedIndex();
if (index < m_scrollOffset) {
m_scrollbar->setValue(index);
return true;
}
if (index >= m_scrollOffset + visibleItems()) {
m_scrollbar->setValue(index - visibleItems() + 1);
return true;
}
return false;
}
void PopupMenu::updateFromElement()
{
if (!m_popup)
return;
m_focusedIndex = client()->selectedIndex();
::InvalidateRect(m_popup, 0, TRUE);
if (!scrollToRevealSelection())
::UpdateWindow(m_popup);
}
bool PopupMenu::itemWritingDirectionIsNatural()
{
return true;
}
const int separatorPadding = 4;
const int separatorHeight = 1;
void PopupMenu::paint(const IntRect& damageRect, HDC hdc)
{
if (!m_popup)
return;
if (!m_DC) {
m_DC = ::CreateCompatibleDC(::GetDC(m_popup));
if (!m_DC)
return;
}
if (m_bmp) {
bool keepBitmap = false;
BITMAP bitmap;
if (GetObject(m_bmp, sizeof(bitmap), &bitmap))
keepBitmap = bitmap.bmWidth == clientRect().width()
&& bitmap.bmHeight == clientRect().height();
if (!keepBitmap) {
DeleteObject(m_bmp);
m_bmp = 0;
}
}
if (!m_bmp) {
#if PLATFORM(WINCE)
BitmapInfo bitmapInfo(true, clientRect().width(), clientRect().height());
#else
BitmapInfo bitmapInfo = BitmapInfo::createBottomUp(clientRect().size());
#endif
void* pixels = 0;
m_bmp = ::CreateDIBSection(m_DC, &bitmapInfo, DIB_RGB_COLORS, &pixels, 0, 0);
if (!m_bmp)
return;
::SelectObject(m_DC, m_bmp);
}
GraphicsContext context(m_DC);
int itemCount = client()->listSize();
// listRect is the damageRect translated into the coordinates of the entire menu list (which is itemCount * m_itemHeight pixels tall)
IntRect listRect = damageRect;
listRect.move(IntSize(0, m_scrollOffset * m_itemHeight));
for (int y = listRect.y(); y < listRect.bottom(); y += m_itemHeight) {
int index = y / m_itemHeight;
Color optionBackgroundColor, optionTextColor;
PopupMenuStyle itemStyle = client()->itemStyle(index);
if (index == focusedIndex()) {
optionBackgroundColor = RenderTheme::defaultTheme()->activeListBoxSelectionBackgroundColor();
optionTextColor = RenderTheme::defaultTheme()->activeListBoxSelectionForegroundColor();
} else {
optionBackgroundColor = itemStyle.backgroundColor();
optionTextColor = itemStyle.foregroundColor();
}
// itemRect is in client coordinates
IntRect itemRect(0, (index - m_scrollOffset) * m_itemHeight, damageRect.width(), m_itemHeight);
// Draw the background for this menu item
if (itemStyle.isVisible())
context.fillRect(itemRect, optionBackgroundColor);
if (client()->itemIsSeparator(index)) {
IntRect separatorRect(itemRect.x() + separatorPadding, itemRect.y() + (itemRect.height() - separatorHeight) / 2, itemRect.width() - 2 * separatorPadding, separatorHeight);
context.fillRect(separatorRect, optionTextColor);
continue;
}
String itemText = client()->itemText(index);
unsigned length = itemText.length();
const UChar* string = itemText.characters();
TextRun textRun(string, length, false, 0, 0, itemText.defaultWritingDirection() == WTF::Unicode::RightToLeft);
context.setFillColor(optionTextColor);
Font itemFont = client()->menuStyle().font();
if (client()->itemIsLabel(index)) {
FontDescription d = itemFont.fontDescription();
d.setWeight(d.bolderWeight());
itemFont = Font(d, itemFont.letterSpacing(), itemFont.wordSpacing());
itemFont.update(m_popupClient->fontSelector());
}
// Draw the item text
if (itemStyle.isVisible()) {
int textX = max(0, client()->clientPaddingLeft() - client()->clientInsetLeft());
if (RenderTheme::defaultTheme()->popupOptionSupportsTextIndent() && itemStyle.textDirection() == LTR)
textX += itemStyle.textIndent().calcMinValue(itemRect.width());
int textY = itemRect.y() + itemFont.ascent() + (itemRect.height() - itemFont.height()) / 2;
context.drawBidiText(itemFont, textRun, IntPoint(textX, textY));
}
}
if (m_scrollbar)
m_scrollbar->paint(&context, damageRect);
HDC localDC = hdc ? hdc : ::GetDC(m_popup);
::BitBlt(localDC, damageRect.x(), damageRect.y(), damageRect.width(), damageRect.height(), m_DC, damageRect.x(), damageRect.y(), SRCCOPY);
if (!hdc)
::ReleaseDC(m_popup, localDC);
}
void PopupMenu::valueChanged(Scrollbar* scrollBar)
{
ASSERT(m_scrollbar);
if (!m_popup)
return;
int offset = scrollBar->value();
if (m_scrollOffset == offset)
return;
int scrolledLines = m_scrollOffset - offset;
m_scrollOffset = offset;
UINT flags = SW_INVALIDATE;
#ifdef CAN_SET_SMOOTH_SCROLLING_DURATION
BOOL shouldSmoothScroll = FALSE;
::SystemParametersInfo(SPI_GETLISTBOXSMOOTHSCROLLING, 0, &shouldSmoothScroll, 0);
if (shouldSmoothScroll)
flags |= MAKEWORD(SW_SMOOTHSCROLL, smoothScrollAnimationDuration);
#endif
IntRect listRect = clientRect();
if (m_scrollbar)
listRect.setWidth(listRect.width() - m_scrollbar->frameRect().width());
RECT r = listRect;
::ScrollWindowEx(m_popup, 0, scrolledLines * m_itemHeight, &r, 0, 0, 0, flags);
if (m_scrollbar) {
r = m_scrollbar->frameRect();
::InvalidateRect(m_popup, &r, TRUE);
}
::UpdateWindow(m_popup);
}
void PopupMenu::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
{
IntRect scrollRect = rect;
scrollRect.move(scrollbar->x(), scrollbar->y());
RECT r = scrollRect;
::InvalidateRect(m_popup, &r, false);
}
static ATOM registerPopup()
{
static bool haveRegisteredWindowClass = false;
if (haveRegisteredWindowClass)
return true;
#if PLATFORM(WINCE)
WNDCLASS wcex;
#else
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hIconSm = 0;
wcex.style = CS_DROPSHADOW;
#endif
wcex.lpfnWndProc = PopupWndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(PopupMenu*); // For the PopupMenu pointer
wcex.hInstance = Page::instanceHandle();
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(0, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = kPopupWindowClassName;
haveRegisteredWindowClass = true;
#if PLATFORM(WINCE)
return ::RegisterClass(&wcex);
#else
return ::RegisterClassEx(&wcex);
#endif
}
const int smoothScrollAnimationDuration = 5000;
static LRESULT CALLBACK PopupWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = 0;
#if PLATFORM(WINCE)
LONG longPtr = GetWindowLong(hWnd, 0);
#else
LONG_PTR longPtr = GetWindowLongPtr(hWnd, 0);
#endif
PopupMenu* popup = reinterpret_cast<PopupMenu*>(longPtr);
switch (message) {
case WM_SIZE:
if (popup && popup->scrollbar()) {
IntSize size(LOWORD(lParam), HIWORD(lParam));
popup->scrollbar()->setFrameRect(IntRect(size.width() - popup->scrollbar()->width(), 0, popup->scrollbar()->width(), size.height()));
int visibleItems = popup->visibleItems();
popup->scrollbar()->setEnabled(visibleItems < popup->client()->listSize());
popup->scrollbar()->setSteps(1, max(1, visibleItems - 1));
popup->scrollbar()->setProportion(visibleItems, popup->client()->listSize());
}
break;
case WM_ACTIVATE:
if (popup && popup->client() && wParam == WA_INACTIVE)
popup->client()->hidePopup();
break;
case WM_KILLFOCUS:
if (popup && popup->client() && (HWND)wParam != popup->popupHandle())
// Focus is going elsewhere, so hide
popup->client()->hidePopup();
break;
case WM_KEYDOWN:
if (popup && popup->client()) {
lResult = 0;
switch (LOWORD(wParam)) {
case VK_DOWN:
case VK_RIGHT:
popup->down();
break;
case VK_UP:
case VK_LEFT:
popup->up();
break;
case VK_HOME:
popup->focusFirst();
break;
case VK_END:
popup->focusLast();
break;
case VK_PRIOR:
if (popup->focusedIndex() != popup->scrollOffset()) {
// Set the selection to the first visible item
int firstVisibleItem = popup->scrollOffset();
popup->up(popup->focusedIndex() - firstVisibleItem);
} else
// The first visible item is selected, so move the selection back one page
popup->up(popup->visibleItems());
break;
case VK_NEXT:
if (popup) {
int lastVisibleItem = popup->scrollOffset() + popup->visibleItems() - 1;
if (popup->focusedIndex() != lastVisibleItem) {
// Set the selection to the last visible item
popup->down(lastVisibleItem - popup->focusedIndex());
} else
// The last visible item is selected, so move the selection forward one page
popup->down(popup->visibleItems());
}
break;
case VK_TAB:
::SendMessage(popup->client()->hostWindow()->platformWindow(), message, wParam, lParam);
popup->client()->hidePopup();
break;
case VK_ESCAPE:
popup->client()->hidePopup();
break;
default:
if (isASCIIPrintable(wParam))
// Send the keydown to the WebView so it can be used for type-to-select.
::PostMessage(popup->client()->hostWindow()->platformWindow(), message, wParam, lParam);
else
lResult = 1;
break;
}
}
break;
case WM_CHAR:
if (popup && popup->client()) {
lResult = 0;
int index;
switch (wParam) {
case 0x0D: // Enter/Return
popup->client()->hidePopup();
index = popup->focusedIndex();
ASSERT(index >= 0);
popup->client()->valueChanged(index);
break;
case 0x1B: // Escape
popup->client()->hidePopup();
break;
case 0x09: // TAB
case 0x08: // Backspace
case 0x0A: // Linefeed
default: // Character
lResult = 1;
break;
}
}
break;
case WM_MOUSEMOVE:
if (popup) {
IntPoint mousePoint(MAKEPOINTS(lParam));
if (popup->scrollbar()) {
IntRect scrollBarRect = popup->scrollbar()->frameRect();
if (popup->scrollbarCapturingMouse() || scrollBarRect.contains(mousePoint)) {
// Put the point into coordinates relative to the scroll bar
mousePoint.move(-scrollBarRect.x(), -scrollBarRect.y());
PlatformMouseEvent event(hWnd, message, wParam, MAKELPARAM(mousePoint.x(), mousePoint.y()));
popup->scrollbar()->mouseMoved(event);
break;
}
}
BOOL shouldHotTrack = FALSE;
#if !PLATFORM(WINCE)
::SystemParametersInfo(SPI_GETHOTTRACKING, 0, &shouldHotTrack, 0);
#endif
RECT bounds;
GetClientRect(popup->popupHandle(), &bounds);
if ((shouldHotTrack || wParam & MK_LBUTTON) && ::PtInRect(&bounds, mousePoint))
popup->setFocusedIndex(popup->listIndexAtPoint(mousePoint), true);
// Release capture if the left button isn't down, and the mousePoint is outside the popup window.
// This way, the WebView will get future mouse events in the rest of the window.
if (!(wParam & MK_LBUTTON) && !::PtInRect(&bounds, mousePoint)) {
::ReleaseCapture();
break;
}
}
break;
case WM_LBUTTONDOWN:
if (popup) {
::SetCapture(popup->popupHandle());
IntPoint mousePoint(MAKEPOINTS(lParam));
if (popup->scrollbar()) {
IntRect scrollBarRect = popup->scrollbar()->frameRect();
if (scrollBarRect.contains(mousePoint)) {
// Put the point into coordinates relative to the scroll bar
mousePoint.move(-scrollBarRect.x(), -scrollBarRect.y());
PlatformMouseEvent event(hWnd, message, wParam, MAKELPARAM(mousePoint.x(), mousePoint.y()));
popup->scrollbar()->mouseDown(event);
popup->setScrollbarCapturingMouse(true);
break;
}
}
popup->setFocusedIndex(popup->listIndexAtPoint(mousePoint), true);
}
break;
case WM_LBUTTONUP:
if (popup) {
IntPoint mousePoint(MAKEPOINTS(lParam));
if (popup->scrollbar()) {
::ReleaseCapture();
IntRect scrollBarRect = popup->scrollbar()->frameRect();
if (popup->scrollbarCapturingMouse() || scrollBarRect.contains(mousePoint)) {
popup->setScrollbarCapturingMouse(false);
// Put the point into coordinates relative to the scroll bar
mousePoint.move(-scrollBarRect.x(), -scrollBarRect.y());
PlatformMouseEvent event(hWnd, message, wParam, MAKELPARAM(mousePoint.x(), mousePoint.y()));
popup->scrollbar()->mouseUp();
// FIXME: This is a hack to work around Scrollbar not invalidating correctly when it doesn't have a parent widget
RECT r = scrollBarRect;
::InvalidateRect(popup->popupHandle(), &r, TRUE);
break;
}
}
// Only release capture and hide the popup if the mouse is inside the popup window.
RECT bounds;
GetClientRect(popup->popupHandle(), &bounds);
if (popup->client() && ::PtInRect(&bounds, mousePoint)) {
::ReleaseCapture();
popup->client()->hidePopup();
int index = popup->focusedIndex();
if (index >= 0)
popup->client()->valueChanged(index);
}
}
break;
case WM_MOUSEWHEEL:
if (popup && popup->scrollbar()) {
int i = 0;
for (popup->incrementWheelDelta(GET_WHEEL_DELTA_WPARAM(wParam)); abs(popup->wheelDelta()) >= WHEEL_DELTA; popup->reduceWheelDelta(WHEEL_DELTA))
if (popup->wheelDelta() > 0)
++i;
else
--i;
popup->scrollbar()->scroll(i > 0 ? ScrollUp : ScrollDown, ScrollByLine, abs(i));
}
break;
case WM_PAINT:
if (popup) {
PAINTSTRUCT paintInfo;
::BeginPaint(popup->popupHandle(), &paintInfo);
popup->paint(paintInfo.rcPaint, paintInfo.hdc);
::EndPaint(popup->popupHandle(), &paintInfo);
lResult = 0;
}
break;
#if !PLATFORM(WINCE)
case WM_PRINTCLIENT:
if (popup)
popup->paint(popup->clientRect(), (HDC)wParam);
break;
#endif
default:
lResult = DefWindowProc(hWnd, message, wParam, lParam);
}
return lResult;
}
}
|