COLLADA branch: merge from trunk -r 24522:24758.
[blender.git] / source / blender / editors / space_view3d / view3d_buttons.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) 2009 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <float.h>
33
34 #include "DNA_ID.h"
35 #include "DNA_action_types.h"
36 #include "DNA_armature_types.h"
37 #include "DNA_curve_types.h"
38 #include "DNA_camera_types.h"
39 #include "DNA_gpencil_types.h"
40 #include "DNA_lamp_types.h"
41 #include "DNA_lattice_types.h"
42 #include "DNA_meta_types.h"
43 #include "DNA_mesh_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_space_types.h"
47 #include "DNA_scene_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_userdef_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_world_types.h"
52
53 #include "MEM_guardedalloc.h"
54
55 #include "BLI_math.h"
56 #include "BLI_blenlib.h"
57 #include "BLI_editVert.h"
58 #include "BLI_rand.h"
59
60 #include "BKE_action.h"
61 #include "BKE_brush.h"
62 #include "BKE_context.h"
63 #include "BKE_curve.h"
64 #include "BKE_customdata.h"
65 #include "BKE_depsgraph.h"
66 #include "BKE_idprop.h"
67 #include "BKE_mesh.h"
68 #include "BKE_object.h"
69 #include "BKE_global.h"
70 #include "BKE_scene.h"
71 #include "BKE_screen.h"
72 #include "BKE_utildefines.h"
73
74 #include "BIF_gl.h"
75
76 #include "WM_api.h"
77 #include "WM_types.h"
78
79 #include "RNA_access.h"
80 #include "RNA_define.h"
81
82 #include "ED_armature.h"
83 #include "ED_curve.h"
84 #include "ED_image.h"
85 #include "ED_gpencil.h"
86 #include "ED_keyframing.h"
87 #include "ED_mesh.h"
88 #include "ED_object.h"
89 #include "ED_particle.h"
90 #include "ED_screen.h"
91 #include "ED_transform.h"
92 #include "ED_types.h"
93 #include "ED_util.h"
94
95 #include "UI_interface.h"
96 #include "UI_resources.h"
97 #include "UI_view2d.h"
98
99 #include "view3d_intern.h"      // own include
100
101
102 /* ******************* view3d space & buttons ************** */
103 #define B_NOP           1
104 #define B_REDR          2
105 #define B_OBJECTPANELROT        1007
106 #define B_OBJECTPANELMEDIAN 1008
107 #define B_ARMATUREPANEL1        1009
108 #define B_ARMATUREPANEL2        1010
109 #define B_OBJECTPANELPARENT 1011
110 #define B_OBJECTPANEL           1012
111 #define B_ARMATUREPANEL3        1013
112 #define B_OBJECTPANELSCALE      1014
113 #define B_OBJECTPANELDIMS       1015
114 #define B_TRANSFORMSPACEADD     1016
115 #define B_TRANSFORMSPACECLEAR   1017
116 #define B_SETPT_AUTO    2125
117 #define B_SETPT_VECTOR  2126
118 #define B_SETPT_ALIGN   2127
119 #define B_SETPT_FREE    2128
120 #define B_RECALCMBALL   2501
121
122 #define B_WEIGHT0_0             2840
123 #define B_WEIGHT1_4             2841
124 #define B_WEIGHT1_2             2842
125 #define B_WEIGHT3_4             2843
126 #define B_WEIGHT1_0             2844
127
128 #define B_OPA1_8                2845
129 #define B_OPA1_4                2846
130 #define B_OPA1_2                2847
131 #define B_OPA3_4                2848
132 #define B_OPA1_0                2849
133
134 #define B_CLR_WPAINT    2850
135
136 #define B_RV3D_LOCKED   2900
137 #define B_RV3D_BOXVIEW  2901
138 #define B_RV3D_BOXCLIP  2902
139
140 #define B_IDNAME                3000
141
142 /* temporary struct for storing transform properties */
143 typedef struct {
144         float ob_eul[4];        // used for quat too....
145         float ob_scale[3]; // need temp space due to linked values
146         float ob_dims[3];
147         short link_scale;
148         float ve_median[5];
149         int curdef;
150         float *defweightp;
151 } TransformProperties;
152
153
154 /* is used for both read and write... */
155 static void v3d_editvertex_buts(const bContext *C, uiLayout *layout, View3D *v3d, Object *ob, float lim)
156 {
157         uiBlock *block= (layout)? uiLayoutAbsoluteBlock(layout): NULL;
158         MDeformVert *dvert=NULL;
159         TransformProperties *tfp= v3d->properties_storage;
160         float median[5], ve_median[5];
161         int tot, totw, totweight, totedge;
162         char defstr[320];
163         
164         median[0]= median[1]= median[2]= median[3]= median[4]= 0.0;
165         tot= totw= totweight= totedge= 0;
166         defstr[0]= 0;
167
168         if(ob->type==OB_MESH) {
169                 Mesh *me= ob->data;
170                 EditMesh *em = BKE_mesh_get_editmesh(me);
171                 EditVert *eve, *evedef=NULL;
172                 EditEdge *eed;
173                 
174                 eve= em->verts.first;
175                 while(eve) {
176                         if(eve->f & SELECT) {
177                                 evedef= eve;
178                                 tot++;
179                                 add_v3_v3v3(median, median, eve->co);
180                         }
181                         eve= eve->next;
182                 }
183                 eed= em->edges.first;
184                 while(eed) {
185                         if((eed->f & SELECT)) {
186                                 totedge++;
187                                 median[3]+= eed->crease;
188                         }
189                         eed= eed->next;
190                 }
191
192                 /* check for defgroups */
193                 if(evedef)
194                         dvert= CustomData_em_get(&em->vdata, evedef->data, CD_MDEFORMVERT);
195                 if(tot==1 && dvert && dvert->totweight) {
196                         bDeformGroup *dg;
197                         int i, max=1, init=1;
198                         char str[320];
199                         
200                         for (i=0; i<dvert->totweight; i++){
201                                 dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
202                                 if(dg) {
203                                         max+= BLI_snprintf(str, sizeof(str), "%s %%x%d|", dg->name, dvert->dw[i].def_nr); 
204                                         if(max<320) strcat(defstr, str);
205                                 }
206                                 else printf("oh no!\n");
207                                 if(tfp->curdef==dvert->dw[i].def_nr) {
208                                         init= 0;
209                                         tfp->defweightp= &dvert->dw[i].weight;
210                                 }
211                         }
212                         
213                         if(init) {      // needs new initialized 
214                                 tfp->curdef= dvert->dw[0].def_nr;
215                                 tfp->defweightp= &dvert->dw[0].weight;
216                         }
217                 }
218
219                 BKE_mesh_end_editmesh(me, em);
220         }
221         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
222                 Curve *cu= ob->data;
223                 Nurb *nu;
224                 BPoint *bp;
225                 BezTriple *bezt;
226                 int a;
227                 
228                 nu= cu->editnurb->first;
229                 while(nu) {
230                         if(nu->type == CU_BEZIER) {
231                                 bezt= nu->bezt;
232                                 a= nu->pntsu;
233                                 while(a--) {
234                                         if(bezt->f2 & SELECT) {
235                                                 add_v3_v3v3(median, median, bezt->vec[1]);
236                                                 tot++;
237                                                 median[4]+= bezt->weight;
238                                                 totweight++;
239                                         }
240                                         else {
241                                                 if(bezt->f1 & SELECT) {
242                                                         add_v3_v3v3(median, median, bezt->vec[0]);
243                                                         tot++;
244                                                 }
245                                                 if(bezt->f3 & SELECT) {
246                                                         add_v3_v3v3(median, median, bezt->vec[2]);
247                                                         tot++;
248                                                 }
249                                         }
250                                         bezt++;
251                                 }
252                         }
253                         else {
254                                 bp= nu->bp;
255                                 a= nu->pntsu*nu->pntsv;
256                                 while(a--) {
257                                         if(bp->f1 & SELECT) {
258                                                 add_v3_v3v3(median, median, bp->vec);
259                                                 median[3]+= bp->vec[3];
260                                                 totw++;
261                                                 tot++;
262                                                 median[4]+= bp->weight;
263                                                 totweight++;
264                                         }
265                                         bp++;
266                                 }
267                         }
268                         nu= nu->next;
269                 }
270         }
271         else if(ob->type==OB_LATTICE) {
272                 Lattice *lt= ob->data;
273                 BPoint *bp;
274                 int a;
275                 
276                 a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
277                 bp= lt->editlatt->def;
278                 while(a--) {
279                         if(bp->f1 & SELECT) {
280                                 add_v3_v3v3(median, median, bp->vec);
281                                 tot++;
282                                 median[4]+= bp->weight;
283                                 totweight++;
284                         }
285                         bp++;
286                 }
287         }
288         
289         if(tot==0) return;
290
291         median[0] /= (float)tot;
292         median[1] /= (float)tot;
293         median[2] /= (float)tot;
294         if(totedge) median[3] /= (float)totedge;
295         else if(totw) median[3] /= (float)totw;
296         if(totweight) median[4] /= (float)totweight;
297         
298         if(v3d->flag & V3D_GLOBAL_STATS)
299                 mul_m4_v3(ob->obmat, median);
300         
301         if(block) {     // buttons
302                 int but_y;
303                 if((ob->parent) && (ob->partype == PARBONE))    but_y = 135;
304                 else                                                                                    but_y = 150;
305                 
306                 
307                 
308                 memcpy(tfp->ve_median, median, sizeof(tfp->ve_median));
309                 
310                 uiBlockBeginAlign(block);
311                 if(tot==1) {
312                         uiDefBut(block, LABEL, 0, "Vertex:",                                    0, 130, 200, 20, 0, 0, 0, 0, 0, "");
313                         uiBlockBeginAlign(block);
314                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "X:",                0, 110, 200, 20, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
315                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Y:",                0, 90, 200, 20, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
316                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Z:",                0, 70, 200, 20, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
317                         
318                         if(totw==1) {
319                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "W:",        0, 50, 200, 20, &(tfp->ve_median[3]), 0.01, 100.0, 10, 3, "");
320                                 uiBlockBeginAlign(block);
321                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
322                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
323                                 uiBlockEndAlign(block);
324                                 if(totweight)
325                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 0, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "");
326                                 }
327                         else {
328                                 uiBlockBeginAlign(block);
329                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
330                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
331                                 uiBlockEndAlign(block);
332                                 if(totweight)
333                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 20, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "");
334                         }
335                 }
336                 else {
337                         uiDefBut(block, LABEL, 0, "Median:",                                    0, 130, 200, 20, 0, 0, 0, 0, 0, "");
338                         uiBlockBeginAlign(block);
339                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "X:",                0, 110, 200, 20, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
340                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Y:",                0, 90, 200, 20, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
341                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Z:",                0, 70, 200, 20, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
342                         if(totw==tot) {
343                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "W:",        0, 50, 200, 20, &(tfp->ve_median[3]), 0.01, 100.0, 10, 3, "");
344                                 uiBlockEndAlign(block);
345                                 uiBlockBeginAlign(block);
346                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
347                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
348                                 uiBlockEndAlign(block);
349                                 if(totweight)
350                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 0, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
351                                 uiBlockEndAlign(block);
352                         }
353                         else {
354                                 uiBlockBeginAlign(block);
355                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
356                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
357                                 uiBlockEndAlign(block);
358                                 if(totweight)
359                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 20, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "Weight is used for SoftBody Goal");
360                                 uiBlockEndAlign(block);
361                         }
362                 }
363                                 
364                 if(totedge==1)
365                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Crease:",   0, 20, 200, 20, &(tfp->ve_median[3]), 0.0, 1.0, 10, 3, "");
366                 else if(totedge>1)
367                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Median Crease:",    0, 20, 200, 20, &(tfp->ve_median[3]), 0.0, 1.0, 10, 3, "");
368                 
369         }
370         else {  // apply
371                 memcpy(ve_median, tfp->ve_median, sizeof(tfp->ve_median));
372                 
373                 if(v3d->flag & V3D_GLOBAL_STATS) {
374                         invert_m4_m4(ob->imat, ob->obmat);
375                         mul_m4_v3(ob->imat, median);
376                         mul_m4_v3(ob->imat, ve_median);
377                 }
378                 sub_v3_v3v3(median, ve_median, median);
379                 median[3]= ve_median[3]-median[3];
380                 median[4]= ve_median[4]-median[4];
381                 
382                 if(ob->type==OB_MESH) {
383                         Mesh *me= ob->data;
384                         EditMesh *em = BKE_mesh_get_editmesh(me);
385                         EditVert *eve;
386                         EditEdge *eed;
387                         
388                         eve= em->verts.first;
389                         while(eve) {
390                                 if(eve->f & SELECT) {
391                                         add_v3_v3v3(eve->co, eve->co, median);
392                                 }
393                                 eve= eve->next;
394                         }
395                         
396                         for(eed= em->edges.first; eed; eed= eed->next) {
397                                 if(eed->f & SELECT) {
398                                         /* ensure the median can be set to zero or one */
399                                         if(ve_median[3]==0.0f) eed->crease= 0.0f;
400                                         else if(ve_median[3]==1.0f) eed->crease= 1.0f;
401                                         else {
402                                                 eed->crease+= median[3];
403                                                 CLAMP(eed->crease, 0.0, 1.0);
404                                         }
405                                 }
406                         }
407                         
408                         recalc_editnormals(em);
409
410                         BKE_mesh_end_editmesh(me, em);
411                 }
412                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
413                         Curve *cu= ob->data;
414                         Nurb *nu;
415                         BPoint *bp;
416                         BezTriple *bezt;
417                         int a;
418                         
419                         nu= cu->editnurb->first;
420                         while(nu) {
421                                 if(nu->type == CU_BEZIER) {
422                                         bezt= nu->bezt;
423                                         a= nu->pntsu;
424                                         while(a--) {
425                                                 if(bezt->f2 & SELECT) {
426                                                         add_v3_v3v3(bezt->vec[0], bezt->vec[0], median);
427                                                         add_v3_v3v3(bezt->vec[1], bezt->vec[1], median);
428                                                         add_v3_v3v3(bezt->vec[2], bezt->vec[2], median);
429                                                         bezt->weight+= median[4];
430                                                 }
431                                                 else {
432                                                         if(bezt->f1 & SELECT) {
433                                                                 add_v3_v3v3(bezt->vec[0], bezt->vec[0], median);
434                                                         }
435                                                         if(bezt->f3 & SELECT) {
436                                                                 add_v3_v3v3(bezt->vec[2], bezt->vec[2], median);
437                                                         }
438                                                 }
439                                                 bezt++;
440                                         }
441                                 }
442                                 else {
443                                         bp= nu->bp;
444                                         a= nu->pntsu*nu->pntsv;
445                                         while(a--) {
446                                                 if(bp->f1 & SELECT) {
447                                                         add_v3_v3v3(bp->vec, bp->vec, median);
448                                                         bp->vec[3]+= median[3];
449                                                         bp->weight+= median[4];
450                                                 }
451                                                 bp++;
452                                         }
453                                 }
454                                 test2DNurb(nu);
455                                 testhandlesNurb(nu); /* test for bezier too */
456
457                                 nu= nu->next;
458                         }
459                 }
460                 else if(ob->type==OB_LATTICE) {
461                         Lattice *lt= ob->data;
462                         BPoint *bp;
463                         int a;
464                         
465                         a= lt->editlatt->pntsu*lt->editlatt->pntsv*lt->editlatt->pntsw;
466                         bp= lt->editlatt->def;
467                         while(a--) {
468                                 if(bp->f1 & SELECT) {
469                                         add_v3_v3v3(bp->vec, bp->vec, median);
470                                         bp->weight+= median[4];
471                                 }
472                                 bp++;
473                         }
474                 }
475                 
476 //              ED_undo_push(C, "Transform properties");
477         }
478 }
479
480 #if 0
481 /* assumes armature active */
482 static void validate_bonebutton_cb(bContext *C, void *bonev, void *namev)
483 {
484         Object *ob= CTX_data_active_object(C);
485         
486         if(ob && ob->type==OB_ARMATURE) {
487                 Bone *bone= bonev;
488                 char oldname[32], newname[32];
489                 
490                 /* need to be on the stack */
491                 BLI_strncpy(newname, bone->name, 32);
492                 BLI_strncpy(oldname, (char *)namev, 32);
493                 /* restore */
494                 BLI_strncpy(bone->name, oldname, 32);
495                 
496                 ED_armature_bone_rename(ob->data, oldname, newname); // editarmature.c
497         }
498 }
499 #endif
500
501 static void v3d_transform_butsR(uiLayout *layout, PointerRNA *ptr)
502 {
503         uiLayout *split, *colsub;
504         
505         split = uiLayoutSplit(layout, 0.8);
506         
507         if (ptr->type == &RNA_PoseBone) {
508                 PointerRNA boneptr;
509                 Bone *bone;
510                 
511                 boneptr = RNA_pointer_get(ptr, "bone");
512                 bone = boneptr.data;
513                 uiLayoutSetActive(split, !(bone->parent && bone->flag & BONE_CONNECTED));
514         }
515         colsub = uiLayoutColumn(split, 1);
516         uiItemR(colsub, "Location", 0, ptr, "location", 0);
517         colsub = uiLayoutColumn(split, 1);
518         uiItemL(colsub, "", 0);
519         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_location", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
520         
521         split = uiLayoutSplit(layout, 0.8);
522         
523         switch(RNA_enum_get(ptr, "rotation_mode")) {
524                 case ROT_MODE_XYZ:
525                 case ROT_MODE_XZY:
526                 case ROT_MODE_YXZ:
527                 case ROT_MODE_YZX:
528                 case ROT_MODE_ZXY:
529                 case ROT_MODE_ZYX:
530                         colsub = uiLayoutColumn(split, 1);
531                         uiItemR(colsub, "Rotation", 0, ptr, "rotation_euler", 0);
532                         colsub = uiLayoutColumn(split, 1);
533                         uiItemL(colsub, "", 0);
534                         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
535                         break;
536                 case ROT_MODE_QUAT:
537                         colsub = uiLayoutColumn(split, 1);
538                         uiItemR(colsub, "Rotation", 0, ptr, "rotation_quaternion", 0);
539                         colsub = uiLayoutColumn(split, 1);
540                         uiItemR(colsub, "W", 0, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE);
541                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
542                                 uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
543                         else
544                                 uiItemL(colsub, "", 0);
545                         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
546                         break;
547                 case ROT_MODE_AXISANGLE:
548                         colsub = uiLayoutColumn(split, 1);
549                         uiItemR(colsub, "Rotation", 0, ptr, "rotation_axis_angle", 0);
550                         colsub = uiLayoutColumn(split, 1);
551                         uiItemR(colsub, "W", 0, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE);
552                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
553                                 uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
554                         else
555                                 uiItemL(colsub, "", 0);
556                         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
557                         break;
558         }
559         uiItemR(layout, "", 0, ptr, "rotation_mode", 0);
560         
561         split = uiLayoutSplit(layout, 0.8);
562         colsub = uiLayoutColumn(split, 1);
563         uiItemR(colsub, "Scale", 0, ptr, "scale", 0);
564         colsub = uiLayoutColumn(split, 1);
565         uiItemL(colsub, "", 0);
566         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_scale", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
567         
568         if (ptr->type == &RNA_Object) {
569                 Object *ob = ptr->data;
570                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
571                         uiItemR(layout, "Dimensions", 0, ptr, "dimensions", 0);
572         }
573 }
574
575 static void v3d_posearmature_buts(uiLayout *layout, View3D *v3d, Object *ob, float lim)
576 {
577 //      uiBlock *block= uiLayoutGetBlock(layout);
578 //      bArmature *arm;
579         bPoseChannel *pchan;
580 //      TransformProperties *tfp= v3d->properties_storage;
581         PointerRNA pchanptr;
582         uiLayout *col;
583 //      uiLayout *row;
584
585         pchan= get_active_posechannel(ob);
586
587 //      row= uiLayoutRow(layout, 0);
588         
589         if (!pchan)     {
590                 uiItemL(layout, "No Bone Active", 0);
591                 return; 
592         }
593
594         RNA_pointer_create(&ob->id, &RNA_PoseBone, pchan, &pchanptr);
595
596         col= uiLayoutColumn(layout, 0);
597         
598         /* XXX: RNA buts show data in native types (i.e. quats, 4-component axis/angle, etc.)
599          * but oldskool UI shows in eulers always. Do we want to be able to still display in Eulers?
600          * Maybe needs RNA/ui options to display rotations as different types... */
601         v3d_transform_butsR(col, &pchanptr);
602
603 #if 0
604         uiLayoutAbsoluteBlock(layout);
605
606         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
607                 float quat[4];
608                 /* convert to euler, passing through quats... */
609                 axis_angle_to_quat(quat, pchan->rotAxis, pchan->rotAngle);
610                 quat_to_eul( tfp->ob_eul,quat);
611         }
612         else if (pchan->rotmode == ROT_MODE_QUAT)
613                 quat_to_eul( tfp->ob_eul,pchan->quat);
614         else
615                 copy_v3_v3(tfp->ob_eul, pchan->eul);
616         tfp->ob_eul[0]*= 180.0/M_PI;
617         tfp->ob_eul[1]*= 180.0/M_PI;
618         tfp->ob_eul[2]*= 180.0/M_PI;
619         
620         uiDefBut(block, LABEL, 0, "Location:",                  0, 240, 100, 20, 0, 0, 0, 0, 0, "");
621         uiBlockBeginAlign(block);
622         uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",   0, 220, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
623         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",   0, 200, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
624         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",   0, 180, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
625         uiBlockEndAlign(block);
626         
627         uiBlockBeginAlign(block);
628         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCX, B_REDR, ICON_UNLOCKED,   125, 220, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects X Location value from being Transformed");
629         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCY, B_REDR, ICON_UNLOCKED,   125, 200, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Y Location value from being Transformed");
630         uiDefIconButBitS(block, ICONTOG, OB_LOCK_LOCZ, B_REDR, ICON_UNLOCKED,   125, 180, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Z Location value from being Transformed");
631         uiBlockEndAlign(block);
632         
633         uiDefBut(block, LABEL, 0, "Rotation:",                  0, 160, 100, 20, 0, 0, 0, 0, 0, "");
634         uiBlockBeginAlign(block);
635         uiDefButF(block, NUM, B_ARMATUREPANEL3, "X:",   0, 140, 120, 19, tfp->ob_eul, -1000.0, 1000.0, 100, 3, "");
636         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Y:",   0, 120, 120, 19, tfp->ob_eul+1, -1000.0, 1000.0, 100, 3, "");
637         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Z:",   0, 100, 120, 19, tfp->ob_eul+2, -1000.0, 1000.0, 100, 3, "");
638         uiBlockEndAlign(block);
639         
640         uiBlockBeginAlign(block);
641         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTX, B_REDR, ICON_UNLOCKED,   125, 140, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects X Rotation value from being Transformed");
642         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTY, B_REDR, ICON_UNLOCKED,   125, 120, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Y Rotation value from being Transformed");
643         uiDefIconButBitS(block, ICONTOG, OB_LOCK_ROTZ, B_REDR, ICON_UNLOCKED,   125, 100, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Z Rotation value from being Transformed");
644         uiBlockEndAlign(block);
645         
646         uiDefBut(block, LABEL, 0, "Scale:",                             0, 80, 100, 20, 0, 0, 0, 0, 0, "");
647         uiBlockBeginAlign(block);
648         uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",   0, 60, 120, 19, pchan->size, -lim, lim, 10, 3, "");
649         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",   0, 40, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
650         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",   0, 20, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
651         uiBlockEndAlign(block);
652         
653         uiBlockBeginAlign(block);
654         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEX, B_REDR, ICON_UNLOCKED, 125, 60, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects X Scale value from being Transformed");
655         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEY, B_REDR, ICON_UNLOCKED, 125, 40, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects Y Scale value from being Transformed");
656         uiDefIconButBitS(block, ICONTOG, OB_LOCK_SCALEZ, B_REDR, ICON_UNLOCKED, 125, 20, 25, 19, &(pchan->protectflag), 0, 0, 0, 0, "Protects z Scale value from being Transformed");
657         uiBlockEndAlign(block);
658 #endif
659 }
660
661 /* assumes armature editmode */
662 void validate_editbonebutton_cb(bContext *C, void *bonev, void *namev)
663 {
664         EditBone *eBone= bonev;
665         char oldname[32], newname[32];
666         
667         /* need to be on the stack */
668         BLI_strncpy(newname, eBone->name, 32);
669         BLI_strncpy(oldname, (char *)namev, 32);
670         /* restore */
671         BLI_strncpy(eBone->name, oldname, 32);
672         
673         ED_armature_bone_rename(CTX_data_edit_object(C)->data, oldname, newname); // editarmature.c
674         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, CTX_data_edit_object(C)); // XXX fix
675 }
676
677 static void v3d_editarmature_buts(uiLayout *layout, View3D *v3d, Object *ob, float lim)
678 {
679 //      uiBlock *block= uiLayoutGetBlock(layout);
680         bArmature *arm= ob->data;
681         EditBone *ebone;
682 //      TransformProperties *tfp= v3d->properties_storage;
683 //      uiLayout *row;
684         uiLayout *col;
685         PointerRNA eboneptr;
686         
687         ebone= arm->act_edbone;
688
689         if (!ebone || (ebone->layer & arm->layer)==0)
690                 return;
691         
692 //      row= uiLayoutRow(layout, 0);
693         RNA_pointer_create(&arm->id, &RNA_EditBone, ebone, &eboneptr);
694
695
696         col= uiLayoutColumn(layout, 0);
697         uiItemR(col, "Head", 0, &eboneptr, "head", 0);
698         if (ebone->parent && ebone->flag & BONE_CONNECTED ) {
699                 PointerRNA parptr = RNA_pointer_get(&eboneptr, "parent");
700                 uiItemR(col, "Radius", 0, &parptr, "tail_radius", 0);
701         } else {
702                 uiItemR(col, "Radius", 0, &eboneptr, "head_radius", 0);
703         }
704         
705         uiItemR(col, "Tail", 0, &eboneptr, "tail", 0);
706         uiItemR(col, "Radius", 0, &eboneptr, "tail_radius", 0);
707         
708         uiItemR(col, "Roll", 0, &eboneptr, "roll", 0);
709 }
710
711 static void v3d_editmetaball_buts(uiLayout *layout, Object *ob, float lim)
712 {
713         PointerRNA mbptr, ptr;
714         MetaBall *mball= ob->data;
715 //      uiLayout *row;
716         uiLayout *col;
717         
718         if (!mball || !(mball->lastelem)) return;
719         
720         RNA_pointer_create(&mball->id, &RNA_MetaBall, mball, &mbptr);
721         
722 //      row= uiLayoutRow(layout, 0);
723
724         RNA_pointer_create(&mball->id, &RNA_MetaElement, mball->lastelem, &ptr);
725         
726         col= uiLayoutColumn(layout, 0);
727         uiItemR(col, "Location", 0, &ptr, "location", 0);
728         
729         uiItemR(col, "Radius", 0, &ptr, "radius", 0);
730         uiItemR(col, "Stiffness", 0, &ptr, "stiffness", 0);
731         
732         uiItemR(col, "Type", 0, &ptr, "type", 0);
733         
734         col= uiLayoutColumn(layout, 1);
735         switch (RNA_enum_get(&ptr, "type")) {
736                 case MB_BALL:
737                         break;
738                 case MB_CUBE:
739                         uiItemL(col, "Size:", 0);
740                         uiItemR(col, "X", 0, &ptr, "size_x", 0);
741                         uiItemR(col, "Y", 0, &ptr, "size_y", 0);
742                         uiItemR(col, "Z", 0, &ptr, "size_z", 0);
743                         break;
744                 case MB_TUBE:
745                         uiItemL(col, "Size:", 0);
746                         uiItemR(col, "X", 0, &ptr, "size_x", 0);
747                         break;
748                 case MB_PLANE:
749                         uiItemL(col, "Size:", 0);
750                         uiItemR(col, "X", 0, &ptr, "size_x", 0);
751                         uiItemR(col, "Y", 0, &ptr, "size_y", 0);
752                         break;
753                 case MB_ELIPSOID:
754                         uiItemL(col, "Size:", 0);
755                         uiItemR(col, "X", 0, &ptr, "size_x", 0);
756                         uiItemR(col, "Y", 0, &ptr, "size_y", 0);
757                         uiItemR(col, "Z", 0, &ptr, "size_z", 0);
758                         break;             
759         }       
760 }
761
762 /* test if 'ob' is a parent somewhere in par's parents */
763 static int test_parent_loop(Object *par, Object *ob)
764 {
765         if(par == NULL) return 0;
766         if(ob == par) return 1;
767         return test_parent_loop(par->parent, ob);
768 }
769
770 static void do_view3d_region_buttons(bContext *C, void *arg, int event)
771 {
772         Scene *scene= CTX_data_scene(C);
773 //      Object *obedit= CTX_data_edit_object(C);
774         View3D *v3d= CTX_wm_view3d(C);
775 //      BoundBox *bb;
776         Object *ob= OBACT;
777         TransformProperties *tfp= v3d->properties_storage;
778         
779         switch(event) {
780         
781         case B_REDR:
782                 ED_area_tag_redraw(CTX_wm_area(C));
783                 return; /* no notifier! */
784                 
785         case B_OBJECTPANEL:
786                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);
787                 break;
788
789         
790         case B_OBJECTPANELMEDIAN:
791                 if(ob) {
792                         v3d_editvertex_buts(C, NULL, v3d, ob, 1.0);
793                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
794                 }
795                 break;
796                 
797                 /* note; this case also used for parbone */
798         case B_OBJECTPANELPARENT:
799                 if(ob) {
800                         if(ob->id.lib || test_parent_loop(ob->parent, ob) ) 
801                                 ob->parent= NULL;
802                         else {
803                                 DAG_scene_sort(scene);
804                                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);
805                         }
806                 }
807                 break;
808                 
809
810         case B_ARMATUREPANEL3:  // rotate button on channel
811                 {
812                         bPoseChannel *pchan;
813                         float eul[3];
814                         
815                         pchan= get_active_posechannel(ob);
816                         if (!pchan) return;
817                         
818                         /* make a copy to eul[3], to allow TAB on buttons to work */
819                         eul[0]= M_PI*tfp->ob_eul[0]/180.0;
820                         eul[1]= M_PI*tfp->ob_eul[1]/180.0;
821                         eul[2]= M_PI*tfp->ob_eul[2]/180.0;
822                         
823                         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
824                                 float quat[4];
825                                 /* convert to axis-angle, passing through quats  */
826                                 eul_to_quat( quat,eul);
827                                 quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,quat);
828                         }
829                         else if (pchan->rotmode == ROT_MODE_QUAT)
830                                 eul_to_quat( pchan->quat,eul);
831                         else
832                                 copy_v3_v3(pchan->eul, eul);
833                 }
834                 /* no break, pass on */
835         case B_ARMATUREPANEL2:
836                 {
837                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
838                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
839                 }
840                 break;
841         case B_TRANSFORMSPACEADD:
842                 BIF_createTransformOrientation(C, NULL, "", 1, 0);
843                 break;
844         case B_TRANSFORMSPACECLEAR:
845                 BIF_clearTransformOrientation(C);
846                 break;
847                 
848 #if 0 // XXX
849         case B_WEIGHT0_0:
850                 wpaint->weight = 0.0f;
851                 break;
852                 
853         case B_WEIGHT1_4:
854                 wpaint->weight = 0.25f;
855                 break;
856         case B_WEIGHT1_2:
857                 wpaint->weight = 0.5f;
858                 break;
859         case B_WEIGHT3_4:
860                 wpaint->weight = 0.75f;
861                 break;
862         case B_WEIGHT1_0:
863                 wpaint->weight = 1.0f;
864                 break;
865                 
866         case B_OPA1_8:
867                 wpaint->a = 0.125f;
868                 break;
869         case B_OPA1_4:
870                 wpaint->a = 0.25f;
871                 break;
872         case B_OPA1_2:
873                 wpaint->a = 0.5f;
874                 break;
875         case B_OPA3_4:
876                 wpaint->a = 0.75f;
877                 break;
878         case B_OPA1_0:
879                 wpaint->a = 1.0f;
880                 break;
881 #endif
882         case B_CLR_WPAINT:
883 //              if(!multires_level1_test()) {
884                 {
885                         bDeformGroup *defGroup = BLI_findlink(&ob->defbase, ob->actdef-1);
886                         if(defGroup) {
887                                 Mesh *me= ob->data;
888                                 int a;
889                                 for(a=0; a<me->totvert; a++)
890                                         ED_vgroup_vert_remove (ob, defGroup, a);
891                                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
892                         }
893                 }
894                 break;
895         case B_RV3D_LOCKED:
896         case B_RV3D_BOXVIEW:
897         case B_RV3D_BOXCLIP:
898                 {
899                         ScrArea *sa= CTX_wm_area(C);
900                         ARegion *ar= sa->regionbase.last;
901                         RegionView3D *rv3d;
902                         short viewlock;
903                         
904                         ar= ar->prev;
905                         rv3d= ar->regiondata;
906                         viewlock= rv3d->viewlock;
907                         
908                         if((viewlock & RV3D_LOCKED)==0)
909                                 viewlock= 0;
910                         else if((viewlock & RV3D_BOXVIEW)==0)
911                                 viewlock &= ~RV3D_BOXCLIP;
912                         
913                         for(; ar; ar= ar->prev) {
914                                 if(ar->alignment==RGN_ALIGN_QSPLIT) {
915                                         rv3d= ar->regiondata;
916                                         rv3d->viewlock= viewlock;
917                                 }
918                         }
919                         
920                         if(rv3d->viewlock & RV3D_BOXVIEW)
921                                 view3d_boxview_copy(sa, sa->regionbase.last);
922                         
923                         ED_area_tag_redraw(sa);
924                 }
925                 break;
926         }
927
928         /* default for now */
929         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
930 }
931
932 void removeTransformOrientation_func(bContext *C, void *target, void *unused)
933 {
934         BIF_removeTransformOrientation(C, (TransformOrientation *) target);
935 }
936
937 void selectTransformOrientation_func(bContext *C, void *target, void *unused)
938 {
939         BIF_selectTransformOrientation(C, (TransformOrientation *) target);
940 }
941
942 #if 0 // XXX not used
943 static void view3d_panel_transform_spaces(const bContext *C, Panel *pa)
944 {
945         Scene *scene= CTX_data_scene(C);
946         Object *obedit= CTX_data_edit_object(C);
947         View3D *v3d= CTX_wm_view3d(C);
948         ListBase *transform_spaces = &scene->transform_spaces;
949         TransformOrientation *ts = transform_spaces->first;
950         uiBlock *block;
951         uiBut *but;
952         int xco = 20, yco = 70;
953         int index;
954
955         block= uiLayoutAbsoluteBlock(pa->layout);
956
957         uiBlockBeginAlign(block);
958         
959         if (obedit)
960                 uiDefBut(block, BUT, B_TRANSFORMSPACEADD, "Add", xco,120,80,20, 0, 0, 0, 0, 0, "Add the selected element as a Transform Orientation");
961         else
962                 uiDefBut(block, BUT, B_TRANSFORMSPACEADD, "Add", xco,120,80,20, 0, 0, 0, 0, 0, "Add the active object as a Transform Orientation");
963
964         uiDefBut(block, BUT, B_TRANSFORMSPACECLEAR, "Clear", xco + 80,120,80,20, 0, 0, 0, 0, 0, "Removal all Transform Orientations");
965         
966         uiBlockEndAlign(block);
967         
968         uiBlockBeginAlign(block);
969         
970         uiDefButS(block, ROW, B_REDR, "Global", xco,            90, 40,20, &v3d->twmode, 5.0, (float)V3D_MANIP_GLOBAL,0, 0, "Global Transform Orientation");
971         uiDefButS(block, ROW, B_REDR, "Local",  xco + 40,       90, 40,20, &v3d->twmode, 5.0, (float)V3D_MANIP_LOCAL, 0, 0, "Local Transform Orientation");
972         uiDefButS(block, ROW, B_REDR, "Normal", xco + 80,       90, 40,20, &v3d->twmode, 5.0, (float)V3D_MANIP_NORMAL,0, 0, "Normal Transform Orientation");
973         uiDefButS(block, ROW, B_REDR, "View",           xco + 120,      90, 40,20, &v3d->twmode, 5.0, (float)V3D_MANIP_VIEW,    0, 0, "View Transform Orientation");
974         
975         for (index = V3D_MANIP_CUSTOM, ts = transform_spaces->first ; ts ; ts = ts->next, index++) {
976
977                 if (v3d->twmode == index) {
978                         but = uiDefIconButS(block,ROW, B_REDR, ICON_CHECKBOX_HLT, xco,yco,XIC,YIC, &v3d->twmode, 5.0, (float)index, 0, 0, "Use this Custom Transform Orientation");
979                 }
980                 else {
981                         but = uiDefIconButS(block,ROW, B_REDR, ICON_CHECKBOX_DEHLT, xco,yco,XIC,YIC, &v3d->twmode, 5.0, (float)index, 0, 0, "Use this Custom Transform Orientation");
982                 }
983                 uiButSetFunc(but, selectTransformOrientation_func, ts, NULL);
984                 uiDefBut(block, TEX, 0, "", xco+=XIC, yco,100+XIC,20, &ts->name, 0, 30, 0, 0, "Edits the name of this Transform Orientation");
985                 but = uiDefIconBut(block, BUT, B_REDR, ICON_X, xco+=100+XIC,yco,XIC,YIC, 0, 0, 0, 0, 0, "Deletes this Transform Orientation");
986                 uiButSetFunc(but, removeTransformOrientation_func, ts, NULL);
987
988                 xco = 20;
989                 yco -= 25;
990         }
991         uiBlockEndAlign(block);
992 }
993 #endif // XXX not used
994
995 #if 0
996 static void brush_idpoin_handle(bContext *C, ID *id, int event)
997 {
998         Brush **br = current_brush_source(CTX_data_scene(C));
999
1000         if(!br)
1001                 return;
1002
1003         switch(event) {
1004         case UI_ID_BROWSE:
1005                 (*br) = (Brush*)id;
1006                 break;
1007         case UI_ID_DELETE:
1008                 brush_delete(br);
1009                 break;
1010         case UI_ID_RENAME:
1011                 /* XXX ? */
1012                 break;
1013         case UI_ID_ADD_NEW:
1014                 if(id) {
1015                         (*br) = copy_brush((Brush*)id);
1016                         id->us--;
1017                 }
1018                 else
1019                         (*br) = add_brush("Brush");
1020                 break;
1021         case UI_ID_OPEN:
1022                 /* XXX not implemented */
1023                 break;
1024         }
1025 }
1026 #endif
1027
1028 static void view3d_panel_object(const bContext *C, Panel *pa)
1029 {
1030         uiBlock *block;
1031         Scene *scene= CTX_data_scene(C);
1032         Object *obedit= CTX_data_edit_object(C);
1033         View3D *v3d= CTX_wm_view3d(C);
1034         //uiBut *bt;
1035         Object *ob= OBACT;
1036         TransformProperties *tfp;
1037         PointerRNA obptr;
1038         uiLayout *col, *row;
1039         float lim;
1040         
1041         if(ob==NULL) return;
1042
1043         /* make sure we got storage */
1044         if(v3d->properties_storage==NULL)
1045                 v3d->properties_storage= MEM_callocN(sizeof(TransformProperties), "TransformProperties");
1046         tfp= v3d->properties_storage;
1047         
1048 // XXX  uiSetButLock(object_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
1049         /*
1050         if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)) {
1051         }
1052         else {
1053                 if((ob->mode & OB_MODE_PARTICLE_EDIT)==0) {
1054                         uiBlockEndAlign(block);
1055                 }
1056         }
1057         */
1058
1059         lim= 10000.0f*MAX2(1.0, v3d->grid);
1060
1061         block= uiLayoutGetBlock(pa->layout);
1062         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1063
1064         col= uiLayoutColumn(pa->layout, 0);
1065         row= uiLayoutRow(col, 0);
1066         RNA_id_pointer_create(&ob->id, &obptr);
1067
1068         if(ob==obedit) {
1069                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(col, v3d, ob, lim);
1070                 if(ob->type==OB_MBALL) v3d_editmetaball_buts(col, ob, lim);
1071                 else v3d_editvertex_buts(C, col, v3d, ob, lim);
1072         }
1073         else if(ob->mode & OB_MODE_POSE) {
1074                 v3d_posearmature_buts(col, v3d, ob, lim);
1075         }
1076         else {
1077
1078                 v3d_transform_butsR(col, &obptr);
1079         }
1080 }
1081
1082 #if 0
1083 static void view3d_panel_preview(bContext *C, ARegion *ar, short cntrl) // VIEW3D_HANDLER_PREVIEW
1084 {
1085         uiBlock *block;
1086         View3D *v3d= sa->spacedata.first;
1087         int ofsx, ofsy;
1088         
1089         block= uiBeginBlock(C, ar, "view3d_panel_preview", UI_EMBOSS);
1090         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
1091         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
1092         
1093         ofsx= -150+(sa->winx/2)/v3d->blockscale;
1094         ofsy= -100+(sa->winy/2)/v3d->blockscale;
1095         if(uiNewPanel(C, ar, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
1096
1097         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
1098         
1099         if(scene->recalc & SCE_PRV_CHANGED) {
1100                 scene->recalc &= ~SCE_PRV_CHANGED;
1101                 //printf("found recalc\n");
1102                 BIF_view3d_previewrender_free(sa->spacedata.first);
1103                 BIF_preview_changed(0);
1104         }
1105 }
1106 #endif
1107
1108 #if 0 // XXX not used
1109 static void delete_sketch_armature(bContext *C, void *arg1, void *arg2)
1110 {
1111         BIF_deleteSketch(C);
1112 }
1113
1114 static void convert_sketch_armature(bContext *C, void *arg1, void *arg2)
1115 {
1116         BIF_convertSketch(C);
1117 }
1118
1119 static void assign_template_sketch_armature(bContext *C, void *arg1, void *arg2)
1120 {
1121         int index = *(int*)arg1;
1122         BIF_setTemplate(C, index);
1123 }
1124
1125
1126 static int view3d_panel_bonesketch_spaces_poll(const bContext *C, PanelType *pt)
1127 {
1128         Object *obedit = CTX_data_edit_object(C);
1129
1130         /* replace with check call to sketching lib */
1131         return (obedit && obedit->type == OB_ARMATURE);
1132 }
1133 static void view3d_panel_bonesketch_spaces(const bContext *C, Panel *pa)
1134 {
1135         Scene *scene = CTX_data_scene(C);
1136         static int template_index;
1137         static char joint_label[128];
1138         uiBlock *block;
1139         uiBut *but;
1140         char *bone_name;
1141         int yco = 130;
1142         int nb_joints;
1143         static char subdiv_tooltip[4][64] = {
1144                 "Subdivide arcs based on a fixed number of bones",
1145                 "Subdivide arcs in bones of equal length",
1146                 "Subdivide arcs based on correlation",
1147                 "Retarget template to stroke"
1148                 };
1149
1150         
1151         block= uiLayoutAbsoluteBlock(pa->layout);
1152         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1153
1154         uiBlockBeginAlign(block);
1155         
1156         /* use real flag instead of 1 */
1157         uiDefButBitC(block, TOG, BONE_SKETCHING, B_REDR, "Use Bone Sketching", 10, yco, 160, 20, &scene->toolsettings->bone_sketching, 0, 0, 0, 0, "Use sketching to create and edit bones, (Ctrl snaps to mesh volume)");
1158         uiDefButBitC(block, TOG, BONE_SKETCHING_ADJUST, B_REDR, "A", 170, yco, 20, 20, &scene->toolsettings->bone_sketching, 0, 0, 0, 0, "Adjust strokes by drawing near them");
1159         uiDefButBitC(block, TOG, BONE_SKETCHING_QUICK, B_REDR, "Q", 190, yco, 20, 20, &scene->toolsettings->bone_sketching, 0, 0, 0, 0, "Automatically convert and delete on stroke end");
1160         yco -= 20;
1161         
1162         but = uiDefBut(block, BUT, B_REDR, "Convert", 10,yco,100,20, 0, 0, 0, 0, 0, "Convert sketch to armature");
1163         uiButSetFunc(but, convert_sketch_armature, NULL, NULL);
1164
1165         but = uiDefBut(block, BUT, B_REDR, "Delete", 110,yco,100,20, 0, 0, 0, 0, 0, "Delete sketch");
1166         uiButSetFunc(but, delete_sketch_armature, NULL, NULL);
1167         yco -= 20;
1168         
1169         uiBlockEndAlign(block);
1170
1171         uiBlockBeginAlign(block);
1172         
1173         uiDefButC(block, MENU, B_REDR, "Subdivision Method%t|Length%x1|Adaptative%x2|Fixed%x0|Template%x3", 10,yco,60,19, &scene->toolsettings->bone_sketching_convert, 0, 0, 0, 0, subdiv_tooltip[(unsigned char)scene->toolsettings->bone_sketching_convert]);
1174
1175         switch(scene->toolsettings->bone_sketching_convert)
1176         {
1177         case SK_CONVERT_CUT_LENGTH:
1178                 uiDefButF(block, NUM, B_REDR,                                   "Lim:",         70, yco, 140, 19, &scene->toolsettings->skgen_length_limit,0.1,50.0, 10, 0,             "Maximum length of the subdivided bones");
1179                 yco -= 20;
1180                 break;
1181         case SK_CONVERT_CUT_ADAPTATIVE:
1182                 uiDefButF(block, NUM, B_REDR,                                   "Thres:",                       70, yco, 140, 19, &scene->toolsettings->skgen_correlation_limit,0.0, 1.0, 0.01, 0,      "Correlation threshold for subdivision");
1183                 yco -= 20;
1184                 break;
1185         default:
1186         case SK_CONVERT_CUT_FIXED:
1187                 uiDefButC(block, NUM, B_REDR,                                   "Num:",         70, yco, 140, 19, &scene->toolsettings->skgen_subdivision_number,1, 100, 1, 5,  "Number of subdivided bones");
1188                 yco -= 20;
1189                 break;
1190         case SK_CONVERT_RETARGET:
1191                 uiDefButC(block, ROW, B_NOP, "No",                      70,  yco, 40,19, &scene->toolsettings->skgen_retarget_roll, 0, 0, 0, 0,                                                                 "No special roll treatment");
1192                 uiDefButC(block, ROW, B_NOP, "View",            110,  yco, 50,19, &scene->toolsettings->skgen_retarget_roll, 0, SK_RETARGET_ROLL_VIEW, 0, 0,                            "Roll bones perpendicular to view");
1193                 uiDefButC(block, ROW, B_NOP, "Joint",           160, yco, 50,19, &scene->toolsettings->skgen_retarget_roll, 0, SK_RETARGET_ROLL_JOINT, 0, 0,                            "Roll bones relative to joint bend");
1194                 yco -= 30;
1195
1196                 uiBlockEndAlign(block);
1197
1198                 uiBlockBeginAlign(block);
1199                 /* button here to select what to do (copy or not), template, ...*/
1200
1201                 BIF_makeListTemplates(C);
1202                 template_index = BIF_currentTemplate(C);
1203                 
1204                 but = uiDefButI(block, MENU, B_REDR, BIF_listTemplates(C), 10,yco,200,19, &template_index, 0, 0, 0, 0, "Template");
1205                 uiButSetFunc(but, assign_template_sketch_armature, &template_index, NULL);
1206                 
1207                 yco -= 20;
1208                 
1209                 uiDefButF(block, NUM, B_NOP,                                                    "A:",                   10, yco, 66,19, &scene->toolsettings->skgen_retarget_angle_weight, 0, 10, 1, 0,         "Angle Weight");
1210                 uiDefButF(block, NUM, B_NOP,                                                    "L:",                   76, yco, 67,19, &scene->toolsettings->skgen_retarget_length_weight, 0, 10, 1, 0,                "Length Weight");
1211                 uiDefButF(block, NUM, B_NOP,                                                    "D:",           143,yco, 67,19, &scene->toolsettings->skgen_retarget_distance_weight, 0, 10, 1, 0,              "Distance Weight");
1212                 yco -= 20;
1213                 
1214                 uiDefBut(block, TEX,B_REDR,"S:",                                                        10,  yco, 90, 20, scene->toolsettings->skgen_side_string, 0.0, 8.0, 0, 0, "Text to replace &S with");
1215                 uiDefBut(block, TEX,B_REDR,"N:",                                                        100, yco, 90, 20, scene->toolsettings->skgen_num_string, 0.0, 8.0, 0, 0, "Text to replace &N with");
1216                 uiDefIconButBitC(block, TOG, SK_RETARGET_AUTONAME, B_NOP, ICON_AUTO,190,yco,20,20, &scene->toolsettings->skgen_retarget_options, 0, 0, 0, 0, "Use Auto Naming");        
1217                 yco -= 20;
1218
1219                 /* auto renaming magic */
1220                 uiBlockEndAlign(block);
1221                 
1222                 nb_joints = BIF_nbJointsTemplate(C);
1223
1224                 if (nb_joints == -1)
1225                 {
1226                         //XXX
1227                         //nb_joints = G.totvertsel;
1228                 }
1229                 
1230                 bone_name = BIF_nameBoneTemplate(C);
1231                 
1232                 BLI_snprintf(joint_label, 32, "%i joints: %s", nb_joints, bone_name);
1233                 
1234                 uiDefBut(block, LABEL, 1, joint_label,                                  10, yco, 200, 20, NULL, 0.0, 0.0, 0, 0, "");
1235                 yco -= 20;
1236                 break;
1237         }
1238
1239         uiBlockEndAlign(block);
1240 }
1241
1242 /* op->invoke */
1243 static void redo_cb(bContext *C, void *arg_op, void *arg2)
1244 {
1245         wmOperator *lastop= arg_op;
1246         
1247         if(lastop) {
1248                 int retval;
1249                 
1250                 printf("operator redo %s\n", lastop->type->name);
1251                 ED_undo_pop(C);
1252                 retval= WM_operator_repeat(C, lastop);
1253                 if((retval & OPERATOR_FINISHED)==0) {
1254                         printf("operator redo failed %s\n", lastop->type->name);
1255                         ED_undo_redo(C);
1256                 }
1257         }
1258 }
1259
1260 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
1261 {
1262         wmWindowManager *wm= CTX_wm_manager(C);
1263         wmOperator *op;
1264         PointerRNA ptr;
1265         uiBlock *block;
1266         
1267         block= uiLayoutGetBlock(pa->layout);
1268
1269         /* only for operators that are registered and did an undo push */
1270         for(op= wm->operators.last; op; op= op->prev)
1271                 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
1272                         break;
1273         
1274         if(op==NULL)
1275                 return;
1276         
1277         uiBlockSetFunc(block, redo_cb, op, NULL);
1278         
1279         if(!op->properties) {
1280                 IDPropertyTemplate val = {0};
1281                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
1282         }
1283         
1284         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
1285         uiDefAutoButsRNA(C, pa->layout, &ptr, 2);
1286 }
1287 #endif // XXX not used
1288
1289 void view3d_buttons_register(ARegionType *art)
1290 {
1291         PanelType *pt;
1292
1293         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel object");
1294         strcpy(pt->idname, "VIEW3D_PT_object");
1295         strcpy(pt->label, "Transform");
1296         pt->draw= view3d_panel_object;
1297         BLI_addtail(&art->paneltypes, pt);
1298         
1299         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel gpencil");
1300         strcpy(pt->idname, "VIEW3D_PT_gpencil");
1301         strcpy(pt->label, "Grease Pencil");
1302         pt->draw= gpencil_panel_standard;
1303         BLI_addtail(&art->paneltypes, pt);
1304         
1305         // XXX view3d_panel_preview(C, ar, 0);
1306 }
1307
1308 static int view3d_properties(bContext *C, wmOperator *op)
1309 {
1310         ScrArea *sa= CTX_wm_area(C);
1311         ARegion *ar= view3d_has_buttons_region(sa);
1312         
1313         if(ar)
1314                 ED_region_toggle_hidden(C, ar);
1315
1316         return OPERATOR_FINISHED;
1317 }
1318
1319 void VIEW3D_OT_properties(wmOperatorType *ot)
1320 {
1321         ot->name= "Properties";
1322         ot->description= "Toggles the properties panel display.";
1323         ot->idname= "VIEW3D_OT_properties";
1324         
1325         ot->exec= view3d_properties;
1326         ot->poll= ED_operator_view3d_active;
1327         
1328         /* flags */
1329         ot->flag= 0;
1330 }