summaryrefslogtreecommitdiffstats
path: root/tksao/widget/widget.C
blob: 5427f644bd313b34d3ced17bdf295a6d4f16a0b9 (plain)
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
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <tkInt.h>

#include "widget.h"
#include "util.h"

// Tk Canvas Widget Functions Declaration

int WidgetConfigProc(Tcl_Interp* interp, Tk_Canvas canvas, Tk_Item* item, 
		     int argc, Tcl_Obj *const argv[], int flags)
{
  return WIDGET(item).configure(argc, (const char**)argv, flags);
}

int WidgetCoordProc(Tcl_Interp* interp, Tk_Canvas canvas, Tk_Item* item, 
		    int argc, Tcl_Obj *const argv[])
{
  return WIDGET(item).coordProc(argc, (char**)argv);
}

void WidgetDeleteProc(Tk_Canvas canvas, Tk_Item* item, Display* display)
{
  delete ((WidgetOptions*)item)->widget;
}

void WidgetDisplayProc(Tk_Canvas canvas, Tk_Item* item, Display* display,
		       Drawable draw, int x, int y, int width, int height)
{
  WIDGET(item).displayProc(draw, x, y, width, height);
}

double WidgetPointProc(Tk_Canvas canvas, Tk_Item* item, double* point)
{
  return WIDGET(item).pointProc(point);
}

int WidgetAreaProc(Tk_Canvas canvas, Tk_Item* item, double* bbox)
{
  return WIDGET(item).areaProc(bbox);
}

int WidgetPostscriptProc(Tcl_Interp* interp, Tk_Canvas canvas,
			 Tk_Item* item, int prepass)
{
  return WIDGET(item).postscriptProc(prepass);
}

void WidgetScaleProc(Tk_Canvas canvas, Tk_Item* item, 
		     double Ox, double Oy , double Sx, double Sy)
{
  WIDGET(item).scaleProc(Ox, Oy, Sx, Sy);
}

void WidgetTranslateProc(Tk_Canvas canvas, Tk_Item* item, double x, double y)
{
  WIDGET(item).translateProc(x, y);
}

int WidgetIndexProc(Tcl_Interp* interp, Tk_Canvas canvas, Tk_Item* item,
		    char indexString, int* indexPtr)
{
  WIDGET(item).indexProc(indexString, indexPtr);
  return 1;
}

void WidgetICursorProc(Tk_Canvas canvas, Tk_Item* item, int index)
{
  WIDGET(item).icursorProc(index);
}

int WidgetSelectionProc(Tk_Canvas canvas, Tk_Item* item,
			int offset, char* buffer, int maxBytes)
{
  WIDGET(item).selectionProc(offset, buffer, maxBytes);
  return 1;
}

void WidgetInsertProc(Tk_Canvas canvas, Tk_Item* item, int index, char* string)
{
  WIDGET(item).insertProc(index, string);
}

void WidgetDCharsProc(Tk_Canvas canvas, Tk_Item* item, int first, int last)
{
  WIDGET(item).dcharsProc(first, last);
}

int WidgetParse(ClientData widget, Tcl_Interp* interp, int argc, 
		const char** argv)
{
  int result;
  Tcl_Preserve(widget);

  if (argc >= 2 && !strncmp(argv[1],"config",6))
    result = ((Widget*)widget)->configCmd(argc-2, argv+2);

  else {
    istringstream istr(ios::in|ios::out);
    ostream ostr(istr.rdbuf());

    for (int i=1; i<argc; i++)
      ostr << argv[i] << " ";
    ostr << ends;

    result = ((Widget*)widget)->parse(istr);
  }

  Tcl_Release(widget);
  return result;
}

// Member Functions

