Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / src / SampleNativePaint / CASCADEView3d.cxx
1 // File:        CASCADEView3d.cxx
2 // Created:     Wed Jul 18 11:11:22 2001
3 // Author:      
4 //              <avo@TIREX>
5
6
7 #ifdef WNT
8 # include <windows.h>
9 #endif
10
11 #include <Standard_Stream.hxx>
12
13 #include <jawt_md.h>
14
15 #include <CASCADEView3d.hxx>
16
17 #include <jcas.hxx>
18 #include <V3d_View.hxx>
19 #ifdef WNT
20 # include <WNT_Window.hxx>
21 # include <Graphic3d_WNTGraphicDevice.hxx>
22 #else
23 # include <Xw_Window.hxx>
24 # include <Graphic3d_GraphicDevice.hxx>
25 #endif  // WNT
26
27 #define RETURN_ERROR(msg) {cout << "Error: '"<< msg <<"'" << endl;return;}
28
29 // #include "Aspect_Handle.hxx>
30
31 /*
32  * Class:     CASCADEView3d
33  * Method:    paint
34  * Signature: (Ljava/awt/Graphics;)V
35  */
36 JNIEXPORT void JNICALL Java_CASCADEView3d_paint (JNIEnv *env, jobject theCanvas, jobject theGraphics){
37 //   cout << "Info: -------------------------- Java_CASCADEView3d_paint ----------------------" << endl;
38
39   jclass jViewPort3dClass = env->FindClass("CASCADEView3d");
40   if(!jViewPort3dClass)RETURN_ERROR("cant find canvas class");
41   jfieldID jIsWindowID = env->GetFieldID(jViewPort3dClass, "hasWindow", "Z");
42   if(!jIsWindowID)RETURN_ERROR("cant find the boolean field id");
43   jboolean jIsWindow = env->GetBooleanField(theCanvas, jIsWindowID);
44
45   Handle(V3d_View) theView;
46   Handle(V3d_Viewer) theViewer;
47
48   if(!jIsWindow){
49     // get the window view and viewer
50 //     cout << "Info: First time, init fields." << endl;
51     jfieldID jViewID = env->GetFieldID(jViewPort3dClass, "myView", "LCASCADESamplesJni/V3d_View;");
52     if(!jViewID) RETURN_ERROR("cant find the view field id");
53     jobject jView = env->GetObjectField(theCanvas, jViewID);
54
55     if(!jView) { // viewer and view are not created, create them now.
56       Standard_ExtString aViewerName = TCollection_ExtendedString ("V3d_Viewer in SimpleViewer").ToExtString();
57 #ifdef WNT
58       static Handle(Graphic3d_WNTGraphicDevice) defaultDevice;
59       if (defaultDevice.IsNull()) 
60         defaultDevice = new Graphic3d_WNTGraphicDevice();
61
62       theViewer = new V3d_Viewer(defaultDevice, aViewerName);
63 #else
64       static Handle(Graphic3d_GraphicDevice) defaultDevice;
65     
66       if (defaultDevice.IsNull()) 
67         defaultDevice = new Graphic3d_GraphicDevice("");
68       theViewer = new V3d_Viewer(defaultDevice, aViewerName);
69 #endif //WNT
70       theView = theViewer->CreateView();
71       // store the values into the fields
72     }else {
73       void *ptrView = jcas_GetHandle(env, jView);
74       if (ptrView != NULL) {
75         theView = *((Handle(V3d_View)*)ptrView);
76       }else{
77         cout << "Error: cant get V3d_View from canvas field" << endl;
78       }
79     }
80
81 // start paste
82     if (!theView.IsNull()){
83           // get drawing surface
84           //-----------------------------------------------
85           JAWT awt;
86           JAWT_DrawingSurface* ds;
87           JAWT_DrawingSurfaceInfo* dsi;
88 #ifdef WNT
89           JAWT_Win32DrawingSurfaceInfo* dsi_win;
90 #else
91           JAWT_X11DrawingSurfaceInfo* dsi_x11;
92 #endif //WNT
93           jboolean result;
94           jint lock;
95
96           // Get the AWT
97           awt.version = JAWT_VERSION_1_3;
98           result = JAWT_GetAWT(env, &awt);
99           if (result == JNI_FALSE)
100             {
101               cout << "AWT not found" << endl;
102               jcas_ThrowException(env, "AWT not found\n");
103 //            alock.Release();
104               return;
105             }
106
107           // Get the drawing surface
108           ds = awt.GetDrawingSurface(env, theCanvas);
109           if (ds == NULL)
110             {
111               cout << "NULL drawing surface" << endl;
112               jcas_ThrowException(env, "NULL drawing surface\n");
113               return;
114             }
115
116           // Lock the drawing surface
117           lock = ds->Lock(ds);
118           if ((lock & JAWT_LOCK_ERROR) != 0)
119             {
120               cout << "Error locking surface" << endl;
121               jcas_ThrowException(env, "Error locking surface\n");
122               awt.FreeDrawingSurface(ds);
123               return;
124             }
125
126           // Get the drawing surface info
127           dsi = ds->GetDrawingSurfaceInfo(ds);
128           if (dsi == NULL)
129             {
130               cout << "Error getting surface info" << endl;
131               jcas_ThrowException(env, "Error getting surface info\n");
132               ds->Unlock(ds);
133               awt.FreeDrawingSurface(ds);
134 //            alock.Release();
135               return;
136             }
137
138           // Get the platform-specific drawing info
139           Aspect_Handle theWindow;
140 #ifdef WNT
141           dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
142           theWindow = dsi_win->hwnd;
143 #else
144           dsi_x11 = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
145           theWindow = dsi_x11->drawable;
146 #endif
147
148               if (jIsWindow == JNI_FALSE)
149                 {
150 #ifdef WNT
151                   long wd = GetWindowLong (( HWND )theWindow, GWL_USERDATA);
152                   long wProc = GetWindowLong (( HWND )theWindow, GWL_WNDPROC);
153 #endif
154
155
156 #ifdef WNT
157                   Handle(WNT_Window) w = new WNT_Window(Handle(Graphic3d_WNTGraphicDevice)::DownCast(theView->Viewer()->Device()),theWindow);
158 #else
159                   Handle(Xw_Window) w = new Xw_Window(Handle(Graphic3d_GraphicDevice)::DownCast(theView->Viewer()->Device()),theWindow,Xw_WQ_SAMEQUALITY);
160 #endif
161 //                cout << "Info: Finally, set window:" << endl;
162                   theView->SetWindow(w); 
163 //                cout << "Info: set window done" << endl;
164
165 #ifdef WNT
166                   long wd1 = SetWindowLong ((HWND) theWindow, GWL_USERDATA, wd);
167                   long wProc1 = SetWindowLong ((HWND) theWindow, GWL_WNDPROC, wProc);
168 #endif
169
170                   jIsWindow = JNI_TRUE;
171                 }
172               
173           env->SetBooleanField(theCanvas, jIsWindowID, jIsWindow);
174
175 //        jobject jView = env->GetObjectField(theCanvas, jViewID);
176 //        jobject jNewView = ;
177 //        env->SetObjectField(theCanvas, jViewID, jNewView); // remember V3d_View
178 /******************************
179 fill fields with viewer and viev
180 *******************************/
181
182 //        jclass testClass = env->FindClass("CASCADESamplesJni/V2d_Viewer");
183 //        cout << "Info: V3d fil lthe fields : get V2d_Viewer class : "<<testClass << endl;
184
185           Handle(V3d_View)* theViewPtr = new Handle(V3d_View);
186           *theViewPtr = theView;
187           jobject aJavaView = jcas_CreateObject(env,"CASCADESamplesJni/V3d_View", theViewPtr);
188           env->SetObjectField(theCanvas, jViewID, aJavaView);
189
190           Handle(V3d_Viewer)* theViewerPtr = new Handle(V3d_Viewer);
191           *theViewerPtr = theViewer;
192           jfieldID jViewerID = env->GetFieldID(jViewPort3dClass, "myViewer", "LCASCADESamplesJni/V3d_Viewer;");
193           jobject aJavaViewer = jcas_CreateObject(env,"CASCADESamplesJni/V3d_Viewer", theViewerPtr);
194           env->SetObjectField(theCanvas, jViewerID, aJavaViewer);
195
196           // fill the myView and myViewer fields.
197
198           // Free the AWT
199           //-----------------------------------------------
200
201           // Free the drawing surface info
202           ds->FreeDrawingSurfaceInfo(dsi);
203
204           // Unlock the drawing surface
205           ds->Unlock(ds);
206
207           // Free the drawing surface
208           awt.FreeDrawingSurface(ds);
209         }else{
210           cout << "Error: Cant obtain V3d_View" << endl;
211         }
212 /////////////////////////// end paste
213 //   cout << "Info: Init viewer done" << endl;
214 } // end !hasWindow. 
215
216 // get stored view field
217   jfieldID jViewID = env->GetFieldID(jViewPort3dClass, "myView", "LCASCADESamplesJni/V3d_View;");
218   jobject jView = env->GetObjectField(theCanvas, jViewID);
219   if(!jView){
220     jcas_ThrowException(env, "Error getting viewer while has window ?\n");
221     return;
222   }
223   void *ptrView = jcas_GetHandle(env, jView);
224   if (ptrView != NULL)
225     theView = *((Handle(V3d_View)*)ptrView);
226
227 // paint routine.
228   if(theView.IsNull()){
229     jcas_ThrowException(env, "Error getting V3d_View ?\n");
230     return;
231   }
232
233           JAWT awt;
234           JAWT_DrawingSurface* ds;
235           jboolean result;
236           jint lock;
237
238           // Get the AWT
239           awt.version = JAWT_VERSION_1_3;
240           result = JAWT_GetAWT(env, &awt);
241           if (result == JNI_FALSE)
242             {
243               cout << "AWT not found" << endl;
244               jcas_ThrowException(env, "AWT not found\n");
245 //            alock.Release();
246               return;
247             }
248
249           // Get the drawing surface
250           ds = awt.GetDrawingSurface(env, theCanvas);
251           if (ds == NULL)
252             {
253               cout << "NULL drawing surface" << endl;
254               jcas_ThrowException(env, "NULL drawing surface\n");
255 //            alock.Release();
256               return;
257             }
258
259           // Lock the drawing surface
260           lock = ds->Lock(ds);
261 //        cout << "Result of locking is " << lock << endl;
262           if ((lock & JAWT_LOCK_ERROR) != 0)
263             {
264               cout << "Error locking surface" << endl;
265               jcas_ThrowException(env, "Error locking surface\n");
266               awt.FreeDrawingSurface(ds);
267 //            alock.Release();
268               return;
269             }
270
271           // Redraw V3d_View after locking AWT drawing surface
272 //  cout << " CASCADEView3d.cxx: redraw "<<endl;
273
274           jfieldID jNeedResizeID = env->GetFieldID(jViewPort3dClass, "needResize", "Z");
275           if(!jNeedResizeID){ 
276                   ds->Unlock(ds);
277                   awt.FreeDrawingSurface(ds);
278                   jcas_ThrowException(env, "cant find the needREsize field id"); 
279                   return; 
280           }
281
282           jboolean jNeedResize = env->GetBooleanField(theCanvas, jNeedResizeID);
283           if(jNeedResize == JNI_TRUE){  
284 //     cout << "Info: bef update, need resize." << endl;
285                   theView->MustBeResized();
286                   jNeedResize = JNI_FALSE;
287                   env->SetBooleanField(theCanvas, jNeedResizeID, jNeedResize);
288           }
289           theView->Redraw();
290           ds->Unlock(ds);
291
292           // Free the drawing surface
293           awt.FreeDrawingSurface(ds);
294 }