From 4b1db2bcd67150643a6a87499b70218ae3130dc0 Mon Sep 17 00:00:00 2001
From: Eduardo Bart <edub4rt@gmail.com>
Date: Sat, 26 Jan 2013 18:40:03 -0200
Subject: [PATCH] Fix #235 and other changes

---
 data/styles/10-listboxes.otui               |  1 +
 modules/client_entergame/characterlist.otui | 14 +++++---
 modules/game_cooldown/cooldown.lua          | 22 ++++++-------
 modules/game_npctrade/npctrade.lua          |  2 ++
 modules/game_spelllist/spelllist.lua        |  6 ++--
 src/framework/ui/uiwidget.cpp               |  2 +-
 src/framework/ui/uiwidgetbasestyle.cpp      | 36 ++++++++++++---------
 7 files changed, 46 insertions(+), 37 deletions(-)

diff --git a/data/styles/10-listboxes.otui b/data/styles/10-listboxes.otui
index bdbd99d3..621c5361 100644
--- a/data/styles/10-listboxes.otui
+++ b/data/styles/10-listboxes.otui
@@ -4,6 +4,7 @@ TextList < UIScrollArea
   border-color: #1d222b
   background-color: #222833
   padding: 1
+  auto-focus: none
 
 HorizontalList < UIScrollArea
   layout: horizontalBox
diff --git a/modules/client_entergame/characterlist.otui b/modules/client_entergame/characterlist.otui
index 858d6639..42e8f907 100644
--- a/modules/client_entergame/characterlist.otui
+++ b/modules/client_entergame/characterlist.otui
@@ -1,12 +1,15 @@
 CharacterWidget < UIWidget
   height: 14
-  focusable: true
   background-color: alpha
-  @onFocusChange: |
-    local children = self:getChildren()
-    for i=1,#children do
-      children[i]:setOn(self:isFocused())
+  &updateOnStates: |
+    function(self)
+      local children = self:getChildren()
+      for i=1,#children do
+        children[i]:setOn(self:isFocused())
+      end
     end
+  @onFocusChange: self:updateOnStates()
+  @onSetup: self:updateOnStates()
 
   $focus:
     background-color: #ffffff22
@@ -60,6 +63,7 @@ MainWindow
     padding: 1
     focusable: false
     vertical-scrollbar: characterListScrollBar
+    auto-focus: first
 
   VerticalScrollBar
     id: characterListScrollBar
diff --git a/modules/game_cooldown/cooldown.lua b/modules/game_cooldown/cooldown.lua
index 9bdb8b88..24ea09b3 100644
--- a/modules/game_cooldown/cooldown.lua
+++ b/modules/game_cooldown/cooldown.lua
@@ -4,8 +4,8 @@ contentsPanel = nil
 spellCooldownPanel = nil
 
 function init()
