/**
 * Scout - The Amiga System Monitor
 *
 *------------------------------------------------------------------
 *
 * 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
 * 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 the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * You must not use this source code to gain profit of any kind!
 *
 *------------------------------------------------------------------
 *
 * @author Andreas Gelhausen
 * @author Richard Körber <rkoerber@gmx.de>
 */

#include "system_headers.h"

static void SetDetails( struct IClass *cl,
                        Object *obj,
                        struct FontEntry *fe )
{
    struct FontsDetailWinData *fdwd = INST_DATA(cl, obj);
    UBYTE *tmp;
    static UBYTE fontstyle[] = { FSF_UNDERLINED, FSF_BOLD, FSF_ITALIC, FSF_EXTENDED, FSF_COLORFONT, FSF_TAGGED, 0 };
    static UBYTE *fontstyletext[] = { "UNDERLINED", "BOLD", "ITALIC", "EXTENDED", "COLORFONT", "TAGGED" };
    static UBYTE fontflags[] = { FPF_ROMFONT, FPF_DISKFONT, FPF_REVPATH, FPF_TALLDOT, FPF_WIDEDOT, FPF_PROPORTIONAL, FPF_DESIGNED, FPF_REMOVED, 0 };
    static UBYTE *fontflagstext[] = { "ROMFONT", "DISKFONT", "REVPATH", "TALLDOT", "WIDEDOT", "PROPORTIONAL", "DESIGNED", "REMOVED" };

    if (tmp = tbAllocVecPooled(globalPool, PATH_LENGTH)) {
        struct TextFont *tf = fe->fe_Addr;

        MySetContentsHealed(fdwd->fdwd_FontMoreText[0], "%s", tf->tf_Message.mn_Node.ln_Name);
        MySetContents(fdwd->fdwd_FontMoreText[1], MUIX_R "$%08lx\n" MUIX_R "%lD\n" MUIX_R "%lD\n" MUIX_R "%lD", tf, tf->tf_YSize, tf->tf_LoChar, tf->tf_Baseline);

        MySetContents(fdwd->fdwd_FontMoreText[2], MUIX_R "%lD\n" MUIX_R "%lD\n" MUIX_R "%lD\n" MUIX_R "%lD", tf->tf_Accessors, tf->tf_XSize, tf->tf_HiChar, tf->tf_BoldSmear);

        tmp[0] = 0x00;
        if (tf->tf_Style) {
            ULONG i;

            i = 0;
            while (fontstyle[i]) {
                if (tf->tf_Style & fontstyle[i]) {
                    if (strlen(tmp)) _strcatn(tmp, ", ", PATH_LENGTH);
                    _strcatn(tmp, fontstyletext[i], PATH_LENGTH);
                }
                i++;
            }
        } else {
            _strcatn(tmp, "NORMAL", PATH_LENGTH);
        }
        MySetContents(fdwd->fdwd_FontMoreText[3], tmp);

        tmp[0] = 0x00;
        if (tf->tf_Flags) {
            ULONG i;

            i = 0;
            while (fontflags[i]) {
                if (tf->tf_Flags & fontflags[i]) {
                    if (strlen(tmp)) _strcatn(tmp, ", ", PATH_LENGTH);
                    _strcatn(tmp, fontflagstext[i], PATH_LENGTH);
                }
                i++;
            }
        }
        MySetContents(fdwd->fdwd_FontMoreText[4], tmp);

        set(fdwd->fdwd_FontMoreExample, MUIA_FontDisplay_Font, tf);
        set(fdwd->fdwd_FontMoreExample, MUIA_FontDisplay_Text, "¤˘ the quick brown fox jumps over the lazy dog");

        set(obj, MUIA_Window_Title, MyGetChildWindowTitle("FONT", fe->fe_Name, fdwd->fdwd_Title, sizeof(fdwd->fdwd_Title)));

        tbFreeVecPooled(globalPool, tmp);
    }
}

