28566dcda07206651e8fdc52a6389fc639edc9a9
[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 static void check_editmode(int type)
596 {
597         Object *obedit= NULL; // XXX
598         
599         if (obedit==NULL || obedit->type==type) return;
600
601 // XXX  ED_object_exit_editmode(C, EM_FREEDATA|EM_FREEUNDO|EM_WAITCURSOR|EM_DO_UNDO); /* freedata, and undo */
602 }
603
604 #if 0
605 // XXX should be in view3d?
606
607 /* context: ob = lamp */
608 /* code should be replaced with proper (custom) transform handles for lamp properties */
609 static void spot_interactive(Object *ob, int mode)
610 {
611         Lamp *la= ob->data;
612         float transfac, dx, dy, ratio, origval;
613         int keep_running= 1, center2d[2];
614         short mval[2], mvalo[2];
615         
616 //      getmouseco_areawin(mval);
617 //      getmouseco_areawin(mvalo);
618         
619         project_int(ob->obmat[3], center2d);
620         if( center2d[0] > 100000 ) {            /* behind camera */
621 //              center2d[0]= curarea->winx/2;
622 //              center2d[1]= curarea->winy/2;
623         }
624
625 //      helpline(mval, center2d);
626         
627         /* ratio is like scaling */
628         dx = (float)(center2d[0] - mval[0]);
629         dy = (float)(center2d[1] - mval[1]);
630         transfac = (float)sqrt( dx*dx + dy*dy);
631         if(transfac==0.0f) transfac= 1.0f;
632         
633         if(mode==1)     
634                 origval= la->spotsize;
635         else if(mode==2)        
636                 origval= la->dist;
637         else if(mode==3)        
638                 origval= la->clipsta;
639         else    
640                 origval= la->clipend;
641         
642         while (keep_running>0) {
643                 
644 //              getmouseco_areawin(mval);
645                 
646                 /* essential for idling subloop */
647                 if(mval[0]==mvalo[0] && mval[1]==mvalo[1]) {
648                         PIL_sleep_ms(2);
649                 }
650                 else {
651                         char str[32];
652                         
653                         dx = (float)(center2d[0] - mval[0]);
654                         dy = (float)(center2d[1] - mval[1]);
655                         ratio = (float)(sqrt( dx*dx + dy*dy))/transfac;
656                         
657                         /* do the trick */
658                         
659                         if(mode==1) {   /* spot */
660                                 la->spotsize = ratio*origval;
661                                 CLAMP(la->spotsize, 1.0f, 180.0f);
662                                 sprintf(str, "Spot size %.2f\n", la->spotsize);
663                         }
664                         else if(mode==2) {      /* dist */
665                                 la->dist = ratio*origval;
666                                 CLAMP(la->dist, 0.01f, 5000.0f);
667                                 sprintf(str, "Distance %.2f\n", la->dist);
668                         }
669                         else if(mode==3) {      /* sta */
670                                 la->clipsta = ratio*origval;
671                                 CLAMP(la->clipsta, 0.001f, 5000.0f);
672                                 sprintf(str, "Distance %.2f\n", la->clipsta);
673                         }
674                         else if(mode==4) {      /* end */
675                                 la->clipend = ratio*origval;
676                                 CLAMP(la->clipend, 0.1f, 5000.0f);
677                                 sprintf(str, "Clip End %.2f\n", la->clipend);
678                         }
679
680                         /* cleanup */
681                         mvalo[0]= mval[0];
682                         mvalo[1]= mval[1];
683                         
684                         /* handle shaded mode */
685 // XXX                  shade_buttons_change_3d();
686
687                         /* DRAW */      
688                         headerprint(str);
689                         force_draw_plus(SPACE_BUTS, 0);
690
691 //                      helpline(mval, center2d);
692                 }
693                 
694                 while( qtest() ) {
695                         short val;
696                         unsigned short event= extern_qread(&val);
697                         
698                         switch (event){
699                                 case ESCKEY:
700                                 case RIGHTMOUSE:
701                                         keep_running= 0;
702                                         break;
703                                 case LEFTMOUSE:
704                                 case SPACEKEY:
705                                 case PADENTER:
706                                 case RETKEY:
707                                         if(val)
708                                                 keep_running= -1;
709                                         break;
710                         }
711                 }
712         }
713
714         if(keep_running==0) {
715                 if(mode==1)     
716                         la->spotsize= origval;
717                 else if(mode==2)        
718                         la->dist= origval;
719                 else if(mode==3)        
720                         la->clipsta= origval;
721                 else    
722                         la->clipend= origval;
723         }
724
725 }
726 #endif
727
728 static void special_editmenu(Scene *scene, View3D *v3d)
729 {
730 // XXX  static short numcuts= 2;
731         Object *ob= OBACT;
732         Object *obedit= NULL; // XXX
733         int nr,ret=0;
734         
735         if(ob==NULL) return;
736         
737         if(obedit==NULL) {
738                 
739                 if(ob->mode & OB_MODE_POSE) {
740 // XXX                  pose_special_editmenu();
741                 }
742                 else if(paint_facesel_test(ob)) {
743                         Mesh *me= get_mesh(ob);
744                         MTFace *tface;
745                         MFace *mface;
746                         int a;
747
748                         if(me==NULL || me->mtface==NULL) return;
749
750                         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");
751                         
752                         tface= me->mtface;
753                         mface= me->mface;
754                         for(a=me->totface; a>0; a--, tface++, mface++) {
755                                 if(mface->flag & ME_FACE_SEL) {
756                                         switch(nr) {
757                                         case 1:
758                                                 tface->mode |= TF_TEX; break;
759                                         case 2:
760                                                 tface->mode |= TF_SHAREDCOL; break;
761                                         case 3:
762                                                 tface->mode |= TF_LIGHT; break; 
763                                         case 4:
764                                                 tface->mode |= TF_INVISIBLE; break;
765                                         case 5:
766                                                 tface->mode |= TF_DYNAMIC; break;
767                                         case 6:
768                                                 tface->mode |= TF_TWOSIDE; break;
769                                         case 7:
770                                                 tface->mode &= ~TF_TEX;
771                                                 tface->tpage= NULL;
772                                                 break;
773                                         case 8:
774                                                 tface->mode &= ~TF_SHAREDCOL; break;
775                                         case 9:
776                                                 tface->mode &= ~TF_LIGHT; break;
777                                         case 10:
778                                                 tface->mode &= ~TF_INVISIBLE; break;
779                                         case 11:
780                                                 tface->mode &= ~TF_DYNAMIC; break;
781                                         case 12:
782                                                 tface->mode &= ~TF_TWOSIDE; break;
783                                         }
784                                 }
785                         }
786                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
787                 }
788                 else if(ob->mode & OB_MODE_VERTEX_PAINT) {
789                         Mesh *me= get_mesh(ob);
790                         
791                         if(me==NULL || (me->mcol==NULL && me->mtface==NULL) ) return;
792                         
793                         nr= pupmenu("Specials%t|Shared VertexCol%x1");
794                         if(nr==1) {
795                                 
796 // XXX                          do_shared_vertexcol(me);
797                                 
798                                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
799                         }
800                 }
801                 else if(ob->mode & OB_MODE_WEIGHT_PAINT) {
802                         Object *par= modifiers_isDeformedByArmature(ob);
803
804                         if(par && (par->mode & OB_MODE_POSE)) {
805                                 nr= pupmenu("Specials%t|Apply Bone Envelopes to Vertex Groups %x1|Apply Bone Heat Weights to Vertex Groups %x2");
806
807 // XXX                          if(nr==1 || nr==2)
808 // XXX                                  pose_adds_vgroups(ob, (nr == 2));
809                         }
810                 }
811                 else if(ob->mode & OB_MODE_PARTICLE_EDIT) {
812 #if 0
813                         // XXX
814                         ParticleSystem *psys = PE_get_current(ob);
815                         ParticleEditSettings *pset = PE_settings();
816
817                         if(!psys)
818                                 return;
819
820                         if(pset->selectmode & SCE_SELECT_POINT)
821                                 nr= pupmenu("Specials%t|Rekey%x1|Subdivide%x2|Select First%x3|Select Last%x4|Remove Doubles%x5");
822                         else
823                                 nr= pupmenu("Specials%t|Rekey%x1|Remove Doubles%x5");
824                         
825                         switch(nr) {
826                         case 1:
827 // XXX                          if(button(&pset->totrekey, 2, 100, "Number of Keys:")==0) return;
828                                 waitcursor(1);
829                                 PE_rekey();
830                                 break;
831                         case 2:
832                                 PE_subdivide();
833                                 break;
834                         case 3:
835                                 PE_select_root();
836                                 break;
837                         case 4:
838                                 PE_select_tip();
839                                 break;
840                         case 5:
841                                 PE_remove_doubles();
842                                 break;
843                         }
844                         
845                         DAG_id_tag_update(&obedit->id, OB_RECALC_DATA);
846                         
847                         if(nr>0) waitcursor(0);
848 #endif
849                 }
850                 else {
851                         Base *base, *base_select= NULL;
852                         
853                         /* Get the active object mesh. */
854                         Mesh *me= get_mesh(ob);
855
856                         /* Booleans, if the active object is a mesh... */
857                         if (me && ob->id.lib==NULL) {
858                                 
859                                 /* Bring up a little menu with the boolean operation choices on. */
860                                 nr= pupmenu("Boolean Tools%t|Intersect%x1|Union%x2|Difference%x3|Add Intersect Modifier%x4|Add Union Modifier%x5|Add Difference Modifier%x6");
861
862                                 if (nr > 0) {
863                                         /* user has made a choice of a menu element.
864                                            All of the boolean functions require 2 mesh objects 
865                                            we search through the object list to find the other 
866                                            selected item and make sure it is distinct and a mesh. */
867
868                                         for(base= FIRSTBASE; base; base= base->next) {
869                                                 if(TESTBASELIB(v3d, base)) {
870                                                         if(base->object != ob) base_select= base;
871                                                 }
872                                         }
873
874                                         if (base_select) {
875                                                 if (get_mesh(base_select->object)) {
876                                                         if(nr <= 3){
877                                                                 waitcursor(1);
878 // XXX                                                          ret = NewBooleanMesh(BASACT,base_select,nr);
879                                                                 if (ret==0) {
880                                                                         error("An internal error occurred");
881                                                                 } else if(ret==-1) {
882                                                                         error("Selected meshes must have faces to perform boolean operations");
883                                                                 } else if (ret==-2) {
884                                                                         error("Both meshes must be a closed mesh");
885                                                                 }
886                                                                 waitcursor(0);
887                                                         } else {
888                                                                 BooleanModifierData *bmd = NULL;
889                                                                 bmd = (BooleanModifierData *)modifier_new(eModifierType_Boolean);
890                                                                 BLI_addtail(&ob->modifiers, bmd);
891                                                                 modifier_unique_name(&ob->modifiers, (ModifierData*)bmd);
892                                                                 bmd->object = base_select->object;
893                                                                 bmd->modifier.mode |= eModifierMode_Realtime;
894                                                                 switch(nr){
895                                                                         case 4: bmd->operation = eBooleanModifierOp_Intersect; break;
896                                                                         case 5: bmd->operation = eBooleanModifierOp_Union; break;
897                                                                         case 6: bmd->operation = eBooleanModifierOp_Difference; break;
898                                                                 }
899 // XXX                                                          do_common_editbuts(B_CHANGEDEP);
900                                                         }                                                               
901                                                 } else {
902                                                         error("Please select 2 meshes");
903                                                 }
904                                         } else {
905                                                 error("Please select 2 meshes");
906                                         }
907                                 }
908
909                         }
910                         else if (ob->type == OB_FONT) {
911                                 /* removed until this gets a decent implementation (ton) */
912 /*                              nr= pupmenu("Split %t|Characters%x1");
913                                 if (nr > 0) {
914                                         switch(nr) {
915                                                 case 1: split_font();
916                                         }
917                                 }
918 */
919                         }                       
920                 }
921         }
922         else if(obedit->type==OB_MESH) {
923         }
924         else if(ELEM(obedit->type, OB_CURVE, OB_SURF)) {
925         }
926         else if(obedit->type==OB_ARMATURE) {
927                 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");
928 //              if(nr==1)
929 // XXX                  subdivide_armature(1);
930                 if(nr==2) {
931 // XXX                  if(button(&numcuts, 1, 128, "Number of Cuts:")==0) return;
932                         waitcursor(1);
933 // XXX                  subdivide_armature(numcuts);
934                 }
935 //              else if(nr==3)
936 // XXX                  armature_flip_names();
937                 else if(ELEM3(nr, 4, 5, 6)) {
938 // XXX                  armature_autoside_names(nr-4);
939                 }
940 //              else if(nr == 7)
941 // XXX                  switch_direction_armature();
942         }
943         else if(obedit->type==OB_LATTICE) {
944                 Lattice *lt= obedit->data;
945                 static float weight= 1.0f;
946                 { // XXX
947 // XXX          if(fbutton(&weight, 0.0f, 1.0f, 10, 10, "Set Weight")) {
948                         Lattice *editlt= lt->editlatt->latt;
949                         int a= editlt->pntsu*editlt->pntsv*editlt->pntsw;
950                         BPoint *bp= editlt->def;
951                         
952                         while(a--) {
953                                 if(bp->f1 & SELECT)
954                                         bp->weight= weight;
955                                 bp++;
956                         }
957                 }
958         }
959
960 }
961
962 static void copymenu_properties(Scene *scene, View3D *v3d, Object *ob)
963 {       
964 //XXX no longer used - to be removed - replaced by game_properties_copy_exec
965         bProperty *prop;
966         Base *base;
967         int nr, tot=0;
968         char *str;
969         
970         prop= ob->prop.first;
971         while(prop) {
972                 tot++;
973                 prop= prop->next;
974         }
975         
976         str= MEM_callocN(50 + 33*tot, "copymenu prop");
977         
978         if (tot)
979                 strcpy(str, "Copy Property %t|Replace All|Merge All|%l");
980         else
981                 strcpy(str, "Copy Property %t|Clear All (no properties on active)");
982         
983         tot= 0; 
984         prop= ob->prop.first;
985         while(prop) {
986                 tot++;
987                 strcat(str, "|");
988                 strcat(str, prop->name);
989                 prop= prop->next;
990         }
991
992         nr= pupmenu(str);
993         
994         if ( nr==1 || nr==2 ) {
995                 for(base= FIRSTBASE; base; base= base->next) {
996                         if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
997                                 if (nr==1) { /* replace */
998                                         copy_properties( &base->object->prop, &ob->prop );
999                                 } else {
1000                                         for(prop = ob->prop.first; prop; prop= prop->next ) {
1001                                                 set_ob_property(base->object, prop);
1002                                         }
1003                                 }
1004                         }
1005                 }
1006         } else if(nr>0) {
1007                 prop = BLI_findlink(&ob->prop, nr-4); /* account for first 3 menu items & menu index starting at 1*/
1008                 
1009                 if(prop) {
1010                         for(base= FIRSTBASE; base; base= base->next) {
1011                                 if((base != BASACT) &&(TESTBASELIB(v3d, base))) {
1012                                         set_ob_property(base->object, prop);
1013                                 }
1014                         }
1015                 }
1016         }
1017         MEM_freeN(str);
1018         
1019 }
1020
1021 static void copymenu_logicbricks(Scene *scene, View3D *v3d, Object *ob)
1022 {
1023 //XXX no longer used - to be removed - replaced by logicbricks_copy_exec
1024         Base *base;
1025         
1026         for(base= FIRSTBASE; base; base= base->next) {
1027                 if(base->object != ob) {
1028                         if(TESTBASELIB(v3d, base)) {
1029                                 
1030                                 /* first: free all logic */
1031                                 free_sensors(&base->object->sensors);                           
1032                                 unlink_controllers(&base->object->controllers);
1033                                 free_controllers(&base->object->controllers);
1034                                 unlink_actuators(&base->object->actuators);
1035                                 free_actuators(&base->object->actuators);
1036                                 
1037                                 /* now copy it, this also works without logicbricks! */
1038                                 clear_sca_new_poins_ob(ob);
1039                                 copy_sensors(&base->object->sensors, &ob->sensors);
1040                                 copy_controllers(&base->object->controllers, &ob->controllers);
1041                                 copy_actuators(&base->object->actuators, &ob->actuators);
1042                                 set_sca_new_poins_ob(base->object);
1043                                 
1044                                 /* some menu settings */
1045                                 base->object->scavisflag= ob->scavisflag;
1046                                 base->object->scaflag= ob->scaflag;
1047                                 
1048                                 /* set the initial state */
1049                                 base->object->state= ob->state;
1050                                 base->object->init_state= ob->init_state;
1051                         }
1052                 }
1053         }
1054 }
1055
1056 static void copymenu_modifiers(Main *bmain, Scene *scene, View3D *v3d, Object *ob)
1057 {
1058         Base *base;
1059         int i, event;
1060         char str[512];
1061         const char *errorstr= NULL;
1062
1063         strcpy(str, "Copy Modifiers %t");
1064
1065         sprintf(str+strlen(str), "|All%%x%d|%%l", NUM_MODIFIER_TYPES);
1066
1067         for (i=eModifierType_None+1; i<NUM_MODIFIER_TYPES; i++) {
1068                 ModifierTypeInfo *mti = modifierType_getInfo(i);
1069
1070                 if(ELEM3(i, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance)) continue;
1071                 
1072                 if(i == eModifierType_Collision)
1073                         continue;
1074
1075                 if (    (mti->flags&eModifierTypeFlag_AcceptsCVs) || 
1076                                 (ob->type==OB_MESH && (mti->flags&eModifierTypeFlag_AcceptsMesh))) {
1077                         sprintf(str+strlen(str), "|%s%%x%d", mti->name, i);
1078                 }
1079         }
1080
1081         event = pupmenu(str);
1082         if(event<=0) return;
1083
1084         for (base= FIRSTBASE; base; base= base->next) {
1085                 if(base->object != ob) {
1086                         if(TESTBASELIB(v3d, base)) {
1087
1088                                 base->object->recalc |= OB_RECALC_OB|OB_RECALC_DATA;
1089
1090                                 if (base->object->type==ob->type) {
1091                                         /* copy all */
1092                                         if (event==NUM_MODIFIER_TYPES) {
1093                                                 ModifierData *md;
1094                                                 object_free_modifiers(base->object);
1095
1096                                                 for (md=ob->modifiers.first; md; md=md->next) {
1097                                                         ModifierData *nmd = NULL;
1098                                                         
1099                                                         if(ELEM3(md->type, eModifierType_Hook, eModifierType_Softbody, eModifierType_ParticleInstance)) continue;
1100                 
1101                                                         if(md->type == eModifierType_Collision)
1102                                                                 continue;
1103                                                         
1104                                                         nmd = modifier_new(md->type);
1105                                                         modifier_copyData(md, nmd);
1106                                                         BLI_addtail(&base->object->modifiers, nmd);
1107                                                         modifier_unique_name(&base->object->modifiers, nmd);
1108                                                 }
1109
1110                                                 copy_object_particlesystems(base->object, ob);
1111                                                 copy_object_softbody(base->object, ob);
1112                                         } else {
1113                                                 /* copy specific types */
1114                                                 ModifierData *md, *mdn;
1115                                                 
1116                                                 /* remove all with type 'event' */
1117                                                 for (md=base->object->modifiers.first; md; md=mdn) {
1118                                                         mdn= md->next;
1119                                                         if(md->type==event) {
1120                                                                 BLI_remlink(&base->object->modifiers, md);
1121                                                                 modifier_free(md);
1122                                                         }
1123                                                 }
1124                                                 
1125                                                 /* copy all with type 'event' */
1126                                                 for (md=ob->modifiers.first; md; md=md->next) {
1127                                                         if (md->type==event) {
1128                                                                 
1129                                                                 mdn = modifier_new(event);
1130                                                                 BLI_addtail(&base->object->modifiers, mdn);
1131                                                                 modifier_unique_name(&base->object->modifiers, mdn);
1132
1133                                                                 modifier_copyData(md, mdn);
1134                                                         }
1135                                                 }
1136
1137                                                 if(event == eModifierType_ParticleSystem) {
1138                                                         object_free_particlesystems(base->object);
1139                                                         copy_object_particlesystems(base->object, ob);
1140                                                 }
1141                                                 else if(event == eModifierType_Softbody) {
1142                                                         object_free_softbody(base->object);
1143                                                         copy_object_softbody(base->object, ob);
1144                                                 }
1145                                         }
1146                                 }
1147                                 else
1148                                         errorstr= "Did not copy modifiers to other Object types";
1149                         }
1150                 }
1151         }
1152         
1153 //      if(errorstr) notice(errorstr);
1154         
1155         DAG_scene_sort(bmain, scene);
1156         
1157 }
1158
1159 /* both pointers should exist */
1160 static void copy_texture_space(Object *to, Object *ob)
1161 {
1162         float *poin1= NULL, *poin2= NULL;
1163         short texflag= 0;
1164         
1165         if(ob->type==OB_MESH) {
1166                 texflag= ((Mesh *)ob->data)->texflag;
1167                 poin2= ((Mesh *)ob->data)->loc;
1168         }
1169         else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
1170                 texflag= ((Curve *)ob->data)->texflag;
1171                 poin2= ((Curve *)ob->data)->loc;
1172         }
1173         else if(ob->type==OB_MBALL) {
1174                 texflag= ((MetaBall *)ob->data)->texflag;
1175                 poin2= ((MetaBall *)ob->data)->loc;
1176         }
1177         else
1178                 return;
1179                 
1180         if(to->type==OB_MESH) {
1181                 ((Mesh *)to->data)->texflag= texflag;
1182                 poin1= ((Mesh *)to->data)->loc;
1183         }
1184         else if (ELEM3(to->type, OB_CURVE, OB_SURF, OB_FONT)) {
1185                 ((Curve *)to->data)->texflag= texflag;
1186                 poin1= ((Curve *)to->data)->loc;
1187         }
1188         else if(to->type==OB_MBALL) {
1189                 ((MetaBall *)to->data)->texflag= texflag;
1190                 poin1= ((MetaBall *)to->data)->loc;
1191         }
1192         else
1193                 return;
1194         
1195         memcpy(poin1, poin2, 9*sizeof(float));  /* this was noted in DNA_mesh, curve, mball */
1196         
1197         if(to->type==OB_MESH) ;
1198         else if(to->type==OB_MBALL) tex_space_mball(to);
1199         else tex_space_curve(to->data);
1200         
1201 }
1202
1203 static void copy_attr(Main *bmain, Scene *scene, View3D *v3d, short event)
1204 {
1205         Object *ob;
1206         Base *base;
1207         Curve *cu, *cu1;
1208         Nurb *nu;
1209         int do_scene_sort= 0;
1210         
1211         if(scene->id.lib) return;
1212
1213         if(!(ob=OBACT)) return;
1214         
1215         if(scene->obedit) { // XXX get from context
1216                 /* obedit_copymenu(); */
1217                 return;
1218         }
1219         if(event==9) {
1220                 copymenu_properties(scene, v3d, ob);
1221                 return;
1222         }
1223         else if(event==10) {
1224                 copymenu_logicbricks(scene, v3d, ob);
1225                 return;
1226         }
1227         else if(event==24) {
1228                 copymenu_modifiers(bmain, scene, v3d, ob);
1229                 return;
1230         }
1231
1232         for(base= FIRSTBASE; base; base= base->next) {
1233                 if(base != BASACT) {
1234                         if(TESTBASELIB(v3d, base)) {
1235                                 base->object->recalc |= OB_RECALC_OB;
1236                                 
1237                                 if(event==1) {  /* loc */
1238                                         VECCOPY(base->object->loc, ob->loc);
1239                                         VECCOPY(base->object->dloc, ob->dloc);
1240                                 }
1241                                 else if(event==2) {  /* rot */
1242                                         VECCOPY(base->object->rot, ob->rot);
1243                                         VECCOPY(base->object->drot, ob->drot);
1244
1245                                         QUATCOPY(base->object->quat, ob->quat);
1246                                         QUATCOPY(base->object->dquat, ob->dquat);
1247                                 }
1248                                 else if(event==3) {  /* size */
1249                                         VECCOPY(base->object->size, ob->size);
1250                                         VECCOPY(base->object->dsize, ob->dsize);
1251                                 }
1252                                 else if(event==4) {  /* drawtype */
1253                                         base->object->dt= ob->dt;
1254                                         base->object->dtx= ob->dtx;
1255                                         base->object->empty_drawtype= ob->empty_drawtype;
1256                                         base->object->empty_drawsize= ob->empty_drawsize;
1257                                 }
1258                                 else if(event==5) {  /* time offs */
1259                                         base->object->sf= ob->sf;
1260                                 }
1261                                 else if(event==6) {  /* dupli */
1262                                         base->object->dupon= ob->dupon;
1263                                         base->object->dupoff= ob->dupoff;
1264                                         base->object->dupsta= ob->dupsta;
1265                                         base->object->dupend= ob->dupend;
1266                                         
1267                                         base->object->transflag &= ~OB_DUPLI;
1268                                         base->object->transflag |= (ob->transflag & OB_DUPLI);
1269
1270                                         base->object->dup_group= ob->dup_group;
1271                                         if(ob->dup_group)
1272                                                 id_lib_extern(&ob->dup_group->id);
1273                                 }
1274                                 else if(event==7) {     /* mass */
1275                                         base->object->mass= ob->mass;
1276                                 }
1277                                 else if(event==8) {     /* damping */
1278                                         base->object->damping= ob->damping;
1279                                         base->object->rdamping= ob->rdamping;
1280                                 }
1281                                 else if(event==11) {    /* all physical attributes */
1282                                         base->object->gameflag = ob->gameflag;
1283                                         base->object->inertia = ob->inertia;
1284                                         base->object->formfactor = ob->formfactor;
1285                                         base->object->damping= ob->damping;
1286                                         base->object->rdamping= ob->rdamping;
1287                                         base->object->min_vel= ob->min_vel;
1288                                         base->object->max_vel= ob->max_vel;
1289                                         if (ob->gameflag & OB_BOUNDS) {
1290                                                 base->object->boundtype = ob->boundtype;
1291                                         }
1292                                         base->object->margin= ob->margin;
1293                                         base->object->bsoft= copy_bulletsoftbody(ob->bsoft);
1294
1295                                 }
1296                                 else if(event==17) {    /* tex space */
1297                                         copy_texture_space(base->object, ob);
1298                                 }
1299                                 else if(event==18) {    /* font settings */
1300                                         
1301                                         if(base->object->type==ob->type) {
1302                                                 cu= ob->data;
1303                                                 cu1= base->object->data;
1304                                                 
1305                                                 cu1->spacemode= cu->spacemode;
1306                                                 cu1->spacing= cu->spacing;
1307                                                 cu1->linedist= cu->linedist;
1308                                                 cu1->shear= cu->shear;
1309                                                 cu1->fsize= cu->fsize;
1310                                                 cu1->xof= cu->xof;
1311                                                 cu1->yof= cu->yof;
1312                                                 cu1->textoncurve= cu->textoncurve;
1313                                                 cu1->wordspace= cu->wordspace;
1314                                                 cu1->ulpos= cu->ulpos;
1315                                                 cu1->ulheight= cu->ulheight;
1316                                                 if(cu1->vfont) cu1->vfont->id.us--;
1317                                                 cu1->vfont= cu->vfont;
1318                                                 id_us_plus((ID *)cu1->vfont);
1319                                                 if(cu1->vfontb) cu1->vfontb->id.us--;
1320                                                 cu1->vfontb= cu->vfontb;
1321                                                 id_us_plus((ID *)cu1->vfontb);
1322                                                 if(cu1->vfonti) cu1->vfonti->id.us--;
1323                                                 cu1->vfonti= cu->vfonti;
1324                                                 id_us_plus((ID *)cu1->vfonti);
1325                                                 if(cu1->vfontbi) cu1->vfontbi->id.us--;
1326                                                 cu1->vfontbi= cu->vfontbi;
1327                                                 id_us_plus((ID *)cu1->vfontbi);                                         
1328
1329                                                 BKE_text_to_curve(scene, base->object, 0);              /* needed? */
1330
1331                                                 
1332                                                 BLI_strncpy(cu1->family, cu->family, sizeof(cu1->family));
1333                                                 
1334                                                 base->object->recalc |= OB_RECALC_DATA;
1335                                         }
1336                                 }
1337                                 else if(event==19) {    /* bevel settings */
1338                                         
1339                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1340                                                 cu= ob->data;
1341                                                 cu1= base->object->data;
1342                                                 
1343                                                 cu1->bevobj= cu->bevobj;
1344                                                 cu1->taperobj= cu->taperobj;
1345                                                 cu1->width= cu->width;
1346                                                 cu1->bevresol= cu->bevresol;
1347                                                 cu1->ext1= cu->ext1;
1348                                                 cu1->ext2= cu->ext2;
1349                                                 
1350                                                 base->object->recalc |= OB_RECALC_DATA;
1351                                         }
1352                                 }
1353                                 else if(event==25) {    /* curve resolution */
1354
1355                                         if(ELEM(base->object->type, OB_CURVE, OB_FONT)) {
1356                                                 cu= ob->data;
1357                                                 cu1= base->object->data;
1358                                                 
1359                                                 cu1->resolu= cu->resolu;
1360                                                 cu1->resolu_ren= cu->resolu_ren;
1361                                                 
1362                                                 nu= cu1->nurb.first;
1363                                                 
1364                                                 while(nu) {
1365                                                         nu->resolu= cu1->resolu;
1366                                                         nu= nu->next;
1367                                                 }
1368                                                 
1369                                                 base->object->recalc |= OB_RECALC_DATA;
1370                                         }
1371                                 }
1372                                 else if(event==21){
1373                                         if (base->object->type==OB_MESH) {
1374                                                 ModifierData *md = modifiers_findByType(ob, eModifierType_Subsurf);
1375
1376                                                 if (md) {
1377                                                         ModifierData *tmd = modifiers_findByType(base->object, eModifierType_Subsurf);
1378
1379                                                         if (!tmd) {
1380                                                                 tmd = modifier_new(eModifierType_Subsurf);
1381                                                                 BLI_addtail(&base->object->modifiers, tmd);
1382                                                         }
1383
1384                                                         modifier_copyData(md, tmd);
1385                                                         base->object->recalc |= OB_RECALC_DATA;
1386                                                 }
1387                                         }
1388                                 }
1389                                 else if(event==22) {
1390                                         /* Copy the constraint channels over */
1391                                         copy_constraints(&base->object->constraints, &ob->constraints, TRUE);
1392                                         
1393                                         do_scene_sort= 1;
1394                                 }
1395                                 else if(event==23) {
1396                                         base->object->softflag= ob->softflag;
1397                                         if(base->object->soft) sbFree(base->object->soft);
1398                                         
1399                                         base->object->soft= copy_softbody(ob->soft);
1400
1401                                         if (!modifiers_findByType(base->object, eModifierType_Softbody)) {
1402                                                 BLI_addhead(&base->object->modifiers, modifier_new(eModifierType_Softbody));
1403                                         }
1404                                 }
1405                                 else if(event==26) {
1406 #if 0 // XXX old animation system
1407                                         copy_nlastrips(&base->object->nlastrips, &ob->nlastrips);
1408 #endif // XXX old animation system
1409                                 }
1410                                 else if(event==27) {    /* autosmooth */
1411                                         if (base->object->type==OB_MESH) {
1412                                                 Mesh *me= ob->data;
1413                                                 Mesh *cme= base->object->data;
1414                                                 cme->smoothresh= me->smoothresh;
1415                                                 if(me->flag & ME_AUTOSMOOTH)
1416                                                         cme->flag |= ME_AUTOSMOOTH;
1417                                                 else
1418                                                         cme->flag &= ~ME_AUTOSMOOTH;
1419                                         }
1420                                 }
1421                                 else if(event==28) { /* UV orco */
1422                                         if(ELEM(base->object->type, OB_CURVE, OB_SURF)) {
1423                                                 cu= ob->data;
1424                                                 cu1= base->object->data;
1425                                                 
1426                                                 if(cu->flag & CU_UV_ORCO)
1427                                                         cu1->flag |= CU_UV_ORCO;
1428                                                 else
1429                                                         cu1->flag &= ~CU_UV_ORCO;
1430                                         }               
1431                                 }
1432                                 else if(event==29) { /* protected bits */
1433                                         base->object->protectflag= ob->protectflag;
1434                                 }
1435                                 else if(event==30) { /* index object */
1436                                         base->object->index= ob->index;
1437                                 }
1438                                 else if(event==31) { /* object color */
1439                                         QUATCOPY(base->object->col, ob->col);
1440                                 }
1441                         }
1442                 }
1443         }
1444         
1445         if(do_scene_sort)
1446                 DAG_scene_sort(bmain, scene);
1447
1448         DAG_ids_flush_update(bmain, 0);
1449 }
1450
1451 static void copy_attr_menu(Main *bmain, Scene *scene, View3D *v3d)
1452 {
1453         Object *ob;
1454         short event;
1455         char str[512];
1456         
1457         if(!(ob=OBACT)) return;
1458         
1459         if (scene->obedit) { // XXX get from context
1460 //              if (ob->type == OB_MESH)
1461 // XXX                  mesh_copy_menu();
1462                 return;
1463         }
1464         
1465         /* Object Mode */
1466         
1467         /* If you change this menu, don't forget to update the menu in header_view3d.c
1468          * view3d_edit_object_copyattrmenu() and in toolbox.c
1469          */
1470         
1471         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");
1472         
1473         strcat (str, "|Object Constraints%x22");
1474         strcat (str, "|NLA Strips%x26");
1475         
1476 // XXX  if (OB_SUPPORT_MATERIAL(ob)) {
1477 //              strcat(str, "|Texture Space%x17");
1478 //      }       
1479         
1480         if(ob->type == OB_FONT) strcat(str, "|Font Settings%x18|Bevel Settings%x19");
1481         if(ob->type == OB_CURVE) strcat(str, "|Bevel Settings%x19|UV Orco%x28");
1482         
1483         if((ob->type == OB_FONT) || (ob->type == OB_CURVE)) {
1484                         strcat(str, "|Curve Resolution%x25");
1485         }
1486
1487         if(ob->type==OB_MESH){
1488                 strcat(str, "|Subsurf Settings%x21|AutoSmooth%x27");
1489         }
1490
1491         if(ob->soft) strcat(str, "|Soft Body Settings%x23");
1492         
1493         strcat(str, "|Pass Index%x30");
1494         
1495         if(ob->type==OB_MESH || ob->type==OB_CURVE || ob->type==OB_LATTICE || ob->type==OB_SURF){
1496                 strcat(str, "|Modifiers ...%x24");
1497         }
1498
1499         event= pupmenu(str);
1500         if(event<= 0) return;
1501         
1502         copy_attr(bmain, scene, v3d, event);
1503 }
1504
1505 /* ******************* force field toggle operator ***************** */
1506
1507 static int forcefield_toggle_exec(bContext *C, wmOperator *UNUSED(op))
1508 {
1509         Object *ob = CTX_data_active_object(C);
1510
1511         if(ob->pd == NULL)
1512                 ob->pd = object_add_collision_fields(PFIELD_FORCE);
1513
1514         if(ob->pd->forcefield == 0)
1515                 ob->pd->forcefield = PFIELD_FORCE;
1516         else
1517                 ob->pd->forcefield = 0;
1518         
1519         return OPERATOR_FINISHED;
1520 }
1521
1522 void OBJECT_OT_forcefield_toggle(wmOperatorType *ot)
1523 {
1524         
1525         /* identifiers */
1526         ot->name= "Toggle Force Field";
1527         ot->description = "Toggle object's force field";
1528         ot->idname= "OBJECT_OT_forcefield_toggle";
1529         
1530         /* api callbacks */
1531         ot->exec= forcefield_toggle_exec;
1532         ot->poll= ED_operator_object_active_editable;
1533         
1534         /* flags */
1535         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1536 }
1537
1538 /* ********************************************** */
1539 /* Motion Paths */
1540
1541 /* For the object with pose/action: update paths for those that have got them
1542  * This should selectively update paths that exist...
1543  *
1544  * To be called from various tools that do incremental updates 
1545  */
1546 void ED_objects_recalculate_paths(bContext *C, Scene *scene)
1547 {
1548         ListBase targets = {NULL, NULL};
1549         
1550         /* loop over objects in scene */
1551         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1552         {
1553                 /* set flag to force recalc, then grab the relevant bones to target */
1554                 ob->avs.recalc |= ANIMVIZ_RECALC_PATHS;
1555                 animviz_get_object_motionpaths(ob, &targets);
1556         }
1557         CTX_DATA_END;
1558         
1559         /* recalculate paths, then free */
1560         animviz_calc_motionpaths(scene, &targets);
1561         BLI_freelistN(&targets);
1562 }
1563
1564 /* For the object with pose/action: create path curves for selected bones 
1565  * This recalculates the WHOLE path within the pchan->pathsf and pchan->pathef range
1566  */
1567 static int object_calculate_paths_exec (bContext *C, wmOperator *UNUSED(op))
1568 {
1569         Scene *scene= CTX_data_scene(C);
1570         
1571         /* set up path data for bones being calculated */
1572         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects)  
1573         {
1574                 /* verify makes sure that the selected bone has a bone with the appropriate settings */
1575                 animviz_verify_motionpaths(scene, ob, NULL);
1576         }
1577         CTX_DATA_END;
1578         
1579         /* calculate the bones that now have motionpaths... */
1580         // TODO: only make for the selected bones?
1581         ED_objects_recalculate_paths(C, scene);
1582         
1583         /* notifiers for updates */
1584         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1585         
1586         return OPERATOR_FINISHED; 
1587 }
1588
1589 void OBJECT_OT_paths_calculate (wmOperatorType *ot)
1590 {
1591         /* identifiers */
1592         ot->name= "Calculate Object Paths";
1593         ot->idname= "OBJECT_OT_paths_calculate";
1594         ot->description= "Calculate paths for the selected bones";
1595         
1596         /* api callbacks */
1597         ot->exec= object_calculate_paths_exec;
1598         ot->poll= ED_operator_object_active_editable;
1599         
1600         /* flags */
1601         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1602 }
1603
1604 /* --------- */
1605
1606 /* for the object with pose/action: clear path curves for selected bones only */
1607 void ED_objects_clear_paths(bContext *C)
1608 {
1609         /* loop over objects in scene */
1610         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) 
1611         {
1612                 if (ob->mpath) {
1613                         animviz_free_motionpath(ob->mpath);
1614                         ob->mpath= NULL;
1615                         ob->avs.path_bakeflag &= ~MOTIONPATH_BAKE_HAS_PATHS;
1616                 }
1617         }
1618         CTX_DATA_END;
1619 }
1620
1621 /* operator callback for this */
1622 static int object_clear_paths_exec (bContext *C, wmOperator *UNUSED(op))
1623 {       
1624         /* use the backend function for this */
1625         ED_objects_clear_paths(C);
1626         
1627         /* notifiers for updates */
1628         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
1629         
1630         return OPERATOR_FINISHED; 
1631 }
1632
1633 void OBJECT_OT_paths_clear (wmOperatorType *ot)
1634 {
1635         /* identifiers */
1636         ot->name= "Clear Object Paths";
1637         ot->idname= "OBJECT_OT_paths_clear";
1638         ot->description= "Clear path caches for selected bones";
1639         
1640         /* api callbacks */
1641         ot->exec= object_clear_paths_exec;
1642         ot->poll= ED_operator_object_active_editable;
1643         
1644         /* flags */
1645         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
1646 }
1647
1648
1649 /********************** Smooth/Flat *********************/
1650
1651 static int shade_smooth_exec(bContext *C, wmOperator *op)
1652 {
1653         Curve *cu;
1654         Nurb *nu;
1655         int clear= (strcmp(op->idname, "OBJECT_OT_shade_flat") == 0);
1656         int done= 0;
1657
1658         CTX_DATA_BEGIN(C, Object*, ob, selected_editable_objects) {
1659
1660                 if(ob->type==OB_MESH) {
1661                         mesh_set_smooth_flag(ob, !clear);
1662
1663                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1664                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1665
1666                         done= 1;
1667                 }
1668                 else if ELEM(ob->type, OB_SURF, OB_CURVE) {
1669                         cu= ob->data;
1670
1671                         for(nu=cu->nurb.first; nu; nu=nu->next) {
1672                                 if(!clear) nu->flag |= ME_SMOOTH;
1673                                 else nu->flag &= ~ME_SMOOTH;
1674                         }
1675
1676                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1677                         WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
1678
1679                         done= 1;
1680                 }
1681         }
1682         CTX_DATA_END;
1683
1684         return (done)? OPERATOR_FINISHED: OPERATOR_CANCELLED;
1685 }
1686
1687 static int shade_poll(bContext *C)
1688 {
1689         return (ED_operator_object_active_editable(C) && !ED_operator_editmesh(C));
1690 }
1691
1692 void OBJECT_OT_shade_flat(wmOperatorType *ot)
1693 {
1694         /* identifiers */
1695         ot->name= "Shade Flat";
1696         ot->idname= "OBJECT_OT_shade_flat";
1697         
1698         /* api callbacks */
1699         ot->poll= shade_poll;
1700         ot->exec= shade_smooth_exec;
1701
1702         /* flags */
1703         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1704 }
1705
1706 void OBJECT_OT_shade_smooth(wmOperatorType *ot)
1707 {
1708         /* identifiers */
1709         ot->name= "Shade Smooth";
1710         ot->idname= "OBJECT_OT_shade_smooth";
1711         
1712         /* api callbacks */
1713         ot->poll= shade_poll;
1714         ot->exec= shade_smooth_exec;
1715         
1716         /* flags */
1717         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1718 }
1719
1720 /* ********************** */
1721
1722 static void image_aspect(Scene *scene, View3D *v3d)
1723 {
1724         /* all selected objects with an image map: scale in image aspect */
1725         Base *base;
1726         Object *ob;
1727         Material *ma;
1728         Tex *tex;
1729         float x, y, space;
1730         int a, b, done;
1731         
1732         if(scene->obedit) return; // XXX get from context
1733         if(scene->id.lib) return;
1734         
1735         for(base= FIRSTBASE; base; base= base->next) {
1736                 if(TESTBASELIB(v3d, base)) {
1737                         ob= base->object;
1738                         done= 0;
1739                         
1740                         for(a=1; a<=ob->totcol; a++) {
1741                                 ma= give_current_material(ob, a);
1742                                 if(ma) {
1743                                         for(b=0; b<MAX_MTEX; b++) {
1744                                                 if(ma->mtex[b] && ma->mtex[b]->tex) {
1745                                                         tex= ma->mtex[b]->tex;
1746                                                         if(tex->type==TEX_IMAGE && tex->ima) {
1747                                                                 ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, NULL);
1748                                                                 
1749                                                                 /* texturespace */
1750                                                                 space= 1.0;
1751                                                                 if(ob->type==OB_MESH) {
1752                                                                         float size[3];
1753                                                                         mesh_get_texspace(ob->data, NULL, NULL, size);
1754                                                                         space= size[0]/size[1];
1755                                                                 }
1756                                                                 else if(ELEM3(ob->type, OB_CURVE, OB_FONT, OB_SURF)) {
1757                                                                         Curve *cu= ob->data;
1758                                                                         space= cu->size[0]/cu->size[1];
1759                                                                 }
1760                                                         
1761                                                                 x= ibuf->x/space;
1762                                                                 y= ibuf->y;
1763                                                                 
1764                                                                 if(x>y) ob->size[0]= ob->size[1]*x/y;
1765                                                                 else ob->size[1]= ob->size[0]*y/x;
1766                                                                 
1767                                                                 done= 1;
1768                                                                 DAG_id_tag_update(&ob->id, OB_RECALC_OB);                                                               
1769                                                         }
1770                                                 }
1771                                                 if(done) break;
1772                                         }
1773                                 }
1774                                 if(done) break;
1775                         }
1776                 }
1777         }
1778         
1779 }
1780
1781 static int vergbaseco(const void *a1, const void *a2)
1782 {
1783         Base **x1, **x2;
1784         
1785         x1= (Base **) a1;
1786         x2= (Base **) a2;
1787         
1788         if( (*x1)->sy > (*x2)->sy ) return 1;
1789         else if( (*x1)->sy < (*x2)->sy) return -1;
1790         else if( (*x1)->sx > (*x2)->sx ) return 1;
1791         else if( (*x1)->sx < (*x2)->sx ) return -1;
1792
1793         return 0;
1794 }
1795
1796
1797 static void auto_timeoffs(Scene *scene, View3D *v3d)
1798 {
1799         Base *base, **basesort, **bs;
1800         float start, delta;
1801         int tot=0, a;
1802         short offset=25;
1803
1804         if(BASACT==0 || v3d==NULL) return;
1805 // XXX  if(button(&offset, 0, 1000,"Total time")==0) return;
1806
1807         /* make array of all bases, xco yco (screen) */
1808         for(base= FIRSTBASE; base; base= base->next) {
1809                 if(TESTBASELIB(v3d, base)) {
1810                         tot++;
1811                 }
1812         }
1813
1814         delta= (float)offset/(float)tot;
1815         start= OBACT->sf;
1816
1817         bs= basesort= MEM_mallocN(sizeof(void *)*tot,"autotimeoffs");
1818         for(base= FIRSTBASE; base; base= base->next) {
1819                 if(TESTBASELIB(v3d, base)) {
1820                         *bs= base;
1821                         bs++;
1822                 }
1823         }
1824         qsort(basesort, tot, sizeof(void *), vergbaseco);
1825
1826         bs= basesort;
1827         for(a=0; a<tot; a++) {
1828                 
1829                 (*bs)->object->sf= start;
1830                 start+= delta;
1831
1832                 bs++;
1833         }
1834         MEM_freeN(basesort);
1835
1836 }
1837
1838 static void ofs_timeoffs(Scene *scene, View3D *v3d)
1839 {
1840         float offset=0.0f;
1841
1842         if(BASACT==0 || v3d==NULL) return;
1843         
1844 // XXX  if(fbutton(&offset, -10000.0f, 10000.0f, 10, 10, "Offset")==0) return;
1845
1846         /* make array of all bases, xco yco (screen) */
1847         CTX_DATA_BEGIN(evil_C, Object*, ob, selected_editable_objects) {
1848                 ob->sf += offset;
1849                 if (ob->sf < -MAXFRAMEF)                ob->sf = -MAXFRAMEF;
1850                 else if (ob->sf > MAXFRAMEF)    ob->sf = MAXFRAMEF;
1851         }
1852         CTX_DATA_END;
1853
1854 }
1855
1856
1857 static void rand_timeoffs(Scene *scene, View3D *v3d)
1858 {
1859         Base *base;
1860         float rand_ofs=0.0f;
1861
1862         if(BASACT==0 || v3d==NULL) return;
1863         
1864 // XXX  if(fbutton(&rand_ofs, 0.0f, 10000.0f, 10, 10, "Randomize")==0) return;
1865         
1866         rand_ofs *= 2;
1867         
1868         for(base= FIRSTBASE; base; base= base->next) {
1869                 if(TESTBASELIB(v3d, base)) {
1870                         base->object->sf += (BLI_drand()-0.5) * rand_ofs;
1871                         if (base->object->sf < -MAXFRAMEF)              base->object->sf = -MAXFRAMEF;
1872                         else if (base->object->sf > MAXFRAMEF)  base->object->sf = MAXFRAMEF;
1873                 }
1874         }
1875
1876 }
1877
1878 static EnumPropertyItem *object_mode_set_itemsf(bContext *C, PointerRNA *UNUSED(ptr), int *free)
1879 {       
1880         EnumPropertyItem *input = object_mode_items;
1881         EnumPropertyItem *item= NULL;
1882         Object *ob;
1883         int totitem= 0;
1884         
1885         if(!C) /* needed for docs */
1886                 return object_mode_items;
1887
1888         ob = CTX_data_active_object(C);
1889         while(ob && input->identifier) {
1890                 if((input->value == OB_MODE_EDIT && ((ob->type == OB_MESH) || (ob->type == OB_ARMATURE) ||
1891                                                         (ob->type == OB_CURVE) || (ob->type == OB_SURF) ||
1892                                                          (ob->type == OB_FONT) || (ob->type == OB_MBALL) || (ob->type == OB_LATTICE))) ||
1893                    (input->value == OB_MODE_POSE && (ob->type == OB_ARMATURE)) ||
1894                    (input->value == OB_MODE_PARTICLE_EDIT && ob->particlesystem.first) ||
1895                    ((input->value == OB_MODE_SCULPT || input->value == OB_MODE_VERTEX_PAINT ||
1896                          input->value == OB_MODE_WEIGHT_PAINT || input->value == OB_MODE_TEXTURE_PAINT) && (ob->type == OB_MESH)) ||
1897                    (input->value == OB_MODE_OBJECT))
1898                         RNA_enum_item_add(&item, &totitem, input);
1899                 ++input;
1900         }
1901
1902         RNA_enum_item_end(&item, &totitem);
1903
1904         *free= 1;
1905
1906         return item;
1907 }
1908
1909 static const char *object_mode_op_string(int mode)
1910 {
1911         if(mode & OB_MODE_EDIT)
1912                 return "OBJECT_OT_editmode_toggle";
1913         if(mode == OB_MODE_SCULPT)
1914                 return "SCULPT_OT_sculptmode_toggle";
1915         if(mode == OB_MODE_VERTEX_PAINT)
1916                 return "PAINT_OT_vertex_paint_toggle";
1917         if(mode == OB_MODE_WEIGHT_PAINT)
1918                 return "PAINT_OT_weight_paint_toggle";
1919         if(mode == OB_MODE_TEXTURE_PAINT)
1920                 return "PAINT_OT_texture_paint_toggle";
1921         if(mode == OB_MODE_PARTICLE_EDIT)
1922                 return "PARTICLE_OT_particle_edit_toggle";
1923         if(mode == OB_MODE_POSE)
1924                 return "OBJECT_OT_posemode_toggle";
1925         return NULL;
1926 }
1927
1928 /* checks the mode to be set is compatible with the object
1929  * should be made into a generic function */
1930 static int object_mode_set_compat(bContext *UNUSED(C), wmOperator *op, Object *ob)
1931 {
1932         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1933
1934         if(ob) {
1935                 if(mode == OB_MODE_OBJECT)
1936                         return 1;
1937
1938                 switch(ob->type) {
1939                 case OB_MESH:
1940                         if(mode & (OB_MODE_EDIT|OB_MODE_SCULPT|OB_MODE_VERTEX_PAINT|OB_MODE_WEIGHT_PAINT|OB_MODE_TEXTURE_PAINT|OB_MODE_PARTICLE_EDIT))
1941                                 return 1;
1942                         return 0;
1943                 case OB_CURVE:
1944                 case OB_SURF:
1945                 case OB_FONT:
1946                 case OB_MBALL:
1947                         if(mode & (OB_MODE_EDIT))
1948                                 return 1;
1949                         return 0;
1950                 case OB_LATTICE:
1951                         if(mode & (OB_MODE_EDIT|OB_MODE_WEIGHT_PAINT))
1952                                 return 1;
1953                 case OB_ARMATURE:
1954                         if(mode & (OB_MODE_EDIT|OB_MODE_POSE))
1955                                 return 1;
1956                 }
1957         }
1958
1959         return 0;
1960 }
1961
1962 static int object_mode_set_exec(bContext *C, wmOperator *op)
1963 {
1964         Object *ob= CTX_data_active_object(C);
1965         ObjectMode mode = RNA_enum_get(op->ptr, "mode");
1966         ObjectMode restore_mode = (ob) ? ob->mode : OB_MODE_OBJECT;
1967         int toggle = RNA_boolean_get(op->ptr, "toggle");
1968
1969         if(!ob || !object_mode_set_compat(C, op, ob))
1970                 return OPERATOR_PASS_THROUGH;
1971
1972         /* Exit current mode if it's not the mode we're setting */
1973         if(ob->mode != OB_MODE_OBJECT && ob->mode != mode)
1974                 WM_operator_name_call(C, object_mode_op_string(ob->mode), WM_OP_EXEC_REGION_WIN, NULL);
1975
1976         if(mode != OB_MODE_OBJECT) {
1977                 /* Enter new mode */
1978                 if(ob->mode != mode || toggle)
1979                         WM_operator_name_call(C, object_mode_op_string(mode), WM_OP_EXEC_REGION_WIN, NULL);
1980
1981                 if(toggle) {
1982                         if(ob->mode == mode)
1983                                 /* For toggling, store old mode so we know what to go back to */
1984                                 ob->restore_mode = restore_mode;
1985                         else if(ob->restore_mode != OB_MODE_OBJECT && ob->restore_mode != mode) {
1986                                 WM_operator_name_call(C, object_mode_op_string(ob->restore_mode), WM_OP_EXEC_REGION_WIN, NULL);
1987                         }
1988                 }
1989         }
1990
1991         return OPERATOR_FINISHED;
1992 }
1993
1994 void OBJECT_OT_mode_set(wmOperatorType *ot)
1995 {
1996         PropertyRNA *prop;
1997
1998         /* identifiers */
1999         ot->name= "Set Object Mode";
2000         ot->description = "Sets the object interaction mode";
2001         ot->idname= "OBJECT_OT_mode_set";
2002         
2003         /* api callbacks */
2004         ot->exec= object_mode_set_exec;
2005         
2006         ot->poll= ED_operator_object_active_editable;
2007         
2008         /* flags */
2009         ot->flag= 0; /* no register/undo here, leave it to operators being called */
2010         
2011         prop= RNA_def_enum(ot->srna, "mode", object_mode_items, OB_MODE_OBJECT, "Mode", "");
2012         RNA_def_enum_funcs(prop, object_mode_set_itemsf);
2013
2014         RNA_def_boolean(ot->srna, "toggle", 0, "Toggle", "");
2015 }
2016
2017
2018
2019 void ED_object_toggle_modes(bContext *C, int mode)
2020 {
2021         if(mode & OB_MODE_SCULPT)
2022                 WM_operator_name_call(C, "SCULPT_OT_sculptmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2023         if(mode & OB_MODE_VERTEX_PAINT)
2024                 WM_operator_name_call(C, "PAINT_OT_vertex_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2025         if(mode & OB_MODE_WEIGHT_PAINT)
2026                 WM_operator_name_call(C, "PAINT_OT_weight_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2027         if(mode & OB_MODE_TEXTURE_PAINT)
2028                 WM_operator_name_call(C, "PAINT_OT_texture_paint_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2029         if(mode & OB_MODE_PARTICLE_EDIT)
2030                 WM_operator_name_call(C, "PARTICLE_OT_particle_edit_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2031         if(mode & OB_MODE_POSE)
2032                 WM_operator_name_call(C, "OBJECT_OT_posemode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2033         if(mode & OB_MODE_EDIT)
2034                 WM_operator_name_call(C, "OBJECT_OT_editmode_toggle", WM_OP_EXEC_REGION_WIN, NULL);
2035 }
2036
2037 /************************ Game Properties ***********************/
2038
2039 static int game_property_new(bContext *C, wmOperator *UNUSED(op))
2040 {
2041         Object *ob= CTX_data_active_object(C);
2042         bProperty *prop;
2043
2044         if(!ob)
2045                 return OPERATOR_CANCELLED;
2046
2047         prop= new_property(PROP_FLOAT);
2048         BLI_addtail(&ob->prop, prop);
2049         unique_property(NULL, prop, 0); // make_unique_prop_names(prop->name);
2050
2051         WM_event_add_notifier(C, NC_LOGIC, NULL);
2052         return OPERATOR_FINISHED;
2053 }
2054
2055
2056 void OBJECT_OT_game_property_new(wmOperatorType *ot)
2057 {
2058         /* identifiers */
2059         ot->name= "New Game Property";
2060         ot->idname= "OBJECT_OT_game_property_new";
2061
2062         /* api callbacks */
2063         ot->exec= game_property_new;
2064         ot->poll= ED_operator_object_active_editable;
2065
2066         /* flags */
2067         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2068 }
2069
2070 static int game_property_remove(bContext *C, wmOperator *op)
2071 {
2072         Object *ob= CTX_data_active_object(C);
2073         bProperty *prop;
2074         int index= RNA_int_get(op->ptr, "index");
2075
2076         if(!ob)
2077                 return OPERATOR_CANCELLED;
2078
2079         prop= BLI_findlink(&ob->prop, index);
2080
2081         if(prop) {
2082                 BLI_remlink(&ob->prop, prop);
2083                 free_property(prop);
2084
2085                 WM_event_add_notifier(C, NC_LOGIC, NULL);
2086                 return OPERATOR_FINISHED;
2087         }
2088         else {
2089                 return OPERATOR_CANCELLED;
2090         }
2091 }
2092
2093 void OBJECT_OT_game_property_remove(wmOperatorType *ot)
2094 {
2095         /* identifiers */
2096         ot->name= "Remove Game Property";
2097         ot->idname= "OBJECT_OT_game_property_remove";
2098
2099         /* api callbacks */
2100         ot->exec= game_property_remove;
2101         ot->poll= ED_operator_object_active_editable;
2102
2103         /* flags */
2104         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2105
2106         RNA_def_int(ot->srna, "index", 0, 0, INT_MAX, "Index", "Property index to remove ", 0, INT_MAX);
2107 }
2108
2109 #define COPY_PROPERTIES_REPLACE 1
2110 #define COPY_PROPERTIES_MERGE   2
2111 #define COPY_PROPERTIES_COPY    3
2112
2113 static EnumPropertyItem game_properties_copy_operations[] ={
2114         {COPY_PROPERTIES_REPLACE, "REPLACE", 0, "Replace Properties", ""},
2115         {COPY_PROPERTIES_MERGE, "MERGE", 0, "Merge Properties", ""},
2116         {COPY_PROPERTIES_COPY, "COPY", 0, "Copy a Property", ""},
2117         {0, NULL, 0, NULL, NULL}};
2118
2119 static EnumPropertyItem gameprops_items[]= {
2120         {0, NULL, 0, NULL, NULL}};
2121
2122 static EnumPropertyItem *gameprops_itemf(bContext *C, PointerRNA *UNUSED(ptr), int *free)
2123 {       
2124         Object *ob= ED_object_active_context(C);
2125         EnumPropertyItem tmp = {0, "", 0, "", ""};
2126         EnumPropertyItem *item= NULL;
2127         bProperty *prop;
2128         int a, totitem= 0;
2129         
2130         if(!ob)
2131                 return gameprops_items;
2132
2133         for(a=1, prop= ob->prop.first; prop; prop=prop->next, a++) {
2134                 tmp.value= a;
2135                 tmp.identifier= prop->name;
2136                 tmp.name= prop->name;
2137                 RNA_enum_item_add(&item, &totitem, &tmp);
2138         }
2139
2140         RNA_enum_item_end(&item, &totitem);
2141         *free= 1;
2142
2143         return item;
2144 }
2145
2146 static int game_property_copy_exec(bContext *C, wmOperator *op)
2147 {
2148         Object *ob=ED_object_active_context(C);
2149         bProperty *prop;
2150         int type = RNA_enum_get(op->ptr, "operation");
2151         int propid= RNA_enum_get(op->ptr, "property");
2152
2153         if(propid > 0) { /* copy */
2154                 prop = BLI_findlink(&ob->prop, propid-1);
2155                 
2156                 if(prop) {
2157                         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2158                                 if (ob != ob_iter) {
2159                                         if (ob->data != ob_iter->data)
2160                                                 set_ob_property(ob_iter, prop);
2161                                 }
2162                         } CTX_DATA_END;
2163                 }
2164         }
2165         else if (ELEM(type, COPY_PROPERTIES_REPLACE, COPY_PROPERTIES_MERGE)) {
2166                 CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2167                         if (ob != ob_iter) {
2168                                 if (ob->data != ob_iter->data){
2169                                         if (type == 2) {/* merge */
2170                                                 for(prop = ob->prop.first; prop; prop= prop->next ) {
2171                                                         set_ob_property(ob_iter, prop);
2172                                                 }
2173                                         } else /* replace */
2174                                                 copy_properties( &ob_iter->prop, &ob->prop );
2175                                 }
2176                         }
2177                 }
2178                 CTX_DATA_END;
2179         }
2180
2181         return OPERATOR_FINISHED;
2182 }
2183
2184 void OBJECT_OT_game_property_copy(wmOperatorType *ot)
2185 {
2186         PropertyRNA *prop;
2187         /* identifiers */
2188         ot->name= "Copy Game Property";
2189         ot->idname= "OBJECT_OT_game_property_copy";
2190
2191         /* api callbacks */
2192         ot->exec= game_property_copy_exec;
2193         ot->poll= ED_operator_object_active_editable;
2194
2195         /* flags */
2196         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2197
2198         RNA_def_enum(ot->srna, "operation", game_properties_copy_operations, 3, "Operation", "");
2199         prop=RNA_def_enum(ot->srna, "property", gameprops_items, 0, "Property", "Properties to copy");
2200         RNA_def_enum_funcs(prop, gameprops_itemf);
2201         ot->prop=prop;
2202 }
2203
2204 static int game_property_clear_exec(bContext *C, wmOperator *UNUSED(op))
2205 {
2206         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2207                 free_properties(&ob_iter->prop);
2208         }
2209         CTX_DATA_END;
2210
2211         WM_event_add_notifier(C, NC_LOGIC, NULL);
2212         return OPERATOR_FINISHED;
2213 }
2214 void OBJECT_OT_game_property_clear(wmOperatorType *ot)
2215 {
2216         /* identifiers */
2217         ot->name= "Clear Game Property";
2218         ot->idname= "OBJECT_OT_game_property_clear";
2219
2220         /* api callbacks */
2221         ot->exec= game_property_clear_exec;
2222         ot->poll= ED_operator_object_active_editable;
2223
2224         /* flags */
2225         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2226 }
2227
2228 /************************ Copy Logic Bricks ***********************/
2229
2230 static int logicbricks_copy_exec(bContext *C, wmOperator *UNUSED(op))
2231 {
2232         Object *ob=ED_object_active_context(C);
2233
2234         CTX_DATA_BEGIN(C, Object*, ob_iter, selected_editable_objects) {
2235                 if(ob != ob_iter) {
2236                         /* first: free all logic */
2237                         free_sensors(&ob_iter->sensors);                                
2238                         unlink_controllers(&ob_iter->controllers);
2239                         free_controllers(&ob_iter->controllers);
2240                         unlink_actuators(&ob_iter->actuators);
2241                         free_actuators(&ob_iter->actuators);
2242                 
2243                         /* now copy it, this also works without logicbricks! */
2244                         clear_sca_new_poins_ob(ob);
2245                         copy_sensors(&ob_iter->sensors, &ob->sensors);
2246                         copy_controllers(&ob_iter->controllers, &ob->controllers);
2247                         copy_actuators(&ob_iter->actuators, &ob->actuators);
2248                         set_sca_new_poins_ob(ob_iter);
2249                 
2250                         /* some menu settings */
2251                         ob_iter->scavisflag= ob->scavisflag;
2252                         ob_iter->scaflag= ob->scaflag;
2253                 
2254                         /* set the initial state */
2255                         ob_iter->state= ob->state;
2256                         ob_iter->init_state= ob->init_state;
2257
2258                         if(ob_iter->totcol==ob->totcol) {
2259                                 ob_iter->actcol= ob->actcol;
2260                                 WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob_iter);
2261                         }
2262                 }
2263         }
2264         CTX_DATA_END;
2265
2266         WM_event_add_notifier(C, NC_LOGIC, NULL);
2267
2268         return OPERATOR_FINISHED;
2269 }
2270
2271 void OBJECT_OT_logic_bricks_copy(wmOperatorType *ot)
2272 {
2273         /* identifiers */
2274         ot->name= "Copy Logic Bricks to Selected";
2275         ot->description = "Copy logic bricks to other selected objects.";
2276         ot->idname= "OBJECT_OT_logic_bricks_copy";
2277
2278         /* api callbacks */
2279         ot->exec= logicbricks_copy_exec;
2280         ot->poll= ED_operator_object_active_editable;
2281
2282         /* flags */
2283         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
2284 }