vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Auctioneer Addon for World of Warcraft(tm).
3 Version: 3.9.0.1000 (Kangaroo)
4 Revision: $Id: ListTemplate.lua 958 2006-08-16 04:21:17Z mentalpower $
5  
6 List Frame Template
7  
8 License:
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13  
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18  
19 You should have received a copy of the GNU General Public License
20 along with this program(see GPL.txt); if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 ]]
23  
24 local MAX_COLUMNS = 6;
25  
26 -------------------------------------------------------------------------------
27 -- Compare two rows
28 -------------------------------------------------------------------------------
29 local CurrentListFrame = nil;
30 function ListTemplate_CompareRows(row1, row2)
31 frame = CurrentListFrame;
32 for index, value in ipairs(frame.sortOrder) do
33 local column = value;
34 local physicalColumn = frame.physicalColumns[column.columnIndex];
35 local logicalColumn = physicalColumn.logicalColumn;
36 if (logicalColumn.compareAscendingFunc and logicalColumn.compareDescendingFunc) then
37 local ascending = logicalColumn.compareAscendingFunc(row1, row2);
38 local descending = logicalColumn.compareDescendingFunc(row1, row2);
39 if (ascending or descending) then
40 if (column.sortAscending) then
41 return ascending;
42 else
43 return descending;
44 end
45 end
46 end
47 end
48 return false;
49 end
50  
51 -------------------------------------------------------------------------------
52 -- Initialize the list with the column information
53 -------------------------------------------------------------------------------
54 function ListTemplate_Initialize(frame, physicalColumns, logicalColumns)
55 frame.lines = 19;
56 frame.lineHeight = 16;
57 frame.content = {};
58 frame.physicalColumns = physicalColumns;
59 frame.logicalColumns = logicalColumns;
60  
61 frame.sortOrder = {};
62 for physicalColumnIndex = 1, MAX_COLUMNS do
63 local button = getglobal(frame:GetName().."Column"..physicalColumnIndex.."Sort");
64 local dropdown = getglobal(frame:GetName().."Column"..physicalColumnIndex.."DropDown");
65 if (physicalColumnIndex <= table.getn(physicalColumns)) then
66 local physicalColumn = physicalColumns[physicalColumnIndex];
67 local logicalColumn = physicalColumn.logicalColumn
68 local column = {};
69 column.columnIndex = physicalColumnIndex;
70 column.sortAscending = true;
71 table.insert(frame.sortOrder, column);
72 getglobal(button:GetName().."Arrow"):Hide();
73 getglobal(button:GetName().."Text"):SetText(logicalColumn.title);
74 button:Show();
75 if (table.getn(physicalColumn.logicalColumns) > 1) then
76 dropdown:Show();
77 else
78 dropdown:Hide();
79 end
80 ListTemplate_SetColumnWidth(frame, physicalColumnIndex, physicalColumn.width);
81 else
82 button:Hide();
83 dropdown:Hide();
84 end
85 end
86 getglobal(frame:GetName().."Column1SortArrow"):Show();
87 getglobal(frame:GetName().."Column1SortArrow"):SetTexCoord(0, 0.5625, 0, 1.0);
88 end
89  
90 -------------------------------------------------------------------------------
91 -- Initialize the list with the column information
92 -------------------------------------------------------------------------------
93 function ListTemplate_SetColumnWidth(frame, column, width)
94 -- Resize the header
95 frame.physicalColumns[column].width = width;
96 local button = getglobal(frame:GetName().."Column"..column.."Sort");
97 button:SetWidth(width + 2);
98 local dropdown = getglobal(frame:GetName().."Column"..column.."DropDown");
99 UIDropDownMenu_SetWidth(width - 18, dropdown);
100  
101 -- Resize each cell in the columns
102 for line = 1, frame.lines do
103 local textControl = getglobal(frame:GetName().."Item"..line.."Column"..column);
104 if (textControl) then
105 textControl:SetWidth(width - 20);
106 end
107 end
108 end
109  
110 -------------------------------------------------------------------------------
111 -- Set the item to display.
112 -------------------------------------------------------------------------------
113 function ListTemplate_SetContent(frame, content)
114 frame.content = content;
115  
116 -- Perform the sort.
117 CurrentListFrame = frame;
118 table.sort(frame.content, ListTemplate_CompareRows);
119 CurrentListFrame = nil;
120  
121 -- Update the scroll pane.
122 ListTemplateScrollFrame_Update(getglobal(frame:GetName().."ScrollFrame"));
123 end
124  
125 -------------------------------------------------------------------------------
126 -------------------------------------------------------------------------------
127 function ListTemplate_SelectRow(frame, row)
128 if (frame.selectedRow ~= row) then
129 local scrollFrame = getglobal(frame:GetName().."ScrollFrame");
130 local firstVisibleRow = FauxScrollFrame_GetOffset(scrollFrame) + 1;
131 local lastVisibleRow = firstVisibleRow + frame.lines - 1;
132  
133 -- Deselect the previous row
134 if (frame.selectedRow and firstVisibleRow <= frame.selectedRow and frame.selectedRow <= lastVisibleRow) then
135 local line = frame.selectedRow - firstVisibleRow + 1;
136 local item = getglobal(frame:GetName().."Item"..line);
137 item:UnlockHighlight();
138 end
139  
140 -- Update the selected item
141 frame.selectedRow = row;
142  
143 -- Select the new row
144 if (frame.selectedRow and firstVisibleRow <= frame.selectedRow and frame.selectedRow <= lastVisibleRow) then
145 local line = frame.selectedRow - firstVisibleRow + 1;
146 local item = getglobal(frame:GetName().."Item"..line);
147 item:LockHighlight();
148 end
149 end
150 end
151  
152 -------------------------------------------------------------------------------
153 -------------------------------------------------------------------------------
154 function ListTemplate_Sort(frame, columnIndex)
155 -- Update the SortColumn order
156 if (frame.sortOrder[1].columnIndex == columnIndex) then
157 frame.sortOrder[1].sortAscending = not frame.sortOrder[1].sortAscending;
158 else
159 getglobal(frame:GetName().."Column"..frame.sortOrder[1].columnIndex.."SortArrow"):Hide();
160 for index, value in ipairs(frame.sortOrder) do
161 if (value.columnIndex == columnIndex) then
162 local temp = value;
163 table.remove(frame.sortOrder, index);
164 table.insert(frame.sortOrder, 1, temp);
165 break;
166 end
167 end
168 frame.sortOrder[1].sortAscending = true;
169 getglobal(frame:GetName().."Column"..frame.sortOrder[1].columnIndex.."SortArrow"):Show();
170 end
171  
172 -- Perform the sort.
173 CurrentListFrame = frame;
174 table.sort(frame.content, ListTemplate_CompareRows);
175 CurrentListFrame = nil;
176  
177 -- Update the sort arrow.
178 if (frame.sortOrder[1].sortAscending) then
179 getglobal(frame:GetName().."Column"..frame.sortOrder[1].columnIndex.."SortArrow"):SetTexCoord(0, 0.5625, 0, 1.0);
180 else
181 getglobal(frame:GetName().."Column"..frame.sortOrder[1].columnIndex.."SortArrow"):SetTexCoord(0, 0.5625, 1.0, 0);
182 end
183  
184 -- Update the scroll pane.
185 ListTemplateScrollFrame_Update(getglobal(frame:GetName().."ScrollFrame"));
186 end
187  
188 -------------------------------------------------------------------------------
189 -------------------------------------------------------------------------------
190 function ListTemplateScrollFrame_Update(frame)
191 if (not frame) then frame = this end;
192 local parent = frame:GetParent();
193 local content = parent.content;
194 FauxScrollFrame_Update(frame, table.getn(content), parent.lines, parent.lineHeight);
195 for line = 1, parent.lines do
196 local item = getglobal(parent:GetName().."Item"..line);
197 local contentIndex = line + FauxScrollFrame_GetOffset(frame);
198 if contentIndex <= table.getn(content) then
199 for columnIndex = 1, MAX_COLUMNS do
200 -- Get the text control (if any)
201 local text = getglobal(parent:GetName().."Item"..line.."Column"..columnIndex);
202 if (text) then
203 text:Hide();
204 end
205  
206 -- Get the money frame (if any)
207 local moneyFrame = getglobal(parent:GetName().."Item"..line.."Column"..columnIndex.."MoneyFrame");
208 if (moneyFrame) then
209 moneyFrame:Hide();
210 end
211  
212 -- If the column exists, update it
213 if (columnIndex <= table.getn(parent.physicalColumns)) then
214 local physicalColumn = parent.physicalColumns[columnIndex];
215 local logicalColumn = physicalColumn.logicalColumn;
216 local value = logicalColumn.valueFunc(content[contentIndex]);
217  
218 -- Setup the control based on the datatype
219 if (value) then
220 if (text and (logicalColumn.dataType == "Date" or logicalColumn.dataType == "Number" or logicalColumn.dataType == "String")) then
221 text:SetText(value);
222 if (logicalColumn.colorFunc) then
223 local color = logicalColumn.colorFunc(content[contentIndex]);
224 text:SetTextColor(color.r, color.g, color.b);
225 else
226 text:SetTextColor(255, 255, 255);
227 end
228 if (logicalColumn.alphaFunc) then
229 text:SetAlpha(logicalColumn.alphaFunc(content[contentIndex]));
230 else
231 text:SetAlpha(1.0);
232 end
233 if (logicalColumn.dataType == "Number") then
234 text:SetJustifyH("RIGHT");
235 else
236 text:SetJustifyH("LEFT");
237 end
238 text:Show();
239 elseif (moneyFrame and logicalColumn.dataType == "Money") then
240 if (value >= 0) then
241 MoneyFrame_Update(moneyFrame:GetName(), value);
242 SetMoneyFrameColor(moneyFrame:GetName(), 255, 255, 255);
243 else
244 MoneyFrame_Update(moneyFrame:GetName(), -value);
245 SetMoneyFrameColor(moneyFrame:GetName(), 255, 0, 0);
246 end
247 if (logicalColumn.alphaFunc) then
248 moneyFrame:SetAlpha(logicalColumn.alphaFunc(content[contentIndex]));
249 else
250 moneyFrame:SetAlpha(1.0);
251 end
252 moneyFrame:Show();
253 end
254 end
255 end
256 end
257  
258 -- Update the row highlight
259 if (parent.selectedRow and parent.selectedRow == contentIndex) then
260 item:LockHighlight();
261 else
262 item:UnlockHighlight();
263 end
264 item:Show();
265 else
266 item:Hide();
267 end
268 end
269 end
270  
271 -------------------------------------------------------------------------------
272 -------------------------------------------------------------------------------
273 function ListTemplate_DropDown_OnLoad()
274 getglobal(this:GetName().."Text"):Hide();
275 this.initialize = ListTemplate_DropDown_Initialize;
276 UIDropDownMenu_SetSelectedID(this, 1);
277 end
278  
279 -------------------------------------------------------------------------------
280 -------------------------------------------------------------------------------
281 function ListTemplate_DropDown_Initialize()
282 local dropdown = this:GetParent();
283 local frame = dropdown:GetParent();
284 if (frame.physicalColumns) then
285 local physicalColumnIndex = dropdown:GetID();
286 local physicalColumn = frame.physicalColumns[physicalColumnIndex];
287 for index, value in pairs(physicalColumn.logicalColumns) do
288 local logicalColumn = value;
289 local info = {};
290 info.text = logicalColumn.title;
291 info.func = ListTemplate_DropDownItem_OnClick;
292 info.owner = dropdown;
293 UIDropDownMenu_AddButton(info);
294 end
295 end
296 end
297  
298 -------------------------------------------------------------------------------
299 -------------------------------------------------------------------------------
300 function ListTemplate_DropDownItem_OnClick()
301 local logicalColumnIndex = this:GetID();
302 local physicalColumnIndex = this.owner:GetID();
303 local dropdown = this.owner;
304 local frame = dropdown:GetParent();
305 if (frame.physicalColumns[physicalColumnIndex].logicalColumn ~= frame.physicalColumns[physicalColumnIndex].logicalColumns[logicalColumnIndex]) then
306 -- Change the physical column's logical column
307 frame.physicalColumns[physicalColumnIndex].logicalColumn = frame.physicalColumns[physicalColumnIndex].logicalColumns[logicalColumnIndex];
308 getglobal(frame:GetName().."Column"..physicalColumnIndex.."SortText"):SetText(frame.physicalColumns[physicalColumnIndex].logicalColumn.title);
309  
310 -- Sort the content based on the new logical column
311 CurrentListFrame = frame;
312 table.sort(frame.content, ListTemplate_CompareRows);
313 CurrentListFrame = nil;
314  
315 -- Update the combo and scroll pane.
316 UIDropDownMenu_SetSelectedID(dropdown, logicalColumnIndex);
317 ListTemplateScrollFrame_Update(getglobal(frame:GetName().."ScrollFrame"));
318 end
319 end
320  
321