15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/base/models/list_selection_model.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <valarray>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace ui {
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)const int ListSelectionModel::kUnselectedIndex = -1;
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void IncrementFromImpl(int index, int* value) {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (*value >= index)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    (*value)++;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static bool DecrementFromImpl(int index, int* value) {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (*value == index) {
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    *value = ListSelectionModel::kUnselectedIndex;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return true;
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (*value > index)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    (*value)--;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ListSelectionModel::ListSelectionModel()
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : active_(kUnselectedIndex),
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      anchor_(kUnselectedIndex) {
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ListSelectionModel::~ListSelectionModel() {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::IncrementFrom(int index) {
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Shift the selection to account for the newly inserted tab.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (SelectedIndices::iterator i = selected_indices_.begin();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       i != selected_indices_.end(); ++i) {
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IncrementFromImpl(index, &(*i));
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IncrementFromImpl(index, &anchor_);
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  IncrementFromImpl(index, &active_);
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::DecrementFrom(int index) {
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (SelectedIndices::iterator i = selected_indices_.begin();
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       i != selected_indices_.end(); ) {
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (DecrementFromImpl(index, &(*i)))
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      i = selected_indices_.erase(i);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ++i;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DecrementFromImpl(index, &anchor_);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DecrementFromImpl(index, &active_);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::SetSelectedIndex(int index) {
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  anchor_ = active_ = index;
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  selected_indices_.clear();
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (index != kUnselectedIndex)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    selected_indices_.push_back(index);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool ListSelectionModel::IsSelected(int index) const {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return std::find(selected_indices_.begin(), selected_indices_.end(), index) !=
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      selected_indices_.end();
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::AddIndexToSelection(int index) {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!IsSelected(index)) {
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    selected_indices_.push_back(index);
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::sort(selected_indices_.begin(), selected_indices_.end());
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::RemoveIndexFromSelection(int index) {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SelectedIndices::iterator i = std::find(selected_indices_.begin(),
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                          selected_indices_.end(), index);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (i != selected_indices_.end())
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    selected_indices_.erase(i);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::SetSelectionFromAnchorTo(int index) {
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (anchor_ == kUnselectedIndex) {
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetSelectedIndex(index);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int delta = std::abs(index - anchor_);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SelectedIndices new_selection(delta + 1, 0);
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (int i = 0, min = std::min(index, anchor_); i <= delta; ++i)
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      new_selection[i] = i + min;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    selected_indices_.swap(new_selection);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    active_ = index;
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::AddSelectionFromAnchorTo(int index) {
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (anchor_ == kUnselectedIndex) {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SetSelectedIndex(index);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    for (int i = std::min(index, anchor_), end = std::max(index, anchor_);
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         i <= end; ++i) {
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!IsSelected(i))
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        selected_indices_.push_back(i);
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::sort(selected_indices_.begin(), selected_indices_.end());
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    active_ = index;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::Move(int from, int to) {
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_NE(to, from);
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool was_anchor = from == anchor_;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool was_active = from == active_;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool was_selected = IsSelected(from);
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (to < from) {
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IncrementFrom(to);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DecrementFrom(from + 1);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DecrementFrom(from);
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    IncrementFrom(to);
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (was_active)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    active_ = to;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (was_anchor)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    anchor_ = to;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (was_selected)
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    AddIndexToSelection(to);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::Clear() {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  anchor_ = active_ = kUnselectedIndex;
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SelectedIndices empty_selection;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  selected_indices_.swap(empty_selection);
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void ListSelectionModel::Copy(const ListSelectionModel& source) {
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  selected_indices_ = source.selected_indices_;
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  active_ = source.active_;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  anchor_ = source.anchor_;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool ListSelectionModel::Equals(const ListSelectionModel& rhs) const {
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return active_ == rhs.active() &&
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      anchor_ == rhs.anchor() &&
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      selected_indices() == rhs.selected_indices();
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace ui
154