Merge from trunk -r 25003:25745.
[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 static void v3d_transform_butsR(uiLayout *layout, PointerRNA *ptr)
481 {
482         uiLayout *split, *colsub;
483         
484         split = uiLayoutSplit(layout, 0.8, 0);
485         
486         if (ptr->type == &RNA_PoseBone) {
487                 PointerRNA boneptr;
488                 Bone *bone;
489                 
490                 boneptr = RNA_pointer_get(ptr, "bone");
491                 bone = boneptr.data;
492                 uiLayoutSetActive(split, !(bone->parent && bone->flag & BONE_CONNECTED));
493         }
494         colsub = uiLayoutColumn(split, 1);
495         uiItemR(colsub, "Location", 0, ptr, "location", 0);
496         colsub = uiLayoutColumn(split, 1);
497         uiItemL(colsub, "", 0);
498         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_location", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
499         
500         split = uiLayoutSplit(layout, 0.8, 0);
501         
502         switch(RNA_enum_get(ptr, "rotation_mode")) {
503                 case ROT_MODE_QUAT: /* quaternion */
504                         colsub = uiLayoutColumn(split, 1);
505                         uiItemR(colsub, "Rotation", 0, ptr, "rotation_quaternion", 0);
506                         colsub = uiLayoutColumn(split, 1);
507                         uiItemR(colsub, "4L", 0, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE);
508                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
509                                 uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
510                         else
511                                 uiItemL(colsub, "", 0);
512                         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
513                         break;
514                 case ROT_MODE_AXISANGLE: /* axis angle */
515                         colsub = uiLayoutColumn(split, 1);
516                         uiItemR(colsub, "Rotation", 0, ptr, "rotation_axis_angle", 0);
517                         colsub = uiLayoutColumn(split, 1);
518                         uiItemR(colsub, "4L", 0, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE);
519                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
520                                 uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
521                         else
522                                 uiItemL(colsub, "", 0);
523                         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
524                         break;
525                 default: /* euler rotations */
526                         colsub = uiLayoutColumn(split, 1);
527                         uiItemR(colsub, "Rotation", 0, ptr, "rotation_euler", 0);
528                         colsub = uiLayoutColumn(split, 1);
529                         uiItemL(colsub, "", 0);
530                         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
531                         break;
532         }
533         uiItemR(layout, "", 0, ptr, "rotation_mode", 0);
534         
535         split = uiLayoutSplit(layout, 0.8, 0);
536         colsub = uiLayoutColumn(split, 1);
537         uiItemR(colsub, "Scale", 0, ptr, "scale", 0);
538         colsub = uiLayoutColumn(split, 1);
539         uiItemL(colsub, "", 0);
540         uiItemR(colsub, "", ICON_LOCKED, ptr, "lock_scale", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY);
541         
542         if (ptr->type == &RNA_Object) {
543                 Object *ob = ptr->data;
544                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
545                         uiItemR(layout, "Dimensions", 0, ptr, "dimensions", 0);
546         }
547 }
548
549 static void v3d_posearmature_buts(uiLayout *layout, View3D *v3d, Object *ob, float lim)
550 {
551 //      uiBlock *block= uiLayoutGetBlock(layout);
552 //      bArmature *arm;
553         bPoseChannel *pchan;
554 //      TransformProperties *tfp= v3d->properties_storage;
555         PointerRNA pchanptr;
556         uiLayout *col;
557 //      uiLayout *row;
558
559         pchan= get_active_posechannel(ob);
560
561 //      row= uiLayoutRow(layout, 0);
562         
563         if (!pchan)     {
564                 uiItemL(layout, "No Bone Active", 0);
565                 return; 
566         }
567
568         RNA_pointer_create(&ob->id, &RNA_PoseBone, pchan, &pchanptr);
569
570         col= uiLayoutColumn(layout, 0);
571         
572         /* XXX: RNA buts show data in native types (i.e. quats, 4-component axis/angle, etc.)
573          * but oldskool UI shows in eulers always. Do we want to be able to still display in Eulers?
574          * Maybe needs RNA/ui options to display rotations as different types... */
575         v3d_transform_butsR(col, &pchanptr);
576
577 #if 0
578         uiLayoutAbsoluteBlock(layout);
579
580         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
581                 float quat[4];
582                 /* convert to euler, passing through quats... */
583                 axis_angle_to_quat(quat, pchan->rotAxis, pchan->rotAngle);
584                 quat_to_eul( tfp->ob_eul,quat);
585         }
586         else if (pchan->rotmode == ROT_MODE_QUAT)
587                 quat_to_eul( tfp->ob_eul,pchan->quat);
588         else
589                 copy_v3_v3(tfp->ob_eul, pchan->eul);
590         tfp->ob_eul[0]*= 180.0/M_PI;
591         tfp->ob_eul[1]*= 180.0/M_PI;
592         tfp->ob_eul[2]*= 180.0/M_PI;
593         
594         uiDefBut(block, LABEL, 0, "Location:",                  0, 240, 100, 20, 0, 0, 0, 0, 0, "");
595         uiBlockBeginAlign(block);
596         uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",   0, 220, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
597         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",   0, 200, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
598         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",   0, 180, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
599         uiBlockEndAlign(block);
600         
601         uiBlockBeginAlign(block);
602         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");
603         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");
604         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");
605         uiBlockEndAlign(block);
606         
607         uiDefBut(block, LABEL, 0, "Rotation:",                  0, 160, 100, 20, 0, 0, 0, 0, 0, "");
608         uiBlockBeginAlign(block);
609         uiDefButF(block, NUM, B_ARMATUREPANEL3, "X:",   0, 140, 120, 19, tfp->ob_eul, -1000.0, 1000.0, 100, 3, "");
610         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Y:",   0, 120, 120, 19, tfp->ob_eul+1, -1000.0, 1000.0, 100, 3, "");
611         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Z:",   0, 100, 120, 19, tfp->ob_eul+2, -1000.0, 1000.0, 100, 3, "");
612         uiBlockEndAlign(block);
613         
614         uiBlockBeginAlign(block);
615         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");
616         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");
617         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");
618         uiBlockEndAlign(block);
619         
620         uiDefBut(block, LABEL, 0, "Scale:",                             0, 80, 100, 20, 0, 0, 0, 0, 0, "");
621         uiBlockBeginAlign(block);
622         uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",   0, 60, 120, 19, pchan->size, -lim, lim, 10, 3, "");
623         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",   0, 40, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
624         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",   0, 20, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
625         uiBlockEndAlign(block);
626         
627         uiBlockBeginAlign(block);
628         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");
629         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");
630         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");
631         uiBlockEndAlign(block);
632 #endif
633 }
634
635 /* assumes armature editmode */
636 void validate_editbonebutton_cb(bContext *C, void *bonev, void *namev)
637 {
638         EditBone *eBone= bonev;
639         char oldname[32], newname[32];
640         
641         /* need to be on the stack */
642         BLI_strncpy(newname, eBone->name, 32);
643         BLI_strncpy(oldname, (char *)namev, 32);
644         /* restore */
645         BLI_strncpy(eBone->name, oldname, 32);
646         
647         ED_armature_bone_rename(CTX_data_edit_object(C)->data, oldname, newname); // editarmature.c
648         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, CTX_data_edit_object(C)); // XXX fix
649 }
650
651 static void v3d_editarmature_buts(uiLayout *layout, View3D *v3d, Object *ob, float lim)
652 {
653 //      uiBlock *block= uiLayoutGetBlock(layout);
654         bArmature *arm= ob->data;
655         EditBone *ebone;
656 //      TransformProperties *tfp= v3d->properties_storage;
657 //      uiLayout *row;
658         uiLayout *col;
659         PointerRNA eboneptr;
660         
661         ebone= arm->act_edbone;
662
663         if (!ebone || (ebone->layer & arm->layer)==0)
664                 return;
665         
666 //      row= uiLayoutRow(layout, 0);
667         RNA_pointer_create(&arm->id, &RNA_EditBone, ebone, &eboneptr);
668
669
670         col= uiLayoutColumn(layout, 0);
671         uiItemR(col, "Head", 0, &eboneptr, "head", 0);
672         if (ebone->parent && ebone->flag & BONE_CONNECTED ) {
673                 PointerRNA parptr = RNA_pointer_get(&eboneptr, "parent");
674                 uiItemR(col, "Radius", 0, &parptr, "tail_radius", 0);
675         } else {
676                 uiItemR(col, "Radius", 0, &eboneptr, "head_radius", 0);
677         }
678         
679         uiItemR(col, "Tail", 0, &eboneptr, "tail", 0);
680         uiItemR(col, "Radius", 0, &eboneptr, "tail_radius", 0);
681         
682         uiItemR(col, "Roll", 0, &eboneptr, "roll", 0);
683 }
684
685 static void v3d_editmetaball_buts(uiLayout *layout, Object *ob, float lim)
686 {
687         PointerRNA mbptr, ptr;
688         MetaBall *mball= ob->data;
689 //      uiLayout *row;
690         uiLayout *col;
691         
692         if (!mball || !(mball->lastelem)) return;
693         
694         RNA_pointer_create(&mball->id, &RNA_MetaBall, mball, &mbptr);
695         
696 //      row= uiLayoutRow(layout, 0);
697
698         RNA_pointer_create(&mball->id, &RNA_MetaElement, mball->lastelem, &ptr);
699         
700         col= uiLayoutColumn(layout, 0);
701         uiItemR(col, "Location", 0, &ptr, "location", 0);
702         
703         uiItemR(col, "Radius", 0, &ptr, "radius", 0);
704         uiItemR(col, "Stiffness", 0, &ptr, "stiffness", 0);
705         
706         uiItemR(col, "Type", 0, &ptr, "type", 0);
707         
708         col= uiLayoutColumn(layout, 1);
709         switch (RNA_enum_get(&ptr, "type")) {
710                 case MB_BALL:
711                         break;
712                 case MB_CUBE:
713                         uiItemL(col, "Size:", 0);
714                         uiItemR(col, "X", 0, &ptr, "size_x", 0);
715                         uiItemR(col, "Y", 0, &ptr, "size_y", 0);
716                         uiItemR(col, "Z", 0, &ptr, "size_z", 0);
717                         break;
718                 case MB_TUBE:
719                         uiItemL(col, "Size:", 0);
720                         uiItemR(col, "X", 0, &ptr, "size_x", 0);
721                         break;
722                 case MB_PLANE:
723                         uiItemL(col, "Size:", 0);
724                         uiItemR(col, "X", 0, &ptr, "size_x", 0);
725                         uiItemR(col, "Y", 0, &ptr, "size_y", 0);
726                         break;
727                 case MB_ELIPSOID:
728                         uiItemL(col, "Size:", 0);
729                         uiItemR(col, "X", 0, &ptr, "size_x", 0);
730                         uiItemR(col, "Y", 0, &ptr, "size_y", 0);
731                         uiItemR(col, "Z", 0, &ptr, "size_z", 0);
732                         break;             
733         }       
734 }
735
736 /* test if 'ob' is a parent somewhere in par's parents */
737 static int test_parent_loop(Object *par, Object *ob)
738 {
739         if(par == NULL) return 0;
740         if(ob == par) return 1;
741         return test_parent_loop(par->parent, ob);
742 }
743
744 static void do_view3d_region_buttons(bContext *C, void *arg, int event)
745 {
746         Scene *scene= CTX_data_scene(C);
747 //      Object *obedit= CTX_data_edit_object(C);
748         View3D *v3d= CTX_wm_view3d(C);
749 //      BoundBox *bb;
750         Object *ob= OBACT;
751         TransformProperties *tfp= v3d->properties_storage;
752         
753         switch(event) {
754         
755         case B_REDR:
756                 ED_area_tag_redraw(CTX_wm_area(C));
757                 return; /* no notifier! */
758                 
759         case B_OBJECTPANEL:
760                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);
761                 break;
762
763         
764         case B_OBJECTPANELMEDIAN:
765                 if(ob) {
766                         v3d_editvertex_buts(C, NULL, v3d, ob, 1.0);
767                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
768                 }
769                 break;
770                 
771                 /* note; this case also used for parbone */
772         case B_OBJECTPANELPARENT:
773                 if(ob) {
774                         if(ob->id.lib || test_parent_loop(ob->parent, ob) ) 
775                                 ob->parent= NULL;
776                         else {
777                                 DAG_scene_sort(scene);
778                                 DAG_id_flush_update(&ob->id, OB_RECALC_OB);
779                         }
780                 }
781                 break;
782                 
783
784         case B_ARMATUREPANEL3:  // rotate button on channel
785                 {
786                         bPoseChannel *pchan;
787                         float eul[3];
788                         
789                         pchan= get_active_posechannel(ob);
790                         if (!pchan) return;
791                         
792                         /* make a copy to eul[3], to allow TAB on buttons to work */
793                         eul[0]= M_PI*tfp->ob_eul[0]/180.0;
794                         eul[1]= M_PI*tfp->ob_eul[1]/180.0;
795                         eul[2]= M_PI*tfp->ob_eul[2]/180.0;
796                         
797                         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
798                                 float quat[4];
799                                 /* convert to axis-angle, passing through quats  */
800                                 eul_to_quat( quat,eul);
801                                 quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,quat);
802                         }
803                         else if (pchan->rotmode == ROT_MODE_QUAT)
804                                 eul_to_quat( pchan->quat,eul);
805                         else
806                                 copy_v3_v3(pchan->eul, eul);
807                 }
808                 /* no break, pass on */
809         case B_ARMATUREPANEL2:
810                 {
811                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
812                         DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
813                 }
814                 break;
815         case B_TRANSFORMSPACEADD:
816                 BIF_createTransformOrientation(C, NULL, "", 1, 0);
817                 break;
818         case B_TRANSFORMSPACECLEAR:
819                 BIF_clearTransformOrientation(C);
820                 break;
821                 
822 #if 0 // XXX
823         case B_WEIGHT0_0:
824                 wpaint->weight = 0.0f;
825                 break;
826                 
827         case B_WEIGHT1_4:
828                 wpaint->weight = 0.25f;
829                 break;
830         case B_WEIGHT1_2:
831                 wpaint->weight = 0.5f;
832                 break;
833         case B_WEIGHT3_4:
834                 wpaint->weight = 0.75f;
835                 break;
836         case B_WEIGHT1_0:
837                 wpaint->weight = 1.0f;
838                 break;
839                 
840         case B_OPA1_8:
841                 wpaint->a = 0.125f;
842                 break;
843         case B_OPA1_4:
844                 wpaint->a = 0.25f;
845                 break;
846         case B_OPA1_2:
847                 wpaint->a = 0.5f;
848                 break;
849         case B_OPA3_4:
850                 wpaint->a = 0.75f;
851                 break;
852         case B_OPA1_0:
853                 wpaint->a = 1.0f;
854                 break;
855 #endif
856         case B_CLR_WPAINT:
857 //              if(!multires_level1_test()) {
858                 {
859                         bDeformGroup *defGroup = BLI_findlink(&ob->defbase, ob->actdef-1);
860                         if(defGroup) {
861                                 Mesh *me= ob->data;
862                                 int a;
863                                 for(a=0; a<me->totvert; a++)
864                                         ED_vgroup_vert_remove (ob, defGroup, a);
865                                 DAG_id_flush_update(&ob->id, OB_RECALC_DATA);
866                         }
867                 }
868                 break;
869         case B_RV3D_LOCKED:
870         case B_RV3D_BOXVIEW:
871         case B_RV3D_BOXCLIP:
872                 {
873                         ScrArea *sa= CTX_wm_area(C);
874                         ARegion *ar= sa->regionbase.last;
875                         RegionView3D *rv3d;
876                         short viewlock;
877                         
878                         ar= ar->prev;
879                         rv3d= ar->regiondata;
880                         viewlock= rv3d->viewlock;
881                         
882                         if((viewlock & RV3D_LOCKED)==0)
883                                 viewlock= 0;
884                         else if((viewlock & RV3D_BOXVIEW)==0)
885                                 viewlock &= ~RV3D_BOXCLIP;
886                         
887                         for(; ar; ar= ar->prev) {
888                                 if(ar->alignment==RGN_ALIGN_QSPLIT) {
889                                         rv3d= ar->regiondata;
890                                         rv3d->viewlock= viewlock;
891                                 }
892                         }
893                         
894                         if(rv3d->viewlock & RV3D_BOXVIEW)
895                                 view3d_boxview_copy(sa, sa->regionbase.last);
896                         
897                         ED_area_tag_redraw(sa);
898                 }
899                 break;
900         }
901
902         /* default for now */
903         WM_event_add_notifier(C, NC_OBJECT|ND_TRANSFORM, ob);
904 }
905
906 void removeTransformOrientation_func(bContext *C, void *target, void *unused)
907 {
908         BIF_removeTransformOrientation(C, (TransformOrientation *) target);
909 }
910
911 void selectTransformOrientation_func(bContext *C, void *target, void *unused)
912 {
913         BIF_selectTransformOrientation(C, (TransformOrientation *) target);
914 }
915
916 static void view3d_panel_object(const bContext *C, Panel *pa)
917 {
918         uiBlock *block;
919         Scene *scene= CTX_data_scene(C);
920         Object *obedit= CTX_data_edit_object(C);
921         View3D *v3d= CTX_wm_view3d(C);
922         //uiBut *bt;
923         Object *ob= OBACT;
924         TransformProperties *tfp;
925         PointerRNA obptr;
926         uiLayout *col, *row;
927         float lim;
928         
929         if(ob==NULL) return;
930
931         /* make sure we got storage */
932         if(v3d->properties_storage==NULL)
933                 v3d->properties_storage= MEM_callocN(sizeof(TransformProperties), "TransformProperties");
934         tfp= v3d->properties_storage;
935         
936 // XXX  uiSetButLock(object_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
937         /*
938         if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)) {
939         }
940         else {
941                 if((ob->mode & OB_MODE_PARTICLE_EDIT)==0) {
942                         uiBlockEndAlign(block);
943                 }
944         }
945         */
946
947         lim= 10000.0f*MAX2(1.0, v3d->grid);
948
949         block= uiLayoutGetBlock(pa->layout);
950         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
951
952         col= uiLayoutColumn(pa->layout, 0);
953         row= uiLayoutRow(col, 0);
954         RNA_id_pointer_create(&ob->id, &obptr);
955
956         if(ob==obedit) {
957                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(col, v3d, ob, lim);
958                 if(ob->type==OB_MBALL) v3d_editmetaball_buts(col, ob, lim);
959                 else v3d_editvertex_buts(C, col, v3d, ob, lim);
960         }
961         else if(ob->mode & OB_MODE_POSE) {
962                 v3d_posearmature_buts(col, v3d, ob, lim);
963         }
964         else {
965
966                 v3d_transform_butsR(col, &obptr);
967         }
968 }
969
970 #if 0
971 static void view3d_panel_preview(bContext *C, ARegion *ar, short cntrl) // VIEW3D_HANDLER_PREVIEW
972 {
973         uiBlock *block;
974         View3D *v3d= sa->spacedata.first;
975         int ofsx, ofsy;
976         
977         block= uiBeginBlock(C, ar, "view3d_panel_preview", UI_EMBOSS);
978         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
979         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
980         
981         ofsx= -150+(sa->winx/2)/v3d->blockscale;
982         ofsy= -100+(sa->winy/2)/v3d->blockscale;
983         if(uiNewPanel(C, ar, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
984
985         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
986         
987         if(scene->recalc & SCE_PRV_CHANGED) {
988                 scene->recalc &= ~SCE_PRV_CHANGED;
989                 //printf("found recalc\n");
990                 BIF_view3d_previewrender_free(sa->spacedata.first);
991                 BIF_preview_changed(0);
992         }
993 }
994 #endif
995
996 #if 0 // XXX not used
997 static void delete_sketch_armature(bContext *C, void *arg1, void *arg2)
998 {
999         BIF_deleteSketch(C);
1000 }
1001
1002 static void convert_sketch_armature(bContext *C, void *arg1, void *arg2)
1003 {
1004         BIF_convertSketch(C);
1005 }
1006
1007 static void assign_template_sketch_armature(bContext *C, void *arg1, void *arg2)
1008 {
1009         int index = *(int*)arg1;
1010         BIF_setTemplate(C, index);
1011 }
1012
1013
1014 static int view3d_panel_bonesketch_spaces_poll(const bContext *C, PanelType *pt)
1015 {
1016         Object *obedit = CTX_data_edit_object(C);
1017
1018         /* replace with check call to sketching lib */
1019         return (obedit && obedit->type == OB_ARMATURE);
1020 }
1021 static void view3d_panel_bonesketch_spaces(const bContext *C, Panel *pa)
1022 {
1023         Scene *scene = CTX_data_scene(C);
1024         static int template_index;
1025         static char joint_label[128];
1026         uiBlock *block;
1027         uiBut *but;
1028         char *bone_name;
1029         int yco = 130;
1030         int nb_joints;
1031         static char subdiv_tooltip[4][64] = {
1032                 "Subdivide arcs based on a fixed number of bones",
1033                 "Subdivide arcs in bones of equal length",
1034                 "Subdivide arcs based on correlation",
1035                 "Retarget template to stroke"
1036                 };
1037
1038         
1039         block= uiLayoutAbsoluteBlock(pa->layout);
1040         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1041
1042         uiBlockBeginAlign(block);
1043         
1044         /* use real flag instead of 1 */
1045         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)");
1046         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");
1047         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");
1048         yco -= 20;
1049         
1050         but = uiDefBut(block, BUT, B_REDR, "Convert", 10,yco,100,20, 0, 0, 0, 0, 0, "Convert sketch to armature");
1051         uiButSetFunc(but, convert_sketch_armature, NULL, NULL);
1052
1053         but = uiDefBut(block, BUT, B_REDR, "Delete", 110,yco,100,20, 0, 0, 0, 0, 0, "Delete sketch");
1054         uiButSetFunc(but, delete_sketch_armature, NULL, NULL);
1055         yco -= 20;
1056         
1057         uiBlockEndAlign(block);
1058
1059         uiBlockBeginAlign(block);
1060         
1061         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]);
1062
1063         switch(scene->toolsettings->bone_sketching_convert)
1064         {
1065         case SK_CONVERT_CUT_LENGTH:
1066                 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");
1067                 yco -= 20;
1068                 break;
1069         case SK_CONVERT_CUT_ADAPTATIVE:
1070                 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");
1071                 yco -= 20;
1072                 break;
1073         default:
1074         case SK_CONVERT_CUT_FIXED:
1075                 uiDefButC(block, NUM, B_REDR,                                   "Num:",         70, yco, 140, 19, &scene->toolsettings->skgen_subdivision_number,1, 100, 1, 5,  "Number of subdivided bones");
1076                 yco -= 20;
1077                 break;
1078         case SK_CONVERT_RETARGET:
1079                 uiDefButC(block, ROW, B_NOP, "No",                      70,  yco, 40,19, &scene->toolsettings->skgen_retarget_roll, 0, 0, 0, 0,                                                                 "No special roll treatment");
1080                 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");
1081                 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");
1082                 yco -= 30;
1083
1084                 uiBlockEndAlign(block);
1085
1086                 uiBlockBeginAlign(block);
1087                 /* button here to select what to do (copy or not), template, ...*/
1088
1089                 BIF_makeListTemplates(C);
1090                 template_index = BIF_currentTemplate(C);
1091                 
1092                 but = uiDefButI(block, MENU, B_REDR, BIF_listTemplates(C), 10,yco,200,19, &template_index, 0, 0, 0, 0, "Template");
1093                 uiButSetFunc(but, assign_template_sketch_armature, &template_index, NULL);
1094                 
1095                 yco -= 20;
1096                 
1097                 uiDefButF(block, NUM, B_NOP,                                                    "A:",                   10, yco, 66,19, &scene->toolsettings->skgen_retarget_angle_weight, 0, 10, 1, 0,         "Angle Weight");
1098                 uiDefButF(block, NUM, B_NOP,                                                    "L:",                   76, yco, 67,19, &scene->toolsettings->skgen_retarget_length_weight, 0, 10, 1, 0,                "Length Weight");
1099                 uiDefButF(block, NUM, B_NOP,                                                    "D:",           143,yco, 67,19, &scene->toolsettings->skgen_retarget_distance_weight, 0, 10, 1, 0,              "Distance Weight");
1100                 yco -= 20;
1101                 
1102                 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");
1103                 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");
1104                 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");        
1105                 yco -= 20;
1106
1107                 /* auto renaming magic */
1108                 uiBlockEndAlign(block);
1109                 
1110                 nb_joints = BIF_nbJointsTemplate(C);
1111
1112                 if (nb_joints == -1)
1113                 {
1114                         //XXX
1115                         //nb_joints = G.totvertsel;
1116                 }
1117                 
1118                 bone_name = BIF_nameBoneTemplate(C);
1119                 
1120                 BLI_snprintf(joint_label, 32, "%i joints: %s", nb_joints, bone_name);
1121                 
1122                 uiDefBut(block, LABEL, 1, joint_label,                                  10, yco, 200, 20, NULL, 0.0, 0.0, 0, 0, "");
1123                 yco -= 20;
1124                 break;
1125         }
1126
1127         uiBlockEndAlign(block);
1128 }
1129
1130 /* op->invoke */
1131 static void redo_cb(bContext *C, void *arg_op, void *arg2)
1132 {
1133         wmOperator *lastop= arg_op;
1134         
1135         if(lastop) {
1136                 int retval;
1137                 
1138                 printf("operator redo %s\n", lastop->type->name);
1139                 ED_undo_pop(C);
1140                 retval= WM_operator_repeat(C, lastop);
1141                 if((retval & OPERATOR_FINISHED)==0) {
1142                         printf("operator redo failed %s\n", lastop->type->name);
1143                         ED_undo_redo(C);
1144                 }
1145         }
1146 }
1147
1148 static void view3d_panel_operator_redo(const bContext *C, Panel *pa)
1149 {
1150         wmWindowManager *wm= CTX_wm_manager(C);
1151         wmOperator *op;
1152         PointerRNA ptr;
1153         uiBlock *block;
1154         
1155         block= uiLayoutGetBlock(pa->layout);
1156
1157         /* only for operators that are registered and did an undo push */
1158         for(op= wm->operators.last; op; op= op->prev)
1159                 if((op->type->flag & OPTYPE_REGISTER) && (op->type->flag & OPTYPE_UNDO))
1160                         break;
1161         
1162         if(op==NULL)
1163                 return;
1164         
1165         uiBlockSetFunc(block, redo_cb, op, NULL);
1166         
1167         if(!op->properties) {
1168                 IDPropertyTemplate val = {0};
1169                 op->properties= IDP_New(IDP_GROUP, val, "wmOperatorProperties");
1170         }
1171         
1172         RNA_pointer_create(&wm->id, op->type->srna, op->properties, &ptr);
1173         uiDefAutoButsRNA(C, pa->layout, &ptr, 2);
1174 }
1175 #endif // XXX not used
1176
1177 void view3d_buttons_register(ARegionType *art)
1178 {
1179         PanelType *pt;
1180
1181         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel object");
1182         strcpy(pt->idname, "VIEW3D_PT_object");
1183         strcpy(pt->label, "Transform");
1184         pt->draw= view3d_panel_object;
1185         BLI_addtail(&art->paneltypes, pt);
1186         
1187         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel gpencil");
1188         strcpy(pt->idname, "VIEW3D_PT_gpencil");
1189         strcpy(pt->label, "Grease Pencil");
1190         pt->draw= gpencil_panel_standard;
1191         BLI_addtail(&art->paneltypes, pt);
1192         
1193         // XXX view3d_panel_preview(C, ar, 0);
1194 }
1195
1196 static int view3d_properties(bContext *C, wmOperator *op)
1197 {
1198         ScrArea *sa= CTX_wm_area(C);
1199         ARegion *ar= view3d_has_buttons_region(sa);
1200         
1201         if(ar)
1202                 ED_region_toggle_hidden(C, ar);
1203
1204         return OPERATOR_FINISHED;
1205 }
1206
1207 void VIEW3D_OT_properties(wmOperatorType *ot)
1208 {
1209         ot->name= "Properties";
1210         ot->description= "Toggles the properties panel display.";
1211         ot->idname= "VIEW3D_OT_properties";
1212         
1213         ot->exec= view3d_properties;
1214         ot->poll= ED_operator_view3d_active;
1215         
1216         /* flags */
1217         ot->flag= 0;
1218 }