Widget::Widget(Tcl_Interp* interp_, Tk_Canvas canvas_, Tk_Item* item) :
  interp(interp_), canvas(canvas_)
{
  // initialize item ptr to this. This is the method the canvas widget procs
  // know how to call member functions.
  ((WidgetOptions*)item)->widget = this;

  // init members
  options = (WidgetOptions*)item;
  configSpecs = NULL;
  tkwin = Tk_CanvasTkwin(canvas);
  display = Tk_Display(tkwin);
  screenNumber = Tk_ScreenNumber(tkwin);
  visual = Tk_Visual(tkwin);
  depth = Tk_Depth(tkwin);

  pixmap = 0;
  widgetGC = NULL;
  visible = True;

  originX = 0;
  originY = 0;

  // this is needed because of a problem with Tk_ConfigureWidget
  options->cmdName = NULL;
  options->helvetica = NULL;
  options->courier = NULL;
  options->times = NULL;

  cmd = NULL;
  result = TCL_OK;

  // postscript
  psLevel = 2;
  psResolution = 300;
  psColorSpace = RGB;
}

Widget::~Widget()
{
  // free the options
  Tk_FreeOptions(configSpecs, (char*)this->options, display, 0);

  // clean up tcl command
  if (cmd) {
    Tcl_DeleteCommand(interp, cmd);
    delete [] cmd;
  }

  // clean up pixmap resources
  if (pixmap)
    Tk_FreePixmap(display, pixmap);

  if (widgetGC)
    XFreeGC(display, widgetGC);
}

void Widget::error(const char* m)
{
  Tcl_AppendResult(interp, m, NULL);
  result = TCL_ERROR;
}

void Widget::msg(const char* m)
{
  Tcl_AppendResult(interp, m, NULL);
}

int Widget::configure(int argc, const char** argv, int flags)
{
  if (Tk_ConfigureWidget(interp, tkwin, configSpecs, argc, argv, 
			 (char*)this->options, flags) != TCL_OK)
    return TCL_ERROR;

  if (flags != TK_CONFIG_ARGV_ONLY) {
    // create command
    createCommand();
    updateBBox();
    invalidPixmap();
  }
  else {
    // configure command
    if (configSpecs[CONFIGCOMMAND].specFlags & TK_CONFIG_OPTION_SPECIFIED)
      createCommand();

    if ((configSpecs[CONFIGX].specFlags & TK_CONFIG_OPTION_SPECIFIED) ||
	(configSpecs[CONFIGY].specFlags & TK_CONFIG_OPTION_SPECIFIED) ||
	(configSpecs[CONFIGWIDTH].specFlags & TK_CONFIG_OPTION_SPECIFIED) ||
	(configSpecs[CONFIGHEIGHT].specFlags & TK_CONFIG_OPTION_SPECIFIED) ||
	(configSpecs[CONFIGANCHOR].specFlags & TK_CONFIG_OPTION_SPECIFIED))
      updateBBox();

    if ((configSpecs[CONFIGWIDTH].specFlags & TK_CONFIG_OPTION_SPECIFIED) ||
	(configSpecs[CONFIGHEIGHT].specFlags & TK_CONFIG_OPTION_SPECIFIED))
      invalidPixmap();

    if ((configSpecs[CONFIGX].specFlags & TK_CONFIG_OPTION_SPECIFIED) ||
	(configSpecs[CONFIGY].specFlags & TK_CONFIG_OPTION_SPECIFIED) ||
	(configSpecs[CONFIGWIDTH].specFlags & TK_CONFIG_OPTION_SPECIFIED) ||
	(configSpecs[CONFIGHEIGHT].specFlags & TK_CONFIG_OPTION_SPECIFIED) ||
	(configSpecs[CONFIGANCHOR].specFlags & TK_CONFIG_OPTION_SPECIFIED))
      redraw();
  }

  return TCL_OK;
}

// Required Canvas Functions

int Widget::coordProc(int argc, char** argv)
{
  char xStr[TCL_DOUBLE_SPACE], yStr[TCL_DOUBLE_SPACE];

  switch (argc) {

    // print the current values
  case 0:
    Tcl_PrintDouble(interp, options->x, xStr);
    Tcl_PrintDouble(interp, options->y, yStr);
    Tcl_AppendResult(interp, xStr, " ", yStr, NULL);
    return TCL_OK;

    // set current x&y to new values
  case 2: {
    double x0, y0;
    if ((Tk_CanvasGetCoord(interp, canvas, argv[0], &x0) != TCL_OK) ||
	(Tk_CanvasGetCoord(interp, canvas, argv[1], &y0) != TCL_OK))
	    return TCL_ERROR;
    options->x = (int)x0;
    options->y = (int)y0;
    updateBBox();
    return TCL_OK;
  }

 // else, error message
  default:
    Tcl_AppendResult(interp, "wrong # coordinates: expected 0 or 2", NULL);
    return TCL_ERROR;
  }
}

