[nemiver] 644569 Ensure last expr becomes last history item



commit 6b90b90bff64877d4c4d4d546fab47b49263ec86
Author: Dodji Seketeli <dodji seketeli org>
Date:   Sat Mar 12 16:59:09 2011 +0100

    644569 Ensure last expr becomes last history item
    
    	* src/persp/dbgperspective/nmv-call-function-dialog.cc
    	(CallFunctionDialog::Priv::exists_in_history): Return an iterator
    	to the history item found.
    	(CallFunctionDialog::Priv::erase_expression_from_history): New
    	function.
    	(CallFunctionDialog::Priv::add_to_history): Make sure the item to
    	add is the last history item.
    	* src/persp/dbgperspective/nmv-var-inspector-dialog.cc
    	(VarInspectorDialog::Priv::exists_in_history): Return an iterator
    	to the history item found.
    	(VarInspectorDialog::Priv::erase_expression_from_history): New
    	function.
    	(VarInspectorDialog::Priv::add_to_history): Make sure the item to
    	add is the last history item.
    	(VarInspectorDialog::Priv::set_history): New function.
    	(VarInspectorDialog::set_history): Use it.

 .../dbgperspective/nmv-call-function-dialog.cc     |   52 +++++++++++++--
 .../dbgperspective/nmv-var-inspector-dialog.cc     |   71 +++++++++++++++++---
 2 files changed, 108 insertions(+), 15 deletions(-)
---
diff --git a/src/persp/dbgperspective/nmv-call-function-dialog.cc b/src/persp/dbgperspective/nmv-call-function-dialog.cc
index f2ee907..d48e5de 100644
--- a/src/persp/dbgperspective/nmv-call-function-dialog.cc
+++ b/src/persp/dbgperspective/nmv-call-function-dialog.cc
@@ -104,7 +104,18 @@ struct CallFunctionDialog::Priv {
         }
     }
 
-    bool exists_in_history (const UString &a_expr) const
+    /// Tests whether if the expression exists in history.
+    /// 
+    /// \param a_expr the expression to look for.
+    /// 
+    /// \param a_iter if the function returned true and if this
+    /// pointer is non-null, then *a_iter is filled with an iterator
+    /// pointing at the expression found in history.
+    ///
+    /// \return true if the expresison exists in history, false
+    /// otherwise.
+    bool exists_in_history (const UString &a_expr,
+                            Gtk::TreeModel::iterator *a_iter = 0) const
     {
         THROW_IF_FAIL (call_expr_history);
         Gtk::TreeModel::iterator it;
@@ -112,28 +123,57 @@ struct CallFunctionDialog::Priv {
              it != call_expr_history->children ().end ();
              ++it) {
             if ((*it)[get_call_expr_history_cols ().expr] == a_expr) {
+                if (a_iter != 0)
+                    *a_iter = it;
                 return true;
             }
         }
         return false;
     }
 
+    /// Erases an expression from expression history.
+    ///
+    /// \param a_iter the iterator pointing to the expression to erase
+    /// from history.
+    void erase_expression_from_history (Gtk::TreeModel::iterator &a_iter)
+    {
+        THROW_IF_FAIL (call_expr_history);
+        call_expr_history->erase (a_iter);
+    }
+
     void clear_history ()
     {
         call_expr_history->clear ();
     }
 
+    /// Add an expression to expression history.  If the expression
+    /// already exists in history, it can either be duplicated or be
+    /// not.  Also, the new expression can be either appended or
+    /// prepended to history.
+    ///
+    /// \param a_expr the expression to add to history.
+    ///
+    /// \param a_prepend if true, prepend the expression to history.
+    ///
+    /// \param allow_dups if true, allow expressions to be present in
+    /// more than copy in history.
     void add_to_history (const UString &a_expr,
                          bool a_prepend = false,
-                         bool allow_dups = true)
+                         bool allow_dups = false)
     {
-        if (a_expr.empty () // don't append empty expressions
-            // don't append an expression if it exists already.
-            || (!allow_dups && exists_in_history (a_expr)))
+        // don't append empty expressions
+        if (a_expr.empty ())
             return;
+        
+        // If the expression already exists in history, remove it, so
+        // that it can be added again, as to be the latest added item
+        // to historry.
+        Gtk::TreeModel::iterator it;
+        if (!allow_dups
+            && exists_in_history (a_expr, &it))
+            erase_expression_from_history (it);
 
         THROW_IF_FAIL (call_expr_history);
-        Gtk::TreeModel::iterator it;
         if (a_prepend)
             it = call_expr_history->insert
                                     (call_expr_history->children ().begin ());
diff --git a/src/persp/dbgperspective/nmv-var-inspector-dialog.cc b/src/persp/dbgperspective/nmv-var-inspector-dialog.cc
index 2993417..88fc1ec 100644
--- a/src/persp/dbgperspective/nmv-var-inspector-dialog.cc
+++ b/src/persp/dbgperspective/nmv-var-inspector-dialog.cc
@@ -144,7 +144,18 @@ public:
                         false /*don't allow duplicates*/);
     }
 
