1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/view_model.h"
7 #include "base/logging.h"
8 #include "ui/views/view.h"
12 ViewModelBase::~ViewModelBase() {
13 // view are owned by their parent, no need to delete them.
16 void ViewModelBase::Remove(int index
) {
21 entries_
.erase(entries_
.begin() + index
);
24 void ViewModelBase::Move(int index
, int target_index
) {
25 DCHECK_LT(index
, static_cast<int>(entries_
.size()));
27 DCHECK_LT(target_index
, static_cast<int>(entries_
.size()));
28 DCHECK_GE(target_index
, 0);
30 if (index
== target_index
)
32 Entry
entry(entries_
[index
]);
33 entries_
.erase(entries_
.begin() + index
);
34 entries_
.insert(entries_
.begin() + target_index
, entry
);
37 void ViewModelBase::MoveViewOnly(int index
, int target_index
) {
38 if (index
== target_index
)
40 if (target_index
< index
) {
41 View
* view
= entries_
[index
].view
;
42 for (int i
= index
; i
> target_index
; --i
)
43 entries_
[i
].view
= entries_
[i
- 1].view
;
44 entries_
[target_index
].view
= view
;
46 View
* view
= entries_
[index
].view
;
47 for (int i
= index
; i
< target_index
; ++i
)
48 entries_
[i
].view
= entries_
[i
+ 1].view
;
49 entries_
[target_index
].view
= view
;
53 void ViewModelBase::Clear() {
55 entries
.swap(entries_
);
56 for (size_t i
= 0; i
< entries
.size(); ++i
)
57 delete entries
[i
].view
;
60 int ViewModelBase::GetIndexOfView(const View
* view
) const {
61 for (size_t i
= 0; i
< entries_
.size(); ++i
) {
62 if (entries_
[i
].view
== view
)
63 return static_cast<int>(i
);
68 ViewModelBase::ViewModelBase() {
71 void ViewModelBase::AddUnsafe(View
* view
, int index
) {
72 DCHECK_LE(index
, static_cast<int>(entries_
.size()));
76 entries_
.insert(entries_
.begin() + index
, entry
);