File: imaptool.c

package info (click to toggle)
imaptool 0.9-14
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 328 kB
  • ctags: 488
  • sloc: ansic: 3,740; makefile: 93
file content (903 lines) | stat: -rw-r--r-- 25,594 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
/*
 * imaptool.c
 * Copyright (C) 2000 Seth Spitzer - sspitzer@sspitzer.org
 * Copyright (C) 1996-1998 Teemu Maijala - uucee@sci.fi
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <X11/Intrinsic.h>
#include <X11/Xlib.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <X11/cursorfont.h>
#include <X11/Xatom.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/IntrinsicP.h>
#include <X11/CoreP.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "file2pixmap.h"
#include "icon"
#include "iconmask"

#define VERSION "0.9"
#define HTMLCOMMENT "<!-- Map generated by imaptool %s  http://www.sspitzer.org/imaptool/ -->\n"
#define PROGINFO "imaptool %s\n\n(C) 2000 Seth Spitzer - sspitzer@sspitzer.org\n(C) 1996-1998 Teemu Maijala - uucee@sci.fi\n\nimaptool comes with ABSOLUTELY NO WARRANTY; for details see LICENCE.TXT.\n"

#define QUIT_BUTTON_TEXT					"Quit"
#define DUMP_HTML_BUTTON_TEXT				"Dump HTML"
#define CLEAR_HTML_BUTTON_TEXT				"Clear HTML"
#define SHAPE_BUTTON_TEXT					"Shape"

#define CIRCLE_MENU_ITEM_UNSELECTED			"  Circle"
#define CIRCLE_MENU_ITEM_SELECTED			"o Circle"
#define RECTANGLE_MENU_ITEM_UNSELECTED		"  Rectangle"
#define RECTANGLE_MENU_ITEM_SELECTED		"o Rectangle"
#define POLYGON_MENU_ITEM_UNSELECTED		"  Polygon (use the right mouse button to close the polygon)"
#define POLYGON_MENU_ITEM_SELECTED			"o Polygon (use the right mouse button to close the polygon)"

/* Shapes */
#define RECTANGLE  1
#define CIRCLE     2
#define POLYGON    3

/* Buttonstatus */
#define UP 0
#define DOWN 1

/* Maximum number of breakpoints in polygon */
#define POL_MAX_POINTS 100

/* Maximum number of areas, hard coded for now. */
#define MAX_AREAS 1000

/* the longest HTML line for a map area */
#define MAX_AREA_TEXT_LEN 1124

/* Some macros */
#define min(a,b) (a<b)?a:b
#define max(a,b) (a>b)?a:b

/* Misc functions */
void createpicturewindow();
void parseparams(int argc, char *argv[]);
void printusage();
void printversion();
void changeinfotext(char *infotext);
void insert(char *position);
void printrectangle(char *str);
void printcircle(char *str);
void printpolygon(char *str);

/* Event handlers for vviewwindow */
void buttondown(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd);
void buttonup(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd);
void expose(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd);
void enterwindow(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd);
void leavewindow(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd);
void motion(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd);

/* Event handler for client_message & selections (for WM_DELETE_WINDOW) */
void event_handler(Widget w, XtPointer client_data, XEvent *event,
		   Boolean *ctd);

/* Callback functions */
void quit(Widget w, XtPointer call_data, XtPointer client_data);
void dump(Widget w, XtPointer call_data, XtPointer client_data);
void clear(Widget w, XtPointer call_data, XtPointer client_data);
void shapeselect(Widget w, XtPointer call_data, XtPointer client_data);

/* Functions to draw shapes on vviewwindow */
void drawcircle(Widget w);
void drawpolygon(Widget w);
void drawrectangle(Widget w);
void erase_polygon_motionline(Widget w);

