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