/*
 * ************************************************************************************************************
 * 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: mzstdwin32
 * Version: 1.05
 * First release date:   2010-11-08
 * Current release date: 2011-03-04
 *
 *
 * This file is part of mzstdwin32 library
 * 
 * mzstdwin32 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.
 * 
 * mzstdwin32 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 "mzstdwin32Common.h"
#include "mzTooltip.h"
#include "mzMessageBox.h"


VOID CTooltip::_commonInit(HWND hwndOwner, CString message) {
    this->setRealClassName(L"CTooltip");
    this->_hwndOwner = hwndOwner;
    this->_message = message;

    this->_icex.dwSize = sizeof(this->_icex);
    this->_icex.dwICC  = ICC_BAR_CLASSES;
    this->_hwnd = CreateWindow(TOOLTIPS_CLASS, this->_message.toWCHAR(), WS_POPUP,
                               CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                               NULL, (HMENU)NULL, GetInstance(), NULL);
                               
    if (this->_hwnd) {
        // Prepare TOOLINFO structure for use as tracking ToolTip.
        this->_tinfo.cbSize = sizeof(TOOLINFO);
        this->_tinfo.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE;
        this->_tinfo.hwnd   = hwndOwner;
        this->_tinfo.uId    = (UINT)this->_hwndOwner;
        this->_tinfo.hinst  = NULL;
        this->_tinfo.lpszText  = LPSTR_TEXTCALLBACK;
        this->_tinfo.rect.left = this->_tinfo.rect.top = this->_tinfo.rect.bottom = this->_tinfo.rect.right = 0;
        if (SendMessage(this->_hwnd, TTM_ADDTOOL, 0, (LPARAM)&this->_tinfo)) {
            // ERROR! We drop off here!
            return;
        }
        ShowDetailedLastSystemError();
        DestroyWindow(this->_hwnd);
    }
    
    ErrorBox(L"Couldn't create the ToolTip control.");
}

CTooltip::CTooltip(HWND hwndOwner, CString message) {
    this->_commonInit(hwndOwner, message);
}

CTooltip::~CTooltip() {
    this->_cleanup();
}

VOID CTooltip::_cleanup() {
    if (this->_hwnd) {
        DestroyWindow(this->_hwnd);
        this->_hwnd = NULL;
    }
}

VOID CTooltip::show() {
    if (this->_hwnd) {
        ShowWindow(this->_hwnd, SW_SHOW);
    }
}
