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