---------------------------------------------------------------------------------
--
-- Prat - A framework for World of Warcraft chat mods
--
-- Copyright (C) 2006-2007  Prat Development Team
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to:
--
-- Free Software Foundation, Inc., 
-- 51 Franklin Street, Fifth Floor, 
-- Boston, MA  02110-1301, USA.
--
--
-------------------------------------------------------------------------------



--[[
Name: PratFontSize
Revision: $Revision: 26318 $
Author(s): Curney (asml8ed@gmail.com)
Inspired by: ChatFrameExtender by Satrina
Website: http://www.wowace.com/files/index.php?path=Prat/
Documentation: http://www.wowace.com/wiki/Prat/Integrated_Modules#FontSize
SVN: http://svn.wowace.com/wowace/trunk/Prat/
Description: Module for Prat that sets the font size for chat windows (default=12).
Dependencies: Prat
]]

local L = AceLibrary("AceLocale-2.2"):new("PratFontSize")

L:RegisterTranslations("enUS", function() return {
    ["FontSize"] = true,
    ["Chat window font size options."] = true,
    ["Set Separately"] = true,
    ["Toggle setting options separately for each chat window."] = true,
    ["Set Font Size"] = true,
    ["Set font size for each chat window."] = true,
    ["Chat%dFontSize"] = true,
    ["Set font size for chat window %d."] = true,
    ["Set All Font Sizes"] = true,
    ["Set font size for all chat windows."] = true,
    ["linespacing_name"] = "Line Spacing",
    ["linespacing_desc"] = "Add additional space between each chat line.",
    ["autorestore_name"] = "Automatic Restore",
    ["autorestore_desc"] = "Workaround a Blizzard bug which changes the font size when you open a system menu.",
    ["textenh_name"] = "Text Enhancements",
    ["textenh_desc"] = "Other enchancements to the chat text appearance.",
} end)

L:RegisterTranslations("frFR", function() return {
    ["FontSize"] = "Taille du texte",
    ["Chat window font size options."] = "Options des tailles des textes",
    ["Set Separately"] = "Fen\195\170tre par fen\195\170tre",
    ["Toggle setting options separately for each chat window."] = "Changer les options fen\195\170tre par fen\195\170tre ou globalement.",
    ["Set Font Size"] = "Changer la taille des textes",
    ["Set font size for each chat window."] = "Change la taille du texte pour chaque fen\195\170tre.",
    ["Chat%dFontSize"] = "Taille du texte, fen\195\170tre %d",
    ["Set font size for chat window %d."] = "Change la taille du texte pour la fen\195\170tre %d.",
    ["Set All Font Sizes"] = "Changer la taille de tous les textes",
    ["Set font size for all chat windows."] = "R\195\168gle la taille des textes de chaque fen\195\170tre.",
    ["linespacing_name"] = "Interligne",
    ["linespacing_desc"] = "Ajoute un espace suppl\195\169mentaire entre chaque ligne.",
    ["textenh_name"] = "Am\195\169liorations du texte",
    ["textenh_desc"] = "Autres am\195\169liorations de l'apparence du texte.",
} end)

-- L:RegisterTranslations("zhCN", function() return {
-- 	["FontSize"] = "字体尺寸",
-- 	["Chat window font size options."] = "聊天窗口字体尺寸选项。",
-- 	["SetMode"] = "设置模式",
-- 	["Choose mode for setting font sizes."] = "选择设置字体尺寸模式。",
-- 	["Set"] = "设置",
-- 	["Set the font size for each window."] = "为每一聊天窗口设置字体尺寸。",
--     ["Chat%dFontSize"] = "窗口 %d 字体尺寸",
--     ["Sets font size for chat window %d."] = "设置聊天窗口 %d 字体尺寸。",
-- 	["SetAll"] = "所有尺寸",
-- 	["Sets font size for all chat windows."] = "设置所有聊天窗口字体尺寸。",
-- } end)

