#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "gis.h"
#include "display.h"
#include "raster.h"
#include "local_proto.h"

static int max(int,int);

int make_window_box ( struct Cell_head *window, double magnify, int full, int hand)
{
    char buffer[1000] ;
    int screen_x, screen_y ;
    double px, py, ux1, uy1, ux2, uy2 ;
    double ns, ew;
    int button ;
    int cur_screen_x, cur_screen_y ;
    int mode; /* 1, 2 */
    int resetwin, resetres;
    struct Cell_head defwin;
    int printmenu = 1;
    int limit, t;
    
    G_get_default_window(&defwin);

    mode = 1;
    while (1) {
	resetwin = 0;
        if ( !hand ) {
	    if ( printmenu ) {
		fprintf(stderr, "\n\nButtons:\n") ;
		fprintf(stderr, "%s 1. corner\n", lefts) ;
		fprintf(stderr, "%s Unzoom\n", middles) ;
		if ( full )
		    fprintf(stderr, "%s Main menu\n\n", rights) ;
		else 
		    fprintf(stderr, "%s Quit\n\n", rights) ;

		printmenu = 0;
	    }
	} else {
	    if ( mode == 1 )
		fprintf(stderr, "\r1. corner") ;
	    else
		fprintf(stderr, "\r2. corner") ;
	}
	if ( mode == 1 ) {
	    if ( !hand ) {
		R_get_location_with_pointer(&screen_x, &screen_y, &button);
	    } else {
	        R_get_location_with_box(0, 0, &screen_x, &screen_y, &button) ;
	    }
	    cur_screen_x = screen_x;
	    cur_screen_y = screen_y;
	} else	{
	    R_get_location_with_box(cur_screen_x, cur_screen_y, &screen_x, &screen_y, &button) ;
	}
	
	/* For print only */
	px = D_d_to_u_col((double)screen_x)  ;
	py = D_d_to_u_row((double)screen_y)  ;
        if ( !hand ) 
            print_coor ( window, py, px );
	
	if(button == leftb){
		if ( !hand ) {
		    if ( mode == 1 ) {
			fprintf(stderr, "\n\nButtons:\n") ;
			fprintf(stderr, "%s 1. corner (reset)\n", lefts) ;
			fprintf(stderr, "%s 2. corner\n", middles) ;
			if ( full )
			    fprintf(stderr, "%s Main menu\n\n", rights) ;
			else 
			    fprintf(stderr, "%s Quit\n\n", rights) ;
		        mode = 2;
		    }
		    if ( mode == 2 ) {
		      cur_screen_x = screen_x ;
		      cur_screen_y = screen_y ;
		    }
		} else { /* hand */
		    if ( mode == 1 ) {
                        mode = 2;
		    } else {
			ux1 = D_d_to_u_col((double)cur_screen_x)  ;
			uy1 = D_d_to_u_row((double)cur_screen_y)  ;
			ux2 = D_d_to_u_col((double)screen_x)  ;
			uy2 = D_d_to_u_row((double)screen_y)  ;
		        resetwin = 1;
		        mode = 1;
		    }
		}
	}else
	if(button == middleb){
		if ( mode == 1 ) { /* unzoom */
	            ux2 = D_d_to_u_col((double)screen_x)  ;
	            uy2 = D_d_to_u_row((double)screen_y)  ;
		    ew = window->east - window->west;
		    ns = window->north - window->south;

		    if ( ns <= window->ns_res )
		        ns = 2 * window->ns_res;
		    else
		        ew /= magnify;

		    if ( ew <= window->ew_res ) 
			ew = 2 * window->ew_res;
		    else
		        ns /= magnify;

		    ux1 = window->east + ew/2;
		    ux2 = window->west - ew/2;
		    uy1 = window->north + ns/2;
		    uy2 = window->south - ns/2;
		} else {
	            ux1 = D_d_to_u_col((double)cur_screen_x)  ;
	            uy1 = D_d_to_u_row((double)cur_screen_y)  ;
	            ux2 = D_d_to_u_col((double)screen_x)  ;
	            uy2 = D_d_to_u_row((double)screen_y)  ;
	            printmenu = 1;
		    mode = 1;
		}
	        fprintf(stderr, "\n") ;
		resetwin = 1;
	}else{
                fprintf(stderr, "\n") ;
		return 1;
	}
	if ( resetwin ) {
	    set_win ( window, ux1, uy1, ux2, uy2, hand);
	}
    } 

    fprintf(stderr, "\n") ;
    return 1; /* not reached */
}


