InterfaceOptionsFrame.lua

InterfaceOptionsFrame.lua (8.3.7.35662; unchanged since 8.2.0.30920)
  1. INTERFACEOPTIONS_ADDONCATEGORIES = {};
  2. local blizzardCategories = {};
  3. local next = next;
  4. local function SecureNext(elements, key)
  5. return securecall(next, elements, key);
  6. end
  7. local tinsert = tinsert;
  8. local strlower = strlower;
  9. -- [[ InterfaceOptionsList functions ]] --
  10. function InterfaceOptionsList_DisplayPanel (frame)
  11. if ( InterfaceOptionsFramePanelContainer.displayedPanel ) then
  12. InterfaceOptionsFramePanelContainer.displayedPanel:Hide();
  13. end
  14. InterfaceOptionsFramePanelContainer.displayedPanel = frame;
  15. frame:SetParent(InterfaceOptionsFramePanelContainer);
  16. frame:ClearAllPoints();
  17. frame:SetPoint("TOPLEFT", InterfaceOptionsFramePanelContainer, "TOPLEFT");
  18. frame:SetPoint("BOTTOMRIGHT", InterfaceOptionsFramePanelContainer, "BOTTOMRIGHT");
  19. frame:Show();
  20. end
  21. function InterfaceOptionsListButton_OnClick (self, mouseButton)
  22. if ( mouseButton == "RightButton" ) then
  23. if ( self.element.hasChildren ) then
  24. OptionsListButtonToggle_OnClick(self.toggle);
  25. end
  26. return;
  27. end
  28. local parent = self:GetParent();
  29. local buttons = parent.buttons;
  30. OptionsList_ClearSelection(InterfaceOptionsFrameCategories, InterfaceOptionsFrameCategories.buttons);
  31. OptionsList_ClearSelection(InterfaceOptionsFrameAddOns, InterfaceOptionsFrameAddOns.buttons);
  32. OptionsList_SelectButton(parent, self);
  33. InterfaceOptionsList_DisplayPanel(self.element);
  34. end
  35. function InterfaceOptionsListButton_ToggleSubCategories (self)
  36. local element = self.element;
  37. element.collapsed = not element.collapsed;
  38. local collapsed = element.collapsed;
  39. for _, category in SecureNext, blizzardCategories do
  40. if ( category.parent == element.name ) then
  41. if ( collapsed ) then
  42. category.hidden = true;
  43. else
  44. category.hidden = false;
  45. end
  46. end
  47. end
  48. for _, category in SecureNext, INTERFACEOPTIONS_ADDONCATEGORIES do
  49. if ( category.parent == element.name ) then
  50. if ( collapsed ) then
  51. category.hidden = true;
  52. else
  53. category.hidden = false;
  54. end
  55. end
  56. end
  57. InterfaceCategoryList_Update();
  58. InterfaceAddOnsList_Update();
  59. end
  60. --Table to reuse! Yay reuse!
  61. local displayedElements = {}
  62. function InterfaceCategoryList_Update ()
  63. --Redraw the scroll lists
  64. local offset = FauxScrollFrame_GetOffset(InterfaceOptionsFrameCategoriesList);
  65. local buttons = InterfaceOptionsFrameCategories.buttons;
  66. local element;
  67. for i, element in SecureNext, displayedElements do
  68. displayedElements[i] = nil;
  69. end
  70. for i, element in SecureNext, blizzardCategories do
  71. if ( not element.hidden ) then
  72. tinsert(displayedElements, element);
  73. end
  74. end
  75. local numButtons = #buttons;
  76. local numCategories = #displayedElements;
  77. if ( numCategories > numButtons and ( not InterfaceOptionsFrameCategoriesList:IsShown() ) ) then
  78. OptionsList_DisplayScrollBar(InterfaceOptionsFrameCategories);
  79. elseif ( numCategories <= numButtons and ( InterfaceOptionsFrameCategoriesList:IsShown() ) ) then
  80. OptionsList_HideScrollBar(InterfaceOptionsFrameCategories);
  81. end
  82. FauxScrollFrame_Update(InterfaceOptionsFrameCategoriesList, numCategories, numButtons, buttons[1]:GetHeight());
  83. local selection = InterfaceOptionsFrameCategories.selection;
  84. if ( selection ) then
  85. -- Store the currently selected element and clear all the buttons, we're redrawing.
  86. OptionsList_ClearSelection(InterfaceOptionsFrameCategories, InterfaceOptionsFrameCategories.buttons);
  87. end
  88. for i = 1, numButtons do
  89. element = displayedElements[i + offset];
  90. if ( not element ) then
  91. OptionsList_HideButton(buttons[i]);
  92. else
  93. OptionsList_DisplayButton(buttons[i], element);
  94. if ( selection ) and ( selection == element ) and ( not InterfaceOptionsFrameCategories.selection ) then
  95. OptionsList_SelectButton(InterfaceOptionsFrameCategories, buttons[i]);
  96. end
  97. end
  98. end
  99. if ( selection ) then
  100. -- If there was a selected element before we cleared the button highlights, restore it, 'cause we're done.
  101. -- Note: This theoretically might already have been done by OptionsList_SelectButton, but in the event that the selected button hasn't been drawn, this is still necessary.
  102. InterfaceOptionsFrameCategories.selection = selection;
  103. end
  104. end
  105. function InterfaceAddOnsList_Update ()
  106. -- Might want to merge this into InterfaceCategoryList_Update depending on whether or not things get differentiated.
  107. local offset = FauxScrollFrame_GetOffset(InterfaceOptionsFrameAddOnsList);
  108. local buttons = InterfaceOptionsFrameAddOns.buttons;
  109. local element;
  110. for i, element in SecureNext, displayedElements do
  111. displayedElements[i] = nil;
  112. end
  113. for i, element in SecureNext, INTERFACEOPTIONS_ADDONCATEGORIES do
  114. if ( not element.hidden ) then
  115. tinsert(displayedElements, element);
  116. end
  117. end
  118. local numAddOnCategories = #displayedElements;
  119. local numButtons = #buttons;
  120. -- Show the AddOns tab if it's not empty.
  121. local showTabs = numAddOnCategories > 0;
  122. InterfaceOptionsFrameTab1:SetShown(showTabs);
  123. InterfaceOptionsFrameTab2:SetShown(showTabs);
  124. if ( numAddOnCategories > numButtons and ( not InterfaceOptionsFrameAddOnsList:IsShown() ) ) then
  125. -- We need to show the scroll bar, we have more elements than buttons.
  126. OptionsList_DisplayScrollBar(InterfaceOptionsFrameAddOns);
  127. elseif ( numAddOnCategories <= numButtons and ( InterfaceOptionsFrameAddOnsList:IsShown() ) ) then
  128. -- Hide the scrollbar, there's nothing to scroll.
  129. OptionsList_HideScrollBar(InterfaceOptionsFrameAddOns);
  130. end
  131. FauxScrollFrame_Update(InterfaceOptionsFrameAddOnsList, numAddOnCategories, numButtons, buttons[1]:GetHeight());
  132. local selection = InterfaceOptionsFrameAddOns.selection;
  133. if ( selection ) then
  134. OptionsList_ClearSelection(InterfaceOptionsFrameAddOns, InterfaceOptionsFrameAddOns.buttons);
  135. end
  136. for i = 1, #buttons do
  137. element = displayedElements[i + offset]
  138. if ( not element ) then
  139. OptionsList_HideButton(buttons[i]);
  140. else
  141. OptionsList_DisplayButton(buttons[i], element);
  142. if ( selection ) and ( selection == element ) and ( not InterfaceOptionsFrameAddOns.selection ) then
  143. OptionsList_SelectButton(InterfaceOptionsFrameAddOns, buttons[i]);
  144. end
  145. end
  146. end
  147. if ( selection ) then
  148. InterfaceOptionsFrameAddOns.selection = selection;
  149. end
  150. end
  151. -- [[ InterfaceOptionsFrame ]] --
  152. function InterfaceOptionsFrame_Show ()
  153. if ( InterfaceOptionsFrame:IsShown() ) then
  154. InterfaceOptionsFrame:Hide();
  155. else
  156. InterfaceOptionsFrame:Show();
  157. end
  158. end
  159. local function InterfaceOptionsFrame_RunOkayForCategory (category)
  160. pcall(category.okay, category);
  161. end
  162. local function InterfaceOptionsFrame_RunDefaultForCategory (category)
  163. pcall(category.default, category);
  164. end
  165. local function InterfaceOptionsFrame_RunCancelForCategory (category)
  166. pcall(category.cancel, category);
  167. end
  168. local function InterfaceOptionsFrame_RunRefreshForCategory (category)
  169. pcall(category.refresh, category);
  170. end
  171. function InterfaceOptionsFrameOkay_OnClick (self, button, apply)
  172. --Iterate through registered panels and run their okay methods in a taint-safe fashion
  173. for _, category in SecureNext, blizzardCategories do
  174. securecall(InterfaceOptionsFrame_RunOkayForCategory, category);
  175. end
  176. for _, category in SecureNext, INTERFACEOPTIONS_ADDONCATEGORIES do
  177. securecall(InterfaceOptionsFrame_RunOkayForCategory, category);
  178. end
  179. if ( InterfaceOptionsFrame.gameRestart ) then
  180. StaticPopup_Show("CLIENT_RESTART_ALERT");
  181. InterfaceOptionsFrame.gameRestart = nil;
  182. elseif ( InterfaceOptionsFrame.logout ) then
  183. StaticPopup_Show("CLIENT_LOGOUT_ALERT");
  184. InterfaceOptionsFrame.logout = nil;
  185. end
  186. if ( not apply ) then
  187. InterfaceOptionsFrame_Show();
  188. end
  189. end
  190. function InterfaceOptionsFrameCancel_OnClick (self, button)
  191. --Iterate through registered panels and run their cancel methods in a taint-safe fashion
  192. for _, category in SecureNext, blizzardCategories do
  193. securecall(InterfaceOptionsFrame_RunCancelForCategory, category);
  194. end
  195. for _, category in SecureNext, INTERFACEOPTIONS_ADDONCATEGORIES do
  196. securecall(InterfaceOptionsFrame_RunCancelForCategory, category);
  197. end
  198. InterfaceOptionsFrame.gameRestart = nil;
  199. InterfaceOptionsFrame.logout = nil;
  200. InterfaceOptionsFrame_Show();
  201. end
  202. function InterfaceOptionsFrameDefaults_OnClick (self, button)
  203. StaticPopup_Show("CONFIRM_RESET_INTERFACE_SETTINGS");
  204. end
  205. function InterfaceOptionsSocialPanelRedockChat_OnClick ()
  206. FCF_RedockAllWindows();
  207. end
  208. function InterfaceOptionsFrame_SetAllToDefaults ()
  209. --Iterate through registered panels and run their default methods in a taint-safe fashion
  210. for _, category in SecureNext, blizzardCategories do
  211. securecall(InterfaceOptionsFrame_RunDefaultForCategory, category);
  212. end
  213. for _, category in SecureNext, INTERFACEOPTIONS_ADDONCATEGORIES do
  214. securecall(InterfaceOptionsFrame_RunDefaultForCategory, category);
  215. end
  216. --Refresh the categories to pick up changes made.
  217. InterfaceOptionsOptionsFrame_RefreshCategories();
  218. InterfaceOptionsOptionsFrame_RefreshAddOns();
  219. end
  220. function InterfaceOptionsFrame_SetCurrentToDefaults ()
  221. local displayedPanel = InterfaceOptionsFramePanelContainer.displayedPanel;
  222. if ( not displayedPanel or not displayedPanel.default ) then
  223. return;
  224. end
  225. displayedPanel.default(displayedPanel);
  226. --Run the refresh method to refresh any values that were changed.
  227. displayedPanel.refresh(displayedPanel);
  228. end
  229. function InterfaceOptionsOptionsFrame_RefreshCategories ()
  230. for _, category in SecureNext, blizzardCategories do
  231. securecall(InterfaceOptionsFrame_RunRefreshForCategory, category);
  232. end
  233. end
  234. function InterfaceOptionsOptionsFrame_RefreshAddOns ()
  235. for _, category in SecureNext, INTERFACEOPTIONS_ADDONCATEGORIES do
  236. securecall(InterfaceOptionsFrame_RunRefreshForCategory, category);
  237. end
  238. end
  239. local UVARINFO = {
  240. ["REMOVE_CHAT_DELAY"] = { default = "0", cvar = "removeChatDelay", event = "REMOVE_CHAT_DELAY_TEXT" },
  241. ["LOCK_ACTIONBAR"] = { default = "0", cvar = "lockActionBars", event = "LOCK_ACTIONBAR_TEXT" },
  242. ["SHOW_BUFF_DURATIONS"] = { default = "1", cvar = "buffDurations", event = "SHOW_BUFF_DURATION_TEXT", func = function () SHOW_BUFF_DURATIONS = GetCVar("buffDurations"); BuffFrame_UpdatePositions(); end},
  243. ["ALWAYS_SHOW_MULTIBARS"] = { default = "0", cvar = "alwaysShowActionBars", event = "ALWAYS_SHOW_MULTIBARS_TEXT" },
  244. ["SHOW_PARTY_PETS"] = { default = "0", cvar = "showPartyPets", event = "SHOW_PARTY_PETS_TEXT" },
  245. ["SHOW_PARTY_BACKGROUND"] = { default = "0", cvar = "showPartyBackground", event = "SHOW_PARTY_BACKGROUND_TEXT" },
  246. ["SHOW_TARGET_OF_TARGET"] = { default = "0", cvar = "showTargetOfTarget", event = "SHOW_TARGET_OF_TARGET_TEXT" },
  247. ["AUTO_QUEST_WATCH"] = { default = "1", cvar = "autoQuestWatch", event = "AUTO_QUEST_WATCH_TEXT" },
  248. ["LOOT_UNDER_MOUSE"] = { default = "0", cvar = "lootUnderMouse", event = "LOOT_UNDER_MOUSE_TEXT" },
  249. ["AUTO_LOOT_DEFAULT"] = { default = "0", cvar = "autoLootDefault", event = "AUTO_LOOT_DEFAULT_TEXT" },
  250. ["SHOW_COMBAT_TEXT"] = { default = "1", cvar = "enableFloatingCombatText", event = "SHOW_COMBAT_TEXT_TEXT" },
  251. ["COMBAT_TEXT_SHOW_LOW_HEALTH_MANA"] = { default = "1", cvar = "floatingCombatTextLowManaHealth", event = "COMBAT_TEXT_SHOW_LOW_HEALTH_MANA_TEXT" },
  252. ["COMBAT_TEXT_SHOW_AURAS"] = { default = "0", cvar = "floatingCombatTextAuras", event = "COMBAT_TEXT_SHOW_AURAS_TEXT" },
  253. ["COMBAT_TEXT_SHOW_AURA_FADE"] = { default = "0", cvar = "floatingCombatTextAuras", event = "COMBAT_TEXT_SHOW_AURAS_TEXT" },
  254. ["COMBAT_TEXT_SHOW_COMBAT_STATE"] = { default = "0", cvar = "floatingCombatTextCombatState", event = "COMBAT_TEXT_SHOW_COMBAT_STATE_TEXT" },
  255. ["COMBAT_TEXT_SHOW_DODGE_PARRY_MISS"] = { default = "0", cvar = "floatingCombatTextDodgeParryMiss", event = "COMBAT_TEXT_SHOW_DODGE_PARRY_MISS_TEXT" },
  256. ["COMBAT_TEXT_SHOW_RESISTANCES"] = { default = "0", cvar = "floatingCombatTextDamageReduction", event = "COMBAT_TEXT_SHOW_RESISTANCES_TEXT" },
  257. ["COMBAT_TEXT_SHOW_REPUTATION"] = { default = "1", cvar = "floatingCombatTextRepChanges", event = "COMBAT_TEXT_SHOW_REPUTATION_TEXT" },
  258. ["COMBAT_TEXT_SHOW_REACTIVES"] = { default = "0", cvar = "floatingCombatTextReactives", event = "COMBAT_TEXT_SHOW_REACTIVES_TEXT" },
  259. ["COMBAT_TEXT_SHOW_FRIENDLY_NAMES"] = { default = "0", cvar = "floatingCombatTextFriendlyHealers", event = "COMBAT_TEXT_SHOW_FRIENDLY_NAMES_TEXT" },
  260. ["COMBAT_TEXT_SHOW_COMBO_POINTS"] = { default = "0", cvar = "floatingCombatTextComboPoints", event = "COMBAT_TEXT_SHOW_COMBO_POINTS_TEXT" },
  261. ["COMBAT_TEXT_SHOW_ENERGIZE"] = { default = "0", cvar = "floatingCombatTextEnergyGains", event = "COMBAT_TEXT_SHOW_ENERGIZE_TEXT" },
  262. ["COMBAT_TEXT_SHOW_PERIODIC_ENERGIZE"] = { default = "0", cvar = "floatingCombatTextPeriodicEnergyGains", event = "COMBAT_TEXT_SHOW_PERIODIC_ENERGIZE_TEXT" },
  263. ["COMBAT_TEXT_FLOAT_MODE"] = { default = "1", cvar = "floatingCombatTextFloatMode", event = "COMBAT_TEXT_FLOAT_MODE" },
  264. ["COMBAT_TEXT_SHOW_HONOR_GAINED"] = { default = "0", cvar = "floatingCombatTextHonorGains", event = "COMBAT_TEXT_SHOW_HONOR_GAINED_TEXT" },
  265. ["ALWAYS_SHOW_MULTIBARS"] = { default = "0", cvar = "alwaysShowActionBars", },
  266. ["SHOW_CASTABLE_BUFFS"] = { default = "0", cvar = "showCastableBuffs", event = "SHOW_CASTABLE_BUFFS_TEXT" },
  267. ["SHOW_DISPELLABLE_DEBUFFS"] = { default = "1", cvar = "showDispelDebuffs", event = "SHOW_DISPELLABLE_DEBUFFS_TEXT" },
  268. ["SHOW_ARENA_ENEMY_FRAMES"] = { default = "1", cvar = "showArenaEnemyFrames", event = "SHOW_ARENA_ENEMY_FRAMES_TEXT" },
  269. ["SHOW_ARENA_ENEMY_CASTBAR"] = { default = "1", cvar = "showArenaEnemyCastbar", event = "SHOW_ARENA_ENEMY_CASTBAR_TEXT" },
  270. ["SHOW_ARENA_ENEMY_PETS"] = { default = "1", cvar = "showArenaEnemyPets", event = "SHOW_ARENA_ENEMY_PETS_TEXT" },
  271. }
  272. function InterfaceOptionsFrame_InitializeUVars ()
  273. -- Setup UVars that keep settings
  274. for uvar, setting in SecureNext, UVARINFO do
  275. _G[uvar] = setting.default;
  276. end
  277. end
  278. function InterfaceOptionsFrame_LoadUVars ()
  279. local variable, cvarValue
  280. for uvar, setting in SecureNext, UVARINFO do
  281. variable = _G[uvar];
  282. cvarValue = GetCVar(setting.cvar);
  283. if ( cvarValue == setting.default and variable ~= setting.default ) then
  284. SetCVar(setting.cvar, variable, setting.event)
  285. if ( setting.func ) then
  286. setting.func()
  287. end
  288. elseif ( cvarValue ~= setting.default or ( not ( _G[uvar] ) ) ) then
  289. _G[uvar] = cvarValue;
  290. if ( setting.func ) then
  291. setting.func()
  292. end
  293. end
  294. end
  295. end
  296. function InterfaceOptionsFrame_OnLoad (self)
  297. --Make sure all the UVars get their default values set, since systems that require them to be defined will be loaded before anything in UIOptionsPanels
  298. self:RegisterEvent("VARIABLES_LOADED");
  299. InterfaceOptionsFrame_InitializeUVars();
  300. PanelTemplates_SetNumTabs(self, 2);
  301. InterfaceOptionsFrame.selectedTab = 1;
  302. PanelTemplates_UpdateTabs(self);
  303. end
  304. function InterfaceOptionsFrame_OnEvent (self, event, ...)
  305. if ( event == "VARIABLES_LOADED" ) then
  306. InterfaceOptionsFrame_LoadUVars();
  307. end
  308. end
  309. function InterfaceOptionsFrame_OnShow (self)
  310. --Refresh the two category lists and display the "Controls" group of options if nothing is selected.
  311. InterfaceCategoryList_Update();
  312. InterfaceOptionsOptionsFrame_RefreshCategories();
  313. InterfaceAddOnsList_Update();
  314. if ( not InterfaceOptionsFramePanelContainer.displayedPanel ) then
  315. InterfaceOptionsFrame_OpenToCategory(CONTROLS_LABEL);
  316. end
  317. --Refresh the categories to pick up changes made while the options frame was hidden.
  318. InterfaceOptionsOptionsFrame_RefreshAddOns();
  319. end
  320. function InterfaceOptionsFrame_OnHide (self)
  321. OptionsFrame_OnHide(InterfaceOptionsFrame);
  322. if ( InterfaceOptionsFrame.gameRestart ) then
  323. StaticPopup_Show("CLIENT_RESTART_ALERT");
  324. InterfaceOptionsFrame.gameRestart = nil;
  325. elseif ( InterfaceOptionsFrame.logout ) then
  326. StaticPopup_Show("CLIENT_LOGOUT_ALERT");
  327. InterfaceOptionsFrame.logout = nil;
  328. end
  329. end
  330. function InterfaceOptionsFrame_TabOnClick ()
  331. if ( InterfaceOptionsFrame.selectedTab == 1 ) then
  332. InterfaceOptionsFrameCategories:Show();
  333. InterfaceOptionsFrameAddOns:Hide();
  334. else
  335. InterfaceOptionsFrameCategories:Hide();
  336. InterfaceOptionsFrameAddOns:Show();
  337. end
  338. end
  339. function InterfaceOptionsFrame_OpenToCategory (panel)
  340. local panelName;
  341. if ( type(panel) == "string" ) then
  342. panelName = panel;
  343. panel = nil;
  344. end
  345. assert(panelName or panel, 'Usage: InterfaceOptionsFrame_OpenToCategory("categoryName" or panel)');
  346. local blizzardElement, elementToDisplay
  347. for i, element in SecureNext, blizzardCategories do
  348. if ( element == panel or (panelName and element.name and element.name == panelName) ) then
  349. elementToDisplay = element;
  350. blizzardElement = true;
  351. break;
  352. end
  353. end
  354. if ( not elementToDisplay ) then
  355. for i, element in SecureNext, INTERFACEOPTIONS_ADDONCATEGORIES do
  356. if ( element == panel or (panelName and element.name and element.name == panelName) ) then
  357. elementToDisplay = element;
  358. break;
  359. end
  360. end
  361. end
  362. if ( not elementToDisplay ) then
  363. return;
  364. end
  365. if ( blizzardElement ) then
  366. InterfaceOptionsFrameTab1:Click();
  367. local buttons = InterfaceOptionsFrameCategories.buttons;
  368. for i, button in SecureNext, buttons do
  369. if ( button.element == elementToDisplay ) then
  370. InterfaceOptionsListButton_OnClick(button);
  371. elseif ( elementToDisplay.parent and button.element and (button.element.name == elementToDisplay.parent and button.element.collapsed) ) then
  372. OptionsListButtonToggle_OnClick(button.toggle);
  373. end
  374. end
  375. if ( not InterfaceOptionsFrame:IsShown() ) then
  376. InterfaceOptionsFrame_Show();
  377. end
  378. else
  379. InterfaceOptionsFrameTab2:Click();
  380. local buttons = InterfaceOptionsFrameAddOns.buttons;
  381. for i, button in SecureNext, buttons do
  382. if ( button.element == elementToDisplay ) then
  383. InterfaceOptionsListButton_OnClick(button);
  384. elseif ( elementToDisplay.parent and button.element and (button.element.name == elementToDisplay.parent and button.element.collapsed) ) then
  385. OptionsListButtonToggle_OnClick(button.toggle);
  386. end
  387. end
  388. if ( not InterfaceOptionsFrame:IsShown() ) then
  389. InterfaceOptionsFrame_Show();
  390. end
  391. end
  392. end
  393. ---------------------------------------------------------------------------------------------------
  394. -- HOWTO: Add new categories of options
  395. --
  396. -- The new Interface Options frame allows authors to place their configuration
  397. -- frames (aka "panels") alongside the panels for modifying the default UI.
  398. --
  399. -- Adding a new panel to the Interface Options frame is a fairly straightforward process.
  400. -- Any frame can be used as a panel as long as it implements the required values and methods.
  401. -- Once a frame is ready to be used as a panel, it must be registered using the function
  402. -- InterfaceOptions_AddCategory, i.e. InterfaceOptions_AddCategory(panel)
  403. --
  404. -- Panels can be designated as sub-categories of existing options. These panels are listed
  405. -- with smaller text, offset, and tied to parent categories. The parent categories can be expanded
  406. -- or collapsed to toggle display of their sub-categories.
  407. --
  408. -- When players select a category of options from the Interface Options frame, the panel associated
  409. -- with that category will be anchored to the right hand side of the Interface Options frame and shown.
  410. --
  411. -- The following members and methods are used by the Interface Options frame to display and organize panels.
  412. --
  413. -- panel.name - string (required)
  414. -- The name of the AddOn or group of configuration options.
  415. -- This is the text that will display in the AddOn options list.
  416. --
  417. -- panel.parent - string (optional)
  418. -- Name of the parent of the AddOn or group of configuration options.
  419. -- This identifies "panel" as the child of another category.
  420. -- If the parent category doesn't exist, "panel" will be displayed as a regular category.
  421. --
  422. -- panel.okay - function (optional)
  423. -- This method will run when the player clicks "okay" in the Interface Options.
  424. --
  425. -- panel.cancel - function (optional)
  426. -- This method will run when the player clicks "cancel" in the Interface Options.
  427. -- Use this to revert their changes.
  428. --
  429. -- panel.default - function (optional)
  430. -- This method will run when the player clicks "defaults".
  431. -- Use this to revert their changes to your defaults.
  432. --
  433. -- panel.refresh - function (optional)
  434. -- This method will run when the Interface Options frame calls its OnShow function and after defaults
  435. -- have been applied via the panel.default method described above.
  436. -- Use this to refresh your panel's UI in case settings were changed without player interaction.
  437. --
  438. -- EXAMPLE -- Use XML to create a frame, and through its OnLoad function, make the frame a panel.
  439. --
  440. -- MyAddOn.xml
  441. -- ≤Frame name="ExamplePanel">
  442. -- ≤Scripts>
  443. -- ≤OnLoad>
  444. -- ExamplePanel_OnLoad(self);
  445. -- ≤/OnLoad>
  446. -- ≤/Scripts>
  447. -- ≤/Frame>
  448. --
  449. -- MyAddOn.lua
  450. -- function ExamplePanel_OnLoad (panel)
  451. -- panel.name = "My AddOn"
  452. -- InterfaceOptions_AddCategory(panel);
  453. -- end
  454. --
  455. -- EXAMPLE -- Dynamically create a frame and use it as a subcategory for "My AddOn".
  456. --
  457. -- local panel = CreateFrame("FRAME", "ExampleSubCategory");
  458. -- panel.name = "My SubCategory";
  459. -- panel.parent = "My AddOn";
  460. --
  461. -- InterfaceOptions_AddCategory(panel);
  462. --
  463. -- EXAMPLE -- Create a frame with a control, an okay and a cancel method
  464. --
  465. -- --[[ Create a frame to use as the panel ]] --
  466. -- local panel = CreateFrame("FRAME", "ExamplePanel");
  467. -- panel.name = "My AddOn";
  468. --
  469. -- -- [[ When the player clicks okay, set the original value to the current setting ]] --
  470. -- panel.okay =
  471. -- function (self)
  472. -- self.originalValue = MY_VARIABLE;
  473. -- end
  474. --
  475. -- -- [[ When the player clicks cancel, set the current setting to the original value ]] --
  476. -- panel.cancel =
  477. -- function (self)
  478. -- MY_VARIABLE = self.originalValue;
  479. -- end
  480. --
  481. -- -- [[ Add the panel to the Interface Options ]] --
  482. -- InterfaceOptions_AddCategory(panel);
  483. -------------------------------------------------------------------------------------------------
  484. local function AddAddOnCategory(categories, index, frame)
  485. if index then
  486. tinsert(categories, index, frame);
  487. else
  488. tinsert(categories, frame);
  489. end
  490. InterfaceCategoryList_Update();
  491. end
  492. function InterfaceOptions_AddCategory (frame, addOn, position)
  493. if ( issecure() and ( not addOn ) ) then
  494. local parent = frame.parent;
  495. if ( parent ) then
  496. for i = 1, #blizzardCategories do
  497. if ( blizzardCategories[i].name == parent ) then
  498. if ( blizzardCategories[i].hasChildren ) then
  499. frame.hidden = ( blizzardCategories[i].collapsed );
  500. else
  501. frame.hidden = true;
  502. blizzardCategories[i].hasChildren = true;
  503. blizzardCategories[i].collapsed = true;
  504. end
  505. tinsert(blizzardCategories, i + 1, frame);
  506. InterfaceCategoryList_Update();
  507. return;
  508. end
  509. end
  510. end
  511. if ( position ) then
  512. tinsert(blizzardCategories, position, frame);
  513. else
  514. tinsert(blizzardCategories, frame);
  515. end
  516. InterfaceCategoryList_Update();
  517. elseif ( not type(frame) == "table" or not frame.name ) then
  518. --Check to make sure that AddOn interface panels have the necessary attributes to work with the system.
  519. return;
  520. else
  521. frame.okay = frame.okay or function () end;
  522. frame.cancel = frame.cancel or function () end;
  523. frame.default = frame.default or function () end;
  524. frame.refresh = frame.refresh or function () end;
  525. local categories = INTERFACEOPTIONS_ADDONCATEGORIES;
  526. local name = strlower(frame.name);
  527. local parent = frame.parent;
  528. if ( parent ) then
  529. for i = 1, #categories do
  530. if ( categories[i].name == parent ) then
  531. if ( not categories[i].hasChildren ) then
  532. frame.hidden = true;
  533. categories[i].hasChildren = true;
  534. categories[i].collapsed = true;
  535. AddAddOnCategory(categories, i + 1, frame);
  536. return;
  537. end
  538. frame.hidden = ( categories[i].collapsed );
  539. local j = i + 1;
  540. while ( categories[j] and categories[j].parent == parent ) do
  541. -- Skip to the end of the list of children, add this there.
  542. j = j + 1;
  543. end
  544. AddAddOnCategory(categories, j, frame);
  545. return;
  546. end
  547. end
  548. end
  549. for i = 1, #categories do
  550. if ( ( not categories[i].parent ) and ( name < strlower(categories[i].name) ) ) then
  551. AddAddOnCategory(categories, i, frame);
  552. return;
  553. end
  554. end
  555. AddAddOnCategory(categories, position, frame);
  556. end
  557. end