/*
 * COPYRIGHT NOTICE
 * Copyright (c) 1994 Integrated Computer Solutions
 * 
 */

/*      Function Name:	XiDBInsertAtEndCB
 *
 *      Description:   	This function accepts a string of the form:
 *			"[query_widget_name]".
 *			It causes a PRE_INSERT at the end of the set of rows
 *
 *      Arguments:      Widget		w:	the activating widget.
 *			XtPointer	client:	the set values string.
 *			XtPointer	call:	the call data (unused).
 *
 *      Notes:        * This function expects that there is an application
 *                      shell from which all other widgets are descended
 *			in order to find a query which may be in another
 *			shell.
 */

/* local debugging is available by setting DBPAK_QUERY_OP_DEBUG */

/* XiDB*CB callbacks are also built into the DBPak libraries. By default, the
** ones that DX emits are used; to use the ones in the DBPak libraries, compile
** with this flag set
*/
#ifndef USE_DBPAK_LIBRARY

#ifndef _XiDBInsertAtEndCB_
#define _XiDBInsertAtEndCB_ 1

#include <ctype.h>
#include <string.h>
#include <X11/StringDefs.h>

#include <Xi/DBPak.h>
#include <Xi/Query.h>


/* ARGSUSED */
#if NeedFunctionPrototypes
void XiDBInsertAtEndCB(Widget w, XtPointer client, XtPointer call)
#else
void XiDBInsertAtEndCB(w, client, call)
	Widget w;
	XtPointer client;
	XtPointer call;
#endif
{
	Widget	query;		/* The query widget */
	char	*start;
	Boolean	oldAuto;

	start = (char *) client;

	if(start == NULL || *start == '\0') {
		/* No query name, assume this widget is an XiDBPushButton and
		 * get the query from it
		 */
		query = NULL;
		XtVaGetValues(w,	XmNquery, (XtPointer*) &query,
					NULL);
		if(query == NULL) {
			XtAppWarning(XtWidgetToApplicationContext(w), "XiDBInsertAtEndCB: no query n");
			return;
		}
	} else {
		/* extract query widget name */

		char	*end, *widget, quote = '\0';

		while(isspace(*start)) start++;
		if(*start == '"') {
			quote = '"';
			start++;
		}
		/* Find last position of string */
		end = start;
		while(*end && !isspace(*end) && *end != quote) end++;

        	widget = (char *)XtMalloc((end - start + 1) * sizeof(char));
		memcpy(widget, start, end - start);
		widget[end-start] = '\0';

		/*
		 * Now convert the widget name to a Widget ID
		 */
		query = XiDBNameToQueryWidget(w,widget);

		if ( query == NULL ) {
			/*
			 * If we can't convert the widget name to an ID, say so 
			 */
			Cardinal num_params = 2;
			String params[2];
			params[0] = XiDBName(w);
			params[1] = widget;
			XtAppWarningMsg(XtWidgetToApplicationContext(w), 
				"InvalidParameters",
				"XiDBInsertAtEndCB",
				"XiDBInsertAtEndCB",
				"Callback Error on %s (XiDBInsertAtEndCB): Cannot find query %s\n", params, &num_params);
			XtFree(widget);
			return;
		}
		XtFree(widget);
	}

	XtVaGetValues(query, XmNautoInsertAtEnd, &oldAuto, NULL);
	XtVaSetValues(query, XmNautoInsertAtEnd, True, NULL);
	XiDBQueryOperation(query, XiDB_LAST_RECORD);
	XiDBQueryOperation(query, XiDB_NEXT_RECORD);
	if(oldAuto != True) {
		XtVaSetValues(query, XmNautoInsertAtEnd, oldAuto, NULL);
	}
}

#endif /* _XiDBInsertAtEndCB_ */

#endif /* USE_DBPAK_LIBRARY */
