/**************************************************
 **************    notemenus.c   ******************
 **************************************************/

#define INTUI_V36_NAMES_ONLY

#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/iffparse.h>
#include <libraries/gadtools.h>

#include <clib/iffparse_protos.h>

#include <stdio.h>		/* for printf */
#include <string.h>

#include "stickit2.h"

#include "consts.h"
#include "structs.h"
#include "proto.h"

#define ID_FTXT		MAKE_ID('F','T','X','T')
#define ID_CHRS		MAKE_ID('C','H','R','S')

extern prj_p prj;

/*
Function : BOOL notemenu_editcopy(note_p curr_note)
Puporse : Copy the current note's text into the clipboard. Returns TRUE if
	sucessful, FALSE otherwise.
*/

BOOL notemenu_editcopy(note_p curr_note)
{
	struct IFFHandle *iffhandle = NULL;

	char errmsg[STRLEN_ERRMSG];

	long errno = 0;
	int textlen;

	/* Is it a valid note ? */

	if (!curr_note)
		return (FALSE);

	/* Allocate IFF structure */

	iffhandle = AllocIFF();

	if (!iffhandle) {
		error("Can't allocate clipboard structure",ERR_WARNING,__LINE__,
__FILE__);
		return (FALSE);
	}

	/* Set up IFF_File for clipboard IO */

	iffhandle->iff_Stream = (ULONG)OpenClipboard(prj->prefs.clipunit);

	if (!iffhandle) {
		sprintf(errmsg,"Can't open clipboard unit %ul",
prj->prefs.clipunit);
		error(errmsg,ERR_WARNING,__LINE__,__FILE__);

		FreeIFF(iffhandle);

		return(FALSE);
	}

	InitIFFasClip(iffhandle);

	/* Start the write to the clipboard */

	if (OpenIFF(iffhandle,IFFF_WRITE)) {
		error("Can't write to clipboard unit",ERR_WARNING,__LINE__,
__FILE__);

		CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);
		FreeIFF(iffhandle);

		return(FALSE);
	}

	/* Write the data out */

	if (!(errno = PushChunk(iffhandle,ID_FTXT,ID_FORM,IFFSIZE_UNKNOWN))) {
		if (!(errno = PushChunk(iffhandle,0,ID_CHRS,IFFSIZE_UNKNOWN))) {
			textlen = strlen(curr_note->text);

			if (WriteChunkBytes(iffhandle,curr_note->text,textlen)!=
textlen) {
				error("Can't write text to clipboard",
ERR_WARNING,__LINE__,__FILE__);

				errno = IFFERR_WRITE;
			}
		}

		if (!errno)
			errno = PopChunk(iffhandle);
	}

	if (!errno)
		errno = PopChunk(iffhandle);

	/* Free resources */

	CloseIFF(iffhandle);

	CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);

	/* Finally, free iffhandle */

	FreeIFF(iffhandle);

	if (errno)
		return (FALSE);
	else
		return (TRUE);
}

/*
Function : void notemenu_editpaste(note_p curr_note)
Puporse : Copy the current note's from the clipboard into the current cursor
	position.
*/

void notemenu_editpaste(note_p curr_note)
{
	struct IFFHandle *iffhandle = NULL;
	struct ContextNode *contextnode = NULL;

	char errmsg[STRLEN_ERRMSG];
	char pastebuffer[STRLEN_NOTETEXT];

	long errno = 0;
	int textlen;
	int l;

	/* Is it a valid note with carat ? */

	if (!curr_note)
		return;

	if (!curr_note->carat.text)
		return;

	/* Allocate IFF structure */

	iffhandle = AllocIFF();

	if (!iffhandle) {
		error("Can't allocate clipboard structure",ERR_WARNING,__LINE__,
__FILE__);
		return;
	}

	/* Set up IFF_File for clipboard IO */

	iffhandle->iff_Stream = (ULONG)OpenClipboard(prj->prefs.clipunit);

	if (!iffhandle) {
		sprintf(errmsg,"Can't open clipboard unit %ul",
prj->prefs.clipunit);
		error(errmsg,ERR_WARNING,__LINE__,__FILE__);

		FreeIFF(iffhandle);

		return;
	}

	InitIFFasClip(iffhandle);

	/* Start the write to the clipboard */

	if (OpenIFF(iffhandle,IFFF_READ)) {
		error("Can't read from clipboard unit",ERR_WARNING,__LINE__,
__FILE__);

		CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);
		FreeIFF(iffhandle);

		return;
	}

	/* If can't find right chunk */

	if (StopChunk(iffhandle,ID_FTXT,ID_CHRS)) {
		CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);
		FreeIFF(iffhandle);

		return;
	}

	while (TRUE) {
		errno = ParseIFF(iffhandle,IFFPARSE_SCAN);

		if (errno == IFFERR_EOC)
			continue;		/* enter next context */
		else if (errno)
			break;

		contextnode = CurrentChunk(iffhandle);

		if ((contextnode) && (contextnode->cn_Type == ID_FTXT) &&
(contextnode->cn_ID)) {
			while ((textlen = ReadChunkBytes(iffhandle,pastebuffer,
STRLEN_NOTETEXT)) > 0) {
				pastebuffer[textlen] = '\0';

				for (l = 0; l < textlen; l++)
					carat_addachar(curr_note,(char)
((pastebuffer[l] == '\012')?' ':pastebuffer[l]),FALSE);
			}

			if (textlen < 0)
				errno = textlen;
		}
	}

	/* Refresh the note */

	carat_from_ptr(curr_note,PTRCHANGE_CLICK);
	note_refresh(curr_note);

	/* Free resources */

	CloseIFF(iffhandle);

	CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);

	/* Finally, free iffhandle */

	FreeIFF(iffhandle);

	/* The note text has probably been changed */

	prj->projectchanged = TRUE;
}
