-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPRINTMAN.PAS
700 lines (654 loc) · 17.7 KB
/
PRINTMAN.PAS
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
{/////////////////////////////////////////////////////////////////////////
//
// Dos Navigator Open Source 1.51.08
// Based on Dos Navigator (C) 1991-99 RIT Research Labs
//
// This programs is free for commercial and non-commercial use as long as
// the following conditions are aheared to.
//
// Copyright remains RIT Research Labs, and as such any Copyright notices
// in the code are not to be removed. If this package is used in a
// product, RIT Research Labs should be given attribution as the RIT Research
// Labs of the parts of the library used. This can be in the form of a textual
// message at program startup or in documentation (online or textual)
// provided with the package.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// "Based on Dos Navigator by RIT Research Labs."
//
// THIS SOFTWARE IS PROVIDED BY RIT RESEARCH LABS "AS IS" AND ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The licence and distribution terms for any publically available
// version or derivative of this code cannot be changed. i.e. this code
// cannot simply be copied and put under another distribution licence
// (including the GNU Public Licence).
//
//////////////////////////////////////////////////////////////////////////}
{$I STDEFINE.INC}
unit PrintMan;
interface
{$IFDEF PrintManager}
uses
Streams, Defines, Views, Drivers,
Dialogs, Commands, Collect
;
type
PStringCol = ^TStringCol;
TStringCol = object(TCollection)
procedure FreeItem(P: Pointer); virtual;
procedure PutItem(var S: TStream; P: Pointer); virtual;
function GetItem(var S: TStream): Pointer; virtual;
end;
PPrintManager = ^TPrintManager;
TPrintManager = object(TListBox)
isValid: Boolean;
OutName: PString;
LockUpdate: Byte;
Paused: Boolean;
FileLen: TFileSize;
FilePos: TFileSize;
Status: PView;
PrintStream: PStream;
Buffer: PByteArray;
BufSize: Word;
BufCount: Word;
PrintDevice: PDosStream;
constructor Init(var Bounds: TRect; AStatus: PView;
AScrollBar: PScrollBar);
constructor Load(var S: TStream);
procedure Store(var S: TStream);
function PrintBuffer(Num: Word): Boolean;
procedure HandleEvent(var Event: TEvent); virtual;
procedure PrintFile(const FileName: String);
function SetDestination: Boolean;
function GetStatus: Byte;
procedure InitPrinter;
function Valid(C: Word): Boolean; virtual;
procedure Update; virtual;
destructor Done; virtual;
end;
PPrintStatus = ^TPrintStatus;
TPrintStatus = object(TView)
Print: PPrintManager;
procedure Draw; virtual;
constructor Load(var S: TStream);
procedure Store(var S: TStream);
end;
PPMWindow = ^TPMWindow;
TPMWindow = object(TDialog)
constructor Init(R: TRect);
end;
const
Printer: PPrintManager = nil;
MaxBufCount = 128;
procedure SetupPrinter;
{$ENDIF}
procedure PrintFile(const S: String);
implementation
{$IFDEF PrintManager}
uses
Startup, DNHelp, DNApp, Messages
, Advance, Advance1, Advance2
, VpSysLow {¤«ï Open_Access_ReadOnly}
{$IFDEF DPMI32}, LfnVP {$ENDIF}
;
procedure TStringCol.FreeItem;
begin
DisposeStr(PString(P));
end;
procedure TStringCol.PutItem;
begin
S.WriteStr(P);
end;
function TStringCol.GetItem;
begin
GetItem := S.ReadStr;
end;
constructor TPMWindow.Init;
var
P: PView;
S: PView;
begin
inherited Init(R, GetString(dlPManagerTitle));
Number := GetNum;
R.Assign(Size.X-13, 1, Size.X-12, Size.Y-4);
P := New(PScrollBar, Init(R));
Insert(P);
R.Assign(2, Size.Y-4, Size.X-14, Size.Y-2);
S := New(PPrintStatus, Init(R));
Insert(S);
R.Assign(2, 1, Size.X-13, Size.Y-4);
P := New(PPrintManager, Init(R, S, PScrollBar(P)));
Insert(P);
R.Assign(Size.X-12, 2, Size.X-2, 4);
Insert(New(PButton, Init(R, GetString(dlDeleteButton), cmOK, 0)));
R.Assign(Size.X-12, 4, Size.X-2, 6);
Insert(New(PButton, Init(R, GetString(dlCloseButton), cmClose, 0)));
R.Assign(Size.X-12, 6, Size.X-2, 8);
Insert(New(PButton, Init(R, GetString(dlPauseButton), cmNo, 0)));
SelectNext(False);
HelpCtx := hcPrintManager;
end { TPMWindow.Init };
constructor TPrintStatus.Load;
begin
inherited Load(S);
GetPeerViewPtr(S, Print);
end;
procedure TPrintStatus.Store;
begin
inherited Store(S);
PutPeerViewPtr(S, Printer);
end;
procedure TPrintStatus.Draw;
var
B: TDrawBuffer;
C: Word;
S, S1: String;
begin
S1 := '';
C := GetColor($0102);
MoveChar(B, ' ', C, Size.X);
if (Print^.Paused) or (Print^.List = nil) or (Print^.List^.Count = 0)
then
S := GetString(dlPrintingPaused)
else
begin
S := GetString(dlPrinting);
S := S+CutH(PString(Print^.List^.At(0))^, Size.X-CStrLen(S));
if Print^.FileLen <> 0 then
S1 := '~'+Copy(
Strg(#219, Trunc(((Size.X-6)*Print^.FilePos) / Print^.FileLen)) +
Strg(#177, Size.X-6),
1, Size.X-6) +
' ~' + Percent(Print^.FilePos+1, Print^.FileLen+1);
end;
MoveCStr(B, '~'+S+'~', C);
WriteLine(0, 0, Size.X, 1, B);
MoveChar(B, ' ', C, Size.X);
MoveCStr(B, S1, C);
WriteLine(0, 1, Size.X, 1, B);
end { TPrintStatus.Draw };
constructor TPrintManager.Init;
var
S: String;
begin
inherited Init(Bounds, 1, AScrollBar);
Options := Options or ofPostProcess;
Status := AStatus;
if Status <> nil then
PPrintStatus(Status)^.Print := @Self;
Printer := @Self;
BufSize := MaxBufCount;
GetMem(Buffer, BufSize);
LockUpdate := 0;
isValid := False;
Paused := True;
OutName := nil;
if not SetDestination then
Exit;
isValid := True;
Paused := False;
RegisterToBackground(@Self);
end;
function TPrintManager.SetDestination;
var
S, S1: String;
P: Boolean;
OldP: PDosStream;
hFile: LongInt;
NotDev: Boolean;
begin
SetDestination := False;
NotDev := False;
P := Paused;
Paused := True;
case RPrinterSetup.Device of
0:
S := 'LPT1';
1:
S := 'LPT2';
2:
S := 'LPT3';
3:
S := 'LPT4';
5:
S := 'NUL';
6:
S := 'COM1';
7:
S := 'COM2';
8:
S := 'COM3';
9:
S := 'COM4';
10:
S := RPrinterSetup.CustomDev1;
11:
S := RPrinterSetup.CustomDev2;
else {4:} {case}
begin
S := '';
if InputBox(GetString(dlPrintOut), GetString(dlFileName), S, 80,
hsPrintOut) <> cmOK
then
Exit;
end;
end {case};
S1 := S + #0;
//JO: çâ®¡ë ¥ £ ¤¨âì ¢ ª ª®©-«¨¡® ä ©« ¤¨áª¥
if RPrinterSetup.Device in [10,11] then
if {$IFDEF DPMI32}LfnVP.{$ENDIF}SysFileOpen(@S1[1],
Open_Access_ReadOnly or open_share_DenyNone, hFile) = 0
then
begin
if SysFileIsDevice(hFile) and $FF = 0 then
//AK155: 'and $FF' ¥®¡å®¤¨¬®, â ª ª ª 奫¯ ª SysFileIsDevice
// ¥¯à ¢¤¨¢ë©; á¬. 奫¯ ª DosQueryHType
NotDev := True;
SysFileClose(hFile);
end;
if NotDev then
begin
MessageBox(GetString(dlNotPrinter), nil, mfError+mfOKButton);
if PrintDevice <> nil then
PrintDevice^.Status := 0;
Exit;
end;
OldP := PrintDevice;
PrintDevice := New(PDosStream, Init(S, stCreate));
//JO: ¤«ï à áè à¥ëå á¥â¥¢ëå ¯à¨â¥à®¢ áâ âãá ¥ ¡ã¤¥â stOK, â.ª.
// ¤«ï ¨å ¥ à ¡®â ¥â SysFileSeek, ¢ë§ë¢ ¥¬ ï ¢ TDOSStream.Init,
// ç⮠⥬ ¥ ¬¥¥¥ ¥ ¬¥è ¥â ¯à®¨§¢®¤¨âì ¨å § ¯¨áì. ¨âã æ¨î
// ¬®¦® ®â«®¢¨âì ¯® ª®¤ã ®è¨¡ª¨ 87 (¯à®¢¥à¥® ¯® ªà ©¥© ¬¥à¥ ¯®¤ Win XP)
if (PrintDevice^.Status <> stOK)
and (PrintDevice^.ErrorInfo = 87) then
PrintDevice^.Status := stOK;
if PrintDevice^.Status <> stOK then
begin
MessageBox(GetString(dlPrintNoInit) + ^M^C'RC=' +
ItoS(PrintDevice^.ErrorInfo), nil, mfError+mfOKButton);
PrintDevice := OldP;
if PrintDevice <> nil then
PrintDevice^.Status := 0;
Exit;
end;
if (RPrinterSetup.Device in [10,11])
and (SysFileIsDevice(PrintDevice^.Handle) and $FF = 0) then
begin
MessageBox(GetString(dlNotPrinter), nil, mfError+mfOKButton);
Dispose(PrintDevice, Done);
EraseByName(S);
PrintDevice := OldP;
if PrintDevice <> nil then
PrintDevice^.Status := 0;
Exit;
end;
if OldP <> nil then
Dispose(OldP, Done);
OldP := nil;
DisposeStr(OutName);
OutName := NewStr(S);
InitPrinter;
Paused := P;
SetDestination := True;
end { TPrintManager.SetDestination };
constructor TPrintManager.Load;
begin
inherited Load(S);
GetPeerViewPtr(S, Status);
OutName := S.ReadStr;
S.Read(Paused, 1);
S.Read(FilePos, 4);
PrintDevice := New(PDosStream, Init(OutName^, stCreate));
BufSize := MaxBufCount;
GetMem(Buffer, BufSize);
isValid := True;
//JO: á¬. ª®¬¬¥â ਩ ª TPrintManager.SetDestination;
if (PrintDevice^.Status <> stOK)
and (PrintDevice^.ErrorInfo = 87) then
PrintDevice^.Status := stOK;
if PrintDevice^.Status <> stOK then
begin
isValid := False;
MessageBox(GetString(dlPrintNoInit)+ ^M^C'RC=' +
ItoS(PrintDevice^.ErrorInfo), nil, mfError+mfOKButton);
PrintDevice^.Status := 0;
Exit;
end;
PrintStream := New(PBufStream, Init(PString(List^.At(0))^, stOpenRead,
$400));
FileLen := PrintStream^.GetSize;
PrintStream^.Seek(FilePos);
{PrintStream := nil;}
Printer := @Self;
RegisterToBackground(@Self);
end { TPrintManager.Load };
procedure TPrintManager.Store;
begin
inherited Store(S);
PutPeerViewPtr(S, Status);
S.WriteStr(OutName);
S.Write(Paused, 1);
S.Write(FilePos, 4);
end;
destructor TPrintManager.Done;
begin
Printer := nil;
if PrintStream <> nil then
Dispose(PrintStream, Done);
PrintStream := nil;
if PrintDevice <> nil then
Dispose(PrintDevice, Done);
PrintDevice := nil;
if Buffer <> nil then
FreeMem(Buffer, BufSize);
Buffer := nil;
DisposeStr(OutName);
inherited Done;
end;
function TPrintManager.Valid;
begin
Inc(LockUpdate);
case C of
cmValid:
Valid := isValid and inherited Valid(C);
cmClose:
if MessageBox(GetString(dlPrintCancelQuery), nil, mfYesNoConfirm)
= cmYes
then
begin
Valid := True;
InitPrinter;
end
else
Valid := False;
end {case};
Dec(LockUpdate);
end;
procedure TPrintManager.HandleEvent;
begin
inherited HandleEvent(Event);
case Event.What of
evCommand:
case Event.Command of
cmGetName:
PString(Event.InfoPtr)^:= GetString(dlPManagerTitle);
cmClose:
Event.InfoPtr := nil;
cmNo:
begin
Paused := not Paused;
if Status <> nil then
Status^.DrawView;
ClearEvent(Event);
end;
cmOK:
begin
Inc(LockUpdate);
if (List <> nil) and (List^.Count > Focused) and
(MessageBox(GetString(dlPDeleteQeury1)+Cut(PString(List^.At(
Focused))^, 40)+
GetString(dlPDeleteQeury2), nil, mfYesNoConfirm) = cmYes)
then
begin
if (Focused = 0) and (PrintStream <> nil) then
begin
Dispose(PrintStream, Done);
PrintStream := nil;
InitPrinter;
PrintStream := nil;
Buffer^[0] := 12;
repeat
until PrintBuffer(1);
end;
List^.AtFree(Focused);
SetRange(List^.Count);
DrawView;
if Status <> nil then
Status^.DrawView;
end;
ClearEvent(Event);
Dec(LockUpdate);
end;
end {case};
end {case};
end { TPrintManager.HandleEvent };
procedure TPrintManager.PrintFile;
begin
if List = nil then
List := New(PStringCol, Init(10, 10));
{$IFDEF DPMI32}
List^.Insert(NewStr(lfGetLongFileName(FileName)));
{$ELSE}
List^.Insert(NewStr(FileName));
{$ENDIF}
SetRange(List^.Count);
DrawView;
if Status <> nil then
Status^.DrawView;
end;
procedure TPrintManager.InitPrinter;
//JO: ¯®¤ ¬®£®§ ¤ çª ¬¨ ¯à¨â¥à ¨¨æ¨ «¨§¨à®¢ âì ¥ ¤®;
// 㦮 ¡ã¤¥â ¯¥à¥¯¨á âì íâã ¯à®æ¥¤ãàã ¤«ï DPMI32
{var
Hndl: Word;}
begin
(* Hndl := PrintDevice^.Handle;
asm
MOV BX, Hndl
MOV AX,4400H { IOCTL, GET DEV ATTR }
INT 21H
JC @@100
{MOV [OLD_PRINTER_ATTR],AL}
TEST AL,80H {; 0 - disk}
JZ @@100
MOV DH,0
OR DL,20H {; BINARY MODE}
MOV BX,4
MOV AX,4401H {; IOCTL, SET DEV ATTR}
INT 21H
@@100:
end; *)
end;
function TPrintManager.GetStatus: Byte;
{ assembler;
asm
mov ah, 2
mov al, $90
mov dx, word ptr RPrinterSetup
cmp dx, 2
jnc @@1
int 17h
mov al, ah
@@1: }
begin
Result := $90; //¢à¥¬¥®! ®¤¥« âì!
end;
function TPrintManager.PrintBuffer;
label 1;
const
TryCount: Byte = 0;
var
B: Byte;
BB: Boolean;
begin
PrintBuffer := False;
B := GetStatus;
if (B and 8 <> 0) or (B and $10 = 0) then
begin
Inc(TryCount);
PrintBuffer := False;
Abort := False;
if TryCount > 20 then
begin
1:
TryCount := 0;
Paused := True;
MessageBox(GetString(dlCantPrint), nil, mfError+mfOKButton);
PrintBuffer := Num = 1;
end;
Exit;
end;
if (B and $90 = 0) or (B and 1 <> 0) then
Exit;
PrintBuffer := True;
{BB := NeedAbort;
NeedAbort := True; Abort := False;}
PrintDevice^.Status := 0;
PrintDevice^.Write(Buffer^, Num);
PrintDevice^.Status := 0;
{NeedAbort := BB;}
if Abort then
goto 1;
end { TPrintManager.PrintBuffer };
procedure TPrintManager.Update;
var
I, L: LongInt;
S: Byte;
procedure WriteERROR;
var
S: String;
begin
if List <> nil then
S := PString(List^.At(0))^;
Inc(LockUpdate);
if (PrintStream^.Status <> stOK) then
MessageBox(GetString(dlCantPrintFile)+S, nil,
mfError+mfOKButton);
Dispose(PrintStream, Done);
PrintStream := nil;
if InMask(GetName(S), '$DN????$.PRN') then
EraseFile(S);
List^.AtFree(0);
SetRange(List^.Count);
FocusItem(Focused-1);
DrawView;
if Status <> nil then
Status^.DrawView;
Dec(LockUpdate);
end;
begin { TPrintManager.Update }
if (List = nil) or Paused or (LockUpdate <> 0) then
Exit;
if (PrintStream = nil) then
begin
if (List = nil) or (List^.Count < 1) then
begin
if not Owner^.GetState(sfDragging) then
Owner^.Free;
Exit;
end;
PrintStream := New(PBufStream, Init(PString(List^.At(0))^,
stOpenRead, $400));
FileLen := 0;
FilePos := 0;
if PrintStream^.Status <> stOK
then
WriteERROR
else
begin
FileLen := PrintStream^.GetSize;
if RPrinterSetup.InitPrinter = '' then
begin
BufCount := MaxBufCount;
if BufCount > FileLen then
BufCount := i32(FileLen);
PrintStream^.Read(Buffer^, BufCount);
end
else
begin
BufCount := Length(RPrinterSetup.InitPrinter);
Move(RPrinterSetup.InitPrinter[1], Buffer^, BufCount);
end;
end;
end
else
begin
if PrintBuffer(BufCount) then
begin
FilePos := PrintStream^.GetPos;
if (FilePos >= FileLen) or (PrintStream^.Status <> stOK) then
begin
if RPrinterSetup.AfterFile <> '' then
begin
BufCount := Length(RPrinterSetup.AfterFile);
Move(RPrinterSetup.AfterFile[1], Buffer^, BufCount);
repeat
until PrintBuffer(BufCount);
if not Paused then
WriteERROR;
end
else if not Paused then
WriteERROR;
end
else
begin
BufCount := MaxBufCount;
if BufCount+FilePos > FileLen then
BufCount := i32(FileLen-FilePos);
PrintStream^.Read(Buffer^, BufCount);
end;
end;
end;
if Status <> nil then
Status^.DrawView;
end { TPrintManager.Update };
procedure SetupPrinter;
var
LastDest: Word;
begin
LastDest := RPrinterSetup.Device;
if ExecResource(dlgPrinterSetup, RPrinterSetup) = cmOK then
Message(Application, evCommand, cmUpdateConfig, nil);
if (LastDest <> RPrinterSetup.Device) and (Printer <> nil) then
Printer^.SetDestination;
end;
procedure PrintFile(const S: String);
var
W: PView;
R: TRect;
begin
R.Assign(0, 0, 50, 9);
if Printer = nil then
begin
W := Application^.ValidView(New(PPMWindow, Init(R)));
if W <> nil then
begin
W^.Hide;
Desktop^.InsertView(W, Desktop^.Last);
W^.Show;
end;
end;
if Printer <> nil then
Printer^.PrintFile(S);
end;
{$ELSE}
uses
Messages
;
procedure PrintFile(const S: String);
begin
MessageBox('Simulating: '+S, nil, mfOKButton);
end;
{$ENDIF}
end.