/*--------------------------------------------------------------------------
  Project:  Manual 2D Library - m2d
  File:     m2d_manualViewer.cpp

  Copyright (C)2007-2009 Nintendo  All Rights Reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law. They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.

  $Revision: 1758 $
 *-------------------------------------------------------------------------*/

#include <nitro.h>
#include <nnsys/fnd.h>
#include <nnsys/misc.h>
#include <ntmv/m2d/manualViewer.h>
#include <ntmv/m2d/manualView.h>
#include <new>

/*---------------------------------------------------------------------------*
  Name:         NTMV_M2dInitForFilePath

  Description:  Performs initialization based on file paths for the manual data.

  Arguments:    pManualViewerInfo: Pointer to an NTMV Manual Viewer information structure.
                pAllocator:        Pointer to an NNS allocator.
                manualFilePath:    Path to the manual archive file.
                                   This file must be compressed by the compBLZ TWL-SDK tool.
                                   
                uiResFilePath:     Path to the resource file for the user interface.
                                   This file must be compressed by the compBLZ TWL-SDK tool.
                                   
                fonts:             Array of pointers to internal system fonts (TWL bitmap fonts).
                                   
                                   Pointers to font data must be set in the following order.
                                   
                                       0 ... Large font
                                       1 ... Medium font
                                       2 ... Small font

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
NTMV_M2dInitForFilePath(
    NTMVM2dManualViewerInfo*    pManualViewerInfo,
    NNSFndAllocator*            pAllocator,
    const char*                 manualFilePath,
    const char*                 uiResFilePath,
    const NNSG2dFont *const     fonts[]
)
{
    const u16 lang = 0;

    NTMV_M2dInitForFilePathEx(pManualViewerInfo, pAllocator, manualFilePath, uiResFilePath, fonts, lang);
}

/*---------------------------------------------------------------------------*
  Name:         NTMV_M2dInitForFilePathEx

  Description:  Performs initialization based on file paths for the manual data.

  Arguments:    pManualViewerInfo: Pointer to an NTMV Manual Viewer information structure.
                pAllocator:        Pointer to an NNS allocator.
                manualFilePath:    Path to the manual archive file.
                                   This file must be compressed by the compBLZ TWL-SDK tool.
                                   
                uiResFilePath:     Path to the resource file for the user interface.
                                   This file must be compressed by the compBLZ TWL-SDK tool.
                                   
                fonts:             Array of pointers to internal system fonts (TWL bitmap fonts).
                                   
                                   Pointers to font data must be set in the following order.
                                   
                                       0 ... Large font
                                       1 ... Medium font
                                       2 ... Small font
                lang:              Language to use to display the manual.
                                   The ISO 639 language code (two alphabetic characters) should be specified in the upper and lower bytes using character codes.
                                   

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
NTMV_M2dInitForFilePathEx(
    NTMVM2dManualViewerInfo*    pManualViewerInfo,
    NNSFndAllocator*            pAllocator,
    const char*                 manualFilePath,
    const char*                 uiResFilePath,
    const NNSG2dFont *const     fonts[],
    u16                         lang
)
{
    NNS_NULL_ASSERT(pManualViewerInfo);

    void *const manViewMem = NNS_FndAllocFromAllocator(pAllocator, sizeof(ntmv::m2d::ManualView));
    NNS_NULL_ASSERT(manViewMem);
    ntmv::m2d::ManualView* pManView = new (manViewMem) ntmv::m2d::ManualView;

    pManView->Init(pAllocator, manualFilePath, uiResFilePath, fonts, lang);

    pManualViewerInfo->viewerObject = pManView;
}

/*---------------------------------------------------------------------------*
  Name:         NTMV_M2dInitForMem

  Description:  Performs initialization based on manual data.

  Arguments:    pManualViewerInfo: Pointer to an NTMV Manual Viewer information structure.
                pAllocator:        Pointer to an NNS allocator.
                manualArcData:     Manual archive data.
                                   This data must be uncompressed.
                uiResource:        Resource archive data for the user interface.
                                   This data must be expanded to a 32-byte boundary.
                fonts:             Array of pointers to internal system fonts (TWL bitmap fonts).
                                   
                                   Pointers to font data must be set in the following order.
                                   
                                       0 ... Large font
                                       1 ... Medium font
                                       2 ... Small font

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
NTMV_M2dInitForMem(
    NTMVM2dManualViewerInfo*    pManualViewerInfo,
    NNSFndAllocator*            pAllocator,
    void*                       manualArcData,
    void*                       uiResource,
    const NNSG2dFont *const     fonts[]
)
{
    const u16 lang = 0;

    NTMV_M2dInitForMemEx(pManualViewerInfo, pAllocator, manualArcData, uiResource, fonts, lang);
}

/*---------------------------------------------------------------------------*
  Name:         NTMV_M2dInitForMemEx

  Description:  Performs initialization based on manual data.

  Arguments:    pManualViewerInfo: Pointer to an NTMV Manual Viewer information structure.
                pAllocator:        Pointer to an NNS allocator.
                manualArcData:     Manual archive data.
                                   This data must be uncompressed.
                uiResource:        Resource archive data for the user interface.
                                   This data must be expanded to a 32-byte boundary.
                fonts:             Array of pointers to internal system fonts (TWL bitmap fonts).
                                   
                                   Pointers to font data must be set in the following order.
                                   
                                       0 ... Large font
                                       1 ... Medium font
                                       2 ... Small font
                lang:              Language to use to display the manual.
                                   The ISO 639 language code (two alphabetic characters) should be specified in the upper and lower bytes using character codes.
                                   

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
NTMV_M2dInitForMemEx(
    NTMVM2dManualViewerInfo*    pManualViewerInfo,
    NNSFndAllocator*            pAllocator,
    void*                       manualArcData,
    void*                       uiResource,
    const NNSG2dFont *const     fonts[],
    u16                         lang
)
{
    NNS_NULL_ASSERT(pManualViewerInfo);

    void *const manViewMem = NNS_FndAllocFromAllocator(pAllocator, sizeof(ntmv::m2d::ManualView));
    NNS_NULL_ASSERT(manViewMem);
    ntmv::m2d::ManualView* pManView = new (manViewMem) ntmv::m2d::ManualView;

    pManView->Init(pAllocator, manualArcData, uiResource, fonts, lang);

    pManualViewerInfo->viewerObject = pManView;
}

/*---------------------------------------------------------------------------*
  Name:         NTMV_M2dRun

  Description:  Runs the Viewer.
                Control will not return until the Viewer is closed.

  Arguments:    pManualViewerInfo: Pointer to an NTMV Manual Viewer information structure.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
NTMV_M2dRun(NTMVM2dManualViewerInfo* pManualViewerInfo)
{
    NNS_NULL_ASSERT(pManualViewerInfo);

    ntmv::m2d::ManualView* pManView = (ntmv::m2d::ManualView*)pManualViewerInfo->viewerObject;

    pManView->Run();    // Manual View loop
}

/*---------------------------------------------------------------------------*
  Name:         NTMV_M2dFinalize

  Description:  Performs shutdown.

  Arguments:    pManualViewerInfo: Pointer to an NTMV Manual Viewer information structure.
                pAllocator:        Pointer to an NNS allocator.

  Returns:      None.
 *---------------------------------------------------------------------------*/
void
NTMV_M2dFinalize(
    NTMVM2dManualViewerInfo*    pManualViewerInfo,
    NNSFndAllocator*            pAllocator
)
{
    NNS_NULL_ASSERT(pManualViewerInfo);

    ntmv::m2d::ManualView* pManView = (ntmv::m2d::ManualView*)pManualViewerInfo->viewerObject;

    pManView->~ManualView();

    NNS_FndFreeToAllocator(pAllocator, pManView);

    pManualViewerInfo->viewerObject = NULL;
}

