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