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