xmTerm

/*
 * xmTerm.h
 *
 *  Created on: Mar 26, 2017
 *      Author: chuck
 */

#ifndef XMTERM_H_
#define XMTERM_H_

#include 
#include 


#define FILE_OPEN  0
#define FILE_SAVE  1
#define FILE_EXIT  2
#define EDIT_CUT   0
#define EDIT_COPY  1
#define EDIT_PASTE 2
#define EDIT_CLEAR 3

Widget termEditor;

/* cut, copy, paste, clear */
void menuEditText(int reason);

/* term is a scrolled text */
void loadTerm(Widget term);
	void newTextFile(String fileName);
	void openTextFile(String fileName);
	void saveTextFile(String fileName);

/* proper */
String getFileContents(String file_name);

/* file to edit */
FILE *termFile;

/* normal, enter does new line */
void changeTermCB(Widget widget, XtPointer clientData, XtPointer callData);

/* numeric side enter key, no new line */
void enterTermCB(Widget widget, XtPointer clientData, XtPointer callData);


#endif /* XMTERM_H_ */

/*
 * xmTerm.c
 *
 *  Created on: Mar 26, 2017
 *      Author: chuck
 */

#include "xmTerm.h"

/*  */
void newTextFile(String fileName) {

	printf("newTextFile\n");
	XmTextSetString(termEditor, "");
}

/*  */
void openTextFile(String fileName) {

	printf("openTextFile\n");
	XmTextSetString(termEditor, getFileContents(fileName));
}

/*  */
void saveTextFile(String fileName) {

	printf("saveTextFile\n");

	FILE *saveFile;
	String saveText;

	XtVaGetValues(termEditor, XmNvalue, &saveText, NULL);

	saveFile = fopen(fileName, "w");
	fprintf(saveFile, saveText);
	fclose(saveFile);

	XtFree(saveText);

}

void menuEditText(int reason) {
	//
	Time when = CurrentTime;
	Boolean result = True;
	//printf("reason = %d, time = %x\n", reason, when);
	//

	switch(reason) {
		case EDIT_CUT:
			result = XmTextCut(termEditor, when);
			break;
		case EDIT_COPY:
			result = XmTextCopy(termEditor, when);
			break;
		case EDIT_PASTE:
			result = XmTextPaste(termEditor);
			break;
		case EDIT_CLEAR:
			XmTextClearSelection(termEditor, when);
			break;
	}


}

/* temp file to store term text and later to act on it */
void changeTermCB(Widget widget, XtPointer clientData, XtPointer callData) {

	String strTerm;

	XtVaGetValues(widget, XmNvalue, &strTerm, NULL);

	/* open file */
	termFile = fopen("src/term.txt", "w");

	/* write to file */
	fprintf(termFile, strTerm);

	/* close file */
	fclose(termFile);

	XtFree(strTerm);

}

/*  */
void enterTermCB(Widget widget, XtPointer clientData, XtPointer callData) {

	printf("enter\n");

}


/**** a Stack Overflow solution ****/
String getFileContents(String file_name) {
	char *file_contents;
	long input_file_size;
	FILE *input_file = fopen(file_name, "rb");
	fseek(input_file, 0, SEEK_END);
	input_file_size = ftell(input_file);
	rewind(input_file);
	file_contents = malloc((input_file_size + 1) * (sizeof(char)));
	fread(file_contents, sizeof(char), input_file_size, input_file);
	fclose(input_file);
	file_contents[input_file_size] = 0;
	return file_contents;
}

/****  ****/
void loadTerm(Widget term) {
	//
	XmTextSetString(term, getFileContents("src/term.txt"));
	//XmTextSetString(term, getFileContents(termFileName));

	XtAddCallback(term, XmNvalueChangedCallback, changeTermCB, NULL);
	XtAddCallback(term, XmNactivateCallback, enterTermCB, NULL);

	termEditor = term;

}