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