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
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "views/controls/menu/submenu_view.h"
#include "app/gfx/canvas.h"
#include "views/controls/menu/menu_controller.h"
#include "views/controls/menu/menu_scroll_view_container.h"
#include "views/widget/root_view.h"
#if defined(OS_WIN)
#include "views/controls/menu/menu_host_win.h"
#elif defined(OS_LINUX)
#include "views/controls/menu/menu_host_gtk.h"
#endif
// Height of the drop indicator. This should be an even number.
static const int kDropIndicatorHeight = 2;
// Color of the drop indicator.
static const SkColor kDropIndicatorColor = SK_ColorBLACK;
namespace views {
// static
const int SubmenuView::kSubmenuBorderSize = 3;
SubmenuView::SubmenuView(MenuItemView* parent)
: parent_menu_item_(parent),
host_(NULL),
drop_item_(NULL),
drop_position_(MenuDelegate::DROP_NONE),
scroll_view_container_(NULL) {
DCHECK(parent);
// We'll delete ourselves, otherwise the ScrollView would delete us on close.
SetParentOwned(false);
}
SubmenuView::~SubmenuView() {
// The menu may not have been closed yet (it will be hidden, but not
// necessarily closed).
Close();
delete scroll_view_container_;
}
int SubmenuView::GetMenuItemCount() {
int count = 0;
for (int i = 0; i < GetChildViewCount(); ++i) {
if (GetChildViewAt(i)->GetID() == MenuItemView::kMenuItemViewID)
count++;
}
return count;
}
MenuItemView* SubmenuView::GetMenuItemAt(int index) {
for (int i = 0, count = 0; i < GetChildViewCount(); ++i) {
if (GetChildViewAt(i)->GetID() == MenuItemView::kMenuItemViewID &&
count++ == index) {
return static_cast<MenuItemView*>(GetChildViewAt(i));
}
}
NOTREACHED();
return NULL;
}
void SubmenuView::Layout() {
// We're in a ScrollView, and need to set our width/height ourselves.
View* parent = GetParent();
if (!parent)
return;
SetBounds(x(), y(), parent->width(), GetPreferredSize().height());
gfx::Insets insets = GetInsets();
int x = insets.left();
int y = insets.top();
int menu_item_width = width() - insets.width();
for (int i = 0; i < GetChildViewCount(); ++i) {
View* child = GetChildViewAt(i);
gfx::Size child_pref_size = child->GetPreferredSize();
child->SetBounds(x, y, menu_item_width, child_pref_size.height());
y += child_pref_size.height();
}
}
gfx::Size SubmenuView::GetPreferredSize() {
if (GetChildViewCount() == 0)
return gfx::Size();
int max_width = 0;
int height = 0;
for (int i = 0; i < GetChildViewCount(); ++i) {
View* child = GetChildViewAt(i);
gfx::Size child_pref_size = child->GetPreferredSize();
max_width = std::max(max_width, child_pref_size.width());
height += child_pref_size.height();
}
gfx::Insets insets = GetInsets();
return gfx::Size(max_width + insets.width(), height + insets.height());
}
void SubmenuView::DidChangeBounds(const gfx::Rect& previous,
const gfx::Rect& current) {
SchedulePaint();
}
void SubmenuView::PaintChildren(gfx::Canvas* canvas) {
View::PaintChildren(canvas);
if (drop_item_ && drop_position_ != MenuDelegate::DROP_ON)
PaintDropIndicator(canvas, drop_item_, drop_position_);
}
bool SubmenuView::GetDropFormats(
int* formats,
std::set<OSExchangeData::CustomFormat>* custom_formats) {
DCHECK(GetMenuItem()->GetMenuController());
return GetMenuItem()->GetMenuController()->GetDropFormats(this, formats,
custom_formats);
}
bool SubmenuView::AreDropTypesRequired() {
DCHECK(GetMenuItem()->GetMenuController());
return GetMenuItem()->GetMenuController()->AreDropTypesRequired(this);
}
bool SubmenuView::CanDrop(const OSExchangeData& data) {
DCHECK(GetMenuItem()->GetMenuController());
return GetMenuItem()->GetMenuController()->CanDrop(this, data);
}
void SubmenuView::OnDragEntered(const DropTargetEvent& event) {
DCHECK(GetMenuItem()->GetMenuController());
GetMenuItem()->GetMenuController()->OnDragEntered(this, event);
}
int SubmenuView::OnDragUpdated(const DropTargetEvent& event) {
DCHECK(GetMenuItem()->GetMenuController());
return GetMenuItem()->GetMenuController()->OnDragUpdated(this, event);
}
void SubmenuView::OnDragExited() {
DCHECK(GetMenuItem()->GetMenuController());
GetMenuItem()->GetMenuController()->OnDragExited(this);
}
int SubmenuView::OnPerformDrop(const DropTargetEvent& event) {
DCHECK(GetMenuItem()->GetMenuController());
return GetMenuItem()->GetMenuController()->OnPerformDrop(this, event);
}
bool SubmenuView::OnMouseWheel(const MouseWheelEvent& e) {
gfx::Rect vis_bounds = GetVisibleBounds();
int menu_item_count = GetMenuItemCount();
if (vis_bounds.height() == height() || !menu_item_count) {
// All menu items are visible, nothing to scroll.
return true;
}
// Find the index of the first menu item whose y-coordinate is >= visible
// y-coordinate.
int first_vis_index = -1;
for (int i = 0; i < menu_item_count; ++i) {
MenuItemView* menu_item = GetMenuItemAt(i);
if (menu_item->y() == vis_bounds.y()) {
first_vis_index = i;
break;
} else if (menu_item->y() > vis_bounds.y()) {
first_vis_index = std::max(0, i - 1);
break;
}
}
if (first_vis_index == -1)
return true;
// If the first item isn't entirely visible, make it visible, otherwise make
// the next/previous one entirely visible.
#if defined(OS_WIN)
int delta = abs(e.GetOffset() / WHEEL_DELTA);
#elif defined(OS_LINUX)
int delta = abs(e.GetOffset());
#endif
bool scroll_up = (e.GetOffset() > 0);
while (delta-- > 0) {
int scroll_amount = 0;
if (scroll_up) {
if (GetMenuItemAt(first_vis_index)->y() == vis_bounds.y()) {
if (first_vis_index != 0) {
scroll_amount = GetMenuItemAt(first_vis_index - 1)->y() -
vis_bounds.y();
first_vis_index--;
} else {
break;
}
} else {
scroll_amount = GetMenuItemAt(first_vis_index)->y() - vis_bounds.y();
}
} else {
if (first_vis_index + 1 == GetMenuItemCount())
break;
scroll_amount = GetMenuItemAt(first_vis_index + 1)->y() -
vis_bounds.y();
if (GetMenuItemAt(first_vis_index)->y() == vis_bounds.y())
first_vis_index++;
}
ScrollRectToVisible(0, vis_bounds.y() + scroll_amount, vis_bounds.width(),
vis_bounds.height());
vis_bounds = GetVisibleBounds();
}
return true;
}
bool SubmenuView::IsShowing() {
return host_ && host_->IsVisible();
}
void SubmenuView::ShowAt(gfx::NativeWindow parent,
const gfx::Rect& bounds,
bool do_capture) {
if (host_) {
host_->Show();
return;
}
host_ = new MenuHost(this);
// Force construction of the scroll view container.
GetScrollViewContainer();
// Make sure the first row is visible.
ScrollRectToVisible(0, 0, 1, 1);
host_->Init(parent, bounds, scroll_view_container_, do_capture);
}
void SubmenuView::Close() {
if (host_) {
host_->Close();
host_ = NULL;
}
}
void SubmenuView::Hide() {
if (host_)
host_->HideWindow();
}
void SubmenuView::ReleaseCapture() {
host_->ReleaseCapture();
}
bool SubmenuView::SkipDefaultKeyEventProcessing(const views::KeyEvent& e) {
return views::FocusManager::IsTabTraversalKeyEvent(e);
}
void SubmenuView::SetDropMenuItem(MenuItemView* item,
MenuDelegate::DropPosition position) {
if (drop_item_ == item && drop_position_ == position)
return;
SchedulePaintForDropIndicator(drop_item_, drop_position_);
drop_item_ = item;
drop_position_ = position;
SchedulePaintForDropIndicator(drop_item_, drop_position_);
}
bool SubmenuView::GetShowSelection(MenuItemView* item) {
if (drop_item_ == NULL)
return true;
// Something is being dropped on one of this menus items. Show the
// selection if the drop is on the passed in item and the drop position is
// ON.
return (drop_item_ == item && drop_position_ == MenuDelegate::DROP_ON);
}
MenuScrollViewContainer* SubmenuView::GetScrollViewContainer() {
if (!scroll_view_container_) {
scroll_view_container_ = new MenuScrollViewContainer(this);
// Otherwise MenuHost would delete us.
scroll_view_container_->SetParentOwned(false);
}
return scroll_view_container_;
}
gfx::NativeWindow SubmenuView::native_window() const {
return host_ ? host_->GetNativeWindow() : NULL;
}
void SubmenuView::PaintDropIndicator(gfx::Canvas* canvas,
MenuItemView* item,
MenuDelegate::DropPosition position) {
if (position == MenuDelegate::DROP_NONE)
return;
gfx::Rect bounds = CalculateDropIndicatorBounds(item, position);
canvas->FillRectInt(kDropIndicatorColor, bounds.x(), bounds.y(),
bounds.width(), bounds.height());
}
void SubmenuView::SchedulePaintForDropIndicator(
MenuItemView* item,
MenuDelegate::DropPosition position) {
if (item == NULL)
return;
if (position == MenuDelegate::DROP_ON) {
item->SchedulePaint();
} else if (position != MenuDelegate::DROP_NONE) {
gfx::Rect bounds = CalculateDropIndicatorBounds(item, position);
SchedulePaint(bounds.x(), bounds.y(), bounds.width(), bounds.height());
}
}
gfx::Rect SubmenuView::CalculateDropIndicatorBounds(
MenuItemView* item,
MenuDelegate::DropPosition position) {
DCHECK(position != MenuDelegate::DROP_NONE);
gfx::Rect item_bounds = item->bounds();
switch (position) {
case MenuDelegate::DROP_BEFORE:
item_bounds.Offset(0, -kDropIndicatorHeight / 2);
item_bounds.set_height(kDropIndicatorHeight);
return item_bounds;
case MenuDelegate::DROP_AFTER:
item_bounds.Offset(0, item_bounds.height() - kDropIndicatorHeight / 2);
item_bounds.set_height(kDropIndicatorHeight);
return item_bounds;
default:
// Don't render anything for on.
return gfx::Rect();
}
}
} // namespace views
|