From a2804705db7259109232c23e9cd1ef86093237b1 Mon Sep 17 00:00:00 2001
From: markt <markt>
Date: Mon, 15 Jan 2007 19:00:09 +0000
Subject: prevent per-window alpha menu from scrolling past 0 or 255: suppose
 your alpha was at 3 and then you double-clicked -- IntResMenuItem was setting
 the alpha to -2, which in FbWinFrame::setAlpha got cast to an unsigned char,
 or 254; then, IntResMenuItem would check if the value was less than 0, which,
 of course, it wasn't now, IntResMenuItem checks if the value will exceed the
 max/min before setting

---
 ChangeLog             |  2 ++
 src/IntResMenuItem.hh | 30 ++++++++++++++++--------------
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3393cb6..20ea2e9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,8 @@
  (Format: Year/Month/Day)
 Changes for 1.0rc3:
 *07/01/15:
+   * Prevent per-window alpha menu from scrolling past 0 or 255 (Mark)
+     IntResMenuItem.hh
    * Fix rootmenu disappearing on reconfigure (Mark)
      Screen.cc
 *07/01/14:
diff --git a/src/IntResMenuItem.hh b/src/IntResMenuItem.hh
index 0be2775..768870e 100644
--- a/src/IntResMenuItem.hh
+++ b/src/IntResMenuItem.hh
@@ -56,20 +56,22 @@ public:
         if (time - last_time <= 200)
             inc_val = 5;
 
-
         last_time = time;
-        
-        if ((button == 4 || button == 3)&& *m_res < m_max) // scroll up
-            m_res.get() += inc_val;
-        else if ((button == 5 || button == 1) && *m_res > m_min) // scroll down
-            m_res.get() -= inc_val;
-        
-        // clamp value
-        if (*m_res > m_max)
-            m_res.get() = m_max;
-        else if (*m_res < m_min)
-            m_res.get() = m_min;
-        
+
+        // make sure values stay within bounds _before_ we try to set m_res
+        // otherwise, this may cause bugs (say, with casting to unsigned char)
+        if ((button == 4 || button == 3) && *m_res < m_max) { // up
+            if (*m_res + inc_val < m_max)
+                m_res.get() += inc_val;
+            else
+                m_res.get() = m_max;
+        } else if ((button == 5 || button == 1) && *m_res > m_min) { // down
+            if (*m_res - inc_val >= m_min)
+                m_res.get() -= inc_val;
+            else
+                m_res.get() = m_min;
+        }
+
         // update label
         updateLabel();
         // call other commands
@@ -86,7 +88,7 @@ public:
     void updateLabel() {
         setLabel(appendIntValue(m_org_label, *m_res));
     }
-        
+
 private:
     std::string m_org_label; ///< original label
     const int m_max; ///< maximum value the integer can have
-- 
cgit v0.11.2