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