L:RegisterTranslations("koKR", function() return {
    ["FontSize"] = "글꼴크기",
    ["Chat window font size options."] = "대화창 글꼴 크기를 설정합니다.",
    ["Set Separately"] = "구분 설정",
    ["Toggle setting options separately for each chat window."] = "각각의 대화창에 구분 설정을 사용합니다.",
    ["Set Font Size"] = "글꼴 크기 설정",
    ["Set font size for each chat window."] = "각각의 대화창에 대한 글꼴 크기를 설정합니다.",
    ["Chat%dFontSize"] = "대화창%d글꼴크기",
    ["Set font size for chat window %d."] = "대화창 %d의 글꼴 크기를 설정합니다.",
    ["Set All Font Sizes"] = "모든 글꼴 크기 설정",
    ["Set font size for all chat windows."] = "모든 대화창의 글꼴 크기를 설정합니다.",
    ["linespacing_name"] = "줄 간격",
    ["linespacing_desc"] = "각 대화글의 줄 간격을 조절합니다.",
    ["autorestore_name"] = "자동 복원",
    ["autorestore_desc"] = "시스템 메뉴 열 때 글꼴 크기가 변경되는 게임 버그를 수정합니다.",
    ["textenh_name"] = "대화글 향상",
    ["textenh_desc"] = "대화글의 외형을 향상시킵니다.",
} end)

-- L:RegisterTranslations("zhTW", function() return {
--     ["FontSize"] = "字體大小",
--     ["Chat window font size options."] = "聊天窗口字體大小選項。",
--     ["SetMode"] = "設定模式",
--     ["Choose mode for setting font sizes."] = "選擇設定字體大小模式。",
--     ["Set"] = "設定",
--     ["Set the font size for each window."] = "為每一聊天視窗設置字體大小。",
--     ["Chat%dFontSize"] = "視窗 %d 字體大小",
--     ["Sets font size for chat window %d."] = "設定聊天視窗 %d 字體大小。",
--     ["SetAll"] = "所有視窗",
--     ["Sets font size for all chat windows."] = "設定所有聊天視窗字體大小。",
-- } end)

L:RegisterTranslations("esES", function() return {
    ["FontSize"] = "Tama\195\177o de Fuente",
    ["Chat window font size options."] = "Opciones del tama\195\177o de fuente en las ventanas de chat",
    ["Set Separately"] = "Por separado",
    ["Toggle setting options separately for each chat window."] = "Determina si se establecen las opciones para cada ventana de chat por separado",
    ["Set Font Size"] = "Tama\195\177o de Fuente",
    ["Set font size for each chat window."] = "Establece el tama\195\177o de fuente para cada ventana de chat",
    ["Chat%dFontSize"] = "Tama\195\177o de Fuente del Chat %d",
    ["Set font size for chat window %d."] = "Establece el tama\195\177o de fuente para la ventana de chat %d",
    ["Set All Font Sizes"] = "Todos los tama\195\177os de fuente",
    ["Set font size for all chat windows."] = "Establece el tama\195\177o de fuente para todas las ventanas de chat",
    ["linespacing_name"] = "Espaciado de l\195\173nea",
    ["linespacing_desc"] = "A\195\177ade un espacio adicional entre cada l\195\173nea de chat.",
    ["autorestore_name"] = "Recuperaci\195\179n Autom\195\161tica",
    ["autorestore_desc"] = "Solucina temporalmente un bug de Blizzard que cambia el tama\195\177o de fuente cuando abres el men\195\186 de sistema.",
    ["textenh_name"] = "Mejoras de Texto",
    ["textenh_desc"] = "Otras mejoras a la apariencia del texto de chat",
} end)

