Add ICU message format support
[chromium-blink-merge.git] / ui / views / controls / tree / tree_view.h
blobfdcff6727359982b5592b83558506b9f8c80fd42
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 #ifndef UI_VIEWS_CONTROLS_TREE_TREE_VIEW_VIEWS_H_
6 #define UI_VIEWS_CONTROLS_TREE_TREE_VIEW_VIEWS_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "ui/base/models/tree_node_model.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/image/image_skia.h"
16 #include "ui/views/controls/prefix_delegate.h"
17 #include "ui/views/controls/textfield/textfield_controller.h"
18 #include "ui/views/focus/focus_manager.h"
19 #include "ui/views/view.h"
21 namespace gfx {
22 class Rect;
23 } // namespace gfx
25 namespace views {
27 class Textfield;
28 class TreeViewController;
29 class PrefixSelector;
31 // TreeView displays hierarchical data as returned from a TreeModel. The user
32 // can expand, collapse and edit the items. A Controller may be attached to
33 // receive notification of selection changes and restrict editing.
35 // Note on implementation. This implementation doesn't scale well. In particular
36 // it does not store any row information, but instead calculates it as
37 // necessary. But it's more than adequate for current uses.
38 class VIEWS_EXPORT TreeView : public ui::TreeModelObserver,
39 public TextfieldController,
40 public FocusChangeListener,
41 public PrefixDelegate {
42 public:
43 // The tree view's class name.
44 static const char kViewClassName[];
46 TreeView();
47 ~TreeView() override;
49 // Returns new ScrollPane that contains the receiver.
50 View* CreateParentIfNecessary();
52 // Sets the model. TreeView does not take ownership of the model.
53 void SetModel(ui::TreeModel* model);
54 ui::TreeModel* model() const { return model_; }
56 // Sets whether to automatically expand children when a parent node is
57 // expanded. The default is false. If true, when a node in the tree is
58 // expanded for the first time, its children are also automatically expanded.
59 // If a node is subsequently collapsed and expanded again, the children
60 // will not be automatically expanded.
61 void set_auto_expand_children(bool auto_expand_children) {
62 auto_expand_children_ = auto_expand_children;
65 // Sets whether the user can edit the nodes. The default is true. If true,
66 // the Controller is queried to determine if a particular node can be edited.
67 void SetEditable(bool editable);
69 // Edits the specified node. This cancels the current edit and expands all
70 // parents of node.
71 void StartEditing(ui::TreeModelNode* node);
73 // Cancels the current edit. Does nothing if not editing.
74 void CancelEdit();
76 // Commits the current edit. Does nothing if not editing.
77 void CommitEdit();
79 // If the user is editing a node, it is returned. If the user is not
80 // editing a node, NULL is returned.
81 ui::TreeModelNode* GetEditingNode();
83 // Selects the specified node. This expands all the parents of node.
84 void SetSelectedNode(ui::TreeModelNode* model_node);
86 // Returns the selected node, or NULL if nothing is selected.
87 ui::TreeModelNode* GetSelectedNode();
89 // Marks |model_node| as collapsed. This only effects the UI if node and all
90 // its parents are expanded (IsExpanded(model_node) returns true).
91 void Collapse(ui::TreeModelNode* model_node);
93 // Make sure node and all its parents are expanded.
94 void Expand(ui::TreeModelNode* node);
96 // Invoked from ExpandAll(). Expands the supplied node and recursively
97 // invokes itself with all children.
98 void ExpandAll(ui::TreeModelNode* node);
100 // Returns true if the specified node is expanded.
101 bool IsExpanded(ui::TreeModelNode* model_node);
103 // Sets whether the root is shown. If true, the root node of the tree is
104 // shown, if false only the children of the root are shown. The default is
105 // true.
106 void SetRootShown(bool root_visible);
108 // Sets the controller, which may be null. TreeView does not take ownership
109 // of the controller.
110 void SetController(TreeViewController* controller) {
111 controller_ = controller;
114 // Returns the node for the specified row, or NULL for an invalid row index.
115 ui::TreeModelNode* GetNodeForRow(int row);
117 // Maps a node to a row, returns -1 if node is not valid.
118 int GetRowForNode(ui::TreeModelNode* node);
120 views::Textfield* editor() { return editor_; }
122 // View overrides:
123 void Layout() override;
124 gfx::Size GetPreferredSize() const override;
125 bool AcceleratorPressed(const ui::Accelerator& accelerator) override;
126 bool OnMousePressed(const ui::MouseEvent& event) override;
127 void OnGestureEvent(ui::GestureEvent* event) override;
128 void ShowContextMenu(const gfx::Point& p,
129 ui::MenuSourceType source_type) override;
130 void GetAccessibleState(ui::AXViewState* state) override;
131 const char* GetClassName() const override;
133 // TreeModelObserver overrides:
134 void TreeNodesAdded(ui::TreeModel* model,
135 ui::TreeModelNode* parent,
136 int start,
137 int count) override;
138 void TreeNodesRemoved(ui::TreeModel* model,
139 ui::TreeModelNode* parent,
140 int start,
141 int count) override;
142 void TreeNodeChanged(ui::TreeModel* model,
143 ui::TreeModelNode* model_node) override;
145 // TextfieldController overrides:
146 void ContentsChanged(Textfield* sender,
147 const base::string16& new_contents) override;
148 bool HandleKeyEvent(Textfield* sender,
149 const ui::KeyEvent& key_event) override;
151 // FocusChangeListener overrides:
152 void OnWillChangeFocus(View* focused_before, View* focused_now) override;
153 void OnDidChangeFocus(View* focused_before, View* focused_now) override;
155 // PrefixDelegate overrides:
156 int GetRowCount() override;
157 int GetSelectedRow() override;
158 void SetSelectedRow(int row) override;
159 base::string16 GetTextForRow(int row) override;
161 protected:
162 // View overrides:
163 gfx::Point GetKeyboardContextMenuLocation() override;
164 bool OnKeyPressed(const ui::KeyEvent& event) override;
165 void OnPaint(gfx::Canvas* canvas) override;
166 void OnFocus() override;
167 void OnBlur() override;
169 private:
170 friend class TreeViewTest;
172 // Selects, expands or collapses nodes in the tree. Consistent behavior for
173 // tap gesture and click events.
174 bool OnClickOrTap(const ui::LocatedEvent& event);
176 // InternalNode is used to track information about the set of nodes displayed
177 // by TreeViewViews.
178 class InternalNode : public ui::TreeNode<InternalNode> {
179 public:
180 InternalNode();
181 ~InternalNode() override;
183 // Resets the state from |node|.
184 void Reset(ui::TreeModelNode* node);
186 // The model node this InternalNode represents.
187 ui::TreeModelNode* model_node() { return model_node_; }
189 // Whether the node is expanded.
190 void set_is_expanded(bool expanded) { is_expanded_ = expanded; }
191 bool is_expanded() const { return is_expanded_; }
193 // Whether children have been loaded.
194 void set_loaded_children(bool value) { loaded_children_ = value; }
195 bool loaded_children() const { return loaded_children_; }
197 // Width needed to display the string.
198 void set_text_width(int width) { text_width_ = width; }
199 int text_width() const { return text_width_; }
201 // Returns the total number of descendants (including this node).
202 int NumExpandedNodes() const;
204 // Returns the max width of all descendants (including this node). |indent|
205 // is how many pixels each child is indented and |depth| is the depth of
206 // this node from its parent.
207 int GetMaxWidth(int indent, int depth);
209 private:
210 // The node from the model.
211 ui::TreeModelNode* model_node_;
213 // Whether the children have been loaded.
214 bool loaded_children_;
216 bool is_expanded_;
218 int text_width_;
220 DISALLOW_COPY_AND_ASSIGN(InternalNode);
223 // Used by GetInternalNodeForModelNode.
224 enum GetInternalNodeCreateType {
225 // If an InternalNode hasn't been created yet, create it.
226 CREATE_IF_NOT_LOADED,
228 // Don't create an InternalNode if one hasn't been created yet.
229 DONT_CREATE_IF_NOT_LOADED,
232 // Used by IncrementSelection.
233 enum IncrementType {
234 // Selects the next node.
235 INCREMENT_NEXT,
237 // Selects the previous node.
238 INCREMENT_PREVIOUS
241 // Row of the root node. This varies depending upon whether the root is
242 // visible.
243 int root_row() const { return root_shown_ ? 0 : -1; }
245 // Depth of the root node.
246 int root_depth() const { return root_shown_ ? 0 : -1; }
248 // Loads the children of the specified node.
249 void LoadChildren(InternalNode* node);
251 // Configures an InternalNode from a node from the model. This is used
252 // when a node changes as well as when loading.
253 void ConfigureInternalNode(ui::TreeModelNode* model_node, InternalNode* node);
255 // Sets |node|s text_width.
256 void UpdateNodeTextWidth(InternalNode* node);
258 // Invoked when the set of drawn nodes changes.
259 void DrawnNodesChanged();
261 // Updates |preferred_size_| from the state of the UI.
262 void UpdatePreferredSize();
264 // Positions |editor_|.
265 void LayoutEditor();
267 // Schedules a paint for |node|.
268 void SchedulePaintForNode(InternalNode* node);
270 // Recursively paints rows from |min_row| to |max_row|. |node| is the node for
271 // the row |*row|. |row| is updated as this walks the tree. Depth is the depth
272 // of |*row|.
273 void PaintRows(gfx::Canvas* canvas,
274 int min_row,
275 int max_row,
276 InternalNode* node,
277 int depth,
278 int* row);
280 // Invoked to paint a single node.
281 void PaintRow(gfx::Canvas* canvas,
282 InternalNode* node,
283 int row,
284 int depth);
286 // Paints the expand control given the specified nodes bounds.
287 void PaintExpandControl(gfx::Canvas* canvas,
288 const gfx::Rect& node_bounds,
289 bool expanded);
291 // Returns the InternalNode for a model node. |create_type| indicates wheter
292 // this should load InternalNode or not.
293 InternalNode* GetInternalNodeForModelNode(
294 ui::TreeModelNode* model_node,
295 GetInternalNodeCreateType create_type);
297 // Returns the bounds for a node.
298 gfx::Rect GetBoundsForNode(InternalNode* node);
300 // Implementation of GetBoundsForNode. Separated out as some callers already
301 // know the row/depth.
302 gfx::Rect GetBoundsForNodeImpl(InternalNode* node, int row, int depth);
304 // Returns the row and depth of a node.
305 int GetRowForInternalNode(InternalNode* node, int* depth);
307 // Returns the row and depth of the specified node.
308 InternalNode* GetNodeByRow(int row, int* depth);
310 // Implementation of GetNodeByRow. |curent_row| is updated as we iterate.
311 InternalNode* GetNodeByRowImpl(InternalNode* node,
312 int target_row,
313 int current_depth,
314 int* current_row,
315 int* node_depth);
317 // Increments the selection. Invoked in response to up/down arrow.
318 void IncrementSelection(IncrementType type);
320 // If the current node is expanded, it's collapsed, otherwise selection is
321 // moved to the parent.
322 void CollapseOrSelectParent();
324 // If the selected node is collapsed, it's expanded. Otherwise the first child
325 // is seleected.
326 void ExpandOrSelectChild();
328 // Implementation of Expand(). Returns true if at least one node was expanded
329 // that previously wasn't.
330 bool ExpandImpl(ui::TreeModelNode* model_node);
332 PrefixSelector* GetPrefixSelector();
334 // The model, may be null.
335 ui::TreeModel* model_;
337 // Default icons for closed/open.
338 gfx::ImageSkia closed_icon_;
339 gfx::ImageSkia open_icon_;
341 // Icons from the model.
342 std::vector<gfx::ImageSkia> icons_;
344 // The root node.
345 InternalNode root_;
347 // The selected node, may be NULL.
348 InternalNode* selected_node_;
350 bool editing_;
352 // The editor; lazily created and never destroyed (well, until TreeView is
353 // destroyed). Hidden when no longer editing. We do this avoid destruction
354 // problems.
355 Textfield* editor_;
357 // Preferred size of |editor_| with no content.
358 gfx::Size empty_editor_size_;
360 // If non-NULL we've attached a listener to this focus manager. Used to know
361 // when focus is changing to another view so that we can cancel the edit.
362 FocusManager* focus_manager_;
364 // Whether to automatically expand children when a parent node is expanded.
365 bool auto_expand_children_;
367 // Whether the user can edit the items.
368 bool editable_;
370 // The controller.
371 TreeViewController* controller_;
373 // Whether or not the root is shown in the tree.
374 bool root_shown_;
376 // Cached preferred size.
377 gfx::Size preferred_size_;
379 // Font list used to display text.
380 gfx::FontList font_list_;
382 // Height of each row. Based on font and some padding.
383 int row_height_;
385 // Offset the text is drawn at. This accounts for the size of the expand
386 // control, icon and offsets.
387 int text_offset_;
389 scoped_ptr<PrefixSelector> selector_;
391 DISALLOW_COPY_AND_ASSIGN(TreeView);
394 } // namespace views
396 #endif // UI_VIEWS_CONTROLS_TREE_TREE_VIEW_VIEWS_H_