File: gvwprn.c

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (267 lines) | stat: -rw-r--r-- 7,033 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

/* Copyright (C) 1993-1996, Russell Lang.  All rights reserved.

   This file is part of GSview.
   This program is distributed with NO WARRANTY OF ANY KIND.  No author
   or distributor accepts any responsibility for the consequences of using it,
   or for whether it serves any particular purpose or works at all, unless he
   or she says so in writing.  Refer to the GSview Free Public Licence 
   (the "Licence") for full details.
   Every copy of GSview must include a copy of the Licence, normally in a 
   plain ASCII text file named LICENCE.  The Licence grants you the right 
   to copy, modify and redistribute GSview, but only under certain conditions 
   described in the Licence.  Among other things, the Licence requires that 
   the copyright notice and this notice be preserved on all copies.

   1997 : modified for Scilab
   Jean-Philippe Chancelier 
   See gp_printfile : choose a printer and send a file to printer 
 */

#include "gvwin.h"
#include "wgnuplib.h"
#include "wresource.h"
#include "wcommon.h"

/** 
  Warning : Remove AbortPrinter from 
  b18/H-i386-cygwin32/i386-cygwin32/include/Windows32/Functions.h
  for gc-win32 
  **/

#ifdef GVW_ALONE
/* standalone executable for print spool */
/* get a filename and spool it for printing */
/* XXXX : a finir en rajoutant des arguments * */

int 
main (int argc, char *argv[])
{
  HWND parent;
  HANDLE x = GetModuleHandleA (0);
  parent = GetActiveWindow ();
  if (argc == 2)
    gp_printfile (x, parent, argv[1], (char *) 0);
  else
    gp_printfile (x, parent, (char *) 0, (char *) 0);
  return (0);
}

void 
sciprint (char *fmt,...)
{
  int count;
  va_list args;
  va_start (args, fmt);
  count = vfprintf (stdout, fmt, args);
}

#endif /* GW_ALONE */

/* documented in Device Driver Adaptation Guide */

#define PORT_BUF_SIZE 4096

/* Dialog box to select printer port */

char printer_port[32];

EXPORT BOOL CALLBACK
SpoolDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  LPSTR entry;
  switch (message)
    {
    case WM_INITDIALOG:
      entry = (LPSTR) lParam;
      while (*entry)
	{
	  SendDlgItemMessage (hDlg, SPOOL_PORT, LB_ADDSTRING, 0, (LPARAM) entry);
	  entry += lstrlen (entry) + 1;
	}
      if ((printer_port == '\0') ||
	  (SendDlgItemMessage (hDlg, SPOOL_PORT, LB_SELECTSTRING, 0, (LPARAM) (LPSTR) printer_port)
	   == LB_ERR))
	SendDlgItemMessage (hDlg, SPOOL_PORT, LB_SETCURSEL, 0, (LPARAM) 0);
      return TRUE;
    case WM_COMMAND:
      switch (LOWORD (wParam))
	{
	case SPOOL_PORT:
	  if (HIWORD (wParam) == LBN_DBLCLK)
	    PostMessage (hDlg, WM_COMMAND, IDOK, 0L);
	  return FALSE;
	case IDOK:
	  SendDlgItemMessage (hDlg, SPOOL_PORT, LB_GETTEXT,
	   (int) SendDlgItemMessage (hDlg, SPOOL_PORT, LB_GETCURSEL, 0, 0L),
			      (LPARAM) (LPSTR) printer_port);
	  EndDialog (hDlg, 1 + (int) SendDlgItemMessage (hDlg, SPOOL_PORT, LB_GETCURSEL, 0, 0L));
	  return TRUE;
	case IDCANCEL:
	  EndDialog (hDlg, 0);
	  return TRUE;
	}
    }
  return FALSE;
}

