avoid a crash
[QuestHelper.git] / textviewer.lua
blob3b91569f4631dcc6964b99244132e5710e71be37
1 QuestHelper_File["textviewer.lua"] = "Development Version"
2 QuestHelper_Loadtime["textviewer.lua"] = GetTime()
4 local viewer
6 local function viewer_cancelmove(self)
7 if self.isMoving then
8 self:StopMovingOrSizing()
9 self.isMoving = false
10 end
11 end
13 local function viewer_mousedown(self, button)
14 if button == "LeftButton" then
15 self:StartMoving()
16 self.isMoving = true
17 end
18 end
20 local function viewer_closebutton(self)
21 viewer.text:SetText("")
22 viewer:Hide()
23 end
25 local frammis = {}
27 function QuestHelper:ShowText(text, title, width, border, divide)
28 local border = border or 8
29 local divide = divide or 4
31 if not frammis[border] then frammis[border] = {} end
32 viewer = frammis[border][divide]
33 local suffix = string.format("_%d_%d", border, divide)
35 if not viewer then
36 viewer = CreateFrame("Frame", "QuestHelperTextViewer", nil) -- With no parent, this will always be visible.
37 viewer:SetFrameStrata("FULLSCREEN_DIALOG")
38 viewer:SetPoint("CENTER", UIParent)
39 viewer:EnableMouse(true)
40 viewer:SetMovable(true)
41 QH_Hook(viewer, "OnMouseDown", viewer_mousedown)
42 QH_Hook(viewer, "OnMouseUp", viewer_cancelmove)
43 QH_Hook(viewer, "OnHide", viewer_cancelmove)
45 -- This will cause it to be hidden if Esc is pressed.
46 table.insert(UISpecialFrames, viewer:GetName())
48 viewer.title = viewer:CreateFontString()
49 viewer.title:SetFont(self.font.serif, 14)
50 viewer.title:SetPoint("TOPLEFT", viewer, border, -border)
51 viewer.title:SetPoint("RIGHT", viewer, -border, 0)
53 viewer:SetBackdrop({
54 bgFile = "Interface/Tooltips/UI-Tooltip-Background",
55 edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
56 edgeSize = 16,
57 tile = true,
58 tileSize = 16,
59 insets = { left = 4, right = 4, top = 4, bottom = 4 }})
60 viewer:SetBackdropColor(0, 0, 0, 0.8)
61 viewer:SetBackdropBorderColor(1, 1, 1, 0.7)
63 viewer.scrollframe = CreateFrame("ScrollFrame", "QuestHelperTextViewer_ScrollFrame" .. suffix, viewer, "UIPanelScrollFrameTemplate")
65 viewer.scrollframe:SetPoint("LEFT", viewer, "LEFT", border, 0)
66 viewer.scrollframe:SetPoint("TOP", viewer.title, "BOTTOM", 0, -divide)
68 viewer.scrollbar = _G["QuestHelperTextViewer_ScrollFrame" .. suffix .. "ScrollBar"]
69 --QuestHelperTextViewer_ScrollFrameThumbTexture = self:CreateIconTexture(viewer.scrollbar, 26) -- Use the snazzy blue thumb
70 viewer.scrollbar:SetBackdrop({ -- Note: These settings are coppied from UIPanelScrollBarTemplateLightBorder in UIPanelTemplates.xml
71 edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
72 edgeSize = 12,
73 tileSize = 16,
74 insets = { left = 0, right = 0, top = 5, bottom = 5 }})
76 viewer.closebutton = CreateFrame("Button", "QuestHelperTextViewer_CloseButton" .. suffix, viewer, "UIPanelCloseButton")
77 viewer.closebutton:SetPoint("TOPRIGHT", viewer)
78 QH_Hook(viewer.closebutton, "OnClick", viewer_closebutton)
80 viewer.frame = CreateFrame("Frame", "QuestHelperTextViewer_Frame" .. suffix, viewer.scrollframe)
81 viewer.scrollframe:SetScrollChild(viewer.frame)
83 viewer.text = viewer.frame:CreateFontString()
84 viewer.text:SetFont(self.font.sans, 12)
85 viewer.text:SetJustifyH("LEFT")
86 viewer.text:SetPoint("TOPLEFT", viewer.frame)
87 end
89 viewer:Show()
90 viewer.title:SetText(title or "QuestHelper")
91 viewer.text:SetText(text or "No text.")
92 viewer.scrollframe:SetVerticalScroll(0)
94 local w = width or math.min(600, math.max(100, viewer.text:GetStringWidth()))
95 viewer.text:SetWidth(w)
96 viewer:SetWidth(w+border * 2)
97 viewer.scrollframe:SetWidth(w)
98 viewer.frame:SetWidth(w)
100 local h = math.max(10, viewer.text:GetHeight())
101 local title_h = viewer.title:GetHeight()
103 if h > 400 then
104 viewer.frame:SetHeight(400)
105 viewer.scrollframe:SetHeight(400)
106 viewer:SetHeight(420+title_h+border * 2+divide)
107 viewer:SetWidth(w+border * 2 + 22)
108 viewer.scrollbar:Show()
109 else
110 viewer.frame:SetHeight(h)
111 viewer.scrollframe:SetHeight(h)
112 viewer:SetHeight(h+border * 2 + divide+title_h)
113 viewer.scrollbar:Hide()
114 --[[
115 WoW Bug: For some reason, setting the thumb texture on the scrollbar causes the following scenario:
116 1. Display the viewer with scrollable text (eg /qh)
117 2. Display the viewer with smaller text (eg /qh help filter)
118 The second time the viewer is displayed, the close button doesn't show its normal state.
119 When you hover over it, the glow appears. If you press the left button over it, the depressed state appears.
120 If you drag off of it, then release, the normal state appears, and the button is fine until you repeat 1 & 2.
122 viewer.closebutton:SetButtonState("PUSHED") -- Workaround: there's a wierd quirk that's causing it to not show sometimes...
123 viewer.closebutton:SetButtonState("NORMAL") -- Workaround, part 2
126 frammis[border][divide] = viewer