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