File: view.c

package info (click to toggle)
via 1.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,480 kB
  • ctags: 2,266
  • sloc: ansic: 30,757; makefile: 101; sh: 46
file content (1047 lines) | stat: -rwxr-xr-x 31,581 bytes parent folder | download
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
/*
 *  $Id: view.c 3177 2008-04-01 14:47:24Z karstenm $
 *
 *  This file implements views.
 */

/*
 *  Copyright 1993, 1994 University of British Columbia
 *
 *  Permission to use, copy, modify, distribute, and sell this software and its
 *  documentation for any purpose is hereby granted without fee, provided that
 *  the above copyright notice appears in all copies and that both that
 *  copyright notice and this permission notice appear in supporting
 *  documentation. UBC makes no representations about the suitability of this
 *  software for any purpose. It is provided "as is" without express or
 *  implied warranty.
 *
 *  Author: Arthur Pope, UBC Laboratory for Computational Intelligence
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

/* From the local directory: */
#include "vxview.h"

/* From the Vista library: */
#include <viaio/mu.h>
#include <viaio/VEdges.h>
#include <viaio/VImage.h>
#include <viaio/VImageView.h>

/* From X Windows and the Motif toolkit: */
#include <X11/IntrinsicP.h>
#include <X11/keysym.h>
#include <X11/CoreP.h>
#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 5
#include <X11/Xmu/Editres.h>
#endif
#include <Xm/CascadeBG.h>
#include <Xm/Form.h>
#include <Xm/Frame.h>
#include <Xm/List.h>
#include <Xm/Label.h>
#include <Xm/LabelG.h>
#include <Xm/MainW.h>
#include <Xm/PushBG.h>
#include <Xm/Scale.h>

/* From the standard C library: */
#include <math.h>

/* File identification string: */
VRcsId ("$Id: view.c 3177 2008-04-01 14:47:24Z karstenm $");

/* Later in this file: */
static void FileMenuCB (Widget, XtPointer, XtPointer);
static void ViewMenuCB (Widget, XtPointer, XtPointer);
static void HelpMenuCB (Widget, XtPointer, XtPointer);
static void SelectObjectCB (Widget, XtPointer, XtPointer);
static void SelectBandCB (Widget, XtPointer, XtPointer);
static void ImageExposeCB (Widget, XtPointer, XtPointer);
static void DrawEdge (View, VEdge);
static void EdgeToWindowCoords (View, VFloat [2], XPoint *);
static void LocateEdge (View, double, double);
static void UpdatePixelReport (View, int, int);


/*
 *  CreateView
 *
 *  Create a new view.
 */

