Home | History | Annotate | Download | only in undo
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UNDO_UNDO_MANAGER_H_
      6 #define CHROME_BROWSER_UNDO_UNDO_MANAGER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/memory/scoped_vector.h"
     11 
     12 class UndoOperation;
     13 
     14 // UndoGroup ------------------------------------------------------------------
     15 
     16 // UndoGroup represents a user action and stores all the operations that
     17 // make that action.  Typically there is only one operation per UndoGroup.
     18 class UndoGroup {
     19  public:
     20   UndoGroup();
     21   ~UndoGroup();
     22 
     23   void AddOperation(scoped_ptr<UndoOperation> operation);
     24   const std::vector<UndoOperation*>& undo_operations() {
     25     return operations_.get();
     26   }
     27   void Undo();
     28 
     29  private:
     30   ScopedVector<UndoOperation> operations_;
     31 
     32   DISALLOW_COPY_AND_ASSIGN(UndoGroup);
     33 };
     34 
     35 // UndoManager ----------------------------------------------------------------
     36 
     37 // Maintains user actions as a group of operations that store enough info to
     38 // undo and redo those operations.
     39 class UndoManager {
     40  public:
     41   UndoManager();
     42   ~UndoManager();
     43 
     44   // Perform an undo or redo operation.
     45   void Undo();
     46   void Redo();
     47 
     48   size_t undo_count() const { return undo_actions_.size(); }
     49   size_t redo_count() const { return redo_actions_.size(); }
     50 
     51   void AddUndoOperation(scoped_ptr<UndoOperation> operation);
     52 
     53   // Group multiple operations into one undoable action.
     54   void StartGroupingActions();
     55   void EndGroupingActions();
     56 
     57   // Suspend undo tracking while processing non-user initiated changes such as
     58   // profile synchonization.
     59   void SuspendUndoTracking();
     60   void ResumeUndoTracking();
     61   bool IsUndoTrakingSuspended() const;
     62 
     63   // Returns all UndoOperations that are awaiting Undo or Redo. Note that
     64   // ownership of the UndoOperations is retained by UndoManager.
     65   std::vector<UndoOperation*> GetAllUndoOperations() const;
     66 
     67   // Remove all undo and redo operations. Note that grouping of actions and
     68   // suspension of undo tracking states are left unchanged.
     69   void RemoveAllOperations();
     70 
     71  private:
     72   void Undo(bool* performing_indicator,
     73             ScopedVector<UndoGroup>* active_undo_group);
     74   bool is_user_action() const { return !performing_undo_ && !performing_redo_; }
     75 
     76   // Handle the addition of |new_undo_group| to the active undo group container.
     77   void AddUndoGroup(UndoGroup* new_undo_group);
     78 
     79   // Returns the undo or redo UndoGroup container that should store the next
     80   // change taking into account if an undo or redo is being executed.
     81   ScopedVector<UndoGroup>* GetActiveUndoGroup();
     82 
     83   // Containers of user actions ready for an undo or redo treated as a stack.
     84   ScopedVector<UndoGroup> undo_actions_;
     85   ScopedVector<UndoGroup> redo_actions_;
     86 
     87   // Supports grouping operations into a single undo action.
     88   int group_actions_count_;
     89 
     90   // The container that is used when actions are grouped.
     91   scoped_ptr<UndoGroup> pending_grouped_action_;
     92 
     93   // Supports the suspension of undo tracking.
     94   int undo_suspended_count_;
     95 
     96   // Set when executing Undo or Redo so that incoming changes are correctly
     97   // processed.
     98   bool performing_undo_;
     99   bool performing_redo_;
    100 
    101   DISALLOW_COPY_AND_ASSIGN(UndoManager);
    102 };
    103 
    104 #endif  // CHROME_BROWSER_UNDO_UNDO_MANAGER_H_
    105