2012-04-26 17:11:03 +02:00
|
|
|
if not UIWindow then dofile 'uiwindow' end
|
|
|
|
|
2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
2012-01-15 16:13:22 +01:00
|
|
|
UIMessageBox = extends(UIWindow)
|
|
|
|
|
2012-02-06 20:19:47 +01:00
|
|
|
-- messagebox cannot be created from otui files
|
|
|
|
UIMessageBox.create = nil
|
|
|
|
|
2012-08-13 01:27:41 +02:00
|
|
|
function UIMessageBox.display(title, message, buttons, onEnterCallback, onEscapeCallback)
|
|
|
|
local messageBox = UIMessageBox.internalCreate()
|
|
|
|
rootWidget:addChild(messageBox)
|
2012-01-15 16:13:22 +01:00
|
|
|
|
2012-08-13 01:27:41 +02:00
|
|
|
messageBox:setStyle('MainWindow')
|
|
|
|
messageBox:setText(title)
|
2012-01-15 16:13:22 +01:00
|
|
|
|
2012-08-13 01:27:41 +02:00
|
|
|
local messageLabel = g_ui.createWidget('MessageBoxLabel', messageBox)
|
2012-02-06 20:19:47 +01:00
|
|
|
messageLabel:setText(message)
|
2012-08-13 01:27:41 +02:00
|
|
|
|
|
|
|
local buttonsWidth = 0
|
|
|
|
local buttonsHeight = 0
|
|
|
|
|
|
|
|
local anchor = AnchorRight
|
|
|
|
if buttons.anchor then anchor = buttons.anchor end
|
|
|
|
|
|
|
|
local buttonHolder = g_ui.createWidget('MessageBoxButtonHolder', messageBox)
|
|
|
|
buttonHolder:addAnchor(anchor, 'parent', anchor)
|
|
|
|
|
|
|
|
for i=1,#buttons do
|
|
|
|
local button = messageBox:addButton(buttons[i].text, buttons[i].callback)
|
|
|
|
if i == 1 then
|
|
|
|
button:setMarginLeft(0)
|
|
|
|
button:addAnchor(AnchorBottom, 'parent', AnchorBottom)
|
|
|
|
button:addAnchor(AnchorLeft, 'parent', AnchorLeft)
|
|
|
|
buttonsHeight = button:getHeight()
|
|
|
|
else
|
|
|
|
button:addAnchor(AnchorBottom, 'prev', AnchorBottom)
|
|
|
|
button:addAnchor(AnchorLeft, 'prev', AnchorRight)
|
|
|
|
end
|
|
|
|
buttonsWidth = buttonsWidth + button:getWidth() + button:getMarginLeft()
|
2012-02-06 20:19:47 +01:00
|
|
|
end
|
|
|
|
|
2012-08-13 01:27:41 +02:00
|
|
|
buttonHolder:setWidth(buttonsWidth)
|
|
|
|
buttonHolder:setHeight(buttonsHeight)
|
2012-08-06 21:15:20 +02:00
|
|
|
|
2012-08-13 01:27:41 +02:00
|
|
|
if onEnterCallback then connect(messageBox, { onEnter = onEnterCallback }) end
|
|
|
|
if onEscapeCallback then connect(messageBox, { onEscape = onEscapeCallback }) end
|
2012-01-15 16:13:22 +01:00
|
|
|
|
2012-08-13 01:27:41 +02:00
|
|
|
messageBox:setWidth(math.max(messageLabel:getWidth(), messageBox:getTextSize().width, buttonHolder:getWidth()) + messageBox:getPaddingLeft() + messageBox:getPaddingRight())
|
|
|
|
messageBox:setHeight(messageLabel:getHeight() + messageBox:getPaddingTop() + messageBox:getPaddingBottom() + buttonHolder:getHeight() + buttonHolder:getMarginTop())
|
|
|
|
return messageBox
|
2012-01-15 16:13:22 +01:00
|
|
|
end
|
|
|
|
|
2012-02-06 20:19:47 +01:00
|
|
|
function displayInfoBox(title, message)
|
2012-08-14 22:37:07 +02:00
|
|
|
local messageBox
|
|
|
|
local defaultCallback = function() messageBox:ok() end
|
|
|
|
messageBox = UIMessageBox.display(title, message, {{text='Ok', callback=defaultCallback}}, defaultCallback, defaultCallback)
|
|
|
|
return messageBox
|
2012-02-06 20:19:47 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function displayErrorBox(title, message)
|
2012-08-14 22:37:07 +02:00
|
|
|
local messageBox
|
|
|
|
local defaultCallback = function() messageBox:ok() end
|
|
|
|
messageBox = UIMessageBox.display(title, message, {{text='Ok', callback=defaultCallback}}, defaultCallback, defaultCallback)
|
|
|
|
return messageBox
|
2012-02-06 20:19:47 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function displayCancelBox(title, message)
|
2012-08-14 22:37:07 +02:00
|
|
|
local messageBox
|
|
|
|
local defaultCallback = function() messageBox:cancel() end
|
|
|
|
messageBox = UIMessageBox.display(title, message, {{text='Cancel', callback=defaultCallback}}, defaultCallback, defaultCallback)
|
|
|
|
return messageBox
|
2012-08-13 01:27:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function displayGeneralBox(title, message, buttons, onEnterCallback, onEscapeCallback)
|
|
|
|
return UIMessageBox.display(title, message, buttons, onEnterCallback, onEscapeCallback)
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIMessageBox:addButton(text, callback)
|
|
|
|
local buttonHolder = self:getChildById('buttonHolder')
|
|
|
|
local button = g_ui.createWidget('MessageBoxButton', buttonHolder)
|
|
|
|
button:setText(text)
|
|
|
|
connect(button, { onClick = callback })
|
|
|
|
return button
|
2012-02-06 20:19:47 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function UIMessageBox:ok()
|
|
|
|
signalcall(self.onOk, self)
|
|
|
|
self.onOk = nil
|
|
|
|
self:destroy()
|
2012-01-15 16:13:22 +01:00
|
|
|
end
|
|
|
|
|
2012-02-06 20:19:47 +01:00
|
|
|
function UIMessageBox:cancel()
|
|
|
|
signalcall(self.onCancel, self)
|
|
|
|
self.onCancel = nil
|
|
|
|
self:destroy()
|
2012-01-15 16:13:22 +01:00
|
|
|
end
|