-  connect(g_game, { onGameStart = show,
-                    onGameEnd = hide,
+  connect(g_game, { onGameStart = online,
+                    onGameEnd = offline,
                     onSpellGroupCooldown = onSpellGroupCooldown,
                     onSpellCooldown = onSpellCooldown })
 
@@ -19,15 +19,15 @@ function init()
   
   contentsPanel = cooldownWindow:getChildById('contentsPanel')
   spellCooldownPanel = contentsPanel:getChildById('spellCooldownPanel')
-  
+
   if g_game.isOnline() then
-    show()
+    online()
   end
 end
 
 function terminate()
-  disconnect(g_game, { onGameStart = show,
-                       onGameEnd = hide,
+  disconnect(g_game, { onGameStart = online,
+                       onGameEnd = offline,
                        onSpellGroupCooldown = onSpellGroupCooldown,
                        onSpellCooldown = onSpellCooldown })
 
@@ -49,18 +49,16 @@ function toggle()
   end
 end
 
-function show()
+function online()
   if g_game.getFeature(GameSpellList) then
-    cooldownWindow:show()
     cooldownButton:show()
   else
-    hide()
+    cooldownButton:hide()
+    cooldownWindow:close()
   end
 end
 
-function hide()
-  cooldownWindow:hide()
-  cooldownButton:hide()
+function offline()
 end
 
 function updateProgressRect(progressRect, interval, init)
diff --git a/modules/game_npctrade/npctrade.lua b/modules/game_npctrade/npctrade.lua
index 27ab9f99..8b44780a 100644
--- a/modules/game_npctrade/npctrade.lua
+++ b/modules/game_npctrade/npctrade.lua
@@ -352,6 +352,8 @@ function refreshTradeItems()
 end
 
 function refreshPlayerGoods()
+  if not g_game.isOnline() then return end
+
   moneyLabel:setText(playerMoney .. ' ' .. CURRENCY)
   capacityLabel:setText(string.format('%.2f', playerFreeCapacity) .. ' ' .. WEIGHT_UNIT)
 
diff --git a/modules/game_spelllist/spelllist.lua b/modules/game_spelllist/spelllist.lua
index 458d2996..16f06747 100644
--- a/modules/game_spelllist/spelllist.lua
+++ b/modules/game_spelllist/spelllist.lua
@@ -80,10 +80,10 @@ function getIconImageClip(id)
 end
 
 function online()
-  if g_game.getProtocolVersion() < 870 then
-    spelllistButton:setVisible(false)
+  if g_game.getFeature(GameSpellList) then
+    spelllistButton:show()
   else
-    spelllistButton:setVisible(true)
+    spelllistButton:hide()
   end
   if g_game.getProtocolVersion() >= 950 then -- Vocation is only send in newer clients
     spelllistWindow:getChildById('buttonFilterVocation'):setVisible(true)
diff --git a/src/framework/ui/uiwidget.cpp b/src/framework/ui/uiwidget.cpp
index 8c637116..a6ca0163 100644
--- a/src/framework/ui/uiwidget.cpp
+++ b/src/framework/ui/uiwidget.cpp
@@ -977,7 +977,7 @@ void UIWidget::setFocusable(bool focusable)
         if(UIWidgetPtr parent = getParent()) {
             if(!focusable && isFocused()) {
                 parent->focusPreviousChild(Fw::ActiveFocusReason, true);
-            } else if(focusable && (!parent->getFocusedChild() && parent->getAutoFocusPolicy() != Fw::AutoFocusNone)) {
+            } else if(focusable && !parent->getFocusedChild() && parent->getAutoFocusPolicy() != Fw::AutoFocusNone) {
                 focus();
             }
         }
diff --git a/src/framework/ui/uiwidgetbasestyle.cpp b/src/framework/ui/uiwidgetbasestyle.cpp
index cb340c49..59d0537a 100644
--- a/src/framework/ui/uiwidgetbasestyle.cpp
+++ b/src/framework/ui/uiwidgetbasestyle.cpp
@@ -48,6 +48,26 @@ void UIWidget::initBaseStyle()
 
 void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
 {
+    // parse lua variables and callbacks first
+    for(const OTMLNodePtr& node : styleNode->children()) {
+        // lua functions
+        if(stdext::starts_with(node->tag(), "@")) {
+            // load once
+            if(m_firstOnStyle) {
+                std::string funcName = node->tag().substr(1);
+                std::string funcOrigin = "@" + node->source() + ": [" + node->tag() + "]";
+                g_lua.loadFunction(node->value(), funcOrigin);
+                luaSetField(funcName);
+            }
+        // lua fields value
+        } else if(stdext::starts_with(node->tag(), "&")) {
+            std::string fieldName = node->tag().substr(1);
+            std::string fieldOrigin = "@" + node->source() + ": [" + node->tag() + "]";
+
+            g_lua.evaluateExpression(node->value(), fieldOrigin);
+            luaSetField(fieldName);
+        }
+    }
     // load styles used by all widgets
     for(const OTMLNodePtr& node : styleNode->children()) {
         if(node->tag() == "color")
@@ -309,22 +329,6 @@ void UIWidget::parseBaseStyle(const OTMLNodePtr& styleNode)
                     addAnchor(anchoredEdge, hookedWidgetId, hookedEdge);
                 }
             }
-        // lua functions
-        } else if(stdext::starts_with(node->tag(), "@")) {
-            // load once
-            if(m_firstOnStyle) {
-                std::string funcName = node->tag().substr(1);
-                std::string funcOrigin = "@" + node->source() + ": [" + node->tag() + "]";
-                g_lua.loadFunction(node->value(), funcOrigin);
-                luaSetField(funcName);
-            }
-        // lua fields value
-        } else if(stdext::starts_with(node->tag(), "&")) {
-            std::string fieldName = node->tag().substr(1);
-            std::string fieldOrigin = "@" + node->source() + ": [" + node->tag() + "]";
-
-            g_lua.evaluateExpression(node->value(), fieldOrigin);
-            luaSetField(fieldName);
         }
     }
 }