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