73fc627ebe63b8146187a0dca07c0d4b7627b067
[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= (int) 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%t|To Selected (top)%x2|To Selected (front)%x1|To Selected (side)%x0");
640
641         if (nr!=-1) {
642                 int axis= nr;
643
644                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
645                         editmesh_align_view_to_selected(v3d, axis);
646                         addqueue(v3d->area->win, REDRAW, 1);
647                 } else if (G.f & G_FACESELECT) {
648                         Object *obact= OBACT;
649                         if (obact && obact->type==OB_MESH) {
650                                 Mesh *me= obact->data;
651
652                                 if (me->mtface) {
653                                         faceselect_align_view_to_selected(v3d, me, axis);
654                                         addqueue(v3d->area->win, REDRAW, 1);
655                                 }
656                         }
657                 }
658         }
659 }
660
661
662 static short select_children(Object *ob, int recursive)
663 {
664         short changed = 0;
665         Base *base;
666         
667         for (base= FIRSTBASE; base; base= base->next) {
668                 if (ob == base->object->parent) {
669                         if (BASE_SELECTABLE(base) && !(base->flag & SELECT)) {
670                                 base->flag |= SELECT;
671                                 base->object->flag |= SELECT;
672                                 changed = 1;
673                         }
674                         
675                         if (recursive)
676                                 changed |= select_children(base->object, 1);
677                 }
678         }
679         return changed;
680 }
681
682 static short select_parent(void)        /* Makes parent active and de-selected OBACT */
683 {
684         short changed = 0;
685         Base *base, *startbase, *basact=NULL, *oldbasact;
686         
687         if (!(OBACT) || !(OBACT->parent)) return 0;
688         BASACT->flag &= (~SELECT);
689         BASACT->object->flag &= (~SELECT);
690         startbase=  FIRSTBASE;
691         if(BASACT && BASACT->next) startbase= BASACT->next;
692         base = startbase;
693         while(base) {
694                 if(base->object==BASACT->object->parent) { basact=base; break; }
695                 base=base->next;
696                 if(base==NULL) base= FIRSTBASE;
697                 if(base==startbase) break;
698         }
699         /* can be NULL if parent in other scene */
700         if(basact) {
701                 oldbasact = BASACT;
702                 BASACT = basact;
703                 basact->flag |= SELECT;         
704                 
705                 basact->object->flag= basact->flag;
706                 
707                 set_active_base(basact);
708                 changed = 1;
709         }
710         return changed;
711 }
712
713
714 #define GROUP_MENU_MAX  24
715 static short select_same_group(Object *ob)      /* Select objects in the same group as the active */
716 {
717         short changed = 0;
718         Base *base;
719         Group *group, *ob_groups[GROUP_MENU_MAX];
720         char str[10 + (24*GROUP_MENU_MAX)];
721         char *p = str;
722         int group_count=0, menu, i;
723
724         if (!ob)
725                 return 0;
726         
727         for (   group=G.main->group.first;
728                         group && group_count < GROUP_MENU_MAX;
729                         group=group->id.next
730                 ) {
731                 if (object_in_group (ob, group)) {
732                         ob_groups[group_count] = group;
733                         group_count++;
734                 }
735         }
736         
737         if (!group_count)
738                 return 0;
739         
740         else if (group_count == 1) {
741                 group = ob_groups[0];
742                 for (base= FIRSTBASE; base; base= base->next) {
743                         if (BASE_SELECTABLE(base) && !(base->flag & SELECT) && object_in_group(base->object, group)) {
744                                 base->flag |= SELECT;
745                                 base->object->flag |= SELECT;
746                                 changed = 1;
747                         }
748                 }
749                 return changed;
750         }
751         
752         /* build the menu. */
753         p += sprintf(str, "Groups%%t"); 
754         for (i=0; i<group_count; i++) {
755                 group = ob_groups[i];
756                 p += sprintf (p, "|%s%%x%i", group->id.name+2, i);
757         }
758         
759         menu = pupmenu (str);
760         if (menu == -1)
761                 return 0;
762         
763         group = ob_groups[menu];
764         for (base= FIRSTBASE; base; base= base->next) {
765                 if (!(base->flag & SELECT) && object_in_group(base->object, group)) {
766                         base->flag |= SELECT;
767                         base->object->flag |= SELECT;
768                         changed = 1;
769                 }
770         }
771         return changed;
772 }
773
774 static short select_object_hooks(Object *ob)
775 {
776         short changed = 0;
777         Base *base;
778         ModifierData *md;
779         HookModifierData *hmd;
780         
781         if (!ob)
782                 return 0;
783         
784         for (md = ob->modifiers.first; md; md=md->next) {
785                 if (md->type==eModifierType_Hook) {
786                         hmd= (HookModifierData*) md;
787                         if (hmd->object && !(hmd->object->flag & SELECT)) {
788                                 base= object_in_scene(hmd->object, G.scene);
789                                 if (base && BASE_SELECTABLE(base)) {
790                                         base->flag |= SELECT;
791                                         base->object->flag |= SELECT;
792                                         changed = 1;
793                                 }
794                         }
795                 }
796         }
797         return changed;
798 }
799
800 /* Select objects woth the same parent as the active (siblings),
801  * parent can be NULL also */
802 static short select_same_parent(Object *ob)     
803 {
804         short changed = 0;
805         Base *base;
806         if (!ob)
807                 return 0;
808         
809         for (base= FIRSTBASE; base; base= base->next) {
810                 if (BASE_SELECTABLE(base) && (base->object->parent==ob->parent)  && !(base->flag & SELECT)) {
811                         base->flag |= SELECT;
812                         base->object->flag |= SELECT;
813                         changed = 1;
814                 }
815         }
816         return changed;
817 }
818
819 static short select_same_type(Object *ob)
820 {
821         short changed = 0;
822         Base *base;
823         if (!ob)
824                 return 0;
825         
826         for (base= FIRSTBASE; base; base= base->next) {
827                 if (BASE_SELECTABLE(base) && (base->object->type == ob->type) && !(base->flag & SELECT)) {
828                         base->flag |= SELECT;
829                         base->object->flag |= SELECT;
830                         changed = 1;
831                 }
832         }
833         return changed;
834 }
835
836 static short select_same_layer(Object *ob)
837 {
838         char changed = 0;
839         Base *base = FIRSTBASE;
840         
841         if (!ob)
842                 return 0;
843         
844         while(base) {
845                 if (BASE_SELECTABLE(base) && (base->lay & ob->lay) && !(base->flag & SELECT)) {
846                         base->flag |= SELECT;
847                         base->object->flag |= SELECT;
848                         changed = 1;
849                 }
850                 base= base->next;
851         }
852         return changed;
853 }
854
855 void select_object_grouped(short nr)
856 {
857         short changed = 0;
858         if(nr==1)               changed = select_children(OBACT, 1);
859         else if(nr==2)  changed = select_children(OBACT, 0);
860         else if(nr==3)  changed = select_parent();
861         else if(nr==4)  changed = select_same_parent(OBACT);    
862         else if(nr==5)  changed = select_same_type(OBACT);
863         else if(nr==6)  changed = select_same_layer(OBACT);     
864         else if(nr==7)  changed = select_same_group(OBACT);
865         else if(nr==8)  changed = select_object_hooks(OBACT);
866         
867         if (changed) {
868                 countall();
869                 allqueue(REDRAWVIEW3D, 0);
870                 allqueue(REDRAWBUTSOBJECT, 0);
871                 allspace(REMAKEIPO, 0);
872                 allqueue(REDRAWIPO, 0);
873                 BIF_undo_push("Select Grouped");
874         }
875 }
876
877 static void select_object_grouped_menu(void)
878 {
879         char *str;
880         short nr;
881
882         /* make menu string */
883         
884         str= MEM_mallocN(512, "groupmenu");
885         strcpy(str, "Select Grouped%t|Children%x1|"
886                     "Immediate Children%x2|Parent%x3|"
887                     "Siblings (Shared Parent)%x4|"
888                     "Objects of Same Type%x5|"
889                                 "Objects on Shared Layers%x6|"
890                 "Objects in Same Group%x7|"
891                 "Object Hooks%x8|");
892
893         /* here we go */
894         
895         nr= pupmenu(str);
896         MEM_freeN(str);
897         
898         select_object_grouped(nr);
899 }
900
901 void join_menu(void)
902 {
903         Object *ob= OBACT;
904         if (ob && !G.obedit) {
905                 if(ob->type == OB_MESH) {
906                         if(okee("Join selected meshes")==0) return;
907                         join_mesh();
908                 } else if(ob->type == OB_CURVE) {
909                         if(okee("Join selected curves")==0) return;
910                         join_curve(OB_CURVE);
911                 } else if(ob->type == OB_SURF) {
912                         if(okee("Join selected NURBS")==0) return;
913                         join_curve(OB_SURF);
914                 } else if(ob->type == OB_ARMATURE) {
915                         /*      Make sure the user wants to continue*/
916                         if(okee("Join selected armatures")==0) return;
917                         join_armature ();
918                 }
919         }
920 }
921
922 static unsigned short convert_for_nonumpad(unsigned short event)
923 {
924         if (event>=ZEROKEY && event<=NINEKEY) {
925                 return event - ZEROKEY + PAD0;
926         } else if (event==MINUSKEY) {
927                 return PADMINUS;
928         } else if (event==EQUALKEY) {
929                 return PADPLUSKEY;
930         } else if (event==BACKSLASHKEY) {
931                 return PADSLASHKEY;
932         } else {
933                 return event;
934         }
935 }
936
937 /* *************** */
938
939 void BIF_undo_push(char *str)
940 {
941         if(G.obedit) {
942                 if (U.undosteps == 0) return;
943
944                 if(G.obedit->type==OB_MESH)
945                         undo_push_mesh(str);
946                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
947                         undo_push_curve(str);
948                 else if (G.obedit->type==OB_FONT)
949                         undo_push_font(str);
950                 else if (G.obedit->type==OB_MBALL)
951                         undo_push_mball(str);
952                 else if (G.obedit->type==OB_LATTICE)
953                         undo_push_lattice(str);
954                 else if (G.obedit->type==OB_ARMATURE)
955                         undo_push_armature(str);
956         }
957         else {
958                 if(U.uiflag & USER_GLOBALUNDO) 
959                         BKE_write_undo(str);
960         }
961 }
962
963 void BIF_undo(void)
964 {       
965         if(G.obedit) {
966                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
967                         undo_editmode_step(1);
968         }
969         else {
970                 if(G.f & G_TEXTUREPAINT)
971                         imagepaint_undo();
972                 else if(curarea->spacetype==SPACE_IMAGE && (G.sima->flag & SI_DRAWTOOL))
973                         imagepaint_undo();
974                 else {
975                         /* now also in faceselect mode */
976                         if(U.uiflag & USER_GLOBALUNDO) {
977                                 BKE_undo_step(1);
978                                 sound_initialize_sounds();
979                         }
980                 }
981         }
982 }
983
984 void BIF_redo(void)
985 {
986         if(G.obedit) {
987                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
988                         undo_editmode_step(-1);
989         }
990         else {
991                 if(G.f & G_TEXTUREPAINT)
992                         imagepaint_undo();
993                 else if(curarea->spacetype==SPACE_IMAGE && (G.sima->flag & SI_DRAWTOOL))
994                         imagepaint_undo();
995                 else {
996                         /* includes faceselect now */
997                         if(U.uiflag & USER_GLOBALUNDO) {
998                                 BKE_undo_step(-1);
999                                 sound_initialize_sounds();
1000                         }
1001                 }
1002         }
1003 }
1004
1005 void BIF_undo_menu(void)
1006 {
1007         if(G.obedit) {
1008                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
1009                         undo_editmode_menu();
1010                 allqueue(REDRAWALL, 0);
1011         }
1012         else {
1013                 if(U.uiflag & USER_GLOBALUNDO) {
1014                         char *menu= BKE_undo_menu_string();
1015                         if(menu) {
1016                                 short event= pupmenu_col(menu, 20);
1017                                 MEM_freeN(menu);
1018                                 if(event>0) BKE_undo_number(event);
1019                         }
1020                 }
1021         }
1022 }
1023
1024 /* *************** */
1025
1026 void handle_view_middlemouse() {
1027         /* use '&' here, because of alt+leftmouse which emulates middlemouse */
1028         if(U.flag & USER_VIEWMOVE) {
1029                 if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1030                         viewmove(0);
1031                 else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1032                         viewmove(2);
1033                 else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1034                         viewmove(1);
1035         }
1036         else {
1037                 if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1038                         viewmove(1);
1039                 else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1040                         viewmove(2);
1041                 else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1042                         viewmove(0);
1043         }
1044 }
1045
1046 void handle_view_wheelup()
1047 {
1048         /* Regular:   Zoom in */
1049         /* Shift:     Scroll up */
1050         /* Ctrl:      Scroll right */
1051         /* Alt-Shift: Rotate up */
1052         /* Alt-Ctrl:  Rotate right */
1053
1054         if( G.qual & LR_SHIFTKEY ) {
1055                 if( G.qual & LR_ALTKEY ) { 
1056                         G.qual &= ~LR_SHIFTKEY;
1057                         persptoetsen(PAD2);
1058                         G.qual |= LR_SHIFTKEY;
1059                 } else {
1060                         persptoetsen(PAD2);
1061                 }
1062         } else if( G.qual & LR_CTRLKEY ) {
1063                 if( G.qual & LR_ALTKEY ) { 
1064                         G.qual &= ~LR_CTRLKEY;
1065                         persptoetsen(PAD4);
1066                         G.qual |= LR_CTRLKEY;
1067                 } else {
1068                         persptoetsen(PAD4);
1069                 }
1070         } else if(U.uiflag & USER_WHEELZOOMDIR) 
1071                 persptoetsen(PADMINUS);
1072         else
1073                 persptoetsen(PADPLUSKEY);
1074 }
1075
1076 void handle_view_wheeldown()
1077 {
1078         /* Regular:   Zoom out */
1079         /* Shift:     Scroll down */
1080         /* Ctrl:      Scroll left */
1081         /* Alt-Shift: Rotate down */
1082         /* Alt-Ctrl:  Rotate left */
1083
1084         if( G.qual & LR_SHIFTKEY ) {
1085                 if( G.qual & LR_ALTKEY ) { 
1086                         G.qual &= ~LR_SHIFTKEY;
1087                         persptoetsen(PAD8);
1088                         G.qual |= LR_SHIFTKEY;
1089                 } else {
1090                         persptoetsen(PAD8);
1091                 }
1092         } else if( G.qual & LR_CTRLKEY ) {
1093                 if( G.qual & LR_ALTKEY ) { 
1094                         G.qual &= ~LR_CTRLKEY;
1095                         persptoetsen(PAD6);
1096                         G.qual |= LR_CTRLKEY;
1097                 } else {
1098                         persptoetsen(PAD6);
1099                 }
1100         } else if(U.uiflag & USER_WHEELZOOMDIR) 
1101                 persptoetsen(PADPLUSKEY);
1102         else
1103                 persptoetsen(PADMINUS);
1104 }
1105
1106 int mouse_in_header(ScrArea *sa)
1107 {
1108         short mouse[2];
1109         getmouseco_sc(mouse);
1110         return mouse[0] >= sa->headrct.xmin &&
1111                mouse[0] <= sa->headrct.xmax &&
1112                mouse[1] >= sa->headrct.ymin &&
1113                mouse[1] <= sa->headrct.ymax;
1114 }
1115
1116 static void winqreadview3dspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1117 {
1118         View3D *v3d= sa->spacedata.first;
1119         Object *ob= OBACT;      /* do not change! */
1120         float *curs;
1121         int doredraw= 0, pupval;
1122         unsigned short event= evt->event, origevent= evt->event;
1123         short val= evt->val;
1124         char ascii= evt->ascii;
1125         
1126         if(curarea->win==0) return;     /* when it comes from sa->headqread() */
1127         
1128         if(val) {
1129
1130                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1131                 
1132                 if(event==UI_BUT_EVENT) do_butspace(val); /* temporal, view3d deserves own queue? */
1133                 
1134                 /* we consider manipulator a button, defaulting to leftmouse */
1135                 if(event==LEFTMOUSE) if(BIF_do_manipulator(sa)) return;
1136                 
1137                 /* swap mouse buttons based on user preference */
1138                 if (U.flag & USER_LMOUSESELECT) {
1139                         if (event==LEFTMOUSE) event = RIGHTMOUSE;
1140                         else if (event==RIGHTMOUSE) event = LEFTMOUSE;
1141                 }
1142
1143                 if(!mouse_in_header(sa)) {
1144                         if(!G.obedit && (G.f & G_SCULPTMODE)) {
1145                                 SculptSession *ss= sculpt_session();
1146                                 if(ss && ss->propset) {
1147                                         sculptmode_propset(event);
1148                                         return;
1149                                 }
1150                                 else if(event!=LEFTMOUSE && event!=MIDDLEMOUSE && (event==MOUSEY || event==MOUSEX)) {
1151                                         if(!bwin_qtest(sa->win))
1152                                                 allqueue(REDRAWVIEW3D, 0);
1153                                 }
1154                         }
1155
1156                         /* Handle retopo painting */
1157                         if(retopo_mesh_paint_check()) {
1158                                 if(!retopo_paint(event))
1159                                         return;
1160                         }
1161                 }
1162
1163                 /* run any view3d event handler script links */
1164                 if (event && sa->scriptlink.totscript)
1165                         if (BPY_do_spacehandlers(sa, event, SPACEHANDLER_VIEW3D_EVENT))
1166                                 return; /* return if event was processed (swallowed) by handler(s) */
1167
1168                 /* TEXTEDITING?? */
1169                 if((G.obedit) && G.obedit->type==OB_FONT) {
1170                         switch(event) {
1171                         
1172                         case LEFTMOUSE:
1173                                 mouse_cursor();
1174                                 break;
1175                         case MIDDLEMOUSE:
1176                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
1177                                 if(U.flag & USER_VIEWMOVE) {
1178                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1179                                                 viewmove(0);
1180                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1181                                                 viewmove(2);
1182                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1183                                                 viewmove(1);
1184                                 }
1185                                 else {
1186                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1187                                                 viewmove(1);
1188                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1189                                                 viewmove(2);
1190                                         else
1191                                                 viewmove(0);
1192                                 }
1193                                 break;
1194                                 
1195                         case WHEELUPMOUSE:
1196                                 /* Regular:   Zoom in */
1197                                 /* Shift:     Scroll up */
1198                                 /* Ctrl:      Scroll right */
1199                                 /* Alt-Shift: Rotate up */
1200                                 /* Alt-Ctrl:  Rotate right */
1201
1202                                 if( G.qual & LR_SHIFTKEY ) {
1203                                         if( G.qual & LR_ALTKEY ) { 
1204                                                 G.qual &= ~LR_SHIFTKEY;
1205                                                 persptoetsen(PAD2);
1206                                                 G.qual |= LR_SHIFTKEY;
1207                                         } else {
1208                                                 persptoetsen(PAD2);
1209                                         }
1210                                 } else if( G.qual & LR_CTRLKEY ) {
1211                                         if( G.qual & LR_ALTKEY ) { 
1212                                                 G.qual &= ~LR_CTRLKEY;
1213                                                 persptoetsen(PAD4);
1214                                                 G.qual |= LR_CTRLKEY;
1215                                         } else {
1216                                                 persptoetsen(PAD4);
1217                                         }
1218                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1219                                         persptoetsen(PADMINUS);
1220                                 else
1221                                         persptoetsen(PADPLUSKEY);
1222
1223                                 doredraw= 1;
1224                                 break;
1225
1226                         case WHEELDOWNMOUSE:
1227                                 /* Regular:   Zoom out */
1228                                 /* Shift:     Scroll down */
1229                                 /* Ctrl:      Scroll left */
1230                                 /* Alt-Shift: Rotate down */
1231                                 /* Alt-Ctrl:  Rotate left */
1232
1233                                 if( G.qual & LR_SHIFTKEY ) {
1234                                         if( G.qual & LR_ALTKEY ) { 
1235                                                 G.qual &= ~LR_SHIFTKEY;
1236                                                 persptoetsen(PAD8);
1237                                                 G.qual |= LR_SHIFTKEY;
1238                                         } else {
1239                                                 persptoetsen(PAD8);
1240                                         }
1241                                 } else if( G.qual & LR_CTRLKEY ) {
1242                                         if( G.qual & LR_ALTKEY ) { 
1243                                                 G.qual &= ~LR_CTRLKEY;
1244                                                 persptoetsen(PAD6);
1245                                                 G.qual |= LR_CTRLKEY;
1246                                         } else {
1247                                                 persptoetsen(PAD6);
1248                                         }
1249                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1250                                         persptoetsen(PADPLUSKEY);
1251                                 else
1252                                         persptoetsen(PADMINUS);
1253                                 
1254                                 doredraw= 1;
1255                                 break;
1256
1257                         case UKEY:
1258                                 if(G.qual==LR_ALTKEY) {
1259                                         remake_editText();
1260                                         doredraw= 1;
1261                                 } 
1262                                 else {
1263                                         do_textedit(event, val, ascii);
1264                                 }
1265                                 break;
1266                         case VKEY:
1267                                 if(G.qual==LR_ALTKEY) {
1268                                         paste_editText();
1269                                         doredraw= 1;
1270                                 } 
1271                                 else {
1272                                         do_textedit(event, val, ascii);
1273                                 }
1274                                 break;
1275                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1276                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1277                         case PADENTER:
1278                                 persptoetsen(event);
1279                                 doredraw= 1;
1280                                 break;
1281                                 
1282                         default:
1283                                 do_textedit(event, val, ascii);
1284                                 break;
1285                         }
1286                 }
1287                 else if(!G.obedit && (G.f & G_SCULPTMODE)) {
1288                         SculptData *sd= sculpt_data();
1289                         SculptSession *ss= sculpt_session();
1290                         BrushData *br= sculptmode_brush();
1291                         Mesh *me= get_mesh(OBACT);
1292                         char update_prop= 0;
1293                         
1294                         if(U.flag & USER_NONUMPAD) {
1295                                 event= convert_for_nonumpad(event);
1296                         }
1297                         
1298                         switch(event) {
1299                         case LEFTMOUSE:
1300                                 if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1301                                         sculptmode_pmv(0);
1302                                 else if(!(ss && ss->propset))
1303                                         sculpt();
1304                                 break;
1305                         /* View */
1306                         case MIDDLEMOUSE:
1307                                 handle_view_middlemouse();
1308                                 break;
1309                         case WHEELUPMOUSE:
1310                                 handle_view_wheelup();
1311                                 doredraw= 1;
1312                                 break;
1313                         case WHEELDOWNMOUSE:
1314                                 handle_view_wheeldown();
1315                                 doredraw= 1;
1316                                 break;
1317                         case RIGHTMOUSE:
1318                                 if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1319                                         sculptmode_pmv(1);
1320                                 break;
1321                         case CKEY:
1322                                 if(G.qual==LR_SHIFTKEY) {
1323                                         view3d_home(1);
1324                                         curs= give_cursor();
1325                                         curs[0]=curs[1]=curs[2]= 0.0;
1326                                         allqueue(REDRAWVIEW3D, 0);
1327                                 }
1328                                 else if(G.qual==0) {
1329                                         if (G.vd->persp==2)
1330                                                 /* center the camera offset */
1331                                                 G.vd->camdx= G.vd->camdy= 0.0;
1332                                         else {
1333                                                 /*non camera center*/
1334                                                 curs= give_cursor();
1335                                                 G.vd->ofs[0]= -curs[0];
1336                                                 G.vd->ofs[1]= -curs[1];
1337                                                 G.vd->ofs[2]= -curs[2];
1338                                         }
1339                                 }
1340                                 doredraw= 1;
1341                                 break;
1342                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1343                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1344                         case PADPLUSKEY:
1345                         case PADMINUS:
1346                         case PADENTER:
1347                                 persptoetsen(event);
1348                                 doredraw= 1;
1349                                 break;
1350                         case BKEY:
1351                                 if(G.qual==LR_ALTKEY)
1352                                         view3d_edit_clipping(v3d);
1353                                 else if(G.qual==LR_SHIFTKEY) {
1354                                         if(G.vd->persp==2)
1355                                                 set_render_border();
1356                                         else
1357                                                 view3d_border_zoom();
1358                                 }
1359                                 break;
1360                         case PADSLASHKEY:
1361                                 if(!G.qual) {
1362                                         if(G.vd->localview) {
1363                                                 G.vd->localview= 0;
1364                                                 endlocalview(curarea);
1365                                         }
1366                                         else {
1367                                                 G.vd->localview= 1;
1368                                                 initlocalview();
1369                                         }
1370                                         allqueue(REDRAWVIEW3D, 1);
1371                                 }
1372                                 break;
1373                         /* Brush properties */
1374                         case AKEY:
1375                                 br->airbrush= !br->airbrush;
1376                                 update_prop= 1; break;
1377                         case FKEY:
1378                                 if(G.qual==0)
1379                                         sculptmode_propset_init(PropsetSize);
1380                                 if(G.qual==LR_SHIFTKEY)
1381                                         sculptmode_propset_init(PropsetStrength);
1382                                 if(G.qual==LR_CTRLKEY)
1383                                         sculptmode_propset_init(PropsetTexRot);
1384                                 break;
1385                         case VKEY:
1386                                 br->dir= br->dir==1 ? 2 : 1;
1387                                 update_prop= 1; break;
1388                         /* Set brush */
1389                         case DKEY:
1390                                 sd->brush_type= DRAW_BRUSH;
1391                                 update_prop= 1; break;
1392                         case SKEY:
1393                                 sd->brush_type= SMOOTH_BRUSH;
1394                                 update_prop= 1; break;
1395                         case PKEY:
1396                                 sd->brush_type= PINCH_BRUSH;
1397                                 update_prop= 1; break;
1398                         case IKEY:
1399                                 sd->brush_type= INFLATE_BRUSH;
1400                                 update_prop= 1; break;
1401                         case GKEY:
1402                                 sd->brush_type= GRAB_BRUSH;
1403                                 update_prop= 1; break;
1404                         case LKEY:
1405                                 sd->brush_type= LAYER_BRUSH;
1406                                 update_prop= 1; break;
1407                         /* Symmetry */
1408                         case XKEY:
1409                                 sd->symm^= SYMM_X;
1410                                 update_prop= 1; break;
1411                         case YKEY:
1412                                 sd->symm^= SYMM_Y;
1413                                 update_prop= 1; break;
1414                         case ZKEY:
1415                                 sd->symm^= SYMM_Z;
1416                                 update_prop= 1; break;
1417                         /* Interface */
1418                         case NKEY:
1419                                 if(G.qual==0) {
1420                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1421                                         allqueue(REDRAWVIEW3D, 0);
1422                                 }
1423                                 break;
1424                         /* Multires */
1425                         case PAGEUPKEY:
1426                                 if(me && me->mr) {
1427                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current+1;
1428                                         multires_set_level(ob, ob->data, 0);
1429                                 }
1430                                 break;
1431                         case PAGEDOWNKEY:
1432                                 if(me && me->mr) {
1433                                         me->mr->newlvl= ((Mesh*)ob->data)->mr->current-1;
1434                                         multires_set_level(ob, ob->data, 0);
1435                                 }
1436                                 break;
1437                         /* Partial Visibility */
1438                         case HKEY:
1439                                 if(G.qual==LR_ALTKEY) {
1440                                         waitcursor(1);
1441                                         sculptmode_pmv_off(get_mesh(ob));
1442                                         BIF_undo_push("Partial mesh hide");
1443                                         allqueue(REDRAWVIEW3D,0);
1444                                         waitcursor(0);
1445                                 }
1446                                 break;
1447                         /* Layers */
1448                         case ONEKEY:
1449                                 do_layer_buttons(0); break;
1450                         case TWOKEY:
1451                                 do_layer_buttons(1); break;
1452                         case THREEKEY:
1453                                 do_layer_buttons(2); break;
1454                         case FOURKEY:
1455                                 do_layer_buttons(3); break;
1456                         case FIVEKEY:
1457                                 do_layer_buttons(4); break;
1458                         case SIXKEY:
1459                                 do_layer_buttons(5); break;
1460                         case SEVENKEY:
1461                                 do_layer_buttons(6); break;
1462                         case EIGHTKEY:
1463                                 do_layer_buttons(7); break;
1464                         case NINEKEY:
1465                                 do_layer_buttons(8); break;
1466                         case ZEROKEY:
1467                                 do_layer_buttons(9); break;
1468                         case MINUSKEY:
1469                                 do_layer_buttons(10); break;
1470                         case EQUALKEY:
1471                                 do_layer_buttons(11); break;
1472                         case ACCENTGRAVEKEY:
1473                                 do_layer_buttons(-1); break;
1474                         }
1475                         
1476                         /* Redraw buttons window as well as view 3d (for floating panel) */
1477                         if(update_prop) {
1478                                 allqueue(REDRAWVIEW3D, 0);
1479                                 allqueue(REDRAWBUTSEDIT, 0);
1480                         }
1481                 } else {
1482
1483                         if (U.flag & USER_NONUMPAD) {
1484                                 event= convert_for_nonumpad(event);
1485                         }
1486
1487                         switch(event) {
1488                         
1489                         /* Afterqueue events */
1490                         case BACKBUFDRAW:
1491                                 backdrawview3d(1);
1492                                 break;
1493                         case RENDERPREVIEW:
1494                                 BIF_view3d_previewrender(sa);
1495                                 break;
1496                                 
1497                         case TABKEY:
1498                                 /* Shift-Tabe handling (other cases are in toets) */
1499                                 if (G.qual == LR_SHIFTKEY)
1500                                 {
1501                                         /* Snap toggle (only edit mesh right now) */
1502                                         if (G.obedit && G.obedit->type==OB_MESH)
1503                                         {
1504                                                 G.vd->flag2 ^= V3D_TRANSFORM_SNAP;
1505                                                 allqueue(REDRAWHEADERS, 0);
1506                                         }                                       
1507                                 }
1508                                 break;
1509                                 
1510                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1511                          * based on user preference USER_LMOUSESELECT
1512                          */
1513                         case LEFTMOUSE: 
1514                                 if ((G.obedit) || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT))) {
1515                                         mouse_cursor();
1516                                 }
1517                                 else if (G.f & G_WEIGHTPAINT) {
1518                                         weight_paint();
1519                                 }
1520                                 else if (G.f & G_VERTEXPAINT) {
1521                                         vertex_paint();
1522                                 }
1523                                 else if (G.f & G_TEXTUREPAINT) {
1524                                         imagepaint_paint(origevent==LEFTMOUSE? L_MOUSE: R_MOUSE, 1);
1525                                 }
1526                                 break;
1527                         case MIDDLEMOUSE:
1528                                 handle_view_middlemouse();
1529                                 break;
1530                         case RIGHTMOUSE:
1531                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
1532                                         if(G.obedit->type==OB_MESH)
1533                                                 mouse_mesh();
1534                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1535                                                 mouse_nurb();
1536                                         else if(G.obedit->type==OB_MBALL)
1537                                                 mouse_mball();
1538                                         else if(G.obedit->type==OB_LATTICE)
1539                                                 mouse_lattice();
1540                                         else if(G.obedit->type==OB_ARMATURE)
1541                                                 mouse_armature();
1542                                 }
1543                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
1544                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1545                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
1546                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1547                                 else if(G.qual==LR_CTRLKEY)
1548                                         mouse_select(); /* also allow in editmode, for vertex parenting */
1549                                 else if(G.f & G_FACESELECT)
1550                                         face_select();
1551                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
1552                                         sample_vpaint();
1553                                 else
1554                                         mouse_select(); /* does poses too */
1555                                 break;
1556                         case WHEELUPMOUSE:
1557                                 handle_view_wheelup();
1558                                 doredraw= 1;
1559                                 break;
1560                         case WHEELDOWNMOUSE:
1561                                 handle_view_wheeldown();
1562                                 doredraw= 1;
1563                                 break;
1564
1565                         case ONEKEY:
1566                                 if(G.qual==LR_CTRLKEY) {
1567                                         flip_subdivison(1);
1568                                 }
1569                                 else do_layer_buttons(0); 
1570                                 break;
1571                                 
1572                         case TWOKEY:
1573                                 if(G.qual==LR_CTRLKEY) {
1574                                         flip_subdivison(2);
1575                                 }
1576                                 else do_layer_buttons(1); 
1577                                 break;
1578                                 
1579                         case THREEKEY:
1580                                 if(G.qual==LR_CTRLKEY) {
1581                                         flip_subdivison(3);
1582                                 }
1583                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1584                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1585                                                 select_faces_by_numverts(3);
1586                                 }
1587                                 else do_layer_buttons(2); 
1588                                 break;
1589                                 
1590                         case FOURKEY:
1591                                 if(G.qual==LR_CTRLKEY) {
1592                                         flip_subdivison(4);
1593                                 }
1594                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1595                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1596                                                 select_faces_by_numverts(4);
1597                                 }
1598                                 else do_layer_buttons(3); 
1599                                 break;
1600                                 
1601                         case FIVEKEY:
1602                                 if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1603                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1604                                                 select_faces_by_numverts(5);
1605                                 }
1606                                 else do_layer_buttons(4);
1607                                 break;
1608
1609                         case SIXKEY:
1610                                 do_layer_buttons(5); break;
1611                         case SEVENKEY:
1612                                 do_layer_buttons(6); break;
1613                         case EIGHTKEY:
1614                                 do_layer_buttons(7); break;
1615                         case NINEKEY:
1616                                 do_layer_buttons(8); break;
1617                         case ZEROKEY:
1618                                 do_layer_buttons(9); break;
1619                         case MINUSKEY:
1620                                 do_layer_buttons(10); break;
1621                         case EQUALKEY:
1622                                 do_layer_buttons(11); break;
1623                         case ACCENTGRAVEKEY:
1624                                 do_layer_buttons(-1); break;
1625                         
1626                         case SPACEKEY:
1627                                 if(G.qual == LR_CTRLKEY) {
1628                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1629                                         if(val>0) {
1630                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1631                                                 else {
1632                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1633                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1634                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1635                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1636                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1637                                                 }
1638                                                 doredraw= 1;
1639                                         }
1640                                 }
1641                                 else if(G.qual == LR_ALTKEY) {
1642                                         BIF_selectOrientation();
1643                                         doredraw= 1;
1644                                 }
1645
1646                                 break;
1647                                 
1648                         case AKEY:
1649                                 if(G.qual & LR_CTRLKEY) apply_object(); /* also with shift! */
1650                                 else if((G.qual==LR_SHIFTKEY)) {
1651                                         toolbox_n_add();
1652                                 }
1653                                 else {
1654                                         if(G.obedit) {
1655                                                 if(G.obedit->type==OB_MESH)
1656                                                         deselectall_mesh();
1657                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1658                                                         deselectall_nurb();
1659                                                 else if(G.obedit->type==OB_MBALL)
1660                                                         deselectall_mball();
1661                                                 else if(G.obedit->type==OB_LATTICE)
1662                                                         deselectall_Latt();
1663                                                 else if(G.obedit->type==OB_ARMATURE)
1664                                                         deselectall_armature(1, 1);     /* 1 == toggle */
1665                                         }
1666                                         else if (ob && (ob->flag & OB_POSEMODE)){
1667                                                 deselectall_posearmature(ob, 1, 1);
1668                                         }
1669                                         else {
1670                                                 if(G.f & G_FACESELECT) deselectall_tface();
1671                                                 else {
1672                                                         /* by design, the center of the active object 
1673                                                          * (which need not necessarily by selected) will
1674                                                          * still be drawn as if it were selected.
1675                                                          */
1676                                                         deselectall();
1677                                                 }
1678                                         }
1679                                 }
1680                                 break;
1681                         case BKEY:
1682                                 if(G.qual==LR_ALTKEY)
1683                                         view3d_edit_clipping(v3d);
1684                                 else if(G.qual==LR_SHIFTKEY)
1685                                 {
1686                                         if(G.vd->persp==2)
1687                                                 set_render_border();
1688                                         else
1689                                                 view3d_border_zoom();
1690                                 }
1691                                 else if(G.qual==LR_CTRLKEY) {
1692                                         if(okee("Bake all selected")) {
1693                                                 extern void softbody_bake(Object *ob);
1694                                                 extern void fluidsimBake(Object *ob);
1695                                                 softbody_bake(NULL);
1696                                                 /* also bake first domain of selected objects... */
1697                                                 fluidsimBake(NULL);
1698                                         }
1699                                 }
1700                                 else if(G.qual== (LR_ALTKEY|LR_CTRLKEY))
1701                                         objects_bake_render_menu();
1702                                 else if(G.qual==0)
1703                                         borderselect();
1704                                 break;
1705                         case CKEY:
1706                                 if(G.qual==LR_CTRLKEY) {
1707                                         if(ob && (ob->flag & OB_POSEMODE))
1708                                                 pose_copy_menu();       /* poseobject.c */
1709                                         else
1710                                                 copy_attr_menu();
1711                                 }
1712                                 else if(G.qual==LR_ALTKEY) {
1713                                         if(ob && (ob->flag & OB_POSEMODE))
1714                                                 pose_clear_constraints();       /* poseobject.c */
1715                                         else
1716                                                 convertmenu();  /* editobject.c */
1717                                 }
1718                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY)) 
1719                                         add_constraint(0);      /* editconstraint.c, generic for objects and posemode */
1720                                 else if((G.qual==LR_SHIFTKEY)) {
1721                                         view3d_home(1);
1722                                         curs= give_cursor();
1723                                         curs[0]=curs[1]=curs[2]= 0.0;
1724                                         allqueue(REDRAWVIEW3D, 0);
1725                                 }
1726                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1727                                         makecyclicNurb();
1728                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1729                                         allqueue(REDRAWVIEW3D, 0);
1730                                 }
1731                                 else if((G.qual==0)){
1732                                         if (G.vd->persp==2)
1733                                                 /* center the camera offset */
1734                                                 G.vd->camdx= G.vd->camdy= 0.0;
1735                                         else {
1736                                                 /*non camera center*/
1737                                                 curs= give_cursor();
1738                                                 G.vd->ofs[0]= -curs[0];
1739                                                 G.vd->ofs[1]= -curs[1];
1740                                                 G.vd->ofs[2]= -curs[2];
1741                                         }
1742                                         scrarea_queue_winredraw(curarea);
1743                                 }
1744                                 break;
1745                         case DKEY:
1746                                 if((G.qual==LR_SHIFTKEY)) {
1747                                         duplicate_context_selected();
1748                                 }
1749                                 else if(G.qual==LR_ALTKEY) {
1750                                         if(ob && (ob->flag & OB_POSEMODE))
1751                                                 error ("Duplicate not possible in posemode.");
1752                                         else if((G.obedit==NULL))
1753                                                 adduplicate(0, 0);
1754                                 }
1755                                 else if(G.qual==LR_CTRLKEY) {
1756                                         imagestodisplist();
1757                                 }
1758                                 else if((G.qual==0)){
1759                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
1760                                         if(pupval>0) {
1761                                                 G.vd->drawtype= pupval;
1762                                                 doredraw= 1;
1763                                         }
1764                                 }
1765                                 
1766                                 break;
1767                         case EKEY:
1768                                 if (G.qual==0){
1769                                         if(G.obedit) {
1770                                                 if(G.obedit->type==OB_MESH)
1771                                                         extrude_mesh();
1772                                                 else if(G.obedit->type==OB_CURVE)
1773                                                         addvert_Nurb('e');
1774                                                 else if(G.obedit->type==OB_SURF)
1775                                                         extrude_nurb();
1776                                                 else if(G.obedit->type==OB_ARMATURE)
1777                                                         extrude_armature(0);
1778                                         }
1779                                 }
1780                                 else if (G.qual==LR_CTRLKEY) {
1781                                         if(G.obedit && G.obedit->type==OB_MESH)
1782                                                 Edge_Menu();
1783                                         else if (G.f & G_FACESELECT)
1784                                                 seam_mark_clear_tface(0);
1785                                 }
1786                                 else if (G.qual==LR_SHIFTKEY) {
1787                                         if (G.obedit && G.obedit->type==OB_MESH &&
1788                                             !multires_level1_test()) {
1789                                                 initTransform(TFM_CREASE, CTX_EDGE);
1790                                                 Transform();
1791                                         }
1792                                         else if (G.obedit && G.obedit->type==OB_ARMATURE) {
1793                                                 extrude_armature(1);
1794                                         }
1795                                 }
1796                                 break;
1797                         case FKEY:
1798                                 if(G.obedit) {
1799                                         if(G.obedit->type==OB_MESH) {
1800                                                 if((G.qual==LR_SHIFTKEY))
1801                                                         fill_mesh();
1802                                                 else if(G.qual==LR_ALTKEY)
1803                                                         beauty_fill();
1804                                                 else if(G.qual==LR_CTRLKEY)
1805                                                         edge_flip();
1806                                                 else if (G.qual==0)
1807                                                         addedgeface_mesh();
1808                                                 else if ( G.qual == 
1809                                                          (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1810                                                         select_linked_flat_faces();
1811                                                 }
1812
1813                                         }
1814                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
1815                                 }
1816                                 else if(G.qual==LR_CTRLKEY)
1817                                         sort_faces();
1818                                 else if((G.qual==LR_SHIFTKEY)) {
1819                                         if(ob && (ob->flag & OB_POSEMODE))
1820                                            pose_activate_flipped_bone();
1821                                         else if(G.f & G_WEIGHTPAINT)
1822                                                 pose_activate_flipped_bone();
1823                                         else
1824                                                 fly();
1825                                 }
1826                                 else {
1827                                         set_faceselect();
1828                                 }
1829                                 
1830                                 break;
1831                         case GKEY:
1832                                 if(G.qual == LR_CTRLKEY) 
1833                                         group_operation_with_menu();
1834                                 else if((G.qual==LR_SHIFTKEY))
1835                                         if(G.obedit) {
1836                                                 if(G.obedit->type==OB_MESH)
1837                                                         select_mesh_group_menu();
1838                                         } else
1839                                                 select_object_grouped_menu();
1840                                 else if((G.obedit==0) && G.qual==LR_ALTKEY) {
1841                                         if(okee("Clear location")) {
1842                                                 clear_object('g');
1843                                         }
1844                                 }
1845                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1846                                         v3d->twtype= V3D_MANIP_TRANSLATE;
1847                                         doredraw= 1;
1848                                 }
1849                                 else if((G.qual==0)) {
1850                                         initTransform(TFM_TRANSLATION, CTX_NONE);
1851                                         Transform();
1852                                 }
1853                                 break;
1854                         case HKEY:
1855                                 if(G.obedit) {
1856                                         if(G.obedit->type==OB_MESH) {
1857                                                 if(G.qual==LR_CTRLKEY)
1858                                                         add_hook();
1859                                                 else if(G.qual==LR_ALTKEY)
1860                                                         reveal_mesh();
1861                                                 else if((G.qual==LR_SHIFTKEY))
1862                                                         hide_mesh(1);
1863                                                 else if((G.qual==0)) 
1864                                                         hide_mesh(0);
1865                                         }
1866                                         else if(G.obedit->type== OB_SURF) {
1867                                                 if(G.qual==LR_CTRLKEY)
1868                                                         add_hook();
1869                                                 else if(G.qual==LR_ALTKEY)
1870                                                         revealNurb();
1871                                                 else if((G.qual==LR_SHIFTKEY))
1872                                                         hideNurb(1);
1873                                                 else if((G.qual==0))
1874                                                         hideNurb(0);
1875                                         }
1876                                         else if(G.obedit->type==OB_CURVE) {
1877                                                 if(G.qual==LR_CTRLKEY)
1878                                                         add_hook();
1879                                                 else {
1880                                                         if(G.qual==LR_CTRLKEY)
1881                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
1882                                                         else if((G.qual==LR_SHIFTKEY))
1883                                                                 sethandlesNurb(1);
1884                                                         else if((G.qual==0))
1885                                                                 sethandlesNurb(3);
1886                                                         
1887                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1888                                                         BIF_undo_push("Handle change");
1889                                                         allqueue(REDRAWVIEW3D, 0);
1890                                                 }
1891                                         }
1892                                         else if(G.obedit->type==OB_LATTICE) {
1893                                                 if(G.qual==LR_CTRLKEY) add_hook();
1894                                         }
1895                                         else if(G.obedit->type==OB_MBALL) {
1896                                                 if(G.qual==LR_ALTKEY)
1897                                                         reveal_mball();
1898                                                 else if((G.qual==LR_SHIFTKEY))
1899                                                         hide_mball(1);
1900                                                 else if((G.qual==0)) 
1901                                                         hide_mball(0);
1902                                         }
1903                                         else if(G.obedit->type==OB_ARMATURE) {
1904                                                 if (G.qual==0)
1905                                                         hide_selected_armature_bones();
1906                                                 else if (G.qual==LR_SHIFTKEY)
1907                                                         hide_unselected_armature_bones();
1908                                                 else if (G.qual==LR_ALTKEY)
1909                                                         show_all_armature_bones();
1910                                         }
1911                                 }
1912                                 else if(G.f & G_FACESELECT)
1913                                         hide_tface();
1914                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1915                                         if (G.qual==0)
1916                                                 hide_selected_pose_bones();
1917                                         else if (G.qual==LR_SHIFTKEY)
1918                                                 hide_unselected_pose_bones();
1919                                         else if (G.qual==LR_ALTKEY)
1920                                                 show_all_pose_bones();
1921                                 }
1922                                 break;
1923                         case IKEY:
1924                                 if(G.obedit);
1925                                 else if(G.qual==LR_CTRLKEY) {
1926                                         if(ob && ob->type==OB_ARMATURE) 
1927                                                 if(ob->flag & OB_POSEMODE) 
1928                                                         pose_add_IK();
1929                                 }
1930                                 else if(G.qual==LR_ALTKEY) {
1931                                         if(ob && ob->type==OB_ARMATURE) 
1932                                                 if(ob->flag & OB_POSEMODE) 
1933                                                         pose_clear_IK();
1934                                 }
1935                                 break;
1936                                 
1937                         case JKEY:
1938                                 if(G.qual==LR_CTRLKEY) {
1939                                         if( ob ) {
1940                                                 join_menu();
1941                                         }
1942                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1943                                                 addsegment_nurb();
1944                                 }
1945                                 else if(G.obedit) {
1946                                         if(G.obedit->type==OB_MESH) {
1947                                                 join_triangles();
1948                                         }
1949                                 }
1950
1951                                 break;
1952                         case KKEY:
1953                                 if(G.obedit) {
1954                                         if (G.obedit->type==OB_MESH) {
1955                                                 if (G.qual==LR_SHIFTKEY)
1956                                                         KnifeSubdivide(KNIFE_PROMPT);
1957                                                 else if (G.qual==0)
1958                                                         LoopMenu();
1959                                         }
1960                                         else if(G.obedit->type==OB_SURF)
1961                                                 printknots();
1962                                 }
1963                                 else {
1964                                         if((G.qual==LR_SHIFTKEY)) {
1965                                                 if(G.f & G_FACESELECT)
1966                                                         if (G.f & G_WEIGHTPAINT)
1967                                                                 clear_wpaint_selectedfaces();
1968                                                         else
1969                                                                 clear_vpaint_selectedfaces();
1970                                                 else if(G.f & G_VERTEXPAINT)
1971                                                         clear_vpaint();
1972                                                 else
1973                                                         select_select_keys();
1974                                         }
1975                                         else if (G.qual==0)
1976                                                 set_ob_ipoflags();
1977                                 }
1978                                 
1979                                 break;
1980                         case LKEY:
1981                                 if(G.obedit) {
1982                                         if(G.obedit->type==OB_MESH)
1983                                                 selectconnected_mesh(G.qual);
1984                                         if(G.obedit->type==OB_ARMATURE)
1985                                                 selectconnected_armature();
1986                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1987                                                 selectconnected_nurb();
1988                                 }
1989                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1990                                         selectconnected_posearmature();
1991                                 }
1992                                 else {
1993                                         if(G.f & G_FACESELECT) {
1994                                                 if((G.qual==0))
1995                                                         select_linked_tfaces(0);
1996                                                 else if((G.qual==LR_SHIFTKEY))
1997                                                         select_linked_tfaces(1);
1998                                                 else if(G.qual==LR_CTRLKEY)
1999                                                         select_linked_tfaces(2);
2000                                         }
2001                                         else {
2002                                                 if((G.qual==0))
2003                                                         make_local_menu();
2004                                                 else if((G.qual==LR_SHIFTKEY))
2005                                                         selectlinks_menu();
2006                                                 else if(G.qual==LR_CTRLKEY)
2007                                                         make_links_menu();
2008                                         }
2009                                 }
2010                                 break;
2011                         case MKEY:
2012                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
2013                                         mirror_uv_tface();
2014                                 else if(G.obedit){
2015                                         if(G.qual==LR_ALTKEY) {
2016                                                 if(G.obedit->type==OB_MESH) {
2017                                                         mergemenu();
2018                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2019                                                 }
2020                                         }
2021                                         else if((G.qual==0) || (G.qual==LR_CTRLKEY)) {
2022                                                 mirrormenu();
2023                                         }
2024                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2025                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
2026                                         }
2027                                 }
2028                                 else if(G.qual & LR_CTRLKEY) {
2029                                         mirrormenu();
2030                                 }
2031                                 else if(G.qual==0 || G.qual==LR_SHIFTKEY) {
2032                                         if(ob && (ob->flag & OB_POSEMODE))
2033                                                 pose_movetolayer();
2034                                         else
2035                                                 movetolayer();
2036                                 }
2037                                 break;
2038                         case NKEY:
2039                                 if((G.qual==0)) {
2040                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
2041                                         allqueue(REDRAWVIEW3D, 0);
2042                                 }
2043                                 else if(G.obedit) {
2044                                         switch (G.obedit->type){
2045                                         case OB_ARMATURE:
2046                                                 if(G.qual==LR_CTRLKEY){
2047                                                         if (okee("Recalculate bone roll angles")) {
2048                                                                 auto_align_armature();
2049                                                                 allqueue(REDRAWVIEW3D, 0);
2050                                                         }
2051                                                 }
2052                                                 break;
2053                                         case OB_MESH: 
2054                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2055                                                         if(okee("Recalculate normals inside")) {
2056                                                                 righthandfaces(2);
2057                                                                 allqueue(REDRAWVIEW3D, 0);
2058                                                                 BIF_undo_push("Recalculate normals inside");
2059                                                         }
2060                                                 }
2061                                                 else if(G.qual==LR_CTRLKEY){
2062                                                         if(okee("Recalculate normals outside")) {
2063                                                                 righthandfaces(1);
2064                                                                 allqueue(REDRAWVIEW3D, 0);
2065                                                                 BIF_undo_push("Recalculate normals outside");
2066                                                         }
2067                                                 }
2068                                                 break;
2069                                         }
2070                                 }
2071                                 
2072                                 break;
2073                         case OKEY:
2074                                 if (G.obedit) {
2075                                         if (G.qual==LR_SHIFTKEY) {
2076                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%7;
2077                                                 allqueue(REDRAWHEADERS, 0);
2078                                         }
2079                                         else if((G.qual==LR_ALTKEY)) {
2080                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
2081                                                 else G.scene->proportional= 2;
2082                                                 allqueue(REDRAWHEADERS, 0);
2083                                         }
2084                                         else if((G.qual==0)) {
2085                                                 G.scene->proportional= !G.scene->proportional;
2086                                                 allqueue(REDRAWHEADERS, 0);
2087                                         }
2088                                 }
2089                                 else if((G.qual==LR_SHIFTKEY || G.qual==(LR_ALTKEY|LR_SHIFTKEY))) {
2090                                         flip_subdivison(-1);
2091                                 }
2092                                 else if(G.qual==LR_ALTKEY) {
2093                                         if(okee("Clear origin")) {
2094                                                 clear_object('o');
2095                                         }
2096                                 }
2097                                 break;
2098
2099                         case PKEY:
2100                                 if(G.obedit) {
2101                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2102                                                 if(G.obedit->type==OB_ARMATURE)
2103                                                         make_bone_parent();
2104                                                 else
2105                                                         make_parent();
2106                                         }
2107                                         
2108                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
2109                                                 clear_bone_parent();
2110                                         else if((G.qual==0) && (G.obedit->type==OB_ARMATURE)) 
2111                                                 select_bone_parent();
2112                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
2113                                                 separatemenu();
2114                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
2115                                                 separate_nurb();
2116                                         else if (G.qual==LR_SHIFTKEY) {
2117                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
2118                                                 Transform();
2119                                         }
2120                                 }
2121                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
2122                                         make_parent();
2123                                 else if(G.qual==LR_SHIFTKEY) {
2124                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_PREVIEW, 0);
2125                                         doredraw= 1;
2126                                 }
2127                                 else if(G.qual==(LR_ALTKEY|LR_SHIFTKEY)) {
2128                                         initTransform(TFM_PUSHPULL, CTX_NONE);
2129                                         Transform();
2130                                 }
2131                                 else if(G.qual==LR_ALTKEY)
2132                                         clear_parent();
2133                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY))
2134                                         make_proxy();
2135                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY|LR_SHIFTKEY)) {
2136                         start_RBSimulation();
2137                                 }
2138                                 else if((G.qual==0) && (OBACT) && (OBACT->type==OB_ARMATURE) && (OBACT->flag & OB_POSEMODE))
2139                                         select_bone_parent();
2140                                 else if((G.qual==0)) {
2141                         start_game();
2142                                 }
2143                                 break;                          
2144                         case RKEY:
2145                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0) && !(G.f & G_WEIGHTPAINT))
2146                                         rotate_uv_tface();
2147                                 else if((G.obedit==0) && G.qual==LR_ALTKEY) {
2148                                         if(okee("Clear rotation")) {
2149                                                 clear_object('r');
2150                                         }
2151                                 } 
2152                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2153                                         v3d->twtype= V3D_MANIP_ROTATE;
2154                                         doredraw= 1;
2155                                 }
2156                                 else if (G.obedit) {
2157                                         if((G.qual==LR_SHIFTKEY)) {
2158                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
2159                                                         selectrow_nurb();
2160                                         }
2161                                         else if(G.qual==LR_CTRLKEY) {
2162                                                 if (G.obedit->type==OB_MESH) {
2163                                                         CutEdgeloop(1);
2164                                                         BIF_undo_push("Cut Edgeloop");
2165                                                 }
2166                                                 else if (G.obedit->type==OB_ARMATURE) {
2167                                                         initTransform(TFM_BONE_ROLL, CTX_NONE);
2168                                                         Transform();
2169                                                 }
2170                                         }
2171                                         else if((G.qual==0)) {
2172                                                 initTransform(TFM_ROTATION, CTX_NONE);
2173                                                 Transform();
2174                                         }
2175                                 }
2176                                 else if((G.qual==0)) {
2177                                         initTransform(TFM_ROTATION, CTX_NONE);
2178                                         Transform();
2179                                 }
2180                                 break;
2181                         case SKEY:
2182                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
2183                                         v3d->twtype= V3D_MANIP_SCALE;
2184                                         doredraw= 1;
2185                                 }
2186                                 else if(G.obedit) {
2187                                         
2188                                         if(G.qual==LR_ALTKEY) {
2189                                                 if(G.obedit->type==OB_ARMATURE) {
2190                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2191                                                 }
2192                                                 else if (G.obedit->type==OB_CURVE) {
2193                                                         initTransform(TFM_CURVE_SHRINKFATTEN, CTX_NONE);
2194                                                 } else {
2195                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
2196                                                 }
2197                                                 Transform();
2198                                         }
2199                                         else if(G.qual==LR_CTRLKEY) {
2200                                                 initTransform(TFM_SHEAR, CTX_NONE);
2201                                                 Transform();
2202                                         }
2203                                         else if(G.qual==LR_SHIFTKEY)
2204                                                 snapmenu();
2205                                         else if(G.qual==0) {
2206                                                 if(G.obedit->type==OB_ARMATURE) {
2207                                                         bArmature *arm= G.obedit->data;
2208                                                         if(arm->drawtype==ARM_ENVELOPE)
2209                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
2210                                                         else
2211                                                                 initTransform(TFM_RESIZE, CTX_NONE);
2212                                                 }
2213                                                 else
2214                                                         initTransform(TFM_RESIZE, CTX_NONE);
2215                                                 Transform();
2216                                         }
2217                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
2218                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
2219                                                 Transform();
2220                                         }
2221                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
2222                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
2223                                         }
2224                                 }
2225                                 else if(G.qual==LR_ALTKEY) {
2226                                         if(G.f & G_WEIGHTPAINT)
2227                                                 ob= ob->parent;
2228                                         if(ob && (ob->flag & OB_POSEMODE)) {
2229                                                 bArmature *arm= ob->data;
2230                                                 if( ELEM(arm->drawtype, ARM_B_BONE, ARM_ENVELOPE)) {
2231                                                         initTransform(TFM_BONESIZE, CTX_NONE);
2232                                                         Transform();
2233                                                         break;
2234                                                 }
2235                                         }
2236                                         
2237                                         if(okee("Clear scale")) {
2238                                                 clear_object('s');
2239                                         }
2240                                 }
2241                                 else if(G.qual==LR_SHIFTKEY) {
2242                                         snapmenu();
2243                                 }
2244                                 else if((G.qual==0)) {
2245                                         initTransform(TFM_RESIZE, CTX_NONE);
2246                                         Transform();
2247                                 }
2248                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
2249                                         initTransform(TFM_TOSPHERE, CTX_NONE);
2250                                         Transform();
2251                                 }
2252                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
2253                                         initTransform(TFM_SHEAR, CTX_NONE);
2254                                         Transform();
2255                                 }
2256                                 break;
2257                         case TKEY:
2258                                 if(G.obedit){
2259                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
2260                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
2261                                                 allqueue(REDRAWVIEW3D, 0);
2262                                                 countall();
2263                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2264                                         }
2265                                         if (G.obedit->type==OB_CURVE) {
2266                                                 if (G.qual==LR_ALTKEY) {
2267                                                         clear_tilt();
2268                                                 }
2269                                                 else if (G.qual==0) {
2270                                                         initTransform(TFM_TILT, CTX_NONE);
2271                                                         Transform();
2272                                                 }
2273                                         }
2274                                 }
2275                                 else if(G.qual==LR_CTRLKEY) {
2276                                         if(ob && (ob->flag & OB_POSEMODE));
2277                                         else make_track();
2278                                 }
2279                                 else if(G.qual==LR_ALTKEY) {
2280                                         if(ob && (ob->flag & OB_POSEMODE));
2281                                         else clear_track();
2282                                 }
2283                                 else if((G.qual==0)){
2284                                         texspace_edit();
2285                                 }
2286                                 
2287                                 break;
2288                         case UKEY:
2289                                 if(G.obedit) {
2290                                         if(G.obedit->type==OB_MESH) {
2291                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2292                                         }
2293                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
2294                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
2295                                         }
2296                                 }
2297                                 else if((G.qual==0)) {
2298                                         if(G.f & G_WEIGHTPAINT)
2299                                                 BIF_undo();
2300                                         else if(G.f & G_VERTEXPAINT)
2301                                                 BIF_undo();
2302                                         else if(G.f & G_TEXTUREPAINT)
2303                                                 imagepaint_undo();
2304                                         else if (G.f & G_FACESELECT)
2305                                                 uv_autocalc_tface();
2306                                         else {
2307                                                 single_user();
2308                                         }
2309                                 }
2310                                         
2311                                 break;
2312                         case VKEY:
2313                                 if((G.qual==LR_SHIFTKEY)) {
2314                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
2315                                                 align_view_to_selected(v3d);
2316                                         }
2317                                         else if (G.f & G_FACESELECT) {
2318                                                 align_view_to_selected(v3d);
2319                                         }
2320                                 }
2321                                 else if(G.qual==LR_ALTKEY)
2322                                         image_aspect();
2323                                 else if (G.qual==0){
2324                                         if(G.obedit) {
2325                                                 if(G.obedit->type==OB_MESH) {
2326                                                         mesh_rip();
2327                                                 }
2328                                                 else if(G.obedit->type==OB_CURVE) {
2329                                                         sethandlesNurb(2);
2330                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
2331                                                         allqueue(REDRAWVIEW3D, 0);
2332                                                         BIF_undo_push("Handle change");
2333                                                 }
2334                                         }
2335                                         else if(ob && ob->type == OB_MESH) 
2336                                                 set_vpaint();
2337                                 }
2338                                 break;
2339                         case WKEY:
2340                                 if((G.qual==LR_SHIFTKEY)) {
2341                                         initTransform(TFM_WARP, CTX_NONE);
2342                                         Transform();
2343                                 }
2344                                 else if(G.qual==LR_ALTKEY) {
2345                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
2346                                 }
2347                                 else if(G.qual==LR_CTRLKEY) {
2348                                         if(G.obedit) {
2349                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
2350                                                         switchdirectionNurb2();
2351                                                 }
2352                                         }
2353                                 }
2354                                 else if((G.qual==0))
2355                                         special_editmenu();
2356                                 
2357                                 break;
2358                         case XKEY:
2359                         case DELKEY:
2360                                 if(G.qual==0 || G.qual==LR_SHIFTKEY)
2361                                         delete_context_selected();
2362                                 break;
2363                         case YKEY:
2364                                 if((G.qual==0) && (G.obedit)) {
2365                                         if(G.obedit->type==OB_MESH) split_mesh();
2366                                 }
2367                                 break;
2368                         case ZKEY:
2369                                 toggle_shading();
2370                                 
2371                                 scrarea_queue_headredraw(curarea);
2372                                 scrarea_queue_winredraw(curarea);
2373                                 break;
2374                         
2375                         case HOMEKEY:
2376                                 if(G.qual==0)
2377                                         view3d_home(0);
2378                                 break;
2379                         case COMMAKEY:
2380                                 if(G.qual==LR_CTRLKEY) {
2381                                         G.vd->around= V3D_CENTROID;
2382                                 } else if(G.qual==LR_SHIFTKEY) {
2383                                         G.vd->around= V3D_CENTROID;
2384                                 } else if(G.qual==0) {
2385                                         G.vd->around= V3D_CENTER;
2386                                 }
2387                                 handle_view3d_around();
2388                                 
2389                                 scrarea_queue_headredraw(curarea);
2390                                 scrarea_queue_winredraw(curarea);
2391                                 break;
2392                                 
2393                         case PERIODKEY:
2394                                 if(G.qual==LR_CTRLKEY) {
2395                                         G.vd->around= V3D_LOCAL;
2396                                 }       else if(G.qual==0) {
2397                                         G.vd->around= V3D_CURSOR;
2398                                 }
2399                                 handle_view3d_around();
2400                                 
2401                                 scrarea_queue_headredraw(curarea);
2402                                 scrarea_queue_winredraw(curarea);
2403                                 break;
2404                         
2405                         case PADSLASHKEY:
2406                                 if(G.qual==0) {
2407                                         if(G.vd->localview) {
2408                                                 G.vd->localview= 0;
2409                                                 endlocalview(curarea);
2410                                         }
2411                                         else {
2412                                                 G.vd->localview= 1;
2413                                                 initlocalview();
2414                                         }
2415                                         scrarea_queue_headredraw(curarea);
2416                                 }
2417                                 break;
2418                         case PADASTERKEY:       /* '*' */
2419                                 if(G.qual==0) {
2420                                         if(ob) {
2421                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
2422                                                         editmesh_align_view_to_selected(G.vd, 2);
2423                                                 } 
2424                                                 else if (G.f & G_FACESELECT) {
2425                                                         if(ob->type==OB_MESH) {
2426                                                                 Mesh *me= ob->data;
2427                                                                 faceselect_align_view_to_selected(G.vd, me, 2);
2428                                                         }
2429                                                 }
2430                                                 else
2431                                                         obmat_to_viewmat(ob);
2432                                                 
2433                                                 if(G.vd->persp==2) G.vd->persp= 1;
2434                                                 scrarea_queue_winredraw(curarea);
2435                                         }
2436                                 }
2437                                 break;
2438                         case PADPERIOD: /* '.' */
2439                                 if(G.qual==0)
2440                                         centerview();
2441                                 break;
2442                         
2443                         case PAGEUPKEY:
2444                                 if(G.qual==LR_CTRLKEY)
2445                                         movekey_obipo(1);
2446                                 else if((G.qual==0))
2447                                         nextkey_obipo(1);       /* in editipo.c */
2448                                 break;
2449
2450                         case PAGEDOWNKEY:
2451                                 if(G.qual==LR_CTRLKEY)
2452                                         movekey_obipo(-1);
2453                                 else if((G.qual==0))
2454                                         nextkey_obipo(-1);
2455                                 break;
2456
2457                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
2458                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
2459                         case PADENTER:
2460                                 persptoetsen(event);
2461                                 doredraw= 1;
2462                                 break;
2463                         case PADMINUS:
2464                                 if ( (G.qual==LR_CTRLKEY)
2465                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2466                                         select_less();
2467                                 else if ( (G.qual==LR_CTRLKEY)
2468                                          && (G.obedit) && (G.obedit->type==OB_CURVE) )
2469                                         select_less_nurb();
2470                                 /*
2471                                 else if ( (G.qual==LR_CTRLKEY)
2472                                          && (G.obedit) && (G.obedit->type==OB_SURF) )
2473                                         select_less_nurb(); 
2474                                 */
2475                                 else {
2476                                         persptoetsen(event);
2477                                         doredraw= 1;
2478                                 }
2479                                 break;
2480
2481                         case PADPLUSKEY:
2482                                 if ( (G.qual==LR_CTRLKEY)
2483                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2484                                         select_more();
2485                                 else if ( (G.qual==LR_CTRLKEY)
2486                                          && (G.obedit) && (G.obedit->type==OB_CURVE) )
2487                                         select_more_nurb();
2488                                 /*
2489                                 else if ( (G.qual==LR_CTRLKEY)
2490                                          && (G.obedit) && (G.obedit->type==OB_SURF) )
2491                                         select_more_nurb();
2492                                 */
2493                                 else {
2494                                         persptoetsen(event);
2495                                         doredraw= 1;
2496                                 }
2497                                 break;
2498
2499                         case ESCKEY:
2500                                 if(G.qual==0) {
2501                                         if (G.vd->flag & V3D_DISPIMAGE) {
2502                                                 G.vd->flag &= ~V3D_DISPIMAGE;
2503                                                 doredraw= 1;
2504                                         }
2505                                 }
2506                                 break;
2507                         }
2508                 }
2509         }
2510         
2511         if(doredraw) {
2512                 scrarea_queue_winredraw(curarea);
2513                 scrarea_queue_headredraw(curarea);
2514         }
2515 }
2516
2517 static void initview3d(ScrArea *sa)
2518 {
2519         View3D *vd;
2520         
2521         vd= MEM_callocN(sizeof(View3D), "initview3d");
2522         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
2523
2524         vd->spacetype= SPACE_VIEW3D;
2525         vd->blockscale= 0.7f;
2526         vd->viewquat[0]= 1.0f;
2527         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
2528         vd->persp= 1;
2529         vd->drawtype= OB_WIRE;
2530         vd->view= 7;
2531         vd->dist= 10.0;
2532         vd->lens= 35.0f;
2533         vd->near= 0.01f;
2534         vd->far= 500.0f;
2535         vd->grid= 1.0f;
2536         vd->gridlines= 16;
2537         vd->lay= vd->layact= 1;
2538         if(G.scene) {
2539                 vd->lay= vd->layact= G.scene->lay;
2540                 vd->camera= G.scene->camera;
2541         }
2542         vd->scenelock= 1;
2543         vd->gridflag |= V3D_SHOW_X;
2544         vd->gridflag |= V3D_SHOW_Y;
2545         vd->gridflag |= V3D_SHOW_FLOOR;
2546         vd->gridflag &= ~V3D_SHOW_Z;
2547
2548         vd->depths= NULL;
2549 }
2550
2551
2552 /* ******************** SPACE: IPO ********************** */
2553
2554 static void changeview2dspace(ScrArea *sa, void *spacedata)
2555 {
2556         if(G.v2d==0) return;
2557
2558         test_view2d(G.v2d, curarea->winx, curarea->winy);
2559         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
2560 }
2561
2562 static int get_cfra_from_dx(SpaceIpo * sipo, int dx)
2563 {
2564         if (sipo->blocktype == ID_SEQ) {
2565                 Sequence * seq = (Sequence*) sipo->from;
2566                 if (!seq) {
2567                         return dx;
2568                 }
2569                 if ((seq->flag & SEQ_IPO_FRAME_LOCKED) != 0) {
2570                         return dx;
2571                 } else {
2572                         float m= (seq->enddisp - seq->startdisp)/100.0f;
2573                         float cfra = dx * m + seq->startdisp;
2574
2575                         return (int) cfra;
2576                 }
2577         } else {
2578                 return dx;
2579         }
2580 }
2581
2582 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2583 {
2584         extern void do_ipobuts(unsigned short event);   /* drawipo.c */
2585         unsigned short event= evt->event;
2586         short val= evt->val;
2587         SpaceIpo *sipo= curarea->spacedata.first;
2588         View2D *v2d= &sipo->v2d;
2589         float dx, dy;
2590         int cfra, doredraw= 0;
2591         short mval[2];
2592         short mousebut = L_MOUSE;
2593         
2594         if(sa->win==0) return;
2595
2596         if(val) {
2597                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
2598
2599                 /* swap mouse buttons based on user preference */
2600                 if (U.flag & USER_LMOUSESELECT) {
2601                         if (event == LEFTMOUSE) {
2602                                 event = RIGHTMOUSE;
2603                                 mousebut = L_MOUSE;
2604                         } else if (event == RIGHTMOUSE) {
2605                                 event = LEFTMOUSE;
2606                                 mousebut = R_MOUSE;
2607                         }
2608                 }
2609
2610                 switch(event) {
2611                 case UI_BUT_EVENT:
2612                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
2613                         if(val>0 && val < 256) do_ipowin_buts(val-1);
2614                         else do_ipobuts(val);
2615                         break;
2616                         
2617                 case LEFTMOUSE:
2618                         if( in_ipo_buttons() ) {
2619                                 do_ipo_selectbuttons();
2620                                 doredraw= 1;
2621                         }
2622                         else if(view2dmove(LEFTMOUSE)); /* only checks for sliders */
2623                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
2624                         else {
2625                                 do {
2626                                         getmouseco_areawin(mval);
2627                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
2628                                         
2629                                         cfra = get_cfra_from_dx(sipo, (int)dx);
2630                                         if(cfra< 1) cfra= 1;
2631                                         
2632                                         if( cfra!=CFRA ) {
2633                                                 CFRA= cfra;
2634                                                 update_for_newframe_nodraw(0);  /* 1 = nosound */
2635                                                 force_draw_all(0); /* To make constraint sliders redraw */
2636                                         }
2637                                         else PIL_sleep_ms(30);
2638                                 
2639                                 } while(get_mbut() & mousebut);
2640                         }
2641                         break;
2642                 case RIGHTMOUSE:
2643                         mouse_select_ipo();
2644                         allqueue(REDRAWTIME, 0);
2645                         allqueue(REDRAWIPO, 0);
2646                         allqueue(REDRAWACTION, 0);
2647                         allqueue(REDRAWNLA, 0);
2648                         allqueue(REDRAWSOUND, 0);
2649                         break;
2650                 case MIDDLEMOUSE:
2651                         if(in_ipo_buttons()) {
2652                                 scroll_ipobuts();
2653                         }
2654                         else view2dmove(event); /* in drawipo.c */
2655                         break;
2656                 case WHEELUPMOUSE:
2657                 case WHEELDOWNMOUSE:
2658                         view2dmove(event);      /* in drawipo.c */
2659                         break;
2660                 case PADPLUSKEY:
2661                         view2d_zoom(v2d, 0.1154f, sa->winx, sa->winy);
2662                         doredraw= 1;
2663                         break;
2664                 case PADMINUS:
2665                         view2d_zoom(v2d, -0.15f, sa->winx, sa->winy);
2666                         doredraw= 1;
2667                         break;
2668                 case PAGEUPKEY:
2669                         if(G.qual & LR_CTRLKEY)
2670                                 movekey_ipo(1);
2671                         else if(G.qual & LR_SHIFTKEY)
2672                                 nextkey_ipo(1);
2673                         else
2674                                 nextprev_marker(1);
2675                         break;
2676                 case PAGEDOWNKEY:
2677                         if(G.qual & LR_CTRLKEY)
2678                                 movekey_ipo(-1);
2679                         else if(G.qual & LR_SHIFTKEY)
2680                                 nextkey_ipo(-1);
2681                         else
2682                                 nextprev_marker(-1);
2683                         break;
2684                 case HOMEKEY:
2685                         if((G.qual==0))
2686                                 do_ipo_buttons(B_IPOHOME);
2687                         break;
2688                         
2689                 case AKEY:
2690                         if (G.qual & LR_CTRLKEY) {
2691                                 deselect_markers(1, 0);
2692                                 allqueue(REDRAWTIME, 0);
2693                                 allqueue(REDRAWIPO, 0);
2694                                 allqueue(REDRAWACTION, 0);
2695                                 allqueue(REDRAWNLA, 0);
2696                                 allqueue(REDRAWSOUND, 0);
2697                         }
2698                         else if (G.qual==0) {
2699                                 if(in_ipo_buttons()) {
2700                                         swap_visible_editipo();
2701                                 }
2702                                 else {
2703                                         swap_selectall_editipo();
2704                                 }
2705                                 allspace (REMAKEIPO, 0);
2706                                 allqueue (REDRAWNLA, 0);
2707                                 allqueue (REDRAWACTION, 0);
2708                         }
2709                         
2710                         break;
2711                 case BKEY:
2712                         if (G.qual==0)
2713                                 borderselect_ipo();
2714                         else if (G.qual==LR_CTRLKEY) {
2715                                 borderselect_markers();
2716                         }
2717                         break;
2718                 case CKEY:
2719                         if (G.qual == LR_SHIFTKEY)
2720                                 center_currframe();
2721                         else
2722                                 move_to_frame();
2723                         break;
2724                 case DKEY:
2725                         if (G.qual==LR_SHIFTKEY)
2726                                 add_duplicate_editipo();
2727                         else if (G.qual == (LR_CTRLKEY|LR_SHIFTKEY))
2728                                 duplicate_marker();
2729