forked from evl/auctionlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListBox.lua
More file actions
174 lines (151 loc) · 4.91 KB
/
ListBox.lua
File metadata and controls
174 lines (151 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
-------------------------------------------------------------------------------
-- ListBox.lua
--
-- A custom control for use with AceConfig. This control doesn't implement
-- the spec exactly--it just does what it needs to for AuctionLite's purposes.
-------------------------------------------------------------------------------
local AceGUI = LibStub("AceGUI-3.0");
local Version = 1;
local Type = "ListBox";
local ListBox = {};
-- Draw the list box.
local function ListBox_Update(box)
local scrollFrame = box.scrollFrame;
local buttons = box.buttons;
local list = box.obj.list;
local values = box.obj.values;
local numButtons = #buttons;
local numCategories = #list;
if numCategories > numButtons and not scrollFrame:IsShown() then
OptionsList_DisplayScrollBar(box);
elseif numCategories <= numButtons and scrollFrame:IsShown() then
OptionsList_HideScrollBar(box);
end
FauxScrollFrame_Update(scrollFrame, numCategories, numButtons,
buttons[1]:GetHeight());
OptionsList_ClearSelection(box, box.buttons);
local offset = FauxScrollFrame_GetOffset(scrollFrame);
for i = 1, numButtons do
local item = list[i + offset];
if item then
OptionsList_DisplayButton(buttons[i], { name = item, id = i + offset });
if values[i + offset] then
OptionsList_SelectButton(box, buttons[i]);
end
else
OptionsList_HideButton(buttons[i]);
end
end
end
-- Handle a mouse click on an entry.
local function ListButton_OnClick(button)
local box = button:GetParent();
local id = button.element.id;
box.obj:Fire("OnValueChanged", id, not box.obj.values[id]);
box.obj:Fire("OnClosed");
ListBox_Update(box);
end
-- Acquire this widget.
function ListBox:OnAcquire()
self:SetDisabled(false);
end
-- Release this widget for reuse.
function ListBox:OnRelease()
self.frame:ClearAllPoints();
self.frame:Hide();
self:SetDisabled(false);
end
-- Enable/disable the widget.
function ListBox:SetDisabled(disabled)
self.disabled = disabled;
if disabled then
self.box:EnableMouse(false);
self.label:SetTextColor(0.5, 0.5, 0.5);
else
self.box:EnableMouse(true);
self.label:SetTextColor(1, 0.82, 0);
end
end
-- Indicate whether it's a multiselect control.
function ListBox:SetMultiselect(multi)
self.multi = multi;
end
-- Set (and show/hide) the widget label.
function ListBox:SetLabel(text)
if (text or "") == "" then
self.frame:SetHeight(196);
self.box:SetPoint("TOPLEFT", self.frame, "TOPLEFT", 0, 0);
self.label:Hide();
self.label:SetText("");
else
self.frame:SetHeight(216);
self.box:SetPoint("TOPLEFT", self.frame, "TOPLEFT", 0, -20);
self.label:Show();
self.label:SetText(text);
end
end
-- Set the contents of the widget. Must be a integer-indexed list.
function ListBox:SetList(list)
self.list = list;
self.values = {};
ListBox_Update(self.box);
end
-- Unused, since only multiselect is supported for now.
function ListBox:SetValue(value)
assert(false);
end
-- Activate/deactivate an item.
function ListBox:SetItemValue(key, value)
if not value then
value = nil;
end
self.values[key] = value;
ListBox_Update(self.box);
end
-- Build a new widget. We use OptionsFrameListTemplate as provided by
-- Blizzard, with a few nefarious tweaks.
local function Constructor()
local num = AceGUI:GetNextWidgetNum(Type);
local frame = CreateFrame("Frame", "AceGUI30ListBox" .. num, UIParent);
local self = {};
for k, v in pairs(ListBox) do self[k] = v end;
self.num = num;
self.type = Type;
self.frame = frame;
frame.obj = self;
frame:SetHeight(216);
local box = CreateFrame("Frame", "AceGUI30ListBoxBox" .. num, frame,
"OptionsFrameListTemplate");
box.update = ListBox_Update;
box:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -20);
box:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, 0);
box:SetHeight(196);
local buttons = box.buttons;
local maxButtons = math.floor((box:GetHeight() - 8) / box.buttonHeight);
while #buttons < maxButtons do
local index = #buttons + 1;
local button = CreateFrame("Button", box:GetName() .. "Button" .. index,
box, "OptionsListButtonTemplate");
button:SetPoint("TOPLEFT", buttons[#buttons], "BOTTOMLEFT");
tinsert(buttons, button);
end
local button;
for _, button in ipairs(buttons) do
button.text:SetText("Button");
button:SetPoint("RIGHT", button:GetParent(), "RIGHT");
button:SetScript("OnClick", ListButton_OnClick);
end
box.buttons = buttons;
self.box = box;
box.obj = self;
local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall");
label:SetPoint("TOPLEFT", frame, "TOPLEFT", 0, -2);
label:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 0, -2);
label:SetJustifyH("LEFT");
label:SetHeight(18);
self.label = label;
label.obj = self;
AceGUI:RegisterAsWidget(self);
return self;
end
AceGUI:RegisterWidgetType(Type, Constructor, Version);