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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
|
// Copyright (c) 2010 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/view.h"
#include <algorithm>
#ifndef NDEBUG
#include <iostream>
#endif
#include "app/drag_drop_types.h"
#include "app/gfx/canvas.h"
#include "app/gfx/path.h"
#include "app/l10n_util.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/scoped_handle.h"
#include "base/string_util.h"
#include "third_party/skia/include/core/SkShader.h"
#include "views/background.h"
#include "views/layout_manager.h"
#include "views/views_delegate.h"
#include "views/widget/root_view.h"
#include "views/widget/tooltip_manager.h"
#include "views/widget/widget.h"
#include "views/window/window.h"
#if defined(OS_WIN)
#include "views/accessibility/view_accessibility_wrapper.h"
#endif
#if defined(OS_LINUX)
#include "app/scoped_handle_gtk.h"
#endif
namespace views {
// static
ViewsDelegate* ViewsDelegate::views_delegate = NULL;
// static
char View::kViewClassName[] = "views/View";
// static
const int View::kShowFolderDropMenuDelay = 400;
/////////////////////////////////////////////////////////////////////////////
//
// View - constructors, destructors, initialization
//
/////////////////////////////////////////////////////////////////////////////
View::View()
: id_(0),
group_(-1),
enabled_(true),
focusable_(false),
bounds_(0, 0, 0, 0),
parent_(NULL),
is_visible_(true),
is_parent_owned_(true),
notify_when_visible_bounds_in_root_changes_(false),
registered_for_visible_bounds_notification_(false),
accelerator_registration_delayed_(false),
next_focusable_view_(NULL),
previous_focusable_view_(NULL),
accelerator_focus_manager_(NULL),
registered_accelerator_count_(0),
context_menu_controller_(NULL),
#if defined(OS_WIN)
accessibility_(NULL),
#endif
drag_controller_(NULL),
ui_mirroring_is_enabled_for_rtl_languages_(true),
flip_canvas_on_paint_for_rtl_ui_(false) {
}
View::~View() {
int c = static_cast<int>(child_views_.size());
while (--c >= 0) {
if (child_views_[c]->IsParentOwned())
delete child_views_[c];
else
child_views_[c]->SetParent(NULL);
}
#if defined(OS_WIN)
if (accessibility_.get())
accessibility_->Uninitialize();
#endif
}
/////////////////////////////////////////////////////////////////////////////
//
// View - sizing
//
/////////////////////////////////////////////////////////////////////////////
gfx::Rect View::GetBounds(PositionMirroringSettings settings) const {
gfx::Rect bounds(bounds_);
// If the parent uses an RTL UI layout and if we are asked to transform the
// bounds to their mirrored position if necessary, then we should shift the
// rectangle appropriately.
if (settings == APPLY_MIRRORING_TRANSFORMATION)
bounds.set_x(MirroredX());
return bounds;
}
// y(), width() and height() are agnostic to the RTL UI layout of the
// parent view. x(), on the other hand, is not.
int View::GetX(PositionMirroringSettings settings) const {
return settings == IGNORE_MIRRORING_TRANSFORMATION ? x() : MirroredX();
}
void View::SetBounds(const gfx::Rect& bounds) {
if (bounds == bounds_)
return;
gfx::Rect prev = bounds_;
bounds_ = bounds;
DidChangeBounds(prev, bounds_);
RootView* root = GetRootView();
if (root) {
bool size_changed = prev.size() != bounds_.size();
bool position_changed = prev.origin() != bounds_.origin();
if (size_changed || position_changed)
root->ViewBoundsChanged(this, size_changed, position_changed);
}
}
gfx::Rect View::GetLocalBounds(bool include_border) const {
if (include_border || !border_.get())
return gfx::Rect(0, 0, width(), height());
gfx::Insets insets;
border_->GetInsets(&insets);
return gfx::Rect(insets.left(), insets.top(),
std::max(0, width() - insets.width()),
std::max(0, height() - insets.height()));
}
gfx::Point View::GetPosition() const {
return gfx::Point(GetX(APPLY_MIRRORING_TRANSFORMATION), y());
}
gfx::Size View::GetPreferredSize() {
if (layout_manager_.get())
return layout_manager_->GetPreferredSize(this);
return gfx::Size();
}
int View::GetBaseline() {
return -1;
}
void View::SizeToPreferredSize() {
gfx::Size prefsize = GetPreferredSize();
if ((prefsize.width() != width()) || (prefsize.height() != height()))
SetBounds(x(), y(), prefsize.width(), prefsize.height());
}
void View::PreferredSizeChanged() {
if (parent_)
parent_->ChildPreferredSizeChanged(this);
}
gfx::Size View::GetMinimumSize() {
return GetPreferredSize();
}
int View::GetHeightForWidth(int w) {
if (layout_manager_.get())
return layout_manager_->GetPreferredHeightForWidth(this, w);
return GetPreferredSize().height();
}
void View::DidChangeBounds(const gfx::Rect& previous,
const gfx::Rect& current) {
Layout();
}
void View::ScrollRectToVisible(int x, int y, int width, int height) {
View* parent = GetParent();
// We must take RTL UI mirroring into account when adjusting the position of
// the region.
if (parent)
parent->ScrollRectToVisible(
GetX(APPLY_MIRRORING_TRANSFORMATION) + x, View::y() + y, width, height);
}
/////////////////////////////////////////////////////////////////////////////
//
// View - layout
//
/////////////////////////////////////////////////////////////////////////////
void View::Layout() {
// Layout child Views
if (layout_manager_.get()) {
layout_manager_->Layout(this);
SchedulePaint();
// TODO(beng): We believe the right thing to do here is return since the
// layout manager should be handling things, but it causes
// regressions (missing options from Options dialog and a hang
// in interactive_ui_tests).
}
// Lay out contents of child Views
for (int i = 0, count = GetChildViewCount(); i < count; ++i) {
View* child = GetChildViewAt(i);
child->Layout();
}
}
LayoutManager* View::GetLayoutManager() const {
return layout_manager_.get();
}
void View::SetLayoutManager(LayoutManager* layout_manager) {
if (layout_manager_.get())
layout_manager_->Uninstalled(this);
layout_manager_.reset(layout_manager);
if (layout_manager_.get())
layout_manager_->Installed(this);
}
bool View::UILayoutIsRightToLeft() const {
return (ui_mirroring_is_enabled_for_rtl_languages_ &&
l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT);
}
////////////////////////////////////////////////////////////////////////////////
//
// View - Right-to-left UI layout
//
////////////////////////////////////////////////////////////////////////////////
inline int View::MirroredX() const {
// TODO(beng): reimplement in terms of MirroredLeftPointForRect.
View* parent = GetParent();
if (parent && parent->UILayoutIsRightToLeft())
return parent->width() - x() - width();
return x();
}
int View::MirroredLeftPointForRect(const gfx::Rect& bounds) const {
if (!UILayoutIsRightToLeft())
return bounds.x();
return width() - bounds.x() - bounds.width();
}
////////////////////////////////////////////////////////////////////////////////
//
// View - states
//
////////////////////////////////////////////////////////////////////////////////
bool View::IsEnabled() const {
return enabled_;
}
void View::SetEnabled(bool state) {
if (enabled_ != state) {
enabled_ = state;
SchedulePaint();
}
}
bool View::IsFocusable() const {
return focusable_ && enabled_ && is_visible_;
}
void View::SetFocusable(bool focusable) {
focusable_ = focusable;
}
FocusManager* View::GetFocusManager() {
Widget* widget = GetWidget();
return widget ? widget->GetFocusManager() : NULL;
}
bool View::HasFocus() {
FocusManager* focus_manager = GetFocusManager();
if (focus_manager)
return focus_manager->GetFocusedView() == this;
return false;
}
void View::Focus() {
// By default, we clear the native focus. This ensures that no visible native
// view as the focus and that we still receive keyboard inputs.
FocusManager* focus_manager = GetFocusManager();
if (focus_manager)
focus_manager->ClearNativeFocus();
}
void View::SetHotTracked(bool flag) {
}
/////////////////////////////////////////////////////////////////////////////
//
// View - painting
//
/////////////////////////////////////////////////////////////////////////////
void View::SchedulePaint(const gfx::Rect& r, bool urgent) {
if (!IsVisible())
return;
if (parent_) {
// Translate the requested paint rect to the parent's coordinate system
// then pass this notification up to the parent.
gfx::Rect paint_rect = r;
paint_rect.Offset(GetPosition());
parent_->SchedulePaint(paint_rect, urgent);
}
}
void View::SchedulePaint() {
SchedulePaint(GetLocalBounds(true), false);
}
void View::SchedulePaint(int x, int y, int w, int h) {
SchedulePaint(gfx::Rect(x, y, w, h), false);
}
void View::Paint(gfx::Canvas* canvas) {
PaintBackground(canvas);
PaintFocusBorder(canvas);
PaintBorder(canvas);
}
void View::PaintBackground(gfx::Canvas* canvas) {
if (background_.get())
background_->Paint(canvas, this);
}
void View::PaintBorder(gfx::Canvas* canvas) {
if (border_.get())
border_->Paint(*this, canvas);
}
void View::PaintFocusBorder(gfx::Canvas* canvas) {
if (HasFocus() && IsFocusable())
canvas->DrawFocusRect(0, 0, width(), height());
}
void View::PaintChildren(gfx::Canvas* canvas) {
for (int i = 0, count = GetChildViewCount(); i < count; ++i) {
View* child = GetChildViewAt(i);
if (!child) {
NOTREACHED() << "Should not have a NULL child View for index in bounds";
continue;
}
child->ProcessPaint(canvas);
}
}
void View::ProcessPaint(gfx::Canvas* canvas) {
if (!IsVisible())
return;
// We're going to modify the canvas, save it's state first.
canvas->save();
// Paint this View and its children, setting the clip rect to the bounds
// of this View and translating the origin to the local bounds' top left
// point.
//
// Note that the X (or left) position we pass to ClipRectInt takes into
// consideration whether or not the view uses a right-to-left layout so that
// we paint our view in its mirrored position if need be.
if (canvas->ClipRectInt(MirroredX(), y(), width(), height())) {
// Non-empty clip, translate the graphics such that 0,0 corresponds to
// where this view is located (related to its parent).
canvas->TranslateInt(MirroredX(), y());
// Save the state again, so that any changes don't effect PaintChildren.
canvas->save();
// If the View we are about to paint requested the canvas to be flipped, we
// should change the transform appropriately.
bool flip_canvas = FlipCanvasOnPaintForRTLUI();
if (flip_canvas) {
canvas->TranslateInt(width(), 0);
canvas->ScaleInt(-1, 1);
canvas->save();
}
Paint(canvas);
// We must undo the canvas mirroring once the View is done painting so that
// we don't pass the canvas with the mirrored transform to Views that
// didn't request the canvas to be flipped.
if (flip_canvas)
canvas->restore();
canvas->restore();
PaintChildren(canvas);
}
// Restore the canvas's original transform.
canvas->restore();
}
void View::PaintNow() {
if (!IsVisible())
return;
View* view = GetParent();
if (view)
view->PaintNow();
}
gfx::Insets View::GetInsets() const {
gfx::Insets insets;
if (border_.get())
border_->GetInsets(&insets);
return insets;
}
gfx::NativeCursor View::GetCursorForPoint(Event::EventType event_type, int x,
int y) {
return NULL;
}
bool View::HitTest(const gfx::Point& l) const {
if (l.x() >= 0 && l.x() < width() && l.y() >= 0 && l.y() < height()) {
if (HasHitTestMask()) {
gfx::Path mask;
GetHitTestMask(&mask);
ScopedRegion rgn(mask.CreateNativeRegion());
// TODO: can this use SkRegion's contains instead?
#if defined(OS_WIN)
return !!PtInRegion(rgn, l.x(), l.y());
#elif defined(TOOLKIT_USES_GTK)
return gdk_region_point_in(rgn.Get(), l.x(), l.y());
#endif
}
// No mask, but inside our bounds.
return true;
}
// Outside our bounds.
return false;
}
void View::SetContextMenuController(ContextMenuController* menu_controller) {
context_menu_controller_ = menu_controller;
}
void View::ShowContextMenu(int x, int y, bool is_mouse_gesture) {
if (!context_menu_controller_)
return;
context_menu_controller_->ShowContextMenu(this, x, y, is_mouse_gesture);
}
/////////////////////////////////////////////////////////////////////////////
//
// View - tree
//
/////////////////////////////////////////////////////////////////////////////
bool View::ProcessMousePressed(const MouseEvent& e, DragInfo* drag_info) {
const bool enabled = enabled_;
int drag_operations;
if (enabled && e.IsOnlyLeftMouseButton() && HitTest(e.location()))
drag_operations = GetDragOperations(e.x(), e.y());
else
drag_operations = 0;
ContextMenuController* context_menu_controller =
e.IsRightMouseButton() ? context_menu_controller_ : 0;
const bool result = OnMousePressed(e);
// WARNING: we may have been deleted, don't use any View variables;
if (!enabled)
return result;
if (drag_operations != DragDropTypes::DRAG_NONE) {
drag_info->PossibleDrag(e.x(), e.y());
return true;
}
return !!context_menu_controller || result;
}
bool View::ProcessMouseDragged(const MouseEvent& e, DragInfo* drag_info) {
// Copy the field, that way if we're deleted after drag and drop no harm is
// done.
ContextMenuController* context_menu_controller = context_menu_controller_;
const bool possible_drag = drag_info->possible_drag;
if (possible_drag && ExceededDragThreshold(drag_info->start_x - e.x(),
drag_info->start_y - e.y())) {
if (!drag_controller_ ||
drag_controller_->CanStartDrag(this,
drag_info->start_x,
drag_info->start_y,
e.x(),
e.y()))
DoDrag(e, drag_info->start_x, drag_info->start_y);
} else {
if (OnMouseDragged(e))
return true;
// Fall through to return value based on context menu controller.
}
// WARNING: we may have been deleted.
return (context_menu_controller != NULL) || possible_drag;
}
void View::ProcessMouseReleased(const MouseEvent& e, bool canceled) {
if (!canceled && context_menu_controller_ && e.IsOnlyRightMouseButton()) {
// Assume that if there is a context menu controller we won't be deleted
// from mouse released.
gfx::Point location(e.location());
OnMouseReleased(e, canceled);
if (HitTest(location)) {
ConvertPointToScreen(this, &location);
ShowContextMenu(location.x(), location.y(), true);
}
} else {
OnMouseReleased(e, canceled);
}
// WARNING: we may have been deleted.
}
void View::AddChildView(View* v) {
AddChildView(static_cast<int>(child_views_.size()), v);
}
void View::AddChildView(int index, View* v) {
CHECK(v != this) << "You cannot add a view as its own child";
// Remove the view from its current parent if any.
if (v->GetParent())
v->GetParent()->RemoveChildView(v);
// Sets the prev/next focus views.
InitFocusSiblings(v, index);
// Let's insert the view.
child_views_.insert(child_views_.begin() + index, v);
v->SetParent(this);
for (View* p = this; p; p = p->GetParent())
p->ViewHierarchyChangedImpl(false, true, this, v);
v->PropagateAddNotifications(this, v);
UpdateTooltip();
RootView* root = GetRootView();
if (root)
RegisterChildrenForVisibleBoundsNotification(root, v);
if (layout_manager_.get())
layout_manager_->ViewAdded(this, v);
}
View* View::GetChildViewAt(int index) const {
return index < GetChildViewCount() ? child_views_[index] : NULL;
}
int View::GetChildViewCount() const {
return static_cast<int>(child_views_.size());
}
void View::RemoveChildView(View* a_view) {
DoRemoveChildView(a_view, true, true, false);
}
void View::RemoveAllChildViews(bool delete_views) {
ViewList::iterator iter;
while ((iter = child_views_.begin()) != child_views_.end()) {
DoRemoveChildView(*iter, false, false, delete_views);
}
UpdateTooltip();
}
void View::DoDrag(const MouseEvent& e, int press_x, int press_y) {
int drag_operations = GetDragOperations(press_x, press_y);
if (drag_operations == DragDropTypes::DRAG_NONE)
return;
OSExchangeData data;
WriteDragData(press_x, press_y, &data);
// Message the RootView to do the drag and drop. That way if we're removed
// the RootView can detect it and avoid calling us back.
RootView* root_view = GetRootView();
root_view->StartDragForViewFromMouseEvent(this, data, drag_operations);
}
void View::DoRemoveChildView(View* a_view,
bool update_focus_cycle,
bool update_tool_tip,
bool delete_removed_view) {
#ifndef NDEBUG
DCHECK(!IsProcessingPaint()) << "Should not be removing a child view " <<
"during a paint, this will seriously " <<
"mess things up!";
#endif
DCHECK(a_view);
const ViewList::iterator i = find(child_views_.begin(),
child_views_.end(),
a_view);
if (i != child_views_.end()) {
if (update_focus_cycle) {
// Let's remove the view from the focus traversal.
View* next_focusable = a_view->next_focusable_view_;
View* prev_focusable = a_view->previous_focusable_view_;
if (prev_focusable)
prev_focusable->next_focusable_view_ = next_focusable;
if (next_focusable)
next_focusable->previous_focusable_view_ = prev_focusable;
}
RootView* root = GetRootView();
if (root)
UnregisterChildrenForVisibleBoundsNotification(root, a_view);
a_view->PropagateRemoveNotifications(this);
a_view->SetParent(NULL);
if (delete_removed_view && a_view->IsParentOwned())
delete a_view;
child_views_.erase(i);
}
if (update_tool_tip)
UpdateTooltip();
if (layout_manager_.get())
layout_manager_->ViewRemoved(this, a_view);
}
void View::PropagateRemoveNotifications(View* parent) {
for (int i = 0, count = GetChildViewCount(); i < count; ++i)
GetChildViewAt(i)->PropagateRemoveNotifications(parent);
for (View* v = this; v; v = v->GetParent())
v->ViewHierarchyChangedImpl(true, false, parent, this);
}
void View::PropagateAddNotifications(View* parent, View* child) {
for (int i = 0, count = GetChildViewCount(); i < count; ++i)
GetChildViewAt(i)->PropagateAddNotifications(parent, child);
ViewHierarchyChangedImpl(true, true, parent, child);
}
void View::ThemeChanged() {
int c = GetChildViewCount();
for (int i = c - 1; i >= 0; --i)
GetChildViewAt(i)->ThemeChanged();
}
#ifndef NDEBUG
bool View::IsProcessingPaint() const {
return GetParent() && GetParent()->IsProcessingPaint();
}
#endif
gfx::Point View::GetKeyboardContextMenuLocation() {
gfx::Rect vis_bounds = GetVisibleBounds();
gfx::Point screen_point(vis_bounds.x() + vis_bounds.width() / 2,
vis_bounds.y() + vis_bounds.height() / 2);
ConvertPointToScreen(this, &screen_point);
return screen_point;
}
bool View::HasHitTestMask() const {
return false;
}
void View::GetHitTestMask(gfx::Path* mask) const {
DCHECK(mask);
}
void View::ViewHierarchyChanged(bool is_add,
View* parent,
View* child) {
}
void View::ViewHierarchyChangedImpl(bool register_accelerators,
bool is_add,
View* parent,
View* child) {
if (register_accelerators) {
if (is_add) {
// If you get this registration, you are part of a subtree that has been
// added to the view hierarchy.
if (GetFocusManager()) {
RegisterPendingAccelerators();
} else {
// Delay accelerator registration until visible as we do not have
// focus manager until then.
accelerator_registration_delayed_ = true;
}
} else {
if (child == this)
UnregisterAccelerators(true);
}
}
ViewHierarchyChanged(is_add, parent, child);
}
void View::PropagateVisibilityNotifications(View* start, bool is_visible) {
for (int i = 0, count = GetChildViewCount(); i < count; ++i)
GetChildViewAt(i)->PropagateVisibilityNotifications(start, is_visible);
VisibilityChanged(start, is_visible);
}
void View::VisibilityChanged(View* starting_from, bool is_visible) {
}
void View::PropagateNativeViewHierarchyChanged(bool attached,
gfx::NativeView native_view,
RootView* root_view) {
for (int i = 0, count = GetChildViewCount(); i < count; ++i)
GetChildViewAt(i)->PropagateNativeViewHierarchyChanged(attached,
native_view,
root_view);
NativeViewHierarchyChanged(attached, native_view, root_view);
}
void View::NativeViewHierarchyChanged(bool attached,
gfx::NativeView native_view,
RootView* root_view) {
FocusManager* focus_manager = GetFocusManager();
if (!accelerator_registration_delayed_ &&
accelerator_focus_manager_ &&
accelerator_focus_manager_ != focus_manager) {
UnregisterAccelerators(true);
accelerator_registration_delayed_ = true;
}
if (accelerator_registration_delayed_ && attached) {
if (focus_manager) {
RegisterPendingAccelerators();
accelerator_registration_delayed_ = false;
}
}
}
void View::SetNotifyWhenVisibleBoundsInRootChanges(bool value) {
if (notify_when_visible_bounds_in_root_changes_ == value)
return;
notify_when_visible_bounds_in_root_changes_ = value;
RootView* root = GetRootView();
if (root) {
if (value)
root->RegisterViewForVisibleBoundsNotification(this);
else
root->UnregisterViewForVisibleBoundsNotification(this);
}
}
bool View::GetNotifyWhenVisibleBoundsInRootChanges() {
return notify_when_visible_bounds_in_root_changes_;
}
View* View::GetViewForPoint(const gfx::Point& point) {
// Walk the child Views recursively looking for the View that most
// tightly encloses the specified point.
for (int i = GetChildViewCount() - 1; i >= 0; --i) {
View* child = GetChildViewAt(i);
if (!child->IsVisible())
continue;
gfx::Point point_in_child_coords(point);
View::ConvertPointToView(this, child, &point_in_child_coords);
if (child->HitTest(point_in_child_coords))
return child->GetViewForPoint(point_in_child_coords);
}
return this;
}
Widget* View::GetWidget() const {
// The root view holds a reference to this view hierarchy's Widget.
return parent_ ? parent_->GetWidget() : NULL;
}
Window* View::GetWindow() const {
Widget* widget = GetWidget();
return widget ? widget->GetWindow() : NULL;
}
// Get the containing RootView
RootView* View::GetRootView() {
Widget* widget = GetWidget();
return widget ? widget->GetRootView() : NULL;
}
View* View::GetViewByID(int id) const {
if (id == id_)
return const_cast<View*>(this);
for (int i = 0, count = GetChildViewCount(); i < count; ++i) {
View* child = GetChildViewAt(i);
View* view = child->GetViewByID(id);
if (view)
return view;
}
return NULL;
}
void View::GetViewsWithGroup(int group_id, std::vector<View*>* out) {
if (group_ == group_id)
out->push_back(this);
for (int i = 0, count = GetChildViewCount(); i < count; ++i)
GetChildViewAt(i)->GetViewsWithGroup(group_id, out);
}
View* View::GetSelectedViewForGroup(int group_id) {
std::vector<View*> views;
GetRootView()->GetViewsWithGroup(group_id, &views);
if (views.size() > 0)
return views[0];
else
return NULL;
}
void View::SetID(int id) {
id_ = id;
}
int View::GetID() const {
return id_;
}
void View::SetGroup(int gid) {
// Don't change the group id once it's set.
DCHECK(group_ == -1 || group_ == gid);
group_ = gid;
}
int View::GetGroup() const {
return group_;
}
void View::SetParent(View* parent) {
if (parent != parent_)
parent_ = parent;
}
bool View::IsParentOf(View* v) const {
DCHECK(v);
View* parent = v->GetParent();
while (parent) {
if (this == parent)
return true;
parent = parent->GetParent();
}
return false;
}
int View::GetChildIndex(View* v) const {
for (int i = 0, count = GetChildViewCount(); i < count; i++) {
if (v == GetChildViewAt(i))
return i;
}
return -1;
}
///////////////////////////////////////////////////////////////////////////////
//
// View - focus
//
///////////////////////////////////////////////////////////////////////////////
View* View::GetNextFocusableView() {
return next_focusable_view_;
}
View* View::GetPreviousFocusableView() {
return previous_focusable_view_;
}
void View::SetNextFocusableView(View* view) {
view->previous_focusable_view_ = this;
next_focusable_view_ = view;
}
void View::InitFocusSiblings(View* v, int index) {
int child_count = static_cast<int>(child_views_.size());
if (child_count == 0) {
v->next_focusable_view_ = NULL;
v->previous_focusable_view_ = NULL;
} else {
if (index == child_count) {
// We are inserting at the end, but the end of the child list may not be
// the last focusable element. Let's try to find an element with no next
// focusable element to link to.
View* last_focusable_view = NULL;
for (std::vector<View*>::iterator iter = child_views_.begin();
iter != child_views_.end(); ++iter) {
if (!(*iter)->next_focusable_view_) {
last_focusable_view = *iter;
break;
}
}
if (last_focusable_view == NULL) {
// Hum... there is a cycle in the focus list. Let's just insert ourself
// after the last child.
View* prev = child_views_[index - 1];
v->previous_focusable_view_ = prev;
v->next_focusable_view_ = prev->next_focusable_view_;
prev->next_focusable_view_->previous_focusable_view_ = v;
prev->next_focusable_view_ = v;
} else {
last_focusable_view->next_focusable_view_ = v;
v->next_focusable_view_ = NULL;
v->previous_focusable_view_ = last_focusable_view;
}
} else {
View* prev = child_views_[index]->GetPreviousFocusableView();
v->previous_focusable_view_ = prev;
v->next_focusable_view_ = child_views_[index];
if (prev)
prev->next_focusable_view_ = v;
child_views_[index]->previous_focusable_view_ = v;
}
}
}
#ifndef NDEBUG
void View::PrintViewHierarchy() {
PrintViewHierarchyImp(0);
}
void View::PrintViewHierarchyImp(int indent) {
std::wostringstream buf;
int ind = indent;
while (ind-- > 0)
buf << L' ';
buf << UTF8ToWide(GetClassName());
buf << L' ';
buf << GetID();
buf << L' ';
buf << bounds_.x() << L"," << bounds_.y() << L",";
buf << bounds_.right() << L"," << bounds_.bottom();
buf << L' ';
buf << this;
LOG(INFO) << buf.str();
std::cout << buf.str() << std::endl;
for (int i = 0, count = GetChildViewCount(); i < count; ++i)
GetChildViewAt(i)->PrintViewHierarchyImp(indent + 2);
}
void View::PrintFocusHierarchy() {
PrintFocusHierarchyImp(0);
}
void View::PrintFocusHierarchyImp(int indent) {
std::wostringstream buf;
int ind = indent;
while (ind-- > 0)
buf << L' ';
buf << UTF8ToWide(GetClassName());
buf << L' ';
buf << GetID();
buf << L' ';
buf << GetClassName().c_str();
buf << L' ';
buf << this;
LOG(INFO) << buf.str();
std::cout << buf.str() << std::endl;
if (GetChildViewCount() > 0)
GetChildViewAt(0)->PrintFocusHierarchyImp(indent + 2);
View* v = GetNextFocusableView();
if (v)
v->PrintFocusHierarchyImp(indent);
}
#endif
////////////////////////////////////////////////////////////////////////////////
//
// View - accelerators
//
////////////////////////////////////////////////////////////////////////////////
void View::AddAccelerator(const Accelerator& accelerator) {
if (!accelerators_.get())
accelerators_.reset(new std::vector<Accelerator>());
std::vector<Accelerator>::iterator iter =
std::find(accelerators_->begin(), accelerators_->end(), accelerator);
DCHECK(iter == accelerators_->end())
<< "Registering the same accelerator multiple times";
accelerators_->push_back(accelerator);
RegisterPendingAccelerators();
}
void View::RemoveAccelerator(const Accelerator& accelerator) {
std::vector<Accelerator>::iterator iter;
if (!accelerators_.get() ||
((iter = std::find(accelerators_->begin(), accelerators_->end(),
accelerator)) == accelerators_->end())) {
NOTREACHED() << "Removing non-existing accelerator";
return;
}
size_t index = iter - accelerators_->begin();
accelerators_->erase(iter);
if (index >= registered_accelerator_count_) {
// The accelerator is not registered to FocusManager.
return;
}
--registered_accelerator_count_;
RootView* root_view = GetRootView();
if (!root_view) {
// We are not part of a view hierarchy, so there is nothing to do as we
// removed ourselves from accelerators_, we won't be registered when added
// to one.
return;
}
// If accelerator_focus_manager_ is NULL then we did not registered
// accelerators so there is nothing to unregister.
if (accelerator_focus_manager_) {
accelerator_focus_manager_->UnregisterAccelerator(accelerator, this);
}
}
void View::ResetAccelerators() {
if (accelerators_.get())
UnregisterAccelerators(false);
}
void View::RegisterPendingAccelerators() {
if (!accelerators_.get() ||
registered_accelerator_count_ == accelerators_->size()) {
// No accelerators are waiting for registration.
return;
}
RootView* root_view = GetRootView();
if (!root_view) {
// We are not yet part of a view hierarchy, we'll register ourselves once
// added to one.
return;
}
accelerator_focus_manager_ = GetFocusManager();
if (!accelerator_focus_manager_) {
// Some crash reports seem to show that we may get cases where we have no
// focus manager (see bug #1291225). This should never be the case, just
// making sure we don't crash.
// TODO(jcampan): This fails for a view under WidgetGtk with TYPE_CHILD.
// (see http://crbug.com/21335) reenable NOTREACHED assertion and
// verify accelerators works as expected.
#if defined(OS_WIN)
NOTREACHED();
#endif
return;
}
std::vector<Accelerator>::const_iterator iter;
for (iter = accelerators_->begin() + registered_accelerator_count_;
iter != accelerators_->end(); ++iter) {
accelerator_focus_manager_->RegisterAccelerator(*iter, this);
}
registered_accelerator_count_ = accelerators_->size();
}
void View::UnregisterAccelerators(bool leave_data_intact) {
if (!accelerators_.get())
return;
RootView* root_view = GetRootView();
if (root_view) {
if (accelerator_focus_manager_) {
// We may not have a FocusManager if the window containing us is being
// closed, in which case the FocusManager is being deleted so there is
// nothing to unregister.
accelerator_focus_manager_->UnregisterAccelerators(this);
accelerator_focus_manager_ = NULL;
}
if (!leave_data_intact) {
accelerators_->clear();
accelerators_.reset();
}
registered_accelerator_count_ = 0;
}
}
int View::GetDragOperations(int press_x, int press_y) {
if (!drag_controller_)
return DragDropTypes::DRAG_NONE;
return drag_controller_->GetDragOperations(this, press_x, press_y);
}
void View::WriteDragData(int press_x, int press_y, OSExchangeData* data) {
DCHECK(drag_controller_);
drag_controller_->WriteDragData(this, press_x, press_y, data);
}
void View::OnDragDone() {
}
bool View::InDrag() {
RootView* root_view = GetRootView();
return root_view ? (root_view->GetDragView() == this) : false;
}
// static
void View::ConvertPointToView(const View* src,
const View* dst,
gfx::Point* point) {
ConvertPointToView(src, dst, point, true);
}
// static
void View::ConvertPointToView(const View* src,
const View* dst,
gfx::Point* point,
bool try_other_direction) {
// src can be NULL
DCHECK(dst);
DCHECK(point);
const View* v;
gfx::Point offset;
for (v = dst; v && v != src; v = v->GetParent()) {
offset.SetPoint(offset.x() + v->GetX(APPLY_MIRRORING_TRANSFORMATION),
offset.y() + v->y());
}
// The source was not found. The caller wants a conversion
// from a view to a transitive parent.
if (src && v == NULL && try_other_direction) {
gfx::Point p;
// note: try_other_direction is force to FALSE so we don't
// end up in an infinite recursion should both src and dst
// are not parented.
ConvertPointToView(dst, src, &p, false);
// since the src and dst are inverted, p should also be negated
point->SetPoint(point->x() - p.x(), point->y() - p.y());
} else {
point->SetPoint(point->x() - offset.x(), point->y() - offset.y());
// If src is NULL, sp is in the screen coordinate system
if (src == NULL) {
Widget* widget = dst->GetWidget();
if (widget) {
gfx::Rect b;
widget->GetBounds(&b, false);
point->SetPoint(point->x() - b.x(), point->y() - b.y());
}
}
}
}
// static
void View::ConvertPointToWidget(const View* src, gfx::Point* p) {
DCHECK(src);
DCHECK(p);
gfx::Point offset;
for (const View* v = src; v; v = v->GetParent()) {
offset.set_x(offset.x() + v->GetX(APPLY_MIRRORING_TRANSFORMATION));
offset.set_y(offset.y() + v->y());
}
p->SetPoint(p->x() + offset.x(), p->y() + offset.y());
}
// static
void View::ConvertPointFromWidget(const View* dest, gfx::Point* p) {
gfx::Point t;
ConvertPointToWidget(dest, &t);
p->SetPoint(p->x() - t.x(), p->y() - t.y());
}
// static
void View::ConvertPointToScreen(const View* src, gfx::Point* p) {
DCHECK(src);
DCHECK(p);
// If the view is not connected to a tree, there's nothing we can do.
Widget* widget = src->GetWidget();
if (widget) {
ConvertPointToWidget(src, p);
gfx::Rect r;
widget->GetBounds(&r, false);
p->SetPoint(p->x() + r.x(), p->y() + r.y());
}
}
/////////////////////////////////////////////////////////////////////////////
//
// View - event handlers
//
/////////////////////////////////////////////////////////////////////////////
bool View::OnMousePressed(const MouseEvent& e) {
return false;
}
bool View::OnMouseDragged(const MouseEvent& e) {
return false;
}
void View::OnMouseReleased(const MouseEvent& e, bool canceled) {
}
void View::OnMouseMoved(const MouseEvent& e) {
}
void View::OnMouseEntered(const MouseEvent& e) {
}
void View::OnMouseExited(const MouseEvent& e) {
}
void View::SetMouseHandler(View *new_mouse_handler) {
// It is valid for new_mouse_handler to be NULL
if (parent_)
parent_->SetMouseHandler(new_mouse_handler);
}
void View::SetVisible(bool flag) {
if (flag != is_visible_) {
// If the tab is currently visible, schedule paint to
// refresh parent
if (IsVisible())
SchedulePaint();
is_visible_ = flag;
// This notifies all subviews recursively.
PropagateVisibilityNotifications(this, flag);
// If we are newly visible, schedule paint.
if (IsVisible())
SchedulePaint();
}
}
bool View::IsVisibleInRootView() const {
View* parent = GetParent();
if (IsVisible() && parent)
return parent->IsVisibleInRootView();
else
return false;
}
/////////////////////////////////////////////////////////////////////////////
//
// View - keyboard and focus
//
/////////////////////////////////////////////////////////////////////////////
void View::RequestFocus() {
RootView* rv = GetRootView();
if (rv && IsFocusable())
rv->FocusView(this);
}
void View::WillGainFocus() {
}
void View::DidGainFocus() {
}
void View::WillLoseFocus() {
}
bool View::OnKeyPressed(const KeyEvent& e) {
return false;
}
bool View::OnKeyReleased(const KeyEvent& e) {
return false;
}
bool View::OnMouseWheel(const MouseWheelEvent& e) {
return false;
}
void View::SetDragController(DragController* drag_controller) {
drag_controller_ = drag_controller;
}
DragController* View::GetDragController() {
return drag_controller_;
}
bool View::GetDropFormats(
int* formats,
std::set<OSExchangeData::CustomFormat>* custom_formats) {
return false;
}
bool View::AreDropTypesRequired() {
return false;
}
bool View::CanDrop(const OSExchangeData& data) {
// TODO(sky): when I finish up migration, this should default to true.
return false;
}
void View::OnDragEntered(const DropTargetEvent& event) {
}
int View::OnDragUpdated(const DropTargetEvent& event) {
return DragDropTypes::DRAG_NONE;
}
void View::OnDragExited() {
}
int View::OnPerformDrop(const DropTargetEvent& event) {
return DragDropTypes::DRAG_NONE;
}
// static
bool View::ExceededDragThreshold(int delta_x, int delta_y) {
return (abs(delta_x) > GetHorizontalDragThreshold() ||
abs(delta_y) > GetVerticalDragThreshold());
}
// Tooltips -----------------------------------------------------------------
bool View::GetTooltipText(int x, int y, std::wstring* tooltip) {
return false;
}
bool View::GetTooltipTextOrigin(int x, int y, gfx::Point* loc) {
return false;
}
void View::TooltipTextChanged() {
Widget* widget = GetWidget();
if (widget && widget->GetTooltipManager())
widget->GetTooltipManager()->TooltipTextChanged(this);
}
void View::UpdateTooltip() {
Widget* widget = GetWidget();
if (widget && widget->GetTooltipManager())
widget->GetTooltipManager()->UpdateTooltip();
}
std::string View::GetClassName() const {
return kViewClassName;
}
View* View::GetAncestorWithClassName(const std::string& name) {
for (View* view = this; view; view = view->GetParent()) {
if (view->GetClassName() == name)
return view;
}
return NULL;
}
gfx::Rect View::GetVisibleBounds() {
if (!IsVisibleInRootView())
return gfx::Rect();
gfx::Rect vis_bounds(0, 0, width(), height());
gfx::Rect ancestor_bounds;
View* view = this;
int root_x = 0;
int root_y = 0;
while (view != NULL && !vis_bounds.IsEmpty()) {
root_x += view->GetX(APPLY_MIRRORING_TRANSFORMATION);
root_y += view->y();
vis_bounds.Offset(view->GetX(APPLY_MIRRORING_TRANSFORMATION), view->y());
View* ancestor = view->GetParent();
if (ancestor != NULL) {
ancestor_bounds.SetRect(0, 0, ancestor->width(),
ancestor->height());
vis_bounds = vis_bounds.Intersect(ancestor_bounds);
} else if (!view->GetWidget()) {
// If the view has no Widget, we're not visible. Return an empty rect.
return gfx::Rect();
}
view = ancestor;
}
if (vis_bounds.IsEmpty())
return vis_bounds;
// Convert back to this views coordinate system.
vis_bounds.Offset(-root_x, -root_y);
return vis_bounds;
}
int View::GetPageScrollIncrement(ScrollView* scroll_view,
bool is_horizontal, bool is_positive) {
return 0;
}
int View::GetLineScrollIncrement(ScrollView* scroll_view,
bool is_horizontal, bool is_positive) {
return 0;
}
ThemeProvider* View::GetThemeProvider() const {
Widget* widget = GetWidget();
return widget ? widget->GetThemeProvider() : NULL;
}
// static
void View::RegisterChildrenForVisibleBoundsNotification(
RootView* root, View* view) {
DCHECK(root && view);
if (view->GetNotifyWhenVisibleBoundsInRootChanges())
root->RegisterViewForVisibleBoundsNotification(view);
for (int i = 0; i < view->GetChildViewCount(); ++i)
RegisterChildrenForVisibleBoundsNotification(root, view->GetChildViewAt(i));
}
// static
void View::UnregisterChildrenForVisibleBoundsNotification(
RootView* root, View* view) {
DCHECK(root && view);
if (view->GetNotifyWhenVisibleBoundsInRootChanges())
root->UnregisterViewForVisibleBoundsNotification(view);
for (int i = 0; i < view->GetChildViewCount(); ++i)
UnregisterChildrenForVisibleBoundsNotification(root,
view->GetChildViewAt(i));
}
void View::AddDescendantToNotify(View* view) {
DCHECK(view);
if (!descendants_to_notify_.get())
descendants_to_notify_.reset(new ViewList());
descendants_to_notify_->push_back(view);
}
void View::RemoveDescendantToNotify(View* view) {
DCHECK(view && descendants_to_notify_.get());
ViewList::iterator i = find(descendants_to_notify_->begin(),
descendants_to_notify_->end(),
view);
DCHECK(i != descendants_to_notify_->end());
descendants_to_notify_->erase(i);
if (descendants_to_notify_->empty())
descendants_to_notify_.reset();
}
// DropInfo --------------------------------------------------------------------
void View::DragInfo::Reset() {
possible_drag = false;
start_x = start_y = 0;
}
void View::DragInfo::PossibleDrag(int x, int y) {
possible_drag = true;
start_x = x;
start_y = y;
}
} // namespace
|