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