View CreateView (VBoolean propagate_WM_hints)
{
    View view;
    Widget main_window, work, form, w;
    Pixel pixel;
    XmString str;
    static Arg list_args[] = { { XmNlistSizePolicy, XmCONSTANT },
			       { XmNselectionPolicy, XmMULTIPLE_SELECT } };

    /* Allocate a record for keeping track of the view: */
    view = VNew (ViewRec);
    view->next = views;
    views = view;
    view->filename = view->short_name = NULL;
    view->attributes = NULL;
    view->nobjects = 0;
    view->objects = view->image_object = NULL;
    view->image = view->tmp_image = NULL;
    view->prefs = defaultPrefs;
    view->band = 0;
    view->act_color = view->show_color = view->edge_selected = FALSE;
    view->gc = 0;
    view->attr_shell = view->file_shell = view->pref_shell = NULL;

    /* Create the view's top-level shell: */
    view->view_shell = XVCPS ("view", topLevelShellWidgetClass, topLevelShell,
			      XmNcolormap, VColormapColormap (vcolormap),
			      XmNkeyboardFocusPolicy, XmPOINTER,
			      XmNtitle, defaultTitle,
			      XmNvisual, VColormapVisual (vcolormap),
			      (char *) NULL);
    if (iconPixmap != XmUNSPECIFIED_PIXMAP)
	XtVaSetValues (view->view_shell, XmNiconPixmap, iconPixmap,
		       (char *) NULL);
    XtAddCallback (view->view_shell, XmNdestroyCallback,
		   DestroyView, (XtPointer) view);

    /* If this window is being created at program startup, pass along
       the geometry and iconic resources which supply hints for the
       window manager: */
    if (propagate_WM_hints) {
	String geometry;
	Boolean iconic;

	XtVaGetValues (topLevelShell,
		       XtNgeometry, & geometry, XtNiconic, & iconic,
		       (char *) NULL);
	XtVaSetValues (view->view_shell,
		       XtNgeometry, geometry, XtNiconic, iconic,
		       (char *) NULL);
    }

    /* Participate in the editres protocol: */
#if defined(XtSpecificationRelease) && XtSpecificationRelease >= 5
    XtAddEventHandler (view->view_shell, (EventMask) 0, TRUE,
		       _XEditResCheckMessages, NULL);
#endif

    /* Create the main window widgets: */
    main_window = XmCreateMainWindow (view->view_shell, "main", NULL, 0);
    XtAddCallback (main_window, XmNhelpCallback, ShowHelpDialog, NULL);
    view->menu_bar =
	XmVaCreateSimpleMenuBar (main_window, "menubar",
				 XmVaCASCADEBUTTON, NULL, 0,	/* File */
				 XmVaCASCADEBUTTON, NULL, 0,	/* View */
				 XmVaCASCADEBUTTON, NULL, 0,	/* Help */
				 (char *) NULL);
    XtVaSetValues (view->menu_bar, XmNmenuHelpWidget,
		   XtNameToWidget (view->menu_bar, "button_2"),
		   (char *) NULL);
    work = XVCW ("work", xmFormWidgetClass, main_window, (char *) NULL);
    XmMainWindowSetAreas (main_window, view->menu_bar, NULL, NULL, NULL, work);

    /* Create the view's menus: */
    view->menus[fileMenu] =
	XmVaCreateSimplePulldownMenu (
	    view->menu_bar, "file", fileMenu, FileMenuCB,
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* New */
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* Remove */
	    XmVaSEPARATOR,					/* ---- */
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* Open */
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* Reread */
	    XmVaSEPARATOR,					/* ---- */
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* Quit */
	    (char *) NULL);

    view->menus[viewMenu] =
	XmVaCreateSimplePulldownMenu (
	    view->menu_bar, "view", viewMenu, ViewMenuCB,
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* Attrs */
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* Display */
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* Edge Data */
	    (char *) NULL);

    view->menus[helpMenu] =
	XmVaCreateSimplePulldownMenu (
	    view->menu_bar, "help", helpMenu, HelpMenuCB,
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* Index */
	    XmVaPUSHBUTTON, NULL, 0, NULL, NULL,		/* Version */
	    (char *) NULL);

    /* The main window contains a form on the left, an image on the right: */
    form = XVCW ("form", xmFormWidgetClass, work,
		 XmNtopAttachment, XmATTACH_FORM,
		 XmNleftAttachment, XmATTACH_FORM,
		 XmNresizePolicy, XmRESIZE_NONE, (char *) NULL);

    w = XVCW ("frame", xmFrameWidgetClass, work,
	      XmNtopAttachment, XmATTACH_FORM,
	      XmNbottomAttachment, XmATTACH_FORM,
	      XmNleftAttachment, XmATTACH_WIDGET,
	      XmNleftWidget, form,
	      XmNrightAttachment, XmATTACH_FORM, (char *) NULL);
    XtVaGetValues (form, XmNbackground, & pixel, NULL);
    view->image_view = XVCMW ("image", vImageViewWidgetClass, w,
			      XmNbackground, pixel,
			      XmNheight, appData.size,
			      XmNwidth, appData.size,
			      VxNproportion, TRUE,
			      VxNresize, FALSE,
			      VxNvColormap, vcolormap,
			      (char *) NULL);
    XtManageChild (w);
    XtAddCallback (view->image_view, VxNexposeCallback,
		   ImageExposeCB, (XtPointer) view);

    /* Create control widgets on the left: */
    XVCMW ("object_label", xmLabelGadgetClass, form,
	   XmNtopAttachment, XmATTACH_FORM,
	   XmNleftAttachment, XmATTACH_FORM, (char *) NULL);

    view->object_list = XmCreateScrolledList (form, "object_list",
					      list_args, XtNumber (list_args));
    XtVaSetValues (XtParent (view->object_list),
		   XmNtopAttachment, XmATTACH_FORM,
		   XmNleftAttachment, XmATTACH_FORM,
		   XmNrightAttachment, XmATTACH_FORM, (char *) NULL);
    XtAddCallback (view->object_list, XmNmultipleSelectionCallback,
		   SelectObjectCB, (XtPointer) view);
    XtManageChild (view->object_list);

    XVCMW ("image_label", xmLabelGadgetClass, form,
	   XmNtopAttachment, XmATTACH_WIDGET,
	   XmNtopWidget, XtParent (view->object_list),
	   XmNleftAttachment, XmATTACH_FORM, (char *) NULL);

    view->image_info = XVCMW ("image_info", xmLabelGadgetClass, form,
			      XmNtopAttachment, XmATTACH_WIDGET,
			      XmNtopWidget, XtParent (view->object_list),
			      XmNleftAttachment, XmATTACH_FORM,
			      XmNrightAttachment, XmATTACH_FORM,
			      XmNalignment, XmALIGNMENT_BEGINNING,
			      XmNrecomputeSize, FALSE, (char *) NULL);

    w = XVCMW ("band_label", xmLabelGadgetClass, form,
	       XmNtopAttachment, XmATTACH_WIDGET,
	       XmNtopWidget, view->image_info,
	       XmNleftAttachment, XmATTACH_FORM, (char *) NULL);

    view->band_0 = XVCMW ("band_0", xmLabelWidgetClass, form,
			  XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
			  XmNtopOffset, 0,
			  XmNtopWidget, w,
			  XmNleftAttachment, XmATTACH_FORM,
			  XmNrightAttachment, XmATTACH_FORM,
			  XmNalignment, XmALIGNMENT_BEGINNING,
			  XmNmappedWhenManaged, FALSE, (char *) NULL);

    view->band_scale = XVCMW ("band_scale", xmScaleWidgetClass, form,
			      XmNtopAttachment, XmATTACH_WIDGET,
			      XmNtopWidget, view->image_info,
			      XmNleftAttachment, XmATTACH_FORM,
			      XmNmappedWhenManaged, FALSE,
			      XmNminimum, 0,
			      XmNorientation, XmHORIZONTAL,
			      XmNshowValue, TRUE, (char *) NULL);
    XtAddCallback (view->band_scale, XmNvalueChangedCallback,
		   SelectBandCB, (XtPointer) view);

    view->band_n = XVCMW ("band_n", xmLabelWidgetClass, form,
			  XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
			  XmNtopOffset, 0,
			  XmNtopWidget, w,
			  XmNleftAttachment, XmATTACH_WIDGET,
			  XmNleftWidget, view->band_scale,
			  XmNrightAttachment, XmATTACH_FORM,
			  XmNmappedWhenManaged, FALSE, (char *) NULL);

    XVCMW ("pixel_label", xmLabelGadgetClass, form,
	   XmNtopAttachment, XmATTACH_WIDGET,
	   XmNtopWidget, view->band_scale,
	   XmNleftAttachment, XmATTACH_FORM, (char *) NULL);

    str = XmStringCreateSimple (" ");
    view->pixel_info = XVCMW ("pixel_info", xmLabelGadgetClass, form,
			      XmNtopAttachment, XmATTACH_WIDGET,
			      XmNtopWidget, view->band_scale,
			      XmNleftAttachment, XmATTACH_FORM,
			      XmNrightAttachment, XmATTACH_FORM,
			      XmNalignment, XmALIGNMENT_BEGINNING,
			      XmNlabelString, str,
			      XmNrecomputeSize, FALSE, (char *) NULL);
    XmStringFree (str);

    view->edge_label = XVCMW ("edge_label", xmLabelWidgetClass, form,
			      XmNtopAttachment, XmATTACH_WIDGET,
			      XmNtopWidget, view->pixel_info,
			      XmNleftAttachment, XmATTACH_FORM, (char *) NULL);

    view->edge_info = XVCMW ("edge_info", xmLabelWidgetClass, form,
			     XmNtopAttachment, XmATTACH_WIDGET,
			     XmNtopWidget, view->pixel_info,
			     XmNleftAttachment, XmATTACH_FORM,
			     XmNrightAttachment, XmATTACH_FORM,
			     XmNalignment, XmALIGNMENT_BEGINNING,
			     XmNrecomputeSize, FALSE, (char *) NULL);

    view->point_label = XVCMW ("point_label", xmLabelWidgetClass, form,
			       XmNtopAttachment, XmATTACH_WIDGET,
			       XmNtopWidget, view->edge_info,
			       XmNleftAttachment, XmATTACH_FORM,
			       (char *) NULL);

    view->point_info = XVCMW ("point_info", xmLabelWidgetClass, form,
			      XmNtopAttachment, XmATTACH_WIDGET,
			      XmNtopWidget, view->edge_info,
			      XmNleftAttachment, XmATTACH_FORM,
			      XmNrightAttachment, XmATTACH_FORM,
			      XmNalignment, XmALIGNMENT_BEGINNING,
			      XmNrecomputeSize, FALSE, (char *) NULL);

    XtManageChild (form);
    XtManageChild (work);
    XtManageChild (view->menu_bar);
    XtManageChild (main_window);

    nviews++;

    return view;
}