/* Global variables */
Atom wm_protocols, wm_delete_window;
char *picturefilename; /* name of current gif/jpeg-file */ 
GC defgc;              /* GC for LineDoubleDash */
Pixmap pixmap;         /* Pixmap for gif/jpeg-file */
Pixmap iconpixmap, iconmask;     /* Pixmap for imaptool-icon and -mask*/
int currentarea = 0;
char areas[MAX_AREAS][MAX_AREA_TEXT_LEN];

#define QUIT_SAVENOTIFY 1
#define OTHER_SAVENOTIFY 0

struct _pixmasize {
  int width, height;
} pixmapsize;          /* size of current pixmap */
 
int nextshape = RECTANGLE;     /* shape selected in shapemenu */
int currentshape = RECTANGLE;  /* shape on screen */

struct _rectangle {            /* geometry of rectangle */
  int x1, y1, x2, y2;
} rectangle = {0,0,0,0};

struct _circle {               /* geometry of circle */
  int x, y, r;
} circle = {0,0,0};

struct _polygon_motionline {   /* motionline when drawing polygon */
  int x, y;
  Boolean active;
} polygon_motionline = {0,0, FALSE};

XPoint polygon[POL_MAX_POINTS];  /* A table containing brakpoints of polygon */
int polcount;                    /* total abount of breakpoints in polygon */

/* if active is true, drawcircle, drawrectangle and drawpolygon should draw
   polygon, otherwise they should erase it. Usually the functions are first
   called width active false and then with active true. */

Boolean active = FALSE;
Boolean outputHtmlInLowerCase = FALSE;

int buttonstatus; /* status of mousebuttons */ 

/* Widgets */
Widget root, picturewindow;
Widget vform, vmenubox, vbox, vviewwindow, vquit, vinfobox, vdump, vclear;
Widget vshapemenu, vshapemenubutton, vrectangle, vcircle, vpolygon;

int main(int argc, char *argv[])
{
  int screennum;
  XtAppContext app_context;
  int cursor_shape = XC_tcross;
  Cursor cursor;
  XGCValues values;
  root = XtVaAppInitialize(&app_context,
			   "imaptool",
			   NULL, 0,
			   &argc, argv,
			   NULL, NULL);
  screennum = XScreenNumberOfScreen(XtScreen(root));
  parseparams(argc, argv);
  /* loading picture to pixmap */
  if (!load_file_to_pixmap(picturefilename, XtDisplay(root), screennum,
			   &pixmapsize.width, &pixmapsize.height, &pixmap)) {
    printusage();
	exit(1);
  }
  /* creating pixmaps for icon & iconmask */
  iconpixmap = XCreatePixmapFromBitmapData(XtDisplay(root),
					   RootWindow(XtDisplay(root), screennum),
					   (char*)icon_bits,
					   icon_width,
					   icon_height,
					   WhitePixel(XtDisplay(root), screennum),
					   BlackPixel(XtDisplay(root), screennum),
					   DefaultDepth(XtDisplay(root),
							screennum));
  iconmask = XCreatePixmapFromBitmapData(XtDisplay(root),
					 RootWindow(XtDisplay(root), screennum),
					 (char*)iconmask_bits,
					 iconmask_width,
					 iconmask_height,
					 WhitePixel(XtDisplay(root), screennum),
					 BlackPixel(XtDisplay(root), screennum),
					 1);

  /* creating window widgets */
  createpicturewindow();
  
  /* showing windows */
  XtPopup(picturewindow, XtGrabNone);
  cursor = XCreateFontCursor(XtDisplay(vviewwindow), cursor_shape);
  XDefineCursor(XtDisplay(vviewwindow), XtWindow(vviewwindow), cursor);

  /* setting up default GC */
  values.foreground = WhitePixel(XtDisplay(picturewindow),
				 XScreenNumberOfScreen(XtScreen(picturewindow)));
  values.background = BlackPixel(XtDisplay(picturewindow),
				 XScreenNumberOfScreen(XtScreen(picturewindow)));
  values.line_style = LineDoubleDash;
  defgc = XCreateGC(XtDisplay(picturewindow), XtWindow(picturewindow),
		 (GCForeground|GCBackground|GCLineStyle), &values);

  XtAddEventHandler(vviewwindow, 0, TRUE, event_handler, (XtPointer)NULL);
  /* going to main loop */
  XtAppMainLoop(app_context);
}

