/***************************************************************************
  
  gb.qt.h
  
  (c) Benoît Minisini <benoit.minisini@gambas-basic.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.
  
  This program 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.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  MA 02110-1301, USA.
  
***************************************************************************/

#ifndef __GB_QT_H
#define __GB_QT_H

#include "gambas.h"
#include "gb.image.h"

#ifdef OS_MACOSX
//#define NO_X_WINDOW 1
#endif

#include <QWidget>
#include <QFont>
#include <QString>
#include <QPixmap>
#include <QImage>
#include <QEvent>
#include <QPainter>
#include <QPainterPath>
#include <QBrush>
#include <QPen>
#include <QTransform>
#include <QColor>
#include <QMenu>

#define QT_INTERFACE_VERSION 1

#if QT_VERSION >= 0x060000
#define QT6 1
#define QT5 1
#define QT_NAME "gb.qt6"
#elif QT_VERSION >= 0x050000
#define QT5 1
#define QT_NAME "gb.qt5"
#else
#define QT_NAME "gb.qt4"
#endif

#define TO_QSTRING(_str) (QString::fromUtf8((const char *)(_str)))

#ifdef DO_NOT_USE_QT_INTERFACE

	#define TO_UTF8(_str) QT_ToUtf8(_str)
	#define LAST_UTF8_LENGTH() QT_GetLastUtf8Length()
	#define NEW_STRING(_str) QT_NewString(_str)
	#define RETURN_NEW_STRING(_str) QT_ReturnNewString(_str)
	#define GET_SENDER() void *_object = QT_GetObject((QWidget*)sender())

#else

	#define TO_UTF8(_str) QT.ToUtf8(_str)
	#define LAST_UTF8_LENGTH() QT.GetLastUtf8Length()
	#define NEW_STRING(_str) QT.NewString(_str)
	#define RETURN_NEW_STRING(_str) QT.ReturnNewString(_str)
	#define GET_SENDER() void *_object = QT.GetObject((QWidget*)sender())

#endif

#define QSTRING_ARG(_arg) (QString::fromUtf8((const char *)(VARG(_arg).addr + VARG(_arg).start), VARG(_arg).len))
#define QSTRING_PROP() (QString::fromUtf8((const char *)(VPROP(GB_STRING).addr + VPROP(GB_STRING).start), VPROP(GB_STRING).len))

#define TO_QCOLOR(_col) QColor::fromRgba((QRgb)((_col) ^ 0xFF000000))
	
typedef
	struct {
		GB_BASE ob;
		QWidget *widget;
		void *ext;
		struct {
			unsigned deleted : 1;
			unsigned scrollview : 1;           // inherits QScrollView
			unsigned expand : 1;               //
			unsigned ignore : 1;               //
			unsigned visible : 1;              //
			unsigned auto_fill_background : 1;
			unsigned fill_background : 1;      //
			unsigned no_background : 1;         //

			unsigned shown : 1;                // for containers
			unsigned tracking : 1;
			unsigned old_tracking : 1;
			unsigned grab : 1;
			unsigned dragging: 1;              //
			unsigned no_tab_focus : 1;
			unsigned inside : 1;               //
			unsigned inside_later : 1;
			
			unsigned use_tablet : 1;           //
			unsigned has_action : 1;
			unsigned drop : 1;
			unsigned resized : 1;
			unsigned wheel : 1;                // eat wheel events
			unsigned design : 1;               //
			unsigned design_ignore : 1;        //
			unsigned no_design : 1;

			unsigned orientation : 1;          // for scrollbars & similar widgets
			unsigned inverted : 1;             // if layout has been inverted
			unsigned direction : 2;            // text direction
			unsigned user : 1;                 // UserControl or UserContainer
			unsigned no_animation : 1;         // enable or disable animations explicitly
			unsigned eat_keyboard : 1;         // if keyboard event must not be propagated
			} flag;
		char *name;
		void *font;
		uint key;
		}
	QT_WIDGET;

typedef
	struct {
		QT_WIDGET widget;
		QWidget *container;
		int arrangement;
		}
	QT_CONTAINER;

typedef
	struct {
		GB_BASE ob;
		QFont *font;
		void *func;
		void *object;    
	}
	QT_FONT;

typedef
	void *QT_PICTURE;

typedef
	void *QT_IMAGE;

typedef
	struct {
		QPainter *p;
		QPainter *pm;
		QBitmap *mask;
		int fg;
		int fillColor;
		}
	QT_DRAW_EXTRA;

typedef
	void (*QT_FONT_FUNC)(QFont &, void *);

typedef
	void (*QT_COLOR_FUNC)(void *);

typedef
	struct {
		intptr_t version;
		void (*InitEventLoop)(void);
		void (*Init)(void);
		void (*InitWidget)(QWidget *, void *, int);
		void *(*GetObject)(QWidget *);
		QWidget *(*GetContainer)(void *);
		void (*BorderProperty)(void *, void *);
		void (*FullBorderProperty)(void *, void *);
		void (*ScrollBarProperty)(void *, void *);
		void (*FontProperty)(void *, void *);
		QT_FONT *(*CreateFont)(const QFont &, QT_FONT_FUNC, void *);
		void (*SetFont)(QT_FONT_FUNC, void*, void *);
		QT_PICTURE (*CreatePicture)(const QPixmap &);
		//QMimeSourceFactory *(*MimeSourceFactory)(void);
		QPixmap *(*GetPixmap)(QT_PICTURE);
		//QT_IMAGE (*CreateImage)(const QImage &);
		const char *(*ToUtf8)(const QString &);
		int (*GetLastUtf8Length)();
		char *(*NewString)(const QString &);
		void (*ReturnNewString)(const QString &);
		bool (*EventFilter)(QEvent *);
		int (*Alignment)(int, int, bool);
		void (*Link)(QObject *, void *);
		void *(*GetLink)(QObject *);
		QPainter *(*GetCurrentPainter)();
		uint (*GetBackgroundColor)(void *);
		void (*MouseProperty)(void *, void *);
		QT_COLOR_FUNC (*AfterSetColor)(QT_COLOR_FUNC);
		int (*GetDesktopScale)(void);
		void *(*GetPrinter)(void *);
		void *_null;
		}
	QT_INTERFACE;


#define QT_WIDGET_PROPERTIES "*"

#define QT_EVENT_FIRST ((QEvent::Type)(QEvent::User + 10))

#endif

