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