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