ac97acbd87dd539100f45b85473ad8ce253feb6a
[blender.git] / source / blender / src / drawview.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 //#define NAN_LINEAR_PHYSICS
34
35 #include <math.h>
36 #include <string.h>
37
38 #ifndef WIN32
39 #include <unistd.h>
40 #include <sys/times.h>
41 #else
42 #include <io.h>
43 #endif   
44
45 #include "MEM_guardedalloc.h"
46
47 #include "PIL_time.h"
48
49 #include "BMF_Api.h"
50
51 #include "IMB_imbuf_types.h"
52 #include "IMB_imbuf.h"
53
54 #include "DNA_action_types.h"
55 #include "DNA_armature_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_constraint_types.h"
58 #include "DNA_curve_types.h"
59 #include "DNA_group_types.h"
60 #include "DNA_image_types.h"
61 #include "DNA_lattice_types.h"
62 #include "DNA_mesh_types.h"
63 #include "DNA_meshdata_types.h"
64 #include "DNA_meta_types.h"
65 #include "DNA_object_types.h"
66 #include "DNA_screen_types.h"
67 #include "DNA_scene_types.h"
68 #include "DNA_space_types.h"
69 #include "DNA_texture_types.h"
70 #include "DNA_userdef_types.h"
71 #include "DNA_view3d_types.h"
72 #include "DNA_world_types.h"
73
74 #include "BLI_blenlib.h"
75 #include "BLI_arithb.h"
76 #include "BLI_editVert.h"
77
78 #include "BKE_action.h"
79 #include "BKE_armature.h"
80 #include "BKE_anim.h"
81 #include "BKE_constraint.h"
82 #include "BKE_curve.h"
83 #include "BKE_displist.h"
84 #include "BKE_depsgraph.h"
85 #include "BKE_DerivedMesh.h"
86 #include "BKE_global.h"
87 #include "BKE_lattice.h"
88 #include "BKE_library.h"
89 #include "BKE_image.h"
90 #include "BKE_ipo.h"
91 #include "BKE_key.h"
92 #include "BKE_main.h"
93 #include "BKE_object.h"
94 #include "BKE_texture.h"
95 #include "BKE_utildefines.h"
96
97 #include "BIF_butspace.h"
98 #include "BIF_drawimage.h"
99 #include "BIF_editgroup.h"
100 #include "BIF_editarmature.h"
101 #include "BIF_editmesh.h"
102 #include "BIF_gl.h"
103 #include "BIF_glutil.h"
104 #include "BIF_interface.h"
105 #include "BIF_interface_icons.h"
106 #include "BIF_mywindow.h"
107 #include "BIF_poseobject.h"
108 #include "BIF_previewrender.h"
109 #include "BIF_resources.h"
110 #include "BIF_screen.h"
111 #include "BIF_space.h"
112
113 #include "BDR_drawmesh.h"
114 #include "BDR_drawobject.h"
115 #include "BDR_editobject.h"
116 #include "BDR_vpaint.h"
117
118 #include "BSE_drawview.h"
119 #include "BSE_filesel.h"
120 #include "BSE_headerbuttons.h"
121 #include "BSE_seqaudio.h"
122 #include "BSE_trans_types.h"
123 #include "BSE_time.h"
124 #include "BSE_view.h"
125
126 #include "BPY_extern.h"
127
128 #include "blendef.h"
129 #include "mydevice.h"
130 #include "butspace.h"  // event codes
131
132 #include "BIF_transform.h"
133
134 /* Modules used */
135 #include "radio.h"
136
137 /* locals */
138 void drawname(Object *ob);
139 void star_stuff_init_func(void);
140 void star_stuff_vertex_func(float* i);
141 void star_stuff_term_func(void);
142
143 void star_stuff_init_func(void)
144 {
145         cpack(-1);
146         glPointSize(1.0);
147         glBegin(GL_POINTS);
148 }
149 void star_stuff_vertex_func(float* i)
150 {
151         glVertex3fv(i);
152 }
153 void star_stuff_term_func(void)
154 {
155         glEnd();
156 }
157
158 void setalpha_bgpic(BGpic *bgpic)
159 {
160         int x, y, alph;
161         char *rect;
162         
163         alph= (int)(255.0*(1.0-bgpic->blend));
164         
165         rect= (char *)bgpic->rect;
166         for(y=0; y< bgpic->yim; y++) {
167                 for(x= bgpic->xim; x>0; x--, rect+=4) {
168                         rect[3]= alph;
169                 }
170         }
171 }
172
173
174 void default_gl_light(void)
175 {
176         int a;
177         
178         /* initialize */
179         if(U.light[0].flag==0 && U.light[1].flag==0 && U.light[2].flag==0) {
180                 U.light[0].flag= 1;
181                 U.light[0].vec[0]= -0.3; U.light[0].vec[1]= 0.3; U.light[0].vec[2]= 0.9;
182                 U.light[0].col[0]= 0.8; U.light[0].col[1]= 0.8; U.light[0].col[2]= 0.8;
183                 U.light[0].spec[0]= 0.5; U.light[0].spec[1]= 0.5; U.light[0].spec[2]= 0.5;
184                 U.light[0].spec[3]= 1.0;
185                 
186                 U.light[1].flag= 0;
187                 U.light[1].vec[0]= 0.5; U.light[1].vec[1]= 0.5; U.light[1].vec[2]= 0.1;
188                 U.light[1].col[0]= 0.4; U.light[1].col[1]= 0.4; U.light[1].col[2]= 0.8;
189                 U.light[1].spec[0]= 0.3; U.light[1].spec[1]= 0.3; U.light[1].spec[2]= 0.5;
190                 U.light[1].spec[3]= 1.0;
191         
192                 U.light[2].flag= 0;
193                 U.light[2].vec[0]= 0.3; U.light[2].vec[1]= -0.3; U.light[2].vec[2]= -0.2;
194                 U.light[2].col[0]= 0.8; U.light[2].col[1]= 0.5; U.light[2].col[2]= 0.4;
195                 U.light[2].spec[0]= 0.5; U.light[2].spec[1]= 0.4; U.light[2].spec[2]= 0.3;
196                 U.light[2].spec[3]= 1.0;
197         }
198         
199
200         glLightfv(GL_LIGHT0, GL_POSITION, U.light[0].vec); 
201         glLightfv(GL_LIGHT0, GL_DIFFUSE, U.light[0].col); 
202         glLightfv(GL_LIGHT0, GL_SPECULAR, U.light[0].spec); 
203
204         glLightfv(GL_LIGHT1, GL_POSITION, U.light[1].vec); 
205         glLightfv(GL_LIGHT1, GL_DIFFUSE, U.light[1].col); 
206         glLightfv(GL_LIGHT1, GL_SPECULAR, U.light[1].spec); 
207
208         glLightfv(GL_LIGHT2, GL_POSITION, U.light[2].vec); 
209         glLightfv(GL_LIGHT2, GL_DIFFUSE, U.light[2].col); 
210         glLightfv(GL_LIGHT2, GL_SPECULAR, U.light[2].spec); 
211
212         for(a=0; a<8; a++) {
213                 if(a<3) {
214                         if(U.light[a].flag) glEnable(GL_LIGHT0+a);
215                         else glDisable(GL_LIGHT0+a);
216                         
217                         // clear stuff from other opengl lamp usage
218                         glLightf(GL_LIGHT0+a, GL_SPOT_CUTOFF, 180.0);
219                         glLightf(GL_LIGHT0+a, GL_CONSTANT_ATTENUATION, 1.0);
220                         glLightf(GL_LIGHT0+a, GL_LINEAR_ATTENUATION, 0.0);
221                 }
222                 else glDisable(GL_LIGHT0+a);
223         }
224         
225         glDisable(GL_LIGHTING);
226
227         glDisable(GL_COLOR_MATERIAL);
228 }
229
230 /* also called when render 'ogl' */
231 void init_gl_stuff(void)        
232 {
233         float mat_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
234         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
235         float mat_shininess[] = { 35.0 };
236         int a, x, y;
237         GLubyte pat[32*32];
238         const GLubyte *patc= pat;
239         
240         
241         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
242         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
243         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
244         glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
245
246         default_gl_light();
247         
248         /* no local viewer, looks ugly in ortho mode */
249         /* glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, &one); */
250         
251         glDepthFunc(GL_LEQUAL);
252         /* scaling matrices */
253         glEnable(GL_NORMALIZE);
254
255         glShadeModel(GL_FLAT);
256
257         glDisable(GL_ALPHA_TEST);
258         glDisable(GL_BLEND);
259         glDisable(GL_DEPTH_TEST);
260         glDisable(GL_FOG);
261         glDisable(GL_LIGHTING);
262         glDisable(GL_LOGIC_OP);
263         glDisable(GL_STENCIL_TEST);
264         glDisable(GL_TEXTURE_1D);
265         glDisable(GL_TEXTURE_2D);
266
267         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
268         glPixelTransferi(GL_RED_SCALE, 1);
269         glPixelTransferi(GL_RED_BIAS, 0);
270         glPixelTransferi(GL_GREEN_SCALE, 1);
271         glPixelTransferi(GL_GREEN_BIAS, 0);
272         glPixelTransferi(GL_BLUE_SCALE, 1);
273         glPixelTransferi(GL_BLUE_BIAS, 0);
274         glPixelTransferi(GL_ALPHA_SCALE, 1);
275         glPixelTransferi(GL_ALPHA_BIAS, 0);
276         
277         glPixelTransferi(GL_DEPTH_BIAS, 0);
278         glPixelTransferi(GL_DEPTH_SCALE, 1);
279         glDepthRange(0.0, 1.0);
280         
281         a= 0;
282         for(x=0; x<32; x++) {
283                 for(y=0; y<4; y++) {
284                         if( (x) & 1) pat[a++]= 0x88;
285                         else pat[a++]= 0x22;
286                 }
287         }
288         
289         glPolygonStipple(patc);
290
291
292         init_realtime_GL();     
293 }
294
295 void circf(float x, float y, float rad)
296 {
297         GLUquadricObj *qobj = gluNewQuadric(); 
298         
299         gluQuadricDrawStyle(qobj, GLU_FILL); 
300         
301         glPushMatrix(); 
302         
303         glTranslatef(x,  y, 0.); 
304         
305         gluDisk( qobj, 0.0,  rad, 32, 1); 
306         
307         glPopMatrix(); 
308         
309         gluDeleteQuadric(qobj);
310 }
311
312 void circ(float x, float y, float rad)
313 {
314         GLUquadricObj *qobj = gluNewQuadric(); 
315         
316         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
317         
318         glPushMatrix(); 
319         
320         glTranslatef(x,  y, 0.); 
321         
322         gluDisk( qobj, 0.0,  rad, 32, 1); 
323         
324         glPopMatrix(); 
325         
326         gluDeleteQuadric(qobj);
327 }
328
329 /* **********  ********** */
330
331 static void draw_bgpic(void)
332 {
333         BGpic *bgpic;
334         Image *ima;
335         float vec[4], fac, asp, zoomx, zoomy;
336         float x1, y1, x2, y2, cx, cy;
337         
338         bgpic= G.vd->bgpic;
339         if(bgpic==0) return;
340         
341         if(bgpic->tex) {
342 //              init_render_texture(bgpic->tex);
343                 free_unused_animimages();
344                 ima= bgpic->tex->ima;
345         }
346         else {
347                 ima= bgpic->ima;
348         }
349         
350         if(ima==0) return;
351         if(ima->ok==0) return;
352
353         tag_image_time(ima);
354         
355         /* test for image */
356         if(ima->ibuf==0) {
357         
358                 if(bgpic->rect) MEM_freeN(bgpic->rect);
359                 bgpic->rect= 0;
360                 
361                 if(bgpic->tex) {
362                         ima_ibuf_is_nul(bgpic->tex, bgpic->tex->ima);
363                 }
364                 else {
365                         waitcursor(1);
366                         load_image(ima, IB_rect, G.sce, G.scene->r.cfra);
367                         waitcursor(0);
368                 }
369                 if(ima->ibuf==0) {
370                         ima->ok= 0;
371                         return;
372                 }
373         }
374
375         if(bgpic->rect==0) {
376                 
377                 bgpic->rect= MEM_dupallocN(ima->ibuf->rect);
378                 bgpic->xim= ima->ibuf->x;
379                 bgpic->yim= ima->ibuf->y;
380                 setalpha_bgpic(bgpic);
381         }
382
383         if(G.vd->persp==2) {
384                 rcti vb;
385
386                 calc_viewborder(G.vd, &vb);
387
388                 x1= vb.xmin;
389                 y1= vb.ymin;
390                 x2= vb.xmax;
391                 y2= vb.ymax;
392         }
393         else {
394                 float sco[2];
395
396                 /* calc window coord */
397                 initgrabz(0.0, 0.0, 0.0);
398                 window_to_3d(vec, 1, 0);
399                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
400                 fac= 1.0/fac;
401         
402                 asp= ( (float)ima->ibuf->y)/(float)ima->ibuf->x;
403
404                 vec[0] = vec[1] = vec[2] = 0.0;
405                 view3d_project_float(curarea, vec, sco, G.vd->persmat);
406                 cx = sco[0];
407                 cy = sco[1];
408         
409                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
410                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
411                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
412                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
413         }
414         
415         /* complete clip? */
416         
417         if(x2 < 0 ) return;
418         if(y2 < 0 ) return;
419         if(x1 > curarea->winx ) return;
420         if(y1 > curarea->winy ) return;
421         
422         zoomx= (x2-x1)/ima->ibuf->x;
423         zoomy= (y2-y1)/ima->ibuf->y;
424
425         glEnable(GL_BLEND);
426         if(G.vd->zbuf) glDisable(GL_DEPTH_TEST);
427
428         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
429          
430         glMatrixMode(GL_PROJECTION);
431         glPushMatrix();
432         glMatrixMode(GL_MODELVIEW);
433         glPushMatrix();
434         
435         glaDefine2DArea(&curarea->winrct);
436         glPixelZoom(zoomx, zoomy);
437         glaDrawPixelsSafe(x1, y1, ima->ibuf->x, ima->ibuf->y, ima->ibuf->x, GL_RGBA, GL_UNSIGNED_BYTE, bgpic->rect);
438         glPixelZoom(1.0, 1.0);
439
440         glMatrixMode(GL_PROJECTION);
441         glPopMatrix();
442         glMatrixMode(GL_MODELVIEW);
443         glPopMatrix();
444         
445         glBlendFunc(GL_ONE,  GL_ZERO); 
446         glDisable(GL_BLEND);
447         if(G.vd->zbuf) glEnable(GL_DEPTH_TEST);
448         
449         areawinset(curarea->win);       // restore viewport / scissor
450 }
451
452 static void drawgrid_draw(float wx, float wy, float x, float y, float dx)
453 {
454         float fx, fy;
455         
456         x+= (wx); 
457         y+= (wy);
458         fx= x/dx;
459         fx= x-dx*floor(fx);
460         
461         while(fx< curarea->winx) {
462                 fdrawline(fx,  0.0,  fx,  (float)curarea->winy); 
463                 fx+= dx; 
464         }
465
466         fy= y/dx;
467         fy= y-dx*floor(fy);
468         
469
470         while(fy< curarea->winy) {
471                 fdrawline(0.0,  fy,  (float)curarea->winx,  fy); 
472                 fy+= dx;
473         }
474
475 }
476
477 // not intern, called in editobject for constraint axis too
478 void make_axis_color(char *col, char *col2, char axis)
479 {
480         if(axis=='x') {
481                 col2[0]= col[0]>219?255:col[0]+36;
482                 col2[1]= col[1]<26?0:col[1]-26;
483                 col2[2]= col[2]<26?0:col[2]-26;
484         }
485         else if(axis=='y') {
486                 col2[0]= col[0]<46?0:col[0]-36;
487                 col2[1]= col[1]>189?255:col[1]+66;
488                 col2[2]= col[2]<46?0:col[2]-36; 
489         }
490         else {
491                 col2[0]= col[0]<26?0:col[0]-26; 
492                 col2[1]= col[1]<26?0:col[1]-26; 
493                 col2[2]= col[2]>209?255:col[2]+46;
494         }
495         
496 }
497
498 static void drawgrid(void)
499 {
500         /* extern short bgpicmode; */
501         float wx, wy, x, y, fw, fx, fy, dx;
502         float vec4[4];
503         char col[3], col2[3];
504         
505         vec4[0]=vec4[1]=vec4[2]=0.0; 
506         vec4[3]= 1.0;
507         Mat4MulVec4fl(G.vd->persmat, vec4);
508         fx= vec4[0]; 
509         fy= vec4[1]; 
510         fw= vec4[3];
511
512         wx= (curarea->winx/2.0);        /* because of rounding errors, grid at wrong location */
513         wy= (curarea->winy/2.0);
514
515         x= (wx)*fx/fw;
516         y= (wy)*fy/fw;
517
518         vec4[0]=vec4[1]=G.vd->grid; 
519         vec4[2]= 0.0;
520         vec4[3]= 1.0;
521         Mat4MulVec4fl(G.vd->persmat, vec4);
522         fx= vec4[0]; 
523         fy= vec4[1]; 
524         fw= vec4[3];
525
526         dx= fabs(x-(wx)*fx/fw);
527         if(dx==0) dx= fabs(y-(wy)*fy/fw);
528         
529         glDepthMask(0);         // disable write in zbuffer
530
531         /* check zoom out */
532         BIF_ThemeColor(TH_GRID);
533         persp(PERSP_WIN);
534         
535         if(dx<6.0) {
536                 G.vd->gridview*= 10.0;
537                 dx*= 10.0;
538                 
539                 if(dx<6.0) {    
540                         G.vd->gridview*= 10.0;
541                         dx*= 10.0;
542                         
543                         if(dx<6.0) {
544                                 G.vd->gridview*= 10.0;
545                                 dx*=10;
546                                 if(dx<6.0);
547                                 else {
548                                         BIF_ThemeColor(TH_GRID);
549                                         drawgrid_draw(wx, wy, x, y, dx);
550                                 }
551                         }
552                         else {  // start blending out
553                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
554                                 drawgrid_draw(wx, wy, x, y, dx);
555                         
556                                 BIF_ThemeColor(TH_GRID);
557                                 drawgrid_draw(wx, wy, x, y, 10*dx);
558                         }
559                 }
560                 else {  // start blending out (6 < dx < 60)
561                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
562                         drawgrid_draw(wx, wy, x, y, dx);
563                         
564                         BIF_ThemeColor(TH_GRID);
565                         drawgrid_draw(wx, wy, x, y, 10*dx);
566                 }
567         }
568         else {
569                 if(dx>60.0) {           // start blending in
570                         G.vd->gridview/= 10.0;
571                         dx/= 10.0;                      
572                         if(dx>60.0) {           // start blending in
573                                 G.vd->gridview/= 10.0;
574                                 dx/= 10.0;
575                                 if(dx>60.0) {
576                                         BIF_ThemeColor(TH_GRID);
577                                         drawgrid_draw(wx, wy, x, y, dx);
578                                 }
579                                 else {
580                                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
581                                         drawgrid_draw(wx, wy, x, y, dx);
582                                         BIF_ThemeColor(TH_GRID);
583                                         drawgrid_draw(wx, wy, x, y, dx*10);
584                                 }
585                         }
586                         else {
587                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
588                                 drawgrid_draw(wx, wy, x, y, dx);
589                                 BIF_ThemeColor(TH_GRID);                                
590                                 drawgrid_draw(wx, wy, x, y, dx*10);
591                         }
592                 }
593                 else {
594                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
595                         drawgrid_draw(wx, wy, x, y, dx);
596                         BIF_ThemeColor(TH_GRID);
597                         drawgrid_draw(wx, wy, x, y, dx*10);
598                 }
599         }
600
601         x+= (wx); 
602         y+= (wy);
603         BIF_GetThemeColor3ubv(TH_GRID, col);
604
605         setlinestyle(0);
606         
607         /* center cross */
608         if(G.vd->view==3) make_axis_color(col, col2, 'y');
609         else make_axis_color(col, col2, 'x');
610         glColor3ubv(col2);
611         
612         fdrawline(0.0,  y,  (float)curarea->winx,  y); 
613         
614         if(G.vd->view==7) make_axis_color(col, col2, 'y');
615         else make_axis_color(col, col2, 'z');
616         glColor3ubv(col2);
617
618         fdrawline(x, 0.0, x, (float)curarea->winy); 
619
620         glDepthMask(1);         // enable write in zbuffer
621         persp(PERSP_VIEW);
622 }
623
624
625 static void drawfloor(void)
626 {
627         View3D *vd;
628         float vert[3], grid;
629         int a, gridlines;
630         char col[3], col2[3];
631         short draw_line = 0;
632                 
633         vd= curarea->spacedata.first;
634
635         vert[2]= 0.0;
636
637         if(vd->gridlines<3) return;
638         
639         if(G.vd->zbuf && G.obedit) glDepthMask(0);      // for zbuffer-select
640         
641         gridlines= vd->gridlines/2;
642         grid= gridlines*vd->grid;
643         
644         BIF_GetThemeColor3ubv(TH_GRID, col);
645         
646         /* draw the Y axis and/or grid lines */
647         for(a= -gridlines;a<=gridlines;a++) {
648                 if(a==0) {
649                         /* check for the 'show Y axis' preference */
650                         if (vd->gridflag & V3D_SHOW_Y) { 
651                                 make_axis_color(col, col2, 'y');
652                                 glColor3ubv(col2);
653                                 
654                                 draw_line = 1;
655                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
656                                 BIF_ThemeColorShade(TH_GRID, -10);
657                         } else {
658                                 draw_line = 0;
659                         }
660                 } else {
661                         /* check for the 'show grid floor' preference */
662                         if (vd->gridflag & V3D_SHOW_FLOOR) {
663                                 if( (a % 10)==0) {
664                                         BIF_ThemeColorShade(TH_GRID, -10);
665                                 }
666                                 else BIF_ThemeColorShade(TH_GRID, 10);
667                                 
668                                 draw_line = 1;
669                         } else {
670                                 draw_line = 0;
671                         }
672                 }
673                 
674                 if (draw_line) {
675                         glBegin(GL_LINE_STRIP);
676                 vert[0]= a*vd->grid;
677                 vert[1]= grid;
678                 glVertex3fv(vert);
679                 vert[1]= -grid;
680                 glVertex3fv(vert);
681                         glEnd();
682                 }
683         }
684         
685         /* draw the X axis and/or grid lines */
686         for(a= -gridlines;a<=gridlines;a++) {
687                 if(a==0) {
688                         /* check for the 'show X axis' preference */
689                         if (vd->gridflag & V3D_SHOW_X) { 
690                                 make_axis_color(col, col2, 'x');
691                                 glColor3ubv(col2);
692                                 
693                                 draw_line = 1;
694                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
695                                 BIF_ThemeColorShade(TH_GRID, -10);
696                         } else {
697                                 draw_line = 0;
698                         }
699                 } else {
700                         /* check for the 'show grid floor' preference */
701                         if (vd->gridflag & V3D_SHOW_FLOOR) {
702                                 if( (a % 10)==0) {
703                                         BIF_ThemeColorShade(TH_GRID, -10);
704                                 }
705                                 else BIF_ThemeColorShade(TH_GRID, 10);
706                                 
707                                 draw_line = 1;
708                         } else {
709                                 draw_line = 0;
710                         }
711                 }
712                 
713                 if (draw_line) {
714                         glBegin(GL_LINE_STRIP);
715                 vert[1]= a*vd->grid;
716                 vert[0]= grid;
717                 glVertex3fv(vert );
718                 vert[0]= -grid;
719                 glVertex3fv(vert);
720                         glEnd();
721                 }
722         }
723         
724         /* draw the Z axis line */      
725         /* check for the 'show Z axis' preference */
726         if (vd->gridflag & V3D_SHOW_Z) {
727                 make_axis_color(col, col2, 'z');
728                 glColor3ubv(col2);
729                 
730                 glBegin(GL_LINE_STRIP);
731                 vert[0]= 0;
732                 vert[1]= 0;
733                 vert[2]= grid;
734                 glVertex3fv(vert );
735                 vert[2]= -grid;
736                 glVertex3fv(vert);
737                 glEnd();
738         }
739
740         if(G.vd->zbuf && G.obedit) glDepthMask(1);      
741
742 }
743
744 static void drawcursor(View3D *v3d)
745 {
746         short mx,my,co[2];
747         int flag;
748         
749         /* we dont want the clipping for cursor */
750         flag= v3d->flag;
751         v3d->flag= 0;
752         project_short( give_cursor(), co);
753         v3d->flag= flag;
754         
755         mx = co[0];
756         my = co[1];
757
758         if(mx!=IS_CLIPPED) {
759                 setlinestyle(0); 
760                 cpack(0xFF);
761                 circ((float)mx, (float)my, 10.0);
762                 setlinestyle(4); 
763                 cpack(0xFFFFFF);
764                 circ((float)mx, (float)my, 10.0);
765                 setlinestyle(0);
766                 cpack(0x0);
767                 
768                 sdrawline(mx-20, my, mx-5, my);
769                 sdrawline(mx+5, my, mx+20, my);
770                 sdrawline(mx, my-20, mx, my-5);
771                 sdrawline(mx, my+5, mx, my+20);
772         }
773 }
774
775 /* ********* custom clipping *********** */
776
777 static void view3d_draw_clipping(View3D *v3d)
778 {
779         BoundBox *bb= v3d->clipbb;
780         
781         BIF_ThemeColorShade(TH_BACK, -8);
782         
783         glBegin(GL_QUADS);
784
785         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
786         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
787         glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
788         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
789         glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
790         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
791         
792         glEnd();
793 }
794
795 void view3d_set_clipping(View3D *v3d)
796 {
797         double plane[4];
798         int a;
799         
800         for(a=0; a<4; a++) {
801                 QUATCOPY(plane, v3d->clip[a]);
802                 glClipPlane(GL_CLIP_PLANE0+a, plane);
803                 glEnable(GL_CLIP_PLANE0+a);
804         }
805 }
806
807 void view3d_clr_clipping(void)
808 {
809         int a;
810         
811         for(a=0; a<4; a++) {
812                 glDisable(GL_CLIP_PLANE0+a);
813         }
814 }
815
816 int view3d_test_clipping(View3D *v3d, float *vec)
817 {
818         /* vec in world coordinates, returns 1 if clipped */
819         float view[3];
820         
821         VECCOPY(view, vec);
822         
823         if(0.0f < v3d->clip[0][3] + INPR(view, v3d->clip[0]))
824                 if(0.0f < v3d->clip[1][3] + INPR(view, v3d->clip[1]))
825                         if(0.0f < v3d->clip[2][3] + INPR(view, v3d->clip[2]))
826                                 if(0.0f < v3d->clip[3][3] + INPR(view, v3d->clip[3]))
827                                         return 0;
828
829         return 1;
830 }
831
832 /* ********* end custom clipping *********** */
833
834 static void view3d_get_viewborder_size(View3D *v3d, float size_r[2])
835 {
836         float winmax= MAX2(v3d->area->winx, v3d->area->winy);
837         float aspect= (float) (G.scene->r.xsch*G.scene->r.xasp)/(G.scene->r.ysch*G.scene->r.yasp);
838
839         if(aspect>1.0) {
840                 size_r[0]= winmax;
841                 size_r[1]= winmax/aspect;
842         } else {
843                 size_r[0]= winmax*aspect;
844                 size_r[1]= winmax;
845         }
846 }
847
848 void calc_viewborder(struct View3D *v3d, rcti *viewborder_r)
849 {
850         float zoomfac, size[2];
851         float dx= 0.0f, dy= 0.0f;
852         
853         view3d_get_viewborder_size(v3d, size);
854
855                 /* magic zoom calculation, no idea what
856              * it signifies, if you find out, tell me! -zr
857                  */
858         /* simple, its magic dude!
859          * well, to be honest, this gives a natural feeling zooming
860          * with multiple keypad presses (ton)
861          */
862         
863         zoomfac= (M_SQRT2 + v3d->camzoom/50.0);
864         zoomfac= (zoomfac*zoomfac)*0.25;
865         
866         size[0]= size[0]*zoomfac;
867         size[1]= size[1]*zoomfac;
868
869                 /* center in window */
870         viewborder_r->xmin= 0.5*v3d->area->winx - 0.5*size[0];
871         viewborder_r->ymin= 0.5*v3d->area->winy - 0.5*size[1];
872         viewborder_r->xmax= viewborder_r->xmin + size[0];
873         viewborder_r->ymax= viewborder_r->ymin + size[1];
874         
875         dx= v3d->area->winx*G.vd->camdx;
876         dy= v3d->area->winy*G.vd->camdy;
877         
878         /* apply offset */
879         viewborder_r->xmin-= dx;
880         viewborder_r->ymin-= dy;
881         viewborder_r->xmax-= dx;
882         viewborder_r->ymax-= dy;
883 }
884
885 void view3d_set_1_to_1_viewborder(View3D *v3d)
886 {
887         float size[2];
888         int im_width= (G.scene->r.size*G.scene->r.xsch)/100;
889
890         view3d_get_viewborder_size(v3d, size);
891
892         v3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
893         v3d->camzoom= CLAMPIS(v3d->camzoom, -30, 300);
894 }
895
896 static void drawviewborder(void)
897 {
898         extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad);          // interface_panel.c
899         float fac, a;
900         float x1, x2, y1, y2;
901         float x3, y3, x4, y4;
902         rcti viewborder;
903         Camera *ca= NULL;
904
905         if(G.vd->camera==NULL)
906                 return;
907         if(G.vd->camera->type==OB_CAMERA)
908                 ca = G.vd->camera->data;
909         
910         calc_viewborder(G.vd, &viewborder);
911         x1= viewborder.xmin;
912         y1= viewborder.ymin;
913         x2= viewborder.xmax;
914         y2= viewborder.ymax;
915
916         /* passepartout, specified in camera edit buttons */
917         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT)) {
918                 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
919                 glEnable(GL_BLEND);
920                 glColor4f(0, 0, 0, ca->passepartalpha);
921                 
922                 if (x1 > 0.0)
923                         glRectf(0.0, (float)curarea->winy, x1, 0.0);
924                 if (x2 < (float)curarea->winx)
925                         glRectf(x2, (float)curarea->winy, (float)curarea->winx, 0.0);
926                 if (y2 < (float)curarea->winy)
927                         glRectf(x1, (float)curarea->winy, x2, y2);
928                 if (y2 > 0.0) 
929                         glRectf(x1, y1, x2, 0.0);
930
931                 glDisable(GL_BLEND);
932         }
933         
934         /* edge */
935         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
936         
937         setlinestyle(0);
938         BIF_ThemeColor(TH_BACK);
939         glRectf(x1, y1, x2, y2);
940         
941         setlinestyle(3);
942         BIF_ThemeColor(TH_WIRE);
943         glRectf(x1, y1, x2, y2);
944                 
945         /* camera name */
946         if (ca && (ca->flag & CAM_SHOWNAME)) {
947                 glRasterPos2f(x1, y1-15);
948                 
949                 BMF_DrawString(G.font, G.vd->camera->id.name+2);
950         }
951
952
953         /* border */
954         if(G.scene->r.mode & R_BORDER) {
955                 
956                 cpack(0);
957                 x3= x1+ G.scene->r.border.xmin*(x2-x1);
958                 y3= y1+ G.scene->r.border.ymin*(y2-y1);
959                 x4= x1+ G.scene->r.border.xmax*(x2-x1);
960                 y4= y1+ G.scene->r.border.ymax*(y2-y1);
961                 
962                 cpack(0x4040FF);
963                 glRectf(x3,  y3,  x4,  y4); 
964         }
965
966         /* safety border */
967         if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
968                 fac= 0.1;
969                 
970                 a= fac*(x2-x1);
971                 x1+= a; 
972                 x2-= a;
973         
974                 a= fac*(y2-y1);
975                 y1+= a;
976                 y2-= a;
977         
978                 BIF_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
979                 
980                 uiSetRoundBox(15);
981                 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
982         }
983         
984         setlinestyle(0);
985         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
986
987 }
988
989
990 void backdrawview3d(int test)
991 {
992         struct Base *base;
993
994         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT));
995         else if(G.obedit && G.vd->drawtype>OB_WIRE && (G.vd->flag & V3D_ZBUF_SELECT));
996         else {
997                 G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
998                 return;
999         }
1000
1001         if( !(G.vd->flag & V3D_NEEDBACKBUFDRAW) ) return;
1002
1003         if(test) {
1004                 if(qtest()) {
1005                         addafterqueue(curarea->win, BACKBUFDRAW, 1);
1006                         return;
1007                 }
1008         }
1009         persp(PERSP_VIEW);
1010
1011 #ifdef __APPLE__
1012         glDrawBuffer(GL_AUX0);
1013 #endif  
1014         if(G.vd->drawtype > OB_WIRE) G.vd->zbuf= TRUE;
1015         curarea->win_swap &= ~WIN_BACK_OK;
1016         
1017         glDisable(GL_DITHER);
1018
1019         glClearColor(0.0, 0.0, 0.0, 0.0); 
1020         if(G.vd->zbuf) {
1021                 glEnable(GL_DEPTH_TEST);
1022                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1023         }
1024         else {
1025                 glClear(GL_COLOR_BUFFER_BIT);
1026                 glDisable(GL_DEPTH_TEST);
1027         }
1028         
1029         if(G.vd->flag & V3D_CLIPPING)
1030                 view3d_set_clipping(G.vd);
1031         
1032         G.f |= G_BACKBUFSEL;
1033         
1034         base= (G.scene->basact);
1035         if(base && (base->lay & G.vd->lay)) {
1036                 draw_object_backbufsel(base->object);
1037         }
1038
1039         G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
1040
1041         G.f &= ~G_BACKBUFSEL;
1042         G.vd->zbuf= FALSE; 
1043         glDisable(GL_DEPTH_TEST);
1044         glEnable(GL_DITHER);
1045
1046 #ifdef __APPLE__
1047         glDrawBuffer(GL_BACK); /* we were in aux buffers */
1048 #endif
1049
1050         if(G.vd->flag & V3D_CLIPPING)
1051                 view3d_clr_clipping();
1052         
1053         /* it is important to end a view in a transform compatible with buttons */
1054         persp(PERSP_WIN);  // set ortho
1055         bwin_scalematrix(curarea->win, G.vd->blockscale, G.vd->blockscale, G.vd->blockscale);
1056
1057 }
1058
1059 void check_backbuf(void)
1060 {
1061         if(G.vd->flag & V3D_NEEDBACKBUFDRAW)
1062                 backdrawview3d(0);
1063 }
1064
1065 /* samples a single pixel (copied from vpaint) */
1066 unsigned int sample_backbuf(int x, int y)
1067 {
1068         unsigned int col;
1069         
1070         if(x>=curarea->winx || y>=curarea->winy) return 0;
1071         x+= curarea->winrct.xmin;
1072         y+= curarea->winrct.ymin;
1073         
1074         check_backbuf(); // actually not needed for apple
1075
1076 #ifdef __APPLE__
1077         glReadBuffer(GL_AUX0);
1078 #endif
1079         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
1080         glReadBuffer(GL_BACK);  
1081         
1082         if(G.order==B_ENDIAN) SWITCH_INT(col);
1083         
1084         return framebuffer_to_index(col);
1085 }
1086
1087 /* reads full rect, converts indices */
1088 ImBuf *read_backbuf(short xmin, short ymin, short xmax, short ymax)
1089 {
1090         unsigned int *dr, *rd;
1091         struct ImBuf *ibuf, *ibuf1;
1092         int a;
1093         short xminc, yminc, xmaxc, ymaxc, xs, ys;
1094         
1095         /* clip */
1096         if(xmin<0) xminc= 0; else xminc= xmin;
1097         if(xmax>=curarea->winx) xmaxc= curarea->winx-1; else xmaxc= xmax;
1098         if(xminc > xmaxc) return NULL;
1099
1100         if(ymin<0) yminc= 0; else yminc= ymin;
1101         if(ymax>=curarea->winy) ymaxc= curarea->winy-1; else ymaxc= ymax;
1102         if(yminc > ymaxc) return NULL;
1103         
1104         ibuf= IMB_allocImBuf((xmaxc-xminc+1),(ymaxc-yminc+1),32,IB_rect,0);
1105
1106         check_backbuf(); // actually not needed for apple
1107         
1108 #ifdef __APPLE__
1109         glReadBuffer(GL_AUX0);
1110 #endif
1111         glReadPixels(curarea->winrct.xmin+xminc, curarea->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1112         glReadBuffer(GL_BACK);  
1113
1114         if(G.order==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1115
1116         a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
1117         dr= ibuf->rect;
1118         while(a--) {
1119                 if(*dr) *dr= framebuffer_to_index(*dr);
1120                 dr++;
1121         }
1122         
1123         /* put clipped result back, if needed */
1124         if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax) return ibuf;
1125         ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect,0);
1126         rd= ibuf->rect;
1127         dr= ibuf1->rect;
1128                 
1129         for(ys= ymin; ys<=ymax; ys++) {
1130                 for(xs= xmin; xs<=xmax; xs++, dr++) {
1131                         if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
1132                                 *dr= *rd;
1133                                 rd++;
1134                         }
1135                 }
1136         }
1137         IMB_freeImBuf(ibuf);
1138         return ibuf1;
1139 }
1140
1141 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1142 unsigned int sample_backbuf_rect(short mval[2], int size, unsigned int min, unsigned int max, short *dist)
1143 {
1144         struct ImBuf *buf;
1145         unsigned int *bufmin, *bufmax, *tbuf;
1146         int minx, miny;
1147         int a, b, rc, nr, amount, dirvec[4][2];
1148         short distance=0;
1149         unsigned int index = 0;
1150         
1151         amount= (size-1)/2;
1152
1153         minx = mval[0]-(amount+1);
1154         miny = mval[1]-(amount+1);
1155         buf = read_backbuf(minx, miny, minx+size-1, miny+size-1);
1156         if (!buf) return 0;
1157
1158         rc= 0;
1159         
1160         dirvec[0][0]= 1; dirvec[0][1]= 0;
1161         dirvec[1][0]= 0; dirvec[1][1]= -size;
1162         dirvec[2][0]= -1; dirvec[2][1]= 0;
1163         dirvec[3][0]= 0; dirvec[3][1]= size;
1164         
1165         bufmin = buf->rect;
1166         tbuf = buf->rect;
1167         bufmax = buf->rect + size*size;
1168         tbuf+= amount*size+ amount;
1169         
1170         for(nr=1; nr<=size; nr++) {
1171                 
1172                 for(a=0; a<2; a++) {
1173                         for(b=0; b<nr; b++, distance++) {
1174                                 if (*tbuf && *tbuf>=min && *tbuf<max) {
1175                                         *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong - zr
1176                                         index = *tbuf - min+1; // messy yah, but indices start at 1
1177                                         goto exit;
1178                                 }
1179                                 
1180                                 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
1181                                 
1182                                 if(tbuf<bufmin || tbuf>=bufmax) {
1183                                         goto exit;
1184                                 }
1185                         }
1186                         rc++;
1187                         rc &= 3;
1188                 }
1189         }
1190
1191 exit:
1192         IMB_freeImBuf(buf);
1193         return index;
1194 }
1195
1196 void drawname(Object *ob)
1197 {
1198         cpack(0x404040);
1199         glRasterPos3f(0.0,  0.0,  0.0);
1200         
1201         BMF_DrawString(G.font, " ");
1202         BMF_DrawString(G.font, ob->id.name+2);
1203 }
1204
1205
1206 static void draw_selected_name(Object *ob)
1207 {
1208         char info[128];
1209
1210         if(ob->type==OB_ARMATURE) {
1211                 bArmature *arm= ob->data;
1212                 char *name= NULL;
1213                 
1214                 if(ob==G.obedit) {
1215                         EditBone *ebo;
1216                         for (ebo=G.edbo.first; ebo; ebo=ebo->next){
1217                                 if ((ebo->flag & BONE_ACTIVE) && (ebo->layer & arm->layer)) {
1218                                         name= ebo->name;
1219                                         break;
1220                                 }
1221                         }
1222                 }
1223                 else if(ob->pose && (ob->flag & OB_POSEMODE)) {
1224                         bPoseChannel *pchan;
1225                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1226                                 if((pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer)) {
1227                                         name= pchan->name;
1228                                         break;
1229                                 }
1230                         }
1231                 }
1232                 if(name)
1233                         sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
1234                 else
1235                         sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1236         }
1237         else sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1238
1239         BIF_ThemeColor(TH_TEXT_HI);
1240         glRasterPos2i(30,  10);
1241         BMF_DrawString(G.fonts, info);
1242 }
1243
1244
1245 static void draw_view_icon(void)
1246 {
1247         BIFIconID icon;
1248         
1249         if(G.vd->view==7) icon= ICON_AXIS_TOP;
1250         else if(G.vd->view==1) icon= ICON_AXIS_FRONT;
1251         else if(G.vd->view==3) icon= ICON_AXIS_SIDE;
1252         else return ;
1253
1254         glEnable(GL_BLEND);
1255         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
1256         
1257         BIF_icon_draw(5.0, 5.0, icon);
1258         
1259         glBlendFunc(GL_ONE,  GL_ZERO); 
1260         glDisable(GL_BLEND);
1261 }
1262
1263 /* ******************* view3d space & buttons ************** */
1264
1265 static void view3d_change_bgpic_ima(View3D *v3d, Image *newima) {
1266         if (v3d->bgpic && v3d->bgpic->ima!=newima) {
1267                 if (newima)
1268                         id_us_plus((ID*) newima);
1269                 if (v3d->bgpic->ima)
1270                         v3d->bgpic->ima->id.us--;
1271                 v3d->bgpic->ima= newima;
1272
1273                 if(v3d->bgpic->rect) MEM_freeN(v3d->bgpic->rect);
1274                 v3d->bgpic->rect= NULL;
1275                 
1276                 allqueue(REDRAWVIEW3D, 0);
1277         }
1278 }
1279 static void view3d_change_bgpic_tex(View3D *v3d, Tex *newtex) {
1280         if (v3d->bgpic && v3d->bgpic->tex!=newtex) {
1281                 if (newtex)
1282                         id_us_plus((ID*) newtex);
1283                 if (v3d->bgpic->tex)
1284                         v3d->bgpic->tex->id.us--;
1285                 v3d->bgpic->tex= newtex;
1286                 
1287                 allqueue(REDRAWVIEW3D, 0);
1288         }
1289 }
1290
1291 static void load_bgpic_image(char *name)
1292 {
1293         Image *ima;
1294         View3D *vd;
1295         
1296         areawinset(curarea->win);
1297         vd= G.vd;
1298         if(vd==0 || vd->bgpic==0) return;
1299         
1300         ima= add_image(name);
1301         if(ima) {
1302                 if(vd->bgpic->ima) {
1303                         vd->bgpic->ima->id.us--;
1304                 }
1305                 vd->bgpic->ima= ima;
1306                 
1307                 free_image_buffers(ima);        /* force read again */
1308                 ima->ok= 1;
1309         }
1310         allqueue(REDRAWVIEW3D, 0);
1311         
1312 }
1313
1314 /* this one assumes there is only one global active object in blender...  (for object panel) */
1315 static float ob_eul[4]; // used for quat too....
1316 /* this one assumes there is only one editmode in blender...  (for object panel) */
1317 static float ve_median[5];
1318
1319 /* is used for both read and write... */
1320 static void v3d_editvertex_buts(uiBlock *block, Object *ob, float lim)
1321 {
1322         static int curdef=0;
1323         static float *defweightp= NULL;
1324         EditMesh *em = G.editMesh;
1325         EditVert *eve, *evedef=NULL;
1326         EditEdge *eed;
1327         float median[5];
1328         int tot, totw, totweight, totedge;
1329         char defstr[320];
1330         
1331         median[0]= median[1]= median[2]= median[3]= median[4]= 0.0;
1332         tot= totw= totweight= totedge= 0;
1333         defstr[0]= 0;
1334
1335         if(ob->type==OB_MESH) {         
1336                 eve= em->verts.first;
1337                 while(eve) {
1338                         if(eve->f & 1) {
1339                                 evedef= eve;
1340                                 tot++;
1341                                 VecAddf(median, median, eve->co);
1342                         }
1343                         eve= eve->next;
1344                 }
1345                 eed= em->edges.first;
1346                 while(eed) {
1347                         if((eed->f & SELECT)) {
1348                                 totedge++;
1349                                 median[3]+= eed->crease;
1350                         }
1351                         eed= eed->next;
1352                 }
1353                 /* check for defgroups */
1354                 if(tot==1 && evedef->totweight) {
1355                         bDeformGroup *dg;
1356                         int i, max=1, init=1;
1357                         char str[32];
1358                         
1359                         for (i=0; i<evedef->totweight; i++){
1360                                 dg = BLI_findlink (&ob->defbase, evedef->dw[i].def_nr);
1361                                 if(dg) {
1362                                         max+= sprintf(str, "%s %%x%d|", dg->name, evedef->dw[i].def_nr); 
1363                                         if(max<320) strcat(defstr, str);
1364                                 }
1365                                 else printf("oh no!\n");
1366                                 if(curdef==evedef->dw[i].def_nr) {
1367                                         init= 0;
1368                                         defweightp= &evedef->dw[i].weight;
1369                                 }
1370                         }
1371                         
1372                         if(init) {      // needs new initialized 
1373                                 curdef= evedef->dw[0].def_nr;
1374                                 defweightp= &evedef->dw[0].weight;
1375                         }
1376                 }
1377         }
1378         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1379                 extern ListBase editNurb; /* editcurve.c */
1380                 Nurb *nu;
1381                 BPoint *bp;
1382                 BezTriple *bezt;
1383                 int a;
1384                 
1385                 nu= editNurb.first;
1386                 while(nu) {
1387                         if((nu->type & 7)==1) {
1388                                 bezt= nu->bezt;
1389                                 a= nu->pntsu;
1390                                 while(a--) {
1391                                         if(bezt->f2 & 1) {
1392                                                 VecAddf(median, median, bezt->vec[1]);
1393                                                 tot++;
1394                                                 median[4]+= bezt->weight;
1395                                                 totweight++;
1396                                         }
1397                                         else {
1398                                                 if(bezt->f1 & 1) {
1399                                                         VecAddf(median, median, bezt->vec[0]);
1400                                                         tot++;
1401                                                 }
1402                                                 if(bezt->f3 & 1) {
1403                                                         VecAddf(median, median, bezt->vec[2]);
1404                                                         tot++;
1405                                                 }
1406                                         }
1407                                         bezt++;
1408                                 }
1409                         }
1410                         else {
1411                                 bp= nu->bp;
1412                                 a= nu->pntsu*nu->pntsv;
1413                                 while(a--) {
1414                                         if(bp->f1 & 1) {
1415                                                 VecAddf(median, median, bp->vec);
1416                                                 median[3]+= bp->vec[3];
1417                                                 totw++;
1418                                                 tot++;
1419                                                 median[4]+= bp->weight;
1420                                                 totweight++;
1421                                         }
1422                                         bp++;
1423                                 }
1424                         }
1425                         nu= nu->next;
1426                 }
1427         }
1428         else if(ob->type==OB_LATTICE) {
1429                 BPoint *bp;
1430                 int a;
1431                 
1432                 a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1433                 bp= editLatt->def;
1434                 while(a--) {
1435                         if(bp->f1 & SELECT) {
1436                                 VecAddf(median, median, bp->vec);
1437                                 tot++;
1438                                 median[4]+= bp->weight;
1439                                 totweight++;
1440                         }
1441                         bp++;
1442                 }
1443         }
1444         
1445         if(tot==0) return;
1446
1447         median[0] /= (float)tot;
1448         median[1] /= (float)tot;
1449         median[2] /= (float)tot;
1450         if(totedge) median[3] /= (float)totedge;
1451         else if(totw) median[3] /= (float)totw;
1452         if(totweight) median[4] /= (float)totweight;
1453         
1454         if(G.vd->flag & V3D_GLOBAL_STATS)
1455                 Mat4MulVecfl(ob->obmat, median);
1456         
1457         if(block) {     // buttons
1458         
1459                 uiBlockBeginAlign(block);
1460                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Global",              160, 150, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays global values");
1461                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Local",              230, 150, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays local values");
1462                 
1463                 memcpy(ve_median, median, sizeof(ve_median));
1464                 
1465                 uiBlockBeginAlign(block);
1466                 if(tot==1) {
1467                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex X:", 10, 110, 290, 19, &(ve_median[0]), -lim, lim, 10, 3, "");
1468                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Y:", 10, 90, 290, 19, &(ve_median[1]), -lim, lim, 10, 3, "");
1469                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Z:", 10, 70, 290, 19, &(ve_median[2]), -lim, lim, 10, 3, "");
1470                         if(totw==1)
1471                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex W:", 10, 50, 290, 19, &(ve_median[3]), 0.01, 100.0, 10, 3, "");
1472                         uiBlockEndAlign(block);
1473         
1474                         if(defstr[0]) {
1475                                 uiDefBut(block, LABEL, 1, "Vertex Deform Groups",               10, 40, 290, 20, NULL, 0.0, 0.0, 0, 0, "");
1476
1477                                 uiBlockBeginAlign(block);
1478                                 uiDefButF(block, NUM, B_NOP, "Weight:",                 10, 20, 150, 19, defweightp, 0.0f, 1.0f, 10, 3, "Weight value");
1479                                 uiDefButI(block, MENU, REDRAWVIEW3D, defstr,    160, 20, 140, 19, &curdef, 0.0, 0.0, 0, 0, "Current Vertex Group");
1480                                 uiBlockEndAlign(block);
1481                         }
1482                         else if(totweight)
1483                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(ve_median[4]), 0.0, 1.0, 10, 3, "");
1484
1485                 }
1486                 else {
1487                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median X:", 10, 110, 290, 19, &(ve_median[0]), -lim, lim, 10, 3, "");
1488                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Y:", 10, 90, 290, 19, &(ve_median[1]), -lim, lim, 10, 3, "");
1489                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Z:", 10, 70, 290, 19, &(ve_median[2]), -lim, lim, 10, 3, "");
1490                         if(totw==tot)
1491                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median W:", 10, 50, 290, 19, &(ve_median[3]), 0.01, 100.0, 10, 3, "");
1492                         uiBlockEndAlign(block);
1493                         if(totweight)
1494                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
1495                 }
1496                 
1497                 if(totedge==1)
1498                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Crease W:", 10, 30, 290, 19, &(ve_median[3]), 0.0, 1.0, 10, 3, "");
1499                 else if(totedge>1)
1500                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Crease W:",  10, 30, 290, 19, &(ve_median[3]), 0.0, 1.0, 10, 3, "");
1501                 
1502         }
1503         else {  // apply
1504                 
1505                 if(G.vd->flag & V3D_GLOBAL_STATS) {
1506                         Mat4Invert(ob->imat, ob->obmat);
1507                         Mat4MulVecfl(ob->imat, median);
1508                         Mat4MulVecfl(ob->imat, ve_median);
1509                 }
1510                 VecSubf(median, ve_median, median);
1511                 median[3]= ve_median[3]-median[3];
1512                 median[4]= ve_median[4]-median[4];
1513                 
1514                 if(ob->type==OB_MESH) {
1515                         float diffac= 1.0;
1516                         
1517                         eve= em->verts.first;
1518                         while(eve) {
1519                                 if(eve->f & 1) {
1520                                         VecAddf(eve->co, eve->co, median);
1521                                 }
1522                                 eve= eve->next;
1523                         }
1524                         
1525                         /* calculate the differences to squeeze a range smaller when values are close to 1 or 0 */
1526                         /* this way you can edit a median value which is applied on clipped values :) */
1527                         if(totedge>1) {
1528                                 float max= 0.0;
1529                                 for(eed= em->edges.first; eed; eed= eed->next) {
1530                                         if(eed->f & SELECT) {
1531                                                 if(max < ABS(eed->crease-ve_median[3])) max= ABS(eed->crease-ve_median[3]);
1532                                         }
1533                                 }
1534                                 if(max>0.0) {
1535                                         if(ve_median[3]> 0.5) diffac= (1.0-ve_median[3])/max;
1536                                         else diffac= (ve_median[3])/max;
1537                                         if(diffac>1.0) diffac= 1.0;
1538                                 }
1539                         }
1540                         
1541                         for(eed= em->edges.first; eed; eed= eed->next) {
1542                                 if(eed->f & SELECT) {
1543                                         eed->crease+= median[3];
1544                                         eed->crease= ve_median[3] + diffac*(eed->crease-ve_median[3]);
1545                                         
1546                                         CLAMP(eed->crease, 0.0, 1.0);
1547                                 }
1548                         }
1549                         
1550                         recalc_editnormals();
1551                 }
1552                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1553                         extern ListBase editNurb; /* editcurve.c */
1554                         Nurb *nu;
1555                         BPoint *bp;
1556                         BezTriple *bezt;
1557                         int a;
1558                         
1559                         nu= editNurb.first;
1560                         while(nu) {
1561                                 if((nu->type & 7)==1) {
1562                                         bezt= nu->bezt;
1563                                         a= nu->pntsu;
1564                                         while(a--) {
1565                                                 if(bezt->f2 & 1) {
1566                                                         VecAddf(bezt->vec[0], bezt->vec[0], median);
1567                                                         VecAddf(bezt->vec[1], bezt->vec[1], median);
1568                                                         VecAddf(bezt->vec[2], bezt->vec[2], median);
1569                                                         bezt->weight+= median[4];
1570                                                 }
1571                                                 else {
1572                                                         if(bezt->f1 & 1) {
1573                                                                 VecAddf(bezt->vec[0], bezt->vec[0], median);
1574                                                         }
1575                                                         if(bezt->f3 & 1) {
1576                                                                 VecAddf(bezt->vec[2], bezt->vec[2], median);
1577                                                         }
1578                                                 }
1579                                                 bezt++;
1580                                         }
1581                                 }
1582                                 else {
1583                                         bp= nu->bp;
1584                                         a= nu->pntsu*nu->pntsv;
1585                                         while(a--) {
1586                                                 if(bp->f1 & 1) {
1587                                                         VecAddf(bp->vec, bp->vec, median);
1588                                                         bp->vec[3]+= median[3];
1589                                                         bp->weight+= median[4];
1590                                                 }
1591                                                 bp++;
1592                                         }
1593                                 }
1594                                 test2DNurb(nu);
1595                                 testhandlesNurb(nu); /* test for bezier too */
1596
1597                                 nu= nu->next;
1598                         }
1599                 }
1600                 else if(ob->type==OB_LATTICE) {
1601                         BPoint *bp;
1602                         int a;
1603                         
1604                         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1605                         bp= editLatt->def;
1606                         while(a--) {
1607                                 if(bp->f1 & SELECT) {
1608                                         VecAddf(bp->vec, bp->vec, median);
1609                                         bp->weight+= median[4];
1610                                 }
1611                                 bp++;
1612                         }
1613                 }
1614                 
1615                 BIF_undo_push("Transform properties");
1616         }
1617 }
1618
1619 /* assumes armature active */
1620 static void validate_bonebutton_cb(void *bonev, void *namev)
1621 {
1622         Object *ob= OBACT;
1623         
1624         if(ob && ob->type==OB_ARMATURE) {
1625                 Bone *bone= bonev;
1626                 char oldname[32], newname[32];
1627                 
1628                 /* need to be on the stack */
1629                 BLI_strncpy(newname, bone->name, 32);
1630                 BLI_strncpy(oldname, (char *)namev, 32);
1631                 /* restore */
1632                 BLI_strncpy(bone->name, oldname, 32);
1633                 
1634                 armature_bone_rename(ob->data, oldname, newname); // editarmature.c
1635                 allqueue(REDRAWALL, 0);
1636         }
1637 }
1638
1639
1640 static void v3d_posearmature_buts(uiBlock *block, Object *ob, float lim)
1641 {
1642         uiBut *but;
1643         bArmature *arm;
1644         bPoseChannel *pchan;
1645         Bone *bone;
1646
1647         arm = get_armature(OBACT);
1648         if (!arm || !ob->pose) return;
1649
1650         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1651                 bone = pchan->bone;
1652                 if(bone && (bone->flag & BONE_ACTIVE) && (bone->layer & arm->layer))
1653                         break;
1654         }
1655         if (!pchan) return;
1656         
1657         but= uiDefBut(block, TEX, B_DIFF, "Bone:",                              160, 140, 140, 19, bone->name, 1, 31, 0, 0, "");
1658         uiButSetFunc(but, validate_bonebutton_cb, bone, NULL);
1659         
1660         QuatToEul(pchan->quat, ob_eul);
1661         ob_eul[0]*= 180.0/M_PI;
1662         ob_eul[1]*= 180.0/M_PI;
1663         ob_eul[2]*= 180.0/M_PI;
1664         
1665         uiBlockBeginAlign(block);
1666         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, REDRAWVIEW3D, ICON_UNLOCKED,     10,140,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1667         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocX:",        30, 140, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
1668         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, REDRAWVIEW3D, ICON_UNLOCKED,     10,120,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1669         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocY:",        30, 120, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
1670         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,100,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1671         uiDefButF(block, NUM, B_ARMATUREPANEL2, "locZ:",        30, 100, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
1672
1673         uiBlockBeginAlign(block);
1674         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     10,70,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1675         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotX:",        30, 70, 120, 19, ob_eul, -1000.0, 1000.0, 100, 3, "");
1676         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     10,50,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1677         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotY:",        30, 50, 120, 19, ob_eul+1, -1000.0, 1000.0, 100, 3, "");
1678         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,30,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1679         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotZ:",        30, 30, 120, 19, ob_eul+2, -1000.0, 1000.0, 100, 3, "");
1680         
1681         uiBlockBeginAlign(block);
1682         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEX, REDRAWVIEW3D, ICON_UNLOCKED,    160,70,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1683         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeX:",       180, 70, 120, 19, pchan->size, -lim, lim, 10, 3, "");
1684         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEY, REDRAWVIEW3D, ICON_UNLOCKED,    160,50,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1685         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeY:",       180, 50, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
1686         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEZ, REDRAWVIEW3D, ICON_UNLOCKED,    160,30,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1687         uiDefButF(block, NUM, B_ARMATUREPANEL2, "SizeZ:",       180, 30, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
1688         uiBlockEndAlign(block);
1689 }
1690
1691 static void v3d_editarmature_buts(uiBlock *block, Object *ob, float lim)
1692 {
1693         bArmature *arm= G.obedit->data;
1694         EditBone *ebone;
1695         uiBut *but;
1696         
1697         ebone= G.edbo.first;
1698
1699         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1700                 if ((ebone->flag & BONE_ACTIVE) && (ebone->layer & arm->layer))
1701                         break;
1702         }
1703
1704         if (!ebone)
1705                 return;
1706         
1707         but= uiDefBut(block, TEX, B_DIFF, "Bone:",                      160, 150, 140, 19, ebone->name, 1, 31, 0, 0, "");
1708         uiButSetFunc(but, validate_editbonebutton_cb, ebone, NULL);
1709
1710         uiBlockBeginAlign(block);
1711         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootX:",       10, 70, 140, 19, ebone->head, -lim, lim, 10, 3, "");
1712         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootY:",       10, 50, 140, 19, ebone->head+1, -lim, lim, 10, 3, "");
1713         uiDefButF(block, NUM, B_ARMATUREPANEL1, "RootZ:",       10, 30, 140, 19, ebone->head+2, -lim, lim, 10, 3, "");
1714         uiBlockBeginAlign(block);
1715         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipX:",        160, 70, 140, 19, ebone->tail, -lim, lim, 10, 3, "");
1716         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipY:",        160, 50, 140, 19, ebone->tail+1, -lim, lim, 10, 3, "");
1717         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipZ:",        160, 30, 140, 19, ebone->tail+2, -lim, lim, 10, 3, "");
1718         uiBlockEndAlign(block);
1719         ob_eul[0]= 180.0*ebone->roll/M_PI;
1720         uiDefButF(block, NUM, B_ARMATUREPANEL1, "Roll:",        10, 100, 140, 19, ob_eul, -lim, lim, 1000, 3, "");
1721         
1722
1723         uiBlockBeginAlign(block);
1724         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TipRadius:",   10, 150, 140, 19, &ebone->rad_tail, 0, lim, 10, 3, "");
1725         if (ebone->parent && ebone->flag & BONE_CONNECTED )
1726                 uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadRadius:",  10, 130, 140, 19, &ebone->parent->rad_tail, 0, lim, 10, 3, "");
1727         else
1728                 uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadRadius:",  10, 130, 140, 19, &ebone->rad_head, 0, lim, 10, 3, "");
1729         uiBlockEndAlign(block);
1730 }
1731
1732 static void v3d_editmetaball_buts(uiBlock *block, Object *ob, float lim)
1733 {
1734         extern MetaElem *lastelem;
1735
1736         if(lastelem) {
1737                 uiBlockBeginAlign(block);
1738                 uiDefButF(block, NUM, B_RECALCMBALL, "LocX:", 10, 70, 140, 19, &lastelem->x, -lim, lim, 100, 3, "");
1739                 uiDefButF(block, NUM, B_RECALCMBALL, "LocY:", 10, 50, 140, 19, &lastelem->y, -lim, lim, 100, 3, "");
1740                 uiDefButF(block, NUM, B_RECALCMBALL, "LocZ:", 10, 30, 140, 19, &lastelem->z, -lim, lim, 100, 3, "");
1741
1742                 uiBlockBeginAlign(block);
1743                 if(lastelem->type!=MB_BALL)
1744                 uiDefButF(block, NUM, B_RECALCMBALL, "dx:", 160, 70, 140, 19, &lastelem->expx, 0, lim, 100, 3, "");
1745                 if((lastelem->type!=MB_BALL) && (lastelem->type!=MB_TUBE))
1746                 uiDefButF(block, NUM, B_RECALCMBALL, "dy:", 160, 50, 140, 19, &lastelem->expy, 0, lim, 100, 3, "");
1747                 if((lastelem->type==MB_ELIPSOID) || (lastelem->type==MB_CUBE))
1748                 uiDefButF(block, NUM, B_RECALCMBALL, "dz:", 160, 30, 140, 19, &lastelem->expz, 0, lim, 100, 3, "");
1749
1750                 uiBlockEndAlign(block);
1751
1752                 uiDefButF(block, NUM, B_RECALCMBALL, "Stiffness:", 10, 100, 140, 19, &lastelem->s, 0, lim, 100, 3, "");
1753         }
1754 }
1755
1756 void do_viewbuts(unsigned short event)
1757 {
1758         View3D *vd;
1759         Object *ob= OBACT;
1760         char *name;
1761         
1762         vd= G.vd;
1763         if(vd==0) return;
1764
1765         switch(event) {
1766         case B_LOADBGPIC:
1767                 if(vd->bgpic && vd->bgpic->ima) name= vd->bgpic->ima->name;
1768                 else name= G.ima;
1769                 
1770                 if(G.qual==LR_CTRLKEY)
1771                         activate_imageselect(FILE_SPECIAL, "Select Image", name, load_bgpic_image);
1772                 else
1773                         activate_fileselect(FILE_SPECIAL, "Select Image", name, load_bgpic_image);
1774                 break;
1775                 
1776         case B_BLENDBGPIC:
1777                 if(vd->bgpic && vd->bgpic->rect) setalpha_bgpic(vd->bgpic);
1778                 addqueue(curarea->win, REDRAW, 1);
1779                 break;
1780                 
1781         case B_BGPICBROWSE:
1782                 if(vd->bgpic) {
1783                         if (vd->menunr==-2) {
1784                                 activate_databrowse((ID*) vd->bgpic->ima, ID_IM, 0, B_BGPICBROWSE, &vd->menunr, do_viewbuts);
1785                         } else if (vd->menunr>0) {
1786                                 Image *newima= (Image*) BLI_findlink(&G.main->image, vd->menunr-1);
1787
1788                                 if (newima)
1789                                         view3d_change_bgpic_ima(vd, newima);
1790                         }
1791                 }
1792                 break;
1793                 
1794         case B_BGPICCLEAR:
1795                 if (vd->bgpic)
1796                         view3d_change_bgpic_ima(vd, NULL);
1797                 break;
1798                 
1799         case B_BGPICTEX:
1800                 if (vd->bgpic) {
1801                         if (vd->texnr==-2) {
1802                                 activate_databrowse((ID*) vd->bgpic->tex, ID_TE, 0, B_BGPICTEX, &vd->texnr, do_viewbuts);
1803                         } else if (vd->texnr>0) {
1804                                 Tex *newtex= (Tex*) BLI_findlink(&G.main->tex, vd->texnr-1);
1805                                 
1806                                 if (newtex)
1807                                         view3d_change_bgpic_tex(vd, newtex);
1808                         }
1809                 }
1810                 break;
1811                 
1812         case B_BGPICTEXCLEAR:
1813                 if (vd->bgpic)
1814                         view3d_change_bgpic_tex(vd, NULL);
1815                 break;
1816         
1817         case B_OBJECTPANEL:
1818                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1819                 allqueue(REDRAWVIEW3D, 1);
1820                 break;
1821                 
1822         case B_OBJECTPANELROT:
1823                 if(ob) {
1824                         ob->rot[0]= M_PI*ob_eul[0]/180.0;
1825                         ob->rot[1]= M_PI*ob_eul[1]/180.0;
1826                         ob->rot[2]= M_PI*ob_eul[2]/180.0;
1827                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1828                         allqueue(REDRAWVIEW3D, 1);
1829                 }
1830                 break;
1831         
1832         case B_OBJECTPANELMEDIAN:
1833                 if(ob) {
1834                         v3d_editvertex_buts(NULL, ob, 1.0);
1835                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1836                         allqueue(REDRAWVIEW3D, 1);
1837                 }
1838                 break;
1839         case B_OBJECTPANELPARENT:
1840                 if(ob) {
1841                         if( test_parent_loop(ob->parent, ob) ) 
1842                                 ob->parent= NULL;
1843                         else {
1844                                 DAG_scene_sort(G.scene);
1845                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1846                                 allqueue(REDRAWVIEW3D, 1);
1847                                 allqueue(REDRAWBUTSOBJECT, 0);
1848                         }
1849                 }
1850                 break;
1851                 
1852         case B_ARMATUREPANEL1:
1853                 {
1854                         bArmature *arm= G.obedit->data;
1855                         EditBone *ebone, *child;
1856                         
1857                         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1858                                 if ((ebone->flag & BONE_ACTIVE) && (ebone->layer & arm->layer))
1859                                         break;
1860                         }
1861                         if (ebone) {
1862                                 ebone->roll= M_PI*ob_eul[0]/180.0;
1863                                 //      Update our parent
1864                                 if (ebone->parent && ebone->flag & BONE_CONNECTED){
1865                                         VECCOPY (ebone->parent->tail, ebone->head);
1866                                 }
1867                         
1868                                 //      Update our children if necessary
1869                                 for (child = G.edbo.first; child; child=child->next){
1870                                         if (child->parent == ebone && (child->flag & BONE_CONNECTED)){
1871                                                 VECCOPY (child->head, ebone->tail);
1872                                         }
1873                                 }
1874                                 if(arm->flag & ARM_MIRROR_EDIT) {
1875                                         EditBone *eboflip= armature_bone_get_mirrored(ebone);
1876                                         if(eboflip) {
1877                                                 eboflip->roll= -ebone->roll;
1878                                                 eboflip->head[0]= -ebone->head[0];
1879                                                 eboflip->tail[0]= -ebone->tail[0];
1880                                                 
1881                                                 //      Update our parent
1882                                                 if (eboflip->parent && eboflip->flag & BONE_CONNECTED){
1883                                                         VECCOPY (eboflip->parent->tail, eboflip->head);
1884                                                 }
1885                                                 
1886                                                 //      Update our children if necessary
1887                                                 for (child = G.edbo.first; child; child=child->next){
1888                                                         if (child->parent == eboflip && (child->flag & BONE_CONNECTED)){
1889                                                                 VECCOPY (child->head, eboflip->tail);
1890                                                         }
1891                                                 }
1892                                         }
1893                                 }
1894                                 
1895                                 allqueue(REDRAWVIEW3D, 1);
1896                         }
1897                 }
1898                 break;
1899         case B_ARMATUREPANEL3:  // rotate button on channel
1900                 {
1901                         bArmature *arm;
1902                         bPoseChannel *pchan;
1903                         Bone *bone;
1904                         
1905                         arm = get_armature(OBACT);
1906                         if (!arm || !ob->pose) return;
1907                                 
1908                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1909                                 bone = pchan->bone;
1910                                 if(bone && (bone->flag & BONE_ACTIVE) && (bone->layer & arm->layer))
1911                                         break;
1912                         }
1913                         if (!pchan) return;
1914                         
1915                         ob_eul[0]*= M_PI/180.0;
1916                         ob_eul[1]*= M_PI/180.0;
1917                         ob_eul[2]*= M_PI/180.0;
1918                         EulToQuat(ob_eul, pchan->quat);
1919                 }
1920                 /* no break, pass on */
1921         case B_ARMATUREPANEL2:
1922                 {
1923                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1924                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1925                         allqueue(REDRAWVIEW3D, 1);
1926                 }
1927                 break;
1928         }
1929 }
1930
1931
1932 static void view3d_panel_object(short cntrl)    // VIEW3D_HANDLER_OBJECT
1933 {
1934         uiBlock *block;
1935         uiBut *bt;
1936         Object *ob= OBACT;
1937         float lim;
1938         static char hexcol[128];
1939         
1940         if(ob==NULL) return;
1941
1942         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_object", UI_EMBOSS, UI_HELV, curarea->win);
1943         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1944         uiSetPanelHandler(VIEW3D_HANDLER_OBJECT);  // for close and esc
1945         
1946 /* (ton) can't use the rename trick for paint... panel names and settings are stored in the files and
1947    used to find previous locations when re-open. This causes flipping */
1948
1949         if(uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 204)==0) return;
1950         
1951         if(ob->id.lib) uiSetButLock(1, "Can't edit library data");
1952         
1953         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
1954                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
1955         }
1956         else {
1957                 bt= uiDefBut(block, TEX, B_IDNAME, "OB: ",      10,180,140,20, ob->id.name+2, 0.0, 19.0, 0, 0, "");
1958                 uiButSetFunc(bt, test_idbutton_cb, ob->id.name, NULL);
1959
1960                 uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_OBJECTPANELPARENT, "Par:", 160, 180, 140, 20, &ob->parent, "Parent Object"); 
1961         }
1962
1963         lim= 10000.0f*MAX2(1.0, G.vd->grid);
1964
1965         if(ob==G.obedit) {
1966                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(block, ob, lim);
1967                 if(ob->type==OB_MBALL) v3d_editmetaball_buts(block, ob, lim);
1968                 else v3d_editvertex_buts(block, ob, lim);
1969         }
1970         else if(ob->flag & OB_POSEMODE) {
1971                 v3d_posearmature_buts(block, ob, lim);
1972         }
1973         else if(G.f & (G_VERTEXPAINT|G_TEXTUREPAINT)) {
1974                 extern VPaint Gvp;         /* from vpaint */
1975                 static float hsv[3], old[3];    // used as temp mem for picker
1976                 uiBlockPickerButtons(block, &Gvp.r, hsv, old, hexcol, 'f', REDRAWBUTSEDIT);     /* 'f' is for floating panel */
1977         }
1978         else {
1979                 uiBlockBeginAlign(block);
1980                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, REDRAWVIEW3D, ICON_UNLOCKED,     10,140,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1981                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocX:",           30, 140, 120, 19, &(ob->loc[0]), -lim, lim, 100, 3, "");
1982                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, REDRAWVIEW3D, ICON_UNLOCKED,     10,120,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1983                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocY:",           30, 120, 120, 19, &(ob->loc[1]), -lim, lim, 100, 3, "");
1984                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,100,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1985                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocZ:",           30, 100, 120, 19, &(ob->loc[2]), -lim, lim, 100, 3, "");
1986                 
1987                 ob_eul[0]= 180.0*ob->rot[0]/M_PI;
1988                 ob_eul[1]= 180.0*ob->rot[1]/M_PI;
1989                 ob_eul[2]= 180.0*ob->rot[2]/M_PI;
1990                 
1991                 uiBlockBeginAlign(block);
1992                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     10,70,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1993                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotX:",        30, 70, 120, 19, &(ob_eul[0]), -lim, lim, 1000, 3, "");
1994                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     10,50,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1995                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotY:",        30, 50, 120, 19, &(ob_eul[1]), -lim, lim, 1000, 3, "");
1996                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,30,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1997                 uiDefButF(block, NUM, B_OBJECTPANELROT, "RotZ:",        30, 30, 120, 19, &(ob_eul[2]), -lim, lim, 1000, 3, "");
1998                 
1999                 uiBlockBeginAlign(block);
2000                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEX, REDRAWVIEW3D, ICON_UNLOCKED,    160,70,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2001                 uiDefButF(block, NUM, B_OBJECTPANEL, "SizeX:",          180, 70, 120, 19, &(ob->size[0]), -lim, lim, 10, 3, "");
2002                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEY, REDRAWVIEW3D, ICON_UNLOCKED,    160,50,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2003                 uiDefButF(block, NUM, B_OBJECTPANEL, "SizeY:",          180, 50, 120, 19, &(ob->size[1]), -lim, lim, 10, 3, "");
2004                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SIZEZ, REDRAWVIEW3D, ICON_UNLOCKED,    160,30,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2005                 uiDefButF(block, NUM, B_OBJECTPANEL, "SizeZ:",          180, 30, 120, 19, &(ob->size[2]), -lim, lim, 10, 3, "");
2006                 uiBlockEndAlign(block);
2007         }
2008         uiClearButLock();
2009 }
2010
2011 static void view3d_panel_background(short cntrl)        // VIEW3D_HANDLER_BACKGROUND
2012 {
2013         uiBlock *block;
2014         View3D *vd;
2015         ID *id;
2016         char *strp;
2017         
2018         vd= G.vd;
2019
2020         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_background", UI_EMBOSS, UI_HELV, curarea->win);
2021         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2022         uiSetPanelHandler(VIEW3D_HANDLER_BACKGROUND);  // for close and esc
2023         if(uiNewPanel(curarea, block, "Background Image", "View3d", 340, 10, 318, 204)==0) return;
2024
2025         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2026                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2027         }
2028         
2029         if(vd->flag & V3D_DISPBGPIC) {
2030                 if(vd->bgpic==0) {
2031                         vd->bgpic= MEM_callocN(sizeof(BGpic), "bgpic");
2032                         vd->bgpic->size= 5.0;
2033                         vd->bgpic->blend= 0.5;
2034                 }
2035         }
2036         
2037         uiDefButBitS(block, TOG, V3D_DISPBGPIC, REDRAWVIEW3D, "Use Background Image", 0, 162, 200, 20, &vd->flag, 0, 0, 0, 0, "Display an image in the background of the 3D View");
2038         
2039         uiDefBut(block, LABEL, 1, " ",  206, 162, 84, 20, NULL, 0.0, 0.0, 0, 0, "");
2040         
2041         
2042         if(vd->flag & V3D_DISPBGPIC) {
2043
2044                 /* Background Image */
2045                 uiDefBut(block, LABEL, 1, "Image:",     0, 128, 76, 19, NULL, 0.0, 0.0, 0, 0, "");
2046                 
2047                 uiBlockBeginAlign(block);
2048                 uiDefIconBut(block, BUT, B_LOADBGPIC, ICON_FILESEL,     90, 128, 20, 20, 0, 0, 0, 0, 0, "Open a new background image");
2049
2050                 id= (ID *)vd->bgpic->ima;
2051                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->image), id, &(vd->menunr));
2052                 if(strp[0]) {
2053                 
2054                         uiDefButS(block, MENU, B_BGPICBROWSE, strp,     110, 128, 20, 20, &(vd->menunr), 0, 0, 0, 0, "Select a background image");
2055                 
2056                         if(vd->bgpic->ima)  {
2057                                 uiDefBut(block, TEX,        0,"BG: ",           130, 128, 140, 20, &vd->bgpic->ima->name,0.0,100.0, 0, 0, "The currently selected background image");
2058                                 uiDefIconBut(block, BUT, B_BGPICCLEAR, ICON_X, 270, 128, 20, 20, 0, 0, 0, 0, 0, "Remove background image link");
2059                         }
2060                         uiBlockEndAlign(block);
2061                 } else {
2062                         uiBlockEndAlign(block);
2063                 }
2064                 MEM_freeN(strp);
2065
2066
2067                 /* Background texture */
2068                 uiDefBut(block, LABEL, 1, "Texture:",   0, 100, 76, 19, NULL, 0.0, 0.0, 0, 0, "");
2069                 
2070                 id= (ID *)vd->bgpic->tex;
2071                 IDnames_to_pupstring(&strp, NULL, NULL, &(G.main->tex), id, &(vd->texnr));
2072                 if (strp[0])
2073                         uiBlockBeginAlign(block);
2074                         uiDefButS(block, MENU, B_BGPICTEX, strp,                        90, 100, 20,20, &(vd->texnr), 0, 0, 0, 0, "Select a texture to use as an animated background image");
2075                 MEM_freeN(strp);
2076                 
2077                 if (id) {
2078                         uiDefBut(block, TEX, B_IDNAME, "TE:",                           110, 100, 160, 20, id->name+2, 0.0, 18.0, 0, 0, "");
2079                         uiDefIconBut(block, BUT, B_BGPICTEXCLEAR, ICON_X,       270, 100, 20, 20, 0, 0, 0, 0, 0, "Remove background texture link");
2080                         uiBlockEndAlign(block);
2081                 } else {
2082                         uiBlockEndAlign(block);
2083                 }
2084
2085                 uiDefButF(block, NUMSLI, B_BLENDBGPIC, "Blend:",        0, 60 , 290, 19, &vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the transparency of the background image");
2086
2087                 uiDefButF(block, NUM, REDRAWVIEW3D, "Size:",            0, 28, 140, 19, &vd->bgpic->size, 0.1, 250.0*vd->grid, 100, 0, "Set the size (width) of the background image");
2088
2089                 uiDefButF(block, NUM, REDRAWVIEW3D, "X Offset:",        0, 6, 140, 19, &vd->bgpic->xof, -250.0*vd->grid,250.0*vd->grid, 10, 2, "Set the horizontal offset of the background image");
2090                 uiDefButF(block, NUM, REDRAWVIEW3D, "Y Offset:",        150, 6, 140, 19, &vd->bgpic->yof, -250.0*vd->grid,250.0*vd->grid, 10, 2, "Set the vertical offset of the background image");
2091
2092         
2093                 
2094                 // uiDefButF(block, NUM, REDRAWVIEW3D, "Size:",                 160,160,150,20, &vd->bgpic->size, 0.1, 250.0, 100, 0, "Set the size for the width of the BackGroundPic");
2095                 
2096
2097
2098 //              uiDefButF(block, NUMSLI, B_BLENDBGPIC, "Blend:",        120,100,190,20,&vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the BackGroundPic transparency");
2099                 
2100 //              uiDefButF(block, NUM, B_DIFF, "Center X: ",     10,70,140,20,&vd->bgpic->xof, -20.0,20.0, 10, 2, "Set the BackGroundPic X Offset");
2101 //              uiDefButF(block, NUM, B_DIFF, "Center Y: ",     160,70,140,20,&vd->bgpic->yof, -20.0,20.0, 10, 2, "Set the BackGroundPic Y Offset");
2102
2103         }
2104 }
2105
2106
2107 static void view3d_panel_properties(short cntrl)        // VIEW3D_HANDLER_SETTINGS
2108 {
2109         uiBlock *block;
2110         View3D *vd;
2111         float *curs;
2112         
2113         vd= G.vd;
2114
2115         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
2116         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2117         uiSetPanelHandler(VIEW3D_HANDLER_PROPERTIES);  // for close and esc
2118         if(uiNewPanel(curarea, block, "View Properties", "View3d", 340, 30, 318, 254)==0) return;
2119
2120         /* to force height */
2121         uiNewPanelHeight(block, 254);
2122
2123         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2124                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2125         }
2126
2127         uiDefBut(block, LABEL, 1, "Grid:",                                      10, 220, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2128         uiDefButF(block, NUM, REDRAWVIEW3D, "Spacing:",         10, 200, 140, 19, &vd->grid, 0.001, 100.0, 10, 0, "Set the distance between grid lines");
2129         uiDefButS(block, NUM, REDRAWVIEW3D, "Lines:",           10, 176, 140, 19, &vd->gridlines, 0.0, 100.0, 100, 0, "Set the number of grid lines");
2130
2131         uiDefBut(block, LABEL, 1, "3D Display:",                                                        160, 220, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2132         uiDefButBitS(block, TOG, V3D_SHOW_FLOOR, REDRAWVIEW3D, "Grid Floor",160, 200, 150, 19, &vd->gridflag, 0, 0, 0, 0, "Show the grid floor in free camera mode");
2133         uiDefButBitS(block, TOG, V3D_SHOW_X, REDRAWVIEW3D, "X Axis",            160, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the X Axis line");
2134         uiDefButBitS(block, TOG, V3D_SHOW_Y, REDRAWVIEW3D, "Y Axis",            212, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Y Axis line");
2135         uiDefButBitS(block, TOG, V3D_SHOW_Z, REDRAWVIEW3D, "Z Axis",            262, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Z Axis line");
2136
2137         uiDefBut(block, LABEL, 1, "View Camera:",                       10, 150, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
2138         
2139         uiDefButF(block, NUM, REDRAWVIEW3D, "Lens:",            10, 130, 140, 19, &vd->lens, 10.0, 120.0, 100, 0, "The lens angle in perspective view");
2140         uiBlockBeginAlign(block);
2141         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip Start:",      10, 106, 140, 19, &vd->near, vd->grid/10.0, 100.0, 10, 0, "Set the beginning of the range in which 3D objects are displayed (perspective view)");
2142         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip End:",        10, 86, 140, 19, &vd->far, 1.0, 1000.0*vd->grid, 100, 0, "Set the end of the range in which 3D objects are displayed (perspective view)");
2143         uiBlockEndAlign(block);
2144
2145         uiDefBut(block, LABEL, 1, "3D Cursor:",                         160, 150, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
2146
2147         uiBlockBeginAlign(block);
2148         curs= give_cursor();
2149         uiDefButF(block, NUM, REDRAWVIEW3D, "X:",                       160, 130, 150, 22, curs, -1000.0*vd->grid, 1000.0*vd->grid, 10, 0, "X co-ordinate of the 3D cursor");
2150         uiDefButF(block, NUM, REDRAWVIEW3D, "Y:",                       160, 108, 150, 22, curs+1, -1000.0*vd->grid, 1000.0*vd->grid, 10, 0, "Y co-ordinate of the 3D cursor");
2151         uiDefButF(block, NUM, REDRAWVIEW3D, "Z:",                       160, 86, 150, 22, curs+2, -1000.0*vd->grid, 1000.0*vd->grid, 10, 0, "Z co-ordinate of the 3D cursor");
2152         uiBlockEndAlign(block);
2153
2154         uiDefBut(block, LABEL, 1, "Display:",                           10, 50, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2155         
2156         uiDefButBitS(block, TOG, V3D_SELECT_OUTLINE, REDRAWVIEW3D, "Outline Selected", 10, 30, 140, 19, &vd->flag, 0, 0, 0, 0, "Highlight selected objects with an outline, in Solid, Shaded or Textured viewport shading modes");
2157         uiDefButBitS(block, TOG, V3D_DRAW_CENTERS, REDRAWVIEW3D, "All Object Centers", 160, 30, 150, 19, &vd->flag, 0, 0, 0, 0, "Draw the center points on all objects");
2158
2159         uiDefButBitS(block, TOGN, V3D_HIDE_HELPLINES, REDRAWVIEW3D, "Relationship Lines", 10, 6, 140, 19, &vd->flag, 0, 0, 0, 0, "Draw dashed lines indicating Parent, Constraint, or Hook relationships");
2160
2161 }
2162
2163 static void view3d_panel_preview(ScrArea *sa, short cntrl)      // VIEW3D_HANDLER_PREVIEW
2164 {
2165         uiBlock *block;
2166         View3D *v3d= sa->spacedata.first;
2167         int ofsx, ofsy;
2168         
2169         block= uiNewBlock(&sa->uiblocks, "view3d_panel_preview", UI_EMBOSS, UI_HELV, sa->win);
2170         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
2171         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
2172         
2173         ofsx= -150+(sa->winx/2)/v3d->blockscale;
2174         ofsy= -100+(sa->winy/2)/v3d->blockscale;
2175         if(uiNewPanel(sa, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
2176
2177         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
2178         
2179         if(G.scene->recalc & SCE_PRV_CHANGED) {
2180                 G.scene->recalc &= ~SCE_PRV_CHANGED;
2181                 //printf("found recalc\n");
2182                 BIF_view3d_previewrender_free(sa->spacedata.first);
2183                 BIF_preview_changed(0);
2184         }
2185 }
2186
2187
2188 static void view3d_blockhandlers(ScrArea *sa)
2189 {
2190         View3D *v3d= sa->spacedata.first;
2191         short a;
2192         
2193         /* warning; blocks need to be freed each time, handlers dont remove */
2194         uiFreeBlocksWin(&sa->uiblocks, sa->win);
2195
2196         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
2197         
2198                 switch(v3d->blockhandler[a]) {
2199
2200                 case VIEW3D_HANDLER_PROPERTIES:
2201                         view3d_panel_properties(v3d->blockhandler[a+1]);
2202                         break;
2203                 case VIEW3D_HANDLER_BACKGROUND:
2204                         view3d_panel_background(v3d->blockhandler[a+1]);
2205                         break;
2206                 case VIEW3D_HANDLER_OBJECT:
2207                         view3d_panel_object(v3d->blockhandler[a+1]);
2208                         break;
2209                 case VIEW3D_HANDLER_PREVIEW:
2210                         view3d_panel_preview(sa, v3d->blockhandler[a+1]);
2211                         break;
2212                         
2213                 }
2214                 /* clear action value for event */
2215                 v3d->blockhandler[a+1]= 0;
2216         }
2217         uiDrawBlocksPanels(sa, 0);
2218
2219 }
2220
2221 /* ****************** View3d afterdraw *************** */
2222
2223 typedef struct View3DAfter {
2224         struct View3DAfter *next, *prev;
2225         struct Base *base;
2226         int type;
2227 } View3DAfter;
2228
2229 /* temp storage of Objects that need to be drawn as last */
2230 void add_view3d_after(View3D *v3d, Base *base, int type)
2231 {
2232         View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
2233
2234         BLI_addtail(&v3d->afterdraw, v3da);
2235         v3da->base= base;
2236         v3da->type= type;
2237 }
2238
2239 /* clears zbuffer and draws it over */
2240 static void view3d_draw_xray(View3D *v3d, int flag)
2241 {
2242         View3DAfter *v3da, *next;
2243         int doit= 0;
2244         
2245         for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
2246                 if(v3da->type==V3D_XRAY) doit= 1;
2247         
2248         if(doit) {
2249                 if(v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
2250                 v3d->xray= TRUE;
2251                 
2252                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2253                         next= v3da->next;
2254                         if(v3da->type==V3D_XRAY) {
2255                                 draw_object(v3da->base, flag);
2256                                 BLI_remlink(&v3d->afterdraw, v3da);
2257                                 MEM_freeN(v3da);
2258                         }
2259                 }
2260                 v3d->xray= FALSE;
2261         }
2262 }
2263
2264 /* disables write in zbuffer and draws it over */
2265 static void view3d_draw_transp(View3D *v3d, int flag)
2266 {
2267         View3DAfter *v3da, *next;
2268
2269         glDepthMask(0);
2270         v3d->transp= TRUE;
2271                 
2272         for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2273                 next= v3da->next;
2274                 if(v3da->type==V3D_TRANSP) {
2275                         draw_object(v3da->base, flag);
2276                         BLI_remlink(&v3d->afterdraw, v3da);
2277                         MEM_freeN(v3da);
2278                 }
2279         }
2280         v3d->transp= FALSE;
2281
2282         glDepthMask(1);
2283
2284 }
2285
2286 /* *********************** */
2287
2288 static void draw_dupli_objects(View3D *v3d, Base *base)
2289 {
2290         ListBase *lb;
2291         DupliObject *dob;
2292         Base tbase;
2293         int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
2294         char dt, dtx;
2295         
2296         /* debug */
2297         if(base->object->dup_group && base->object->dup_group->id.us<1)
2298                 color= TH_REDALERT;
2299         
2300         tbase.flag= OB_FROMDUPLI|base->flag;
2301         lb= object_duplilist(G.scene, base->object);
2302
2303         for(dob= lb->first; dob; dob= dob->next) {
2304                 tbase.object= dob->ob;
2305                 
2306                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
2307                 
2308                 /* extra service: draw the duplicator in drawtype of parent */
2309                 dt= tbase.object->dt; tbase.object->dt= base->object->dt;
2310                 dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
2311                 
2312                 BIF_ThemeColorBlend(color, TH_BACK, 0.5);
2313                 draw_object(&tbase, DRAW_CONSTCOLOR);
2314                 
2315                 tbase.object->dt= dt;
2316                 tbase.object->dtx= dtx;
2317         }
2318         
2319         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
2320         
2321         free_object_duplilist(lb);      /* does restore */
2322                                 
2323 }
2324
2325 void drawview3dspace(ScrArea *sa, void *spacedata)
2326 {
2327         View3D *v3d= spacedata;
2328         Base *base;
2329         Object *ob;
2330         Scene *setscene;
2331         
2332         setwinmatrixview3d(sa->winx, sa->winy, NULL);   /* 0= no pick rect */
2333         setviewmatrixview3d();  /* note: calls where_is_object for camera... */
2334
2335         Mat4MulMat4(v3d->persmat, v3d->viewmat, sa->winmat);
2336         Mat4Invert(v3d->persinv, v3d->persmat);
2337         Mat4Invert(v3d->viewinv, v3d->viewmat);
2338
2339         /* calculate pixelsize factor once, is used for lamps and obcenters */
2340         {
2341                 float len1, len2, vec[3];
2342
2343                 VECCOPY(vec, v3d->persinv[0]);
2344                 len1= Normalise(vec);
2345                 VECCOPY(vec, v3d->persinv[1]);
2346                 len2= Normalise(vec);
2347                 
2348                 v3d->pixsize= 2.0f*(len1>len2?len1:len2);
2349                 
2350                 /* correct for window size */
2351                 if(sa->winx > sa->winy) v3d->pixsize/= (float)sa->winx;
2352                 else v3d->pixsize/= (float)sa->winy;
2353         }
2354         
2355         if(v3d->drawtype > OB_WIRE) {
2356                 if(G.f & G_SIMULATION)
2357                         glClearColor(0.0, 0.0, 0.0, 0.0); 
2358                 else {
2359                         float col[3];
2360                         BIF_GetThemeColor3fv(TH_BACK, col);
2361                         glClearColor(col[0], col[1], col[2], 0.0); 
2362                 }
2363                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2364                 
2365                 glLoadIdentity();
2366         }
2367         else {
2368                 float col[3];
2369                 BIF_GetThemeColor3fv(TH_BACK, col);
2370                 glClearColor(col[0], col[1], col[2], 0.0);
2371                 glClear(GL_COLOR_BUFFER_BIT);
2372         }
2373         
2374         myloadmatrix(v3d->viewmat);
2375         persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
2376
2377         if(v3d->flag & V3D_CLIPPING)
2378                 view3d_draw_clipping(v3d);
2379         
2380         /* set zbuffer after we draw clipping region */
2381         if(v3d->drawtype > OB_WIRE) {
2382                 v3d->zbuf= TRUE;
2383                 glEnable(GL_DEPTH_TEST);
2384         }
2385         
2386         // needs to be done always, gridview is adjusted in drawgrid() now
2387         v3d->gridview= v3d->grid;
2388         
2389         if(v3d->view==0 || v3d->persp!=0) {
2390                 drawfloor();
2391                 if(v3d->persp==2) {
2392                         if(G.scene->world) {
2393                                 if(G.scene->world->mode & WO_STARS) {
2394 //                                      RE_make_stars(star_stuff_init_func, star_stuff_vertex_func,
2395 //                                                                star_stuff_term_func);
2396                                 }
2397                         }
2398                         if(v3d->flag & V3D_DISPBGPIC) draw_bgpic();
2399                 }
2400         }
2401         else {
2402                 drawgrid();
2403
2404                 if(v3d->flag & V3D_DISPBGPIC) {
2405                         draw_bgpic();
2406                 }
2407         }
2408         
2409         if(v3d->flag & V3D_CLIPPING)
2410                 view3d_set_clipping(v3d);
2411         
2412         /* draw set first */
2413         setscene= G.scene->set;
2414         if(setscene) { /* if(G.scene->set) { */
2415         
2416                 base= setscene->base.first; /* base= G.scene->set->base.first; */
2417                 while(base) {
2418                         
2419                         if(v3d->lay & base->lay) {
2420
2421                                 object_handle_update(base->object);
2422                                 
2423                                 BIF_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2424                                 draw_object(base, DRAW_CONSTCOLOR);
2425
2426                                 if(base->object->transflag & OB_DUPLI) {
2427                                         draw_dupli_objects(v3d, base);
2428                                 }
2429                         }
2430                         
2431                         base= base->next;
2432                         if(base==0 && setscene && setscene->set) {
2433                                 setscene= setscene->set;
2434                                 base= setscene->base.first;
2435                         }
2436                 }
2437
2438                 /* Transp and X-ray afterdraw stuff */
2439                 view3d_draw_xray(v3d, DRAW_CONSTCOLOR); // clears zbuffer if it is used!
2440                 view3d_draw_transp(v3d, DRAW_CONSTCOLOR);
2441         }
2442         
2443         /* update all objects, ipos, matrices, displists, etc. Flags set by depgraph or manual, no layer check here, gets correct flushed */
2444         for(base= G.scene->base.first; base; base= base->next) {
2445                 object_handle_update(base->object);   // bke_object.h
2446 #if 0           
2447                 if(v3d->lay & base->lay) {
2448                         if(base->object->dup_group) {
2449                                 GroupObject *go;
2450                                 for(go= base->object->dup_group->gobject.first; go; go= go->next) {
2451                                         if(go->ob) {
2452                                                 if(go->ob->type==OB_MESH) {
2453                                                         int dummy;
2454                                                         mesh_get_derived_deform(go->ob, &dummy);
2455                                                         mesh_get_derived_final(go->ob, &dummy);
2456                                                 }
2457                                                 else if(go->ob->type==OB_CURVE) {
2458                                                         cu= ob->data;
2459                                                         if (cu->disp.first==NULL) makeDispListCurveTypes(ob, 0);
2460                                                 }
2461                                         }
2462                                 }
2463                         }
2464                 }
2465 #endif
2466         }
2467         
2468         /* then draw not selected and the duplis, but skip editmode object */
2469         for(base= G.scene->base.first; base; base= base->next) {
2470                 if(v3d->lay & base->lay) {
2471                         
2472                         /* dupli drawing */
2473                         if(base->object->transflag & OB_DUPLI) {
2474                                 draw_dupli_objects(v3d, base);
2475                         }
2476                         if((base->flag & SELECT)==0) {
2477                                 if(base->object!=G.obedit) draw_object(base, 0);
2478                         }
2479                 }
2480         }
2481         /* draw selected and editmode */
2482         for(base= G.scene->base.first; base; base= base->next) {
2483                 if(v3d->lay & base->lay) {
2484                         if (base->object==G.obedit || ( base->flag & SELECT) ) 
2485                                 draw_object(base, 0);
2486                 }
2487         }
2488
2489         if(G.moving) {
2490                 BIF_drawConstraint();
2491                 if(G.obedit) BIF_drawPropCircle();      // only editmode has proportional edit
2492         }
2493
2494         if(G.scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
2495         
2496         /* Transp and X-ray afterdraw stuff */
2497         view3d_draw_xray(v3d, 0);       // clears zbuffer if it is used!
2498         view3d_draw_transp(v3d, 0);
2499         
2500         if(v3d->flag & V3D_CLIPPING)
2501                 view3d_clr_clipping();
2502         
2503         BIF_draw_manipulator(sa);
2504                 
2505         if(v3d->zbuf) {
2506                 v3d->zbuf= FALSE;
2507                 glDisable(GL_DEPTH_TEST);
2508         }
2509
2510         persp(PERSP_WIN);  // set ortho
2511         
2512         if(v3d->persp>1) drawviewborder();
2513         if(!(G.f & G_PLAYANIM)) drawcursor(v3d);
2514         draw_view_icon();
2515
2516         ob= OBACT;
2517         if(ob && (U.uiflag & USER_DRAWVIEWINFO)) 
2518                 draw_selected_name(ob);
2519         
2520         draw_area_emboss(sa);
2521         
2522         /* it is important to end a view in a transform compatible with buttons */
2523
2524         bwin_scalematrix(sa->win, v3d->blockscale, v3d->blockscale, v3d->blockscale);
2525         view3d_blockhandlers(sa);
2526
2527         sa->win_swap= WIN_BACK_OK;
2528         
2529         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2530                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2531                 addafterqueue(sa->win, BACKBUFDRAW, 1);
2532         }
2533         // test for backbuf select
2534         if(G.obedit && v3d->drawtype>OB_WIRE && (v3d->flag & V3D_ZBUF_SELECT)) {
2535                 extern int afterqtest(short win, unsigned short evt);   //editscreen.c
2536
2537                 v3d->flag |= V3D_NEEDBACKBUFDRAW;
2538                 if(afterqtest(sa->win, BACKBUFDRAW)==0) {
2539                         addafterqueue(sa->win, BACKBUFDRAW, 1);
2540                 }
2541         }
2542
2543         /* run any view3d draw handler script links */
2544         if (sa->scriptlink.totscript)
2545                 BPY_do_spacehandlers(sa, 0, SPACEHANDLER_VIEW3D_DRAW);
2546
2547         /* run scene redraw script links */
2548         if((G.f & G_DOSCRIPTLINKS) && G.scene->scriptlink.totscript &&
2549                         !during_script()) {
2550                 BPY_do_pyscript((ID *)G.scene, SCRIPT_REDRAW);
2551         }
2552
2553 }
2554
2555
2556 void drawview3d_render(struct View3D *v3d, int winx, int winy)
2557 {
2558         Base *base;
2559         Scene *setscene;
2560         float winmat[4][4];
2561         
2562         update_for_newframe_muted();    /* first, since camera can be animated */
2563
2564         setwinmatrixview3d(winx, winy, NULL);
2565         
2566         setviewmatrixview3d();
2567         myloadmatrix(v3d->viewmat);
2568         glMatrixMode(GL_PROJECTION);
2569         mygetmatrix(winmat);
2570         glMatrixMode(GL_MODELVIEW);
2571         Mat4MulMat4(v3d->persmat, v3d->viewmat, winmat);
2572         Mat4Invert(v3d->persinv, v3d->persmat);
2573         Mat4Invert(v3d->viewinv, v3d->viewmat);
2574
2575         free_all_realtime_images();
2576         reshadeall_displist();
2577         
2578         if(v3d->drawtype > OB_WIRE) {
2579                 v3d->zbuf= TRUE;
2580                 glEnable(GL_DEPTH_TEST);
2581         }
2582
2583         if(v3d->flag & V3D_CLIPPING)
2584                 view3d_set_clipping(v3d);
2585
2586         if (v3d->drawtype==OB_TEXTURE && G.scene->world) {
2587                 glClearColor(G.scene->world->horr, G.scene->world->horg, G.scene->world->horb, 0.0); 
2588         } else {
2589                 float col[3];
2590                 BIF_GetThemeColor3fv(TH_BACK, col);
2591                 glClearColor(col[0], col[1], col[2], 0.0); 
2592         }
2593         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2594
2595         /* abuse! to make sure it doesnt draw the helpstuff */
2596         G.f |= G_SIMULATION;
2597
2598         /* first draw set */
2599         setscene= G.scene->set;
2600         if(setscene) { /* if(G.scene->set) { */
2601         
2602                 base= setscene->base.first; /* base= G.scene->set->base.first; */
2603                 while(base) {
2604                         if(v3d->lay & base->lay) {
2605                                 if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
2606                                 else {
2607                                         where_is_object(base->object);
2608         
2609                                         BIF_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
2610                                         draw_object(base, DRAW_CONSTCOLOR);
2611         
2612                                         if(base->object->transflag & OB_DUPLI) {
2613                                                 draw_dupli_objects(v3d, base);
2614                                         }
2615                                 }
2616                         }
2617                         base= base->next;
2618                         if(base==0 && setscene && setscene->set) {
2619                                 setscene= setscene->set;
2620                                 base= setscene->base.first;
2621                         }
2622                 }
2623                 
2624                 /* Transp and X-ray afterdraw stuff */
2625                 view3d_draw_xray(v3d, DRAW_CONSTCOLOR); // clears zbuffer if it is used!
2626                 view3d_draw_transp(v3d, DRAW_CONSTCOLOR);
2627         }
2628
2629         /* first not selected and duplis */
2630         base= G.scene->base.first;
2631         while(base) {
2632                 
2633                 if(v3d->lay & base->lay) {
2634                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
2635                         else {
2636         
2637                                 if(base->object->transflag & OB_DUPLI) {
2638                                         draw_dupli_objects(v3d, base);
2639                                 }
2640                                 else if((base->flag & SELECT)==0) {
2641                                         draw_object(base, 0);
2642                                 }
2643                         }
2644                 }
2645                 
2646                 base= base->next;
2647         }
2648
2649         /* draw selected */
2650         base= G.scene->base.first;
2651         while(base) {
2652                 
2653                 if ( ((base)->flag & SELECT) && ((base)->lay & v3d->lay) ) {
2654                         if ELEM3(base->object->type, OB_LAMP, OB_CAMERA, OB_LATTICE);
2655                         else draw_object(base, 0);
2656                 }
2657                 
2658                 base= base->next;
2659         }
2660
2661         if(G.scene->radio) RAD_drawall(v3d->drawtype>=OB_SOLID);
2662
2663         /* Transp and X-ray afterdraw stuff */
2664         view3d_draw_xray(v3d, 0);       // clears zbuffer if it is used!
2665         view3d_draw_transp(v3d, 0);
2666         
2667         if(v3d->flag & V3D_CLIPPING)
2668                 view3d_clr_clipping();
2669         
2670         if(v3d->zbuf) {
2671                 v3d->zbuf= FALSE;
2672                 glDisable(GL_DEPTH_TEST);
2673         }
2674         
2675         G.f &= ~G_SIMULATION;
2676
2677         glFlush();
2678
2679         glLoadIdentity();
2680
2681         free_all_realtime_images();
2682 }
2683
2684
2685 double tottime = 0.0;
2686
2687 int update_time(void)
2688 {
2689         static double ltime;
2690         double time;
2691
2692         if ((U.mixbufsize)&&(audiostream_pos() != CFRA)&&(G.scene->audio.flag & AUDIO_SYNC)) return 0;
2693
2694         time = PIL_check_seconds_timer();
2695         
2696         tottime += (time - ltime);
2697         ltime = time;
2698         return (tottime < 0.0);
2699 }
2700
2701 void inner_play_anim_loop(int init, int mode)
2702 {
2703         ScrArea *sa;
2704         static ScrArea *oldsa;
2705         static double swaptime;
2706         static int curmode;
2707
2708         /* init */
2709         if(init) {
2710                 oldsa= curarea;
2711                 swaptime= 1.0/(float)G.scene->r.frs_sec;
2712                 tottime= 0.0;
2713                 curmode= mode;
2714
2715                 return;
2716         }
2717
2718         set_timecursor(CFRA);
2719         
2720         update_for_newframe_muted();
2721
2722         sa= G.curscreen->areabase.first;
2723         while(sa) {
2724                 if(sa==oldsa) {
2725                         scrarea_do_windraw(sa);
2726                 }
2727                 else if(curmode & 1) { /* catch modes 1 and 3 */
2728                         if ELEM(sa->spacetype, SPACE_VIEW3D, SPACE_SEQ) {
2729                                 scrarea_do_windraw(sa);
2730                         }
2731                 }
2732                 
2733                 sa= sa->next;   
2734         }
2735         
2736         /* make sure that swaptime passed by */
2737         tottime -= swaptime;
2738         while (update_time()) PIL_sleep_ms(1);
2739
2740         if(CFRA>=EFRA) {
2741                 if (tottime > 0.0) tottime = 0.0;
2742                 CFRA= SFRA;
2743                 audiostream_stop();
2744                 audiostream_start( CFRA );
2745         }
2746         else {
2747                 if (U.mixbufsize && (G.scene->audio.flag & AUDIO_SYNC)) CFRA = audiostream_pos();
2748                 else CFRA++;
2749         }
2750 }
2751
2752 /* play_anim: 'mode' defines where to play and if repeat is on:
2753  * - 0: current area
2754  * - 1: all view3d and seq areas
2755  * - 2: current area, no replay
2756  * - 3: all view3d and seq areas, no replay */
2757 int play_anim(int mode)
2758 {
2759         ScrArea *sa, *oldsa;
2760         int cfraont;
2761         unsigned short event=0;
2762         short val;
2763
2764         /* patch for very very old scenes */
2765         if(SFRA==0) SFRA= 1;
2766         if(EFRA==0) EFRA= 250;
2767
2768         if(SFRA>EFRA) return 0;
2769         
2770         update_time();
2771
2772         /* waitcursor(1); */
2773         G.f |= G_PLAYANIM;              /* in sequence.c and view.c this is handled */
2774
2775         cfraont= CFRA;
2776         oldsa= curarea;
2777
2778         audiostream_start( CFRA );
2779         
2780         inner_play_anim_loop(1, mode);  /* 1==init */
2781         
2782          /* forces all buffers to be OK for current frame (otherwise other windows get redrawn with CFRA+1) */
2783         curarea->win_swap= WIN_BACK_OK;
2784         screen_swapbuffers();
2785         
2786         while(TRUE) {
2787
2788                 while(qtest()) {
2789                         
2790                         /* we test events first because of MKEY event */
2791                         
2792                         event= extern_qread(&val);
2793                         if(event==ESCKEY) break;
2794                         else if(event==MIDDLEMOUSE) {
2795                                 if(U.flag & USER_VIEWMOVE) {
2796                                         if(G.qual & LR_SHIFTKEY) viewmove(0);
2797                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2798                                         else viewmove(1);
2799                                 }
2800                                 else {
2801                                         if(G.qual & LR_SHIFTKEY) viewmove(1);
2802                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
2803                                         else viewmove(0);
2804                                 }
2805                         }
2806                         else if(event==MKEY) {
2807                                 if(val) add_timeline_marker(CFRA-1);
2808                         }
2809                 }
2810                 if(ELEM3(event, ESCKEY, SPACEKEY, RIGHTMOUSE)) break;
2811                 
2812                 inner_play_anim_loop(0, 0);
2813                 screen_swapbuffers();
2814                                 
2815                 if((mode > 1) && CFRA==EFRA) break; /* no replay */     
2816         }
2817
2818         if(event==SPACEKEY);
2819         else CFRA= cfraont;
2820         
2821         audiostream_stop();
2822
2823         if(oldsa!=curarea) areawinset(oldsa->win);
2824         
2825         /* restore all areas */
2826         sa= G.curscreen->areabase.first;
2827         while(sa) {
2828                 if( (mode && sa->spacetype==SPACE_VIEW3D) || sa==curarea) addqueue(sa->win, REDRAW, 1);
2829                 sa= sa->next;   
2830         }
2831         
2832         /* groups could have changed ipo */
2833         allspace(REMAKEIPO, 0);
2834         allqueue(REDRAWIPO, 0);
2835         allqueue(REDRAWNLA, 0);
2836         allqueue (REDRAWACTION, 0);
2837         
2838         /* restore for cfra */
2839         update_for_newframe_muted();
2840
2841         waitcursor(0);
2842         G.f &= ~G_PLAYANIM;
2843         
2844         if (event==ESCKEY || event==SPACEKEY) return 1;
2845         else return 0;
2846 }