#include "xvote.h"

pop_move_pp(win, gc1, gc2, voters, scale, font1_info, voter_num)
     int voter_num;
     GC gc1, gc2;
     Window win;
     struct side *scale;
     struct voter voters[];
     XFontStruct *font1_info;
       {
	 unsigned int x = 0, y = 0;
	 unsigned int border_width = 4;    /* four pixels */
	 int box;
	 int pop_width, pop_height;
	 Window pop_win;
         XEvent report;
	 KeySym keysym;
	 char buffer[10];
	 int count;
	 XComposeStatus compose;
         char *string1 = "Move Ideal Point:";
         char *string2 = "X-Coordinate:";
         char *string3 = "Y-Coordinate:";
         char *string7 = "Cancel";
         char *string8 = "OK";
         int len1, len2, len3, len7, len8;
	 int bx, by;
	 char *xcoor[NAMELENGTH];
	 char *ycoor[NAMELENGTH];
	 float xc;
	 float yc;

	 sprintf(xcoor, "%g", voters[voter_num].x);
         sprintf(ycoor,  "%g", voters[voter_num].y);
	 xc = voters[voter_num].x;
	 box = 0;
         pop_width = 750;
         pop_height = 110;
         pop_win = XCreateSimpleWindow(display, win, x, y,
                                       pop_width, pop_height,
                                       border_width,
                                       BlackPixel(display, screen),
                                       WhitePixel(display, screen));
         XSelectInput(display, pop_win, ExposureMask | KeyPressMask
		      | ButtonPressMask | StructureNotifyMask);
         XMapWindow(display, pop_win);
         len1 = strlen(string1);
         len2 = strlen(string2);
         len3 = strlen(string3);
         len7 = strlen(string7);
         len8 = strlen(string8);
         while (1)
           {
             XNextEvent(display, &report);
             switch  (report.type)
               {
               case Expose:
                 while (XCheckTypedEvent(display, Expose, &report));
                 place_scale(win, gc1, gc2, (*scale));
/*                                                x,   y           */
                 XDrawString(display,pop_win,gc2, 20, 15, string1, len1);

                 XDrawString(display,pop_win,gc2, 40, 40, string2, len2);
                 XDrawString(display,pop_win,gc2, 40, 60, string3, len3);
                 XDrawString(display,pop_win,gc2, 613, 40, string8, len8);
                 XDrawString(display,pop_win,gc2, 600, 80, string7, len7);
/*                                                      x    y  w    h   */
                 XDrawRectangle(display, pop_win, gc2, 598, 26, 52, 15);
                 XDrawRectangle(display, pop_win, gc2, 598, 66, 52, 15);
/*************************************************************************/

/*                                                x,   y           */

/*                                                      x    y  w    h   */
                 XDrawString(display,pop_win,gc2, 145, 40, xcoor,
                             strlen(xcoor));
                 XDrawString(display,pop_win,gc2, 145, 60, ycoor,
                             strlen(ycoor));
                 switch (box)
		   {
		   case 1:
                     XDrawRectangle(display, pop_win, gc2, 143, 26,
                                    (XTextWidth(font1_info,
                                                xcoor, strlen(xcoor)) + 4),
                                    15);
                     break;
		   case 2:
                     XDrawRectangle(display, pop_win, gc2, 143, 46,
                                    (XTextWidth(font1_info,
                                                ycoor, strlen(ycoor)) + 4),
                                    15);
                     break;
		   default:
		     break;
		   };
		 
                 break;
               case ButtonPress:
                 bx =report.xbutton.x;
                 by =report.xbutton.y;
		 if ((by > 26) && (by < 41) && (bx > 143) && (bx < 193))
		   {
                     box = 1;
                     XClearArea(display, pop_win,0,0,900,900,True);
                   };
                 if ((by > 46) && (by < 61) && (bx > 143) && (bx < 193))
                   {
                     XClearArea(display, pop_win,0,0,900,900,True);
                     box = 2;
                   };

		 
		 if ((by > 66) && (by < 81) && (bx > 598) && (bx < 655))
                   {
                     XDestroyWindow(display,pop_win);
                     return;
                   };
                 if ((by > 26) && (by < 41) && (bx > 598) && (bx < 655))
                   {
                     sscanf(xcoor, "%f", &xc);
                     sscanf(ycoor, "%f", &yc);
                     voters[voter_num].x = xc;
                     voters[voter_num].y = yc;
		     XDestroyWindow(display,pop_win);
		     XClearArea(display, win,101,141,619,619,True);
                     return;
                   };
                 break;
               case KeyPress:		 
		 count = XLookupString(&report, buffer, 1, &keysym,
				       &compose);
		 buffer[count]=0;
		 if ((keysym == XK_Return) || (keysym == XK_KP_Enter)
		     || (keysym == XK_Linefeed))
		   {
		     break;
		   };
		 if (((keysym >= XK_KP_Space) && (keysym <= XK_KP_9))
		     || ((keysym >= XK_space) &&
			 (keysym <= XK_asciitilde)))
		   {
		     switch (box)
		       {
                       case 1:
                         if (strlen(xcoor) >= NAMELENGTH)
                           {
                             XBell(display, 30);
                           }
                         else
                           {
                             XClearArea(display, pop_win, 143, 26,
                                        (XTextWidth(font1_info,
                                                    xcoor,
                                                    strlen(xcoor)) + 5),
                                        18, True);
                             strcat(xcoor, buffer);
                           };
                         break;
                       case 2:
                         if (strlen(ycoor) >= NAMELENGTH)
                           {
                             XBell(display, 30);
                           }
                         else
                           {
                             XClearArea(display, pop_win, 143, 46,
                                        (XTextWidth(font1_info,
                                                    ycoor,
                                                    strlen(ycoor)) + 5),
                                        18, True);
                             strcat(ycoor, buffer);
                           };
                         break;
		       default:
			 break;
		       };		    		   
		   };
		 
		 if ((keysym >= XK_Shift_L) && (keysym <= XK_Hyper_R));
		 if ((keysym == XK_BackSpace) ||
		     (keysym == XK_Delete)) 
		   {			 
		     switch (box)
		       {
                       case 1:
                         if (strlen(xcoor) > 0)
                           {
                             XClearArea(display, pop_win, 143, 26,
                                        (XTextWidth(font1_info,
                                                    xcoor,
                                                    strlen(xcoor)) + 5),
                                        18, True);
                             delete(xcoor);
                           }
                         else
                           {
                             XBell(display, 100);
                           };
                         break;
                       case 2:
                         if (strlen(ycoor) > 0)
                           {
                             XClearArea(display, pop_win, 143, 46,
                                        (XTextWidth(font1_info,
                                                    ycoor,
                                                    strlen(ycoor)) + 5),
                                        18, True);
                             delete(ycoor);
                           }
                         else
                           {
                             XBell(display, 100);
                           };
                         break;
		       default:
			 break;
		       };		   
		   };
	       default:
		 break;
	       } /* end switch */
	   } /* end while */
       }
	 
