summaryrefslogtreecommitdiff
path: root/libgui/src/tclwinprint.c
blob: 221cc14bf772a96b0d625c8c35c8e37a520e9ea4 (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
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
/* tclwinprint.c -- Tcl routines for printing on Windows.
   Copyright (C) 1997 Cygnus Solutions.
   Written by Ian Lance Taylor <ian@cygnus.com>.

   This file contains routines to support printing on Windows from
   Tcl.  */

#ifdef _WIN32

#include <windows.h>

#include <tcl.h>
#include <tk.h>

#include "subcommand.h"
#include "guitcl.h"

#include <wingdi.h>

#undef PRINT_BUFSIZE
#define PRINT_BUFSIZE 10240

/* FIXME: We need to dig into the Tk window implementation internals
   to convert a Tk Window to an HWND.  */

#include <tkWinInt.h>

/* This implementation is minimal.  It's just enough to print a text
   file.  Additional features can be added as necessary.

   One interesting idea that would fit into the Windows printing
   scheme would be to have printing generate a limited canvas widget,
   and permit Tk scripts to use canvas commands to draw items on the
   page.

   This file defines a Tcl command with subcommands.

   ide_winprint page_setup OPTIONS
       Invoke the Windows Page Setup dialog.  This will record
       information internally that will be used for later printing.

       Supported options:
           -parent WINDOW
	       Set the parent window of the dialog box.  The dialog
	       box is modal with respect to this window.  The default
	       is the main window.

   ide_winprint print_text QUERYPROC TEXTPROC OPTIONS
       Print text.  This will start formatting the print job.  The
       user will still be able to interact with Tk.  Typically, a
       dialog box would be put up with a cancel button to permit the
       user to cancel the print job by calling ide_winprint abort.

       The QUERYPROC argument is a Tcl procedure which tells the print
       job what to do next.  This is invoked alternately with the text
       procedure until the print job is finished.  QUERYPROC is called
       first.  This should return one of the following strings:
	   continue
	       Just invoke the text procedure and continue
	       printing.
	   done
	       The print job is finished.
	   newpage
	       Skip to a new page and continue printing.

       The TEXTPROC argument is a Tcl procedure which returns a single
       line of text to print.  This procedure will be invoked
       alternately with the query procedure until the query procedure
       indicates that the print job is complete.  Page breaks are
       handled automatically.

       Supported options:
           -dialog BOOLEAN
	       Whether to display the Windows Print dialog.  The
	       default is true.  If false, this will use the default
	       printer.
           -parent WINDOW
	       Set the parent window of the dialog box.  The dialog
	       box is modal with respect to this window.  The default
	       is the main window.
	   -name STRING
	       Set the name of the document.  The default name is the
	       empty string.
	   -pageproc PAGEPROC
	       PAGEPROC is executed at the start of each new page.  It
	       will be called with one argument, which is the page
	       number.  It will be called before either QUERYPROC or
	       TEXTPROC is called on this page.  If QUERYPROC never
	       returns newpage, then PAGEPROC will always be invoked
	       after a call to TEXTPROC.  PAGEPROC should return one
	       of the following strings:
	           continue
		       Keep going.
		   done
		       Stop printing.
           -postscript
	       Use PostScript output.
	   -initproc INITPROC
	       INITPROC is called at the start of the print job.

   ide_winprint abort
       Abort a print job in process.  If there is no current print
       job, this does nothing.

   */

/* An instance of this structure is the client data for the
   ide_winprint command.  */

struct winprint_data
{
  /* Information from the Page Setup dialog.  */
  PAGESETUPDLG *page_setup;
  /* This is set non-zero if the print job is aborted.  */
  int aborted;
};

/* Delete the ide_winprint command.  */

static void
winprint_command_deleted (ClientData cd)
{
  struct winprint_data *wd = (struct winprint_data *) cd;

  if (wd->page_setup != NULL)
    {
      /* FIXME: I don't know if we are supposed to free the hDevMode
         and hDevNames fields.  */
      Tcl_Free ((char *) wd->page_setup);
    }

  Tcl_Free ((char *) wd);
}

/* Implement ide_winprint page_setup.  */

static int
winprint_page_setup_command (ClientData cd, Tcl_Interp *interp, int argc,
			     char **argv)
{
  struct winprint_data *wd = (struct winprint_data *) cd;
  Tk_Window parent;
  int i, mode, ret;
  PAGESETUPDLG psd;

  parent = Tk_MainWindow (interp);

  for (i = 2; i < argc; i += 2)
    {
      if (i + 1 >= argc)
	{
	  Tcl_ResetResult (interp);
	  Tcl_AppendStringsToObj (Tcl_GetObjResult (interp),
				  "value for \"", argv[i], "\" missing",
				  (char *) NULL);
	  return TCL_ERROR;
	}

      if (strcmp (argv[i], "-parent") == 0)
	{
	  parent = Tk_NameToWindow (interp, argv[i + 1],
				    Tk_MainWindow (interp));
	  if (parent == NULL)
	    return TCL_ERROR;
	}
      else
	{
	  Tcl_ResetResult (interp);
	  Tcl_AppendStringsToObj (Tcl_GetObjResult (interp),
				  "unknown option \"", argv[i], "\"",
				  (char *) NULL);
	  return TCL_ERROR;
	}
    }

  if (wd->page_setup != NULL)
    psd = *wd->page_setup;
  else
    {
      memset (&psd, 0, sizeof (PAGESETUPDLG));
      psd.lStructSize = sizeof (PAGESETUPDLG);
      psd.Flags = PSD_DEFAULTMINMARGINS;
    }

  if (Tk_WindowId (parent) == None)
    Tk_MakeWindowExist (parent);
  psd.hwndOwner = Tk_GetHWND (Tk_WindowId (parent));

  mode = Tcl_SetServiceMode (TCL_SERVICE_ALL);

  ret = PageSetupDlg (&psd);

  (void) Tcl_SetServiceMode (mode);

  if (! ret)
    {
      DWORD code;

      code = CommDlgExtendedError ();
      if (code == 0)
	{
	  /* The user pressed cancel.  */
	  return TCL_OK;
	}
      else
	{
	  char buf[20];

	  sprintf (buf, "0x%lx", (unsigned long) code);
	  Tcl_ResetResult (interp);
	  Tcl_AppendStringsToObj (Tcl_GetObjResult (interp),
				  "Windows common dialog error ", buf,
				  (char *) NULL);
	  return TCL_ERROR;
	}
    }

  if (wd->page_setup == NULL)
    wd->page_setup = (PAGESETUPDLG *) Tcl_Alloc (sizeof (PAGESETUPDLG));

  *wd->page_setup = psd;

  return TCL_OK;
}

/* The abort function needs a static variable (ewww).  */

static struct winprint_data *abort_wd;

/* This is the abort function we pass to the Windows print routine.  */

static BOOL CALLBACK
abort_function (HDC hdc, int code)
{
  while (Tcl_DoOneEvent (TCL_DONT_WAIT))
    ;
  return ! abort_wd->aborted;
}

/* Handle an error in a Windows system call.  */

static void
windows_error (Tcl_Interp *interp, const char *fn)
{
  char buf[20];

  sprintf (buf, "%lu", (unsigned long) GetLastError ());
  Tcl_ResetResult (interp);
  Tcl_AppendStringsToObj (Tcl_GetObjResult (interp),
			  "Windows error in ", fn, ": ", buf, (char *) NULL);
}

/* This structure holds the options for the ide_winprint print_text
   command.  */

struct print_text_options
{
  /* Whether to use the print dialog.  */
  int dialog;
  /* The parent window.  */
  char *parent;
  /* The document name.  */
  char *name;
  /* The page procedure.  */
  char *pageproc;
  /* The init procedure. This is called once before printing. */
  char *initproc;
  /* Print using PostScript? */
  int postscript;
};

/* Handle options for the ide_winprint print_text command.  */

static int
winprint_print_text_options (struct winprint_data *wd, Tcl_Interp *interp,
			     int argc, char **argv,
			     struct print_text_options *pto)
{
  int i;

  pto->dialog = 1;
  pto->parent = NULL;
  pto->name = "";
  pto->pageproc = NULL;
  pto->postscript = 0;
  pto->initproc = NULL;
  
  for (i = 4; i < argc; i += 2)
    {
      if (i + 1 >= argc)
	{
	  Tcl_ResetResult (interp);
	  Tcl_AppendStringsToObj (Tcl_GetObjResult (interp),
				  "value for \"", argv[i], "\" missing",
				  (char *) NULL);
	  return TCL_ERROR;
	}

      if (strcmp (argv[i], "-dialog") == 0)
	{
	  if (Tcl_GetBoolean (interp, argv[i + 1], &pto->dialog) != TCL_OK)
	    return TCL_ERROR;
	}
      else if (strcmp (argv[i], "-parent") == 0)
	pto->parent = argv[i + 1];
      else if (strcmp (argv[i], "-name") == 0)
	pto->name = argv[i + 1];
      else if (strcmp (argv[i], "-pageproc") == 0)
	pto->pageproc = argv[i + 1];
      else if (strcmp (argv[i], "-initproc") == 0)
	pto->initproc = argv[i + 1];
      else if (strcmp (argv[i], "-postscript") == 0)
	pto->postscript = 1;
      else
	{
	  Tcl_ResetResult (interp);
	  Tcl_AppendStringsToObj (Tcl_GetObjResult (interp),
				  "unknown option \"", argv[i], "\"",
				  (char *) NULL);
	  return TCL_ERROR;
	}
    }

  return TCL_OK;
}

/* Invoke the print dialog for the ide_winprint print_text command.
   We always call PrintDlg, even if the -dialog false option was used,
   because it returns the device context we use for printing.  */

static int
winprint_print_text_dialog (struct winprint_data *wd, Tcl_Interp *interp,
			    const struct print_text_options *pto,
			    PRINTDLG *pd, int *cancelled)
{
  int mode, ret;

  *cancelled = 0;

  memset (pd, 0, sizeof (PRINTDLG));
  pd->lStructSize = sizeof (PRINTDLG);

  if (! pto->dialog)
    pd->Flags = PD_RETURNDEFAULT | PD_RETURNDC;
  else
    {
      Tk_Window parent;

      if (pto->parent == NULL)
	parent = Tk_MainWindow (interp);
      else
	{
	  parent = Tk_NameToWindow (interp, pto->parent,
				    Tk_MainWindow (interp));
	  if (parent == NULL)
	    return TCL_ERROR;
	}
      if (Tk_WindowId (parent) == None)
	Tk_MakeWindowExist (parent);
      pd->hwndOwner = Tk_GetHWND (Tk_WindowId (parent));

      if (wd->page_setup != NULL)
	{
	  pd->hDevMode = wd->page_setup->hDevMode;
	  pd->hDevNames = wd->page_setup->hDevNames;
	}

      pd->Flags = PD_NOSELECTION | PD_RETURNDC | PD_USEDEVMODECOPIES;

      pd->nCopies = 1;
      pd->nFromPage = 1;
      pd->nToPage = 1;
      pd->nMinPage = 1;
      pd->nMaxPage = 0xffff;
    }

  mode = Tcl_SetServiceMode (TCL_SERVICE_ALL);

  ret = PrintDlg (pd);

  (void) Tcl_SetServiceMode (mode);

  if (! ret)
    {
      DWORD code;

      code = CommDlgExtendedError ();

      /* For some errors, the print dialog will already have reported
         an error.  We treat those as though the user pressed cancel.
         Unfortunately, I do not know just which errors those are.  */

      if (code == 0 || code == PDERR_NODEFAULTPRN)
	{
	  *cancelled = 1;
	  return TCL_OK;
	}
      else
	{
	  char buf[20];

	  sprintf (buf, "0x%lx", (unsigned long) code);
	  Tcl_ResetResult (interp);
	  Tcl_AppendStringsToObj (Tcl_GetObjResult (interp),
				  "Windows common dialog error ", buf,
				  (char *) NULL);
	  return TCL_ERROR;
	}
    }

  return TCL_OK;
}

/* Get the margins in device units.  */

static void
winprint_get_margins (struct winprint_data *wd, const PRINTDLG *pd,
		      int *top_ptr, int *left_ptr, int *bottom_ptr)
{
  int topmargin, leftmargin, bottommargin;
  int logx, logy;

  if (wd->page_setup == NULL)
    {
      /* Use 1 inch margins.  */
      topmargin = 1000;
      leftmargin = 1000;
      bottommargin = 1000;
    }
  else
    {
      topmargin = wd->page_setup->rtMargin.top;
      leftmargin = wd->page_setup->rtMargin.left;
      bottommargin = wd->page_setup->rtMargin.bottom;
      if ((wd->page_setup->Flags & PSD_INHUNDREDTHSOFMILLIMETERS) != 0)
	{
	  topmargin = (topmargin * 1000) / 2540;
	  leftmargin = (leftmargin * 1000) / 2540;
	  bottommargin = (bottommargin * 1000) / 2540;
	}
    }

  logx = GetDeviceCaps (pd->hDC, LOGPIXELSX);
  logy = GetDeviceCaps (pd->hDC, LOGPIXELSY);

  topmargin = (topmargin * logy) / 1000;
  leftmargin = (leftmargin * logx) / 1000;
  bottommargin = (bottommargin * logy) / 1000;

  *top_ptr = topmargin;
  *left_ptr = leftmargin;
  *bottom_ptr = GetDeviceCaps (pd->hDC, VERTRES) - bottommargin;
}

/* Prepare to start printing.  */

static int
winprint_start (struct winprint_data *wd, Tcl_Interp *interp, PRINTDLG *pd,
		const struct print_text_options *pto, int *cancelled)
{
  DOCINFO di;

  *cancelled = 0;

  wd->aborted = 0;

  /* We have no way to pass information to the abort function, so we
     need to use a global variable.  */
  abort_wd = wd;
  if (! SetAbortProc (pd->hDC, abort_function))
    {
      windows_error (interp, "SetAbortFunc");
      return TCL_ERROR;
    }

  di.cbSize = sizeof (DOCINFO);
  di.lpszDocName = pto->name;
  di.lpszOutput = NULL;
  di.lpszDatatype = NULL;
  di.fwType = 0;

  if (StartDoc (pd->hDC, &di) <= 0)
    {
      if (GetLastError () == ERROR_CANCELLED)
	*cancelled = 1;
      else
	{
	  windows_error (interp, "StartDoc");
	  return TCL_ERROR;
	}
    }

  return TCL_OK;
}

/* Finish printing.  */

static int
winprint_finish (struct winprint_data *wd, Tcl_Interp *interp,
		 PRINTDLG *pd, int error)
{
  int ret;

  ret = TCL_OK;

  if (error || wd->aborted)
    AbortDoc (pd->hDC);
  else
    {
      if (EndDoc (pd->hDC) <= 0)
	{
	  windows_error (interp, "EndDoc");
	  ret = TCL_ERROR;
	}
    }

  DeleteDC (pd->hDC);

  return ret;
}

/* Values the ide_winprint print_text query or page procedure can
   return.  */

enum winprint_query { Q_CONTINUE, Q_NEWPAGE, Q_DONE };

/* Invoke the query or page procedure for ide_winprint print_text.  */

static int
winprint_print_text_invoke (Tcl_Interp *interp, char *proc, const char *name,
			    enum winprint_query *result)
{
  char *q;

  if (Tcl_Eval (interp, proc) == TCL_ERROR)
    return TCL_ERROR;

  q = Tcl_GetStringFromObj (Tcl_GetObjResult (interp), (int *) NULL);
  if (strcmp (q, "continue") == 0)
    *result = Q_CONTINUE;
  else if (strcmp (q, "newpage") == 0)
    *result = Q_NEWPAGE;
  else if (strcmp (q, "done") == 0)
    *result = Q_DONE;
  else
    {
      Tcl_ResetResult (interp);
      Tcl_AppendStringsToObj (Tcl_GetObjResult (interp),
			      "bad return from ", name, " procedure: \"",
			      q, "\"", (char *) NULL);
      return TCL_ERROR;
    }

  return TCL_OK;  
}

/* Implement ide_winprint print_text.  */
static int
winprint_print_command (ClientData cd, Tcl_Interp *interp, int argc,
			     char **argv)
{
  struct winprint_data *wd = (struct winprint_data *) cd;
  char *queryproc;
  char *textproc;
  struct print_text_options pto;
  PRINTDLG pd;
  int cancelled;
  int top, bottom, left;
  TEXTMETRIC tm;
  POINT pt;
  int lineheight;
  int pageno;
  int error=0, done, needquery;
  struct {
	 short len; /* Defined to be 16 bits.... */
	 char buffer[PRINT_BUFSIZE+1];
  } indata;

  queryproc = argv[2];
  textproc = argv[3];
 
  if (winprint_print_text_options (wd, interp, argc, argv, &pto) != TCL_OK)
    return TCL_ERROR;

  if (winprint_print_text_dialog (wd, interp, &pto, &pd, &cancelled) != TCL_OK)
    return TCL_ERROR;
  if (cancelled)
    return TCL_OK;

  if (pto.postscript)
  {
	int eps_printing = 33;
	int result;
	short bresult = 1; /* EPS printing download suppressed */
	result = Escape (pd.hDC, eps_printing, sizeof (BOOL), (LPCSTR)&bresult, NULL);
	if ( result < 0 )
	{
		/* The EPSPRINTING escape failed! */
		Tcl_AppendElement(interp, 
                   "ide_winprint: EPSPRINTING escape implemented but failed");
		DeleteDC (pd.hDC);
		return TCL_ERROR;
	  }
  }
  else
  {
	winprint_get_margins(wd, &pd, &top, &left, &bottom);
  }

  if (winprint_start (wd, interp, &pd, &pto, &cancelled) != TCL_OK)
    {
      DeleteDC (pd.hDC);
      return TCL_ERROR;
    }
  if (cancelled)
    {
      DeleteDC (pd.hDC);
      return TCL_OK;
    }

  /* init and start init-procedure if available */
  if (pto.initproc != NULL)
  {
    	Tcl_DString initStr;
	char buf[64];
	Tcl_DStringInit (&initStr);
	Tcl_DStringAppend (&initStr, pto.initproc, -1);
	
	/* Here we must pass the customer selection from the PrintDialog
	 * as parameters for the init command, */
	/* From page */
	Tcl_DStringAppendElement (&initStr, "-frompage");
	sprintf (buf, "%i", pd.nFromPage);
	Tcl_DStringAppendElement (&initStr, buf);
	/* To Page */
	Tcl_DStringAppendElement (&initStr, "-topage");
	sprintf (buf, "%i", pd.nToPage);
	Tcl_DStringAppendElement (&initStr, buf);
	/* # Copies */
	Tcl_DStringAppendElement (&initStr, "-copies");
	sprintf (buf, "%i", pd.nCopies);
	Tcl_DStringAppendElement (&initStr, buf);
	/* Print Selection? */
	Tcl_DStringAppendElement (&initStr, "-selection");
	Tcl_DStringAppendElement (&initStr, (pd.Flags&PD_SELECTION) ? "1" : "0");
	
	/* Execute tcl/command */
	if (Tcl_Eval (interp, Tcl_DStringValue(&initStr)) != TCL_OK)
	{
	      Tcl_DStringFree (&initStr);
	      return TCL_ERROR;
	}
	Tcl_DStringFree (&initStr);
  }
    
  if (pto.postscript)
  {
    Tcl_DString pageStr;
    int status, retval, len, i;
    char *l, msgbuf[128];
    enum winprint_query q = 0;
    
    /* Note: NT 4.0 seems to leave the default CTM quite tiny! */
    strcpy (indata.buffer, "\r\nsave\r\ninitmatrix\r\n");
    indata.len = strlen(indata.buffer);
    Escape(pd.hDC, PASSTHROUGH, 0, (LPCSTR)&indata, NULL);
    
    /* Init command for page-procedure */
    if (pto.pageproc != NULL)
      {
	Tcl_DStringInit (&pageStr);
	Tcl_DStringAppend (&pageStr, pto.pageproc, -1);
	Tcl_DStringAppendElement (&pageStr, "-1");
      }
    
    /* Start printing */
    while (1)
      {
    	/* Run page-procedure to update the display */
	status = winprint_print_text_invoke (interp, Tcl_DStringValue(&pageStr), "page", &q);
	if (status != TCL_OK || q == Q_DONE)
	  {
	    error = 1;
	    break;
	  }
	
	/* query next characters to send to printer */
	if (winprint_print_text_invoke (interp, queryproc, "query", &q) != TCL_OK)
	  {
	    error = 1;
	    break;
	  }
	if (q != Q_CONTINUE)
	  {
	    done = 1;
	    break;
	  }
	if (Tcl_Eval (interp, textproc) == TCL_ERROR)
	  {
	    error = 1;
	    break;
	  }
	l = Tcl_GetStringFromObj (Tcl_GetObjResult (interp), &len);
	for (i=0; i<len; i+=PRINT_BUFSIZE)
	  {
	    int lpos = min (PRINT_BUFSIZE, len-i);
	    strncpy (indata.buffer, l+i, lpos);
	    indata.buffer[lpos] = 0;
	    indata.len = lpos;
	    
	    retval = Escape (pd.hDC, PASSTHROUGH, 0, (LPCSTR)&indata, NULL);
	    if (retval < 0)
	      {
		Tcl_AppendElement(interp, "ide_winprint: PASSTHROUGH Escape failed");
		error = 1;
		break;
	      }
	    else if (retval != indata.len)
	      {
		sprintf(msgbuf, "ide_winprint: Short write (%d vs. %d)", retval, indata.len);
		Tcl_AppendElement(interp, msgbuf);
		error = 1;
		break;
	      }
	  }
      }
    
    strcpy (indata.buffer, "\r\nrestore\r\n");
    indata.len = strlen(indata.buffer);
    Escape(pd.hDC, PASSTHROUGH, 0, (LPCSTR)&indata, NULL);
  }
  else
    {
      GetTextMetrics (pd.hDC, &tm);
      pt.x = 0;
      pt.y = tm.tmHeight + tm.tmExternalLeading;
      LPtoDP (pd.hDC, &pt, 1);
      lineheight = pt.y;
      
      pageno = 1;
      
      /* The main print loop.  */
      done = 0;
      error = 0;
      needquery = 1;
      while (1)
	{
	  int y;
	  
	  if (wd->aborted)
	    break;
	  
	  /* Start a new page.  */
	  if (pto.pageproc != NULL)
	    {
	      Tcl_DString ds;
	      char buf[20];
	      enum winprint_query q;
	      int status;
	      
	      Tcl_DStringInit (&ds);
	      Tcl_DStringAppend (&ds, pto.pageproc, -1);
	      sprintf (buf, "%d", pageno);
	      Tcl_DStringAppendElement (&ds, buf);
	      
	      status = winprint_print_text_invoke (interp, Tcl_DStringValue (&ds),
						   "page", &q);
	      
	      Tcl_DStringFree (&ds);
	      
	      if (status != TCL_OK)
		{
		  error = 1;
		  break;
		}
	      
	      if (q == Q_DONE)
		{
		  done = 1;
		  break;
		}
	    }
	  
	  if (needquery)
	    {
	      enum winprint_query q;
	      
	      if (winprint_print_text_invoke (interp, queryproc, "query", &q)
		  != TCL_OK)
		{
		  error = 1;
		  break;
		}
	      
	      if (q == Q_DONE)
		{
		  done = 1;
		  break;
		}
	      
	      /* Ignore Q_NEWPAGE, since we're about to start a new page
		 anyhow.  */
	      
	      needquery = 0;
	    }
	  
	  if (StartPage (pd.hDC) <= 0)
	    {
	      windows_error (interp, "StartPage");
	      error = 1;
	      break;
	    }
	  
	  y = top;
	  
	  /* Print a page.  */
	  
	  while (1)
	    {
	      char *l;
	      int len;
	      enum winprint_query q;
	      
	      if (Tcl_Eval (interp, textproc) == TCL_ERROR)
		{
		  error = 1;
		  break;
		}
	      
	      l = Tcl_GetStringFromObj (Tcl_GetObjResult (interp), &len);
	      
	      TextOut (pd.hDC, left, y, l, len);
	      y += lineheight;
	      
	      if (y >= bottom)
		{
		  needquery = 1;
		  break;
		}
	      
	      if (winprint_print_text_invoke (interp, queryproc, "query", &q)
		  != TCL_OK)
		{
		  error = 1;
		  break;
		}
	      
	      if (q == Q_DONE)
		{
		  done = 1;
		  break;
		}
	      else if (q == Q_NEWPAGE)
		break;
	    }
	  
	  if (error)
	    break;
	  
	  if (EndPage (pd.hDC) <= 0)
	    {
	      /* It's OK for EndPage to return an error if the print job
		 was cancelled.  */
	      if (! wd->aborted)
		{
		  windows_error (interp, "EndPage");
		  error = 1;
		}
	      break;
	    }
	  
	  if (done)
	    break;
	  
	  ++pageno;
	}
    }
  
  if (winprint_finish (wd, interp, &pd, error) != TCL_OK)
    error = 1;
  
  if (error)
    return TCL_ERROR;
  
  Tcl_ResetResult (interp);
  return TCL_OK;
}

/* Implement ide_winprint abort.  */

static int
winprint_abort_command (ClientData cd, Tcl_Interp *interp, int argc,
			char **argv)
{
  struct winprint_data *wd = (struct winprint_data *) cd;

  wd->aborted = 1;
  return TCL_OK;
}

/* The subcommand table.  */

static const struct ide_subcommand_table winprint_commands[] =
{
  { "page_setup",	winprint_page_setup_command,	2, -1 },
  { "print_text",	winprint_print_command,	4, -1 },
  { "print",	     winprint_print_command,	6, -1 },
  { "abort",		    winprint_abort_command,		2, 2 },
  { NULL, NULL, 0, 0 }
};

/* This function creates the ide_winprint Tcl command.  */

int
ide_create_winprint_command (Tcl_Interp *interp)
{
  struct winprint_data *wd;
  
  wd = (struct winprint_data *) Tcl_Alloc (sizeof *wd);
  wd->page_setup = NULL;
  wd->aborted = 0;
  
  return ide_create_command_with_subcommands (interp, "ide_winprint",
					      winprint_commands,
					      (ClientData) wd,
					      winprint_command_deleted);
}

#endif /* _WIN32 */