/*
 *  DestroyView
 *
 *  Remove a view.
 *  Arguments are defined for use as a callback.
 */

/* ARGSUSED */
void DestroyView (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    View view = (View) cl_data, prev;

    /* Delete it from the list of views: */
    if (view == views)
	views = views->next;
    else {
	for (prev = views; prev && prev->next != view; prev = prev->next) ;
	if (! prev || prev->next != view)
	    VError ("Internal error in DestroyView");
	prev->next = view->next;
    }

    /* Close any file open in the view: */
    CloseFile (view);

    /* Destroy widgets used by the view: */
    XtDestroyWidget (view->view_shell);

    VFree (view);
    nviews--;
}


/*
 *  Menu item callbacks.
 *
 *	FileMenuCB	File menu item selected
 *	ViewMenuCB	View menu item selected
 *	HelpMenuCB	Help menu item selected
 */

/* ARGSUSED */
static void FileMenuCB (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    View view = MapWidgetToView (w);
    VStringConst filename;
    VAttrList attributes;

    if (! view)
	return;

    switch ((int) cl_data) {

    case newButton:
	ShowFileSelectDialog (NULL);
	break;

    case removeButton:
	XtDestroyWidget (view->view_shell);
	break;

    case openButton:
	ShowFileSelectDialog (view);
	break;

    case rereadButton:
	if (ReadFile (view->filename, & attributes)) {
	    filename = VNewString (view->filename);
	    CloseFile (view);
	    view->filename = filename;
	    view->attributes = attributes;
	    ShowFile (view, -1);
	}
	break;

    case quitButton:
	exit (EXIT_SUCCESS);
    }
}

