/*
 * $Source: $
 * $Revision: $
 * $Date: $
 * $State: $
 * $Author: $
 *
 *
 * $Log: $
 *
 */

/* 
 *	Copyright (c) 1991 by the Massachusetts Institute of Technology,
 *	For copying and distribution information, see the file
 *	"mit-copyright.h".
 *
 * 	doit.c  - WADE 5/3/91
 *
 *	  miscellanous routines
 *
 *	do_find_menu_id
 *
 */
 
#include <Limits.h>
#include <Types.h>
#include <QuickDraw.h>
#include <Fonts.h>
#include <Events.h>
#include <Controls.h>
#include <Windows.h>
#include <Menus.h>
#include <TextEdit.h>
#include <Dialogs.h>
#include <Desk.h>
#include <Scrap.h>
#include <ToolUtils.h>
#include <Memory.h>
#include <SegLoad.h>
#include <Files.h>
#include <OSUtils.h>
#include <Traps.h>
#include <Lists.h>
#include <string.h>
#include <time.h>
#include "pips.h"
#include "prototype.h"
#include <stdio.h>
#include "mit-copyright.h"

/*
 *
 *  do_find_menu_id:  
 *	Used to by the move and reorder routines, this routine fills in the menu 
 *	for a related mouse event, taking in consideration the heirarchy of menu's.
 */
#pragma segment misc
void
do_find_menu_id(menu_node_id,mouse_up_event,cell,move_down)
char	*menu_node_id;
Boolean	 mouse_up_event;
short	 cell;
Boolean	 move_down;
{
	Str255	    	list_buffer;
	Boolean	    	rc;
	char		level[3];
	short		indent_level;
	short		itemp;
	Str255		temp;
	char		tlevel[3];
	short		list_size;
	
	/*  initialize some stuff */
	list_size = list_get_size() - 1;  /* use [0:n] range */
	*menu_node_id = NULL;
		
	/* get cell buffer where event occured */
	rc = list_get_buffer( list_buffer, cell );
	
	/* If we have a mouse up event then check the cell below the cell with the event*/
	/* to determine if it has a greater level, in which case we return the cell	*/
	/* where the event occured, otherwise find a cell with a			*/					
	/* less level than ours above us and using the menu located in the banner	*/
	/* if required. 								*/
		
	/* get level number of mouse down cell */
	if (rc) rc = get_field(list_buffer,LEVEL,level);
	if (rc) indent_level = atoi(level);
	
	/* there is a special case when the mouse up cell */
	if ( mouse_up_event ) {
		/* are we dragging a cell down, ie. top to bottom of window */
		if (move_down && cell < list_size) {
			rc = list_get_buffer( temp, cell + 1 );
			if (rc) rc = get_field(temp,LEVEL,tlevel);
			if (rc && (indent_level < atoi(tlevel)))
				rc = get_field(list_buffer,NODE,menu_node_id);
		}
		else	/* must be a move_up, ie. dragging a cell from bottom to top */
			if ( cell != 0) {
				rc = list_get_buffer( temp, cell - 1 );
				if (rc) rc = get_field(temp,LEVEL,tlevel);
				if (rc && (indent_level < atoi(tlevel))) {
					indent_level = atoi(tlevel);
					itemp = cell - 2;
					for(; indent_level == atoi(tlevel) && itemp >= 0; itemp--) {
						rc = list_get_buffer( temp, itemp );
						if (rc) rc = get_field(temp,LEVEL,tlevel);
					}
					rc = get_field(temp,NODE,menu_node_id);
				}
					
			}
	}
	
	if (*menu_node_id == NULL) {
		if (indent_level == 1)
			data_current_nodeid(retrieve,menu_node_id);
		else {
			/* search each cell back from mouse down cell until level changes */
			itemp = cell - 1;
			strcpy(tlevel,level);
			for(; indent_level == atoi(tlevel) && itemp >= 0; itemp--) {
				rc = list_get_buffer( temp, itemp );
				if (rc) rc = get_field(temp,LEVEL,tlevel);
			}
			rc = get_field(temp,NODE,menu_node_id);
		}
	}
}

/*
 *
 *  do_find_link_id:  
 *	Used to by the move function to determine what node in a menu the node 
 *	that is moving should be linked above.
 */
#pragma segment misc
void
do_find_link_id(node_id,cell,move_down)
char	*node_id;
short	 cell;
Boolean  move_down;
{
	Str255	    	list_buffer;
	Boolean	    	rc;
	char		level[3];
	short		indent_level;
	Str255		temp;
	char		tlevel[3];
	short		list_size;
	
	/*  initialize some stuff */
	list_size = list_get_size() - 1;  /* use [0:n] range */
	*node_id = NULL;
		
	/* get cell buffer where mouse down occured */
	rc = list_get_buffer( list_buffer, cell );
			
	/* get level number of mouse down cell */
	if (rc) rc = get_field(list_buffer,LEVEL,level);
	if (rc) indent_level = atoi(level);
	
	/* what direction are we moving? */
	if (move_down) {
		/* is there a cell below us that is part of the move menu  */
		if (cell < list_size) {
			rc = list_get_buffer( temp, cell + 1 );
			if (rc) rc = get_field(temp,LEVEL,tlevel);
			if (rc && (indent_level <= atoi(tlevel)))
				rc = get_field(temp,NODE,node_id);
		}
	}
	else {
		/* muse be a move up */
		if ( cell == 0 )
			rc = get_field(list_buffer,NODE,node_id);
		else {
			rc = list_get_buffer(temp, cell - 1);
			if (rc) rc = get_field(temp,LEVEL,tlevel);
			if (indent_level >= atoi(tlevel) )
				rc = get_field(list_buffer,NODE,node_id);
		}
	}
}
