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