- add sources.
[platform/framework/web/crosswalk.git] / src / ui / base / models / list_selection_model.h
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.
4
5 #ifndef UI_BASE_MODELS_LIST_SELECTION_MODEL_H_
6 #define UI_BASE_MODELS_LIST_SELECTION_MODEL_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "ui/base/ui_export.h"
12
13 namespace ui {
14
15 // Selection model represented as a list of ints. Used by the TabStrip. In
16 // addition to the set of selected indices ListSelectionModel maintains the
17 // following:
18 // active: the index of the currently visible tab in the tab strip.
19 // anchor: the index of the last tab the user clicked on. Extending the
20 // selection extends it from this index.
21 //
22 // Typically there is only one selected item, in which case the anchor and
23 // active index correspond to the same thing.
24 class UI_EXPORT ListSelectionModel {
25  public:
26   typedef std::vector<int> SelectedIndices;
27
28   // Used to identify no selection.
29   static const int kUnselectedIndex;
30
31   ListSelectionModel();
32   ~ListSelectionModel();
33
34   // See class description for details of the anchor.
35   void set_anchor(int anchor) { anchor_ = anchor; }
36   int anchor() const { return anchor_; }
37
38   // See class description for details of active.
39   void set_active(int active) { active_ = active; }
40   int active() const { return active_; }
41
42   // True if nothing is selected.
43   bool empty() const { return selected_indices_.empty(); }
44
45   // Number of selected indices.
46   size_t size() const { return selected_indices_.size(); }
47
48   // Increments all indices >= |index|. For example, if the selection consists
49   // of [0, 1, 5] and this is invoked with 1, it results in [0, 2, 6]. This also
50   // updates the anchor and active indices.
51   // This is used when a new tab is inserted into the tabstrip.
52   void IncrementFrom(int index);
53
54   // Shifts all indices > |index| down by 1. If |index| is selected, it is
55   // removed. For example, if the selection consists of [0, 1, 5] and this is
56   // invoked with 1, it results in [0, 4]. This is used when a tab is removed
57   // from the tabstrip.
58   void DecrementFrom(int index);
59
60   // Sets the anchor, active and selection to |index|.
61   void SetSelectedIndex(int index);
62
63   // Returns true if |index| is selected.
64   bool IsSelected(int index) const;
65
66   // Adds |index| to the selection. This does not change the active or anchor
67   // indices.
68   void AddIndexToSelection(int index);
69
70   // Removes |index| from the selection. This does not change the active or
71   // anchor indices.
72   void RemoveIndexFromSelection(int index);
73
74   // Extends the selection from the anchor to |index|. If the anchor is empty,
75   // this sets the anchor, selection and active indices to |index|.
76   void SetSelectionFromAnchorTo(int index);
77
78   // Makes sure the indices from the anchor to |index| are selected. This only
79   // adds to the selection.
80   void AddSelectionFromAnchorTo(int index);
81
82   // Invoked when an item moves. |from| is the original index, and |to| the
83   // target index.
84   // NOTE: this matches the TabStripModel API. If moving to a greater index,
85   // |to| should be the index *after* removing |from|. For example, consider
86   // three tabs 'A B C', to move A to the end of the list, this should be
87   // invoked with '0, 2'.
88   void Move(int from, int to);
89
90   // Sets the anchor and active to kUnselectedIndex, and removes all the
91   // selected indices.
92   void Clear();
93
94   // Returns the selected indices. The selection is always ordered in acending
95   // order.
96   const SelectedIndices& selected_indices() const { return selected_indices_; }
97
98   // Copies the selection from |source| to this.
99   void Copy(const ListSelectionModel& source);
100
101   // Compares this selection with |rhs|.
102   bool Equals(const ListSelectionModel& rhs) const;
103
104  private:
105   SelectedIndices selected_indices_;
106
107   int active_;
108
109   int anchor_;
110
111   DISALLOW_COPY_AND_ASSIGN(ListSelectionModel);
112 };
113
114 }  // namespace ui
115
116 #endif  // UI_BASE_MODELS_LIST_SELECTION_MODEL_H_