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