// Widget Display Procedure. It takes the contents of the pixmap and
// after clipping, copies it into the drawable.

void Widget::displayProc(Drawable draw, int clipX, int clipY, 
			int clipWidth, int clipHeight)
{
  if (visible == False)
    return;

  // create bbox
  BBox bb(clipX, clipY, clipX+clipWidth, clipY+clipHeight);

  // update the pixmap from ximage/graphics
  if (updatePixmap(bb) != TCL_OK)
    return; // something is wrong, bail out

  // define pixmap clip region
  // NOTE: it appears that the canvas coord system is 1 to n, width/height = n
  // with the original of value 1,1 located at upper left corner
  int pmX, pmY, pmWidth, pmHeight;

  if (clipX > options->item.x1) {
    pmX = clipX - options->item.x1;
    pmWidth = options->item.x2 - clipX;
  }
  else {
    pmX = 0;
    if ((clipX + clipWidth) < options->item.x2)
      pmWidth = clipX + clipWidth - options->item.x1;
    else
      pmWidth = options->item.x2 - options->item.x1;
  }

  if (clipY > options->item.y1) {
    pmY = clipY - options->item.y1;
    pmHeight = options->item.y2 - clipY;
  } 
  else {
    pmY = 0;
    if ((clipY + clipHeight) < options->item.y2)
      pmHeight = clipY + clipHeight - options->item.y1;
    else
      pmHeight = options->item.y2 - options->item.y1;
  }

  // convert to canvas coords
  short drawX, drawY;
  Tk_CanvasDrawableCoords(canvas, (double)(options->item.x1 + pmX),
			  (double)(options->item.y1 + pmY), &drawX, &drawY);

  // just in case
  if (!widgetGC)
    widgetGC = XCreateGC(display, Tk_WindowId(tkwin), 0, NULL);

  // set the clip region and copy the pixmap into the drawable
  XSetClipOrigin(display, widgetGC, drawX - pmX, drawY - pmY);
  XCopyArea(display, pixmap, draw, widgetGC, pmX, pmY, (unsigned int) pmWidth,
	    (unsigned int) pmHeight, drawX, drawY);
}

double Widget::pointProc(double* point)
{
  double xdiff, ydiff;

  if (point[0] < options->item.x1)
    xdiff = options->item.x1 - point[0];
  else if (point[0] > options->item.x2)
    xdiff = point[0] - options->item.x2;
  else
    xdiff = 0;

  if (point[1] < options->item.y1)
    ydiff = options->item.y1 - point[1];
  else if (point[1] > options->item.y2)
    ydiff = point[1] - options->item.y2;
  else
    ydiff = 0;

  return hypot(xdiff, ydiff);
}

int Widget::areaProc(double* bbox)
{
  if ((bbox[2] <= options->item.x1) || (bbox[0] >= options->item.x2) ||
      (bbox[3] <= options->item.y1) || (bbox[1] >= options->item.y2))
    return -1; // item is outside bbox

  if ((bbox[0] <= options->item.x1) && (bbox[1] <= options->item.y1) &&
      (bbox[2] >= options->item.x2) && (bbox[3] >= options->item.y2))
    return 1; // item is inside bbox

  return 0;   // item overlaps bbox
}

int Widget::postscriptProc(int prepass)
{
  return TCL_OK;
}

void Widget::scaleProc(double Ox, double Oy, double Sx, double Sy)
{
  // translate to (Ox,Oy), scale by (Sx,Sy), translate back from (Ox,Oy)
  options->x = (int)(Ox + (Sx * (options->x - Ox)));
  options->y = (int)(Oy + (Sy * (options->y - Oy)));
  options->width = (int)(options->width * Sx);
  options->height = (int)(options->height * Sy);

  updateBBox();
  invalidPixmap();  // width and height have changed
}

void Widget::translateProc(double deltaX, double deltaY)
{
  options->x += (int)deltaX;
  options->y += (int)deltaY;

  updateBBox();
}

