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