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