73552449fba3f5beb916c617d7d3481431d4642e
[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 (U.undosteps == 0) return;
647
648                 if(G.obedit->type==OB_MESH)
649                         undo_push_mesh(str);
650                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
651                         undo_push_curve(str);
652                 else if (G.obedit->type==OB_FONT)
653                         undo_push_font(str);
654                 else if (G.obedit->type==OB_MBALL)
655                         undo_push_mball(str);
656                 else if (G.obedit->type==OB_LATTICE)
657                         undo_push_lattice(str);
658                 else if (G.obedit->type==OB_ARMATURE)
659                         undo_push_armature(str);
660         }
661         else {
662                 if(U.uiflag & USER_GLOBALUNDO) 
663                         BKE_write_undo(str);
664         }
665 }
666
667 void BIF_undo(void)
668 {       
669         if(G.obedit) {
670                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
671                         undo_editmode_step(1);
672         }
673         else {
674                 if(G.f & G_WEIGHTPAINT)
675                         wpaint_undo();
676                 else if(G.f & G_VERTEXPAINT)
677                         vpaint_undo();
678                 else if(G.f & G_TEXTUREPAINT); /* no texture paint undo yet */
679                 else if(curarea->spacetype==SPACE_IMAGE && (G.sima->flag & SI_DRAWTOOL));
680                 else {
681                         /* now also in faceselect mode */
682                         if(U.uiflag & USER_GLOBALUNDO) BKE_undo_step(1);
683                 }
684         }
685 }
686
687 void BIF_redo(void)
688 {
689         if(G.obedit) {
690                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
691                         undo_editmode_step(-1);
692         }
693         else {
694                 if(G.f & G_WEIGHTPAINT)
695                         wpaint_undo();
696                 else if(G.f & G_VERTEXPAINT)
697                         vpaint_undo();
698                 else {
699                         /* includes faceselect now */
700                         if(U.uiflag & USER_GLOBALUNDO) BKE_undo_step(-1);
701                 }
702         }
703 }
704
705 void BIF_undo_menu(void)
706 {
707         if(G.obedit) {
708                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
709                         undo_editmode_menu();
710                 allqueue(REDRAWALL, 0);
711         }
712         else {
713                 if(G.f & G_WEIGHTPAINT)
714                         ;
715                 else if(G.f & G_VERTEXPAINT)
716                         ;
717                 else {
718                         if(U.uiflag & USER_GLOBALUNDO) {
719                                 char *menu= BKE_undo_menu_string();
720                                 if(menu) {
721                                         short event= pupmenu_col(menu, 20);
722                                         MEM_freeN(menu);
723                                         if(event>0) BKE_undo_number(event);
724                                 }
725                         }
726                 }
727         }
728 }
729
730 /* *************** */
731
732 static void winqreadview3dspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
733 {
734         View3D *v3d= sa->spacedata.first;
735         Object *ob= OBACT;      // do not change!
736         float *curs;
737         int doredraw= 0, pupval;
738         unsigned short event= evt->event;
739         short val= evt->val;
740         char ascii= evt->ascii;
741         
742         if(curarea->win==0) return;     /* when it comes from sa->headqread() */
743         
744         if(val) {
745
746                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
747                 if(event==MOUSEY || event==MOUSEX) return;
748                 
749                 if(event==UI_BUT_EVENT) do_butspace(val); // temporal, view3d deserves own queue?
750                 
751                 /* we consider manipulator a button, defaulting to leftmouse */
752                 if(event==LEFTMOUSE) if(BIF_do_manipulator(sa)) return;
753                 
754                 /* swap mouse buttons based on user preference */
755                 if (U.flag & USER_LMOUSESELECT) {
756                         if (event==LEFTMOUSE) event = RIGHTMOUSE;
757                         else if (event==RIGHTMOUSE) event = LEFTMOUSE;
758                 }
759
760                 /* run any view3d event handler script links */
761                 if (event && sa->scriptlink.totscript)
762                         if (BPY_do_spacehandlers(sa, event, SPACEHANDLER_VIEW3D_EVENT))
763                                 return; /* return if event was processed (swallowed) by handler(s) */
764
765                 /* TEXTEDITING?? */
766                 if((G.obedit) && G.obedit->type==OB_FONT) {
767                         switch(event) {
768                         
769                         case LEFTMOUSE:
770                                 mouse_cursor();
771                                 break;
772                         case MIDDLEMOUSE:
773                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
774                                 if(U.flag & USER_VIEWMOVE) {
775                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
776                                                 viewmove(0);
777                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
778                                                 viewmove(2);
779                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
780                                                 viewmove(1);
781                                 }
782                                 else {
783                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
784                                                 viewmove(1);
785                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
786                                                 viewmove(2);
787                                         else
788                                                 viewmove(0);
789                                 }
790                                 break;
791                                 
792                         case WHEELUPMOUSE:
793                                 /* Regular:   Zoom in */
794                                 /* Shift:     Scroll up */
795                                 /* Ctrl:      Scroll right */
796                                 /* Alt-Shift: Rotate up */
797                                 /* Alt-Ctrl:  Rotate right */
798
799                                 if( G.qual & LR_SHIFTKEY ) {
800                                         if( G.qual & LR_ALTKEY ) { 
801                                                 G.qual &= ~LR_SHIFTKEY;
802                                                 persptoetsen(PAD2);
803                                                 G.qual |= LR_SHIFTKEY;
804                                         } else {
805                                                 persptoetsen(PAD2);
806                                         }
807                                 } else if( G.qual & LR_CTRLKEY ) {
808                                         if( G.qual & LR_ALTKEY ) { 
809                                                 G.qual &= ~LR_CTRLKEY;
810                                                 persptoetsen(PAD4);
811                                                 G.qual |= LR_CTRLKEY;
812                                         } else {
813                                                 persptoetsen(PAD4);
814                                         }
815                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
816                                         persptoetsen(PADMINUS);
817                                 else
818                                         persptoetsen(PADPLUSKEY);
819
820                                 doredraw= 1;
821                                 break;
822
823                         case WHEELDOWNMOUSE:
824                                 /* Regular:   Zoom out */
825                                 /* Shift:     Scroll down */
826                                 /* Ctrl:      Scroll left */
827                                 /* Alt-Shift: Rotate down */
828                                 /* Alt-Ctrl:  Rotate left */
829
830                                 if( G.qual & LR_SHIFTKEY ) {
831                                         if( G.qual & LR_ALTKEY ) { 
832                                                 G.qual &= ~LR_SHIFTKEY;
833                                                 persptoetsen(PAD8);
834                                                 G.qual |= LR_SHIFTKEY;
835                                         } else {
836                                                 persptoetsen(PAD8);
837                                         }
838                                 } else if( G.qual & LR_CTRLKEY ) {
839                                         if( G.qual & LR_ALTKEY ) { 
840                                                 G.qual &= ~LR_CTRLKEY;
841                                                 persptoetsen(PAD6);
842                                                 G.qual |= LR_CTRLKEY;
843                                         } else {
844                                                 persptoetsen(PAD6);
845                                         }
846                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
847                                         persptoetsen(PADPLUSKEY);
848                                 else
849                                         persptoetsen(PADMINUS);
850                                 
851                                 doredraw= 1;
852                                 break;
853
854                         case UKEY:
855                                 if(G.qual==LR_ALTKEY) {
856                                         remake_editText();
857                                         doredraw= 1;
858                                 } 
859                                 else {
860                                         do_textedit(event, val, ascii);
861                                 }
862                                 break;
863                         case VKEY:
864                                 if(G.qual==LR_ALTKEY) {
865                                         paste_editText();
866                                         doredraw= 1;
867                                 } 
868                                 else {
869                                         do_textedit(event, val, ascii);
870                                 }
871                                 break;
872                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
873                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
874                         case PADENTER:
875                                 persptoetsen(event);
876                                 doredraw= 1;
877                                 break;
878                                 
879                         default:
880                                 do_textedit(event, val, ascii);
881                                 break;
882                         }
883                 }
884                 else {
885
886                         if (U.flag & USER_NONUMPAD) {
887                                 event= convert_for_nonumpad(event);
888                         }
889
890                         switch(event) {
891                         
892                         case BACKBUFDRAW:
893                                 backdrawview3d(1);
894                                 break;
895                                                 
896                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
897                          * based on user preference USER_LMOUSESELECT
898                          */
899                         case LEFTMOUSE: 
900                                 if ((G.obedit) || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT))) {
901                                         mouse_cursor();
902                                 } else if (G.f & G_VERTEXPAINT) {
903                                         vertex_paint();
904                                 }
905                                 else if (G.f & G_WEIGHTPAINT){
906                                         weight_paint();
907                                 }
908                                 else if (G.f & G_TEXTUREPAINT) {
909                                         face_draw();
910                                 }
911                                 break;
912                         case MIDDLEMOUSE:
913                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
914                                 if(U.flag & USER_VIEWMOVE) {
915                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
916                                                 viewmove(0);
917                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
918                                                 viewmove(2);
919                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
920                                                 viewmove(1);
921                                 }
922                                 else {
923                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
924                                                 viewmove(1);
925                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
926                                                 viewmove(2);
927                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
928                                                 viewmove(0);
929                                 }
930                                 break;
931                         case RIGHTMOUSE:
932                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
933                                         if(G.obedit->type==OB_MESH)
934                                                 mouse_mesh();
935                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
936                                                 mouse_nurb();
937                                         else if(G.obedit->type==OB_MBALL)
938                                                 mouse_mball();
939                                         else if(G.obedit->type==OB_LATTICE)
940                                                 mouse_lattice();
941                                         else if(G.obedit->type==OB_ARMATURE)
942                                                 mouse_armature();
943                                 }
944                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
945                                         mouse_mesh();   // loop select for 1 mousebutton dudes
946                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
947                                         mouse_mesh();   // loop select for 1 mousebutton dudes
948                                 else if(G.qual==LR_CTRLKEY)
949                                         mouse_select(); // also allow in editmode, for vertex parenting
950                                 else if(G.f & G_FACESELECT)
951                                         face_select();
952                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
953                                         sample_vpaint();
954                                 else
955                                         mouse_select(); // does poses too
956                                 break;
957                         case WHEELUPMOUSE:
958                                 /* Regular:   Zoom in */
959                                 /* Shift:     Scroll up */
960                                 /* Ctrl:      Scroll right */
961                                 /* Alt-Shift: Rotate up */
962                                 /* Alt-Ctrl:  Rotate right */
963
964                                 if( G.qual & LR_SHIFTKEY ) {
965                                         if( G.qual & LR_ALTKEY ) { 
966                                                 G.qual &= ~LR_SHIFTKEY;
967                                                 persptoetsen(PAD2);
968                                                 G.qual |= LR_SHIFTKEY;
969                                         } else {
970                                                 persptoetsen(PAD2);
971                                         }
972                                 } else if( G.qual & LR_CTRLKEY ) {
973                                         if( G.qual & LR_ALTKEY ) { 
974                                                 G.qual &= ~LR_CTRLKEY;
975                                                 persptoetsen(PAD4);
976                                                 G.qual |= LR_CTRLKEY;
977                                         } else {
978                                                 persptoetsen(PAD4);
979                                         }
980                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
981                                         persptoetsen(PADMINUS);
982                                 else
983                                         persptoetsen(PADPLUSKEY);
984
985                                 doredraw= 1;
986                                 break;
987                         case WHEELDOWNMOUSE:
988                                 /* Regular:   Zoom out */
989                                 /* Shift:     Scroll down */
990                                 /* Ctrl:      Scroll left */
991                                 /* Alt-Shift: Rotate down */
992                                 /* Alt-Ctrl:  Rotate left */
993
994                                 if( G.qual & LR_SHIFTKEY ) {
995                                         if( G.qual & LR_ALTKEY ) { 
996                                                 G.qual &= ~LR_SHIFTKEY;
997                                                 persptoetsen(PAD8);
998                                                 G.qual |= LR_SHIFTKEY;
999                                         } else {
1000                                                 persptoetsen(PAD8);
1001                                         }
1002                                 } else if( G.qual & LR_CTRLKEY ) {
1003                                         if( G.qual & LR_ALTKEY ) { 
1004                                                 G.qual &= ~LR_CTRLKEY;
1005                                                 persptoetsen(PAD6);
1006                                                 G.qual |= LR_CTRLKEY;
1007                                         } else {
1008                                                 persptoetsen(PAD6);
1009                                         }
1010                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1011                                         persptoetsen(PADPLUSKEY);
1012                                 else
1013                                         persptoetsen(PADMINUS);
1014                                 
1015                                 doredraw= 1;
1016                                 break;
1017                         
1018                         case ONEKEY:
1019                                 if(G.qual==LR_CTRLKEY) {
1020                                         if(ob && ob->type == OB_MESH) {
1021                                                 flip_subdivison(ob, 1);
1022                                         }
1023                                 }
1024                                 else do_layer_buttons(0); 
1025                                 break;
1026                                 
1027                         case TWOKEY:
1028                                 if(G.qual==LR_CTRLKEY) {
1029                                         if(ob && ob->type == OB_MESH) {
1030                                                 flip_subdivison(ob, 2);
1031                                         }
1032                                 }
1033                                 else do_layer_buttons(1); 
1034                                 break;
1035                                 
1036                         case THREEKEY:
1037                                 if(G.qual==LR_CTRLKEY) {
1038                                         if(ob && ob->type == OB_MESH) {
1039                                                 flip_subdivison(ob, 3);
1040                                         }
1041                                 }
1042                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1043                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1044                                                 select_faces_by_numverts(3);
1045                                 }
1046                                 else do_layer_buttons(2); 
1047                                 break;
1048                                 
1049                         case FOURKEY:
1050                                 if(G.qual==LR_CTRLKEY) {
1051                                         if(ob && ob->type == OB_MESH) {
1052                                                 flip_subdivison(ob, 4);
1053                                         }
1054                                 }
1055                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1056                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1057                                                 select_faces_by_numverts(4);
1058                                 }
1059                                 else do_layer_buttons(3); 
1060                                 break;
1061                                 
1062                         case FIVEKEY:
1063                                 if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1064                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1065                                                 select_faces_by_numverts(5);
1066                                 }
1067                                 else do_layer_buttons(4);
1068                                 break;
1069
1070                         case SIXKEY:
1071                                 do_layer_buttons(5); break;
1072                         case SEVENKEY:
1073                                 do_layer_buttons(6); break;
1074                         case EIGHTKEY:
1075                                 do_layer_buttons(7); break;
1076                         case NINEKEY:
1077                                 do_layer_buttons(8); break;
1078                         case ZEROKEY:
1079                                 do_layer_buttons(9); break;
1080                         case MINUSKEY:
1081                                 do_layer_buttons(10); break;
1082                         case EQUALKEY:
1083                                 do_layer_buttons(11); break;
1084                         case ACCENTGRAVEKEY:
1085                                 do_layer_buttons(-1); break;
1086                         
1087                         case SPACEKEY:
1088                                 if(G.qual == LR_CTRLKEY) {
1089                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1090                                         if(val>0) {
1091                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1092                                                 else {
1093                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1094                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1095                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1096                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1097                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1098                                                 }
1099                                                 doredraw= 1;
1100                                         }
1101                                 }
1102                                 else if(G.qual == LR_ALTKEY) {
1103                                         BIF_selectOrientation();
1104                                         doredraw= 1;
1105                                 }
1106
1107                                 break;
1108                                 
1109                         case AKEY:
1110                                 if(G.qual & LR_CTRLKEY) apply_object(); // also with shift!
1111                                 else if((G.qual==LR_SHIFTKEY)) {
1112                                         toolbox_n_add();
1113                                 }
1114                                 else {
1115                                         if(G.obedit) {
1116                                                 if(G.obedit->type==OB_MESH)
1117                                                         deselectall_mesh();
1118                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1119                                                         deselectall_nurb();
1120                                                 else if(G.obedit->type==OB_MBALL)
1121                                                         deselectall_mball();
1122                                                 else if(G.obedit->type==OB_LATTICE)
1123                                                         deselectall_Latt();
1124                                                 else if(G.obedit->type==OB_ARMATURE)
1125                                                         deselectall_armature(1);        // 1 == toggle
1126                                         }
1127                                         else if (ob && (ob->flag & OB_POSEMODE)){
1128                                                 deselectall_posearmature(ob, 1);
1129                                         }
1130                                         else {
1131                                                 if(G.f & G_FACESELECT) deselectall_tface();
1132                                                 else {
1133                                                         /* by design, the center of the active object 
1134                                                          * (which need not necessarily by selected) will
1135                                                          * still be drawn as if it were selected.
1136                                                          */
1137                                                         deselectall();
1138                                                 }
1139                                         }
1140                                 }
1141                                 break;
1142                         case BKEY:
1143                                 if(G.qual==LR_ALTKEY)
1144                                         view3d_edit_clipping(v3d);
1145                                 else if(G.qual==LR_SHIFTKEY)
1146                                         set_render_border();
1147                                 else if(G.qual==0)
1148                                         borderselect();
1149                                 break;
1150                         case CKEY:
1151                                 if(G.qual==LR_CTRLKEY) {
1152                                         if(ob && (ob->flag & OB_POSEMODE))
1153                                                 pose_copy_menu();       /* poseobject.c */
1154                                         else
1155                                                 copy_attr_menu();
1156                                 }
1157                                 else if(G.qual==LR_ALTKEY) {
1158                                         if(ob && (ob->flag & OB_POSEMODE))
1159                                                 pose_clear_constraints();       /* poseobject.c */
1160                                         else
1161                                                 convertmenu();  /* editobject.c */
1162                                 }
1163                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY)) 
1164                                         add_constraint(0);      /* editconstraint.c, generic for objects and posemode */
1165                                 else if((G.qual==LR_SHIFTKEY)) {
1166                                         view3d_home(1);
1167                                         curs= give_cursor();
1168                                         curs[0]=curs[1]=curs[2]= 0.0;
1169                                         allqueue(REDRAWVIEW3D, 0);
1170                                 }
1171                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1172                                         makecyclicNurb();
1173                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1174                                         allqueue(REDRAWVIEW3D, 0);
1175                                 }
1176                                 else if((G.qual==0)){
1177                                         curs= give_cursor();
1178                                         G.vd->ofs[0]= -curs[0];
1179                                         G.vd->ofs[1]= -curs[1];
1180                                         G.vd->ofs[2]= -curs[2];
1181                                         scrarea_queue_winredraw(curarea);
1182                                 }
1183                         
1184                                 break;
1185                         case DKEY:
1186                                 if((G.qual==LR_SHIFTKEY)) {
1187                                         duplicate_context_selected();
1188                                 }
1189                                 else if(G.qual==LR_ALTKEY) {
1190                                         if(ob && (ob->flag & OB_POSEMODE))
1191                                                 error ("Duplicate not possible in posemode.");
1192                                         else if((G.obedit==NULL))
1193                                                 adduplicate(0);
1194                                 }
1195                                 else if(G.qual==LR_CTRLKEY) {
1196                                         imagestodisplist();
1197                                 }
1198                                 else if((G.qual==0)){
1199                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
1200                                         if(pupval>0) {
1201                                                 G.vd->drawtype= pupval;
1202                                                 doredraw= 1;
1203                                         
1204                                         }
1205                                 }
1206                                 
1207                                 break;
1208                         case EKEY:
1209                                 if (G.qual==0){
1210                                         if(G.obedit) {
1211                                                 if(G.obedit->type==OB_MESH)
1212                                                         extrude_mesh();
1213                                                 else if(G.obedit->type==OB_CURVE)
1214                                                         addvert_Nurb('e');
1215                                                 else if(G.obedit->type==OB_SURF)
1216                                                         extrude_nurb();
1217                                                 else if(G.obedit->type==OB_ARMATURE)
1218                                                         extrude_armature(0);
1219                                         }
1220                                 }
1221                                 else if (G.qual==LR_CTRLKEY) {
1222                                         if(G.obedit && G.obedit->type==OB_MESH)
1223                                                 Edge_Menu();
1224                                 }
1225                                 else if (G.qual==LR_SHIFTKEY) {
1226                                         if (G.obedit && G.obedit->type==OB_MESH) {
1227                                                 initTransform(TFM_CREASE, CTX_EDGE);
1228                                                 Transform();
1229                                         }
1230                                         else if (G.obedit && G.obedit->type==OB_ARMATURE) {
1231                                                 extrude_armature(1);
1232                                         }
1233                                 }
1234                                 break;
1235                         case FKEY:
1236                                 if(G.obedit) {
1237                                         if(G.obedit->type==OB_MESH) {
1238                                                 if((G.qual==LR_SHIFTKEY))
1239                                                         fill_mesh();
1240                                                 else if(G.qual==LR_ALTKEY)
1241                                                         beauty_fill();
1242                                                 else if(G.qual==LR_CTRLKEY)
1243                                                         edge_flip();
1244                                                 else if (G.qual==0)
1245                                                         addedgeface_mesh();
1246                                         }
1247                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
1248                                 }
1249                                 else if(G.qual==LR_CTRLKEY)
1250                                         sort_faces();
1251                                 else if((G.qual==LR_SHIFTKEY)) {
1252                                         if(ob && (ob->flag & OB_POSEMODE))
1253                                            pose_activate_flipped_bone();
1254                                         else if(G.f & G_WEIGHTPAINT)
1255                                                 pose_activate_flipped_bone();
1256                                         else
1257                                                 fly();
1258                                 }
1259                                 else {
1260                                         set_faceselect();
1261                                 }
1262                                 
1263                                 break;
1264                         case GKEY:
1265                                 /* RMGRP if(G.qual & LR_CTRLKEY) add_selected_to_group();
1266                                 else if(G.qual & LR_ALTKEY) rem_selected_from_group(); */
1267                                 if((G.qual==LR_SHIFTKEY))
1268                                         select_group_menu();
1269                                 else if(G.qual==LR_ALTKEY) {
1270                                         if(okee("Clear location")) {
1271                                                 clear_object('g');
1272                                         }
1273                                 }
1274                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1275                                         v3d->twtype= V3D_MANIP_TRANSLATE;
1276                                         doredraw= 1;
1277                                 }
1278                                 else if((G.qual==0)) {
1279                                         initTransform(TFM_TRANSLATION, CTX_NONE);
1280                                         Transform();
1281                                 }
1282                                 break;
1283                         case HKEY:
1284                                 if(G.obedit) {
1285                                         if(G.obedit->type==OB_MESH) {
1286                                                 if(G.qual==LR_CTRLKEY)
1287                                                         add_hook();
1288                                                 else if(G.qual==LR_ALTKEY)
1289                                                         reveal_mesh();
1290                                                 else if((G.qual==LR_SHIFTKEY))
1291                                                         hide_mesh(1);
1292                                                 else if((G.qual==0)) 
1293                                                         hide_mesh(0);
1294                                         }
1295                                         else if(G.obedit->type== OB_SURF) {
1296                                                 if(G.qual==LR_CTRLKEY)
1297                                                         add_hook();
1298                                                 else if(G.qual==LR_ALTKEY)
1299                                                         revealNurb();
1300                                                 else if((G.qual==LR_SHIFTKEY))
1301                                                         hideNurb(1);
1302                                                 else if((G.qual==0))
1303                                                         hideNurb(0);
1304                                         }
1305                                         else if(G.obedit->type==OB_CURVE) {
1306                                                 if(G.qual==LR_CTRLKEY)
1307                                                         add_hook();
1308                                                 else {
1309                                                         if(G.qual==LR_CTRLKEY)
1310                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
1311                                                         else if((G.qual==LR_SHIFTKEY))
1312                                                                 sethandlesNurb(1);
1313                                                         else if((G.qual==0))
1314                                                                 sethandlesNurb(3);
1315                                                         
1316                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1317                                                         BIF_undo_push("Handle change");
1318                                                         allqueue(REDRAWVIEW3D, 0);
1319                                                 }
1320                                         }
1321                                         else if(G.obedit->type==OB_LATTICE) {
1322                                                 if(G.qual==LR_CTRLKEY) add_hook();
1323                                         }
1324                                         else if(G.obedit->type==OB_MBALL) {
1325                                                 if(G.qual==LR_ALTKEY)
1326                                                         reveal_mball();
1327                                                 else if((G.qual==LR_SHIFTKEY))
1328                                                         hide_mball(1);
1329                                                 else if((G.qual==0)) 
1330                                                         hide_mball(0);
1331                                         }
1332                                         else if(G.obedit->type==OB_ARMATURE) {
1333                                                 if (G.qual==0)
1334                                                         hide_selected_armature_bones();
1335                                                 else if (G.qual==LR_SHIFTKEY)
1336                                                         hide_unselected_armature_bones();
1337                                                 else if (G.qual==LR_ALTKEY)
1338                                                         show_all_armature_bones();
1339                                         }
1340                                 }
1341                                 else if(G.f & G_FACESELECT)
1342                                         hide_tface();
1343                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1344                                         if (G.qual==0)
1345                                                 hide_selected_pose_bones();
1346                                         else if (G.qual==LR_SHIFTKEY)
1347                                                 hide_unselected_pose_bones();
1348                                         else if (G.qual==LR_ALTKEY)
1349                                                 show_all_pose_bones();
1350                                 }
1351                                 break;
1352                         case IKEY:
1353                                 if(G.obedit);
1354                                 else if(G.qual==LR_CTRLKEY) {
1355                                         if(ob && ob->type==OB_ARMATURE) 
1356                                                 if(ob->flag & OB_POSEMODE) 
1357                                                         pose_add_IK();
1358                                 }
1359                                 else if(G.qual==LR_ALTKEY) {
1360                                         if(ob && ob->type==OB_ARMATURE) 
1361                                                 if(ob->flag & OB_POSEMODE) 
1362                                                         pose_clear_IK();
1363                                 }
1364                                 break;
1365                                 
1366                         case JKEY:
1367                                 if(G.qual==LR_CTRLKEY) {
1368                                         if( ob ) {
1369                                                 if(ob->type == OB_MESH)
1370                                                         join_mesh();
1371                                                 else if(ob->type == OB_CURVE)
1372                                                         join_curve(OB_CURVE);
1373                                                 else if(ob->type == OB_SURF)
1374                                                         join_curve(OB_SURF);
1375                                                 else if(ob->type == OB_ARMATURE)
1376                                                         join_armature ();
1377                                         }
1378                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1379                                                 addsegment_nurb();
1380                                 }
1381                                 else if(G.obedit) {
1382                                         if(G.obedit->type==OB_MESH) {
1383                                                 join_triangles();
1384                                         }
1385                                 }
1386
1387                                 break;
1388                         case KKEY:
1389                                 if(G.obedit) {
1390                                         if (G.obedit->type==OB_MESH) {
1391                                                 if (G.qual==LR_SHIFTKEY)
1392                                                         KnifeSubdivide(KNIFE_PROMPT);
1393                                                 else if (G.qual==0)
1394                                                         LoopMenu();
1395                                         }
1396                                         else if(G.obedit->type==OB_SURF)
1397                                                 printknots();
1398                                 }
1399                                 else {
1400                                         if((G.qual==LR_SHIFTKEY)) {
1401                                                 if(G.f & G_FACESELECT)
1402                                                         clear_vpaint_selectedfaces();
1403                                                 else if(G.f & G_VERTEXPAINT)
1404                                                         clear_vpaint();
1405                                                 else
1406                                                         select_select_keys();
1407                                         }
1408                                         else if (G.qual==0)
1409                                                 set_ob_ipoflags();
1410                                 }
1411                                 
1412                                 break;
1413                         case LKEY:
1414                                 if(G.obedit) {
1415                                         if(G.obedit->type==OB_MESH)
1416                                                 selectconnected_mesh(G.qual);
1417                                         if(G.obedit->type==OB_ARMATURE)
1418                                                 selectconnected_armature();
1419                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1420                                                 selectconnected_nurb();
1421                                 }
1422                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1423                                         selectconnected_posearmature();
1424                                 }
1425                                 else {
1426                                         if(G.f & G_FACESELECT) {
1427                                                 if((G.qual==0))
1428                                                         select_linked_tfaces(0);
1429                                                 else if((G.qual==LR_SHIFTKEY))
1430                                                         select_linked_tfaces(1);
1431                                                 else if(G.qual==LR_CTRLKEY)
1432                                                         select_linked_tfaces(2);
1433                                         }
1434                                         else {
1435                                                 if((G.qual==0))
1436                                                         make_local();
1437                                                 else if((G.qual==LR_SHIFTKEY))
1438                                                         selectlinks_menu();
1439                                                 else if(G.qual==LR_CTRLKEY)
1440                                                         make_links_menu();
1441                                         }
1442                                 }
1443                                 break;
1444                         case MKEY:
1445                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1446                                         mirror_uv_tface();
1447                                 else if(G.obedit){
1448                                         if(G.qual==LR_ALTKEY) {
1449                                                 if(G.obedit->type==OB_MESH) {
1450                                                         mergemenu();
1451                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1452                                                 }
1453                                         }
1454                                         else if((G.qual==0) || (G.qual==LR_CTRLKEY)) {
1455                                                 mirrormenu();
1456                                         }
1457                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1458                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
1459                                         }
1460                                 }
1461                                 else if(G.qual & LR_CTRLKEY) {
1462                                         mirrormenu();
1463                                 }
1464                                 else if(G.qual==0) {
1465                                      movetolayer();
1466                                 }
1467                                 break;
1468                         case NKEY:
1469                                 if((G.qual==0)) {
1470                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1471                                         allqueue(REDRAWVIEW3D, 0);
1472                                 }
1473                                 else if(G.obedit) {
1474                                         switch (G.obedit->type){
1475                                         case OB_ARMATURE:
1476                                                 if(G.qual==LR_CTRLKEY){
1477                                                         if (okee("Recalculate bone roll angles")) {
1478                                                                 auto_align_armature();
1479                                                                 allqueue(REDRAWVIEW3D, 0);
1480                                                         }
1481                                                 }
1482                                                 break;
1483                                         case OB_MESH: 
1484                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1485                                                         if(okee("Recalculate normals inside")) {
1486                                                                 righthandfaces(2);
1487                                                                 allqueue(REDRAWVIEW3D, 0);
1488                                                                 BIF_undo_push("Recalculate normals inside");
1489                                                         }
1490                                                 }
1491                                                 else if(G.qual==LR_CTRLKEY){
1492                                                         if(okee("Recalculate normals outside")) {
1493                                                                 righthandfaces(1);
1494                                                                 allqueue(REDRAWVIEW3D, 0);
1495                                                                 BIF_undo_push("Recalculate normals outside");
1496                                                         }
1497                                                 }
1498                                                 break;
1499                                         }
1500                                 }
1501                                 
1502                                 break;
1503                         case OKEY:
1504                                 if (G.obedit) {
1505                                         if (G.qual==LR_SHIFTKEY) {
1506                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%6;
1507                                                 allqueue(REDRAWHEADERS, 0);
1508                                         }
1509                                         else if((G.qual==LR_ALTKEY)) {
1510                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
1511                                                 else G.scene->proportional= 2;
1512                                                 allqueue(REDRAWHEADERS, 0);
1513                                         }
1514                                         else if((G.qual==0)) {
1515                                                 G.scene->proportional= !G.scene->proportional;
1516                                                 allqueue(REDRAWHEADERS, 0);
1517                                         }
1518                                 }
1519                                 else if((G.qual==LR_SHIFTKEY)) {
1520                                         if(ob && ob->type == OB_MESH) {
1521                                                 flip_subdivison(ob, -1);
1522                                         }
1523                                 }
1524                                 else if(G.qual==LR_ALTKEY) {
1525                                         if(okee("Clear origin")) {
1526                                                 clear_object('o');
1527                                         }
1528                                 }
1529                                 break;
1530
1531                         case PKEY:
1532                                 if(G.obedit) {
1533                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1534                                                 if(G.obedit->type==OB_ARMATURE)
1535                                                         make_bone_parent();
1536                                                 else
1537                                                         make_parent();
1538                                         }
1539                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
1540                                                 clear_bone_parent();
1541                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
1542                                                 separatemenu();
1543                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1544                                                 separate_nurb();
1545                                         else if (G.qual==LR_SHIFTKEY) {
1546                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
1547                                                 Transform();
1548                                         }
1549                                 }
1550                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
1551                                         make_parent();
1552                                 else if(G.qual==LR_SHIFTKEY) {
1553                                         initTransform(TFM_PUSHPULL, CTX_NONE);
1554                                         Transform();
1555                                 }
1556                                 else if(G.qual==LR_ALTKEY)
1557                                         clear_parent();
1558                                 else if((G.qual==0)) {
1559                         start_game();
1560                                 }
1561                                 break;                          
1562                         case RKEY:
1563                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1564                                         rotate_uv_tface();
1565                                 else if(G.qual==LR_ALTKEY) {
1566                                         if(okee("Clear rotation")) {
1567                                                 clear_object('r');
1568                                         }
1569                                 } 
1570                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1571                                         v3d->twtype= V3D_MANIP_ROTATE;
1572                                         doredraw= 1;
1573                                 }
1574                                 else if (G.obedit) {
1575                                         if((G.qual==LR_SHIFTKEY)) {
1576                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
1577                                                         selectrow_nurb();
1578                                         }
1579                                         else if(G.qual==LR_CTRLKEY) {
1580                                                 if (G.obedit->type==OB_MESH)
1581                                                         CutEdgeloop(1);
1582                                                         BIF_undo_push("Cut Edgeloop");
1583                                         }
1584                                         else if((G.qual==0)) {
1585                                                 initTransform(TFM_ROTATION, CTX_NONE);
1586                                                 Transform();
1587                                         }
1588                                 }
1589                                 else if((G.qual==0)) {
1590                                         initTransform(TFM_ROTATION, CTX_NONE);
1591                                         Transform();
1592                                 }
1593                                 break;
1594                         case SKEY:
1595                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1596                                         v3d->twtype= V3D_MANIP_SCALE;
1597                                         doredraw= 1;
1598                                 }
1599                                 else if(G.obedit) {
1600                                         
1601                                         if(G.qual==LR_ALTKEY) {
1602                                                 if(G.obedit->type==OB_ARMATURE) {
1603                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1604                                                 }
1605                                                 else
1606                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
1607                                                 Transform();
1608                                         }
1609                                         else if(G.qual==LR_CTRLKEY) {
1610                                                 initTransform(TFM_SHEAR, CTX_NONE);
1611                                                 Transform();
1612                                         }
1613                                         else if(G.qual==LR_SHIFTKEY)
1614                                                 snapmenu();
1615                                         else if(G.qual==0) {
1616                                                 if(G.obedit->type==OB_ARMATURE) {
1617                                                         bArmature *arm= G.obedit->data;
1618                                                         if(arm->drawtype==ARM_ENVELOPE)
1619                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
1620                                                         else
1621                                                                 initTransform(TFM_RESIZE, CTX_NONE);
1622                                                 }
1623                                                 else
1624                                                         initTransform(TFM_RESIZE, CTX_NONE);
1625                                                 Transform();
1626                                         }
1627                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
1628                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
1629                                                 Transform();
1630                                         }
1631                                         
1632                                 }
1633                                 else if(G.qual==LR_ALTKEY) {
1634                                         if(G.f & G_WEIGHTPAINT)
1635                                                 ob= ob->parent;
1636                                         if(ob && (ob->flag & OB_POSEMODE)) {
1637                                                 bArmature *arm= ob->data;
1638                                                 if( ELEM(arm->drawtype, ARM_B_BONE, ARM_ENVELOPE)) {
1639                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1640                                                         Transform();
1641                                                         break;
1642                                                 }
1643                                         }
1644                                         
1645                                         if(okee("Clear size")) {
1646                                                 clear_object('s');
1647                                         }
1648                                 }
1649                                 else if(G.qual==LR_SHIFTKEY) {
1650                                         snapmenu();
1651                                 }
1652                                 else if((G.qual==0)) {
1653                                         initTransform(TFM_RESIZE, CTX_NONE);
1654                                         Transform();
1655                                 }
1656                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1657                                         initTransform(TFM_TOSPHERE, CTX_NONE);
1658                                         Transform();
1659                                 }
1660                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
1661                                         initTransform(TFM_SHEAR, CTX_NONE);
1662                                         Transform();
1663                                 }
1664                                 break;
1665                         case TKEY:
1666                                 if(G.obedit){
1667                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
1668                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
1669                                                 allqueue(REDRAWVIEW3D, 0);
1670                                                 countall();
1671                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1672                                         }
1673                                         if (G.obedit->type==OB_CURVE) {
1674                                                 if (G.qual==LR_ALTKEY) {
1675                                                         clear_tilt();
1676                                                 }
1677                                                 else if (G.qual==0) {
1678                                                         initTransform(TFM_TILT, CTX_NONE);
1679                                                         Transform();
1680                                                 }
1681                                         }
1682                                 }
1683                                 else if(G.qual==LR_CTRLKEY) {
1684                                         if(ob && (ob->flag & OB_POSEMODE));
1685                                         else make_track();
1686                                 }
1687                                 else if(G.qual==LR_ALTKEY) {
1688                                         if(ob && (ob->flag & OB_POSEMODE));
1689                                         else clear_track();
1690                                 }
1691                                 else if((G.qual==0)){
1692                                         texspace_edit();
1693                                 }
1694                                 
1695                                 break;
1696                         case UKEY:
1697                                 if(G.obedit) {
1698                                         if(G.obedit->type==OB_MESH) {
1699                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1700                                         }
1701                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
1702                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1703                                         }
1704                                 }
1705                                 else if((G.qual==0)) {
1706                                         if (G.f & G_FACESELECT)
1707                                                 uv_autocalc_tface();
1708                                         else if(G.f & G_WEIGHTPAINT)
1709                                                 wpaint_undo();
1710                                         else if(G.f & G_VERTEXPAINT)
1711                                                 vpaint_undo();
1712                                         else {
1713                                                 single_user();
1714                                         }
1715                                 }
1716                                         
1717                                 break;
1718                         case VKEY:
1719                                 if((G.qual==LR_SHIFTKEY)) {
1720                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
1721                                                 align_view_to_selected(v3d);
1722                                         }
1723                                         else if (G.f & G_FACESELECT) {
1724                                                 align_view_to_selected(v3d);
1725                                         }
1726                                 }
1727                                 else if(G.qual==LR_ALTKEY)
1728                                         image_aspect();
1729                                 else if (G.qual==0){
1730                                         if(G.obedit) {
1731                                                 if(G.obedit->type==OB_MESH) {
1732                                                         mesh_rip();
1733                                                 }
1734                                                 else if(G.obedit->type==OB_CURVE) {
1735                                                         sethandlesNurb(2);
1736                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1737                                                         allqueue(REDRAWVIEW3D, 0);
1738                                                         BIF_undo_push("Handle change");
1739                                                 }
1740                                         }
1741                                         else if(ob && ob->type == OB_MESH) 
1742                                                 set_vpaint();
1743                                 }
1744                                 break;
1745                         case WKEY:
1746                                 if((G.qual==LR_SHIFTKEY)) {
1747                                         initTransform(TFM_WARP, CTX_NONE);
1748                                         Transform();
1749                                 }
1750                                 else if(G.qual==LR_ALTKEY) {
1751                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
1752                                 }
1753                                 else if(G.qual==LR_CTRLKEY) {
1754                                         if(G.obedit) {
1755                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
1756                                                         switchdirectionNurb2();
1757                                                 }
1758                                         }
1759                                 }
1760                                 else if((G.qual==0))
1761                                         special_editmenu();
1762                                 
1763                                 break;
1764                         case XKEY:
1765                         case DELKEY:
1766                                 if(G.qual==0)
1767                                         delete_context_selected();
1768                                 break;
1769                         case YKEY:
1770                                 if((G.qual==0) && (G.obedit)) {
1771                                         if(G.obedit->type==OB_MESH) split_mesh();
1772                                 }
1773                                 break;
1774                         case ZKEY:
1775                                 toggle_shading();
1776                                 
1777                                 scrarea_queue_headredraw(curarea);
1778                                 scrarea_queue_winredraw(curarea);
1779                                 break;
1780                         
1781                         case HOMEKEY:
1782                                 if(G.qual==0)
1783                                         view3d_home(0);
1784                                 break;
1785                         case COMMAKEY:
1786                                 if(G.qual==LR_CTRLKEY) {
1787                                         G.vd->around= V3D_CENTROID;
1788                                 } else if(G.qual==LR_SHIFTKEY) {
1789                                         G.vd->around= V3D_CENTROID;
1790                                 } else if(G.qual==0) {
1791                                         G.vd->around= V3D_CENTRE;
1792                                 }
1793                                 handle_view3d_around();
1794                                 
1795                                 scrarea_queue_headredraw(curarea);
1796                                 scrarea_queue_winredraw(curarea);
1797                                 break;
1798                                 
1799                         case PERIODKEY:
1800                                 if(G.qual==LR_CTRLKEY) {
1801                                         G.vd->around= V3D_LOCAL;
1802                                 }       else if(G.qual==0) {
1803                                         G.vd->around= V3D_CURSOR;
1804                                 }
1805                                 handle_view3d_around();
1806                                 
1807                                 scrarea_queue_headredraw(curarea);
1808                                 scrarea_queue_winredraw(curarea);
1809                                 break;
1810                         
1811                         case PADSLASHKEY:
1812                                 if(G.qual==0) {
1813                                         if(G.vd->localview) {
1814                                                 G.vd->localview= 0;
1815                                                 endlocalview(curarea);
1816                                         }
1817                                         else {
1818                                                 G.vd->localview= 1;
1819                                                 initlocalview();
1820                                         }
1821                                         scrarea_queue_headredraw(curarea);
1822                                 }
1823                                 break;
1824                         case PADASTERKEY:       /* '*' */
1825                                 if(G.qual==0) {
1826                                         if(ob) {
1827                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
1828                                                         editmesh_align_view_to_selected(G.vd, 2);
1829                                                 } 
1830                                                 else if (G.f & G_FACESELECT) {
1831                                                         if(ob->type==OB_MESH) {
1832                                                                 Mesh *me= ob->data;
1833                                                                 faceselect_align_view_to_selected(G.vd, me, 2);
1834                                                         }
1835                                                 }
1836                                                 else
1837                                                         obmat_to_viewmat(ob);
1838                                                 
1839                                                 if(G.vd->persp==2) G.vd->persp= 1;
1840                                                 scrarea_queue_winredraw(curarea);
1841                                         }
1842                                 }
1843                                 break;
1844                         case PADPERIOD: /* '.' */
1845                                 if(G.qual==0)
1846                                         centreview();
1847                                 break;
1848                         
1849                         case PAGEUPKEY:
1850                                 if(G.qual==LR_CTRLKEY)
1851                                         movekey_obipo(1);
1852                                 else if((G.qual==0))
1853                                         nextkey_obipo(1);       /* in editipo.c */
1854                                 break;
1855
1856                         case PAGEDOWNKEY:
1857                                 if(G.qual==LR_CTRLKEY)
1858                                         movekey_obipo(-1);
1859                                 else if((G.qual==0))
1860                                         nextkey_obipo(-1);
1861                                 break;
1862                                 
1863                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1864                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1865                         case PADENTER:
1866                                 persptoetsen(event);
1867                                 doredraw= 1;
1868                                 break;
1869                         case PADMINUS:
1870                                 if ( (G.qual==LR_CTRLKEY)
1871                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
1872                                         select_less();
1873                                 else {
1874                                         persptoetsen(event);
1875                                         doredraw= 1;
1876                                 }
1877                                 break;
1878
1879                         case PADPLUSKEY:
1880                                 if ( (G.qual==LR_CTRLKEY)
1881                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
1882                                         select_more();
1883                                 else {
1884                                         persptoetsen(event);
1885                                         doredraw= 1;
1886                                 }
1887                                 break;
1888
1889                         case ESCKEY:
1890                                 if(G.qual==0) {
1891                                         if (G.vd->flag & V3D_DISPIMAGE) {
1892                                                 G.vd->flag &= ~V3D_DISPIMAGE;
1893                                                 doredraw= 1;
1894                                         }
1895                                 }
1896                                 break;
1897                         }
1898                 }
1899         }
1900         
1901         if(doredraw) {
1902                 scrarea_queue_winredraw(curarea);
1903                 scrarea_queue_headredraw(curarea);
1904         }
1905 }
1906
1907 static void initview3d(ScrArea *sa)
1908 {
1909         View3D *vd;
1910         
1911         vd= MEM_callocN(sizeof(View3D), "initview3d");
1912         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
1913
1914         vd->spacetype= SPACE_VIEW3D;
1915         vd->blockscale= 0.7f;
1916         vd->viewquat[0]= 1.0f;
1917         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
1918         vd->persp= 1;
1919         vd->drawtype= OB_WIRE;
1920         vd->view= 7;
1921         vd->dist= 10.0;
1922         vd->lens= 35.0f;
1923         vd->near= 0.01f;
1924         vd->far= 500.0f;
1925         vd->grid= 1.0f;
1926         vd->gridlines= 16;
1927         vd->lay= vd->layact= 1;
1928         if(G.scene) {
1929                 vd->lay= vd->layact= G.scene->lay;
1930                 vd->camera= G.scene->camera;
1931         }
1932         vd->scenelock= 1;
1933         vd->gridflag |= V3D_SHOW_X;
1934         vd->gridflag |= V3D_SHOW_Y;
1935         vd->gridflag |= V3D_SHOW_FLOOR;
1936         vd->gridflag &= ~V3D_SHOW_Z;
1937 }
1938
1939
1940 /* ******************** SPACE: IPO ********************** */
1941
1942 static void changeview2dspace(ScrArea *sa, void *spacedata)
1943 {
1944         if(G.v2d==0) return;
1945
1946         test_view2d(G.v2d, curarea->winx, curarea->winy);
1947         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1948 }
1949
1950 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1951 {
1952         extern void do_ipobuts(unsigned short event);   // drawipo.c
1953         unsigned short event= evt->event;
1954         short val= evt->val;
1955         SpaceIpo *sipo= curarea->spacedata.first;
1956         View2D *v2d= &sipo->v2d;
1957         float dx, dy;
1958         int cfra, doredraw= 0;
1959         short mval[2];
1960         short mousebut = L_MOUSE;
1961         
1962         if(sa->win==0) return;
1963
1964         if(val) {
1965                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
1966
1967                 /* swap mouse buttons based on user preference */
1968                 if (U.flag & USER_LMOUSESELECT) {
1969                         if (event == LEFTMOUSE) {
1970                                 event = RIGHTMOUSE;
1971                                 mousebut = L_MOUSE;
1972                         } else if (event == RIGHTMOUSE) {
1973                                 event = LEFTMOUSE;
1974                                 mousebut = R_MOUSE;
1975                         }
1976                 }
1977
1978                 switch(event) {
1979                 case UI_BUT_EVENT:
1980                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
1981                         if(val>0 && val < 65) do_ipowin_buts(val-1);
1982                         else do_ipobuts(val);
1983                         break;
1984                         
1985                 case LEFTMOUSE:
1986                         if( in_ipo_buttons() ) {
1987                                 do_ipo_selectbuttons();
1988                                 doredraw= 1;
1989                         }
1990                         else if(view2dmove(LEFTMOUSE)); // only checks for sliders
1991                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
1992                         else {
1993                                 do {
1994                                         getmouseco_areawin(mval);
1995                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
1996                                         
1997                                         cfra= (int)dx;
1998                                         if(cfra< 1) cfra= 1;
1999                                         
2000                                         if( cfra!=CFRA ) {
2001                                                 CFRA= cfra;
2002                                                 update_for_newframe_nodraw(1);  /* 1 = nosound */
2003                                                 force_draw_all(0); /* To make constraint sliders redraw */
2004                                         }
2005                                         else PIL_sleep_ms(30);
2006                                 
2007                                 } while(get_mbut() & mousebut);
2008                         }
2009                         break;
2010                 case RIGHTMOUSE:
2011                         mouse_select_ipo();
2012                         allqueue (REDRAWACTION, 0);
2013                         allqueue(REDRAWNLA, 0);
2014                         break;
2015                 case MIDDLEMOUSE:
2016                         if(in_ipo_buttons()) {
2017                                 scroll_ipobuts();
2018                         }
2019                         else view2dmove(event); /* in drawipo.c */
2020                         break;
2021                 case WHEELUPMOUSE:
2022                 case WHEELDOWNMOUSE:
2023                         view2dmove(event);      /* in drawipo.c */
2024                         break;
2025                 case PADPLUSKEY:
2026                         view2d_zoom(v2d, 0.1154f, sa->winx, sa->winy);
2027                         doredraw= 1;
2028                         break;
2029                 case PADMINUS:
2030                         view2d_zoom(v2d, -0.15f, sa->winx, sa->winy);
2031                         doredraw= 1;
2032                         break;
2033                 case PAGEUPKEY:
2034                         if(G.qual==LR_CTRLKEY)
2035                                 movekey_ipo(1);
2036                         else if((G.qual==0))
2037                                 nextkey_ipo(1);
2038                         break;
2039                 case PAGEDOWNKEY:
2040                         if(G.qual==LR_CTRLKEY)
2041                                 movekey_ipo(-1);
2042                         else if((G.qual==0))
2043                                 nextkey_ipo(-1);
2044                         break;
2045                 case HOMEKEY:
2046                         if((G.qual==0))
2047                                 do_ipo_buttons(B_IPOHOME);
2048                         break;
2049                         
2050                 case AKEY:
2051                         if((G.qual==0)) {
2052                                 if(in_ipo_buttons()) {
2053                                         swap_visible_editipo();
2054                                 }
2055                                 else {
2056                                         swap_selectall_editipo();
2057                                 }
2058                                 allspace (REMAKEIPO, 0);
2059                                 allqueue (REDRAWNLA, 0);
2060                                 allqueue (REDRAWACTION, 0);
2061                         }
2062                         break;
2063                 case BKEY:
2064                         if((G.qual==0))
2065                                 borderselect_ipo();
2066                         break;
2067                 case CKEY:
2068                         if((G.qual==0))
2069                                 move_to_frame();
2070                         break;
2071                 case DKEY:
2072                         if((G.qual==LR_SHIFTKEY))
2073                                 add_duplicate_editipo();
2074                         break;
2075                 case GKEY:
2076                         if((G.qual==0))
2077                                 transform_ipo('g');
2078                         break;
2079                 case HKEY:
2080                         if(G.qual==LR_ALTKEY)
2081                                 sethandles_ipo(HD_AUTO_ANIM);
2082                         if(G.qual==LR_SHIFTKEY)
2083                                 sethandles_ipo(HD_AUTO);
2084                         else if(G.qual==0)
2085                                 sethandles_ipo(HD_ALIGN);
2086                         break;
2087                 case JKEY:
2088                         if((G.qual==0))
2089                                 join_ipo_menu();
2090                         break;
2091                 case KKEY:
2092                         if((G.qual==0)) {
2093                                 ipo_toggle_showkey();
2094                                 scrarea_queue_headredraw(curarea);
2095                                 allqueue(REDRAWVIEW3D, 0);
2096                                 doredraw= 1;
2097                         }
2098                         break;
2099                 case NKEY:
2100                         toggle_blockhandler(sa, IPO_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2101                         doredraw= 1;
2102                         break;
2103                 case RKEY:
2104                         if((G.qual==0))
2105                                 ipo_record();
2106                         break;
2107                 case SKEY:
2108                         if((G.qual==LR_SHIFTKEY)) {             
2109                                 ipo_snap_menu();
2110                         } else if((G.qual==0))
2111                                 transform_ipo('s');
2112                         break;
2113                 case TKEY:
2114                         if((G.qual==0))
2115                                 set_ipotype();
2116                         break;
2117                 case VKEY:
2118                         if((G.qual==0))
2119                                 sethandles_ipo(HD_VECT);
2120                         break;
2121                 case XKEY:
2122                 case DELKEY:
2123                         del_ipo();
2124                         break;
2125                 }
2126         }
2127
2128         if(doredraw) scrarea_queue_winredraw(sa);
2129 }
2130
2131 void initipo(ScrArea *sa)
2132 {
2133         SpaceIpo *sipo;
2134         
2135         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
2136         BLI_addhead(&sa->spacedata, sipo);
2137
2138         sipo->spacetype= SPACE_IPO;
2139         sipo->blockscale= 0.7f;
2140         
2141         /* sipo space loopt van (0,-?) tot (??,?) */
2142         sipo->v2d.tot.xmin= 0.0;
2143         sipo->v2d.tot.ymin= -10.0;
2144         sipo->v2d.tot.xmax= G.scene->r.efra;
2145         sipo->v2d.tot.ymax= 10.0;
2146
2147         sipo->v2d.cur= sipo->v2d.tot;
2148
2149         sipo->v2d.min[0]= 0.01f;
2150         sipo->v2d.min[1]= 0.01f;
2151
2152         sipo->v2d.max[0]= 15000.0f;
2153         sipo->v2d.max[1]= 10000.0f;
2154         
2155         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
2156         sipo->v2d.keeptot= 0;
2157
2158         sipo->blocktype= ID_OB;
2159 }
2160
2161 /* ******************** SPACE: INFO ********************** */
2162
2163 void space_mipmap_button_function(int event) {
2164         set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
2165
2166         allqueue(REDRAWVIEW3D, 0);
2167 }
2168
2169 #if 0
2170 static void space_sound_button_function(int event)
2171 {
2172         int a;
2173         SYS_SystemHandle syshandle;
2174
2175         if ((syshandle = SYS_GetSystem()))
2176         {
2177                 a = (U.gameflags & USER_DISABLE_SOUND);
2178                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
2179         }
2180 }
2181 #endif
2182
2183 // needed for event; choose new 'curmain' resets it...
2184 static short th_curcol= TH_BACK;
2185 static char *th_curcol_ptr= NULL;
2186 static char th_curcol_arr[4]={0, 0, 0, 255};
2187
2188 static void info_user_themebuts(uiBlock *block, short y1, short y2, short y3)
2189 {
2190         bTheme *btheme, *bt;
2191         int spacetype= 0;
2192         static short cur=1, curmain=2;
2193         short a, tot=0, isbuiltin= 0;
2194         char string[21*32], *strp, *col;
2195         
2196         y3= y2+23;      // exception!
2197         
2198         /* count total, max 16! */
2199         for(bt= U.themes.first; bt; bt= bt->next) tot++;
2200         
2201         /* if cur not is 1; move that to front of list */
2202         if(cur!=1) {
2203                 a= 1;
2204                 for(bt= U.themes.first; bt; bt= bt->next, a++) {
2205                         if(a==cur) {
2206                                 BLI_remlink(&U.themes, bt);
2207                                 BLI_addhead(&U.themes, bt);
2208                                 allqueue(REDRAWALL, 0);
2209                                 cur= 1;
2210                                 break;
2211                         }
2212                 }
2213         }
2214         
2215         /* the current theme */
2216         btheme= U.themes.first;
2217         if(strcmp(btheme->name, "Default")==0) isbuiltin= 1;
2218
2219         /* construct popup script */
2220         string[0]= 0;
2221         for(bt= U.themes.first; bt; bt= bt->next) {
2222                 strcat(string, bt->name);
2223                 if(btheme->next) strcat(string, "   |");
2224         }
2225         uiDefButS(block, MENU, B_UPDATE_THEME, string,                  45,y3,200,20, &cur, 0, 0, 0, 0, "Current theme");
2226         
2227         /* add / delete / name */
2228
2229         if(tot<16)
2230                 uiDefBut(block, BUT, B_ADD_THEME, "Add",        45,y2,200,20, NULL, 0, 0, 0, 0, "Makes new copy of this theme");
2231         if(tot>1 && isbuiltin==0)
2232                 uiDefBut(block, BUT, B_DEL_THEME, "Delete", 45,y1,200,20, NULL, 0, 0, 0, 0, "Delete theme");
2233
2234         if(isbuiltin) return;
2235         
2236         /* name */
2237         uiDefBut(block, TEX, B_NAME_THEME, "",                  255,y3,200,20, btheme->name, 1.0, 30.0, 0, 0, "Rename theme");
2238
2239         /* main choices pup */
2240         uiDefButS(block, MENU, B_CHANGE_THEME, "UI and Buttons %x1|%l|3D View %x2|%l|Ipo Curve Editor %x3|Action Editor %x4|"
2241                 "NLA Editor %x5|%l|UV/Image Editor %x6|Video Sequence Editor %x7|Timeline %x15|Audio Window %x8|Text Editor %x9|%l|User Preferences %x10|"
2242                 "Outliner %x11|Buttons Window %x12|%l|File Browser %x13|Image Browser %x14",
2243                                                                                                         255,y2,200,20, &curmain, 0, 0, 0, 0, "Specify theme for...");
2244         if(curmain==1) spacetype= 0;
2245         else if(curmain==2) spacetype= SPACE_VIEW3D;
2246         else if(curmain==3) spacetype= SPACE_IPO;
2247         else if(curmain==4) spacetype= SPACE_ACTION;
2248         else if(curmain==5) spacetype= SPACE_NLA;
2249         else if(curmain==6) spacetype= SPACE_IMAGE;
2250         else if(curmain==7) spacetype= SPACE_SEQ;
2251         else if(curmain==8) spacetype= SPACE_SOUND;
2252         else if(curmain==9) spacetype= SPACE_TEXT;
2253         else if(curmain==10) spacetype= SPACE_INFO;
2254         else if(curmain==11) spacetype= SPACE_OOPS;
2255         else if(curmain==12) spacetype= SPACE_BUTS;
2256         else if(curmain==13) spacetype= SPACE_FILE;
2257         else if(curmain==14) spacetype= SPACE_IMASEL;
2258         else if(curmain==15) spacetype= SPACE_TIME;
2259         else return; // only needed while coding... when adding themes for more windows
2260         
2261         /* color choices pup */
2262         if(curmain==1) {
2263                 strp= BIF_ThemeColorsPup(0);
2264                 if(th_curcol==TH_BACK) th_curcol= TH_BUT_OUTLINE;  // switching main choices...
2265         }
2266         else strp= BIF_ThemeColorsPup(spacetype);
2267         
2268         uiDefButS(block, MENU, B_REDR, strp,                    255,y1,200,20, &th_curcol, 0, 0, 0, 0, "Current color");
2269         MEM_freeN(strp);
2270         
2271         th_curcol_ptr= col= BIF_ThemeGetColorPtr(btheme, spacetype, th_curcol);
2272         if(col==NULL) return;
2273         
2274         /* first handle exceptions, special single values, row selection, etc */
2275         if(th_curcol==TH_VERTEX_SIZE) {
2276                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Vertex size ", 465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2277         }
2278         else if(th_curcol==TH_FACEDOT_SIZE) {
2279                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Face dot size ",       465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2280         }
2281         else if(th_curcol==TH_BUT_DRAWTYPE) {
2282                 uiBlockBeginAlign(block);
2283                 uiDefButC(block, ROW, B_UPDATE_THEME, "Minimal",        465,y3,100,20,  col, 2.0, 0.0, 0, 0, "");
2284                 uiDefButC(block, ROW, B_UPDATE_THEME, "Shaded", 565,y3,100,20,  col, 2.0, 1.0, 0, 0, "");
2285                 uiDefButC(block, ROW, B_UPDATE_THEME, "Rounded",        465,y2,100,20,  col, 2.0, 2.0, 0, 0, "");
2286                 uiDefButC(block, ROW, B_UPDATE_THEME, "OldSkool",       565,y2,100,20,  col, 2.0, 3.0, 0, 0, "");
2287                 uiBlockEndAlign(block);
2288         }
2289         else {
2290                 uiBlockBeginAlign(block);
2291                 if ELEM7(th_curcol, TH_PANEL, TH_LAMP, TH_FACE, TH_FACE_SELECT, TH_MENU_BACK, TH_MENU_HILITE, TH_MENU_ITEM) {
2292                         uiDefButC(block, NUMSLI, B_UPDATE_THEME,"A ",   465,y3+25,200,20,  col+3, 0.0, 255.0, B_THEMECOL, 0, "");
2293                 }
2294                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"R ",   465,y3,200,20,  col, 0.0, 255.0, B_THEMECOL, 0, "");
2295                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"G ",   465,y2,200,20,  col+1, 0.0, 255.0, B_THEMECOL, 0, "");
2296                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"B ",   465,y1,200,20,  col+2, 0.0, 255.0, B_THEMECOL, 0, "");
2297                 uiBlockEndAlign(block);
2298
2299                 uiDefButC(block, COL, B_UPDATE_THEME, "",               675,y1,50,y3-y1+20, col, 0, 0, 0, 0, "");
2300                                 
2301                 /* copy paste */
2302                 uiBlockBeginAlign(block);
2303                 uiDefBut(block, BUT, B_THEME_COPY, "Copy Color",        755,y2,120,20, NULL, 0, 0, 0, 0, "Stores current color in buffer");
2304                 uiDefBut(block, BUT, B_THEME_PASTE, "Paste Color",      755,y1,120,20, NULL, 0, 0, 0, 0, "Pastes buffer color");
2305                 uiBlockEndAlign(block);
2306                 
2307                 uiDefButC(block, COL, 0, "",                            885,y1,50,y2-y1+20, th_curcol_arr, 0, 0, 0, 0, "");
2308                 
2309         }
2310 }
2311
2312
2313 void drawinfospace(ScrArea *sa, void *spacedata)
2314 {
2315         uiBlock *block;
2316         static short cur_light=0, cur_light_var=0;
2317         float fac, col[3];
2318         short xpos, ypos, ypostab,  buth, rspace, dx, y1, y2, y3, y4, y5, y6;
2319         short y2label, y3label, y4label, y5label, y6label;
2320         short spref, mpref, lpref, smfileselbut;
2321         short edgsp, midsp;
2322         char naam[32];
2323
2324         if(curarea->win==0 || curarea->winy<2) return;
2325
2326         BIF_GetThemeColor3fv(TH_BACK, col);
2327         glClearColor(col[0], col[1], col[2], 0.0);
2328         glClear(GL_COLOR_BUFFER_BIT);
2329
2330         if(curarea->winx<=1280.0) {
2331                 fac= ((float)curarea->winx)/1280.0f;
2332                 myortho2(0.375f, 1280.375f, 0.375f, curarea->winy/fac + 0.375f);
2333         }
2334         else {
2335                 myortho2(0.375f, (float)curarea->winx + 0.375f, 0.375f, (float)curarea->winy + 0.375f);
2336         }
2337         
2338         sprintf(naam, "infowin %d", curarea->win);
2339         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->win);
2340
2341
2342         /* Vars for nice grid alignment */ 
2343         dx= (1280-90)/7;        /* spacing for use in equally dividing 'tab' row */
2344
2345         xpos = 45;              /* left padding */
2346         ypos = 50;              /* bottom padding for buttons */
2347         ypostab = 10;           /* bottom padding for 'tab' row */
2348
2349         buth = 20;              /* standard button height */
2350
2351         spref = 90;     /* standard size for small preferences button */
2352         mpref = 189;    /* standard size for medium preferences button */
2353         lpref = 288;    /* standard size for large preferences button */
2354         smfileselbut = buth;    /* standard size for fileselect button (square) */
2355
2356         edgsp = 3;              /* space from edge of end 'tab' to edge of end button */
2357         midsp = 9;              /* horizontal space between buttons */
2358
2359         rspace = 3;             /* default space between rows */
2360
2361         y1 = ypos;              /* grid alignment for each row of buttons */
2362         y2 = ypos+buth+rspace;
2363         y3 = ypos+2*(buth+rspace);
2364         y4 = ypos+3*(buth+rspace);
2365         y5 = ypos+4*(buth+rspace);
2366         y6 = ypos+5*(buth+rspace);
2367
2368
2369         y2label = y2-2;         /* adjustments to offset the labels down to align better */
2370         y3label = y3-2;
2371         y4label = y4-2;
2372         y5label = y5-2;
2373         y6label = y6-2;
2374
2375
2376         /* set the colour to blue and draw the main 'tab' controls */
2377
2378         uiBlockSetCol(block, TH_BUT_SETTING1);
2379         uiBlockBeginAlign(block);
2380         
2381         uiDefButS(block, ROW,B_USERPREF,"View & Controls",
2382                 xpos,ypostab,(short)dx,buth,
2383                 &U.userpref,1.0,0.0, 0, 0,"");
2384                 
2385         uiDefButS(block, ROW,B_USERPREF,"Edit Methods",
2386                 (short)(xpos+dx),ypostab,(short)dx,buth,
2387                 &U.userpref,1.0,1.0, 0, 0,"");
2388
2389         uiDefButS(block, ROW,B_USERPREF,"Language & Font",
2390                 (short)(xpos+2*dx),ypostab,(short)dx,buth,
2391                 &U.userpref,1.0,2.0, 0, 0,"");
2392
2393         uiDefButS(block, ROW,B_USERPREF,"Themes",
2394                 (short)(xpos+3*dx),ypostab,(short)dx,buth,
2395                 &U.userpref,1.0,6.0, 0, 0,"");
2396
2397         uiDefButS(block, ROW,B_USERPREF,"Auto Save",
2398                 (short)(xpos+4*dx),ypostab,(short)dx,buth,
2399                 &U.userpref,1.0,3.0, 0, 0,"");
2400
2401         uiDefButS(block, ROW,B_USERPREF,"System & OpenGL",
2402                 (short)(xpos+5*dx),ypostab,(short)dx,buth,
2403                 &U.userpref,1.0,4.0, 0, 0,"");
2404                 
2405         uiDefButS(block, ROW,B_USERPREF,"File Paths",
2406                 (short)(xpos+6*dx),ypostab,(short)dx,buth,
2407                 &U.userpref,1.0,5.0, 0, 0,"");
2408
2409         uiBlockSetCol(block, TH_AUTO);
2410         uiBlockEndAlign(block);
2411         /* end 'tab' controls */
2412
2413         /* line 2: left x co-ord, top y co-ord, width, height */
2414
2415         if(U.userpref == 6) {
2416                 info_user_themebuts(block, y1, y2, y3);
2417         }
2418         else if (U.userpref == 0) { /* view & controls */
2419
2420                 uiDefBut(block, LABEL,0,"Display:",
2421                         xpos,y6label,spref,buth,
2422                         0, 0, 0, 0, 0, "");     
2423                 uiBlockBeginAlign(block);
2424                 uiDefButBitI(block, TOG, USER_TOOLTIPS, 0, "ToolTips",
2425                         (xpos+edgsp),y5,spref,buth,
2426                         &(U.flag), 0, 0, 0, 0,
2427                         "Display tooltips (help tags) over buttons");
2428                 uiDefButBitI(block, TOG, USER_DRAWVIEWINFO, B_DRAWINFO, "Object Info",
2429                         (xpos+edgsp),y4,spref,buth,
2430                         &(U.uiflag), 0, 0, 0, 0,
2431                         "Display active object name and frame number in the 3D View");
2432                 uiDefButBitI(block, TOG, USER_SCENEGLOBAL, 0, "Global Scene",
2433                         (xpos+edgsp),y3,spref,buth,
2434                         &(U.flag), 0, 0, 0, 0,
2435                         "Forces the current Scene to be displayed in all Screens");
2436 #ifndef __APPLE__       
2437                 uiDefButBitS(block, TOG, 1, 0, "Large Cursors",
2438                         (xpos+edgsp),y2,spref,buth,
2439                         &(U.curssize), 0, 0, 0, 0,
2440                         "Use large mouse cursors when available");
2441 #else 
2442                 U.curssize=0; /*Small Cursor always for OS X for now */
2443 #endif
2444                 uiDefButBitI(block, TOG, USER_PLAINMENUS, B_PLAINMENUS, "Plain menus",
2445                         (xpos+edgsp),y1,spref,buth,
2446                         &(U.uiflag), 0, 0, 0, 0,
2447                         "Use column layout for toolbox and do not flip contents in any menu");
2448                 uiBlockEndAlign(block);
2449
2450                 uiDefBut(block, LABEL,0,"Menus:",
2451                         (xpos+(2*edgsp)+spref),y6label,spref,buth,
2452                         0, 0, 0, 0, 0, "");
2453                 uiBlockBeginAlign(block);
2454                 uiDefButBitI(block, TOG, USER_MENUOPENAUTO, 0, "Open on Mouse Over",
2455                         (xpos+edgsp+spref+midsp),y5,mpref,buth,
2456                         &(U.uiflag), 0, 0, 0, 0,
2457                         "Open menu buttons and pulldowns automatically when the mouse is hovering");
2458                 uiDefButS(block, NUM, 0, "Top Level:",
2459                         (xpos+edgsp+spref+midsp),y4,spref+edgsp,buth,
2460                         &(U.menuthreshold1), 1, 40, 0, 0,
2461                         "Time delay in 1/10 seconds before automatically opening top level menus");
2462                 uiDefButS(block, NUM, 0, "Sublevels:",
2463                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y4,spref+edgsp,buth,
2464                         &(U.menuthreshold2), 1, 40, 0, 0,
2465                         "Time delay in 1/10 seconds before automatically opening menu sublevels");
2466                 uiBlockEndAlign(block);
2467
2468                 uiDefBut(block, LABEL,0,"Toolbox click-hold delay:",
2469                         (xpos+(2*edgsp)+spref),y3label,mpref,buth,
2470                         0, 0, 0, 0, 0, "");
2471                 uiBlockBeginAlign(block);
2472                 uiDefButS(block, NUM, 0, "LMB:",
2473                         (xpos+edgsp+spref+midsp),y2,spref+edgsp,buth,
2474                         &(U.tb_leftmouse), 2, 40, 0, 0,
2475                         "Time in 1/10 seconds to hold the Left Mouse Button before opening the toolbox");
2476                 uiDefButS(block, NUM, 0, "RMB:",
2477                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y2,spref+edgsp,buth,
2478                         &(U.tb_rightmouse), 2, 40, 0, 0,
2479                         "Time in 1/10 seconds to hold the Right Mouse Button before opening the toolbox");      
2480                 uiBlockEndAlign(block);
2481
2482                 uiDefButBitI(block, TOG, USER_PANELPINNED, 0, "Pin Floating Panels",
2483                         (xpos+edgsp+spref+midsp),y1,(mpref/2),buth,
2484                         &(U.uiflag), 0, 0, 0, 0,
2485                         "Make floating panels invoked by a hotkey (eg. N Key) open at the previous location");
2486                 
2487                 uiDefButBitI(block, TOG, USER_LOCKAROUND, B_DRAWINFO, "Global Pivot",
2488                         (xpos+edgsp+spref+midsp+(mpref/2)),y1,(mpref/2),buth,
2489                         &(U.uiflag), 0, 0, 0, 0,
2490                         "Lock the same rotation/scaling pivot in all 3D Views");        
2491                 
2492                 uiDefBut(block, LABEL,0,"Snap to grid:",
2493                         (xpos+(2*edgsp)+spref+midsp+mpref),y6label,mpref,buth,
2494                         0, 0, 0, 0, 0, "");
2495                 uiBlockBeginAlign(block);
2496                 uiDefButBitI(block, TOG, USER_AUTOGRABGRID, 0, "Grab/Move",
2497                         (xpos+edgsp+mpref+spref+(2*midsp)),y5,spref,buth,
2498                         &(U.flag), 0, 0, 0, 0,
2499                         "Snap objects and sub-objects to grid units when moving");
2500                 uiDefButBitI(block, TOG, USER_AUTOROTGRID, 0, "Rotate",
2501                         (xpos+edgsp+mpref+spref+(2*midsp)),y4,spref,buth,
2502                         &(U.flag), 0, 0, 0, 0,
2503                         "Snap objects and sub-objects to grid units when rotating");
2504                 uiDefButBitI(block, TOG, USER_AUTOSIZEGRID, 0, "Scale",
2505                         (xpos+edgsp+mpref+spref+(2*midsp)),y3,spref,buth,
2506                         &(U.flag), 0, 0, 0, 0,
2507                         "Snap objects and sub-objects to grid units when scaling");
2508                 uiBlockEndAlign(block);
2509                 
2510                 uiDefBut(block, LABEL,0,"View zoom:",
2511                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y6label,mpref,buth,
2512                         0, 0, 0, 0, 0, "");
2513                 uiBlockBeginAlign(block);
2514                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2515                 uiDefButS(block, ROW, 0, "Continue",
2516                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y5,(mpref/3),buth,
2517                         &(U.viewzoom), 40, USER_ZOOM_CONT, 0, 0,
2518                         "Old style zoom, continues while moving mouse up or down");
2519                 uiDefButS(block, ROW, 0, "Dolly",
2520                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/3)),y5,(mpref/3),buth,
2521                         &(U.viewzoom), 40, USER_ZOOM_DOLLY, 0, 0,
2522                         "Zooms in and out based on vertical mouse movement.");
2523                 uiDefButS(block, ROW, 0, "Scale",
2524                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(2*mpref/3)),y5,(mpref/3),buth,
2525                         &(U.viewzoom), 40, USER_ZOOM_SCALE, 0, 0,
2526                         "Zooms in and out like scaling the view, mouse movements relative to center.");
2527                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2528                 uiBlockEndAlign(block);
2529                 
2530                 uiDefBut(block, LABEL,0,"View rotation:",
2531                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y4label,mpref,buth,
2532                         0, 0, 0, 0, 0, "");
2533                 uiBlockBeginAlign(block);
2534                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2535                 uiDefButBitI(block, TOG, USER_TRACKBALL, B_DRAWINFO, "Trackball",
2536                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y3,(mpref/2),buth,
2537                         &(U.flag), 0, 0, 0, 0,
2538                         "Allow the view to tumble freely when orbiting with the Middle Mouse Button");
2539                 uiDefButBitI(block, TOGN, USER_TRACKBALL, B_DRAWINFO, "Turntable",
2540                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y3,(mpref/2),buth,
2541                         &(U.flag), 0, 0, 0, 0,
2542                         "Use fixed up axis for orbiting with Middle Mouse Button");
2543                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2544                 uiDefButBitI(block, TOG, USER_AUTOPERSP, B_DRAWINFO, "Auto Perspective",
2545                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y2,(mpref/2),buth,
2546                         &(U.uiflag), 0, 0, 0, 0,
2547                         "Automatically switch between orthographic and perspective when changing from top/front/side views");
2548                 uiDefButBitI(block, TOG, USER_ORBIT_SELECTION, B_DRAWINFO, "Around Active",
2549                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y2,(mpref/2),buth,
2550                         &(U.uiflag), 0, 0, 0, 0,
2551                         "Keep the active object in place when orbiting the views (Object Mode)");
2552                 uiBlockEndAlign(block);
2553
2554                 uiDefBut(block, LABEL,0,"Select with:",
2555                         (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y6label,mpref,buth,
2556                         0, 0, 0, 0, 0, "");
2557                 uiBlockBeginAlign(block);
2558                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2559                 uiDefButBitI(block, TOG, USER_LMOUSESELECT, B_DRAWINFO, "Left Mouse",
2560                         (xpos+edgsp+(3*mpref)+(4*midsp)),y5,(mpref/2),buth,
2561                         &(U.flag), 0, 0, 0, 0, "Use the Left Mouse Button for selection");
2562                 uiDefButBitI(block, TOGN, USER_LMOUSESELECT, B_DRAWINFO, "Right Mouse",
2563                         (xpos+edgsp+(3*mpref)+(4*midsp)+(mpref/2)),y5,(mpref/2),buth,
2564                         &(U.flag), 0, 0, 0, 0, "Use the Right Mouse Button for selection");
2565                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2566                 uiBlockEndAlign(block);
2567                 
2568                 
2569                 if(U.flag & USER_LMOUSESELECT) {
2570                         uiDefBut(block, LABEL,0,"Cursor with: Right Mouse",
2571                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2572                                 0, 0, 0, 0, 0, "");
2573                 } else {
2574                         uiDefBut(block, LABEL,0,"Cursor with: Left Mouse",
2575                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2576                                 0, 0, 0, 0, 0, "");
2577                 }
2578                 
2579                 /* illegal combo... */
2580                 if (U.flag & USER_LMOUSESELECT) 
2581                         U.flag &= ~USER_TWOBUTTONMOUSE;
2582                 
2583                 uiDefButBitI(block, TOG, USER_TWOBUTTONMOUSE, B_DRAWINFO, "Emulate 3 Button Mouse",
2584                         (xpos+edgsp+(3*mpref)+(4*midsp)),y3,mpref,buth,
2585                         &(U.flag), 0, 0, 0, 0,
2586                         "Emulates Middle Mouse with Alt+LeftMouse (doesnt work with Left Mouse Select option)");
2587                 
2588                         
2589                 uiDefBut(block, LABEL,0,"Middle Mouse Button:",
2590                         (xpos+(2*edgsp)+(4*mpref)+(4*midsp)),y6label,mpref,buth,
2591                         0, 0, 0, 0, 0, "");
2592                 uiBlockBeginAlign(block);
2593                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2594                 uiDefButBitI(block, TOGN, USER_VIEWMOVE, B_DRAWINFO, "Rotate View",
2595                         (xpos+edgsp+(4*mpref)+(5*midsp)),y5,(mpref/2),buth,
2596                         &(U.flag), 0, 0, 0, 0, "Default action for the Middle Mouse Button");
2597                 uiDefButBitI(block, TOG, USER_VIEWMOVE, B_DRAWINFO, "Pan View",
2598                         (xpos+edgsp+(4*mpref)+(5*midsp)+(mpref/2)),y5,(mpref/2),buth,
2599                         &(U.flag), 0, 0, 0, 0, "Default action for the Middle Mouse Button");
2600                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2601                 uiBlockEndAlign(block);
2602                         
2603                 uiDefBut(block, LABEL,0,"Mouse Wheel:",
2604                         (xpos+(2*edgsp)+(4*mpref)+(4*midsp)),y4label,mpref,buth,
2605                         0, 0, 0, 0, 0, "");
2606                 uiBlockBeginAlign(block);
2607                 uiDefButBitI(block, TOG, USER_WHEELZOOMDIR, 0, "Invert Zoom",
2608                         (xpos+edgsp+(4*mpref)+(5*midsp)),y3,spref,buth,
2609                         &(U.uiflag), 0, 0, 0, 0,
2610                         "Swap the Mouse Wheel zoom direction");
2611                 uiDefButI(block, NUM, 0, "Scroll Lines:",
2612                         (xpos+edgsp+(4*mpref)+(6*midsp)+spref-edgsp),y3,spref+edgsp,buth,
2613                         &U.wheellinescroll, 0.0, 32.0, 0, 0,
2614                         "The number of lines scrolled at a time with the mouse wheel"); 
2615                 uiBlockEndAlign(block);
2616
2617
2618                 uiDefBut(block, LABEL,0,"3D Transform Widget:",
2619                                  (xpos+(2*edgsp)+(5*mpref)+(5*midsp)),y6label,mpref,buth,
2620                                  0, 0, 0, 0, 0, "");
2621                 uiBlockBeginAlign(block);
2622                 uiDefButS(block, NUM, B_REDRCURW3D, "Size:",
2623                                          (xpos+edgsp+(5*mpref)+(6*midsp)),y5,(mpref/2),buth,
2624                                          &(U.tw_size), 2, 40, 0, 0, "Diameter of widget, in 10 pixel units");
2625                 uiDefButS(block, NUM, B_REDRCURW3D, "Handle:",
2626                                          (xpos+edgsp+(5*mpref)+(6*midsp)+(mpref/2)),y5,(mpref/2),buth,
2627                                          &(U.tw_handlesize), 2, 40, 0, 0, "Size of widget handles as percentage of widget radius");
2628                 uiDefButS(block, NUM, B_REDRCURW3D, "Hotspot:",
2629                                   (xpos+edgsp+(5*mpref)+(6*midsp)),y4,(mpref),buth,
2630                                   &(U.tw_hotspot), 4, 40, 0, 0, "Hotspot in pixels for clicking widget handles");
2631                 uiBlockEndAlign(block);
2632                 
2633                 
2634                 uiDefBut(block, LABEL,0,"Object center diameter",
2635                                  (xpos+(2*edgsp)+(5*mpref)+(5*midsp)),y3label,mpref,buth,
2636                                  0, 0, 0, 0, 0, "");
2637                 uiBlockBeginAlign(block);
2638                 uiDefButS(block, NUM, B_REDRCURW3D, "Size",
2639                                   (xpos+(2*edgsp)+(5*mpref)+(5*midsp)),y2,mpref,buth,
2640                                   &(U.obcenter_dia), 4, 10, 0, 0,
2641                                   "Diameter in Pixels for Object/Lamp center drawing");
2642                 
2643                 
2644         } else if (U.userpref == 1) { /* edit methods */
2645
2646
2647                 uiDefBut(block, LABEL,0,"Material linked to:",
2648                         xpos,y3label,mpref,buth,
2649                         0, 0, 0, 0, 0, "");
2650                 uiBlockBeginAlign(block);
2651                 uiDefButBitI(block, TOGN, USER_MAT_ON_OB, B_DRAWINFO, "ObData",
2652                         (xpos+edgsp),y2,(mpref/2),buth,
2653                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the obData block");
2654                 uiDefButBitI(block, TOG, USER_MAT_ON_OB, B_DRAWINFO, "Object",
2655                         (xpos+edgsp+(mpref/2)),y2,(mpref/2),buth,
2656                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the object block");
2657                 uiBlockEndAlign(block);
2658
2659
2660                 uiDefBut(block, LABEL,0,"Editmode undo:",
2661                         (xpos+(2*edgsp)+mpref),y3label, mpref,buth,
2662                         0, 0, 0, 0, 0, "");
2663                 uiBlockBeginAlign(block);
2664                 uiDefButS(block, NUMSLI, B_DRAWINFO, "Steps:",
2665                         (xpos+edgsp+mpref+midsp),y2,mpref,buth,
2666                         &(U.undosteps), 0, 64, 0, 0, "Number of undo steps available in Edit Mode (smaller values conserve memory)");
2667
2668                 uiDefButBitI(block, TOG, USER_GLOBALUNDO, B_DRAWINFO, "Global undo",
2669                         (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2670                         &(U.uiflag), 2, 64, 0, 0, "");
2671                 uiBlockEndAlign(block);
2672
2673
2674                 uiDefBut(block, LABEL,0,"Auto keyframe",
2675                         (xpos+(2*edgsp)+(2*mpref)+midsp),y3label,mpref,buth,
2676                         0, 0, 0, 0, 0, "");
2677                 uiDefButBitI(block, TOG, G_RECORDKEYS, REDRAWTIME, "Action and Object", 
2678                                         (xpos+edgsp+(2*mpref)+(2*midsp)),y2,mpref, buth,
2679                                          &(G.flags), 0, 0, 0, 0, "Automatic keyframe insertion in Object and Action Ipo curves");
2680
2681 //              uiDefButBitS(block, TOG, USER_KEYINSERTACT, 0, "Action",
2682 //                      (xpos+edgsp+(2*mpref)+(2*midsp)),y2,(spref+edgsp),buth,
2683 //                      &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in Action Ipo curve");
2684 //              uiDefButBitS(block, TOG, USER_KEYINSERTOBJ, 0, "Object",
2685 //                      (xpos+edgsp+(2*mpref)+(3*midsp)+spref-edgsp),y2,(spref+edgsp),buth,
2686 //                      &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in Object Ipo curve");
2687
2688
2689                 uiDefBut(block, LABEL,0,"Duplicate with object:",
2690                         (xpos+(2*edgsp)+(3*midsp)+(3*mpref)+spref),y3label,mpref,buth,
2691                         0, 0, 0, 0, 0, "");
2692
2693                 uiBlockBeginAlign(block);
2694                 uiDefButBitI(block, TOG, USER_DUP_MESH, 0, "Mesh",
2695                         (xpos+edgsp+(4*midsp)+(3*mpref)+spref),y2,(spref+edgsp),buth,
2696                         &(U.dupflag), 0, 0, 0, 0, "Causes mesh data to be duplicated with Shift+D");
2697                 uiDefButBitI(block, TOG, USER_DUP_SURF, 0, "Surface",
2698                         (xpos+edgsp+(5*midsp)+(3*mpref)+(2*spref)),y2,(spref+edgsp),buth,
2699                         &(U.dupflag), 0, 0, 0, 0, "Causes surface data to be duplicated with Shift+D");
2700                 uiDefButBitI(block, TOG, USER_DUP_CURVE, 0, "Curve",
2701                         (xpos+edgsp+(6*midsp)+(3*mpref)+(3*spref)),y2,(spref+edgsp),buth,
2702                         &(U.dupflag), 0, 0, 0, 0, "Causes curve data to be duplicated with Shift+D");
2703                 uiDefButBitI(block, TOG, USER_DUP_FONT, 0, "Text",
2704                         (xpos+edgsp+(7*midsp)+(3*mpref)+(4*spref)),y2,(spref+edgsp),buth,
2705                         &(U.dupflag), 0, 0, 0, 0, "Causes text data to be duplicated with Shift+D");
2706                 uiDefButBitI(block, TOG, USER_DUP_MBALL, 0, "Metaball",
2707                         (xpos+edgsp+(8*midsp)+(3*mpref)+(5*spref)),y2,(spref+edgsp),buth,
2708                         &(U.dupflag), 0, 0, 0, 0, "Causes metaball data to be duplicated with Shift+D");
2709
2710                 uiDefButBitI(block, TOG, USER_DUP_ARM, 0, "Armature",
2711                         (xpos+edgsp+(4*midsp)+(3*mpref)+spref),y1,(spref+edgsp),buth,
2712                         &(U.dupflag), 0, 0, 0, 0, "Causes armature data to be duplicated with Shift+D");
2713                 uiDefButBitI(block, TOG, USER_DUP_LAMP, 0, "Lamp",
2714                         (xpos+edgsp+(5*midsp)+(3*mpref)+(2*spref)),y1,(spref+edgsp),buth,
2715                         &(U.dupflag), 0, 0, 0, 0, "Causes lamp data to be duplicated with Shift+D");
2716                 uiDefButBitI(block, TOG, USER_DUP_MAT, 0, "Material",
2717                         (xpos+edgsp+(6*midsp)+(3*mpref)+(3*spref)),y1,(spref+edgsp),buth,
2718                         &(U.dupflag), 0, 0, 0, 0, "Causes material data to be duplicated with Shift+D");
2719                 uiDefButBitI(block, TOG, USER_DUP_TEX, 0, "Texture",
2720                         (xpos+edgsp+(7*midsp)+(3*mpref)+(4*spref)),y1,(spref+edgsp),buth,
2721                         &(U.dupflag), 0, 0, 0, 0, "Causes texture data to be duplicated with Shift+D");
2722                 uiDefButBitI(block, TOG, USER_DUP_IPO, 0, "Ipo",
2723                         (xpos+edgsp+(8*midsp)+(3*mpref)+(5*spref)),y1,(spref+edgsp),buth,
2724                         &(U.dupflag), 0, 0, 0, 0, "Causes ipo data to be duplicated with Shift+D");
2725                 uiBlockEndAlign(block);
2726         
2727         } else if(U.userpref == 2) { /* language & colors */
2728
2729 #ifdef INTERNATIONAL
2730                 uiDefButBitS(block, TOG, USER_DOTRANSLATE, B_DOLANGUIFONT, "International Fonts",
2731                         xpos,y2,mpref,buth,