merging harmonic-skeleton branch into trunk. All changes are hidden behind a disabled...
[blender.git] / source / blender / src / space.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  *
30  * - here initialize and free and handling SPACE data
31  */
32
33 #include <string.h>
34 #include <stdio.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "MEM_guardedalloc.h"
41 #include "MEM_CacheLimiterC-Api.h"
42
43 #ifdef INTERNATIONAL
44 #include "BIF_language.h"
45 #endif
46
47 #include "IMB_imbuf_types.h"
48 #include "IMB_imbuf.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
52 #include "BLI_gsqueue.h"
53 #include "BLI_linklist.h"
54
55 #include "DNA_action_types.h"
56 #include "DNA_armature_types.h"
57 #include "DNA_curve_types.h"
58 #include "DNA_group_types.h" /* used for select_same_group */
59 #include "DNA_gpencil_types.h"
60 #include "DNA_image_types.h"
61 #include "DNA_ipo_types.h"
62 #include "DNA_mesh_types.h"
63 #include "DNA_meshdata_types.h"
64 #include "DNA_modifier_types.h" /* used for select grouped hooks */
65 #include "DNA_object_types.h"
66 #include "DNA_particle_types.h"
67 #include "DNA_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                 /* - we consider manipulator a button, defaulting to leftmouse 
1252                  * - grease-pencil also defaults to leftmouse
1253                  */
1254                 if(event==LEFTMOUSE) {
1255 #ifndef DISABLE_PYTHON
1256                         /* run any view3d event handler script links */
1257                         if (sa->scriptlink.totscript) {
1258                                 if (BPY_do_spacehandlers(sa, event, val, SPACEHANDLER_VIEW3D_EVENT))
1259                                         return; /* return if event was processed (swallowed) by handler(s) */
1260                         }
1261 #endif
1262                         if(gpencil_do_paint(sa, L_MOUSE)) return;
1263                         if(BIF_do_manipulator(sa)) return;
1264                 }
1265                 else if(event==RIGHTMOUSE) {
1266                         if(gpencil_do_paint(sa, R_MOUSE)) return;
1267                 }
1268                 
1269                 /* swap mouse buttons based on user preference */
1270                 if (U.flag & USER_LMOUSESELECT) {
1271                         /* only swap mouse button for selection, in modes where it is relevant.
1272                          * painting/sculpting stays on LEFTMOUSE */
1273                         if (   !((G.f & G_SCULPTMODE) || (G.f & G_WEIGHTPAINT) || (G.f & G_GREASEPENCIL) ||
1274                                 (G.f & G_VERTEXPAINT) || (G.f & G_TEXTUREPAINT) || (G.f & G_PARTICLEEDIT)) ||
1275                                 (G.obedit) )
1276                         {                       
1277                                 if (event==LEFTMOUSE) event = RIGHTMOUSE;
1278                                 else if (event==RIGHTMOUSE) event = LEFTMOUSE;
1279                         }
1280                 }
1281
1282                 if(!mouse_in_header(sa)) {
1283                         if(!G.obedit && (G.f & G_SCULPTMODE)) {
1284                                 SculptSession *ss= sculpt_session();
1285                                 if(ss && ss->radialcontrol) {
1286                                         radialcontrol_do_events(ss->radialcontrol, event);
1287                                         return;
1288                                 }
1289                                 else if(event!=LEFTMOUSE && event!=MIDDLEMOUSE && (event==MOUSEY || event==MOUSEX)) {
1290                                         if(!bwin_qtest(sa->win))
1291                                                 allqueue(REDRAWVIEW3D, 0);
1292                                 }
1293                         }
1294                         else if(!G.obedit && OBACT && G.f&G_PARTICLEEDIT){
1295                                 ParticleSystem *psys=PE_get_current(OBACT);
1296                                 ParticleEditSettings *pset=PE_settings();
1297                                 if(*PE_radialcontrol()) {
1298                                         radialcontrol_do_events(*PE_radialcontrol(), event);
1299                                         return;
1300                                 }
1301                                 if(psys && psys->edit){
1302                                         if(pset->brushtype>=0 &&
1303                                                 event!=LEFTMOUSE && event!=RIGHTMOUSE && event!=MIDDLEMOUSE &&
1304                                                 (event==MOUSEY || event==MOUSEX) && bwin_qtest(sa->win)==0) {
1305                                                 allqueue(REDRAWVIEW3D, 0);
1306                                         }
1307                                 }
1308                         }
1309
1310                         /* Handle retopo painting */
1311                         if(retopo_mesh_paint_check()) {
1312                                 if(!retopo_paint(event))
1313                                         return;
1314                         }
1315                 }
1316
1317 #ifndef DISABLE_PYTHON
1318                 /* run any view3d event handler script links */
1319                 if (event && sa->scriptlink.totscript)
1320                         if (BPY_do_spacehandlers(sa, event, val, SPACEHANDLER_VIEW3D_EVENT))
1321                                 return; /* return if event was processed (swallowed) by handler(s) */
1322 #endif
1323
1324                 /* TEXTEDITING?? */
1325                 if((G.obedit) && G.obedit->type==OB_FONT) {
1326                         switch(event) {
1327                         
1328                         case LEFTMOUSE:
1329                                 mouse_cursor();
1330                                 break;
1331                         case MIDDLEMOUSE:
1332                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
1333                                 if(U.flag & USER_VIEWMOVE) {
1334                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1335                                                 viewmove(0);
1336                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1337                                                 viewmove(2);
1338                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1339                                                 viewmove(1);
1340                                 }
1341                                 else {
1342                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1343                                                 viewmove(1);
1344                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1345                                                 viewmove(2);
1346                                         else
1347                                                 viewmove(0);
1348                                 }
1349                                 break;
1350                                 
1351                         case WHEELUPMOUSE:
1352                                 /* Regular:   Zoom in */
1353                                 /* Shift:     Scroll up */
1354                                 /* Ctrl:      Scroll right */
1355                                 /* Alt-Shift: Rotate up */
1356                                 /* Alt-Ctrl:  Rotate right */
1357
1358                                 if( G.qual & LR_SHIFTKEY ) {
1359                                         if( G.qual & LR_ALTKEY ) { 
1360                                                 G.qual &= ~LR_SHIFTKEY;
1361                                                 persptoetsen(PAD2);
1362                                                 G.qual |= LR_SHIFTKEY;
1363                                         } else {
1364                                                 persptoetsen(PAD2);
1365                                         }
1366                                 } else if( G.qual & LR_CTRLKEY ) {
1367                                         if( G.qual & LR_ALTKEY ) { 
1368                                                 G.qual &= ~LR_CTRLKEY;
1369                                                 persptoetsen(PAD4);
1370                                                 G.qual |= LR_CTRLKEY;
1371                                         } else {
1372                                                 persptoetsen(PAD4);
1373                                         }
1374                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1375                                         persptoetsen(PADMINUS);
1376                                 else
1377                                         persptoetsen(PADPLUSKEY);
1378
1379                                 doredraw= 1;
1380                                 break;
1381
1382                         case WHEELDOWNMOUSE:
1383                                 /* Regular:   Zoom out */
1384                                 /* Shift:     Scroll down */
1385                                 /* Ctrl:      Scroll left */
1386                                 /* Alt-Shift: Rotate down */
1387                                 /* Alt-Ctrl:  Rotate left */
1388
1389                                 if( G.qual & LR_SHIFTKEY ) {
1390                                         if( G.qual & LR_ALTKEY ) { 
1391                                                 G.qual &= ~LR_SHIFTKEY;
1392                                                 persptoetsen(PAD8);
1393                                                 G.qual |= LR_SHIFTKEY;
1394                                         } else {
1395                                                 persptoetsen(PAD8);
1396                                         }
1397                                 } else if( G.qual & LR_CTRLKEY ) {
1398                                         if( G.qual & LR_ALTKEY ) { 
1399                                                 G.qual &= ~LR_CTRLKEY;
1400                                                 persptoetsen(PAD6);
1401                                                 G.qual |= LR_CTRLKEY;
1402                                         } else {
1403                                                 persptoetsen(PAD6);
1404                                         }
1405                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1406                                         persptoetsen(PADPLUSKEY);
1407                                 else
1408                                         persptoetsen(PADMINUS);
1409                                 
1410                                 doredraw= 1;
1411                                 break;
1412
1413                         case UKEY:
1414                                 if(G.qual==LR_ALTKEY) {
1415                                         remake_editText();
1416                                         doredraw= 1;
1417                                 } 
1418                                 else {
1419                                         do_textedit(event, val, ascii);
1420                                 }
1421                                 break;
1422                         case VKEY:
1423                                 if(G.qual==LR_ALTKEY) {
1424                                         paste_editText();
1425                                         doredraw= 1;
1426                                 } 
1427                                 else {
1428                                         do_textedit(event, val, ascii);
1429                                 }
1430                                 break;
1431                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1432                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1433                         case PADENTER:
1434                                 persptoetsen(event);
1435                                 doredraw= 1;
1436                                 break;
1437                                 
1438                         default:
1439                                 do_textedit(event, val, ascii);
1440                                 break;
1441                         }
1442                 }
1443                 else if(!G.obedit && (G.f & G_SCULPTMODE)) {
1444                         SculptData *sd= sculpt_data();
1445                         SculptSession *ss= sculpt_session();
1446                         BrushData *br= sculptmode_brush();
1447                         Mesh *me= get_mesh(OBACT);
1448                         char update_prop= 0;
1449                         
1450                         if(U.flag & USER_NONUMPAD) {
1451                                 event= convert_for_nonumpad(event);
1452                         }
1453                         
1454                         switch(event) {
1455                         case LEFTMOUSE:
1456                                 if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1457                                         sculptmode_pmv(0);
1458                                 else if(!(ss && ss->radialcontrol))
1459                                         sculpt();
1460                                 break;
1461                         /* View */
1462                         case MIDDLEMOUSE:
1463                                 handle_view_middlemouse();
1464                                 break;
1465                         case WHEELUPMOUSE:
1466                                 handle_view_wheelup();
1467                                 doredraw= 1;
1468                                 break;
1469                         case WHEELDOWNMOUSE:
1470                                 handle_view_wheeldown();
1471                                 doredraw= 1;
1472                                 break;
1473                         case RIGHTMOUSE:
1474                                 if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1475                                         sculptmode_pmv(1);
1476                                 break;
1477                         case CKEY:
1478                                 if(G.qual==LR_SHIFTKEY) {
1479                                         view3d_home(1);
1480                                         curs= give_cursor();
1481                                         curs[0]=curs[1]=curs[2]= 0.0;
1482                                         allqueue(REDRAWVIEW3D, 0);
1483                                 }
1484                                 else if(G.qual==0) {
1485                                         if (G.vd->persp==V3D_CAMOB)
1486                                                 /* center the camera offset */
1487                                                 G.vd->camdx= G.vd->camdy= 0.0;
1488                                         else {
1489                                                 /*non camera center*/
1490                                                 float new_ofs[3];
1491                                                 curs= give_cursor();
1492                                                 new_ofs[0]= -curs[0];
1493                                                 new_ofs[1]= -curs[1];
1494                                                 new_ofs[2]= -curs[2];
1495                                                 smooth_view(G.vd, new_ofs, NULL, NULL, NULL);
1496                                         }
1497                                 }
1498                                 doredraw= 1;
1499                                 break;
1500                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1501                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1502                         case PADPLUSKEY:
1503                         case PADMINUS:
1504                         case PADENTER:
1505                                 persptoetsen(event);
1506                                 doredraw= 1;
1507                                 break;
1508                         case BKEY:
1509                                 if(G.qual==LR_ALTKEY)
1510                                         view3d_edit_clipping(v3d);
1511                                 else if(G.qual==LR_SHIFTKEY) {
1512                                         if(G.vd->persp==V3D_CAMOB)
1513                                                 set_render_border();
1514                                         else
1515                                                 view3d_border_zoom();
1516                                 }
1517                                 break;
1518                         case PADSLASHKEY:
1519                                 if(!G.qual) {
1520                                         if(G.vd->localview) {
1521                                                 G.vd->localview= 0;
1522                                                 endlocalview(curarea);
1523                                         }
1524                                         else {
1525                                                 G.vd->localview= 1;
1526                                                 initlocalview();
1527                                         }
1528                                         allqueue(REDRAWVIEW3D, 1);
1529                                 }
1530                                 break;
1531                         /* Brush properties */
1532                         case AKEY:
1533                                 if(G.qual==LR_SHIFTKEY)
1534                                         br->flag ^= SCULPT_BRUSH_ANCHORED;
1535                                 else
1536                                         br->flag ^= SCULPT_BRUSH_AIRBRUSH;
1537                                 update_prop= 1; break;
1538                         case FKEY:
1539                                 if(ss) {
1540                                         sculpt_radialcontrol_start(G.qual == 0 ? RADIALCONTROL_SIZE :
1541                                                                    G.qual == LR_SHIFTKEY ? RADIALCONTROL_STRENGTH :
1542                                                                    G.qual == LR_CTRLKEY ? RADIALCONTROL_ROTATION :
1543                                                                    RADIALCONTROL_NONE);
1544                                 }
1545                                 break;
1546                         case VKEY:
1547                                 br->dir= br->dir==1 ? 2 : 1;
1548                                 update_prop= 1; break;
1549                         /* Set brush */
1550                         case DKEY:
1551                                 sd->brush_type= DRAW_BRUSH;
1552                                 update_prop= 1; break;
1553                         case SKEY:
1554                                 if(G.qual==LR_SHIFTKEY)
1555                                         sd->flags ^= SCULPT_INPUT_SMOOTH;
1556                                 else {
1557                                         sd->brush_type= SMOOTH_BRUSH;
1558                                         update_prop= 1;
1559                                 }
1560                                 break;
1561                         case PKEY:
1562                                 sd->brush_type= PINCH_BRUSH;
1563                                 update_prop= 1; break;
1564                         case IKEY:
1565                                 sd->brush_type= INFLATE_BRUSH;
1566                                 update_prop= 1; break;
1567                         case GKEY:
1568                                 sd->brush_type= GRAB_BRUSH;
1569                                 update_prop= 1; break;
1570                         case LKEY:
1571                                 sd->brush_type= LAYER_BRUSH;
1572                                 update_prop= 1; break;
1573                         case TKEY:
1574                                 sd->brush_type= FLATTEN_BRUSH;
1575                                 update_prop= 1; break;
1576                         /* Symmetry */
1577                         case XKEY:
1578                                 sd->symm^= SYMM_X;
1579                                 update_prop= 1; break;
1580                         case YKEY:
1581                                 sd->symm^= SYMM_Y;
1582                                 update_prop= 1; break;
1583                         case ZKEY:
1584                                 sd->symm^= SYMM_Z;
1585                                 update_prop= 1; break;
1586                         /* Interface */
1587                         case NKEY:
1588                                 if(G.qual==0) {
1589                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1590                                         allqueue(REDRAWVIEW3D, 0);
1591                                 }
1592                                 break;
1593                         /* Multires */
1594                         case PAGEUPKEY:
1595                                 if(me && me->mr) {
1596                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current+1;
1597                                         multires_set_level_cb(ob, ob->data);
1598                                 }
1599                                 break;
1600                         case PAGEDOWNKEY:
1601                                 if(me && me->mr) {
1602                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current-1;
1603                                         multires_set_level_cb(ob, ob->data);
1604                                 }
1605                                 break;
1606                         /* Partial Visibility */
1607                         case HKEY:
1608                                 if(G.qual==LR_ALTKEY) {
1609                                         waitcursor(1);
1610                                         mesh_pmv_off(ob, get_mesh(ob));
1611                                         BIF_undo_push("Partial mesh hide");
1612                                         allqueue(REDRAWVIEW3D,0);
1613                                         waitcursor(0);
1614                                 }
1615                                 break;
1616                         /* Layers */
1617                         case ONEKEY:
1618                                 do_layer_buttons(0); break;
1619                         case TWOKEY:
1620                                 do_layer_buttons(1); break;
1621                         case THREEKEY:
1622                                 do_layer_buttons(2); break;
1623                         case FOURKEY:
1624                                 do_layer_buttons(3); break;
1625                         case FIVEKEY:
1626                                 do_layer_buttons(4); break;
1627                         case SIXKEY:
1628                                 do_layer_buttons(5); break;
1629                         case SEVENKEY:
1630                                 do_layer_buttons(6); break;
1631                         case EIGHTKEY:
1632                                 do_layer_buttons(7); break;
1633                         case NINEKEY:
1634                                 do_layer_buttons(8); break;
1635                         case ZEROKEY:
1636                                 do_layer_buttons(9); break;
1637                         case MINUSKEY:
1638                                 do_layer_buttons(10); break;
1639                         case EQUALKEY:
1640                                 do_layer_buttons(11); break;
1641                         case ACCENTGRAVEKEY:
1642                                 do_layer_buttons(-1); break;
1643                         
1644                         case NDOFMOTION:                
1645                                 if (G.vd->ndofmode == 0) {
1646                                         viewmoveNDOF(1);
1647                                 } else if (G.vd->ndofmode == 1) {
1648                                         viewmoveNDOFfly(1);
1649                                 } else {
1650                                         NDofTransform();
1651                                 }
1652                 break;
1653                                 
1654             case NDOFBUTTON:
1655                                 if (val == 1) {
1656                                         G.vd->ndofmode +=1;
1657                                         if (G.vd->ndofmode > 2)         /* we have currently 3 modes : 0 original, 1 fly, 2 transform */
1658                                                 G.vd->ndofmode = 0;
1659                                 }
1660                                 if (val == 2) {
1661                                         G.vd->ndoffilter =(G.vd->ndoffilter == 1 ? 0 : 1);
1662                                 }
1663                                 allqueue(REDRAWHEADERS, 0);
1664                 break;
1665                                 
1666                         }
1667                         
1668                         /* Redraw buttons window as well as view 3d (for floating panel) */
1669                         if(update_prop) {
1670                                 allqueue(REDRAWVIEW3D, 0);
1671                                 allqueue(REDRAWBUTSEDIT, 0);
1672                         }
1673                 } else {
1674
1675                         if (U.flag & USER_NONUMPAD) {
1676                                 event= convert_for_nonumpad(event);
1677                         }
1678
1679                         switch(event) {
1680                         
1681                         /* Afterqueue events */
1682                         case BACKBUFDRAW:
1683                                 backdrawview3d(1);
1684                                 break;
1685                         case RENDERPREVIEW:
1686                                 BIF_view3d_previewrender(sa);
1687                                 break;
1688                                 
1689                         case TABKEY:
1690                                 /* Shift-Tabe handling (other cases are in toets) */
1691                                 if (G.qual == LR_SHIFTKEY)
1692                                 {
1693                                         /* Snap toggle only when supported */
1694                                         if (BIF_snappingSupported())
1695                                         {
1696                                                 G.scene->snap_flag ^= SCE_SNAP;
1697                                                 allqueue(REDRAWHEADERS, 0);
1698                                         }                                       
1699                                 }
1700                                 break;
1701                                 
1702                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1703                          * based on user preference USER_LMOUSESELECT
1704                          */
1705                         case LEFTMOUSE: 
1706                                 if ((G.obedit) || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT|G_PARTICLEEDIT))) {
1707                                         mouse_cursor();
1708                                 }
1709                                 else if (G.f & G_WEIGHTPAINT) {
1710                                         weight_paint();
1711                                 }
1712                                 else if (G.f & G_VERTEXPAINT) {
1713                                         vertex_paint();
1714                                 }
1715                                 else if (G.f & G_TEXTUREPAINT) {
1716                                         imagepaint_paint(L_MOUSE, 1);
1717                                 }
1718                                 else if (G.f & G_PARTICLEEDIT) {
1719                                         if(G.qual & LR_CTRLKEY)
1720                                                 mouse_cursor();
1721                                         else if(!PE_brush_particles())
1722                                                 mouse_cursor();
1723                                 }
1724                                 break;
1725                         case MIDDLEMOUSE:
1726                                 handle_view_middlemouse();
1727                                 break;
1728                         case RIGHTMOUSE:
1729                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
1730                                         if(G.obedit->type==OB_MESH)
1731                                                 mouse_mesh();
1732                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1733                                                 mouse_nurb();
1734                                         else if(G.obedit->type==OB_MBALL)
1735                                                 mouse_mball();
1736                                         else if(G.obedit->type==OB_LATTICE)
1737                                                 mouse_lattice();
1738                                         else if(G.obedit->type==OB_ARMATURE)
1739                                                 mouse_armature();
1740                                 }
1741                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
1742                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1743                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
1744                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1745                                 else if(G.qual==LR_CTRLKEY)
1746                                         mouse_select(); /* also allow in editmode, for vertex parenting */
1747                                 else if(FACESEL_PAINT_TEST)
1748                                         face_select();
1749                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
1750                                         sample_vpaint();
1751                                 else if( G.f & G_PARTICLEEDIT)
1752                                         PE_mouse_particles();
1753                                 else
1754                                         mouse_select(); /* does poses too */
1755                                 break;
1756                         case WHEELUPMOUSE:
1757                                 handle_view_wheelup();
1758                                 doredraw= 1;
1759                                 break;
1760                         case WHEELDOWNMOUSE:
1761                                 handle_view_wheeldown();
1762                                 doredraw= 1;
1763                                 break;
1764
1765             case NDOFMOTION:            
1766                                 if (G.vd->ndofmode == 0) {
1767                                         viewmoveNDOF(1);
1768                                 } else if (G.vd->ndofmode == 1) {
1769                                         viewmoveNDOFfly(1);
1770                                 } else {
1771                                         NDofTransform();
1772                                 }
1773                 break;
1774
1775             case NDOFBUTTON:
1776                                 if (val == 1) {
1777                                         G.vd->ndofmode +=1;
1778                                         if (G.vd->ndofmode > 2)         /* we have currently 3 modes : 0 original, 1 fly, 2 transform */
1779                                                 G.vd->ndofmode = 0;
1780                                 }
1781                                 if (val == 2) {
1782                                         G.vd->ndoffilter =(G.vd->ndoffilter == 1 ? 0 : 1);
1783                                 }
1784                                 allqueue(REDRAWHEADERS, 0);
1785                 break;
1786                                 
1787                         case ONEKEY:
1788                                 if(G.qual==LR_CTRLKEY) {
1789                                         flip_subdivison(1);
1790                                 }
1791                                 else do_layer_buttons(0); 
1792                                 break;
1793                                 
1794                         case TWOKEY:
1795                                 if(G.qual==LR_CTRLKEY) {
1796                                         flip_subdivison(2);
1797                                 }
1798                                 else do_layer_buttons(1); 
1799                                 break;
1800                                 
1801                         case THREEKEY:
1802                                 if(G.qual==LR_CTRLKEY) {
1803                                         flip_subdivison(3);
1804                                 }
1805                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1806                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1807                                                 select_faces_by_numverts(3);
1808                                 }
1809                                 else do_layer_buttons(2); 
1810                                 break;
1811                                 
1812                         case FOURKEY:
1813                                 if(G.qual==LR_CTRLKEY) {
1814                                         flip_subdivison(4);
1815                                 }
1816                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1817                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1818                                                 select_faces_by_numverts(4);
1819                                 }
1820                                 else do_layer_buttons(3); 
1821                                 break;
1822                                 
1823                         case FIVEKEY:
1824                                 if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1825                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1826                                                 select_faces_by_numverts(5);
1827                                 }
1828                                 
1829                                 else if(G.qual==LR_CTRLKEY) {}
1830                                 else do_layer_buttons(4);
1831                                 break;
1832
1833                         case SIXKEY:
1834                                 if(G.qual==LR_CTRLKEY) {}
1835                                 else do_layer_buttons(5);
1836                                 break;
1837                         case SEVENKEY:
1838                                 if(G.qual==LR_CTRLKEY) {}
1839                                 else do_layer_buttons(6);
1840                                 break;
1841                         case EIGHTKEY:
1842                                 if(G.qual==LR_CTRLKEY) {}
1843                                 else do_layer_buttons(7);
1844                                 break;
1845                         case NINEKEY:
1846                                 if(G.qual==LR_CTRLKEY) {}
1847                                 else do_layer_buttons(8);
1848                                 break;
1849                         case ZEROKEY:
1850                                 if(G.qual==LR_CTRLKEY) {}
1851                                 else do_layer_buttons(9);
1852                                 break;
1853                         case MINUSKEY:
1854                                 if(G.qual==LR_CTRLKEY) {}
1855                                 else do_layer_buttons(10);
1856                                 break;
1857                         case EQUALKEY:
1858                                 if(G.qual==LR_CTRLKEY) {}
1859                                 else do_layer_buttons(11);
1860                                 break;
1861                         case ACCENTGRAVEKEY:
1862                                 do_layer_buttons(-1);
1863                                 break;
1864                         
1865                         case SPACEKEY:
1866                                 if(G.qual == LR_CTRLKEY) {
1867                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1868                                         if(val>0) {
1869                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1870                                                 else {
1871                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1872                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1873                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1874                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1875                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1876                                                 }
1877                                                 doredraw= 1;
1878                                         }
1879                                 }
1880                                 else if(G.qual == LR_ALTKEY) {
1881                                         BIF_selectOrientation();
1882                                         doredraw= 1;
1883                                 }
1884
1885                                 break;
1886                                 
1887                         case AKEY:
1888                                 if(G.qual == (LR_CTRLKEY|LR_ALTKEY)) {
1889                                         if(G.obedit == 0)
1890                                                 alignmenu();
1891                                         else if(G.obedit->type==OB_ARMATURE)
1892                                                 align_selected_bones();
1893                                 }
1894                                 else if(G.qual & LR_CTRLKEY) { /* also with shift! */
1895                                         apply_object(); 
1896                                 }
1897                                 else if((G.qual==LR_SHIFTKEY)) {
1898                                         toolbox_n_add();
1899                                 }
1900                                 else {
1901                                         if(G.obedit) {
1902                                                 if(G.obedit->type==OB_MESH)
1903                                                         deselectall_mesh();
1904                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1905                                                         deselectall_nurb();
1906                                                 else if(G.obedit->type==OB_MBALL)
1907                                                         deselectall_mball();
1908                                                 else if(G.obedit->type==OB_LATTICE)
1909                                                         deselectall_Latt();
1910                                                 else if(G.obedit->type==OB_ARMATURE)
1911                                                         deselectall_armature(1, 1);     /* 1 == toggle */
1912                                         }
1913                                         else if (ob && (ob->flag & OB_POSEMODE)){
1914                                                 deselectall_posearmature(ob, 1, 1);
1915                                         }
1916                                         else {
1917                                                 if(FACESEL_PAINT_TEST) deselectall_tface();
1918                                                 else if(G.f & G_PARTICLEEDIT) PE_deselectall();
1919                                                 else {
1920                                                         /* by design, the center of the active object 
1921                                                          * (which need not necessarily by selected) will
1922                                                          * still be drawn as if it were selected.
1923                                                          */
1924                                                         deselectall();
1925                                                 }
1926                                         }
1927                                 }
1928                                 break;
1929                         case BKEY:
1930                                 if(G.qual==LR_ALTKEY)
1931                                         view3d_edit_clipping(v3d);
1932                                 else if(G.qual==LR_SHIFTKEY)
1933                                 {
1934                                         if(G.vd->persp==V3D_CAMOB)
1935                                                 set_render_border();
1936                                         else
1937                                                 view3d_border_zoom();
1938                                 }
1939                                 else if(G.qual==LR_CTRLKEY) {
1940                                         extern void pointcache_bake(PTCacheID *pid, int startframe);
1941                                         extern void pointcache_free(PTCacheID *pid, int cacheonly);
1942                                         extern void fluidsimBake(Object *ob);
1943                                         extern void fluidsimFreeBake(Object *ob);
1944                                         int pupval;
1945
1946                                         pupval= pupmenu("Physics Baking%t|Bake selected %x1|Free bake selected %x2|Free cache selected %x3");
1947
1948                                         if(pupval > 0) {
1949                                                 if(pupval == 1) {
1950                                                         pointcache_bake(NULL, 0);
1951                                                         /* also bake first domain of selected objects... */
1952                                                         fluidsimBake(NULL);
1953                                                 }
1954                                                 else if(pupval == 2) {
1955                                                         pointcache_free(NULL, 0);
1956                                                         fluidsimFreeBake(NULL);
1957                                                 }
1958                                                 else if(pupval == 3)
1959                                                         pointcache_free(NULL, 1);
1960                                         }
1961                                 }
1962                                 else if(G.qual== (LR_ALTKEY|LR_CTRLKEY))
1963                                         objects_bake_render_menu();
1964                                 else if(G.qual==0)
1965                                         borderselect();
1966                                 break;
1967                         case CKEY:
1968                                 if(G.qual==LR_CTRLKEY) {
1969                                         if(ob && (ob->flag & OB_POSEMODE))
1970                                                 pose_copy_menu();       /* poseobject.c */
1971                                         else
1972                                                 copy_attr_menu();
1973                                 }
1974                                 else if(G.qual==(LR_ALTKEY|LR_SHIFTKEY)) 
1975                                         gpencil_convert_menu(); /* gpencil.c */
1976                                 else if(G.qual==LR_ALTKEY) {
1977                                         if(ob && (ob->flag & OB_POSEMODE))
1978                                                 pose_clear_constraints();       /* poseobject.c */
1979                                         else
1980                                                 convertmenu();  /* editobject.c */
1981                                 }
1982                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY)) 
1983                                         add_constraint(0);      /* editconstraint.c, generic for objects and posemode */
1984                                 else if(G.qual==(LR_CTRLKEY|LR_SHIFTKEY)) {
1985                                         BIF_manageTransformOrientation(0, 1);
1986                                         allqueue(REDRAWVIEW3D, 0);
1987                                 }
1988                                 else if((G.qual==LR_SHIFTKEY)) {
1989                                         view3d_home(1);
1990                                         curs= give_cursor();
1991                                         curs[0]=curs[1]=curs[2]= 0.0;
1992                                         allqueue(REDRAWVIEW3D, 0);
1993                                 }
1994                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1995                                         makecyclicNurb();
1996                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1997                                         allqueue(REDRAWVIEW3D, 0);
1998                                 }
1999                                 else if((G.qual==0)){
2000                                         if (G.vd->persp==V3D_CAMOB)
2001                                                 /* center the camera offset */
2002                                                 G.vd->camdx= G.vd->camdy= 0.0;
2003                                         else {
2004                                                 /*non camera center*/
2005                                                 float new_ofs[3];
2006                                                 curs= give_cursor();
2007                                                 new_ofs[0]= -curs[0];
2008                                                 new_ofs[1]= -curs[1];
2009                                                 new_ofs[2]= -curs[2];
2010                                                 smooth_view(G.vd, new_ofs, NULL, NULL, NULL);
2011                                         }
2012                                         scrarea_queue_winredraw(curarea);
2013                                 }
2014                                 break;
2015                         case DKEY:
2016                                 if((G.qual==LR_SHIFTKEY)) {
2017                                         duplicate_context_selected();
2018                                 }
2019                                 else if(G.qual==LR_ALTKEY) {
2020                                         if(ob && (ob->flag & OB_POSEMODE))
2021                                                 error ("Duplicate not possible in posemode.");
2022                                         else if((G.obedit==NULL))
2023                                                 adduplicate(0, 0);
2024                                 }
2025                                 else if(G.qual==LR_CTRLKEY) {
2026                                         imagestodisplist(); // removed
2027                                 }
2028                                 else if((G.qual==0)){
2029                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
2030                                         if(pupval>0) {
2031                                                 G.vd->drawtype= pupval;
2032                                                 doredraw= 1;
2033                                         }
2034                 }
2035                                 
2036                                 break;
2037                         case EKEY:
2038                                 if (G.qual==0){
2039                                         if(G.obedit) {
2040                                                 if(G.obedit->type==OB_MESH)
2041                                                         extrude_mesh();
2042                                                 else if(G.obedit->type==OB_CURVE)
2043                                                         addvert_Nurb('e');
2044                                                 else if(G.obedit->type==OB_SURF)
2045                                                         extrude_nurb();
2046                                                 else if(G.obedit->type==OB_ARMATURE)
2047                                                         extrude_armature(0);
2048                                         }
2049                                 }
2050                                 else if (G.qual==LR_CTRLKEY) {
2051                                         if(G.obedit && G.obedit->type==OB_MESH)
2052                                                 Edge_Menu();
2053                                         else if (FACESEL_PAINT_TEST)
2054                                                 seam_mark_clear_tface(0);
2055                                 }
2056                                 else if (G.qual==LR_SHIFTKEY) {
2057                                         if (G.obedit && G.obedit->type==OB_MESH &&
2058                                             !multires_level1_test()) {
2059                                                 initTransform(TFM_CREASE, CTX_EDGE);
2060                                                 Transform();
2061                                         }
2062                                         else if (G.obedit && G.obedit->type==OB_ARMATURE) {
2063                                                 extrude_armature(1);
2064                                         }
2065                                 }
2066                                 else if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY)) {
2067                                         if (G.obedit && G.obedit->type==OB_MESH &&
2068                                             !multires_level1_test()) {
2069                                                 if (G.scene->selectmode & SCE_SELECT_VERTEX) {
2070                                                         initTransform(TFM_BWEIGHT, CTX_NONE);
2071                                                 }
2072                                                 else {
2073                                                         initTransform(TFM_BWEIGHT, CTX_EDGE);
2074                                                 }
2075                                                 Transform();
2076                                         }
2077                                 }
2078                                 break;
2079                         case FKEY:
2080                                 if(G.obedit) {
2081                                         if(G.obedit->type==OB_MESH) {
2082                                                 if(G.qual == LR_CTRLKEY)
2083                                                         Face_Menu();
2084                                                 else if((G.qual==LR_SHIFTKEY))
2085                                                         fill_mesh();
2086                                                 else if(G.qual==LR_ALTKEY)
2087                                                         beauty_fill();
2088                                                 else if(G.qual==(LR_CTRLKEY|LR_SHIFTKEY))
2089                                                         edge_flip();
2090                                                 else if (G.qual==0)
2091                                                         addedgeface_mesh();
2092                                                 else if ( G.qual == 
2093                                                          (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2094                                                         select_linked_flat_faces();
2095                                                 }
2096
2097                                         }
2098                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
2099                                                 addsegment_nurb();
2100                                         }
2101                                         else if(G.obedit->type == OB_ARMATURE) {
2102                                                 fill_bones_armature();
2103                                         }
2104                                 }
2105                                 else if(G.qual==LR_CTRLKEY)
2106                                         sort_faces();
2107                                 else if((G.qual==LR_SHIFTKEY)) {
2108                                         if(ob && (ob->flag & OB_POSEMODE))
2109                                            pose_activate_flipped_bone();
2110                                         else if(G.f & G_WEIGHTPAINT)
2111                                                 pose_activate_flipped_bone();
2112                                         else if(G.f & G_PARTICLEEDIT)
2113                                                 PE_radialcontrol_start(RADIALCONTROL_STRENGTH);
2114                                         else
2115                                                 fly();
2116                                 }
2117                                 else if((G.qual==LR_ALTKEY)) {
2118                                         if(ob && (ob->flag & OB_POSEMODE))
2119                                                 if(okee("Flip quaternion rotations"))
2120                                                         pose_flipquats();
2121                                 }
2122                                 else {
2123                                         if (G.f & (G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT)){
2124                                                 G.f ^= G_FACESELECT;
2125                                                 allqueue(REDRAWVIEW3D, 1);
2126                                                 allqueue(REDRAWBUTSEDIT, 1);
2127                                         }
2128                                         else if(G.f & G_PARTICLEEDIT) {
2129                                                 PE_radialcontrol_start(RADIALCONTROL_SIZE);
2130                                         } else {
2131                                                 pupmenu("Note%t|UV/Face Select was removed. Editmode now allows UV editing, Add a UV layer or Unwrap (UKey)");
2132                                         }
2133                                 }
2134                                 
2135                                 break;
2136                         case GKEY:
2137                                 if((G.qual == LR_CTRLKEY)) {
2138                                         if(G.obedit) {
2139                                                 if(ELEM(G.obedit->type, OB_MESH, OB_LATTICE))
2140                                                         vgroup_assign_with_menu();
2141                                         }
2142                                         else if(ob && (ob->flag & OB_POSEMODE))
2143                                                 pgroup_operation_with_menu();
2144                                         else
2145                                                 group_operation_with_menu();
2146                                 }
2147                                 else if((G.qual == (LR_CTRLKEY|LR_SHIFTKEY))) {
2148                                         if(G.obedit) {
2149                                                 if(ELEM(G.obedit->type, OB_MESH, OB_LATTICE))
2150                                                         vgroup_operation_with_menu();
2151                                         }
2152                                 }
2153                                 else if((G.qual==LR_SHIFTKEY)) {
2154                                         if(G.obedit) {
2155                                                 if(G.obedit->type==OB_MESH)
2156                                                         select_mesh_group_menu();
2157                                         } 
2158                                         else if(ob && (ob->flag & OB_POSEMODE))
2159                                                 pose_select_grouped_menu();
2160                                         else if (ob)
2161                                                 select_object_grouped_menu();
2162                                 }
2163                                 else if((G.obedit==0) && G.qual==LR_ALTKEY) {
2164                                         if(okee("Clear location")) {
2165                                                 clear_object('g');
2166                                         }
2167                                 }
2168                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2169                                         v3d->twtype= V3D_MANIP_TRANSLATE;
2170                                         doredraw= 1;
2171                                 }
2172                                 else if((G.qual==0)) {
2173                                         initTransform(TFM_TRANSLATION, CTX_NONE);
2174                                         Transform();
2175                                 }
2176                                 break;
2177                         case HKEY:
2178                                 if(G.obedit) {
2179                                         if(G.obedit->type==OB_MESH) {
2180                                                 if(G.qual==LR_CTRLKEY)
2181                                                         add_hook_menu();
2182                                                 else if(G.qual==LR_ALTKEY)
2183                                                         reveal_mesh();
2184                                                 else if((G.qual==LR_SHIFTKEY))
2185                                                         hide_mesh(1);
2186                                                 else if((G.qual==0)) 
2187                                                         hide_mesh(0);
2188                                         }
2189                                         else if(G.obedit->type== OB_SURF) {
2190                                                 if(G.qual==LR_CTRLKEY)
2191                                                         add_hook_menu();
2192                                                 else if(G.qual==LR_ALTKEY)
2193                                                         revealNurb();
2194                                                 else if((G.qual==LR_SHIFTKEY))
2195                                                         hideNurb(1);
2196                                                 else if((G.qual==0))
2197                                                         hideNurb(0);
2198                                         }
2199                                         else if(G.obedit->type==OB_CURVE) {
2200                                                 if(G.qual==LR_CTRLKEY)
2201                                                         add_hook_menu();
2202                                                 else if(G.qual==LR_ALTKEY)
2203                                                         revealNurb();
2204                                                 /* should be G.qual==LR_SHIFTKEY, but that is taken fro handles already */
2205                                                 else if((G.qual==(LR_ALTKEY|LR_SHIFTKEY))) 
2206                                                         hideNurb(1);
2207                                                 /* should be G.qual==0, but that is taken for handles already */
2208                                                 else if((G.qual==(LR_ALTKEY|LR_CTRLKEY))) 
2209                                                         hideNurb(0);
2210                                                 else {
2211                                                         if(G.qual==LR_CTRLKEY) /* conflict */
2212                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
2213                                                         else if((G.qual==LR_SHIFTKEY))
2214                                                                 sethandlesNurb(1);
2215                                                         else if((G.qual==0))
2216                                                                 sethandlesNurb(3);
2217                                                         
2218                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2219                                                         BIF_undo_push("Handle change");
2220                                                         allqueue(REDRAWVIEW3D, 0);
2221                                                 }
2222                                         }
2223                                         else if(G.obedit->type==OB_LATTICE) {
2224                                                 if(G.qual==LR_CTRLKEY) add_hook_menu();
2225                                         }
2226                                         else if(G.obedit->type==OB_MBALL) {
2227                                                 if(G.qual==LR_ALTKEY)
2228                                                         reveal_mball();
2229                                                 else if((G.qual==LR_SHIFTKEY))
2230                                                         hide_mball(1);
2231                                                 else if((G.qual==0)) 
2232                                                         hide_mball(0);
2233                                         }
2234                                         else if(G.obedit->type==OB_ARMATURE) {
2235                                                 if (G.qual==0)
2236                                                         hide_selected_armature_bones();
2237                                                 else if (G.qual==LR_SHIFTKEY)
2238                                                         hide_unselected_armature_bones();
2239                                                 else if (G.qual==LR_ALTKEY)
2240                                                         show_all_armature_bones();
2241                                         }
2242                                 }
2243                                 else if(FACESEL_PAINT_TEST)
2244                                         hide_tface();
2245                                 else if(G.f & G_PARTICLEEDIT) {
2246                                         if(G.qual == LR_ALTKEY)
2247                                                 PE_hide(0);
2248                                         else if(G.qual == LR_SHIFTKEY)
2249                                                 PE_hide(1);
2250                                         else if(G.qual == 0)
2251                                                 PE_hide(2);
2252                                 }
2253                                 else if(ob && (ob->flag & OB_POSEMODE)) {
2254                                         if (G.qual==0)
2255                                                 hide_selected_pose_bones();
2256                                         else if (G.qual==LR_SHIFTKEY)
2257                                                 hide_unselected_pose_bones();
2258                                         else if (G.qual==LR_ALTKEY)
2259                                                 show_all_pose_bones();
2260                                 }
2261                                 /* Object mode only, other modes are accounted for above */
2262                                 else if ((G.f & (G_VERTEXPAINT|G_TEXTUREPAINT|G_WEIGHTPAINT))==0) {
2263                                         if(G.qual==LR_CTRLKEY)hookmenu();
2264                                         else if(G.qual==LR_ALTKEY) show_objects();
2265                                         else if(G.qual==LR_SHIFTKEY) hide_objects(0);
2266                                         else hide_objects(1);
2267                                 }
2268                                 break;
2269                         case IKEY:
2270                                 if(G.qual==LR_CTRLKEY) {
2271                                         if((ob) && (ob->flag & OB_POSEMODE) && (ob->type==OB_ARMATURE))
2272                                                 deselectall_posearmature(ob, 3, 1);
2273                                         else if(ob && G.obedit) {
2274                                                 if(G.obedit->type == OB_ARMATURE)
2275                                                         deselectall_armature(3, 1);
2276                                                 else
2277                                                         selectswap_mesh();
2278                                         }
2279                                         else
2280                                                 selectswap();
2281                                 }
2282                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY)) {
2283                                         if(ob && (ob->flag & OB_POSEMODE) && ob->type==OB_ARMATURE)
2284                                                 pose_clear_IK();
2285                                 }
2286                                 else if(G.qual==LR_SHIFTKEY) {
2287                                         if(ob && (ob->flag & OB_POSEMODE) && ob->type==OB_ARMATURE)
2288                                                 pose_add_IK();
2289                                 }
2290                                 break;
2291                                 
2292                         case JKEY:
2293                                 if(G.qual==LR_CTRLKEY) {
2294                                         if( ob ) {
2295                                                 join_menu();
2296                                         }
2297                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF)) {
2298                                                 addsegment_nurb();
2299                                         } else {
2300                                                 error("Can't join unless there is an active object");
2301                                         }
2302                                         
2303                                 }
2304                                 else if(G.obedit) {
2305                                         if(G.obedit->type==OB_MESH) {
2306                                                 join_triangles();
2307                                         }
2308                                 }
2309
2310                                 break;
2311                         case KKEY:
2312                                 if(G.obedit) {
2313                                         if (G.obedit->type==OB_MESH) {
2314                                                 if (G.qual==LR_SHIFTKEY)
2315                                                         KnifeSubdivide(KNIFE_PROMPT);
2316                                                 else if (G.qual==0)
2317                                                         LoopMenu();
2318                                         }
2319                                         else if(G.obedit->type==OB_SURF)
2320                                                 printknots();
2321                                 }
2322                                 else {
2323                                         if((G.qual==LR_SHIFTKEY)) {
2324                                                 if(FACESEL_PAINT_TEST)
2325                                                         if (G.f & G_WEIGHTPAINT)
2326                                                                 clear_wpaint_selectedfaces();
2327                                                         else
2328                                                                 clear_vpaint_selectedfaces();
2329                                                 else if(G.f & G_VERTEXPAINT)
2330                                                         clear_vpaint();
2331                                                 else
2332                                                         select_select_keys();
2333                                         }
2334                                         else if (G.qual==0)
2335                                                 set_ob_ipoflags();
2336                                 }
2337                                 
2338                                 break;
2339                         case LKEY:
2340                                 if(G.obedit) {
2341                                         if(G.obedit->type==OB_MESH) {
2342                                                 if (G.qual & LR_CTRLKEY) {
2343                                                         if ((G.scene->selectmode & SCE_SELECT_FACE) == 0) {
2344                                                                 selectconnected_mesh_all(); /* normal select linked */
2345                                                         } else {
2346                                                                 selectconnected_delimit_mesh_all(); /* select linked with edge crease delimiting */
2347                                                         }
2348                                                 } else {
2349                                                         if ((G.scene->selectmode & SCE_SELECT_FACE) == 0) {
2350                                                                 selectconnected_mesh();
2351                                                         } else {
2352                                                                 selectconnected_delimit_mesh();
2353                                                         }
2354                                                 }
2355                                         }
2356                                         if(G.obedit->type==OB_ARMATURE)
2357                                                 selectconnected_armature();
2358                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
2359                                                 selectconnected_nurb();
2360                                 }
2361                                 else if(ob && (ob->flag & OB_POSEMODE)) {
2362                                         if (G.qual == LR_CTRLKEY) 
2363                                                 poselib_preview_poses(ob, 0);
2364                                         else if (G.qual == LR_SHIFTKEY) 
2365                                                 poselib_add_current_pose(ob, 0);
2366                                         else if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY))
2367                                                 poselib_rename_pose(ob);
2368                                         else if (G.qual == LR_ALTKEY)
2369                                                 poselib_remove_pose(ob, NULL);
2370                                         else
2371                                                 selectconnected_posearmature();
2372                                 }
2373                                 else {
2374                                         if(FACESEL_PAINT_TEST) {
2375                                                 if((G.qual==0))
2376                                                         select_linked_tfaces(0);
2377                                                 else if((G.qual==LR_SHIFTKEY))
2378                                                         select_linked_tfaces(1);
2379                                                 else if(G.qual==LR_CTRLKEY)
2380                                                         select_linked_tfaces(2);
2381                                         }
2382                                         else if(G.f & G_PARTICLEEDIT) {
2383                                                 if(G.qual==0)
2384                                                         PE_select_linked();
2385                                         }
2386                                         else {
2387                                                 if((G.qual==0))
2388                                                         make_local_menu();
2389                                                 else if((G.qual==LR_SHIFTKEY))
2390                                                         selectlinks_menu();
2391                                                 else if(G.qual==LR_CTRLKEY)
2392                                                         make_links_menu();
2393                                         }
2394                                 }
2395                                 break;
2396                         case MKEY:
2397                                 if(G.obedit){
2398                                         if (ELEM(G.qual, 0, LR_SHIFTKEY) && (G.obedit->type==OB_ARMATURE)) {
2399                                                 pose_movetolayer();
2400                                         }
2401                                         else if (G.qual==LR_ALTKEY) {
2402                                                 if (G.obedit->type == OB_MESH) {
2403                                                         mergemenu();
2404                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2405                                                 }
2406                                                 else if (G.obedit->type == OB_ARMATURE) {
2407                                                         merge_armature();
2408                                                 }
2409                                         }
2410                                         else if ((G.qual==0) || (G.qual==LR_CTRLKEY)) {
2411                                                 mirrormenu();
2412                                         }
2413                                         else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2414                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
2415                                         }
2416                                 }
2417                                 else if(G.qual & LR_CTRLKEY) {
2418                                         mirrormenu();
2419                                 }
2420                                 else if(G.qual==0 || G.qual==LR_SHIFTKEY) {
2421                                         if(ob && (ob->flag & OB_POSEMODE))
2422                                                 pose_movetolayer();
2423                                         else
2424                                                 movetolayer();
2425                                 }
2426                                 break;
2427                         case NKEY:
2428                                 if((G.qual==0)) {
2429                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
2430                                         allqueue(REDRAWVIEW3D, 0);
2431                                 }
2432                                 else if(G.obedit) {
2433                                         switch (G.obedit->type){
2434                                         case OB_ARMATURE:
2435                                                 if(G.qual==LR_CTRLKEY) {
2436                                                         pupval= pupmenu("Recalculate Bone Roll Angles%t|Clear Roll (Z-Axis Up) %x1|Align Z-Axis to 3D-Cursor %x2");
2437                                                         if (pupval > 0) {
2438                                                                 auto_align_armature(pupval - 1);
2439                                                                 allqueue(REDRAWVIEW3D, 0);
2440                                                         }
2441                                                 }
2442                                                 break;
2443                                         case OB_MESH: 
2444                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2445                                                         if(okee("Recalculate normals inside")) {
2446                                                                 righthandfaces(2);
2447                                                                 allqueue(REDRAWVIEW3D, 0);
2448                                                                 BIF_undo_push("Recalculate normals inside");
2449                                                         }
2450                                                 }
2451                                                 else if(G.qual==LR_CTRLKEY){
2452                                                         if(okee("Recalculate normals outside")) {
2453                                                                 righthandfaces(1);
2454                                                                 allqueue(REDRAWVIEW3D, 0);
2455                                                                 BIF_undo_push("Recalculate normals outside");
2456                                                         }
2457                                                 }
2458                                                 break;
2459                                         }
2460                                 }
2461                                 
2462                                 break;
2463                         case OKEY:
2464                                 if (G.obedit || G.f&G_PARTICLEEDIT) {
2465                                         if (G.qual==LR_SHIFTKEY) {
2466                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%7;
2467                                                 allqueue(REDRAWHEADERS, 0);
2468                                         }
2469                                         else if((G.qual==LR_ALTKEY)) {
2470                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
2471                                                 else G.scene->proportional= 2;
2472                                                 allqueue(REDRAWHEADERS, 0);
2473                                         }
2474                                         else if((G.qual==0)) {
2475                                                 G.scene->proportional= !G.scene->proportional;
2476                                                 allqueue(REDRAWHEADERS, 0);
2477                                         }
2478                                 }
2479                                 else if((G.qual==LR_SHIFTKEY || G.qual==(LR_ALTKEY|LR_SHIFTKEY))) {
2480                                         flip_subdivison(-1);
2481                                 }
2482                                 else if(G.qual==LR_ALTKEY) {
2483                                         if(okee("Clear origin")) {
2484                                                 clear_object('o');
2485                                         }
2486                                 }
2487                                 break;
2488
2489                         case PKEY:
2490                                 if(G.obedit) {
2491                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2492                                                 if(G.obedit->type==OB_ARMATURE)
2493                                                         make_bone_parent();
2494                                                 else
2495                                                         make_parent();
2496                                         }
2497                                         
2498                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
2499                                                 clear_bone_parent();
2500                                         else if((G.qual==0) && (G.obedit->type==OB_ARMATURE)) 
2501                                                 armature_select_hierarchy(BONE_SELECT_PARENT, 1); // 1 = add to selection
2502                                         else if((G.qual==(LR_CTRLKEY|LR_ALTKEY)) && (G.obedit->type==OB_ARMATURE))
2503                                                 separate_armature();
2504                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
2505                                                 separatemenu();
2506                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
2507                                                 separate_nurb();
2508                                         else if (G.qual==LR_SHIFTKEY) {
2509                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
2510                                                 Transform();
2511                                         }
2512                                 }
2513                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
2514                                         make_parent();
2515                                 else if(G.qual==LR_SHIFTKEY) {
2516                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_PREVIEW, 0);
2517                                         doredraw= 1;
2518                                 }
2519                                 else if(G.qual==(LR_ALTKEY|LR_SHIFTKEY)) {
2520                                         initTransform(TFM_PUSHPULL, CTX_NONE);
2521                                         Transform();
2522                                 }
2523                                 else if(G.qual==LR_ALTKEY)
2524                                         clear_parent();
2525                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY))
2526                                         make_proxy();
2527                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY|LR_SHIFTKEY)) {
2528                         start_RBSimulation();
2529                                 }
2530                                 else if((G.qual==0) && (OBACT) && (OBACT->type==OB_ARMATURE) && (OBACT->flag & OB_POSEMODE))
2531                                         pose_select_hierarchy(BONE_SELECT_PARENT, 1); // 1 = add to selection
2532                                 else if((G.qual==0)) {
2533                         start_game();
2534                                 }
2535                                 break;                          
2536                         case RKEY:
2537                                 if((G.obedit==0) && G.qual==LR_ALTKEY) {
2538                                         if(okee("Clear rotation")) {
2539                                                 clear_object('r');
2540                                         }
2541                                 } 
2542                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2543                                         v3d->twtype= V3D_MANIP_ROTATE;
2544                                         doredraw= 1;
2545                                 }
2546                                 else if (G.obedit) {
2547                                         if((G.qual==LR_SHIFTKEY)) {
2548                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
2549                                                         selectrow_nurb();
2550                                         }
2551                                         else if(G.qual==LR_CTRLKEY) {
2552                                                 if (G.obedit->type==OB_MESH) {
2553                                                         CutEdgeloop(1);
2554                                                         BIF_undo_push("Cut Edgeloop");
2555                                                 }
2556                                                 else if (G.obedit->type==OB_ARMATURE) {
2557                                                         initTransform(TFM_BONE_ROLL, CTX_NONE);
2558                                                         Transform();
2559                                                 }
2560                                         }
2561                                         else if((G.qual==0)) {
2562                                                 initTransform(TFM_ROTATION, CTX_NONE);
2563                                                 Transform();
2564                                         }
2565                                 }
2566                                 else if((G.qual==0)) {
2567                                         initTransform(TFM_ROTATION, CTX_NONE);
2568                                         Transform();
2569                                 }
2570                                 break;
2571                         case SKEY:
2572                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2573                                         v3d->twtype= V3D_MANIP_SCALE;
2574                                         doredraw= 1;
2575                                 }
2576                                 else if(G.obedit) {
2577                                         
2578                                         if(G.qual==LR_ALTKEY) {
2579                                                 if(G.obedit->type==OB_ARMATURE) {
2580                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2581                                                 }
2582                                                 else if (G.obedit->type==OB_CURVE) {
2583                                                         initTransform(TFM_CURVE_SHRINKFATTEN, CTX_NONE);
2584                                                 } else {
2585                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
2586                                                 }
2587                                                 Transform();
2588                                         }
2589                                         else if(G.qual==LR_CTRLKEY) {
2590                                                 initTransform(TFM_SHEAR, CTX_NONE);
2591                                                 Transform();
2592                                         }
2593                                         else if(G.qual==LR_SHIFTKEY)
2594                                                 snapmenu();
2595                                         else if(G.qual==0) {
2596                                                 if(G.obedit->type==OB_ARMATURE) {
2597                                                         bArmature *arm= G.obedit->data;
2598                                                         if(arm->drawtype==ARM_ENVELOPE)
2599                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
2600                                                         else
2601                                                                 initTransform(TFM_RESIZE, CTX_NONE);
2602                                                 }
2603                                                 else
2604                                                         initTransform(TFM_RESIZE, CTX_NONE);
2605                                                 Transform();
2606                                         }
2607                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
2608                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
2609                                                 Transform();
2610                                         }
2611                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2612                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
2613                                         }
2614                                 }
2615                                 else if(G.qual==LR_ALTKEY) {
2616                                         if(G.f & G_WEIGHTPAINT)
2617                                                 ob= ob->parent;
2618                                         if(ob && (ob->flag & OB_POSEMODE)) {
2619                                                 bArmature *arm= ob->data;
2620                                                 if( arm->drawtype==ARM_ENVELOPE) {
2621                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2622                                                         Transform();
2623                                                         break;
2624                                                 }
2625                                         }
2626                                         
2627                                         if(okee("Clear scale")) {
2628                                                 clear_object('s');
2629                                         }
2630                                 }
2631                                 else if(G.qual==LR_SHIFTKEY) {
2632                                         snapmenu();
2633                                 }
2634                                 else if((G.qual==0)) {
2635                                         initTransform(TFM_RESIZE, CTX_NONE);
2636                                         Transform();
2637                                 }
2638                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2639                                         initTransform(TFM_TOSPHERE, CTX_NONE);
2640                                         Transform();
2641                                 }
2642                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
2643                                         initTransform(TFM_SHEAR, CTX_NONE);
2644                                         Transform();
2645                                 }
2646                                 break;
2647                         case TKEY:
2648                                 if(G.qual == LR_SHIFTKEY) { /* toggle texture in solid draw mode */
2649                                         G.vd->flag2 ^= V3D_SOLID_TEX;
2650                                         allqueue(REDRAWVIEW3D, 0);
2651                                 } else if(G.obedit){
2652                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
2653                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
2654                                                 allqueue(REDRAWVIEW3D, 0);
2655                                                 if (EM_texFaceCheck())
2656                                                         allqueue(REDRAWIMAGE, 0);
2657                                                 
2658                                                 countall();
2659                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2660                                         }
2661                                         if (G.obedit->type==OB_CURVE) {
2662                                                 if (G.qual==LR_ALTKEY) {
2663                                                         clear_tilt();
2664                                                 }
2665                                                 else if (G.qual==0) {
2666                                                         initTransform(TFM_TILT, CTX_NONE);
2667                                                         Transform();
2668                                                 }
2669                                         }
2670                                 }
2671                                 else if(G.f & G_PARTICLEEDIT) {
2672                                         initTransform(TFM_BAKE_TIME, CTX_NONE);
2673                                         Transform();
2674                                 }
2675                                 else if(G.qual==LR_CTRLKEY) {
2676                                         if(ob && (ob->flag & OB_POSEMODE));
2677                                         else make_track();
2678                                 }
2679                                 else if(G.qual==LR_ALTKEY) {
2680                                         if(ob && (ob->flag & OB_POSEMODE));
2681                                         else clear_track();
2682                                 }
2683                                 else if((G.qual==0)){
2684                                         texspace_edit();
2685                                 }
2686                                 
2687                                 break;
2688                         case UKEY:
2689                                 /*// Use Ctrl Z like everybody else
2690                                 if(G.obedit) {
2691                                         if(G.obedit->type==OB_MESH) {
2692                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2693                                         }
2694                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
2695                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2696                                         }
2697                                 }*/
2698                                 if(G.obedit) {
2699                                         if(G.obedit->type==OB_MESH && G.qual==0) {
2700                                                 uv_autocalc_tface();
2701                                         }
2702                                 }
2703                                 else if (G.f & G_PARTICLEEDIT){
2704                                         if(G.qual==0) BIF_undo(); else BIF_redo();