-    bool exists_in_history (const UString &a_expr)
+    /// Tests wheter if the variable expression exists in history.
+    ///
+    /// \param a_expr the expression to look for.
+    ///
+    /// \param a_iter if the return returned true and if this pointer
+    /// is non-null, then *a_iter is filled with an iterator pointing
+    /// at the expression found in history.
+    /// 
+    /// \return true if the variable expression a_expr exits in
+    /// memory, false otherwise.
+    bool exists_in_history (const UString &a_expr,
+                            Gtk::TreeModel::iterator *a_iter = 0) const
     {
         THROW_IF_FAIL (m_variable_history);
 
@@ -153,22 +164,52 @@ public:
              it != m_variable_history->children ().end ();
              ++it) {
             if ((*it)[get_cols ().varname] == a_expr) {
+                if (a_iter != 0)
+                    *a_iter = it;
                 return true;
             }
         }
         return false;
     }
 
+    /// Erases an expression from expression history.
+    ///
+    /// \param a_iter the iterator pointing to the expression to erase
+    /// from history.
+    void erase_expression_from_history (Gtk::TreeModel::iterator &a_iter)
+    {
+        THROW_IF_FAIL (m_variable_history);
+        m_variable_history->erase (a_iter);
+    }
+
+    /// Add an expression to variable expression history.  If the
+    /// expression already exists in history, it can either be
+    /// duplicated or be not.  Also, the new expression can be either
+    /// appended or prepended to history.
+    ///
+    /// \param a_expr the expression to add to history.
+    ///
+    /// \param a_prepend if true, prepend the expression to history.
+    ///
+    /// \param allow_dups if true, allow expressions to be present in
+    /// more than copy in history.
     void add_to_history (const UString &a_expr,
                          bool a_prepend = false,
-                         bool a_allow_dups = true)
+                         bool a_allow_dups = false)
     {
-        if (a_expr.empty () // don't append empty exprs.
-            // don't append an expr if it exists already.
-            || (!a_allow_dups && exists_in_history (a_expr)))
+        // Don't append empty expressions.
+        if (a_expr.empty ())
             return;
+
+        // If the expression already exists in history, remove it, so
+        // that it can be added again, as to be the latest added item
+        // to historry.
         Gtk::TreeModel::iterator it;
+        if (!a_allow_dups
+            && exists_in_history (a_expr, &it))
+            erase_expression_from_history (it);
 
+        THROW_IF_FAIL (m_variable_history);
         if (a_prepend)
             it = m_variable_history->insert
                 (m_variable_history->children ().begin ());
@@ -188,6 +229,18 @@ public:
         }
     }
 
+    /// Set the current history of variable expression to a new one.
+    ///
+    /// \param a_hist the new history to set.
+    void set_history (const std::list<UString> &a_hist)
+    {
+        m_variable_history->clear ();
+        std::list<UString>::const_iterator it;
+        for (it = a_hist.begin (); it != a_hist.end (); ++it)
+            add_to_history (*it, /*a_prepend=*/false,
+                            /*a_allow_dups=*/false);
+    }
+
     //************************
     //<signal handlers>
     //*************************
@@ -271,15 +324,15 @@ VarInspectorDialog::variable () const
     return m_priv->var_inspector->get_variable ();
 }
 
+/// Set the history of variable expression to a new one.
+///
+/// \param a_hist the new history.
 void
 VarInspectorDialog::set_history (const std::list<UString> &a_hist)
 {
     THROW_IF_FAIL (m_priv);
 
-    list<UString>::const_iterator it;
-    for (it = a_hist.begin (); it != a_hist.end (); ++it) {
-        m_priv->add_to_history (*it, false /*append*/);
-    }
+    m_priv->set_history (a_hist);
 }
 
 void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]