static ULONG __saveds mNew( struct IClass *cl,
                            Object *obj,
                            struct opSet *msg )
{
    APTR fontmoretext[5], fontmoreexample;

    if (obj = (Object *)DoSuperNew(cl, obj,
        MUIA_HelpNode, FontsText,
        MUIA_Window_ID, MakeID('.','F','O','N'),
        WindowContents, VGroup,
            Child, HGroup,
                Child, VGroup, MUIA_Group_SameWidth, TRUE,
                    Child, MyLabel2("Name:"),
                    Child, MyLabel2("Address:\ntf_YSize:\ntf_LoChar:\ntf_Baseline:"),
                    Child, MyLabel2("tf_Style:"),
                    Child, MyLabel2("tf_Flags:"),
                End,
                Child, VGroup,
                    Child, fontmoretext[0] = MyTextObject(),
                    Child, HGroup,
                        Child, fontmoretext[1] = MyTextObject(),
                        Child, MyLabel("tf_Accessors:\ntf_XSize:\ntf_HiChar:\ntf_BoldSmear:"),
                        Child, fontmoretext[2] = MyTextObject(),
                    End,
                    Child, fontmoretext[3] = MyTextObject(),
                    Child, fontmoretext[4] = MyTextObject(),
                End,
            End,

            Child, VGroup,
                Child, HGroup,
                    Child, HSpace(0),
                    Child, MyLabel2("Font example"),
                    Child, HSpace(0),
                End,
                Child, fontmoreexample = FontDisplayObject,
                    TextFrame,
                    MUIA_Background, MUII_BACKGROUND,
                End,
            End,
        End,
        TAG_MORE, msg->ops_AttrList))
    {
        struct FontsDetailWinData *fdwd = INST_DATA(cl, obj);
        APTR parent;

        CopyMem(fontmoretext, fdwd->fdwd_FontMoreText, sizeof(fdwd->fdwd_FontMoreText));
        fdwd->fdwd_FontMoreExample = fontmoreexample;

        parent = (APTR)GetTagData(MUIA_Window_ParentWindow, (ULONG)NULL, msg->ops_AttrList);

        DoMethod(parent, MUIM_Window_AddChildWindow, obj);
        DoMethod(obj,    MUIM_Notify,              MUIA_Window_CloseRequest, TRUE, MUIV_Notify_Application, 5, MUIM_Application_PushMethod, parent, 2, MUIM_Window_RemChildWindow, obj);
    }

    return (ULONG)obj;
}

static ULONG __saveds mDispose( struct IClass *cl,
                                Object *obj,
                                struct opSet *msg )
{
    set(obj, MUIA_Window_Open, FALSE);

    return (DoSuperMethodA(cl, obj, msg));
}

static ULONG __saveds mSet( struct IClass *cl,
                            Object *obj,
                            Msg msg )
{
    struct TagItem *tags, *tag;

    for (tags = ((struct opSet *)msg)->ops_AttrList; tag = NextTagItem(&tags); ) {
        switch (tag->ti_Tag) {
            case MUIA_Window_ParentWindow:
                DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, MUIV_Notify_Application, 5, MUIM_Application_PushMethod, (APTR)tag->ti_Data, 2, MUIM_Window_RemChildWindow, obj);
                break;

            case MUIA_FontsDetailWin_Font:
                SetDetails(cl, obj, (struct FontEntry *)tag->ti_Data);
                break;
        }
    }

    return (DoSuperMethodA(cl,obj,msg));
}

ULONG __asm __saveds FontsDetailWinDispatcher( register __a0 struct IClass *cl,
                                               register __a2 Object *obj,
                                               register __a1 Msg msg )
{
    switch (msg->MethodID) {
        case OM_NEW:     return (mNew(cl, obj, (APTR)msg));
        case OM_DISPOSE: return (mDispose(cl, obj, (APTR)msg));
        case OM_SET:     return (mSet(cl, obj, (APTR)msg));
    }

    return (DoSuperMethodA(cl, obj, msg));
}

