ecb10e5c5384925d246dd4837251402906873e24
[occt.git] / src / OpenGl / OpenGl_togl_print.cxx
1 /* File         OpenGL_togl_print.c 
2 Created      March 2000
3 Author       THA
4 e-mail t-hartl@muenchen.matra-dtv.fr  */
5
6 #define RIC120302       /* GG Enable to use the application display
7 //                      callback at end of traversal
8 */
9
10 #include <OpenGl_tgl_all.hxx>
11 #include <InterfaceGraphic_Graphic3d.hxx>
12 #include <InterfaceGraphic_Visual3d.hxx>
13 #include <OpenGl_tsm_ws.hxx>
14 #include <OpenGl_tgl.hxx>
15 #include <OpenGl_txgl.hxx>
16 #include <OpenGl_tgl_tox.hxx>
17 #include <OpenGl_tgl_funcs.hxx>
18 #include <OpenGl_tgl_subrvis.hxx>
19 #include <OpenGl_animation.hxx>
20
21 #include <string.h>
22
23 /* SAV - begin */
24 /* MSDN says: point is 1/72 inch. But in our case text height in the 3D view
25 differs from printed one. An experiment showed that delimeter equal to 2*72 gives
26 practically equal text heights. */
27 int defaultDelimeter = 72;
28 int delimeter = 144;
29 int defaultPntSize = 12;
30
31 GLuint printerFontBase = 0;
32
33 /* printer DC needed to avoid passing OS specific type as a procedure parameter */
34 #ifdef WNT
35 HDC     printer;
36 HGDIOBJ oldObj;
37 #endif
38
39 GLuint createFont( char* typeFace, int height, int weight, int italic )
40 {
41 #ifdef WNT
42   /* no unicode support yet*/
43   HFONT  font;
44   GLuint base;
45   DWORD  charSet = ANSI_CHARSET;
46
47   if ( ( base = glGenLists( 96 ) ) == 0 )
48     return 0;
49
50   if ( _stricmp( typeFace, "symbol" ) == 0 )
51     charSet = SYMBOL_CHARSET;
52
53   font = CreateFont( height, 0, 0, 0, weight, italic, FALSE, FALSE,
54     charSet, OUT_TT_PRECIS,
55     CLIP_DEFAULT_PRECIS, DRAFT_QUALITY,
56     DEFAULT_PITCH, typeFace );
57   oldObj = SelectObject( printer, font );
58   wglUseFontBitmaps( printer, 32, 96, base );
59   return base;
60 #endif
61   return 0;
62 }
63
64 void deleteFont( GLuint base )
65 {
66 #ifdef WNT
67   HFONT currentFont;
68
69   if ( base == 0 )
70     /* no font created */
71     return;
72   /* deleting font list id */
73   glDeleteLists( base, 96 );
74   currentFont = (HFONT)SelectObject( printer, oldObj );
75   /* deleting current font structure */
76   DeleteObject( currentFont );
77 #endif
78 }
79
80 void updatePrinterFont( char* type, int height )
81 {
82 #ifdef WNT
83   int delim = delimeter;
84   if ( height <= 0 ) {
85     height = defaultPntSize;
86     delim = defaultDelimeter;
87   }
88   /* deleting old font */
89   deleteFont( printerFontBase );
90   /* creating new one */
91   printerFontBase = createFont( type, -MulDiv( height, GetDeviceCaps( printer, LOGPIXELSY ), delim ),
92     (int)FW_NORMAL, 0 );
93 #endif
94 }
95 /* SAV - end */
96
97
98 void EXPORT
99 call_togl_print
100 (
101  CALL_DEF_VIEW *aview,
102  CALL_DEF_LAYER *anunderlayer,
103  CALL_DEF_LAYER *anoverlayer,
104  const Aspect_Drawable hPrintDC,
105  const int background,
106  const char* filename
107  )
108 {
109
110 #ifdef WNT
111
112   CMN_KEY_DATA data;
113   Tint swap = 1; /* swap buffers ? yes */
114
115
116   BOOL bRet = FALSE;
117   HDC hPrnDC;
118   DOCINFO   di;
119
120   hPrnDC = (HDC) hPrintDC;
121   printer = hPrnDC;
122
123   /* Begin main routine **************************************************************/
124   TsmGetWSAttri (aview->WsId, WSWindow, &data);
125   if (TxglWinset (call_thedisplay, (WINDOW) data.ldata) == TSuccess) 
126   {
127     static PIXELFORMATDESCRIPTOR pfd = {
128       sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
129         1,        // version number
130         PFD_SUPPORT_OPENGL|
131         PFD_DRAW_TO_BITMAP,
132         PFD_TYPE_RGBA,
133         0, 0, 0, 0, 0, 0,   // color bits ignored
134         0,        // no alpha buffer
135         0,        // shift bit ignored
136         0,        // no accumulation buffer
137         0, 0, 0, 0,       // accum bits ignored
138         32,       // 32-bit z-buffer  
139         0,        // no stencil buffer
140         0,        // no auxiliary buffer
141         PFD_MAIN_PLANE,     // main layer
142         0,        // reserved
143         0, 0, 0       // layer masks ignored
144     };
145     HWND w;
146     HDC hDC;    
147     HDC hMemDC;
148     HGLRC hGLRC;
149     RECT rect;
150
151     BITMAPINFO* pBMI;
152     BITMAPINFOHEADER* bmHeader;
153
154     BYTE  biInfo[sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD)];
155     HGDIOBJ hBmOld;
156     HBITMAP hBm;
157     VOID* base;
158
159     int nIndex;
160     int bmiSize;
161
162     float viewRatio;
163     int devWidth;
164     int devHeight;
165     int viewWidth;
166     int viewHeight;
167     int width;
168     int height;
169     float tempWidth;
170     float tempHeight;
171     int offsetx = 0;
172     int offsety = 0;
173
174 #ifdef mydebug
175     devWidth = 640;
176     tempWidth = (float) devWidth;
177     devHeight = 480;
178     tempHeight = (float) devHeight;
179 #else
180     devWidth = GetDeviceCaps(hPrnDC, HORZRES);
181     tempWidth = (float) devWidth;
182     devHeight = GetDeviceCaps(hPrnDC, VERTRES);
183     tempHeight = (float) devHeight;
184 #endif
185     GetClientRect((WINDOW) data.ldata, &rect);
186     viewWidth = rect.right-rect.left;
187     viewHeight = rect.bottom-rect.top;
188
189     viewRatio = (float) viewWidth/(float) viewHeight;
190
191     // Calculate correct width/height ratio
192     if (tempHeight < tempWidth/viewRatio)
193       tempWidth = tempHeight*viewRatio;
194     if (tempWidth < tempHeight*viewRatio)
195       tempHeight = tempWidth/viewRatio;
196
197     width = (int) tempWidth;
198     height = (int) tempHeight;
199
200     // Create virtual window    
201     w = CreateWindow(
202       "Button",              
203       "",      
204       WS_OVERLAPPEDWINDOW |   
205       WS_CLIPCHILDREN |
206       WS_CLIPSIBLINGS,
207       0, 0,             
208       width, height,   
209       NULL,                
210       NULL,         
211       NULL,        
212       NULL);  
213 #ifdef mydebug
214     ShowWindow(w, SW_SHOW);
215 #endif
216
217     hDC = GetDC(w);
218     if (!hDC)
219     {
220       MessageBox(0,"hDC == NULL", "Fehler", MB_OK);
221       return;
222     }
223
224     // Initialize Bitmap Information
225
226     pBMI = (BITMAPINFO *) biInfo;
227     ZeroMemory(pBMI, sizeof(*pBMI));
228     bmiSize = sizeof(*pBMI);
229
230     pBMI = (BITMAPINFO *) calloc(1, bmiSize);
231     bmHeader = &pBMI->bmiHeader;
232
233     bmHeader->biSize = sizeof(*bmHeader);
234     bmHeader->biWidth = width;
235     bmHeader->biHeight = height;
236     bmHeader->biPlanes = 1;                     /* must be 1 */
237     bmHeader->biBitCount = 24;
238     bmHeader->biXPelsPerMeter = 0;
239     bmHeader->biYPelsPerMeter = 0;
240     bmHeader->biClrUsed = 0;                    /* all are used */
241     bmHeader->biClrImportant = 0;               /* all are important */
242     bmHeader->biCompression = BI_RGB;
243     bmHeader->biSizeImage = 0;
244
245     // Create Device Independent Bitmap 
246     hMemDC = CreateCompatibleDC(hPrnDC);
247     hBm = CreateDIBSection(hMemDC, pBMI, DIB_RGB_COLORS, &base, NULL, 0); 
248     hBmOld = SelectObject(hMemDC, hBm);
249
250     // Release Memory   
251     free(pBMI);
252     //    free(bmHeader);
253
254     // further initialization
255 #ifdef mydebug
256     pfd.cColorBits = GetDeviceCaps(hDC, BITSPIXEL);
257     nIndex = ChoosePixelFormat(hDC, &pfd);
258     if (nIndex == 0)
259     {
260       MessageBox(0,"ChoosePixelFormat failed", "Error", MB_OK | MB_ICONSTOP);
261       goto Error;
262     }
263
264
265     if (!SetPixelFormat(hDC, nIndex, &pfd))
266     {
267       MessageBox(0,"SetPixelFormat failed", "Error", MB_OK | MB_ICONSTOP);
268       goto Error;
269     }
270
271     // Create Rendering Context
272     hGLRC = wglCreateContext(hDC);
273
274     if (hGLRC == NULL) 
275     {
276       MessageBox(0,"No Rendering Context", "Error", MB_OK | MB_ICONSTOP);
277       goto Error;
278     }
279     wglMakeCurrent(hDC,hGLRC);
280 #else
281     pfd.cColorBits = GetDeviceCaps(hMemDC, BITSPIXEL);
282     nIndex = ChoosePixelFormat(hMemDC, &pfd);
283     if (nIndex == 0)
284     {
285       MessageBox(0,"ChoosePixelFormat failed", "Error", MB_OK | MB_ICONSTOP);
286       goto Error;
287     }
288
289
290     if (!SetPixelFormat(hMemDC, nIndex, &pfd))
291     {
292       MessageBox(0,"SetPixelFormat failed", "Error", MB_OK | MB_ICONSTOP);
293       goto Error;
294     }
295
296     // Create Rendering Context
297     hGLRC = wglCreateContext(hMemDC);
298
299     if (hGLRC == NULL) 
300     {
301       MessageBox(0,"No Rendering Context", "Error", MB_OK | MB_ICONSTOP);
302       goto Error;
303     }
304     wglMakeCurrent(hMemDC,hGLRC);
305 #endif
306
307     /* creating default font */
308     printerFontBase = 
309       createFont( "", -MulDiv( defaultPntSize, GetDeviceCaps( hPrnDC, LOGPIXELSY ), defaultDelimeter ),
310       (int)FW_NORMAL, 0 );
311
312     // redraw to new Rendering Context
313     call_func_redraw_all_structs_begin (aview->WsId);
314
315     if (background == 0)
316     {
317       glClearColor(1.0, 1.0, 1.0, 1.0);
318       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
319     }
320
321     if (anunderlayer->ptrLayer)
322       call_togl_redraw_layer2d (aview, anunderlayer);
323     call_func_redraw_all_structs_proc (aview->WsId);
324
325     if (anoverlayer->ptrLayer)
326       call_togl_redraw_layer2d (aview, anoverlayer);
327 #ifdef RIC120302
328     call_subr_displayCB(aview,OCC_REDRAW_BITMAP);
329 #endif
330
331     call_func_redraw_all_structs_end (aview->WsId, swap);
332
333     call_togl_redraw_immediat_mode (aview);
334 #ifndef mydebug
335     /* Start printing of DIB ********************************************************/
336     di.cbSize = sizeof(DOCINFO);
337     di.lpszDocName = "Text";
338     di.lpszOutput = filename;
339
340     if (StartDoc(hPrnDC, &di) == SP_ERROR) goto Error;
341     if (StartPage(hPrnDC) <= 0) goto Error;
342
343     // calculate offset for centered printing
344     if (width < devWidth)
345       offsetx = (devWidth - width)/2;
346     if (height < devHeight)
347       offsety = (devHeight - height)/2;
348
349     BitBlt(hPrnDC, offsetx, offsety, width, height, hMemDC, 0, 0, SRCCOPY);
350
351     EndPage(hPrnDC);
352     EndDoc(hPrnDC);
353     /* releasing created font */
354     deleteFont( printerFontBase );
355     printerFontBase = 0;
356
357     /* End of printing section ******************************************************/
358 #else
359     Sleep(5000);
360 #endif
361
362 Error:
363     /* Clean memory *****************************************************************/
364     if(hBm != NULL)
365     {
366       SelectObject(hMemDC, hBmOld);
367       DeleteObject(hBm);
368       DeleteObject(hBmOld);
369     }
370     wglMakeCurrent(NULL, NULL);
371     if (hGLRC != NULL)
372     {
373       wglDeleteContext(hGLRC);
374     }
375     if (hMemDC != NULL)
376     {
377       DeleteDC(hMemDC);
378     }
379
380     if (hDC != NULL)
381     {
382       ReleaseDC(w, hDC);
383     }
384     if (w != NULL)
385     {
386       DestroyWindow(w);
387     }
388
389     /* End of clean memory *****************************************************************/
390   }
391
392   /* End main routine ********************************************************************/
393   return;
394 #endif /*WNT*/
395 }