| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UNDO_UNDO_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_UNDO_UNDO_MANAGER_H_ |
| 6 #define CHROME_BROWSER_UNDO_UNDO_MANAGER_H_ | 6 #define CHROME_BROWSER_UNDO_UNDO_MANAGER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| 11 | 11 |
| 12 class UndoOperation; | 12 class UndoOperation; |
| 13 | 13 |
| 14 // UndoGroup ------------------------------------------------------------------ | 14 // UndoGroup ------------------------------------------------------------------ |
| 15 | 15 |
| 16 // UndoGroup represents a user action and stores all the operations that | 16 // UndoGroup represents a user action and stores all the operations that |
| 17 // make that action. Typically there is only one operation per UndoGroup. | 17 // make that action. Typically there is only one operation per UndoGroup. |
| 18 class UndoGroup { | 18 class UndoGroup { |
| 19 public: | 19 public: |
| 20 UndoGroup(); | 20 UndoGroup(); |
| 21 ~UndoGroup(); | 21 ~UndoGroup(); |
| 22 | 22 |
| 23 void AddOperation(scoped_ptr<UndoOperation> operation); | 23 void AddOperation(scoped_ptr<UndoOperation> operation); |
| 24 bool has_operations() const { | 24 bool has_operations() const { |
| 25 return !operations_.empty(); | 25 return !operations_.empty(); |
| 26 } | 26 } |
| 27 void Undo(); |
| 27 | 28 |
| 28 void Undo(); | 29 template <typename F> |
| 30 void ForEachUndoOperation(F fn) { |
| 31 std::for_each(operations_.begin(), operations_.end(), fn); |
| 32 } |
| 29 | 33 |
| 30 private: | 34 private: |
| 31 ScopedVector<UndoOperation> operations_; | 35 ScopedVector<UndoOperation> operations_; |
| 32 | 36 |
| 33 DISALLOW_COPY_AND_ASSIGN(UndoGroup); | 37 DISALLOW_COPY_AND_ASSIGN(UndoGroup); |
| 34 }; | 38 }; |
| 35 | 39 |
| 36 // UndoManager ---------------------------------------------------------------- | 40 // UndoManager ---------------------------------------------------------------- |
| 37 | 41 |
| 38 // Maintains user actions as a group of operations that store enough info to | 42 // Maintains user actions as a group of operations that store enough info to |
| (...skipping 15 matching lines...) Expand all Loading... |
| 54 // Group multiple operations into one undoable action. | 58 // Group multiple operations into one undoable action. |
| 55 void StartGroupingActions(); | 59 void StartGroupingActions(); |
| 56 void EndGroupingActions(); | 60 void EndGroupingActions(); |
| 57 | 61 |
| 58 // Suspend undo tracking while processing non-user initiated changes such as | 62 // Suspend undo tracking while processing non-user initiated changes such as |
| 59 // profile synchonization. | 63 // profile synchonization. |
| 60 void SuspendUndoTracking(); | 64 void SuspendUndoTracking(); |
| 61 void ResumeUndoTracking(); | 65 void ResumeUndoTracking(); |
| 62 bool IsUndoTrakingSuspended() const; | 66 bool IsUndoTrakingSuspended() const; |
| 63 | 67 |
| 68 // Remove all undo and redo operations. |
| 69 void Reset(); |
| 70 |
| 71 // Applies |fn| to UndoOperations in all UndoGroups. |
| 72 template <typename F> |
| 73 void ForEachUndoOperation(F &fn) { |
| 74 for (ScopedVector<UndoGroup>::iterator it = undo_actions_.begin(); |
| 75 it != undo_actions_.end(); ++it) { |
| 76 (*it)->ForEachUndoOperation(fn); |
| 77 } |
| 78 for (ScopedVector<UndoGroup>::iterator it = redo_actions_.begin(); |
| 79 it != redo_actions_.end(); ++it) { |
| 80 (*it)->ForEachUndoOperation(fn); |
| 81 } |
| 82 } |
| 83 |
| 64 private: | 84 private: |
| 65 void Undo(bool* performing_indicator, | 85 void Undo(bool* performing_indicator, |
| 66 ScopedVector<UndoGroup>* active_undo_group); | 86 ScopedVector<UndoGroup>* active_undo_group); |
| 87 bool is_user_action() const { return !performing_undo_ && !performing_redo_; } |
| 67 | 88 |
| 68 void RemoveAllActions(); | |
| 69 void RemoveAllRedoActions(); | 89 void RemoveAllRedoActions(); |
| 70 | 90 |
| 71 ScopedVector<UndoGroup>* GetActiveUndoGroup(); | 91 ScopedVector<UndoGroup>* GetActiveUndoGroup(); |
| 72 | 92 |
| 73 // Containers of user actions ready for an undo or redo treated as a stack. | 93 // Containers of user actions ready for an undo or redo treated as a stack. |
| 74 ScopedVector<UndoGroup> undo_actions_; | 94 ScopedVector<UndoGroup> undo_actions_; |
| 75 ScopedVector<UndoGroup> redo_actions_; | 95 ScopedVector<UndoGroup> redo_actions_; |
| 76 | 96 |
| 77 // Supports grouping operations into a single undo action. | 97 // Supports grouping operations into a single undo action. |
| 78 int group_actions_count_; | 98 int group_actions_count_; |
| 79 | 99 |
| 80 // The container that is used when actions are grouped. | 100 // The container that is used when actions are grouped. |
| 81 scoped_ptr<UndoGroup> pending_grouped_action_; | 101 scoped_ptr<UndoGroup> pending_grouped_action_; |
| 82 | 102 |
| 83 // Supports the suspension of undo tracking. | 103 // Supports the suspension of undo tracking. |
| 84 int undo_suspended_count_; | 104 int undo_suspended_count_; |
| 85 | 105 |
| 86 // Set when executing Undo or Redo so that incoming changes are correctly | 106 // Set when executing Undo or Redo so that incoming changes are correctly |
| 87 // processed. | 107 // processed. |
| 88 bool performing_undo_; | 108 bool performing_undo_; |
| 89 bool performing_redo_; | 109 bool performing_redo_; |
| 90 | 110 |
| 91 DISALLOW_COPY_AND_ASSIGN(UndoManager); | 111 DISALLOW_COPY_AND_ASSIGN(UndoManager); |
| 92 }; | 112 }; |
| 93 | 113 |
| 94 #endif // CHROME_BROWSER_UNDO_UNDO_MANAGER_H_ | 114 #endif // CHROME_BROWSER_UNDO_UNDO_MANAGER_H_ |
| OLD | NEW |