void createpicturewindow()
{
  Pixel menuboxcolor;
  picturewindow = XtVaCreatePopupShell("picturewindow",
				       applicationShellWidgetClass,
				       root,
				       XtNtitle, picturefilename,
				       XtNiconName, picturefilename,
				       XtNiconPixmap, iconpixmap,
				       XtNiconMask, iconmask,
				       NULL);
  vform = XtVaCreateManagedWidget("rootform",
				  formWidgetClass,
				  picturewindow,
				  XtNresizable, TRUE,
				  NULL);
  vmenubox = XtVaCreateManagedWidget("menubox",
				     boxWidgetClass,
				     vform,
				     XtNborderWidth, 0,
				     XtNtop, XawChainTop,
				     XtNbottom, XawChainTop,
				     XtNleft, XawChainLeft,
				     XtNright, XawChainLeft,
				     XtNorientation, XtorientHorizontal, 
				     XtNresizable, TRUE, 
				     NULL);
  XtVaGetValues(vmenubox, XtNbackground, &menuboxcolor, NULL);
  vquit = XtVaCreateManagedWidget(QUIT_BUTTON_TEXT,
				  commandWidgetClass,
				  vmenubox,
				  NULL);
  XtAddCallback(vquit, XtNcallback, quit, (XtPointer)NULL); 
  vdump = XtVaCreateManagedWidget(DUMP_HTML_BUTTON_TEXT,
					commandWidgetClass,
					vmenubox,
					NULL);
  XtAddCallback(vdump, XtNcallback, dump, (XtPointer)NULL); 
  vclear= XtVaCreateManagedWidget(CLEAR_HTML_BUTTON_TEXT,
					commandWidgetClass,
					vmenubox,
					NULL);
  XtAddCallback(vclear, XtNcallback, clear, (XtPointer)NULL); 
  vshapemenu = XtVaCreatePopupShell("vshapemenu",
				    simpleMenuWidgetClass,
				    picturewindow,
				    NULL);
  vshapemenubutton = XtVaCreateManagedWidget("vshapemenubutton",
					     menuButtonWidgetClass,
					     vmenubox,
					     XtNmenuName, "vshapemenu",
					     XtNlabel, SHAPE_BUTTON_TEXT,
					     NULL);
  vrectangle = XtVaCreateManagedWidget(RECTANGLE_MENU_ITEM_SELECTED,
				       smeBSBObjectClass,
				       vshapemenu,
				       NULL);
  vcircle = XtVaCreateManagedWidget(CIRCLE_MENU_ITEM_UNSELECTED,
				    smeBSBObjectClass,
				    vshapemenu,
				    NULL);
  vpolygon = XtVaCreateManagedWidget(POLYGON_MENU_ITEM_UNSELECTED,
				     smeBSBObjectClass,
				     vshapemenu,
				     NULL);
  XtAddCallback(vrectangle, XtNcallback, shapeselect, (XtPointer)RECTANGLE);
  XtAddCallback(vcircle, XtNcallback, shapeselect, (XtPointer)CIRCLE);
  XtAddCallback(vpolygon, XtNcallback, shapeselect, (XtPointer)POLYGON);
  vinfobox = XtVaCreateManagedWidget("infobox",
				     labelWidgetClass,
				     vmenubox,
				     XtNborderWidth, 0,
				     XtNbackground, menuboxcolor,
				     XtNlabel, "                         ",
				     XtNresizable, TRUE,
				     NULL);
  vbox = XtVaCreateManagedWidget("box",
				 boxWidgetClass,
				 vform,
				 XtNfromVert, vmenubox,
				 XtNhSpace, 0,
				 XtNvSpace, 0,
				 XtNtop, XawChainTop,
				 XtNbottom, XawChainTop,
				 XtNleft, XawChainLeft,
				 XtNright, XawChainLeft,
				 NULL);
  vviewwindow = XtVaCreateManagedWidget("viewwindow",
					coreWidgetClass,
					vbox,
					XtNinternalHeight, 0,
					XtNinternalWidth, 0, 
					XtNborderWidth, 0,
					NULL);
  XtAddEventHandler(vviewwindow, ButtonPressMask, FALSE, buttondown,
		    (XtPointer)NULL);
  XtAddEventHandler(vviewwindow, ButtonReleaseMask, FALSE, buttonup,
		    (XtPointer)NULL);
  XtAddEventHandler(vviewwindow, ExposureMask, FALSE, expose,
		    (XtPointer)NULL);
  XtAddEventHandler(vviewwindow, PointerMotionMask, FALSE, motion,
		    (XtPointer)NULL);
  XtAddEventHandler(vviewwindow, EnterWindowMask, FALSE, enterwindow,
		    (XtPointer)NULL);
  XtAddEventHandler(vviewwindow, LeaveWindowMask, FALSE, leavewindow,
		    (XtPointer)NULL);
  XtVaSetValues(vviewwindow, XtNwidth, pixmapsize.width,
		XtNheight, pixmapsize.height, NULL);
}