/* ARGSUSED */
static void ViewMenuCB (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    View view = MapWidgetToView (w);

    if (! view)
	return;

    switch ((int) cl_data) {

    case attrsButton:
	ShowAttrDialog (view);
	break;

    case displayButton:
	ShowPrefDialog (view);
	break;

    case edgeDataButton:
	ShowEdgeData (view);
    }
}

/* ARGSUSED */
static void HelpMenuCB (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    View view = MapWidgetToView (w);

    if (! view)
	return;

    switch ((int) cl_data) {

    case helpIndexButton:
	ShowHelpDialog (NULL, NULL, NULL);
	break;

    case helpVersionButton:
	ShowVersionDialog (view);
    }
}


/*
 *  SelectObjectCB
 *
 *  Called when an object list entry is selected.
 */

/* ARGSUSED */
static void SelectObjectCB (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    View view = (View) cl_data;
    int object = ((XmListCallbackStruct *) ca_data)->item_position - 1;
    ViewObject *obj = & view->objects[object];

    obj->visible = ! obj->visible;
    switch (obj->type) {

    case VEdgesRepn:

	/* If deselecting an edge set, and an edge in that set was selected,
	   deselect the edge also: */
	if (! obj->visible && view->edge_selected &&
	    view->sel_edges_idx == object)
	    SelectEdge (view, FALSE, 0, 0, NULL, 0);
	UpdateView (view, edgeSetChange);
	break;

    case VImageRepn:

	/* If selecting a new image deselect any previous one: */
	if (obj->visible && view->image_object) {
	    view->image_object->visible = FALSE;
	    XmListDeselectPos (w, view->image_object - view->objects + 1);
	}
	view->band = 0;
	UpdateView (view, imageChange);
	break;

    default:
	break;
    }
}


/*
 *  SelectBandCB
 *
 *  Called when an image band is chosen.
 */

/* ARGSUSED */
static void SelectBandCB (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    View view = (View) cl_data;
    XmScaleCallbackStruct *cb = (XmScaleCallbackStruct *) ca_data;

    /* Make the slider jump to an integer position: */
    XmScaleSetValue (w, cb->value);
    /* ==> Redraw the widget because XmScale seems to fail to redraw
       the value label at the new slider position. */
    (w->core.widget_class->core_class.expose) (w, NULL, NULL);

    if (cb->value == view->band)
	return;

    view->band = cb->value;
    XtVaSetValues (view->image_view, VxNband, view->band, (char *) NULL);
}


/*
 *  ImageExposeCB
 *
 *  Called when the image is redrawn.
 */

