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