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