#include "mzstdchartsCommon.h"

#ifdef MZSTDCHARTS_DLL
#ifdef MZSTDCHARTS_INTERNAL
// Memory management functions we are compiling only if dll library is to be build
VOID* __cdecl operator new(size_t size) {
    HANDLE heap = GetProcessHeap();
    if (!heap) {
        return NULL;
    }
    VOID* pv = HeapAlloc(heap, 0, size);
    if (!pv) {
        return NULL;
    }
    return pv;
}

VOID* __cdecl operator new(size_t size, CHAR ch) {
    HANDLE heap = GetProcessHeap();
    if (!heap) {
        return NULL;
    }
    VOID* pv = HeapAlloc(heap, 0, size);
    if (!pv) {
        return NULL;
    }
    memset(pv, ch, size);
    return pv;
}

VOID* __cdecl operator new[](size_t size) {
    HANDLE heap = GetProcessHeap();
    if (!heap) {
        return NULL;
    }
    VOID* pv = HeapAlloc(heap, 0, size);
    if (!pv) {
        return NULL;
    }
    return pv;
}

VOID  __cdecl operator delete(VOID* p) {
    HANDLE heap = GetProcessHeap();
    if (!heap) return;
    HeapFree(heap, 0, p);
}

VOID  __cdecl operator delete[](VOID* p) {
    HANDLE heap = GetProcessHeap();
    if (!heap) return;
    HeapFree(heap, 0, p);
}
#endif // MZSTDCHARTS_DLL
#endif // MZSTDCHARTS_INTERNAL

#include "mzStdObject.h"
#include "mzGuiContainer.h"
#include "mzLayoutFile.h"
#include "mzChart.h"
#include "mzMessageBox.h"


VOID WINAPI initMzstdchartsLibrary() {
}

VOID WINAPI cleanupMzstdchartsLibrary() {
}

VOID WINAPI hello() {
    InformationBox(L"Hello.");
}

VOID WINAPI addGuiControl(CLayoutFile* loader, CStdObject* item, CGuiContainer* parent) {
    if (!item || !parent) {
        return;
    }

    CString type = item->getAsString(L"type");
    if (type == L"chart") {
        CString name = item->getAsString(L"name");
        if (name != COMMON_EMPTY_STRING) {
            CString relativeControl, relativePosition;
            LONG x, y, width, height;

            loader->getDimensionProperties(item, &x, &y, &width, &height);
            loader->getRelativePositionProperties(item, &relativeControl, &relativePosition);

            CChart* gc = new CChart(parent, name, x, y, width, height);
            if (gc) {
                loader->doRelativePositioning(gc, relativeControl.getBuffer(), relativePosition.getBuffer());
                loader->addControl(gc, parent);
                InformationBox(L"Added");
            }
        }
    }
}