void parseparams(int argc, char *argv[])
{
  if (argc < 2) {
	printusage();
    exit(0);
  }

  if ((strcmp("-h",argv[1]) == 0) ||
	  (strcmp("-help",argv[1]) == 0) ||
	  (strcmp("--help",argv[1]) == 0))  {
	printusage();
    exit(0);
  }

  if ((strcmp("-v",argv[1]) == 0) ||
	  (strcmp("-version",argv[1]) == 0) ||
 	  (strcmp("--version",argv[1]) == 0)) {
	printversion();
    exit(0);
  }

  if ((strcmp("-l",argv[1]) == 0) ||
	  (strcmp("-lower",argv[1]) == 0) ||	
	  (strcmp("--lower",argv[1]) == 0)) {
	if (argc == 3) {
		outputHtmlInLowerCase = TRUE;
  		picturefilename = argv[2];
		return;
	}
	else {
		printusage();
		exit(0);
	}
  }

  if ((strcmp("-u",argv[1]) == 0) ||
	  (strcmp("-upper",argv[1]) == 0) ||	
	  (strcmp("--upper",argv[1]) == 0)) {
	if (argc == 3) {
		outputHtmlInLowerCase = FALSE;
  		picturefilename = argv[2];
		return;
	}
	else {
		printusage();
		exit(0);
	}
  }
	
  if (argc == 2) { 
	  picturefilename = argv[1]; 
      return;
  }
  else {
	  printusage();
      exit(0);    
  }
}

void printversion()
{
  printf(PROGINFO,VERSION);
}

void printusage()
{ 
  printf("Usage: imaptool [options] file\n");
  printf("options:\n");
  printf("\t-v\tversion\n");
  printf("\t-h\tusage\n");
  printf("\t-l\toutput html in lowercase\n");
  printf("\t-u\toutput html in uppercase\n");
  printf("\n");
  printf("file types currently supported JPEG and GIF\n");
  printf("\n");
  printf("Report bugs to sspitzer@sspitzer.org\n");
}

void changeinfotext(char *text)
{
  int width, height;
  XExposeEvent xeev;
  char *current_label;
  XtVaGetValues(vinfobox, XtNlabel, &current_label, NULL);
  if(strcmp(current_label, text)) {
    xeev.type = Expose;
    xeev.display = XtDisplay(vinfobox);
    xeev.window = XtWindow(vinfobox);
    xeev.x = xeev.y = 0;
    XtVaGetValues(vinfobox, XtNwidth, &width, XtNheight, &height, NULL);
    xeev.width = width;
    xeev.height = height;
    XtVaSetValues(vinfobox, XtNlabel, text, NULL);
    (XtClass(vinfobox))->core_class.expose (vinfobox, (XEvent*)&xeev, NULL);
  }
}

