/*
 * Copyright (c) 1994 Malcolm Beattie.
 * You may distribute under the terms of the GNU General Public License,
 * as specified in the README file.
 */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <tcl.h>
#include <tk.h>

/*
 * SunOS doesn't seem to like static forward declarations
 * so compile under SunOS with -DSTATIC=
 */
#ifndef STATIC
#define STATIC static
#endif

#define RECOVERSTACK do \
                     { \
		         stack_base = AvARRAY(stack); \
			 stack_sp = stack_base + sp; \
			 stack_max = stack_base + AvMAX(stack); \
		     } while (0)


#define CALLBACKOK(cp) ((cp)->method || (cp)->cvcache)

struct tkpcallback
{
    SV *slave;
    char *method;
    CV *cvcache;
};

typedef struct
{
    char *package;
    HV *stash;
    I32 (*newwidgetproc) _((int, int, int));
    I32 newix;
    I32 (*widgetcmdproc) _((int, int, int));
    I32 *methods; /* array of method ixes terminated by IX_ENDOFLIST*/
    char *filename;
} WidgetClass;

char **StackToArgv _((int, int, int));
I32 uvarEvget _((IV, SV *));
SV *WrefToPath _((SV *, char *, U32));
int WrefToWidget _((SV *, Tk_Window *, ClientData *, char *, U32));
Tk_Window WrefToWindow _((SV *, char *, U32));
void TkpDeleteWidgetHash _((Tk_Window));
void TkpCreateWidgetHash _((Tk_Window, ClientData));
char *TkpGenPathname _((SV *));
SV *NewWidgetRef _((char *, HV *));
SV *PathnameToWref _((char *));

void TkpCallback _((struct tkpcallback *, int));
int TkpTraceOnSet _((IV ix, SV *sv, I32 (*)(IV, SV *)));
int TkpUntraceOnSet _((IV ix, SV *sv, I32 (*)(IV, SV *)));

unsigned long	TkpCreateBinding _((Tk_BindingTable, ClientData,
				    char *, SV *, SV *));
int TkpConfigureInfo _((int sp, Tk_Window tkwin, Tk_ConfigSpec *specs,
			char *widgRec, char *argvName, int flags));
void TkpConfigureWidget _((Tk_Window tkwin, Tk_ConfigSpec *specs,
			   int sp, int items, char *widgRec, int flags));
Tk_BindingTable TkpCreateBindingTable _((Tcl_Interp *interp));
void TkpBindEvent _((Tk_BindingTable bindingTable, XEvent *eventPtr,
		     Tk_Window tkwin, int numObjects, ClientData *objectPtr));
void TkpHandleEvent _((XEvent *eventPtr));
int TkpDoOneEvent _((int flags));
void fatal();
void init_widget _((WidgetClass *));

/*
 * Extensions to the Tk_ConfigSpec type values defined
 * in tk.h (TK_CONFIG_CUSTOM not being flexible enough)
 */
#define TKP_CONFIG_SLAVE	100
#define TKP_CONFIG_METHOD	101
#define TKP_CONFIG_VARIABLE	102

/* The following enum list needs keeping up to date with
 * the methodname array defined in tkperl.c */

#define TKPMETHOD(ix, value) ix,
#define TKPLASTMETHOD(ix, value) ix
enum
{
#include "tkpmethod.h"
};
#undef TKPMETHOD
#undef TKPLASTMETHOD

/* Ixes for the widget support usersubs */
enum { US_GRABCURRENT, US_GRABRELEASE, US_GRAB, US_GRABSTATUS };
enum { US_FOCUS, US_FOCUSDEFAULT };
enum { US_WAITVAR, US_WAITVIS, US_WAITWIN };
enum { US_OPTADD, US_OPTCLEAR, US_OPTGET, US_OPTADDSTRING };

#ifndef TKPEXT
#define TKPEXT extern
#endif

TKPEXT Tcl_Interp *tclinterp;
TKPEXT Tk_Window mainwin;
TKPEXT Tcl_HashTable widgetRecordTable;
