svn merge -r 13452:14721 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender-staging.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) if(BIF_do_manipulator(sa)) return;
1198                 
1199                 /* swap mouse buttons based on user preference */
1200                 if (U.flag & USER_LMOUSESELECT) {
1201                         /* only swap mouse button for selection, in modes where it is relevant.
1202                          * painting/sculpting stays on LEFTMOUSE */
1203                         if (   !((G.f & G_SCULPTMODE) || (G.f & G_WEIGHTPAINT) ||
1204                                 (G.f & G_VERTEXPAINT) || (G.f & G_TEXTUREPAINT) || (G.f & G_PARTICLEEDIT)) ||
1205                                 (G.obedit) )
1206                         {                       
1207                                 if (event==LEFTMOUSE) event = RIGHTMOUSE;
1208                                 else if (event==RIGHTMOUSE) event = LEFTMOUSE;
1209                         }
1210                 }
1211
1212                 if(!mouse_in_header(sa)) {
1213                         if(!G.obedit && (G.f & G_SCULPTMODE)) {
1214                                 SculptSession *ss= sculpt_session();
1215                                 if(ss && ss->radialcontrol) {
1216                                         radialcontrol_do_events(ss->radialcontrol, event);
1217                                         return;
1218                                 }
1219                                 else if(event!=LEFTMOUSE && event!=MIDDLEMOUSE && (event==MOUSEY || event==MOUSEX)) {
1220                                         if(!bwin_qtest(sa->win))
1221                                                 allqueue(REDRAWVIEW3D, 0);
1222                                 }
1223                         }
1224                         else if(!G.obedit && OBACT && G.f&G_PARTICLEEDIT){
1225                                 ParticleSystem *psys=PE_get_current(OBACT);
1226                                 ParticleEditSettings *pset=PE_settings();
1227                                 if(*PE_radialcontrol()) {
1228                                         radialcontrol_do_events(*PE_radialcontrol(), event);
1229                                         return;
1230                                 }
1231                                 if(psys && psys->edit){
1232                                         if(pset->brushtype>=0 &&
1233                                                 event!=LEFTMOUSE && event!=RIGHTMOUSE && event!=MIDDLEMOUSE &&
1234                                                 (event==MOUSEY || event==MOUSEX) && bwin_qtest(sa->win)==0) {
1235                                                 allqueue(REDRAWVIEW3D, 0);
1236                                         }
1237                                 }
1238                         }
1239
1240                         /* Handle retopo painting */
1241                         if(retopo_mesh_paint_check()) {
1242                                 if(!retopo_paint(event))
1243                                         return;
1244                         }
1245                 }
1246
1247                 /* run any view3d event handler script links */
1248                 if (event && sa->scriptlink.totscript)
1249                         if (BPY_do_spacehandlers(sa, event, SPACEHANDLER_VIEW3D_EVENT))
1250                                 return; /* return if event was processed (swallowed) by handler(s) */
1251
1252                 /* TEXTEDITING?? */
1253                 if((G.obedit) && G.obedit->type==OB_FONT) {
1254                         switch(event) {
1255                         
1256                         case LEFTMOUSE:
1257                                 mouse_cursor();
1258                                 break;
1259                         case MIDDLEMOUSE:
1260                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
1261                                 if(U.flag & USER_VIEWMOVE) {
1262                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1263                                                 viewmove(0);
1264                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1265                                                 viewmove(2);
1266                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1267                                                 viewmove(1);
1268                                 }
1269                                 else {
1270                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1271                                                 viewmove(1);
1272                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1273                                                 viewmove(2);
1274                                         else
1275                                                 viewmove(0);
1276                                 }
1277                                 break;
1278                                 
1279                         case WHEELUPMOUSE:
1280                                 /* Regular:   Zoom in */
1281                                 /* Shift:     Scroll up */
1282                                 /* Ctrl:      Scroll right */
1283                                 /* Alt-Shift: Rotate up */
1284                                 /* Alt-Ctrl:  Rotate right */
1285
1286                                 if( G.qual & LR_SHIFTKEY ) {
1287                                         if( G.qual & LR_ALTKEY ) { 
1288                                                 G.qual &= ~LR_SHIFTKEY;
1289                                                 persptoetsen(PAD2);
1290                                                 G.qual |= LR_SHIFTKEY;
1291                                         } else {
1292                                                 persptoetsen(PAD2);
1293                                         }
1294                                 } else if( G.qual & LR_CTRLKEY ) {
1295                                         if( G.qual & LR_ALTKEY ) { 
1296                                                 G.qual &= ~LR_CTRLKEY;
1297                                                 persptoetsen(PAD4);
1298                                                 G.qual |= LR_CTRLKEY;
1299                                         } else {
1300                                                 persptoetsen(PAD4);
1301                                         }
1302                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1303                                         persptoetsen(PADMINUS);
1304                                 else
1305                                         persptoetsen(PADPLUSKEY);
1306
1307                                 doredraw= 1;
1308                                 break;
1309
1310                         case WHEELDOWNMOUSE:
1311                                 /* Regular:   Zoom out */
1312                                 /* Shift:     Scroll down */
1313                                 /* Ctrl:      Scroll left */
1314                                 /* Alt-Shift: Rotate down */
1315                                 /* Alt-Ctrl:  Rotate left */
1316
1317                                 if( G.qual & LR_SHIFTKEY ) {
1318                                         if( G.qual & LR_ALTKEY ) { 
1319                                                 G.qual &= ~LR_SHIFTKEY;
1320                                                 persptoetsen(PAD8);
1321                                                 G.qual |= LR_SHIFTKEY;
1322                                         } else {
1323                                                 persptoetsen(PAD8);
1324                                         }
1325                                 } else if( G.qual & LR_CTRLKEY ) {
1326                                         if( G.qual & LR_ALTKEY ) { 
1327                                                 G.qual &= ~LR_CTRLKEY;
1328                                                 persptoetsen(PAD6);
1329                                                 G.qual |= LR_CTRLKEY;
1330                                         } else {
1331                                                 persptoetsen(PAD6);
1332                                         }
1333                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1334                                         persptoetsen(PADPLUSKEY);
1335                                 else
1336                                         persptoetsen(PADMINUS);
1337                                 
1338                                 doredraw= 1;
1339                                 break;
1340
1341                         case UKEY:
1342                                 if(G.qual==LR_ALTKEY) {
1343                                         remake_editText();
1344                                         doredraw= 1;
1345                                 } 
1346                                 else {
1347                                         do_textedit(event, val, ascii);
1348                                 }
1349                                 break;
1350                         case VKEY:
1351                                 if(G.qual==LR_ALTKEY) {
1352                                         paste_editText();
1353                                         doredraw= 1;
1354                                 } 
1355                                 else {
1356                                         do_textedit(event, val, ascii);
1357                                 }
1358                                 break;
1359                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1360                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1361                         case PADENTER:
1362                                 persptoetsen(event);
1363                                 doredraw= 1;
1364                                 break;
1365                                 
1366                         default:
1367                                 do_textedit(event, val, ascii);
1368                                 break;
1369                         }
1370                 }
1371                 else if(!G.obedit && (G.f & G_SCULPTMODE)) {
1372                         SculptData *sd= sculpt_data();
1373                         SculptSession *ss= sculpt_session();
1374                         BrushData *br= sculptmode_brush();
1375                         Mesh *me= get_mesh(OBACT);
1376                         char update_prop= 0;
1377                         
1378                         if(U.flag & USER_NONUMPAD) {
1379                                 event= convert_for_nonumpad(event);
1380                         }
1381                         
1382                         switch(event) {
1383                         case LEFTMOUSE:
1384                                 if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1385                                         sculptmode_pmv(0);
1386                                 else if(!(ss && ss->radialcontrol))
1387                                         sculpt();
1388                                 break;
1389                         /* View */
1390                         case MIDDLEMOUSE:
1391                                 handle_view_middlemouse();
1392                                 break;
1393                         case WHEELUPMOUSE:
1394                                 handle_view_wheelup();
1395                                 doredraw= 1;
1396                                 break;
1397                         case WHEELDOWNMOUSE:
1398                                 handle_view_wheeldown();
1399                                 doredraw= 1;
1400                                 break;
1401                         case RIGHTMOUSE:
1402                                 if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1403                                         sculptmode_pmv(1);
1404                                 break;
1405                         case CKEY:
1406                                 if(G.qual==LR_SHIFTKEY) {
1407                                         view3d_home(1);
1408                                         curs= give_cursor();
1409                                         curs[0]=curs[1]=curs[2]= 0.0;
1410                                         allqueue(REDRAWVIEW3D, 0);
1411                                 }
1412                                 else if(G.qual==0) {
1413                                         if (G.vd->persp==V3D_CAMOB)
1414                                                 /* center the camera offset */
1415                                                 G.vd->camdx= G.vd->camdy= 0.0;
1416                                         else {
1417                                                 /*non camera center*/
1418                                                 float new_ofs[3];
1419                                                 curs= give_cursor();
1420                                                 new_ofs[0]= -curs[0];
1421                                                 new_ofs[1]= -curs[1];
1422                                                 new_ofs[2]= -curs[2];
1423                                                 smooth_view(G.vd, new_ofs, NULL, NULL, NULL);
1424                                         }
1425                                 }
1426                                 doredraw= 1;
1427                                 break;
1428                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1429                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1430                         case PADPLUSKEY:
1431                         case PADMINUS:
1432                         case PADENTER:
1433                                 persptoetsen(event);
1434                                 doredraw= 1;
1435                                 break;
1436                         case BKEY:
1437                                 if(G.qual==LR_ALTKEY)
1438                                         view3d_edit_clipping(v3d);
1439                                 else if(G.qual==LR_SHIFTKEY) {
1440                                         if(G.vd->persp==V3D_CAMOB)
1441                                                 set_render_border();
1442                                         else
1443                                                 view3d_border_zoom();
1444                                 }
1445                                 break;
1446                         case PADSLASHKEY:
1447                                 if(!G.qual) {
1448                                         if(G.vd->localview) {
1449                                                 G.vd->localview= 0;
1450                                                 endlocalview(curarea);
1451                                         }
1452                                         else {
1453                                                 G.vd->localview= 1;
1454                                                 initlocalview();
1455                                         }
1456                                         allqueue(REDRAWVIEW3D, 1);
1457                                 }
1458                                 break;
1459                         /* Brush properties */
1460                         case AKEY:
1461                                 br->flag ^= SCULPT_BRUSH_AIRBRUSH;
1462                                 update_prop= 1; break;
1463                         case FKEY:
1464                                 if(ss) {
1465                                         sculpt_radialcontrol_start(G.qual == 0 ? RADIALCONTROL_SIZE :
1466                                                                    G.qual == LR_SHIFTKEY ? RADIALCONTROL_STRENGTH :
1467                                                                    G.qual == LR_CTRLKEY ? RADIALCONTROL_ROTATION :
1468                                                                    RADIALCONTROL_NONE);
1469                                 }
1470                                 break;
1471                         case VKEY:
1472                                 br->dir= br->dir==1 ? 2 : 1;
1473                                 update_prop= 1; break;
1474                         /* Set brush */
1475                         case DKEY:
1476                                 sd->brush_type= DRAW_BRUSH;
1477                                 update_prop= 1; break;
1478                         case SKEY:
1479                                 sd->brush_type= SMOOTH_BRUSH;
1480                                 update_prop= 1; break;
1481                         case PKEY:
1482                                 sd->brush_type= PINCH_BRUSH;
1483                                 update_prop= 1; break;
1484                         case IKEY:
1485                                 sd->brush_type= INFLATE_BRUSH;
1486                                 update_prop= 1; break;
1487                         case GKEY:
1488                                 sd->brush_type= GRAB_BRUSH;
1489                                 update_prop= 1; break;
1490                         case LKEY:
1491                                 sd->brush_type= LAYER_BRUSH;
1492                                 update_prop= 1; break;
1493                         case TKEY:
1494                                 sd->brush_type= FLATTEN_BRUSH;
1495                                 update_prop= 1; break;
1496                         /* Symmetry */
1497                         case XKEY:
1498                                 sd->symm^= SYMM_X;
1499                                 update_prop= 1; break;
1500                         case YKEY:
1501                                 sd->symm^= SYMM_Y;
1502                                 update_prop= 1; break;
1503                         case ZKEY:
1504                                 sd->symm^= SYMM_Z;
1505                                 update_prop= 1; break;
1506                         /* Interface */
1507                         case NKEY:
1508                                 if(G.qual==0) {
1509                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1510                                         allqueue(REDRAWVIEW3D, 0);
1511                                 }
1512                                 break;
1513                         /* Multires */
1514                         case PAGEUPKEY:
1515                                 if(me && me->mr) {
1516                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current+1;
1517                                         multires_set_level_cb(ob, ob->data);
1518                                 }
1519                                 break;
1520                         case PAGEDOWNKEY:
1521                                 if(me && me->mr) {
1522                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current-1;
1523                                         multires_set_level_cb(ob, ob->data);
1524                                 }
1525                                 break;
1526                         /* Partial Visibility */
1527                         case HKEY:
1528                                 if(G.qual==LR_ALTKEY) {
1529                                         waitcursor(1);
1530                                         mesh_pmv_off(ob, get_mesh(ob));
1531                                         BIF_undo_push("Partial mesh hide");
1532                                         allqueue(REDRAWVIEW3D,0);
1533                                         waitcursor(0);
1534                                 }
1535                                 break;
1536                         /* Layers */
1537                         case ONEKEY:
1538                                 do_layer_buttons(0); break;
1539                         case TWOKEY:
1540                                 do_layer_buttons(1); break;
1541                         case THREEKEY:
1542                                 do_layer_buttons(2); break;
1543                         case FOURKEY:
1544                                 do_layer_buttons(3); break;
1545                         case FIVEKEY:
1546                                 do_layer_buttons(4); break;
1547                         case SIXKEY:
1548                                 do_layer_buttons(5); break;
1549                         case SEVENKEY:
1550                                 do_layer_buttons(6); break;
1551                         case EIGHTKEY:
1552                                 do_layer_buttons(7); break;
1553                         case NINEKEY:
1554                                 do_layer_buttons(8); break;
1555                         case ZEROKEY:
1556                                 do_layer_buttons(9); break;
1557                         case MINUSKEY:
1558                                 do_layer_buttons(10); break;
1559                         case EQUALKEY:
1560                                 do_layer_buttons(11); break;
1561                         case ACCENTGRAVEKEY:
1562                                 do_layer_buttons(-1); break;
1563                         
1564                         case NDOFMOTION:                
1565                                 if (G.vd->ndofmode == 0) {
1566                                         viewmoveNDOF(1);
1567                                 } else if (G.vd->ndofmode == 1) {
1568                                         viewmoveNDOFfly(1);
1569                                 } else {
1570                                         NDofTransform();
1571                                 }
1572                 break;
1573                                 
1574             case NDOFBUTTON:
1575                                 if (val == 1) {
1576                                         G.vd->ndofmode +=1;
1577                                         if (G.vd->ndofmode > 2)         /* we have currently 3 modes : 0 original, 1 fly, 2 transform */
1578                                                 G.vd->ndofmode = 0;
1579                                 }
1580                                 if (val == 2) {
1581                                         G.vd->ndoffilter =(G.vd->ndoffilter == 1 ? 0 : 1);
1582                                 }
1583                                 allqueue(REDRAWHEADERS, 0);
1584                 break;
1585                                 
1586                         }
1587                         
1588                         /* Redraw buttons window as well as view 3d (for floating panel) */
1589                         if(update_prop) {
1590                                 allqueue(REDRAWVIEW3D, 0);
1591                                 allqueue(REDRAWBUTSEDIT, 0);
1592                         }
1593                 } else {
1594
1595                         if (U.flag & USER_NONUMPAD) {
1596                                 event= convert_for_nonumpad(event);
1597                         }
1598
1599                         switch(event) {
1600                         
1601                         /* Afterqueue events */
1602                         case BACKBUFDRAW:
1603                                 backdrawview3d(1);
1604                                 break;
1605                         case RENDERPREVIEW:
1606                                 BIF_view3d_previewrender(sa);
1607                                 break;
1608                                 
1609                         case TABKEY:
1610                                 /* Shift-Tabe handling (other cases are in toets) */
1611                                 if (G.qual == LR_SHIFTKEY)
1612                                 {
1613                                         /* Snap toggle only when supported */
1614                                         if (BIF_snappingSupported())
1615                                         {
1616                                                 G.scene->snap_flag ^= SCE_SNAP;
1617                                                 allqueue(REDRAWHEADERS, 0);
1618                                         }                                       
1619                                 }
1620                                 break;
1621                                 
1622                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1623                          * based on user preference USER_LMOUSESELECT
1624                          */
1625                         case LEFTMOUSE: 
1626                                 if ((G.obedit) || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT|G_PARTICLEEDIT))) {
1627                                         mouse_cursor();
1628                                 }
1629                                 else if (G.f & G_WEIGHTPAINT) {
1630                                         weight_paint();
1631                                 }
1632                                 else if (G.f & G_VERTEXPAINT) {
1633                                         vertex_paint();
1634                                 }
1635                                 else if (G.f & G_TEXTUREPAINT) {
1636                                         imagepaint_paint(L_MOUSE, 1);
1637                                 }
1638                                 else if (G.f & G_PARTICLEEDIT) {
1639                                         if(G.qual & LR_CTRLKEY)
1640                                                 mouse_cursor();
1641                                         else if(!PE_brush_particles())
1642                                                 mouse_cursor();
1643                                 }
1644                                 break;
1645                         case MIDDLEMOUSE:
1646                                 handle_view_middlemouse();
1647                                 break;
1648                         case RIGHTMOUSE:
1649                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
1650                                         if(G.obedit->type==OB_MESH)
1651                                                 mouse_mesh();
1652                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1653                                                 mouse_nurb();
1654                                         else if(G.obedit->type==OB_MBALL)
1655                                                 mouse_mball();
1656                                         else if(G.obedit->type==OB_LATTICE)
1657                                                 mouse_lattice();
1658                                         else if(G.obedit->type==OB_ARMATURE)
1659                                                 mouse_armature();
1660                                 }
1661                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
1662                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1663                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
1664                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1665                                 else if(G.qual==LR_CTRLKEY)
1666                                         mouse_select(); /* also allow in editmode, for vertex parenting */
1667                                 else if(FACESEL_PAINT_TEST)
1668                                         face_select();
1669                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
1670                                         sample_vpaint();
1671                                 else if( G.f & G_PARTICLEEDIT)
1672                                         PE_mouse_particles();
1673                                 else
1674                                         mouse_select(); /* does poses too */
1675                                 break;
1676                         case WHEELUPMOUSE:
1677                                 handle_view_wheelup();
1678                                 doredraw= 1;
1679                                 break;
1680                         case WHEELDOWNMOUSE:
1681                                 handle_view_wheeldown();
1682                                 doredraw= 1;
1683                                 break;
1684
1685             case NDOFMOTION:            
1686                                 if (G.vd->ndofmode == 0) {
1687                                         viewmoveNDOF(1);
1688                                 } else if (G.vd->ndofmode == 1) {
1689                                         viewmoveNDOFfly(1);
1690                                 } else {
1691                                         NDofTransform();
1692                                 }
1693                 break;
1694
1695             case NDOFBUTTON:
1696                                 if (val == 1) {
1697                                         G.vd->ndofmode +=1;
1698                                         if (G.vd->ndofmode > 2)         /* we have currently 3 modes : 0 original, 1 fly, 2 transform */
1699                                                 G.vd->ndofmode = 0;
1700                                 }
1701                                 if (val == 2) {
1702                                         G.vd->ndoffilter =(G.vd->ndoffilter == 1 ? 0 : 1);
1703                                 }
1704                                 allqueue(REDRAWHEADERS, 0);
1705                 break;
1706                                 
1707                         case ONEKEY:
1708                                 if(G.qual==LR_CTRLKEY) {
1709                                         flip_subdivison(1);
1710                                 }
1711                                 else do_layer_buttons(0); 
1712                                 break;
1713                                 
1714                         case TWOKEY:
1715                                 if(G.qual==LR_CTRLKEY) {
1716                                         flip_subdivison(2);
1717                                 }
1718                                 else do_layer_buttons(1); 
1719                                 break;
1720                                 
1721                         case THREEKEY:
1722                                 if(G.qual==LR_CTRLKEY) {
1723                                         flip_subdivison(3);
1724                                 }
1725                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1726                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1727                                                 select_faces_by_numverts(3);
1728                                 }
1729                                 else do_layer_buttons(2); 
1730                                 break;
1731                                 
1732                         case FOURKEY:
1733                                 if(G.qual==LR_CTRLKEY) {
1734                                         flip_subdivison(4);
1735                                 }
1736                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1737                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1738                                                 select_faces_by_numverts(4);
1739                                 }
1740                                 else do_layer_buttons(3); 
1741                                 break;
1742                                 
1743                         case FIVEKEY:
1744                                 if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1745                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1746                                                 select_faces_by_numverts(5);
1747                                 }
1748                                 else do_layer_buttons(4);
1749                                 break;
1750
1751                         case SIXKEY:
1752                                 do_layer_buttons(5); break;
1753                         case SEVENKEY:
1754                                 do_layer_buttons(6); break;
1755                         case EIGHTKEY:
1756                                 do_layer_buttons(7); break;
1757                         case NINEKEY:
1758                                 do_layer_buttons(8); break;
1759                         case ZEROKEY:
1760                                 do_layer_buttons(9); break;
1761                         case MINUSKEY:
1762                                 do_layer_buttons(10); break;
1763                         case EQUALKEY:
1764                                 do_layer_buttons(11); break;
1765                         case ACCENTGRAVEKEY:
1766                                 do_layer_buttons(-1); break;
1767                         
1768                         case SPACEKEY:
1769                                 if(G.qual == LR_CTRLKEY) {
1770                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1771                                         if(val>0) {
1772                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1773                                                 else {
1774                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1775                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1776                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1777                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1778                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1779                                                 }
1780                                                 doredraw= 1;
1781                                         }
1782                                 }
1783                                 else if(G.qual == LR_ALTKEY) {
1784                                         BIF_selectOrientation();
1785                                         doredraw= 1;
1786                                 }
1787
1788                                 break;
1789                                 
1790                         case AKEY:
1791                                 if (G.obedit == 0 && G.qual == (LR_CTRLKEY|LR_ALTKEY)) {
1792                                         alignmenu();
1793                                 }
1794                                 else if(G.qual & LR_CTRLKEY) { /* also with shift! */
1795                                         apply_object(); 
1796                                 }
1797                                 else if((G.qual==LR_SHIFTKEY)) {
1798                                         toolbox_n_add();
1799                                 }
1800                                 else {
1801                                         if(G.obedit) {
1802                                                 if(G.obedit->type==OB_MESH)
1803                                                         deselectall_mesh();
1804                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1805                                                         deselectall_nurb();
1806                                                 else if(G.obedit->type==OB_MBALL)
1807                                                         deselectall_mball();
1808                                                 else if(G.obedit->type==OB_LATTICE)
1809                                                         deselectall_Latt();
1810                                                 else if(G.obedit->type==OB_ARMATURE)
1811                                                         deselectall_armature(1, 1);     /* 1 == toggle */
1812                                         }
1813                                         else if (ob && (ob->flag & OB_POSEMODE)){
1814                                                 deselectall_posearmature(ob, 1, 1);
1815                                         }
1816                                         else {
1817                                                 if(FACESEL_PAINT_TEST) deselectall_tface();
1818                                                 else if(G.f & G_PARTICLEEDIT) PE_deselectall();
1819                                                 else {
1820                                                         /* by design, the center of the active object 
1821                                                          * (which need not necessarily by selected) will
1822                                                          * still be drawn as if it were selected.
1823                                                          */
1824                                                         deselectall();
1825                                                 }
1826                                         }
1827                                 }
1828                                 break;
1829                         case BKEY:
1830                                 if(G.qual==LR_ALTKEY)
1831                                         view3d_edit_clipping(v3d);
1832                                 else if(G.qual==LR_SHIFTKEY)
1833                                 {
1834                                         if(G.vd->persp==V3D_CAMOB)
1835                                                 set_render_border();
1836                                         else
1837                                                 view3d_border_zoom();
1838                                 }
1839                                 else if(G.qual==LR_CTRLKEY) {
1840                                         extern void pointcache_bake(PTCacheID *pid, int startframe);
1841                                         extern void pointcache_free(PTCacheID *pid, int cacheonly);
1842                                         extern void fluidsimBake(Object *ob);
1843                                         extern void fluidsimFreeBake(Object *ob);
1844                                         int pupval;
1845
1846                                         pupval= pupmenu("Physics Baking%t|Bake selected %x1|Free bake selected %x2|Free cache selected %x3");
1847
1848                                         if(pupval > 0) {
1849                                                 if(pupval == 1) {
1850                                                         pointcache_bake(NULL, 0);
1851                                                         /* also bake first domain of selected objects... */
1852                                                         fluidsimBake(NULL);
1853                                                 }
1854                                                 else if(pupval == 2) {
1855                                                         pointcache_free(NULL, 0);
1856                                                         fluidsimFreeBake(NULL);
1857                                                 }
1858                                                 else if(pupval == 3)
1859                                                         pointcache_free(NULL, 1);
1860                                         }
1861                                 }
1862                                 else if(G.qual== (LR_ALTKEY|LR_CTRLKEY))
1863                                         objects_bake_render_menu();
1864                                 else if(G.qual==0)
1865                                         borderselect();
1866                                 break;
1867                         case CKEY:
1868                                 if(G.qual==LR_CTRLKEY) {
1869                                         if(ob && (ob->flag & OB_POSEMODE))
1870                                                 pose_copy_menu();       /* poseobject.c */
1871                                         else
1872                                                 copy_attr_menu();
1873                                 }
1874                                 else if(G.qual==LR_ALTKEY) {
1875                                         if(ob && (ob->flag & OB_POSEMODE))
1876                                                 pose_clear_constraints();       /* poseobject.c */
1877                                         else
1878                                                 convertmenu();  /* editobject.c */
1879                                 }
1880                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY)) 
1881                                         add_constraint(0);      /* editconstraint.c, generic for objects and posemode */
1882                                 else if(G.qual==(LR_CTRLKEY|LR_SHIFTKEY)) {
1883                                         BIF_manageTransformOrientation(0, 1);
1884                                         allqueue(REDRAWVIEW3D, 0);
1885                                 }
1886                                 else if((G.qual==LR_SHIFTKEY)) {
1887                                         view3d_home(1);
1888                                         curs= give_cursor();
1889                                         curs[0]=curs[1]=curs[2]= 0.0;
1890                                         allqueue(REDRAWVIEW3D, 0);
1891                                 }
1892                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1893                                         makecyclicNurb();
1894                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1895                                         allqueue(REDRAWVIEW3D, 0);
1896                                 }
1897                                 else if((G.qual==0)){
1898                                         if (G.vd->persp==V3D_CAMOB)
1899                                                 /* center the camera offset */
1900                                                 G.vd->camdx= G.vd->camdy= 0.0;
1901                                         else {
1902                                                 /*non camera center*/
1903                                                 float new_ofs[3];
1904                                                 curs= give_cursor();
1905                                                 new_ofs[0]= -curs[0];
1906                                                 new_ofs[1]= -curs[1];
1907                                                 new_ofs[2]= -curs[2];
1908                                                 smooth_view(G.vd, new_ofs, NULL, NULL, NULL);
1909                                         }
1910                                         scrarea_queue_winredraw(curarea);
1911                                 }
1912                                 break;
1913                         case DKEY:
1914                                 if((G.qual==LR_SHIFTKEY)) {
1915                                         duplicate_context_selected();
1916                                 }
1917                                 else if(G.qual==LR_ALTKEY) {
1918                                         if(ob && (ob->flag & OB_POSEMODE))
1919                                                 error ("Duplicate not possible in posemode.");
1920                                         else if((G.obedit==NULL))
1921                                                 adduplicate(0, 0);
1922                                 }
1923                                 else if(G.qual==LR_CTRLKEY) {
1924                                         imagestodisplist();
1925                                 }
1926                                 else if((G.qual==0)){
1927                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
1928                                         if(pupval>0) {
1929                                                 G.vd->drawtype= pupval;
1930                                                 doredraw= 1;
1931                                         }
1932                                 }
1933                                 
1934                                 break;
1935                         case EKEY:
1936                                 if (G.qual==0){
1937                                         if(G.obedit) {
1938                                                 if(G.obedit->type==OB_MESH)
1939                                                         extrude_mesh();
1940                                                 else if(G.obedit->type==OB_CURVE)
1941                                                         addvert_Nurb('e');
1942                                                 else if(G.obedit->type==OB_SURF)
1943                                                         extrude_nurb();
1944                                                 else if(G.obedit->type==OB_ARMATURE)
1945                                                         extrude_armature(0);
1946                                         }
1947                                 }
1948                                 else if (G.qual==LR_CTRLKEY) {
1949                                         if(G.obedit && G.obedit->type==OB_MESH)
1950                                                 Edge_Menu();
1951                                         else if (FACESEL_PAINT_TEST)
1952                                                 seam_mark_clear_tface(0);
1953                                 }
1954                                 else if (G.qual==LR_SHIFTKEY) {
1955                                         if (G.obedit && G.obedit->type==OB_MESH &&
1956                                             !multires_level1_test()) {
1957                                                 initTransform(TFM_CREASE, CTX_EDGE);
1958                                                 Transform();
1959                                         }
1960                                         else if (G.obedit && G.obedit->type==OB_ARMATURE) {
1961                                                 extrude_armature(1);
1962                                         }
1963                                 }
1964                                 else if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY)) {
1965                                         if (G.obedit && G.obedit->type==OB_MESH &&
1966                                             !multires_level1_test()) {
1967                                                 if (G.scene->selectmode & SCE_SELECT_VERTEX) {
1968                                                         initTransform(TFM_BWEIGHT, CTX_NONE);
1969                                                 }
1970                                                 else {
1971                                                         initTransform(TFM_BWEIGHT, CTX_EDGE);
1972                                                 }
1973                                                 Transform();
1974                                         }
1975                                 }
1976                                 break;
1977                         case FKEY:
1978                                 if(G.obedit) {
1979                                         if(G.obedit->type==OB_MESH) {
1980                                                 if(G.qual == LR_CTRLKEY)
1981                                                         Face_Menu();
1982                                                 else if((G.qual==LR_SHIFTKEY))
1983                                                         fill_mesh();
1984                                                 else if(G.qual==LR_ALTKEY)
1985                                                         beauty_fill();
1986                                                 else if(G.qual==(LR_CTRLKEY|LR_SHIFTKEY))
1987                                                         edge_flip();
1988                                                 else if (G.qual==0)
1989                                                         addedgeface_mesh();
1990                                                 else if ( G.qual == 
1991                                                          (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1992                                                         select_linked_flat_faces();
1993                                                 }
1994
1995                                         }
1996                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
1997                                                 addsegment_nurb();
1998                                         }
1999                                         else if(G.obedit->type == OB_ARMATURE) {
2000                                                 fill_bones_armature();
2001                                         }
2002                                 }
2003                                 else if(G.qual==LR_CTRLKEY)
2004                                         sort_faces();
2005                                 else if((G.qual==LR_SHIFTKEY)) {
2006                                         if(ob && (ob->flag & OB_POSEMODE))
2007                                            pose_activate_flipped_bone();
2008                                         else if(G.f & G_WEIGHTPAINT)
2009                                                 pose_activate_flipped_bone();
2010                                         else if(G.f & G_PARTICLEEDIT)
2011                                                 PE_radialcontrol_start(RADIALCONTROL_STRENGTH);
2012                                         else
2013                                                 fly();
2014                                 }
2015                                 else if((G.qual==LR_ALTKEY)) {
2016                                         if(ob && (ob->flag & OB_POSEMODE))
2017                                                 if(okee("Flip quaternion rotations"))
2018                                                         pose_flipquats();
2019                                 }
2020                                 else {
2021                                         if (G.f & (G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT)){
2022                                                 G.f ^= G_FACESELECT;
2023                                                 allqueue(REDRAWVIEW3D, 1);
2024                                         }
2025                                         else if(G.f & G_PARTICLEEDIT) {
2026                                                 PE_radialcontrol_start(RADIALCONTROL_SIZE);
2027                                         } else {
2028                                                 pupmenu("Note%t|UV/Face Select was removed. Editmode now allows UV editing, Add a UV layer or Unwrap (UKey)");
2029                                         }
2030                                 }
2031                                 
2032                                 break;
2033                         case GKEY:
2034                                 if((G.qual == LR_CTRLKEY)) {
2035                                         if(G.obedit) {
2036                                                 if(ELEM(G.obedit->type, OB_MESH, OB_LATTICE))
2037                                                         vgroup_assign_with_menu();
2038                                         }
2039                                         else if(ob && (ob->flag & OB_POSEMODE))
2040                                                 pgroup_operation_with_menu();
2041                                         else
2042                                                 group_operation_with_menu();
2043                                 }
2044                                 else if((G.qual == (LR_CTRLKEY|LR_SHIFTKEY))) {
2045                                         if(G.obedit) {
2046                                                 if(ELEM(G.obedit->type, OB_MESH, OB_LATTICE))
2047                                                         vgroup_operation_with_menu();
2048                                         }
2049                                 }
2050                                 else if((G.qual==LR_SHIFTKEY))
2051                                         if(G.obedit) {
2052                                                 if(G.obedit->type==OB_MESH)
2053                                                         select_mesh_group_menu();
2054                                         } 
2055                                         else if(ob && (ob->flag & OB_POSEMODE))
2056                                                 pose_select_grouped_menu();
2057                                         else
2058                                                 select_object_grouped_menu();
2059                                 else if((G.obedit==0) && G.qual==LR_ALTKEY) {
2060                                         if(okee("Clear location")) {
2061                                                 clear_object('g');
2062                                         }
2063                                 }
2064                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2065                                         v3d->twtype= V3D_MANIP_TRANSLATE;
2066                                         doredraw= 1;
2067                                 }
2068                                 else if((G.qual==0)) {
2069                                         initTransform(TFM_TRANSLATION, CTX_NONE);
2070                                         Transform();
2071                                 }
2072                                 break;
2073                         case HKEY:
2074                                 if(G.obedit) {
2075                                         if(G.obedit->type==OB_MESH) {
2076                                                 if(G.qual==LR_CTRLKEY)
2077                                                         add_hook_menu();
2078                                                 else if(G.qual==LR_ALTKEY)
2079                                                         reveal_mesh();
2080                                                 else if((G.qual==LR_SHIFTKEY))
2081                                                         hide_mesh(1);
2082                                                 else if((G.qual==0)) 
2083                                                         hide_mesh(0);
2084                                         }
2085                                         else if(G.obedit->type== OB_SURF) {
2086                                                 if(G.qual==LR_CTRLKEY)
2087                                                         add_hook_menu();
2088                                                 else if(G.qual==LR_ALTKEY)
2089                                                         revealNurb();
2090                                                 else if((G.qual==LR_SHIFTKEY))
2091                                                         hideNurb(1);
2092                                                 else if((G.qual==0))
2093                                                         hideNurb(0);
2094                                         }
2095                                         else if(G.obedit->type==OB_CURVE) {
2096                                                 if(G.qual==LR_CTRLKEY)
2097                                                         add_hook_menu();
2098                                                 else if(G.qual==LR_ALTKEY)
2099                                                         revealNurb();
2100                                                 /* should be G.qual==LR_SHIFTKEY, but that is taken fro handles already */
2101                                                 else if((G.qual==(LR_ALTKEY|LR_SHIFTKEY))) 
2102                                                         hideNurb(1);
2103                                                 /* should be G.qual==0, but that is taken for handles already */
2104                                                 else if((G.qual==(LR_ALTKEY|LR_CTRLKEY))) 
2105                                                         hideNurb(0);
2106                                                 else {
2107                                                         if(G.qual==LR_CTRLKEY) /* conflict */
2108                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
2109                                                         else if((G.qual==LR_SHIFTKEY))
2110                                                                 sethandlesNurb(1);
2111                                                         else if((G.qual==0))
2112                                                                 sethandlesNurb(3);
2113                                                         
2114                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2115                                                         BIF_undo_push("Handle change");
2116                                                         allqueue(REDRAWVIEW3D, 0);
2117                                                 }
2118                                         }
2119                                         else if(G.obedit->type==OB_LATTICE) {
2120                                                 if(G.qual==LR_CTRLKEY) add_hook_menu();
2121                                         }
2122                                         else if(G.obedit->type==OB_MBALL) {
2123                                                 if(G.qual==LR_ALTKEY)
2124                                                         reveal_mball();
2125                                                 else if((G.qual==LR_SHIFTKEY))
2126                                                         hide_mball(1);
2127                                                 else if((G.qual==0)) 
2128                                                         hide_mball(0);
2129                                         }
2130                                         else if(G.obedit->type==OB_ARMATURE) {
2131                                                 if (G.qual==0)
2132                                                         hide_selected_armature_bones();
2133                                                 else if (G.qual==LR_SHIFTKEY)
2134                                                         hide_unselected_armature_bones();
2135                                                 else if (G.qual==LR_ALTKEY)
2136                                                         show_all_armature_bones();
2137                                         }
2138                                 }
2139                                 else if(FACESEL_PAINT_TEST)
2140                                         hide_tface();
2141                                 else if(G.f & G_PARTICLEEDIT) {
2142                                         if(G.qual == LR_ALTKEY)
2143                                                 PE_hide(0);
2144                                         else if(G.qual == LR_SHIFTKEY)
2145                                                 PE_hide(1);
2146                                         else if(G.qual == 0)
2147                                                 PE_hide(2);
2148                                 }
2149                                 else if(ob && (ob->flag & OB_POSEMODE)) {
2150                                         if (G.qual==0)
2151                                                 hide_selected_pose_bones();
2152                                         else if (G.qual==LR_SHIFTKEY)
2153                                                 hide_unselected_pose_bones();
2154                                         else if (G.qual==LR_ALTKEY)
2155                                                 show_all_pose_bones();
2156                                 }
2157                                 /* Object mode only, other modes are accounted for above */
2158                                 else if ((G.f & (G_VERTEXPAINT|G_TEXTUREPAINT|G_WEIGHTPAINT))==0) {
2159                                         if(G.qual==LR_CTRLKEY)hookmenu();
2160                                         else if(G.qual==LR_ALTKEY) show_objects();
2161                                         else if(G.qual==LR_SHIFTKEY) hide_objects(0);
2162                                         else hide_objects(1);
2163                                 }
2164                                 break;
2165                         case IKEY:
2166                                 if(G.qual==LR_CTRLKEY) {
2167                                         if(ob && (ob->flag & OB_POSEMODE) && ob->type==OB_ARMATURE)
2168                                                 pose_add_IK();
2169                                         else if(ob && G.obedit)
2170                                                 selectswap_mesh();
2171                                         else
2172                                                 selectswap();
2173                                 }
2174                                 else if(G.qual==LR_ALTKEY) {
2175                                         if(ob && (ob->flag & OB_POSEMODE) && ob->type==OB_ARMATURE)
2176                                                 pose_clear_IK();
2177                                 }
2178                                 break;
2179                                 
2180                         case JKEY:
2181                                 if(G.qual==LR_CTRLKEY) {
2182                                         if( ob ) {
2183                                                 join_menu();
2184                                         }
2185                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF)) {
2186                                                 addsegment_nurb();
2187                                         } else {
2188                                                 error("Can't join unless there is an active object");
2189                                         }
2190                                         
2191                                 }
2192                                 else if(G.obedit) {
2193                                         if(G.obedit->type==OB_MESH) {
2194                                                 join_triangles();
2195                                         }
2196                                 }
2197
2198                                 break;
2199                         case KKEY:
2200                                 if(G.obedit) {
2201                                         if (G.obedit->type==OB_MESH) {
2202                                                 if (G.qual==LR_SHIFTKEY)
2203                                                         KnifeSubdivide(KNIFE_PROMPT);
2204                                                 else if (G.qual==0)
2205                                                         LoopMenu();
2206                                         }
2207                                         else if(G.obedit->type==OB_SURF)
2208                                                 printknots();
2209                                 }
2210                                 else {
2211                                         if((G.qual==LR_SHIFTKEY)) {
2212                                                 if(FACESEL_PAINT_TEST)
2213                                                         if (G.f & G_WEIGHTPAINT)
2214                                                                 clear_wpaint_selectedfaces();
2215                                                         else
2216                                                                 clear_vpaint_selectedfaces();
2217                                                 else if(G.f & G_VERTEXPAINT)
2218                                                         clear_vpaint();
2219                                                 else
2220                                                         select_select_keys();
2221                                         }
2222                                         else if (G.qual==0)
2223                                                 set_ob_ipoflags();
2224                                 }
2225                                 
2226                                 break;
2227                         case LKEY:
2228                                 if(G.obedit) {
2229                                         if(G.obedit->type==OB_MESH) {
2230                                                 if (G.qual & LR_CTRLKEY) {
2231                                                         if ((G.scene->selectmode & SCE_SELECT_FACE) == 0) {
2232                                                                 selectconnected_mesh_all(); /* normal select linked */
2233                                                         } else {
2234                                                                 selectconnected_delimit_mesh_all(); /* select linked with edge crease delimiting */
2235                                                         }
2236                                                 } else {
2237                                                         if ((G.scene->selectmode & SCE_SELECT_FACE) == 0) {
2238                                                                 selectconnected_mesh();
2239                                                         } else {
2240                                                                 selectconnected_delimit_mesh();
2241                                                         }
2242                                                 }
2243                                         }
2244                                         if(G.obedit->type==OB_ARMATURE)
2245                                                 selectconnected_armature();
2246                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
2247                                                 selectconnected_nurb();
2248                                 }
2249                                 else if(ob && (ob->flag & OB_POSEMODE)) {
2250                                         if (G.qual == LR_CTRLKEY) 
2251                                                 poselib_preview_poses(ob, 0);
2252                                         else if (G.qual == LR_SHIFTKEY) 
2253                                                 poselib_add_current_pose(ob, 0);
2254                                         else if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY))
2255                                                 poselib_rename_pose(ob);
2256                                         else if (G.qual == LR_ALTKEY)
2257                                                 poselib_remove_pose(ob, NULL);
2258                                         else
2259                                                 selectconnected_posearmature();
2260                                 }
2261                                 else {
2262                                         if(FACESEL_PAINT_TEST) {
2263                                                 if((G.qual==0))
2264                                                         select_linked_tfaces(0);
2265                                                 else if((G.qual==LR_SHIFTKEY))
2266                                                         select_linked_tfaces(1);
2267                                                 else if(G.qual==LR_CTRLKEY)
2268                                                         select_linked_tfaces(2);
2269                                         }
2270                                         else if(G.f & G_PARTICLEEDIT) {
2271                                                 if(G.qual==0)
2272                                                         PE_select_linked();
2273                                         }
2274                                         else {
2275                                                 if((G.qual==0))
2276                                                         make_local_menu();
2277                                                 else if((G.qual==LR_SHIFTKEY))
2278                                                         selectlinks_menu();
2279                                                 else if(G.qual==LR_CTRLKEY)
2280                                                         make_links_menu();
2281                                         }
2282                                 }
2283                                 break;
2284                         case MKEY:
2285                                 if(G.obedit){
2286                                         if (ELEM(G.qual, 0, LR_SHIFTKEY) && (G.obedit->type==OB_ARMATURE)) {
2287                                                 pose_movetolayer();
2288                                         }
2289                                         else if (G.qual==LR_ALTKEY) {
2290                                                 if (G.obedit->type == OB_MESH) {
2291                                                         mergemenu();
2292                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2293                                                 }
2294                                                 else if (G.obedit->type == OB_ARMATURE) {
2295                                                         merge_armature();
2296                                                 }
2297                                         }
2298                                         else if ((G.qual==0) || (G.qual==LR_CTRLKEY)) {
2299                                                 mirrormenu();
2300                                         }
2301                                         else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2302                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
2303                                         }
2304                                 }
2305                                 else if(G.qual & LR_CTRLKEY) {
2306                                         mirrormenu();
2307                                 }
2308                                 else if(G.qual==0 || G.qual==LR_SHIFTKEY) {
2309                                         if(ob && (ob->flag & OB_POSEMODE))
2310                                                 pose_movetolayer();
2311                                         else
2312                                                 movetolayer();
2313                                 }
2314                                 break;
2315                         case NKEY:
2316                                 if((G.qual==0)) {
2317                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
2318                                         allqueue(REDRAWVIEW3D, 0);
2319                                 }
2320                                 else if(G.obedit) {
2321                                         switch (G.obedit->type){
2322                                         case OB_ARMATURE:
2323                                                 if(G.qual==LR_CTRLKEY) {
2324                                                         pupval= pupmenu("Recalculate Bone Roll Angles%t|Clear Roll (Z-Axis Up) %x1|Align Z-Axis to 3D-Cursor %x2");
2325                                                         if (pupval > 0) {
2326                                                                 auto_align_armature(pupval - 1);
2327                                                                 allqueue(REDRAWVIEW3D, 0);
2328                                                         }
2329                                                 }
2330                                                 break;
2331                                         case OB_MESH: 
2332                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2333                                                         if(okee("Recalculate normals inside")) {
2334                                                                 righthandfaces(2);
2335                                                                 allqueue(REDRAWVIEW3D, 0);
2336                                                                 BIF_undo_push("Recalculate normals inside");
2337                                                         }
2338                                                 }
2339                                                 else if(G.qual==LR_CTRLKEY){
2340                                                         if(okee("Recalculate normals outside")) {
2341                                                                 righthandfaces(1);
2342                                                                 allqueue(REDRAWVIEW3D, 0);
2343                                                                 BIF_undo_push("Recalculate normals outside");
2344                                                         }
2345                                                 }
2346                                                 break;
2347                                         }
2348                                 }
2349                                 
2350                                 break;
2351                         case OKEY:
2352                                 if (G.obedit || G.f&G_PARTICLEEDIT) {
2353                                         if (G.qual==LR_SHIFTKEY) {
2354                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%7;
2355                                                 allqueue(REDRAWHEADERS, 0);
2356                                         }
2357                                         else if((G.qual==LR_ALTKEY)) {
2358                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
2359                                                 else G.scene->proportional= 2;
2360                                                 allqueue(REDRAWHEADERS, 0);
2361                                         }
2362                                         else if((G.qual==0)) {
2363                                                 G.scene->proportional= !G.scene->proportional;
2364                                                 allqueue(REDRAWHEADERS, 0);
2365                                         }
2366                                 }
2367                                 else if((G.qual==LR_SHIFTKEY || G.qual==(LR_ALTKEY|LR_SHIFTKEY))) {
2368                                         flip_subdivison(-1);
2369                                 }
2370                                 else if(G.qual==LR_ALTKEY) {
2371                                         if(okee("Clear origin")) {
2372                                                 clear_object('o');
2373                                         }
2374                                 }
2375                                 break;
2376
2377                         case PKEY:
2378                                 if(G.obedit) {
2379                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2380                                                 if(G.obedit->type==OB_ARMATURE)
2381                                                         make_bone_parent();
2382                                                 else
2383                                                         make_parent();
2384                                         }
2385                                         
2386                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
2387                                                 clear_bone_parent();
2388                                         else if((G.qual==0) && (G.obedit->type==OB_ARMATURE)) 
2389                                                 select_bone_parent();
2390                                         else if((G.qual==(LR_CTRLKEY|LR_SHIFTKEY)) && (G.obedit->type==OB_ARMATURE))
2391                                                 separate_armature();
2392                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
2393                                                 separatemenu();
2394                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
2395                                                 separate_nurb();
2396                                         else if (G.qual==LR_SHIFTKEY) {
2397                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
2398                                                 Transform();
2399                                         }
2400                                 }
2401                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
2402                                         make_parent();
2403                                 else if(G.qual==LR_SHIFTKEY) {
2404                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_PREVIEW, 0);
2405                                         doredraw= 1;
2406                                 }
2407                                 else if(G.qual==(LR_ALTKEY|LR_SHIFTKEY)) {
2408                                         initTransform(TFM_PUSHPULL, CTX_NONE);
2409                                         Transform();
2410                                 }
2411                                 else if(G.qual==LR_ALTKEY)
2412                                         clear_parent();
2413                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY))
2414                                         make_proxy();
2415                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY|LR_SHIFTKEY)) {
2416                         start_RBSimulation();
2417                                 }
2418                                 else if((G.qual==0) && (OBACT) && (OBACT->type==OB_ARMATURE) && (OBACT->flag & OB_POSEMODE))
2419                                         select_bone_parent();
2420                                 else if((G.qual==0)) {
2421                         start_game();
2422                                 }
2423                                 break;                          
2424                         case RKEY:
2425                                 if((G.obedit==0) && G.qual==LR_ALTKEY) {
2426                                         if(okee("Clear rotation")) {
2427                                                 clear_object('r');
2428                                         }
2429                                 } 
2430                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2431                                         v3d->twtype= V3D_MANIP_ROTATE;
2432                                         doredraw= 1;
2433                                 }
2434                                 else if (G.obedit) {
2435                                         if((G.qual==LR_SHIFTKEY)) {
2436                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
2437                                                         selectrow_nurb();
2438                                         }
2439                                         else if(G.qual==LR_CTRLKEY) {
2440                                                 if (G.obedit->type==OB_MESH) {
2441                                                         CutEdgeloop(1);
2442                                                         BIF_undo_push("Cut Edgeloop");
2443                                                 }
2444                                                 else if (G.obedit->type==OB_ARMATURE) {
2445                                                         initTransform(TFM_BONE_ROLL, CTX_NONE);
2446                                                         Transform();
2447                                                 }
2448                                         }
2449                                         else if((G.qual==0)) {
2450                                                 initTransform(TFM_ROTATION, CTX_NONE);
2451                                                 Transform();
2452                                         }
2453                                 }
2454                                 else if((G.qual==0)) {
2455                                         initTransform(TFM_ROTATION, CTX_NONE);
2456                                         Transform();
2457                                 }
2458                                 break;
2459                         case SKEY:
2460                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2461                                         v3d->twtype= V3D_MANIP_SCALE;
2462                                         doredraw= 1;
2463                                 }
2464                                 else if(G.obedit) {
2465                                         
2466                                         if(G.qual==LR_ALTKEY) {
2467                                                 if(G.obedit->type==OB_ARMATURE) {
2468                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2469                                                 }
2470                                                 else if (G.obedit->type==OB_CURVE) {
2471                                                         initTransform(TFM_CURVE_SHRINKFATTEN, CTX_NONE);
2472                                                 } else {
2473                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
2474                                                 }
2475                                                 Transform();
2476                                         }
2477                                         else if(G.qual==LR_CTRLKEY) {
2478                                                 initTransform(TFM_SHEAR, CTX_NONE);
2479                                                 Transform();
2480                                         }
2481                                         else if(G.qual==LR_SHIFTKEY)
2482                                                 snapmenu();
2483                                         else if(G.qual==0) {
2484                                                 if(G.obedit->type==OB_ARMATURE) {
2485                                                         bArmature *arm= G.obedit->data;
2486                                                         if(arm->drawtype==ARM_ENVELOPE)
2487                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
2488                                                         else
2489                                                                 initTransform(TFM_RESIZE, CTX_NONE);
2490                                                 }
2491                                                 else
2492                                                         initTransform(TFM_RESIZE, CTX_NONE);
2493                                                 Transform();
2494                                         }
2495                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
2496                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
2497                                                 Transform();
2498                                         }
2499                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2500                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
2501                                         }
2502                                 }
2503                                 else if(G.qual==LR_ALTKEY) {
2504                                         if(G.f & G_WEIGHTPAINT)
2505                                                 ob= ob->parent;
2506                                         if(ob && (ob->flag & OB_POSEMODE)) {
2507                                                 bArmature *arm= ob->data;
2508                                                 if( arm->drawtype==ARM_ENVELOPE) {
2509                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2510                                                         Transform();
2511                                                         break;
2512                                                 }
2513                                         }
2514                                         
2515                                         if(okee("Clear scale")) {
2516                                                 clear_object('s');
2517                                         }
2518                                 }
2519                                 else if(G.qual==LR_SHIFTKEY) {
2520                                         snapmenu();
2521                                 }
2522                                 else if((G.qual==0)) {
2523                                         initTransform(TFM_RESIZE, CTX_NONE);
2524                                         Transform();
2525                                 }
2526                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2527                                         initTransform(TFM_TOSPHERE, CTX_NONE);
2528                                         Transform();
2529                                 }
2530                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
2531                                         initTransform(TFM_SHEAR, CTX_NONE);
2532                                         Transform();
2533                                 }
2534                                 break;
2535                         case TKEY:
2536                                 if(G.qual == LR_SHIFTKEY) { /* toggle texture in solid draw mode */
2537                                         G.vd->flag2 ^= V3D_SOLID_TEX;
2538                                         allqueue(REDRAWVIEW3D, 0);
2539                                 } else if(G.obedit){
2540                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
2541                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
2542                                                 allqueue(REDRAWVIEW3D, 0);
2543                                                 if (EM_texFaceCheck())
2544                                                         allqueue(REDRAWIMAGE, 0);
2545                                                 
2546                                                 countall();
2547                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2548                                         }
2549                                         if (G.obedit->type==OB_CURVE) {
2550                                                 if (G.qual==LR_ALTKEY) {
2551                                                         clear_tilt();
2552                                                 }
2553                                                 else if (G.qual==0) {
2554                                                         initTransform(TFM_TILT, CTX_NONE);
2555                                                         Transform();
2556                                                 }
2557                                         }
2558                                 }
2559                                 else if(G.f & G_PARTICLEEDIT) {
2560                                         initTransform(TFM_BAKE_TIME, CTX_NONE);
2561                                         Transform();
2562                                 }
2563                                 else if(G.qual==LR_CTRLKEY) {
2564                                         if(ob && (ob->flag & OB_POSEMODE));
2565                                         else make_track();
2566                                 }
2567                                 else if(G.qual==LR_ALTKEY) {
2568                                         if(ob && (ob->flag & OB_POSEMODE));
2569                                         else clear_track();
2570                                 }
2571                                 else if((G.qual==0)){
2572                                         texspace_edit();
2573                                 }
2574                                 
2575                                 break;
2576                         case UKEY:
2577                                 /*// Use Ctrl Z like everybody else
2578                                 if(G.obedit) {
2579                                         if(G.obedit->type==OB_MESH) {
2580                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2581                                         }
2582                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
2583                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2584                                         }
2585                                 }*/
2586                                 if(G.obedit) {
2587                                         if(G.obedit->type==OB_MESH && G.qual==0) {
2588                                                 uv_autocalc_tface();
2589                                         }
2590                                 }
2591                                 else if (G.f & G_PARTICLEEDIT){
2592                                         if(G.qual==0) BIF_undo(); else BIF_redo();
2593                                 }
2594                                 else if((G.qual==0)) {
2595                                         if(G.f & G_WEIGHTPAINT)
2596                                                 BIF_undo();
2597                                         else if(G.f & G_VERTEXPAINT)
2598                                                 BIF_undo();
2599                                         else if(G.f & G_TEXTUREPAINT)
2600                                                 imagepaint_undo();
2601                                         else {
2602                                                 single_user();
2603                                         }
2604                                 }
2605                                 
2606                                 break;
2607                         case VKEY:
2608                                 if ((G.qual==LR_CTRLKEY)) {
2609                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
2610                                                 Vertex_Menu();
2611                                         }
2612                                 } else if((G.qual==LR_SHIFTKEY)) {
2613                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
2614                                                 align_view_to_selected(v3d);
2615                                         }
2616                                         else if (FACESEL_PAINT_TEST) {
2617                                                 align_view_to_selected(v3d);
2618                                         }
2619                                 }
2620                                 else if(G.qual==LR_ALTKEY)
2621                                         image_aspect();
2622                                 else if (G.qual==0){
2623                                         if(G.obedit) {
2624                                                 if(G.obedit->type==OB_MESH) {
2625                                                         mesh_rip();
2626                                                 }
2627                                                 else if(G.obedit->type==OB_CURVE) {
2628                                                         sethandlesNurb(2);
2629                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2630                                                         allqueue(REDRAWVIEW3D, 0);
2631                                                         BIF_undo_push("Handle change");
2632                                                 }
2633