void buttondown(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd)
{
  active = FALSE;
  switch(currentshape) {
  case RECTANGLE:
    drawrectangle(w);
    break;
  case CIRCLE:
    drawcircle(w);
    break;
  case POLYGON:
    if (nextshape!=POLYGON) drawpolygon(w);
    break;
  }
  switch(nextshape) {
  case RECTANGLE:
    currentshape = RECTANGLE;
    rectangle.x1 = event->xbutton.x;
    rectangle.y1 = event->xbutton.y;
    rectangle.x2 = event->xbutton.x;
    rectangle.y2 = event->xbutton.y;
    buttonstatus = DOWN;
    break;
  case CIRCLE:
    currentshape = CIRCLE;
    circle.x = event->xbutton.x;
    circle.y = event->xbutton.y;
    circle.r = 0;
    break;
  case POLYGON:
    currentshape = POLYGON;
    break;
  }
  buttonstatus = DOWN;
}

void incrementcurrentarea()
{
    currentarea++;
    if (currentarea >= MAX_AREAS) {
	printf("maximum number of areas reached.  overwriting the last area\n");
	currentarea = MAX_AREAS-1;
    }
}

void buttonup(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd)
{
  int x, y;
  XSetSelectionOwner(XtDisplay(w), XA_PRIMARY,
		     XtWindow(w), CurrentTime);  

  switch(currentshape) {
  case RECTANGLE:
    active = FALSE;
    drawrectangle(w);
    rectangle.x2 = event->xbutton.x;
    rectangle.y2 = event->xbutton.y;
    active = TRUE;
    drawrectangle(w);
    printrectangle(areas[currentarea]);
    incrementcurrentarea();
    break;
   case CIRCLE:
    active = FALSE;
    drawcircle(w);
    x = circle.x-event->xbutton.x;
    y = circle.y-event->xbutton.y;
    circle.r = (int)sqrt(x*x+y*y);
    active = TRUE;
    drawcircle(w);
    printcircle(areas[currentarea]);
    incrementcurrentarea();
    break;
  case POLYGON:
    if (event->xbutton.button==1) {
      if (polcount<POL_MAX_POINTS) {
			polygon[polcount].x = event->xbutton.x;
			polygon[polcount].y = event->xbutton.y;
			polcount++;
      }
      active = TRUE;
      drawpolygon(w);
  	  polygon_motionline.active = TRUE;
    } else if (event->xbutton.button==2) {
		/* ignore middle button events */
    } else if (event->xbutton.button==3) {
      active = FALSE;
      drawpolygon(w);

	  printpolygon(areas[currentarea]);
      incrementcurrentarea();

      if (polygon_motionline.active == TRUE) erase_polygon_motionline(w);
      polcount = 0;
      polygon_motionline.active = FALSE;
    }
    break;
  }
  buttonstatus = UP;
}

void expose(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd)
{
  XCopyArea(XtDisplay(w), pixmap, XtWindow(w), defgc, event->xexpose.x,
	    event->xexpose.y, event->xexpose.width, event->xexpose.height,
	    event->xexpose.x, event->xexpose.y);
  switch(currentshape) {
  case RECTANGLE:
    drawrectangle(w);
    break;
  case CIRCLE:
    drawcircle(w);
    break;
  case POLYGON:
    drawpolygon(w);
    break;
  }
}

void enterwindow(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd)
{
  if ((currentshape==POLYGON)&&(polygon_motionline.active==TRUE)) {
    polygon_motionline.x = event->xcrossing.x;
    polygon_motionline.y = event->xcrossing.y;
    XDrawLine(XtDisplay(w), XtWindow(w), defgc, polygon[polcount-1].x,
	      polygon[polcount-1].y, polygon_motionline.x, polygon_motionline.y);
  }
}

void leavewindow(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd)
{
  if ((currentshape==POLYGON)&&(polygon_motionline.active==TRUE)) {
    erase_polygon_motionline(w);
    active = TRUE;
    drawpolygon(w);
  }
}

