-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintMan.dpr
296 lines (269 loc) · 9.3 KB
/
PrintMan.dpr
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
(*
Print Manàger 2 - ïëàãèí Ìåíåæäåð ïå÷àòè 2
Copyright (c) 2009, [ niarbrnd ]
Roman A. Dyachkov
*)
{$APPTYPE CONSOLE}
{$MINENUMSIZE 4}
library hello;
uses system,sysutils,windows,plugin,Printers,winspool,Registry;
const
PName = 'PrintMan2';
type
TMessage = (MTitle, MTitleMenu, MButton);
var
FARAPI: TPluginStartupInfo;
FRegRoot,PLastPrinter:string;
function GetMsg(MsgId: TMessage): PChar;
begin
result:= FARAPI.GetMsg(FARAPI.ModuleNumber, integer(MsgId));
end;
procedure ReadSettings; //ïîëó÷àåì íàñòðîéêè ïëàãèíà
var
vKey :TRegistry;
begin
vKey:=TRegistry.Create;
vKey.RootKey := HKEY_CURRENT_USER;
if not vKey.OpenKeyReadOnly(FRegRoot+'\'+PName) then
Exit;
try
if vKey.ValueExists('LastSelectPrinter') then
PLastPrinter:= vKey.ReadString('LastSelectPrinter'); //ïîëó÷àåì ïðèíòåð íà êîòîðûé âûïîëíÿëàñü ïðåäûäóùàÿ ïå÷àòü
vKey.CloseKey;
finally
vKey.Free;
end;
end;
procedure mess (strMess: string); // èñïîëüçóåòñÿ äëÿ îòëàäêè è âûâîäà ñîîáùåíèé îá îøèáêàõ
var
Msg: array[0..3] of PChar;
begin
Msg[0]:= pchar(strMess);
Msg[1]:= pchar(strMess);
Msg[2]:= #01#00; // separator line
Msg[3]:= GetMsg(MButton);
FARAPI.Message(FARAPI.ModuleNumber, // PluginNumber
FMSG_WARNING or FMSG_LEFTALIGN, // Flags
'Contents', // HelpTopic
@Msg, // Items
4, // ItemsNumber
1); // ButtonsNumber
end;
function WriteSettings(Param:string;Value:string):boolean;
var
vKey :TRegistry;
begin
result:=false;
vKey:=TRegistry.Create;
vKey.RootKey := HKEY_CURRENT_USER;
try
if vKey.OpenKey (FRegRoot+'\'+PName,True) then
begin
vKey.WriteString(Param,Value);
vKey.CloseKey;
result:=true;
end;
finally
vKey.Free;
end;
end;
procedure SetStartupInfo(var psi: TPluginStartupInfo); stdcall;
begin
Move(psi, FARAPI, SizeOf(FARAPI));
FRegRoot:=FARAPI.RootKey;
end;
var
PluginMenuStrings: array[0..0] of PChar;
procedure GetPluginInfo(var pi: TPluginInfo); stdcall;
begin
pi.StructSize:= SizeOf(pi);
pi.Flags:= PF_EDITOR;
PluginMenuStrings[0]:= GetMsg(MTitle);
pi.PluginMenuStrings:= @PluginMenuStrings;
pi.PluginMenuStringsNumber:= 1;
pi.SysID:=1851870544; //Óêàçôûâàåì SYSID ðàâíûé SYSID ïëàãèíà èäóùåãî â ïîñòàâêå Far
//pi.PluginConfigStrings:= @PluginMenuStrings;
//pi.PluginConfigStringsNumber:= 1;
end;
function OpenPlugin(OpenFrom: integer; Item: integer): THandle; stdcall;
var
Msg:array[0..127] of TFarMenuItem;
i,resultmenu: integer;
pinfo:TPanelInfo;
curdirstr,curfilename:string;
curstringseltext:pchar;
strcurstringseltext:string;
prnfile: file;
buffer: array [0..127] of Char;
Read,iCurrLine: Integer;
ei: TEditorInfo;
egs: TEditorGetString;
isSelectEndString: boolean;
FDevice: PChar;
FPrinterHandle: THandle;
DOC:DOC_INFO_1;
dwBytesWritten:Cardinal;
begin
result:=INVALID_HANDLE_VALUE; //Óñòàíàâëèâàåì äëÿ âîçâðàùåíèÿ â ïîñëåäíåå ñîñòîÿíèå ïîñëå èñïîëíåíèÿ ïëàãèíà
ReadSettings;
fillchar(msg ,128*SizeOf(TFarMenuItem),#0);
for i := 0 to Printer.Printers.Count-1 do
begin
StrPCopy(Msg[i].Text, Printer.Printers.Strings[i]);
if Printer.Printers.Strings[i]=PLastPrinter then
msg[i].Selected:=true;
end;
resultmenu:= farapi.Menu( FARAPI.ModuleNumber,
-1,-1, 0,FMENU_WRAPMODE,
GetMsg(MTitleMenu),// Çàãîëîâîê ìåíþ
nil, 'Contents', nil, nil, @Msg, // Items
Printer.Printers.Count);
if resultmenu > -1 then
begin
if not WriteSettings('LastSelectPrinter',Printer.Printers.Strings[resultmenu]) then
mess('Error save last selected printed');
GetMem(FDevice, 128);
FDevice:=pchar(Printer.Printers.Strings[resultmenu]);
if OpenPrinter(FDevice, FPrinterHandle, nil) then
begin
//çàïóùåí ëè ïëàãèí èç ðåäàêòîðà
//mess ('opened printer');
if OpenFrom= OPEN_EDITOR then
begin
FARAPI.EditorControl(ECTL_GETINFO,@ei);
//åñòü ëè âûäåëåííûé ôðàãìåíò
if ei.BlockType <> BTYPE_None then
begin
iCurrLine:=ei.BlockStartLine-1;
///
doc.pDocName:='Print Manager';
doc.pOutputFile:=nil;
doc.pDatatype:='RAW';
// Íà÷àëî äîêóìåíòà
if StartDocPrinter( FPrinterHandle, 1, @DOC) = 0 then
begin
ClosePrinter( FPrinterHandle );
Exit
end;
// Íà÷àëî ñòðàíèöû.
if not StartPagePrinter( FPrinterHandle ) then
begin
EndDocPrinter( FPrinterHandle );
ClosePrinter( FPrinterHandle );
Exit
end;
// Ïîñûëàåì äàííûå íà ïðèíòåð.
///
repeat
iCurrLine:=iCurrLine+1;
isSelectEndString:=False;
egs.StringNumber:=iCurrLine;
FARAPI.EditorControl(ECTL_GETSTRING,@egs);
if egs.SelStart=-1 then
Break;
if (egs.SelEnd=-1) or (egs.SelEnd>egs.StringLength) then
begin
egs.SelEnd:=egs.StringLength;
isSelectEndString:=True;
if egs.SelEnd < egs.SelStart then
egs.SelEnd:=egs.SelStart;
end;
strcurstringseltext:=copy(egs.StringText,egs.SelStart+1,egs.SelEnd-egs.SelStart);
if isSelectEndString then strcurstringseltext:=strcurstringseltext+#13#10;
Getmem(curstringseltext,length(strcurstringseltext));
curstringseltext:=pchar(strcurstringseltext);
if not WritePrinter( FPrinterHandle, curstringseltext, length(curstringseltext), dwBytesWritten ) then
begin
CloseFile(prnfile);
EndPagePrinter( FPrinterHandle );
EndDocPrinter( FPrinterHandle );
ClosePrinter( FPrinterHandle );
mess ('error send data to printer');
exit
end;
until iCurrLine>ei.TotalLines;
if not EndPagePrinter( FPrinterHandle ) then
begin
mess ('error end page');
EndDocPrinter( FPrinterHandle );
ClosePrinter( FPrinterHandle );
exit
end;
// Èíôîðìèðóåì ñïóëåð î êîíöå äîêóìåíòà.
if not EndDocPrinter( FPrinterHandle ) then
begin
mess ('error end doc');
ClosePrinter( FPrinterHandle );
exit
end;
// Çàêðûâàåì äåñêðèïòîð ïðèíòåðà.
ClosePrinter(FPrinterHandle)
///
end;
end
else //èíà÷å ïëàãèí âûçâàí íå èç ðåäàêòîðà
begin
farapi.Control (INVALID_HANDLE_VALUE,FCTL_GETPANELINFO,@pinfo);
curdirstr:=pinfo.CurDir;
curfilename:=pinfo.PanelItems[pinfo.CurrentItem].FindData.cFileName ;
if pinfo.PanelItems[pinfo.CurrentItem].FindData.dwFileAttributes <> 16 then
begin
doc.pDocName:='Print Manager';
doc.pOutputFile:=nil;
doc.pDatatype:='RAW';
// Íà÷àëî äîêóìåíòà
if StartDocPrinter( FPrinterHandle, 1, @DOC) = 0 then
begin
ClosePrinter( FPrinterHandle );
Exit
end;
// Íà÷àëî ñòðàíèöû.
if not StartPagePrinter( FPrinterHandle ) then
begin
EndDocPrinter( FPrinterHandle );
ClosePrinter( FPrinterHandle );
Exit
end;
// Ïîñûëàåì äàííûå íà ïðèíòåð.
AssignFile(prnfile, curdirstr + '\' + curfilename); //Îòêðûâàåì ôàéë äëÿ ÷òåíèÿ
Reset(prnfile, 1);
//mess ('send data to printer');
repeat
BlockRead(prnfile, buffer, SizeOf(buffer), Read);
if( WritePrinter( FPrinterHandle, @buffer, Read, dwBytesWritten )= False ) then
begin
CloseFile(prnfile);
EndPagePrinter( FPrinterHandle );
EndDocPrinter( FPrinterHandle );
ClosePrinter( FPrinterHandle );
mess ('error send data to printer');
exit
end;
until EOF(prnfile) or (Read <> SizeOf(buffer));
CloseFile(prnfile);
// Êîíåö ñòðàíèöû.
if not EndPagePrinter( FPrinterHandle ) then
begin
mess ('error end page');
EndDocPrinter( FPrinterHandle );
ClosePrinter( FPrinterHandle );
exit
end;
// Èíôîðìèðóåì ñïóëåð î êîíöå äîêóìåíòà.
if not EndDocPrinter( FPrinterHandle ) then
begin
ClosePrinter( FPrinterHandle );
exit
end;
// Çàêðûâàåì äåñêðèïòîð ïðèíòåðà.
ClosePrinter(FPrinterHandle)
end
end;
end
end
end;
exports
SetStartupInfo,
GetPluginInfo,
OpenPlugin;
end.