/*
 * ************************************************************************************************************
 * Author: Marcin Załęczny
 * Copyright (C)2010 Marcin Załęczny <mzaleczny@hotmail.com>
 * License: The GNU Lesser General Public License, version 3.0 (LGPLv3)
 * Library Name: mzstdgraphics
 * Version: 1.05
 * First release date:   2010-11-08
 * Current release date: 2011-03-04
 *
 *
 * This file is part of mzstdgraphics library
 * 
 * mzstdgraphics library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * mzstdgraphics library 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************************************************
 */

#include "mzCursor.h"
#include "mzstdgraphicsGlobals.h"

CCursor::CCursor() {
    this->setRealClassName(L"CCursor");
    this->_systemCursor = TRUE;
    this->_type = OCR_NORMAL;
    this->_hCursor = GetCursor();
}

CCursor::~CCursor() {
    this->_freeCurrentCursor();
}

VOID CCursor::_freeCurrentCursor() {
    if (!this->_systemCursor && this->_hCursor) {
        DestroyCursor(this->_hCursor);
        this->_hCursor = NULL;
    }
}


VOID CCursor::setSystemCursor(LONG type) {
    if (this->_type == type) {
        CURSORINFO ci;
        ZeroMemory(&ci, sizeof(ci));
        ci.cbSize = sizeof(ci);
        GetCursorInfo(&ci);
        if (ci.hCursor == this->_hCursor) {
            return;
        }
    }

    this->_freeCurrentCursor();

    this->_systemCursor = TRUE;
    switch (type) {
    case OCR_ICON: // OBSOLETE
    case OCR_NORMAL:
        this->_type = OCR_NORMAL;
        this->_hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
        break;
    case OCR_SIZE: // OBSOLETE
    case OCR_SIZEALL:
        this->_type = OCR_SIZEALL;
        this->_hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(OCR_SIZEALL), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
        break;
    case OCR_ICOCUR: // OBSOLETE
    case OIC_WINLOGO:
        this->_type = OIC_WINLOGO;
        this->_hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(OIC_WINLOGO), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
        break;
    case OCR_IBEAM:
    case OCR_WAIT:
    case OCR_CROSS:
    case OCR_UP:
    case OCR_SIZENWSE:
    case OCR_SIZENESW:
    case OCR_SIZEWE:
    case OCR_SIZENS:
    case OCR_NO:
    case OCR_HAND:
    case OCR_APPSTARTING:
        this->_type = type;
        this->_hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(type), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
        break;
    default:
        this->_type = OCR_NORMAL;
        this->_hCursor = (HCURSOR)LoadImage(NULL, MAKEINTRESOURCE(OCR_NORMAL), IMAGE_CURSOR, 0, 0, LR_DEFAULTSIZE | LR_SHARED);
        break;
    }

    SetCursor(this->_hCursor);
}

VOID CCursor::setNormalCursor()        { this->setSystemCursor(OCR_NORMAL); }
VOID CCursor::setIBeamCursor()         { this->setSystemCursor(OCR_IBEAM);  }
VOID CCursor::setWaitCursor()          { this->setSystemCursor(OCR_WAIT);   }
VOID CCursor::setCrossCursor()         { this->setSystemCursor(OCR_CROSS);  }
VOID CCursor::setUpArrowCursor()       { this->setSystemCursor(OCR_UP);     }
VOID CCursor::setSizeCursor()          { this->setSystemCursor(OCR_SIZEALL); }
VOID CCursor::setSizeAllCursor()       { this->setSystemCursor(OCR_SIZEALL); }
VOID CCursor::setSizeNWSECursor()      { this->setSystemCursor(OCR_SIZENWSE); }
VOID CCursor::setSizeNESWCursor()      { this->setSystemCursor(OCR_SIZENESW); }
VOID CCursor::setSizeWECursor()        { this->setSystemCursor(OCR_SIZEWE); }
VOID CCursor::setSizeNSCursor()        { this->setSystemCursor(OCR_SIZENS); }
VOID CCursor::setWinLogoCursor()       { this->setSystemCursor(OIC_WINLOGO); }
VOID CCursor::setNoCursor()            { this->setSystemCursor(OCR_NO); }
VOID CCursor::setHandCursor()          { this->setSystemCursor(OCR_HAND); }
VOID CCursor::setAppStartingCursor()   { this->setSystemCursor(OCR_APPSTARTING); }
VOID CCursor::setHelpCursor()          { this->setSystemCursor(OIC_QUES); }
