forked from HizurosWoWAddOns/LibDropDownMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibDropDownMenu.lua
1657 lines (1460 loc) · 51 KB
/
LibDropDownMenu.lua
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local MAJOR, MINOR = "LibDropDownMenu", tonumber((gsub("@project-version@","r",""))) or 9999;
local lib = LibStub:NewLibrary(MAJOR, MINOR);
if not lib then return end
local _G,print,math,type,max,table,tonumber = _G,print,math,type,max,table,tonumber;
local strmatch,gsub,ipairs,strlen,strsub,select = strmatch,gsub,ipairs,strlen,strsub,select;
local hooksecurefunc,C_Texture = hooksecurefunc,C_Texture;
local CreateFrame,GetCursorPosition,PlaySound = CreateFrame,GetCursorPosition,PlaySound;
local GetScreenWidth,GetScreenHeight = GetScreenWidth,GetScreenHeight;
local GetCVar,SetCVar,GetAppropriateTooltip = GetCVar,SetCVar,GetAppropriateTooltip;
local UIParent,SOUNDKIT,GameTooltip_SetTitle = UIParent,SOUNDKIT,GameTooltip_SetTitle;
local GameFontDisableSmallLeft = GameFontDisableSmallLeft;
local GameFontHighlightSmallLeft = GameFontHighlightSmallLeft;
local GameFontNormalSmallLeft = GameFontNormalSmallLeft;
local GameTooltip_AddInstructionLine = GameTooltip_AddInstructionLine;
local GameTooltip_AddNormalLine = GameTooltip_AddNormalLine;
local GameTooltip_AddColoredLine = GameTooltip_AddColoredLine;
local GetValueOrCallFunction = GetValueOrCallFunction;
if WOW_PROJECT_ID ~= WOW_PROJECT_MAINLINE then -- classic compatibilty
function GetValueOrCallFunction(tbl, key, ...)
if not tbl then
return;
end
local value = tbl[key];
if type(value) == "function" then
return value(...);
else
return value;
end
end
-- Start the countdown on a frame
function lib.UIDropDownMenu_StartCounting(frame)
if ( frame.parent ) then
lib.UIDropDownMenu_StartCounting(frame.parent);
else
frame.showTimer = UIDROPDOWNMENU_SHOW_TIME;
frame.isCounting = 1;
end
end
-- Stop the countdown on a frame
function lib.UIDropDownMenu_StopCounting(frame)
if ( frame.parent ) then
lib.UIDropDownMenu_StopCounting(frame.parent);
else
frame.isCounting = nil;
end
end
end
setfenv(1,lib);
-- start of content from UIDropDownMenu.lua
UIDROPDOWNMENU_MAXBUTTONS = 1;
UIDROPDOWNMENU_MAXLEVELS = 3;
UIDROPDOWNMENU_BUTTON_HEIGHT = 16;
UIDROPDOWNMENU_BORDER_HEIGHT = 15;
-- The current open menu
UIDROPDOWNMENU_OPEN_MENU = nil;
-- The current menu being initialized
UIDROPDOWNMENU_INIT_MENU = nil;
-- Current level shown of the open menu
UIDROPDOWNMENU_MENU_LEVEL = 1;
-- Current value of the open menu
UIDROPDOWNMENU_MENU_VALUE = nil;
-- Time to wait to hide the menu
UIDROPDOWNMENU_SHOW_TIME = 2;
-- Default dropdown text height
UIDROPDOWNMENU_DEFAULT_TEXT_HEIGHT = nil;
-- List of open menus
OPEN_DROPDOWNMENUS = {};
local UIDropDownMenuDelegate = CreateFrame("FRAME");
function UIDropDownMenuDelegate_OnAttributeChanged (self, attribute, value)
if ( attribute == "createframes" and value == true ) then
UIDropDownMenu_CreateFrames(self:GetAttribute("createframes-level"), self:GetAttribute("createframes-index"));
elseif ( attribute == "initmenu" ) then
UIDROPDOWNMENU_INIT_MENU = value;
elseif ( attribute == "openmenu" ) then
UIDROPDOWNMENU_OPEN_MENU = value;
end
end
UIDropDownMenuDelegate:SetScript("OnAttributeChanged", UIDropDownMenuDelegate_OnAttributeChanged);
function UIDropDownMenu_InitializeHelper(frame)
-- This deals with the potentially tainted stuff!
if ( frame ~= UIDROPDOWNMENU_OPEN_MENU ) then
UIDROPDOWNMENU_MENU_LEVEL = 1;
end
-- Set the frame that's being intialized
UIDropDownMenuDelegate:SetAttribute("initmenu", frame);
-- Hide all the buttons
local button, dropDownList;
for i = 1, UIDROPDOWNMENU_MAXLEVELS, 1 do
dropDownList = _G["LibDropDownMenu_List"..i];
if ( i >= UIDROPDOWNMENU_MENU_LEVEL or frame ~= UIDROPDOWNMENU_OPEN_MENU ) then
dropDownList.numButtons = 0;
dropDownList.maxWidth = 0;
for j=1, UIDROPDOWNMENU_MAXBUTTONS, 1 do
button = _G["LibDropDownMenu_List"..i.."Button"..j];
button:Hide();
end
dropDownList:Hide();
end
end
frame:SetHeight(UIDROPDOWNMENU_BUTTON_HEIGHT * 2);
end
local function GetChild(frame, name, key)
if (frame[key]) then
return frame[key];
elseif name then
return _G[name..key];
end
return nil;
end
function UIDropDownMenu_Initialize(frame, initFunction, displayMode, level, menuList)
frame.menuList = menuList;
UIDropDownMenu_InitializeHelper(frame);
-- Set the initialize function and call it. The initFunction populates the dropdown list.
if ( initFunction ) then
UIDropDownMenu_SetInitializeFunction(frame, initFunction);
initFunction(frame, level, frame.menuList);
end
--master frame
if(level == nil) then
level = 1;
end
local dropDownList = _G["LibDropDownMenu_List"..level];
dropDownList.dropdown = frame;
dropDownList.shouldRefresh = true;
UIDropDownMenu_SetDisplayMode(frame, displayMode);
end
function UIDropDownMenu_SetInitializeFunction(frame, initFunction)
frame.initialize = initFunction;
end
function UIDropDownMenu_SetDisplayMode(frame, displayMode)
-- Change appearance based on the displayMode
-- Note: this is a one time change based on previous behavior.
if ( displayMode == "MENU" ) then
local name = frame:GetName();
GetChild(frame, name, "Left"):Hide();
GetChild(frame, name, "Middle"):Hide();
GetChild(frame, name, "Right"):Hide();
local button = GetChild(frame, name, "Button");
local buttonName = button:GetName();
GetChild(button, buttonName, "NormalTexture"):SetTexture(nil);
GetChild(button, buttonName, "DisabledTexture"):SetTexture(nil);
GetChild(button, buttonName, "PushedTexture"):SetTexture(nil);
GetChild(button, buttonName, "HighlightTexture"):SetTexture(nil);
local text = GetChild(frame, name, "Text");
button:ClearAllPoints();
button:SetPoint("LEFT", text, "LEFT", -9, 0);
button:SetPoint("RIGHT", text, "RIGHT", 6, 0);
frame.displayMode = "MENU";
end
end
function UIDropDownMenu_SetFrameStrata(frame, frameStrata)
frame.listFrameStrata = frameStrata;
end
function UIDropDownMenu_RefreshDropDownSize(self)
self.maxWidth = UIDropDownMenu_GetMaxButtonWidth(self);
self:SetWidth(self.maxWidth + 25);
for i=1, UIDROPDOWNMENU_MAXBUTTONS, 1 do
local icon = _G[self:GetName().."Button"..i.."Icon"];
if ( icon.tFitDropDownSizeX ) then
icon:SetWidth(self.maxWidth - 5);
end
end
end
-- If dropdown is visible then see if its timer has expired, if so hide the frame
function UIDropDownMenu_OnUpdate(self, elapsed)
if ( self.shouldRefresh ) then
UIDropDownMenu_RefreshDropDownSize(self);
self.shouldRefresh = false;
end
if _G.WOW_PROJECT_ID ~= _G.WOW_PROJECT_MAINLINE then -- classic compatibilty
if ( not self.showTimer or not self.isCounting ) then
return;
elseif ( self.showTimer < 0 ) then
self:Hide();
self.showTimer = nil;
self.isCounting = nil;
else
self.showTimer = self.showTimer - elapsed;
end
end
end
function UIDropDownMenuButtonInvisibleButton_OnEnter(self)
if UIDropDownMenu_StopCounting then -- classic compatibilty
UIDropDownMenu_StopCounting(self:GetParent():GetParent());
end
CloseDropDownMenus(self:GetParent():GetParent():GetID() + 1);
local parent = self:GetParent();
if ( parent.tooltipTitle and parent.tooltipWhileDisabled) then
if ( parent.tooltipOnButton ) then
local tooltip = GetAppropriateTooltip();
tooltip:SetOwner(parent, "ANCHOR_RIGHT");
GameTooltip_SetTitle(tooltip, parent.tooltipTitle);
if parent.tooltipInstruction then
GameTooltip_AddInstructionLine(tooltip, parent.tooltipInstruction);
end
if parent.tooltipText then
GameTooltip_AddNormalLine(tooltip, parent.tooltipText, true);
end
if parent.tooltipWarning then
GameTooltip_AddColoredLine(tooltip, parent.tooltipWarning, RED_FONT_COLOR, true);
end
if parent.tooltipBackdropStyle then
SharedTooltip_SetBackdropStyle(tooltip, parent.tooltipBackdropStyle);
end
tooltip:Show();
end
end
end
function UIDropDownMenuButtonInvisibleButton_OnLeave(self)
if UIDropDownMenu_StartCounting then -- classic compatibilty
UIDropDownMenu_StartCounting(self:GetParent():GetParent());
end
GetAppropriateTooltip():Hide();
end
function UIDropDownMenuButton_OnEnter(self)
if ( self.hasArrow ) then
local level = self:GetParent():GetID() + 1;
local listFrame = _G["LibDropDownMenu_List"..level];
if ( not listFrame or not listFrame:IsShown() or select(2, listFrame:GetPoint(1)) ~= self ) then
ToggleDropDownMenu(self:GetParent():GetID() + 1, self.value, nil, nil, nil, nil, self.menuList, self);
end
else
CloseDropDownMenus(self:GetParent():GetID() + 1);
end
self.Highlight:Show();
if UIDropDownMenu_StopCounting then -- classic compatibilty
UIDropDownMenu_StopCounting(self:GetParent());
end
if ( self.tooltipTitle and not self.noTooltipWhileEnabled and not UIDropDownMenuButton_ShouldShowIconTooltip(self) ) then
if ( self.tooltipOnButton ) then
local tooltip = GetAppropriateTooltip();
tooltip:SetOwner(self, "ANCHOR_RIGHT");
GameTooltip_SetTitle(tooltip, self.tooltipTitle);
if self.tooltipInstruction then
GameTooltip_AddInstructionLine(tooltip, self.tooltipInstruction);
end
if self.tooltipText then
GameTooltip_AddNormalLine(tooltip, self.tooltipText, true);
end
if self.tooltipWarning then
GameTooltip_AddColoredLine(tooltip, self.tooltipWarning, RED_FONT_COLOR, true);
end
if self.tooltipBackdropStyle then
SharedTooltip_SetBackdropStyle(tooltip, self.tooltipBackdropStyle);
end
tooltip:Show();
end
end
if ( self.mouseOverIcon ~= nil ) then
self.Icon:SetTexture(self.mouseOverIcon);
self.Icon:Show();
end
GetValueOrCallFunction(self, "funcOnEnter", self);
end
function UIDropDownMenuButton_OnLeave(self)
self.Highlight:Hide();
if UIDropDownMenu_StartCounting then -- classic compatibilty
UIDropDownMenu_StartCounting(self:GetParent());
end
GetAppropriateTooltip():Hide();
if ( self.mouseOverIcon ~= nil ) then
if ( self.icon ~= nil ) then
self.Icon:SetTexture(self.icon);
else
self.Icon:Hide();
end
end
GetValueOrCallFunction(self, "funcOnLeave", self);
end
function UIDropDownMenuButton_ShouldShowIconTooltip(self)
if self.Icon and (self.iconTooltipTitle or self.iconTooltipText) and (self.icon or self.mouseOverIcon) then
return GetMouseFocus() == self.Icon;
end
return false;
end
function UIDropDownMenuButtonIcon_OnClick(self, mouseButton)
local button = self:GetParent();
if not button then
return;
end
UIDropDownMenuButton_OnClick(button, mouseButton);
end
function UIDropDownMenuButtonIcon_OnEnter(self)
local button = self:GetParent();
if not button then
return;
end
local shouldShowIconTooltip = UIDropDownMenuButton_ShouldShowIconTooltip(button);
if shouldShowIconTooltip then
local tooltip = GetAppropriateTooltip();
tooltip:SetOwner(button, "ANCHOR_RIGHT");
if button.iconTooltipTitle then
GameTooltip_SetTitle(tooltip, button.iconTooltipTitle);
end
if button.iconTooltipText then
GameTooltip_AddNormalLine(tooltip, button.iconTooltipText, true);
end
if button.iconTooltipBackdropStyle then
SharedTooltip_SetBackdropStyle(tooltip, button.iconTooltipBackdropStyle);
end
tooltip:Show();
end
UIDropDownMenuButton_OnEnter(button);
end
function UIDropDownMenuButtonIcon_OnLeave(self)
local button = self:GetParent();
if not button then
return;
end
UIDropDownMenuButton_OnLeave(button);
end
--[[
List of button attributes
======================================================
info.text = [STRING] -- The text of the button
info.value = [ANYTHING] -- The value that UIDROPDOWNMENU_MENU_VALUE is set to when the button is clicked
info.func = [function()] -- The function that is called when you click the button
info.checked = [nil, true, function] -- Check the button if true or function returns true
info.isNotRadio = [nil, true] -- Check the button uses radial image if false check box image if true
info.isTitle = [nil, true] -- If it's a title the button is disabled and the font color is set to yellow
info.disabled = [nil, true] -- Disable the button and show an invisible button that still traps the mouseover event so menu doesn't time out
info.tooltipWhileDisabled = [nil, 1] -- Show the tooltip, even when the button is disabled.
info.hasArrow = [nil, true] -- Show the expand arrow for multilevel menus
info.hasColorSwatch = [nil, true] -- Show color swatch or not, for color selection
info.r = [1 - 255] -- Red color value of the color swatch
info.g = [1 - 255] -- Green color value of the color swatch
info.b = [1 - 255] -- Blue color value of the color swatch
info.colorCode = [STRING] -- "|cAARRGGBB" embedded hex value of the button text color. Only used when button is enabled
info.swatchFunc = [function()] -- Function called by the color picker on color change
info.hasOpacity = [nil, 1] -- Show the opacity slider on the colorpicker frame
info.opacity = [0.0 - 1.0] -- Percentatge of the opacity, 1.0 is fully shown, 0 is transparent
info.opacityFunc = [function()] -- Function called by the opacity slider when you change its value
info.cancelFunc = [function(previousValues)] -- Function called by the colorpicker when you click the cancel button (it takes the previous values as its argument)
info.notClickable = [nil, 1] -- Disable the button and color the font white
info.notCheckable = [nil, 1] -- Shrink the size of the buttons and don't display a check box
info.owner = [Frame] -- Dropdown frame that "owns" the current dropdownlist
info.keepShownOnClick = [nil, 1] -- Don't hide the dropdownlist after a button is clicked
info.tooltipTitle = [nil, STRING] -- Title of the tooltip shown on mouseover
info.tooltipText = [nil, STRING] -- Text of the tooltip shown on mouseover
info.tooltipWarning = [nil, STRING] -- Warning-style text of the tooltip shown on mouseover
info.tooltipInstruction = [nil, STRING] -- Instruction-style text of the tooltip shown on mouseover
info.tooltipOnButton = [nil, 1] -- Show the tooltip attached to the button instead of as a Newbie tooltip.
info.tooltipBackdropStyle = [nil, TABLE] -- Optional Backdrop style of the tooltip shown on mouseover
info.justifyH = [nil, "CENTER"] -- Justify button text
info.arg1 = [ANYTHING] -- This is the first argument used by info.func
info.arg2 = [ANYTHING] -- This is the second argument used by info.func
info.fontObject = [FONT] -- font object replacement for Normal and Highlight
info.menuList = [TABLE] -- This contains an array of info tables to be displayed as a child menu
info.noClickSound = [nil, 1] -- Set to 1 to suppress the sound when clicking the button. The sound only plays if .func is set.
info.padding = [nil, NUMBER] -- Number of pixels to pad the text on the right side
info.topPadding = [nil, NUMBER] -- Extra spacing between buttons.
info.leftPadding = [nil, NUMBER] -- Number of pixels to pad the button on the left side
info.minWidth = [nil, NUMBER] -- Minimum width for this line
info.customFrame = frame -- Allows this button to be a completely custom frame, should inherit from UIDropDownCustomMenuEntryTemplate and override appropriate methods.
info.icon = [TEXTURE] -- An icon for the button.
info.iconXOffset = [nil, NUMBER] -- Number of pixels to shift the button's icon to the left or right (positive numbers shift right, negative numbers shift left).
info.iconTooltipTitle = [nil, STRING] -- Title of the tooltip shown on icon mouseover
info.iconTooltipText = [nil, STRING] -- Text of the tooltip shown on icon mouseover
info.iconTooltipBackdropStyle = [nil, TABLE] -- Optional Backdrop style of the tooltip shown on icon mouseover
info.mouseOverIcon = [TEXTURE] -- An override icon when a button is moused over.
info.ignoreAsMenuSelection [nil, true] -- Never set the menu text/icon to this, even when this button is checked
info.registerForRightClick [nil, true] -- Register dropdown buttons for right clicks
]]
function UIDropDownMenu_CreateInfo()
return {};
end
function UIDropDownMenu_CreateFrames(level, index)
while ( level > UIDROPDOWNMENU_MAXLEVELS ) do
UIDROPDOWNMENU_MAXLEVELS = UIDROPDOWNMENU_MAXLEVELS + 1;
local newList = Create_DropDownList("LibDropDownMenu_List"..UIDROPDOWNMENU_MAXLEVELS);
newList:SetFrameStrata("FULLSCREEN_DIALOG");
newList:SetToplevel(true);
newList:Hide();
newList:SetID(UIDROPDOWNMENU_MAXLEVELS);
newList:SetWidth(180)
newList:SetHeight(10)
for i=1, UIDROPDOWNMENU_MAXBUTTONS do
local newButton = Create_DropDownMenuButton("LibDropDownMenu_List"..UIDROPDOWNMENU_MAXLEVELS.."Button"..i, newList);
newButton:SetID(i);
end
end
while ( index > UIDROPDOWNMENU_MAXBUTTONS ) do
UIDROPDOWNMENU_MAXBUTTONS = UIDROPDOWNMENU_MAXBUTTONS + 1;
for i=1, UIDROPDOWNMENU_MAXLEVELS do
local newButton = Create_DropDownMenuButton("LibDropDownMenu_List"..i.."Button"..UIDROPDOWNMENU_MAXBUTTONS, _G["LibDropDownMenu_List"..i]);
newButton:SetID(UIDROPDOWNMENU_MAXBUTTONS);
end
end
end
function UIDropDownMenu_AddSeparator(level)
local separatorInfo = {
hasArrow = false;
dist = 0;
isTitle = true;
isUninteractable = true;
notCheckable = true;
iconOnly = true;
icon = "Interface\\Common\\UI-TooltipDivider-Transparent";
tCoordLeft = 0;
tCoordRight = 1;
tCoordTop = 0;
tCoordBottom = 1;
tSizeX = 0;
tSizeY = 8;
tFitDropDownSizeX = true;
iconInfo = {
tCoordLeft = 0,
tCoordRight = 1,
tCoordTop = 0,
tCoordBottom = 1,
tSizeX = 0,
tSizeY = 8,
tFitDropDownSizeX = true
},
};
UIDropDownMenu_AddButton(separatorInfo, level);
end
function UIDropDownMenu_AddSpace(level)
local spaceInfo = {
hasArrow = false,
dist = 0,
isTitle = true,
isUninteractable = true,
notCheckable = true,
};
UIDropDownMenu_AddButton(spaceInfo, level);
end
function UIDropDownMenu_AddButton(info, level)
--[[
Might to uncomment this if there are performance issues
if ( not UIDROPDOWNMENU_OPEN_MENU ) then
return;
end
]]
if ( not level ) then
level = 1;
end
local listFrame = _G["LibDropDownMenu_List"..level];
local index = listFrame and (listFrame.numButtons + 1) or 1;
local width;
UIDropDownMenuDelegate:SetAttribute("createframes-level", level);
UIDropDownMenuDelegate:SetAttribute("createframes-index", index);
UIDropDownMenuDelegate:SetAttribute("createframes", true);
listFrame = listFrame or _G["LibDropDownMenu_List"..level];
local listFrameName = listFrame:GetName();
-- Set the number of buttons in the listframe
listFrame.numButtons = index;
local button = _G[listFrameName.."Button"..index];
local normalText = _G[button:GetName().."NormalText"];
local icon = _G[button:GetName().."Icon"];
-- This button is used to capture the mouse OnEnter/OnLeave events if the dropdown button is disabled, since a disabled button doesn't receive any events
-- This is used specifically for drop down menu time outs
local invisibleButton = _G[button:GetName().."InvisibleButton"];
-- Default settings
button:SetDisabledFontObject(GameFontDisableSmallLeft);
invisibleButton:Hide();
button:Enable();
if ( info.registerForRightClick ) then
button:RegisterForClicks("LeftButtonUp", "RightButtonUp");
else
button:RegisterForClicks("LeftButtonUp");
end
-- If not clickable then disable the button and set it white
if ( info.notClickable ) then
info.disabled = true;
button:SetDisabledFontObject(GameFontHighlightSmallLeft);
end
-- Set the text color and disable it if its a title
if ( info.isTitle ) then
info.disabled = true;
button:SetDisabledFontObject(GameFontNormalSmallLeft);
end
-- Disable the button if disabled and turn off the color code
if ( info.disabled ) then
button:Disable();
invisibleButton:Show();
info.colorCode = nil;
end
-- If there is a color for a disabled line, set it
if( info.disablecolor ) then
info.colorCode = info.disablecolor;
end
-- Configure button
if ( info.text ) then
-- look for inline color code this is only if the button is enabled
if ( info.colorCode ) then
button:SetText(info.colorCode..info.text.."|r");
else
button:SetText(info.text);
end
-- Set icon
if ( info.icon or info.mouseOverIcon ) then
icon:SetSize(16,16);
if(info.icon and C_Texture.GetAtlasInfo(info.icon)) then
icon:SetAtlas(info.icon);
else
icon:SetTexture(info.icon);
end
icon:ClearAllPoints();
icon:SetPoint("RIGHT", info.iconXOffset or 0, 0);
if ( info.tCoordLeft ) then
icon:SetTexCoord(info.tCoordLeft, info.tCoordRight, info.tCoordTop, info.tCoordBottom);
else
icon:SetTexCoord(0, 1, 0, 1);
end
icon:Show();
else
icon:Hide();
end
-- Check to see if there is a replacement font
if ( info.fontObject ) then
button:SetNormalFontObject(info.fontObject);
button:SetHighlightFontObject(info.fontObject);
else
button:SetNormalFontObject(GameFontHighlightSmallLeft);
button:SetHighlightFontObject(GameFontHighlightSmallLeft);
end
else
button:SetText("");
icon:Hide();
end
button.iconOnly = nil;
button.icon = nil;
button.iconInfo = nil;
if (info.iconInfo) then
icon.tFitDropDownSizeX = info.iconInfo.tFitDropDownSizeX;
else
icon.tFitDropDownSizeX = nil;
end
if (info.iconOnly and info.icon) then
button.iconOnly = true;
button.icon = info.icon;
button.iconInfo = info.iconInfo;
UIDropDownMenu_SetIconImage(icon, info.icon, info.iconInfo);
icon:ClearAllPoints();
icon:SetPoint("LEFT");
end
-- Pass through attributes
button.func = info.func;
button.funcOnEnter = info.funcOnEnter;
button.funcOnLeave = info.funcOnLeave;
button.owner = info.owner;
button.hasOpacity = info.hasOpacity;
button.opacity = info.opacity;
button.opacityFunc = info.opacityFunc;
button.cancelFunc = info.cancelFunc;
button.swatchFunc = info.swatchFunc;
button.keepShownOnClick = info.keepShownOnClick;
button.tooltipTitle = info.tooltipTitle;
button.tooltipText = info.tooltipText;
button.tooltipInstruction = info.tooltipInstruction;
button.tooltipWarning = info.tooltipWarning;
button.tooltipBackdropStyle = info.tooltipBackdropStyle;
button.arg1 = info.arg1;
button.arg2 = info.arg2;
button.hasArrow = info.hasArrow;
button.hasColorSwatch = info.hasColorSwatch;
button.notCheckable = info.notCheckable;
button.menuList = info.menuList;
button.tooltipWhileDisabled = info.tooltipWhileDisabled;
button.noTooltipWhileEnabled = info.noTooltipWhileEnabled;
button.tooltipOnButton = info.tooltipOnButton;
button.noClickSound = info.noClickSound;
button.padding = info.padding;
button.icon = info.icon;
button.iconTooltipTitle = info.iconTooltipTitle;
button.iconTooltipText = info.iconTooltipText;
button.iconTooltipBackdropStyle = info.iconTooltipBackdropStyle;
button.iconXOffset = info.iconXOffset;
button.mouseOverIcon = info.mouseOverIcon;
button.ignoreAsMenuSelection = info.ignoreAsMenuSelection;
if ( info.value ~= nil) then
button.value = info.value;
elseif ( info.text ) then
button.value = info.text;
else
button.value = nil;
end
local expandArrow = _G[listFrameName.."Button"..index.."ExpandArrow"];
expandArrow:SetShown(info.hasArrow);
expandArrow:SetEnabled(not info.disabled);
-- If not checkable move everything over to the left to fill in the gap where the check would be
local xPos = 5;
local buttonHeight = (info.topPadding or 0) + UIDROPDOWNMENU_BUTTON_HEIGHT;
local yPos = -((button:GetID() - 1) * buttonHeight) - UIDROPDOWNMENU_BORDER_HEIGHT;
local displayInfo = normalText;
if (info.iconOnly) then
displayInfo = icon;
end
displayInfo:ClearAllPoints();
if ( info.notCheckable ) then
if ( info.justifyH and info.justifyH == "CENTER" ) then
displayInfo:SetPoint("CENTER", button, "CENTER", -7, 0);
else
displayInfo:SetPoint("LEFT", button, "LEFT", 0, 0);
end
xPos = xPos + 10;
else
xPos = xPos + 12;
displayInfo:SetPoint("LEFT", button, "LEFT", 20, 0);
end
-- Adjust offset if displayMode is menu
local frame = UIDROPDOWNMENU_OPEN_MENU;
if ( frame and frame.displayMode == "MENU" ) then
if ( not info.notCheckable ) then
xPos = xPos - 6;
end
end
-- If no open frame then set the frame to the currently initialized frame
frame = frame or UIDROPDOWNMENU_INIT_MENU;
if ( info.leftPadding ) then
xPos = xPos + info.leftPadding;
end
button:SetPoint("TOPLEFT", button:GetParent(), "TOPLEFT", xPos, yPos);
-- See if button is selected by id or name
if ( frame ) then
if ( UIDropDownMenu_GetSelectedName(frame) ) then
if ( button:GetText() == UIDropDownMenu_GetSelectedName(frame) ) then
info.checked = 1;
end
elseif ( UIDropDownMenu_GetSelectedID(frame) ) then
if ( button:GetID() == UIDropDownMenu_GetSelectedID(frame) ) then
info.checked = 1;
end
elseif ( UIDropDownMenu_GetSelectedValue(frame) ~= nil ) then
if ( button.value == UIDropDownMenu_GetSelectedValue(frame) ) then
info.checked = 1;
end
end
end
if not info.notCheckable then
local check = _G[listFrameName.."Button"..index.."Check"];
local uncheck = _G[listFrameName.."Button"..index.."UnCheck"];
if ( info.disabled ) then
check:SetDesaturated(true);
check:SetAlpha(0.5);
uncheck:SetDesaturated(true);
uncheck:SetAlpha(0.5);
else
check:SetDesaturated(false);
check:SetAlpha(1);
uncheck:SetDesaturated(false);
uncheck:SetAlpha(1);
end
if info.customCheckIconAtlas or info.customCheckIconTexture then
check:SetTexCoord(0, 1, 0, 1);
uncheck:SetTexCoord(0, 1, 0, 1);
if info.customCheckIconAtlas then
check:SetAtlas(info.customCheckIconAtlas);
uncheck:SetAtlas(info.customUncheckIconAtlas or info.customCheckIconAtlas);
else
check:SetTexture(info.customCheckIconTexture);
uncheck:SetTexture(info.customUncheckIconTexture or info.customCheckIconTexture);
end
elseif info.isNotRadio then
check:SetTexCoord(0.0, 0.5, 0.0, 0.5);
check:SetTexture("Interface\\Common\\UI-DropDownRadioChecks");
uncheck:SetTexCoord(0.5, 1.0, 0.0, 0.5);
uncheck:SetTexture("Interface\\Common\\UI-DropDownRadioChecks");
else
check:SetTexCoord(0.0, 0.5, 0.5, 1.0);
check:SetTexture("Interface\\Common\\UI-DropDownRadioChecks");
uncheck:SetTexCoord(0.5, 1.0, 0.5, 1.0);
uncheck:SetTexture("Interface\\Common\\UI-DropDownRadioChecks");
end
-- Checked can be a function now
local checked = info.checked;
if ( type(checked) == "function" ) then
checked = checked(button);
end
-- Show the check if checked
if ( checked ) then
button:LockHighlight();
check:Show();
uncheck:Hide();
else
button:UnlockHighlight();
check:Hide();
uncheck:Show();
end
else
_G[listFrameName.."Button"..index.."Check"]:Hide();
_G[listFrameName.."Button"..index.."UnCheck"]:Hide();
end
button.checked = info.checked;
-- If has a colorswatch, show it and vertex color it
local colorSwatch = _G[listFrameName.."Button"..index.."ColorSwatch"];
if ( info.hasColorSwatch ) then
_G["LibDropDownMenu_List"..level.."Button"..index.."ColorSwatch"].Color:SetVertexColor(info.r, info.g, info.b);
button.r = info.r;
button.g = info.g;
button.b = info.b;
colorSwatch:Show();
else
colorSwatch:Hide();
end
UIDropDownMenu_CheckAddCustomFrame(listFrame, button, info);
button:SetShown(button.customFrame == nil);
button.minWidth = info.minWidth;
width = max(UIDropDownMenu_GetButtonWidth(button), info.minWidth or 0);
--Set maximum button width
if ( width > listFrame.maxWidth ) then
listFrame.maxWidth = width;
end
local customFrameCount = listFrame.customFrames and #listFrame.customFrames or 0;
local height = ((index - customFrameCount) * buttonHeight) + (UIDROPDOWNMENU_BORDER_HEIGHT * 2);
for frameIndex = 1, customFrameCount do
local frame = listFrame.customFrames[frameIndex];
height = height + frame:GetPreferredEntryHeight();
end
-- Set the height of the listframe
listFrame:SetHeight(height);
return button;
end
function UIDropDownMenu_CheckAddCustomFrame(self, button, info)
local customFrame = info.customFrame;
button.customFrame = customFrame;
if customFrame then
customFrame:SetOwningButton(button);
customFrame:ClearAllPoints();
customFrame:SetPoint("TOPLEFT", button, "TOPLEFT", 0, 0);
customFrame:Show();
UIDropDownMenu_RegisterCustomFrame(self, customFrame);
end
end
function UIDropDownMenu_RegisterCustomFrame(self, customFrame)
self.customFrames = self.customFrames or {}
table.insert(self.customFrames, customFrame);
end
function UIDropDownMenu_GetMaxButtonWidth(self)
local maxWidth = 0;
for i=1, self.numButtons do
local button = _G[self:GetName().."Button"..i];
local width = UIDropDownMenu_GetButtonWidth(button);
if ( width > maxWidth ) then
maxWidth = width;
end
end
return maxWidth;
end
function UIDropDownMenu_GetButtonWidth(button)
local minWidth = button.minWidth or 0;
if button.customFrame and button.customFrame:IsShown() then
return math.max(minWidth, button.customFrame:GetPreferredEntryWidth());
end
if not button:IsShown() then
return 0;
end
local width;
local buttonName = button:GetName();
local icon = _G[buttonName.."Icon"];
local normalText = _G[buttonName.."NormalText"];
if ( button.iconOnly and icon ) then
width = icon:GetWidth();
elseif ( normalText and normalText:GetText() ) then
width = normalText:GetWidth() + 40;
if ( button.icon ) then
-- Add padding for the icon
width = width + 10;
end
else
return minWidth;
end
-- Add padding if has and expand arrow or color swatch
if ( button.hasArrow or button.hasColorSwatch ) then
width = width + 10;
end
if ( button.notCheckable ) then
width = width - 30;
end
if ( button.padding ) then
width = width + button.padding;
end
return math.max(minWidth, width);
end
function UIDropDownMenu_Refresh(frame, useValue, dropdownLevel)
local maxWidth = 0;
local somethingChecked = nil;
if ( not dropdownLevel ) then
dropdownLevel = UIDROPDOWNMENU_MENU_LEVEL;
end
local listFrame = _G["LibDropDownMenu_List"..dropdownLevel];
listFrame.numButtons = listFrame.numButtons or 0;
-- Just redraws the existing menu
for i=1, UIDROPDOWNMENU_MAXBUTTONS do
local button = _G["LibDropDownMenu_List"..dropdownLevel.."Button"..i];
local checked = nil;
if(i <= listFrame.numButtons) then
-- See if checked or not
if ( UIDropDownMenu_GetSelectedName(frame) ) then
if ( button:GetText() == UIDropDownMenu_GetSelectedName(frame) ) then
checked = 1;
end
elseif ( UIDropDownMenu_GetSelectedID(frame) ) then
if ( button:GetID() == UIDropDownMenu_GetSelectedID(frame) ) then
checked = 1;
end
elseif ( UIDropDownMenu_GetSelectedValue(frame) ) then
if ( button.value == UIDropDownMenu_GetSelectedValue(frame) ) then
checked = 1;
end
end
end
if (button.checked and type(button.checked) == "function") then
checked = button.checked(button);
end
if not button.notCheckable and button:IsShown() then
-- If checked show check image
local checkImage = _G["LibDropDownMenu_List"..dropdownLevel.."Button"..i.."Check"];
local uncheckImage = _G["LibDropDownMenu_List"..dropdownLevel.."Button"..i.."UnCheck"];
if ( checked ) then
if not button.ignoreAsMenuSelection then
somethingChecked = true;
local icon = GetChild(frame, frame:GetName(), "Icon");
if (button.iconOnly and icon and button.icon) then
UIDropDownMenu_SetIconImage(icon, button.icon, button.iconInfo);
elseif ( useValue ) then
UIDropDownMenu_SetText(frame, button.value);
icon:Hide();
else
UIDropDownMenu_SetText(frame, button:GetText());
icon:Hide();
end
end
button:LockHighlight();
checkImage:Show();
uncheckImage:Hide();
else
button:UnlockHighlight();
checkImage:Hide();
uncheckImage:Show();
end
end
if ( button:IsShown() ) then
local width = UIDropDownMenu_GetButtonWidth(button);
if ( width > maxWidth ) then
maxWidth = width;
end
end
end
if(somethingChecked == nil) then
UIDropDownMenu_SetText(frame, VIDEO_QUALITY_LABEL6);
local icon = GetChild(frame, frame:GetName(), "Icon");
icon:Hide();
end
if (not frame.noResize) then
for i=1, UIDROPDOWNMENU_MAXBUTTONS do
local button = _G["LibDropDownMenu_List"..dropdownLevel.."Button"..i];
button:SetWidth(maxWidth);
end
UIDropDownMenu_RefreshDropDownSize(_G["LibDropDownMenu_List"..dropdownLevel]);
end
end
function UIDropDownMenu_RefreshAll(frame, useValue)
for dropdownLevel = UIDROPDOWNMENU_MENU_LEVEL, 2, -1 do
local listFrame = _G["LibDropDownMenu_List"..dropdownLevel];
if ( listFrame:IsShown() ) then
UIDropDownMenu_Refresh(frame, nil, dropdownLevel);
end
end
-- useValue is the text on the dropdown, only needs to be set once
UIDropDownMenu_Refresh(frame, useValue, 1);
end
function UIDropDownMenu_SetIconImage(icon, texture, info)
icon:SetTexture(texture);
if ( info.tCoordLeft ) then
icon:SetTexCoord(info.tCoordLeft, info.tCoordRight, info.tCoordTop, info.tCoordBottom);
else
icon:SetTexCoord(0, 1, 0, 1);
end
if ( info.tSizeX ) then
icon:SetWidth(info.tSizeX);
else
icon:SetWidth(16);
end
if ( info.tSizeY ) then
icon:SetHeight(info.tSizeY);
else
icon:SetHeight(16);
end
icon:Show();
end