/* ARGSUSED */
static void ImageExposeCB (Widget w, XtPointer cl_data, XtPointer ca_data)
{
    View view = (View) cl_data;
    ViewObject *obj;
    int i, j;
    Pixel color;
    VEdge e;

    /* First time through (after widgets have been realized) allocate a
       graphics context for drawing edges over the image: */
    if (! view->gc)
	view->gc = XCreateGC (myDisplay, XtWindow (view->image_view), 0, NULL);

    /* Draw each edge set chosen for display: */
    for (i = j = 0, obj = view->objects; i < view->nobjects; i++, obj++)
	if (obj->visible && obj->type == VEdgesRepn) {

	    /* Choose a color for the edge set: */
	    color = appData.edge_set_colors[j++ % numberEdgeSetColors];
	    XSetForeground (myDisplay, view->gc, color);

	    /* Draw each edge: */
	    for (e = VFirstEdge (obj->edges);
		 VEdgeExists (e); e = VNextEdge (e))
		DrawEdge (view, e);
	}

    /* If an edge is selected, redraw it in the hilite color: */
    if (view->edge_selected) {
	XSetForeground (myDisplay, view->gc, appData.hilite_color);
	if (view->prefs.edge.lines)
	    DrawEdge (view, view->sel_edge);
	else {
	    VFloat *ept = VEdgePointArray (view->sel_edge) [view->sel_pt_idx];
	    XPoint xpt;

	    EdgeToWindowCoords (view, ept, & xpt);
	    if (view->prefs.edge.endpoints &&
		(view->sel_pt_idx == 0 ||
		 view->sel_pt_idx == VEdgeNPoints (view->sel_edge) - 1))
		XDrawRectangle (myDisplay, XtWindow (view->image_view),
				view->gc, xpt.x - 1, xpt.y - 1, 2, 2);
	    else XDrawPoint (myDisplay, XtWindow (view->image_view),
			     view->gc, xpt.x, xpt.y);
	}
    }
}


/*
 *  DrawEdge
 *
 *  Draw an edge overtop the image using the current graphic context.
 */

static void DrawEdge (View view, VEdge e)
{
    int i;
    static int npoints = 0;
    static XPoint *points = NULL;
    static XRectangle rectangles[2] = { { 0, 0, 2, 2, },
					{ 0, 0, 2, 2, } };

    /* Ensure that the points vector is large enough to hold all of the
       edges points: */
    if (npoints < VEdgeNPoints (e)) {

	VFree (points);
	npoints = VMax (VEdgeNPoints (e) + 100, 1000);
	points = VMalloc (sizeof (XPoint) * npoints);
    }

    /* Convert each edge point to window coordinates: */
    for (i = 0; i < VEdgeNPoints (e); i++)
	EdgeToWindowCoords (view, VEdgePointArray (e)[i], & points[i]);

    /* Draw the edge: */
    if (view->prefs.edge.points)
	XDrawPoints (myDisplay, XtWindow (view->image_view), view->gc,
		     points, VEdgeNPoints (e), CoordModeOrigin);
    if (view->prefs.edge.lines)
	XDrawLines (myDisplay, XtWindow (view->image_view), view->gc,
		    points, VEdgeNPoints (e), CoordModeOrigin);
    if (view->prefs.edge.endpoints) {
	rectangles[0].x = points[0].x - 1;
	rectangles[0].y = points[0].y - 1;
	rectangles[1].x = points[VEdgeNPoints (e) - 1].x - 1;
	rectangles[1].y = points[VEdgeNPoints (e) - 1].y - 1;
	XDrawRectangles (myDisplay, XtWindow (view->image_view),
			 view->gc, rectangles, 2);
    }
}

static void EdgeToWindowCoords (View view, VFloat ept[2], XPoint *xpt)
{
    int nrows = VImageNRows (view->tmp_image ? view->tmp_image : view->image);
    int ipt[2];

    VImageViewImageToWindow (view->image_view, nrows - ept[1], ept[0],
			     & ipt[0], & ipt[1]);
    xpt->x = ipt[0];
    xpt->y = ipt[1];
}


/*
 *  LocateEdge
 *
 *  Select an edge in response to a button click at (row,column) on the image.
 */