L:RegisterTranslations("deDE", function() return {
	["FontSize"] = "Schriftgr\195\182\195\159e",
	["Chat window font size options."] = "Chatfenster Schriftgr\195\182\195\159en Optionen.",
	["Set Separately"] = "Seperat einstellen",
	["Toggle setting options separately for each chat window."] = "Aktiviere die Einstellungsoptionen f\195\188r jedes Chatfenster seperat.",
	["Set Font Size"] = "Setze Schriftgr\195\182\195\159e",
	["Set font size for each chat window."] = "Setze Schriftgr\195\182\195\159e f\195\188r jedes Fenster.",
	["Chat%dFontSize"] = "Chat %d Schriftgr\195\182\195\159e",
	["Set font size for chat window %d."] = "Setze Schrifgr\195\182\195\159e f\195\188r Chatfenster %d.",
	["Set All Font Sizes"] = "Setze Alle Schriftgr\195\182\195\159en",
	["Set font size for all chat windows."] = "Setze Schriftgr\195\182\195\159e f\195\188r alle Chatfenster.",
	["linespacing_name"] = "Zeilen Anstände",
	["linespacing_desc"] = "F\195\188ge zus\195\164tzliche Leerstelle zwischen jede Chatzeile.",
	["textenh_name"] = "Text Verbesserungen",
	["textenh_desc"] = "Weitere Verbeserrungen f\195\188r das Chattext Erscheinungsbild.",
} end)

Prat_FontSize = Prat:NewModule(Prat:RequestModuleName("PratFontSize"))

Prat_FontSize.revision = tonumber(string.sub("$Revision: 26318 $", 12, -3))

-- Prat categories relevant to this module. list at: http://groups.google.com/group/wow-prat/web/prat-categories
local cat = Prat.Categories
Prat_FontSize.Categories	= { cat.TEXT, cat.FORMATTING }	-- eg { cat.TEXT, cat.INFO }

Prat_FontSize.defaultDB = {
    on = true,
    sizeseparate = true,
    size = {12, 12, 12, 12, 12, 12, 12},
    sizeall = 12,
    linespacing = 0,
    autorestore = false, 
}

local function range_opt(vmin, vmax, vstep)
    return { type = "range", min = vmin, max = vmax, step = vstep }
end
Prat_FontSize.toggleOptions = { autorestore = { type = "toggle", order = 195, set = "SetAutoRestore"}, textenh_pass = {linespacing = range_opt(0,30,1)}}
Prat_FontSize.moduleOptions = {}