char *
get_queues (void)
{
  int i;
  DWORD count, needed;
  PRINTER_INFO_1 *prinfo;
  char *enumbuffer;
  char *buffer;
  char *p;
  /* enumerate all available printers */
  EnumPrinters (PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 1, NULL, 0, &needed, &count);
  enumbuffer = malloc (needed);
  if (enumbuffer == (char *) NULL)
    return FALSE;
  if (!EnumPrinters (PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 1, (LPBYTE) enumbuffer, needed, &needed, &count))
    {
      free (enumbuffer);
      sciprint ("EnumPrinters() failed, error code = %d", GetLastError ());
      return NULL;
    }
  prinfo = (PRINTER_INFO_1 *) enumbuffer;
  if ((buffer = malloc (PORT_BUF_SIZE)) == (char *) NULL)
    {
      free (enumbuffer);
      return NULL;
    }
  /* copy printer names to single buffer */
  p = buffer;
  for (i = 0; i < (int) count; i++)
    {
      if ((int) strlen (prinfo[i].pName) + 1 < (PORT_BUF_SIZE - (p - buffer)))
	{
	  strcpy (p, prinfo[i].pName);
	  p += strlen (p) + 1;
	}
    }
  *p = '\0';			/* double null at end */
  free (enumbuffer);
  return buffer;
}

/* return TRUE if queuename available */
/* return FALSE if cancelled or error */
/* if queue non-NULL, use as suggested queue */

BOOL 
get_queuename (HINSTANCE hInstance, HWND hwnd, char *portname, char *queue)
{
  char *buffer;
  char *p;
  int i, iport;
  buffer = get_queues ();
  if ((queue == (char *) NULL) || (strlen (queue) == 0))
    {
      /* select a queue */
      iport = DialogBoxParam (hInstance, "QueueDlgBox", hwnd, SpoolDlgProc, (LPARAM) buffer);
      if (!iport)
	{
	  free (buffer);
	  return FALSE;
	}
      p = buffer;
      for (i = 1; i < iport && strlen (p) != 0; i++)
	p += lstrlen (p) + 1;
      /* prepend \\spool\ which is used to distinguish */
      /* real files from queues */
      strcpy (portname, "\\\\spool\\");
      strcat (portname, p);
    }
  else
    {
      strcpy (portname, "\\\\spool\\");
      strcat (portname, queue);
    }
  free (buffer);
  return TRUE;
}

/******************************************************************
 * Print File to port or queue 
 * port==NULL means prompt for port or queue with dialog box 
 ******************************************************************/

#define PRINT_BUF_SIZE 16384u

int 
gp_printfile (HINSTANCE hInstance, HWND hwnd, char *filename, char *port)
{
  DWORD count;
  char *buffer;
  char portname[MAXSTR];
  FILE *f;
  HANDLE printer;
  DOC_INFO_1 di;
  DWORD written;

  if (!get_queuename (hInstance, hwnd, portname, port))
    return FALSE;
  port = portname + 8;		/* skip over \\spool\ */

  if ((buffer = malloc (PRINT_BUF_SIZE)) == (char *) NULL)
    return FALSE;
  if (filename != (char *) 0)
    {
      if ((f = fopen (filename, "rb")) == (FILE *) NULL)
	{
	  free (buffer);
	  return FALSE;
	}
    }
  else
    f = stdin;

  /* open a printer */
  if (!OpenPrinter (port, &printer, NULL))
    {
      sciprint ("OpenPrinter() failed for \042%s\042, error code = %d", port, GetLastError ());
      free (buffer);
      return FALSE;
    }
  /* from here until ClosePrinter, should AbortPrinter on error */

  di.pDocName = filename;
  di.pOutputFile = NULL;
  di.pDatatype = "RAW";		/* for available types see EnumPrintProcessorDatatypes */
  if (!StartDocPrinter (printer, 1, (LPBYTE) & di))
    {
      sciprint ("StartDocPrinter() failed, error code = %d", GetLastError ());
      AbortPrinter (printer);
      free (buffer);
      return FALSE;
    }

  while ((count = fread (buffer, 1, PRINT_BUF_SIZE, f)) != 0)
    {
      if (!WritePrinter (printer, (LPVOID) buffer, count, &written))
	{
	  free (buffer);
	  fclose (f);
	  AbortPrinter (printer);
	  return FALSE;
	}
    }
  fclose (f);
  free (buffer);

  if (!EndDocPrinter (printer))
    {
      sciprint ("EndDocPrinter() failed, error code = %d", GetLastError ());
      AbortPrinter (printer);
      return FALSE;
    }

  if (!ClosePrinter (printer))
    {
      sciprint ("ClosePrinter() failed, error code = %d", GetLastError ());
      return FALSE;
    }
  return TRUE;
}