* Added a hotkey (T) for the sculpt mode flatten brush
[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                         case TKEY:
1413                                 sd->brush_type= FLATTEN_BRUSH;
1414                                 update_prop= 1; break;
1415                         /* Symmetry */
1416                         case XKEY:
1417                                 sd->symm^= SYMM_X;
1418                                 update_prop= 1; break;
1419                         case YKEY:
1420                                 sd->symm^= SYMM_Y;
1421                                 update_prop= 1; break;
1422                         case ZKEY:
1423                                 sd->symm^= SYMM_Z;
1424                                 update_prop= 1; break;
1425                         /* Interface */
1426                         case NKEY:
1427                                 if(G.qual==0) {
1428                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1429                                         allqueue(REDRAWVIEW3D, 0);
1430                                 }
1431                                 break;
1432                         /* Multires */
1433                         case PAGEUPKEY:
1434                                 if(me && me->mr) {
1435                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current+1;
1436                                         multires_set_level(ob, ob->data, 0);
1437                                 }
1438                                 break;
1439                         case PAGEDOWNKEY:
1440                                 if(me && me->mr) {
1441                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current-1;
1442                                         multires_set_level(ob, ob->data, 0);
1443                                 }
1444                                 break;
1445                         /* Partial Visibility */
1446                         case HKEY:
1447                                 if(G.qual==LR_ALTKEY) {
1448                                         waitcursor(1);
1449                                         sculptmode_pmv_off(get_mesh(ob));
1450                                         BIF_undo_push("Partial mesh hide");
1451                                         allqueue(REDRAWVIEW3D,0);
1452                                         waitcursor(0);
1453                                 }
1454                                 break;
1455                         /* Layers */
1456                         case ONEKEY:
1457                                 do_layer_buttons(0); break;
1458                         case TWOKEY:
1459                                 do_layer_buttons(1); break;
1460                         case THREEKEY:
1461                                 do_layer_buttons(2); break;
1462                         case FOURKEY:
1463                                 do_layer_buttons(3); break;
1464                         case FIVEKEY:
1465                                 do_layer_buttons(4); break;
1466                         case SIXKEY:
1467                                 do_layer_buttons(5); break;
1468                         case SEVENKEY:
1469                                 do_layer_buttons(6); break;
1470                         case EIGHTKEY:
1471                                 do_layer_buttons(7); break;
1472                         case NINEKEY:
1473                                 do_layer_buttons(8); break;
1474                         case ZEROKEY:
1475                                 do_layer_buttons(9); break;
1476                         case MINUSKEY:
1477                                 do_layer_buttons(10); break;
1478                         case EQUALKEY:
1479                                 do_layer_buttons(11); break;
1480                         case ACCENTGRAVEKEY:
1481                                 do_layer_buttons(-1); break;
1482                         }
1483                         
1484                         /* Redraw buttons window as well as view 3d (for floating panel) */
1485                         if(update_prop) {
1486                                 allqueue(REDRAWVIEW3D, 0);
1487                                 allqueue(REDRAWBUTSEDIT, 0);
1488                         }
1489                 } else {
1490
1491                         if (U.flag & USER_NONUMPAD) {
1492                                 event= convert_for_nonumpad(event);
1493                         }
1494
1495                         switch(event) {
1496                         
1497                         /* Afterqueue events */
1498                         case BACKBUFDRAW:
1499                                 backdrawview3d(1);
1500                                 break;
1501                         case RENDERPREVIEW:
1502                                 BIF_view3d_previewrender(sa);
1503                                 break;
1504                                 
1505                         case TABKEY:
1506                                 /* Shift-Tabe handling (other cases are in toets) */
1507                                 if (G.qual == LR_SHIFTKEY)
1508                                 {
1509                                         /* Snap toggle (only edit mesh right now) */
1510                                         if (G.obedit && G.obedit->type==OB_MESH)
1511                                         {
1512                                                 G.vd->flag2 ^= V3D_TRANSFORM_SNAP;
1513                                                 allqueue(REDRAWHEADERS, 0);
1514                                         }                                       
1515                                 }
1516                                 break;
1517                                 
1518                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1519                          * based on user preference USER_LMOUSESELECT
1520                          */
1521                         case LEFTMOUSE: 
1522                                 if ((G.obedit) || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT))) {
1523                                         mouse_cursor();
1524                                 }
1525                                 else if (G.f & G_WEIGHTPAINT) {
1526                                         weight_paint();
1527                                 }
1528                                 else if (G.f & G_VERTEXPAINT) {
1529                                         vertex_paint();
1530                                 }
1531                                 else if (G.f & G_TEXTUREPAINT) {
1532                                         imagepaint_paint(origevent==LEFTMOUSE? L_MOUSE: R_MOUSE, 1);
1533                                 }
1534                                 break;
1535                         case MIDDLEMOUSE:
1536                                 handle_view_middlemouse();
1537                                 break;
1538                         case RIGHTMOUSE:
1539                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
1540                                         if(G.obedit->type==OB_MESH)
1541                                                 mouse_mesh();
1542                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1543                                                 mouse_nurb();
1544                                         else if(G.obedit->type==OB_MBALL)
1545                                                 mouse_mball();
1546                                         else if(G.obedit->type==OB_LATTICE)
1547                                                 mouse_lattice();
1548                                         else if(G.obedit->type==OB_ARMATURE)
1549                                                 mouse_armature();
1550                                 }
1551                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
1552                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1553                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
1554                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1555                                 else if(G.qual==LR_CTRLKEY)
1556                                         mouse_select(); /* also allow in editmode, for vertex parenting */
1557                                 else if(G.f & G_FACESELECT)
1558                                         face_select();
1559                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
1560                                         sample_vpaint();
1561                                 else
1562                                         mouse_select(); /* does poses too */
1563                                 break;
1564                         case WHEELUPMOUSE:
1565                                 handle_view_wheelup();
1566                                 doredraw= 1;
1567                                 break;
1568                         case WHEELDOWNMOUSE:
1569                                 handle_view_wheeldown();
1570                                 doredraw= 1;
1571                                 break;
1572
1573                         case ONEKEY:
1574                                 if(G.qual==LR_CTRLKEY) {
1575                                         flip_subdivison(1);
1576                                 }
1577                                 else do_layer_buttons(0); 
1578                                 break;
1579                                 
1580                         case TWOKEY:
1581                                 if(G.qual==LR_CTRLKEY) {
1582                                         flip_subdivison(2);
1583                                 }
1584                                 else do_layer_buttons(1); 
1585                                 break;
1586                                 
1587                         case THREEKEY:
1588                                 if(G.qual==LR_CTRLKEY) {
1589                                         flip_subdivison(3);
1590                                 }
1591                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1592                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1593                                                 select_faces_by_numverts(3);
1594                                 }
1595                                 else do_layer_buttons(2); 
1596                                 break;
1597                                 
1598                         case FOURKEY:
1599                                 if(G.qual==LR_CTRLKEY) {
1600                                         flip_subdivison(4);
1601                                 }
1602                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1603                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1604                                                 select_faces_by_numverts(4);
1605                                 }
1606                                 else do_layer_buttons(3); 
1607                                 break;
1608                                 
1609                         case FIVEKEY:
1610                                 if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1611                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1612                                                 select_faces_by_numverts(5);
1613                                 }
1614                                 else do_layer_buttons(4);
1615                                 break;
1616
1617                         case SIXKEY:
1618                                 do_layer_buttons(5); break;
1619                         case SEVENKEY:
1620                                 do_layer_buttons(6); break;
1621                         case EIGHTKEY:
1622                                 do_layer_buttons(7); break;
1623                         case NINEKEY:
1624                                 do_layer_buttons(8); break;
1625                         case ZEROKEY:
1626                                 do_layer_buttons(9); break;
1627                         case MINUSKEY:
1628                                 do_layer_buttons(10); break;
1629                         case EQUALKEY:
1630                                 do_layer_buttons(11); break;
1631                         case ACCENTGRAVEKEY:
1632                                 do_layer_buttons(-1); break;
1633                         
1634                         case SPACEKEY:
1635                                 if(G.qual == LR_CTRLKEY) {
1636                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1637                                         if(val>0) {
1638                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1639                                                 else {
1640                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1641                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1642                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1643                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1644                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1645                                                 }
1646                                                 doredraw= 1;
1647                                         }
1648                                 }
1649                                 else if(G.qual == LR_ALTKEY) {
1650                                         BIF_selectOrientation();
1651                                         doredraw= 1;
1652                                 }
1653
1654                                 break;
1655                                 
1656                         case AKEY:
1657                                 if(G.qual & LR_CTRLKEY) apply_object(); /* also with shift! */
1658                                 else if((G.qual==LR_SHIFTKEY)) {
1659                                         toolbox_n_add();
1660                                 }
1661                                 else {
1662                                         if(G.obedit) {
1663                                                 if(G.obedit->type==OB_MESH)
1664                                                         deselectall_mesh();
1665                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1666                                                         deselectall_nurb();
1667                                                 else if(G.obedit->type==OB_MBALL)
1668                                                         deselectall_mball();
1669                                                 else if(G.obedit->type==OB_LATTICE)
1670                                                         deselectall_Latt();
1671                                                 else if(G.obedit->type==OB_ARMATURE)
1672                                                         deselectall_armature(1, 1);     /* 1 == toggle */
1673                                         }
1674                                         else if (ob && (ob->flag & OB_POSEMODE)){
1675                                                 deselectall_posearmature(ob, 1, 1);
1676                                         }
1677                                         else {
1678                                                 if(G.f & G_FACESELECT) deselectall_tface();
1679                                                 else {
1680                                                         /* by design, the center of the active object 
1681                                                          * (which need not necessarily by selected) will
1682                                                          * still be drawn as if it were selected.
1683                                                          */
1684                                                         deselectall();
1685                                                 }
1686                                         }
1687                                 }
1688                                 break;
1689                         case BKEY:
1690                                 if(G.qual==LR_ALTKEY)
1691                                         view3d_edit_clipping(v3d);
1692                                 else if(G.qual==LR_SHIFTKEY)
1693                                 {
1694                                         if(G.vd->persp==2)
1695                                                 set_render_border();
1696                                         else
1697                                                 view3d_border_zoom();
1698                                 }
1699                                 else if(G.qual==LR_CTRLKEY) {
1700                                         if(okee("Bake all selected")) {
1701                                                 extern void softbody_bake(Object *ob);
1702                                                 extern void fluidsimBake(Object *ob);
1703                                                 softbody_bake(NULL);
1704                                                 /* also bake first domain of selected objects... */
1705                                                 fluidsimBake(NULL);
1706                                         }
1707                                 }
1708                                 else if(G.qual== (LR_ALTKEY|LR_CTRLKEY))
1709                                         objects_bake_render_menu();
1710                                 else if(G.qual==0)
1711                                         borderselect();
1712                                 break;
1713                         case CKEY:
1714                                 if(G.qual==LR_CTRLKEY) {
1715                                         if(ob && (ob->flag & OB_POSEMODE))
1716                                                 pose_copy_menu();       /* poseobject.c */
1717                                         else
1718                                                 copy_attr_menu();
1719                                 }
1720                                 else if(G.qual==LR_ALTKEY) {
1721                                         if(ob && (ob->flag & OB_POSEMODE))
1722                                                 pose_clear_constraints();       /* poseobject.c */
1723                                         else
1724                                                 convertmenu();  /* editobject.c */
1725                                 }
1726                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY)) 
1727                                         add_constraint(0);      /* editconstraint.c, generic for objects and posemode */
1728                                 else if((G.qual==LR_SHIFTKEY)) {
1729                                         view3d_home(1);
1730                                         curs= give_cursor();
1731                                         curs[0]=curs[1]=curs[2]= 0.0;
1732                                         allqueue(REDRAWVIEW3D, 0);
1733                                 }
1734                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1735                                         makecyclicNurb();
1736                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1737                                         allqueue(REDRAWVIEW3D, 0);
1738                                 }
1739                                 else if((G.qual==0)){
1740                                         if (G.vd->persp==2)
1741                                                 /* center the camera offset */
1742                                                 G.vd->camdx= G.vd->camdy= 0.0;
1743                                         else {
1744                                                 /*non camera center*/
1745                                                 float new_ofs[3];
1746                                                 curs= give_cursor();
1747                                                 new_ofs[0]= -curs[0];
1748                                                 new_ofs[1]= -curs[1];
1749                                                 new_ofs[2]= -curs[2];
1750                                                 smooth_view(G.vd, new_ofs, NULL, NULL, NULL);
1751                                         }
1752                                         scrarea_queue_winredraw(curarea);
1753                                 }
1754                                 break;
1755                         case DKEY:
1756                                 if((G.qual==LR_SHIFTKEY)) {
1757                                         duplicate_context_selected();
1758                                 }
1759                                 else if(G.qual==LR_ALTKEY) {
1760                                         if(ob && (ob->flag & OB_POSEMODE))
1761                                                 error ("Duplicate not possible in posemode.");
1762                                         else if((G.obedit==NULL))
1763                                                 adduplicate(0, 0);
1764                                 }
1765                                 else if(G.qual==LR_CTRLKEY) {
1766                                         imagestodisplist();
1767                                 }
1768                                 else if((G.qual==0)){
1769                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
1770                                         if(pupval>0) {
1771                                                 G.vd->drawtype= pupval;
1772                                                 doredraw= 1;
1773                                         }
1774                                 }
1775                                 
1776                                 break;
1777                         case EKEY:
1778                                 if (G.qual==0){
1779                                         if(G.obedit) {
1780                                                 if(G.obedit->type==OB_MESH)
1781                                                         extrude_mesh();
1782                                                 else if(G.obedit->type==OB_CURVE)
1783                                                         addvert_Nurb('e');
1784                                                 else if(G.obedit->type==OB_SURF)
1785                                                         extrude_nurb();
1786                                                 else if(G.obedit->type==OB_ARMATURE)
1787                                                         extrude_armature(0);
1788                                         }
1789                                 }
1790                                 else if (G.qual==LR_CTRLKEY) {
1791                                         if(G.obedit && G.obedit->type==OB_MESH)
1792                                                 Edge_Menu();
1793                                         else if (G.f & G_FACESELECT)
1794                                                 seam_mark_clear_tface(0);
1795                                 }
1796                                 else if (G.qual==LR_SHIFTKEY) {
1797                                         if (G.obedit && G.obedit->type==OB_MESH &&
1798                                             !multires_level1_test()) {
1799                                                 initTransform(TFM_CREASE, CTX_EDGE);
1800                                                 Transform();
1801                                         }
1802                                         else if (G.obedit && G.obedit->type==OB_ARMATURE) {
1803                                                 extrude_armature(1);
1804                                         }
1805                                 }
1806                                 break;
1807                         case FKEY:
1808                                 if(G.obedit) {
1809                                         if(G.obedit->type==OB_MESH) {
1810                                                 if((G.qual==LR_SHIFTKEY))
1811                                                         fill_mesh();
1812                                                 else if(G.qual==LR_ALTKEY)
1813                                                         beauty_fill();
1814                                                 else if(G.qual==LR_CTRLKEY)
1815                                                         edge_flip();
1816                                                 else if (G.qual==0)
1817                                                         addedgeface_mesh();
1818                                                 else if ( G.qual == 
1819                                                          (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1820                                                         select_linked_flat_faces();
1821                                                 }
1822
1823                                         }
1824                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
1825                                 }
1826                                 else if(G.qual==LR_CTRLKEY)
1827                                         sort_faces();
1828                                 else if((G.qual==LR_SHIFTKEY)) {
1829                                         if(ob && (ob->flag & OB_POSEMODE))
1830                                            pose_activate_flipped_bone();
1831                                         else if(G.f & G_WEIGHTPAINT)
1832                                                 pose_activate_flipped_bone();
1833                                         else
1834                                                 fly();
1835                                 }
1836                                 else {
1837                                         set_faceselect();
1838                                 }
1839                                 
1840                                 break;
1841                         case GKEY:
1842                                 if((G.qual == LR_CTRLKEY)) {
1843                                         if(G.obedit) {
1844                                                 if(ELEM(G.obedit->type, OB_MESH, OB_LATTICE))
1845                                                         vgroup_assign_with_menu();
1846                                         }
1847                                         else
1848                                                 group_operation_with_menu();
1849                                 }
1850                                 else if((G.qual == (LR_CTRLKEY|LR_SHIFTKEY))) {
1851                                         if(G.obedit) {
1852                                                 if(ELEM(G.obedit->type, OB_MESH, OB_LATTICE))
1853                                                         vgroup_operation_with_menu();
1854                                         }
1855                                 }
1856                                 else if((G.qual==LR_SHIFTKEY))
1857                                         if(G.obedit) {
1858                                                 if(G.obedit->type==OB_MESH)
1859                                                         select_mesh_group_menu();
1860                                         } else
1861                                                 select_object_grouped_menu();
1862                                 else if((G.obedit==0) && G.qual==LR_ALTKEY) {
1863                                         if(okee("Clear location")) {
1864                                                 clear_object('g');
1865                                         }
1866                                 }
1867                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1868                                         v3d->twtype= V3D_MANIP_TRANSLATE;
1869                                         doredraw= 1;
1870                                 }
1871                                 else if((G.qual==0)) {
1872                                         initTransform(TFM_TRANSLATION, CTX_NONE);
1873                                         Transform();
1874                                 }
1875                                 break;
1876                         case HKEY:
1877                                 if(G.obedit) {
1878                                         if(G.obedit->type==OB_MESH) {
1879                                                 if(G.qual==LR_CTRLKEY)
1880                                                         add_hook();
1881                                                 else if(G.qual==LR_ALTKEY)
1882                                                         reveal_mesh();
1883                                                 else if((G.qual==LR_SHIFTKEY))
1884                                                         hide_mesh(1);
1885                                                 else if((G.qual==0)) 
1886                                                         hide_mesh(0);
1887                                         }
1888                                         else if(G.obedit->type== OB_SURF) {
1889                                                 if(G.qual==LR_CTRLKEY)
1890                                                         add_hook();
1891                                                 else if(G.qual==LR_ALTKEY)
1892                                                         revealNurb();
1893                                                 else if((G.qual==LR_SHIFTKEY))
1894                                                         hideNurb(1);
1895                                                 else if((G.qual==0))
1896                                                         hideNurb(0);
1897                                         }
1898                                         else if(G.obedit->type==OB_CURVE) {
1899                                                 if(G.qual==LR_CTRLKEY)
1900                                                         add_hook();
1901                                                 else if(G.qual==LR_ALTKEY)
1902                                                         revealNurb();
1903                                                 /* should be G.qual==LR_SHIFTKEY, but that is taken fro handles already */
1904                                                 else if((G.qual==(LR_ALTKEY|LR_SHIFTKEY))) 
1905                                                         hideNurb(1);
1906                                                 /* should be G.qual==0, but that is taken for handles already */
1907                                                 else if((G.qual==(LR_ALTKEY|LR_CTRLKEY))) 
1908                                                         hideNurb(0);
1909                                                 else {
1910                                                         if(G.qual==LR_CTRLKEY) /* conflict */
1911                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
1912                                                         else if((G.qual==LR_SHIFTKEY))
1913                                                                 sethandlesNurb(1);
1914                                                         else if((G.qual==0))
1915                                                                 sethandlesNurb(3);
1916                                                         
1917                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1918                                                         BIF_undo_push("Handle change");
1919                                                         allqueue(REDRAWVIEW3D, 0);
1920                                                 }
1921                                         }
1922                                         else if(G.obedit->type==OB_LATTICE) {
1923                                                 if(G.qual==LR_CTRLKEY) add_hook();
1924                                         }
1925                                         else if(G.obedit->type==OB_MBALL) {
1926                                                 if(G.qual==LR_ALTKEY)
1927                                                         reveal_mball();
1928                                                 else if((G.qual==LR_SHIFTKEY))
1929                                                         hide_mball(1);
1930                                                 else if((G.qual==0)) 
1931                                                         hide_mball(0);
1932                                         }
1933                                         else if(G.obedit->type==OB_ARMATURE) {
1934                                                 if (G.qual==0)
1935                                                         hide_selected_armature_bones();
1936                                                 else if (G.qual==LR_SHIFTKEY)
1937                                                         hide_unselected_armature_bones();
1938                                                 else if (G.qual==LR_ALTKEY)
1939                                                         show_all_armature_bones();
1940                                         }
1941                                 }
1942                                 else if(G.f & G_FACESELECT)
1943                                         hide_tface();
1944                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1945                                         if (G.qual==0)
1946                                                 hide_selected_pose_bones();
1947                                         else if (G.qual==LR_SHIFTKEY)
1948                                                 hide_unselected_pose_bones();
1949                                         else if (G.qual==LR_ALTKEY)
1950                                                 show_all_pose_bones();
1951                                 }
1952                                 /* Object mode only, other modes are accounted for above */
1953                                 else if ((G.f & (G_VERTEXPAINT|G_TEXTUREPAINT|G_WEIGHTPAINT))==0) {
1954                                         if(G.qual==LR_CTRLKEY)hookmenu();
1955                                         else if(G.qual==LR_ALTKEY) show_objects();
1956                                         else if(G.qual==LR_SHIFTKEY) hide_objects(0);
1957                                         else hide_objects(1);
1958                                 }
1959                                 break;
1960                         case IKEY:
1961                                 if(G.obedit);
1962                                 else if(G.qual==LR_CTRLKEY) {
1963                                         if(ob && ob->type==OB_ARMATURE) 
1964                                                 if(ob->flag & OB_POSEMODE) 
1965                                                         pose_add_IK();
1966                                 }
1967                                 else if(G.qual==LR_ALTKEY) {
1968                                         if(ob && ob->type==OB_ARMATURE) 
1969                                                 if(ob->flag & OB_POSEMODE) 
1970                                                         pose_clear_IK();
1971                                 }
1972                                 break;
1973                                 
1974                         case JKEY:
1975                                 if(G.qual==LR_CTRLKEY) {
1976                                         if( ob ) {
1977                                                 join_menu();
1978                                         }
1979                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1980                                                 addsegment_nurb();
1981                                 }
1982                                 else if(G.obedit) {
1983                                         if(G.obedit->type==OB_MESH) {
1984                                                 join_triangles();
1985                                         }
1986                                 }
1987
1988                                 break;
1989                         case KKEY:
1990                                 if(G.obedit) {
1991                                         if (G.obedit->type==OB_MESH) {
1992                                                 if (G.qual==LR_SHIFTKEY)
1993                                                         KnifeSubdivide(KNIFE_PROMPT);
1994                                                 else if (G.qual==0)
1995                                                         LoopMenu();
1996                                         }
1997                                         else if(G.obedit->type==OB_SURF)
1998                                                 printknots();
1999                                 }
2000                                 else {
2001                                         if((G.qual==LR_SHIFTKEY)) {
2002                                                 if(G.f & G_FACESELECT)
2003                                                         if (G.f & G_WEIGHTPAINT)
2004                                                                 clear_wpaint_selectedfaces();
2005                                                         else
2006                                                                 clear_vpaint_selectedfaces();
2007                                                 else if(G.f & G_VERTEXPAINT)
2008                                                         clear_vpaint();
2009                                                 else
2010                                                         select_select_keys();
2011                                         }
2012                                         else if (G.qual==0)
2013                                                 set_ob_ipoflags();
2014                                 }
2015                                 
2016                                 break;
2017                         case LKEY:
2018                                 if(G.obedit) {
2019                                         if(G.obedit->type==OB_MESH)
2020                                                 selectconnected_mesh(G.qual);
2021                                         if(G.obedit->type==OB_ARMATURE)
2022                                                 selectconnected_armature();
2023                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
2024                                                 selectconnected_nurb();
2025                                 }
2026                                 else if(ob && (ob->flag & OB_POSEMODE)) {
2027                                         selectconnected_posearmature();
2028                                 }
2029                                 else {
2030                                         if(G.f & G_FACESELECT) {
2031                                                 if((G.qual==0))
2032                                                         select_linked_tfaces(0);
2033                                                 else if((G.qual==LR_SHIFTKEY))
2034                                                         select_linked_tfaces(1);
2035                                                 else if(G.qual==LR_CTRLKEY)
2036                                                         select_linked_tfaces(2);
2037                                         }
2038                                         else {
2039                                                 if((G.qual==0))
2040                                                         make_local_menu();
2041                                                 else if((G.qual==LR_SHIFTKEY))
2042                                                         selectlinks_menu();
2043                                                 else if(G.qual==LR_CTRLKEY)
2044                                                         make_links_menu();
2045                                         }
2046                                 }
2047                                 break;
2048                         case MKEY:
2049                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
2050                                         mirror_uv_tface();
2051                                 else if(G.obedit){
2052                                         if(G.qual==LR_ALTKEY) {
2053                                                 if(G.obedit->type==OB_MESH) {
2054                                                         mergemenu();
2055                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2056                                                 }
2057                                         }
2058                                         else if((G.qual==0) || (G.qual==LR_CTRLKEY)) {
2059                                                 mirrormenu();
2060                                         }
2061                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2062                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
2063                                         }
2064                                 }
2065                                 else if(G.qual & LR_CTRLKEY) {
2066                                         mirrormenu();
2067                                 }
2068                                 else if(G.qual==0 || G.qual==LR_SHIFTKEY) {
2069                                         if(ob && (ob->flag & OB_POSEMODE))
2070                                                 pose_movetolayer();
2071                                         else
2072                                                 movetolayer();
2073                                 }
2074                                 break;
2075                         case NKEY:
2076                                 if((G.qual==0)) {
2077                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
2078                                         allqueue(REDRAWVIEW3D, 0);
2079                                 }
2080                                 else if(G.obedit) {
2081                                         switch (G.obedit->type){
2082                                         case OB_ARMATURE:
2083                                                 if(G.qual==LR_CTRLKEY){
2084                                                         if (okee("Recalculate bone roll angles")) {
2085                                                                 auto_align_armature();
2086                                                                 allqueue(REDRAWVIEW3D, 0);
2087                                                         }
2088                                                 }
2089                                                 break;
2090                                         case OB_MESH: 
2091                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2092                                                         if(okee("Recalculate normals inside")) {
2093                                                                 righthandfaces(2);
2094                                                                 allqueue(REDRAWVIEW3D, 0);
2095                                                                 BIF_undo_push("Recalculate normals inside");
2096                                                         }
2097                                                 }
2098                                                 else if(G.qual==LR_CTRLKEY){
2099                                                         if(okee("Recalculate normals outside")) {
2100                                                                 righthandfaces(1);
2101                                                                 allqueue(REDRAWVIEW3D, 0);
2102                                                                 BIF_undo_push("Recalculate normals outside");
2103                                                         }
2104                                                 }
2105                                                 break;
2106                                         }
2107                                 }
2108                                 
2109                                 break;
2110                         case OKEY:
2111                                 if (G.obedit) {
2112                                         if (G.qual==LR_SHIFTKEY) {
2113                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%7;
2114                                                 allqueue(REDRAWHEADERS, 0);
2115                                         }
2116                                         else if((G.qual==LR_ALTKEY)) {
2117                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
2118                                                 else G.scene->proportional= 2;
2119                                                 allqueue(REDRAWHEADERS, 0);
2120                                         }
2121                                         else if((G.qual==0)) {
2122                                                 G.scene->proportional= !G.scene->proportional;
2123                                                 allqueue(REDRAWHEADERS, 0);
2124                                         }
2125                                 }
2126                                 else if((G.qual==LR_SHIFTKEY || G.qual==(LR_ALTKEY|LR_SHIFTKEY))) {
2127                                         flip_subdivison(-1);
2128                                 }
2129                                 else if(G.qual==LR_ALTKEY) {
2130                                         if(okee("Clear origin")) {
2131                                                 clear_object('o');
2132                                         }
2133                                 }
2134                                 break;
2135
2136                         case PKEY:
2137                                 if(G.obedit) {
2138                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2139                                                 if(G.obedit->type==OB_ARMATURE)
2140                                                         make_bone_parent();
2141                                                 else
2142                                                         make_parent();
2143                                         }
2144                                         
2145                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
2146                                                 clear_bone_parent();
2147                                         else if((G.qual==0) && (G.obedit->type==OB_ARMATURE)) 
2148                                                 select_bone_parent();
2149                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
2150                                                 separatemenu();
2151                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
2152                                                 separate_nurb();
2153                                         else if (G.qual==LR_SHIFTKEY) {
2154                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
2155                                                 Transform();
2156                                         }
2157                                 }
2158                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
2159                                         make_parent();
2160                                 else if(G.qual==LR_SHIFTKEY) {
2161                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_PREVIEW, 0);
2162                                         doredraw= 1;
2163                                 }
2164                                 else if(G.qual==(LR_ALTKEY|LR_SHIFTKEY)) {
2165                                         initTransform(TFM_PUSHPULL, CTX_NONE);
2166                                         Transform();
2167                                 }
2168                                 else if(G.qual==LR_ALTKEY)
2169                                         clear_parent();
2170                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY))
2171                                         make_proxy();
2172                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY|LR_SHIFTKEY)) {
2173                         start_RBSimulation();
2174                                 }
2175                                 else if((G.qual==0) && (OBACT) && (OBACT->type==OB_ARMATURE) && (OBACT->flag & OB_POSEMODE))
2176                                         select_bone_parent();
2177                                 else if((G.qual==0)) {
2178                         start_game();
2179                                 }
2180                                 break;                          
2181                         case RKEY:
2182                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0) && !(G.f & G_WEIGHTPAINT))
2183                                         rotate_uv_tface();
2184                                 else if((G.obedit==0) && G.qual==LR_ALTKEY) {
2185                                         if(okee("Clear rotation")) {
2186                                                 clear_object('r');
2187                                         }
2188                                 } 
2189                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2190                                         v3d->twtype= V3D_MANIP_ROTATE;
2191                                         doredraw= 1;
2192                                 }
2193                                 else if (G.obedit) {
2194                                         if((G.qual==LR_SHIFTKEY)) {
2195                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
2196                                                         selectrow_nurb();
2197                                         }
2198                                         else if(G.qual==LR_CTRLKEY) {
2199                                                 if (G.obedit->type==OB_MESH) {
2200                                                         CutEdgeloop(1);
2201                                                         BIF_undo_push("Cut Edgeloop");
2202                                                 }
2203                                                 else if (G.obedit->type==OB_ARMATURE) {
2204                                                         initTransform(TFM_BONE_ROLL, CTX_NONE);
2205                                                         Transform();
2206                                                 }
2207                                         }
2208                                         else if((G.qual==0)) {
2209                                                 initTransform(TFM_ROTATION, CTX_NONE);
2210                                                 Transform();
2211                                         }
2212                                 }
2213                                 else if((G.qual==0)) {
2214                                         initTransform(TFM_ROTATION, CTX_NONE);
2215                                         Transform();
2216                                 }
2217                                 break;
2218                         case SKEY:
2219                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2220                                         v3d->twtype= V3D_MANIP_SCALE;
2221                                         doredraw= 1;
2222                                 }
2223                                 else if(G.obedit) {
2224                                         
2225                                         if(G.qual==LR_ALTKEY) {
2226                                                 if(G.obedit->type==OB_ARMATURE) {
2227                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2228                                                 }
2229                                                 else if (G.obedit->type==OB_CURVE) {
2230                                                         initTransform(TFM_CURVE_SHRINKFATTEN, CTX_NONE);
2231                                                 } else {
2232                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
2233                                                 }
2234                                                 Transform();
2235                                         }
2236                                         else if(G.qual==LR_CTRLKEY) {
2237                                                 initTransform(TFM_SHEAR, CTX_NONE);
2238                                                 Transform();
2239                                         }
2240                                         else if(G.qual==LR_SHIFTKEY)
2241                                                 snapmenu();
2242                                         else if(G.qual==0) {
2243                                                 if(G.obedit->type==OB_ARMATURE) {
2244                                                         bArmature *arm= G.obedit->data;
2245                                                         if(arm->drawtype==ARM_ENVELOPE)
2246                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
2247                                                         else
2248                                                                 initTransform(TFM_RESIZE, CTX_NONE);
2249                                                 }
2250                                                 else
2251                                                         initTransform(TFM_RESIZE, CTX_NONE);
2252                                                 Transform();
2253                                         }
2254                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
2255                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
2256                                                 Transform();
2257                                         }
2258                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2259                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
2260                                         }
2261                                 }
2262                                 else if(G.qual==LR_ALTKEY) {
2263                                         if(G.f & G_WEIGHTPAINT)
2264                                                 ob= ob->parent;
2265                                         if(ob && (ob->flag & OB_POSEMODE)) {
2266                                                 bArmature *arm= ob->data;
2267                                                 if( ELEM(arm->drawtype, ARM_B_BONE, ARM_ENVELOPE)) {
2268                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2269                                                         Transform();
2270                                                         break;
2271                                                 }
2272                                         }
2273                                         
2274                                         if(okee("Clear scale")) {
2275                                                 clear_object('s');
2276                                         }
2277                                 }
2278                                 else if(G.qual==LR_SHIFTKEY) {
2279                                         snapmenu();
2280                                 }
2281                                 else if((G.qual==0)) {
2282                                         initTransform(TFM_RESIZE, CTX_NONE);
2283                                         Transform();
2284                                 }
2285                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2286                                         initTransform(TFM_TOSPHERE, CTX_NONE);
2287                                         Transform();
2288                                 }
2289                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
2290                                         initTransform(TFM_SHEAR, CTX_NONE);
2291                                         Transform();
2292                                 }
2293                                 break;
2294                         case TKEY:
2295                                 if(G.obedit){
2296                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
2297                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
2298                                                 allqueue(REDRAWVIEW3D, 0);
2299                                                 countall();
2300                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2301                                         }
2302                                         if (G.obedit->type==OB_CURVE) {
2303                                                 if (G.qual==LR_ALTKEY) {
2304                                                         clear_tilt();
2305                                                 }
2306                                                 else if (G.qual==0) {
2307                                                         initTransform(TFM_TILT, CTX_NONE);
2308                                                         Transform();
2309                                                 }
2310                                         }
2311                                 }
2312                                 else if(G.qual==LR_CTRLKEY) {
2313                                         if(ob && (ob->flag & OB_POSEMODE));
2314                                         else make_track();
2315                                 }
2316                                 else if(G.qual==LR_ALTKEY) {
2317                                         if(ob && (ob->flag & OB_POSEMODE));
2318                                         else clear_track();
2319                                 }
2320                                 else if((G.qual==0)){
2321                                         texspace_edit();
2322                                 }
2323                                 
2324                                 break;
2325                         case UKEY:
2326                                 if(G.obedit) {
2327                                         if(G.obedit->type==OB_MESH) {
2328                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2329                                         }
2330                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
2331                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2332                                         }
2333                                 }
2334                                 else if((G.qual==0)) {
2335                                         if(G.f & G_WEIGHTPAINT)
2336                                                 BIF_undo();
2337                                         else if(G.f & G_VERTEXPAINT)
2338                                                 BIF_undo();
2339                                         else if(G.f & G_TEXTUREPAINT)
2340                                                 imagepaint_undo();
2341                                         else if (G.f & G_FACESELECT)
2342                                                 uv_autocalc_tface();
2343                                         else {
2344                                                 single_user();
2345                                         }
2346                                 }
2347                                 
2348                                 break;
2349                         case VKEY:
2350                                 if((G.qual==LR_SHIFTKEY)) {
2351                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
2352                                                 align_view_to_selected(v3d);
2353                                         }
2354                                         else if (G.f & G_FACESELECT) {
2355                                                 align_view_to_selected(v3d);
2356                                         }
2357                                 }
2358                                 else if(G.qual==LR_ALTKEY)
2359                                         image_aspect();
2360                                 else if (G.qual==0){
2361                                         if(G.obedit) {
2362                                                 if(G.obedit->type==OB_MESH) {
2363                                                         mesh_rip();
2364                                                 }
2365                                                 else if(G.obedit->type==OB_CURVE) {
2366                                                         sethandlesNurb(2);
2367                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2368                                                         allqueue(REDRAWVIEW3D, 0);
2369                                                         BIF_undo_push("Handle change");
2370                                                 }
2371                                         }
2372                                         else if(ob && ob->type == OB_MESH) 
2373                                                 set_vpaint();
2374                                 }
2375                                 break;
2376                         case WKEY:
2377                                 if((G.qual==LR_SHIFTKEY)) {
2378                                         initTransform(TFM_WARP, CTX_NONE);
2379                                         Transform();
2380                                 }
2381                                 else if(G.qual==LR_ALTKEY) {
2382                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
2383                                 }
2384                                 else if(G.qual==LR_CTRLKEY) {
2385                                         if(G.obedit) {
2386                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
2387                                                         switchdirectionNurb2();
2388                                                 }
2389                                         }
2390                                 }
2391                                 else if((G.qual==0))
2392                                         special_editmenu();
2393                                 
2394                                 break;
2395                         case XKEY:
2396                         case DELKEY:
2397                                 if(G.qual==0 || G.qual==LR_SHIFTKEY)
2398                                         delete_context_selected();
2399                                 break;
2400                         case YKEY:
2401                                 if((G.qual==0) && (G.obedit)) {
2402                                         if(G.obedit->type==OB_MESH) split_mesh();
2403                                 }
2404                                 break;
2405                         case ZKEY:
2406                                 toggle_shading();
2407                                 
2408                                 scrarea_queue_headredraw(curarea);
2409                                 scrarea_queue_winredraw(curarea);
2410                                 break;
2411                         
2412                         case HOMEKEY:
2413                                 if(G.qual==0)
2414                                         view3d_home(0);
2415                                 break;
2416                         case COMMAKEY:
2417                                 if(G.qual==LR_CTRLKEY) {
2418                                         G.vd->around= V3D_CENTROID;
2419                                 } else if(G.qual==LR_SHIFTKEY) {
2420                                         G.vd->around= V3D_CENTROID;
2421                                 } else if(G.qual==0) {
2422                                         G.vd->around= V3D_CENTER;
2423                                 }
2424                                 handle_view3d_around();
2425                                 
2426                                 scrarea_queue_headredraw(curarea);
2427                                 scrarea_queue_winredraw(curarea);
2428                                 break;
2429                                 
2430                         case PERIODKEY:
2431                                 if(G.qual==LR_CTRLKEY) {
2432                                         G.vd->around= V3D_LOCAL;
2433                                 }       else if(G.qual==0) {
2434                                         G.vd->around= V3D_CURSOR;
2435                                 }
2436                                 handle_view3d_around();
2437                                 
2438                                 scrarea_queue_headredraw(curarea);
2439                                 scrarea_queue_winredraw(curarea);
2440                                 break;
2441                         
2442                         case PADSLASHKEY:
2443                                 if(G.qual==0) {
2444                                         if(G.vd->localview) {
2445                                                 G.vd->localview= 0;
2446                                                 endlocalview(curarea);
2447                                         }
2448                                         else {
2449                                                 G.vd->localview= 1;
2450                                                 initlocalview();
2451                                         }
2452                                         scrarea_queue_headredraw(curarea);
2453                                 }
2454                                 break;
2455                         case PADASTERKEY:       /* '*' */
2456                                 if(G.qual==0) {
2457                                         if(ob) {
2458                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
2459                                                         editmesh_align_view_to_selected(G.vd, 3);
2460                                                 } 
2461                                                 else if (G.f & G_FACESELECT) {
2462                                                         if(ob->type==OB_MESH) {
2463                                                                 Mesh *me= ob->data;
2464                                                                 faceselect_align_view_to_selected(G.vd, me, 3);
2465                                                         }
2466                                                 }
2467                                                 else
2468                                                         obmat_to_viewmat(ob, 1);
2469                                                 
2470                                                 if(G.vd->persp==2) G.vd->persp= 1;
2471                                                 scrarea_queue_winredraw(curarea);
2472                                         }
2473                                 }
2474                                 break;
2475                         case PADPERIOD: /* '.' */
2476                                 if(G.qual==0)
2477                                         centerview();
2478                                 break;
2479                         
2480                         case PAGEUPKEY:
2481                                 if(G.qual==LR_CTRLKEY)
2482                                         movekey_obipo(1);
2483                                 else if((G.qual==0))
2484                                         nextkey_obipo(1);       /* in editipo.c */
2485                                 break;
2486
2487                         case PAGEDOWNKEY:
2488                                 if(G.qual==LR_CTRLKEY)
2489                                         movekey_obipo(-1);
2490                                 else if((G.qual==0))
2491                                         nextkey_obipo(-1);
2492                                 break;
2493
2494                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
2495                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
2496                         case PADENTER:
2497                                 persptoetsen(event);
2498                                 doredraw= 1;
2499                                 break;
2500                         case PADMINUS:
2501                                 if ( (G.qual==LR_CTRLKEY)
2502                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2503                                         select_less();
2504                                 else if ( (G.qual==LR_CTRLKEY)
2505                                          && (G.obedit) && (G.obedit->type==OB_CURVE) )
2506                                         select_less_nurb();
2507                                 else if ( (G.qual==LR_CTRLKEY)
2508                                          && (G.obedit) && (G.obedit->type==OB_SURF) )
2509                                         select_less_nurb(); 
2510                                 else {
2511                                         persptoetsen(event);
2512                                         doredraw= 1;
2513                                 }
2514                                 break;
2515
2516                         case PADPLUSKEY:
2517                                 if ( (G.qual==LR_CTRLKEY)
2518                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2519                                         select_more();
2520                                 else if ( (G.qual==LR_CTRLKEY)
2521                                          && (G.obedit) && (G.obedit->type==OB_CURVE) )
2522                                         select_more_nurb();
2523                                 else if ( (G.qual==LR_CTRLKEY)
2524                                          && (G.obedit) && (G.obedit->type==OB_SURF) )
2525                                         select_more_nurb();
2526                                 else {
2527                                         persptoetsen(event);
2528                                         doredraw= 1;
2529                                 }
2530                                 break;
2531
2532                         case ESCKEY:
2533                                 if(G.qual==0) {
2534                                         if (G.vd->flag & V3D_DISPIMAGE) {
2535                                                 G.vd->flag &= ~V3D_DISPIMAGE;
2536                                                 doredraw= 1;
2537                                         }
2538                                 }
2539                                 break;
2540                         }
2541                 }
2542         }
2543         
2544         if(doredraw) {
2545                 scrarea_queue_winredraw(curarea);
2546                 scrarea_queue_headredraw(curarea);
2547         }
2548 }
2549
2550 static void initview3d(ScrArea *sa)
2551 {
2552         View3D *vd;
2553         
2554         vd= MEM_callocN(sizeof(View3D), "initview3d");
2555         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
2556
2557         vd->spacetype= SPACE_VIEW3D;
2558         vd->blockscale= 0.7f;
2559         vd->viewquat[0]= 1.0f;
2560         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
2561         vd->persp= 1;
2562         vd->drawtype= OB_WIRE;
2563         vd->view= 7;
2564         vd->dist= 10.0;
2565         vd->lens= 35.0f;
2566         vd->near= 0.01f;
2567         vd->far= 500.0f;
2568         vd->grid= 1.0f;
2569         vd->gridlines= 16;
2570         vd->lay= vd->layact= 1;
2571         if(G.scene) {
2572                 vd->lay= vd->layact= G.scene->lay;
2573                 vd->camera= G.scene->camera;
2574         }
2575         vd->scenelock= 1;
2576         vd->gridflag |= V3D_SHOW_X;
2577         vd->gridflag |= V3D_SHOW_Y;
2578         vd->gridflag |= V3D_SHOW_FLOOR;
2579         vd->gridflag &= ~V3D_SHOW_Z;
2580
2581         vd->depths= NULL;
2582 }
2583
2584
2585 /* ******************** SPACE: IPO ********************** */
2586
2587 static void changeview2dspace(ScrArea *sa, void *spacedata)
2588 {
2589         if(G.v2d==0) return;
2590
2591         test_view2d(G.v2d, curarea->winx, curarea->winy);
2592         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
2593 }
2594
2595 static int get_cfra_from_dx(SpaceIpo * sipo, int dx)
2596 {
2597         if (sipo->blocktype == ID_SEQ) {
2598                 Sequence * seq = (Sequence*) sipo->from;
2599                 if (!seq) {
2600                         return dx;
2601                 }
2602                 if ((seq->flag & SEQ_IPO_FRAME_LOCKED) != 0) {
2603                         return dx;
2604                 } else {
2605                         float m= (seq->enddisp - seq->startdisp)/100.0f;
2606                         float cfra = dx * m + seq->startdisp;
2607
2608                         return (int) cfra;
2609                 }
2610         } else {
2611                 return dx;
2612         }
2613 }
2614
2615 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2616 {
2617         extern void do_ipobuts(unsigned short event);   /* drawipo.c */
2618         unsigned short event= evt->event;
2619         short val= evt->val;
2620         SpaceIpo *sipo= curarea->spacedata.first;
2621         View2D *v2d= &sipo->v2d;
2622         float dx, dy;
2623         int cfra, doredraw= 0;
2624         short mval[2];
2625         short mousebut = L_MOUSE;
2626         
2627         if(sa->win==0) return;
2628
2629         if(val) {
2630                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
2631
2632                 /* swap mouse buttons based on user preference */
2633                 if (U.flag & USER_LMOUSESELECT) {
2634                         if (event == LEFTMOUSE) {
2635                                 event = RIGHTMOUSE;
2636                                 mousebut = L_MOUSE;
2637                         } else if (event == RIGHTMOUSE) {
2638                                 event = LEFTMOUSE;
2639                                 mousebut = R_MOUSE;
2640                         }
2641                 }
2642
2643                 switch(event) {
2644                 case UI_BUT_EVENT:
2645                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
2646                         if(val>0 && val < 256) do_ipowin_buts(val-1);
2647                         else do_ipobuts(val);
2648                         break;
2649                         
2650                 case LEFTMOUSE:
2651                         if( in_ipo_buttons() ) {
2652                                 do_ipo_selectbuttons();
2653                                 doredraw= 1;
2654                         }
2655                         else if(view2dmove(LEFTMOUSE)); /* only checks for sliders */
2656                         else if((G.qual & LR_CTRLKEY) && (sipo->showkey==0)) add_vert_ipo();
2657                         else {
2658                                 do {
2659                                         getmouseco_areawin(mval);
2660                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
2661                                         
2662                                         cfra = get_cfra_from_dx(sipo, (int)dx);
2663                                         if(cfra< 1) cfra= 1;
2664                                         
2665                                         if( cfra!=CFRA ) {
2666                                                 CFRA= cfra;
2667                                                 update_for_newframe_nodraw(0);  /* 1 = nosound */
2668                                                 force_draw_all(0); /* To make constraint sliders redraw */
2669                                         }
2670                                         else PIL_sleep_ms(30);
2671                                 
2672                                 } while(get_mbut() & mousebut);
2673                         }
2674                         break;
2675                 case RIGHTMOUSE:
2676                         mouse_select_ipo();
2677                         allqueue(REDRAWTIME, 0);
2678                         allqueue(REDRAWIPO, 0);
2679                         allqueue(REDRAWACTION, 0);
2680                         allqueue(REDRAWNLA, 0);
2681                         allqueue(REDRAWSOUND, 0);
2682                         break;
2683                 case MIDDLEMOUSE:
2684                         if(in_ipo_buttons()) {
2685                                 scroll_ipobuts();
2686                         }
2687                         else view2dmove(event); /* in drawipo.c */
2688                         break;
2689                 case WHEELUPMOUSE:
2690                 case WHEELDOWNMOUSE:
2691                         view2dmove(event);      /* in drawipo.c */
2692                         break;
2693                 case PADPLUSKEY:
2694                         view2d_zoom(v2d, 0.1154f, sa->winx, sa->winy);
2695                         doredraw= 1;
2696                         break;
2697                 case PADMINUS:
2698                         view2d_zoom(v2d, -0.15f, sa->winx, sa->winy);
2699                         doredraw= 1;
2700                         break;
2701                 case PAGEUPKEY:
2702                         if(G.qual & LR_CTRLKEY)
2703                                 movekey_ipo(1);
2704                         else if(G.qual & LR_SHIFTKEY)
2705                                 nextkey_ipo(1);
2706                         else
2707                                 nextprev_marker(1);
2708                         break;
2709                 case PAGEDOWNKEY:
2710                         if(G.qual & LR_CTRLKEY)
2711                                 movekey_ipo(-1);
2712                         else if(G.qual & LR_SHIFTKEY)
2713                                 nextkey_ipo(-1);
2714                         else
2715                                 nextprev_marker(-1);
2716                         break;
2717                 case HOMEKEY:
2718                         if((G.qual==0))
2719                                 do_ipo_buttons(B_IPOHOME);
2720                         break;
2721                         
2722                 case AKEY:
2723