File: imaptool.c

package info (click to toggle)
imaptool 0.6.1-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 160 kB
  • ctags: 279
  • sloc: ansic: 1,941; makefile: 70
file content (697 lines) | stat: -rw-r--r-- 20,435 bytes parent folder | download | duplicates (2)
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
/*
 * imaptool.c
 * 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 proginfo "imaptool 0.6 (C) 1996-1998 Teemu Maijala - uucee@sci.fi\n\
imaptool comes with ABSOLUTELY NO WARRANTY; for details see LICENCE.TXT.\n"

/* 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

/* 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 changeinfotext(char *infotext);
void insert(char *position);

/* 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 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*/

#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;

int buttonstatus; /* status of mousebuttons */ 

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

void 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();
  /* 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",
				  commandWidgetClass,
				  vmenubox,
				  NULL);
  XtAddCallback(vquit, XtNcallback, quit, (XtPointer)NULL); 
  vshapemenu = XtVaCreatePopupShell("vshapemenu",
				    simpleMenuWidgetClass,
				    picturewindow,
				    NULL);
  vshapemenubutton = XtVaCreateManagedWidget("vshapemenubutton",
					     menuButtonWidgetClass,
					     vmenubox,
					     XtNmenuName, "vshapemenu",
					     XtNlabel, "Shape",
					     NULL);
  vrectangle = XtVaCreateManagedWidget("o Rectangle",
				       smeBSBObjectClass,
				       vshapemenu,
				       NULL);
  vcircle = XtVaCreateManagedWidget("  Circle",
				    smeBSBObjectClass,
				    vshapemenu,
				    NULL);
  vpolygon = XtVaCreateManagedWidget("  Polygon",
				     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();
  picturefilename = argv[1];
}

void printusage()
{
  printf("%s\n", proginfo);
  printf("usage: imaptool <imap.gif/jpeg>\n\n");
  exit(0);
}

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 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);
    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);
    break;
  case POLYGON:
    if (event->xbutton.button<3) {
      if (polcount<POL_MAX_POINTS) {
	polygon[polcount].x = event->xbutton.x;
	polygon[polcount].y = event->xbutton.y;
	polcount++;
      }
      active = TRUE;
      drawpolygon(w);
    } else {
      active = FALSE;
      drawpolygon(w);
      if (polygon_motionline.active == TRUE) erase_polygon_motionline(w);
      polcount = 1;
      polygon[0].x = event->xbutton.x;
      polygon[0].y = event->xbutton.y;
      active = TRUE;
    }
    break;
  }
  buttonstatus = UP;
  polygon_motionline.active = TRUE;
}

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[1124];
    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 insert(char *position)
{
  int x1, x2, y1, y2, i;
  char temp[255];
  switch(currentshape) {
  case RECTANGLE:
    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(position,
	    "<AREA SHAPE=RECT COORDS=\042%d,%d,%d,%d\042 HREF=\042\042>",
	    x1, y1, x2, y2);
    break;
  case CIRCLE:
    sprintf(position, "<AREA SHAPE=CIRCLE COORDS=\042%d,%d,%d\042 HREF=\042\042>",
	    circle.x, circle.y, circle.r);
    break;
  case POLYGON:
    sprintf(position, "<AREA SHAPE=POLYGON COORDS=\042");
    for (i=0;i<polcount;i++) {
      sprintf(temp,"%d,%d,", polygon[i].x, polygon[i].y);
      strcat(position, temp);
      if (strlen(position)>1020) break;
    }
    strcpy(position+strlen(position)-1, "\042 HREF=\042\042>");
  }
} 

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", NULL);
    break;
  case CIRCLE:
    XtVaSetValues(vcircle, XtNlabel, "  Circle", NULL);
    break;
  case POLYGON:
    XtVaSetValues(vpolygon, XtNlabel, "  Polygon", NULL);
    break;
  }
  nextshape = (int)call_data;
  switch(nextshape) {
  case RECTANGLE:
    polygon_motionline.active = FALSE;
    XtVaSetValues(vrectangle, XtNlabel, "o Rectangle", NULL);
    break;
  case CIRCLE:
    polygon_motionline.active = FALSE;
    XtVaSetValues(vcircle, XtNlabel, "o Circle", NULL);
    break;
  case POLYGON:
    if (currentshape != POLYGON) {
      polcount = 0;
      polygon_motionline.active = FALSE;
    }
    XtVaSetValues(vpolygon, XtNlabel, "o Polygon", 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));
}