use ICON_NULL define rather then 0, makes UI calls less confusing. (no functional...
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_armature_types.h"
35 #include "DNA_curve_types.h"
36 #include "DNA_lattice_types.h"
37 #include "DNA_meta_types.h"
38 #include "DNA_meshdata_types.h"
39 #include "DNA_object_types.h"
40 #include "DNA_scene_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_math.h"
45 #include "BLI_blenlib.h"
46 #include "BLI_editVert.h"
47 #include "BLI_rand.h"
48
49 #include "BKE_action.h"
50 #include "BKE_context.h"
51 #include "BKE_curve.h"
52 #include "BKE_customdata.h"
53 #include "BKE_depsgraph.h"
54 #include "BKE_main.h"
55 #include "BKE_mesh.h"
56 #include "BKE_screen.h"
57 #include "BKE_deform.h"
58
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "RNA_access.h"
64
65 #include "ED_armature.h"
66 #include "ED_gpencil.h"
67 #include "ED_mesh.h"
68 #include "ED_screen.h"
69 #include "ED_transform.h"
70 #include "ED_curve.h"
71
72 #include "UI_interface.h"
73 #include "UI_resources.h"
74
75 #include "view3d_intern.h"      // own include
76
77
78 /* ******************* view3d space & buttons ************** */
79 #define B_NOP           1
80 #define B_REDR          2
81 #define B_OBJECTPANELROT        1007
82 #define B_OBJECTPANELMEDIAN 1008
83 #define B_ARMATUREPANEL1        1009
84 #define B_ARMATUREPANEL2        1010
85 #define B_OBJECTPANELPARENT 1011
86 #define B_OBJECTPANEL           1012
87 #define B_ARMATUREPANEL3        1013
88 #define B_OBJECTPANELSCALE      1014
89 #define B_OBJECTPANELDIMS       1015
90 #define B_TRANSFORMSPACEADD     1016
91 #define B_TRANSFORMSPACECLEAR   1017
92 #define B_SETPT_AUTO    2125
93 #define B_SETPT_VECTOR  2126
94 #define B_SETPT_ALIGN   2127
95 #define B_SETPT_FREE    2128
96 #define B_RECALCMBALL   2501
97
98 #define B_WEIGHT0_0             2840
99 #define B_WEIGHT1_4             2841
100 #define B_WEIGHT1_2             2842
101 #define B_WEIGHT3_4             2843
102 #define B_WEIGHT1_0             2844
103
104 #define B_OPA1_8                2845
105 #define B_OPA1_4                2846
106 #define B_OPA1_2                2847
107 #define B_OPA3_4                2848
108 #define B_OPA1_0                2849
109
110 #define B_CLR_WPAINT    2850
111
112 #define B_RV3D_LOCKED   2900
113 #define B_RV3D_BOXVIEW  2901
114 #define B_RV3D_BOXCLIP  2902
115
116 #define B_IDNAME                3000
117
118 /* temporary struct for storing transform properties */
119 typedef struct {
120         float ob_eul[4];        // used for quat too....
121         float ob_scale[3]; // need temp space due to linked values
122         float ob_dims[3];
123         short link_scale;
124         float ve_median[6];
125         int curdef;
126         float *defweightp;
127 } TransformProperties;
128
129
130 /* is used for both read and write... */
131 static void v3d_editvertex_buts(uiLayout *layout, View3D *v3d, Object *ob, float lim)
132 {
133         uiBlock *block= (layout)? uiLayoutAbsoluteBlock(layout): NULL;
134         MDeformVert *dvert=NULL;
135         TransformProperties *tfp= v3d->properties_storage;
136         float median[6], ve_median[6];
137         int tot, totw, totweight, totedge, totradius;
138         char defstr[320];
139         
140         median[0]= median[1]= median[2]= median[3]= median[4]= median[5]= 0.0;
141         tot= totw= totweight= totedge= totradius= 0;
142         defstr[0]= 0;
143
144         if(ob->type==OB_MESH) {
145                 Mesh *me= ob->data;
146                 EditMesh *em = BKE_mesh_get_editmesh(me);
147                 EditVert *eve, *evedef=NULL;
148                 EditEdge *eed;
149                 
150                 eve= em->verts.first;
151                 while(eve) {
152                         if(eve->f & SELECT) {
153                                 evedef= eve;
154                                 tot++;
155                                 add_v3_v3(median, eve->co);
156                         }
157                         eve= eve->next;
158                 }
159                 eed= em->edges.first;
160                 while(eed) {
161                         if((eed->f & SELECT)) {
162                                 totedge++;
163                                 median[3]+= eed->crease;
164                         }
165                         eed= eed->next;
166                 }
167
168                 /* check for defgroups */
169                 if(evedef)
170                         dvert= CustomData_em_get(&em->vdata, evedef->data, CD_MDEFORMVERT);
171                 if(tot==1 && dvert && dvert->totweight) {
172                         bDeformGroup *dg;
173                         int i, max=1, init=1;
174                         char str[320];
175                         
176                         for (i=0; i<dvert->totweight; i++){
177                                 dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
178                                 if(dg) {
179                                         max+= BLI_snprintf(str, sizeof(str), "%s %%x%d|", dg->name, dvert->dw[i].def_nr); 
180                                         if(max<320) strcat(defstr, str);
181                                 }
182
183                                 if(tfp->curdef==dvert->dw[i].def_nr) {
184                                         init= 0;
185                                         tfp->defweightp= &dvert->dw[i].weight;
186                                 }
187                         }
188                         
189                         if(init) {      // needs new initialized 
190                                 tfp->curdef= dvert->dw[0].def_nr;
191                                 tfp->defweightp= &dvert->dw[0].weight;
192                         }
193                 }
194
195                 BKE_mesh_end_editmesh(me, em);
196         }
197         else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
198                 Curve *cu= ob->data;
199                 Nurb *nu;
200                 BPoint *bp;
201                 BezTriple *bezt;
202                 int a;
203                 ListBase *nurbs= ED_curve_editnurbs(cu);
204
205                 nu= nurbs->first;
206                 while(nu) {
207                         if(nu->type == CU_BEZIER) {
208                                 bezt= nu->bezt;
209                                 a= nu->pntsu;
210                                 while(a--) {
211                                         if(bezt->f2 & SELECT) {
212                                                 add_v3_v3(median, bezt->vec[1]);
213                                                 tot++;
214                                                 median[4]+= bezt->weight;
215                                                 totweight++;
216                                                 median[5]+= bezt->radius;
217                                                 totradius++;
218                                         }
219                                         else {
220                                                 if(bezt->f1 & SELECT) {
221                                                         add_v3_v3(median, bezt->vec[0]);
222                                                         tot++;
223                                                 }
224                                                 if(bezt->f3 & SELECT) {
225                                                         add_v3_v3(median, bezt->vec[2]);
226                                                         tot++;
227                                                 }
228                                         }
229                                         bezt++;
230                                 }
231                         }
232                         else {
233                                 bp= nu->bp;
234                                 a= nu->pntsu*nu->pntsv;
235                                 while(a--) {
236                                         if(bp->f1 & SELECT) {
237                                                 add_v3_v3(median, bp->vec);
238                                                 median[3]+= bp->vec[3];
239                                                 totw++;
240                                                 tot++;
241                                                 median[4]+= bp->weight;
242                                                 totweight++;
243                                                 median[5]+= bp->radius;
244                                                 totradius++;
245                                         }
246                                         bp++;
247                                 }
248                         }
249                         nu= nu->next;
250                 }
251         }
252         else if(ob->type==OB_LATTICE) {
253                 Lattice *lt= ob->data;
254                 BPoint *bp;
255                 int a;
256                 
257                 a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
258                 bp= lt->editlatt->latt->def;
259                 while(a--) {
260                         if(bp->f1 & SELECT) {
261                                 add_v3_v3(median, bp->vec);
262                                 tot++;
263                                 median[4]+= bp->weight;
264                                 totweight++;
265                         }
266                         bp++;
267                 }
268         }
269         
270         if(tot==0) return;
271
272         median[0] /= (float)tot;
273         median[1] /= (float)tot;
274         median[2] /= (float)tot;
275         if(totedge) median[3] /= (float)totedge;
276         else if(totw) median[3] /= (float)totw;
277         if(totweight) median[4] /= (float)totweight;
278         if(totradius) median[5] /= (float)totradius;
279         
280         if(v3d->flag & V3D_GLOBAL_STATS)
281                 mul_m4_v3(ob->obmat, median);
282         
283         if(block) {     // buttons
284                 uiBut *but;
285                 int but_y;
286                 if((ob->parent) && (ob->partype == PARBONE))    but_y = 135;
287                 else                                                                                    but_y = 150;
288                 
289                 
290                 
291                 memcpy(tfp->ve_median, median, sizeof(tfp->ve_median));
292                 
293                 uiBlockBeginAlign(block);
294                 if(tot==1) {
295                         uiDefBut(block, LABEL, 0, "Vertex:",                                    0, 130, 200, 20, 0, 0, 0, 0, 0, "");
296                         uiBlockBeginAlign(block);
297
298                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "X:",           0, 110, 200, 20, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
299                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
300                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Y:",           0, 90, 200, 20, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
301                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
302                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Z:",           0, 70, 200, 20, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
303                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
304
305                         if(totw==1) {
306                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "W:",        0, 50, 200, 20, &(tfp->ve_median[3]), 0.01, 100.0, 1, 3, "");
307                                 uiBlockBeginAlign(block);
308                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
309                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
310                                 uiBlockEndAlign(block);
311                                 if(totweight)
312                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 0, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 1, 3, "");
313                                 if(totradius)
314                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 0, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 1, 3, "Radius of curve CPs");
315                                 }
316                         else {
317                                 uiBlockBeginAlign(block);
318                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
319                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
320                                 uiBlockEndAlign(block);
321                                 if(totweight)
322                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 20, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 10, 3, "");
323                                 if(totradius)
324                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 20, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 10, 3, "Radius of curve CPs");
325                         }
326                 }
327                 else {
328                         uiDefBut(block, LABEL, 0, "Median:",                                    0, 130, 200, 20, 0, 0, 0, 0, 0, "");
329                         uiBlockBeginAlign(block);
330                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "X:",           0, 110, 200, 20, &(tfp->ve_median[0]), -lim, lim, 10, 3, "");
331                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
332                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Y:",           0, 90, 200, 20, &(tfp->ve_median[1]), -lim, lim, 10, 3, "");
333                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
334                         but= uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Z:",           0, 70, 200, 20, &(tfp->ve_median[2]), -lim, lim, 10, 3, "");
335                         uiButSetUnitType(but, PROP_UNIT_LENGTH);
336                         if(totw==tot) {
337                                 uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "W:",        0, 50, 200, 20, &(tfp->ve_median[3]), 0.01, 100.0, 1, 3, "");
338                                 uiBlockEndAlign(block);
339                                 uiBlockBeginAlign(block);
340                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
341                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 25, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
342                                 uiBlockEndAlign(block);
343                                 if(totweight)
344                                         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");
345                                 if(totradius)
346                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 0, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 10, 3, "Radius of curve CPs");
347                                 uiBlockEndAlign(block);
348                         }
349                         else {
350                                 uiBlockBeginAlign(block);
351                                 uiDefButBitS(block, TOG, V3D_GLOBAL_STATS, B_REDR, "Global",            0, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays global values");
352                                 uiDefButBitS(block, TOGN, V3D_GLOBAL_STATS, B_REDR, "Local",            100, 45, 100, 20, &v3d->flag, 0, 0, 0, 0, "Displays local values");
353                                 uiBlockEndAlign(block);
354                                 if(totweight)
355                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Weight:",   0, 20, 200, 20, &(tfp->ve_median[4]), 0.0, 1.0, 1, 3, "Weight is used for SoftBody Goal");
356                                 if(totradius)
357                                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Radius:",   0, 0, 200, 20, &(tfp->ve_median[5]), 0.0, 100.0, 1, 3, "Radius of curve CPs");
358                                 uiBlockEndAlign(block);
359                         }
360                 }
361                                 
362                 if(totedge==1)
363                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Crease:",   0, 20, 200, 20, &(tfp->ve_median[3]), 0.0, 1.0, 1, 3, "");
364                 else if(totedge>1)
365                         uiDefButF(block, NUM, B_OBJECTPANELMEDIAN, "Mean Crease:",      0, 20, 200, 20, &(tfp->ve_median[3]), 0.0, 1.0, 1, 3, "");
366                 
367         }
368         else {  // apply
369                 memcpy(ve_median, tfp->ve_median, sizeof(tfp->ve_median));
370                 
371                 if(v3d->flag & V3D_GLOBAL_STATS) {
372                         invert_m4_m4(ob->imat, ob->obmat);
373                         mul_m4_v3(ob->imat, median);
374                         mul_m4_v3(ob->imat, ve_median);
375                 }
376                 sub_v3_v3v3(median, ve_median, median);
377                 median[3]= ve_median[3]-median[3];
378                 median[4]= ve_median[4]-median[4];
379                 median[5]= ve_median[5]-median[5];
380                 
381                 if(ob->type==OB_MESH) {
382                         Mesh *me= ob->data;
383                         EditMesh *em = BKE_mesh_get_editmesh(me);
384
385                         /* allow for some rounding error becasue of matrix transform */
386                         if(len_v3(median) > 0.000001) {
387                                 EditVert *eve;
388
389                                 for(eve= em->verts.first; eve; eve= eve->next) {
390                                         if(eve->f & SELECT) {
391                                                 add_v3_v3(eve->co, median);
392                                         }
393                                 }
394
395                                 recalc_editnormals(em);
396                         }
397
398                         if(median[3] != 0.0f) {
399                                 EditEdge *eed;
400                                 const float fixed_crease= (ve_median[3] <= 0.0f ? 0.0 : (ve_median[3] >= 1.0f ? 1.0 : FLT_MAX));
401                                 
402                                 if(fixed_crease != FLT_MAX) {
403                                         /* simple case */
404
405                                         for(eed= em->edges.first; eed; eed= eed->next) {
406                                                 if(eed->f & SELECT) {
407                                                         eed->crease= fixed_crease;
408                                                 }
409                                         }
410                                 }
411                                 else {
412                                         /* scale crease to target median */
413                                         float median_new= ve_median[3];
414                                         float median_orig= ve_median[3] - median[3]; /* previous median value */
415
416                                         /* incase of floating point error */
417                                         CLAMP(median_orig, 0.0, 1.0);
418                                         CLAMP(median_new, 0.0, 1.0);
419
420                                         if(median_new < median_orig) {
421                                                 /* scale down */
422                                                 const float sca= median_new / median_orig;
423                                                 
424                                                 for(eed= em->edges.first; eed; eed= eed->next) {
425                                                         if(eed->f & SELECT) {
426                                                                 eed->crease *= sca;
427                                                                 CLAMP(eed->crease, 0.0, 1.0);
428                                                         }
429                                                 }
430                                         }
431                                         else {
432                                                 /* scale up */
433                                                 const float sca= (1.0f - median_new) / (1.0f - median_orig);
434
435                                                 for(eed= em->edges.first; eed; eed= eed->next) {
436                                                         if(eed->f & SELECT) {
437                                                                 eed->crease = 1.0f - ((1.0f - eed->crease) * sca);
438                                                                 CLAMP(eed->crease, 0.0, 1.0);
439                                                         }
440                                                 }
441                                         }
442                                 }
443                         }
444
445                         BKE_mesh_end_editmesh(me, em);
446                 }
447                 else if(ob->type==OB_CURVE || ob->type==OB_SURF) {
448                         Curve *cu= ob->data;
449                         Nurb *nu;
450                         BPoint *bp;
451                         BezTriple *bezt;
452                         int a;
453                         ListBase *nurbs= ED_curve_editnurbs(cu);
454
455                         nu= nurbs->first;
456                         while(nu) {
457                                 if(nu->type == CU_BEZIER) {
458                                         bezt= nu->bezt;
459                                         a= nu->pntsu;
460                                         while(a--) {
461                                                 if(bezt->f2 & SELECT) {
462                                                         add_v3_v3(bezt->vec[0], median);
463                                                         add_v3_v3(bezt->vec[1], median);
464                                                         add_v3_v3(bezt->vec[2], median);
465                                                         bezt->weight+= median[4];
466                                                         bezt->radius+= median[5];
467                                                 }
468                                                 else {
469                                                         if(bezt->f1 & SELECT) {
470                                                                 add_v3_v3(bezt->vec[0], median);
471                                                         }
472                                                         if(bezt->f3 & SELECT) {
473                                                                 add_v3_v3(bezt->vec[2], median);
474                                                         }
475                                                 }
476                                                 bezt++;
477                                         }
478                                 }
479                                 else {
480                                         bp= nu->bp;
481                                         a= nu->pntsu*nu->pntsv;
482                                         while(a--) {
483                                                 if(bp->f1 & SELECT) {
484                                                         add_v3_v3(bp->vec, median);
485                                                         bp->vec[3]+= median[3];
486                                                         bp->weight+= median[4];
487                                                         bp->radius+= median[5];
488                                                 }
489                                                 bp++;
490                                         }
491                                 }
492                                 test2DNurb(nu);
493                                 testhandlesNurb(nu); /* test for bezier too */
494
495                                 nu= nu->next;
496                         }
497                 }
498                 else if(ob->type==OB_LATTICE) {
499                         Lattice *lt= ob->data;
500                         BPoint *bp;
501                         int a;
502                         
503                         a= lt->editlatt->latt->pntsu*lt->editlatt->latt->pntsv*lt->editlatt->latt->pntsw;
504                         bp= lt->editlatt->latt->def;
505                         while(a--) {
506                                 if(bp->f1 & SELECT) {
507                                         add_v3_v3(bp->vec, median);
508                                         bp->weight+= median[4];
509                                 }
510                                 bp++;
511                         }
512                 }
513                 
514 //              ED_undo_push(C, "Transform properties");
515         }
516 }
517 #define B_VGRP_PNL_COPY 1
518 #define B_VGRP_PNL_NORMALIZE 2
519 #define B_VGRP_PNL_EDIT_SINGLE 8 /* or greater */
520 #define B_VGRP_PNL_COPY_SINGLE 16384 /* or greater */
521
522 static void act_vert_def(Object *ob, EditVert **eve, MDeformVert **dvert)
523 {
524         if(ob && ob->mode & OB_MODE_EDIT && ob->type==OB_MESH && ob->defbase.first) {
525                 Mesh *me= ob->data;
526                 EditMesh *em = BKE_mesh_get_editmesh(me);
527                 EditSelection *ese = ((EditSelection*)em->selected.last);
528
529                 if(ese && ese->type == EDITVERT) {
530                         *eve= (EditVert*)ese->data;
531                         *dvert= CustomData_em_get(&em->vdata, (*eve)->data, CD_MDEFORMVERT);
532                         return;
533                 }
534
535                 BKE_mesh_end_editmesh(me, em);
536         }
537
538         *eve= NULL;
539         *dvert= NULL;
540 }
541
542 static void editvert_mirror_update(Object *ob, EditVert *eve, int def_nr, int index)
543 {
544         Mesh *me= ob->data;
545         EditMesh *em = BKE_mesh_get_editmesh(me);
546         EditVert *eve_mirr;
547
548         eve_mirr= editmesh_get_x_mirror_vert(ob, em, eve, eve->co, index);
549
550         if(eve_mirr && eve_mirr != eve) {
551                 MDeformVert *dvert_src= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
552                 MDeformVert *dvert_dst= CustomData_em_get(&em->vdata, eve_mirr->data, CD_MDEFORMVERT);
553                 if(dvert_dst) {
554                         if(def_nr == -1) {
555                                 /* all vgroups, add groups where neded  */
556
557                                 int *flip_map= defgroup_flip_map(ob, 1);
558                                 defvert_sync_mapped(dvert_dst, dvert_src, flip_map, 1);
559                                 MEM_freeN(flip_map);
560                         }
561                         else {
562                                 /* single vgroup */
563                                 MDeformWeight *dw= defvert_verify_index(dvert_dst, defgroup_flip_index(ob, def_nr, 1));
564                                 if(dw) {
565                                         dw->weight= defvert_find_weight(dvert_src, def_nr);
566                                 }
567                         }
568                 }
569         }
570 }
571
572 static void vgroup_adjust_active(Object *ob, int def_nr)
573 {
574         EditVert *eve_act;
575         MDeformVert *dvert_act;
576
577         act_vert_def(ob, &eve_act, &dvert_act);
578
579         if(dvert_act) {
580                 if(((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X)
581                         editvert_mirror_update(ob, eve_act, def_nr, -1);
582         }
583 }
584
585 static void vgroup_copy_active_to_sel(Object *ob)
586 {
587         EditVert *eve_act;
588         MDeformVert *dvert_act;
589
590         act_vert_def(ob, &eve_act, &dvert_act);
591
592         if(dvert_act==NULL) {
593                 return;
594         }
595         else {
596                 Mesh *me= ob->data;
597                 EditMesh *em = BKE_mesh_get_editmesh(me);
598                 EditVert *eve;
599                 MDeformVert *dvert;
600                 int index= 0;
601
602                 for(eve= em->verts.first; eve; eve= eve->next, index++) {
603                         if(eve->f & SELECT && eve != eve_act) {
604                                 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
605                                 if(dvert) {
606                                         defvert_copy(dvert, dvert_act);
607
608                                         if(me->editflag & ME_EDIT_MIRROR_X)
609                                                 editvert_mirror_update(ob, eve, -1, index);
610
611                                 }
612                         }
613                 }
614         }
615 }
616
617 static void vgroup_copy_active_to_sel_single(Object *ob, int def_nr)
618 {
619         EditVert *eve_act;
620         MDeformVert *dvert_act;
621
622         act_vert_def(ob, &eve_act, &dvert_act);
623
624         if(dvert_act==NULL) {
625                 return;
626         }
627         else {
628                 Mesh *me= ob->data;
629                 EditMesh *em = BKE_mesh_get_editmesh(me);
630                 EditVert *eve;
631                 MDeformVert *dvert;
632                 MDeformWeight *dw;
633                 float act_weight = -1.0f;
634                 int i;
635                 int index= 0;
636
637                 for(i=0, dw=dvert_act->dw; i < dvert_act->totweight; i++, dw++) {
638                         if(def_nr == dw->def_nr) {
639                                 act_weight= dw->weight;
640                                 break;
641                         }
642                 }
643
644                 if(act_weight < -0.5f)
645                         return;
646
647                 for(eve= em->verts.first; eve; eve= eve->next, index++) {
648                         if(eve->f & SELECT && eve != eve_act) {
649                                 dvert= CustomData_em_get(&em->vdata, eve->data, CD_MDEFORMVERT);
650                                 if(dvert) {
651                                         for(i=0, dw=dvert->dw; i < dvert->totweight; i++, dw++) {
652                                                 if(def_nr == dw->def_nr) {
653                                                         dw->weight= act_weight;
654
655                                                         if(me->editflag & ME_EDIT_MIRROR_X)
656                                                                 editvert_mirror_update(ob, eve, -1, index);
657
658                                                         break;
659                                                 }
660                                         }
661                                 }
662                         }
663                 }
664
665                 if(me->editflag & ME_EDIT_MIRROR_X)
666                         editvert_mirror_update(ob, eve_act, -1, -1);
667
668         }
669 }
670
671 static void vgroup_normalize_active(Object *ob)
672 {
673         EditVert *eve_act;
674         MDeformVert *dvert_act;
675
676         act_vert_def(ob, &eve_act, &dvert_act);
677
678         if(dvert_act==NULL)
679                 return;
680
681         defvert_normalize(dvert_act);
682
683         if(((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X)
684                 editvert_mirror_update(ob, eve_act, -1, -1);
685
686
687
688 }
689
690 static void do_view3d_vgroup_buttons(bContext *C, void *UNUSED(arg), int event)
691 {
692         Scene *scene= CTX_data_scene(C);
693         Object *ob= OBACT;
694
695         if(event==B_VGRP_PNL_NORMALIZE) {
696                 vgroup_normalize_active(ob);
697         }
698         else if(event == B_VGRP_PNL_COPY) {
699                 vgroup_copy_active_to_sel(ob);
700         }
701         else if(event >= B_VGRP_PNL_COPY_SINGLE) {
702                 vgroup_copy_active_to_sel_single(ob, event - B_VGRP_PNL_COPY_SINGLE);
703         }
704         else if(event >= B_VGRP_PNL_EDIT_SINGLE) {
705                 vgroup_adjust_active(ob, event - B_VGRP_PNL_EDIT_SINGLE);
706         }
707
708 //  todo
709 //      if(((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_X)
710 //              ED_vgroup_mirror(ob, 1, 1, 0);
711
712         /* default for now */
713         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
714         WM_event_add_notifier(C, NC_GEOM|ND_DATA, ob->data);
715 }
716
717 int view3d_panel_vgroup_poll(const bContext *C, PanelType *UNUSED(pt))
718 {
719         Scene *scene= CTX_data_scene(C);
720         Object *ob= OBACT;
721         EditVert *eve_act;
722         MDeformVert *dvert_act;
723
724         act_vert_def(ob, &eve_act, &dvert_act);
725
726         return dvert_act ? dvert_act->totweight : 0;
727 }
728
729
730 static void view3d_panel_vgroup(const bContext *C, Panel *pa)
731 {
732         uiBlock *block= uiLayoutAbsoluteBlock(pa->layout);
733         Scene *scene= CTX_data_scene(C);
734         Object *ob= OBACT;
735
736         EditVert *eve;
737         MDeformVert *dvert;
738
739         act_vert_def(ob, &eve, &dvert);
740
741         if(dvert && dvert->totweight) {
742                 uiLayout *col;
743                 bDeformGroup *dg;
744                 int i;
745                 int yco = 0;
746
747                 uiBlockSetHandleFunc(block, do_view3d_vgroup_buttons, NULL);
748
749                 col= uiLayoutColumn(pa->layout, 0);
750                 block= uiLayoutAbsoluteBlock(col);
751
752                 uiBlockBeginAlign(block);
753
754                 for (i=0; i<dvert->totweight; i++){
755                         dg = BLI_findlink (&ob->defbase, dvert->dw[i].def_nr);
756                         if(dg) {
757                                 uiDefButF(block, NUM, B_VGRP_PNL_EDIT_SINGLE + dvert->dw[i].def_nr, dg->name,   0, yco, 180, 20, &dvert->dw[i].weight, 0.0, 1.0, 1, 3, "");
758                                 uiDefBut(block, BUT, B_VGRP_PNL_COPY_SINGLE + dvert->dw[i].def_nr, "C", 180,yco,20,20, 0, 0, 0, 0, 0, "Copy this groups weight to other selected verts");
759                                 yco -= 20;
760                         }
761                 }
762                 yco-=2;
763
764                 uiBlockEndAlign(block);
765                 uiBlockBeginAlign(block);
766                 uiDefBut(block, BUT, B_VGRP_PNL_NORMALIZE, "Normalize", 0, yco,100,20, 0, 0, 0, 0, 0, "Normalize active vertex weights");
767                 uiDefBut(block, BUT, B_VGRP_PNL_COPY, "Copy", 100,yco,100,20, 0, 0, 0, 0, 0, "Copy active vertex to other seleted verts");
768                 uiBlockEndAlign(block);
769         }
770 }
771
772 static void v3d_transform_butsR(uiLayout *layout, PointerRNA *ptr)
773 {
774         uiLayout *split, *colsub;
775         
776         split = uiLayoutSplit(layout, 0.8, 0);
777         
778         if (ptr->type == &RNA_PoseBone) {
779                 PointerRNA boneptr;
780                 Bone *bone;
781                 
782                 boneptr = RNA_pointer_get(ptr, "bone");
783                 bone = boneptr.data;
784                 uiLayoutSetActive(split, !(bone->parent && bone->flag & BONE_CONNECTED));
785         }
786         colsub = uiLayoutColumn(split, 1);
787         uiItemR(colsub, ptr, "location", 0, "Location", ICON_NULL);
788         colsub = uiLayoutColumn(split, 1);
789         uiItemL(colsub, "", ICON_NULL);
790         uiItemR(colsub, ptr, "lock_location", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
791         
792         split = uiLayoutSplit(layout, 0.8, 0);
793         
794         switch(RNA_enum_get(ptr, "rotation_mode")) {
795                 case ROT_MODE_QUAT: /* quaternion */
796                         colsub = uiLayoutColumn(split, 1);
797                         uiItemR(colsub, ptr, "rotation_quaternion", 0, "Rotation", ICON_NULL);
798                         colsub = uiLayoutColumn(split, 1);
799                         uiItemR(colsub, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE, "4L", ICON_NULL);
800                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
801                                 uiItemR(colsub, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
802                         else
803                                 uiItemL(colsub, "", ICON_NULL);
804                         uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
805                         break;
806                 case ROT_MODE_AXISANGLE: /* axis angle */
807                         colsub = uiLayoutColumn(split, 1);
808                         uiItemR(colsub, ptr, "rotation_axis_angle", 0, "Rotation", ICON_NULL);
809                         colsub = uiLayoutColumn(split, 1);
810                         uiItemR(colsub, ptr, "lock_rotations_4d", UI_ITEM_R_TOGGLE, "4L", ICON_NULL);
811                         if (RNA_boolean_get(ptr, "lock_rotations_4d"))
812                                 uiItemR(colsub, ptr, "lock_rotation_w", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
813                         else
814                                 uiItemL(colsub, "", ICON_NULL);
815                         uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
816                         break;
817                 default: /* euler rotations */
818                         colsub = uiLayoutColumn(split, 1);
819                         uiItemR(colsub, ptr, "rotation_euler", 0, "Rotation", ICON_NULL);
820                         colsub = uiLayoutColumn(split, 1);
821                         uiItemL(colsub, "", ICON_NULL);
822                         uiItemR(colsub, ptr, "lock_rotation", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
823                         break;
824         }
825         uiItemR(layout, ptr, "rotation_mode", 0, "", ICON_NULL);
826         
827         split = uiLayoutSplit(layout, 0.8, 0);
828         colsub = uiLayoutColumn(split, 1);
829         uiItemR(colsub, ptr, "scale", 0, "Scale", ICON_NULL);
830         colsub = uiLayoutColumn(split, 1);
831         uiItemL(colsub, "", ICON_NULL);
832         uiItemR(colsub, ptr, "lock_scale", UI_ITEM_R_TOGGLE+UI_ITEM_R_ICON_ONLY, "", ICON_NULL);
833         
834         if (ptr->type == &RNA_Object) {
835                 Object *ob = ptr->data;
836                 if (ELEM5(ob->type, OB_MESH, OB_CURVE, OB_SURF, OB_FONT, OB_MBALL))
837                         uiItemR(layout, ptr, "dimensions", 0, "Dimensions", ICON_NULL);
838         }
839 }
840
841 static void v3d_posearmature_buts(uiLayout *layout, Object *ob)
842 {
843 //      uiBlock *block= uiLayoutGetBlock(layout);
844 //      bArmature *arm;
845         bPoseChannel *pchan;
846 //      TransformProperties *tfp= v3d->properties_storage;
847         PointerRNA pchanptr;
848         uiLayout *col;
849 //      uiLayout *row;
850 //      uiBut *but;
851
852         pchan= get_active_posechannel(ob);
853
854 //      row= uiLayoutRow(layout, 0);
855         
856         if (!pchan)     {
857                 uiItemL(layout, "No Bone Active", ICON_NULL);
858                 return; 
859         }
860
861         RNA_pointer_create(&ob->id, &RNA_PoseBone, pchan, &pchanptr);
862
863         col= uiLayoutColumn(layout, 0);
864         
865         /* XXX: RNA buts show data in native types (i.e. quats, 4-component axis/angle, etc.)
866          * but oldskool UI shows in eulers always. Do we want to be able to still display in Eulers?
867          * Maybe needs RNA/ui options to display rotations as different types... */
868         v3d_transform_butsR(col, &pchanptr);
869
870 #if 0
871         uiLayoutAbsoluteBlock(layout);
872
873         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
874                 float quat[4];
875                 /* convert to euler, passing through quats... */
876                 axis_angle_to_quat(quat, pchan->rotAxis, pchan->rotAngle);
877                 quat_to_eul( tfp->ob_eul,quat);
878         }
879         else if (pchan->rotmode == ROT_MODE_QUAT)
880                 quat_to_eul( tfp->ob_eul,pchan->quat);
881         else
882                 copy_v3_v3(tfp->ob_eul, pchan->eul);
883         tfp->ob_eul[0]*= 180.0/M_PI;
884         tfp->ob_eul[1]*= 180.0/M_PI;
885         tfp->ob_eul[2]*= 180.0/M_PI;
886         
887         uiDefBut(block, LABEL, 0, "Location:",                  0, 240, 100, 20, 0, 0, 0, 0, 0, "");
888         uiBlockBeginAlign(block);
889         
890         but= uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",      0, 220, 120, 19, pchan->loc, -lim, lim, 100, 3, "");
891         uiButSetUnitType(but, PROP_UNIT_LENGTH);
892         but= uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",      0, 200, 120, 19, pchan->loc+1, -lim, lim, 100, 3, "");
893         uiButSetUnitType(but, PROP_UNIT_LENGTH);
894         but= uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",      0, 180, 120, 19, pchan->loc+2, -lim, lim, 100, 3, "");
895         uiButSetUnitType(but, PROP_UNIT_LENGTH);
896         uiBlockEndAlign(block);
897         
898         uiBlockBeginAlign(block);
899         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");
900         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");
901         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");
902         uiBlockEndAlign(block);
903         
904         uiDefBut(block, LABEL, 0, "Rotation:",                  0, 160, 100, 20, 0, 0, 0, 0, 0, "");
905         uiBlockBeginAlign(block);
906         uiDefButF(block, NUM, B_ARMATUREPANEL3, "X:",   0, 140, 120, 19, tfp->ob_eul, -1000.0, 1000.0, 100, 3, "");
907         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Y:",   0, 120, 120, 19, tfp->ob_eul+1, -1000.0, 1000.0, 100, 3, "");
908         uiDefButF(block, NUM, B_ARMATUREPANEL3, "Z:",   0, 100, 120, 19, tfp->ob_eul+2, -1000.0, 1000.0, 100, 3, "");
909         uiBlockEndAlign(block);
910         
911         uiBlockBeginAlign(block);
912         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");
913         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");
914         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");
915         uiBlockEndAlign(block);
916         
917         uiDefBut(block, LABEL, 0, "Scale:",                             0, 80, 100, 20, 0, 0, 0, 0, 0, "");
918         uiBlockBeginAlign(block);
919         uiDefButF(block, NUM, B_ARMATUREPANEL2, "X:",   0, 60, 120, 19, pchan->size, -lim, lim, 10, 3, "");
920         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Y:",   0, 40, 120, 19, pchan->size+1, -lim, lim, 10, 3, "");
921         uiDefButF(block, NUM, B_ARMATUREPANEL2, "Z:",   0, 20, 120, 19, pchan->size+2, -lim, lim, 10, 3, "");
922         uiBlockEndAlign(block);
923         
924         uiBlockBeginAlign(block);
925         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");
926         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");
927         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");
928         uiBlockEndAlign(block);
929 #endif
930 }
931
932 /* assumes armature editmode */
933 void validate_editbonebutton_cb(bContext *C, void *bonev, void *namev)
934 {
935         EditBone *eBone= bonev;
936         char oldname[sizeof(eBone->name)], newname[sizeof(eBone->name)];
937
938         /* need to be on the stack */
939         BLI_strncpy(newname, eBone->name, sizeof(eBone->name));
940         BLI_strncpy(oldname, (char *)namev, sizeof(eBone->name));
941         /* restore */
942         BLI_strncpy(eBone->name, oldname, sizeof(eBone->name));
943
944         ED_armature_bone_rename(CTX_data_edit_object(C)->data, oldname, newname); // editarmature.c
945         WM_event_add_notifier(C, NC_OBJECT|ND_BONE_SELECT, CTX_data_edit_object(C)); // XXX fix
946 }
947
948 static void v3d_editarmature_buts(uiLayout *layout, Object *ob)
949 {
950 //      uiBlock *block= uiLayoutGetBlock(layout);
951         bArmature *arm= ob->data;
952         EditBone *ebone;
953 //      TransformProperties *tfp= v3d->properties_storage;
954 //      uiLayout *row;
955         uiLayout *col;
956         PointerRNA eboneptr;
957         
958         ebone= arm->act_edbone;
959
960         if (!ebone || (ebone->layer & arm->layer)==0)
961                 return;
962         
963 //      row= uiLayoutRow(layout, 0);
964         RNA_pointer_create(&arm->id, &RNA_EditBone, ebone, &eboneptr);
965
966
967         col= uiLayoutColumn(layout, 0);
968         uiItemR(col, &eboneptr, "head", 0, "Head", ICON_NULL);
969         if (ebone->parent && ebone->flag & BONE_CONNECTED ) {
970                 PointerRNA parptr = RNA_pointer_get(&eboneptr, "parent");
971                 uiItemR(col, &parptr, "tail_radius", 0, "Radius (Parent)", ICON_NULL);
972         } else {
973                 uiItemR(col, &eboneptr, "head_radius", 0, "Radius", ICON_NULL);
974         }
975         
976         uiItemR(col, &eboneptr, "tail", 0, "Tail", ICON_NULL);
977         uiItemR(col, &eboneptr, "tail_radius", 0, "Radius", ICON_NULL);
978         
979         uiItemR(col, &eboneptr, "roll", 0, "Roll", ICON_NULL);
980 }
981
982 static void v3d_editmetaball_buts(uiLayout *layout, Object *ob)
983 {
984         PointerRNA mbptr, ptr;
985         MetaBall *mball= ob->data;
986 //      uiLayout *row;
987         uiLayout *col;
988         
989         if (!mball || !(mball->lastelem)) return;
990         
991         RNA_pointer_create(&mball->id, &RNA_MetaBall, mball, &mbptr);
992         
993 //      row= uiLayoutRow(layout, 0);
994
995         RNA_pointer_create(&mball->id, &RNA_MetaElement, mball->lastelem, &ptr);
996         
997         col= uiLayoutColumn(layout, 0);
998         uiItemR(col, &ptr, "co", 0, "Location", ICON_NULL);
999         
1000         uiItemR(col, &ptr, "radius", 0, "Radius", ICON_NULL);
1001         uiItemR(col, &ptr, "stiffness", 0, "Stiffness", ICON_NULL);
1002         
1003         uiItemR(col, &ptr, "type", 0, "Type", ICON_NULL);
1004         
1005         col= uiLayoutColumn(layout, 1);
1006         switch (RNA_enum_get(&ptr, "type")) {
1007                 case MB_BALL:
1008                         break;
1009                 case MB_CUBE:
1010                         uiItemL(col, "Size:", ICON_NULL);
1011                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NULL);
1012                         uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NULL);
1013                         uiItemR(col, &ptr, "size_z", 0, "Z", ICON_NULL);
1014                         break;
1015                 case MB_TUBE:
1016                         uiItemL(col, "Size:", ICON_NULL);
1017                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NULL);
1018                         break;
1019                 case MB_PLANE:
1020                         uiItemL(col, "Size:", ICON_NULL);
1021                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NULL);
1022                         uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NULL);
1023                         break;
1024                 case MB_ELIPSOID:
1025                         uiItemL(col, "Size:", ICON_NULL);
1026                         uiItemR(col, &ptr, "size_x", 0, "X", ICON_NULL);
1027                         uiItemR(col, &ptr, "size_y", 0, "Y", ICON_NULL);
1028                         uiItemR(col, &ptr, "size_z", 0, "Z", ICON_NULL);
1029                         break;             
1030         }       
1031 }
1032
1033 /* test if 'ob' is a parent somewhere in par's parents */
1034 static int test_parent_loop(Object *par, Object *ob)
1035 {
1036         if(par == NULL) return 0;
1037         if(ob == par) return 1;
1038         return test_parent_loop(par->parent, ob);
1039 }
1040
1041 static void do_view3d_region_buttons(bContext *C, void *UNUSED(index), int event)
1042 {
1043         Main *bmain= CTX_data_main(C);
1044         Scene *scene= CTX_data_scene(C);
1045 //      Object *obedit= CTX_data_edit_object(C);
1046         View3D *v3d= CTX_wm_view3d(C);
1047 //      BoundBox *bb;
1048         Object *ob= OBACT;
1049         TransformProperties *tfp= v3d->properties_storage;
1050         
1051         switch(event) {
1052         
1053         case B_REDR:
1054                 ED_area_tag_redraw(CTX_wm_area(C));
1055                 return; /* no notifier! */
1056                 
1057         case B_OBJECTPANEL:
1058                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1059                 break;
1060
1061         
1062         case B_OBJECTPANELMEDIAN:
1063                 if(ob) {
1064                         v3d_editvertex_buts(NULL, v3d, ob, 1.0);
1065                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1066                 }
1067                 break;
1068                 
1069                 /* note; this case also used for parbone */
1070         case B_OBJECTPANELPARENT:
1071                 if(ob) {
1072                         if(ob->id.lib || test_parent_loop(ob->parent, ob) ) 
1073                                 ob->parent= NULL;
1074                         else {
1075                                 DAG_scene_sort(bmain, scene);
1076                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);
1077                         }
1078                 }
1079                 break;
1080                 
1081
1082         case B_ARMATUREPANEL3:  // rotate button on channel
1083                 {
1084                         bPoseChannel *pchan;
1085                         float eul[3];
1086                         
1087                         pchan= get_active_posechannel(ob);
1088                         if (!pchan) return;
1089                         
1090                         /* make a copy to eul[3], to allow TAB on buttons to work */
1091                         eul[0]= M_PI*tfp->ob_eul[0]/180.0;
1092                         eul[1]= M_PI*tfp->ob_eul[1]/180.0;
1093                         eul[2]= M_PI*tfp->ob_eul[2]/180.0;
1094                         
1095                         if (pchan->rotmode == ROT_MODE_AXISANGLE) {
1096                                 float quat[4];
1097                                 /* convert to axis-angle, passing through quats  */
1098                                 eul_to_quat( quat,eul);
1099                                 quat_to_axis_angle( pchan->rotAxis, &pchan->rotAngle,quat);
1100                         }
1101                         else if (pchan->rotmode == ROT_MODE_QUAT)
1102                                 eul_to_quat( pchan->quat,eul);
1103                         else
1104                                 copy_v3_v3(pchan->eul, eul);
1105                 }
1106                 /* no break, pass on */
1107         case B_ARMATUREPANEL2:
1108                 {
1109                         ob->pose->flag |= (POSE_LOCKED|POSE_DO_UNLOCK);
1110                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1111                 }
1112                 break;
1113         case B_TRANSFORMSPACEADD:
1114         {
1115                 char names[sizeof(((TransformOrientation *)NULL)->name)]= "";
1116                 BIF_createTransformOrientation(C, NULL, names, 1, 0);
1117                 break;
1118         }
1119         case B_TRANSFORMSPACECLEAR:
1120                 BIF_clearTransformOrientation(C);
1121                 break;
1122                 
1123 #if 0 // XXX
1124         case B_WEIGHT0_0:
1125                 wpaint->weight = 0.0f;
1126                 break;
1127                 
1128         case B_WEIGHT1_4:
1129                 wpaint->weight = 0.25f;
1130                 break;
1131         case B_WEIGHT1_2:
1132                 wpaint->weight = 0.5f;
1133                 break;
1134         case B_WEIGHT3_4:
1135                 wpaint->weight = 0.75f;
1136                 break;
1137         case B_WEIGHT1_0:
1138                 wpaint->weight = 1.0f;
1139                 break;
1140                 
1141         case B_OPA1_8:
1142                 wpaint->a = 0.125f;
1143                 break;
1144         case B_OPA1_4:
1145                 wpaint->a = 0.25f;
1146                 break;
1147         case B_OPA1_2:
1148                 wpaint->a = 0.5f;
1149                 break;
1150         case B_OPA3_4:
1151                 wpaint->a = 0.75f;
1152                 break;
1153         case B_OPA1_0:
1154                 wpaint->a = 1.0f;
1155                 break;
1156 #endif
1157         case B_CLR_WPAINT:
1158 //              if(!multires_level1_test()) {
1159                 {
1160                         bDeformGroup *defGroup = BLI_findlink(&ob->defbase, ob->actdef-1);
1161                         if(defGroup) {
1162                                 Mesh *me= ob->data;
1163                                 int a;
1164                                 for(a=0; a<me->totvert; a++)
1165                                         ED_vgroup_vert_remove (ob, defGroup, a);
1166                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1167                         }
1168                 }
1169                 break;
1170         case B_RV3D_LOCKED:
1171         case B_RV3D_BOXVIEW:
1172         case B_RV3D_BOXCLIP:
1173                 {
1174                         ScrArea *sa= CTX_wm_area(C);
1175                         ARegion *ar= sa->regionbase.last;
1176                         RegionView3D *rv3d;
1177                         short viewlock;
1178                         
1179                         ar= ar->prev;
1180                         rv3d= ar->regiondata;
1181                         viewlock= rv3d->viewlock;
1182                         
1183                         if((viewlock & RV3D_LOCKED)==0)
1184                                 viewlock= 0;
1185                         else if((viewlock & RV3D_BOXVIEW)==0)
1186                                 viewlock &= ~RV3D_BOXCLIP;
1187                         
1188                         for(; ar; ar= ar->prev) {
1189                                 if(ar->alignment==RGN_ALIGN_QSPLIT) {
1190                                         rv3d= ar->regiondata;
1191                                         rv3d->viewlock= viewlock;
1192                                 }
1193                         }
1194                         
1195                         if(rv3d->viewlock & RV3D_BOXVIEW)
1196                                 view3d_boxview_copy(sa, sa->regionbase.last);
1197                         
1198                         ED_area_tag_redraw(sa);
1199                 }
1200                 break;
1201         }
1202
1203         /* default for now */
1204         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_VIEW3D, ob);
1205 }
1206
1207 void removeTransformOrientation_func(bContext *C, void *target, void *UNUSED(arg))
1208 {
1209         BIF_removeTransformOrientation(C, (TransformOrientation *) target);
1210 }
1211
1212 void selectTransformOrientation_func(bContext *C, void *target, void *UNUSED(arg))
1213 {
1214         BIF_selectTransformOrientation(C, (TransformOrientation *) target);
1215 }
1216
1217 static void view3d_panel_object(const bContext *C, Panel *pa)
1218 {
1219         uiBlock *block;
1220         Scene *scene= CTX_data_scene(C);
1221         Object *obedit= CTX_data_edit_object(C);
1222         View3D *v3d= CTX_wm_view3d(C);
1223         //uiBut *bt;
1224         Object *ob= OBACT;
1225         TransformProperties *tfp;
1226         PointerRNA obptr;
1227         uiLayout *col, *row;
1228         float lim;
1229         
1230         if(ob==NULL) return;
1231
1232         /* make sure we got storage */
1233         if(v3d->properties_storage==NULL)
1234                 v3d->properties_storage= MEM_callocN(sizeof(TransformProperties), "TransformProperties");
1235         tfp= v3d->properties_storage;
1236         
1237 // XXX  uiSetButLock(object_is_libdata(ob), ERROR_LIBDATA_MESSAGE);
1238         /*
1239         if(ob->mode & (OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT)) {
1240         }
1241         else {
1242                 if((ob->mode & OB_MODE_PARTICLE_EDIT)==0) {
1243                         uiBlockEndAlign(block);
1244                 }
1245         }
1246         */
1247
1248         lim= 10000.0f*MAX2(1.0, v3d->grid);
1249
1250         block= uiLayoutGetBlock(pa->layout);
1251         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1252
1253         col= uiLayoutColumn(pa->layout, 0);
1254         row= uiLayoutRow(col, 0);
1255         RNA_id_pointer_create(&ob->id, &obptr);
1256
1257         if(ob==obedit) {
1258                 if(ob->type==OB_ARMATURE) v3d_editarmature_buts(col, ob);
1259                 if(ob->type==OB_MBALL) v3d_editmetaball_buts(col, ob);
1260                 else v3d_editvertex_buts(col, v3d, ob, lim);
1261         }
1262         else if(ob->mode & OB_MODE_POSE) {
1263                 v3d_posearmature_buts(col, ob);
1264         }
1265         else {
1266
1267                 v3d_transform_butsR(col, &obptr);
1268         }
1269 }
1270
1271 #if 0
1272 static void view3d_panel_preview(bContext *C, ARegion *ar, short cntrl) // VIEW3D_HANDLER_PREVIEW
1273 {
1274         uiBlock *block;
1275         View3D *v3d= sa->spacedata.first;
1276         int ofsx, ofsy;
1277         
1278         block= uiBeginBlock(C, ar, "view3d_panel_preview", UI_EMBOSS);
1279         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | UI_PNL_SCALE | cntrl);
1280         uiSetPanelHandler(VIEW3D_HANDLER_PREVIEW);  // for close and esc
1281         
1282         ofsx= -150+(sa->winx/2)/v3d->blockscale;
1283         ofsy= -100+(sa->winy/2)/v3d->blockscale;
1284         if(uiNewPanel(C, ar, block, "Preview", "View3d", ofsx, ofsy, 300, 200)==0) return;
1285
1286         uiBlockSetDrawExtraFunc(block, BIF_view3d_previewdraw);
1287         
1288         if(scene->recalc & SCE_PRV_CHANGED) {
1289                 scene->recalc &= ~SCE_PRV_CHANGED;
1290                 //printf("found recalc\n");
1291                 BIF_view3d_previewrender_free(sa->spacedata.first);
1292                 BIF_preview_changed(0);
1293         }
1294 }
1295 #endif
1296
1297 #if 0 // XXX not used
1298 static void delete_sketch_armature(bContext *C, void *arg1, void *arg2)
1299 {
1300         BIF_deleteSketch(C);
1301 }
1302
1303 static void convert_sketch_armature(bContext *C, void *arg1, void *arg2)
1304 {
1305         BIF_convertSketch(C);
1306 }
1307
1308 static void assign_template_sketch_armature(bContext *C, void *arg1, void *arg2)
1309 {
1310         int index = *(int*)arg1;
1311         BIF_setTemplate(C, index);
1312 }
1313
1314
1315 static int view3d_panel_bonesketch_spaces_poll(const bContext *C, PanelType *pt)
1316 {
1317         Object *obedit = CTX_data_edit_object(C);
1318
1319         /* replace with check call to sketching lib */
1320         return (obedit && obedit->type == OB_ARMATURE);
1321 }
1322 static void view3d_panel_bonesketch_spaces(const bContext *C, Panel *pa)
1323 {
1324         Scene *scene = CTX_data_scene(C);
1325         static int template_index;
1326         static char joint_label[128];
1327         uiBlock *block;
1328         uiBut *but;
1329         char *bone_name;
1330         int yco = 130;
1331         int nb_joints;
1332         static char subdiv_tooltip[4][64] = {
1333                 "Subdivide arcs based on a fixed number of bones",
1334                 "Subdivide arcs in bones of equal length",
1335                 "Subdivide arcs based on correlation",
1336                 "Retarget template to stroke"
1337                 };
1338
1339         
1340         block= uiLayoutAbsoluteBlock(pa->layout);
1341         uiBlockSetHandleFunc(block, do_view3d_region_buttons, NULL);
1342
1343         uiBlockBeginAlign(block);
1344         
1345         /* use real flag instead of 1 */
1346         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)");
1347         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");
1348         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");
1349         yco -= 20;
1350         
1351         but = uiDefBut(block, BUT, B_REDR, "Convert", 10,yco,100,20, 0, 0, 0, 0, 0, "Convert sketch to armature");
1352         uiButSetFunc(but, convert_sketch_armature, NULL, NULL);
1353
1354         but = uiDefBut(block, BUT, B_REDR, "Delete", 110,yco,100,20, 0, 0, 0, 0, 0, "Delete sketch");
1355         uiButSetFunc(but, delete_sketch_armature, NULL, NULL);
1356         yco -= 20;
1357         
1358         uiBlockEndAlign(block);
1359
1360         uiBlockBeginAlign(block);
1361         
1362         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]);
1363
1364         switch(scene->toolsettings->bone_sketching_convert)
1365         {
1366         case SK_CONVERT_CUT_LENGTH:
1367                 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");
1368                 yco -= 20;
1369                 break;
1370         case SK_CONVERT_CUT_ADAPTATIVE:
1371                 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");
1372                 yco -= 20;
1373                 break;
1374         default:
1375         case SK_CONVERT_CUT_FIXED:
1376                 uiDefButC(block, NUM, B_REDR,                                   "Num:",         70, yco, 140, 19, &scene->toolsettings->skgen_subdivision_number,1, 100, 1, 5,  "Number of subdivided bones");
1377                 yco -= 20;
1378                 break;
1379         case SK_CONVERT_RETARGET:
1380                 uiDefButC(block, ROW, B_NOP, "No",                      70,  yco, 40,19, &scene->toolsettings->skgen_retarget_roll, 0, 0, 0, 0,                                                                 "No special roll treatment");
1381                 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");
1382                 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");
1383                 yco -= 30;
1384
1385                 uiBlockEndAlign(block);
1386
1387                 uiBlockBeginAlign(block);
1388                 /* button here to select what to do (copy or not), template, ...*/
1389
1390                 BIF_makeListTemplates(C);
1391                 template_index = BIF_currentTemplate(C);
1392                 
1393                 but = uiDefButI(block, MENU, B_REDR, BIF_listTemplates(C), 10,yco,200,19, &template_index, 0, 0, 0, 0, "Template");
1394                 uiButSetFunc(but, assign_template_sketch_armature, &template_index, NULL);
1395                 
1396                 yco -= 20;
1397                 
1398                 uiDefButF(block, NUM, B_NOP,                                                    "A:",                   10, yco, 66,19, &scene->toolsettings->skgen_retarget_angle_weight, 0, 10, 1, 0,         "Angle Weight");
1399                 uiDefButF(block, NUM, B_NOP,                                                    "L:",                   76, yco, 67,19, &scene->toolsettings->skgen_retarget_length_weight, 0, 10, 1, 0,                "Length Weight");
1400                 uiDefButF(block, NUM, B_NOP,                                                    "D:",           143,yco, 67,19, &scene->toolsettings->skgen_retarget_distance_weight, 0, 10, 1, 0,              "Distance Weight");
1401                 yco -= 20;
1402                 
1403                 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");
1404                 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");
1405                 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");        
1406                 yco -= 20;
1407
1408                 /* auto renaming magic */
1409                 uiBlockEndAlign(block);
1410                 
1411                 nb_joints = BIF_nbJointsTemplate(C);
1412
1413                 if (nb_joints == -1)
1414                 {
1415                         //XXX
1416                         //nb_joints = G.totvertsel;
1417                 }
1418                 
1419                 bone_name = BIF_nameBoneTemplate(C);
1420                 
1421                 BLI_snprintf(joint_label, 32, "%i joints: %s", nb_joints, bone_name);
1422                 
1423                 uiDefBut(block, LABEL, 1, joint_label,                                  10, yco, 200, 20, NULL, 0.0, 0.0, 0, 0, "");
1424                 yco -= 20;
1425                 break;
1426         }
1427
1428         uiBlockEndAlign(block);
1429 }
1430
1431 #endif // XXX not used
1432
1433 void view3d_buttons_register(ARegionType *art)
1434 {
1435         PanelType *pt;
1436
1437         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel object");
1438         strcpy(pt->idname, "VIEW3D_PT_object");
1439         strcpy(pt->label, "Transform");
1440         pt->draw= view3d_panel_object;
1441         BLI_addtail(&art->paneltypes, pt);
1442         
1443         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel gpencil");
1444         strcpy(pt->idname, "VIEW3D_PT_gpencil");
1445         strcpy(pt->label, "Grease Pencil");
1446         pt->draw= gpencil_panel_standard;
1447         BLI_addtail(&art->paneltypes, pt);
1448
1449         pt= MEM_callocN(sizeof(PanelType), "spacetype view3d panel vgroup");
1450         strcpy(pt->idname, "VIEW3D_PT_vgroup");
1451         strcpy(pt->label, "Vertex Groups");
1452         pt->draw= view3d_panel_vgroup;
1453         pt->poll= view3d_panel_vgroup_poll;
1454         BLI_addtail(&art->paneltypes, pt);
1455
1456         // XXX view3d_panel_preview(C, ar, 0);
1457 }
1458
1459 static int view3d_properties(bContext *C, wmOperator *UNUSED(op))
1460 {
1461         ScrArea *sa= CTX_wm_area(C);
1462         ARegion *ar= view3d_has_buttons_region(sa);
1463         
1464         if(ar)
1465                 ED_region_toggle_hidden(C, ar);
1466
1467         return OPERATOR_FINISHED;
1468 }
1469
1470 void VIEW3D_OT_properties(wmOperatorType *ot)
1471 {
1472         ot->name= "Properties";
1473         ot->description= "Toggles the properties panel display";
1474         ot->idname= "VIEW3D_OT_properties";
1475         
1476         ot->exec= view3d_properties;
1477         ot->poll= ED_operator_view3d_active;
1478         
1479         /* flags */
1480         ot->flag= 0;
1481 }