static void LocateEdge (View view, double row, double column)
{
    double width, height, x, y, t;
    int edges_idx, edge_idx, pt_idx;
    enum { first_search, resume_search, cont_search } state;
    VBoolean lap = FALSE;
    VEdge edge;
    VFloat *pt;

    x = column;
    y = VImageNRows (view->tmp_image ? view->tmp_image : view->image) - row;

    /* Compute a range threshold, in image pixels, based on the
       current zoom level: */
    VImageViewPixelSize (view->image_view, & width, & height);
    t = VMax (appData.select_range / width,
	      appData.select_range / height);
    t = VMax (t, 1.0);

    /* If the new click was within range of the last one, resume the
       search for an edge point with the last edge point selected, otherwise
       start from the first point, edge, and edge set: */
    state = (view->edge_selected && row >= view->sel_row - t &&
	     row <= view->sel_row + t && column >= view->sel_column - t  &&
	     column <= view->sel_column + t) ? resume_search : first_search;
    while (1) {
	for (edges_idx = 0; edges_idx < view->nobjects; edges_idx++) {
	    if (view->objects[edges_idx].type != VEdgesRepn ||
		! view->objects[edges_idx].visible)
		continue;
	    for (edge = VFirstEdge (view->objects[edges_idx].edges),
		 edge_idx = 0; VEdgeExists (edge);
		 edge = VNextEdge (edge), edge_idx++) {
		for (pt_idx = 0; pt_idx < VEdgeNPoints (edge); pt_idx++) {
		    if (state == resume_search || state == cont_search)
			lap = (edges_idx == view->sel_edges_idx &&
			       edge_idx == view->sel_edge_idx &&
			       pt_idx == view->sel_pt_idx);
		    if (state == resume_search) {
			if (lap)
			    state = cont_search;
		    } else {
			pt = VEdgePointArray (edge)[pt_idx];
			if (pt[0] >= x - t && pt[0] <= x + t &&
			    pt[1] >= y - t && pt[1] <= y + t) {
			    view->sel_row = row;
			    view->sel_column = column;
			    SelectEdge (view, TRUE, edges_idx, edge_idx,
					edge, pt_idx);
			    return;
			}
			if (lap && state == cont_search) {
			    SelectEdge (view, FALSE, 0, 0, NULL, 0);
			    return;
			}
		    }
		}
	    }
	}
	if (state == first_search) {
	    SelectEdge (view, FALSE, 0, 0, NULL, 0);
	    return;
	}
    }
}


/*
 *  Action procedures.
 *
 *  These procedures are tied, via a translation table, to certain events
 *  input via the VImageView widget.
 *
 *	ImageMoveAction		moves the pointer up, left, down, or right
 *				under keyboard control
 *
 *	ImageReportAction	updates report about the pointed-to pixel
 *
 *	ImageSelectAction	(un)selects an edge near the pointer
 */

/* ARGSUSED */
void ImageMoveAction (Widget w, XEvent *event,
		      String *params, Cardinal *nparams)
{
    View view = MapWidgetToView (w);
    int row, column;
    double width, height;

    if (*nparams != 1) {
	VWarning ("ImageMove() action must have exactly one parameter");
	return;
    }

    if (VImageViewPixelSize (view->image_view, & width, & height)) {
	row = (int) VMax (height, 1.0);
	column = (int) VMax (width, 1.0);
    } else row = column = 1;

    if (strcmp (params[0], "left") == 0)
	XWarpPointer (myDisplay, XtWindow (view->image_view), None,
		      0, 0, 0, 0, -column, 0);
    else if (strcmp (params[0], "up") == 0)
	XWarpPointer (myDisplay, XtWindow (view->image_view), None,
		      0, 0, 0, 0, 0, -row);
    else if (strcmp (params[0], "right") == 0)
	XWarpPointer (myDisplay, XtWindow (view->image_view), None,
		      0, 0, 0, 0, column, 0);
    else if (strcmp (params[0], "down") == 0)
	XWarpPointer (myDisplay, XtWindow (view->image_view), None,
		      0, 0, 0, 0, 0, row);
    else VWarning ("ImageMove() action's parameter must be up, left, "
		   "down, or right");
}

/* ARGSUSED */
void ImageReportAction (Widget w, XEvent *event,
			String *params, Cardinal *nparams)
{
    View view = MapWidgetToView (w);
    XmString str;
    char *text;

    XtVaGetValues (view->pixel_info, XmNlabelString, & str, (char *) NULL);
    XmStringGetLtoR (str, XmSTRING_DEFAULT_CHARSET, & text);
    XmStringFree (str);
    printf ("%s\n", text);
    XtFree ((XtPointer) text);
}

/* ARGSUSED */
void ImageSelectAction (Widget w, XEvent *event,
			String *params, Cardinal *nparams)
{
    View view = MapWidgetToView (w);
    int win_x, win_y, root_x, root_y;
    double row, column;
    Window root, child;
    unsigned int keys_buttons;

    switch (event->type) {

    case KeyPress:
    case KeyRelease:
	win_x = event->xkey.x;
	win_y = event->xkey.y;
	break;

    case ButtonPress:
    case ButtonRelease:
	win_x = event->xbutton.x;
	win_y = event->xbutton.y;
	break;

    default:
	if (! XQueryPointer (myDisplay, XtWindow (view->image_view),
			     & root, & child, & root_x, & root_y,
			     & win_x, & win_y, & keys_buttons))
	    return;
    }
    if (VImageViewWindowToImage (view->image_view,
				 win_x, win_y, & row, & column)) {
	LocateEdge (view, row, column);
	UpdateView (view, edgeSelChange);
    }
}

