svn merge -r41575:41602 ^/trunk/blender
[blender.git] / source / blender / editors / object / object_edit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation, 2002-2008 full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/object/object_edit.c
27  *  \ingroup edobj
28  */
29
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33 #include <time.h>
34 #include <float.h>
35 #include <ctype.h>
36 #include <stddef.h> //for offsetof
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_blenlib.h"
41 #include "BLI_math.h"
42 #include "BLI_utildefines.h"
43 #include "BLI_editVert.h"
44 #include "BLI_ghash.h"
45 #include "BLI_rand.h"
46
47 #include "DNA_armature_types.h"
48 #include "DNA_curve_types.h"
49 #include "DNA_group_types.h"
50 #include "DNA_lattice_types.h"
51 #include "DNA_material_types.h"
52 #include "DNA_meta_types.h"
53 #include "DNA_property_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_object_force.h"
57 #include "DNA_meshdata_types.h"
58 #include "DNA_vfont_types.h"
59
60 #include "IMB_imbuf_types.h"
61
62 #include "BKE_anim.h"
63 #include "BKE_constraint.h"
64 #include "BKE_context.h"
65 #include "BKE_curve.h"
66 #include "BKE_effect.h"
67 #include "BKE_depsgraph.h"
68 #include "BKE_font.h"
69 #include "BKE_image.h"
70 #include "BKE_library.h"
71 #include "BKE_main.h"
72 #include "BKE_material.h"
73 #include "BKE_mball.h"
74 #include "BKE_mesh.h"
75 #include "BKE_object.h"
76 #include "BKE_paint.h"
77 #include "BKE_pointcache.h"
78 #include "BKE_property.h"
79 #include "BKE_sca.h"
80 #include "BKE_softbody.h"
81 #include "BKE_modifier.h"
82 #include "BKE_tessmesh.h"
83
84 #include "ED_armature.h"
85 #include "ED_curve.h"
86 #include "ED_mesh.h"
87 #include "ED_mball.h"
88 #include "ED_lattice.h"
89 #include "ED_object.h"
90 #include "ED_screen.h"
91 #include "ED_util.h"
92
93
94 #include "RNA_access.h"
95 #include "RNA_define.h"
96 #include "RNA_enum_types.h"
97
98 /* for menu/popup icons etc etc*/
99
100 #include "UI_interface.h"
101 #include "WM_api.h"
102 #include "WM_types.h"
103
104 #include "object_intern.h"      // own include
105
106 /* ************* XXX **************** */
107 static void error(const char *UNUSED(arg)) {}
108 static void waitcursor(int UNUSED(val)) {}
109 static int pupmenu(const char *UNUSED(msg)) {return 0;}
110
111 /* port over here */
112 static bContext *evil_C;
113 static void error_libdata(void) {}
114
115
116 /* find the correct active object per context
117  * note: context can be NULL when called from a enum with PROP_ENUM_NO_CONTEXT */
118 Object *ED_object_active_context(bContext *C)
119 {
120         Object *ob= NULL;
121         if(C) {
122                 ob= CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
123                 if (!ob) ob= CTX_data_active_object(C);
124         }
125         return ob;
126 }
127
128
129 /* ********* clear/set restrict view *********/
130 static int object_hide_view_clear_exec(bContext *C, wmOperator *UNUSED(op))
131 {
132         Main *bmain= CTX_data_main(C);
133         ScrArea *sa= CTX_wm_area(C);
134         View3D *v3d= sa->spacedata.first;
135         Scene *scene= CTX_data_scene(C);
136         Base *base;
137         int changed = 0;
138         
139         /* XXX need a context loop to handle such cases */
140         for(base = FIRSTBASE; base; base=base->next){
141                 if((base->lay & v3d->lay) && base->object->restrictflag & OB_RESTRICT_VIEW) {
142                         base->flag |= SELECT;
143                         base->object->flag = base->flag;
144                         base->object->restrictflag &= ~OB_RESTRICT_VIEW; 
145                         changed = 1;
146                 }
147         }
148         if (changed) {
149                 DAG_id_type_tag(bmain, ID_OB);
150                 DAG_scene_sort(bmain, scene);
151                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, scene);
152         }
153
154         return OPERATOR_FINISHED;
155 }
156
157 void OBJECT_OT_hide_view_clear(wmOperatorType *ot)
158 {
159         
160         /* identifiers */
161         ot->name= "Clear Restrict View";
162         ot->description = "Reveal the object by setting the hide flag";
163         ot->idname= "OBJECT_OT_hide_view_clear";
164         
165         /* api callbacks */
166         ot->exec= object_hide_view_clear_exec;
167         ot->poll= ED_operator_view3d_active;
168         
169         /* flags */
170         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
171 }
172
173 static int object_hide_view_set_exec(bContext *C, wmOperator *op)
174 {
175         Main *bmain= CTX_data_main(C);
176         Scene *scene= CTX_data_scene(C);
177         short changed = 0;
178         const int unselected= RNA_boolean_get(op->ptr, "unselected");
179         
180         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
181                 if(!unselected) {
182                         if (base->flag & SELECT){
183                                 base->flag &= ~SELECT;
184                                 base->object->flag = base->flag;
185                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
186                                 changed = 1;
187                                 if (base==BASACT) {
188                                         ED_base_object_activate(C, NULL);
189                                 }
190                         }
191                 }
192                 else {
193                         if (!(base->flag & SELECT)){
194                                 base->object->restrictflag |= OB_RESTRICT_VIEW;
195                                 changed = 1;
196                         }
197                 }       
198         }
199         CTX_DATA_END;
200
201         if (changed) {
202                 DAG_id_type_tag(bmain, ID_OB);
203                 DAG_scene_sort(bmain, scene);
204                 
205                 WM_event_add_notifier(C, NC_SCENE|ND_OB_SELECT, CTX_data_scene(C));
206                 
207         }
208
209         return OPERATOR_FINISHED;
210 }
211
212 void OBJECT_OT_hide_view_set(wmOperatorType *ot)
213 {
214         /* identifiers */
215         ot->name= "Set Restrict View";
216         ot->description = "Hide the object by setting the hide flag";
217         ot->idname= "OBJECT_OT_hide_view_set";
218         
219         /* api callbacks */
220         ot->exec= object_hide_view_set_exec;
221         ot->poll= ED_operator_view3d_active;
222         
223         /* flags */
224         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
225         
226         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
227         
228 }
229
230 /* 99% same as above except no need for scene refreshing (TODO, update render preview) */
231 static int object_hide_render_clear_exec(bContext *C, wmOperator *UNUSED(op))
232 {
233         short changed= 0;
234
235         /* XXX need a context loop to handle such cases */
236         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
237                 if(ob->restrictflag & OB_RESTRICT_RENDER) {
238                         ob->restrictflag &= ~OB_RESTRICT_RENDER;
239                         changed= 1;
240                 }
241         }
242         CTX_DATA_END;
243
244         if(changed)
245                 WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
246
247         return OPERATOR_FINISHED;
248 }
249
250 void OBJECT_OT_hide_render_clear(wmOperatorType *ot)
251 {
252
253         /* identifiers */
254         ot->name= "Clear Restrict Render";
255         ot->description = "Reveal the render object by setting the hide render flag";
256         ot->idname= "OBJECT_OT_hide_render_clear";
257
258         /* api callbacks */
259         ot->exec= object_hide_render_clear_exec;
260         ot->poll= ED_operator_view3d_active;
261
262         /* flags */
263         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
264 }
265
266 static int object_hide_render_set_exec(bContext *C, wmOperator *op)
267 {
268         const int unselected= RNA_boolean_get(op->ptr, "unselected");
269
270         CTX_DATA_BEGIN(C, Base*, base, visible_bases) {
271                 if(!unselected) {
272                         if (base->flag & SELECT){
273                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
274                         }
275                 }
276                 else {
277                         if (!(base->flag & SELECT)){
278                                 base->object->restrictflag |= OB_RESTRICT_RENDER;
279                         }
280                 }
281         }
282         CTX_DATA_END;
283         WM_event_add_notifier(C, NC_SPACE|ND_SPACE_OUTLINER, NULL);
284         return OPERATOR_FINISHED;
285 }
286
287 void OBJECT_OT_hide_render_set(wmOperatorType *ot)
288 {
289         /* identifiers */
290         ot->name= "Set Restrict Render";
291         ot->description = "Hide the render object by setting the hide render flag";
292         ot->idname= "OBJECT_OT_hide_render_set";
293
294         /* api callbacks */
295         ot->exec= object_hide_render_set_exec;
296         ot->poll= ED_operator_view3d_active;
297
298         /* flags */
299         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
300
301         RNA_def_boolean(ot->srna, "unselected", 0, "Unselected", "Hide unselected rather than selected objects");
302 }
303
304 /* ******************* toggle editmode operator  ***************** */
305
306 void ED_object_exit_editmode(bContext *C, int flag)
307 {
308         /* Note! only in exceptional cases should 'EM_DO_UNDO' NOT be in the flag */
309
310         Scene *scene= CTX_data_scene(C);
311         Object *obedit= CTX_data_edit_object(C);
312         int freedata = flag & EM_FREEDATA;
313         
314         if(obedit==NULL) return;
315         
316         if(flag & EM_WAITCURSOR) waitcursor(1);
317         if(obedit->type==OB_MESH) {
318                 Mesh *me= obedit->data;
319                 
320 //              if(EM_texFaceCheck())
321                 
322 //              if(retopo_mesh_paint_check())
323 //                      retopo_end_okee();
324                 
325                 if(me->edit_btmesh->bm->totvert>MESH_MAX_VERTS) {
326                         error("Too many vertices");
327                         return;
328                 }
329                 
330                 EDBM_LoadEditBMesh(scene, obedit);
331                 
332                 if(freedata) {
333                         EDBM_FreeEditBMesh(me->edit_btmesh);
334                         MEM_freeN(me->edit_btmesh);
335                         me->edit_btmesh= NULL;
336                 }
337                 if(obedit->restore_mode & OB_MODE_WEIGHT_PAINT) {
338                         mesh_octree_table(NULL, NULL, NULL, 'e');
339                         mesh_mirrtopo_table(NULL, 'e');
340                 }
341         }
342         else if (obedit->type==OB_ARMATURE) {   
343                 ED_armature_from_edit(obedit);
344                 if(freedata)
345                         ED_armature_edit_free(obedit);
346         }
347         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
348                 load_editNurb(obedit);
349                 if(freedata) free_editNurb(obedit);
350         }
351         else if(obedit->type==OB_FONT && freedata) {
352                 load_editText(obedit);
353                 if(freedata) free_editText(obedit);
354         }
355         else if(obedit->type==OB_LATTICE) {
356                 load_editLatt(obedit);
357                 if(freedata) free_editLatt(obedit);
358         }
359         else if(obedit->type==OB_MBALL) {
360                 load_editMball(obedit);
361                 if(freedata) free_editMball(obedit);
362         }
363
364         /* freedata only 0 now on file saves and render */
365         if(freedata) {
366                 ListBase pidlist;
367                 PTCacheID *pid;
368
369                 /* for example; displist make is different in editmode */
370                 scene->obedit= NULL; // XXX for context
371
372                 /* flag object caches as outdated */
373                 BKE_ptcache_ids_from_object(&pidlist, obedit, NULL, 0);
374                 for(pid=pidlist.first; pid; pid=pid->next) {
375                         if(pid->type != PTCACHE_TYPE_PARTICLES) /* particles don't need reset on geometry change */
376                                 pid->cache->flag |= PTCACHE_OUTDATED;
377                 }
378                 BLI_freelistN(&pidlist);
379                 
380                 BKE_ptcache_object_reset(scene, obedit, PTCACHE_RESET_OUTDATED);
381
382                 /* also flush ob recalc, doesn't take much overhead, but used for particles */
383                 DAG_id_tag_update(&obedit->id, OB_RECALC_OB|OB_RECALC_DATA);
384         
385                 if(flag & EM_DO_UNDO)
386                         ED_undo_push(C, "Editmode");
387         
388                 if(flag & EM_WAITCURSOR) waitcursor(0);
389         
390                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
391
392                 obedit->mode &= ~OB_MODE_EDIT;
393         }
394 }
395
396
397 void ED_object_enter_editmode(bContext *C, int flag)
398 {
399         Scene *scene= CTX_data_scene(C);
400         Base *base= NULL;
401         Object *ob;
402         ScrArea *sa= CTX_wm_area(C);
403         View3D *v3d= NULL;
404         int ok= 0;
405         
406         if(scene->id.lib) return;
407         
408         if(sa && sa->spacetype==SPACE_VIEW3D)
409                 v3d= sa->spacedata.first;
410         
411         if((flag & EM_IGNORE_LAYER)==0) {
412                 base= CTX_data_active_base(C); /* active layer checked here for view3d */
413
414                 if(base==NULL) return;
415                 else if(v3d && (base->lay & v3d->lay)==0) return;
416                 else if(!v3d && (base->lay & scene->lay)==0) return;
417         }
418         else {
419                 base= scene->basact;
420         }
421
422         if (ELEM3(NULL, base, base->object, base->object->data)) return;
423
424         ob = base->object;
425         
426         if (object_data_is_libdata(ob)) {
427                 error_libdata();
428                 return;
429         }
430         
431         if(flag & EM_WAITCURSOR) waitcursor(1);
432
433         ob->restore_mode = ob->mode;
434
435         /* note, when switching scenes the object can have editmode data but
436          * not be scene->obedit: bug 22954, this avoids calling self eternally */
437         if((ob->restore_mode & OB_MODE_EDIT)==0)
438                 ED_object_toggle_modes(C, ob->mode);
439
440         ob->mode= OB_MODE_EDIT;
441         
442         if(ob->type==OB_MESH) {
443                 Mesh *me= ob->data;
444                 
445                 if(me->pv) mesh_pmv_off(me);
446                 ok= 1;
447                 scene->obedit= ob;      // context sees this
448                 
449                 EDBM_MakeEditBMesh(CTX_data_tool_settings(C), scene, ob);
450
451                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MESH, scene);
452         }
453         else if (ob->type==OB_ARMATURE){
454                 bArmature *arm= base->object->data;
455                 if (!arm) return;
456                 /*
457                  * The function object_data_is_libdata make a problem here, the
458                  * check for ob->proxy return 0 and let blender enter to edit mode
459                  * this causa a crash when you try leave the edit mode.
460                  * The problem is that i can't remove the ob->proxy check from
461                  * object_data_is_libdata that prevent the bugfix #6614, so
462                  * i add this little hack here.
463                  */
464                 if(arm->id.lib) {
465                         error_libdata();
466                         return;
467                 }
468                 ok=1;
469                 scene->obedit= ob;
470                 ED_armature_to_edit(ob);
471                 /* to ensure all goes in restposition and without striding */
472                 DAG_id_tag_update(&ob->id, OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME); // XXX: should this be OB_RECALC_DATA?
473
474                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_ARMATURE, scene);
475         }
476         else if(ob->type==OB_FONT) {
477                 scene->obedit= ob; // XXX for context
478                 ok= 1;
479                  make_editText(ob);
480
481                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_TEXT, scene);
482         }
483         else if(ob->type==OB_MBALL) {
484                 scene->obedit= ob; // XXX for context
485                 ok= 1;
486                 make_editMball(ob);
487
488                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_MBALL, scene);
489         }
490         else if(ob->type==OB_LATTICE) {
491                 scene->obedit= ob; // XXX for context
492                 ok= 1;
493                 make_editLatt(ob);
494                 
495                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_LATTICE, scene);
496         }
497         else if(ob->type==OB_SURF || ob->type==OB_CURVE) {
498                 ok= 1;
499                 scene->obedit= ob; // XXX for context
500                 make_editNurb(ob);
501                 
502                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_EDITMODE_CURVE, scene);
503         }
504         
505         if(ok) {
506                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
507         }
508         else {
509                 scene->obedit= NULL; // XXX for context
510                 ob->mode &= ~OB_MODE_EDIT;
511                 WM_event_add_notifier(C, NC_SCENE|ND_MODE|NS_MODE_OBJECT, scene);
512         }
513         
514         if(flag & EM_DO_UNDO) ED_undo_push(C, "Enter Editmode");
515         if(flag & EM_WAITCURSOR) waitcursor(0);
516 }
517
518 static int editmode_toggle_exec(bContext *C, wmOperator *UNUSED(op))
519 {
520         if(!CTX_data_edit_object(C))
521                 ED_object_enter_editmode(C, EM_WAITCURSOR);
522         else
523                 ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR); /* had EM_DO_UNDO but op flag calls undo too [#24685] */
524         
525         return OPERATOR_FINISHED;
526 }
527
528 static int editmode_toggle_poll(bContext *C)
529 {
530         Object *ob = CTX_data_active_object(C);
531
532         /* covers proxies too */
533         if(ELEM(NULL, ob, ob->data) || ((ID *)ob->data)->lib)
534                 return 0;
535
536         if (ob->restrictflag & OB_RESTRICT_VIEW)
537                 return 0;
538
539         return (ob->type == OB_MESH || ob->type == OB_ARMATURE ||
540                           ob->type == OB_FONT || ob->type == OB_MBALL ||
541                           ob->type == OB_LATTICE || ob->type == OB_SURF ||
542                           ob->type == OB_CURVE);
543 }
544
545 void OBJECT_OT_editmode_toggle(wmOperatorType *ot)
546 {
547         
548         /* identifiers */
549         ot->name= "Toggle Editmode";
550         ot->description = "Toggle object's editmode";
551         ot->idname= "OBJECT_OT_editmode_toggle";
552         
553         /* api callbacks */
554         ot->exec= editmode_toggle_exec;
555         
556         ot->poll= editmode_toggle_poll;
557         
558         /* flags */
559         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
560 }
561
562 /* *************************** */
563
564 static int posemode_exec(bContext *C, wmOperator *UNUSED(op))
565 {
566         Base *base= CTX_data_active_base(C);
567         
568         if(base->object->type==OB_ARMATURE) {
569                 if(base->object==CTX_data_edit_object(C)) {
570                         ED_object_exit_editmode(C, EM_FREEDATA|EM_DO_UNDO);
571                         ED_armature_enter_posemode(C, base);
572                 }
573                 else if(base->object->mode & OB_MODE_POSE)
574                         ED_armature_exit_posemode(C, base);
575                 else
576                         ED_armature_enter_posemode(C, base);
577                 
578                 return OPERATOR_FINISHED;
579         }
580         
581         return OPERATOR_PASS_THROUGH;
582 }
583
584 void OBJECT_OT_posemode_toggle(wmOperatorType *ot) 
585 {
586         /* identifiers */
587         ot->name= "Toggle Pose Mode";
588         ot->idname= "OBJECT_OT_posemode_toggle";
589         ot->description= "Enable or disable posing/selecting bones";
590         
591         /* api callbacks */
592         ot->exec= posemode_exec;
593         ot->poll= ED_operator_object_active_editable;
594         
595         /* flag */
596         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
597 }
598
599 /* *********************** */
600
601 #if 0
602 // XXX should be in view3d?
603
604 /* context: ob = lamp */
605 /* code should be replaced with proper (custom) transform handles for lamp properties */
606 static void spot_interactive(Object *ob, int mode)
607 {
608         Lamp *la= ob->data;
609         float transfac, dx, dy, ratio, origval;
610         int keep_running= 1, center2d[2];
611         int mval[2], mvalo[2];
612         
613 //      getmouseco_areawin(mval);
614 //      getmouseco_areawin(mvalo);
615         
616         project_int(ob->obmat[3], center2d);
617         if( center2d[0] > 100000 ) {            /* behind camera */
618 //              center2d[0]= curarea->winx/2;
619 //              center2d[1]= curarea->winy/2;
620         }
621
622 //      helpline(mval, center2d);
623         
624         /* ratio is like scaling */
625         dx = (float)(center2d[0] - mval[0]);
626         dy = (float)(center2d[1] - mval[1]);
627         transfac = (float)sqrt( dx*dx + dy*dy);
628         if(transfac==0.0f) transfac= 1.0f;
629         
630         if(mode==1)     
631                 origval= la->spotsize;
632         else if(mode==2)        
633                 origval= la->dist;
634         else if(mode==3)        
635                 origval= la->clipsta;
636         else    
637                 origval= la->clipend;
638         
639         while (keep_running>0) {
640                 
641 //              getmouseco_areawin(mval);
642                 
643                 /* essential for idling subloop */
644                 if(mval[0]==mvalo[0] && mval[1]==mvalo[1]) {
645                         PIL_sleep_ms(2);
646                 }
647                 else {
648                         char str[32];
649                         
650                         dx = (float)(center2d[0] - mval[0]);
651                         dy = (float)(center2d[1] - mval[1]);
652                         ratio = (float)(sqrt( dx*dx + dy*dy))/transfac;
653                         
654                         /* do the trick */
655                         
656                         if(mode==1) {   /* spot */
657                                 la->spotsize = ratio*origval;
658                                 CLAMP(la->spotsize, 1.0f, 180.0f);
659                                 sprintf(str, "Spot size %.2f\n", la->spotsize);
660                         }
661                         else if(mode==2) {      /* dist */
662                                 la->dist = ratio*origval;
663                                 CLAMP(la->dist, 0.01f, 5000.0f);
664                                 sprintf(str, "Distance %.2f\n", la->dist);
665                         }
666                         else if(mode==3) {      /* sta */
667                                 la->clipsta = ratio*origval;
668                                 CLAMP(la->clipsta, 0.001f, 5000.0f);
669                                 sprintf(str, "Distance %.2f\n", la->clipsta);
670                         }
671                         else if(mode==4) {      /* end */
672                                 la->clipend = ratio*origval;
673                                 CLAMP(la->clipend, 0.1f, 5000.0f);
674                                 sprintf(str, "Clip End %.2f\n", la->clipend);
675                         }
676
677                         /* cleanup */
678                         mvalo[0]= mval[0];
679                         mvalo[1]= mval[1];
680                         
681                         /* handle shaded mode */
682 // XXX                  shade_buttons_change_3d();
683
684                         /* DRAW */      
685                         headerprint(str);
686                         force_draw_plus(SPACE_BUTS, 0);
687
688 //                      helpline(mval, center2d);
689                 }
690                 
691                 while( qtest() ) {
692                         short val;
693                         unsigned short event= extern_qread(&val);
694                         
695                         switch (event){
696                                 case ESCKEY:
697                                 case RIGHTMOUSE:
698                                         keep_running= 0;
699                                         break;
700                                 case LEFTMOUSE:
701                                 case SPACEKEY:
702                                 case PADENTER:
703                                 case RETKEY:
704                                         if(val)
705                                                 keep_running= -1;
706                                         break;
707                         }
708                 }
709         }
710
711         if(keep_running==0) {
712                 if(mode==1)     
713                         la->spotsize= origval;
714                 else if(mode==2)        
715                         la->dist= origval;
716                 else if(mode==3)        
717                         la->clipsta= origval;
718                 else    
719                         la->clipend= origval;
720         }
721
722 }
723 #endif
724
725 static void UNUSED_FUNCTION(special_editmenu)(Scene *scene, View3D *v3d)
726 {
727 // XXX  static short numcuts= 2;
728         Object *ob= OBACT;
729         Object *obedit= NULL; // XXX
730         int nr,ret=0;
731         
732         if(ob==NULL) return;
733         
734         if(obedit==NULL) {
735                 
736                 if(ob->mode & OB_MODE_POSE) {
737 // XXX                  pose_special_editmenu();
738                 }
739                 else if(paint_facesel_test(ob)) {
740                         Mesh *me= get_mesh(ob);
741                         MTFace *tface;
742                         MFace *mface;
743                         int a;
744
745                         if(me==NULL || me->mtface==NULL) return;
746
747                         nr= pupmenu("Specials%t|Set     Tex%x1|         Shared%x2|         Light%x3|         Invisible%x4|         Collision%x5|         TwoSide%x6|Clr     Tex%x7|         Shared%x8|         Light%x9|         Invisible%x10|         Collision%x11|         TwoSide%x12");
748                         
749                         tface= me->mtface;
750                         mface= me->mface;
751                         for(a=me->totface; a>0; a--, tface++, mface++) {
752                                 if(mface->flag & ME_FACE_SEL) {
753                                         switch(nr) {
754                                         case 1:
755                                                 tface->mode |= TF_TEX; break;
756                                         case 2:
757                                                 tface->mode |= TF_SHAREDCOL; break;
758                                         case 3:
759                                                 tface->mode |= TF_LIGHT; break; 
760                                         case 4:
761                                                 tface->mode |= TF_INVISIBLE; break;
762                                         case 5:
763                                                 tface->mode |= TF_DYNAMIC; break;
764                                         case 6:
765                                                 tface->mode |= TF_TWOSIDE; break;
766                                         case 7:
767                                                 tface->mode &= ~TF_TEX;
768                                                 tface->tpage= NULL;
769                                                 break;
770                                         case 8:
771                                                 tface->mode &= ~TF_SHAREDCOL; break;
772                                         case 9:
773                                                 tface->mode &= ~TF_LIGHT; break;
774                                         case 10:
775                                                 tface->mode &= ~TF_INVISIBLE; break;
776                                         case 11:
777                                                 tface->mode &= ~TF_DYNAMIC; break;
778                                         case 12:
779                                                 tface->mode &= ~TF_TWOSIDE; break;
780                                         }
781                                 }
782                         }
783                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
784                 }
785                 else if(ob->mode & OB_MODE_VERTEX_PAINT) {
786                         Mesh *me= get_mesh(ob);
787                         
788                         if(me==NULL || (me->mcol==NULL && me->mtface==NULL) ) return;
789                         
790                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
791                         if(nr==1) {
792                                 
793 // XXX                          do_shared_vertexcol(me);
794                                 
795                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
796                         }
797                 }
798                 else if(ob->mode & OB_MODE_WEIGHT_PAINT) {
799                         Object *par= modifiers_isDeformedByArmature(ob);
800
801                         if(par && (par->mode & OB_MODE_POSE)) {
802 // XXX                          nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2");
803
804 // XXX                          if(nr==1 || nr==2)
805 // XXX                                  pose_adds_vgroups(ob, (nr == 2));
806                         }
807                 }
808                 else if(ob->mode & OB_MODE_PARTICLE_EDIT) {
809 #if 0
810                         // XXX
811                         ParticleSystem *psys = PE_get_current(ob);
812                         ParticleEditSettings *pset = PE_settings();
813
814                         if(!psys)
815                                 return;
816
817                         if(pset->selectmode & SCE_SELECT_POINT)
818                                 nr= pupmenu("Specials%t|Rekey%x1|Subdivide%x2|Select First%x3|Select Last%x4|Remove Doubles%x5");
819                         else
820                                 nr= pupmenu("Specials%t|Rekey%x1|Remove Doubles%x5");
821                         
822                         switch(nr) {
823                         case 1:
824 // XXX                          if(button(&pset->totrekey, 2, 100, "Number of Keys:")==0) return;
825                                 waitcursor(1);
826                                 PE_rekey();
827                                 break;
828                         case 2:
829                                 PE_subdivide();
830                                 break;
831                         case 3:
832                                 PE_select_root();
833                                 break;
834                         case 4:
835                                 PE_select_tip();
836                                 break;
837                         case 5:
838                                 PE_remove_doubles();
839                                 break;
840                         }
841                         
842                         DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
843                         
844                         if(nr>0) waitcursor(0);
845 #endif
846                 }
847                 else {
848                         Base *base, *base_select= NULL;
849                         
850                         /* Get the active object mesh. */
851                         Mesh *me= get_mesh(ob);
852
853                         /* Booleans, if the active object is a mesh... */
854                         if (me && ob->id.lib==NULL) {
855                                 
856                                 /* Bring up a little menu with the boolean operation choices on. */
857                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
858
859                                 if (nr > 0) {
860                                         /* user has made a choice of a menu element.
861                                            All of the boolean functions require 2 mesh objects 
862                                            we search through the object list to find the other 
863                                            selected item and make sure it is distinct and a mesh. */
864
865                                         for(base= FIRSTBASE; base; base= base->next) {
866                                                 if(TESTBASELIB(v3d, base)) {
867                                                         if(base->object != ob) base_select= base;
868                                                 }
869                                         }
870
871                                         if (base_select) {
872                                                 if (get_mesh(base_select->object)) {
873                                                         if(nr <= 3){
874                                                                 waitcursor(1);
875 // XXX                                                          ret = NewBooleanMesh(BASACT,base_select,nr);
876                                                                 if (ret==0) {
877                                                                         error("An internal error occurred");
878                                                                 } else if(ret==-1) {
879                                                                         error("Selected meshes must have faces to perform boolean operations");
880                                                                 } else if (ret==-2) {
881                                                                         error("Both meshes must be a closed mesh");
882                                                                 }
883                                                                 waitcursor(0);
884                                                         } else {
885                                                                 BooleanModifierData *bmd = NULL;
886                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
887                                                                 BLI_addtail(&ob->modifiers, bmd);
888                                                                 modifier_unique_name(&ob->modifiers, (ModifierData*)bmd);
889                                                                 bmd->object = base_select->object;
890                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
891                                                                 switch(nr){
892                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
893                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
894                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
895                                                                 }
896 // XXX                                                          do_common_editbuts(B_CHANGEDEP);
897                                                         }                                                               
898                                                 } else {
899                                                         error("Please select 2 meshes");
900                                                 }
901                                         } else {
902                                                 error("Please select 2 meshes");
903                                         }
904                                 }
905
906                         }
907                         else if (ob->type == OB_FONT) {
908                                 /* removed until this gets a decent implementation (ton) */
909 /*                              nr= pupmenu("Split %t|Characters%x1");
910                                 if (nr > 0) {
911                                         switch(nr) {
912                                                 case 1: split_font();
913                                         }
914                                 }
915 */
916                         }                       
917                 }
918         }
919         else if(obedit->type==OB_MESH) {
920         }
921         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
922         }
923         else if(obedit->type==OB_ARMATURE) {
924                 nr= pupmenu("Specials%t|Subdivide %x1|Subdivide Multi%x2|Switch Direction%x7|Flip Left-Right Names%x3|%l|AutoName Left-Right%x4|AutoName Front-Back%x5|AutoName Top-Bottom%x6");
925 //              if(nr==1)
926 // XXX                  subdivide_armature(1);
927                 if(nr==2) {
928 // XXX                  if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
929                         waitcursor(1);
930 // XXX                  subdivide_armature(numcuts);
931                 }
932 //              else if(nr==3)
933 // XXX                  armature_flip_names();
934                 else if(ELEM3(nr, 4, 5, 6)) {
935 // XXX                  armature_autoside_names(nr-4);
936                 }
937 //              else if(nr == 7)
938 // XXX                  switch_direction_armature();
939         }
940         else if(obedit->type==OB_LATTICE) {
941                 Lattice *lt= obedit->data;
942                 static float weight= 1.0f;
943                 { // XXX
944 // XXX          if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
945                         Lattice *editlt= lt->editlatt->latt;
946                         int a= editlt->pntsu*editlt->pntsv*editlt->pntsw;
947                         BPoint *bp= editlt->def;
948                         
949                         while(a--) {
950                                 if(bp->f1 & SELECT)
951                                         bp->weight= weight;
952                                 bp++;
953                         }
954                 }
955         }
956
957 }
958
959 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
960 {       
961 //XXX no longer used - to be removed - replaced by game_properties_copy_exec
962         bProperty *prop;
963         Base *base;
964         int nr, tot=0;
965         char *str;
966         
967         prop= ob->prop.first;
968         while(prop) {
969                 tot++;
970                 prop= prop->next;
971         }
972         
973         str= MEM_callocN(50 + 33*tot, "copymenu prop");
974         
975         if (tot)
976                 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
977         else
978                 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
979         
980         tot= 0; 
981         prop= ob->prop.first;
982         while(prop) {
983                 tot++;
984                 strcat(str, "|");
985                 strcat(str, prop->name);
986                 prop= prop->next;
987         }
988
989         nr= pupmenu(str);
990         
991         if ( nr==1 || nr==2 ) {
992                 for(base= FIRSTBASE; base; base= base->next) {
993                         if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
994                                 if (nr==1) { /* replace */
995                                         copy_properties( &base->object->prop, &ob->prop );
996                                 } else {
997                                         for(prop = ob->prop.first; prop; prop= prop->next ) {
998                                                 set_ob_property(base->object, prop);
999                                         }
1000                                 }
1001                         }
1002                 }
1003         } else if(nr>0) {
1004                 prop = BLI_findlink(&ob->prop, nr-4); /* account for first 3 menu items & menu index starting at 1*/
1005                 
1006                 if(prop) {
1007                         for(base= FIRSTBASE; base; base= base->next) {
1008                                 if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
1009                                         set_ob_property(base->object, prop);
1010                                 }
1011                         }
1012                 }
1013         }
1014         MEM_freeN(str);
1015         
1016 }
1017
1018 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
1019 {
1020 //XXX no longer used - to be removed - replaced by logicbricks_copy_exec
1021         Base *base;
1022         
1023         for(base= FIRSTBASE; base; base= base->next) {
1024                 if(base->object != ob) {
1025                         if(TESTBASELIB(v3d, base)) {
1026                                 
1027                                 /* first: free all logic */
1028                                 free_sensors(&base->object->sensors);                           
1029                                 unlink_controllers(&base->object->controllers);
1030                                 free_controllers(&base->object->controllers);
1031                                 unlink_actuators(&base->object->actuators);
1032                                 free_actuators(&base->object->actuators);
1033                                 
1034                                 /* now copy it, this also works without logicbricks! */
1035                                 clear_sca_new_poins_ob(ob);
1036                                 copy_sensors(&base->object->sensors, &ob->sensors);
1037                                 copy_controllers(&base->object->controllers, &ob->controllers);
1038                                 copy_actuators(&base->object->actuators, &ob->actuators);
1039                                 set_sca_new_poins_ob(base->object);
1040                                 
1041                                 /* some menu settings */
1042                                 base->object->scavisflag= ob->scavisflag;
1043                                 base->object->scaflag= ob->scaflag;
1044                                 
1045                                 /* set the initial state */
1046                                 base->object->state= ob->state;
1047                                 base->object->init_state= ob->init_state;
1048                         }
1049                 }
1050         }
1051 }
1052
1053 /* both pointers should exist */
1054 static void copy_texture_space(Object *to, Object *ob)
1055 {
1056         float *poin1= NULL, *poin2= NULL;
1057         short texflag= 0;
1058         
1059         if(ob->type==OB_MESH) {
1060                 texflag= ((Mesh *)ob->data)->texflag;
1061                 poin2= ((Mesh *)ob->data)->loc;
1062         }
1063         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1064                 texflag= ((Curve *)ob->data)->texflag;
1065                 poin2= ((Curve *)ob->data)->loc;
1066         }
1067         else if(ob->type==OB_MBALL) {
1068                 texflag= ((MetaBall *)ob->data)->texflag;
1069                 poin2= ((MetaBall *)ob->data)->loc;
1070         }
1071         else
1072                 return;
1073                 
1074         if(to->type==OB_MESH) {
1075                 ((Mesh *)to->data)->texflag= texflag;
1076                 poin1= ((Mesh *)to->data)->loc;
1077         }
1078         else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
1079                 ((Curve *)to->data)->texflag= texflag;
1080                 poin1= ((Curve *)to->data)->loc;
1081         }
1082         else if(to->type==OB_MBALL) {
1083                 ((MetaBall *)to->data)->texflag= texflag;
1084                 poin1= ((MetaBall *)to->data)->loc;
1085         }
1086         else
1087                 return;
1088         
1089         memcpy(poin1, poin2, 9*sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
1090         
1091         if(to->type==OB_MESH) ;
1092         else if(to->type==OB_MBALL) tex_space_mball(to);
1093         else tex_space_curve(to->data);
1094         
1095 }
1096
1097 /* UNUSED, keep incase we want to copy functionality for use elsewhere */
1098 static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event)
1099 {
1100         Object *ob;
1101         Base *base;
1102         Curve *cu, *cu1;
1103         Nurb *nu;
1104         int do_scene_sort= 0;
1105         
1106         if(scene->id.lib) return;
1107
1108         if(!(ob=OBACT)) return;
1109         
1110         if(scene->obedit) { // XXX get from context
1111                 /* obedit_copymenu(); */
1112                 return;
1113         }
1114         if(event==9) {
1115                 copymenu_properties(scene, v3d, ob);
1116                 return;
1117         }
1118         else if(event==10) {
1119                 copymenu_logicbricks(scene, v3d, ob);
1120                 return;
1121         }
1122         else if(event==24) {
1123                 /* moved to object_link_modifiers */
1124                 /* copymenu_modifiers(bmain, scene, v3d, ob); */
1125                 return;
1126         }
1127
1128         for(base= FIRSTBASE; base; base= base->next) {
1129                 if(base != BASACT) {
1130                         if(TESTBASELIB(v3d, base)) {
1131                                 base->object->recalc |= OB_RECALC_OB;
1132                                 
1133                                 if(event==1) {  /* loc */
1134                                         copy_v3_v3(base->object->loc, ob->loc);
1135                                         copy_v3_v3(base->object->dloc, ob->dloc);
1136                                 }
1137                                 else if(event==2) {  /* rot */
1138                                         copy_v3_v3(base->object->rot, ob->rot);
1139                                         copy_v3_v3(base->object->drot, ob->drot);
1140
1141                                         copy_qt_qt(base->object->quat, ob->quat);
1142                                         copy_qt_qt(base->object->dquat, ob->dquat);
1143                                 }
1144                                 else if(event==3) {  /* size */
1145                                         copy_v3_v3(base->object->size, ob->size);
1146                                         copy_v3_v3(base->object->dsize, ob->dsize);
1147                                 }
1148                                 else if(event==4) {  /* drawtype */
1149                                         base->object->dt= ob->dt;
1150                                         base->object->dtx= ob->dtx;
1151                                         base->object->empty_drawtype= ob->empty_drawtype;
1152                                         base->object->empty_drawsize= ob->empty_drawsize;
1153                                 }
1154                                 else if(event==5) {  /* time offs */
1155                                         base->object->sf= ob->sf;
1156                                 }
1157                                 else if(event==6) {  /* dupli */
1158                                         base->object->dupon= ob->dupon;
1159                                         base->object->dupoff= ob->dupoff;
1160                                         base->object->dupsta= ob->dupsta;
1161                                         base->object->dupend= ob->dupend;
1162                                         
1163                                         base->object->transflag &= ~OB_DUPLI;
1164                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
1165
1166                                         base->object->dup_group= ob->dup_group;
1167                                         if(ob->dup_group)
1168                                                 id_lib_extern(&ob->dup_group->id);
1169                                 }
1170                                 else if(event==7) {     /* mass */
1171                                         base->object->mass= ob->mass;
1172                                 }
1173                                 else if(event==8) {     /* damping */
1174                                         base->object->damping= ob->damping;
1175                                         base->object->rdamping= ob->rdamping;
1176                                 }
1177                                 else if(event==11) {    /* all physical attributes */
1178                                         base->object->gameflag = ob->gameflag;
1179                                         base->object->inertia = ob->inertia;
1180                                         base->object->formfactor = ob->formfactor;
1181                                         base->object->damping= ob->damping;
1182                                         base->object->rdamping= ob->rdamping;
1183                                         base->object->min_vel= ob->min_vel;
1184                                         base->object->max_vel= ob->max_vel;
1185                                         if (ob->gameflag & OB_BOUNDS) {
1186                                                 base->object->collision_boundtype = ob->collision_boundtype;
1187                                         }
1188                                         base->object->margin= ob->margin;
1189                                         base->object->bsoft= copy_bulletsoftbody(ob->bsoft);
1190
1191                                 }
1192                                 else if(event==17) {    /* tex space */
1193                                         copy_texture_space(base->object, ob);
1194                                 }
1195                                 else if(event==18) {    /* font settings */
1196                                         
1197                                         if(base->object->type==ob->type) {
1198                                                 cu= ob->data;
1199                                                 cu1= base->object->data;
1200                                                 
1201                                                 cu1->spacemode= cu->spacemode;
1202                                                 cu1->spacing= cu->spacing;
1203                                                 cu1->linedist= cu->linedist;
1204                                                 cu1->shear= cu->shear;
1205                                                 cu1->fsize= cu->fsize;
1206                                                 cu1->xof= cu->xof;
1207                                                 cu1->yof= cu->yof;
1208                                                 cu1->textoncurve= cu->textoncurve;
1209                                                 cu1->wordspace= cu->wordspace;
1210                                                 cu1->ulpos= cu->ulpos;
1211                                                 cu1->ulheight= cu->ulheight;
1212                                                 if(cu1->vfont) cu1->vfont->id.us--;
1213                                                 cu1->vfont= cu->vfont;
1214                                                 id_us_plus((ID *)cu1->vfont);
1215                                                 if(cu1->vfontb) cu1->vfontb->id.us--;
1216                                                 cu1->vfontb= cu->vfontb;
1217                                                 id_us_plus((ID *)cu1->vfontb);
1218                                                 if(cu1->vfonti) cu1->vfonti->id.us--;
1219                                                 cu1->vfonti= cu->vfonti;
1220                                                 id_us_plus((ID *)cu1->vfonti);
1221                                                 if(cu1->vfontbi) cu1->vfontbi->id.us--;
1222                                                 cu1->vfontbi= cu->vfontbi;
1223                                                 id_us_plus((ID *)cu1->vfontbi);                                         
1224
1225                                                 BKE_text_to_curve(bmain, scene, base->object, 0); /* needed? */
1226
1227                                                 
1228                                                 BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
1229                                                 
1230                                                 base->object->recalc |= OB_RECALC_DATA;
1231                                         }
1232                                 }
1233                                 else if(event==19) {    /* bevel settings */
1234                                         
1235                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1236                                                 cu= ob->data;
1237                                                 cu1= base->object->data;
1238                                                 
1239                                                 cu1->bevobj= cu->bevobj;
1240                                                 cu1->taperobj= cu->taperobj;
1241                                                 cu1->width= cu->width;
1242                                                 cu1->bevresol= cu->bevresol;
1243                                                 cu1->ext1= cu->ext1;
1244                                                 cu1->ext2= cu->ext2;
1245                                                 
1246                                                 base->object->recalc |= OB_RECALC_DATA;
1247                                         }
1248                                 }
1249                                 else if(event==25) {    /* curve resolution */
1250
1251                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1252                                                 cu= ob->data;
1253                                                 cu1= base->object->data;
1254                                                 
1255                                                 cu1->resolu= cu->resolu;
1256                                                 cu1->resolu_ren= cu->resolu_ren;
1257                                                 
1258                                                 nu= cu1->nurb.first;
1259                                                 
1260                                                 while(nu) {
1261                                                         nu->resolu= cu1->resolu;
1262                                                         nu= nu->next;
1263                                                 }
1264                                                 
1265                                                 base->object->recalc |= OB_RECALC_DATA;
1266                                         }
1267                                 }
1268                                 else if(event==21){
1269                                         if (base->object->type==OB_MESH) {
1270                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
1271
1272                                                 if (md) {
1273                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
1274
1275                                                         if (!tmd) {
1276                                                                 tmd = modifier_new(eModifierType_Subsurf);
1277                                                                 BLI_addtail(&base->object->modifiers, tmd);
1278                                                         }
1279
1280                                                         modifier_copyData(md, tmd);
1281                                                         base->object->recalc |= OB_RECALC_DATA;
1282                                                 }
1283                                         }
1284                                 }
1285                                 else if(event==22) {
1286                                         /* Copy the constraint channels over */
1287                                         copy_constraints(&base->object->constraints, &ob->constraints, TRUE);
1288                                         
1289                                         do_scene_sort= 1;
1290                                 }
1291                                 else if(event==23) {
1292                                         base->object->softflag= ob->softflag;
1293                                         if(base->object->soft) sbFree(base->object->soft);
1294                                         
1295                                         base->object->soft= copy_softbody(ob->soft);
1296
1297                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
1298                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
1299                                         }
1300                                 }
1301                                 else if(event==26) {
1302 #if 0 // XXX old animation system
1303                                         copy_nlastrips(&base->object->nlastrips, &ob->nlastrips);
1304 #endif // XXX old animation system
1305                                 }
1306                                 else if(event==27) {    /* autosmooth */
1307                                         if (base->object->type==OB_MESH) {
1308                                                 Mesh *me= ob->data;
1309                                                 Mesh *cme= base->object->data;
1310                                                 cme->smoothresh= me->smoothresh;
1311                                                 if(me->flag & ME_AUTOSMOOTH)
1312                                                         cme->flag |= ME_AUTOSMOOTH;
1313                                                 else
1314                                                         cme->flag &= ~ME_AUTOSMOOTH;
1315                                         }
1316                                 }
1317                                 else if(event==28) { /* UV orco */
1318                                         if(ELEM(base->object->type, OB_CURVE, OB_SURF)) {
1319                                                 cu= ob->data;
1320                                                 cu1= base->object->data;
1321                                                 
1322                                                 if(cu->flag & CU_UV_ORCO)
1323                                                         cu1->flag |= CU_UV_ORCO;
1324                                                 else
1325                                                         cu1->flag &= ~CU_UV_ORCO;
1326                                         }               
1327                                 }
1328                                 else if(event==29) { /* protected bits */
1329                                         base->object->protectflag= ob->protectflag;
1330                                 }
1331                                 else if(event==30) { /* index object */
1332                                         base->object->index= ob->index;
1333                                 }
1334                                 else if(event==31) { /* object color */
1335                                         copy_v4_v4(base->object->col, ob->col);
1336                                 }
1337                         }
1338                 }
1339         }
1340         
1341         if(do_scene_sort)
1342                 DAG_scene_sort(bmain, scene);
1343
1344         DAG_ids_flush_update(bmain, 0);
1345 }
1346
1347 static void UNUSED_FUNCTION(copy_attr_menu)(Main *bmain, Scene *scene, View3D *v3d)
1348 {
1349         Object *ob;
1350         short event;
1351         char str[512];
1352         
1353         if(!(ob=OBACT)) return;
1354         
1355         if (scene->obedit) { // XXX get from context
1356 //              if (ob->type == OB_MESH)
1357 // XXX                  mesh_copy_menu();
1358                 return;
1359         }
1360         
1361         /* Object Mode */
1362         
1363         /* If you change this menu, don't forget to update the menu in header_view3d.c
1364          * view3d_edit_object_copyattrmenu() and in toolbox.c
1365          */
1366         
1367         strcpy(str, "Copy Attributes %t|Location%x1|Rotation%x2|Size%x3|Draw Options%x4|Time Offset%x5|Dupli%x6|Object Color%x31|%l|Mass%x7|Damping%x8|All Physical Attributes%x11|Properties%x9|Logic Bricks%x10|Protected Transform%x29|%l");
1368         
1369         strcat (str, "|Object Constraints%x22");
1370         strcat (str, "|NLA Strips%x26");
1371         
1372 // XXX  if (OB_TYPE_SUPPORT_MATERIAL(ob->type)) {
1373 //              strcat(str, "|Texture Space%x17");
1374 //      }       
1375         
1376         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1377         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28");
1378         
1379         if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1380                         strcat(str, "|Curve Resolution%x25");
1381         }
1382
1383         if(ob->type==OB_MESH){
1384                 strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27");
1385         }
1386
1387         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
1388         
1389         strcat(str, "|Pass Index%x30");
1390         
1391         if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){
1392                 strcat(str, "|Modifiers ...%x24");
1393         }
1394
1395         event= pupmenu(str);
1396         if(event<= 0) return;
1397         
1398         copy_attr(bmain, scene, v3d, event);
1399 }
1400
1401 /* ******************* force field toggle operator ***************** */
1402
1403 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1404 {
1405         Object *ob = CTX_data_active_object(C);
1406
1407         if(ob->pd == NULL)
1408                 ob->pd = object_add_collision_fields(PFIELD_FORCE);
1409
1410         if(ob->pd->forcefield == 0)
1411                 ob->pd->forcefield = PFIELD_FORCE;
1412         else
1413                 ob->pd->forcefield = 0;
1414         
1415         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, NULL);
1416
1417         return OPERATOR_FINISHED;
1418 }
1419
1420 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
1421 {
1422         
1423         /* identifiers */
1424         ot->name= "Toggle Force Field";
1425         ot->description = "Toggle object's force field";
1426         ot->idname= "OBJECT_OT_forcefield_toggle";
1427         
1428         /* api callbacks */
1429         ot->exec= forcefield_toggle_exec;
1430         ot->poll= ED_operator_object_active_editable;
1431         
1432         /* flags */
1433         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1434 }
1435
1436 /* ********************************************** */
1437 /* Motion Paths */
1438
1439 /* For the object with pose/action: update paths for those that have got them
1440  * This should selectively update paths that exist...
1441  *
1442  * To be called from various tools that do incremental updates 
1443  */
1444 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1445 {
1446         ListBase targets = {NULL, NULL};
1447         
1448         /* loop over objects in scene */
1449         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1450         {
1451                 /* set flag to force recalc, then grab the relevant bones to target */
1452                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1453                 animviz_get_object_motionpaths(ob, &targets);
1454         }
1455         CTX_DATA_END;
1456         
1457         /* recalculate paths, then free */
1458         animviz_calc_motionpaths(scene, &targets);
1459         BLI_freelistN(&targets);
1460 }
1461
1462 /* For the object with pose/action: create path curves for selected bones 
1463  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
1464  */
1465 static int object_calculate_paths_exec (bContext *C, wmOperator *UNUSED(op))
1466 {
1467         Scene *scene= CTX_data_scene(C);
1468         
1469         /* set up path data for bones being calculated */
1470         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects)  
1471         {
1472                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
1473                 animviz_verify_motionpaths(scene, ob, NULL);
1474         }
1475         CTX_DATA_END;
1476         
1477         /* calculate the bones that now have motionpaths... */
1478         // TODO: only make for the selected bones?
1479         ED_objects_recalculate_paths(C, scene);
1480         
1481         /* notifiers for updates */
1482         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1483         
1484         return OPERATOR_FINISHED; 
1485 }
1486
1487 void OBJECT_OT_paths_calculate (wmOperatorType *ot)
1488 {
1489         /* identifiers */
1490         ot->name= "Calculate Object Paths";
1491         ot->idname= "OBJECT_OT_paths_calculate";
1492         ot->description= "Calculate paths for the selected bones";
1493         
1494         /* api callbacks */
1495         ot->exec= object_calculate_paths_exec;
1496         ot->poll= ED_operator_object_active_editable;
1497         
1498         /* flags */
1499         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1500 }
1501
1502 /* --------- */
1503
1504 /* for the object with pose/action: clear path curves for selected bones only */
1505 void ED_objects_clear_paths(bContext *C)
1506 {
1507         /* loop over objects in scene */
1508         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1509         {
1510                 if (ob->mpath) {
1511                         animviz_free_motionpath(ob->mpath);
1512                         ob->mpath= NULL;
1513                         ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1514                 }
1515         }
1516         CTX_DATA_END;
1517 }
1518
1519 /* operator callback for this */
1520 static int object_clear_paths_exec (bContext *C, wmOperator *UNUSED(op))
1521 {       
1522         /* use the backend function for this */
1523         ED_objects_clear_paths(C);
1524         
1525         /* notifiers for updates */
1526         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1527         
1528         return OPERATOR_FINISHED; 
1529 }
1530
1531 void OBJECT_OT_paths_clear (wmOperatorType *ot)
1532 {
1533         /* identifiers */
1534         ot->name= "Clear Object Paths";
1535         ot->idname= "OBJECT_OT_paths_clear";
1536         ot->description= "Clear path caches for selected bones";
1537         
1538         /* api callbacks */
1539         ot->exec= object_clear_paths_exec;
1540         ot->poll= ED_operator_object_active_editable;
1541         
1542         /* flags */
1543         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1544 }
1545
1546
1547 /********************** Smooth/Flat *********************/
1548
1549 static int shade_smooth_exec(bContext *C, wmOperator *op)
1550 {
1551         Curve *cu;
1552         Nurb *nu;
1553         int clear= (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
1554         int done= 0;
1555
1556         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1557
1558                 if(ob->type==OB_MESH) {
1559                         mesh_set_smooth_flag(ob, !clear);
1560
1561                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1562                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1563
1564                         done= 1;
1565                 }
1566                 else if ELEM(ob->type, OB_SURF, OB_CURVE) {
1567                         cu= ob->data;
1568
1569                         for(nu=cu->nurb.first; nu; nu=nu->next) {
1570                                 if(!clear) nu->flag |= ME_SMOOTH;
1571                                 else nu->flag &= ~ME_SMOOTH;
1572                         }
1573
1574                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1575                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1576
1577                         done= 1;
1578                 }
1579         }
1580         CTX_DATA_END;
1581
1582         return (done)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
1583 }
1584
1585 static int shade_poll(bContext *C)
1586 {
1587         return (ED_operator_object_active_editable(C) && !ED_operator_editmesh(C));
1588 }
1589
1590 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1591 {
1592         /* identifiers */
1593         ot->name= "Shade Flat";
1594         ot->description= "Display faces 'flat'";
1595         ot->idname= "OBJECT_OT_shade_flat";
1596         
1597         /* api callbacks */
1598         ot->poll= shade_poll;
1599         ot->exec= shade_smooth_exec;
1600
1601         /* flags */
1602         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1603 }
1604
1605 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1606 {
1607         /* identifiers */
1608         ot->name= "Shade Smooth";
1609         ot->description= "Display faces 'smooth' (using vertex normals)";
1610         ot->idname= "OBJECT_OT_shade_smooth";
1611         
1612         /* api callbacks */
1613         ot->poll= shade_poll;
1614         ot->exec= shade_smooth_exec;
1615         
1616         /* flags */
1617         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1618 }
1619
1620 /* ********************** */
1621
1622 static void UNUSED_FUNCTION(image_aspect)(Scene *scene, View3D *v3d)
1623 {
1624         /* all selected objects with an image map: scale in image aspect */
1625         Base *base;
1626         Object *ob;
1627         Material *ma;
1628         Tex *tex;
1629         float x, y, space;
1630         int a, b, done;
1631         
1632         if(scene->obedit) return; // XXX get from context
1633         if(scene->id.lib) return;
1634         
1635         for(base= FIRSTBASE; base; base= base->next) {
1636                 if(TESTBASELIB(v3d, base)) {
1637                         ob= base->object;
1638                         done= 0;
1639                         
1640                         for(a=1; a<=ob->totcol; a++) {
1641                                 ma= give_current_material(ob, a);
1642                                 if(ma) {
1643                                         for(b=0; b<MAX_MTEX; b++) {
1644                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1645                                                         tex= ma->mtex[b]->tex;
1646                                                         if(tex->type==TEX_IMAGE && tex->ima) {
1647                                                                 ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, NULL);
1648                                                                 
1649                                                                 /* texturespace */
1650                                                                 space= 1.0;
1651                                                                 if(ob->type==OB_MESH) {
1652                                                                         float size[3];
1653                                                                         mesh_get_texspace(ob->data, NULL, NULL, size);
1654                                                                         space= size[0]/size[1];
1655                                                                 }
1656                                                                 else if(ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1657                                                                         Curve *cu= ob->data;
1658                                                                         space= cu->size[0]/cu->size[1];
1659                                                                 }
1660                                                         
1661                                                                 x= ibuf->x/space;
1662                                                                 y= ibuf->y;
1663                                                                 
1664                                                                 if(x>y) ob->size[0]= ob->size[1]*x/y;
1665                                                                 else ob->size[1]= ob->size[0]*y/x;
1666                                                                 
1667                                                                 done= 1;
1668                                                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);                                                               
1669                                                         }
1670                                                 }
1671                                                 if(done) break;
1672                                         }
1673                                 }
1674                                 if(done) break;
1675                         }
1676                 }
1677         }
1678         
1679 }
1680
1681 static int vergbaseco(const void *a1, const void *a2)
1682 {
1683         Base **x1, **x2;
1684         
1685         x1= (Base **) a1;
1686         x2= (Base **) a2;
1687         
1688         if( (*x1)->sy > (*x2)->sy ) return 1;
1689         else if( (*x1)->sy < (*x2)->sy) return -1;
1690         else if( (*x1)->sx > (*x2)->sx ) return 1;
1691         else if( (*x1)->sx < (*x2)->sx ) return -1;
1692
1693         return 0;
1694 }
1695
1696
1697 static void UNUSED_FUNCTION(auto_timeoffs)(Scene *scene, View3D *v3d)
1698 {
1699         Base *base, **basesort, **bs;
1700         float start, delta;
1701         int tot=0, a;
1702         short offset=25;
1703
1704         if(BASACT==NULL || v3d==NULL) return;
1705 // XXX  if(button(&offset, 0, 1000,"Total time")==0) return;
1706
1707         /* make array of all bases, xco yco (screen) */
1708         for(base= FIRSTBASE; base; base= base->next) {
1709                 if(TESTBASELIB(v3d, base)) {
1710                         tot++;
1711                 }
1712         }
1713
1714         delta= (float)offset/(float)tot;
1715         start= OBACT->sf;
1716
1717         bs= basesort= MEM_mallocN(sizeof(void *)*tot,"autotimeoffs");
1718         for(base= FIRSTBASE; base; base= base->next) {
1719                 if(TESTBASELIB(v3d, base)) {
1720                         *bs= base;
1721                         bs++;
1722                 }
1723         }
1724         qsort(basesort, tot, sizeof(void *), vergbaseco);
1725
1726         bs= basesort;
1727         for(a=0; a<tot; a++) {
1728                 
1729                 (*bs)->object->sf= start;
1730                 start+= delta;
1731
1732                 bs++;
1733         }
1734         MEM_freeN(basesort);
1735
1736 }
1737
1738 static void UNUSED_FUNCTION(ofs_timeoffs)(Scene *scene, View3D *v3d)
1739 {
1740         float offset=0.0f;
1741
1742         if(BASACT==NULL || v3d==NULL) return;
1743         
1744 // XXX  if(fbutton(&offset, -10000.0f, 10000.0f, 10, 10, "Offset")==0) return;
1745
1746         /* make array of all bases, xco yco (screen) */
1747         CTX_DATA_BEGIN(evil_C, Object*, ob, selected_editable_objects) {
1748                 ob->sf += offset;
1749                 if (ob->sf < -MAXFRAMEF)                ob->sf = -MAXFRAMEF;
1750                 else if (ob->sf > MAXFRAMEF)    ob->sf = MAXFRAMEF;
1751         }
1752         CTX_DATA_END;
1753
1754 }
1755
1756
1757 static void UNUSED_FUNCTION(rand_timeoffs)(Scene *scene, View3D *v3d)
1758 {
1759         Base *base;
1760         float rand_ofs=0.0f;
1761
1762         if(BASACT==NULL || v3d==NULL) return;
1763         
1764 // XXX  if(fbutton(&rand_ofs, 0.0f, 10000.0f, 10, 10, "Randomize")==0) return;
1765         
1766         rand_ofs *= 2;
1767         
1768         for(base= FIRSTBASE; base; base= base->next) {
1769                 if(TESTBASELIB(v3d, base)) {
1770                         base->object->sf += ((float)BLI_drand()-0.5f) * rand_ofs;
1771                         if (base->object->sf < -MAXFRAMEF)              base->object->sf = -MAXFRAMEF;
1772                         else if (base->object->sf > MAXFRAMEF)  base->object->sf = MAXFRAMEF;
1773                 }
1774         }
1775
1776 }
1777
1778 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
1779 {       
1780         EnumPropertyItem *input = object_mode_items;
1781         EnumPropertyItem *item= NULL;
1782         Object *ob;
1783         int totitem= 0;
1784         
1785         if(!C) /* needed for docs */
1786                 return object_mode_items;
1787
1788         ob = CTX_data_active_object(C);
1789         while(ob && input->identifier) {
1790                 if((input->value == OB_MODE_EDIT && ((ob->type == OB_MESH) || (ob->type == OB_ARMATURE) ||
1791                                                         (ob->type == OB_CURVE) || (ob->type == OB_SURF) ||
1792                                                          (ob->type == OB_FONT) || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) ||
1793                    (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1794                    (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
1795                    ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
1796                          input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1797                    (input->value == OB_MODE_OBJECT))
1798                         RNA_enum_item_add(&item, &totitem, input);
1799                 ++input;
1800         }
1801
1802         RNA_enum_item_end(&item, &totitem);
1803
1804         *free= 1;
1805
1806         return item;
1807 }
1808
1809 static const char *object_mode_op_string(int mode)
1810 {
1811         if(mode & OB_MODE_EDIT)
1812                 return "OBJECT_OT_editmode_toggle";
1813         if(mode == OB_MODE_SCULPT)
1814                 return "SCULPT_OT_sculptmode_toggle";
1815         if(mode == OB_MODE_VERTEX_PAINT)
1816                 return "PAINT_OT_vertex_paint_toggle";
1817         if(mode == OB_MODE_WEIGHT_PAINT)
1818                 return "PAINT_OT_weight_paint_toggle";
1819         if(mode == OB_MODE_TEXTURE_PAINT)
1820                 return "PAINT_OT_texture_paint_toggle";
1821         if(mode == OB_MODE_PARTICLE_EDIT)
1822                 return "PARTICLE_OT_particle_edit_toggle";
1823         if(mode == OB_MODE_POSE)
1824                 return "OBJECT_OT_posemode_toggle";
1825         return NULL;
1826 }
1827
1828 /* checks the mode to be set is compatible with the object
1829  * should be made into a generic function */
1830 static int object_mode_set_compat(bContext *UNUSED(C), wmOperator *op, Object *ob)
1831 {
1832         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1833
1834         if(ob) {
1835                 if(mode == OB_MODE_OBJECT)
1836                         return 1;
1837
1838                 switch(ob->type) {
1839                 case OB_MESH:
1840                         if(mode & (OB_MODE_EDIT|OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT|OB_MODE_PARTICLE_EDIT))
1841                                 return 1;
1842                         return 0;
1843                 case OB_CURVE:
1844                 case OB_SURF:
1845                 case OB_FONT:
1846                 case OB_MBALL:
1847                         if(mode & (OB_MODE_EDIT))
1848                                 return 1;
1849                         return 0;
1850                 case OB_LATTICE:
1851                         if(mode & (OB_MODE_EDIT|OB_MODE_WEIGHT_PAINT))
1852                                 return 1;
1853                 case OB_ARMATURE:
1854                         if(mode & (OB_MODE_EDIT|OB_MODE_POSE))
1855                                 return 1;
1856                 }
1857         }
1858
1859         return 0;
1860 }
1861
1862 static int object_mode_set_exec(bContext *C, wmOperator *op)
1863 {
1864         Object *ob= CTX_data_active_object(C);
1865         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1866         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1867         int toggle = RNA_boolean_get(op->ptr, "toggle");
1868
1869         if(!ob || !object_mode_set_compat(C, op, ob))
1870                 return OPERATOR_PASS_THROUGH;
1871
1872         /* Exit current mode if it's not the mode we're setting */
1873         if(ob->mode != OB_MODE_OBJECT && ob->mode != mode)
1874                 WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
1875
1876         if(mode != OB_MODE_OBJECT) {
1877                 /* Enter new mode */
1878                 if(ob->mode != mode || toggle)
1879                         WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
1880
1881                 if(toggle) {
1882                         if(ob->mode == mode)
1883                                 /* For toggling, store old mode so we know what to go back to */
1884                                 ob->restore_mode = restore_mode;
1885                         else if(ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1886                                 WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
1887                         }
1888                 }
1889         }
1890
1891         return OPERATOR_FINISHED;
1892 }
1893
1894 void OBJECT_OT_mode_set(wmOperatorType *ot)
1895 {
1896         PropertyRNA *prop;
1897
1898         /* identifiers */
1899         ot->name= "Set Object Mode";
1900         ot->description = "Sets the object interaction mode";
1901         ot->idname= "OBJECT_OT_mode_set";
1902         
1903         /* api callbacks */
1904         ot->exec= object_mode_set_exec;
1905         
1906         ot->poll= ED_operator_object_active_editable;
1907         
1908         /* flags */
1909         ot->flag= 0; /* no register/undo here, leave it to operators being called */
1910         
1911         prop= RNA_def_enum(ot->srna, "mode", object_mode_items, OB_MODE_OBJECT, "Mode", "");
1912         RNA_def_enum_funcs(prop, object_mode_set_itemsf);
1913
1914         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
1915 }
1916
1917
1918
1919 void ED_object_toggle_modes(bContext *C, int mode)
1920 {
1921         if(mode & OB_MODE_SCULPT)
1922                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1923         if(mode & OB_MODE_VERTEX_PAINT)
1924                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1925         if(mode & OB_MODE_WEIGHT_PAINT)
1926                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1927         if(mode & OB_MODE_TEXTURE_PAINT)
1928                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1929         if(mode & OB_MODE_PARTICLE_EDIT)
1930                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1931         if(mode & OB_MODE_POSE)
1932                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1933         if(mode & OB_MODE_EDIT)
1934                 WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
1935 }
1936
1937 /************************ Game Properties ***********************/
1938
1939 static int game_property_new(bContext *C, wmOperator *UNUSED(op))
1940 {
1941         Object *ob= CTX_data_active_object(C);
1942         bProperty *prop;
1943
1944         if(!ob)
1945                 return OPERATOR_CANCELLED;
1946
1947         prop= new_property(PROP_FLOAT);
1948         BLI_addtail(&ob->prop, prop);
1949         unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
1950
1951         WM_event_add_notifier(C, NC_LOGIC, NULL);
1952         return OPERATOR_FINISHED;
1953 }
1954
1955
1956 void OBJECT_OT_game_property_new(wmOperatorType *ot)
1957 {
1958         /* identifiers */
1959         ot->name= "New Game Property";
1960         ot->description= "Create a new property available to the game engine";
1961         ot->idname= "OBJECT_OT_game_property_new";
1962
1963         /* api callbacks */
1964         ot->exec= game_property_new;
1965         ot->poll= ED_operator_object_active_editable;
1966
1967         /* flags */
1968         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1969 }
1970
1971 static int game_property_remove(bContext *C, wmOperator *op)
1972 {
1973         Object *ob= CTX_data_active_object(C);
1974         bProperty *prop;
1975         int index= RNA_int_get(op->ptr, "index");
1976
1977         if(!ob)
1978                 return OPERATOR_CANCELLED;
1979
1980         prop= BLI_findlink(&ob->prop, index);
1981
1982         if(prop) {
1983                 BLI_remlink(&ob->prop, prop);
1984                 free_property(prop);
1985
1986                 WM_event_add_notifier(C, NC_LOGIC, NULL);
1987                 return OPERATOR_FINISHED;
1988         }
1989         else {
1990                 return OPERATOR_CANCELLED;
1991         }
1992 }
1993
1994 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
1995 {
1996         /* identifiers */
1997         ot->name= "Remove Game Property";
1998         ot->description= "Remove game property";
1999         ot->idname= "OBJECT_OT_game_property_remove";
2000
2001         /* api callbacks */
2002         ot->exec= game_property_remove;
2003         ot->poll= ED_operator_object_active_editable;
2004
2005         /* flags */
2006         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2007
2008         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
2009 }
2010
2011 #define COPY_PROPERTIES_REPLACE 1
2012 #define COPY_PROPERTIES_MERGE   2
2013 #define COPY_PROPERTIES_COPY    3
2014
2015 static EnumPropertyItem game_properties_copy_operations[] ={
2016         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
2017         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
2018         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
2019         {0, NULL, 0, NULL, NULL}};
2020
2021 static EnumPropertyItem gameprops_items[]= {
2022         {0, NULL, 0, NULL, NULL}};
2023
2024 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), int *free)
2025 {       
2026         Object *ob= ED_object_active_context(C);
2027         EnumPropertyItem tmp = {0, "", 0, "", ""};
2028         EnumPropertyItem *item= NULL;
2029         bProperty *prop;
2030         int a, totitem= 0;
2031         
2032         if(!ob)
2033                 return gameprops_items;
2034
2035         for(a=1, prop= ob->prop.first; prop; prop=prop->next, a++) {
2036                 tmp.value= a;
2037                 tmp.identifier= prop->name;
2038                 tmp.name= prop->name;
2039                 RNA_enum_item_add(&item, &totitem, &tmp);
2040         }
2041
2042         RNA_enum_item_end(&item, &totitem);
2043         *free= 1;
2044
2045         return item;
2046 }
2047
2048 static int game_property_copy_exec(bContext *C, wmOperator *op)
2049 {
2050         Object *ob=ED_object_active_context(C);
2051         bProperty *prop;
2052         int type = RNA_enum_get(op->ptr, "operation");
2053         int propid= RNA_enum_get(op->ptr, "property");
2054
2055         if(propid > 0) { /* copy */
2056                 prop = BLI_findlink(&ob->prop, propid-1);
2057                 
2058                 if(prop) {
2059                         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2060                                 if (ob != ob_iter) {
2061                                         if (ob->data != ob_iter->data)
2062                                                 set_ob_property(ob_iter, prop);
2063                                 }
2064                         } CTX_DATA_END;
2065                 }
2066         }
2067
2068         else {
2069                 CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2070                         if (ob != ob_iter) {
2071                                 if (ob->data != ob_iter->data){
2072                                         if (type == COPY_PROPERTIES_REPLACE)
2073                                                 copy_properties( &ob_iter->prop, &ob->prop );
2074
2075                                         /* merge - the default when calling with no argument */
2076                                         else {
2077                                                 for(prop = ob->prop.first; prop; prop= prop->next ) {
2078                                                         set_ob_property(ob_iter, prop);
2079                                                 }
2080                                         }
2081                                 }
2082                         }
2083                 }
2084                 CTX_DATA_END;
2085         }
2086
2087         return OPERATOR_FINISHED;
2088 }
2089
2090 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
2091 {
2092         PropertyRNA *prop;
2093         /* identifiers */
2094         ot->name= "Copy Game Property";
2095         ot->idname= "OBJECT_OT_game_property_copy";
2096
2097         /* api callbacks */
2098         ot->exec= game_property_copy_exec;
2099         ot->poll= ED_operator_object_active_editable;
2100
2101         /* flags */
2102         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2103
2104         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
2105         prop=RNA_def_enum(ot->srna, "property", gameprops_items, 0, "Property", "Properties to copy");
2106         RNA_def_enum_funcs(prop, gameprops_itemf);
2107         ot->prop=prop;
2108 }
2109
2110 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
2111 {
2112         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2113                 free_properties(&ob_iter->prop);
2114         }
2115         CTX_DATA_END;
2116
2117         WM_event_add_notifier(C, NC_LOGIC, NULL);
2118         return OPERATOR_FINISHED;
2119 }
2120 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
2121 {
2122         /* identifiers */
2123         ot->name= "Clear Game Property";
2124         ot->idname= "OBJECT_OT_game_property_clear";
2125
2126         /* api callbacks */
2127         ot->exec= game_property_clear_exec;
2128         ot->poll= ED_operator_object_active_editable;
2129
2130         /* flags */
2131         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2132 }
2133
2134 /************************ Copy Logic Bricks ***********************/
2135
2136 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
2137 {
2138         Object *ob=ED_object_active_context(C);
2139
2140         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2141                 if(ob != ob_iter) {
2142                         /* first: free all logic */
2143                         free_sensors(&ob_iter->sensors);                                
2144                         unlink_controllers(&ob_iter->controllers);
2145                         free_controllers(&ob_iter->controllers);
2146                         unlink_actuators(&ob_iter->actuators);
2147                         free_actuators(&ob_iter->actuators);
2148                 
2149                         /* now copy it, this also works without logicbricks! */
2150                         clear_sca_new_poins_ob(ob);
2151                         copy_sensors(&ob_iter->sensors, &ob->sensors);
2152                         copy_controllers(&ob_iter->controllers, &ob->controllers);
2153                         copy_actuators(&ob_iter->actuators, &ob->actuators);
2154                         set_sca_new_poins_ob(ob_iter);
2155                 
2156                         /* some menu settings */
2157                         ob_iter->scavisflag= ob->scavisflag;
2158                         ob_iter->scaflag= ob->scaflag;
2159                 
2160                         /* set the initial state */
2161                         ob_iter->state= ob->state;
2162                         ob_iter->init_state= ob->init_state;
2163
2164                         if(ob_iter->totcol==ob->totcol) {
2165                                 ob_iter->actcol= ob->actcol;
2166                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
2167                         }
2168                 }
2169         }
2170         CTX_DATA_END;
2171
2172         WM_event_add_notifier(C, NC_LOGIC, NULL);
2173
2174         return OPERATOR_FINISHED;
2175 }
2176
2177 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
2178 {
2179         /* identifiers */
2180         ot->name= "Copy Logic Bricks to Selected";
2181         ot->description = "Copy logic bricks to other selected objects";
2182         ot->idname= "OBJECT_OT_logic_bricks_copy";
2183
2184         /* api callbacks */
2185         ot->exec= logicbricks_copy_exec;
2186         ot->poll= ED_operator_object_active_editable;
2187
2188         /* flags */
2189         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2190 }