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 ViewModel::ViewModel() {
15 ViewModel::~ViewModel() {
16 // view are owned by their parent, no need to delete them.
19 void ViewModel::Add(View
* view
, int index
) {
20 DCHECK_LE(index
, static_cast<int>(entries_
.size()));
24 entries_
.insert(entries_
.begin() + index
, entry
);
27 void ViewModel::Remove(int index
) {
32 entries_
.erase(entries_
.begin() + index
);
35 void ViewModel::Move(int index
, int target_index
) {
36 if (index
== target_index
)
38 Entry
entry(entries_
[index
]);
39 entries_
.erase(entries_
.begin() + index
);
40 entries_
.insert(entries_
.begin() + target_index
, entry
);
43 void ViewModel::MoveViewOnly(int index
, int target_index
) {
44 if (index
== target_index
)
46 if (target_index
< index
) {
47 View
* view
= entries_
[index
].view
;
48 for (int i
= index
; i
> target_index
; --i
)
49 entries_
[i
].view
= entries_
[i
- 1].view
;
50 entries_
[target_index
].view
= view
;
52 View
* view
= entries_
[index
].view
;
53 for (int i
= index
; i
< target_index
; ++i
)
54 entries_
[i
].view
= entries_
[i
+ 1].view
;
55 entries_
[target_index
].view
= view
;
59 void ViewModel::Clear() {
61 entries
.swap(entries_
);
62 for (size_t i
= 0; i
< entries
.size(); ++i
)
63 delete entries
[i
].view
;
66 int ViewModel::GetIndexOfView(const View
* view
) const {
67 for (size_t i
= 0; i
< entries_
.size(); ++i
) {
68 if (entries_
[i
].view
== view
)
69 return static_cast<int>(i
);