/* ARGSUSED */
void ImageTrackAction (Widget w, XEvent *event,
		       String *params, Cardinal *nparams)
{
    View view = MapWidgetToView (w);
    int root_x, root_y, win_x, win_y;
    Window root, child;
    unsigned int keys_buttons;

    switch (event->type) {

    case LeaveNotify:
	win_x = win_y = -1;
	break;

    case MotionNotify:
	win_x = event->xmotion.x;
	win_y = event->xmotion.y;
	break;

    default:
	if (! XQueryPointer (myDisplay, XtWindow (view->image_view),
			     & root, & child, & root_x, & root_y,
			     & win_x, & win_y, & keys_buttons))
	    win_x = win_y = -1;
    }
    UpdatePixelReport (view, win_x, win_y);
}


/*
 *  UpdateView
 *
 *  Update the depiction of an image and/or some edge sets as a result of
 *  some user-directed change. The nature of the change is indicated by the
 *  changes parameter:
 *
 *	imageChange	different image selected for viewing
 *	edgeSetChange	different edge sets selected for viewing
 *	ignoreBIChange	change in whether band_interp ignored
 *	intensityChange	change in absolute/signed intensity interpretation
 *	contrastChange	change in brightness or contrast adjustment
 *	edgeChange	change in edge drawing options
 *	edgeSelChange	different edge selected
 */

