/*
 * synergy -- mouse and keyboard sharing utility
 * Copyright (C) 2002 Chris Schoeneman
 * Copyright (C) 2006 Knut St. Osmundsen
 *
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file COPYING that should have accompanied this file.
 *
 * This package 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.
 */

#ifndef CPMCLIPBOARD_H
#define CPMCLIPBOARD_H

#include "IClipboard.h"
#include "stdvector.h"
#define INCL_ERRORS
#define INCL_BASE
#define INCL_PM
#include <os2.h>

class IPMClipboardConverter;

//! Microsoft windows clipboard implementation
class CPMClipboard : public IClipboard {
public:
    CPMClipboard(HWND window);
    virtual ~CPMClipboard();

    //! Test if clipboard is owned by synergy
    static bool         isOwnedBySynergy();

    // IClipboard overrides
    virtual bool        empty();
    virtual void        add(EFormat, const CString& data);
    virtual bool        open(Time) const;
    virtual void        close() const;
    virtual Time        getTime() const;
    virtual bool        has(EFormat) const;
    virtual CString     get(EFormat) const;

private:
    void                clearConverters();

    ULONG               convertFormatToPM(EFormat) const;
    ULONG               convertTextToPM(const CString& data) const;
    CString             convertTextFromPM(ULONG) const;

    static ATOM         getOwnershipFormat();

private:
    typedef std::vector<IPMClipboardConverter*> ConverterList;

    HWND                m_window;
    mutable Time        m_time;
    ConverterList       m_converters;
    static ATOM         s_ownershipFormat;
};

//! Clipboard format converter interface
/*!
This interface defines the methods common to all win32 clipboard format
converters.
*/
class IPMClipboardConverter : public IInterface {
public:
    // accessors

    // return the clipboard format this object converts from/to
    virtual IClipboard::EFormat
                        getFormat() const = 0;

    // return the atom representing the PM clipboard format that
    // this object converts from/to
    virtual ULONG       getPMFormat() const = 0;

    // return the format info flags for the PM clipboard format
    // this object converts from/to
    virtual ULONG       getPMFormatInfo() const = 0;

    // convert from the IClipboard format to the PM clipboard format.
    // the input data must be in the IClipboard format returned by
    // getFormat().  the return data will be in the PM clipboard
    // format returned by getPMFormat().
    virtual ULONG       fromIClipboard(const CString&) const = 0;

    // free PM clipboard data (failure case).
    virtual void        freePMData(ULONG) const = 0;

    // convert from the win32 clipboard format to the IClipboard format
    // (i.e., the reverse of fromIClipboard()).
    virtual CString     toIClipboard(ULONG data) const = 0;
};

#endif
