2011-05-31 03:55:34 +02:00
|
|
|
MessageBox = {}
|
|
|
|
MessageBox.__index = MessageBox
|
|
|
|
|
2011-07-17 08:56:57 +02:00
|
|
|
-- messagebox flags
|
|
|
|
MessageBoxOk = 1
|
|
|
|
MessageBoxCancel = 2
|
|
|
|
|
|
|
|
function MessageBox.create(title, text, flags)
|
|
|
|
local box = {}
|
|
|
|
setmetatable(box, MessageBox)
|
|
|
|
|
|
|
|
-- create messagebox window
|
2011-11-17 21:40:31 +01:00
|
|
|
local window = UI.display('messagebox.otui', { locked = true })
|
2011-08-20 22:30:41 +02:00
|
|
|
window:setTitle(title)
|
2011-07-17 08:56:57 +02:00
|
|
|
|
2011-08-26 17:06:52 +02:00
|
|
|
local label = window:getChildById('messageBoxLabel')
|
2011-08-20 22:30:41 +02:00
|
|
|
label:setStyle('Label')
|
|
|
|
label:setText(text)
|
2011-08-14 04:09:11 +02:00
|
|
|
label:resizeToText()
|
2011-07-17 08:56:57 +02:00
|
|
|
|
|
|
|
-- set window size based on label size
|
2011-11-01 19:32:48 +01:00
|
|
|
window:setWidth(math.max(label:getWidth() + 48, 120))
|
2011-08-20 22:30:41 +02:00
|
|
|
window:setHeight(label:getHeight() + 64)
|
2011-08-26 17:06:52 +02:00
|
|
|
window:updateParentLayout()
|
2011-07-17 08:56:57 +02:00
|
|
|
|
|
|
|
-- setup messagebox first button
|
2011-08-26 17:06:52 +02:00
|
|
|
local buttonRight = window:getChildById('messageBoxRightButton')
|
2011-07-17 08:56:57 +02:00
|
|
|
|
|
|
|
if flags == MessageBoxOk then
|
2011-08-26 17:06:52 +02:00
|
|
|
buttonRight:setText("Ok")
|
2011-08-20 22:30:41 +02:00
|
|
|
box.onOk = EmptyFunction
|
2011-08-26 17:06:52 +02:00
|
|
|
buttonRight.onClick = function()
|
2011-07-17 08:56:57 +02:00
|
|
|
box.onOk()
|
|
|
|
box:destroy()
|
|
|
|
end
|
2011-08-29 04:03:31 +02:00
|
|
|
window.onEnter = buttonRight.onClick
|
|
|
|
window.onEscape = buttonRight.onClick
|
2011-07-17 08:56:57 +02:00
|
|
|
elseif flags == MessageBoxCancel then
|
2011-08-26 17:06:52 +02:00
|
|
|
buttonRight:setText("Cancel")
|
2011-08-20 22:30:41 +02:00
|
|
|
box.onCancel = EmptyFunction
|
2011-08-26 17:06:52 +02:00
|
|
|
buttonRight.onClick = function()
|
2011-07-17 08:56:57 +02:00
|
|
|
box.onCancel()
|
|
|
|
box:destroy()
|
|
|
|
end
|
2011-08-29 04:03:31 +02:00
|
|
|
window.onEnter = buttonRight.onClick
|
|
|
|
window.onEscape = buttonRight.onClick
|
2011-07-17 08:56:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
box.window = window
|
|
|
|
return box
|
2011-05-31 03:55:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function MessageBox:destroy()
|
2011-07-17 08:56:57 +02:00
|
|
|
if self.onDestroy then
|
|
|
|
self.onDestroy()
|
|
|
|
self.onDestroy = nil
|
|
|
|
end
|
|
|
|
if self.window then
|
|
|
|
self.window:destroy()
|
|
|
|
self.window = nil
|
|
|
|
end
|
|
|
|
self.onOk = nil
|
|
|
|
self.onCancel = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- shortcuts for creating message boxes
|
|
|
|
function displayMessageBox(title, text, flags)
|
|
|
|
return MessageBox.create(title, text, flags)
|
|
|
|
end
|
|
|
|
|
|
|
|
function displayErrorBox(title, text)
|
|
|
|
return MessageBox.create(title, text, MessageBoxOk)
|
|
|
|
end
|
|
|
|
|
|
|
|
function displayInfoBox(title, text)
|
|
|
|
return MessageBox.create(title, text, MessageBoxOk)
|
2011-05-03 00:48:41 +02:00
|
|
|
end
|
|
|
|
|
2011-07-17 08:56:57 +02:00
|
|
|
function displayCancelBox(title, text)
|
|
|
|
return MessageBox.create(title, text, MessageBoxCancel)
|
2011-05-01 20:47:35 +02:00
|
|
|
end
|
2011-07-17 08:56:57 +02:00
|
|
|
|