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