function Prat_FontSize:GetModuleOptions()
    self.moduleOptions = {
        name = L["FontSize"],
        desc = L["Chat window font size options."],
        type = "group",
        args = {
            separate = {
                name = L["Set Separately"],
                desc = L["Toggle setting options separately for each chat window."],
                type = "toggle",
                order = 170,
                get = function() return self.db.profile.sizeseparate end,
                set = function(v) self.db.profile.sizeseparate = v end,
            },
            set = {
                name = L["Set Font Size"],
                desc = L["Set font size for each chat window."],
                type = "group",
                order = 180,
                args = {
                    chat1 = {
                        name = string.format(L["Chat%dFontSize"], 1),
                        desc = string.format(L["Set font size for chat window %d."], 1),
                        type = "range",
                        get = function() return self.db.profile.size[1] end,
                        set = function(v) self.db.profile.size[1] = v; self:SetSize(1,v) end,
                        min = 4,
                        max = 22,
                        step = 1
                    },
                    chat2 = {
                        name = string.format(L["Chat%dFontSize"], 2),
                        desc = string.format(L["Set font size for chat window %d."], 2),
                        type = "range",
                        get = function() return self.db.profile.size[2] end,
                        set = function(v) self.db.profile.size[2] = v; self:SetSize(2,v) end,
                        min = 4,
                        max = 22,
                        step = 1
                    },
                    chat3 = {
                        name = string.format(L["Chat%dFontSize"], 3),
                        desc = string.format(L["Set font size for chat window %d."], 3),
                        type = "range",
                        get = function() return self.db.profile.size[3] end,
                        set = function(v) self.db.profile.size[3] = v; self:SetSize(3,v) end,
                        min = 4,
                        max = 22,
                        step = 1
                    },
                    chat4 = {
                        name = string.format(L["Chat%dFontSize"], 4),
                        desc = string.format(L["Set font size for chat window %d."], 4),
                        type = "range",
                        get = function() return self.db.profile.size[4] end,
                        set = function(v) self.db.profile.size[4] = v; self:SetSize(4,v) end,
                        min = 4,
                        max = 22,
                        step = 1
                    },
                    chat5 = {
                        name = string.format(L["Chat%dFontSize"], 5),
                        desc = string.format(L["Set font size for chat window %d."], 5),
                        type = "range",
                        get = function() return self.db.profile.size[5] end,
                        set = function(v) self.db.profile.size[5] = v; self:SetSize(5,v) end,
                        min = 4,
                        max = 22,
                        step = 1
                    },
                    chat6 = {
                        name = string.format(L["Chat%dFontSize"], 6),
                        desc = string.format(L["Set font size for chat window %d."], 6),
                        type = "range",
                        get = function() return self.db.profile.size[6] end,
                        set = function(v) self.db.profile.size[6] = v; self:SetSize(6,v) end,
                        min = 4,
                        max = 22,
                        step = 1
                    },
                    chat7 = {
                        name = string.format(L["Chat%dFontSize"], 7),
                        desc = string.format(L["Set font size for chat window %d."], 7),
                        type = "range",
                        get = function() return self.db.profile.size[7] end,
                        set = function(v) self.db.profile.size[7] = v; self:SetSize(7,v) end,
                        min = 4,
                        max = 22,
                        step = 1
                    }
                },
                disabled = function() if not self.db.profile.sizeseparate then return true else return false end end,
            },
            setall = {
                name = L["Set All Font Sizes"],
                desc = L["Set font size for all chat windows."],
                type = "range",
                order = 190,
                get = function() return self.db.profile.sizeall end,
                set = function(v)
                    self.db.profile.sizeall = v
                    for i=1,NUM_CHAT_WINDOWS do
                        self.db.profile.size[i] = v
                        self:SetSize(i,self.db.profile.size[i])
                    end
                end,
                min = 4,
                max = 22,
                step = 1,
                disabled = function() if self.db.profile.sizeseparate then return true else return false end end,
            },
        },
    }
      	return self.moduleOptions
end



function Prat_FontSize:OnModuleEnable()
    self:ConfigureAllChatFrames()
    -- This will resolve the issue where, when font sizes are set smaller than 12,
    -- the size resets to 12 when closing UIOptionsFrame.
    self:SetAutoRestore(self.db.profile.autorestore)
end


function Prat_FontSize:SetAutoRestore(val)
    self.db.profile.autorestore = val
    if self.db.profile.autorestore then
    	if not self:IsHooked("UpdateMicroButtons") then self:SecureHook("UpdateMicroButtons", "ConfigureAllChatFrames") end
    else
    	if self:IsHooked("UpdateMicroButtons") then self:Unhook("UpdateMicroButtons") end
    end
end    



function Prat_FontSize:OnModuleDisable()
    for i=1,NUM_CHAT_WINDOWS do
        self:SetSize(i,12)
        self:SetSpacing(i, 0)
    end
end

function Prat_FontSize:ModuleSetOption(name, val)
    self.super.ModuleSetOption(self, name, val)
    
    self:ConfigureAllChatFrames()
end

--[[------------------------------------------------
	Core Functions
------------------------------------------------]]--

function Prat_FontSize:ConfigureAllChatFrames()
    for i = 1,NUM_CHAT_WINDOWS do

        self:SetSize(i, self.db.profile.size[i])
        self:SetSpacing(i, self.db.profile.linespacing)
    end
end


function Prat_FontSize:SetSize(id, size)
--    local font = getglobal("ChatFrame"..id):GetFont()
--    getglobal("ChatFrame"..id):SetFont(font,size)

    FCF_SetChatWindowFontSize(getglobal("ChatFrame"..id), size)
end

function Prat_FontSize:SetSpacing(id, space)
    local cf = getglobal("ChatFrame"..id)
    getglobal("ChatFrame"..id):SetSpacing(space)
end