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