/*
 * Kangaroo Punch MultiPlayer Game Server Mark II
 * Copyright (C) 2020-2021  Scott Duensing
 *
 * 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 3 of the License, 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, see <https://www.gnu.org/licenses/>.
 *
 */


#ifndef WIDGET_H
#define WIDGET_H


#include "gui.h"


enum WidgetE {
	WIDGET_FLAG_DIRTY = 0,
	WIDGET_FLAG_ALWAYS_PAINT,
	WIDGET_FLAG_ACTIVE,
	WIDGET_FLAG_OWNS_SURFACE,
	WIDGET_FLAG_ALWAYS_RECEIVE_KEYBOARD_EVENTS,
	WIDGET_FLAG_DISABLED,
	WIDGET_FLAG_HIDDEN
};


typedef struct WidgetS WidgetT;
typedef struct WindowS WindowT;


typedef void (*widgetCallback)(struct WidgetS *widget);
typedef void (*widgetDelMethod)(struct WidgetS **widget);
typedef void (*widgetFocusMethod)(struct WidgetS *widget, uint8_t focused);
typedef void (*widgetKeyboardEventMethod)(struct WidgetS *widget, uint8_t ascii, uint8_t extended, uint8_t scancode, uint8_t shift, uint8_t control, uint8_t alt);
typedef void (*widgetPaintMethod)(struct WidgetS *widget, uint8_t enabled, RectT pos);
typedef void (*widgetMouseEventMethod)(struct WidgetS *widget, MouseT *mouse, uint16_t x, uint16_t y, uint8_t event);


typedef struct WidgetS {
	uint8_t                     magic;                // Widget identifier constant
	uint8_t                     flags;                // See above enum
	SurfaceT                   *surface;              // Pointer to compositable surface or NULL
	WindowT                    *window;               // The window holding this widget or NULL
	RectT                       pos;                  // Position of widget on parent
	RectT                       margin;               // Pixels to skip over on each edge when rendering children
	RectT                       clip;                 // Calculated clipping region on surface
	WidgetT                   **children;             // List of children
	WidgetT                    *parent;               // Parent of this widget
	widgetDelMethod             delMethod;            // Delete method
	widgetFocusMethod           focusMethod;          // Focus changed method
	widgetKeyboardEventMethod   keyboardEventMethod;  // Keyboard event handler
	widgetPaintMethod           paintMethod;          // Paint method
	widgetMouseEventMethod      mouseEventMethod;     // Mouse event handler
	void                       *userData;             // Anything the user wants to store
} WidgetT;


uint8_t   widgetEnableGet(WidgetT *widget);
void      widgetEnableSet(WidgetT *widget, uint8_t enabled);
uint16_t  widgetHeightGet(WidgetT *widget);
WidgetT  *widgetInit(WidgetT *widget, uint8_t magic, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t marginX, uint16_t marginY, uint16_t marginX2, uint16_t marginY2);
uint16_t  widgetMarginBottomGet(WidgetT *widget);
uint16_t  widgetMarginLeftGet(WidgetT *widget);
uint16_t  widgetMarginRightGet(WidgetT *widget);
uint16_t  widgetMarginTopGet(WidgetT *widget);
WidgetT  *widgetNew(uint8_t magic, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t marginX, uint16_t marginY, uint16_t marginX2, uint16_t marginY2);
uint8_t   widgetVisibleGet(WidgetT *widget);
void      widgetVisibleSet(WidgetT *widget, uint8_t enabled);
uint16_t  widgetWidthGet(WidgetT *widget);


#endif // WIDGET_H
