2012-09-05 09:45:45 -06:00
|
|
|
-- Some simple dialog screens
|
|
|
|
|
|
|
|
local _ENV = mkmodule('gui.dialogs')
|
|
|
|
|
|
|
|
local gui = require('gui')
|
2012-10-15 10:03:18 -06:00
|
|
|
local widgets = require('gui.widgets')
|
2012-09-05 09:45:45 -06:00
|
|
|
|
|
|
|
MessageBox = defclass(MessageBox, gui.FramedScreen)
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
MessageBox.focus_path = 'MessageBox'
|
2012-09-18 10:30:25 -06:00
|
|
|
|
|
|
|
MessageBox.ATTRS{
|
2023-01-23 05:16:38 -07:00
|
|
|
frame_style = gui.WINDOW_FRAME,
|
2012-10-16 04:18:35 -06:00
|
|
|
frame_inset = 1,
|
2012-09-18 10:30:25 -06:00
|
|
|
-- new attrs
|
|
|
|
on_accept = DEFAULT_NIL,
|
|
|
|
on_cancel = DEFAULT_NIL,
|
|
|
|
on_close = DEFAULT_NIL,
|
|
|
|
}
|
|
|
|
|
2012-10-16 04:18:35 -06:00
|
|
|
function MessageBox:init(info)
|
|
|
|
self:addviews{
|
|
|
|
widgets.Label{
|
|
|
|
view_id = 'label',
|
|
|
|
text = info.text,
|
|
|
|
text_pen = info.text_pen,
|
|
|
|
frame = { l = 0, t = 0 },
|
|
|
|
auto_height = true
|
|
|
|
}
|
|
|
|
}
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function MessageBox:getWantedFrameSize()
|
2012-10-16 04:18:35 -06:00
|
|
|
local label = self.subviews.label
|
|
|
|
local width = math.max(self.frame_width or 0, 20, #(self.frame_title or '') + 4)
|
2022-05-27 16:38:13 -06:00
|
|
|
local text_area_width = label:getTextWidth()
|
|
|
|
if label.frame_inset then
|
|
|
|
-- account for scroll icons
|
|
|
|
text_area_width = text_area_width + (label.frame_inset.l or 0)
|
|
|
|
text_area_width = text_area_width + (label.frame_inset.r or 0)
|
|
|
|
end
|
|
|
|
return math.max(width, text_area_width), label:getTextHeight()
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
2012-10-16 04:18:35 -06:00
|
|
|
function MessageBox:onRenderFrame(dc,rect)
|
|
|
|
MessageBox.super.onRenderFrame(self,dc,rect)
|
2012-09-05 09:45:45 -06:00
|
|
|
if self.on_accept then
|
2022-12-30 23:34:19 -07:00
|
|
|
dc:seek(rect.x1+2,rect.y2):key('LEAVESCREEN'):string('/'):key('SELECT')
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MessageBox:onDestroy()
|
|
|
|
if self.on_close then
|
|
|
|
self.on_close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function MessageBox:onInput(keys)
|
2023-01-06 11:35:22 -07:00
|
|
|
if keys.SELECT or keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
|
2012-09-05 09:45:45 -06:00
|
|
|
self:dismiss()
|
2022-12-30 02:13:13 -07:00
|
|
|
if keys.SELECT and self.on_accept then
|
2012-09-05 09:45:45 -06:00
|
|
|
self.on_accept()
|
2023-01-06 11:35:22 -07:00
|
|
|
elseif (keys.LEAVESCREEN or keys._MOUSE_R_DOWN) and self.on_cancel then
|
2012-09-05 09:45:45 -06:00
|
|
|
self.on_cancel()
|
|
|
|
end
|
2023-03-27 04:51:45 -06:00
|
|
|
gui.markMouseClicksHandled(keys)
|
2022-12-30 02:13:13 -07:00
|
|
|
return true
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
2022-12-30 02:13:13 -07:00
|
|
|
return self:inputToSubviews(keys)
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function showMessage(title, text, tcolor, on_close)
|
2022-04-27 12:51:46 -06:00
|
|
|
local mb = MessageBox{
|
2012-09-18 10:30:25 -06:00
|
|
|
frame_title = title,
|
2012-09-05 09:45:45 -06:00
|
|
|
text = text,
|
|
|
|
text_pen = tcolor,
|
|
|
|
on_close = on_close
|
2022-04-27 12:51:46 -06:00
|
|
|
}
|
|
|
|
mb:show()
|
|
|
|
return mb
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function showYesNoPrompt(title, text, tcolor, on_accept, on_cancel)
|
2022-04-27 12:51:46 -06:00
|
|
|
local mb = MessageBox{
|
2012-09-18 10:30:25 -06:00
|
|
|
frame_title = title,
|
2012-09-05 09:45:45 -06:00
|
|
|
text = text,
|
|
|
|
text_pen = tcolor,
|
|
|
|
on_accept = on_accept,
|
|
|
|
on_cancel = on_cancel,
|
2022-04-27 12:51:46 -06:00
|
|
|
}
|
|
|
|
mb:show()
|
|
|
|
return mb
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
InputBox = defclass(InputBox, MessageBox)
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
InputBox.focus_path = 'InputBox'
|
|
|
|
|
2012-09-18 10:30:25 -06:00
|
|
|
InputBox.ATTRS{
|
|
|
|
on_input = DEFAULT_NIL,
|
|
|
|
}
|
|
|
|
|
|
|
|
function InputBox:preinit(info)
|
|
|
|
info.on_accept = nil
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
2012-10-15 10:03:18 -06:00
|
|
|
function InputBox:init(info)
|
|
|
|
self:addviews{
|
|
|
|
widgets.EditField{
|
|
|
|
view_id = 'edit',
|
|
|
|
text = info.input,
|
|
|
|
text_pen = info.input_pen,
|
2012-10-16 04:18:35 -06:00
|
|
|
frame = { l = 0, r = 0, h = 1 },
|
2012-10-15 10:03:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-09-05 09:45:45 -06:00
|
|
|
function InputBox:getWantedFrameSize()
|
2012-09-18 10:30:25 -06:00
|
|
|
local mw, mh = InputBox.super.getWantedFrameSize(self)
|
2012-10-16 04:18:35 -06:00
|
|
|
self.subviews.edit.frame.t = mh+1
|
2012-09-05 09:45:45 -06:00
|
|
|
return mw, mh+2
|
|
|
|
end
|
|
|
|
|
|
|
|
function InputBox:onInput(keys)
|
|
|
|
if keys.SELECT then
|
|
|
|
self:dismiss()
|
|
|
|
if self.on_input then
|
2012-10-15 10:03:18 -06:00
|
|
|
self.on_input(self.subviews.edit.text)
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
2022-12-30 02:13:13 -07:00
|
|
|
return true
|
2023-01-06 11:35:22 -07:00
|
|
|
elseif keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
|
2012-09-05 09:45:45 -06:00
|
|
|
self:dismiss()
|
|
|
|
if self.on_cancel then
|
|
|
|
self.on_cancel()
|
|
|
|
end
|
2023-03-27 04:51:45 -06:00
|
|
|
gui.markMouseClicksHandled(keys)
|
2022-12-30 02:13:13 -07:00
|
|
|
return true
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
2022-12-30 02:13:13 -07:00
|
|
|
return self:inputToSubviews(keys)
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
function showInputPrompt(title, text, tcolor, input, on_input, on_cancel, min_width)
|
2022-04-27 12:51:46 -06:00
|
|
|
local ib = InputBox{
|
2012-09-18 10:30:25 -06:00
|
|
|
frame_title = title,
|
2012-09-05 09:45:45 -06:00
|
|
|
text = text,
|
|
|
|
text_pen = tcolor,
|
|
|
|
input = input,
|
|
|
|
on_input = on_input,
|
|
|
|
on_cancel = on_cancel,
|
|
|
|
frame_width = min_width,
|
2022-04-27 12:51:46 -06:00
|
|
|
}
|
|
|
|
ib:show()
|
|
|
|
return ib
|
2012-09-05 09:45:45 -06:00
|
|
|
end
|
|
|
|
|
2012-09-08 17:28:07 -06:00
|
|
|
ListBox = defclass(ListBox, MessageBox)
|
|
|
|
|
|
|
|
ListBox.focus_path = 'ListBox'
|
|
|
|
|
2012-09-18 10:30:25 -06:00
|
|
|
ListBox.ATTRS{
|
2012-10-17 00:41:50 -06:00
|
|
|
with_filter = false,
|
2021-07-30 09:25:01 -06:00
|
|
|
dismiss_on_select = true,
|
|
|
|
dismiss_on_select2 = true,
|
2012-10-17 00:41:50 -06:00
|
|
|
cursor_pen = DEFAULT_NIL,
|
2012-09-18 10:30:25 -06:00
|
|
|
select_pen = DEFAULT_NIL,
|
2012-11-29 05:27:51 -07:00
|
|
|
on_select = DEFAULT_NIL,
|
|
|
|
on_select2 = DEFAULT_NIL,
|
|
|
|
select2_hint = DEFAULT_NIL,
|
2020-08-17 22:14:05 -06:00
|
|
|
row_height = DEFAULT_NIL,
|
2022-04-22 09:30:53 -06:00
|
|
|
list_frame_inset = DEFAULT_NIL,
|
2012-09-18 10:30:25 -06:00
|
|
|
}
|
|
|
|
|
2012-10-16 04:18:35 -06:00
|
|
|
function ListBox:preinit(info)
|
2012-09-18 10:30:25 -06:00
|
|
|
info.on_accept = nil
|
|
|
|
end
|
|
|
|
|
2012-09-08 17:28:07 -06:00
|
|
|
function ListBox:init(info)
|
2012-11-03 10:06:33 -06:00
|
|
|
local spen = dfhack.pen.parse(COLOR_CYAN, self.select_pen, nil, false)
|
|
|
|
local cpen = dfhack.pen.parse(COLOR_LIGHTCYAN, self.cursor_pen or self.select_pen, nil, true)
|
2012-10-17 00:41:50 -06:00
|
|
|
|
|
|
|
local list_widget = widgets.List
|
|
|
|
if self.with_filter then
|
|
|
|
list_widget = widgets.FilteredList
|
|
|
|
end
|
2012-10-02 06:45:17 -06:00
|
|
|
|
2012-11-29 05:27:51 -07:00
|
|
|
local on_submit2
|
|
|
|
if self.select2_hint or self.on_select2 then
|
|
|
|
on_submit2 = function(sel, obj)
|
2021-07-30 09:25:01 -06:00
|
|
|
if self.dismiss_on_select2 then self:dismiss() end
|
2012-11-29 05:27:51 -07:00
|
|
|
if self.on_select2 then self.on_select2(sel, obj) end
|
|
|
|
local cb = obj.on_select2
|
|
|
|
if cb then cb(obj, sel) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-16 04:18:35 -06:00
|
|
|
self:addviews{
|
2012-10-17 00:41:50 -06:00
|
|
|
list_widget{
|
2012-10-16 04:18:35 -06:00
|
|
|
view_id = 'list',
|
|
|
|
selected = info.selected,
|
|
|
|
choices = info.choices,
|
2012-10-17 08:29:15 -06:00
|
|
|
icon_width = info.icon_width,
|
2012-10-16 04:18:35 -06:00
|
|
|
text_pen = spen,
|
|
|
|
cursor_pen = cpen,
|
|
|
|
on_submit = function(sel,obj)
|
2021-07-30 09:25:01 -06:00
|
|
|
if self.dismiss_on_select then self:dismiss() end
|
2012-10-16 04:18:35 -06:00
|
|
|
if self.on_select then self.on_select(sel, obj) end
|
|
|
|
local cb = obj.on_select or obj[2]
|
|
|
|
if cb then cb(obj, sel) end
|
|
|
|
end,
|
2012-11-29 05:27:51 -07:00
|
|
|
on_submit2 = on_submit2,
|
2022-04-22 09:30:53 -06:00
|
|
|
frame = { l = 0, r = 0},
|
|
|
|
frame_inset = self.list_frame_inset,
|
2022-04-27 12:51:46 -06:00
|
|
|
row_height = self.row_height,
|
2012-10-16 04:18:35 -06:00
|
|
|
}
|
|
|
|
}
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
|
|
|
|
2012-11-29 05:27:51 -07:00
|
|
|
function ListBox:onRenderFrame(dc,rect)
|
|
|
|
ListBox.super.onRenderFrame(self,dc,rect)
|
2023-03-27 04:51:45 -06:00
|
|
|
if self.select2_hint then
|
|
|
|
dc:seek(rect.x1+2,rect.y2):string('Shift-Click', COLOR_LIGHTGREEN):string(': '..self.select2_hint, COLOR_GREY)
|
|
|
|
end
|
2012-11-29 05:27:51 -07:00
|
|
|
end
|
|
|
|
|
2012-09-08 17:28:07 -06:00
|
|
|
function ListBox:getWantedFrameSize()
|
2022-04-11 17:15:50 -06:00
|
|
|
local mw, mh = ListBox.super.getWantedFrameSize(self)
|
2012-10-16 04:18:35 -06:00
|
|
|
local list = self.subviews.list
|
|
|
|
list.frame.t = mh+1
|
2022-04-11 17:15:50 -06:00
|
|
|
return math.max(mw, list:getContentWidth()), mh+3+math.min(18,list:getContentHeight())
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
2012-09-18 10:30:25 -06:00
|
|
|
|
2012-09-08 17:28:07 -06:00
|
|
|
function ListBox:onInput(keys)
|
2023-01-06 11:35:22 -07:00
|
|
|
if keys.LEAVESCREEN or keys._MOUSE_R_DOWN then
|
2012-09-08 17:28:07 -06:00
|
|
|
self:dismiss()
|
|
|
|
if self.on_cancel then
|
|
|
|
self.on_cancel()
|
|
|
|
end
|
2023-03-27 04:51:45 -06:00
|
|
|
gui.markMouseClicksHandled(keys)
|
2022-12-30 02:13:13 -07:00
|
|
|
return true
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
2022-12-30 02:13:13 -07:00
|
|
|
return self:inputToSubviews(keys)
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
|
|
|
|
2012-10-17 00:41:50 -06:00
|
|
|
function showListPrompt(title, text, tcolor, choices, on_select, on_cancel, min_width, filter)
|
2022-04-27 12:51:46 -06:00
|
|
|
local lb = ListBox{
|
2012-09-18 10:30:25 -06:00
|
|
|
frame_title = title,
|
2012-09-08 17:28:07 -06:00
|
|
|
text = text,
|
|
|
|
text_pen = tcolor,
|
|
|
|
choices = choices,
|
2012-10-02 06:45:17 -06:00
|
|
|
on_select = on_select,
|
2012-09-08 17:28:07 -06:00
|
|
|
on_cancel = on_cancel,
|
|
|
|
frame_width = min_width,
|
2012-10-17 00:41:50 -06:00
|
|
|
with_filter = filter,
|
2022-04-27 12:51:46 -06:00
|
|
|
}
|
|
|
|
lb:show()
|
|
|
|
return lb
|
2012-09-08 17:28:07 -06:00
|
|
|
end
|
2012-09-05 09:45:45 -06:00
|
|
|
|
|
|
|
return _ENV
|