void motion(Widget w, XtPointer client_data, XEvent *event, Boolean *ctd)
{
  char infotext[80];
  int x1, x2, y1, y2;
  switch(currentshape) {
  case RECTANGLE:
    if (buttonstatus==DOWN) {
      active = FALSE;
      drawrectangle(w);
      rectangle.x2 = event->xmotion.x;
      rectangle.y2 = event->xmotion.y;
      active = TRUE;
      drawrectangle(w);
    }
    x1 = min(rectangle.x1, rectangle.x2);
    y1 = min(rectangle.y1, rectangle.y2);
    x2 = max(rectangle.x1, rectangle.x2);
    y2 = max(rectangle.y1, rectangle.y2);
    sprintf(infotext, "%d,%d : %d,%d,%d,%d", event->xmotion.x,
	    event->xmotion.y, x1, y1, x2, y2);
    changeinfotext(infotext);
    break;
  case CIRCLE:
    if (buttonstatus==DOWN) {
      active = FALSE;
      drawcircle(w);
      circle.r = (int)hypot((circle.x-event->xbutton.x),
			    (circle.y-event->xbutton.y));
      active = TRUE;
      drawcircle(w);
    }
    sprintf(infotext, "%d,%d : %d,%d,%d", event->xmotion.x, event->xmotion.y,
	    circle.x, circle.y, circle.r);
    changeinfotext(infotext);
    break;
  case POLYGON:
    if (polygon_motionline.active == TRUE) {
      erase_polygon_motionline(w);
      polygon_motionline.x = event->xmotion.x;
      polygon_motionline.y = event->xmotion.y;
      active = TRUE;
      drawpolygon(w);
      XDrawLine(XtDisplay(w), XtWindow(w), defgc, polygon_motionline.x,
		polygon_motionline.y, polygon[polcount-1].x,
		polygon[polcount-1].y);
      XFlush(XtDisplay(w));
    }
    sprintf(infotext, "%d,%d : %d,%d - %d,%d", event->xmotion.x, event->xmotion.y,
	    polygon[0].x, polygon[0].y, polygon[polcount-1].x,
	    polygon[polcount-1].y);
    changeinfotext(infotext);
    break;
  }
}

void event_handler(Widget w, XtPointer client_data, XEvent *event,
		   Boolean *ctd)
{
  if (event->type==SelectionClear) {
    active = FALSE;
    switch(currentshape) {
    case RECTANGLE:
      drawrectangle(w);
      break;
    case CIRCLE:
      drawcircle(w);
      break;
    case POLYGON:
      drawpolygon(w);
      polcount = 0;
      polygon_motionline.active = FALSE;
      break;
    }
  }
  if (event->type==SelectionRequest) {
    char position[MAX_AREA_TEXT_LEN];
    int i;
    XEvent sendevent;
    insert(position);
    sendevent.type = SelectionNotify;
    sendevent.xselection.display = XtDisplay(w);
    sendevent.xselection.requestor = event->xselectionrequest.requestor;
    sendevent.xselection.selection = event->xselectionrequest.selection;
    sendevent.xselection.target = event->xselectionrequest.target;
    sendevent.xselection.property = event->xselectionrequest.property;
    sendevent.xselection.time = event->xselectionrequest.time;
    XChangeProperty(XtDisplay(root),
		    event->xselectionrequest.requestor,
		    event->xselectionrequest.property,
		    event->xselectionrequest.target,
		    8,
		    PropModeReplace,
		    position,
		    strlen(position));
    XSendEvent(XtDisplay(root),
	       event->xselectionrequest.requestor,
               True,
               0,
               &sendevent);
    XFlush(XtDisplay(root));
  }
}

