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