resolve some compiler warnings with intel c/c++ compiler
[blender-staging.git] / source / blender / src / drawview.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 //#define NAN_LINEAR_PHYSICS
31
32 #include <math.h>
33 #include <string.h>
34
35 #ifndef WIN32
36 #include <unistd.h>
37 #include <sys/times.h>
38 #else
39 #include <io.h>
40 #endif   
41
42 #ifdef WIN32
43 #ifndef snprintf
44 #define snprintf _snprintf
45 #endif
46 #endif
47
48 #include "MEM_guardedalloc.h"
49
50 #include "PIL_time.h"
51
52 #include "BMF_Api.h"
53
54 #include "IMB_imbuf_types.h"
55 #include "IMB_imbuf.h"
56
57 #include "DNA_action_types.h"
58 #include "DNA_armature_types.h"
59 #include "DNA_brush_types.h"
60 #include "DNA_camera_types.h"
61 #include "DNA_constraint_types.h"
62 #include "DNA_curve_types.h"
63 #include "DNA_group_types.h"
64 #include "DNA_gpencil_types.h"
65 #include "DNA_image_types.h"
66 #include "DNA_key_types.h"
67 #include "DNA_lamp_types.h"
68 #include "DNA_lattice_types.h"
69 #include "DNA_mesh_types.h"
70 #include "DNA_meshdata_types.h"
71 #include "DNA_meta_types.h"
72 #include "DNA_modifier_types.h"
73 #include "DNA_object_force.h"
74 #include "DNA_object_types.h"
75 #include "DNA_particle_types.h"
76 #include "DNA_screen_types.h"
77 #include "DNA_scene_types.h"
78 #include "DNA_space_types.h"
79 #include "DNA_texture_types.h"
80 #include "DNA_userdef_types.h"
81 #include "DNA_view3d_types.h"
82 #include "DNA_world_types.h"
83
84 #include "BLI_blenlib.h"
85 #include "BLI_arithb.h"
86 #include "BLI_editVert.h"
87
88 #include "BKE_action.h"
89 #include "BKE_armature.h"
90 #include "BKE_anim.h"
91 #include "BKE_constraint.h"
92 #include "BKE_curve.h"
93 #include "BKE_customdata.h"
94 #include "BKE_displist.h"
95 #include "BKE_depsgraph.h"
96 #include "BKE_DerivedMesh.h"
97 #include "BKE_global.h"
98 #include "BKE_lattice.h"
99 #include "BKE_library.h"
100 #include "BKE_image.h"
101 #include "BKE_ipo.h"
102 #include "BKE_key.h"
103 #include "BKE_main.h"
104 #include "BKE_mesh.h"
105 #include "BKE_modifier.h"
106 #include "BKE_object.h"
107 #include "BKE_particle.h"
108 #include "BKE_pointcache.h"
109 #include "BKE_scene.h"
110 #include "BKE_sculpt.h"
111 #include "BKE_texture.h"
112 #include "BKE_utildefines.h"
113
114 #include "BIF_butspace.h"
115 #include "BIF_drawimage.h"
116 #include "BIF_drawgpencil.h"
117 #include "BIF_editgroup.h"
118 #include "BIF_editarmature.h"
119 #include "BIF_editmesh.h"
120 #include "BIF_editparticle.h"
121 #include "BIF_gl.h"
122 #include "BIF_glutil.h"
123 #include "BIF_interface.h"
124 #include "BIF_interface_icons.h"
125 #include "BIF_mywindow.h"
126 #include "BIF_poseobject.h"
127 #include "BIF_previewrender.h"
128 #include "BIF_radialcontrol.h"
129 #include "BIF_resources.h"
130 #include "BIF_retopo.h"
131 #include "BIF_screen.h"
132 #include "BIF_space.h"
133
134 #ifdef WITH_VERSE
135 #include "BIF_verse.h"
136 #endif
137
138 #include "BDR_drawaction.h"
139 #include "BDR_drawmesh.h"
140 #include "BDR_drawobject.h"
141 #include "BDR_editobject.h"
142 #include "BDR_vpaint.h"
143 #include "BDR_sculptmode.h"
144 #include "BDR_gpencil.h"
145
146 #include "BSE_drawview.h"
147 #include "BSE_filesel.h"
148 #include "BSE_headerbuttons.h"
149 #include "BSE_seqaudio.h"
150 #include "BSE_sequence.h"
151 #include "BSE_trans_types.h"
152 #include "BSE_time.h"
153 #include "BSE_view.h"
154
155 #include "BPY_extern.h"
156
157 #include "RE_render_ext.h"
158
159 #include "blendef.h"
160 #include "mydevice.h"
161 #include "butspace.h"  // event codes
162
163 #include "BIF_transform.h"
164
165 #include "RE_pipeline.h"        // make_stars
166
167 #include "GPU_draw.h"
168 #include "GPU_material.h"
169
170 #include "multires.h"
171
172 /* For MULTISAMPLE_ARB #define.
173    Note that older systems like irix 
174    may not have this, and will need a #ifdef
175    to disable it.*/
176 /* #include "GL/glext.h" Disabled for release, to avoid possibly breaking platforms.
177    Instead, the define we need will just be #defined if it's not in the platform opengl.h.
178 */
179
180 /* Modules used */
181 #include "radio.h"
182
183 /* locals */
184 static void drawname(Object *ob);
185
186 static void star_stuff_init_func(void)
187 {
188         cpack(-1);
189         glPointSize(1.0);
190         glBegin(GL_POINTS);
191 }
192 static void star_stuff_vertex_func(float* i)
193 {
194         glVertex3fv(i);
195 }
196 static void star_stuff_term_func(void)
197 {
198         glEnd();
199 }
200
201 void circf(float x, float y, float rad)
202 {
203         GLUquadricObj *qobj = gluNewQuadric(); 
204         
205         gluQuadricDrawStyle(qobj, GLU_FILL); 
206         
207         glPushMatrix(); 
208         
209         glTranslatef(x,  y, 0.); 
210         
211         gluDisk( qobj, 0.0,  rad, 32, 1); 
212         
213         glPopMatrix(); 
214         
215         gluDeleteQuadric(qobj);
216 }
217
218 void circ(float x, float y, float rad)
219 {
220         GLUquadricObj *qobj = gluNewQuadric(); 
221         
222         gluQuadricDrawStyle(qobj, GLU_SILHOUETTE); 
223         
224         glPushMatrix(); 
225         
226         glTranslatef(x,  y, 0.); 
227         
228         gluDisk( qobj, 0.0,  rad, 32, 1); 
229         
230         glPopMatrix(); 
231         
232         gluDeleteQuadric(qobj);
233 }
234
235 /* **********  ********** */
236
237 static void draw_bgpic(void)
238 {
239         BGpic *bgpic;
240         Image *ima;
241         ImBuf *ibuf= NULL;
242         float vec[4], fac, asp, zoomx, zoomy;
243         float x1, y1, x2, y2, cx, cy;
244         
245         bgpic= G.vd->bgpic;
246         if(bgpic==NULL) return;
247         
248         ima= bgpic->ima;
249         
250         if(ima)
251                 ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
252         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL) ) 
253                 return;
254         if(ibuf->channels!=4)
255                 return;
256         if(ibuf->rect==NULL)
257                 IMB_rect_from_float(ibuf);
258         
259         if(G.vd->persp==2) {
260                 rctf vb;
261
262                 calc_viewborder(G.vd, &vb);
263
264                 x1= vb.xmin;
265                 y1= vb.ymin;
266                 x2= vb.xmax;
267                 y2= vb.ymax;
268         }
269         else {
270                 float sco[2];
271
272                 /* calc window coord */
273                 initgrabz(0.0, 0.0, 0.0);
274                 window_to_3d(vec, 1, 0);
275                 fac= MAX3( fabs(vec[0]), fabs(vec[1]), fabs(vec[1]) );
276                 fac= 1.0/fac;
277         
278                 asp= ( (float)ibuf->y)/(float)ibuf->x;
279
280                 vec[0] = vec[1] = vec[2] = 0.0;
281                 view3d_project_float(curarea, vec, sco, G.vd->persmat);
282                 cx = sco[0];
283                 cy = sco[1];
284         
285                 x1=  cx+ fac*(bgpic->xof-bgpic->size);
286                 y1=  cy+ asp*fac*(bgpic->yof-bgpic->size);
287                 x2=  cx+ fac*(bgpic->xof+bgpic->size);
288                 y2=  cy+ asp*fac*(bgpic->yof+bgpic->size);
289         }
290         
291         /* complete clip? */
292         
293         if(x2 < 0 ) return;
294         if(y2 < 0 ) return;
295         if(x1 > curarea->winx ) return;
296         if(y1 > curarea->winy ) return;
297         
298         zoomx= (x2-x1)/ibuf->x;
299         zoomy= (y2-y1)/ibuf->y;
300         
301         /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
302         if(zoomx < 1.0f || zoomy < 1.0f) {
303                 float tzoom= MIN2(zoomx, zoomy);
304                 int mip= 0;
305                 
306                 if(ibuf->mipmap[0]==NULL)
307                         IMB_makemipmap(ibuf, 0);
308                 
309                 while(tzoom < 1.0f && mip<8 && ibuf->mipmap[mip]) {
310                         tzoom*= 2.0f;
311                         zoomx*= 2.0f;
312                         zoomy*= 2.0f;
313                         mip++;
314                 }
315                 if(mip>0)
316                         ibuf= ibuf->mipmap[mip-1];
317         }
318         
319         if(G.vd->zbuf) glDisable(GL_DEPTH_TEST);
320
321         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
322          
323         glMatrixMode(GL_PROJECTION);
324         glPushMatrix();
325         glMatrixMode(GL_MODELVIEW);
326         glPushMatrix();
327         
328         glaDefine2DArea(&curarea->winrct);
329
330         glEnable(GL_BLEND);
331
332         glPixelZoom(zoomx, zoomy);
333         glColor4f(1.0, 1.0, 1.0, 1.0-bgpic->blend);
334         glaDrawPixelsTex(x1, y1, ibuf->x, ibuf->y, GL_UNSIGNED_BYTE, ibuf->rect);
335         
336         glPixelZoom(1.0, 1.0);
337         glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
338
339         glMatrixMode(GL_PROJECTION);
340         glPopMatrix();
341         glMatrixMode(GL_MODELVIEW);
342         glPopMatrix();
343         
344         glDisable(GL_BLEND);
345         if(G.vd->zbuf) glEnable(GL_DEPTH_TEST);
346         
347         areawinset(curarea->win);       // restore viewport / scissor
348 }
349
350 static void drawgrid_draw(float wx, float wy, float x, float y, float dx)
351 {
352         float fx, fy;
353         
354         x+= (wx); 
355         y+= (wy);
356         fx= x/dx;
357         fx= x-dx*floor(fx);
358         
359         while(fx< curarea->winx) {
360                 fdrawline(fx,  0.0,  fx,  (float)curarea->winy); 
361                 fx+= dx; 
362         }
363
364         fy= y/dx;
365         fy= y-dx*floor(fy);
366         
367
368         while(fy< curarea->winy) {
369                 fdrawline(0.0,  fy,  (float)curarea->winx,  fy); 
370                 fy+= dx;
371         }
372
373 }
374
375 // not intern, called in editobject for constraint axis too
376 void make_axis_color(char *col, char *col2, char axis)
377 {
378         if(axis=='x') {
379                 col2[0]= col[0]>219?255:col[0]+36;
380                 col2[1]= col[1]<26?0:col[1]-26;
381                 col2[2]= col[2]<26?0:col[2]-26;
382         }
383         else if(axis=='y') {
384                 col2[0]= col[0]<46?0:col[0]-36;
385                 col2[1]= col[1]>189?255:col[1]+66;
386                 col2[2]= col[2]<46?0:col[2]-36; 
387         }
388         else {
389                 col2[0]= col[0]<26?0:col[0]-26; 
390                 col2[1]= col[1]<26?0:col[1]-26; 
391                 col2[2]= col[2]>209?255:col[2]+46;
392         }
393         
394 }
395
396 static void drawgrid(void)
397 {
398         /* extern short bgpicmode; */
399         float wx, wy, x, y, fw, fx, fy, dx;
400         float vec4[4];
401         char col[3], col2[3];
402         short sublines = G.vd->gridsubdiv;
403         
404         vec4[0]=vec4[1]=vec4[2]=0.0; 
405         vec4[3]= 1.0;
406         Mat4MulVec4fl(G.vd->persmat, vec4);
407         fx= vec4[0]; 
408         fy= vec4[1]; 
409         fw= vec4[3];
410
411         wx= (curarea->winx/2.0);        /* because of rounding errors, grid at wrong location */
412         wy= (curarea->winy/2.0);
413
414         x= (wx)*fx/fw;
415         y= (wy)*fy/fw;
416
417         vec4[0]=vec4[1]=G.vd->grid; 
418         vec4[2]= 0.0;
419         vec4[3]= 1.0;
420         Mat4MulVec4fl(G.vd->persmat, vec4);
421         fx= vec4[0]; 
422         fy= vec4[1]; 
423         fw= vec4[3];
424
425         dx= fabs(x-(wx)*fx/fw);
426         if(dx==0) dx= fabs(y-(wy)*fy/fw);
427         
428         glDepthMask(0);         // disable write in zbuffer
429
430         /* check zoom out */
431         BIF_ThemeColor(TH_GRID);
432         persp(PERSP_WIN);
433         
434         if(dx<6.0) {
435                 G.vd->gridview*= sublines;
436                 dx*= sublines;
437                 
438                 if(dx<6.0) {    
439                         G.vd->gridview*= sublines;
440                         dx*= sublines;
441                         
442                         if(dx<6.0) {
443                                 G.vd->gridview*= sublines;
444                                 dx*=sublines;
445                                 if(dx<6.0);
446                                 else {
447                                         BIF_ThemeColor(TH_GRID);
448                                         drawgrid_draw(wx, wy, x, y, dx);
449                                 }
450                         }
451                         else {  // start blending out
452                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
453                                 drawgrid_draw(wx, wy, x, y, dx);
454                         
455                                 BIF_ThemeColor(TH_GRID);
456                                 drawgrid_draw(wx, wy, x, y, sublines*dx);
457                         }
458                 }
459                 else {  // start blending out (6 < dx < 60)
460                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
461                         drawgrid_draw(wx, wy, x, y, dx);
462                         
463                         BIF_ThemeColor(TH_GRID);
464                         drawgrid_draw(wx, wy, x, y, sublines*dx);
465                 }
466         }
467         else {
468                 if(dx>60.0) {           // start blending in
469                         G.vd->gridview/= sublines;
470                         dx/= sublines;                  
471                         if(dx>60.0) {           // start blending in
472                                 G.vd->gridview/= sublines;
473                                 dx/= sublines;
474                                 if(dx>60.0) {
475                                         BIF_ThemeColor(TH_GRID);
476                                         drawgrid_draw(wx, wy, x, y, dx);
477                                 }
478                                 else {
479                                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
480                                         drawgrid_draw(wx, wy, x, y, dx);
481                                         BIF_ThemeColor(TH_GRID);
482                                         drawgrid_draw(wx, wy, x, y, dx*sublines);
483                                 }
484                         }
485                         else {
486                                 BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
487                                 drawgrid_draw(wx, wy, x, y, dx);
488                                 BIF_ThemeColor(TH_GRID);                                
489                                 drawgrid_draw(wx, wy, x, y, dx*sublines);
490                         }
491                 }
492                 else {
493                         BIF_ThemeColorBlend(TH_BACK, TH_GRID, dx/60.0);
494                         drawgrid_draw(wx, wy, x, y, dx);
495                         BIF_ThemeColor(TH_GRID);
496                         drawgrid_draw(wx, wy, x, y, dx*sublines);
497                 }
498         }
499
500         x+= (wx); 
501         y+= (wy);
502         BIF_GetThemeColor3ubv(TH_GRID, col);
503
504         setlinestyle(0);
505         
506         /* center cross */
507         if(G.vd->view==3) make_axis_color(col, col2, 'y');
508         else make_axis_color(col, col2, 'x');
509         glColor3ubv((GLubyte *)col2);
510         
511         fdrawline(0.0,  y,  (float)curarea->winx,  y); 
512         
513         if(G.vd->view==7) make_axis_color(col, col2, 'y');
514         else make_axis_color(col, col2, 'z');
515         glColor3ubv((GLubyte *)col2);
516
517         fdrawline(x, 0.0, x, (float)curarea->winy); 
518
519         glDepthMask(1);         // enable write in zbuffer
520         persp(PERSP_VIEW);
521 }
522
523
524
525 static void drawfloor(void)
526 {
527         View3D *vd;
528         float vert[3], grid;
529         int a, gridlines, emphasise;
530         char col[3], col2[3];
531         short draw_line = 0;
532                 
533         vd= curarea->spacedata.first;
534
535         vert[2]= 0.0;
536
537         if(vd->gridlines<3) return;
538         
539         if(G.vd->zbuf && G.obedit) glDepthMask(0);      // for zbuffer-select
540         
541         gridlines= vd->gridlines/2;
542         grid= gridlines*vd->grid;
543         
544         BIF_GetThemeColor3ubv(TH_GRID, col);
545         BIF_GetThemeColor3ubv(TH_BACK, col2);
546         
547         /* emphasise division lines lighter instead of darker, if background is darker than grid */
548         if ( ((col[0]+col[1]+col[2])/3+10) > (col2[0]+col2[1]+col2[2])/3 )
549                 emphasise = 20;
550         else
551                 emphasise = -10;
552         
553         /* draw the Y axis and/or grid lines */
554         for(a= -gridlines;a<=gridlines;a++) {
555                 if(a==0) {
556                         /* check for the 'show Y axis' preference */
557                         if (vd->gridflag & V3D_SHOW_Y) { 
558                                 make_axis_color(col, col2, 'y');
559                                 glColor3ubv((GLubyte *)col2);
560                                 
561                                 draw_line = 1;
562                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
563                                 BIF_ThemeColorShade(TH_GRID, emphasise);
564                         } else {
565                                 draw_line = 0;
566                         }
567                 } else {
568                         /* check for the 'show grid floor' preference */
569                         if (vd->gridflag & V3D_SHOW_FLOOR) {
570                                 if( (a % 10)==0) {
571                                         BIF_ThemeColorShade(TH_GRID, emphasise);
572                                 }
573                                 else BIF_ThemeColorShade(TH_GRID, 10);
574                                 
575                                 draw_line = 1;
576                         } else {
577                                 draw_line = 0;
578                         }
579                 }
580                 
581                 if (draw_line) {
582                         glBegin(GL_LINE_STRIP);
583                 vert[0]= a*vd->grid;
584                 vert[1]= grid;
585                 glVertex3fv(vert);
586                 vert[1]= -grid;
587                 glVertex3fv(vert);
588                         glEnd();
589                 }
590         }
591         
592         /* draw the X axis and/or grid lines */
593         for(a= -gridlines;a<=gridlines;a++) {
594                 if(a==0) {
595                         /* check for the 'show X axis' preference */
596                         if (vd->gridflag & V3D_SHOW_X) { 
597                                 make_axis_color(col, col2, 'x');
598                                 glColor3ubv((GLubyte *)col2);
599                                 
600                                 draw_line = 1;
601                         } else if (vd->gridflag & V3D_SHOW_FLOOR) {
602                                 BIF_ThemeColorShade(TH_GRID, emphasise);
603                         } else {
604                                 draw_line = 0;
605                         }
606                 } else {
607                         /* check for the 'show grid floor' preference */
608                         if (vd->gridflag & V3D_SHOW_FLOOR) {
609                                 if( (a % 10)==0) {
610                                         BIF_ThemeColorShade(TH_GRID, emphasise);
611                                 }
612                                 else BIF_ThemeColorShade(TH_GRID, 10);
613                                 
614                                 draw_line = 1;
615                         } else {
616                                 draw_line = 0;
617                         }
618                 }
619                 
620                 if (draw_line) {
621                         glBegin(GL_LINE_STRIP);
622                 vert[1]= a*vd->grid;
623                 vert[0]= grid;
624                 glVertex3fv(vert );
625                 vert[0]= -grid;
626                 glVertex3fv(vert);
627                         glEnd();
628                 }
629         }
630         
631         /* draw the Z axis line */      
632         /* check for the 'show Z axis' preference */
633         if (vd->gridflag & V3D_SHOW_Z) {
634                 make_axis_color(col, col2, 'z');
635                 glColor3ubv((GLubyte *)col2);
636                 
637                 glBegin(GL_LINE_STRIP);
638                 vert[0]= 0;
639                 vert[1]= 0;
640                 vert[2]= grid;
641                 glVertex3fv(vert );
642                 vert[2]= -grid;
643                 glVertex3fv(vert);
644                 glEnd();
645         }
646
647         if(G.vd->zbuf && G.obedit) glDepthMask(1);      
648
649 }
650
651 static void drawcursor(View3D *v3d)
652 {
653         short mx,my,co[2];
654         int flag;
655         
656         /* we dont want the clipping for cursor */
657         flag= v3d->flag;
658         v3d->flag= 0;
659         project_short( give_cursor(), co);
660         v3d->flag= flag;
661         
662         mx = co[0];
663         my = co[1];
664
665         if(mx!=IS_CLIPPED) {
666                 setlinestyle(0); 
667                 cpack(0xFF);
668                 circ((float)mx, (float)my, 10.0);
669                 setlinestyle(4); 
670                 cpack(0xFFFFFF);
671                 circ((float)mx, (float)my, 10.0);
672                 setlinestyle(0);
673                 cpack(0x0);
674                 
675                 sdrawline(mx-20, my, mx-5, my);
676                 sdrawline(mx+5, my, mx+20, my);
677                 sdrawline(mx, my-20, mx, my-5);
678                 sdrawline(mx, my+5, mx, my+20);
679         }
680 }
681
682 /* ********* custom clipping *********** */
683
684 static void view3d_draw_clipping(View3D *v3d)
685 {
686         BoundBox *bb= v3d->clipbb;
687         
688         BIF_ThemeColorShade(TH_BACK, -8);
689         
690         glBegin(GL_QUADS);
691
692         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[3]);
693         glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[1]);
694         glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[5]);
695         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[3]); glVertex3fv(bb->vec[2]); glVertex3fv(bb->vec[6]);
696         glVertex3fv(bb->vec[1]); glVertex3fv(bb->vec[5]); glVertex3fv(bb->vec[6]); glVertex3fv(bb->vec[2]);
697         glVertex3fv(bb->vec[7]); glVertex3fv(bb->vec[4]); glVertex3fv(bb->vec[0]); glVertex3fv(bb->vec[3]);
698         
699         glEnd();
700 }
701
702 void view3d_set_clipping(View3D *v3d)
703 {
704         double plane[4];
705         int a;
706         
707         for(a=0; a<4; a++) {
708                 QUATCOPY(plane, v3d->clip[a]);
709                 glClipPlane(GL_CLIP_PLANE0+a, plane);
710                 glEnable(GL_CLIP_PLANE0+a);
711         }
712 }
713
714 void view3d_clr_clipping(void)
715 {
716         int a;
717         
718         for(a=0; a<4; a++) {
719                 glDisable(GL_CLIP_PLANE0+a);
720         }
721 }
722
723 int view3d_test_clipping(View3D *v3d, float *vec)
724 {
725         /* vec in world coordinates, returns 1 if clipped */
726         float view[3];
727         
728         VECCOPY(view, vec);
729         
730         if(0.0f < v3d->clip[0][3] + INPR(view, v3d->clip[0]))
731                 if(0.0f < v3d->clip[1][3] + INPR(view, v3d->clip[1]))
732                         if(0.0f < v3d->clip[2][3] + INPR(view, v3d->clip[2]))
733                                 if(0.0f < v3d->clip[3][3] + INPR(view, v3d->clip[3]))
734                                         return 0;
735
736         return 1;
737 }
738
739 /* ********* end custom clipping *********** */
740
741 static void view3d_get_viewborder_size(View3D *v3d, float size_r[2])
742 {
743         float winmax= MAX2(v3d->area->winx, v3d->area->winy);
744         float aspect= (float) (G.scene->r.xsch*G.scene->r.xasp)/(G.scene->r.ysch*G.scene->r.yasp);
745
746         if(aspect>1.0) {
747                 size_r[0]= winmax;
748                 size_r[1]= winmax/aspect;
749         } else {
750                 size_r[0]= winmax*aspect;
751                 size_r[1]= winmax;
752         }
753 }
754
755 void calc_viewborder(struct View3D *v3d, rctf *viewborder_r)
756 {
757         float zoomfac, size[2];
758         float dx= 0.0f, dy= 0.0f;
759         
760         view3d_get_viewborder_size(v3d, size);
761
762                 /* magic zoom calculation, no idea what
763              * it signifies, if you find out, tell me! -zr
764                  */
765         /* simple, its magic dude!
766          * well, to be honest, this gives a natural feeling zooming
767          * with multiple keypad presses (ton)
768          */
769         
770         zoomfac= (M_SQRT2 + v3d->camzoom/50.0);
771         zoomfac= (zoomfac*zoomfac)*0.25;
772         
773         size[0]= size[0]*zoomfac;
774         size[1]= size[1]*zoomfac;
775
776                 /* center in window */
777         viewborder_r->xmin= 0.5*v3d->area->winx - 0.5*size[0];
778         viewborder_r->ymin= 0.5*v3d->area->winy - 0.5*size[1];
779         viewborder_r->xmax= viewborder_r->xmin + size[0];
780         viewborder_r->ymax= viewborder_r->ymin + size[1];
781         
782         dx= v3d->area->winx*G.vd->camdx*zoomfac*2.0f;
783         dy= v3d->area->winy*G.vd->camdy*zoomfac*2.0f;
784         
785         /* apply offset */
786         viewborder_r->xmin-= dx;
787         viewborder_r->ymin-= dy;
788         viewborder_r->xmax-= dx;
789         viewborder_r->ymax-= dy;
790         
791         if(v3d->camera && v3d->camera->type==OB_CAMERA) {
792                 Camera *cam= v3d->camera->data;
793                 float w = viewborder_r->xmax - viewborder_r->xmin;
794                 float h = viewborder_r->ymax - viewborder_r->ymin;
795                 float side = MAX2(w, h);
796
797                 viewborder_r->xmin+= cam->shiftx*side;
798                 viewborder_r->xmax+= cam->shiftx*side;
799                 viewborder_r->ymin+= cam->shifty*side;
800                 viewborder_r->ymax+= cam->shifty*side;
801         }
802 }
803
804 void view3d_set_1_to_1_viewborder(View3D *v3d)
805 {
806         float size[2];
807         int im_width= (G.scene->r.size*G.scene->r.xsch)/100;
808
809         view3d_get_viewborder_size(v3d, size);
810
811         v3d->camzoom= (sqrt(4.0*im_width/size[0]) - M_SQRT2)*50.0;
812         v3d->camzoom= CLAMPIS(v3d->camzoom, -30, 300);
813 }
814
815
816 static void drawviewborder_flymode(void)        
817 {
818         /* draws 4 edge brackets that frame the safe area where the
819         mouse can move during fly mode without spinning the view */
820         float x1, x2, y1, y2;
821         
822         x1= 0.45*(float)curarea->winx;
823         y1= 0.45*(float)curarea->winy;
824         x2= 0.55*(float)curarea->winx;
825         y2= 0.55*(float)curarea->winy;
826         cpack(0);
827         
828         
829         glBegin(GL_LINES);
830         /* bottom left */
831         glVertex2f(x1,y1); 
832         glVertex2f(x1,y1+5);
833         
834         glVertex2f(x1,y1); 
835         glVertex2f(x1+5,y1);
836
837         /* top right */
838         glVertex2f(x2,y2); 
839         glVertex2f(x2,y2-5);
840         
841         glVertex2f(x2,y2); 
842         glVertex2f(x2-5,y2);
843         
844         /* top left */
845         glVertex2f(x1,y2); 
846         glVertex2f(x1,y2-5);
847         
848         glVertex2f(x1,y2); 
849         glVertex2f(x1+5,y2);
850         
851         /* bottom right */
852         glVertex2f(x2,y1); 
853         glVertex2f(x2,y1+5);
854         
855         glVertex2f(x2,y1); 
856         glVertex2f(x2-5,y1);
857         glEnd();        
858 }
859
860
861 static void drawviewborder(void)
862 {
863         extern void gl_round_box(int mode, float minx, float miny, float maxx, float maxy, float rad);          // interface_panel.c
864         float fac, a;
865         float x1, x2, y1, y2;
866         float x3, y3, x4, y4;
867         rctf viewborder;
868         Camera *ca= NULL;
869
870         if(G.vd->camera==NULL)
871                 return;
872         if(G.vd->camera->type==OB_CAMERA)
873                 ca = G.vd->camera->data;
874         
875         calc_viewborder(G.vd, &viewborder);
876         x1= viewborder.xmin;
877         y1= viewborder.ymin;
878         x2= viewborder.xmax;
879         y2= viewborder.ymax;
880
881         /* passepartout, specified in camera edit buttons */
882         if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001) {
883                 if (ca->passepartalpha == 1.0) {
884                         glColor3f(0, 0, 0);
885                 } else {
886                         glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
887                         glEnable(GL_BLEND);
888                         glColor4f(0, 0, 0, ca->passepartalpha);
889                 }
890                 if (x1 > 0.0)
891                         glRectf(0.0, (float)curarea->winy, x1, 0.0);
892                 if (x2 < (float)curarea->winx)
893                         glRectf(x2, (float)curarea->winy, (float)curarea->winx, 0.0);
894                 if (y2 < (float)curarea->winy)
895                         glRectf(x1, (float)curarea->winy, x2, y2);
896                 if (y2 > 0.0) 
897                         glRectf(x1, y1, x2, 0.0);
898
899                 glDisable(GL_BLEND);
900         }
901         
902         /* edge */
903         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
904         
905         setlinestyle(0);
906         BIF_ThemeColor(TH_BACK);
907         glRectf(x1, y1, x2, y2);
908         
909         setlinestyle(3);
910         BIF_ThemeColor(TH_WIRE);
911         glRectf(x1, y1, x2, y2);
912                 
913         /* camera name - draw in highlighted text color */
914         if (ca && (ca->flag & CAM_SHOWNAME)) {
915                 BIF_ThemeColor(TH_TEXT_HI);
916                 glRasterPos2f(x1, y1-15);
917                 
918                 BMF_DrawString(G.font, G.vd->camera->id.name+2);
919                 BIF_ThemeColor(TH_WIRE);
920         }
921
922
923         /* border */
924         if(G.scene->r.mode & R_BORDER) {
925                 
926                 cpack(0);
927                 x3= x1+ G.scene->r.border.xmin*(x2-x1);
928                 y3= y1+ G.scene->r.border.ymin*(y2-y1);
929                 x4= x1+ G.scene->r.border.xmax*(x2-x1);
930                 y4= y1+ G.scene->r.border.ymax*(y2-y1);
931                 
932                 cpack(0x4040FF);
933                 glRectf(x3,  y3,  x4,  y4); 
934         }
935
936         /* safety border */
937         if (ca && (ca->flag & CAM_SHOWTITLESAFE)) {
938                 fac= 0.1;
939                 
940                 a= fac*(x2-x1);
941                 x1+= a; 
942                 x2-= a;
943         
944                 a= fac*(y2-y1);
945                 y1+= a;
946                 y2-= a;
947         
948                 BIF_ThemeColorBlendShade(TH_WIRE, TH_BACK, 0.25, 0);
949                 
950                 uiSetRoundBox(15);
951                 gl_round_box(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
952         }
953         
954         setlinestyle(0);
955         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
956
957 }
958
959 void backdrawview3d(int test)
960 {
961         struct Base *base;
962
963 /*for 2.43 release, don't use glext and just define the constant.
964   this to avoid possibly breaking platforms before release.*/
965 #ifndef GL_MULTISAMPLE_ARB
966         #define GL_MULTISAMPLE_ARB      0x809D
967 #endif
968
969 #ifdef GL_MULTISAMPLE_ARB
970         int m;
971 #endif
972
973         if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT || G.f & G_TEXTUREPAINT);
974         else if(G.obedit && G.vd->drawtype>OB_WIRE && (G.vd->flag & V3D_ZBUF_SELECT));
975         else {
976                 G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
977                 return;
978         }
979
980         if( !(G.vd->flag & V3D_NEEDBACKBUFDRAW) ) return;
981
982         if(test) {
983                 if(qtest()) {
984                         addafterqueue(curarea->win, BACKBUFDRAW, 1);
985                         return;
986                 }
987         }
988         persp(PERSP_VIEW);
989
990         /*Disable FSAA for backbuffer selection.  
991         
992         Only works if GL_MULTISAMPLE_ARB is defined by the header
993         file, which is should be for every OS that supports FSAA.*/
994
995 #ifdef GL_MULTISAMPLE_ARB
996         m = glIsEnabled(GL_MULTISAMPLE_ARB);
997         if (m) glDisable(GL_MULTISAMPLE_ARB);
998 #endif
999
1000 #ifdef __APPLE__
1001         glDrawBuffer(GL_AUX0);
1002 #endif  
1003         if(G.vd->drawtype > OB_WIRE) G.vd->zbuf= TRUE;
1004         curarea->win_swap &= ~WIN_BACK_OK;
1005         
1006         glDisable(GL_DITHER);
1007
1008         glClearColor(0.0, 0.0, 0.0, 0.0); 
1009         if(G.vd->zbuf) {
1010                 glEnable(GL_DEPTH_TEST);
1011                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1012         }
1013         else {
1014                 glClear(GL_COLOR_BUFFER_BIT);
1015                 glDisable(GL_DEPTH_TEST);
1016         }
1017         
1018         if(G.vd->flag & V3D_CLIPPING)
1019                 view3d_set_clipping(G.vd);
1020         
1021         G.f |= G_BACKBUFSEL;
1022         
1023         base= (G.scene->basact);
1024         if(base && (base->lay & G.vd->lay)) {
1025                 draw_object_backbufsel(base->object);
1026         }
1027
1028         G.vd->flag &= ~V3D_NEEDBACKBUFDRAW;
1029
1030         G.f &= ~G_BACKBUFSEL;
1031         G.vd->zbuf= FALSE; 
1032         glDisable(GL_DEPTH_TEST);
1033         glEnable(GL_DITHER);
1034
1035 #ifdef __APPLE__
1036         glDrawBuffer(GL_BACK); /* we were in aux buffers */
1037 #endif
1038
1039         if(G.vd->flag & V3D_CLIPPING)
1040                 view3d_clr_clipping();
1041
1042 #ifdef GL_MULTISAMPLE_ARB
1043         if (m) glEnable(GL_MULTISAMPLE_ARB);
1044 #endif
1045
1046         /* it is important to end a view in a transform compatible with buttons */
1047         persp(PERSP_WIN);  // set ortho
1048         bwin_scalematrix(curarea->win, G.vd->blockscale, G.vd->blockscale, G.vd->blockscale);
1049
1050 }
1051
1052 void check_backbuf(void)
1053 {
1054         if(G.vd->flag & V3D_NEEDBACKBUFDRAW)
1055                 backdrawview3d(0);
1056 }
1057
1058 /* samples a single pixel (copied from vpaint) */
1059 unsigned int sample_backbuf(int x, int y)
1060 {
1061         unsigned int col;
1062         
1063         if(x>=curarea->winx || y>=curarea->winy) return 0;
1064         x+= curarea->winrct.xmin;
1065         y+= curarea->winrct.ymin;
1066         
1067         check_backbuf(); // actually not needed for apple
1068
1069 #ifdef __APPLE__
1070         glReadBuffer(GL_AUX0);
1071 #endif
1072         glReadPixels(x,  y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE,  &col);
1073         glReadBuffer(GL_BACK);  
1074         
1075         if(G.order==B_ENDIAN) SWITCH_INT(col);
1076         
1077         return framebuffer_to_index(col);
1078 }
1079
1080 /* reads full rect, converts indices */
1081 ImBuf *read_backbuf(short xmin, short ymin, short xmax, short ymax)
1082 {
1083         unsigned int *dr, *rd;
1084         struct ImBuf *ibuf, *ibuf1;
1085         int a;
1086         short xminc, yminc, xmaxc, ymaxc, xs, ys;
1087         
1088         /* clip */
1089         if(xmin<0) xminc= 0; else xminc= xmin;
1090         if(xmax>=curarea->winx) xmaxc= curarea->winx-1; else xmaxc= xmax;
1091         if(xminc > xmaxc) return NULL;
1092
1093         if(ymin<0) yminc= 0; else yminc= ymin;
1094         if(ymax>=curarea->winy) ymaxc= curarea->winy-1; else ymaxc= ymax;
1095         if(yminc > ymaxc) return NULL;
1096         
1097         ibuf= IMB_allocImBuf((xmaxc-xminc+1), (ymaxc-yminc+1), 32, IB_rect,0);
1098
1099         check_backbuf(); // actually not needed for apple
1100         
1101 #ifdef __APPLE__
1102         glReadBuffer(GL_AUX0);
1103 #endif
1104         glReadPixels(curarea->winrct.xmin+xminc, curarea->winrct.ymin+yminc, (xmaxc-xminc+1), (ymaxc-yminc+1), GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
1105         glReadBuffer(GL_BACK);  
1106
1107         if(G.order==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1108
1109         a= (xmaxc-xminc+1)*(ymaxc-yminc+1);
1110         dr= ibuf->rect;
1111         while(a--) {
1112                 if(*dr) *dr= framebuffer_to_index(*dr);
1113                 dr++;
1114         }
1115         
1116         /* put clipped result back, if needed */
1117         if(xminc==xmin && xmaxc==xmax && yminc==ymin && ymaxc==ymax) 
1118                 return ibuf;
1119         
1120         ibuf1= IMB_allocImBuf( (xmax-xmin+1),(ymax-ymin+1),32,IB_rect,0);
1121         rd= ibuf->rect;
1122         dr= ibuf1->rect;
1123                 
1124         for(ys= ymin; ys<=ymax; ys++) {
1125                 for(xs= xmin; xs<=xmax; xs++, dr++) {
1126                         if( xs>=xminc && xs<=xmaxc && ys>=yminc && ys<=ymaxc) {
1127                                 *dr= *rd;
1128                                 rd++;
1129                         }
1130                 }
1131         }
1132         IMB_freeImBuf(ibuf);
1133         return ibuf1;
1134 }
1135
1136 /* smart function to sample a rect spiralling outside, nice for backbuf selection */
1137 unsigned int sample_backbuf_rect(short mval[2], int size, unsigned int min, unsigned int max, int *dist, short strict, unsigned int (*indextest)(unsigned int index))
1138 {
1139         struct ImBuf *buf;
1140         unsigned int *bufmin, *bufmax, *tbuf;
1141         int minx, miny;
1142         int a, b, rc, nr, amount, dirvec[4][2];
1143         int distance=0;
1144         unsigned int index = 0;
1145         short indexok = 0;      
1146
1147         amount= (size-1)/2;
1148
1149         minx = mval[0]-(amount+1);
1150         miny = mval[1]-(amount+1);
1151         buf = read_backbuf(minx, miny, minx+size-1, miny+size-1);
1152         if (!buf) return 0;
1153
1154         rc= 0;
1155         
1156         dirvec[0][0]= 1; dirvec[0][1]= 0;
1157         dirvec[1][0]= 0; dirvec[1][1]= -size;
1158         dirvec[2][0]= -1; dirvec[2][1]= 0;
1159         dirvec[3][0]= 0; dirvec[3][1]= size;
1160         
1161         bufmin = buf->rect;
1162         tbuf = buf->rect;
1163         bufmax = buf->rect + size*size;
1164         tbuf+= amount*size+ amount;
1165         
1166         for(nr=1; nr<=size; nr++) {
1167                 
1168                 for(a=0; a<2; a++) {
1169                         for(b=0; b<nr; b++, distance++) {
1170                                 if (*tbuf && *tbuf>=min && *tbuf<max) { //we got a hit
1171                                         if(strict){
1172                                                 indexok =  indextest(*tbuf - min+1);
1173                                                 if(indexok){
1174                                                         *dist= (short) sqrt( (float)distance   );
1175                                                         index = *tbuf - min+1;
1176                                                         goto exit; 
1177                                                 }                                               
1178                                         }
1179                                         else{
1180                                                 *dist= (short) sqrt( (float)distance ); // XXX, this distance is wrong - 
1181                                                 index = *tbuf - min+1; // messy yah, but indices start at 1
1182                                                 goto exit;
1183                                         }                       
1184                                 }
1185                                 
1186                                 tbuf+= (dirvec[rc][0]+dirvec[rc][1]);
1187                                 
1188                                 if(tbuf<bufmin || tbuf>=bufmax) {
1189                                         goto exit;
1190                                 }
1191                         }
1192                         rc++;
1193                         rc &= 3;
1194                 }
1195         }
1196
1197 exit:
1198         IMB_freeImBuf(buf);
1199         return index;
1200 }
1201
1202 static void drawname(Object *ob)
1203 {
1204         cpack(0x404040);
1205         glRasterPos3f(0.0,  0.0,  0.0);
1206         
1207         BMF_DrawString(G.font, " ");
1208         BMF_DrawString(G.font, ob->id.name+2);
1209 }
1210
1211 static char *get_cfra_marker_name()
1212 {
1213         ListBase *markers= &G.scene->markers;
1214         TimeMarker *m1, *m2;
1215         
1216         /* search through markers for match */
1217         for (m1=markers->first, m2=markers->last; m1 && m2; m1=m1->next, m2=m2->prev) {
1218                 if (m1->frame==CFRA)
1219                         return m1->name;
1220                 if (m2->frame==CFRA)
1221                         return m2->name;
1222                 
1223                 if (m1 == m2)
1224                         break;
1225         }
1226         
1227         return NULL;
1228 }
1229
1230 // TODO: move this func into some keyframing API
1231 short ob_cfra_has_keyframe (Object *ob)
1232 {
1233         // fixme... this is slow!
1234         if (ob) {
1235                 ListBase keys = {NULL, NULL};
1236                 ActKeyColumn *ak, *akn;
1237                 Key *key= ob_get_key(ob);
1238                 int cfra, found= 0;
1239                 
1240                 /* check active action */
1241                 if (ob->action) {
1242                         /* get keyframes of action */
1243                         action_to_keylist(ob->action, &keys, NULL, NULL);
1244                         
1245                         cfra= frame_to_float(CFRA);
1246                         cfra= get_action_frame(ob, cfra);
1247                         
1248                         /* check if a keyframe occurs on current frame */
1249                         for (ak=keys.first, akn=keys.last; ak && akn; ak=ak->next, akn=akn->prev) {
1250                                 if (cfra == ak->cfra) {
1251                                         found= 1;
1252                                         break;
1253                                 }
1254                                 else if (cfra == akn->cfra) {
1255                                         found= 1;
1256                                         break;
1257                                 }
1258                                 
1259                                 if (ak == akn)
1260                                         break;
1261                         }
1262                         
1263                         /* free temp list */
1264                         BLI_freelistN(&keys);
1265                         keys.first= keys.last= NULL;
1266                         
1267                         /* return if found */
1268                         if (found) return 1;
1269                 }
1270                 
1271                 /* accumulate keyframes for available ipo's */
1272                 if (ob->ipo)
1273                         ipo_to_keylist(ob->ipo, &keys, NULL, NULL);
1274                 if (key)
1275                         ipo_to_keylist(key->ipo, &keys, NULL, NULL);
1276                 
1277                 if (keys.first) {
1278                         cfra= frame_to_float(CFRA);
1279                         found= 0;
1280                                 
1281                         /* check if a keyframe occurs on current frame */
1282                         for (ak=keys.first, akn=keys.last; ak && akn; ak=ak->next, akn=akn->prev) {
1283                                 if (IS_EQ(cfra, ak->cfra)) {
1284                                         found= 1;
1285                                         break;
1286                                 }
1287                                 else if (IS_EQ(cfra, akn->cfra)) {
1288                                         found= 1;
1289                                         break;
1290                                 }
1291                                 
1292                                 if (ak == akn)
1293                                         break;
1294                         }
1295                         
1296                         /* free temp list */
1297                         BLI_freelistN(&keys);
1298                         keys.first= keys.last= NULL;
1299                         
1300                         /* return if found */
1301                         if (found) return 1;
1302                 }
1303         }
1304         
1305         /* couldn't find a keyframe */
1306         return 0;
1307 }
1308
1309 /* draw info beside axes in bottom left-corner: 
1310  *      framenum, object name, bone name (if available), marker name (if available)
1311  */
1312 static void draw_selected_name(Object *ob)
1313 {
1314         char info[256], *markern;
1315         short offset=30;
1316         
1317         /* get name of marker on current frame (if available) */
1318         markern= get_cfra_marker_name();
1319         
1320         /* check if there is an object */
1321         if(ob) {
1322                 /* name(s) to display depends on type of object */
1323                 if(ob->type==OB_ARMATURE) {
1324                         bArmature *arm= ob->data;
1325                         char *name= NULL;
1326                         
1327                         /* show name of active bone too (if possible) */
1328                         if(ob==G.obedit) {
1329                                 EditBone *ebo;
1330                                 for (ebo=G.edbo.first; ebo; ebo=ebo->next){
1331                                         if ((ebo->flag & BONE_ACTIVE) && (ebo->layer & arm->layer)) {
1332                                                 name= ebo->name;
1333                                                 break;
1334                                         }
1335                                 }
1336                         }
1337                         else if(ob->pose && (ob->flag & OB_POSEMODE)) {
1338                                 bPoseChannel *pchan;
1339                                 for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1340                                         if((pchan->bone->flag & BONE_ACTIVE) && (pchan->bone->layer & arm->layer)) {
1341                                                 name= pchan->name;
1342                                                 break;
1343                                         }
1344                                 }
1345                         }
1346                         if(name && markern)
1347                                 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, name, markern);
1348                         else if(name)
1349                                 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, name);
1350                         else
1351                                 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1352                 }
1353                 else if(ELEM3(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
1354                         Key *key= NULL;
1355                         KeyBlock *kb = NULL;
1356                         char shapes[75];
1357                         
1358                         /* try to display active shapekey too */
1359                         shapes[0] = 0;
1360                         key = ob_get_key(ob);
1361                         if(key){
1362                                 kb = BLI_findlink(&key->block, ob->shapenr-1);
1363                                 if(kb){
1364                                         sprintf(shapes, ": %s ", kb->name);             
1365                                         if(ob->shapeflag == OB_SHAPE_LOCK){
1366                                                 sprintf(shapes, "%s (Pinned)",shapes);
1367                                         }
1368                                 }
1369                         }
1370                         
1371                         if(markern)
1372                                 sprintf(info, "(%d) %s %s <%s>", CFRA, ob->id.name+2, shapes, markern);
1373                         else
1374                                 sprintf(info, "(%d) %s %s", CFRA, ob->id.name+2, shapes);
1375                 }
1376                 else {
1377                         /* standard object */
1378                         if (markern)
1379                                 sprintf(info, "(%d) %s <%s>", CFRA, ob->id.name+2, markern);
1380                         else
1381                                 sprintf(info, "(%d) %s", CFRA, ob->id.name+2);
1382                 }
1383                         
1384                 /* colour depends on whether there is a keyframe */
1385                 if (ob_cfra_has_keyframe(ob))
1386                         BIF_ThemeColor(TH_VERTEX_SELECT);
1387                 else
1388                         BIF_ThemeColor(TH_TEXT_HI);
1389         }
1390         else {
1391                 /* no object */
1392                 if (markern)
1393                         sprintf(info, "(%d) <%s>", CFRA, markern);
1394                 else
1395                         sprintf(info, "(%d)", CFRA);
1396                 
1397                 /* colour is always white */
1398                 BIF_ThemeColor(TH_TEXT_HI);
1399         }
1400         
1401         if (U.uiflag & USER_SHOW_ROTVIEWICON)
1402                 offset = 14 + (U.rvisize * 2);
1403
1404         glRasterPos2i(offset,  10);
1405         BMF_DrawString(G.fonts, info);
1406 }
1407
1408
1409 /* Draw a live substitute of the view icon, which is always shown */
1410 static void draw_view_axis(void)
1411 {
1412         const float k = U.rvisize;   /* axis size */
1413         const float toll = 0.5;      /* used to see when view is quasi-orthogonal */
1414         const float start = k + 1.0; /* axis center in screen coordinates, x=y */
1415         float ydisp = 0.0;          /* vertical displacement to allow obj info text */
1416         
1417         /* rvibright ranges approx. from original axis icon color to gizmo color */
1418         float bright = U.rvibright / 15.0f;
1419         
1420         unsigned char col[3];
1421         unsigned char gridcol[3];
1422         float colf[3];
1423         
1424         float vec[4];
1425         float dx, dy;
1426         float h, s, v;
1427         
1428         /* thickness of lines is proportional to k */
1429         /*      (log(k)-1) gives a more suitable thickness, but fps decreased by about 3 fps */
1430         glLineWidth(k / 10);
1431         //glLineWidth(log(k)-1); // a bit slow
1432         
1433         BIF_GetThemeColor3ubv(TH_GRID, (char *)gridcol);
1434         
1435         /* X */
1436         vec[0] = vec[3] = 1;
1437         vec[1] = vec[2] = 0;
1438         QuatMulVecf(G.vd->viewquat, vec);
1439         
1440         make_axis_color((char *)gridcol, (char *)col, 'x');
1441         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
1442         s = s<0.5 ? s+0.5 : 1.0;
1443         v = 0.3;
1444         v = (v<1.0-(bright) ? v+bright : 1.0);
1445         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
1446         glColor3fv(colf);
1447         
1448         dx = vec[0] * k;
1449         dy = vec[1] * k;
1450         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
1451         if (fabs(dx) > toll || fabs(dy) > toll) {
1452                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
1453                 BMF_DrawString(G.fonts, "x");
1454         }
1455         
1456         /* Y */
1457         vec[1] = vec[3] = 1;
1458         vec[0] = vec[2] = 0;
1459         QuatMulVecf(G.vd->viewquat, vec);
1460         
1461         make_axis_color((char *)gridcol, (char *)col, 'y');
1462         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
1463         s = s<0.5 ? s+0.5 : 1.0;
1464         v = 0.3;
1465         v = (v<1.0-(bright) ? v+bright : 1.0);
1466         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
1467         glColor3fv(colf);
1468         
1469         dx = vec[0] * k;
1470         dy = vec[1] * k;
1471         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
1472         if (fabs(dx) > toll || fabs(dy) > toll) {
1473                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
1474                 BMF_DrawString(G.fonts, "y");
1475         }
1476         
1477         /* Z */
1478         vec[2] = vec[3] = 1;
1479         vec[1] = vec[0] = 0;
1480         QuatMulVecf(G.vd->viewquat, vec);
1481         
1482         make_axis_color((char *)gridcol, (char *)col, 'z');
1483         rgb_to_hsv(col[0]/255.0f, col[1]/255.0f, col[2]/255.0f, &h, &s, &v);
1484         s = s<0.5 ? s+0.5 : 1.0;
1485         v = 0.5;
1486         v = (v<1.0-(bright) ? v+bright : 1.0);
1487         hsv_to_rgb(h, s, v, colf, colf+1, colf+2);
1488         glColor3fv(colf);
1489         
1490         dx = vec[0] * k;
1491         dy = vec[1] * k;
1492         fdrawline(start, start + ydisp, start + dx, start + dy + ydisp);
1493         if (fabs(dx) > toll || fabs(dy) > toll) {
1494                 glRasterPos2i(start + dx + 2, start + dy + ydisp + 2);
1495                 BMF_DrawString(G.fonts, "z");
1496         }
1497         
1498         /* restore line-width */
1499         glLineWidth(1.0);
1500 }
1501
1502         
1503 static void draw_view_icon(void)
1504 {
1505         BIFIconID icon;
1506         
1507         if(G.vd->view==7) icon= ICON_AXIS_TOP;
1508         else if(G.vd->view==1) icon= ICON_AXIS_FRONT;
1509         else if(G.vd->view==3) icon= ICON_AXIS_SIDE;
1510         else return ;
1511
1512         glEnable(GL_BLEND);
1513         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
1514         
1515         BIF_icon_draw(5.0, 5.0, icon);
1516         
1517         glDisable(GL_BLEND);
1518 }
1519
1520 char *view3d_get_name(View3D *v3d)
1521 {
1522         char *name = NULL;
1523         
1524         switch (v3d->view) {
1525                 case 1:
1526                         if (v3d->persp == V3D_ORTHO)
1527                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Back Ortho" : "Front Ortho";
1528                         else
1529                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Back Persp" : "Front Persp";
1530                         break;
1531                 case 3:
1532                         if (v3d->persp == V3D_ORTHO)
1533                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Left Ortho" : "Right Ortho";
1534                         else
1535                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Left Persp" : "Right Persp";
1536                         break;
1537                 case 7:
1538                         if (v3d->persp == V3D_ORTHO)
1539                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Bottom Ortho" : "Top Ortho";
1540                         else
1541                                 name = (v3d->flag2 & V3D_OPP_DIRECTION_NAME) ? "Bottom Persp" : "Top Persp";
1542                         break;
1543                 default:
1544                         if (v3d->persp==V3D_CAMOB) {
1545                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
1546                                         Camera *cam;
1547                                         cam = v3d->camera->data;
1548                                         name = (cam->type != CAM_ORTHO) ? "Camera Persp" : "Camera Ortho";
1549                                 } else {
1550                                         name = "Object as Camera";
1551                                 }
1552                         } else { 
1553                                 name = (v3d->persp == V3D_ORTHO) ? "User Ortho" : "User Persp";
1554                         }
1555                         break;
1556         }
1557         
1558         return name;
1559 }
1560
1561 static void draw_viewport_name(ScrArea *sa)
1562 {
1563         char *name = view3d_get_name(sa->spacedata.first);
1564         char *printable = NULL;
1565         
1566         if (G.vd->localview) {
1567                 printable = malloc(strlen(name) + strlen(" (Local)_")); /* '_' gives space for '\0' */
1568                 strcpy(printable, name);
1569                 strcat(printable, " (Local)");
1570         } else {
1571                 printable = name;
1572         }
1573
1574         if (printable) {
1575                 BIF_ThemeColor(TH_TEXT_HI);
1576                 glRasterPos2i(10,  sa->winy-20);
1577                 BMF_DrawString(G.fonts, printable);
1578         }
1579
1580         if (G.vd->localview) {
1581                 free(printable);
1582         }
1583 }
1584
1585 /* ******************* view3d space & buttons ************** */
1586
1587
1588 /* temporal struct for storing transform properties */
1589 typedef struct {
1590         float ob_eul[4];        // used for quat too....
1591         float ob_scale[3]; // need temp space due to linked values
1592         float ob_dims[3];
1593         short link_scale;
1594         float ve_median[5];
1595         int curdef;
1596         float *defweightp;
1597 } TransformProperties;
1598
1599 /* is used for both read and write... */
1600 static void v3d_editvertex_buts(uiBlock *block, Object *ob, float lim)
1601 {
1602         EditMesh *em = G.editMesh;
1603         EditVert *eve, *evedef=NULL;
1604         EditEdge *eed;
1605         MDeformVert *dvert=NULL;
1606         TransformProperties *tfp= G.vd->properties_storage;
1607         float median[5], ve_median[5];
1608         int tot, totw, totweight, totedge;
1609         char defstr[320];
1610         
1611         median[0]= median[1]= median[2]= median[3]= median[4]= 0.0;
1612         tot= totw= totweight= totedge= 0;
1613         defstr[0]= 0;
1614
1615         if(ob->type==OB_MESH) {         
1616                 eve= em->verts.first;
1617                 while(eve) {
1618                         if(eve->f & SELECT) {
1619                                 evedef= eve;
1620                                 tot++;
1621                                 VecAddf(median, median, eve->co);
1622                         }
1623                         eve= eve->next;
1624                 }
1625                 eed= em->edges.first;
1626                 while(eed) {
1627                         if((eed->f & SELECT)) {
1628                                 totedge++;
1629                                 median[3]+= eed->crease;
1630                         }
1631                         eed= eed->next;
1632                 }
1633
1634                 /* check for defgroups */
1635                 if(evedef)
1636                         dvert= CustomData_em_get(&em->vdata, evedef->data, CD_MDEFORMVERT);
1637                 if(tot==1 && dvert && dvert->totweight) {
1638                         bDeformGroup *dg;
1639                         int i, max=1, init=1;
1640                         char str[320];
1641                         
1642                         for (i=0; i<dvert->totweight; i++){
1643                                 dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
1644                                 if(dg) {
1645                                         max+= snprintf(str, sizeof(str), "%s %%x%d|", dg->name, dvert->dw[i].def_nr); 
1646                                         if(max<320) strcat(defstr, str);
1647                                 }
1648                                 else printf("oh no!\n");
1649                                 if(tfp->curdef==dvert->dw[i].def_nr) {
1650                                         init= 0;
1651                                         tfp->defweightp= &dvert->dw[i].weight;
1652                                 }
1653                         }
1654                         
1655                         if(init) {      // needs new initialized 
1656                                 tfp->curdef= dvert->dw[0].def_nr;
1657                                 tfp->defweightp= &dvert->dw[0].weight;
1658                         }
1659                 }
1660         }
1661         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1662                 extern ListBase editNurb; /* editcurve.c */
1663                 Nurb *nu;
1664                 BPoint *bp;
1665                 BezTriple *bezt;
1666                 int a;
1667                 
1668                 nu= editNurb.first;
1669                 while(nu) {
1670                         if((nu->type & 7)==CU_BEZIER) {
1671                                 bezt= nu->bezt;
1672                                 a= nu->pntsu;
1673                                 while(a--) {
1674                                         if(bezt->f2 & SELECT) {
1675                                                 VecAddf(median, median, bezt->vec[1]);
1676                                                 tot++;
1677                                                 median[4]+= bezt->weight;
1678                                                 totweight++;
1679                                         }
1680                                         else {
1681                                                 if(bezt->f1 & SELECT) {
1682                                                         VecAddf(median, median, bezt->vec[0]);
1683                                                         tot++;
1684                                                 }
1685                                                 if(bezt->f3 & SELECT) {
1686                                                         VecAddf(median, median, bezt->vec[2]);
1687                                                         tot++;
1688                                                 }
1689                                         }
1690                                         bezt++;
1691                                 }
1692                         }
1693                         else {
1694                                 bp= nu->bp;
1695                                 a= nu->pntsu*nu->pntsv;
1696                                 while(a--) {
1697                                         if(bp->f1 & SELECT) {
1698                                                 VecAddf(median, median, bp->vec);
1699                                                 median[3]+= bp->vec[3];
1700                                                 totw++;
1701                                                 tot++;
1702                                                 median[4]+= bp->weight;
1703                                                 totweight++;
1704                                         }
1705                                         bp++;
1706                                 }
1707                         }
1708                         nu= nu->next;
1709                 }
1710         }
1711         else if(ob->type==OB_LATTICE) {
1712                 BPoint *bp;
1713                 int a;
1714                 
1715                 a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1716                 bp= editLatt->def;
1717                 while(a--) {
1718                         if(bp->f1 & SELECT) {
1719                                 VecAddf(median, median, bp->vec);
1720                                 tot++;
1721                                 median[4]+= bp->weight;
1722                                 totweight++;
1723                         }
1724                         bp++;
1725                 }
1726         }
1727         
1728         if(tot==0) return;
1729
1730         median[0] /= (float)tot;
1731         median[1] /= (float)tot;
1732         median[2] /= (float)tot;
1733         if(totedge) median[3] /= (float)totedge;
1734         else if(totw) median[3] /= (float)totw;
1735         if(totweight) median[4] /= (float)totweight;
1736         
1737         if(G.vd->flag & V3D_GLOBAL_STATS)
1738                 Mat4MulVecfl(ob->obmat, median);
1739         
1740         if(block) {     // buttons
1741                 int but_y;
1742                 if((ob->parent) && (ob->partype == PARBONE))    but_y = 135;
1743                 else                                                                                    but_y = 150;
1744                 
1745                 uiBlockBeginAlign(block);
1746                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Global",              160, but_y, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays global values");
1747                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, REDRAWVIEW3D, "Local",              230, but_y, 70, 19, &G.vd->flag, 0, 0, 0, 0, "Displays local values");
1748                 uiBlockEndAlign(block);
1749                 
1750                 memcpy(tfp->ve_median, median, sizeof(tfp->ve_median));
1751                 
1752                 uiBlockBeginAlign(block);
1753                 if(tot==1) {
1754                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex X:", 10, 110, 290, 19, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
1755                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Y:", 10, 90, 290, 19, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
1756                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex Z:", 10, 70, 290, 19, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
1757                         if(totw==1)
1758                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Vertex W:", 10, 50, 290, 19, &(tfp->ve_median[3]), 0.01, 100.0, 10, 3, "");
1759                         uiBlockEndAlign(block);
1760         
1761                         if(defstr[0]) {
1762                                 uiDefBut(block, LABEL, 1, "Vertex Deform Groups",               10, 40, 290, 20, NULL, 0.0, 0.0, 0, 0, "");
1763
1764                                 uiBlockBeginAlign(block);
1765                                 uiDefButF(block, NUM, B_NOP, "Weight:",                 10, 20, 150, 19, tfp->defweightp, 0.0f, 1.0f, 10, 3, "Weight value");
1766                                 uiDefButI(block, MENU, REDRAWVIEW3D, defstr,    160, 20, 140, 19, &tfp->curdef, 0.0, 0.0, 0, 0, "Current Vertex Group");
1767                                 uiBlockEndAlign(block);
1768                         }
1769                         else if(totweight)
1770                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "");
1771
1772                 }
1773                 else {
1774                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median X:", 10, 110, 290, 19, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
1775                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Y:", 10, 90, 290, 19, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
1776                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Z:", 10, 70, 290, 19, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
1777                         if(totw==tot)
1778                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median W:", 10, 50, 290, 19, &(tfp->ve_median[3]), 0.01, 100.0, 10, 3, "");
1779                         uiBlockEndAlign(block);
1780                         if(totweight)
1781                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   10, 20, 290, 19, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
1782                 }
1783                 
1784                 if(ob->type==OB_CURVE && (totw==0)) { /* bez curves have no w */
1785                         uiBlockBeginAlign(block);
1786                         uiDefBut(block, BUT,B_SETPT_AUTO,"Auto",        10, 44, 72, 19, 0, 0, 0, 0, 0, "Auto handles (Shift H)");
1787                         uiDefBut(block, BUT,B_SETPT_VECTOR,"Vector",82, 44, 73, 19, 0, 0, 0, 0, 0, "Vector handles (V)");
1788                         uiDefBut(block, BUT,B_SETPT_ALIGN,"Align",155, 44, 73, 19, 0, 0, 0, 0, 0, "Align handles (H Toggles)");
1789                         uiDefBut(block, BUT,B_SETPT_FREE,"Free",        227, 44, 72, 19, 0, 0, 0, 0, 0, "Align handles (H Toggles)");
1790                         uiBlockEndAlign(block);
1791                 }
1792                 
1793                 if(totedge==1)
1794                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Crease W:", 10, 30, 290, 19, &(tfp->ve_median[3]), 0.0, 1.0, 10, 3, "");
1795                 else if(totedge>1)
1796                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Crease W:",  10, 30, 290, 19, &(tfp->ve_median[3]), 0.0, 1.0, 10, 3, "");
1797                 
1798         }
1799         else {  // apply
1800                 memcpy(ve_median, tfp->ve_median, sizeof(tfp->ve_median));
1801                 
1802                 if(G.vd->flag & V3D_GLOBAL_STATS) {
1803                         Mat4Invert(ob->imat, ob->obmat);
1804                         Mat4MulVecfl(ob->imat, median);
1805                         Mat4MulVecfl(ob->imat, ve_median);
1806                 }
1807                 VecSubf(median, ve_median, median);
1808                 median[3]= ve_median[3]-median[3];
1809                 median[4]= ve_median[4]-median[4];
1810                 
1811                 if(ob->type==OB_MESH) {
1812                         
1813                         eve= em->verts.first;
1814                         while(eve) {
1815                                 if(eve->f & SELECT) {
1816                                         VecAddf(eve->co, eve->co, median);
1817                                 }
1818                                 eve= eve->next;
1819                         }
1820                         
1821                         for(eed= em->edges.first; eed; eed= eed->next) {
1822                                 if(eed->f & SELECT) {
1823                                         /* ensure the median can be set to zero or one */
1824                                         if(ve_median[3]==0.0f) eed->crease= 0.0f;
1825                                         else if(ve_median[3]==1.0f) eed->crease= 1.0f;
1826                                         else {
1827                                                 eed->crease+= median[3];
1828                                                 CLAMP(eed->crease, 0.0, 1.0);
1829                                         }
1830                                 }
1831                         }
1832                         
1833                         recalc_editnormals();
1834                 }
1835                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
1836                         extern ListBase editNurb; /* editcurve.c */
1837                         Nurb *nu;
1838                         BPoint *bp;
1839                         BezTriple *bezt;
1840                         int a;
1841                         
1842                         nu= editNurb.first;
1843                         while(nu) {
1844                                 if((nu->type & 7)==1) {
1845                                         bezt= nu->bezt;
1846                                         a= nu->pntsu;
1847                                         while(a--) {
1848                                                 if(bezt->f2 & SELECT) {
1849                                                         VecAddf(bezt->vec[0], bezt->vec[0], median);
1850                                                         VecAddf(bezt->vec[1], bezt->vec[1], median);
1851                                                         VecAddf(bezt->vec[2], bezt->vec[2], median);
1852                                                         bezt->weight+= median[4];
1853                                                 }
1854                                                 else {
1855                                                         if(bezt->f1 & SELECT) {
1856                                                                 VecAddf(bezt->vec[0], bezt->vec[0], median);
1857                                                         }
1858                                                         if(bezt->f3 & SELECT) {
1859                                                                 VecAddf(bezt->vec[2], bezt->vec[2], median);
1860                                                         }
1861                                                 }
1862                                                 bezt++;
1863                                         }
1864                                 }
1865                                 else {
1866                                         bp= nu->bp;
1867                                         a= nu->pntsu*nu->pntsv;
1868                                         while(a--) {
1869                                                 if(bp->f1 & SELECT) {
1870                                                         VecAddf(bp->vec, bp->vec, median);
1871                                                         bp->vec[3]+= median[3];
1872                                                         bp->weight+= median[4];
1873                                                 }
1874                                                 bp++;
1875                                         }
1876                                 }
1877                                 test2DNurb(nu);
1878                                 testhandlesNurb(nu); /* test for bezier too */
1879
1880                                 nu= nu->next;
1881                         }
1882                 }
1883                 else if(ob->type==OB_LATTICE) {
1884                         BPoint *bp;
1885                         int a;
1886                         
1887                         a= editLatt->pntsu*editLatt->pntsv*editLatt->pntsw;
1888                         bp= editLatt->def;
1889                         while(a--) {
1890                                 if(bp->f1 & SELECT) {
1891                                         VecAddf(bp->vec, bp->vec, median);
1892                                         bp->weight+= median[4];
1893                                 }
1894                                 bp++;
1895                         }
1896                 }
1897                 
1898                 BIF_undo_push("Transform properties");
1899         }
1900 }
1901
1902 /* assumes armature active */
1903 static void validate_bonebutton_cb(void *bonev, void *namev)
1904 {
1905         Object *ob= OBACT;
1906         
1907         if(ob && ob->type==OB_ARMATURE) {
1908                 Bone *bone= bonev;
1909                 char oldname[32], newname[32];
1910                 
1911                 /* need to be on the stack */
1912                 BLI_strncpy(newname, bone->name, 32);
1913                 BLI_strncpy(oldname, (char *)namev, 32);
1914                 /* restore */
1915                 BLI_strncpy(bone->name, oldname, 32);
1916                 
1917                 armature_bone_rename(ob->data, oldname, newname); // editarmature.c
1918                 allqueue(REDRAWALL, 0);
1919         }
1920 }
1921
1922 static void v3d_posearmature_buts(uiBlock *block, Object *ob, float lim)
1923 {
1924         uiBut *but;
1925         bArmature *arm;
1926         bPoseChannel *pchan;
1927         Bone *bone= NULL;
1928         TransformProperties *tfp= G.vd->properties_storage;
1929
1930         arm = get_armature(OBACT);
1931         if (!arm || !ob->pose) return;
1932
1933         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
1934                 bone = pchan->bone;
1935                 if(bone && (bone->flag & BONE_ACTIVE) && (bone->layer & arm->layer))
1936                         break;
1937         }
1938         if (!pchan || !bone) return;
1939
1940         if((ob->parent) && (ob->partype == PARBONE))
1941                 but= uiDefBut (block, TEX, B_DIFF, "Bone:",                             160, 130, 140, 19, bone->name, 1, 31, 0, 0, "");
1942         else
1943                 but= uiDefBut(block, TEX, B_DIFF, "Bone:",                              160, 140, 140, 19, bone->name, 1, 31, 0, 0, "");
1944         uiButSetFunc(but, validate_bonebutton_cb, bone, NULL);
1945         
1946         QuatToEul(pchan->quat, tfp->ob_eul);
1947         tfp->ob_eul[0]*= 180.0/M_PI;
1948         tfp->ob_eul[1]*= 180.0/M_PI;
1949         tfp->ob_eul[2]*= 180.0/M_PI;
1950         
1951         uiBlockBeginAlign(block);
1952         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");
1953         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocX:",        30, 140, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
1954         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");
1955         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocY:",        30, 120, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
1956         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");
1957         uiDefButF(block, NUM, B_ARMATUREPANEL2, "LocZ:",        30, 100, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
1958
1959         uiBlockBeginAlign(block);
1960         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");
1961         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotX:",        30, 70, 120, 19, tfp->ob_eul, -1000.0, 1000.0, 100, 3, "");
1962         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");
1963         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotY:",        30, 50, 120, 19, tfp->ob_eul+1, -1000.0, 1000.0, 100, 3, "");
1964         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");
1965         uiDefButF(block, NUM, B_ARMATUREPANEL3, "RotZ:",        30, 30, 120, 19, tfp->ob_eul+2, -1000.0, 1000.0, 100, 3, "");
1966         
1967         uiBlockBeginAlign(block);
1968         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEX, REDRAWVIEW3D, ICON_UNLOCKED,   160,70,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1969         uiDefButF(block, NUM, B_ARMATUREPANEL2, "ScaleX:",      180, 70, 120, 19, pchan->size, -lim, lim, 10, 3, "");
1970         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEY, REDRAWVIEW3D, ICON_UNLOCKED,   160,50,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1971         uiDefButF(block, NUM, B_ARMATUREPANEL2, "ScaleY:",      180, 50, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
1972         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEZ, REDRAWVIEW3D, ICON_UNLOCKED,   160,30,20,19, &(pchan->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
1973         uiDefButF(block, NUM, B_ARMATUREPANEL2, "ScaleZ:",      180, 30, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
1974         uiBlockEndAlign(block);
1975 }
1976
1977 static void v3d_editarmature_buts(uiBlock *block, Object *ob, float lim)
1978 {
1979         bArmature *arm= G.obedit->data;
1980         EditBone *ebone;
1981         uiBut *but;
1982         TransformProperties *tfp= G.vd->properties_storage;
1983         
1984         ebone= G.edbo.first;
1985
1986         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
1987                 if ((ebone->flag & BONE_ACTIVE) && (ebone->layer & arm->layer))
1988                         break;
1989         }
1990
1991         if (!ebone)
1992                 return;
1993         
1994         if((ob->parent) && (ob->partype == PARBONE))
1995                 but= uiDefBut(block, TEX, B_DIFF, "Bone:", 160, 130, 140, 19, ebone->name, 1, 31, 0, 0, "");
1996         else
1997                 but= uiDefBut(block, TEX, B_DIFF, "Bone:",                      160, 150, 140, 19, ebone->name, 1, 31, 0, 0, "");
1998         uiButSetFunc(but, validate_editbonebutton_cb, ebone, NULL);
1999
2000         uiBlockBeginAlign(block);
2001         uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadX:",       10, 70, 140, 19, ebone->head, -lim, lim, 10, 3, "");
2002         uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadY:",       10, 50, 140, 19, ebone->head+1, -lim, lim, 10, 3, "");
2003         uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadZ:",       10, 30, 140, 19, ebone->head+2, -lim, lim, 10, 3, "");
2004         uiBlockBeginAlign(block);
2005         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TailX:",       160, 70, 140, 19, ebone->tail, -lim, lim, 10, 3, "");
2006         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TailY:",       160, 50, 140, 19, ebone->tail+1, -lim, lim, 10, 3, "");
2007         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TailZ:",       160, 30, 140, 19, ebone->tail+2, -lim, lim, 10, 3, "");
2008         uiBlockEndAlign(block);
2009         
2010         tfp->ob_eul[0]= 180.0*ebone->roll/M_PI;
2011         uiDefButF(block, NUM, B_ARMATUREPANEL1, "Roll:",        10, 100, 140, 19, tfp->ob_eul, -lim, lim, 1000, 3, "");
2012
2013         uiDefButBitI(block, TOG, BONE_EDITMODE_LOCKED, REDRAWVIEW3D, "Lock", 160, 100, 140, 19, &(ebone->flag), 0, 0, 0, 0, "Prevents bone from being transformed in edit mode");
2014         
2015         uiBlockBeginAlign(block);
2016         uiDefButF(block, NUM, B_ARMATUREPANEL1, "TailRadius:",  10, 150, 140, 19, &ebone->rad_tail, 0, lim, 10, 3, "");
2017         if (ebone->parent && ebone->flag & BONE_CONNECTED )
2018                 uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadRadius:",  10, 130, 140, 19, &ebone->parent->rad_tail, 0, lim, 10, 3, "");
2019         else
2020                 uiDefButF(block, NUM, B_ARMATUREPANEL1, "HeadRadius:",  10, 130, 140, 19, &ebone->rad_head, 0, lim, 10, 3, "");
2021         uiBlockEndAlign(block);
2022 }
2023
2024 static void v3d_editmetaball_buts(uiBlock *block, Object *ob, float lim)
2025 {
2026         extern MetaElem *lastelem;
2027
2028         if(lastelem) {
2029                 uiBlockBeginAlign(block);
2030                 uiDefButF(block, NUM, B_RECALCMBALL, "LocX:", 10, 70, 140, 19, &lastelem->x, -lim, lim, 100, 3, "");
2031                 uiDefButF(block, NUM, B_RECALCMBALL, "LocY:", 10, 50, 140, 19, &lastelem->y, -lim, lim, 100, 3, "");
2032                 uiDefButF(block, NUM, B_RECALCMBALL, "LocZ:", 10, 30, 140, 19, &lastelem->z, -lim, lim, 100, 3, "");
2033
2034                 uiBlockBeginAlign(block);
2035                 if(lastelem->type!=MB_BALL)
2036                         uiDefButF(block, NUM, B_RECALCMBALL, "dx:", 160, 70, 140, 19, &lastelem->expx, 0, lim, 100, 3, "");
2037                 if((lastelem->type!=MB_BALL) && (lastelem->type!=MB_TUBE))
2038                         uiDefButF(block, NUM, B_RECALCMBALL, "dy:", 160, 50, 140, 19, &lastelem->expy, 0, lim, 100, 3, "");
2039                 if((lastelem->type==MB_ELIPSOID) || (lastelem->type==MB_CUBE))
2040                         uiDefButF(block, NUM, B_RECALCMBALL, "dz:", 160, 30, 140, 19, &lastelem->expz, 0, lim, 100, 3, "");
2041
2042                 uiBlockEndAlign(block); 
2043
2044                 uiBlockBeginAlign(block);
2045                 uiDefButF(block, NUM, B_RECALCMBALL, "Radius:", 10, 120, 140, 19, &lastelem->rad, 0, lim, 100, 3, "Size of the active metaball");
2046                 uiDefButF(block, NUM, B_RECALCMBALL, "Stiffness:", 10, 100, 140, 19, &lastelem->s, 0, 10, 100, 3, "Stiffness of the active metaball");
2047                 uiBlockEndAlign(block);
2048                 
2049                 uiDefButS(block, MENU, B_RECALCMBALL, "Type%t|Ball%x0|Tube%x4|Plane%x5|Elipsoid%x6|Cube%x7", 160, 120, 140, 19, &lastelem->type, 0.0, 0.0, 0, 0, "Set active element type");
2050                 
2051         }
2052 }
2053
2054 void do_viewbuts(unsigned short event)
2055 {
2056         BoundBox *bb;
2057         View3D *vd;
2058         Object *ob= OBACT;
2059         TransformProperties *tfp= G.vd->properties_storage;
2060         
2061         vd= G.vd;
2062         if(vd==NULL) return;
2063
2064         switch(event) {
2065                 
2066         case B_OBJECTPANEL:
2067                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2068                 allqueue(REDRAWVIEW3D, 1);
2069                 break;
2070                 
2071         case B_OBJECTPANELROT:
2072                 if(ob) {
2073                         ob->rot[0]= M_PI*tfp->ob_eul[0]/180.0;
2074                         ob->rot[1]= M_PI*tfp->ob_eul[1]/180.0;
2075                         ob->rot[2]= M_PI*tfp->ob_eul[2]/180.0;
2076                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2077                         allqueue(REDRAWVIEW3D, 1);
2078                 }
2079                 break;
2080
2081         case B_OBJECTPANELSCALE:
2082                 if(ob) {
2083
2084                         /* link scale; figure out which axis changed */
2085                         if (tfp->link_scale) {
2086                                 float ratio, tmp, max = 0.0;
2087                                 int axis;
2088                                 
2089                                 axis = 0;
2090                                 max = fabs(tfp->ob_scale[0] - ob->size[0]);
2091                                 tmp = fabs(tfp->ob_scale[1] - ob->size[1]);
2092                                 if (tmp > max) {
2093                                         axis = 1;
2094                                         max = tmp;
2095                                 }
2096                                 tmp = fabs(tfp->ob_scale[2] - ob->size[2]);
2097                                 if (tmp > max) {
2098                                         axis = 2;
2099                                         max = tmp;
2100                                 }
2101                         
2102                                 if (ob->size[axis] != tfp->ob_scale[axis]) {
2103                                         if (fabs(ob->size[axis]) > FLT_EPSILON) {
2104                                                 ratio = tfp->ob_scale[axis] / ob->size[axis];
2105                                                 ob->size[0] *= ratio;
2106                                                 ob->size[1] *= ratio;
2107                                                 ob->size[2] *= ratio;
2108                                         }
2109                                 }
2110                         }
2111                         else {
2112                                 VECCOPY(ob->size, tfp->ob_scale);
2113                                 
2114                         }
2115                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2116                         allqueue(REDRAWVIEW3D, 1);
2117                 }
2118                 break;
2119
2120         case B_OBJECTPANELDIMS:
2121                 bb= object_get_boundbox(ob);
2122                 if(bb) {
2123                         float old_dims[3], scale[3], ratio, len[3];
2124                         int axis;
2125
2126                         Mat4ToSize(ob->obmat, scale);
2127
2128                         len[0] = bb->vec[4][0] - bb->vec[0][0];
2129                         len[1] = bb->vec[2][1] - bb->vec[0][1];
2130                         len[2] = bb->vec[1][2] - bb->vec[0][2];
2131
2132                         old_dims[0] = fabs(scale[0]) * len[0];
2133                         old_dims[1] = fabs(scale[1]) * len[1];
2134                         old_dims[2] = fabs(scale[2]) * len[2];
2135
2136                         /* for each axis changed */
2137                         for (axis = 0; axis<3; axis++) {
2138                                 if (fabs(old_dims[axis] - tfp->ob_dims[axis]) > 0.0001) {
2139                                         if (old_dims[axis] > 0.0) {
2140                                                 ratio = tfp->ob_dims[axis] / old_dims[axis]; 
2141                                                 if (tfp->link_scale) {
2142                                                         ob->size[0] *= ratio;
2143                                                         ob->size[1] *= ratio;
2144                                                         ob->size[2] *= ratio;
2145                                                         break;
2146                                                 }
2147                                                 else {
2148                                                         ob->size[axis] *= ratio;
2149                                                 }
2150                                         }
2151                                         else {
2152                                                 if (len[axis] > 0) {
2153                                                         ob->size[axis] = tfp->ob_dims[axis] / len[axis];
2154                                                 }
2155                                         }
2156                                 }
2157                         }
2158                         
2159                         /* prevent multiple B_OBJECTPANELDIMS events to keep scaling, cycling with TAB on buttons can cause that */
2160                         VECCOPY(tfp->ob_dims, old_dims);
2161                         
2162                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2163                         allqueue(REDRAWVIEW3D, 1);
2164                 }
2165                 break;
2166         
2167         case B_OBJECTPANELMEDIAN:
2168                 if(ob) {
2169                         v3d_editvertex_buts(NULL, ob, 1.0);
2170                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2171                         allqueue(REDRAWVIEW3D, 1);
2172                 }
2173                 break;
2174                 
2175                 /* note; this case also used for parbone */
2176         case B_OBJECTPANELPARENT:
2177                 if(ob) {
2178                         if(ob->id.lib || test_parent_loop(ob->parent, ob) ) 
2179                                 ob->parent= NULL;
2180                         else {
2181                                 DAG_scene_sort(G.scene);
2182                                 DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
2183                         }
2184                         allqueue(REDRAWVIEW3D, 1);
2185                         allqueue(REDRAWBUTSOBJECT, 0);
2186                         allqueue(REDRAWOOPS, 0);
2187                 }
2188                 break;
2189                 
2190         case B_ARMATUREPANEL1:
2191                 {
2192                         bArmature *arm= G.obedit->data;
2193                         EditBone *ebone, *child;
2194                         
2195                         for (ebone = G.edbo.first; ebone; ebone=ebone->next){
2196                                 if ((ebone->flag & BONE_ACTIVE) && (ebone->layer & arm->layer))
2197                                         break;
2198                         }
2199                         if (ebone) {
2200                                 ebone->roll= M_PI*tfp->ob_eul[0]/180.0;
2201                                 //      Update our parent
2202                                 if (ebone->parent && ebone->flag & BONE_CONNECTED){
2203                                         VECCOPY (ebone->parent->tail, ebone->head);
2204                                 }
2205                         
2206                                 //      Update our children if necessary
2207                                 for (child = G.edbo.first; child; child=child->next){
2208                                         if (child->parent == ebone && (child->flag & BONE_CONNECTED)){
2209                                                 VECCOPY (child->head, ebone->tail);
2210                                         }
2211                                 }
2212                                 if(arm->flag & ARM_MIRROR_EDIT) {
2213                                         EditBone *eboflip= armature_bone_get_mirrored(ebone);
2214                                         if(eboflip) {
2215                                                 eboflip->roll= -ebone->roll;
2216                                                 eboflip->head[0]= -ebone->head[0];
2217                                                 eboflip->tail[0]= -ebone->tail[0];
2218                                                 
2219                                                 //      Update our parent
2220                                                 if (eboflip->parent && eboflip->flag & BONE_CONNECTED){
2221                                                         VECCOPY (eboflip->parent->tail, eboflip->head);
2222                                                 }
2223                                                 
2224                                                 //      Update our children if necessary
2225                                                 for (child = G.edbo.first; child; child=child->next){
2226                                                         if (child->parent == eboflip && (child->flag & BONE_CONNECTED)){
2227                                                                 VECCOPY (child->head, eboflip->tail);
2228                                                         }
2229                                                 }
2230                                         }
2231                                 }
2232                                 
2233                                 allqueue(REDRAWVIEW3D, 1);
2234                         }
2235                 }
2236                 break;
2237         case B_ARMATUREPANEL3:  // rotate button on channel
2238                 {
2239                         bArmature *arm;
2240                         bPoseChannel *pchan;
2241                         Bone *bone;
2242                         float eul[3];
2243                         
2244                         arm = get_armature(OBACT);
2245                         if (!arm || !ob->pose) return;
2246                                 
2247                         for(pchan= ob->pose->chanbase.first; pchan; pchan= pchan->next) {
2248                                 bone = pchan->bone;
2249                                 if(bone && (bone->flag & BONE_ACTIVE) && (bone->layer & arm->layer))
2250                                         break;
2251                         }
2252                         if (!pchan) return;
2253                         
2254                         /* make a copy to eul[3], to allow TAB on buttons to work */
2255                         eul[0]= M_PI*tfp->ob_eul[0]/180.0;
2256                         eul[1]= M_PI*tfp->ob_eul[1]/180.0;
2257                         eul[2]= M_PI*tfp->ob_eul[2]/180.0;
2258                         EulToQuat(eul, pchan->quat);
2259                 }
2260                 /* no break, pass on */
2261         case B_ARMATUREPANEL2:
2262                 {
2263                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
2264                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
2265                         allqueue(REDRAWVIEW3D, 1);
2266                 }
2267                 break;
2268         case B_TRANSFORMSPACEADD:
2269                 BIF_manageTransformOrientation(1, 0);
2270                 allqueue(REDRAWVIEW3D, 1);
2271                 break;
2272         case B_TRANSFORMSPACECLEAR:
2273                 BIF_clearTransformOrientation();
2274                 allqueue(REDRAWVIEW3D, 1);
2275         }
2276 }
2277
2278 void removeTransformOrientation_func(void *target, void *unused)
2279 {
2280         BIF_removeTransformOrientation((TransformOrientation *) target);
2281 }
2282
2283 void selectTransformOrientation_func(void *target, void *unused)
2284 {
2285         BIF_selectTransformOrientation((TransformOrientation *) target);
2286 }
2287
2288 static void view3d_panel_transform_spaces(short cntrl)
2289 {
2290         ListBase *transform_spaces = &G.scene->transform_spaces;
2291         TransformOrientation *ts = transform_spaces->first;
2292         uiBlock *block;
2293         uiBut *but;
2294         int xco = 20, yco = 70, height = 140;
2295         int index;
2296
2297         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_transform", UI_EMBOSS, UI_HELV, curarea->win);
2298         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2299         uiSetPanelHandler(VIEW3D_HANDLER_TRANSFORM);  // for close and esc
2300
2301         if(uiNewPanel(curarea, block, "Transform Orientations", "View3d", 10, 230, 318, height)==0) return;
2302
2303         uiNewPanelHeight(block, height);
2304
2305         uiBlockBeginAlign(block);
2306         
2307         if (G.obedit)
2308                 uiDefBut(block, BUT, B_TRANSFORMSPACEADD, "Add", xco,120,80,20, 0, 0, 0, 0, 0, "Add the selected element as a Transform Orientation");
2309         else
2310                 uiDefBut(block, BUT, B_TRANSFORMSPACEADD, "Add", xco,120,80,20, 0, 0, 0, 0, 0, "Add the active object as a Transform Orientation");
2311
2312         uiDefBut(block, BUT, B_TRANSFORMSPACECLEAR, "Clear", xco + 80,120,80,20, 0, 0, 0, 0, 0, "Removal all Transform Orientations");
2313         
2314         uiBlockEndAlign(block);
2315         
2316         uiBlockBeginAlign(block);
2317         
2318         uiDefButS(block, ROW, REDRAWHEADERS, "Global",  xco,            90, 40,20, &G.vd->twmode, 5.0, (float)V3D_MANIP_GLOBAL,0, 0, "Global Transform Orientation");
2319         uiDefButS(block, ROW, REDRAWHEADERS, "Local",   xco + 40,       90, 40,20, &G.vd->twmode, 5.0, (float)V3D_MANIP_LOCAL, 0, 0, "Local Transform Orientation");
2320         uiDefButS(block, ROW, REDRAWHEADERS, "Normal",  xco + 80,       90, 40,20, &G.vd->twmode, 5.0, (float)V3D_MANIP_NORMAL,0, 0, "Normal Transform Orientation");
2321         uiDefButS(block, ROW, REDRAWHEADERS, "View",            xco + 120,      90, 40,20, &G.vd->twmode, 5.0, (float)V3D_MANIP_VIEW,   0, 0, "View Transform Orientation");
2322         
2323         for (index = V3D_MANIP_CUSTOM, ts = transform_spaces->first ; ts ; ts = ts->next, index++) {
2324
2325                 BIF_ThemeColor(TH_BUT_ACTION);
2326                 if (G.vd->twmode == index) {
2327                         but = uiDefIconButS(block,ROW, REDRAWHEADERS, ICON_CHECKBOX_HLT, xco,yco,XIC,YIC, &G.vd->twmode, 5.0, (float)index, 0, 0, "Use this Custom Transform Orientation");
2328                 }
2329                 else {
2330                         but = uiDefIconButS(block,ROW, REDRAWHEADERS, ICON_CHECKBOX_DEHLT, xco,yco,XIC,YIC, &G.vd->twmode, 5.0, (float)index, 0, 0, "Use this Custom Transform Orientation");
2331                 }
2332                 uiButSetFunc(but, selectTransformOrientation_func, ts, NULL);
2333                 uiDefBut(block, TEX, 0, "", xco+=XIC, yco,100+XIC,20, &ts->name, 0, 30, 0, 0, "Edits the name of this Transform Orientation");
2334                 but = uiDefIconBut(block, BUT, REDRAWVIEW3D, ICON_X, xco+=100+XIC,yco,XIC,YIC, 0, 0, 0, 0, 0, "Deletes this Transform Orientation");
2335                 uiButSetFunc(but, removeTransformOrientation_func, ts, NULL);
2336
2337                 xco = 20;
2338                 yco -= 25;
2339         }
2340         uiBlockEndAlign(block);
2341         
2342         if(yco < 0) uiNewPanelHeight(block, height-yco);
2343 }
2344
2345
2346 static void view3d_panel_object(short cntrl)    // VIEW3D_HANDLER_OBJECT
2347 {
2348         uiBlock *block;
2349         uiBut *bt;
2350         Object *ob= OBACT;
2351         TransformProperties *tfp;
2352         float lim;
2353         static char hexcol[128];
2354         
2355         if(ob==NULL) return;
2356
2357         /* make sure we got storage */
2358         if(G.vd->properties_storage==NULL)
2359                 G.vd->properties_storage= MEM_callocN(sizeof(TransformProperties), "TransformProperties");
2360         tfp= G.vd->properties_storage;
2361         
2362         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_object", UI_EMBOSS, UI_HELV, curarea->win);
2363         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
2364         uiSetPanelHandler(VIEW3D_HANDLER_OBJECT);  // for close and esc
2365
2366         if((G.f & G_SCULPTMODE) && !G.obedit) {
2367                 if(!uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 234))
2368                         return;
2369         } else if(G.f & G_PARTICLEEDIT && !G.obedit){
2370                 if(!uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 234))
2371                         return;
2372         } else {
2373                 if(!uiNewPanel(curarea, block, "Transform Properties", "View3d", 10, 230, 318, 204))
2374                         return;
2375         }
2376
2377         uiSetButLock(object_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
2378         
2379         if(G.f & (G_VERTEXPAINT|G_TEXTUREPAINT|G_WEIGHTPAINT)) {
2380                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2381         }
2382         else {
2383                 bt= uiDefBut(block, TEX, B_IDNAME, "OB: ",      10,180,140,20, ob->id.name+2, 0.0, 21.0, 0, 0, "");
2384 #ifdef WITH_VERSE
2385                 if(ob->vnode) uiButSetFunc(bt, test_and_send_idbutton_cb, ob, ob->id.name);
2386                 else uiButSetFunc(bt, test_idbutton_cb, ob->id.name, NULL);
2387 #else
2388                 uiButSetFunc(bt, test_idbutton_cb, ob->id.name, NULL);
2389 #endif
2390
2391                 if((G.f & G_PARTICLEEDIT)==0) {
2392                         uiBlockBeginAlign(block);
2393                         uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_OBJECTPANELPARENT, "Par:", 160, 180, 140, 20, &ob->parent, "Parent Object"); 
2394                         if((ob->parent) && (ob->partype == PARBONE)) {
2395                                 bt= uiDefBut(block, TEX, B_OBJECTPANELPARENT, "ParBone:", 160, 160, 140, 20, ob->parsubstr, 0, 30, 0, 0, "");
2396                                 uiButSetCompleteFunc(bt, autocomplete_bone, (void *)ob->parent);
2397                         }
2398                         else {
2399                                 strcpy(ob->parsubstr, "");
2400                         }
2401                         uiBlockEndAlign(block);
2402                 }
2403         }
2404
2405         lim= 10000.0f*MAX2(1.0, G.vd->grid);
2406
2407         if(ob==G.obedit) {
2408                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(block, ob, lim);
2409                 if(ob->type==OB_MBALL) v3d_editmetaball_buts(block, ob, lim);
2410                 else v3d_editvertex_buts(block, ob, lim);
2411         }
2412         else if(ob->flag & OB_POSEMODE) {
2413                 v3d_posearmature_buts(block, ob, lim);
2414         }
2415         else if(G.f & G_WEIGHTPAINT) {
2416                 uiNewPanelTitle(block, "Weight Paint Properties");
2417                 weight_paint_buttons(block);
2418         }
2419         else if(G.f & (G_VERTEXPAINT|G_TEXTUREPAINT)) {
2420                 extern VPaint Gvp;         /* from vpaint */
2421                 static float hsv[3], old[3];    // used as temp mem for picker
2422                 float *rgb= NULL;
2423                 ToolSettings *settings= G.scene->toolsettings;
2424
2425                 if(G.f & G_VERTEXPAINT) rgb= &Gvp.r;
2426                 else if(settings->imapaint.brush) rgb= settings->imapaint.brush->rgb;
2427                 
2428                 uiNewPanelTitle(block, "Paint Properties");
2429                 if (rgb)
2430                         /* 'f' is for floating panel */
2431                         uiBlockPickerButtons(block, rgb, hsv, old, hexcol, 'f', REDRAWBUTSEDIT);
2432         }
2433         else if(G.f & G_SCULPTMODE) {
2434                 uiNewPanelTitle(block, "Sculpt Properties");
2435                 sculptmode_draw_interface_tools(block,10,150);
2436         } else if(G.f & G_PARTICLEEDIT){
2437                 uiNewPanelTitle(block, "Particle Edit Properties");
2438                 particle_edit_buttons(block);
2439         } else {
2440                 BoundBox *bb = NULL;
2441
2442                 uiBlockBeginAlign(block);
2443                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, REDRAWVIEW3D, ICON_UNLOCKED,     10,150,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2444                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocX:",           30, 150, 120, 19, &(ob->loc[0]), -lim, lim, 100, 3, "");
2445                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, REDRAWVIEW3D, ICON_UNLOCKED,     10,130,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2446                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocY:",           30, 130, 120, 19, &(ob->loc[1]), -lim, lim, 100, 3, "");
2447                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, REDRAWVIEW3D, ICON_UNLOCKED,     10,110,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2448                 uiDefButF(block, NUM, B_OBJECTPANEL, "LocZ:",           30, 110, 120, 19, &(ob->loc[2]), -lim, lim, 100, 3, "");
2449                 
2450                 tfp->ob_eul[0]= 180.0*ob->rot[0]/M_PI;
2451                 tfp->ob_eul[1]= 180.0*ob->rot[1]/M_PI;
2452                 tfp->ob_eul[2]= 180.0*ob->rot[2]/M_PI;
2453                 
2454                 uiBlockBeginAlign(block);
2455                 if ((ob->parent) && (ob->partype == PARBONE)) {
2456                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     160,130,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2457                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotX:",        180, 130, 120, 19, &(tfp->ob_eul[0]), -lim, lim, 1000, 3, "");
2458                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     160,110,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2459                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotY:",        180, 110, 120, 19, &(tfp->ob_eul[1]), -lim, lim, 1000, 3, "");
2460                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     160,90,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2461                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotZ:",        180, 90, 120, 19, &(tfp->ob_eul[2]), -lim, lim, 1000, 3, "");
2462
2463                 }
2464                 else {
2465                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, REDRAWVIEW3D, ICON_UNLOCKED,     160,150,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2466                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotX:",        180, 150, 120, 19, &(tfp->ob_eul[0]), -lim, lim, 1000, 3, "");
2467                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, REDRAWVIEW3D, ICON_UNLOCKED,     160,130,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2468                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotY:",        180, 130, 120, 19, &(tfp->ob_eul[1]), -lim, lim, 1000, 3, "");
2469                         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, REDRAWVIEW3D, ICON_UNLOCKED,     160,110,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2470                         uiDefButF(block, NUM, B_OBJECTPANELROT, "RotZ:",        180, 110, 120, 19, &(tfp->ob_eul[2]), -lim, lim, 1000, 3, "");
2471                 }
2472
2473                 tfp->ob_scale[0]= ob->size[0];
2474                 tfp->ob_scale[1]= ob->size[1];
2475                 tfp->ob_scale[2]= ob->size[2];
2476
2477                 uiBlockBeginAlign(block);
2478                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEX, REDRAWVIEW3D, ICON_UNLOCKED,   10,80,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2479                 uiDefButF(block, NUM, B_OBJECTPANELSCALE, "ScaleX:",            30, 80, 120, 19, &(tfp->ob_scale[0]), -lim, lim, 10, 3, "");
2480                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEY, REDRAWVIEW3D, ICON_UNLOCKED,   10,60,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2481                 uiDefButF(block, NUM, B_OBJECTPANELSCALE, "ScaleY:",            30, 60, 120, 19, &(tfp->ob_scale[1]), -lim, lim, 10, 3, "");
2482                 uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEZ, REDRAWVIEW3D, ICON_UNLOCKED,   10,40,20,19, &(ob->protectflag), 0, 0, 0, 0, "Protects this value from being Transformed");
2483                 uiDefButF(block, NUM, B_OBJECTPANELSCALE, "ScaleZ:",            30, 40, 120, 19, &(tfp->ob_scale[2]), -lim, lim, 10, 3, "");
2484                 uiBlockEndAlign(block);
2485                 
2486                 uiDefButS(block, TOG, REDRAWVIEW3D, "Link Scale",               10, 10, 140, 19, &(tfp->link_scale), 0, 1, 0, 0, "Scale values vary proportionally in all directions");
2487
2488                 bb= object_get_boundbox(ob);
2489                 if (bb) {
2490                         float scale[3];
2491
2492                         Mat4ToSize(ob->obmat, scale);
2493
2494                         tfp->ob_dims[0] = fabs(scale[0]) * (bb->vec[4][0] - bb->vec[0][0]);
2495                         tfp->ob_dims[1] = fabs(scale[1]) * (bb->vec[2][1] - bb->vec[0][1]);
2496                         tfp->ob_dims[2] = fabs(scale[2]) * (bb->vec[1][2] - bb->vec[0][2]);
2497
2498                         uiBlockBeginAlign(block);
2499                         if ((ob->parent) && (ob->partype == PARBONE)) {
2500                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimX:",               160, 60, 140, 19, &(tfp->ob_dims[0]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2501                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimY:",               160, 40, 140, 19, &(tfp->ob_dims[1]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2502                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimZ:",               160, 20, 140, 19, &(tfp->ob_dims[2]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2503
2504                         }
2505                         else {
2506                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimX:",               160, 80, 140, 19, &(tfp->ob_dims[0]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2507                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimY:",               160, 60, 140, 19, &(tfp->ob_dims[1]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2508                                 uiDefButF(block, NUM, B_OBJECTPANELDIMS, "DimZ:",               160, 40, 140, 19, &(tfp->ob_dims[2]), 0.0, lim, 10, 3, "Manipulate bounding box size");
2509                         }
2510
2511                         uiBlockEndAlign(block);
2512                 }
2513         }
2514         uiClearButLock();
2515 }
2516
2517 static void view3d_panel_background(short cntrl)        // VIEW3D_HANDLER_BACKGROUND
2518 {
2519         uiBlock *block;
2520         View3D *vd;
2521         
2522         vd= G.vd;
2523
2524         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_background", UI_EMBOSS, UI_HELV, curarea->win);
2525         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2526         uiSetPanelHandler(VIEW3D_HANDLER_BACKGROUND);  // for close and esc
2527         if(uiNewPanel(curarea, block, "Background Image", "View3d", 340, 10, 318, 204)==0) return;
2528
2529         if(G.f & G_VERTEXPAINT || G.f & G_WEIGHTPAINT || G.f & G_TEXTUREPAINT || G.f & G_GREASEPENCIL) {
2530                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2531         }
2532         
2533         if(vd->flag & V3D_DISPBGPIC) {
2534                 if(vd->bgpic==NULL) {
2535                         vd->bgpic= MEM_callocN(sizeof(BGpic), "bgpic");
2536                         vd->bgpic->size= 5.0;
2537                         vd->bgpic->blend= 0.5;
2538                         vd->bgpic->iuser.fie_ima= 2;
2539                         vd->bgpic->iuser.ok= 1;
2540                 }
2541         }
2542         
2543         if(!(vd->flag & V3D_DISPBGPIC)) {
2544                 uiDefButBitS(block, TOG, V3D_DISPBGPIC, B_REDR, "Use Background Image", 10, 180, 150, 20, &vd->flag, 0, 0, 0, 0, "Display an image in the background of this 3D View");
2545                 uiDefBut(block, LABEL, 1, " ",  160, 180, 150, 20, NULL, 0.0, 0.0, 0, 0, "");
2546         }
2547         else {
2548                 uiBlockBeginAlign(block);
2549                 uiDefButBitS(block, TOG, V3D_DISPBGPIC, B_REDR, "Use", 10, 225, 50, 20, &vd->flag, 0, 0, 0, 0, "Display an image in the background of this 3D View");
2550                 uiDefButF(block, NUMSLI, B_REDR, "Blend:",      60, 225, 150, 20, &vd->bgpic->blend, 0.0,1.0, 0, 0, "Set the transparency of the background image");
2551                 uiDefButF(block, NUM, B_REDR, "Size:",          210, 225, 100, 20, &vd->bgpic->size, 0.1, 250.0*vd->grid, 100, 0, "Set the size (width) of the background image");
2552
2553                 uiDefButF(block, NUM, B_REDR, "X Offset:",      10, 205, 150, 20, &vd->bgpic->xof, -250.0*vd->grid,250.0*vd->grid, 10, 2, "Set the horizontal offset of the background image");
2554                 uiDefButF(block, NUM, B_REDR, "Y Offset:",      160, 205, 150, 20, &vd->bgpic->yof, -250.0*vd->grid,250.0*vd->grid, 10, 2, "Set the vertical offset of the background image");
2555                 
2556                 uiblock_image_panel(block, &vd->bgpic->ima, &vd->bgpic->iuser, B_REDR, B_REDR);
2557         }
2558 }
2559
2560
2561 static void view3d_panel_properties(short cntrl)        // VIEW3D_HANDLER_SETTINGS
2562 {
2563         uiBlock *block;
2564         View3D *vd;
2565         float *curs;
2566         
2567         vd= G.vd;
2568
2569         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
2570         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2571         uiSetPanelHandler(VIEW3D_HANDLER_PROPERTIES);  // for close and esc
2572         if(uiNewPanel(curarea, block, "View Properties", "View3d", 340, 30, 318, 254)==0) return;
2573
2574         /* to force height */
2575         uiNewPanelHeight(block, 264);
2576
2577         if(G.f & (G_VERTEXPAINT|G_FACESELECT|G_TEXTUREPAINT|G_WEIGHTPAINT|G_GREASEPENCIL)) {
2578                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2579         }
2580
2581         uiDefBut(block, LABEL, 1, "Grid:",                                      10, 220, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2582         uiBlockBeginAlign(block);
2583         uiDefButF(block, NUM, REDRAWVIEW3D, "Spacing:",         10, 200, 140, 19, &vd->grid, 0.001, 100.0, 10, 0, "Set the distance between grid lines");
2584         uiDefButS(block, NUM, REDRAWVIEW3D, "Lines:",           10, 180, 140, 19, &vd->gridlines, 0.0, 100.0, 100, 0, "Set the number of grid lines in perspective view");
2585         uiDefButS(block, NUM, REDRAWVIEW3D, "Divisions:",               10, 160, 140, 19, &vd->gridsubdiv, 1.0, 100.0, 100, 0, "Set the number of grid lines");
2586         uiBlockEndAlign(block);
2587
2588         uiDefBut(block, LABEL, 1, "3D Display:",                                                        160, 220, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2589         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");
2590         uiDefButBitS(block, TOG, V3D_SHOW_X, REDRAWVIEW3D, "X Axis",            160, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the X Axis line");
2591         uiDefButBitS(block, TOG, V3D_SHOW_Y, REDRAWVIEW3D, "Y Axis",            212, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Y Axis line");
2592         uiDefButBitS(block, TOG, V3D_SHOW_Z, REDRAWVIEW3D, "Z Axis",            262, 176, 48, 19, &vd->gridflag, 0, 0, 0, 0, "Show the Z Axis line");
2593
2594         uiDefBut(block, LABEL, 1, "View Camera:",                       10, 140, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
2595         
2596         uiDefButF(block, NUM, REDRAWVIEW3D, "Lens:",            10, 120, 140, 19, &vd->lens, 10.0, 120.0, 100, 0, "The lens angle in perspective view");
2597         uiBlockBeginAlign(block);
2598         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip Start:",      10, 96, 140, 19, &vd->near, vd->grid/100.0, 100.0, 10, 0, "Set the beginning of the range in which 3D objects are displayed (perspective view)");
2599         uiDefButF(block, NUM, REDRAWVIEW3D, "Clip End:",        10, 76, 140, 19, &vd->far, 1.0, 10000.0*vd->grid, 100, 0, "Set the end of the range in which 3D objects are displayed (perspective view)");
2600         uiBlockEndAlign(block);
2601
2602         uiDefBut(block, LABEL, 1, "3D Cursor:",                         160, 150, 140, 19, NULL, 0.0, 0.0, 0, 0, "");
2603
2604         uiBlockBeginAlign(block);
2605         curs= give_cursor();
2606         uiDefButF(block, NUM, REDRAWVIEW3D, "X:",                       160, 130, 150, 22, curs, -10000.0*vd->grid, 10000.0*vd->grid, 10, 0, "X co-ordinate of the 3D cursor");
2607         uiDefButF(block, NUM, REDRAWVIEW3D, "Y:",                       160, 108, 150, 22, curs+1, -10000.0*vd->grid, 10000.0*vd->grid, 10, 0, "Y co-ordinate of the 3D cursor");
2608         uiDefButF(block, NUM, REDRAWVIEW3D, "Z:",                       160, 86, 150, 22, curs+2, -10000.0*vd->grid, 10000.0*vd->grid, 10, 0, "Z co-ordinate of the 3D cursor");
2609         uiBlockEndAlign(block);
2610
2611         uiDefBut(block, LABEL, 1, "Display:",                           10, 50, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2612         
2613         uiBlockBeginAlign(block);
2614         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");
2615         uiDefButBitS(block, TOG, V3D_DRAW_CENTERS, REDRAWVIEW3D, "All Object Centers", 10, 10, 140, 19, &vd->flag, 0, 0, 0, 0, "Draw the center points on all objects");
2616         uiDefButBitS(block, TOGN, V3D_HIDE_HELPLINES, REDRAWVIEW3D, "Relationship Lines", 10, -10, 140, 19, &vd->flag, 0, 0, 0, 0, "Draw dashed lines indicating Parent, Constraint, or Hook relationships");
2617         uiDefButBitS(block, TOG, V3D_SOLID_TEX, REDRAWVIEW3D, "Solid Tex", 10, -30, 140, 19, &vd->flag2, 0, 0, 0, 0, "Display textures in Solid draw type (Shift T)");
2618         uiBlockEndAlign(block);
2619
2620         uiDefBut(block, LABEL, 1, "View Locking:",                              160, 50, 150, 19, NULL, 0.0, 0.0, 0, 0, "");
2621         uiBlockBeginAlign(block);
2622         uiDefIDPoinBut(block, test_obpoin_but, ID_OB, REDRAWVIEW3D, "Object:", 160, 30, 140, 19, &vd->ob_centre, "Lock view to center to this Object"); 
2623         uiDefBut(block, TEX, REDRAWVIEW3D, "Bone:",                                             160, 10, 140, 19, vd->ob_centre_bone, 1, 31, 0, 0, "If view locked to Object, use this Bone to lock to view to");
2624
2625 }
2626
2627 static void view3d_panel_preview(ScrArea *sa, short cntrl)      // VIEW3D_HANDLER_PREVIEW
2628 {
2629         uiBlock *block;
2630         View3D *v3d= sa->spacedata.first;
2631         int ofsx, ofsy;
2632         
2633         block= uiNewBlock(&sa->uiblocks, "view3d_panel_preview", UI_EMBOSS, UI_HELV, sa->win);
2634         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
2635         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
2636         
2637         ofsx= -150+(sa->winx/2)/v3d->blockscale;
2638         ofsy= -100+(sa->winy/2)/v3d->blockscale;
2639         if(uiNewPanel(sa, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
2640
2641         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
2642         
2643         if(G.scene->recalc & SCE_PRV_CHANGED) {
2644                 G.scene->recalc &= ~SCE_PRV_CHANGED;
2645                 //printf("found recalc\n");
2646                 BIF_view3d_previewrender_free(sa->spacedata.first);
2647                 BIF_preview_changed(0);
2648         }
2649 }
2650
2651 static void view3d_panel_gpencil(short cntrl)   // VIEW3D_HANDLER_GREASEPENCIL
2652 {
2653         uiBlock *block;
2654         View3D *vd;
2655         
2656         vd= G.vd;
2657
2658         block= uiNewBlock(&curarea->uiblocks, "view3d_panel_gpencil", UI_EMBOSS, UI_HELV, curarea->win);
2659         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE  | cntrl);
2660         uiSetPanelHandler(VIEW3D_HANDLER_GREASEPENCIL);  // for close and esc
2661         if (uiNewPanel(curarea, block, "Grease Pencil", "View3d", 100, 30, 318, 204)==0) return;
2662
2663         if (G.f & (G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT|G_GREASEPENCIL)) {
2664                 uiBlockSetFlag(block, UI_BLOCK_FRONTBUFFER);    // force old style frontbuffer draw
2665         }
2666         
2667         /* allocate memory for gpd if drawing enabled (this must be done first or else we crash) */
2668         if (vd->flag2 & V3D_DISPGP) {
2669                 if (vd->gpd == NULL)
2670                         gpencil_data_setactive(curarea, gpencil_data_addnew());
2671         }
2672         
2673         if (vd->flag2 & V3D_DISPGP) {
2674                 bGPdata *gpd= vd->gpd;
2675                 short newheight;
2676                 
2677                 /* this is a variable height panel, newpanel doesnt force new size on existing panels */
2678                 /* so first we make it default height */
2679                 uiNewPanelHeight(block, 204);
2680                 
2681                 /* draw button for showing gpencil settings and drawings */
2682                 uiDefButBitS(block, TOG, V3D_DISPGP, B_REDR, "Use Grease Pencil", 10, 225, 150, 20, &vd->flag2, 0, 0, 0, 0, "Display freehand annotations overlay over this 3D View (draw using Shift-LMB)");
2683                 
2684                 /* extend the panel if the contents won't fit */
2685                 newheight= draw_gpencil_panel(block, gpd, curarea); 
2686                 uiNewPanelHeight(block, newheight);
2687         }
2688         else {
2689                 uiDefButBitS(block, TOG, V3D_DISPGP, B_REDR, "Use Grease Pencil", 10, 225, 150, 20, &vd->flag2, 0, 0, 0, 0, "Display freehand annotations overlay over this 3D View");
2690                 uiDefBut(block, LABEL, 1, " ",  160, 180, 150, 20, NULL, 0.0, 0.0, 0, 0, "");
2691         }
2692 }
2693
2694
2695 static void view3d_blockhandlers(ScrArea *sa)
2696 {
2697         View3D *v3d= sa->spacedata.first;
2698         short a;
2699         
2700         /* warning; blocks need to be freed each time, handlers dont remove */
2701         uiFreeBlocksWin(&sa->uiblocks, sa->win);
2702         
2703         /*uv face-sel and wp mode when mixed with wire leave depth enabled causing
2704         models to draw over the UI */
2705         glDisable(GL_DEPTH_TEST); 
2706         
2707         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
2708                 switch(v3d->blockhandler[a]) {
2709                 case VIEW3D_HANDLER_PROPERTIES:
2710                         view3d_panel_properties(v3d->blockhandler[a+1]);
2711                         break;
2712                 case VIEW3D_HANDLER_BACKGROUND:
2713                         view3d_panel_background(v3d->blockhandler[a+1]);
2714                         break;
2715                 case VIEW3D_HANDLER_OBJECT:
2716                         view3d_panel_object(v3d->blockhandler[a+1]);
2717                         break;
2718                 case VIEW3D_HANDLER_PREVIEW:
2719                         view3d_panel_preview(sa, v3d->blockhandler[a+1]);
2720                         break;                  
2721                 case VIEW3D_HANDLER_TRANSFORM:
2722                         view3d_panel_transform_spaces(v3d->blockhandler[a+1]);
2723                         break;
2724                 case VIEW3D_HANDLER_GREASEPENCIL:
2725                         view3d_panel_gpencil(v3d->blockhandler[a+1]);
2726                         break;
2727                 }
2728                 /* clear action value for event */
2729                 v3d->blockhandler[a+1]= 0;
2730         }
2731         uiDrawBlocksPanels(sa, 0);
2732
2733 }
2734
2735 /* ****************** View3d afterdraw *************** */
2736
2737 typedef struct View3DAfter {
2738         struct View3DAfter *next, *prev;
2739         struct Base *base;
2740         int type, flag;
2741 } View3DAfter;
2742
2743 /* temp storage of Objects that need to be drawn as last */
2744 void add_view3d_after(View3D *v3d, Base *base, int type, int flag)
2745 {
2746         View3DAfter *v3da= MEM_callocN(sizeof(View3DAfter), "View 3d after");
2747
2748         BLI_addtail(&v3d->afterdraw, v3da);
2749         v3da->base= base;
2750         v3da->type= type;
2751         v3da->flag= flag;
2752 }
2753
2754 /* clears zbuffer and draws it over */
2755 static void view3d_draw_xray(View3D *v3d, int clear)
2756 {
2757         View3DAfter *v3da, *next;
2758         int doit= 0;
2759         
2760         for(v3da= v3d->afterdraw.first; v3da; v3da= v3da->next)
2761                 if(v3da->type==V3D_XRAY) doit= 1;
2762         
2763         if(doit) {
2764                 if(clear && v3d->zbuf) glClear(GL_DEPTH_BUFFER_BIT);
2765                 v3d->xray= TRUE;
2766                 
2767                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2768                         next= v3da->next;
2769                         if(v3da->type==V3D_XRAY) {
2770                                 draw_object(v3da->base, v3da->flag);
2771                                 BLI_remlink(&v3d->afterdraw, v3da);
2772                                 MEM_freeN(v3da);
2773                         }
2774                 }
2775                 v3d->xray= FALSE;
2776         }
2777 }
2778
2779 /* disables write in zbuffer and draws it over */
2780 static void view3d_draw_transp(View3D *v3d)
2781 {
2782         View3DAfter *v3da, *next;
2783
2784         glDepthMask(0);
2785         v3d->transp= TRUE;
2786
2787         for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
2788                 next= v3da->next;
2789                 if(v3da->type==V3D_TRANSP) {
2790                         draw_object(v3da->base, v3da->flag);
2791                         BLI_remlink(&v3d->afterdraw, v3da);
2792                         MEM_freeN(v3da);
2793                 }
2794         }
2795         v3d->transp= FALSE;
2796
2797         glDepthMask(1);
2798
2799 }
2800
2801 /* *********************** */
2802
2803 /*
2804         In most cases call draw_dupli_objects,
2805         draw_dupli_objects_color was added because when drawing set dupli's
2806         we need to force the color
2807 */
2808 static void draw_dupli_objects_color(View3D *v3d, Base *base, int color)
2809 {       
2810         ListBase *lb;
2811         DupliObject *dob;
2812         Base tbase;
2813         BoundBox *bb= NULL;
2814         GLuint displist=0;
2815         short transflag, use_displist= -1;      /* -1 is initialize */
2816         char dt, dtx;
2817         
2818         if (base->object->restrictflag & OB_RESTRICT_VIEW) return;
2819         
2820         tbase.flag= OB_FROMDUPLI|base->flag;
2821         lb= object_duplilist(G.scene, base->object);
2822
2823         for(dob= lb->first; dob; dob= dob->next) {
2824                 if(dob->no_draw);
2825                 else {
2826                         tbase.object= dob->ob;
2827                         
2828                         /* extra service: draw the duplicator in drawtype of parent */
2829                         /* MIN2 for the drawtype to allow bounding box objects in groups for lods */
2830                         dt= tbase.object->dt;   tbase.object->dt= MIN2(tbase.object->dt, base->object->dt);
2831                         dtx= tbase.object->dtx; tbase.object->dtx= base->object->dtx;
2832                         
2833                         /* negative scale flag has to propagate */
2834                         transflag= tbase.object->transflag;
2835                         if(base->object->transflag & OB_NEG_SCALE)
2836                                 tbase.object->transflag ^= OB_NEG_SCALE;
2837                         
2838                         BIF_ThemeColorBlend(color, TH_BACK, 0.5);
2839                         
2840                         /* generate displist, test for new object */
2841                         if(use_displist==1 && dob->prev && dob->prev->ob!=dob->ob) {
2842                                 use_displist= -1;
2843                                 glDeleteLists(displist, 1);
2844                         }
2845                         /* generate displist */
2846                         if(use_displist == -1) {
2847                                 
2848                                 /* lamp drawing messes with matrices, could be handled smarter... but this works */
2849                                 if(dob->ob->type==OB_LAMP || dob->type==OB_DUPLIGROUP)
2850                                         use_displist= 0;
2851                                 else {
2852                                         /* disable boundbox check for list creation */
2853                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 1);
2854                                         /* need this for next part of code */
2855                                         bb= object_get_boundbox(dob->ob);
2856                                         
2857                                         Mat4One(dob->ob->obmat);        /* obmat gets restored */
2858                                         
2859                                         displist= glGenLists(1);
2860                                         glNewList(displist, GL_COMPILE);
2861                                         draw_object(&tbase, DRAW_CONSTCOLOR);
2862                                         glEndList();
2863                                         
2864                                         use_displist= 1;
2865                                         object_boundbox_flag(dob->ob, OB_BB_DISABLED, 0);
2866                                 }
2867                         }
2868                         if(use_displist) {
2869                                 mymultmatrix(dob->mat);
2870                                 if(boundbox_clip(dob->mat, bb))
2871                                    glCallList(displist);
2872                                 myloadmatrix(G.vd->viewmat);
2873                         }
2874                         else {
2875                                 Mat4CpyMat4(dob->ob->obmat, dob->mat);
2876                                 draw_object(&tbase, DRAW_CONSTCOLOR);
2877                         }
2878                         
2879                         tbase.object->dt= dt;
2880                         tbase.object->dtx= dtx;
2881                         tbase.object->transflag= transflag;
2882                 }
2883         }
2884         
2885         /* Transp afterdraw disabled, afterdraw only stores base pointers, and duplis can be same obj */
2886         
2887         free_object_duplilist(lb);      /* does restore */
2888         
2889         if(use_displist)
2890                 glDeleteLists(displist, 1);
2891 }
2892
2893 static void draw_dupli_objects(View3D *v3d, Base *base)
2894 {
2895         /* define the color here so draw_dupli_objects_color can be called
2896          * from the set loop */
2897         
2898         int color= (base->flag & SELECT)?TH_SELECT:TH_WIRE;
2899         /* debug */
2900         if(base->object->dup_group && base->object->dup_group->id.us<1)
2901                 color= TH_REDALERT;
2902         
2903         draw_dupli_objects_color(v3d, base, color);
2904 }
2905
2906 void view3d_update_depths(View3D *v3d)
2907 {
2908         /* Create storage for, and, if necessary, copy depth buffer */
2909         if(!v3d->depths) v3d->depths= MEM_callocN(sizeof(ViewDepths),"ViewDepths");
2910         if(v3d->depths) {
2911                 ViewDepths *d= v3d->depths;
2912                 if(d->w != v3d->area->winx ||
2913                    d->h != v3d->area->winy ||
2914                    !d->depths) {
2915                         d->w= v3d->area->winx;
2916                         d->h= v3d->area->winy;
2917                         if(d->depths)
2918                                 MEM_freeN(d->depths);
2919                         d->depths= MEM_mallocN(sizeof(float)*d->w*d->h,"View depths");
2920                         d->damaged= 1;
2921                 }
2922                 
2923                 if(d->damaged) {
2924                         glReadPixels(v3d->area->winrct.xmin,v3d->area->winrct.ymin,d->w,d->h,
2925                                      GL_DEPTH_COMPONENT,GL_FLOAT, d->depths);
2926                         
2927                         glGetDoublev(GL_DEPTH_RANGE,d->depth_range);
2928                         
2929                         d->damaged= 0;
2930                 }
2931         }
2932 }
2933
2934 /* Enable sculpting in wireframe mode by drawing sculpt object only to the depth buffer */
2935 static void draw_sculpt_depths(View3D *v3d)
2936 {
2937         Object *ob = OBACT;
2938
2939         int dt= MIN2(v3d->drawtype, ob->dt);
2940         if(v3d->zbuf==0 && dt>OB_WIRE)
2941                 dt= OB_WIRE;
2942         if(dt == OB_WIRE) {
2943                 GLboolean depth_on;
2944                 int orig_vdt = v3d->drawtype;
2945                 int orig_zbuf = v3d->zbuf;
2946                 int orig_odt = ob->dt;
2947
2948                 glGetBooleanv(GL_DEPTH_TEST, &depth_on);
2949                 v3d->drawtype = ob->dt = OB_SOLID;
2950                 v3d->zbuf = 1;
2951
2952                 glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
2953                 glEnable(GL_DEPTH_TEST);
2954                 draw_object(BASACT, 0);
2955                 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2956                 if(!depth_on)
2957                         glDisable(GL_DEPTH_TEST);
2958
2959                 v3d->drawtype = orig_vdt;
2960                 v3d->zbuf = orig_zbuf;
2961                 ob->dt = orig_odt;
2962         }
2963 }
2964
2965 void draw_depth(ScrArea *sa, void *spacedata, int (* func)(void *))
2966 {
2967         View3D *v3d= spacedata;
2968         Base *base;
2969         Scene *sce;
2970         short zbuf, flag;
2971         float glalphaclip;
2972         /* temp set drawtype to solid */
2973         
2974         /* Setting these temporarily is not nice */
2975         zbuf = v3d->zbuf;
2976         flag = v3d->flag;
2977         glalphaclip = U.glalphaclip;
2978         
2979         U.glalphaclip = 0.5; /* not that nice but means we wont zoom into billboards */
2980         v3d->flag &= ~V3D_SELECT_OUTLINE;
2981
2982         setwinmatrixview3d(sa->winx, sa->winy, NULL);   /* 0= no pick rect */
2983         setviewmatrixview3d();  /* note: calls where_is_object for camera... */
2984         
2985         Mat4MulMat4(v3d->persmat, v3d->viewmat, sa->winmat);
2986         Mat4Invert(v3d->persinv, v3d->persmat);
2987         Mat4Invert(v3d->viewinv, v3d->viewmat);
2988         
2989         glClear(GL_DEPTH_BUFFER_BIT);
2990         
2991         myloadmatrix(v3d->viewmat);
2992         persp(PERSP_STORE);  // store correct view for persp(PERSP_VIEW) calls
2993         
2994         if(v3d->flag & V3D_CLIPPING) {
2995                 view3d_set_clipping(v3d);
2996         }
2997         
2998         v3d->zbuf= TRUE;
2999         glEnable(GL_DEPTH_TEST);
3000         
3001         /* draw set first */
3002         if(G.scene->set) {
3003                 for(SETLOOPER(G.scene->set, base)) {
3004                         if(v3d->lay & base->lay) {
3005                                 if (func == NULL || func(base)) {
3006                                         draw_object(base, 0);
3007                                         if(base->object->transflag & OB_DUPLI) {
3008                                                 draw_dupli_objects_color(v3d, base, TH_WIRE);
3009                                         }
3010                                 }
3011                         }
3012                 }
3013         }
3014         
3015         for(base= G.scene->base.first; base; base= base->next) {
3016                 if(v3d->lay & base->lay) {
3017                         if (func == NULL || func(base)) {
3018                                 /* dupli drawing */
3019                                 if(base->object->transflag & OB_DUPLI) {
3020                                         draw_dupli_objects(v3d, base);
3021                                 }
3022                                 draw_object(base, 0);
3023                         }
3024                 }
3025         }
3026         
3027         /* this isnt that nice, draw xray objects as if they are normal */
3028         if (v3d->afterdraw.first) {
3029                 View3DAfter *v3da, *next;
3030                 int num = 0;
3031                 v3d->xray= TRUE;
3032                 
3033                 glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
3034                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
3035                         next= v3da->next;
3036                         if(v3da->type==V3D_XRAY) {
3037                                 draw_object(v3da->base, 0);
3038                                 num++;
3039                         }
3040                         /* dont remove this time */
3041                 }
3042                 v3d->xray= FALSE;
3043                 
3044                 glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
3045                 for(v3da= v3d->afterdraw.first; v3da; v3da= next) {
3046                         next= v3da->next;
3047                         if(v3da->type==V3D_XRAY) {
3048                                 v3d->xray= TRUE; v3d->transp= FALSE;  
3049                         } else if (v3da->type==V3D_TRANSP) {
3050                                 v3d->xray= FALSE; v3d->transp= TRUE;
3051                         }
3052                         
3053                         draw_object(v3da->base, 0); /* Draw Xray or Transp objects normally */
3054                         BLI_remlink(&v3d->afterdraw, v3da);
3055                         MEM_freeN(v3da);
3056                 }
3057                 v3d->xray= FALSE;