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