summaryrefslogtreecommitdiff
path: root/app/wlib/mswlib/mswprint.c
blob: 91f05ea40b49f9388b51a6f433f8475abfc11313 (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
#include <windows.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <commdlg.h>
#include <math.h>
#ifndef WIN32
#include <print.h>
#endif
#include "mswint.h"

/*
 *****************************************************************************
 *
 * PRINT
 *
 *****************************************************************************
 */


struct wDraw_t print_d;

#ifdef WIN32
struct tagPDA printDlg;
#else
struct tagPD printDlg;
#endif
static int printStatus = FALSE;
static DOCINFO docInfo;
static double pageSizeW = 8.5, pageSizeH = 11.0;
static double physSizeW = 8.5, physSizeH = 11.0;
static int pageCount = -1;

static HPALETTE newPrintPalette;
static HPALETTE oldPrintPalette;


void wPrintClip( wPos_t x, wPos_t y, wPos_t w, wPos_t h )
{
	wDrawClip( &print_d, x, y, w, h );
}


void getPageDim( HDC hDc )
{
	int rc;
	POINT dims;
	POINT offs;
	int res_w, res_h, size_w, size_h;
	rc = Escape( hDc, GETPHYSPAGESIZE, 0, NULL, (LPPOINT)&dims );
	if (rc <0) {
	   mswFail( "GETPHYPAGESIZE" );
	}
	rc = Escape( hDc, GETPRINTINGOFFSET, 0, NULL, (LPPOINT)&offs );
	if (rc <0) {
	   mswFail( "GETPRINTINGOFFSET" );
	}
	print_d.wFactor = (double)GetDeviceCaps( hDc, LOGPIXELSX );
	print_d.hFactor = (double)GetDeviceCaps( hDc, LOGPIXELSY );
	if (print_d.wFactor <= 0 || print_d.hFactor <= 0) {
		mswFail( "getPageDim: LOGPIXELS... <= 0" );
		abort();
	}
	print_d.DPI = min( print_d.wFactor, print_d.hFactor );
	size_w = GetDeviceCaps( hDc, HORZSIZE );
	size_h = GetDeviceCaps( hDc, VERTSIZE );
	print_d.w = res_w = GetDeviceCaps( hDc, HORZRES );
	print_d.h = res_h = GetDeviceCaps( hDc, VERTRES );
	pageSizeW = ((double)res_w)/print_d.wFactor;
	pageSizeH = ((double)res_h)/print_d.hFactor;
	physSizeW = ((double)dims.x)/print_d.wFactor;
	physSizeH = ((double)dims.y)/print_d.hFactor;
}

static wBool_t printInit( void )
{
	static int initted = FALSE;
	static int printerOk = FALSE;
	if (initted) {
		if (!printerOk) {
			mswFail( "No Printers are defined" );
		}
		return printerOk;
	}
	initted = TRUE;
	printDlg.lStructSize = sizeof printDlg;
	printDlg.hwndOwner = NULL;
	printDlg.Flags = PD_RETURNDC|PD_RETURNDEFAULT;
	if (PrintDlg(&printDlg) != 0  && printDlg.hDC) {
		getPageDim( printDlg.hDC );
		DeleteDC( printDlg.hDC );
	}
#ifdef LATER
	DEVMODE * printMode;
	HDC hDc;
	char ptrInfo[80];
	char ptrDrvrDvr[80];
	char *temp;
	char *ptrDevice;
	char *ptrDrvr;
	char *ptrPort;
	int size;
	int rc;
	FARPROC extDeviceMode;
	FARPROC deviceMode;
	HINSTANCE hDriver;

	GetProfileString("windows", "device", "", ptrInfo, sizeof ptrInfo );
	if (ptrInfo[0] == 0) {
		mswFail( "No Printers are defined" );
		return FALSE;
	}
	temp = ptrDevice = ptrInfo;
	ptrDrvr = ptrPort = NULL;
	while (*temp) {
		if (*temp == ',') {
			*temp++ = 0;
			while( *temp == ' ' )
				temp++;
			if (!ptrDrvr)
				ptrDrvr = temp;
			else {
				ptrPort = temp;
				break;
			}
		}
		else
			temp = AnsiNext(temp);
	}
	strcpy( ptrDrvrDvr, ptrDrvr );
	strcat( ptrDrvrDvr, ".drv" );
	if ((long)(hDriver = LoadLibrary( ptrDrvrDvr )) <= 32) {
		mswFail( "printInit: LoadLibrary" );
		return FALSE;
	}
	if (( extDeviceMode = GetProcAddress( hDriver, "ExtDeviceMode" )) != NULL) {
		size = extDeviceMode( mswHWnd, (HANDLE)hDriver, (LPDEVMODE)NULL, (LPSTR)ptrDevice, (LPSTR)ptrPort, (LPDEVMODE)NULL, (LPSTR)NULL, 0 );
		printMode = (DEVMODE*)malloc( size );
		rc = extDeviceMode( mswHWnd, (HANDLE)hDriver, (LPDEVMODE)printMode, (LPSTR)ptrDevice, (LPSTR)ptrPort, (LPDEVMODE)NULL, (LPSTR)NULL, DM_OUT_BUFFER );
#ifdef LATER
		if (rc != IDOK && rc != IDCANCEL) {
			mswFail( "printInit: extDeviceMode" );
			return FALSE;
		}
#endif
	} else if (( deviceMode = GetProcAddress( hDriver, "DeviceMode" )) != NULL) {
		rc = deviceMode( mswHWnd, (HANDLE)hDriver, (LPSTR)ptrDevice, (LPSTR)ptrPort );
#ifdef LATER
		if (rc != IDOK && rc != IDCANCEL) {
			mswFail( "printInit: deviceMode" );
			return FALSE;
		}
#endif
	}

	hDc = CreateDC( (LPSTR)ptrDrvr, (LPSTR)ptrDevice, (LPSTR)ptrPort, NULL );
	if (hDc == NULL) {
		mswFail("printInit: createDC" );
		abort();
	}
	getPageDim( hDc );
	DeleteDC( hDc );
	
	FreeLibrary( hDriver );
#endif
	printerOk = TRUE;
	return TRUE;
}


wBool_t wPrintInit( void )
{
	if (!printInit()) {
		return FALSE;
	}
	return TRUE;
}


void wPrintSetup( wPrintSetupCallBack_p callback )
{
	if (!printInit()) {
		return;
	}
	/*memset( &printDlg, 0, sizeof printDlg );*/
	printDlg.lStructSize = sizeof printDlg;
	printDlg.hwndOwner = NULL;
	printDlg.Flags = PD_RETURNDC|PD_PRINTSETUP;
	if (PrintDlg(&printDlg) != 0  && printDlg.hDC) {
		getPageDim( printDlg.hDC );
	}
	if ( callback ) {
		callback( TRUE );
	}
}


void wPrintGetPageSize( double *w, double *h )
{
	printInit();
	*w = pageSizeW;
	*h = pageSizeH;
}


void wPrintGetPhysSize( double *w, double *h )
{
	printInit();
	*w = physSizeW;
	*h = physSizeH;
}


HDC mswGetPrinterDC( void )
{
	if (!printInit()) {
		return (HDC)0;
	}
	/*memset( &printDlg, 0, sizeof printDlg );*/
	printDlg.lStructSize = sizeof printDlg;
	printDlg.hwndOwner = NULL;
	printDlg.Flags = PD_RETURNDC|PD_NOPAGENUMS|PD_NOSELECTION;
	if (PrintDlg(&printDlg) != 0)
		return printDlg.hDC;
	else
		return (HDC)0;
}


static wBool_t printAbort = FALSE;
HWND hAbortDlgWnd;
FARPROC lpAbortDlg, lpAbortProc;
static int pageNumber;

int FAR PASCAL mswAbortDlg( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	if (msg == WM_COMMAND) {
		if (WCMD_PARAM_ID == IDCANCEL) {
			printAbort = TRUE;
			EndDialog( hWnd, wParam );
			return TRUE;
		}
	} else if (msg == WM_INITDIALOG) {
		SetFocus( GetDlgItem( hWnd, IDCANCEL ) );
		return TRUE;
	}
	return FALSE;
}


int FAR PASCAL _export mswAbortProc( HDC hdcPrinter, int Code )
{
	MSG msg;
	while (PeekMessage((LPMSG)&msg, (HWND)0, 0, 0, PM_REMOVE)) {
		if (!IsDialogMessage(hAbortDlgWnd, (LPMSG)&msg) ) {
			TranslateMessage( (LPMSG)&msg );
			DispatchMessage( (LPMSG)&msg );
		}
	}
	return !printAbort;
}


wBool_t wPrintDocStart( const char * title, int fpageCount, int * copiesP )
{
	printStatus = FALSE;
	pageCount = fpageCount;
	pageNumber = 0;
	print_d.hDc = mswGetPrinterDC();
	if (print_d.hDc == (HDC)0) {
		return FALSE;
	}
		printStatus = TRUE;
		docInfo.cbSize = sizeof docInfo;
		docInfo.lpszDocName = title;
		docInfo.lpszOutput = NULL;
		lpAbortDlg = MakeProcInstance( (FARPROC)mswAbortDlg, mswHInst );
		lpAbortProc = MakeProcInstance( (FARPROC)mswAbortProc, mswHInst );
		SetAbortProc( print_d.hDc, (ABORTPROC)lpAbortProc );
		if (StartDoc( print_d.hDc, &docInfo ) < 0) {
			MessageBox( mswHWnd, "Unable to start print job",
						NULL, MB_OK|MB_ICONHAND );
			FreeProcInstance( lpAbortDlg );
			FreeProcInstance( lpAbortProc );
			DeleteDC( print_d.hDc );
			return FALSE;
		}
		printAbort = FALSE;
		hAbortDlgWnd = CreateDialog( mswHInst, "MswAbortDlg", mswHWnd,
						(DLGPROC)lpAbortDlg );
		/*SetDlgItemText( hAbortDlgWnd, IDM_PRINTAPP, title );*/
		SetWindowText( hAbortDlgWnd, title );
		ShowWindow( hAbortDlgWnd, SW_NORMAL );
		UpdateWindow( hAbortDlgWnd );
		EnableWindow( mswHWnd, FALSE );
		if (copiesP)
			*copiesP = printDlg.nCopies;
		if (printDlg.nCopies>1)
			pageCount *= printDlg.nCopies;
		if ( (GetDeviceCaps( printDlg.hDC, RASTERCAPS ) & RC_PALETTE) ) {
			newPrintPalette = mswCreatePalette();
			oldPrintPalette = SelectPalette( printDlg.hDC, newPrintPalette, 0 );
			RealizePalette( printDlg.hDC );
		}
		return TRUE;
}

wDraw_p wPrintPageStart( void )
{
	char pageL[80];
	if (!printStatus)
		return NULL;
	pageNumber++;
	if (pageCount > 0)
		wsprintf( pageL, "Page %d of %d", pageNumber, pageCount );
	else
		wsprintf( pageL, "Page %d", pageNumber );
	SetDlgItemText( hAbortDlgWnd, IDM_PRINTPAGE, pageL );
	StartPage( printDlg.hDC );
#ifdef LATER
	if (mswPrintPalette) {
		SelectPalette( printDlg.hDC, mswPrintPalette, 0 );
		RealizePalette( printDlg.hDC );
	}
#endif
	getPageDim( printDlg.hDC );
	SelectClipRgn( print_d.hDc, NULL );
	return &print_d;
}

wBool_t wPrintPageEnd( wDraw_p d )
{
	return EndPage( printDlg.hDC ) >= 0;
}

wBool_t wPrintQuit( void )
{
	MSG msg;
	while (PeekMessage((LPMSG)&msg, (HWND)0, 0, 0, PM_REMOVE)) {
		if (!IsDialogMessage(hAbortDlgWnd, (LPMSG)&msg) ) {
			TranslateMessage( (LPMSG)&msg );
			DispatchMessage( (LPMSG)&msg );
		}
	}
	return printAbort;
}

void wPrintDocEnd( void )
{
	if (!printStatus)
		return;
	EndDoc( printDlg.hDC );
	if ( newPrintPalette ) {
		SelectPalette( printDlg.hDC, oldPrintPalette, 0 );
		DeleteObject( newPrintPalette );
		newPrintPalette = (HPALETTE)0;
	}
		
	EnableWindow( mswHWnd, TRUE );
	DestroyWindow( hAbortDlgWnd );
	FreeProcInstance( lpAbortDlg );
	FreeProcInstance( lpAbortProc );
	DeleteDC( printDlg.hDC );
	printStatus = FALSE;
}

wBool_t wPrintFontAlias( const char * font, const char * alias )
{
	return TRUE;
}

wBool_t wPrintNewPrinter( const char * printer )
{
	return TRUE;
}

wBool_t wPrintNewMargin( const char * name, double t, double b, double l, double r )
{
	return TRUE;
}

void wPrintSetCallBacks(
		wAddPrinterCallBack_p newPrinter,
		wAddMarginCallBack_p newMargin,
		wAddFontAliasCallBack_p newFontAlias )
{
}