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