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