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