void printrectangle(char *str)
{
    int x1, x2, y1, y2, i;

    x1 = min(rectangle.x1, rectangle.x2);
    y1 = min(rectangle.y1, rectangle.y2);
    x2 = max(rectangle.x1, rectangle.x2);
    y2 = max(rectangle.y1, rectangle.y2);

	if (outputHtmlInLowerCase) {
    	sprintf(str,
				"<area shape=\"rect\" coords=\"%d,%d,%d,%d\" href=\"\">",
				x1, y1, x2, y2);
    }
	else {
    	sprintf(str,
				"<AREA SHAPE=\"RECT\" COORDS=\"%d,%d,%d,%d\" HREF=\"\">",
				x1, y1, x2, y2);
    }
}

void printcircle(char *str)
{
	if (outputHtmlInLowerCase) {
			sprintf(str, "<area shape=\"circle\" coords=\"%d,%d,%d\" href=\"\">", circle.x, circle.y, circle.r);
	}	
	else {	
			sprintf(str, "<AREA SHAPE=\"CIRCLE\" COORDS=\"%d,%d,%d\" HREF=\"\">", circle.x, circle.y, circle.r);
	}
}

void printpolygon(char *str) 
{
    char temp[255];
    int i;

	if (outputHtmlInLowerCase) {
		sprintf(str, "<area shape=\"polygon\" coords=\"");
	}
	else {
		sprintf(str, "<AREA SHAPE=\"POLYGON\" COORDS=\"");
	}
    for (i=0;i<polcount;i++) {
      sprintf(temp,"%d,%d,", polygon[i].x, polygon[i].y);
      strcat(str, temp);
      if (strlen(str)>1020) break;
    }
	if (outputHtmlInLowerCase) {
		strcpy(str+strlen(str)-1, "\" href=\"\">");
	}
	else {
		strcpy(str+strlen(str)-1, "\" HREF=\"\">");
	}
}

void insert(char *position)
{
  switch(currentshape) {
  case RECTANGLE:
    printrectangle(position);
    break;
  case CIRCLE:
    printcircle(position);
    break;
  case POLYGON:
    printpolygon(position);
  }
} 

void clear(Widget w, XtPointer client_data, XtPointer call_data)
{
	currentarea = 0;
}
void dump(Widget w, XtPointer client_data, XtPointer call_data)
{
	int i;
	/* if the picturefile name is "/tmp/foobar.gif", let the mapname be "foobar.gif" */
	char *mapname = NULL;

	mapname = strrchr(picturefilename,'/');
	if (mapname) {
		/* skip over the "/" */
		mapname++;
	}
	else {
		mapname = picturefilename;
	}

	if (outputHtmlInLowerCase) {
		printf("<map name=\"map-%s\">\n",mapname);
	}
	else {
		printf("<MAP NAME=\"map-%s\">\n",mapname);
	}
	for (i=0;i<currentarea;i++) {
		printf("%s\n",areas[i]);
	}
	
	if (outputHtmlInLowerCase) {
		printf("</map>\n");
		printf(HTMLCOMMENT,VERSION);
		printf("<img src=\"%s\" usemap=\"#map-%s\" alt=\"map-%s\" border=0>\n",mapname,mapname,mapname);
	}
	else {
		printf("</MAP>\n");
		printf(HTMLCOMMENT,VERSION);
		printf("<IMG SRC=\"%s\" USEMAP=\"#map-%s\" ALT=\"map-%s\" BORDER=0>\n",mapname,mapname,mapname);
	}
	printf("\n\n\n");
}

void quit(Widget w, XtPointer client_data, XtPointer call_data)
{
  XtPopdown(picturewindow);
  XFreePixmap(XtDisplay(root), pixmap);
  XtDestroyWidget(root);
  exit(0);
}