void UpdateView (View view, int changes)
{
    int nbands, nrows, ncolumns, i, n;
    VRepnKind repn;
    Arg args[20];
    XmString str;
    char buf[100];

    if (changes & (imageChange | ignoreBIChange)) {

	/* Note the image object now chosen for display: */
	for (i = 0; i < view->nobjects; i++)
	    if (view->objects[i].type == VImageRepn &&
		view->objects[i].visible)
		break;
	if (i < view->nobjects) {
	    view->image_object = & view->objects[i];
	    view->image = view->image_object->image;
	    view->act_color =
		(VImageNBands (view->image) == 3 &&
		 VImageColorInterp (view->image) == VBandInterpRGB);
	    view->show_color =
		view->act_color && ! view->prefs.image.ignore_BI;
	} else {
	    view->image_object = NULL;
	    view->image = NULL;
	    view->act_color = view->show_color = FALSE;
	}

	/* Format a string describing the image: */
	if (view->image)
	    sprintf (buf, "%d rows, %d cols, %s",
		     VImageNRows (view->image), VImageNColumns (view->image),
		     VPixelRepnName (view->image));
	else buf[0] = 0;
	str = XmStringCreateSimple (buf);
	XtVaSetValues (view->image_info, XmNlabelString, str, (char *) NULL);
	XmStringFree (str);

	/* Tailor the band selection widgets for this image: */
	if (! view->image || VImageNBands (view->image) == 1 ||
	    view->show_color) {
	    str = XmStringCreateSimple (view->show_color ?
					"(3-Band Color)" :
					"(Single Band)");
	    XtVaSetValues (view->band_0, XmNlabelString, str, (char *) NULL);
	    XmStringFree (str);
	    XtSetMappedWhenManaged (view->band_0, TRUE);
	    XtSetMappedWhenManaged (view->band_n, FALSE);
	    XtSetMappedWhenManaged (view->band_scale, FALSE);
	} else {
	    XtVaSetValues (view->band_scale,
			   XmNmaximum, VImageNBands (view->image) - 1,
			   XmNvalue, view->band, (char *) NULL);
	    sprintf (buf, "of [0,%d]", VImageNBands (view->image) - 1);
	    str = XmStringCreateSimple (buf);
	    XtVaSetValues (view->band_n, XmNlabelString, str, (char *) NULL);
	    XmStringFree (str);
	    XtSetMappedWhenManaged (view->band_0, FALSE);
	    XtSetMappedWhenManaged (view->band_n, TRUE);
	    XtSetMappedWhenManaged (view->band_scale, TRUE);
	}
    }

    if ((changes & (imageChange | ignoreBIChange)) ||
	((changes & edgeSetChange) && ! view->image)) {

	/* Determine new dimensions for the image display area: */
	if (view->image) {
	    nbands = VImageNBands (view->image);
	    nrows = VImageNRows (view->image);
	    ncolumns = VImageNColumns (view->image);
	    repn = VPixelRepn (view->image);
	} else {

	    /* If no image is being displayed, use the dimensions of the first
	       edge set that is being displayed: */
	    nbands = nrows = ncolumns = 1;
	    repn = VBitRepn;
	    for (i = 0; i < view->nobjects; i++)
		if (view->objects[i].type == VEdgesRepn &&
		    view->objects[i].visible) {
		    nrows = VEdgesNRows (view->objects[i].edges);
		    ncolumns = VEdgesNColumns (view->objects[i].edges);
		    break;
		}
	}

	/* Discard any temporary image if it doesn't have the same
	   properties as the image to be displayed: */
	if (view->tmp_image &&
	    (VImageNBands (view->tmp_image) != nbands ||
	     VImageNRows (view->tmp_image) != nrows ||
	     VImageNColumns (view->tmp_image) != ncolumns ||
	     VPixelRepn (view->tmp_image) != repn)) {
	    VDestroyImage (view->tmp_image);
	    view->tmp_image = NULL;
	    changes |= imageChange;
	}

	/* Create a blank background image if necessary: */
	if (! view->image) {
	    if (! view->tmp_image)
		view->tmp_image = VCreateImage (nbands, nrows, ncolumns, repn);
	    VFillImage (view->tmp_image, VAllBands, 0.0);
	}
    }

    /* Perform any required contrast adjustment or stripping of the band
       interpretation attribute (when "Ignore Band Interpretation" mode is
       selected): */
    if (view->image &&
	(changes & (imageChange | ignoreBIChange | contrastChange))) {
	VImage tmp_image = view->tmp_image;

	view->tmp_image = NULL;

	/* Create a contrast-adjusted version of the image: */
	if (view->prefs.image.brightness != 0 ||
	    view->prefs.image.contrast != 0)
	    view->tmp_image =
		VAdjustImage (view->image, tmp_image, VAllBands,
			      (double) view->prefs.image.brightness,
			      (double) view->prefs.image.contrast);

	/* If the image has an RGB band interpretation, but that attribute
	   is to be ignored, then ensure we're displaying a copy of the image
	   with ncolors set to 1: */
	if (view->act_color && view->prefs.image.ignore_BI &&
	    ! view->tmp_image) {
	    view->tmp_image = VCopyImage (view->image, tmp_image, VAllBands);
	    VExtractAttr (VImageAttrList (view->tmp_image),
			  VColorInterpAttr, NULL, VLongRepn, NULL, FALSE);
	}

	/* Discard any temporary image we didn't end up needing: */
	if (tmp_image && tmp_image != view->tmp_image) {
	    VDestroyImage (tmp_image);
	    view->tmp_image = NULL;
	}
    }

    /* Pass new resource values to the image display widget: */
    n = 0;
    if (changes & (imageChange | ignoreBIChange | contrastChange)) {
	XtSetArg (args[n], VxNimage,
		  view->tmp_image ? view->tmp_image : view->image); n++;
	XtSetArg (args[n], VxNband, view->band); n++;
	XtSetArg (args[n], VxNabsolute,
		  view->prefs.image.intensity == IntensityAbsolute); n++;
    } else if (changes & intensityChange) {
	XtSetArg (args[n], VxNabsolute,
		  view->prefs.image.intensity == IntensityAbsolute); n++;
    }
    if (n > 0)
	XtSetValues (view->image_view, args, (unsigned int) n);
    else VImageViewRedraw (view->image_view);

    /* Update the report of pointer location and pixel value if the
       underlying image may have changed: */
    if ((changes & (imageChange | ignoreBIChange)) &&
	XtIsRealized (view->view_shell)) {
	Window root, child;
	int root_x, root_y, win_x, win_y;
	unsigned int keys_buttons;

	if (! XQueryPointer (XtDisplay (view->image_view),
			     XtWindow (view->image_view),
			     & root, & child, & root_x, & root_y,
			     & win_x, & win_y, & keys_buttons))
	    win_x = win_y = -1;
	UpdatePixelReport (view, win_x, win_y);
    }
}


/*
 *  UpdatePixelReport
 *
 *  Update the display of the current pixel coordinates and value.
 */

static void UpdatePixelReport (View view, int x, int y)
{
    double row, column;
    int r, c;
    char buf[100];
    XmString str;

    if (VImageViewWindowToImage (view->image_view, x, y, & row, & column)) {
	r = row;
	c = column;
	if (view->image) {
	    if (view->show_color)
		sprintf (buf, "[%d,%d]:  %g %g %g", r, c,
			 VGetPixel (view->image, 0, r, c),
			 VGetPixel (view->image, 1, r, c),
			 VGetPixel (view->image, 2, r, c));
	    else
		sprintf (buf, "[%d,%d]:  %g", r, c,
			 VGetPixel (view->image, view->band, r, c));
	} else sprintf (buf, "[%d, %d]", r, c);
    } else buf[0] = 0;
    str = XmStringCreateSimple (buf);
    XtVaSetValues (view->pixel_info, XmNlabelString, str, (char *) NULL);
    XmStringFree (str);
}