Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/teraterm/teraterm/teraprn.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3227 - (hide annotations) (download) (as text)
Tue Mar 24 15:10:33 2009 UTC (15 years ago) by maya
File MIME type: text/x-c++src
File size: 14683 byte(s)
CVS から SVN へ移行: 改行コードを LF から CR+LF へ変換
1 maya 3227 /* Tera Term
2     Copyright(C) 1994-1998 T. Teranishi
3     All rights reserved. */
4    
5     /* TERATERM.EXE, Printing routines */
6     #include "stdafx.h"
7     #include "teraterm.h"
8     #include "tttypes.h"
9     #include <commdlg.h>
10     #include <stdio.h>
11    
12     #include "ttwinman.h"
13     #include "commlib.h"
14     #include "ttcommon.h"
15     #include "ttlib.h"
16    
17     #include "tt_res.h"
18     #include "prnabort.h"
19    
20     #include "teraprn.h"
21    
22     #ifdef _DEBUG
23     #define new DEBUG_NEW
24     #undef THIS_FILE
25     static char THIS_FILE[] = __FILE__;
26     #endif
27    
28     static PRINTDLG PrnDlg;
29    
30     static HDC PrintDC;
31     static LOGFONT Prnlf;
32     static HFONT PrnFont[AttrFontMask+1];
33     static int PrnFW, PrnFH;
34     static RECT Margin;
35     static COLORREF White, Black;
36     static int PrnX, PrnY;
37     static int PrnDx[256];
38     static TCharAttr PrnAttr;
39    
40     static BOOL Printing = FALSE;
41     static BOOL PrintAbortFlag = FALSE;
42    
43     /* pass-thru printing */
44     static char PrnFName[MAXPATHLEN];
45     static int HPrnFile = 0;
46     static char PrnBuff[TermWidthMax];
47     static int PrnBuffCount = 0;
48    
49     static CPrnAbortDlg *PrnAbortDlg;
50     static HWND HPrnAbortDlg;
51    
52     /* Print Abortion Call Back Function */
53     BOOL CALLBACK PrnAbortProc(HDC PDC, int Code)
54     {
55     MSG m;
56    
57     while ((! PrintAbortFlag) && PeekMessage(&m, 0,0,0, PM_REMOVE))
58     if ((HPrnAbortDlg==NULL) || (! IsDialogMessage(HPrnAbortDlg, &m)))
59     {
60     TranslateMessage(&m);
61     DispatchMessage(&m);
62     }
63    
64     if (PrintAbortFlag)
65     {
66     HPrnAbortDlg = NULL;
67     PrnAbortDlg = NULL;
68     return FALSE;
69     }
70     else
71     return TRUE;
72     }
73    
74     extern "C" {
75     HDC PrnBox(HWND HWin, PBOOL Sel)
76     {
77     /* initialize PrnDlg record */
78     memset(&PrnDlg, 0, sizeof(PRINTDLG));
79     PrnDlg.lStructSize = sizeof(PRINTDLG);
80     PrnDlg.hwndOwner = HWin;
81     PrnDlg.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_SHOWHELP;
82     if (! *Sel)
83     PrnDlg.Flags = PrnDlg.Flags | PD_NOSELECTION;
84     PrnDlg.nCopies = 1;
85    
86     /* 'Print' dialog box */
87     if (! PrintDlg(&PrnDlg)) return NULL; /* if 'Cancel' button clicked, exit */
88     if (PrnDlg.hDC == NULL) return NULL;
89     PrintDC = PrnDlg.hDC;
90     *Sel = (PrnDlg.Flags & PD_SELECTION) != 0;
91     return PrintDC;
92     }
93     }
94    
95     extern "C" {
96     BOOL PrnStart(LPSTR DocumentName)
97     {
98     DOCINFO Doc;
99     char DocName[50];
100     CWnd* pParent;
101     char uimsg[MAX_UIMSG];
102    
103     Printing = FALSE;
104     PrintAbortFlag = FALSE;
105    
106     PrnAbortDlg = new CPrnAbortDlg();
107     if (PrnAbortDlg==NULL) return FALSE;
108     if (ActiveWin==IdVT)
109     pParent = (CWnd*)pVTWin;
110     else
111     pParent = (CWnd*)pTEKWin;
112     PrnAbortDlg->Create(pParent,&PrintAbortFlag,&ts);
113     HPrnAbortDlg = PrnAbortDlg->GetSafeHwnd();
114    
115     GetDlgItemText(HPrnAbortDlg, IDC_PRNABORT_PRINTING, uimsg, sizeof(uimsg));
116     get_lang_msg("DLG_PRNABORT_PRINTING", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
117     SetDlgItemText(HPrnAbortDlg, IDC_PRNABORT_PRINTING, ts.UIMsg);
118     GetDlgItemText(HPrnAbortDlg, IDCANCEL, uimsg, sizeof(uimsg));
119     get_lang_msg("BTN_CANCEL", ts.UIMsg, sizeof(ts.UIMsg), uimsg, ts.UILanguageFile);
120     SetDlgItemText(HPrnAbortDlg, IDCANCEL, ts.UIMsg);
121    
122     SetAbortProc(PrintDC,PrnAbortProc);
123    
124     Doc.cbSize = sizeof(DOCINFO);
125     strncpy_s(DocName,sizeof(DocName),DocumentName,_TRUNCATE);
126     Doc.lpszDocName = DocName;
127     Doc.lpszOutput = NULL;
128     Doc.lpszDatatype = NULL;
129     Doc.fwType = 0;
130     if (StartDoc(PrintDC, &Doc) > 0)
131     Printing = TRUE;
132     else
133     if (PrnAbortDlg != NULL)
134     {
135     PrnAbortDlg->DestroyWindow();
136     PrnAbortDlg = NULL;
137     HPrnAbortDlg = NULL;
138     }
139     return Printing;
140     }
141     }
142    
143     extern "C" {
144     void PrnStop()
145     {
146     if (Printing)
147     {
148     EndDoc(PrintDC);
149     DeleteDC(PrintDC);
150     Printing = FALSE;
151     }
152     if (PrnAbortDlg != NULL)
153     {
154     PrnAbortDlg->DestroyWindow();
155     PrnAbortDlg = NULL;
156     HPrnAbortDlg = NULL;
157     }
158     }
159     }
160    
161     extern "C" {
162     int VTPrintInit(int PrnFlag)
163     // Initialize printing of VT window
164     // PrnFlag: specifies object to be printed
165     // = IdPrnScreen Current screen
166     // = IdPrnSelectedText Selected text
167     // = IdPrnScrollRegion Scroll region
168     // = IdPrnFile Spooled file (printer sequence)
169     // Return: print object ID specified by user
170     // = IdPrnCancel (user clicks "Cancel" button)
171     // = IdPrnScreen (user don't select "print selection" option)
172     // = IdPrnSelectedText (user selects "print selection")
173     // = IdPrnScrollRegion (always when PrnFlag=IdPrnScrollRegion)
174     // = IdPrnFile (always when PrnFlag=IdPrnFile)
175     {
176     BOOL Sel;
177     TEXTMETRIC Metrics;
178     POINT PPI, PPI2;
179     HDC DC;
180     int i;
181     TCharAttr TempAttr = {
182     AttrDefault,
183     AttrDefault,
184     AttrDefaultFG,
185     AttrDefaultBG
186     };
187    
188     Sel = (PrnFlag & IdPrnSelectedText)!=0;
189     if (PrnBox(HVTWin,&Sel)==NULL) return (IdPrnCancel);
190    
191     if (PrintDC==0) return (IdPrnCancel);
192    
193     /* start printing */
194     if (! PrnStart(ts.Title)) return (IdPrnCancel);
195    
196     /* initialization */
197     StartPage(PrintDC);
198    
199     /* pixels per inch */
200     if ((ts.VTPPI.x>0) && (ts.VTPPI.y>0))
201     PPI = ts.VTPPI;
202     else {
203     PPI.x = GetDeviceCaps(PrintDC,LOGPIXELSX);
204     PPI.y = GetDeviceCaps(PrintDC,LOGPIXELSY);
205     }
206    
207     Margin.left = /* left margin */
208     (int)((float)ts.PrnMargin[0] / 100.0 * (float)PPI.x);
209     Margin.right = /* right margin */
210     GetDeviceCaps(PrintDC,HORZRES) -
211     (int)((float)ts.PrnMargin[1] / 100.0 * (float)PPI.x);
212     Margin.top = /* top margin */
213     (int)((float)ts.PrnMargin[2] / 100.0 * (float)PPI.y);
214     Margin.bottom = /* bottom margin */
215     GetDeviceCaps(PrintDC,VERTRES) -
216     (int)((float)ts.PrnMargin[3] / 100.0 * (float)PPI.y);
217    
218     /* create test font */
219     memset(&Prnlf, 0, sizeof(LOGFONT));
220    
221     if (ts.PrnFont[0]==0)
222     {
223     Prnlf.lfHeight = ts.VTFontSize.y;
224     Prnlf.lfWidth = ts.VTFontSize.x;
225     Prnlf.lfCharSet = ts.VTFontCharSet;
226     strncpy_s(Prnlf.lfFaceName, sizeof(Prnlf.lfFaceName), ts.VTFont, _TRUNCATE);
227     }
228     else {
229     Prnlf.lfHeight = ts.PrnFontSize.y;
230     Prnlf.lfWidth = ts.PrnFontSize.x;
231     Prnlf.lfCharSet = ts.PrnFontCharSet;
232     strncpy_s(Prnlf.lfFaceName, sizeof(Prnlf.lfFaceName), ts.PrnFont, _TRUNCATE);
233     }
234     Prnlf.lfWeight = FW_NORMAL;
235     Prnlf.lfItalic = 0;
236     Prnlf.lfUnderline = 0;
237     Prnlf.lfStrikeOut = 0;
238     Prnlf.lfOutPrecision = OUT_CHARACTER_PRECIS;
239     Prnlf.lfClipPrecision = CLIP_CHARACTER_PRECIS;
240     Prnlf.lfQuality = DEFAULT_QUALITY;
241     Prnlf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
242    
243     PrnFont[0] = CreateFontIndirect(&Prnlf);
244    
245     DC = GetDC(HVTWin);
246     SelectObject(DC, PrnFont[0]);
247     GetTextMetrics(DC, &Metrics);
248     PPI2.x = GetDeviceCaps(DC,LOGPIXELSX);
249     PPI2.y = GetDeviceCaps(DC,LOGPIXELSY);
250     ReleaseDC(HVTWin,DC);
251     DeleteObject(PrnFont[0]); /* Delete test font */
252    
253     /* Adjust font size */
254     Prnlf.lfHeight =
255     (int)((float)Metrics.tmHeight * (float)PPI.y / (float)PPI2.y);
256     Prnlf.lfWidth =
257     (int)((float)Metrics.tmAveCharWidth * (float)PPI.x / (float)PPI2.x);
258    
259     /* Create New Fonts */
260    
261     /* Normal Font */
262     Prnlf.lfWeight = FW_NORMAL;
263     Prnlf.lfUnderline = 0;
264     PrnFont[0] = CreateFontIndirect(&Prnlf);
265     SelectObject(PrintDC,PrnFont[0]);
266     GetTextMetrics(PrintDC, &Metrics);
267     PrnFW = Metrics.tmAveCharWidth;
268     PrnFH = Metrics.tmHeight;
269     /* Under line */
270     Prnlf.lfUnderline = 1;
271     PrnFont[AttrUnder] = CreateFontIndirect(&Prnlf);
272    
273     if (ts.EnableBold > 0)
274     {
275     /* Bold */
276     Prnlf.lfUnderline = 0;
277     Prnlf.lfWeight = FW_BOLD;
278     PrnFont[AttrBold] = CreateFontIndirect(&Prnlf);
279     /* Bold + Underline */
280     Prnlf.lfUnderline = 1;
281     PrnFont[AttrBold | AttrUnder] = CreateFontIndirect(&Prnlf);
282     }
283     else {
284     PrnFont[AttrBold] = PrnFont[AttrDefault];
285     PrnFont[AttrBold | AttrUnder] = PrnFont[AttrUnder];
286     }
287     /* Special font */
288     Prnlf.lfWeight = FW_NORMAL;
289     Prnlf.lfUnderline = 0;
290     Prnlf.lfWidth = PrnFW; /* adjust width */
291     Prnlf.lfHeight = PrnFH;
292     Prnlf.lfCharSet = SYMBOL_CHARSET;
293    
294     strncpy_s(Prnlf.lfFaceName, sizeof(Prnlf.lfFaceName),"Tera Special", _TRUNCATE);
295     PrnFont[AttrSpecial] = CreateFontIndirect(&Prnlf);
296     PrnFont[AttrSpecial | AttrBold] = PrnFont[AttrSpecial];
297     PrnFont[AttrSpecial | AttrUnder] = PrnFont[AttrSpecial];
298     PrnFont[AttrSpecial | AttrBold | AttrUnder] = PrnFont[AttrSpecial];
299    
300     Black = RGB(0,0,0);
301     White = RGB(255,255,255);
302     for (i = 0 ; i <= 255 ; i++)
303     PrnDx[i] = PrnFW;
304     PrnSetAttr(TempAttr);
305    
306     PrnY = Margin.top;
307     PrnX = Margin.left;
308    
309     if (PrnFlag == IdPrnScrollRegion)
310     return (IdPrnScrollRegion);
311     if (PrnFlag == IdPrnFile)
312     return (IdPrnFile);
313     if (Sel) return (IdPrnSelectedText);
314     else return (IdPrnScreen);
315    
316     }
317     }
318     extern "C" {
319     void PrnSetAttr(TCharAttr Attr)
320     // Set text attribute of printing
321     //
322     {
323     PrnAttr = Attr;
324     SelectObject(PrintDC, PrnFont[Attr.Attr & AttrFontMask]);
325    
326     if ((Attr.Attr & AttrReverse) != 0)
327     {
328     SetTextColor(PrintDC,White);
329     SetBkColor( PrintDC,Black);
330     }
331     else {
332     SetTextColor(PrintDC,Black);
333     SetBkColor( PrintDC,White);
334     }
335     }
336     }
337    
338     extern "C" {
339     void PrnOutText(PCHAR Buff, int Count)
340     // Print out text
341     // Buff: points text buffer
342     // Count: number of characters to be printed
343     {
344     int i;
345     RECT RText;
346     PCHAR Ptr, Ptr1, Ptr2;
347     char Buff2[256];
348    
349     if (Count<=0) return;
350     if (Count>(sizeof(Buff2)-1)) Count=sizeof(Buff2)-1;
351     memcpy(Buff2,Buff,Count);
352     Buff2[Count] = 0;
353     Ptr = Buff2;
354    
355     if (ts.Language==IdRussian)
356     {
357     if (ts.PrnFont[0]==0)
358     RussConvStr(ts.RussClient,ts.RussFont,Buff2,Count);
359     else
360     RussConvStr(ts.RussClient,ts.RussPrint,Buff2,Count);
361     }
362    
363     do {
364     if (PrnX+PrnFW > Margin.right)
365     {
366     /* new line */
367     PrnX = Margin.left;
368     PrnY = PrnY + PrnFH;
369     }
370     if (PrnY+PrnFH > Margin.bottom)
371     {
372     /* next page */
373     EndPage(PrintDC);
374     StartPage(PrintDC);
375     PrnSetAttr(PrnAttr);
376     PrnY = Margin.top;
377     }
378    
379     i = (Margin.right-PrnX) / PrnFW;
380     if (i==0) i=1;
381     if (i>Count) i=Count;
382    
383     if (i<Count)
384     {
385     Ptr2 = Ptr;
386     do {
387     Ptr1 = Ptr2;
388     Ptr2 = CharNext(Ptr1);
389     } while ((Ptr2!=NULL) && ((Ptr2-Ptr)<=i));
390     i = Ptr1-Ptr;
391     if (i<=0) i=1;
392     }
393    
394     RText.left = PrnX;
395     RText.right = PrnX + i*PrnFW;
396     RText.top = PrnY;
397     RText.bottom = PrnY+PrnFH;
398     ExtTextOut(PrintDC,PrnX,PrnY,6,&RText,Ptr,
399     i,&PrnDx[0]);
400     PrnX = RText.right;
401     Count=Count-i;
402     Ptr = Ptr + i;
403     } while (Count>0);
404    
405     }
406     }
407    
408     extern "C" {
409     void PrnNewLine()
410     // Moves to the next line in printing
411     {
412     PrnX = Margin.left;
413     PrnY = PrnY + PrnFH;
414     }
415     }
416    
417     extern "C" {
418     void VTPrintEnd()
419     {
420     int i, j;
421    
422     EndPage(PrintDC);
423    
424     for (i = 0 ; i <= AttrFontMask ; i++)
425     {
426     for (j = i+1 ; j <= AttrFontMask ; j++)
427     if (PrnFont[j]==PrnFont[i])
428     PrnFont[j] = NULL;
429     if (PrnFont[i] != NULL) DeleteObject(PrnFont[i]);
430     }
431    
432     PrnStop();
433     return;
434     }
435     }
436    
437     /* printer emulation routines */
438     extern "C" {
439     void OpenPrnFile()
440     {
441     char Temp[MAXPATHLEN];
442    
443     KillTimer(HVTWin,IdPrnStartTimer);
444     if (HPrnFile > 0) return;
445     if (PrnFName[0] == 0)
446     {
447     GetTempPath(sizeof(Temp),Temp);
448     if (GetTempFileName(Temp,"tmp",0,PrnFName)==0) return;
449     HPrnFile = _lcreat(PrnFName,0);
450     }
451     else {
452     HPrnFile = _lopen(PrnFName,OF_WRITE);
453     if (HPrnFile <= 0)
454     HPrnFile = _lcreat(PrnFName,0);
455     }
456     if (HPrnFile > 0)
457     _llseek(HPrnFile,0,2);
458     }
459     }
460    
461     void PrintFile()
462     {
463     char Buff[256];
464     BOOL CRFlag = FALSE;
465     int c, i;
466     BYTE b;
467    
468     if (VTPrintInit(IdPrnFile)==IdPrnFile)
469     {
470     HPrnFile = _lopen(PrnFName,OF_READ);
471     if (HPrnFile>0)
472     {
473     do {
474     i = 0;
475     do {
476     c = _lread(HPrnFile,&b,1);
477     if (c==1)
478     {
479     switch (b) {
480     case HT:
481     memset(&(Buff[i]),0x20,8);
482     i = i + 8;
483     CRFlag = FALSE;
484     break;
485     case LF:
486     CRFlag = ! CRFlag;
487     break;
488     case FF:
489     case CR:
490     CRFlag = TRUE;
491     break;
492     default:
493     if (b >= 0x20)
494     {
495     Buff[i] = b;
496     i++;
497     }
498     CRFlag = FALSE;
499     break;
500     }
501     }
502     if (i>=(sizeof(Buff)-7)) CRFlag=TRUE;
503     } while ((c>0) && (! CRFlag));
504     if (i>0) PrnOutText(Buff, i);
505     if (CRFlag)
506     {
507     PrnX = Margin.left;
508     if ((b==FF) && (ts.PrnConvFF==0)) // new page
509     PrnY = Margin.bottom;
510     else // new line
511     PrnY = PrnY + PrnFH;
512     }
513     CRFlag = (b==CR);
514     } while (c>0);
515     _lclose(HPrnFile);
516     }
517     HPrnFile = 0;
518     VTPrintEnd();
519     }
520     remove(PrnFName);
521     PrnFName[0] = 0;
522     }
523    
524     void PrintFileDirect()
525     {
526     CWnd* pParent;
527    
528     PrnAbortDlg = new CPrnAbortDlg();
529     if (PrnAbortDlg==NULL)
530     {
531     remove(PrnFName);
532     PrnFName[0] = 0;
533     return;
534     }
535     if (ActiveWin==IdVT)
536     pParent = (CWnd*)pVTWin;
537     else
538     pParent = (CWnd*)pTEKWin;
539     PrnAbortDlg->Create(pParent,&PrintAbortFlag,&ts);
540     HPrnAbortDlg = PrnAbortDlg->GetSafeHwnd();
541    
542     HPrnFile = _lopen(PrnFName,OF_READ);
543     PrintAbortFlag = (HPrnFile<=HFILE_ERROR) || ! PrnOpen(ts.PrnDev);
544     PrnBuffCount = 0;
545     SetTimer(HVTWin,IdPrnProcTimer,0,NULL);
546     }
547    
548     void PrnFileDirectProc()
549     {
550     int c;
551    
552     if (HPrnFile==0) return;
553     if (PrintAbortFlag)
554     {
555     HPrnAbortDlg = NULL;
556     PrnAbortDlg = NULL;
557     PrnCancel();
558     }
559     if (!PrintAbortFlag && (HPrnFile>0))
560     {
561     do {
562     if (PrnBuffCount==0)
563     {
564     PrnBuffCount = _lread(HPrnFile,PrnBuff,1);
565     if (ts.Language==IdRussian)
566     RussConvStr(ts.RussClient,ts.RussPrint,PrnBuff,PrnBuffCount);
567     }
568    
569     if (PrnBuffCount==1)
570     {
571     c = PrnWrite(PrnBuff,1);
572     if (c==0)
573     {
574     SetTimer(HVTWin,IdPrnProcTimer,10,NULL);
575     return;
576     }
577     PrnBuffCount = 0;
578     }
579     else
580     c = 0;
581     } while (c>0);
582     }
583     if (HPrnFile > 0)
584     _lclose(HPrnFile);
585     HPrnFile = 0;
586     PrnClose();
587     remove(PrnFName);
588     PrnFName[0] = 0;
589     if (PrnAbortDlg!=NULL)
590     {
591     PrnAbortDlg->DestroyWindow();
592     PrnAbortDlg = NULL;
593     HPrnAbortDlg = NULL;
594     }
595     }
596    
597     void PrnFileStart()
598     {
599     if (HPrnFile>0) return;
600     if (PrnFName[0]==0) return;
601     if (ts.PrnDev[0]!=0)
602     PrintFileDirect(); // send file directry to printer port
603     else // print file by using Windows API
604     PrintFile();
605     }
606    
607     extern "C" {
608     void ClosePrnFile()
609     {
610     PrnBuffCount = 0;
611     if (HPrnFile > 0)
612     _lclose(HPrnFile);
613     HPrnFile = 0;
614     SetTimer(HVTWin,IdPrnStartTimer,ts.PassThruDelay*1000,NULL);
615     }
616     }
617    
618     extern "C" {
619     void WriteToPrnFile(BYTE b, BOOL Write)
620     // (b,Write) =
621     // (0,FALSE): clear buffer
622     // (0,TRUE): write buffer to file
623     // (b,FALSE): put b in buff
624     // (b,TRUE): put b in buff and
625     // write buffer to file
626     {
627     if ((b>0) && (PrnBuffCount<sizeof(PrnBuff)))
628     PrnBuff[PrnBuffCount++] = b;
629    
630     if (Write)
631     {
632     _lwrite(HPrnFile,PrnBuff,PrnBuffCount);
633     PrnBuffCount = 0;
634     }
635     if ((b==0) && ! Write) PrnBuffCount = 0;
636     }
637     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26