From fe98efdc21c96244cd4c133b90c7a1fcd472cc42 Mon Sep 17 00:00:00 2001
From: TheSumm <sum9900@web.de>
Date: Mon, 20 Apr 2015 19:22:50 +0200
Subject: [PATCH] Fix modal dialog auto sizing, fixes #556

---
 modules/game_modaldialog/modaldialog.lua  | 22 +++++++++++-----------
 modules/game_modaldialog/modaldialog.otui | 16 ++++++++++------
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/modules/game_modaldialog/modaldialog.lua b/modules/game_modaldialog/modaldialog.lua
index aab82b37..7c89ca10 100644
--- a/modules/game_modaldialog/modaldialog.lua
+++ b/modules/game_modaldialog/modaldialog.lua
@@ -35,16 +35,12 @@ function onModalDialog(id, title, message, buttons, enterButton, escapeButton, c
   local messageLabel = modalDialog:getChildById('messageLabel')
   local choiceList = modalDialog:getChildById('choiceList')
   local choiceScrollbar = modalDialog:getChildById('choiceScrollBar')
-  local buttonList = modalDialog:getChildById('buttonList')
+  local buttonsPanel = modalDialog:getChildById('buttonsPanel')
 
   modalDialog:setText(title)
   messageLabel:setText(message)
 
-  local horizontalPadding = modalDialog:getPaddingLeft() + modalDialog:getPaddingRight()
-  modalDialog:setWidth(math.min(modalDialog.maximumWidth, math.max(messageLabel:getWidth(), modalDialog.minimumWidth)))
-  messageLabel:setWidth(math.min(modalDialog.maximumWidth, math.max(messageLabel:getWidth(), modalDialog.minimumWidth)) - horizontalPadding)
-
-  local labelHeight = nil
+  local labelHeight
   for i = 1, #choices do
     local choiceId = choices[i][1]
     local choiceName = choices[i][2]
@@ -59,11 +55,12 @@ function onModalDialog(id, title, message, buttons, enterButton, escapeButton, c
   end
   choiceList:focusNextChild()
 
+  local buttonsWidth = 0
   for i = 1, #buttons do
     local buttonId = buttons[i][1]
     local buttonText = buttons[i][2]
 
-    local button = g_ui.createWidget('ModalButton', buttonList)
+    local button = g_ui.createWidget('ModalButton', buttonsPanel)
     button:setText(buttonText)
     button.onClick = function(self)
                        local focusedChoice = choiceList:getFocusedChild()
@@ -74,6 +71,7 @@ function onModalDialog(id, title, message, buttons, enterButton, escapeButton, c
                        g_game.answerModalDialog(id, buttonId, choice)
                        destroyDialog()
                      end
+    buttonsWidth = buttonsWidth + button:getWidth() + button:getMarginLeft() + button:getMarginRight()
   end
 
   local additionalHeight = 0
@@ -84,11 +82,13 @@ function onModalDialog(id, title, message, buttons, enterButton, escapeButton, c
     additionalHeight = math.min(modalDialog.maximumChoices, math.max(modalDialog.minimumChoices, #choices)) * labelHeight
     additionalHeight = additionalHeight + choiceList:getPaddingTop() + choiceList:getPaddingBottom()
   end
-  modalDialog:setHeight(modalDialog:getHeight() + additionalHeight)
 
-  addEvent(function()
-             modalDialog:setHeight(modalDialog:getHeight() + messageLabel:getHeight() - 14)
-           end)
+  local horizontalPadding = modalDialog:getPaddingLeft() + modalDialog:getPaddingRight()
+  buttonsWidth = buttonsWidth + horizontalPadding
+
+  modalDialog:setWidth(math.min(modalDialog.maximumWidth, math.max(buttonsWidth, messageLabel:getWidth(), modalDialog.minimumWidth)))
+  messageLabel:setWidth(math.min(modalDialog.maximumWidth, math.max(buttonsWidth, messageLabel:getWidth(), modalDialog.minimumWidth)) - horizontalPadding)
+  modalDialog:setHeight(modalDialog:getHeight() + additionalHeight + messageLabel:getHeight() - 8)
 
   local enterFunc = function()
     local focusedChoice = choiceList:getFocusedChild()
diff --git a/modules/game_modaldialog/modaldialog.otui b/modules/game_modaldialog/modaldialog.otui
index b4fe180b..2f1824df 100644
--- a/modules/game_modaldialog/modaldialog.otui
+++ b/modules/game_modaldialog/modaldialog.otui
@@ -5,7 +5,7 @@ ChoiceListLabel < Label
   focusable: true
 
   $focus:
-    background-color: #ffffff22
+    background-color: #00000055
     color: #ffffff
 
 ChoiceList < TextList
@@ -14,7 +14,6 @@ ChoiceList < TextList
   anchors.fill: parent
   anchors.top: prev.bottom
   anchors.bottom: next.top
-  padding: 1
   margin-top: 4
   margin-bottom: 10
   focusable: false
@@ -30,14 +29,19 @@ ChoiceScrollBar < VerticalScrollBar
   visible: false
 
 ModalButton < Button
-  width: 60
-  margin: 2
+  text-auto-resize: true
+  margin-top: 2
+  margin-bottom: 2
+  margin-left: 2
+
+  $pressed:
+    text-offset: 0 0
 
 ModalDialog < MainWindow
   id: modalDialog
   size: 280 97
   &minimumWidth: 200
-  &maximumWidth: 500
+  &maximumWidth: 600
   &minimumChoices: 4
   &maximumChoices: 10
 
@@ -57,7 +61,7 @@ ModalDialog < MainWindow
     anchors.bottom: next.top
 
   Panel
-    id: buttonList
+    id: buttonsPanel
     anchors.left: parent.left
     anchors.right: parent.right
     anchors.bottom: parent.bottom