/* Y o u r   D e s c r i p t i o n                       */
/*                            AppBuilder Photon Code Lib */
/*                                         Version 1.10  */

/* Standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/* Toolkit headers */
#include <Ph.h>
#include <Pt.h>
#include <Ap.h>
#include <photon/PxHelp.h>

/* Local headers */
#include "phterm.h"
#include "abimport.h"
#include "proto.h"

#if defined(FAKE_HELP_STUFF)
	#define PxHelpSearch( a, b, c, d ) ((void)0)
#endif

static void setmenuitem( int item, int active ) {
	ApModifyItemState( &menu, active ? AB_ITEM_NORMAL : AB_ITEM_DIM, item, NULL );
	}

int
menu_init( PtWidget_t *link_instance, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PtWidget_t *tty; int active; char *str;
	PtTerminalSelection_t const *sel;
	PhEvent_t *event = cbinfo->event;
	if ( ( str = PhClipboardPasteString( event ? event->input_group : 0 ) ) == NULL )
		active = 0;
	else {
		free( str );
		active = 1;
		}
	setmenuitem( ABN_paste, active );
	setmenuitem( ABN_pastesp, active );

	sel = getptr( tty = ABW_tty, Pt_ARG_TERM_SELECTION );
	active = sel->type != Pt_TERM_SELECTION_NONE;
	setmenuitem( ABN_copy, active );
	setmenuitem( ABN_pastesel, active );
	setmenuitem( ABN_unselect, active );
	setmenuitem( ABN_restoresel,
		! active && sel->old_type != Pt_TERM_SELECTION_NONE
		);

	active = ( sel->flags & Pt_TERM_SELECTION_ALWAYS ) == 0;
	setmenuitem( ABN_select, active );
	setmenuitem( ABN_textmse, ! active );

	/* eliminate 'unreferenced' warnings */
	(void)link_instance; (void)apinfo;
	return Pt_CONTINUE;
	}

int
menu_unrealized( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PtContainerGiveFocus( ABW_tty, cbinfo->event );
	/* eliminate 'unreferenced' warnings */
	(void)widget; (void)apinfo; (void)cbinfo;
	return Pt_CONTINUE;
	}

int copy( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PtTerminalCopy( ABW_tty, cbinfo->event );
	(void)widget; (void)apinfo; /* eliminate 'unreferenced' warnings */
	return Pt_CONTINUE;
	}

int
paste( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PtTerminalPasteClipboard( ABW_tty, cbinfo->event );
	(void)widget; (void)apinfo; /* eliminate 'unreferenced' warnings */
	return Pt_CONTINUE;
	}

int
pastesp( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PhEvent_t *event = cbinfo->event;
	char *string = PhClipboardPasteString( event ? event->input_group : 0 );
	if ( string != NULL )
		PtTerminalPaste( ABW_tty, string, strlen(string), event, Pt_TERM_PASTE_INPUT );
	(void)widget; (void)apinfo; /* eliminate 'unreferenced' warnings */
	return Pt_CONTINUE;
	}

int
pastesel( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PtTerminalPasteSelection( ABW_tty, cbinfo->event );
	(void)widget; (void)apinfo; /* eliminate 'unreferenced' warnings */
	return Pt_CONTINUE;
	}


int
mselect( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PtWidget_t *tty;
	PtTerminalSelection_t sel;
	sel = * (PtTerminalSelection_t*) getptr( tty = ABW_tty, Pt_ARG_TERM_SELECTION );
	sel.flags |= Pt_TERM_SELECTION_ALWAYS;
	setres( tty, Pt_ARG_TERM_SELECTION, &sel );
	/* eliminate 'unreferenced' warnings */
	(void)widget; (void)apinfo; (void)cbinfo;
	return Pt_CONTINUE;
	}

int
textmse( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PtWidget_t *tty;
	PtTerminalSelection_t sel;
	sel = * (PtTerminalSelection_t*) getptr( tty = ABW_tty, Pt_ARG_TERM_SELECTION );
	sel.flags &= ~Pt_TERM_SELECTION_ALWAYS;
	setres( tty, Pt_ARG_TERM_SELECTION, &sel );
	/* eliminate 'unreferenced' warnings */
	(void)widget; (void)apinfo; (void)cbinfo;
	return Pt_CONTINUE;
	}

int
switchsel( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PtWidget_t *tty;
	PtTerminalSelection_t sel;
	sel = * (PtTerminalSelection_t*) getptr( tty = ABW_tty, Pt_ARG_TERM_SELECTION );
	sel.type
	=	sel.type == Pt_TERM_SELECTION_NONE
	?	sel.old_type : Pt_TERM_SELECTION_NONE;
	setres( tty, Pt_ARG_TERM_SELECTION, &sel );
	/* eliminate 'unreferenced' warnings */
	(void)widget; (void)apinfo; (void)cbinfo;
	return Pt_CONTINUE;
	}

int
searchhelp( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) {
	PtWidget_t *tty = ABW_tty;
	char *sel;
	if	(	( sel = PtTerminalGetSelection( tty ) ) != NULL
		||	PtTerminalSelectWord( tty, NULL ) == 1
		&&	( sel = PtTerminalGetSelection( tty ) ) != NULL
			) {
		PxHelpSearch( sel,
			HELP_SEARCH_MODE_TITLE,
			HELP_SEARCH_SCOPE_ALL,
			HELP_SEARCH_METHOD_WORD
			);
		free( sel );
		}
	/* eliminate 'unreferenced' warnings */
	(void)widget; (void)apinfo; (void)cbinfo;
	return Pt_CONTINUE;
	}