void shapeselect(Widget w, XtPointer call_data, XtPointer client_data)
{
  switch(nextshape) {
  case RECTANGLE:
    XtVaSetValues(vrectangle, XtNlabel, RECTANGLE_MENU_ITEM_UNSELECTED, NULL);
    break;
  case CIRCLE:
    XtVaSetValues(vcircle, XtNlabel, CIRCLE_MENU_ITEM_UNSELECTED, NULL);
    break;
  case POLYGON:
    XtVaSetValues(vpolygon, XtNlabel, POLYGON_MENU_ITEM_UNSELECTED, NULL);
    break;
  }
  nextshape = (int)call_data;
  switch(nextshape) {
  case RECTANGLE:
    polygon_motionline.active = FALSE;
    XtVaSetValues(vrectangle, XtNlabel, RECTANGLE_MENU_ITEM_SELECTED, NULL);
    break;
  case CIRCLE:
    polygon_motionline.active = FALSE;
    XtVaSetValues(vcircle, XtNlabel, CIRCLE_MENU_ITEM_SELECTED, NULL);
    break;
  case POLYGON:
    if (currentshape != POLYGON) {
      polcount = 0;
      polygon_motionline.active = FALSE;
    }
    XtVaSetValues(vpolygon, XtNlabel, POLYGON_MENU_ITEM_SELECTED, NULL);
    break;
  }
}

void drawcircle(Widget w)
{
  if (active) {
    XDrawArc(XtDisplay(w), XtWindow(w), defgc, circle.x-circle.r,
	     circle.y-circle.r, 2*circle.r, 2*circle.r, 0, 360*64);
  } else {
    int x, y;
    x = circle.x-circle.r; y = circle.y-circle.r;
    if (x<0) x=0; if (y<0) y=0;
    XCopyArea(XtDisplay(w), pixmap, XtWindow(w), defgc, x, y, 2*circle.r+1,
	      2*circle.r+1, x, y);
  }
  XFlush(XtDisplay(w));
}

void drawpolygon(Widget w)
{
  if (active) {
    XDrawLines(XtDisplay(w), XtWindow(w), defgc, polygon, polcount,
	       CoordModeOrigin);
  } else {
    int minx, miny, maxx, maxy, i;
    minx = miny = maxx = maxy = 0;
    for (i=0;i<polcount;i++) {
      if (polygon[i].x<minx) minx = polygon[i].x;
      if (polygon[i].y<miny) miny = polygon[i].y;
      if (polygon[i].x>maxx) maxx = polygon[i].x;
      if (polygon[i].y>maxy) maxy = polygon[i].y;
    }
    XCopyArea(XtDisplay(w), pixmap, XtWindow(w), defgc, minx, miny,
	      maxx-minx+1, maxy-miny+1, minx, miny);
  }
  XFlush(XtDisplay(w));
}

void drawrectangle(Widget w)
{
  int x1, x2, y1, y2;
  x1 = min(rectangle.x1, rectangle.x2);
  y1 = min(rectangle.y1, rectangle.y2);
  x2 = max(rectangle.x1, rectangle.x2);
  y2 = max(rectangle.y1, rectangle.y2);
  if (active) {
    XDrawRectangle(XtDisplay(w), XtWindow(w), defgc, x1, y1, x2-x1, y2-y1);
  } else {
    GC gc;
    gc = XCreateGC(XtDisplay(w), XtWindow(w), 0, NULL);
     if (x1<0) x1=0; if (y1<0) y1=0;
    XCopyArea(XtDisplay(w), pixmap, XtWindow(w), gc, x1, y1, x2-x1+1, y2-y1+1,
	      x1, y1);
    XFreeGC(XtDisplay(w), gc);
  }
  XFlush(XtDisplay(w));
}

void erase_polygon_motionline(Widget w)
{
  int x1, y1, x2, y2;
  x1 = min(polygon[polcount-1].x, polygon_motionline.x);
  y1 = min(polygon[polcount-1].y, polygon_motionline.y);
  x2 = max(polygon[polcount-1].x, polygon_motionline.x);
  y2 = max(polygon[polcount-1].y, polygon_motionline.y);
  XCopyArea(XtDisplay(w), pixmap, XtWindow(w), defgc, x1, y1, x2-x1+1,
	    y2-y1+1, x1, y1);
  XFlush(XtDisplay(w));
}