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