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