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