Added select grouped property (objects with shared property names will be selected)
[blender.git] / source / blender / src / space.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  *
30  * - here initialize and free and handling SPACE data
31  */
32
33 #include <string.h>
34 #include <stdio.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "MEM_guardedalloc.h"
41 #include "MEM_CacheLimiterC-Api.h"
42
43 #ifdef INTERNATIONAL
44 #include "BIF_language.h"
45 #endif
46
47 #include "IMB_imbuf_types.h"
48 #include "IMB_imbuf.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
52 #include "BLI_gsqueue.h"
53 #include "BLI_linklist.h"
54
55 #include "DNA_action_types.h"
56 #include "DNA_armature_types.h"
57 #include "DNA_curve_types.h"
58 #include "DNA_group_types.h" /* used for select_same_group */
59 #include "DNA_gpencil_types.h"
60 #include "DNA_image_types.h"
61 #include "DNA_ipo_types.h"
62 #include "DNA_mesh_types.h"
63 #include "DNA_meshdata_types.h"
64 #include "DNA_modifier_types.h" /* used for select grouped hooks */
65 #include "DNA_object_types.h"
66 #include "DNA_particle_types.h"
67 #include "DNA_property_types.h"
68 #include "DNA_scene_types.h"
69 #include "DNA_screen_types.h"
70 #include "DNA_sequence_types.h"
71 #include "DNA_sound_types.h"
72 #include "DNA_space_types.h"
73 #include "DNA_userdef_types.h"
74 #include "DNA_view2d_types.h"
75 #include "DNA_view3d_types.h"
76
77 #include "BKE_blender.h"
78 #include "BKE_colortools.h"
79 #include "BKE_curve.h"
80 #include "BKE_depsgraph.h"
81 #include "BKE_DerivedMesh.h"
82 #include "BKE_displist.h"
83 #include "BKE_global.h"
84 #include "BKE_group.h"
85 #include "BKE_ipo.h"
86 #include "BKE_main.h"
87 #include "BKE_mesh.h"
88 #include "BKE_multires.h"
89 #include "BKE_node.h"
90 #include "BKE_pointcache.h"
91 #include "BKE_scene.h"
92 #include "BKE_sculpt.h"
93 #include "BKE_texture.h"
94 #include "BKE_utildefines.h"
95 #include "BKE_image.h" /* for IMA_TYPE_COMPOSITE and IMA_TYPE_R_RESULT */
96 #include "BKE_particle.h"
97 #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->parent)) return 0; /* we know OBACT is valid */
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         for (   group=G.main->group.first;
751                         group && group_count < GROUP_MENU_MAX;
752                         group=group->id.next
753                 ) {
754                 if (object_in_group (ob, group)) {
755                         ob_groups[group_count] = group;
756                         group_count++;
757                 }
758         }
759         
760         if (!group_count)
761                 return 0;
762         
763         else if (group_count == 1) {
764                 group = ob_groups[0];
765                 for (base= FIRSTBASE; base; base= base->next) {
766                         if (BASE_SELECTABLE(base) && !(base->flag & SELECT) && object_in_group(base->object, group)) {
767                                 base->flag |= SELECT;
768                                 base->object->flag |= SELECT;
769                                 changed = 1;
770                         }
771                 }
772                 return changed;
773         }
774         
775         /* build the menu. */
776         p += sprintf(str, "Groups%%t"); 
777         for (i=0; i<group_count; i++) {
778                 group = ob_groups[i];
779                 p += sprintf (p, "|%s%%x%i", group->id.name+2, i);
780         }
781         
782         menu = pupmenu (str);
783         if (menu == -1)
784                 return 0;
785         
786         group = ob_groups[menu];
787         for (base= FIRSTBASE; base; base= base->next) {
788                 if (!(base->flag & SELECT) && object_in_group(base->object, group)) {
789                         base->flag |= SELECT;
790                         base->object->flag |= SELECT;
791                         changed = 1;
792                 }
793         }
794         return changed;
795 }
796
797 static short select_object_hooks(Object *ob)
798 {
799         short changed = 0;
800         Base *base;
801         ModifierData *md;
802         HookModifierData *hmd;
803         
804         for (md = ob->modifiers.first; md; md=md->next) {
805                 if (md->type==eModifierType_Hook) {
806                         hmd= (HookModifierData*) md;
807                         if (hmd->object && !(hmd->object->flag & SELECT)) {
808                                 base= object_in_scene(hmd->object, G.scene);
809                                 if (base && BASE_SELECTABLE(base)) {
810                                         base->flag |= SELECT;
811                                         base->object->flag |= SELECT;
812                                         changed = 1;
813                                 }
814                         }
815                 }
816         }
817         return changed;
818 }
819
820 /* Select objects woth the same parent as the active (siblings),
821  * parent can be NULL also */
822 static short select_same_parent(Object *ob)     
823 {
824         short changed = 0;
825         Base *base;
826         
827         for (base= FIRSTBASE; base; base= base->next) {
828                 if (BASE_SELECTABLE(base) && (base->object->parent==ob->parent)  && !(base->flag & SELECT)) {
829                         base->flag |= SELECT;
830                         base->object->flag |= SELECT;
831                         changed = 1;
832                 }
833         }
834         return changed;
835 }
836
837 static short select_same_type(Object *ob)
838 {
839         short changed = 0;
840         Base *base;
841         
842         for (base= FIRSTBASE; base; base= base->next) {
843                 if (BASE_SELECTABLE(base) && (base->object->type == ob->type) && !(base->flag & SELECT)) {
844                         base->flag |= SELECT;
845                         base->object->flag |= SELECT;
846                         changed = 1;
847                 }
848         }
849         return changed;
850 }
851
852 static short select_same_layer(Object *ob)
853 {
854         char changed = 0;
855         Base *base = FIRSTBASE;
856         
857         while(base) {
858                 if (BASE_SELECTABLE(base) && (base->lay & ob->lay) && !(base->flag & SELECT)) {
859                         base->flag |= SELECT;
860                         base->object->flag |= SELECT;
861                         changed = 1;
862                 }
863                 base= base->next;
864         }
865         return changed;
866 }
867
868 static short select_same_index_object(Object *ob)
869 {
870         char changed = 0;
871         Base *base = FIRSTBASE;
872         
873         while(base) {
874                 if (BASE_SELECTABLE(base) && (base->object->index == ob->index) && !(base->flag & SELECT)) {
875                         base->flag |= SELECT;
876                         base->object->flag |= SELECT;
877                         changed = 1;
878                 }
879                 base= base->next;
880         }
881         return changed;
882 }
883
884 static short select_same_color(Object *ob)
885 {
886         char changed = 0;
887         Base *base = FIRSTBASE;
888         
889         while(base) {
890                 if (BASE_SELECTABLE(base) && !(base->flag & SELECT) && (FloatCompare(base->object->col, ob->col, 0.005))) {
891                         base->flag |= SELECT;
892                         base->object->flag |= SELECT;
893                         changed = 1;
894                 }
895                 base= base->next;
896         }
897         return changed;
898 }
899
900 static short objects_share_gameprop(Object *a, Object *b)
901 {
902         bProperty *prop;
903         /*make a copy of all its properties*/
904         
905         for( prop= a->prop.first; prop; prop = prop->next ) {
906                 if ( get_ob_property(b, prop->name) )
907                         return 1;
908         }
909         return 0;
910 }
911
912 static short select_same_gameprops(Object *ob)
913 {
914         char changed = 0;
915         Base *base = FIRSTBASE;
916         
917         while(base) {
918                 if (BASE_SELECTABLE(base) && !(base->flag & SELECT) && (objects_share_gameprop(base->object, ob))) {
919                         base->flag |= SELECT;
920                         base->object->flag |= SELECT;
921                         changed = 1;
922                 }
923                 base= base->next;
924         }
925         return changed;
926 }
927
928 void select_object_grouped(short nr)
929 {
930         Object *ob = OBACT;
931         short changed = 0;
932         
933         if (ob==NULL) return;
934         
935         if(nr==1)               changed = select_children(ob, 1);
936         else if(nr==2)  changed = select_children(ob, 0);
937         else if(nr==3)  changed = select_parent();
938         else if(nr==4)  changed = select_same_parent(ob);       
939         else if(nr==5)  changed = select_same_type(ob);
940         else if(nr==6)  changed = select_same_layer(ob);        
941         else if(nr==7)  changed = select_same_group(ob);
942         else if(nr==8)  changed = select_object_hooks(ob);
943         else if(nr==9)  changed = select_same_index_object(ob);
944         else if(nr==10) changed = select_same_color(ob);
945         else if(nr==11) changed = select_same_gameprops(ob);
946         
947         if (changed) {
948                 countall();
949                 allqueue(REDRAWVIEW3D, 0);
950                 allqueue(REDRAWBUTSOBJECT, 0);
951                 allspace(REMAKEIPO, 0);
952                 allqueue(REDRAWIPO, 0);
953                 BIF_undo_push("Select Grouped");
954         }
955 }
956
957 static void select_object_grouped_menu(void)
958 {
959         char *str;
960         short nr;
961
962         /* make menu string */
963         
964         str= MEM_mallocN(512, "groupmenu");
965         strcpy(str, "Select Grouped%t|Children%x1|"
966                     "Immediate Children%x2|Parent%x3|"
967                     "Siblings (Shared Parent)%x4|"
968                     "Objects of Same Type%x5|"
969                                 "Objects on Shared Layers%x6|"
970                 "Objects in Same Group%x7|"
971                 "Object Hooks%x8|"
972                                 "Object PassIndex%x9|"
973                                 "Object Color%x10|"
974                                 "Game Properties%x11");
975
976         /* here we go */
977         
978         nr= pupmenu(str);
979         MEM_freeN(str);
980         
981         select_object_grouped(nr);
982 }
983
984 void join_menu(void)
985 {
986         Object *ob= OBACT;
987         if (G.obedit) {
988                 error("This data does not support joining in editmode");
989                 return;
990         }
991         if (!ob) {
992                 error("Can't join unless there is an active object");
993                 return;
994         }
995         
996         if(ob->type == OB_MESH) {
997                 if(okee("Join selected meshes")==0) return;
998                 join_mesh();
999         } else if(ob->type == OB_CURVE) {
1000                 if(okee("Join selected curves")==0) return;
1001                 join_curve(OB_CURVE);
1002         } else if(ob->type == OB_SURF) {
1003                 if(okee("Join selected NURBS")==0) return;
1004                 join_curve(OB_SURF);
1005         } else if(ob->type == OB_ARMATURE) {
1006                 /*      Make sure the user wants to continue*/
1007                 if(okee("Join selected armatures")==0) return;
1008                 join_armature ();
1009         } else {
1010                 error("This object type doesn't support joining");
1011         }
1012 }
1013
1014 static unsigned short convert_for_nonumpad(unsigned short event)
1015 {
1016         if (event>=ZEROKEY && event<=NINEKEY) {
1017                 return event - ZEROKEY + PAD0;
1018         } else if (event==MINUSKEY) {
1019                 return PADMINUS;
1020         } else if (event==EQUALKEY) {
1021                 return PADPLUSKEY;
1022         } else if (event==BACKSLASHKEY) {
1023                 return PADSLASHKEY;
1024         } else {
1025                 return event;
1026         }
1027 }
1028
1029 /* *************** */
1030
1031 void BIF_undo_push(char *str)
1032 {
1033         if(G.obedit) {
1034                 if (U.undosteps == 0) return;
1035
1036                 if(G.obedit->type==OB_MESH)
1037                         undo_push_mesh(str);
1038                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1039                         undo_push_curve(str);
1040                 else if (G.obedit->type==OB_FONT)
1041                         undo_push_font(str);
1042                 else if (G.obedit->type==OB_MBALL)
1043                         undo_push_mball(str);
1044                 else if (G.obedit->type==OB_LATTICE)
1045                         undo_push_lattice(str);
1046                 else if (G.obedit->type==OB_ARMATURE)
1047                         undo_push_armature(str);
1048         }
1049         else if(G.f & G_PARTICLEEDIT) {
1050                 if (U.undosteps == 0) return;
1051
1052                 PE_undo_push(str);
1053         }
1054         else {
1055                 if(U.uiflag & USER_GLOBALUNDO) 
1056                         BKE_write_undo(str);
1057         }
1058 }
1059
1060 void BIF_undo(void)
1061 {       
1062         if(G.obedit) {
1063                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
1064                         undo_editmode_step(1);
1065         }
1066         else {
1067                 if(G.f & G_TEXTUREPAINT)
1068                         undo_imagepaint_step(1);
1069                 else if(curarea->spacetype==SPACE_IMAGE && (G.sima->flag & SI_DRAWTOOL))
1070                         undo_imagepaint_step(1);
1071                 else if(G.f & G_PARTICLEEDIT)
1072                         PE_undo();
1073                 else {
1074                         /* now also in faceselect mode */
1075                         if(U.uiflag & USER_GLOBALUNDO) {
1076                                 BPY_scripts_clear_pyobjects();
1077                                 BKE_undo_step(1);
1078                                 sound_initialize_sounds();
1079                         }
1080                 }
1081         }
1082 }
1083
1084 void BIF_redo(void)
1085 {
1086         if(G.obedit) {
1087                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
1088                         undo_editmode_step(-1);
1089         }
1090         else {
1091                 if(G.f & G_TEXTUREPAINT)
1092                         undo_imagepaint_step(-1);
1093                 else if(curarea->spacetype==SPACE_IMAGE && (G.sima->flag & SI_DRAWTOOL))
1094                         undo_imagepaint_step(-1);
1095                 else if(G.f & G_PARTICLEEDIT)
1096                         PE_redo();
1097                 else {
1098                         /* includes faceselect now */
1099                         if(U.uiflag & USER_GLOBALUNDO) {
1100                                 BKE_undo_step(-1);
1101                                 sound_initialize_sounds();
1102                         }
1103                 }
1104         }
1105 }
1106
1107 void BIF_undo_menu(void)
1108 {
1109         if(G.obedit) {
1110                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
1111                         undo_editmode_menu();
1112                 allqueue(REDRAWALL, 0);
1113         }
1114         else {
1115                 if(G.f & G_PARTICLEEDIT)
1116                         PE_undo_menu();
1117                 else if(U.uiflag & USER_GLOBALUNDO) {
1118                         char *menu= BKE_undo_menu_string();
1119                         if(menu) {
1120                                 short event= pupmenu_col(menu, 20);
1121                                 MEM_freeN(menu);
1122                                 if(event>0) {
1123                                         BKE_undo_number(event);
1124                                         sound_initialize_sounds();
1125                                 }
1126                         }
1127                 }
1128         }
1129 }
1130
1131 /* *************** */
1132
1133 void handle_view_middlemouse() {
1134         /* use '&' here, because of alt+leftmouse which emulates middlemouse */
1135         if(U.flag & USER_VIEWMOVE) {
1136                 if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1137                         viewmove(0);
1138                 else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1139                         viewmove(2);
1140                 else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1141                         viewmove(1);
1142         }
1143         else {
1144                 if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1145                         viewmove(1);
1146                 else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1147                         viewmove(2);
1148                 else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1149                         viewmove(0);
1150         }
1151 }
1152
1153 void handle_view_wheelup()
1154 {
1155         /* Regular:   Zoom in */
1156         /* Shift:     Scroll up */
1157         /* Ctrl:      Scroll right */
1158         /* Alt-Shift: Rotate up */
1159         /* Alt-Ctrl:  Rotate right */
1160
1161         if( G.qual & LR_SHIFTKEY ) {
1162                 if( G.qual & LR_ALTKEY ) { 
1163                         G.qual &= ~LR_SHIFTKEY;
1164                         persptoetsen(PAD2);
1165                         G.qual |= LR_SHIFTKEY;
1166                 } else {
1167                         persptoetsen(PAD2);
1168                 }
1169         } else if( G.qual & LR_CTRLKEY ) {
1170                 if( G.qual & LR_ALTKEY ) { 
1171                         G.qual &= ~LR_CTRLKEY;
1172                         persptoetsen(PAD4);
1173                         G.qual |= LR_CTRLKEY;
1174                 } else {
1175                         persptoetsen(PAD4);
1176                 }
1177         } else if(U.uiflag & USER_WHEELZOOMDIR) 
1178                 persptoetsen(PADMINUS);
1179         else
1180                 persptoetsen(PADPLUSKEY);
1181 }
1182
1183 void handle_view_wheeldown()
1184 {
1185         /* Regular:   Zoom out */
1186         /* Shift:     Scroll down */
1187         /* Ctrl:      Scroll left */
1188         /* Alt-Shift: Rotate down */
1189         /* Alt-Ctrl:  Rotate left */
1190
1191         if( G.qual & LR_SHIFTKEY ) {
1192                 if( G.qual & LR_ALTKEY ) { 
1193                         G.qual &= ~LR_SHIFTKEY;
1194                         persptoetsen(PAD8);
1195                         G.qual |= LR_SHIFTKEY;
1196                 } else {
1197                         persptoetsen(PAD8);
1198                 }
1199         } else if( G.qual & LR_CTRLKEY ) {
1200                 if( G.qual & LR_ALTKEY ) { 
1201                         G.qual &= ~LR_CTRLKEY;
1202                         persptoetsen(PAD6);
1203                         G.qual |= LR_CTRLKEY;
1204                 } else {
1205                         persptoetsen(PAD6);
1206                 }
1207         } else if(U.uiflag & USER_WHEELZOOMDIR) 
1208                 persptoetsen(PADPLUSKEY);
1209         else
1210                 persptoetsen(PADMINUS);
1211 }
1212
1213 int mouse_in_header(ScrArea *sa)
1214 {
1215         short mouse[2];
1216         getmouseco_sc(mouse);
1217         return mouse[0] >= sa->headrct.xmin &&
1218                mouse[0] <= sa->headrct.xmax &&
1219                mouse[1] >= sa->headrct.ymin &&
1220                mouse[1] <= sa->headrct.ymax;
1221 }
1222
1223 static void winqreadview3dspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1224 {
1225         View3D *v3d= sa->spacedata.first;
1226         Object *ob= OBACT;      /* do not change! */
1227         float *curs;
1228         int doredraw= 0, pupval;
1229         unsigned short event= evt->event;
1230         short val= evt->val;
1231         char ascii= evt->ascii;
1232         
1233         if(curarea->win==0) return;     /* when it comes from sa->headqread() */
1234         
1235         if(val) {
1236
1237                 if( uiDoBlocks(&curarea->uiblocks, event, 1)!=UI_NOTHING ) event= 0;
1238                 
1239                 if(event==UI_BUT_EVENT) do_butspace(val); /* temporal, view3d deserves own queue? */
1240                 
1241                 /* - we consider manipulator a button, defaulting to leftmouse 
1242                  * - grease-pencil also defaults to leftmouse
1243                  */
1244                 if(event==LEFTMOUSE) {
1245                         /* run any view3d event handler script links */
1246                         if (sa->scriptlink.totscript) {
1247                                 if (BPY_do_spacehandlers(sa, event, val, SPACEHANDLER_VIEW3D_EVENT))
1248                                         return; /* return if event was processed (swallowed) by handler(s) */
1249                         }
1250                         
1251                         if(gpencil_do_paint(sa, L_MOUSE)) return;
1252                         if(BIF_do_manipulator(sa)) return;
1253                 }
1254                 else if(event==RIGHTMOUSE) {
1255                         if(gpencil_do_paint(sa, R_MOUSE)) return;
1256                 }
1257                 
1258                 /* swap mouse buttons based on user preference */
1259                 if (U.flag & USER_LMOUSESELECT) {
1260                         /* only swap mouse button for selection, in modes where it is relevant.
1261                          * painting/sculpting stays on LEFTMOUSE */
1262                         if (   !((G.f & G_SCULPTMODE) || (G.f & G_WEIGHTPAINT) || (G.f & G_GREASEPENCIL) ||
1263                                 (G.f & G_VERTEXPAINT) || (G.f & G_TEXTUREPAINT) || (G.f & G_PARTICLEEDIT)) ||
1264                                 (G.obedit) )
1265                         {                       
1266                                 if (event==LEFTMOUSE) event = RIGHTMOUSE;
1267                                 else if (event==RIGHTMOUSE) event = LEFTMOUSE;
1268                         }
1269                 }
1270
1271                 if(!mouse_in_header(sa)) {
1272                         if(!G.obedit && (G.f & G_SCULPTMODE)) {
1273                                 SculptSession *ss= sculpt_session();
1274                                 if(ss && ss->radialcontrol) {
1275                                         radialcontrol_do_events(ss->radialcontrol, event);
1276                                         return;
1277                                 }
1278                                 else if(event!=LEFTMOUSE && event!=MIDDLEMOUSE && (event==MOUSEY || event==MOUSEX)) {
1279                                         if(!bwin_qtest(sa->win))
1280                                                 allqueue(REDRAWVIEW3D, 0);
1281                                 }
1282                         }
1283                         else if(!G.obedit && OBACT && G.f&G_PARTICLEEDIT){
1284                                 ParticleSystem *psys=PE_get_current(OBACT);
1285                                 ParticleEditSettings *pset=PE_settings();
1286                                 if(*PE_radialcontrol()) {
1287                                         radialcontrol_do_events(*PE_radialcontrol(), event);
1288                                         return;
1289                                 }
1290                                 if(psys && psys->edit){
1291                                         if(pset->brushtype>=0 &&
1292                                                 event!=LEFTMOUSE && event!=RIGHTMOUSE && event!=MIDDLEMOUSE &&
1293                                                 (event==MOUSEY || event==MOUSEX) && bwin_qtest(sa->win)==0) {
1294                                                 allqueue(REDRAWVIEW3D, 0);
1295                                         }
1296                                 }
1297                         }
1298
1299                         /* Handle retopo painting */
1300                         if(retopo_mesh_paint_check()) {
1301                                 if(!retopo_paint(event))
1302                                         return;
1303                         }
1304                 }
1305
1306                 /* run any view3d event handler script links */
1307                 if (event && sa->scriptlink.totscript)
1308                         if (BPY_do_spacehandlers(sa, event, val, SPACEHANDLER_VIEW3D_EVENT))
1309                                 return; /* return if event was processed (swallowed) by handler(s) */
1310
1311                 /* TEXTEDITING?? */
1312                 if((G.obedit) && G.obedit->type==OB_FONT) {
1313                         switch(event) {
1314                         
1315                         case LEFTMOUSE:
1316                                 mouse_cursor();
1317                                 break;
1318                         case MIDDLEMOUSE:
1319                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
1320                                 if(U.flag & USER_VIEWMOVE) {
1321                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1322                                                 viewmove(0);
1323                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1324                                                 viewmove(2);
1325                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1326                                                 viewmove(1);
1327                                 }
1328                                 else {
1329                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1330                                                 viewmove(1);
1331                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1332                                                 viewmove(2);
1333                                         else
1334                                                 viewmove(0);
1335                                 }
1336                                 break;
1337                                 
1338                         case WHEELUPMOUSE:
1339                                 /* Regular:   Zoom in */
1340                                 /* Shift:     Scroll up */
1341                                 /* Ctrl:      Scroll right */
1342                                 /* Alt-Shift: Rotate up */
1343                                 /* Alt-Ctrl:  Rotate right */
1344
1345                                 if( G.qual & LR_SHIFTKEY ) {
1346                                         if( G.qual & LR_ALTKEY ) { 
1347                                                 G.qual &= ~LR_SHIFTKEY;
1348                                                 persptoetsen(PAD2);
1349                                                 G.qual |= LR_SHIFTKEY;
1350                                         } else {
1351                                                 persptoetsen(PAD2);
1352                                         }
1353                                 } else if( G.qual & LR_CTRLKEY ) {
1354                                         if( G.qual & LR_ALTKEY ) { 
1355                                                 G.qual &= ~LR_CTRLKEY;
1356                                                 persptoetsen(PAD4);
1357                                                 G.qual |= LR_CTRLKEY;
1358                                         } else {
1359                                                 persptoetsen(PAD4);
1360                                         }
1361                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1362                                         persptoetsen(PADMINUS);
1363                                 else
1364                                         persptoetsen(PADPLUSKEY);
1365
1366                                 doredraw= 1;
1367                                 break;
1368
1369                         case WHEELDOWNMOUSE:
1370                                 /* Regular:   Zoom out */
1371                                 /* Shift:     Scroll down */
1372                                 /* Ctrl:      Scroll left */
1373                                 /* Alt-Shift: Rotate down */
1374                                 /* Alt-Ctrl:  Rotate left */
1375
1376                                 if( G.qual & LR_SHIFTKEY ) {
1377                                         if( G.qual & LR_ALTKEY ) { 
1378                                                 G.qual &= ~LR_SHIFTKEY;
1379                                                 persptoetsen(PAD8);
1380                                                 G.qual |= LR_SHIFTKEY;
1381                                         } else {
1382                                                 persptoetsen(PAD8);
1383                                         }
1384                                 } else if( G.qual & LR_CTRLKEY ) {
1385                                         if( G.qual & LR_ALTKEY ) { 
1386                                                 G.qual &= ~LR_CTRLKEY;
1387                                                 persptoetsen(PAD6);
1388                                                 G.qual |= LR_CTRLKEY;
1389                                         } else {
1390                                                 persptoetsen(PAD6);
1391                                         }
1392                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1393                                         persptoetsen(PADPLUSKEY);
1394                                 else
1395                                         persptoetsen(PADMINUS);
1396                                 
1397                                 doredraw= 1;
1398                                 break;
1399
1400                         case UKEY:
1401                                 if(G.qual==LR_ALTKEY) {
1402                                         remake_editText();
1403                                         doredraw= 1;
1404                                 } 
1405                                 else {
1406                                         do_textedit(event, val, ascii);
1407                                 }
1408                                 break;
1409                         case VKEY:
1410                                 if(G.qual==LR_ALTKEY) {
1411                                         paste_editText();
1412                                         doredraw= 1;
1413                                 } 
1414                                 else {
1415                                         do_textedit(event, val, ascii);
1416                                 }
1417                                 break;
1418                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1419                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1420                         case PADENTER:
1421                                 persptoetsen(event);
1422                                 doredraw= 1;
1423                                 break;
1424                                 
1425                         default:
1426                                 do_textedit(event, val, ascii);
1427                                 break;
1428                         }
1429                 }
1430                 else if(!G.obedit && (G.f & G_SCULPTMODE)) {
1431                         SculptData *sd= sculpt_data();
1432                         SculptSession *ss= sculpt_session();
1433                         BrushData *br= sculptmode_brush();
1434                         Mesh *me= get_mesh(OBACT);
1435                         char update_prop= 0;
1436                         
1437                         if(U.flag & USER_NONUMPAD) {
1438                                 event= convert_for_nonumpad(event);
1439                         }
1440                         
1441                         switch(event) {
1442                         case LEFTMOUSE:
1443                                 if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1444                                         sculptmode_pmv(0);
1445                                 else if(!(ss && ss->radialcontrol))
1446                                         sculpt();
1447                                 break;
1448                         /* View */
1449                         case MIDDLEMOUSE:
1450                                 handle_view_middlemouse();
1451                                 break;
1452                         case WHEELUPMOUSE:
1453                                 handle_view_wheelup();
1454                                 doredraw= 1;
1455                                 break;
1456                         case WHEELDOWNMOUSE:
1457                                 handle_view_wheeldown();
1458                                 doredraw= 1;
1459                                 break;
1460                         case RIGHTMOUSE:
1461                                 if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1462                                         sculptmode_pmv(1);
1463                                 break;
1464                         case CKEY:
1465                                 if(G.qual==LR_SHIFTKEY) {
1466                                         view3d_home(1);
1467                                         curs= give_cursor();
1468                                         curs[0]=curs[1]=curs[2]= 0.0;
1469                                         allqueue(REDRAWVIEW3D, 0);
1470                                 }
1471                                 else if(G.qual==0) {
1472                                         if (G.vd->persp==V3D_CAMOB)
1473                                                 /* center the camera offset */
1474                                                 G.vd->camdx= G.vd->camdy= 0.0;
1475                                         else {
1476                                                 /*non camera center*/
1477                                                 float new_ofs[3];
1478                                                 curs= give_cursor();
1479                                                 new_ofs[0]= -curs[0];
1480                                                 new_ofs[1]= -curs[1];
1481                                                 new_ofs[2]= -curs[2];
1482                                                 smooth_view(G.vd, new_ofs, NULL, NULL, NULL);
1483                                         }
1484                                 }
1485                                 doredraw= 1;
1486                                 break;
1487                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1488                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1489                         case PADPLUSKEY:
1490                         case PADMINUS:
1491                         case PADENTER:
1492                                 persptoetsen(event);
1493                                 doredraw= 1;
1494                                 break;
1495                         case BKEY:
1496                                 if(G.qual==LR_ALTKEY)
1497                                         view3d_edit_clipping(v3d);
1498                                 else if(G.qual==LR_SHIFTKEY) {
1499                                         if(G.vd->persp==V3D_CAMOB)
1500                                                 set_render_border();
1501                                         else
1502                                                 view3d_border_zoom();
1503                                 }
1504                                 break;
1505                         case PADSLASHKEY:
1506                                 if(!G.qual) {
1507                                         if(G.vd->localview) {
1508                                                 G.vd->localview= 0;
1509                                                 endlocalview(curarea);
1510                                         }
1511                                         else {
1512                                                 G.vd->localview= 1;
1513                                                 initlocalview();
1514                                         }
1515                                         allqueue(REDRAWVIEW3D, 1);
1516                                 }
1517                                 break;
1518                         /* Brush properties */
1519                         case AKEY:
1520                                 br->flag ^= SCULPT_BRUSH_AIRBRUSH;
1521                                 update_prop= 1; break;
1522                         case FKEY:
1523                                 if(ss) {
1524                                         sculpt_radialcontrol_start(G.qual == 0 ? RADIALCONTROL_SIZE :
1525                                                                    G.qual == LR_SHIFTKEY ? RADIALCONTROL_STRENGTH :
1526                                                                    G.qual == LR_CTRLKEY ? RADIALCONTROL_ROTATION :
1527                                                                    RADIALCONTROL_NONE);
1528                                 }
1529                                 break;
1530                         case VKEY:
1531                                 br->dir= br->dir==1 ? 2 : 1;
1532                                 update_prop= 1; break;
1533                         /* Set brush */
1534                         case DKEY:
1535                                 sd->brush_type= DRAW_BRUSH;
1536                                 update_prop= 1; break;
1537                         case SKEY:
1538                                 sd->brush_type= SMOOTH_BRUSH;
1539                                 update_prop= 1; break;
1540                         case PKEY:
1541                                 sd->brush_type= PINCH_BRUSH;
1542                                 update_prop= 1; break;
1543                         case IKEY:
1544                                 sd->brush_type= INFLATE_BRUSH;
1545                                 update_prop= 1; break;
1546                         case GKEY:
1547                                 sd->brush_type= GRAB_BRUSH;
1548                                 update_prop= 1; break;
1549                         case LKEY:
1550                                 sd->brush_type= LAYER_BRUSH;
1551                                 update_prop= 1; break;
1552                         case TKEY:
1553                                 sd->brush_type= FLATTEN_BRUSH;
1554                                 update_prop= 1; break;
1555                         /* Symmetry */
1556                         case XKEY:
1557                                 sd->symm^= SYMM_X;
1558                                 update_prop= 1; break;
1559                         case YKEY:
1560                                 sd->symm^= SYMM_Y;
1561                                 update_prop= 1; break;
1562                         case ZKEY:
1563                                 sd->symm^= SYMM_Z;
1564                                 update_prop= 1; break;
1565                         /* Interface */
1566                         case NKEY:
1567                                 if(G.qual==0) {
1568                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1569                                         allqueue(REDRAWVIEW3D, 0);
1570                                 }
1571                                 break;
1572                         /* Multires */
1573                         case PAGEUPKEY:
1574                                 if(me && me->mr) {
1575                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current+1;
1576                                         multires_set_level_cb(ob, ob->data);
1577                                 }
1578                                 break;
1579                         case PAGEDOWNKEY:
1580                                 if(me && me->mr) {
1581                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current-1;
1582                                         multires_set_level_cb(ob, ob->data);
1583                                 }
1584                                 break;
1585                         /* Partial Visibility */
1586                         case HKEY:
1587                                 if(G.qual==LR_ALTKEY) {
1588                                         waitcursor(1);
1589                                         mesh_pmv_off(ob, get_mesh(ob));
1590                                         BIF_undo_push("Partial mesh hide");
1591                                         allqueue(REDRAWVIEW3D,0);
1592                                         waitcursor(0);
1593                                 }
1594                                 break;
1595                         /* Layers */
1596                         case ONEKEY:
1597                                 do_layer_buttons(0); break;
1598                         case TWOKEY:
1599                                 do_layer_buttons(1); break;
1600                         case THREEKEY:
1601                                 do_layer_buttons(2); break;
1602                         case FOURKEY:
1603                                 do_layer_buttons(3); break;
1604                         case FIVEKEY:
1605                                 do_layer_buttons(4); break;
1606                         case SIXKEY:
1607                                 do_layer_buttons(5); break;
1608                         case SEVENKEY:
1609                                 do_layer_buttons(6); break;
1610                         case EIGHTKEY:
1611                                 do_layer_buttons(7); break;
1612                         case NINEKEY:
1613                                 do_layer_buttons(8); break;
1614                         case ZEROKEY:
1615                                 do_layer_buttons(9); break;
1616                         case MINUSKEY:
1617                                 do_layer_buttons(10); break;
1618                         case EQUALKEY:
1619                                 do_layer_buttons(11); break;
1620                         case ACCENTGRAVEKEY:
1621                                 do_layer_buttons(-1); break;
1622                         
1623                         case NDOFMOTION:                
1624                                 if (G.vd->ndofmode == 0) {
1625                                         viewmoveNDOF(1);
1626                                 } else if (G.vd->ndofmode == 1) {
1627                                         viewmoveNDOFfly(1);
1628                                 } else {
1629                                         NDofTransform();
1630                                 }
1631                 break;
1632                                 
1633             case NDOFBUTTON:
1634                                 if (val == 1) {
1635                                         G.vd->ndofmode +=1;
1636                                         if (G.vd->ndofmode > 2)         /* we have currently 3 modes : 0 original, 1 fly, 2 transform */
1637                                                 G.vd->ndofmode = 0;
1638                                 }
1639                                 if (val == 2) {
1640                                         G.vd->ndoffilter =(G.vd->ndoffilter == 1 ? 0 : 1);
1641                                 }
1642                                 allqueue(REDRAWHEADERS, 0);
1643                 break;
1644                                 
1645                         }
1646                         
1647                         /* Redraw buttons window as well as view 3d (for floating panel) */
1648                         if(update_prop) {
1649                                 allqueue(REDRAWVIEW3D, 0);
1650                                 allqueue(REDRAWBUTSEDIT, 0);
1651                         }
1652                 } else {
1653
1654                         if (U.flag & USER_NONUMPAD) {
1655                                 event= convert_for_nonumpad(event);
1656                         }
1657
1658                         switch(event) {
1659                         
1660                         /* Afterqueue events */
1661                         case BACKBUFDRAW:
1662                                 backdrawview3d(1);
1663                                 break;
1664                         case RENDERPREVIEW:
1665                                 BIF_view3d_previewrender(sa);
1666                                 break;
1667                                 
1668                         case TABKEY:
1669                                 /* Shift-Tabe handling (other cases are in toets) */
1670                                 if (G.qual == LR_SHIFTKEY)
1671                                 {
1672                                         /* Snap toggle only when supported */
1673                                         if (BIF_snappingSupported())
1674                                         {
1675                                                 G.scene->snap_flag ^= SCE_SNAP;
1676                                                 allqueue(REDRAWHEADERS, 0);
1677                                         }                                       
1678                                 }
1679                                 break;
1680                                 
1681                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1682                          * based on user preference USER_LMOUSESELECT
1683                          */
1684                         case LEFTMOUSE: 
1685                                 if ((G.obedit) || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT|G_PARTICLEEDIT))) {
1686                                         mouse_cursor();
1687                                 }
1688                                 else if (G.f & G_WEIGHTPAINT) {
1689                                         weight_paint();
1690                                 }
1691                                 else if (G.f & G_VERTEXPAINT) {
1692                                         vertex_paint();
1693                                 }
1694                                 else if (G.f & G_TEXTUREPAINT) {
1695                                         imagepaint_paint(L_MOUSE, 1);
1696                                 }
1697                                 else if (G.f & G_PARTICLEEDIT) {
1698                                         if(G.qual & LR_CTRLKEY)
1699                                                 mouse_cursor();
1700                                         else if(!PE_brush_particles())
1701                                                 mouse_cursor();
1702                                 }
1703                                 break;
1704                         case MIDDLEMOUSE:
1705                                 handle_view_middlemouse();
1706                                 break;
1707                         case RIGHTMOUSE:
1708                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
1709                                         if(G.obedit->type==OB_MESH)
1710                                                 mouse_mesh();
1711                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1712                                                 mouse_nurb();
1713                                         else if(G.obedit->type==OB_MBALL)
1714                                                 mouse_mball();
1715                                         else if(G.obedit->type==OB_LATTICE)
1716                                                 mouse_lattice();
1717                                         else if(G.obedit->type==OB_ARMATURE)
1718                                                 mouse_armature();
1719                                 }
1720                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
1721                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1722                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
1723                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1724                                 else if(G.qual==LR_CTRLKEY)
1725                                         mouse_select(); /* also allow in editmode, for vertex parenting */
1726                                 else if(FACESEL_PAINT_TEST)
1727                                         face_select();
1728                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
1729                                         sample_vpaint();
1730                                 else if( G.f & G_PARTICLEEDIT)
1731                                         PE_mouse_particles();
1732                                 else
1733                                         mouse_select(); /* does poses too */
1734                                 break;
1735                         case WHEELUPMOUSE:
1736                                 handle_view_wheelup();
1737                                 doredraw= 1;
1738                                 break;
1739                         case WHEELDOWNMOUSE:
1740                                 handle_view_wheeldown();
1741                                 doredraw= 1;
1742                                 break;
1743
1744             case NDOFMOTION:            
1745                                 if (G.vd->ndofmode == 0) {
1746                                         viewmoveNDOF(1);
1747                                 } else if (G.vd->ndofmode == 1) {
1748                                         viewmoveNDOFfly(1);
1749                                 } else {
1750                                         NDofTransform();
1751                                 }
1752                 break;
1753
1754             case NDOFBUTTON:
1755                                 if (val == 1) {
1756                                         G.vd->ndofmode +=1;
1757                                         if (G.vd->ndofmode > 2)         /* we have currently 3 modes : 0 original, 1 fly, 2 transform */
1758                                                 G.vd->ndofmode = 0;
1759                                 }
1760                                 if (val == 2) {
1761                                         G.vd->ndoffilter =(G.vd->ndoffilter == 1 ? 0 : 1);
1762                                 }
1763                                 allqueue(REDRAWHEADERS, 0);
1764                 break;
1765                                 
1766                         case ONEKEY:
1767                                 if(G.qual==LR_CTRLKEY) {
1768                                         flip_subdivison(1);
1769                                 }
1770                                 else do_layer_buttons(0); 
1771                                 break;
1772                                 
1773                         case TWOKEY:
1774                                 if(G.qual==LR_CTRLKEY) {
1775                                         flip_subdivison(2);
1776                                 }
1777                                 else do_layer_buttons(1); 
1778                                 break;
1779                                 
1780                         case THREEKEY:
1781                                 if(G.qual==LR_CTRLKEY) {
1782                                         flip_subdivison(3);
1783                                 }
1784                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1785                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1786                                                 select_faces_by_numverts(3);
1787                                 }
1788                                 else do_layer_buttons(2); 
1789                                 break;
1790                                 
1791                         case FOURKEY:
1792                                 if(G.qual==LR_CTRLKEY) {
1793                                         flip_subdivison(4);
1794                                 }
1795                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1796                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1797                                                 select_faces_by_numverts(4);
1798                                 }
1799                                 else do_layer_buttons(3); 
1800                                 break;
1801                                 
1802                         case FIVEKEY:
1803                                 if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1804                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1805                                                 select_faces_by_numverts(5);
1806                                 }
1807                                 
1808                                 else if(G.qual==LR_CTRLKEY) {}
1809                                 else do_layer_buttons(4);
1810                                 break;
1811
1812                         case SIXKEY:
1813                                 if(G.qual==LR_CTRLKEY) {}
1814                                 else do_layer_buttons(5);
1815                                 break;
1816                         case SEVENKEY:
1817                                 if(G.qual==LR_CTRLKEY) {}
1818                                 else do_layer_buttons(6);
1819                                 break;
1820                         case EIGHTKEY:
1821                                 if(G.qual==LR_CTRLKEY) {}
1822                                 else do_layer_buttons(7);
1823                                 break;
1824                         case NINEKEY:
1825                                 if(G.qual==LR_CTRLKEY) {}
1826                                 else do_layer_buttons(8);
1827                                 break;
1828                         case ZEROKEY:
1829                                 if(G.qual==LR_CTRLKEY) {}
1830                                 else do_layer_buttons(9);
1831                                 break;
1832                         case MINUSKEY:
1833                                 if(G.qual==LR_CTRLKEY) {}
1834                                 else do_layer_buttons(10);
1835                                 break;
1836                         case EQUALKEY:
1837                                 if(G.qual==LR_CTRLKEY) {}
1838                                 else do_layer_buttons(11);
1839                                 break;
1840                         case ACCENTGRAVEKEY:
1841                                 do_layer_buttons(-1);
1842                                 break;
1843                         
1844                         case SPACEKEY:
1845                                 if(G.qual == LR_CTRLKEY) {
1846                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1847                                         if(val>0) {
1848                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1849                                                 else {
1850                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1851                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1852                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1853                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1854                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1855                                                 }
1856                                                 doredraw= 1;
1857                                         }
1858                                 }
1859                                 else if(G.qual == LR_ALTKEY) {
1860                                         BIF_selectOrientation();
1861                                         doredraw= 1;
1862                                 }
1863
1864                                 break;
1865                                 
1866                         case AKEY:
1867                                 if (G.obedit == 0 && G.qual == (LR_CTRLKEY|LR_ALTKEY)) {
1868                                         alignmenu();
1869                                 }
1870                                 else if(G.qual & LR_CTRLKEY) { /* also with shift! */
1871                                         apply_object(); 
1872                                 }
1873                                 else if((G.qual==LR_SHIFTKEY)) {
1874                                         toolbox_n_add();
1875                                 }
1876                                 else {
1877                                         if(G.obedit) {
1878                                                 if(G.obedit->type==OB_MESH)
1879                                                         deselectall_mesh();
1880                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1881                                                         deselectall_nurb();
1882                                                 else if(G.obedit->type==OB_MBALL)
1883                                                         deselectall_mball();
1884                                                 else if(G.obedit->type==OB_LATTICE)
1885                                                         deselectall_Latt();
1886                                                 else if(G.obedit->type==OB_ARMATURE)
1887                                                         deselectall_armature(1, 1);     /* 1 == toggle */
1888                                         }
1889                                         else if (ob && (ob->flag & OB_POSEMODE)){
1890                                                 deselectall_posearmature(ob, 1, 1);
1891                                         }
1892                                         else {
1893                                                 if(FACESEL_PAINT_TEST) deselectall_tface();
1894                                                 else if(G.f & G_PARTICLEEDIT) PE_deselectall();
1895                                                 else {
1896                                                         /* by design, the center of the active object 
1897                                                          * (which need not necessarily by selected) will
1898                                                          * still be drawn as if it were selected.
1899                                                          */
1900                                                         deselectall();
1901                                                 }
1902                                         }
1903                                 }
1904                                 break;
1905                         case BKEY:
1906                                 if(G.qual==LR_ALTKEY)
1907                                         view3d_edit_clipping(v3d);
1908                                 else if(G.qual==LR_SHIFTKEY)
1909                                 {
1910                                         if(G.vd->persp==V3D_CAMOB)
1911                                                 set_render_border();
1912                                         else
1913                                                 view3d_border_zoom();
1914                                 }
1915                                 else if(G.qual==LR_CTRLKEY) {
1916                                         extern void pointcache_bake(PTCacheID *pid, int startframe);
1917                                         extern void pointcache_free(PTCacheID *pid, int cacheonly);
1918                                         extern void fluidsimBake(Object *ob);
1919                                         extern void fluidsimFreeBake(Object *ob);
1920                                         int pupval;
1921
1922                                         pupval= pupmenu("Physics Baking%t|Bake selected %x1|Free bake selected %x2|Free cache selected %x3");
1923
1924                                         if(pupval > 0) {
1925                                                 if(pupval == 1) {
1926                                                         pointcache_bake(NULL, 0);
1927                                                         /* also bake first domain of selected objects... */
1928                                                         fluidsimBake(NULL);
1929                                                 }
1930                                                 else if(pupval == 2) {
1931                                                         pointcache_free(NULL, 0);
1932                                                         fluidsimFreeBake(NULL);
1933                                                 }
1934                                                 else if(pupval == 3)
1935                                                         pointcache_free(NULL, 1);
1936                                         }
1937                                 }
1938                                 else if(G.qual== (LR_ALTKEY|LR_CTRLKEY))
1939                                         objects_bake_render_menu();
1940                                 else if(G.qual==0)
1941                                         borderselect();
1942                                 break;
1943                         case CKEY:
1944                                 if(G.qual==LR_CTRLKEY) {
1945                                         if(ob && (ob->flag & OB_POSEMODE))
1946                                                 pose_copy_menu();       /* poseobject.c */
1947                                         else
1948                                                 copy_attr_menu();
1949                                 }
1950                                 else if(G.qual==(LR_ALTKEY|LR_SHIFTKEY)) 
1951                                         gpencil_convert_menu(); /* gpencil.c */
1952                                 else if(G.qual==LR_ALTKEY) {
1953                                         if(ob && (ob->flag & OB_POSEMODE))
1954                                                 pose_clear_constraints();       /* poseobject.c */
1955                                         else
1956                                                 convertmenu();  /* editobject.c */
1957                                 }
1958                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY)) 
1959                                         add_constraint(0);      /* editconstraint.c, generic for objects and posemode */
1960                                 else if(G.qual==(LR_CTRLKEY|LR_SHIFTKEY)) {
1961                                         BIF_manageTransformOrientation(0, 1);
1962                                         allqueue(REDRAWVIEW3D, 0);
1963                                 }
1964                                 else if((G.qual==LR_SHIFTKEY)) {
1965                                         view3d_home(1);
1966                                         curs= give_cursor();
1967                                         curs[0]=curs[1]=curs[2]= 0.0;
1968                                         allqueue(REDRAWVIEW3D, 0);
1969                                 }
1970                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1971                                         makecyclicNurb();
1972                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1973                                         allqueue(REDRAWVIEW3D, 0);
1974                                 }
1975                                 else if((G.qual==0)){
1976                                         if (G.vd->persp==V3D_CAMOB)
1977                                                 /* center the camera offset */
1978                                                 G.vd->camdx= G.vd->camdy= 0.0;
1979                                         else {
1980                                                 /*non camera center*/
1981                                                 float new_ofs[3];
1982                                                 curs= give_cursor();
1983                                                 new_ofs[0]= -curs[0];
1984                                                 new_ofs[1]= -curs[1];
1985                                                 new_ofs[2]= -curs[2];
1986                                                 smooth_view(G.vd, new_ofs, NULL, NULL, NULL);
1987                                         }
1988                                         scrarea_queue_winredraw(curarea);
1989                                 }
1990                                 break;
1991                         case DKEY:
1992                                 if((G.qual==LR_SHIFTKEY)) {
1993                                         duplicate_context_selected();
1994                                 }
1995                                 else if(G.qual==LR_ALTKEY) {
1996                                         if(ob && (ob->flag & OB_POSEMODE))
1997                                                 error ("Duplicate not possible in posemode.");
1998                                         else if((G.obedit==NULL))
1999                                                 adduplicate(0, 0);
2000                                 }
2001                                 else if(G.qual==LR_CTRLKEY) {
2002                                         imagestodisplist(); // removed
2003                                 }
2004                                 else if((G.qual==0)){
2005                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
2006                                         if(pupval>0) {
2007                                                 G.vd->drawtype= pupval;
2008                                                 doredraw= 1;
2009                                         }
2010                 }
2011                                 
2012                                 break;
2013                         case EKEY:
2014                                 if (G.qual==0){
2015                                         if(G.obedit) {
2016                                                 if(G.obedit->type==OB_MESH)
2017                                                         extrude_mesh();
2018                                                 else if(G.obedit->type==OB_CURVE)
2019                                                         addvert_Nurb('e');
2020                                                 else if(G.obedit->type==OB_SURF)
2021                                                         extrude_nurb();
2022                                                 else if(G.obedit->type==OB_ARMATURE)
2023                                                         extrude_armature(0);
2024                                         }
2025                                 }
2026                                 else if (G.qual==LR_CTRLKEY) {
2027                                         if(G.obedit && G.obedit->type==OB_MESH)
2028                                                 Edge_Menu();
2029                                         else if (FACESEL_PAINT_TEST)
2030                                                 seam_mark_clear_tface(0);
2031                                 }
2032                                 else if (G.qual==LR_SHIFTKEY) {
2033                                         if (G.obedit && G.obedit->type==OB_MESH &&
2034                                             !multires_level1_test()) {
2035                                                 initTransform(TFM_CREASE, CTX_EDGE);
2036                                                 Transform();
2037                                         }
2038                                         else if (G.obedit && G.obedit->type==OB_ARMATURE) {
2039                                                 extrude_armature(1);
2040                                         }
2041                                 }
2042                                 else if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY)) {
2043                                         if (G.obedit && G.obedit->type==OB_MESH &&
2044                                             !multires_level1_test()) {
2045                                                 if (G.scene->selectmode & SCE_SELECT_VERTEX) {
2046                                                         initTransform(TFM_BWEIGHT, CTX_NONE);
2047                                                 }
2048                                                 else {
2049                                                         initTransform(TFM_BWEIGHT, CTX_EDGE);
2050                                                 }
2051                                                 Transform();
2052                                         }
2053                                 }
2054                                 break;
2055                         case FKEY:
2056                                 if(G.obedit) {
2057                                         if(G.obedit->type==OB_MESH) {
2058                                                 if(G.qual == LR_CTRLKEY)
2059                                                         Face_Menu();
2060                                                 else if((G.qual==LR_SHIFTKEY))
2061                                                         fill_mesh();
2062                                                 else if(G.qual==LR_ALTKEY)
2063                                                         beauty_fill();
2064                                                 else if(G.qual==(LR_CTRLKEY|LR_SHIFTKEY))
2065                                                         edge_flip();
2066                                                 else if (G.qual==0)
2067                                                         addedgeface_mesh();
2068                                                 else if ( G.qual == 
2069                                                          (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2070                                                         select_linked_flat_faces();
2071                                                 }
2072
2073                                         }
2074                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) {
2075                                                 addsegment_nurb();
2076                                         }
2077                                         else if(G.obedit->type == OB_ARMATURE) {
2078                                                 fill_bones_armature();
2079                                         }
2080                                 }
2081                                 else if(G.qual==LR_CTRLKEY)
2082                                         sort_faces();
2083                                 else if((G.qual==LR_SHIFTKEY)) {
2084                                         if(ob && (ob->flag & OB_POSEMODE))
2085                                            pose_activate_flipped_bone();
2086                                         else if(G.f & G_WEIGHTPAINT)
2087                                                 pose_activate_flipped_bone();
2088                                         else if(G.f & G_PARTICLEEDIT)
2089                                                 PE_radialcontrol_start(RADIALCONTROL_STRENGTH);
2090                                         else
2091                                                 fly();
2092                                 }
2093                                 else if((G.qual==LR_ALTKEY)) {
2094                                         if(ob && (ob->flag & OB_POSEMODE))
2095                                                 if(okee("Flip quaternion rotations"))
2096                                                         pose_flipquats();
2097                                 }
2098                                 else {
2099                                         if (G.f & (G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT)){
2100                                                 G.f ^= G_FACESELECT;
2101                                                 allqueue(REDRAWVIEW3D, 1);
2102                                                 allqueue(REDRAWBUTSEDIT, 1);
2103                                         }
2104                                         else if(G.f & G_PARTICLEEDIT) {
2105                                                 PE_radialcontrol_start(RADIALCONTROL_SIZE);
2106                                         } else {
2107                                                 pupmenu("Note%t|UV/Face Select was removed. Editmode now allows UV editing, Add a UV layer or Unwrap (UKey)");
2108                                         }
2109                                 }
2110                                 
2111                                 break;
2112                         case GKEY:
2113                                 if((G.qual == LR_CTRLKEY)) {
2114                                         if(G.obedit) {
2115                                                 if(ELEM(G.obedit->type, OB_MESH, OB_LATTICE))
2116                                                         vgroup_assign_with_menu();
2117                                         }
2118                                         else if(ob && (ob->flag & OB_POSEMODE))
2119                                                 pgroup_operation_with_menu();
2120                                         else
2121                                                 group_operation_with_menu();
2122                                 }
2123                                 else if((G.qual == (LR_CTRLKEY|LR_SHIFTKEY))) {
2124                                         if(G.obedit) {
2125                                                 if(ELEM(G.obedit->type, OB_MESH, OB_LATTICE))
2126                                                         vgroup_operation_with_menu();
2127                                         }
2128                                 }
2129                                 else if((G.qual==LR_SHIFTKEY)) {
2130                                         if(G.obedit) {
2131                                                 if(G.obedit->type==OB_MESH)
2132                                                         select_mesh_group_menu();
2133                                         } 
2134                                         else if(ob && (ob->flag & OB_POSEMODE))
2135                                                 pose_select_grouped_menu();
2136                                         else if (ob)
2137                                                 select_object_grouped_menu();
2138                                 }
2139                                 else if((G.obedit==0) && G.qual==LR_ALTKEY) {
2140                                         if(okee("Clear location")) {
2141                                                 clear_object('g');
2142                                         }
2143                                 }
2144                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2145                                         v3d->twtype= V3D_MANIP_TRANSLATE;
2146                                         doredraw= 1;
2147                                 }
2148                                 else if((G.qual==0)) {
2149                                         initTransform(TFM_TRANSLATION, CTX_NONE);
2150                                         Transform();
2151                                 }
2152                                 break;
2153                         case HKEY:
2154                                 if(G.obedit) {
2155                                         if(G.obedit->type==OB_MESH) {
2156                                                 if(G.qual==LR_CTRLKEY)
2157                                                         add_hook_menu();
2158                                                 else if(G.qual==LR_ALTKEY)
2159                                                         reveal_mesh();
2160                                                 else if((G.qual==LR_SHIFTKEY))
2161                                                         hide_mesh(1);
2162                                                 else if((G.qual==0)) 
2163                                                         hide_mesh(0);
2164                                         }
2165                                         else if(G.obedit->type== OB_SURF) {
2166                                                 if(G.qual==LR_CTRLKEY)
2167                                                         add_hook_menu();
2168                                                 else if(G.qual==LR_ALTKEY)
2169                                                         revealNurb();
2170                                                 else if((G.qual==LR_SHIFTKEY))
2171                                                         hideNurb(1);
2172                                                 else if((G.qual==0))
2173                                                         hideNurb(0);
2174                                         }
2175                                         else if(G.obedit->type==OB_CURVE) {
2176                                                 if(G.qual==LR_CTRLKEY)
2177                                                         add_hook_menu();
2178                                                 else if(G.qual==LR_ALTKEY)
2179                                                         revealNurb();
2180                                                 /* should be G.qual==LR_SHIFTKEY, but that is taken fro handles already */
2181                                                 else if((G.qual==(LR_ALTKEY|LR_SHIFTKEY))) 
2182                                                         hideNurb(1);
2183                                                 /* should be G.qual==0, but that is taken for handles already */
2184                                                 else if((G.qual==(LR_ALTKEY|LR_CTRLKEY))) 
2185                                                         hideNurb(0);
2186                                                 else {
2187                                                         if(G.qual==LR_CTRLKEY) /* conflict */
2188                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
2189                                                         else if((G.qual==LR_SHIFTKEY))
2190                                                                 sethandlesNurb(1);
2191                                                         else if((G.qual==0))
2192                                                                 sethandlesNurb(3);
2193                                                         
2194                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2195                                                         BIF_undo_push("Handle change");
2196                                                         allqueue(REDRAWVIEW3D, 0);
2197                                                 }
2198                                         }
2199                                         else if(G.obedit->type==OB_LATTICE) {
2200                                                 if(G.qual==LR_CTRLKEY) add_hook_menu();
2201                                         }
2202                                         else if(G.obedit->type==OB_MBALL) {
2203                                                 if(G.qual==LR_ALTKEY)
2204                                                         reveal_mball();
2205                                                 else if((G.qual==LR_SHIFTKEY))
2206                                                         hide_mball(1);
2207                                                 else if((G.qual==0)) 
2208                                                         hide_mball(0);
2209                                         }
2210                                         else if(G.obedit->type==OB_ARMATURE) {
2211                                                 if (G.qual==0)
2212                                                         hide_selected_armature_bones();
2213                                                 else if (G.qual==LR_SHIFTKEY)
2214                                                         hide_unselected_armature_bones();
2215                                                 else if (G.qual==LR_ALTKEY)
2216                                                         show_all_armature_bones();
2217                                         }
2218                                 }
2219                                 else if(FACESEL_PAINT_TEST)
2220                                         hide_tface();
2221                                 else if(G.f & G_PARTICLEEDIT) {
2222                                         if(G.qual == LR_ALTKEY)
2223                                                 PE_hide(0);
2224                                         else if(G.qual == LR_SHIFTKEY)
2225                                                 PE_hide(1);
2226                                         else if(G.qual == 0)
2227                                                 PE_hide(2);
2228                                 }
2229                                 else if(ob && (ob->flag & OB_POSEMODE)) {
2230                                         if (G.qual==0)
2231                                                 hide_selected_pose_bones();
2232                                         else if (G.qual==LR_SHIFTKEY)
2233                                                 hide_unselected_pose_bones();
2234                                         else if (G.qual==LR_ALTKEY)
2235                                                 show_all_pose_bones();
2236                                 }
2237                                 /* Object mode only, other modes are accounted for above */
2238                                 else if ((G.f & (G_VERTEXPAINT|G_TEXTUREPAINT|G_WEIGHTPAINT))==0) {
2239                                         if(G.qual==LR_CTRLKEY)hookmenu();
2240                                         else if(G.qual==LR_ALTKEY) show_objects();
2241                                         else if(G.qual==LR_SHIFTKEY) hide_objects(0);
2242                                         else hide_objects(1);
2243                                 }
2244                                 break;
2245                         case IKEY:
2246                                 if(G.qual==LR_CTRLKEY) {
2247                                         if((ob) && (ob->flag & OB_POSEMODE) && (ob->type==OB_ARMATURE))
2248                                                 deselectall_posearmature(ob, 3, 1);
2249                                         else if(ob && G.obedit) {
2250                                                 if(G.obedit->type == OB_ARMATURE)
2251                                                         deselectall_armature(3, 1);
2252                                                 else
2253                                                         selectswap_mesh();
2254                                         }
2255                                         else
2256                                                 selectswap();
2257                                 }
2258                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY)) {
2259                                         if(ob && (ob->flag & OB_POSEMODE) && ob->type==OB_ARMATURE)
2260                                                 pose_clear_IK();
2261                                 }
2262                                 else if(G.qual==LR_SHIFTKEY) {
2263                                         if(ob && (ob->flag & OB_POSEMODE) && ob->type==OB_ARMATURE)
2264                                                 pose_add_IK();
2265                                 }
2266                                 break;
2267                                 
2268                         case JKEY:
2269                                 if(G.qual==LR_CTRLKEY) {
2270                                         if( ob ) {
2271                                                 join_menu();
2272                                         }
2273                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF)) {
2274                                                 addsegment_nurb();
2275                                         } else {
2276                                                 error("Can't join unless there is an active object");
2277                                         }
2278                                         
2279                                 }
2280                                 else if(G.obedit) {
2281                                         if(G.obedit->type==OB_MESH) {
2282                                                 join_triangles();
2283                                         }
2284                                 }
2285
2286                                 break;
2287                         case KKEY:
2288                                 if(G.obedit) {
2289                                         if (G.obedit->type==OB_MESH) {
2290                                                 if (G.qual==LR_SHIFTKEY)
2291                                                         KnifeSubdivide(KNIFE_PROMPT);
2292                                                 else if (G.qual==0)
2293                                                         LoopMenu();
2294                                         }
2295                                         else if(G.obedit->type==OB_SURF)
2296                                                 printknots();
2297                                 }
2298                                 else {
2299                                         if((G.qual==LR_SHIFTKEY)) {
2300                                                 if(FACESEL_PAINT_TEST)
2301                                                         if (G.f & G_WEIGHTPAINT)
2302                                                                 clear_wpaint_selectedfaces();
2303                                                         else
2304                                                                 clear_vpaint_selectedfaces();
2305                                                 else if(G.f & G_VERTEXPAINT)
2306                                                         clear_vpaint();
2307                                                 else
2308                                                         select_select_keys();
2309                                         }
2310                                         else if (G.qual==0)
2311                                                 set_ob_ipoflags();
2312                                 }
2313                                 
2314                                 break;
2315                         case LKEY:
2316                                 if(G.obedit) {
2317                                         if(G.obedit->type==OB_MESH) {
2318                                                 if (G.qual & LR_CTRLKEY) {
2319                                                         if ((G.scene->selectmode & SCE_SELECT_FACE) == 0) {
2320                                                                 selectconnected_mesh_all(); /* normal select linked */
2321                                                         } else {
2322                                                                 selectconnected_delimit_mesh_all(); /* select linked with edge crease delimiting */
2323                                                         }
2324                                                 } else {
2325                                                         if ((G.scene->selectmode & SCE_SELECT_FACE) == 0) {
2326                                                                 selectconnected_mesh();
2327                                                         } else {
2328                                                                 selectconnected_delimit_mesh();
2329                                                         }
2330                                                 }
2331                                         }
2332                                         if(G.obedit->type==OB_ARMATURE)
2333                                                 selectconnected_armature();
2334                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
2335                                                 selectconnected_nurb();
2336                                 }
2337                                 else if(ob && (ob->flag & OB_POSEMODE)) {
2338                                         if (G.qual == LR_CTRLKEY) 
2339                                                 poselib_preview_poses(ob, 0);
2340                                         else if (G.qual == LR_SHIFTKEY) 
2341                                                 poselib_add_current_pose(ob, 0);
2342                                         else if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY))
2343                                                 poselib_rename_pose(ob);
2344                                         else if (G.qual == LR_ALTKEY)
2345                                                 poselib_remove_pose(ob, NULL);
2346                                         else
2347                                                 selectconnected_posearmature();
2348                                 }
2349                                 else {
2350                                         if(FACESEL_PAINT_TEST) {
2351                                                 if((G.qual==0))
2352                                                         select_linked_tfaces(0);
2353                                                 else if((G.qual==LR_SHIFTKEY))
2354                                                         select_linked_tfaces(1);
2355                                                 else if(G.qual==LR_CTRLKEY)
2356                                                         select_linked_tfaces(2);
2357                                         }
2358                                         else if(G.f & G_PARTICLEEDIT) {
2359                                                 if(G.qual==0)
2360                                                         PE_select_linked();
2361                                         }
2362                                         else {
2363                                                 if((G.qual==0))
2364                                                         make_local_menu();
2365                                                 else if((G.qual==LR_SHIFTKEY))
2366                                                         selectlinks_menu();
2367                                                 else if(G.qual==LR_CTRLKEY)
2368                                                         make_links_menu();
2369                                         }
2370                                 }
2371                                 break;
2372                         case MKEY:
2373                                 if(G.obedit){
2374                                         if (ELEM(G.qual, 0, LR_SHIFTKEY) && (G.obedit->type==OB_ARMATURE)) {
2375                                                 pose_movetolayer();
2376                                         }
2377                                         else if (G.qual==LR_ALTKEY) {
2378                                                 if (G.obedit->type == OB_MESH) {
2379                                                         mergemenu();
2380                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2381                                                 }
2382                                                 else if (G.obedit->type == OB_ARMATURE) {
2383                                                         merge_armature();
2384                                                 }
2385                                         }
2386                                         else if ((G.qual==0) || (G.qual==LR_CTRLKEY)) {
2387                                                 mirrormenu();
2388                                         }
2389                                         else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2390                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
2391                                         }
2392                                 }
2393                                 else if(G.qual & LR_CTRLKEY) {
2394                                         mirrormenu();
2395                                 }
2396                                 else if(G.qual==0 || G.qual==LR_SHIFTKEY) {
2397                                         if(ob && (ob->flag & OB_POSEMODE))
2398                                                 pose_movetolayer();
2399                                         else
2400                                                 movetolayer();
2401                                 }
2402                                 break;
2403                         case NKEY:
2404                                 if((G.qual==0)) {
2405                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
2406                                         allqueue(REDRAWVIEW3D, 0);
2407                                 }
2408                                 else if(G.obedit) {
2409                                         switch (G.obedit->type){
2410                                         case OB_ARMATURE:
2411                                                 if(G.qual==LR_CTRLKEY) {
2412                                                         pupval= pupmenu("Recalculate Bone Roll Angles%t|Clear Roll (Z-Axis Up) %x1|Align Z-Axis to 3D-Cursor %x2");
2413                                                         if (pupval > 0) {
2414                                                                 auto_align_armature(pupval - 1);
2415                                                                 allqueue(REDRAWVIEW3D, 0);
2416                                                         }
2417                                                 }
2418                                                 break;
2419                                         case OB_MESH: 
2420                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2421                                                         if(okee("Recalculate normals inside")) {
2422                                                                 righthandfaces(2);
2423                                                                 allqueue(REDRAWVIEW3D, 0);
2424                                                                 BIF_undo_push("Recalculate normals inside");
2425                                                         }
2426                                                 }
2427                                                 else if(G.qual==LR_CTRLKEY){
2428                                                         if(okee("Recalculate normals outside")) {
2429                                                                 righthandfaces(1);
2430                                                                 allqueue(REDRAWVIEW3D, 0);
2431                                                                 BIF_undo_push("Recalculate normals outside");
2432                                                         }
2433                                                 }
2434                                                 break;
2435                                         }
2436                                 }
2437                                 
2438                                 break;
2439                         case OKEY:
2440                                 if (G.obedit || G.f&G_PARTICLEEDIT) {
2441                                         if (G.qual==LR_SHIFTKEY) {
2442                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%7;
2443                                                 allqueue(REDRAWHEADERS, 0);
2444                                         }
2445                                         else if((G.qual==LR_ALTKEY)) {
2446                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
2447                                                 else G.scene->proportional= 2;
2448                                                 allqueue(REDRAWHEADERS, 0);
2449                                         }
2450                                         else if((G.qual==0)) {
2451                                                 G.scene->proportional= !G.scene->proportional;
2452                                                 allqueue(REDRAWHEADERS, 0);
2453                                         }
2454                                 }
2455                                 else if((G.qual==LR_SHIFTKEY || G.qual==(LR_ALTKEY|LR_SHIFTKEY))) {
2456                                         flip_subdivison(-1);
2457                                 }
2458                                 else if(G.qual==LR_ALTKEY) {
2459                                         if(okee("Clear origin")) {
2460                                                 clear_object('o');
2461                                         }
2462                                 }
2463                                 break;
2464
2465                         case PKEY:
2466                                 if(G.obedit) {
2467                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2468                                                 if(G.obedit->type==OB_ARMATURE)
2469                                                         make_bone_parent();
2470                                                 else
2471                                                         make_parent();
2472                                         }
2473                                         
2474                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
2475                                                 clear_bone_parent();
2476                                         else if((G.qual==0) && (G.obedit->type==OB_ARMATURE)) 
2477                                                 armature_select_hierarchy(BONE_SELECT_PARENT, 1); // 1 = add to selection
2478                                         else if((G.qual==(LR_CTRLKEY|LR_ALTKEY)) && (G.obedit->type==OB_ARMATURE))
2479                                                 separate_armature();
2480                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
2481                                                 separatemenu();
2482                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
2483                                                 separate_nurb();
2484                                         else if (G.qual==LR_SHIFTKEY) {
2485                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
2486                                                 Transform();
2487                                         }
2488                                 }
2489                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
2490                                         make_parent();
2491                                 else if(G.qual==LR_SHIFTKEY) {
2492                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_PREVIEW, 0);
2493                                         doredraw= 1;
2494                                 }
2495                                 else if(G.qual==(LR_ALTKEY|LR_SHIFTKEY)) {
2496                                         initTransform(TFM_PUSHPULL, CTX_NONE);
2497                                         Transform();
2498                                 }
2499                                 else if(G.qual==LR_ALTKEY)
2500                                         clear_parent();
2501                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY))
2502                                         make_proxy();
2503                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY|LR_SHIFTKEY)) {
2504                         start_RBSimulation();
2505                                 }
2506                                 else if((G.qual==0) && (OBACT) && (OBACT->type==OB_ARMATURE) && (OBACT->flag & OB_POSEMODE))
2507                                         pose_select_hierarchy(BONE_SELECT_PARENT, 1); // 1 = add to selection
2508                                 else if((G.qual==0)) {
2509                         start_game();
2510                                 }
2511                                 break;                          
2512                         case RKEY:
2513                                 if((G.obedit==0) && G.qual==LR_ALTKEY) {
2514                                         if(okee("Clear rotation")) {
2515                                                 clear_object('r');
2516                                         }
2517                                 } 
2518                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2519                                         v3d->twtype= V3D_MANIP_ROTATE;
2520                                         doredraw= 1;
2521                                 }
2522                                 else if (G.obedit) {
2523                                         if((G.qual==LR_SHIFTKEY)) {
2524                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
2525                                                         selectrow_nurb();
2526                                         }
2527                                         else if(G.qual==LR_CTRLKEY) {
2528                                                 if (G.obedit->type==OB_MESH) {
2529                                                         CutEdgeloop(1);
2530                                                         BIF_undo_push("Cut Edgeloop");
2531                                                 }
2532                                                 else if (G.obedit->type==OB_ARMATURE) {
2533                                                         initTransform(TFM_BONE_ROLL, CTX_NONE);
2534                                                         Transform();
2535                                                 }
2536                                         }
2537                                         else if((G.qual==0)) {
2538                                                 initTransform(TFM_ROTATION, CTX_NONE);
2539                                                 Transform();
2540                                         }
2541                                 }
2542                                 else if((G.qual==0)) {
2543                                         initTransform(TFM_ROTATION, CTX_NONE);
2544                                         Transform();
2545                                 }
2546                                 break;
2547                         case SKEY:
2548                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2549                                         v3d->twtype= V3D_MANIP_SCALE;
2550                                         doredraw= 1;
2551                                 }
2552                                 else if(G.obedit) {
2553                                         
2554                                         if(G.qual==LR_ALTKEY) {
2555                                                 if(G.obedit->type==OB_ARMATURE) {
2556                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2557                                                 }
2558                                                 else if (G.obedit->type==OB_CURVE) {
2559                                                         initTransform(TFM_CURVE_SHRINKFATTEN, CTX_NONE);
2560                                                 } else {
2561                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
2562                                                 }
2563                                                 Transform();
2564                                         }
2565                                         else if(G.qual==LR_CTRLKEY) {
2566                                                 initTransform(TFM_SHEAR, CTX_NONE);
2567                                                 Transform();
2568                                         }
2569                                         else if(G.qual==LR_SHIFTKEY)
2570                                                 snapmenu();
2571                                         else if(G.qual==0) {
2572                                                 if(G.obedit->type==OB_ARMATURE) {
2573                                                         bArmature *arm= G.obedit->data;
2574                                                         if(arm->drawtype==ARM_ENVELOPE)
2575                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
2576                                                         else
2577                                                                 initTransform(TFM_RESIZE, CTX_NONE);
2578                                                 }
2579                                                 else
2580                                                         initTransform(TFM_RESIZE, CTX_NONE);
2581                                                 Transform();
2582                                         }
2583                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
2584                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
2585                                                 Transform();
2586                                         }
2587                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2588                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
2589                                         }
2590                                 }
2591                                 else if(G.qual==LR_ALTKEY) {
2592                                         if(G.f & G_WEIGHTPAINT)
2593                                                 ob= ob->parent;
2594                                         if(ob && (ob->flag & OB_POSEMODE)) {
2595                                                 bArmature *arm= ob->data;
2596                                                 if( arm->drawtype==ARM_ENVELOPE) {
2597                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2598                                                         Transform();
2599                                                         break;
2600                                                 }
2601                                         }
2602                                         
2603                                         if(okee("Clear scale")) {
2604                                                 clear_object('s');
2605                                         }
2606                                 }
2607                                 else if(G.qual==LR_SHIFTKEY) {
2608                                         snapmenu();
2609                                 }
2610                                 else if((G.qual==0)) {
2611                                         initTransform(TFM_RESIZE, CTX_NONE);
2612                                         Transform();
2613                                 }
2614                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2615                                         initTransform(TFM_TOSPHERE, CTX_NONE);
2616                                         Transform();
2617                                 }
2618                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
2619                                         initTransform(TFM_SHEAR, CTX_NONE);
2620                                         Transform();
2621                                 }
2622                                 break;
2623                         case TKEY:
2624                                 if(G.qual == LR_SHIFTKEY) { /* toggle texture in solid draw mode */
2625                                         G.vd->flag2 ^= V3D_SOLID_TEX;
2626                                         allqueue(REDRAWVIEW3D, 0);
2627                                 } else if(G.obedit){
2628                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
2629                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
2630                                                 allqueue(REDRAWVIEW3D, 0);
2631                                                 if (EM_texFaceCheck())
2632                                                         allqueue(REDRAWIMAGE, 0);
2633                                                 
2634                                                 countall();
2635                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2636                                         }
2637                                         if (G.obedit->type==OB_CURVE) {
2638                                                 if (G.qual==LR_ALTKEY) {
2639                                                         clear_tilt();
2640                                                 }
2641                                                 else if (G.qual==0) {
2642                                                         initTransform(TFM_TILT, CTX_NONE);
2643                                                         Transform();
2644                                                 }
2645                                         }
2646                                 }
2647                                 else if(G.f & G_PARTICLEEDIT) {
2648                                         initTransform(TFM_BAKE_TIME, CTX_NONE);
2649                                         Transform();
2650                                 }
2651                                 else if(G.qual==LR_CTRLKEY) {
2652                                         if(ob && (ob->flag & OB_POSEMODE));
2653                                         else make_track();
2654                                 }
2655                                 else if(G.qual==LR_ALTKEY) {
2656                                         if(ob && (ob->flag & OB_POSEMODE));
2657                                         else clear_track();
2658                                 }
2659                                 else if((G.qual==0)){
2660                                         texspace_edit();
2661                                 }
2662                                 
2663                                 break;
2664                         case UKEY:
2665                                 /*// Use Ctrl Z like everybody else
2666                                 if(G.obedit) {
2667                                         if(G.obedit->type==OB_MESH) {
2668                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2669                                         }
2670                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
2671                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2672                                         }
2673                                 }*/
2674                                 if(G.obedit) {
2675                                         if(G.obedit->type==OB_MESH && G.qual==0) {
2676                                                 uv_autocalc_tface();
2677                                         }
2678                                 }
2679                                 else if (G.f & G_PARTICLEEDIT){
2680                                         if(G.qual==0) BIF_undo(); else BIF_redo();
2681                                 }
2682                                 else if((G.qual==0)) {
2683                                         if(G.f & G_WEIGHTPAINT)
2684                                                 BIF_undo();
2685                                         else if(G.f & G_VERTEXPAINT)
2686                                                 BIF_undo();
2687                                         else if(G.f & G_TEXTUREPAINT)
2688                                                 undo_imagepaint_step(1);
2689                                         else
2690                                                 single_user();
2691                                 }
2692                                 
2693                                 break;
2694                         case VKEY:
2695                                 if ((G.qual==LR_CTRLKEY)) {
2696                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
2697                                                 Vertex_Menu();
2698                                         }