// Subcommand Functions

int Widget::configCmd(int argc, const char** argv)
{
  switch (argc) {
  case 0:
    return Tk_ConfigureInfo(interp, tkwin, configSpecs, (char*)this->options, NULL, 0);
  case 1:
    return Tk_ConfigureInfo(interp, tkwin, configSpecs, 
			    (char*)this->options, argv[0],0);
  default:
    return configure(argc, argv, TK_CONFIG_ARGV_ONLY);
  }
}

void Widget::getHeightCmd()
{
  ostringstream str;
  str << options->height << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);
}

void Widget::getWidthCmd()
{
  ostringstream str;
  str << options->width << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);
}

void Widget::hideCmd()
{
  visible = False;
  redraw();
}

void Widget::queryCursorCmd()
{
    Window root, child;
    int rootx,rooty,winx,winy;
    unsigned int msk;
    XQueryPointer(display,Tk_WindowId(tkwin),&root,&child,
		  &rootx,&rooty,&winx,&winy,&msk);

#ifdef MAC_OSX_TK
    // XQueryPointer: Only a partial implementation
    // only rootx,rooty are valid
    int xx,yy;
    Tk_GetRootCoords(tkwin,&xx,&yy);
    winx = rootx-xx;
    winy = rooty-yy;
#endif

#ifdef _WIN32
    winx = rootx;
    winy = rooty;
#endif

    ostringstream str;
    str << winx << ' ' << winy << ends;
    Tcl_AppendResult(interp, str.str().c_str(), NULL);
}

void Widget::resetCmd()
{
  reset();
  invalidPixmap(); 
  redraw();
}

void Widget::showCmd()
{
  visible = True;
  redraw();
}

// Private Member Functions

void Widget::createCommand()
{
  if (cmd) {
    Tcl_DeleteCommand(interp, cmd);
    if (cmd)
      delete [] cmd;
  }

  cmd = new char[strlen(options->cmdName)+1];
  strcpy(cmd, options->cmdName);
  Tcl_CreateCommand(interp, cmd, WidgetParse, (ClientData)this, NULL);
}

int Widget::checkArgs(int should, int argc, char** argv)
{
  // if should is negative, don't check
  if (should >= 0) {
    if (should != argc) {
      ostringstream str;
      str << should << '\0';
      Tcl_AppendResult(interp, "wrong # args: requires ", str.str().c_str(),
		       " args.", NULL);
      return TCL_ERROR;
    }
    else
      return TCL_OK;
  }

  return TCL_OK;
}

double Widget::getDisplayRatio()
{
#ifdef MAC_OSX_TK
  return 1.416;
#else
  return 25.4/72 * 
    DisplayWidth(display, screenNumber) /
    DisplayWidthMM(display, screenNumber);
#endif
}

void Widget::invalidPixmap()
{
  if (pixmap)
    Tk_FreePixmap(display, pixmap);
  pixmap = 0;
}

void Widget::redraw()
{
  Tk_CanvasEventuallyRedraw(canvas, options->item.x1, options->item.y1, 
			    options->item.x2, options->item.y2);
}

void Widget::redraw(BBox bb)
{
  // bb in canvas coords
  // we need to expand by 1, i don't know why
  Tk_CanvasEventuallyRedraw(canvas, (int)bb.ll[0], (int)bb.ll[1], 
			    (int)bb.ur[0]+1, (int)bb.ur[1]+1);
}

void Widget::redrawNow()
{
  Tk_CanvasEventuallyRedraw(canvas, options->item.x1, options->item.y1, 
			    options->item.x2, options->item.y2);
  Tcl_DoOneEvent(TCL_IDLE_EVENTS);
}

void Widget::redrawNow(BBox bb)
{
  // bb in canvas coords
  // we need to expand by 1, i don't know why
  Tk_CanvasEventuallyRedraw(canvas, (int)bb.ll[0], (int)bb.ll[1], 
			    (int)bb.ur[0]+1, (int)bb.ur[1]+1);
  Tcl_DoOneEvent(TCL_IDLE_EVENTS);
}

void Widget::forceUpdate()
{
  Tcl_DoOneEvent(TCL_IDLE_EVENTS);
}

