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