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