// Update Bounding Box. For image at (n,n) with size (m,m), 
// bbox is defined from (n,n) to (n+m,n+m)

void Widget::updateBBox()
{
  originX = options->x;
  originY = options->y;

  int& ww = options->width;
  int& hh = options->height;

  // Modify position point using anchor information.
  switch (options->anchor) {
  case TK_ANCHOR_N:
    originX -= ww/2;
    break;
  case TK_ANCHOR_NE:
    originX -= ww;
    break;
  case TK_ANCHOR_E:
    originX -= ww;
    originY -= hh/2;
    break;
  case TK_ANCHOR_SE:
    originX -= ww;
    originY -= hh;
    break;
  case TK_ANCHOR_S:
    originX -= ww/2;
    originY -= hh;
    break;
  case TK_ANCHOR_SW:
    originY -= hh;
    break;
  case TK_ANCHOR_W:
    originY -= hh/2;
    break;
  case TK_ANCHOR_NW:
    break;
  case TK_ANCHOR_CENTER:
    originX -= ww/2;
    originY -= hh/2;
    break;
  }

  // Update item item
  options->item.x1 = originX;
  options->item.y1 = originY;
  options->item.x2 = originX + options->width;
  options->item.y2 = originY + options->height;
}

int Widget::getColor(const char* str)
{
  XColor* cc= NULL;
  // starting with tk8.6, colors are W3C based, not X11
  if (!strncmp(str,"green",5) || !strncmp(str,"GREEN",5))
    cc = Tk_GetColor(interp, tkwin, "lime");
  else
    cc = Tk_GetColor(interp, tkwin, str);

  return cc ? cc->pixel : 0;
}

XColor* Widget::getXColor(const char* str)
{
  // note: Tk_GetColor can return an XColor without valid red,green,blue
  //  if common colorname is used. (uses XAllocNamedColor).
  //  force an hash string instead (uses XAllocColor).

  XColor* cc=NULL;
  if (!strncmp(str,"white",5))
    cc = Tk_GetColor(interp, tkwin, "#ffffff");
  else if (!strncmp(str,"black",5))
    cc = Tk_GetColor(interp, tkwin, "#000000");
  else if (!strncmp(str,"red",3))
    cc = Tk_GetColor(interp, tkwin, "#ff0000");
  else if (!strncmp(str,"green",5))
    cc = Tk_GetColor(interp, tkwin, "#00ff00");
  else if (!strncmp(str,"blue",4))
    cc = Tk_GetColor(interp, tkwin, "#0000ff");
  else if (!strncmp(str,"cyan",4))
    cc = Tk_GetColor(interp, tkwin, "#00ffff");
  else if (!strncmp(str,"magenta",7))
    cc = Tk_GetColor(interp, tkwin, "#ff00ff");
  else if (!strncmp(str,"yellow",6))
    cc = Tk_GetColor(interp, tkwin, "#ffff00");
  else 
    cc = Tk_GetColor(interp, tkwin, str);

  return cc ? cc : Tk_GetColor(interp, tkwin, "white");
}

void Widget::warp(Vector& vv)
{
#if !(_WIN32 || MAC_OSX_TK)
  XWarpPointer(display, None, None, 0, 0, 0, 0, vv[0], vv[1]);
#endif

#ifdef MAC_OSX_TK
  XXWarpPointer(display, None, None, 0, 0, 0, 0, vv[0], vv[1]);
#endif

#ifdef _WIN32
  Window root, child;
  int rootx,rooty,winx,winy;
  unsigned int msk;
  XQueryPointer(display,Tk_WindowId(tkwin),&root,&child,
		&rootx,&rooty,&winx,&winy,&msk);
  int xx,yy;
  Tk_GetRootCoords(tkwin,&xx,&yy);

  XWarpPointer(display, None, Tk_WindowId(tkwin), 0, 0, 0, 0, 
	       rootx-xx+vv[0], rooty-yy+vv[1]);
#endif
}

void Widget::warpTo(Vector& vv)
{
#if !MAC_OSX_TK
  XWarpPointer(display, None, Tk_WindowId(tkwin), 0, 0, 0, 0, vv[0], vv[1]);
#endif

#ifdef MAC_OSX_TK
  XXWarpPointer(display, None, Tk_WindowId(tkwin), 0, 0, 0, 0, vv[0], vv[1]);
#endif
}

