Reserved some Bullet 2.x constraint data.
[blender.git] / source / blender / src / space.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  *
32  *
33  * - here initialize and free and handling SPACE data
34  */
35
36 #include <string.h>
37 #include <stdio.h>
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 #include "MEM_guardedalloc.h"
44 #include "MEM_CacheLimiterC-Api.h"
45
46 #ifdef INTERNATIONAL
47 #include "BIF_language.h"
48 #endif
49
50 #include "IMB_imbuf_types.h"
51 #include "IMB_imbuf.h"
52
53 #include "BLI_blenlib.h"
54 #include "BLI_arithb.h"
55 #include "BLI_gsqueue.h"
56 #include "BLI_linklist.h"
57
58 #include "DNA_action_types.h"
59 #include "DNA_armature_types.h"
60 #include "DNA_curve_types.h"
61 #include "DNA_group_types.h" /* used for select_same_group */
62 #include "DNA_image_types.h"
63 #include "DNA_ipo_types.h"
64 #include "DNA_mesh_types.h"
65 #include "DNA_meshdata_types.h"
66 #include "DNA_modifier_types.h" /* used for select grouped hooks */
67 #include "DNA_object_types.h"
68 #include "DNA_scene_types.h"
69 #include "DNA_screen_types.h"
70 #include "DNA_sequence_types.h"
71 #include "DNA_sound_types.h"
72 #include "DNA_space_types.h"
73 #include "DNA_userdef_types.h"
74 #include "DNA_view2d_types.h"
75 #include "DNA_view3d_types.h"
76
77 #include "BKE_blender.h"
78 #include "BKE_colortools.h"
79 #include "BKE_curve.h"
80 #include "BKE_depsgraph.h"
81 #include "BKE_displist.h"
82 #include "BKE_global.h"
83 #include "BKE_group.h"
84 #include "BKE_ipo.h"
85 #include "BKE_main.h"
86 #include "BKE_mesh.h"
87 #include "BKE_node.h"
88 #include "BKE_scene.h"
89 #include "BKE_utildefines.h"
90
91 #include "BIF_spacetypes.h"  /* first, nasty dependency with typedef */
92
93 #include "BIF_butspace.h"
94 #include "BIF_drawimage.h"
95 #include "BIF_drawseq.h"
96 #include "BIF_drawtext.h"
97 #include "BIF_drawscript.h"
98 #include "BIF_editarmature.h"
99 #include "BIF_editconstraint.h"
100 #include "BIF_editfont.h"
101 #include "BIF_editgroup.h"
102 #include "BIF_editkey.h"
103 #include "BIF_editlattice.h"
104 #include "BIF_editmesh.h"
105 #include "BIF_editmode_undo.h"
106 #include "BIF_editnla.h"
107 #include "BIF_editoops.h"
108 #include "BIF_editseq.h"
109 #include "BIF_editsima.h"
110 #include "BIF_editsound.h"
111 #include "BIF_editview.h"
112 #include "BIF_gl.h"
113 #include "BIF_imasel.h"
114 #include "BIF_interface.h"
115 #include "BIF_meshtools.h"
116 #include "BIF_mywindow.h"
117 #include "BIF_oops.h"
118 #include "BIF_poseobject.h"
119 #include "BIF_outliner.h"
120 #include "BIF_resources.h"
121 #include "BIF_retopo.h"
122 #include "BIF_screen.h"
123 #include "BIF_space.h"
124 #include "BIF_toets.h"
125 #include "BIF_toolbox.h"
126 #include "BIF_usiblender.h"
127 #include "BIF_previewrender.h"
128
129 #include "BSE_edit.h"
130 #include "BSE_view.h"
131 #include "BSE_editipo.h"
132 #include "BSE_drawipo.h"
133 #include "BSE_drawview.h"
134 #include "BSE_drawnla.h"
135 #include "BSE_filesel.h"
136 #include "BSE_headerbuttons.h"
137 #include "BSE_editnla_types.h"
138
139 #include "BDR_vpaint.h"
140 #include "BDR_editmball.h"
141 #include "BDR_editobject.h"
142 #include "BDR_editcurve.h"
143 #include "BDR_editface.h"
144 #include "BDR_drawmesh.h"
145 #include "BDR_drawobject.h"
146 #include "BDR_imagepaint.h"
147 #include "BDR_sculptmode.h"
148 #include "BDR_unwrapper.h"
149
150 #include "BLO_readfile.h" /* for BLO_blendhandle_close */
151
152 #include "PIL_time.h"
153
154 #include "BPY_extern.h"
155
156 #include "butspace.h"
157 #include "mydevice.h"
158 #include "blendef.h"
159 #include "datatoc.h"
160 #include "multires.h"
161
162 #include "BIF_transform.h"
163
164 #include "BKE_depsgraph.h"
165
166 #include "BSE_trans_types.h"
167
168 #include "SYS_System.h" /* for the user def menu ... should move elsewhere. */
169
170
171 extern void StartKetsjiShell(ScrArea *area, char* startscenename, struct Main* maggie, struct SpaceIpo* sipo,int always_use_expand_framing);
172 extern void StartKetsjiShellSimulation(ScrArea *area, char* startscenename, struct Main* maggie, struct SpaceIpo* sipo,int always_use_expand_framing);//rcruiz
173
174 /**
175  * When the mipmap setting changes, we want to redraw the view right
176  * away to reflect this setting.
177  */
178 void space_mipmap_button_function(int event);
179
180 void free_soundspace(SpaceSound *ssound);
181
182 /* *************************************** */
183
184 /* don't know yet how the handlers will evolve, for simplicity
185    i choose for an array with eventcodes, this saves in a file!
186    */
187 void add_blockhandler(ScrArea *sa, short eventcode, short val)
188 {
189         SpaceLink *sl= sa->spacedata.first;
190         short a;
191         
192         /* find empty spot */
193         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
194                 if( sl->blockhandler[a]==eventcode ) {
195                         sl->blockhandler[a+1]= val;
196                         break;
197                 }
198                 else if( sl->blockhandler[a]==0) {
199                         sl->blockhandler[a]= eventcode;
200                         sl->blockhandler[a+1]= val;
201                         break;
202                 }
203         }
204         if(a==SPACE_MAXHANDLER) printf("error; max (4) blockhandlers reached!\n");
205 }
206
207 void rem_blockhandler(ScrArea *sa, short eventcode)
208 {
209         SpaceLink *sl= sa->spacedata.first;
210         short a;
211         
212         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
213                 if( sl->blockhandler[a]==eventcode) {
214                         sl->blockhandler[a]= 0;
215                         
216                         /* specific free calls */
217                         if(eventcode==IMAGE_HANDLER_PREVIEW)
218                                 image_preview_event(0);
219                         break;
220                 }
221         }
222 }
223
224 void toggle_blockhandler(ScrArea *sa, short eventcode, short val)
225 {
226         SpaceLink *sl= sa->spacedata.first;
227         short a, addnew=1;
228         
229         /* find if it exists */
230         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
231                 if( sl->blockhandler[a]==eventcode ) {
232                         sl->blockhandler[a]= 0;
233                         
234                         /* specific free calls */
235                         if(eventcode==VIEW3D_HANDLER_PREVIEW)
236                                 BIF_view3d_previewrender_free(sa->spacedata.first);
237                         else if(eventcode==IMAGE_HANDLER_PREVIEW)
238                                 image_preview_event(0);
239                         
240                         addnew= 0;
241                 }
242         }
243         if(addnew) add_blockhandler(sa, eventcode, val);
244 }
245
246
247
248 /* ************* SPACE: VIEW3D  ************* */
249
250 /*  extern void drawview3dspace(ScrArea *sa, void *spacedata); BSE_drawview.h */
251
252
253 void copy_view3d_lock(short val)
254 {
255         bScreen *sc;
256         int bit;
257         
258         /* from G.scene copy to the other views */
259         sc= G.main->screen.first;
260         
261         while(sc) {
262                 if(sc->scene==G.scene) {
263                         ScrArea *sa= sc->areabase.first;
264                         while(sa) {
265                                 SpaceLink *sl= sa->spacedata.first;
266                                 while(sl) {
267                                         if(sl->spacetype==SPACE_OOPS && val==REDRAW) {
268                                                 if(sa->win) scrarea_queue_winredraw(sa);
269                                         }
270                                         else if(sl->spacetype==SPACE_VIEW3D) {
271                                                 View3D *vd= (View3D*) sl;
272                                                 if(vd->scenelock && vd->localview==0) {
273                                                         vd->lay= G.scene->lay;
274                                                         vd->camera= G.scene->camera;
275                                                         
276                                                         if(vd->camera==0 && vd->persp>1) vd->persp= 1;
277                                                         
278                                                         if( (vd->lay & vd->layact) == 0) {
279                                                                 bit= 0;
280                                                                 while(bit<32) {
281                                                                         if(vd->lay & (1<<bit)) {
282                                                                                 vd->layact= 1<<bit;
283                                                                                 break;
284                                                                         }
285                                                                         bit++;
286                                                                 }
287                                                         }
288                                                         
289                                                         if(val==REDRAW && vd==sa->spacedata.first) {
290                                                                 if(sa->win) scrarea_queue_redraw(sa);
291                                                         }
292                                                 }
293                                         }
294                                         sl= sl->next;
295                                 }
296                                 sa= sa->next;
297                         }
298                 }
299                 sc= sc->id.next;
300         }
301 }
302
303 void handle_view3d_around()
304 {
305         bScreen *sc;
306         
307         if ((U.uiflag & USER_LOCKAROUND)==0) return;
308         
309         /* copies from G.vd->around to other view3ds */
310         
311         sc= G.main->screen.first;
312         
313         while(sc) {
314                 if(sc->scene==G.scene) {
315                         ScrArea *sa= sc->areabase.first;
316                         while(sa) {
317                                 SpaceLink *sl= sa->spacedata.first;
318                                 while(sl) {
319                                         if(sl->spacetype==SPACE_VIEW3D) {
320                                                 View3D *vd= (View3D*) sl;
321                                                 if (vd != G.vd) {
322                                                         vd->around= G.vd->around;
323                                                         if (G.vd->flag & V3D_ALIGN)
324                                                                 vd->flag |= V3D_ALIGN;
325                                                         else
326                                                                 vd->flag &= ~V3D_ALIGN;
327                                                         scrarea_queue_headredraw(sa);
328                                                 }
329                                         }
330                                         sl= sl->next;
331                                 }
332                                 sa= sa->next;
333                         }
334                 }
335                 sc= sc->id.next;
336         }
337 }
338
339 void handle_view3d_lock()
340 {
341         if (G.vd != NULL) {
342                 if(G.vd->localview==0 && G.vd->scenelock && curarea->spacetype==SPACE_VIEW3D) {
343
344                         /* copy to scene */
345                         G.scene->lay= G.vd->lay;
346                         G.scene->camera= G.vd->camera;
347         
348                         copy_view3d_lock(REDRAW);
349                 }
350         }
351 }
352
353 void space_set_commmandline_options(void) {
354         SYS_SystemHandle syshandle;
355         int a;
356                 
357         if ( (syshandle = SYS_GetSystem()) ) {
358                 /* User defined settings */
359                 a= (U.gameflags & USER_VERTEX_ARRAYS);
360                 SYS_WriteCommandLineInt(syshandle, "vertexarrays", a);
361
362                 a= (U.gameflags & USER_DISABLE_SOUND);
363                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
364
365                 a= (U.gameflags & USER_DISABLE_MIPMAP);
366                 set_mipmap(!a);
367                 SYS_WriteCommandLineInt(syshandle, "nomipmap", a);
368
369                 /* File specific settings: */
370                 /* Only test the first one. These two are switched
371                  * simultaneously. */
372                 a= (G.fileflags & G_FILE_SHOW_FRAMERATE);
373                 SYS_WriteCommandLineInt(syshandle, "show_framerate", a);
374                 SYS_WriteCommandLineInt(syshandle, "show_profile", a);
375
376                 /* When in wireframe mode, always draw debug props. */
377                 if (G.vd) {
378                         a = ( (G.fileflags & G_FILE_SHOW_DEBUG_PROPS) 
379                                   || (G.vd->drawtype == OB_WIRE)          
380                                   || (G.vd->drawtype == OB_SOLID)         );
381                         SYS_WriteCommandLineInt(syshandle, "show_properties", a);
382                 }
383
384                 a= (G.fileflags & G_FILE_ENABLE_ALL_FRAMES);
385                 SYS_WriteCommandLineInt(syshandle, "fixedtime", a);
386
387                 a= (G.fileflags & G_FILE_GAME_TO_IPO);
388                 SYS_WriteCommandLineInt(syshandle, "game2ipo", a);
389
390                 a=(G.fileflags & G_FILE_GAME_MAT);
391                 SYS_WriteCommandLineInt(syshandle, "blender_material", a);
392                 a=(G.fileflags & G_FILE_DIAPLAY_LISTS);
393                 SYS_WriteCommandLineInt(syshandle, "displaylists", a);
394
395
396         }
397 }
398
399 #if GAMEBLENDER == 1
400         /**
401          * These two routines imported from the gameengine, 
402          * I suspect a lot of the resetting stuff is cruft
403          * and can be removed, but it should be checked.
404          */
405 static void SaveState(void)
406 {
407         glPushAttrib(GL_ALL_ATTRIB_BITS);
408
409         init_realtime_GL();
410         init_gl_stuff();
411
412         if(G.f & G_TEXTUREPAINT)
413                 texpaint_enable_mipmap();
414
415         if(G.scene->camera==0 || G.scene->camera->type!=OB_CAMERA)
416                 error("no (correct) camera");
417
418         waitcursor(1);
419 }
420
421 static void RestoreState(void)
422 {
423         if(G.f & G_TEXTUREPAINT)
424                 texpaint_disable_mipmap();
425
426         curarea->win_swap = 0;
427         curarea->head_swap=0;
428         allqueue(REDRAWVIEW3D, 1);
429         allqueue(REDRAWBUTSALL, 0);
430         reset_slowparents();
431         waitcursor(0);
432         G.qual= 0;
433         glPopAttrib();
434 }
435
436 static LinkNode *save_and_reset_all_scene_cfra(void)
437 {
438         LinkNode *storelist= NULL;
439         Scene *sc;
440         
441         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
442                 BLI_linklist_prepend(&storelist, (void*) (long) sc->r.cfra);
443
444                 /* why is this reset to 1 ?*/
445                 /* sc->r.cfra= 1;*/
446
447                 set_scene_bg(sc);
448         }
449         
450         BLI_linklist_reverse(&storelist);
451         
452         return storelist;
453 }
454
455 static void restore_all_scene_cfra(LinkNode *storelist) {
456         LinkNode *sc_store= storelist;
457         Scene *sc;
458         
459         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
460                 int stored_cfra= (int) sc_store->link;
461                 
462                 sc->r.cfra= stored_cfra;
463                 set_scene_bg(sc);
464                 
465                 sc_store= sc_store->next;
466         }
467         
468         BLI_linklist_free(storelist, NULL);
469 }
470 #endif
471
472 void start_game(void)
473 {
474 #if GAMEBLENDER == 1
475 #ifndef NO_KETSJI
476         Scene *sc, *startscene = G.scene;
477         LinkNode *scene_cfra_store;
478
479                 /* XXX, silly code -  the game engine can
480                  * access any scene through logic, so we try 
481                  * to make sure each scene has a valid camera, 
482                  * just in case the game engine tries to use it.
483                  * 
484                  * Better would be to make a better routine
485                  * in the game engine for finding the camera.
486                  *  - zr
487                  * Note: yes, this is all very badly hacked! (ton)
488                  */
489         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
490                 if (!sc->camera) {
491                         Base *base;
492         
493                         for (base= sc->base.first; base; base= base->next)
494                                 if (base->object->type==OB_CAMERA)
495                                         break;
496                         
497                         sc->camera= base?base->object:NULL;
498                 }
499         }
500
501         /* these two lines make sure front and backbuffer are equal. for swapbuffers */
502         markdirty_all();
503         screen_swapbuffers();
504
505         /* can start from header */
506         mywinset(curarea->win);
507     
508         scene_cfra_store= save_and_reset_all_scene_cfra();
509         
510
511         /* game engine will do its own sounds. */
512         sound_stop_all_sounds();
513         sound_exit_audio();
514         
515         /* Before jumping into Ketsji, we configure some settings. */
516         space_set_commmandline_options();
517
518         SaveState();
519         StartKetsjiShell(curarea, startscene->id.name+2, G.main,G.sipo, 1);
520         RestoreState();
521
522         /* Restart BPY - unload the game engine modules. */
523         BPY_end_python();
524         BPY_start_python(0, NULL); /* argc, argv stored there already */
525         BPY_post_start_python(); /* userpref path and menus init */
526
527         restore_all_scene_cfra(scene_cfra_store);
528         set_scene_bg(startscene);
529         scene_update_for_newframe(G.scene, G.scene->lay);
530
531         if (G.flags & G_FILE_AUTOPLAY)
532                 exit_usiblender();
533
534                 /* groups could have changed ipo */
535         allqueue(REDRAWNLA, 0);
536         allqueue(REDRAWACTION, 0);
537         allspace(REMAKEIPO, 0);
538         allqueue(REDRAWIPO, 0);
539 #endif
540 #else
541         notice("Game engine is disabled in this release!");
542 #endif
543 }
544
545 void start_RBSimulation(void)
546 {
547 #if GAMEBLENDER == 1
548 #ifndef NO_KETSJI
549         Scene *sc, *startscene = G.scene;
550         LinkNode *scene_cfra_store;
551
552                 /* XXX, silly code -  the game engine can
553                  * access any scene through logic, so we try
554                  * to make sure each scene has a valid camera,
555                  * just in case the game engine tries to use it.
556                  *
557                  * Better would be to make a better routine
558                  * in the game engine for finding the camera.
559                  *  - zr
560                  * Note: yes, this is all very badly hacked! (ton)
561                  */
562         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
563                 if (!sc->camera) {
564                         Base *base;
565
566                         for (base= sc->base.first; base; base= base->next)
567                                 if (base->object->type==OB_CAMERA)
568                                         break;
569
570                         sc->camera= base?base->object:NULL;
571                 }
572         }
573
574         /* these two lines make sure front and backbuffer are equal. for swapbuffers */
575         markdirty_all();
576         screen_swapbuffers();
577
578         /* can start from header */
579         mywinset(curarea->win);
580
581         scene_cfra_store= save_and_reset_all_scene_cfra();
582
583
584         /* game engine will do its own sounds. */
585         sound_stop_all_sounds();
586         sound_exit_audio();
587
588         /* Before jumping into Ketsji, we configure some settings. */
589         space_set_commmandline_options();
590
591         SaveState();
592         StartKetsjiShellSimulation(curarea, startscene->id.name+2, G.main,G.sipo, 1);
593         RestoreState();
594         /* Restart BPY - unload the game engine modules. */
595         BPY_end_python();
596         BPY_start_python(0, NULL); /* argc, argv stored there already */
597         BPY_post_start_python(); /* userpref path and menus init */
598
599         restore_all_scene_cfra(scene_cfra_store);
600         set_scene_bg(startscene);
601         scene_update_for_newframe(G.scene, G.scene->lay);
602
603         if (G.flags & G_FILE_AUTOPLAY)
604                 exit_usiblender();
605
606                 /* groups could have changed ipo */
607         allqueue(REDRAWNLA, 0);
608         allqueue(REDRAWACTION, 0);
609         allspace(REMAKEIPO, 0);
610         allqueue(REDRAWIPO, 0);
611 #endif
612 #else
613         notice("YOU NEED GAME ENGIEN TO RUN THE SIMULATION!");
614 #endif
615 }
616
617 static void changeview3dspace(ScrArea *sa, void *spacedata)
618 {
619         setwinmatrixview3d(sa->winx, sa->winy, NULL);   /* 0= no pick rect */
620 }
621
622         /* Callable from editmode and faceselect mode from the
623          * moment, would be nice (and is easy) to generalize
624          * to any mode.
625          */
626 static void align_view_to_selected(View3D *v3d)
627 {
628         int nr= pupmenu("Align View%t|To Selected (top)%x2|To Selected (front)%x1|To Selected (side)%x0");
629
630         if (nr!=-1) {
631                 int axis= nr;
632
633                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
634                         editmesh_align_view_to_selected(v3d, axis);
635                         addqueue(v3d->area->win, REDRAW, 1);
636                 } else if (G.f & G_FACESELECT) {
637                         Object *obact= OBACT;
638                         if (obact && obact->type==OB_MESH) {
639                                 Mesh *me= obact->data;
640
641                                 if (me->mtface) {
642                                         faceselect_align_view_to_selected(v3d, me, axis);
643                                         addqueue(v3d->area->win, REDRAW, 1);
644                                 }
645                         }
646                 }
647         }
648 }
649
650 static void select_children(Object *ob, int recursive)
651 {
652         Base *base;
653
654         for (base= FIRSTBASE; base; base= base->next)
655                 if (ob == base->object->parent) {
656                         base->flag |= SELECT;
657                         base->object->flag |= SELECT;
658                         if (recursive) select_children(base->object, 1);
659                 }
660 }
661
662 static void select_parent(void) /* Makes parent active and de-selected OBACT */
663 {
664         Base *base, *startbase, *basact=NULL, *oldbasact;
665         
666         if (!(OBACT) || !(OBACT->parent)) return;
667         BASACT->flag &= (~SELECT);
668         BASACT->object->flag &= (~SELECT);
669         startbase=  FIRSTBASE;
670         if(BASACT && BASACT->next) startbase= BASACT->next;
671         base = startbase;
672         while(base) {
673                 if(base->object==BASACT->object->parent) { basact=base; break; }
674                 base=base->next;
675                 if(base==NULL) base= FIRSTBASE;
676                 if(base==startbase) break;
677         }
678         /* can be NULL if parent in other scene */
679         if(basact) {
680                 oldbasact = BASACT;
681                 BASACT = basact;
682                 basact->flag |= SELECT;         
683                 
684                 basact->object->flag= basact->flag;
685                 
686                 set_active_base(basact);
687         }
688 }
689
690 static void select_same_group(Object *ob)       /* Select objects in the same group as the active */
691 {
692         Base *base;
693         Group *group= find_group(ob);
694         if (!group || !ob)
695                 return;
696         
697         for (base= FIRSTBASE; base; base= base->next) {
698                 if (!(base->flag & SELECT) && object_in_group(base->object, group)) {
699                         base->flag |= SELECT;
700                         base->object->flag |= SELECT;
701                 }
702         }
703 }
704
705 static void select_object_hooks(Object *ob)     /* Select objects in the same group as the active */
706 {
707         Base *base;
708         ModifierData *md;
709         HookModifierData *hmd;
710         
711         if (!ob)
712                 return;
713         
714         for (md = ob->modifiers.first; md; md=md->next) {
715                 if (md->type==eModifierType_Hook) {
716                         hmd= (HookModifierData*) md;
717                         if (hmd->object && !(hmd->object->flag & SELECT)) {
718                                 base= object_in_scene(hmd->object, G.scene);
719                                 base->flag |= SELECT;
720                                 base->object->flag |= SELECT;
721                         }
722                 }
723         }
724 }
725
726 static void select_same_parent(Object *ob)      /* Select objects woth the same parent as the active (siblings), parent can be NULL also */
727 {
728         Base *base;
729         if (!ob)
730                 return;
731         
732         for (base= FIRSTBASE; base; base= base->next)
733                 if (base->object->parent==ob->parent) {
734                         base->flag |= SELECT;
735                         base->object->flag |= SELECT;
736                 }
737 }
738
739 static void select_same_type(Object *ob)        /* Select objects woth the same parent as the active (siblings), parent can be NULL also */
740 {
741         Base *base;
742         if (!ob)
743                 return;
744         
745         for (base= FIRSTBASE; base; base= base->next)
746                 if (base->object->type==ob->type) {
747                         base->flag |= SELECT;
748                         base->object->flag |= SELECT;
749                 }
750 }
751
752 void select_object_grouped(short nr)
753 {
754         Base *base;
755         
756         if(nr==6) {
757                 base= FIRSTBASE;
758                 while(base) {
759                         if (base->lay & OBACT->lay) {
760                                 base->flag |= SELECT;
761                                 base->object->flag |= SELECT;
762                         }
763                         base= base->next;
764                 }
765         }
766         else if(nr==1) select_children(OBACT, 1);
767         else if(nr==2) select_children(OBACT, 0);
768         else if(nr==3) select_parent();
769         else if(nr==4) select_same_parent(OBACT);       
770         else if(nr==5) select_same_type(OBACT); 
771         else if(nr==7) select_same_group(OBACT);
772         else if(nr==8) select_object_hooks(OBACT);
773         
774         
775         
776         
777         countall();
778         allqueue(REDRAWVIEW3D, 0);
779         allqueue(REDRAWBUTSOBJECT, 0);
780         allspace(REMAKEIPO, 0);
781         allqueue(REDRAWIPO, 0);
782 }
783
784 static void select_object_grouped_menu(void)
785 {
786         char *str;
787         short nr;
788
789         /* make menu string */
790         
791         str= MEM_mallocN(512, "groupmenu");
792         strcpy(str, "Select Grouped%t|Children%x1|"
793                     "Immediate Children%x2|Parent%x3|"
794                     "Siblings (Shared Parent)%x4|"
795                     "Objects of Same Type%x5|"
796                                 "Objects on Shared Layers%x6|"
797                 "Objects in Same Group%x7|"
798                 "Object Hooks%x8|");
799
800         /* here we go */
801         
802         nr= pupmenu(str);
803         MEM_freeN(str);
804         
805         select_object_grouped(nr);
806 }
807
808 void join_menu(void)
809 {
810         Object *ob= OBACT;
811         if (ob && !G.obedit) {
812                 if(ob->type == OB_MESH) {
813                         if(okee("Join selected meshes")==0) return;
814                         join_mesh();
815                 } else if(ob->type == OB_CURVE) {
816                         if(okee("Join selected curves")==0) return;
817                         join_curve(OB_CURVE);
818                 } else if(ob->type == OB_SURF) {
819                         if(okee("Join selected NURBS")==0) return;
820                         join_curve(OB_SURF);
821                 } else if(ob->type == OB_ARMATURE) {
822                         /*      Make sure the user wants to continue*/
823                         if(okee("Join selected armatures")==0) return;
824                         join_armature ();
825                 }
826         }
827 }
828
829 static unsigned short convert_for_nonumpad(unsigned short event)
830 {
831         if (event>=ZEROKEY && event<=NINEKEY) {
832                 return event - ZEROKEY + PAD0;
833         } else if (event==MINUSKEY) {
834                 return PADMINUS;
835         } else if (event==EQUALKEY) {
836                 return PADPLUSKEY;
837         } else if (event==BACKSLASHKEY) {
838                 return PADSLASHKEY;
839         } else {
840                 return event;
841         }
842 }
843
844 /* *************** */
845
846 void BIF_undo_push(char *str)
847 {
848         if(G.obedit) {
849                 if (U.undosteps == 0) return;
850
851                 if(G.obedit->type==OB_MESH)
852                         undo_push_mesh(str);
853                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
854                         undo_push_curve(str);
855                 else if (G.obedit->type==OB_FONT)
856                         undo_push_font(str);
857                 else if (G.obedit->type==OB_MBALL)
858                         undo_push_mball(str);
859                 else if (G.obedit->type==OB_LATTICE)
860                         undo_push_lattice(str);
861                 else if (G.obedit->type==OB_ARMATURE)
862                         undo_push_armature(str);
863         }
864         else if(G.f & G_SCULPTMODE) {
865         }
866         else {
867                 if(U.uiflag & USER_GLOBALUNDO) 
868                         BKE_write_undo(str);
869         }
870 }
871
872 void BIF_undo(void)
873 {       
874         if(G.obedit) {
875                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
876                         undo_editmode_step(1);
877         }
878         else {
879                 if(G.f & G_TEXTUREPAINT)
880                         imagepaint_undo();
881                 else if(G.f & G_SCULPTMODE)
882                         sculptmode_undo();
883                 else if(curarea->spacetype==SPACE_IMAGE && (G.sima->flag & SI_DRAWTOOL))
884                         imagepaint_undo();
885                 else {
886                         /* now also in faceselect mode */
887                         if(U.uiflag & USER_GLOBALUNDO) {
888                                 if(G.f & G_SCULPTMODE)
889                                         set_sculpt_object(NULL);
890
891                                 BKE_undo_step(1);
892                                 sound_initialize_sounds();
893                         }
894                 }
895         }
896 }
897
898 void BIF_redo(void)
899 {
900         if(G.obedit) {
901                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
902                         undo_editmode_step(-1);
903         }
904         else {
905                 if(G.f & G_TEXTUREPAINT)
906                         imagepaint_undo();
907                 else if(G.f & G_SCULPTMODE)
908                         sculptmode_redo();
909                 else if(curarea->spacetype==SPACE_IMAGE && (G.sima->flag & SI_DRAWTOOL))
910                         imagepaint_undo();
911                 else {
912                         /* includes faceselect now */
913                         if(U.uiflag & USER_GLOBALUNDO) {
914                                 BKE_undo_step(-1);
915                                 sound_initialize_sounds();
916                         }
917                 }
918         }
919 }
920
921 void BIF_undo_menu(void)
922 {
923         if(G.obedit) {
924                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
925                         undo_editmode_menu();
926                 allqueue(REDRAWALL, 0);
927         }
928         else {
929                 if(G.f & G_SCULPTMODE)
930                         sculptmode_undo_menu();
931                 else {
932                         if(U.uiflag & USER_GLOBALUNDO) {
933                                 char *menu= BKE_undo_menu_string();
934                                 if(menu) {
935                                         short event= pupmenu_col(menu, 20);
936                                         MEM_freeN(menu);
937                                         if(event>0) BKE_undo_number(event);
938                                 }
939                         }
940                 }
941         }
942 }
943
944 /* *************** */
945
946 static void winqreadview3dspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
947 {
948         View3D *v3d= sa->spacedata.first;
949         Object *ob= OBACT;      /* do not change! */
950         float *curs;
951         int doredraw= 0, pupval;
952         unsigned short event= evt->event, origevent= evt->event;
953         short val= evt->val;
954         char ascii= evt->ascii;
955         
956         if(curarea->win==0) return;     /* when it comes from sa->headqread() */
957         
958         if(val) {
959
960                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
961
962                 //if(event==MOUSEY || event==MOUSEX) return;
963                 
964                 if(event==UI_BUT_EVENT) do_butspace(val); /* temporal, view3d deserves own queue? */
965                 
966                 /* we consider manipulator a button, defaulting to leftmouse */
967                 if(event==LEFTMOUSE) if(BIF_do_manipulator(sa)) return;
968                 
969                 /* swap mouse buttons based on user preference */
970                 if (U.flag & USER_LMOUSESELECT) {
971                         if (event==LEFTMOUSE) event = RIGHTMOUSE;
972                         else if (event==RIGHTMOUSE) event = LEFTMOUSE;
973                 }
974
975                 if(!G.obedit && (G.f & G_SCULPTMODE)) {
976                         if(G.scene->sculptdata.propset) {
977                                 sculptmode_propset(event);
978                                 return;
979                         }
980                         else if(event!=LEFTMOUSE && event!=MIDDLEMOUSE && (event==MOUSEY || event==MOUSEX)) {
981                                 if(!bwin_qtest(sa->win))
982                                         allqueue(REDRAWVIEW3D, 0);
983                         }
984                 }
985
986                 /* Handle retopo painting */
987                 if(retopo_mesh_paint_check()) {
988                         if(!retopo_paint(event))
989                                 return;
990                 }
991
992                 /* run any view3d event handler script links */
993                 if (event && sa->scriptlink.totscript)
994                         if (BPY_do_spacehandlers(sa, event, SPACEHANDLER_VIEW3D_EVENT))
995                                 return; /* return if event was processed (swallowed) by handler(s) */
996
997                 /* TEXTEDITING?? */
998                 if((G.obedit) && G.obedit->type==OB_FONT) {
999                         switch(event) {
1000                         
1001                         case LEFTMOUSE:
1002                                 mouse_cursor();
1003                                 break;
1004                         case MIDDLEMOUSE:
1005                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
1006                                 if(U.flag & USER_VIEWMOVE) {
1007                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1008                                                 viewmove(0);
1009                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1010                                                 viewmove(2);
1011                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1012                                                 viewmove(1);
1013                                 }
1014                                 else {
1015                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1016                                                 viewmove(1);
1017                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1018                                                 viewmove(2);
1019                                         else
1020                                                 viewmove(0);
1021                                 }
1022                                 break;
1023                                 
1024                         case WHEELUPMOUSE:
1025                                 /* Regular:   Zoom in */
1026                                 /* Shift:     Scroll up */
1027                                 /* Ctrl:      Scroll right */
1028                                 /* Alt-Shift: Rotate up */
1029                                 /* Alt-Ctrl:  Rotate right */
1030
1031                                 if( G.qual & LR_SHIFTKEY ) {
1032                                         if( G.qual & LR_ALTKEY ) { 
1033                                                 G.qual &= ~LR_SHIFTKEY;
1034                                                 persptoetsen(PAD2);
1035                                                 G.qual |= LR_SHIFTKEY;
1036                                         } else {
1037                                                 persptoetsen(PAD2);
1038                                         }
1039                                 } else if( G.qual & LR_CTRLKEY ) {
1040                                         if( G.qual & LR_ALTKEY ) { 
1041                                                 G.qual &= ~LR_CTRLKEY;
1042                                                 persptoetsen(PAD4);
1043                                                 G.qual |= LR_CTRLKEY;
1044                                         } else {
1045                                                 persptoetsen(PAD4);
1046                                         }
1047                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1048                                         persptoetsen(PADMINUS);
1049                                 else
1050                                         persptoetsen(PADPLUSKEY);
1051
1052                                 doredraw= 1;
1053                                 break;
1054
1055                         case WHEELDOWNMOUSE:
1056                                 /* Regular:   Zoom out */
1057                                 /* Shift:     Scroll down */
1058                                 /* Ctrl:      Scroll left */
1059                                 /* Alt-Shift: Rotate down */
1060                                 /* Alt-Ctrl:  Rotate left */
1061
1062                                 if( G.qual & LR_SHIFTKEY ) {
1063                                         if( G.qual & LR_ALTKEY ) { 
1064                                                 G.qual &= ~LR_SHIFTKEY;
1065                                                 persptoetsen(PAD8);
1066                                                 G.qual |= LR_SHIFTKEY;
1067                                         } else {
1068                                                 persptoetsen(PAD8);
1069                                         }
1070                                 } else if( G.qual & LR_CTRLKEY ) {
1071                                         if( G.qual & LR_ALTKEY ) { 
1072                                                 G.qual &= ~LR_CTRLKEY;
1073                                                 persptoetsen(PAD6);
1074                                                 G.qual |= LR_CTRLKEY;
1075                                         } else {
1076                                                 persptoetsen(PAD6);
1077                                         }
1078                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1079                                         persptoetsen(PADPLUSKEY);
1080                                 else
1081                                         persptoetsen(PADMINUS);
1082                                 
1083                                 doredraw= 1;
1084                                 break;
1085
1086                         case UKEY:
1087                                 if(G.qual==LR_ALTKEY) {
1088                                         remake_editText();
1089                                         doredraw= 1;
1090                                 } 
1091                                 else {
1092                                         do_textedit(event, val, ascii);
1093                                 }
1094                                 break;
1095                         case VKEY:
1096                                 if(G.qual==LR_ALTKEY) {
1097                                         paste_editText();
1098                                         doredraw= 1;
1099                                 } 
1100                                 else {
1101                                         do_textedit(event, val, ascii);
1102                                 }
1103                                 break;
1104                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1105                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1106                         case PADENTER:
1107                                 persptoetsen(event);
1108                                 doredraw= 1;
1109                                 break;
1110                                 
1111                         default:
1112                                 do_textedit(event, val, ascii);
1113                                 break;
1114                         }
1115                 }
1116                 else {
1117
1118                         if (U.flag & USER_NONUMPAD) {
1119                                 event= convert_for_nonumpad(event);
1120                         }
1121
1122                         switch(event) {
1123                         
1124                         /* Afterqueue events */
1125                         case BACKBUFDRAW:
1126                                 backdrawview3d(1);
1127                                 break;
1128                         case RENDERPREVIEW:
1129                                 BIF_view3d_previewrender(sa);
1130                                 break;
1131                                 
1132                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1133                          * based on user preference USER_LMOUSESELECT
1134                          */
1135                         case LEFTMOUSE: 
1136                                 if ((G.obedit) || !(G.f&(G_SCULPTMODE|G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT))) {
1137                                         mouse_cursor();
1138                                 }
1139                                 else if (G.f & G_SCULPTMODE) {
1140                                         if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1141                                                 sculptmode_pmv(0);
1142                                         else if(!G.scene->sculptdata.propset)
1143                                                 sculpt();
1144                                 }
1145                                 else if (G.f & G_WEIGHTPAINT) {
1146                                         weight_paint();
1147                                 }
1148                                 else if (G.f & G_VERTEXPAINT) {
1149                                         vertex_paint();
1150                                 }
1151                                 else if (G.f & G_TEXTUREPAINT) {
1152                                         imagepaint_paint(origevent==LEFTMOUSE? L_MOUSE: R_MOUSE, 1);
1153                                 }
1154                                 break;
1155                         case MIDDLEMOUSE:
1156                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
1157                                 if(U.flag & USER_VIEWMOVE) {
1158                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1159                                                 viewmove(0);
1160                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1161                                                 viewmove(2);
1162                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1163                                                 viewmove(1);
1164                                 }
1165                                 else {
1166                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1167                                                 viewmove(1);
1168                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1169                                                 viewmove(2);
1170                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1171                                                 viewmove(0);
1172                                 }
1173                                 break;
1174                         case RIGHTMOUSE:
1175                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
1176                                         if(G.obedit->type==OB_MESH)
1177                                                 mouse_mesh();
1178                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1179                                                 mouse_nurb();
1180                                         else if(G.obedit->type==OB_MBALL)
1181                                                 mouse_mball();
1182                                         else if(G.obedit->type==OB_LATTICE)
1183                                                 mouse_lattice();
1184                                         else if(G.obedit->type==OB_ARMATURE)
1185                                                 mouse_armature();
1186                                 }
1187                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
1188                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1189                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
1190                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1191                                 else if(G.qual==LR_CTRLKEY)
1192                                         mouse_select(); /* also allow in editmode, for vertex parenting */
1193                                 else if(G.f & G_FACESELECT)
1194                                         face_select();
1195                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
1196                                         sample_vpaint();
1197                                 else if((G.f & G_SCULPTMODE) && G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1198                                         sculptmode_pmv(1);
1199                                 else
1200                                         mouse_select(); /* does poses too */
1201                                 break;
1202                         case WHEELUPMOUSE:
1203                                 /* Regular:   Zoom in */
1204                                 /* Shift:     Scroll up */
1205                                 /* Ctrl:      Scroll right */
1206                                 /* Alt-Shift: Rotate up */
1207                                 /* Alt-Ctrl:  Rotate right */
1208
1209                                 if( G.qual & LR_SHIFTKEY ) {
1210                                         if( G.qual & LR_ALTKEY ) { 
1211                                                 G.qual &= ~LR_SHIFTKEY;
1212                                                 persptoetsen(PAD2);
1213                                                 G.qual |= LR_SHIFTKEY;
1214                                         } else {
1215                                                 persptoetsen(PAD2);
1216                                         }
1217                                 } else if( G.qual & LR_CTRLKEY ) {
1218                                         if( G.qual & LR_ALTKEY ) { 
1219                                                 G.qual &= ~LR_CTRLKEY;
1220                                                 persptoetsen(PAD4);
1221                                                 G.qual |= LR_CTRLKEY;
1222                                         } else {
1223                                                 persptoetsen(PAD4);
1224                                         }
1225                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1226                                         persptoetsen(PADMINUS);
1227                                 else
1228                                         persptoetsen(PADPLUSKEY);
1229
1230                                 doredraw= 1;
1231                                 break;
1232                         case WHEELDOWNMOUSE:
1233                                 /* Regular:   Zoom out */
1234                                 /* Shift:     Scroll down */
1235                                 /* Ctrl:      Scroll left */
1236                                 /* Alt-Shift: Rotate down */
1237                                 /* Alt-Ctrl:  Rotate left */
1238
1239                                 if( G.qual & LR_SHIFTKEY ) {
1240                                         if( G.qual & LR_ALTKEY ) { 
1241                                                 G.qual &= ~LR_SHIFTKEY;
1242                                                 persptoetsen(PAD8);
1243                                                 G.qual |= LR_SHIFTKEY;
1244                                         } else {
1245                                                 persptoetsen(PAD8);
1246                                         }
1247                                 } else if( G.qual & LR_CTRLKEY ) {
1248                                         if( G.qual & LR_ALTKEY ) { 
1249                                                 G.qual &= ~LR_CTRLKEY;
1250                                                 persptoetsen(PAD6);
1251                                                 G.qual |= LR_CTRLKEY;
1252                                         } else {
1253                                                 persptoetsen(PAD6);
1254                                         }
1255                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1256                                         persptoetsen(PADPLUSKEY);
1257                                 else
1258                                         persptoetsen(PADMINUS);
1259                                 
1260                                 doredraw= 1;
1261                                 break;
1262
1263                         case ONEKEY:
1264                                 if(G.qual==LR_CTRLKEY) {
1265                                         flip_subdivison(1);
1266                                 }
1267                                 else do_layer_buttons(0); 
1268                                 break;
1269                                 
1270                         case TWOKEY:
1271                                 if(G.qual==LR_CTRLKEY) {
1272                                         flip_subdivison(2);
1273                                 }
1274                                 else do_layer_buttons(1); 
1275                                 break;
1276                                 
1277                         case THREEKEY:
1278                                 if(G.qual==LR_CTRLKEY) {
1279                                         flip_subdivison(3);
1280                                 }
1281                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1282                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1283                                                 select_faces_by_numverts(3);
1284                                 }
1285                                 else do_layer_buttons(2); 
1286                                 break;
1287                                 
1288                         case FOURKEY:
1289                                 if(G.qual==LR_CTRLKEY) {
1290                                         flip_subdivison(4);
1291                                 }
1292                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1293                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1294                                                 select_faces_by_numverts(4);
1295                                 }
1296                                 else do_layer_buttons(3); 
1297                                 break;
1298                                 
1299                         case FIVEKEY:
1300                                 if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1301                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1302                                                 select_faces_by_numverts(5);
1303                                 }
1304                                 else do_layer_buttons(4);
1305                                 break;
1306
1307                         case SIXKEY:
1308                                 do_layer_buttons(5); break;
1309                         case SEVENKEY:
1310                                 do_layer_buttons(6); break;
1311                         case EIGHTKEY:
1312                                 do_layer_buttons(7); break;
1313                         case NINEKEY:
1314                                 do_layer_buttons(8); break;
1315                         case ZEROKEY:
1316                                 do_layer_buttons(9); break;
1317                         case MINUSKEY:
1318                                 do_layer_buttons(10); break;
1319                         case EQUALKEY:
1320                                 do_layer_buttons(11); break;
1321                         case ACCENTGRAVEKEY:
1322                                 do_layer_buttons(-1); break;
1323                         
1324                         case SPACEKEY:
1325                                 if(G.qual == LR_CTRLKEY) {
1326                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1327                                         if(val>0) {
1328                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1329                                                 else {
1330                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1331                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1332                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1333                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1334                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1335                                                 }
1336                                                 doredraw= 1;
1337                                         }
1338                                 }
1339                                 else if(G.qual == LR_ALTKEY) {
1340                                         BIF_selectOrientation();
1341                                         doredraw= 1;
1342                                 }
1343
1344                                 break;
1345                                 
1346                         case AKEY:
1347                                 if(G.qual & LR_CTRLKEY) apply_object(); /* also with shift! */
1348                                 else if((G.qual==LR_SHIFTKEY)) {
1349                                         toolbox_n_add();
1350                                 }
1351                                 else {
1352                                         if(G.obedit) {
1353                                                 if(G.obedit->type==OB_MESH)
1354                                                         deselectall_mesh();
1355                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1356                                                         deselectall_nurb();
1357                                                 else if(G.obedit->type==OB_MBALL)
1358                                                         deselectall_mball();
1359                                                 else if(G.obedit->type==OB_LATTICE)
1360                                                         deselectall_Latt();
1361                                                 else if(G.obedit->type==OB_ARMATURE)
1362                                                         deselectall_armature(1);        /* 1 == toggle */
1363                                         }
1364                                         else if (ob && (ob->flag & OB_POSEMODE)){
1365                                                 deselectall_posearmature(ob, 1);
1366                                         }
1367                                         else {
1368                                                 if(G.f & G_FACESELECT) deselectall_tface();
1369                                                 else {
1370                                                         /* by design, the center of the active object 
1371                                                          * (which need not necessarily by selected) will
1372                                                          * still be drawn as if it were selected.
1373                                                          */
1374                                                         deselectall();
1375                                                 }
1376                                         }
1377                                 }
1378                                 break;
1379                         case BKEY:
1380                                 if(G.qual==LR_ALTKEY)
1381                                         view3d_edit_clipping(v3d);
1382                                 else if(G.qual==LR_SHIFTKEY)
1383                                 {
1384                                         if(G.vd->persp==2)
1385                                                 set_render_border();
1386                                         else
1387                                                 view3d_border_zoom();
1388                                 }
1389                                 else if(G.qual==LR_CTRLKEY) {
1390                                         if(okee("Bake all selected")) {
1391                                                 extern void softbody_bake(Object *ob);
1392                                                 extern void fluidsimBake(Object *ob);
1393                                                 softbody_bake(NULL);
1394                                                 /* also bake first domain of selected objects... */
1395                                                 fluidsimBake(NULL);
1396                                         }
1397                                 }
1398                                 else if(G.qual== (LR_ALTKEY|LR_CTRLKEY))
1399                                         objects_bake_render();
1400                                 else if(G.qual==0)
1401                                         borderselect();
1402                                 break;
1403                         case CKEY:
1404                                 if(G.qual==LR_CTRLKEY) {
1405                                         if(ob && (ob->flag & OB_POSEMODE))
1406                                                 pose_copy_menu();       /* poseobject.c */
1407                                         else
1408                                                 copy_attr_menu();
1409                                 }
1410                                 else if(G.qual==LR_ALTKEY) {
1411                                         if(ob && (ob->flag & OB_POSEMODE))
1412                                                 pose_clear_constraints();       /* poseobject.c */
1413                                         else
1414                                                 convertmenu();  /* editobject.c */
1415                                 }
1416                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY)) 
1417                                         add_constraint(0);      /* editconstraint.c, generic for objects and posemode */
1418                                 else if((G.qual==LR_SHIFTKEY)) {
1419                                         view3d_home(1);
1420                                         curs= give_cursor();
1421                                         curs[0]=curs[1]=curs[2]= 0.0;
1422                                         allqueue(REDRAWVIEW3D, 0);
1423                                 }
1424                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1425                                         makecyclicNurb();
1426                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1427                                         allqueue(REDRAWVIEW3D, 0);
1428                                 }
1429                                 else if((G.qual==0)){
1430                                         if (G.vd->persp==2)
1431                                                 /* center the camera offset */
1432                                                 G.vd->camdx= G.vd->camdy= 0.0;
1433                                         else {
1434                                                 /*non camera center*/
1435                                                 curs= give_cursor();
1436                                                 G.vd->ofs[0]= -curs[0];
1437                                                 G.vd->ofs[1]= -curs[1];
1438                                                 G.vd->ofs[2]= -curs[2];
1439                                         }
1440                                         scrarea_queue_winredraw(curarea);
1441                                 }
1442                                 break;
1443                         case DKEY:
1444                                 if((G.qual==LR_SHIFTKEY)) {
1445                                         duplicate_context_selected();
1446                                 }
1447                                 else if(G.qual==LR_ALTKEY) {
1448                                         if(ob && (ob->flag & OB_POSEMODE))
1449                                                 error ("Duplicate not possible in posemode.");
1450                                         else if((G.obedit==NULL))
1451                                                 adduplicate(0, 0);
1452                                 }
1453                                 else if(G.qual==LR_CTRLKEY) {
1454                                         imagestodisplist();
1455                                 }
1456                                 else if((G.qual==0)){
1457                                         if(G.f & G_SCULPTMODE)
1458                                                 sculptmode_propset_init(DKEY);
1459                                         else {
1460                                                 pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
1461                                                 if(pupval>0) {
1462                                                         G.vd->drawtype= pupval;
1463                                                         doredraw= 1;
1464                                                 }
1465                                         }
1466                                 }
1467                                 
1468                                 break;
1469                         case EKEY:
1470                                 if (G.qual==0){
1471                                         if(G.obedit) {
1472                                                 if(G.obedit->type==OB_MESH)
1473                                                         extrude_mesh();
1474                                                 else if(G.obedit->type==OB_CURVE)
1475                                                         addvert_Nurb('e');
1476                                                 else if(G.obedit->type==OB_SURF)
1477                                                         extrude_nurb();
1478                                                 else if(G.obedit->type==OB_ARMATURE)
1479                                                         extrude_armature(0);
1480                                         }
1481                                 }
1482                                 else if (G.qual==LR_CTRLKEY) {
1483                                         if(G.obedit && G.obedit->type==OB_MESH)
1484                                                 Edge_Menu();
1485                                         else if (G.f & G_FACESELECT)
1486                                                 seam_mark_clear_tface(0);
1487                                 }
1488                                 else if (G.qual==LR_SHIFTKEY) {
1489                                         if (G.obedit && G.obedit->type==OB_MESH) {
1490                                                 initTransform(TFM_CREASE, CTX_EDGE);
1491                                                 Transform();
1492                                         }
1493                                         else if (G.obedit && G.obedit->type==OB_ARMATURE) {
1494                                                 extrude_armature(1);
1495                                         }
1496                                 }
1497                                 break;
1498                         case FKEY:
1499                                 if(G.obedit) {
1500                                         if(G.obedit->type==OB_MESH) {
1501                                                 if((G.qual==LR_SHIFTKEY))
1502                                                         fill_mesh();
1503                                                 else if(G.qual==LR_ALTKEY)
1504                                                         beauty_fill();
1505                                                 else if(G.qual==LR_CTRLKEY)
1506                                                         edge_flip();
1507                                                 else if (G.qual==0)
1508                                                         addedgeface_mesh();
1509                                                 else if ( G.qual == 
1510                                                          (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1511                                                         select_linked_flat_faces();
1512                                                 }
1513
1514                                         }
1515                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
1516                                 }
1517                                 else if(G.qual==LR_CTRLKEY)
1518                                         sort_faces();
1519                                 else if((G.qual==LR_SHIFTKEY)) {
1520                                         if(ob && (ob->flag & OB_POSEMODE))
1521                                            pose_activate_flipped_bone();
1522                                         else if(G.f & G_WEIGHTPAINT)
1523                                                 pose_activate_flipped_bone();
1524                                         else
1525                                                 fly();
1526                                 }
1527                                 else {
1528                                         set_faceselect();
1529                                 }
1530                                 
1531                                 break;
1532                         case GKEY:
1533                                 if(G.qual == LR_CTRLKEY) 
1534                                         group_operation_with_menu();
1535                                 else if((G.qual==LR_SHIFTKEY))
1536                                         if(G.obedit) {
1537                                                 if(G.obedit->type==OB_MESH)
1538                                                         select_mesh_group_menu();
1539                                         } else
1540                                                 select_object_grouped_menu();
1541                                 else if(G.qual==LR_ALTKEY) {
1542                                         if(okee("Clear location")) {
1543                                                 clear_object('g');
1544                                         }
1545                                 }
1546                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1547                                         v3d->twtype= V3D_MANIP_TRANSLATE;
1548                                         doredraw= 1;
1549                                 }
1550                                 else if((G.qual==0)) {
1551                                         initTransform(TFM_TRANSLATION, CTX_NONE);
1552                                         Transform();
1553                                 }
1554                                 break;
1555                         case HKEY:
1556                                 if(G.obedit) {
1557                                         if(G.obedit->type==OB_MESH) {
1558                                                 if(G.qual==LR_CTRLKEY)
1559                                                         add_hook();
1560                                                 else if(G.qual==LR_ALTKEY)
1561                                                         reveal_mesh();
1562                                                 else if((G.qual==LR_SHIFTKEY))
1563                                                         hide_mesh(1);
1564                                                 else if((G.qual==0)) 
1565                                                         hide_mesh(0);
1566                                         }
1567                                         else if(G.obedit->type== OB_SURF) {
1568                                                 if(G.qual==LR_CTRLKEY)
1569                                                         add_hook();
1570                                                 else if(G.qual==LR_ALTKEY)
1571                                                         revealNurb();
1572                                                 else if((G.qual==LR_SHIFTKEY))
1573                                                         hideNurb(1);
1574                                                 else if((G.qual==0))
1575                                                         hideNurb(0);
1576                                         }
1577                                         else if(G.obedit->type==OB_CURVE) {
1578                                                 if(G.qual==LR_CTRLKEY)
1579                                                         add_hook();
1580                                                 else {
1581                                                         if(G.qual==LR_CTRLKEY)
1582                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
1583                                                         else if((G.qual==LR_SHIFTKEY))
1584                                                                 sethandlesNurb(1);
1585                                                         else if((G.qual==0))
1586                                                                 sethandlesNurb(3);
1587                                                         
1588                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1589                                                         BIF_undo_push("Handle change");
1590                                                         allqueue(REDRAWVIEW3D, 0);
1591                                                 }
1592                                         }
1593                                         else if(G.obedit->type==OB_LATTICE) {
1594                                                 if(G.qual==LR_CTRLKEY) add_hook();
1595                                         }
1596                                         else if(G.obedit->type==OB_MBALL) {
1597                                                 if(G.qual==LR_ALTKEY)
1598                                                         reveal_mball();
1599                                                 else if((G.qual==LR_SHIFTKEY))
1600                                                         hide_mball(1);
1601                                                 else if((G.qual==0)) 
1602                                                         hide_mball(0);
1603                                         }
1604                                         else if(G.obedit->type==OB_ARMATURE) {
1605                                                 if (G.qual==0)
1606                                                         hide_selected_armature_bones();
1607                                                 else if (G.qual==LR_SHIFTKEY)
1608                                                         hide_unselected_armature_bones();
1609                                                 else if (G.qual==LR_ALTKEY)
1610                                                         show_all_armature_bones();
1611                                         }
1612                                 }
1613                                 else if(G.f & G_FACESELECT)
1614                                         hide_tface();
1615                                 else if(G.f & G_SCULPTMODE) {
1616                                         if(G.qual==LR_ALTKEY) {
1617                                                 waitcursor(1);
1618                                                 sculptmode_pmv_off(get_mesh(ob));
1619                                                 BIF_undo_push("Partial mesh hide");
1620                                                 allqueue(REDRAWVIEW3D,0);
1621                                                 waitcursor(0);
1622                                         }
1623                                 }
1624                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1625                                         if (G.qual==0)
1626                                                 hide_selected_pose_bones();
1627                                         else if (G.qual==LR_SHIFTKEY)
1628                                                 hide_unselected_pose_bones();
1629                                         else if (G.qual==LR_ALTKEY)
1630                                                 show_all_pose_bones();
1631                                 }
1632                                 break;
1633                         case IKEY:
1634                                 if(G.obedit);
1635                                 else if(G.qual==LR_CTRLKEY) {
1636                                         if(ob && ob->type==OB_ARMATURE) 
1637                                                 if(ob->flag & OB_POSEMODE) 
1638                                                         pose_add_IK();
1639                                 }
1640                                 else if(G.qual==LR_ALTKEY) {
1641                                         if(ob && ob->type==OB_ARMATURE) 
1642                                                 if(ob->flag & OB_POSEMODE) 
1643                                                         pose_clear_IK();
1644                                 }
1645                                 break;
1646                                 
1647                         case JKEY:
1648                                 if(G.qual==LR_CTRLKEY) {
1649                                         if( ob ) {
1650                                                 join_menu();
1651                                         }
1652                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1653                                                 addsegment_nurb();
1654                                 }
1655                                 else if(G.obedit) {
1656                                         if(G.obedit->type==OB_MESH) {
1657                                                 join_triangles();
1658                                         }
1659                                 }
1660
1661                                 break;
1662                         case KKEY:
1663                                 if(G.obedit) {
1664                                         if (G.obedit->type==OB_MESH) {
1665                                                 if (G.qual==LR_SHIFTKEY)
1666                                                         KnifeSubdivide(KNIFE_PROMPT);
1667                                                 else if (G.qual==0)
1668                                                         LoopMenu();
1669                                         }
1670                                         else if(G.obedit->type==OB_SURF)
1671                                                 printknots();
1672                                 }
1673                                 else {
1674                                         if((G.qual==LR_SHIFTKEY)) {
1675                                                 if(G.f & G_FACESELECT)
1676                                                         if (G.f & G_WEIGHTPAINT)
1677                                                                 clear_wpaint_selectedfaces();
1678                                                         else
1679                                                                 clear_vpaint_selectedfaces();
1680                                                 else if(G.f & G_VERTEXPAINT)
1681                                                         clear_vpaint();
1682                                                 else
1683                                                         select_select_keys();
1684                                         }
1685                                         else if (G.qual==0)
1686                                                 set_ob_ipoflags();
1687                                 }
1688                                 
1689                                 break;
1690                         case LKEY:
1691                                 if(G.obedit) {
1692                                         if(G.obedit->type==OB_MESH)
1693                                                 selectconnected_mesh(G.qual);
1694                                         if(G.obedit->type==OB_ARMATURE)
1695                                                 selectconnected_armature();
1696                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1697                                                 selectconnected_nurb();
1698                                 }
1699                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1700                                         selectconnected_posearmature();
1701                                 }
1702                                 else {
1703                                         if(G.f & G_FACESELECT) {
1704                                                 if((G.qual==0))
1705                                                         select_linked_tfaces(0);
1706                                                 else if((G.qual==LR_SHIFTKEY))
1707                                                         select_linked_tfaces(1);
1708                                                 else if(G.qual==LR_CTRLKEY)
1709                                                         select_linked_tfaces(2);
1710                                         }
1711                                         else {
1712                                                 if((G.qual==0))
1713                                                         make_local_menu();
1714                                                 else if((G.qual==LR_SHIFTKEY))
1715                                                         selectlinks_menu();
1716                                                 else if(G.qual==LR_CTRLKEY)
1717                                                         make_links_menu();
1718                                         }
1719                                 }
1720                                 break;
1721                         case MKEY:
1722                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1723                                         mirror_uv_tface();
1724                                 else if(G.obedit){
1725                                         if(G.qual==LR_ALTKEY) {
1726                                                 if(G.obedit->type==OB_MESH) {
1727                                                         mergemenu();
1728                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1729                                                 }
1730                                         }
1731                                         else if((G.qual==0) || (G.qual==LR_CTRLKEY)) {
1732                                                 mirrormenu();
1733                                         }
1734                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1735                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
1736                                         }
1737                                 }
1738                                 else if(G.qual & LR_CTRLKEY) {
1739                                         mirrormenu();
1740                                 }
1741                                 else if(G.qual==0 || G.qual==LR_SHIFTKEY) {
1742                                         if(ob && (ob->flag & OB_POSEMODE))
1743                                                 pose_movetolayer();
1744                                         else
1745                                                 movetolayer();
1746                                 }
1747                                 break;
1748                         case NKEY:
1749                                 if((G.qual==0)) {
1750                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1751                                         allqueue(REDRAWVIEW3D, 0);
1752                                 }
1753                                 else if(G.obedit) {
1754                                         switch (G.obedit->type){
1755                                         case OB_ARMATURE:
1756                                                 if(G.qual==LR_CTRLKEY){
1757                                                         if (okee("Recalculate bone roll angles")) {
1758                                                                 auto_align_armature();
1759                                                                 allqueue(REDRAWVIEW3D, 0);
1760                                                         }
1761                                                 }
1762                                                 break;
1763                                         case OB_MESH: 
1764                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1765                                                         if(okee("Recalculate normals inside")) {
1766                                                                 righthandfaces(2);
1767                                                                 allqueue(REDRAWVIEW3D, 0);
1768                                                                 BIF_undo_push("Recalculate normals inside");
1769                                                         }
1770                                                 }
1771                                                 else if(G.qual==LR_CTRLKEY){
1772                                                         if(okee("Recalculate normals outside")) {
1773                                                                 righthandfaces(1);
1774                                                                 allqueue(REDRAWVIEW3D, 0);
1775                                                                 BIF_undo_push("Recalculate normals outside");
1776                                                         }
1777                                                 }
1778                                                 break;
1779                                         }
1780                                 }
1781                                 
1782                                 break;
1783                         case OKEY:
1784                                 if (G.obedit) {
1785                                         if (G.qual==LR_SHIFTKEY) {
1786                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%7;
1787                                                 allqueue(REDRAWHEADERS, 0);
1788                                         }
1789                                         else if((G.qual==LR_ALTKEY)) {
1790                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
1791                                                 else G.scene->proportional= 2;
1792                                                 allqueue(REDRAWHEADERS, 0);
1793                                         }
1794                                         else if((G.qual==0)) {
1795                                                 G.scene->proportional= !G.scene->proportional;
1796                                                 allqueue(REDRAWHEADERS, 0);
1797                                         }
1798                                 }
1799                                 else if((G.qual==LR_SHIFTKEY || G.qual==(LR_ALTKEY|LR_SHIFTKEY))) {
1800                                         flip_subdivison(-1);
1801                                 }
1802                                 else if(G.qual==LR_ALTKEY) {
1803                                         if(okee("Clear origin")) {
1804                                                 clear_object('o');
1805                                         }
1806                                 }
1807                                 break;
1808
1809                         case PKEY:
1810                                 if(G.obedit) {
1811                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1812                                                 if(G.obedit->type==OB_ARMATURE)
1813                                                         make_bone_parent();
1814                                                 else
1815                                                         make_parent();
1816                                         }
1817                                         
1818                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
1819                                                 clear_bone_parent();
1820                                         else if((G.qual==0) && (G.obedit->type==OB_ARMATURE)) 
1821                                                 select_bone_parent();
1822                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
1823                                                 separatemenu();
1824                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1825                                                 separate_nurb();
1826                                         else if (G.qual==LR_SHIFTKEY) {
1827                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
1828                                                 Transform();
1829                                         }
1830                                 }
1831                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
1832                                         make_parent();
1833                                 else if(G.qual==LR_SHIFTKEY) {
1834                                         if(G.obedit) {
1835                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
1836                                                 Transform();
1837                                         }
1838                                         else {
1839                                                 toggle_blockhandler(curarea, VIEW3D_HANDLER_PREVIEW, 0);
1840                                                 doredraw= 1;
1841                                         }
1842                                 }
1843                                 else if(G.qual==LR_ALTKEY)
1844                                         clear_parent();
1845                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY))
1846                                         make_proxy();
1847                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY|LR_SHIFTKEY)) {
1848                         start_RBSimulation();
1849                                 }
1850                                 else if((G.qual==0) && (OBACT) && (OBACT->type==OB_ARMATURE) && (OBACT->flag & OB_POSEMODE))
1851                                         select_bone_parent();
1852                                 else if((G.qual==0)) {
1853                         start_game();
1854                                 }
1855                                 break;                          
1856                         case RKEY:
1857                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1858                                         rotate_uv_tface();
1859                                 else if(G.qual==LR_ALTKEY) {
1860                                         if(okee("Clear rotation")) {
1861                                                 clear_object('r');
1862                                         }
1863                                 } 
1864                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1865                                         v3d->twtype= V3D_MANIP_ROTATE;
1866                                         doredraw= 1;
1867                                 }
1868                                 else if (G.obedit) {
1869                                         if((G.qual==LR_SHIFTKEY)) {
1870                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
1871                                                         selectrow_nurb();
1872                                         }
1873                                         else if(G.qual==LR_CTRLKEY) {
1874                                                 if (G.obedit->type==OB_MESH)
1875                                                         CutEdgeloop(1);
1876                                                         BIF_undo_push("Cut Edgeloop");
1877                                         }
1878                                         else if((G.qual==0)) {
1879                                                 initTransform(TFM_ROTATION, CTX_NONE);
1880                                                 Transform();
1881                                         }
1882                                 }
1883                                 else if((G.qual==0)) {
1884                                         initTransform(TFM_ROTATION, CTX_NONE);
1885                                         Transform();
1886                                 }
1887                                 break;
1888                         case SKEY:
1889                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1890                                         v3d->twtype= V3D_MANIP_SCALE;
1891                                         doredraw= 1;
1892                                 }
1893                                 else if(G.obedit) {
1894                                         
1895                                         if(G.qual==LR_ALTKEY) {
1896                                                 if(G.obedit->type==OB_ARMATURE) {
1897                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1898                                                 }
1899                                                 else if (G.obedit->type==OB_CURVE) {
1900                                                         initTransform(TFM_CURVE_SHRINKFATTEN, CTX_NONE);
1901                                                 } else {
1902                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
1903                                                 }
1904                                                 Transform();
1905                                         }
1906                                         else if(G.qual==LR_CTRLKEY) {
1907                                                 initTransform(TFM_SHEAR, CTX_NONE);
1908                                                 Transform();
1909                                         }
1910                                         else if(G.qual==LR_SHIFTKEY)
1911                                                 snapmenu();
1912                                         else if(G.qual==0) {
1913                                                 if(G.obedit->type==OB_ARMATURE) {
1914                                                         bArmature *arm= G.obedit->data;
1915                                                         if(arm->drawtype==ARM_ENVELOPE)
1916                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
1917                                                         else
1918                                                                 initTransform(TFM_RESIZE, CTX_NONE);
1919                                                 }
1920                                                 else
1921                                                         initTransform(TFM_RESIZE, CTX_NONE);
1922                                                 Transform();
1923                                         }
1924                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
1925                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
1926                                                 Transform();
1927                                         }
1928                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1929                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
1930                                         }
1931                                 }
1932                                 else if(G.qual==LR_ALTKEY) {
1933                                         if(G.f & G_WEIGHTPAINT)
1934                                                 ob= ob->parent;
1935                                         if(ob && (ob->flag & OB_POSEMODE)) {
1936                                                 bArmature *arm= ob->data;
1937                                                 if( ELEM(arm->drawtype, ARM_B_BONE, ARM_ENVELOPE)) {
1938                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1939                                                         Transform();
1940                                                         break;
1941                                                 }
1942                                         }
1943                                         
1944                                         if(okee("Clear scale")) {
1945                                                 clear_object('s');
1946                                         }
1947                                 }
1948                                 else if(G.qual==LR_SHIFTKEY) {
1949                                         snapmenu();
1950                                 }
1951                                 else if((G.qual==0)) {
1952                                         initTransform(TFM_RESIZE, CTX_NONE);
1953                                         Transform();
1954                                 }
1955                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1956                                         initTransform(TFM_TOSPHERE, CTX_NONE);
1957                                         Transform();
1958                                 }
1959                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
1960                                         initTransform(TFM_SHEAR, CTX_NONE);
1961                                         Transform();
1962                                 }
1963                                 break;
1964                         case TKEY:
1965                                 if(G.obedit){
1966                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
1967                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
1968                                                 allqueue(REDRAWVIEW3D, 0);
1969                                                 countall();
1970                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1971                                         }
1972                                         if (G.obedit->type==OB_CURVE) {
1973                                                 if (G.qual==LR_ALTKEY) {
1974                                                         clear_tilt();
1975                                                 }
1976                                                 else if (G.qual==0) {
1977                                                         initTransform(TFM_TILT, CTX_NONE);
1978                                                         Transform();
1979                                                 }
1980                                         }
1981                                 }
1982                                 else if(G.qual==LR_CTRLKEY) {
1983                                         if(ob && (ob->flag & OB_POSEMODE));
1984                                         else make_track();
1985                                 }
1986                                 else if(G.qual==LR_ALTKEY) {
1987                                         if(ob && (ob->flag & OB_POSEMODE));
1988                                         else clear_track();
1989                                 }
1990                                 else if((G.qual==0)){
1991                                         texspace_edit();
1992                                 }
1993                                 
1994                                 break;
1995                         case UKEY:
1996                                 if(G.obedit) {
1997                                         if(G.obedit->type==OB_MESH) {
1998                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1999                                         }
2000                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
2001                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2002                                         }
2003                                 }
2004                                 else if((G.qual==0)) {
2005                                         if(G.f & G_WEIGHTPAINT)
2006                                                 BIF_undo();
2007                                         else if(G.f & G_VERTEXPAINT)
2008                                                 BIF_undo();
2009                                         else if(G.f & G_TEXTUREPAINT)
2010                                                 imagepaint_undo();
2011                                         else if (G.f & G_FACESELECT)
2012                                                 uv_autocalc_tface();
2013                                         else {
2014                                                 single_user();
2015                                         }
2016                                 }
2017                                         
2018                                 break;
2019                         case VKEY:
2020                                 if((G.qual==LR_SHIFTKEY)) {
2021                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
2022                                                 align_view_to_selected(v3d);
2023                                         }
2024                                         else if (G.f & G_FACESELECT) {
2025                                                 align_view_to_selected(v3d);
2026                                         }
2027                                 }
2028                                 else if(G.qual==LR_ALTKEY)
2029                                         image_aspect();
2030                                 else if (G.qual==0){
2031                                         if(G.obedit) {
2032                                                 if(G.obedit->type==OB_MESH) {
2033                                                         mesh_rip();
2034                                                 }
2035                                                 else if(G.obedit->type==OB_CURVE) {
2036                                                         sethandlesNurb(2);
2037                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2038                                                         allqueue(REDRAWVIEW3D, 0);
2039                                                         BIF_undo_push("Handle change");
2040                                                 }
2041                                         }
2042                                         else if(ob && ob->type == OB_MESH) 
2043                                                 set_vpaint();
2044                                 }
2045                                 break;
2046                         case WKEY:
2047                                 if((G.qual==LR_SHIFTKEY)) {
2048                                         initTransform(TFM_WARP, CTX_NONE);
2049                                         Transform();
2050                                 }
2051                                 else if(G.qual==LR_ALTKEY) {
2052                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
2053                                 }
2054                                 else if(G.qual==LR_CTRLKEY) {
2055                                         if(G.obedit) {
2056                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
2057                                                         switchdirectionNurb2();
2058                                                 }
2059                                         }
2060                                 }
2061                                 else if((G.qual==0))
2062                                         special_editmenu();
2063                                 
2064                                 break;
2065                         case XKEY:
2066                         case DELKEY:
2067                                 if(G.qual==0 || G.qual==LR_SHIFTKEY)
2068                                         delete_context_selected();
2069                                 break;
2070                         case YKEY:
2071                                 if((G.qual==0) && (G.obedit)) {
2072                                         if(G.obedit->type==OB_MESH) split_mesh();
2073                                 }
2074                                 break;
2075                         case ZKEY:
2076                                 toggle_shading();
2077                                 
2078                                 scrarea_queue_headredraw(curarea);
2079                                 scrarea_queue_winredraw(curarea);
2080                                 break;
2081                         
2082                         case HOMEKEY:
2083                                 if(G.qual==0)
2084                                         view3d_home(0);
2085                                 break;
2086                         case COMMAKEY:
2087                                 if(G.qual==LR_CTRLKEY) {
2088                                         G.vd->around= V3D_CENTROID;
2089                                 } else if(G.qual==LR_SHIFTKEY) {
2090                                         G.vd->around= V3D_CENTROID;
2091                                 } else if(G.qual==0) {
2092                                         G.vd->around= V3D_CENTRE;
2093                                 }
2094                                 handle_view3d_around();
2095                                 
2096                                 scrarea_queue_headredraw(curarea);
2097                                 scrarea_queue_winredraw(curarea);
2098                                 break;
2099                                 
2100                         case PERIODKEY:
2101                                 if(G.qual==LR_CTRLKEY) {
2102                                         G.vd->around= V3D_LOCAL;
2103                                 }       else if(G.qual==0) {
2104                                         G.vd->around= V3D_CURSOR;
2105                                 }
2106                                 handle_view3d_around();
2107                                 
2108                                 scrarea_queue_headredraw(curarea);
2109                                 scrarea_queue_winredraw(curarea);
2110                                 break;
2111                         
2112                         case PADSLASHKEY:
2113                                 if(G.qual==0) {
2114                                         if(G.vd->localview) {
2115                                                 G.vd->localview= 0;
2116                                                 endlocalview(curarea);
2117                                         }
2118                                         else {
2119                                                 G.vd->localview= 1;
2120                                                 initlocalview();
2121                                         }
2122                                         scrarea_queue_headredraw(curarea);
2123                                 }
2124                                 break;
2125                         case PADASTERKEY:       /* '*' */
2126                                 if(G.qual==0) {
2127                                         if(ob) {
2128                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
2129                                                         editmesh_align_view_to_selected(G.vd, 2);
2130                                                 } 
2131                                                 else if (G.f & G_FACESELECT) {
2132                                                         if(ob->type==OB_MESH) {
2133                                                                 Mesh *me= ob->data;
2134                                                                 faceselect_align_view_to_selected(G.vd, me, 2);
2135                                                         }
2136                                                 }
2137                                                 else
2138                                                         obmat_to_viewmat(ob);
2139                                                 
2140                                                 if(G.vd->persp==2) G.vd->persp= 1;
2141                                                 scrarea_queue_winredraw(curarea);
2142                                         }
2143                                 }
2144                                 break;
2145                         case PADPERIOD: /* '.' */
2146                                 if(G.qual==0)
2147                                         centreview();
2148                                 break;
2149                         
2150                         case PAGEUPKEY:
2151                                 if(G.f & G_SCULPTMODE) {
2152                                         if(ob && ob->type == OB_MESH && ((Mesh*)ob->data)->mr) {
2153                                                 ((Mesh*)ob->data)->mr->newlvl= ((Mesh*)ob->data)->mr->current+1;
2154                                                 multires_set_level(ob,ob->data);
2155                                         }
2156                                 } else {
2157                                         if(G.qual==LR_CTRLKEY)
2158                                                 movekey_obipo(1);
2159                                         else if((G.qual==0))
2160                                                 nextkey_obipo(1);       /* in editipo.c */
2161                                 }
2162                                 break;
2163
2164                         case PAGEDOWNKEY:
2165                                 if(G.f & G_SCULPTMODE) {
2166                                         if(ob && ob->type == OB_MESH && ((Mesh*)ob->data)->mr) {
2167                                                 ((Mesh*)ob->data)->mr->newlvl= ((Mesh*)ob->data)->mr->current-1;
2168                                                 multires_set_level(ob,ob->data);
2169                                         }
2170                                 } else {
2171                                         if(G.qual==LR_CTRLKEY)
2172                                                 movekey_obipo(-1);
2173                                         else if((G.qual==0))
2174                                                 nextkey_obipo(-1);
2175                                 }
2176                                 break;
2177
2178                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
2179                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
2180                         case PADENTER:
2181                                 persptoetsen(event);
2182                                 doredraw= 1;
2183                                 break;
2184                         case PADMINUS:
2185                                 if ( (G.qual==LR_CTRLKEY)
2186                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2187                                         select_less();
2188                                 else if ( (G.qual==LR_CTRLKEY)
2189                                          && (G.obedit) && (G.obedit->type==OB_CURVE) )
2190                                         select_less_nurb();
2191                                 /*
2192                                 else if ( (G.qual==LR_CTRLKEY)
2193                                          && (G.obedit) && (G.obedit->type==OB_SURF) )
2194                                         select_less_nurb(); 
2195                                 */
2196                                 else {
2197                                         persptoetsen(event);
2198                                         doredraw= 1;
2199                                 }
2200                                 break;
2201
2202                         case PADPLUSKEY:
2203                                 if ( (G.qual==LR_CTRLKEY)
2204                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2205                                         select_more();
2206                                 else if ( (G.qual==LR_CTRLKEY)
2207                                          && (G.obedit) && (G.obedit->type==OB_CURVE) )
2208                                         select_more_nurb();
2209                                 /*
2210                                 else if ( (G.qual==LR_CTRLKEY)
2211                                          && (G.obedit) && (G.obedit->type==OB_SURF) )
2212                                         select_more_nurb();
2213                                 */
2214                                 else {
2215                                         persptoetsen(event);
2216                                         doredraw= 1;
2217                                 }
2218                                 break;
2219
2220                         case ESCKEY:
2221                                 if(G.qual==0) {
2222                                         if (G.vd->flag & V3D_DISPIMAGE) {
2223                                                 G.vd->flag &= ~V3D_DISPIMAGE;
2224                                                 doredraw= 1;
2225                                         }
2226                                 }
2227                                 break;
2228                         }
2229                 }
2230         }
2231         
2232         if(doredraw) {
2233                 scrarea_queue_winredraw(curarea);
2234                 scrarea_queue_headredraw(curarea);
2235         }
2236 }
2237
2238 static void initview3d(ScrArea *sa)
2239 {
2240         View3D *vd;
2241         
2242         vd= MEM_callocN(sizeof(View3D), "initview3d");
2243         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
2244
2245         vd->spacetype= SPACE_VIEW3D;
2246         vd->blockscale= 0.7f;
2247         vd->viewquat[0]= 1.0f;
2248         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
2249         vd->persp= 1;
2250         vd->drawtype= OB_WIRE;
2251         vd->view= 7;
2252         vd->dist= 10.0;
2253         vd->lens= 35.0f;
2254         vd->near= 0.01f;
2255         vd->far= 500.0f;
2256         vd->grid= 1.0f;
2257         vd->gridlines= 16;
2258         vd->lay= vd->layact= 1;
2259         if(G.scene) {
2260                 vd->lay= vd->layact= G.scene->lay;
2261                 vd->camera= G.scene->camera;
2262         }
2263         vd->scenelock= 1;
2264         vd->gridflag |= V3D_SHOW_X;
2265         vd->gridflag |= V3D_SHOW_Y;
2266         vd->gridflag |= V3D_SHOW_FLOOR;
2267         vd->gridflag &= ~V3D_SHOW_Z;
2268
2269         vd->depths= NULL;
2270 }
2271
2272
2273 /* ******************** SPACE: IPO ********************** */
2274
2275 static void changeview2dspace(ScrArea *sa, void *spacedata)
2276 {
2277         if(G.v2d==0) return;
2278
2279         test_view2d(G.v2d, curarea->winx, curarea->winy);
2280         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
2281 }
2282
2283 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2284 {
2285         extern void do_ipobuts(unsigned short event);   /* drawipo.c */
2286         unsigned short event= evt->event;
2287         short val= evt->val;
2288         SpaceIpo *sipo= curarea->spacedata.first;
2289         View2D *v2d= &sipo->v2d;
2290         float dx, dy;
2291         int cfra, doredraw= 0;
2292         short mval[2];
2293         short mousebut = L_MOUSE;
2294         
2295         if(sa->win==0) return;
2296
2297         if(val) {
2298                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
2299
2300                 /* swap mouse buttons based on user preference */
2301                 if (U.flag & USER_LMOUSESELECT) {
2302                         if (event == LEFTMOUSE) {
2303                                 event = RIGHTMOUSE;
2304                                 mousebut = L_MOUSE;
2305                         } else if (event == RIGHTMOUSE) {
2306                                 event = LEFTMOUSE;
2307                                 mousebut = R_MOUSE;
2308                         }
2309                 }
2310
2311                 switch(event) {
2312                 case UI_BUT_EVENT:
2313                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
2314                         if(val>0 && val < 256) do_ipowin_buts(val-1);
2315                         else do_ipobuts(val);
2316                         break;
2317                         
2318                 case LEFTMOUSE:
2319                         if( in_ipo_buttons() ) {
2320                                 do_ipo_selectbuttons();
2321                                 doredraw= 1;
2322                         }
2323                         else if(view2dmove(LEFTMOUSE)); /* only checks for sliders */
2324                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
2325                         else {
2326                                 do {
2327                                         getmouseco_areawin(mval);
2328                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
2329                                         
2330                                         cfra= (int)dx;
2331                                         if(cfra< 1) cfra= 1;
2332                                         
2333                                         if( cfra!=CFRA ) {
2334                                                 CFRA= cfra;
2335                                                 update_for_newframe_nodraw(0);  /* 1 = nosound */
2336                                                 force_draw_all(0); /* To make constraint sliders redraw */
2337                                         }
2338                                         else PIL_sleep_ms(30);
2339                                 
2340                                 } while(get_mbut() & mousebut);
2341                         }
2342                         break;
2343                 case RIGHTMOUSE:
2344                         mouse_select_ipo();
2345                         allqueue (REDRAWACTION, 0);
2346                         allqueue(REDRAWNLA, 0);
2347                         break;
2348                 case MIDDLEMOUSE:
2349                         if(in_ipo_buttons()) {
2350                                 scroll_ipobuts();
2351                         }
2352                         else view2dmove(event); /* in drawipo.c */
2353                         break;
2354                 case WHEELUPMOUSE:
2355                 case WHEELDOWNMOUSE:
2356                         view2dmove(event);      /* in drawipo.c */
2357                         break;
2358                 case PADPLUSKEY:
2359                         view2d_zoom(v2d, 0.1154f, sa->winx, sa->winy);
2360                         doredraw= 1;
2361                         break;
2362                 case PADMINUS:
2363                         view2d_zoom(v2d, -0.15f, sa->winx, sa->winy);
2364                         doredraw= 1;
2365                         break;
2366                 case PAGEUPKEY:
2367                         if(G.qual==LR_CTRLKEY)
2368                                 movekey_ipo(1);
2369                         else if((G.qual==0))
2370                                 nextkey_ipo(1);
2371                         break;
2372                 case PAGEDOWNKEY:
2373                         if(G.qual==LR_CTRLKEY)
2374                                 movekey_ipo(-1);
2375                         else if((G.qual==0))
2376                                 nextkey_ipo(-1);
2377                         break;
2378                 case HOMEKEY:
2379                         if((G.qual==0))
2380                                 do_ipo_buttons(B_IPOHOME);
2381                         break;
2382                         
2383                 case AKEY:
2384                         if((G.qual==0)) {
2385                                 if(in_ipo_buttons()) {
2386                                         swap_visible_editipo();
2387                                 }
2388                                 else {
2389                                         swap_selectall_editipo();
2390                                 }
2391                                 allspace (REMAKEIPO, 0);
2392                                 allqueue (REDRAWNLA, 0);
2393                                 allqueue (REDRAWACTION, 0);
2394                         }
2395                         break;
2396                 case BKEY:
2397                         if((G.qual==0))
2398                                 borderselect_ipo();
2399                         break;
2400                 case CKEY:
2401                         if((G.qual==0))
2402                                 move_to_frame();
2403                         break;
2404                 case DKEY:
2405                         if((G.qual==LR_SHIFTKEY))
2406                                 add_duplicate_editipo();
2407                         break;
2408                 case GKEY:
2409                         if((G.qual==0))
2410                                 transform_ipo('g');
2411                         break;
2412                 case HKEY:
2413                         if(G.qual==LR_ALTKEY)
2414                                 sethandles_ipo(HD_AUTO_ANIM);
2415                         if(G.qual==LR_SHIFTKEY)
2416                                 sethandles_ipo(HD_AUTO);
2417                         else if(G.qual==0)
2418                                 sethandles_ipo(HD_ALIGN);
2419                         break;
2420                 case JKEY:
2421                         if((G.qual==LR_CTRLKEY))
2422                                 join_ipo_menu();
2423                         break;
2424                 case KKEY:
2425                         if((G.qual==0)) {
2426                                 ipo_toggle_showkey();
2427                                 scrarea_queue_headredraw(curarea);
2428                                 allqueue(REDRAWVIEW3D, 0);
2429                                 doredraw= 1;
2430                         }
2431                         break;
2432                 case MKEY:
2433                         if((G.qual==0))
2434                                 ipo_mirror_menu();
2435                         break;
2436                 case NKEY:
2437                         toggle_blockhandler(sa, IPO_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2438                         doredraw= 1;
2439                         break;
2440                 case OKEY: 
2441                         if(G.qual==LR_SHIFTKEY) 
2442                                 smooth_ipo();
2443                         else
2444                                 clean_ipo(sipo->ipo, 1);
2445                         break;
2446                 case RKEY:
2447                         if((G.qual==0))
2448                                 ipo_record();
2449                         break;
2450                 case SKEY:
2451                         if((G.qual==LR_SHIFTKEY)) {             
2452                                 ipo_snap_menu();
2453                         } else if((G.qual==0))
2454                                 transform_ipo('s');
2455                         break;
2456                 case TKEY:
2457                         if((G.qual==0))
2458                                 set_ipotype();
2459                         break;
2460                 case VKEY:
2461                         if((G.qual==0))
2462                                 sethandles_ipo(HD_VECT);
2463                         break;
2464                 case XKEY:
2465                 case DELKEY:
2466                         del_ipo();
2467                         break;
2468                 }
2469         }
2470
2471         if(doredraw) scrarea_queue_winredraw(sa);
2472 }
2473
2474 void initipo(ScrArea *sa)
2475 {
2476         SpaceIpo *sipo;
2477         
2478         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
2479         BLI_addhead(&sa->spacedata, sipo);
2480
2481         sipo->spacetype= SPACE_IPO;
2482         sipo->blockscale= 0.7f;
2483         
2484         /* sipo space loopt van (0,-?) tot (??,?) */
2485         sipo->v2d.tot.xmin= 0.0;
2486         sipo->v2d.tot.ymin= -10.0;
2487         sipo->v2d.tot.xmax= G.scene->r.efra;
2488         sipo->v2d.tot.ymax= 10.0;
2489
2490         sipo->v2d.cur= sipo->v2d.tot;
2491
2492         sipo->v2d.min[0]= 0.01f;
2493         sipo->v2d.min[1]= 0.01f;
2494
2495         sipo->v2d.max[0]= 15000.0f;
2496         sipo->v2d.max[1]= 10000.0f;
2497         
2498         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
2499         sipo->v2d.keeptot= 0;
2500
2501         sipo->blocktype= ID_OB;
2502 }
2503
2504 /* ******************** SPACE: INFO ********************** */
2505
2506 void space_mipmap_button_function(int event) {
2507         set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
2508
2509         allqueue(REDRAWVIEW3D, 0);
2510 }
2511
2512 #if 0
2513 static void space_sound_button_function(int event)
2514 {
2515         int a;
2516         SYS_SystemHandle syshandle;
2517
2518         if ((syshandle = SYS_GetSystem()))
2519         {
2520                 a = (U.gameflags & USER_DISABLE_SOUND);
2521                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
2522         }
2523 }
2524 #endif
2525
2526 /* needed for event; choose new 'curmain' resets it... */
2527 static short th_curcol= TH_BACK;
2528 static char *th_curcol_ptr= NULL;
2529 static char th_curcol_arr[4]={0, 0, 0, 255};
2530
2531 static void info_user_themebuts(uiBlock *block, short y1, short y2, short y3)
2532 {
2533         bTheme *btheme, *bt;
2534         int spacetype= 0;
2535         static short cur=1, curmain=2;
2536         short a, tot=0, isbuiltin= 0;
2537         char string[21*32], *strp, *col;
2538         
2539         y3= y2+23;      /* exception! */
2540         
2541         /* count total, max 16! */
2542         for(bt= U.themes.first; bt; bt= bt->next) tot++;
2543         
2544         /* if cur not is 1; move that to front of list */
2545         if(cur!=1) {
2546                 a= 1;
2547                 for(bt= U.themes.first; bt; bt= bt->next, a++) {
2548                         if(a==cur) {
2549                                 BLI_remlink(&U.themes, bt);
2550                                 BLI_addhead(&U.themes, bt);
2551                                 allqueue(REDRAWALL, 0);
2552                                 cur= 1;
2553                                 break;
2554                         }
2555                 }
2556         }
2557         
2558         /* the current theme */
2559         btheme= U.themes.first;
2560         if(strcmp(btheme->name, "Default")==0) isbuiltin= 1;
2561
2562         /* construct popup script */
2563         string[0]= 0;
2564         for(bt= U.themes.first; bt; bt= bt->next) {
2565                 strcat(string, bt->name);
2566                 if(btheme->next) strcat(string, "   |");
2567         }
2568         uiDefButS(block, MENU, B_UPDATE_THEME, string,                  45,y3,200,20, &cur, 0, 0, 0, 0, "Current theme");
2569         
2570         /* add / delete / name */
2571
2572         if(tot<16)
2573                 uiDefBut(block, BUT, B_ADD_THEME, "Add",        45,y2,200,20, NULL, 0, 0, 0, 0, "Makes new copy of this theme");
2574         if(tot>1 && isbuiltin==0)
2575                 uiDefBut(block, BUT, B_DEL_THEME, "Delete", 45,y1,200,20, NULL, 0, 0, 0, 0, "Delete theme");
2576
2577         if(isbuiltin) return;
2578         
2579         /* name */
2580         uiDefBut(block, TEX, B_NAME_THEME, "",                  255,y3,200,20, btheme->name, 1.0, 30.0, 0, 0, "Rename theme");
2581
2582         /* main choices pup */
2583         uiDefButS(block, MENU, B_CHANGE_THEME, "UI and Buttons %x1|%l|3D View %x2|%l|Ipo Curve Editor %x3|Action Editor %x4|"
2584                 "NLA Editor %x5|%l|UV/Image Editor %x6|Video Sequence Editor %x7|Node Editor %x16|Timeline %x15|Audio Window %x8|Text Editor %x9|%l|User Preferences %x10|"
2585                 "Outliner %x11|Buttons Window %x12|%l|File Browser %x13|Image Browser %x14",
2586                                                                                                         255,y2,200,20, &curmain, 0, 0, 0, 0, "Specify theme for...");
2587         if(curmain==1) spacetype= 0;
2588         else if(curmain==2) spacetype= SPACE_VIEW3D;
2589         else if(curmain==3) spacetype= SPACE_IPO;
2590         else if(curmain==4) spacetype= SPACE_ACTION;
2591         else if(curmain==5) spacetype= SPACE_NLA;
2592         else if(curmain==6) spacetype= SPACE_IMAGE;
2593         else if(curmain==7) spacetype= SPACE_SEQ;
2594         else if(curmain==8) spacetype= SPACE_SOUND;
2595         else if(curmain==9) spacetype= SPACE_TEXT;
2596         else if(curmain==10) spacetype= SPACE_INFO;
2597         else if(curmain==11) spacetype= SPACE_OOPS;
2598         else if(curmain==12) spacetype= SPACE_BUTS;
2599         else if(curmain==13) spacetype= SPACE_FILE;
2600         else if(curmain==14) spacetype= SPACE_IMASEL;
2601         else if(curmain==15) spacetype= SPACE_TIME;
2602         else if(curmain==16) spacetype= SPACE_NODE;
2603         else return; /* only needed while coding... when adding themes for more windows */
2604         
2605         /* color choices pup */
2606         if(curmain==1) {
2607                 strp= BIF_ThemeColorsPup(0);
2608                 if(th_curcol==TH_BACK) th_curcol= TH_BUT_OUTLINE;  /* switching main choices... */
2609         }
2610         else strp= BIF_ThemeColorsPup(spacetype);
2611         
2612         uiDefButS(block, MENU, B_REDR, strp,                    255,y1,200,20, &th_curcol, 0, 0, 0, 0, "Current color");
2613         MEM_freeN(strp);
2614         
2615         th_curcol_ptr= col= BIF_ThemeGetColorPtr(btheme, spacetype, th_curcol);
2616         if(col==NULL) return;
2617         
2618         /* first handle exceptions, special single values, row selection, etc */
2619         if(th_curcol==TH_VERTEX_SIZE) {
2620                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Vertex size ", 465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2621         }
2622         else if(th_curcol==TH_FACEDOT_SIZE) {
2623                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Face dot size ",       465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2624         }
2625         else if(th_curcol==TH_BUT_DRAWTYPE) {
2626                 uiBlockBeginAlign(block);
2627                 uiDefButC(block, ROW, B_UPDATE_THEME, "Minimal", 465,y3,100,20,  col, 2.0, (float) TH_MINIMAL, 0, 0, "");
2628                 uiDefButC(block, ROW, B_UPDATE_THEME, "Shaded", 565,y3,100,20,  col, 2.0, (float) TH_SHADED, 0, 0, "");
2629                 uiDefButC(block, ROW, B_UPDATE_THEME, "Rounded", 465,y2,100,20,  col, 2.0, (float) TH_ROUNDED, 0, 0, "");
2630                 uiDefButC(block, ROW, B_UPDATE_THEME, "OldSkool", 565,y2,100,20,  col, 2.0, (float) TH_OLDSKOOL, 0, 0, "");
2631                 uiBlockEndAlign(block);
2632         }
2633         else {
2634                 uiBlockBeginAlign(block);
2635                 if ELEM8(th_curcol, TH_PANEL, TH_LAMP, TH_FACE, TH_FACE_SELECT, TH_MENU_BACK, TH_MENU_HILITE, TH_MENU_ITEM, TH_NODE) {
2636                         uiDefButC(block, NUMSLI, B_UPDATE_THEME,"A ",   465,y3+25,200,20,  col+3, 0.0, 255.0, B_THEMECOL, 0, "");
2637                 }
2638                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"R ",   465,y3,200,20,  col, 0.0, 255.0, B_THEMECOL, 0, "");
2639                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"G ",   465,y2,200,20,  col+1, 0.0, 255.0, B_THEMECOL, 0, "");
2640                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"B ",   465,y1,200,20,  col+2, 0.0, 255.0, B_THEMECOL, 0, "");
2641                 uiBlockEndAlign(block);
2642
2643                 uiDefButC(block, COL, B_UPDATE_THEME, "",               675,y1,50,y3-y1+20, col, 0, 0, 0, 0, "");
2644                                 
2645                 /* copy paste */
2646                 uiBlockBeginAlign(block);
2647                 uiDefBut(block, BUT, B_THEME_COPY, "Copy Color",        755,y2,120,20, NULL, 0, 0, 0, 0, "Stores current color in buffer");
2648                 uiDefBut(block, BUT, B_THEME_PASTE, "Paste Color",      755,y1,120,20, NULL, 0, 0, 0, 0, "Pastes buffer color");
2649                 uiBlockEndAlign(block);
2650                 
2651                 uiDefButC(block, COL, 0, "",                            885,y1,50,y2-y1+20, th_curcol_arr, 0, 0, 0, 0, "");
2652                 
2653         }
2654 }
2655
2656
2657 void drawinfospace(ScrArea *sa, void *spacedata)
2658 {
2659         uiBlock *block;
2660         static short cur_light=0;
2661         float fac, col[3];
2662         short xpos, ypos, ypostab,  buth, rspace, dx, y1, y2, y3, y4, y5, y6;
2663         short y2label, y3label, y4label, y5label, y6label;
2664         short spref, mpref, lpref, smfileselbut;
2665         short edgsp, midsp;
2666         char naam[32];
2667
2668         if(curarea->win==0 || curarea->winy<2) return;
2669
2670         BIF_GetThemeColor3fv(TH_BACK, col);
2671         glClearColor(col[0], col[1], col[2], 0.0);
2672         glClear(GL_COLOR_BUFFER_BIT);
2673
2674         if(curarea->winx<=1280.0) {
2675                 fac= ((float)curarea->winx)/1280.0f;
2676                 myortho2(0.375f, 1280.375f, 0.375f, curarea->winy/fac + 0.375f);
2677         }
2678         else {
2679                 myortho2(0.375f, (float)curarea->winx + 0.375f, 0.375f, (float)curarea->winy + 0.375f);
2680         }
2681         
2682         sprintf(naam, "infowin %d", curarea->win);
2683         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->win);
2684
2685
2686         /* Vars for nice grid alignment */ 
2687         dx= (1280-90)/7;        /* spacing for use in equally dividing 'tab' row */
2688
2689         xpos = 45;              /* left padding */
2690         ypos = 50;              /* bottom padding for buttons */
2691         ypostab = 10;           /* bottom padding for 'tab' row */
2692
2693         buth = 20;              /* standard button height */
2694
2695         spref = 90;     /* standard size for small preferences button */
2696         mpref = 189;    /* standard size for medium preferences button */
2697         lpref = 288;    /* standard size for large preferences button */
2698         smfileselbut = buth;    /* standard size for fileselect button (square) */
2699
2700         edgsp = 3;              /* space from edge of end 'tab' to edge of end button */
2701         midsp = 9;              /* horizontal space between buttons */
2702
2703         rspace = 3;             /* default space between rows */
2704
2705         y1 = ypos;              /* grid alignment for each row of buttons */
2706         y2 = ypos+buth+rspace;
2707         y3 = ypos+2*(buth+rspace);
2708         y4 = ypos+3*(buth+rspace);
2709         y5 = ypos+4*(buth+rspace);
2710         y6 = ypos+5*(buth+rspace);
2711
2712
2713         y2label = y2-2;         /* adjustments to offset the labels down to align better */
2714         y3label = y3-2;
2715         y4label = y4-2;
2716         y5label = y5-2;
2717         y6label = y6-2;
2718
2719
2720         /* set the colour to blue and draw the main 'tab' controls */
2721
2722         uiBlockSetCol(block, TH_BUT_SETTING1);
2723         uiBlockBeginAlign(block);
2724         
2725         uiDefButS(block, ROW,B_USERPREF,"View & Controls",
2726                 xpos,ypostab,(short)dx,buth,
2727                 &U.userpref,1.0,0.0, 0, 0,"");
2728                 
2729         uiDefButS(block, ROW,B_USERPREF,"Edit Methods",
2730                 (short)(xpos+dx),ypostab,(short)dx,buth,
2731                 &U.userpref,1.0,1.0, 0, 0,"");
2732
2733         uiDefButS(block, ROW,B_USERPREF,"Language & Font",
2734                 (short)(xpos+2*dx),ypostab,(short)dx,buth,
2735                 &U.userpref,1.0,2.0, 0, 0,"");
2736
2737         uiDefButS(block, ROW,B_USERPREF,"Themes",
2738                 (short)(xpos+3*dx),ypostab,(short)dx,buth,
2739                 &U.userpref,1.0,6.0, 0, 0,"");
2740
2741         uiDefButS(block, ROW,B_USERPREF,"Auto Save",
2742                 (short)(xpos+4*dx),ypostab,(short)dx,buth,
2743                 &U.userpref,1.0,3.0, 0, 0,"");
2744
2745         uiDefButS(block, ROW,B_USERPREF,"System & OpenGL",
2746                 (short)(xpos+5*dx),ypostab,(short)dx,buth,
2747                 &U.userpref,1.0,4.0, 0, 0,"");
2748                 
2749         uiDefButS(block, ROW,B_USERPREF,"File Paths",
2750                 (short)(xpos+6*dx),ypostab,(short)dx,buth,
2751                 &U.userpref,1.0,5.0, 0, 0,"");
2752
2753         uiBlockSetCol(block, TH_AUTO);
2754         uiBlockEndAlign(block);