Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa_test / FormFiller_Test / ExportPage.cpp
1 // ExportPage.cpp : implementation file\r
2 //\r
3 \r
4 #include "stdafx.h"\r
5 #include "ReaderVC.h"\r
6 #include "ExportPage.h"\r
7 \r
8 #include "MainFrm.h"\r
9 #include "ChildFrm.h"\r
10 #include "ReaderVCView.h"\r
11 \r
12 #ifdef _DEBUG\r
13 #define new DEBUG_NEW\r
14 #undef THIS_FILE\r
15 static char THIS_FILE[] = __FILE__;\r
16 #endif\r
17 \r
18 /////////////////////////////////////////////////////////////////////////////\r
19 // CExportPage dialog\r
20 \r
21 \r
22 CExportPage::CExportPage(CWnd* pParent /*=NULL*/)\r
23         : CDialog(CExportPage::IDD, pParent)\r
24 {\r
25         //{{AFX_DATA_INIT(CExportPage)\r
26         m_nHeight = 0;\r
27         m_nPageHeight = 0;\r
28         m_nRotate = 0;\r
29         m_nWidth = 0;\r
30         m_nPageWidth = 0;\r
31         //}}AFX_DATA_INIT\r
32         m_bitmap = NULL;\r
33 }\r
34 \r
35 \r
36 void CExportPage::DoDataExchange(CDataExchange* pDX)\r
37 {\r
38         CDialog::DoDataExchange(pDX);\r
39         //{{AFX_DATA_MAP(CExportPage)\r
40         DDX_Text(pDX, IDC_EDIT_HEIGHT, m_nHeight);\r
41         DDX_Text(pDX, IDC_EDIT_PAGE_HEIGHT, m_nPageHeight);\r
42         DDX_Text(pDX, IDC_EDIT_ROTATE, m_nRotate);\r
43         DDX_Text(pDX, IDC_EDIT_WIDTH, m_nWidth);\r
44         DDX_Text(pDX, IDC_EDIT_PAGE_WIDTH, m_nPageWidth);\r
45         //}}AFX_DATA_MAP\r
46 }\r
47 \r
48 \r
49 BEGIN_MESSAGE_MAP(CExportPage, CDialog)\r
50         //{{AFX_MSG_MAP(CExportPage)\r
51         ON_BN_CLICKED(IDC_Rander_Page, OnRanderPage)\r
52         ON_BN_CLICKED(IDC_Save, OnSave)\r
53         ON_WM_PAINT()\r
54         //}}AFX_MSG_MAP\r
55 END_MESSAGE_MAP()\r
56 \r
57 /////////////////////////////////////////////////////////////////////////////\r
58 // CExportPage message handlers\r
59 \r
60 void CExportPage::OnRanderPage() \r
61 {\r
62         if (m_pView)\r
63         {\r
64                 UpdateData(TRUE);\r
65                 m_page=m_pView->GetPage();\r
66                 if (!m_page) return;\r
67                 if (m_nRotate==1 || m_nRotate==3)\r
68                 {\r
69                         int temp;\r
70                         temp=m_nHeight;\r
71                         m_nHeight=m_nWidth;\r
72                         m_nWidth=temp;\r
73                 }\r
74                 m_bitmap=FPDFBitmap_Create(m_nWidth,m_nHeight,FPDFBitmap_BGRx);\r
75                 FPDFBitmap_FillRect(m_bitmap,0,0,m_nWidth,m_nHeight,0xff,0xff,0xff,0xff);\r
76                 FPDF_RenderPageBitmap(m_bitmap,m_page,0,0,200,200,m_nRotate,FPDF_ANNOT);\r
77                 Invalidate();\r
78                 if (m_nRotate==1 || m_nRotate==3)\r
79                 {\r
80                         int temp;\r
81                         temp=m_nHeight;\r
82                         m_nHeight=m_nWidth;\r
83                         m_nWidth=temp;\r
84                 }\r
85                 //FPDFBitmap_Destroy(bitmap);\r
86                 UpdateData(FALSE);\r
87         }\r
88         \r
89 }\r
90 \r
91 void CExportPage::InitDialogInfo(CReaderVCView *pView)\r
92 {\r
93         m_pView=pView;\r
94         if (pView)      m_page=pView->GetPage();\r
95         SetDlgInfo();\r
96 }\r
97 \r
98 void CExportPage::SetDlgInfo()\r
99 {\r
100         if (!m_page) return;\r
101         double height,width;\r
102         width=FPDF_GetPageWidth(m_page);\r
103         height=FPDF_GetPageHeight(m_page);\r
104         \r
105         UpdateData(TRUE);       \r
106         m_nPageHeight = (int)height;\r
107         m_nPageWidth = (int)width;\r
108         m_nWidth=(int)width/3;\r
109         m_nHeight=(int)height/3;\r
110         UpdateData(FALSE);\r
111 }\r
112 \r
113 void CExportPage::OnSave() \r
114 {\r
115         CFileDialog SavDlg(FALSE,"","",OFN_FILEMUSTEXIST |OFN_HIDEREADONLY,"bmp(*.bmp)|*.bmp||All Files(*.*)|*.*");\r
116         if (SavDlg.DoModal()==IDOK)\r
117         {\r
118                 CString strFileName = SavDlg.GetPathName();\r
119                 BITMAPFILEHEADER bitmapFileHeader;\r
120                 BITMAPINFOHEADER bitmapInfoHeader;\r
121                 BITMAP strBitmap;\r
122                 int wBitCount = 32;\r
123                 winbmp.GetBitmap(&strBitmap);\r
124                 DWORD dwBmBitsSize = ((strBitmap.bmWidth * wBitCount+31)/32) * 4 * strBitmap.bmHeight;\r
125                 \r
126                 bitmapFileHeader.bfType = 0x4D42;\r
127                 bitmapFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;\r
128                 bitmapFileHeader.bfReserved1 = bitmapFileHeader.bfReserved2 = 0;\r
129                 bitmapFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);\r
130                 \r
131                 bitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);\r
132                 bitmapInfoHeader.biWidth = strBitmap.bmWidth;\r
133                 bitmapInfoHeader.biHeight = strBitmap.bmHeight;\r
134                 bitmapInfoHeader.biPlanes = 1;\r
135                 bitmapInfoHeader.biBitCount = wBitCount;\r
136                 bitmapInfoHeader.biClrImportant = BI_RGB;\r
137                 bitmapInfoHeader.biSizeImage = 0; //strBitmap.bmWidth * strBitmap.bmHeight;\r
138                 bitmapInfoHeader.biXPelsPerMeter = 0;\r
139                 bitmapInfoHeader.biYPelsPerMeter = 0;\r
140                 bitmapInfoHeader.biClrUsed = 0;\r
141                 bitmapInfoHeader.biCompression = 0;\r
142                 \r
143                 char* context = new char[dwBmBitsSize];\r
144                 CWindowDC dc(NULL);\r
145                 GetDIBits(dc.GetSafeHdc(), (HBITMAP)winbmp.m_hObject, 0, bitmapInfoHeader.biHeight, (LPVOID)context,(BITMAPINFO*)&bitmapInfoHeader, DIB_RGB_COLORS);\r
146                 \r
147                 CFile file;\r
148                 file.Open(strFileName, CFile::modeCreate|CFile::modeWrite);\r
149                 file.Write(&bitmapFileHeader, sizeof(BITMAPFILEHEADER));\r
150                 file.Write(&bitmapInfoHeader, sizeof(BITMAPINFOHEADER));\r
151                 file.Write(context, dwBmBitsSize);\r
152                 \r
153                 file.Close();\r
154                 delete context;\r
155         }\r
156 }\r
157 \r
158 void CExportPage::OnPaint() \r
159 {\r
160         CPaintDC dc(this); // device context for painting\r
161         \r
162         // TODO: Add your message handler code here\r
163         if(m_bitmap)\r
164         {\r
165                 //      CDC* pDc=GetDC();\r
166                 int bufsize=FPDFBitmap_GetStride(m_bitmap)*m_nHeight;\r
167                 void* bmpbuf=FPDFBitmap_GetBuffer(m_bitmap);\r
168                 CDC MemDC;\r
169                 CDC   *pDc   =   GetDlgItem(IDC_STATIC_BITMAP)->GetDC();     //ID:   picture \r
170                 CRect   rect;\r
171                 ((CWnd *)GetDlgItem(IDC_STATIC_BITMAP))->GetWindowRect(rect);\r
172                 \r
173                 MemDC.CreateCompatibleDC(pDc);\r
174                 if((HBITMAP)winbmp != NULL)\r
175                         winbmp.DeleteObject();\r
176                 if(HBITMAP(winbmp) == NULL)\r
177                 {\r
178                         winbmp.CreateCompatibleBitmap(pDc,m_nWidth,m_nHeight);\r
179                         winbmp.SetBitmapBits(bufsize,bmpbuf);\r
180                 }\r
181 \r
182                 MemDC.SelectObject(&winbmp); \r
183                 \r
184                 pDc->BitBlt(0 , 0 , rect.Width(), rect.Height(), &MemDC,0,0,SRCCOPY);\r
185                 //pDc->StretchBlt(0,0,rect.right-rect.left,rect.bottom-rect.top,&MemDC,0,0,m_nWidth,m_nHeight,SRCCOPY);   \r
186                 MemDC.DeleteDC();     \r
187         }\r
188         // Do not call CDialog::OnPaint() for painting messages\r
189 }\r