void Widget::psHead1(ostream& str, int width, int height)
{
  switch (psColorSpace) {
  case BW:
  case GRAY:
    str << "/picstr " << dec << width << " string def" << endl
	<< width << ' ' << height << " 8 matrix" << endl
	<< "{currentfile picstr readhexstring pop}" << endl
	<< "image" << endl;
    break;
  case RGB:
  case CMYK:
    str << "/picstr " << dec << width*3 << " string def" << endl
	<< width << ' ' << height << " 8 matrix" << endl
	<< "{currentfile picstr readhexstring pop}" << endl
	<< "false 3 colorimage" << endl;
    break;
  }
}

void Widget::psHead2(ostream& str, int width, int height, const char* compress, 
		     const char* decode)
{
  switch (psColorSpace) {
  case BW:
  case GRAY:
    str << "/DeviceGray setcolorspace" << endl;
    break;
  case RGB:
    str << "/DeviceRGB setcolorspace" << endl;
    break;
  case CMYK:
    str << "/DeviceCMYK setcolorspace" << endl;
    break;
  }

  str << "<<" << endl
      << "/ImageType 1" << endl
      << "/Width " << dec << width << endl
      << "/Height " << dec << height << endl
      << "/BitsPerComponent 8" << endl;

  switch (psColorSpace) {
  case BW:
  case GRAY:
    str << "/Decode [0 1]" << endl;
    break;
  case RGB:
    str << "/Decode [0 1 0 1 0 1]" << endl;
    break;
  case CMYK:
    str << "/Decode [0 1 0 1 0 1 0 1]" << endl;
    break;
  }

  str << "/ImageMatrix matrix" << endl
      << "/DataSource currentfile" << endl;

  if (decode)
    str << "/" << decode << "Decode filter" << endl;

  if (compress)
    str << "/" << compress << "Decode filter" << endl;

  str << ">>" << endl
      << "image" << endl;
}

// compute coordinates of lower-left corner of the image
// taking into account the anchor position for the image.
// this mapping is different than in widget.c, due to postscript origin 
// (y is flipped)

Vector Widget::psOrigin()
{
  double xx = options->x;
  double yy = Tk_CanvasPsY(canvas, options->y);

  double ww = options->width;
  double hh = options->height;

  switch (options->anchor) {
  case TK_ANCHOR_N:
    xx -= ww/2;
    yy -= hh;
    break;
  case TK_ANCHOR_NE:
    xx -= ww;
    yy -= hh;
    break;
  case TK_ANCHOR_E:
    xx -= ww;
    yy -= hh/2;
    break;
  case TK_ANCHOR_SE:
    xx -= ww;
    break;
  case TK_ANCHOR_S:
    xx -= ww/2;
    break;
  case TK_ANCHOR_SW:
    break;
  case TK_ANCHOR_W:
    yy -= hh/2;
    break;
  case TK_ANCHOR_NW:
    yy -= hh;
    break;
  case TK_ANCHOR_CENTER:
    xx -= ww/2;
    yy -= hh/2;
    break;
  }

  return Vector(xx,yy);
}

// this routine attemps a work around for a problems with level 2 postscript
// and other software such as dvips and assemble

/*
void Widget::psFix(ostringstream& ostr)
{
  string str = ostr.str();
  const char* buf = str.c_str();
  int size = str.length();
  char* a = (char*)buf;

  while (*a && a<buf+size) {
    // dvips version 5.55. Basically, we can't have %% at the start of a line
    if (*a=='\n' && *(a+1)=='%' && *(a+2)=='%') {
      // reverse the order so that we don't have a %% at the begining of a line
      *a++ = '%';
      *a++ = '%';
      *a++ = '\n';
    }

    // no one uses this anymore
    // assemble. Basically, we can't have % at the start of a line
    else if (*a=='\n' && *(a+1)=='%') {
      // reverse the order so that we don't have a % at the begining of a line
      *a++ = '%';
      *a++ = '\n';
    }

    a++;
  }

  ostr.str(str);
}
*/