source: dev/trunk/abdev/ProjectEditor/Print.cpp@ 157

Last change on this file since 157 was 24, checked in by dai_9181, 18 years ago

保存されていないドキュメントのタブに(*)をつける機能に対応。
MDITEXTEDITINFOをCMdiTextEditに変更。今後、オブジェクト指向化を進める。

File size: 13.8 KB
Line 
1#include "common.h"
2
3
4int iPreviewNowPage;
5int iZoomNowPage;
6
7#define ZOOM_NUM 8
8#define ZOOM_INIT 4
9int array_Zoom[ZOOM_NUM]={
10 500,
11 200,
12 150,
13 100,
14 75,
15 50,
16 25,
17 10
18};
19
20
21CPage obj_Page;
22
23CPage::CPage(){
24 memset(&psd, 0, sizeof(PAGESETUPDLG));
25 psd.lStructSize = sizeof(PAGESETUPDLG);
26 psd.hwndOwner = hOwner;
27 psd.Flags = PSD_MARGINS |PSD_INHUNDREDTHSOFMILLIMETERS|PSD_DISABLEORIENTATION;
28 psd.rtMargin.left=2500;
29 psd.rtMargin.top=2500;
30 psd.rtMargin.right=2500;
31 psd.rtMargin.bottom=2500;
32 psd.ptPaperSize.x=21000;
33 psd.ptPaperSize.y=29700;
34}
35CPage::~CPage(){
36}
37
38BOOL CPage::SetupDlg(void){
39 //ページ設定ダイアログ
40 return PageSetupDlg(&psd);
41}
42
43void DrawPage(HDC memdc,BOOL bPreview,BOOL *lpbLastPage=0){
44 RECT rc;
45
46 //ページの幅、高さ(単位は0.1ミリメートル)
47 int iPageWidth,iPageHeight;
48 iPageWidth=obj_Page.psd.ptPaperSize.x/10;
49 iPageHeight=obj_Page.psd.ptPaperSize.y/10;
50
51 //マッピングモードをセット
52 SetMapMode(memdc,MM_LOMETRIC);
53
54 POINT px_pos;
55 px_pos.x=iPageWidth;
56 px_pos.y=iPageHeight;
57 LPtoDP(memdc,&px_pos,1);
58 px_pos.y=-px_pos.y;
59
60 int WndNum;
61 WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
62
63 //フォント生成
64 LOGFONT lf;
65 lf=pobj_nv->lfPrint;
66 lf.lfHeight=-MulDiv(pobj_nv->iPrintFontPointSize,GetDeviceCaps(memdc, LOGPIXELSY),960);
67 HFONT hFont;
68 hFont=CreateFontIndirect(&lf);
69
70 //フォントをセット
71 HFONT hOldFont;
72 hOldFont=(HFONT)SelectObject(memdc,hFont);
73
74 //マージン(単位は0.1ミリメートル)
75 RECT rcMargin;
76 rcMargin.left=obj_Page.psd.rtMargin.left/10;
77 rcMargin.top=obj_Page.psd.rtMargin.top/10;
78 rcMargin.right=obj_Page.psd.rtMargin.right/10;
79 rcMargin.bottom=obj_Page.psd.rtMargin.bottom/10;
80
81 //タブ文字数
82 DRAWTEXTPARAMS dtp;
83 dtp.cbSize=sizeof(DRAWTEXTPARAMS);
84 dtp.iLeftMargin=0;
85 dtp.iRightMargin=0;
86 dtp.iTabLength=pobj_nv->TabSize;
87 dtp.uiLengthDrawn=0;
88
89 char *temp;
90 temp=(char *)malloc(65535);
91
92 char *buffer;
93 buffer=MdiInfo[WndNum].pMdiTextEdit->buffer;
94 int i=0,i2=0,iPage=0;
95 for(i=0,i2=0;;i++,i2++){
96 if(buffer[i]=='\0'||buffer[i]=='\r'&&buffer[i+1]=='\n'){
97 temp[i2]=0;
98
99 memset(&rc,0,sizeof(RECT));
100 rc.right=iPageWidth-(rcMargin.left + rcMargin.right);
101 DrawTextEx(memdc,temp,-1,&rc,DT_CALCRECT | DT_WORDBREAK|DT_TABSTOP|DT_EXPANDTABS|DT_NOPREFIX,&dtp);
102
103 if(rc.bottom<=-(iPageHeight-(rcMargin.top+rcMargin.bottom)) || buffer[i]=='\0'){
104
105 if(rc.bottom<-(iPageHeight-(rcMargin.top+rcMargin.bottom))){
106 i--;i2--;
107 while(i>0 && (!(buffer[i]=='\r'&&buffer[i+1]=='\n'))){
108 i--;
109 i2--;
110 }
111 temp[i2]=0;
112 }
113
114 rc.left=rcMargin.left;
115 rc.right=iPageWidth-rcMargin.right;
116 rc.top=-rcMargin.top;
117 rc.bottom=-(iPageHeight-rcMargin.bottom);
118
119 char szTempPage[255],*temp2,temp3[255];
120 lstrcpy(temp3,pobj_nv->szPageStr);
121 temp2=strstr(temp3,"&n");
122 if(temp2){
123 temp2[0]='%';
124 temp2[1]='d';
125
126 sprintf(szTempPage,temp3,iPage+1);
127 }
128
129 if(bPreview&&iPage==iPreviewNowPage || bPreview==0){
130 if(bPreview==0) StartPage(memdc);
131
132 DrawTextEx(memdc,temp,-1,&rc,DT_LEFT | DT_WORDBREAK|DT_TABSTOP|DT_EXPANDTABS|DT_NOPREFIX,&dtp);
133
134 if(pobj_nv->bShowPageNum){
135 //ページ番号
136 rc.top=-(iPageHeight-rcMargin.bottom);
137 rc.bottom=-iPageHeight;
138 DrawText(memdc,szTempPage,-1,&rc,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_NOPREFIX);
139 }
140
141 if(bPreview==0) EndPage(memdc);
142
143 if(lpbLastPage){
144 if(buffer[i]=='\0'){
145 *lpbLastPage=1;
146 }
147 else *lpbLastPage=0;
148 }
149
150 if(bPreview) break;
151 }
152
153 if(buffer[i]=='\0') break;
154
155 iPage++;
156
157 i++;
158 i2=-1;
159 continue;
160 }
161 }
162 temp[i2]=buffer[i];
163 }
164
165 free(temp);
166
167 SelectObject(memdc,hOldFont);
168}
169
170void CreatePreviewDCAndBmp(HDC *lphDC,HBITMAP *lphBmp,POINT *lpPxPos,BOOL *lpbLastPage){
171
172 //印刷ダイアログ
173 PRINTDLG pd;
174 memset(&pd,0,sizeof(PRINTDLG));
175 pd.lStructSize=sizeof(PRINTDLG);
176 pd.hwndOwner=hOwner;
177 pd.Flags=PD_RETURNDC|PD_RETURNDEFAULT;
178 pd.nFromPage=1;
179 pd.nToPage=1;
180 pd.nMinPage=1;
181 pd.nMaxPage=1;
182 pd.nCopies=1;
183 if(PrintDlg(&pd)==0){
184 return;
185 }
186
187 HDC memdc;
188 memdc=CreateCompatibleDC(pd.hDC);
189
190 //ページの幅、高さ(単位は0.1ミリメートル)
191 int iPageWidth,iPageHeight;
192 iPageWidth=obj_Page.psd.ptPaperSize.x/10;
193 iPageHeight=obj_Page.psd.ptPaperSize.y/10;
194
195 //マッピングモードをセット
196 SetMapMode(memdc,MM_LOMETRIC);
197
198 POINT px_pos;
199 px_pos.x=iPageWidth;
200 px_pos.y=iPageHeight;
201 LPtoDP(memdc,&px_pos,1);
202 px_pos.y=-px_pos.y;
203
204 HBITMAP hMemBmp;
205 hMemBmp=CreateCompatibleBitmap(pd.hDC,px_pos.x,px_pos.y);
206 SelectObject(memdc,hMemBmp);
207
208 //背景を初期化
209 HBRUSH hOldBrush;
210 hOldBrush=(HBRUSH)SelectObject(memdc,GetStockObject(WHITE_BRUSH));
211 PatBlt(memdc,0,0,px_pos.x,-px_pos.y,PATCOPY);
212 SelectObject(memdc,hOldBrush);
213
214 //メモリへ描画
215 DrawPage(memdc,1,lpbLastPage);
216
217 DeleteDC(pd.hDC);
218
219
220 *lphDC=memdc;
221 *lphBmp=hMemBmp;
222 *lpPxPos=px_pos;
223}
224
225void DrawPreview(HWND hPreview,HDC hdc,HDC memdc,POINT *lpPxPos){
226 SetMapMode(memdc,MM_TEXT);
227
228 POINT px_pos;
229 px_pos=*lpPxPos;
230
231 SIZE size;
232 RECT rc;
233 GetClientRect(hPreview,&rc);
234 size.cy=(long)((double)rc.bottom*0.9);
235 size.cx=(long)((double)size.cy*((double)obj_Page.psd.ptPaperSize.x/(double)obj_Page.psd.ptPaperSize.y));
236
237 SIZE size_hori;
238 size_hori.cx=(long)((double)rc.right*0.9);
239 size_hori.cy=(long)((double)size_hori.cx*((double)obj_Page.psd.ptPaperSize.y/(double)obj_Page.psd.ptPaperSize.x));
240
241 if(size.cx>size_hori.cx) size=size_hori;
242 size.cx=px_pos.x;
243 size.cy=px_pos.y;
244
245
246 //////////////////////////////////
247 // スクロールバー情報を取得
248 //////////////////////////////////
249
250 //垂直
251 SCROLLINFO si_vert;
252 si_vert.cbSize=sizeof(SCROLLINFO);
253 si_vert.fMask=SIF_PAGE|SIF_RANGE|SIF_POS;
254 GetScrollInfo(hPreview,SB_VERT,&si_vert);
255
256 //水平
257 SCROLLINFO si_horz;
258 si_horz.cbSize=sizeof(SCROLLINFO);
259 si_horz.fMask=SIF_PAGE|SIF_RANGE|SIF_POS;
260 GetScrollInfo(hPreview,SB_HORZ,&si_horz);
261
262
263
264 //表示率
265 double dblZoomRatio;
266 dblZoomRatio=((double)array_Zoom[iZoomNowPage]/(double)100)*0.18;
267
268
269 ///////////////////////////
270 // メモリへ描画
271 ///////////////////////////
272
273 HDC memdc2;
274 memdc2=CreateCompatibleDC(hdc);
275
276 HBITMAP hMemBmp;
277 hMemBmp=CreateCompatibleBitmap(hdc,(int)((double)size.cx*dblZoomRatio)+40, (int)((double)size.cy*dblZoomRatio)+40);
278
279 SelectObject(memdc2,hMemBmp);
280 RECT rc2;
281 rc2.left=0;rc2.top=0;
282 rc2.right=(int)((double)size.cx*dblZoomRatio)+40;
283 rc2.bottom=(int)((double)size.cy*dblZoomRatio)+40;
284 FillRect(memdc2,&rc2,(HBRUSH)GetStockObject(GRAY_BRUSH));
285
286 SetStretchBltMode(memdc2,HALFTONE);
287 StretchBlt(memdc2,
288 20, 20,
289 (int)((double)size.cx*dblZoomRatio), (int)((double)size.cy*dblZoomRatio),
290 memdc,0,0,px_pos.x,px_pos.y,SRCCOPY);
291
292 //画面へ描画
293 BitBlt(hdc,-si_horz.nPos,-si_vert.nPos,rc2.right,rc2.bottom,
294 memdc2,0,0,SRCCOPY);
295
296 DeleteDC(memdc2);
297 DeleteObject(hMemBmp);
298
299
300 //////////////////////////////
301 // スクロールバーを設定
302 //////////////////////////////
303 GetClientRect(hPreview,&rc);
304
305 //垂直
306 si_vert.nMin=0;
307 si_vert.nMax=rc2.bottom;
308 si_vert.nPage=rc.bottom;
309 SetScrollInfo(hPreview,SB_VERT,&si_vert,1);
310
311 //水平
312 si_horz.nMin=0;
313 si_horz.nMax=rc2.right;
314 si_horz.nPage=rc.right;
315 SetScrollInfo(hPreview,SB_HORZ,&si_horz,1);
316}
317#define WM_RESETPREVIEW WM_USER+100
318LRESULT CALLBACK PreviewWndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
319 static HDC memdc;
320 static HBITMAP hMemBmp;
321 static POINT px_pos;
322 SCROLLINFO si;
323 int i;
324 RECT rc;
325 switch(message){
326 case WM_CREATE:
327 memdc=0;
328 hMemBmp=0;
329 SendMessage(hwnd,WM_RESETPREVIEW,0,0);
330 return 0;
331 case WM_PAINT:
332 HDC hdc;
333 PAINTSTRUCT ps;
334 hdc=BeginPaint(hwnd,&ps);
335 DrawPreview(hwnd,hdc,memdc,&px_pos);
336 EndPaint(hwnd,&ps);
337 return 0;
338 case WM_RESETPREVIEW:
339 if(memdc) DeleteDC(memdc);
340 if(hMemBmp) DeleteObject(hMemBmp);
341 BOOL bLastPage;
342 CreatePreviewDCAndBmp(&memdc,&hMemBmp,&px_pos,&bLastPage);
343 InvalidateRect(hwnd,NULL,1);
344
345 if(iPreviewNowPage==0) EnableWindow(GetDlgItem(GetParent(hwnd),IDC_PREV),0);
346 else EnableWindow(GetDlgItem(GetParent(hwnd),IDC_PREV),1);
347 if(bLastPage) EnableWindow(GetDlgItem(GetParent(hwnd),IDC_NEXT),0);
348 else EnableWindow(GetDlgItem(GetParent(hwnd),IDC_NEXT),1);
349
350 if(iZoomNowPage<=0) EnableWindow(GetDlgItem(GetParent(hwnd),IDC_ZOOMIN),0);
351 else EnableWindow(GetDlgItem(GetParent(hwnd),IDC_ZOOMIN),1);
352 if(iZoomNowPage>=ZOOM_NUM-1)
353 EnableWindow(GetDlgItem(GetParent(hwnd),IDC_ZOOMOUT),0);
354 else EnableWindow(GetDlgItem(GetParent(hwnd),IDC_ZOOMOUT),1);
355 return 0;
356 case WM_VSCROLL:
357 case WM_HSCROLL:
358 int fnBar;
359 if(message==WM_VSCROLL) fnBar=SB_VERT;
360 else fnBar=SB_HORZ;
361
362 si.cbSize=sizeof(SCROLLINFO);
363 si.fMask=SIF_POS|SIF_PAGE|SIF_RANGE;
364
365 GetScrollInfo(hwnd,fnBar,&si);
366 if(LOWORD(wParam)==SB_LINEUP) i=-1;
367 else if(LOWORD(wParam)==SB_LINEDOWN) i=1;
368 else if(LOWORD(wParam)==SB_PAGEUP) i=-(signed int)si.nPage;
369 else if(LOWORD(wParam)==SB_PAGEDOWN) i=si.nPage;
370 else if(LOWORD(wParam)==SB_THUMBTRACK) i=HIWORD(wParam)-si.nPos;
371 else i=0;
372 GetClientRect(hwnd,&rc);
373 if(message==WM_VSCROLL)
374 i=max(-si.nPos,min(i,si.nMax-rc.bottom-si.nPos));
375 else
376 i=max(-si.nPos,min(i,si.nMax-rc.right-si.nPos));
377 if(i!=0){
378 si.nPos+=i;
379 SetScrollInfo(hwnd,fnBar,&si,1);
380
381 InvalidateRect(hwnd,NULL,0);
382 }
383 return 0;
384 case WM_DESTROY:
385 DeleteDC(memdc);
386 DeleteObject(hMemBmp);
387 return 0;
388 }
389 return DefWindowProc(hwnd,message,wParam,lParam);
390}
391BOOL CALLBACK DlgPreview(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam){
392 static HWND hPreview;
393 char temporary[255];
394 switch(message){
395 case WM_INITDIALOG:
396 SetPosCenter(GetDesktopWindow(),hwnd);
397
398 hPreview=CreateWindowEx(WS_EX_STATICEDGE,"PreviewWindow",NULL,
399 WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL,
400 0,0,0,0,
401 hwnd,0,hInst,0);
402
403 RECT rc;
404 GetClientRect(hwnd,&rc);
405 SetWindowPos(hwnd,NULL,0,0,rc.right,rc.bottom,SWP_NOMOVE);
406
407 iPreviewNowPage=0;
408
409 SetDlgItemText(hwnd,IDC_FONT,pobj_nv->lfPrint.lfFaceName);
410
411 //ページ番号を表示するかどうか
412 if(pobj_nv->bShowPageNum) SendDlgItemMessage(hwnd,IDC_SHOWPAGENUM,BM_SETCHECK,BST_CHECKED,0);
413 SendMessage(hwnd,WM_COMMAND,IDC_SHOWPAGENUM,0);
414
415 //ページ番号文字列
416 SetDlgItemText(hwnd,IDC_PAGESTR,pobj_nv->szPageStr);
417
418 //拡大・縮小アイコンを表示
419 SendDlgItemMessage(hwnd,IDC_ZOOMOUT,BM_SETIMAGE,IMAGE_ICON ,
420 (LPARAM)LoadImage(hResInst,MAKEINTRESOURCE(IDI_ZOOMOUT),IMAGE_ICON,16,16,LR_SHARED));
421 SendDlgItemMessage(hwnd,IDC_ZOOMIN,BM_SETIMAGE,IMAGE_ICON ,
422 (LPARAM)LoadImage(hResInst,MAKEINTRESOURCE(IDI_ZOOMIN),IMAGE_ICON,16,16,LR_SHARED));
423 iZoomNowPage=ZOOM_INIT;
424
425 sprintf(temporary,"%d%%",array_Zoom[iZoomNowPage]);
426 SetDlgItemText(hwnd,IDC_ZOOMRATIO,temporary);
427
428 SendMessage(hPreview,WM_RESETPREVIEW,0,0);
429
430 break;
431 case WM_COMMAND:
432 switch(LOWORD(wParam)){
433 case IDOK:
434 case IDCANCEL:
435 EndDialog(hwnd,0);
436 return 1;
437
438 case IDC_PREV:
439 iPreviewNowPage--;
440 SendMessage(hPreview,WM_RESETPREVIEW,0,0);
441 return 1;
442 case IDC_NEXT:
443 iPreviewNowPage++;
444 SendMessage(hPreview,WM_RESETPREVIEW,0,0);
445 return 1;
446
447 case IDC_ZOOMOUT:
448 case IDC_ZOOMIN:
449 if(LOWORD(wParam)==IDC_ZOOMOUT)
450 iZoomNowPage++;
451 if(LOWORD(wParam)==IDC_ZOOMIN)
452 iZoomNowPage--;
453 SendMessage(hPreview,WM_RESETPREVIEW,0,0);
454
455 sprintf(temporary,"%d%%",array_Zoom[iZoomNowPage]);
456 SetDlgItemText(hwnd,IDC_ZOOMRATIO,temporary);
457 return 1;
458
459 case IDC_PRINT:
460 Printout();
461 return 1;
462
463 case IDC_PAGESET:
464 if(obj_Page.SetupDlg()){
465 //再描画
466 SendMessage(hPreview,WM_RESETPREVIEW,0,0);
467 }
468 return 1;
469
470 case IDC_FONT:
471 int iPointSize;
472 LOGFONT temp_lf;
473 temp_lf=pobj_nv->lfPrint;
474 if(!SetFontDialog(hwnd,&temp_lf,0,&iPointSize)) return 1;
475 pobj_nv->lfPrint=temp_lf;
476 pobj_nv->iPrintFontPointSize=iPointSize;
477
478 SetDlgItemText(hwnd,IDC_FONT,pobj_nv->lfPrint.lfFaceName);
479
480 //再描画
481 SendMessage(hPreview,WM_RESETPREVIEW,0,0);
482 return 1;
483
484 case IDC_SHOWPAGENUM:
485 //ページ番号を表示するかどうか
486 if(SendDlgItemMessage(hwnd,IDC_SHOWPAGENUM,BM_GETCHECK,0,0)){
487 EnableWindow(GetDlgItem(hwnd,IDC_PAGESTR),1);
488 pobj_nv->bShowPageNum=1;
489 }
490 else{
491 EnableWindow(GetDlgItem(hwnd,IDC_PAGESTR),0);
492 pobj_nv->bShowPageNum=0;
493 }
494 SendMessage(hPreview,WM_RESETPREVIEW,0,0);
495 return 1;
496
497 case IDC_PAGESTR:
498 //ページ番号文字列
499 GetDlgItemText(hwnd,IDC_PAGESTR,pobj_nv->szPageStr,255);
500 return 1;
501 }
502 return 0;
503
504 case WM_SIZE:
505 MoveWindow(hPreview,
506 140,40,
507 LOWORD(lParam)-155,
508 HIWORD(lParam)-55,0);
509 InvalidateRect(hwnd,NULL,0);
510 return 0;
511
512 case WM_DESTROY:
513 DestroyWindow(hPreview);
514 return 0;
515 }
516 return 0;
517}
518
519void Preview(void){
520 DialogBox(hResInst,MAKEINTRESOURCE(IDD_PREVIEW),hOwner,DlgPreview);
521}
522
523
524void Printout(void){
525 int WndNum;
526 WndNum=GetWndNum(GetWindow(hClient,GW_CHILD));
527
528 //印刷ダイアログ
529 PRINTDLG pd;
530 memset(&pd,0,sizeof(PRINTDLG));
531 pd.lStructSize=sizeof(PRINTDLG);
532 pd.hwndOwner=hOwner;
533 pd.Flags=PD_ALLPAGES | PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE | PD_NOSELECTION | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE;
534 pd.nFromPage=1;
535 pd.nToPage=1;
536 pd.nMinPage=1;
537 pd.nMaxPage=1;
538 pd.nCopies=1;
539 if(PrintDlg(&pd)==0){
540 return;
541 }
542
543 //印刷ドキュメントをスタート
544 DOCINFO di;
545 memset(&di,0,sizeof(DOCINFO));
546 di.cbSize=sizeof(DOCINFO);
547 di.lpszDocName=MdiInfo[WndNum].title;
548 if(StartDoc(pd.hDC,&di)==0){
549 MessageBox(hOwner,"印刷できません。プリンターの状況を確認してください。",APPLICATION_NAME,MB_OK | MB_ICONEXCLAMATION);
550 return;
551 }
552
553 //紙へ描画
554 DrawPage(pd.hDC,0);
555
556 //印刷ドキュメントを終了
557 EndDoc(pd.hDC);
558
559 DeleteDC(pd.hDC);
560}
Note: See TracBrowser for help on using the repository browser.