Result of 2 weeks of quiet coding work in Greece :)
[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_depsgraph.h"
74 #include "BKE_displist.h"
75 #include "BKE_global.h"
76 #include "BKE_ipo.h"
77 #include "BKE_main.h"
78 #include "BKE_scene.h"
79 #include "BKE_utildefines.h"
80
81 #include "BIF_spacetypes.h"  // first, nasty dependency with typedef
82
83 #include "BIF_butspace.h"
84 #include "BIF_drawimage.h"
85 #include "BIF_drawseq.h"
86 #include "BIF_drawtext.h"
87 #include "BIF_drawscript.h"
88 #include "BIF_editarmature.h"
89 #include "BIF_editfont.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                 else if (G.obedit->type==OB_ARMATURE)
647                         undo_push_armature(str);
648         }
649         else {
650                 if(U.uiflag & USER_GLOBALUNDO) 
651                         BKE_write_undo(str);
652         }
653 }
654
655 void BIF_undo(void)
656 {       
657         if(G.obedit) {
658                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
659                         undo_editmode_step(1);
660         }
661         else {
662                 if(G.f & G_WEIGHTPAINT)
663                         wpaint_undo();
664                 else if(G.f & G_VERTEXPAINT)
665                         vpaint_undo();
666                 else {
667                         /* now also in faceselect mode */
668                         if(U.uiflag & USER_GLOBALUNDO) BKE_undo_step(1);
669                 }
670         }
671 }
672
673 void BIF_redo(void)
674 {
675         if(G.obedit) {
676                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
677                         undo_editmode_step(-1);
678         }
679         else {
680                 if(G.f & G_WEIGHTPAINT)
681                         wpaint_undo();
682                 else if(G.f & G_VERTEXPAINT)
683                         vpaint_undo();
684                 else {
685                         /* includes faceselect now */
686                         if(U.uiflag & USER_GLOBALUNDO) BKE_undo_step(-1);
687                 }
688         }
689 }
690
691 void BIF_undo_menu(void)
692 {
693         if(G.obedit) {
694                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
695                         undo_editmode_menu();
696                 allqueue(REDRAWALL, 0);
697         }
698         else {
699                 if(G.f & G_WEIGHTPAINT)
700                         ;
701                 else if(G.f & G_VERTEXPAINT)
702                         ;
703                 else {
704                         if(U.uiflag & USER_GLOBALUNDO) {
705                                 char *menu= BKE_undo_menu_string();
706                                 if(menu) {
707                                         short event= pupmenu_col(menu, 20);
708                                         MEM_freeN(menu);
709                                         if(event>0) BKE_undo_number(event);
710                                 }
711                         }
712                 }
713         }
714 }
715
716 /* *************** */
717
718 static void winqreadview3dspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
719 {
720         unsigned short event= evt->event;
721         short val= evt->val;
722         char ascii= evt->ascii;
723         View3D *v3d= sa->spacedata.first;
724         Object *ob;
725         float *curs;
726         int doredraw= 0, pupval;
727         
728         if(curarea->win==0) return;     /* when it comes from sa->headqread() */
729         
730         if(val) {
731
732                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
733                 if(event==MOUSEY || event==MOUSEX) return;
734                 
735                 if(event==UI_BUT_EVENT) do_butspace(val); // temporal, view3d deserves own queue?
736                 
737                 /* we consider manipulator a button, defaulting to leftmouse */
738                 if(event==LEFTMOUSE) if(BIF_do_manipulator(sa)) return;
739                 
740                 /* swap mouse buttons based on user preference */
741                 if (U.flag & USER_LMOUSESELECT) {
742                         if (event==LEFTMOUSE) event = RIGHTMOUSE;
743                         else if (event==RIGHTMOUSE) event = LEFTMOUSE;
744                 }
745
746                 /* run any view3d event handler script links */
747                 if (event && sa->scriptlink.totscript)
748                         if (BPY_do_spacehandlers(sa, event, SPACEHANDLER_VIEW3D_EVENT))
749                                 return; /* return if event was processed (swallowed) by handler(s) */
750
751                 /* TEXTEDITING?? */
752                 if((G.obedit) && G.obedit->type==OB_FONT) {
753                         switch(event) {
754                         
755                         case LEFTMOUSE:
756                                 mouse_cursor();
757                                 break;
758                         case MIDDLEMOUSE:
759                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
760                                 if(U.flag & USER_VIEWMOVE) {
761                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
762                                                 viewmove(0);
763                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
764                                                 viewmove(2);
765                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
766                                                 viewmove(1);
767                                 }
768                                 else {
769                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
770                                                 viewmove(1);
771                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
772                                                 viewmove(2);
773                                         else
774                                                 viewmove(0);
775                                 }
776                                 break;
777                                 
778                         case WHEELUPMOUSE:
779                                 /* Regular:   Zoom in */
780                                 /* Shift:     Scroll up */
781                                 /* Ctrl:      Scroll right */
782                                 /* Alt-Shift: Rotate up */
783                                 /* Alt-Ctrl:  Rotate right */
784
785                                 if( G.qual & LR_SHIFTKEY ) {
786                                         if( G.qual & LR_ALTKEY ) { 
787                                                 G.qual &= ~LR_SHIFTKEY;
788                                                 persptoetsen(PAD2);
789                                                 G.qual |= LR_SHIFTKEY;
790                                         } else {
791                                                 persptoetsen(PAD2);
792                                         }
793                                 } else if( G.qual & LR_CTRLKEY ) {
794                                         if( G.qual & LR_ALTKEY ) { 
795                                                 G.qual &= ~LR_CTRLKEY;
796                                                 persptoetsen(PAD4);
797                                                 G.qual |= LR_CTRLKEY;
798                                         } else {
799                                                 persptoetsen(PAD4);
800                                         }
801                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
802                                         persptoetsen(PADMINUS);
803                                 else
804                                         persptoetsen(PADPLUSKEY);
805
806                                 doredraw= 1;
807                                 break;
808
809                         case WHEELDOWNMOUSE:
810                                 /* Regular:   Zoom out */
811                                 /* Shift:     Scroll down */
812                                 /* Ctrl:      Scroll left */
813                                 /* Alt-Shift: Rotate down */
814                                 /* Alt-Ctrl:  Rotate left */
815
816                                 if( G.qual & LR_SHIFTKEY ) {
817                                         if( G.qual & LR_ALTKEY ) { 
818                                                 G.qual &= ~LR_SHIFTKEY;
819                                                 persptoetsen(PAD8);
820                                                 G.qual |= LR_SHIFTKEY;
821                                         } else {
822                                                 persptoetsen(PAD8);
823                                         }
824                                 } else if( G.qual & LR_CTRLKEY ) {
825                                         if( G.qual & LR_ALTKEY ) { 
826                                                 G.qual &= ~LR_CTRLKEY;
827                                                 persptoetsen(PAD6);
828                                                 G.qual |= LR_CTRLKEY;
829                                         } else {
830                                                 persptoetsen(PAD6);
831                                         }
832                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
833                                         persptoetsen(PADPLUSKEY);
834                                 else
835                                         persptoetsen(PADMINUS);
836                                 
837                                 doredraw= 1;
838                                 break;
839
840                         case UKEY:
841                                 if(G.qual==LR_ALTKEY) {
842                                         remake_editText();
843                                         doredraw= 1;
844                                 } 
845                                 else {
846                                         do_textedit(event, val, ascii);
847                                 }
848                                 break;
849                         case VKEY:
850                                 if(G.qual==LR_ALTKEY) {
851                                         paste_editText();
852                                         doredraw= 1;
853                                 } 
854                                 else {
855                                         do_textedit(event, val, ascii);
856                                 }
857                                 break;
858                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
859                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
860                         case PADENTER:
861                                 persptoetsen(event);
862                                 doredraw= 1;
863                                 break;
864                                 
865                         default:
866                                 do_textedit(event, val, ascii);
867                                 break;
868                         }
869                 }
870                 else {
871
872                         if (U.flag & USER_NONUMPAD) {
873                                 event= convert_for_nonumpad(event);
874                         }
875
876                         switch(event) {
877                         
878                         case BACKBUFDRAW:
879                                 backdrawview3d(1);
880                                 break;
881                                                 
882                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
883                          * based on user preference USER_LMOUSESELECT
884                          */
885                         case LEFTMOUSE: 
886                                 if ((G.obedit) || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT))) {
887                                         mouse_cursor();
888                                 } else if (G.f & G_VERTEXPAINT) {
889                                         vertex_paint();
890                                 }
891                                 else if (G.f & G_WEIGHTPAINT){
892                                         weight_paint();
893                                 }
894                                 else if (G.f & G_TEXTUREPAINT) {
895                                         face_draw();
896                                 }
897                                 break;
898                         case MIDDLEMOUSE:
899                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
900                                 if(U.flag & USER_VIEWMOVE) {
901                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
902                                                 viewmove(0);
903                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
904                                                 viewmove(2);
905                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
906                                                 viewmove(1);
907                                 }
908                                 else {
909                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
910                                                 viewmove(1);
911                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
912                                                 viewmove(2);
913                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
914                                                 viewmove(0);
915                                 }
916                                 break;
917                         case RIGHTMOUSE:
918                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
919                                         if(G.obedit->type==OB_MESH)
920                                                 mouse_mesh();
921                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
922                                                 mouse_nurb();
923                                         else if(G.obedit->type==OB_MBALL)
924                                                 mouse_mball();
925                                         else if(G.obedit->type==OB_LATTICE)
926                                                 mouse_lattice();
927                                         else if(G.obedit->type==OB_ARMATURE)
928                                                 mouse_armature();
929                                 }
930                                 else if((G.obedit) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
931                                         mouse_mesh();   // loop select for 1 mousebutton dudes
932                                 else if((G.obedit) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
933                                         mouse_mesh();   // loop select for 1 mousebutton dudes
934                                 else if(G.obpose) { 
935                                         if (G.obpose->type==OB_ARMATURE)
936                                                 mousepose_armature();
937                                 }
938                                 else if(G.qual==LR_CTRLKEY)
939                                         mouse_select(); // also allow in editmode, for vertex parenting
940                                 else if(G.f & G_FACESELECT)
941                                         face_select();
942                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
943                                         sample_vpaint();
944                                 else
945                                         mouse_select();
946                                 break;
947                         case WHEELUPMOUSE:
948                                 /* Regular:   Zoom in */
949                                 /* Shift:     Scroll up */
950                                 /* Ctrl:      Scroll right */
951                                 /* Alt-Shift: Rotate up */
952                                 /* Alt-Ctrl:  Rotate right */
953
954                                 if( G.qual & LR_SHIFTKEY ) {
955                                         if( G.qual & LR_ALTKEY ) { 
956                                                 G.qual &= ~LR_SHIFTKEY;
957                                                 persptoetsen(PAD2);
958                                                 G.qual |= LR_SHIFTKEY;
959                                         } else {
960                                                 persptoetsen(PAD2);
961                                         }
962                                 } else if( G.qual & LR_CTRLKEY ) {
963                                         if( G.qual & LR_ALTKEY ) { 
964                                                 G.qual &= ~LR_CTRLKEY;
965                                                 persptoetsen(PAD4);
966                                                 G.qual |= LR_CTRLKEY;
967                                         } else {
968                                                 persptoetsen(PAD4);
969                                         }
970                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
971                                         persptoetsen(PADMINUS);
972                                 else
973                                         persptoetsen(PADPLUSKEY);
974
975                                 doredraw= 1;
976                                 break;
977                         case WHEELDOWNMOUSE:
978                                 /* Regular:   Zoom out */
979                                 /* Shift:     Scroll down */
980                                 /* Ctrl:      Scroll left */
981                                 /* Alt-Shift: Rotate down */
982                                 /* Alt-Ctrl:  Rotate left */
983
984                                 if( G.qual & LR_SHIFTKEY ) {
985                                         if( G.qual & LR_ALTKEY ) { 
986                                                 G.qual &= ~LR_SHIFTKEY;
987                                                 persptoetsen(PAD8);
988                                                 G.qual |= LR_SHIFTKEY;
989                                         } else {
990                                                 persptoetsen(PAD8);
991                                         }
992                                 } else if( G.qual & LR_CTRLKEY ) {
993                                         if( G.qual & LR_ALTKEY ) { 
994                                                 G.qual &= ~LR_CTRLKEY;
995                                                 persptoetsen(PAD6);
996                                                 G.qual |= LR_CTRLKEY;
997                                         } else {
998                                                 persptoetsen(PAD6);
999                                         }
1000                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1001                                         persptoetsen(PADPLUSKEY);
1002                                 else
1003                                         persptoetsen(PADMINUS);
1004                                 
1005                                 doredraw= 1;
1006                                 break;
1007                         
1008                         case ONEKEY:
1009                                 ob= OBACT;
1010                                 if(G.qual==LR_CTRLKEY) {
1011                                         if(ob && ob->type == OB_MESH) {
1012                                                 flip_subdivison(ob, 1);
1013                                         }
1014                                 }
1015                                 else do_layer_buttons(0); 
1016                                 break;
1017                                 
1018                         case TWOKEY:
1019                                 ob= OBACT;
1020                                 if(G.qual==LR_CTRLKEY) {
1021                                         if(ob && ob->type == OB_MESH) {
1022                                                 flip_subdivison(ob, 2);
1023                                         }
1024                                 }
1025                                 else do_layer_buttons(1); 
1026                                 break;
1027                                 
1028                         case THREEKEY:
1029                                 ob= OBACT;
1030                                 if(G.qual==LR_CTRLKEY) {
1031                                         if(ob && ob->type == OB_MESH) {
1032                                                 flip_subdivison(ob, 3);
1033                                         }
1034                                 }
1035                                 else do_layer_buttons(2); 
1036                                 break;
1037                                 
1038                         case FOURKEY:
1039                                 ob= OBACT;
1040                                 if(G.qual==LR_CTRLKEY) {
1041                                         if(ob && ob->type == OB_MESH) {
1042                                                 flip_subdivison(ob, 4);
1043                                         }
1044                                 }
1045                                 else do_layer_buttons(3); 
1046                                 break;
1047                                 
1048                         case FIVEKEY:
1049                                 do_layer_buttons(4); break;
1050                         case SIXKEY:
1051                                 do_layer_buttons(5); break;
1052                         case SEVENKEY:
1053                                 do_layer_buttons(6); break;
1054                         case EIGHTKEY:
1055                                 do_layer_buttons(7); break;
1056                         case NINEKEY:
1057                                 do_layer_buttons(8); break;
1058                         case ZEROKEY:
1059                                 do_layer_buttons(9); break;
1060                         case MINUSKEY:
1061                                 do_layer_buttons(10); break;
1062                         case EQUALKEY:
1063                                 do_layer_buttons(11); break;
1064                         case ACCENTGRAVEKEY:
1065                                 do_layer_buttons(-1); break;
1066                         
1067                         case SPACEKEY:
1068                                 if(G.qual == LR_CTRLKEY) {
1069                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1070                                         if(val>0) {
1071                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1072                                                 else {
1073                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1074                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1075                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1076                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1077                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1078                                                 }
1079                                                 doredraw= 1;
1080                                         }
1081                                 }
1082                                 else if(G.qual == LR_ALTKEY) {
1083                                         val= pupmenu("Manipulator Orientation%t|Global|Local|Normal");
1084                                         if(val>0) {
1085                                                 if(val==1) v3d->twmode= V3D_MANIP_GLOBAL;
1086                                                 else if(val==2) v3d->twmode= V3D_MANIP_LOCAL;
1087                                                 else if(val==3) v3d->twmode= V3D_MANIP_NORMAL;
1088                                                 v3d->twflag |= V3D_USE_MANIPULATOR;
1089                                                 doredraw= 1;
1090                                         }
1091                                 }
1092
1093                                 break;
1094                                 
1095                         case AKEY:
1096                                 if(G.qual & LR_CTRLKEY) apply_object(); // also with shift!
1097                                 else if((G.qual==LR_SHIFTKEY)) {
1098                                         toolbox_n_add();
1099                                 }
1100                                 else {
1101                                         if(G.obedit) {
1102                                                 if(G.obedit->type==OB_MESH)
1103                                                         deselectall_mesh();
1104                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1105                                                         deselectall_nurb();
1106                                                 else if(G.obedit->type==OB_MBALL)
1107                                                         deselectall_mball();
1108                                                 else if(G.obedit->type==OB_LATTICE)
1109                                                         deselectall_Latt();
1110                                                 else if(G.obedit->type==OB_ARMATURE)
1111                                                         deselectall_armature();
1112                                         }
1113                                         else if (G.obpose){
1114                                                 switch (G.obpose->type){
1115                                                 case OB_ARMATURE:
1116                                                         deselectall_posearmature(1);
1117                                                         break;
1118                                                 }
1119                                         }
1120                                         else {
1121                                                 if(G.f & G_FACESELECT) deselectall_tface();
1122                                                 else {
1123                                                         /* by design, the center of the active object 
1124                                                          * (which need not necessarily by selected) will
1125                                                          * still be drawn as if it were selected.
1126                                                          */
1127                                                         deselectall();
1128                                                 }
1129                                         }
1130                                 }
1131                                 break;
1132                         case BKEY:
1133                                 if((G.qual==LR_SHIFTKEY))
1134                                         set_render_border();
1135                                 else if((G.qual==LR_ALTKEY)){
1136                                         if(G.obedit && G.obedit->type==OB_MESH) {
1137                                                 /* Loop Select Operations */
1138                                                 /*   Vertexloop */
1139                                                 /*   Faceloop   */                                              
1140                                                 vertex_loop_select(); 
1141                                         }
1142                                 }
1143                                 else if((G.qual==0))
1144                                         borderselect();
1145                                 break;
1146                         case CKEY:
1147                                 if(G.qual==LR_CTRLKEY) {
1148                                         copy_attr_menu();
1149                                 }
1150                                 else if(G.qual==LR_ALTKEY) {
1151                                         convertmenu();  /* editobject.c */
1152                                 }
1153                                 else if((G.qual==LR_SHIFTKEY)) {
1154                                         view3d_home(1);
1155                                         curs= give_cursor();
1156                                         curs[0]=curs[1]=curs[2]= 0.0;
1157                                         allqueue(REDRAWVIEW3D, 0);
1158                                 }
1159                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1160                                         makecyclicNurb();
1161                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1162                                         allqueue(REDRAWVIEW3D, 0);
1163                                 }
1164                                 else if((G.qual==0)){
1165                                         curs= give_cursor();
1166                                         G.vd->ofs[0]= -curs[0];
1167                                         G.vd->ofs[1]= -curs[1];
1168                                         G.vd->ofs[2]= -curs[2];
1169                                         scrarea_queue_winredraw(curarea);
1170                                 }
1171                         
1172                                 break;
1173                         case DKEY:
1174                                 if((G.qual==LR_SHIFTKEY)) {
1175                                         duplicate_context_selected();
1176                                 }
1177                                 else if(G.qual==LR_ALTKEY) {
1178                                         if(G.obpose)
1179                                                 error ("Duplicate not possible in posemode.");
1180                                         else if((G.obedit==0))
1181                                                 adduplicate(0);
1182                                 }
1183                                 else if(G.qual==LR_CTRLKEY) {
1184                                         imagestodisplist();
1185                                 }
1186                                 else if((G.qual==0)){
1187                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
1188                                         if(pupval>0) {
1189                                                 G.vd->drawtype= pupval;
1190                                                 doredraw= 1;
1191                                         
1192                                         }
1193                                 }
1194                                 
1195                                 break;
1196                         case EKEY:
1197                                 if (G.qual==0){
1198                                         if(G.obedit) {
1199                                                 if(G.obedit->type==OB_MESH)
1200                                                         extrude_mesh();
1201                                                 else if(G.obedit->type==OB_CURVE)
1202                                                         addvert_Nurb('e');
1203                                                 else if(G.obedit->type==OB_SURF)
1204                                                         extrude_nurb();
1205                                                 else if(G.obedit->type==OB_ARMATURE)
1206                                                         extrude_armature();
1207                                         }
1208                                 }
1209                                 else if (G.qual==LR_CTRLKEY) {
1210                                         if(G.obedit && G.obedit->type==OB_MESH)
1211                                                 Edge_Menu();
1212                                 }
1213                                 else if (G.qual==LR_SHIFTKEY) {
1214                                         if (G.obedit && G.obedit->type==OB_MESH) {
1215                                                 initTransform(TFM_CREASE, CTX_EDGE);
1216                                                 Transform();
1217                                         }
1218                                 }
1219                                 break;
1220                         case FKEY:
1221                                 if(G.obedit) {
1222                                         if(G.obedit->type==OB_MESH) {
1223                                                 if((G.qual==LR_SHIFTKEY))
1224                                                         fill_mesh();
1225                                                 else if(G.qual==LR_ALTKEY)
1226                                                         beauty_fill();
1227                                                 else if(G.qual==LR_CTRLKEY)
1228                                                         edge_flip();
1229                                                 else if (G.qual==0)
1230                                                         addedgeface_mesh();
1231                                         }
1232                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
1233                                 }
1234                                 else if(G.qual==LR_CTRLKEY)
1235                                         sort_faces();
1236                                 else if((G.qual==LR_SHIFTKEY))
1237                                         fly();
1238                                 else {
1239                                                 set_faceselect();
1240                                         }
1241                                 
1242                                 break;
1243                         case GKEY:
1244                                 /* RMGRP if(G.qual & LR_CTRLKEY) add_selected_to_group();
1245                                 else if(G.qual & LR_ALTKEY) rem_selected_from_group(); */
1246                                 if((G.qual==LR_SHIFTKEY))
1247                                         select_group_menu();
1248                                 else if(G.qual==LR_ALTKEY) {
1249                                         if(okee("Clear location")) {
1250                                                 clear_object('g');
1251                                         }
1252                                 }
1253                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1254                                         v3d->twtype= V3D_MANIP_TRANSLATE;
1255                                         doredraw= 1;
1256                                 }
1257                                 else if((G.qual==0)) {
1258                                         initTransform(TFM_TRANSLATION, CTX_NONE);
1259                                         Transform();
1260                                 }
1261                                 break;
1262                         case HKEY:
1263                                 if(G.obedit) {
1264                                         if(G.obedit->type==OB_MESH) {
1265                                                 if(G.qual==LR_CTRLKEY)
1266                                                         add_hook();
1267                                                 else if(G.qual==LR_ALTKEY)
1268                                                         reveal_mesh();
1269                                                 else if((G.qual==LR_SHIFTKEY))
1270                                                         hide_mesh(1);
1271                                                 else if((G.qual==0)) 
1272                                                         hide_mesh(0);
1273                                         }
1274                                         else if(G.obedit->type== OB_SURF) {
1275                                                 if(G.qual==LR_CTRLKEY)
1276                                                         add_hook();
1277                                                 else if(G.qual==LR_ALTKEY)
1278                                                         revealNurb();
1279                                                 else if((G.qual==LR_SHIFTKEY))
1280                                                         hideNurb(1);
1281                                                 else if((G.qual==0))
1282                                                         hideNurb(0);
1283                                         }
1284                                         else if(G.obedit->type==OB_CURVE) {
1285                                                 if(G.qual==LR_CTRLKEY)
1286                                                         add_hook();
1287                                                 else {
1288                                                         if(G.qual==LR_CTRLKEY)
1289                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
1290                                                         else if((G.qual==LR_SHIFTKEY))
1291                                                                 sethandlesNurb(1);
1292                                                         else if((G.qual==0))
1293                                                                 sethandlesNurb(3);
1294                                                         
1295                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1296                                                         BIF_undo_push("Handle change");
1297                                                         allqueue(REDRAWVIEW3D, 0);
1298                                                 }
1299                                         }
1300                                         else if(G.obedit->type==OB_LATTICE) {
1301                                                 if(G.qual==LR_CTRLKEY) add_hook();
1302                                         }
1303                                         else if(G.obedit->type==OB_MBALL) {
1304                                                 if(G.qual==LR_ALTKEY)
1305                                                         reveal_mball();
1306                                                 else if((G.qual==LR_SHIFTKEY))
1307                                                         hide_mball(1);
1308                                                 else if((G.qual==0)) 
1309                                                         hide_mball(0);
1310                                         }
1311                                 }
1312                                 else if(G.f & G_FACESELECT)
1313                                         hide_tface();
1314                                 else if(G.obpose) {
1315                                         if (G.qual==0)
1316                                                 hide_selected_pose_bones();
1317                                         else if (G.qual==LR_SHIFTKEY)
1318                                                 hide_unselected_pose_bones();
1319                                         else if (G.qual==LR_ALTKEY)
1320                                                 show_all_pose_bones();
1321                                 }
1322                                 break;
1323                         case IKEY:
1324                                 break;
1325                                 
1326                         case JKEY:
1327                                 if(G.qual==LR_CTRLKEY) {
1328                                         if( (ob= OBACT) ) {
1329                                                 if(ob->type == OB_MESH)
1330                                                         join_mesh();
1331                                                 else if(ob->type == OB_CURVE)
1332                                                         join_curve(OB_CURVE);
1333                                                 else if(ob->type == OB_SURF)
1334                                                         join_curve(OB_SURF);
1335                                                 else if(ob->type == OB_ARMATURE)
1336                                                         join_armature ();
1337                                         }
1338                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1339                                                 addsegment_nurb();
1340                                 }
1341                                 else if(G.obedit) {
1342                                         if(G.obedit->type==OB_MESH) {
1343                                                 join_triangles();
1344                                         }
1345                                 }
1346
1347                                 break;
1348                         case KKEY:
1349                                 if(G.obedit) {
1350                                         if (G.obedit->type==OB_MESH) {
1351                                                 if (G.qual==LR_SHIFTKEY)
1352                                                         KnifeSubdivide(KNIFE_PROMPT);
1353                                                 else if (G.qual==0)
1354                                                         LoopMenu();
1355                                         }
1356                                         else if(G.obedit->type==OB_SURF)
1357                                                 printknots();
1358                                 }
1359                                 else {
1360                                         if((G.qual==LR_SHIFTKEY)) {
1361                                                 if(G.f & G_FACESELECT)
1362                                                         clear_vpaint_selectedfaces();
1363                                                 else if(G.f & G_VERTEXPAINT)
1364                                                         clear_vpaint();
1365                                                 else
1366                                                         select_select_keys();
1367                                         }
1368                                         else if (G.qual==0)
1369                                                 set_ob_ipoflags();
1370                                 }
1371                                 
1372                                 break;
1373                         case LKEY:
1374                                 if(G.obedit) {
1375                                         if(G.obedit->type==OB_MESH)
1376                                                 selectconnected_mesh(G.qual);
1377                                         if(G.obedit->type==OB_ARMATURE)
1378                                                 selectconnected_armature();
1379                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1380                                                 selectconnected_nurb();
1381                                 }
1382                                 else if(G.obpose) {
1383                                         if(G.obpose->type==OB_ARMATURE)
1384                                                 selectconnected_posearmature();
1385                                 }
1386                                 else {
1387                                         if(G.f & G_FACESELECT) {
1388                                                 if((G.qual==0))
1389                                                         select_linked_tfaces(0);
1390                                                 else if((G.qual==LR_SHIFTKEY))
1391                                                         select_linked_tfaces(1);
1392                                                 else if(G.qual==LR_CTRLKEY)
1393                                                         select_linked_tfaces(2);
1394                                         }
1395                                         else {
1396                                                 if((G.qual==0))
1397                                                         make_local();
1398                                                 else if((G.qual==LR_SHIFTKEY))
1399                                                         selectlinks_menu();
1400                                                 else if(G.qual==LR_CTRLKEY)
1401                                                         make_links_menu();
1402                                         }
1403                                 }
1404                                 break;
1405                         case MKEY:
1406                                 if(G.obedit){
1407                                         if(G.qual==LR_ALTKEY) {
1408                                                 if(G.obedit->type==OB_MESH) {
1409                                                         mergemenu();
1410                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1411                                                 }
1412                                         }
1413                                         else if((G.qual==0) || (G.qual==LR_CTRLKEY)) {
1414                                                 mirrormenu();
1415                                         }
1416                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1417                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
1418                                         }
1419                                 }
1420                                 else if(G.qual & LR_CTRLKEY) {
1421                                         mirrormenu();
1422                                 }
1423                                 else if(G.qual==0) {
1424                                      movetolayer();
1425                                 }
1426                                 break;
1427                         case NKEY:
1428                                 if((G.qual==0)) {
1429                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1430                                         allqueue(REDRAWVIEW3D, 0);
1431                                 }
1432                                 else if(G.obedit) {
1433                                         switch (G.obedit->type){
1434                                         case OB_ARMATURE:
1435                                                 if(G.qual==LR_CTRLKEY){
1436                                                         if (okee("Recalculate bone roll angles")) {
1437                                                                 auto_align_armature();
1438                                                                 allqueue(REDRAWVIEW3D, 0);
1439                                                         }
1440                                                 }
1441                                                 break;
1442                                         case OB_MESH: 
1443                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1444                                                         if(okee("Recalculate normals inside")) {
1445                                                                 righthandfaces(2);
1446                                                                 allqueue(REDRAWVIEW3D, 0);
1447                                                                 BIF_undo_push("Recalculate normals inside");
1448                                                         }
1449                                                 }
1450                                                 else if(G.qual==LR_CTRLKEY){
1451                                                         if(okee("Recalculate normals outside")) {
1452                                                                 righthandfaces(1);
1453                                                                 allqueue(REDRAWVIEW3D, 0);
1454                                                                 BIF_undo_push("Recalculate normals outside");
1455                                                         }
1456                                                 }
1457                                                 break;
1458                                         }
1459                                 }
1460                                 
1461                                 break;
1462                         case OKEY:
1463                                 ob= OBACT;
1464                                 if (G.obedit) {
1465                                         if (G.qual==LR_SHIFTKEY) {
1466                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%6;
1467                                                 allqueue(REDRAWHEADERS, 0);
1468                                         }
1469                                         else if((G.qual==LR_ALTKEY)) {
1470                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
1471                                                 else G.scene->proportional= 2;
1472                                                 allqueue(REDRAWHEADERS, 0);
1473                                         }
1474                                         else if((G.qual==0)) {
1475                                                 G.scene->proportional= !G.scene->proportional;
1476                                                 allqueue(REDRAWHEADERS, 0);
1477                                         }
1478                                 }
1479                                 else if((G.qual==LR_SHIFTKEY)) {
1480                                         if(ob && ob->type == OB_MESH) {
1481                                                 flip_subdivison(ob, -1);
1482                                         }
1483                                 }
1484                                 else if(G.qual==LR_ALTKEY) {
1485                                         if(okee("Clear origin")) {
1486                                                 clear_object('o');
1487                                         }
1488                                 }
1489                                 break;
1490
1491                         case PKEY:
1492                                 if(G.obedit) {
1493                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1494                                                 if(G.obedit->type==OB_ARMATURE)
1495                                                         make_bone_parent();
1496                                                 else
1497                                                         make_parent();
1498                                         }
1499                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
1500                                                 clear_bone_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                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
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 ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
1641                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1642                                         }
1643                                 }
1644                                 else if((G.qual==0)) {
1645                                         if (G.f & G_FACESELECT)
1646                                                 uv_autocalc_tface();
1647                                         else if(G.f & G_WEIGHTPAINT)
1648                                                 wpaint_undo();
1649                                         else if(G.f & G_VERTEXPAINT)
1650                                                 vpaint_undo();
1651                                         else {
1652                                                 single_user();
1653                                         }
1654                                 }
1655                                         
1656                                 break;
1657                         case VKEY:
1658                                 ob= OBACT;
1659                                 if((G.qual==LR_SHIFTKEY)) {
1660                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
1661                                                 align_view_to_selected(v3d);
1662                                         }
1663                                         else if (G.f & G_FACESELECT) {
1664                                                 align_view_to_selected(v3d);
1665                                         }
1666                                 }
1667                                 else if(G.qual==LR_ALTKEY)
1668                                         image_aspect();
1669                                 else if (G.qual==0){
1670                                         if(G.obedit) {
1671                                                 if(G.obedit->type==OB_CURVE) {
1672                                                         sethandlesNurb(2);
1673                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1674                                                         allqueue(REDRAWVIEW3D, 0);
1675                                                         BIF_undo_push("Handle change");
1676                                                 }
1677                                         }
1678                                         else if(ob && ob->type == OB_MESH) 
1679                                                 set_vpaint();
1680                                 }
1681                                 break;
1682                         case WKEY:
1683                                 if((G.qual==LR_SHIFTKEY)) {
1684                                         initTransform(TFM_WARP, CTX_NONE);
1685                                         Transform();
1686                                 }
1687                                 else if(G.qual==LR_ALTKEY) {
1688                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
1689                                 }
1690                                 else if(G.qual==LR_CTRLKEY) {
1691                                         if(G.obedit) {
1692                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
1693                                                         switchdirectionNurb2();
1694                                                 }
1695                                         }
1696                                 }
1697                                 else if((G.qual==0))
1698                                         special_editmenu();
1699                                 
1700                                 break;
1701                         case XKEY:
1702                         case DELKEY:
1703                                 if(G.qual==0)
1704                                         delete_context_selected();
1705                                 break;
1706                         case YKEY:
1707                                 if((G.qual==0) && (G.obedit)) {
1708                                         if(G.obedit->type==OB_MESH) split_mesh();
1709                                 }
1710                                 break;
1711                         case ZKEY:
1712                                 toggle_shading();
1713                                 
1714                                 scrarea_queue_headredraw(curarea);
1715                                 scrarea_queue_winredraw(curarea);
1716                                 break;
1717                         
1718                         case HOMEKEY:
1719                                 if(G.qual==0)
1720                                         view3d_home(0);
1721                                 break;
1722                         case COMMAKEY:
1723                                 if(G.qual==LR_CTRLKEY) {
1724                                         G.vd->around= V3D_CENTROID;
1725                                 } else if(G.qual==LR_SHIFTKEY) {
1726                                         G.vd->around= V3D_CENTROID;
1727                                 } else if(G.qual==0) {
1728                                         G.vd->around= V3D_CENTRE;
1729                                 }
1730                                 handle_view3d_around();
1731                                 
1732                                 scrarea_queue_headredraw(curarea);
1733                                 scrarea_queue_winredraw(curarea);
1734                                 break;
1735                                 
1736                         case PERIODKEY:
1737                                 if(G.qual==LR_CTRLKEY) {
1738                                         G.vd->around= V3D_LOCAL;
1739                                 }       else if(G.qual==0) {
1740                                         G.vd->around= V3D_CURSOR;
1741                                 }
1742                                 handle_view3d_around();
1743                                 
1744                                 scrarea_queue_headredraw(curarea);
1745                                 scrarea_queue_winredraw(curarea);
1746                                 break;
1747                         
1748                         case PADSLASHKEY:
1749                                 if(G.qual==0) {
1750                                         if(G.vd->localview) {
1751                                                 G.vd->localview= 0;
1752                                                 endlocalview(curarea);
1753                                         }
1754                                         else {
1755                                                 G.vd->localview= 1;
1756                                                 initlocalview();
1757                                         }
1758                                         scrarea_queue_headredraw(curarea);
1759                                 }
1760                                 break;
1761                         case PADASTERKEY:       /* '*' */
1762                                 if(G.qual==0) {
1763                                         ob= OBACT;
1764                                         if(ob) {
1765                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
1766                                                         editmesh_align_view_to_selected(G.vd, 2);
1767                                                 } 
1768                                                 else if (G.f & G_FACESELECT) {
1769                                                         if(ob->type==OB_MESH) {
1770                                                                 Mesh *me= ob->data;
1771                                                                 faceselect_align_view_to_selected(G.vd, me, 2);
1772                                                         }
1773                                                 }
1774                                                 else
1775                                                         obmat_to_viewmat(ob);
1776                                                 
1777                                                 if(G.vd->persp==2) G.vd->persp= 1;
1778                                                 scrarea_queue_winredraw(curarea);
1779                                         }
1780                                 }
1781                                 break;
1782                         case PADPERIOD: /* '.' */
1783                                 if(G.qual==0)
1784                                         centreview();
1785                                 break;
1786                         
1787                         case PAGEUPKEY:
1788                                 if(G.qual==LR_CTRLKEY)
1789                                         movekey_obipo(1);
1790                                 else if((G.qual==0))
1791                                         nextkey_obipo(1);       /* in editipo.c */
1792                                 break;
1793
1794                         case PAGEDOWNKEY:
1795                                 if(G.qual==LR_CTRLKEY)
1796                                         movekey_obipo(-1);
1797                                 else if((G.qual==0))
1798                                         nextkey_obipo(-1);
1799                                 break;
1800                                 
1801                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1802                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1803                         case PADENTER:
1804                                 persptoetsen(event);
1805                                 doredraw= 1;
1806                                 break;
1807                         case PADMINUS:
1808                                 if ( (G.qual==LR_CTRLKEY)
1809                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
1810                                         select_less();
1811                                 else {
1812                                         persptoetsen(event);
1813                                         doredraw= 1;
1814                                 }
1815                                 break;
1816
1817                         case PADPLUSKEY:
1818                                 if ( (G.qual==LR_CTRLKEY)
1819                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
1820                                         select_more();
1821                                 else {
1822                                         persptoetsen(event);
1823                                         doredraw= 1;
1824                                 }
1825                                 break;
1826
1827                         case ESCKEY:
1828                                 if(G.qual==0) {
1829                                         if (G.vd->flag & V3D_DISPIMAGE) {
1830                                                 G.vd->flag &= ~V3D_DISPIMAGE;
1831                                                 doredraw= 1;
1832                                         }
1833                                 }
1834                                 break;
1835                         }
1836                 }
1837         }
1838         
1839         if(doredraw) {
1840                 scrarea_queue_winredraw(curarea);
1841                 scrarea_queue_headredraw(curarea);
1842         }
1843 }
1844
1845 static void initview3d(ScrArea *sa)
1846 {
1847         View3D *vd;
1848         
1849         vd= MEM_callocN(sizeof(View3D), "initview3d");
1850         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
1851
1852         vd->spacetype= SPACE_VIEW3D;
1853         vd->blockscale= 0.7;
1854         vd->viewquat[0]= 1.0;
1855         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0;
1856         vd->persp= 1;
1857         vd->drawtype= OB_WIRE;
1858         vd->view= 7;
1859         vd->dist= 10.0;
1860         vd->lens= 35.0f;
1861         vd->near= 0.01f;
1862         vd->far= 500.0f;
1863         vd->grid= 1.0f;
1864         vd->gridlines= 16;
1865         vd->lay= vd->layact= 1;
1866         if(G.scene) {
1867                 vd->lay= vd->layact= G.scene->lay;
1868                 vd->camera= G.scene->camera;
1869         }
1870         vd->scenelock= 1;
1871         vd->gridflag |= V3D_SHOW_X;
1872         vd->gridflag |= V3D_SHOW_Y;
1873         vd->gridflag |= V3D_SHOW_FLOOR;
1874         vd->gridflag &= ~V3D_SHOW_Z;
1875 }
1876
1877
1878 /* ******************** SPACE: IPO ********************** */
1879
1880 static void changeview2dspace(ScrArea *sa, void *spacedata)
1881 {
1882         if(G.v2d==0) return;
1883
1884         test_view2d(G.v2d, curarea->winx, curarea->winy);
1885         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1886 }
1887
1888 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1889 {
1890         extern void do_ipobuts(unsigned short event);   // drawipo.c
1891         unsigned short event= evt->event;
1892         short val= evt->val;
1893         SpaceIpo *sipo= curarea->spacedata.first;
1894         View2D *v2d= &sipo->v2d;
1895         float dx, dy;
1896         int cfra, doredraw= 0;
1897         short mval[2];
1898         short mousebut = L_MOUSE;
1899         
1900         if(sa->win==0) return;
1901
1902         if(val) {
1903                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
1904
1905                 /* swap mouse buttons based on user preference */
1906                 if (U.flag & USER_LMOUSESELECT) {
1907                         if (event == LEFTMOUSE) {
1908                                 event = RIGHTMOUSE;
1909                                 mousebut = L_MOUSE;
1910                         } else if (event == RIGHTMOUSE) {
1911                                 event = LEFTMOUSE;
1912                                 mousebut = R_MOUSE;
1913                         }
1914                 }
1915
1916                 switch(event) {
1917                 case UI_BUT_EVENT:
1918                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
1919                         if(val>0 && val < 65) do_ipowin_buts(val-1);
1920                         else do_ipobuts(val);
1921                         break;
1922                         
1923                 case LEFTMOUSE:
1924                         if( in_ipo_buttons() ) {
1925                                 do_ipo_selectbuttons();
1926                                 doredraw= 1;
1927                         }
1928                         else if(view2dmove(LEFTMOUSE)); // only checks for sliders
1929                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
1930                         else {
1931                                 do {
1932                                         getmouseco_areawin(mval);
1933                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
1934                                         
1935                                         cfra= (int)dx;
1936                                         if(cfra< 1) cfra= 1;
1937                                         
1938                                         if( cfra!=CFRA ) {
1939                                                 CFRA= cfra;
1940                                                 update_for_newframe();
1941                                                 force_draw_all(0); /* To make constraint sliders redraw */
1942                                         }
1943                                         else PIL_sleep_ms(30);
1944                                 
1945                                 } while(get_mbut() & mousebut);
1946                         }
1947                         break;
1948                 case RIGHTMOUSE:
1949                         mouse_select_ipo();
1950                         allqueue (REDRAWACTION, 0);
1951                         allqueue(REDRAWNLA, 0);
1952                         break;
1953                 case MIDDLEMOUSE:
1954                         if(in_ipo_buttons()) {
1955                                 scroll_ipobuts();
1956                         }
1957                         else view2dmove(event); /* in drawipo.c */
1958                         break;
1959                 case WHEELUPMOUSE:
1960                 case WHEELDOWNMOUSE:
1961                         view2dmove(event);      /* in drawipo.c */
1962                         break;
1963                 case PADPLUSKEY:
1964                         view2d_zoom(v2d, 0.1154, sa->winx, sa->winy);
1965                         doredraw= 1;
1966                         break;
1967                 case PADMINUS:
1968                         view2d_zoom(v2d, -0.15, sa->winx, sa->winy);
1969                         doredraw= 1;
1970                         break;
1971                 case PAGEUPKEY:
1972                         if(G.qual==LR_CTRLKEY)
1973                                 movekey_ipo(1);
1974                         else if((G.qual==0))
1975                                 nextkey_ipo(1);
1976                         break;
1977                 case PAGEDOWNKEY:
1978                         if(G.qual==LR_CTRLKEY)
1979                                 movekey_ipo(-1);
1980                         else if((G.qual==0))
1981                                 nextkey_ipo(-1);
1982                         break;
1983                 case HOMEKEY:
1984                         if((G.qual==0))
1985                                 do_ipo_buttons(B_IPOHOME);
1986                         break;
1987                         
1988                 case AKEY:
1989                         if((G.qual==0)) {
1990                                 if(in_ipo_buttons()) {
1991                                         swap_visible_editipo();
1992                                 }
1993                                 else {
1994                                         swap_selectall_editipo();
1995                                 }
1996                                 allspace (REMAKEIPO, 0);
1997                                 allqueue (REDRAWNLA, 0);
1998                                 allqueue (REDRAWACTION, 0);
1999                         }
2000                         break;
2001                 case BKEY:
2002                         if((G.qual==0))
2003                                 borderselect_ipo();
2004                         break;
2005                 case CKEY:
2006                         if((G.qual==0))
2007                                 move_to_frame();
2008                         break;
2009                 case DKEY:
2010                         if((G.qual==LR_SHIFTKEY))
2011                                 add_duplicate_editipo();
2012                         break;
2013                 case GKEY:
2014                         if((G.qual==0))
2015                                 transform_ipo('g');
2016                         break;
2017                 case HKEY:
2018                         if((G.qual==LR_SHIFTKEY))
2019                                 sethandles_ipo(HD_AUTO);
2020                         else if((G.qual==0))
2021                                 sethandles_ipo(HD_ALIGN);
2022                         break;
2023                 case JKEY:
2024                         if((G.qual==0))
2025                                 join_ipo_menu();
2026                         break;
2027                 case KKEY:
2028                         if((G.qual==0)) {
2029                                 ipo_toggle_showkey();
2030                                 scrarea_queue_headredraw(curarea);
2031                                 allqueue(REDRAWVIEW3D, 0);
2032                                 doredraw= 1;
2033                         }
2034                         break;
2035                 case NKEY:
2036                         toggle_blockhandler(sa, IPO_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2037                         doredraw= 1;
2038                         break;
2039                 case RKEY:
2040                         if((G.qual==0))
2041                                 ipo_record();
2042                         break;
2043                 case SKEY:
2044                         if((G.qual==LR_SHIFTKEY)) {             
2045                                 ipo_snap_menu();
2046                         } else if((G.qual==0))
2047                                 transform_ipo('s');
2048                         break;
2049                 case TKEY:
2050                         if((G.qual==0))
2051                                 set_ipotype();
2052                         break;
2053                 case VKEY:
2054                         if((G.qual==0))
2055                                 sethandles_ipo(HD_VECT);
2056                         break;
2057                 case XKEY:
2058                 case DELKEY:
2059                         if((G.qual==LR_SHIFTKEY))
2060                                 delete_key();
2061                         else if((G.qual==0))
2062                                 del_ipo();
2063                         break;
2064                 }
2065         }
2066
2067         if(doredraw) scrarea_queue_winredraw(sa);
2068 }
2069
2070 void initipo(ScrArea *sa)
2071 {
2072         SpaceIpo *sipo;
2073         
2074         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
2075         BLI_addhead(&sa->spacedata, sipo);
2076
2077         sipo->spacetype= SPACE_IPO;
2078         sipo->blockscale= 0.7;
2079         
2080         /* sipo space loopt van (0,-?) tot (??,?) */
2081         sipo->v2d.tot.xmin= 0.0;
2082         sipo->v2d.tot.ymin= -10.0;
2083         sipo->v2d.tot.xmax= G.scene->r.efra;
2084         sipo->v2d.tot.ymax= 10.0;
2085
2086         sipo->v2d.cur= sipo->v2d.tot;
2087
2088         sipo->v2d.min[0]= 0.01f;
2089         sipo->v2d.min[1]= 0.01f;
2090
2091         sipo->v2d.max[0]= 15000.0f;
2092         sipo->v2d.max[1]= 10000.0f;
2093         
2094         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
2095         sipo->v2d.keeptot= 0;
2096
2097         sipo->blocktype= ID_OB;
2098 }
2099
2100 /* ******************** SPACE: INFO ********************** */
2101
2102 void space_mipmap_button_function(int event) {
2103         set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
2104
2105         allqueue(REDRAWVIEW3D, 0);
2106 }
2107
2108 #if 0
2109 static void space_sound_button_function(int event)
2110 {
2111         int a;
2112         SYS_SystemHandle syshandle;
2113
2114         if ((syshandle = SYS_GetSystem()))
2115         {
2116                 a = (U.gameflags & USER_DISABLE_SOUND);
2117                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
2118         }
2119 }
2120 #endif
2121
2122 #define B_ADD_THEME     3301
2123 #define B_DEL_THEME     3302
2124 #define B_NAME_THEME    3303
2125 #define B_THEMECOL              3304
2126 #define B_UPDATE_THEME  3305
2127 #define B_CHANGE_THEME  3306
2128 #define B_THEME_COPY    3307
2129 #define B_THEME_PASTE   3308
2130
2131 #define B_RECALCLIGHT   3310
2132
2133 // needed for event; choose new 'curmain' resets it...
2134 static short th_curcol= TH_BACK;
2135 static char *th_curcol_ptr= NULL;
2136 static char th_curcol_arr[4]={0, 0, 0, 255};
2137
2138 static void info_user_themebuts(uiBlock *block, short y1, short y2, short y3)
2139 {
2140         bTheme *btheme, *bt;
2141         int spacetype= 0;
2142         static short cur=1, curmain=2;
2143         short a, tot=0, isbuiltin= 0;
2144         char string[21*32], *strp, *col;
2145         
2146         y3= y2+23;      // exception!
2147         
2148         /* count total, max 16! */
2149         for(bt= U.themes.first; bt; bt= bt->next) tot++;
2150         
2151         /* if cur not is 1; move that to front of list */
2152         if(cur!=1) {
2153                 a= 1;
2154                 for(bt= U.themes.first; bt; bt= bt->next, a++) {
2155                         if(a==cur) {
2156                                 BLI_remlink(&U.themes, bt);
2157                                 BLI_addhead(&U.themes, bt);
2158                                 allqueue(REDRAWALL, 0);
2159                                 cur= 1;
2160                                 break;
2161                         }
2162                 }
2163         }
2164         
2165         /* the current theme */
2166         btheme= U.themes.first;
2167         if(strcmp(btheme->name, "Default")==0) isbuiltin= 1;
2168
2169         /* construct popup script */
2170         string[0]= 0;
2171         for(bt= U.themes.first; bt; bt= bt->next) {
2172                 strcat(string, bt->name);
2173                 if(btheme->next) strcat(string, "   |");
2174         }
2175         uiDefButS(block, MENU, B_UPDATE_THEME, string,                  45,y3,200,20, &cur, 0, 0, 0, 0, "Current theme");
2176         
2177         /* add / delete / name */
2178
2179         if(tot<16)
2180                 uiDefBut(block, BUT, B_ADD_THEME, "Add",        45,y2,200,20, NULL, 0, 0, 0, 0, "Makes new copy of this theme");
2181         if(tot>1 && isbuiltin==0)
2182                 uiDefBut(block, BUT, B_DEL_THEME, "Delete", 45,y1,200,20, NULL, 0, 0, 0, 0, "Delete theme");
2183
2184         if(isbuiltin) return;
2185         
2186         /* name */
2187         uiDefBut(block, TEX, B_NAME_THEME, "",                  255,y3,200,20, btheme->name, 1.0, 30.0, 0, 0, "Rename theme");
2188
2189         /* main choices pup */
2190         uiDefButS(block, MENU, B_CHANGE_THEME, "UI and Buttons %x1|%l|3D View %x2|%l|Ipo Curve Editor %x3|Action Editor %x4|"
2191                 "NLA Editor %x5|%l|UV/Image Editor %x6|Video Sequence Editor %x7|Timeline %x15|Audio Window %x8|Text Editor %x9|%l|User Preferences %x10|"
2192                 "Outliner %x11|Buttons Window %x12|%l|File Browser %x13|Image Browser %x14",
2193                                                                                                         255,y2,200,20, &curmain, 0, 0, 0, 0, "Specify theme for...");
2194         if(curmain==1) spacetype= 0;
2195         else if(curmain==2) spacetype= SPACE_VIEW3D;
2196         else if(curmain==3) spacetype= SPACE_IPO;
2197         else if(curmain==4) spacetype= SPACE_ACTION;
2198         else if(curmain==5) spacetype= SPACE_NLA;
2199         else if(curmain==6) spacetype= SPACE_IMAGE;
2200         else if(curmain==7) spacetype= SPACE_SEQ;
2201         else if(curmain==8) spacetype= SPACE_SOUND;
2202         else if(curmain==9) spacetype= SPACE_TEXT;
2203         else if(curmain==10) spacetype= SPACE_INFO;
2204         else if(curmain==11) spacetype= SPACE_OOPS;
2205         else if(curmain==12) spacetype= SPACE_BUTS;
2206         else if(curmain==13) spacetype= SPACE_FILE;
2207         else if(curmain==14) spacetype= SPACE_IMASEL;
2208         else if(curmain==15) spacetype= SPACE_TIME;
2209         else return; // only needed while coding... when adding themes for more windows
2210         
2211         /* color choices pup */
2212         if(curmain==1) {
2213                 strp= BIF_ThemeColorsPup(0);
2214                 if(th_curcol==TH_BACK) th_curcol= TH_BUT_OUTLINE;  // switching main choices...
2215         }
2216         else strp= BIF_ThemeColorsPup(spacetype);
2217         
2218         uiDefButS(block, MENU, B_REDR, strp,                    255,y1,200,20, &th_curcol, 0, 0, 0, 0, "Current color");
2219         MEM_freeN(strp);
2220         
2221         th_curcol_ptr= col= BIF_ThemeGetColorPtr(btheme, spacetype, th_curcol);
2222         if(col==NULL) return;
2223         
2224         /* first handle exceptions, special single values, row selection, etc */
2225         if(th_curcol==TH_VERTEX_SIZE) {
2226                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Vertex size ", 465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2227         }
2228         else if(th_curcol==TH_FACEDOT_SIZE) {
2229                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Face dot size ",       465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2230         }
2231         else if(th_curcol==TH_BUT_DRAWTYPE) {
2232                 uiBlockBeginAlign(block);
2233                 uiDefButC(block, ROW, B_UPDATE_THEME, "Minimal",        465,y3,100,20,  col, 2.0, 0.0, 0, 0, "");
2234                 uiDefButC(block, ROW, B_UPDATE_THEME, "Shaded", 565,y3,100,20,  col, 2.0, 1.0, 0, 0, "");
2235                 uiDefButC(block, ROW, B_UPDATE_THEME, "Rounded",        465,y2,100,20,  col, 2.0, 2.0, 0, 0, "");
2236                 uiDefButC(block, ROW, B_UPDATE_THEME, "OldSkool",       565,y2,100,20,  col, 2.0, 3.0, 0, 0, "");
2237                 uiBlockEndAlign(block);
2238         }
2239         else {
2240                 uiBlockBeginAlign(block);
2241                 if ELEM6(th_curcol, TH_PANEL, TH_FACE, TH_FACE_SELECT, TH_MENU_BACK, TH_MENU_HILITE, TH_MENU_ITEM) {
2242                         uiDefButC(block, NUMSLI, B_UPDATE_THEME,"A ",   465,y3+25,200,20,  col+3, 0.0, 255.0, B_THEMECOL, 0, "");
2243                 }
2244                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"R ",   465,y3,200,20,  col, 0.0, 255.0, B_THEMECOL, 0, "");
2245                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"G ",   465,y2,200,20,  col+1, 0.0, 255.0, B_THEMECOL, 0, "");
2246                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"B ",   465,y1,200,20,  col+2, 0.0, 255.0, B_THEMECOL, 0, "");
2247                 uiBlockEndAlign(block);
2248
2249                 uiDefButC(block, COL, B_UPDATE_THEME, "",               675,y1,50,y3-y1+20, col, 0, 0, 0, 0, "");
2250                                 
2251                 /* copy paste */
2252                 uiBlockBeginAlign(block);
2253                 uiDefBut(block, BUT, B_THEME_COPY, "Copy Color",        755,y2,120,20, NULL, 0, 0, 0, 0, "Stores current color in buffer");
2254                 uiDefBut(block, BUT, B_THEME_PASTE, "Paste Color",      755,y1,120,20, NULL, 0, 0, 0, 0, "Pastes buffer color");
2255                 uiBlockEndAlign(block);
2256                 
2257                 uiDefButC(block, COL, 0, "",                            885,y1,50,y2-y1+20, th_curcol_arr, 0, 0, 0, 0, "");
2258                 
2259         }
2260 }
2261
2262
2263 void drawinfospace(ScrArea *sa, void *spacedata)
2264 {
2265         uiBlock *block;
2266         static short cur_light=0, cur_light_var=0;
2267         float fac, col[3];
2268         short xpos, ypos, ypostab,  buth, rspace, dx, y1, y2, y3, y4, y5, y6;
2269         short y2label, y3label, y4label, y5label, y6label;
2270         short spref, mpref, lpref, smfileselbut;
2271         short edgsp, midsp;
2272         char naam[32];
2273
2274         if(curarea->win==0) return;
2275
2276         BIF_GetThemeColor3fv(TH_BACK, col);
2277         glClearColor(col[0], col[1], col[2], 0.0);
2278         glClear(GL_COLOR_BUFFER_BIT);
2279
2280         if(curarea->winx<=1280.0) {
2281                 fac= ((float)curarea->winx)/1280.0f;
2282                 myortho2(0.375, 1280.375, 0.375, curarea->winy/fac + 0.375);
2283         }
2284         else {
2285                 myortho2(0.375, (float)curarea->winx + 0.375, 0.375, (float)curarea->winy + 0.375);
2286         }
2287         
2288         sprintf(naam, "infowin %d", curarea->win);
2289         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->win);
2290
2291
2292         /* Vars for nice grid alignment */ 
2293         dx= (1280-90)/7;        /* spacing for use in equally dividing 'tab' row */
2294
2295         xpos = 45;              /* left padding */
2296         ypos = 50;              /* bottom padding for buttons */
2297         ypostab = 10;           /* bottom padding for 'tab' row */
2298
2299         buth = 20;              /* standard button height */
2300
2301         spref = 90;     /* standard size for small preferences button */
2302         mpref = 189;    /* standard size for medium preferences button */
2303         lpref = 288;    /* standard size for large preferences button */
2304         smfileselbut = buth;    /* standard size for fileselect button (square) */
2305
2306         edgsp = 3;              /* space from edge of end 'tab' to edge of end button */
2307         midsp = 9;              /* horizontal space between buttons */
2308
2309         rspace = 3;             /* default space between rows */
2310
2311         y1 = ypos;              /* grid alignment for each row of buttons */
2312         y2 = ypos+buth+rspace;
2313         y3 = ypos+2*(buth+rspace);
2314         y4 = ypos+3*(buth+rspace);
2315         y5 = ypos+4*(buth+rspace);
2316         y6 = ypos+5*(buth+rspace);
2317
2318
2319         y2label = y2-2;         /* adjustments to offset the labels down to align better */
2320         y3label = y3-2;
2321         y4label = y4-2;
2322         y5label = y5-2;
2323         y6label = y6-2;
2324
2325
2326         /* set the colour to blue and draw the main 'tab' controls */
2327
2328         uiBlockSetCol(block, TH_BUT_SETTING1);
2329         uiBlockBeginAlign(block);
2330         
2331         uiDefButS(block, ROW,B_USERPREF,"View & Controls",
2332                 xpos,ypostab,(short)dx,buth,
2333                 &U.userpref,1.0,0.0, 0, 0,"");
2334                 
2335         uiDefButS(block, ROW,B_USERPREF,"Edit Methods",
2336                 (short)(xpos+dx),ypostab,(short)dx,buth,
2337                 &U.userpref,1.0,1.0, 0, 0,"");
2338
2339         uiDefButS(block, ROW,B_USERPREF,"Language & Font",
2340                 (short)(xpos+2*dx),ypostab,(short)dx,buth,
2341                 &U.userpref,1.0,2.0, 0, 0,"");
2342
2343         uiDefButS(block, ROW,B_USERPREF,"Themes",
2344                 (short)(xpos+3*dx),ypostab,(short)dx,buth,
2345                 &U.userpref,1.0,6.0, 0, 0,"");
2346
2347         uiDefButS(block, ROW,B_USERPREF,"Auto Save",
2348                 (short)(xpos+4*dx),ypostab,(short)dx,buth,
2349                 &U.userpref,1.0,3.0, 0, 0,"");
2350
2351         uiDefButS(block, ROW,B_USERPREF,"System & OpenGL",
2352                 (short)(xpos+5*dx),ypostab,(short)dx,buth,
2353                 &U.userpref,1.0,4.0, 0, 0,"");
2354                 
2355         uiDefButS(block, ROW,B_USERPREF,"File Paths",
2356                 (short)(xpos+6*dx),ypostab,(short)dx,buth,
2357                 &U.userpref,1.0,5.0, 0, 0,"");
2358
2359         uiBlockSetCol(block, TH_AUTO);
2360         uiBlockEndAlign(block);
2361         /* end 'tab' controls */
2362
2363         /* line 2: left x co-ord, top y co-ord, width, height */
2364
2365         if(U.userpref == 6) {
2366                 info_user_themebuts(block, y1, y2, y3);
2367         }
2368         else if (U.userpref == 0) { /* view & controls */
2369
2370                 uiDefBut(block, LABEL,0,"Display:",
2371                         xpos,y6label,spref,buth,
2372                         0, 0, 0, 0, 0, "");     
2373                 uiDefButBitS(block, TOG, USER_TOOLTIPS, 0, "ToolTips",
2374                         (xpos+edgsp),y5,spref,buth,
2375                         &(U.flag), 0, 0, 0, 0,
2376                         "Display tooltips (help tags) over buttons");
2377                 uiDefButBitS(block, TOG, USER_DRAWVIEWINFO, B_DRAWINFO, "Object Info",
2378                         (xpos+edgsp),y4,spref,buth,
2379                         &(U.uiflag), 0, 0, 0, 0,
2380                         "Display active object name and frame number in the 3D View");
2381                 uiDefButBitS(block, TOG, USER_SCENEGLOBAL, 0, "Global Scene",
2382                         (xpos+edgsp),y3,spref,buth,
2383                         &(U.flag), 0, 0, 0, 0,
2384                         "Forces the current Scene to be displayed in all Screens");
2385 #ifndef __APPLE__       
2386                 uiDefButS(block, TOG|BIT|0, 0, "Large Cursors",
2387                         (xpos+edgsp),y2,spref,buth,
2388                         &(U.curssize), 0, 0, 0, 0,
2389                         "Use large mouse cursors when available");
2390 #else 
2391                 U.curssize=0; /*Small Cursor always for OS X for now */
2392 #endif
2393
2394                 uiDefBut(block, LABEL,0,"Menus:",
2395                         (xpos+(2*edgsp)+spref),y6label,spref,buth,
2396                         0, 0, 0, 0, 0, "");
2397                 uiDefButBitS(block, TOG, USER_MENUOPENAUTO, 0, "Open on Mouse Over",
2398                         (xpos+edgsp+spref+midsp),y5,mpref,buth,
2399                         &(U.uiflag), 0, 0, 0, 0,
2400                         "Open menu buttons and pulldowns automatically when the mouse is hovering");
2401                 uiDefButS(block, NUM, 0, "Top Level:",
2402                         (xpos+edgsp+spref+midsp),y4,spref+edgsp,buth,
2403                         &(U.menuthreshold1), 1, 40, 0, 0,
2404                         "Time delay in 1/10 seconds before automatically opening top level menus");
2405                 uiDefButS(block, NUM, 0, "Sublevels:",
2406                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y4,spref+edgsp,buth,
2407                         &(U.menuthreshold2), 1, 40, 0, 0,
2408                         "Time delay in 1/10 seconds before automatically opening menu sublevels");
2409
2410                 uiDefBut(block, LABEL,0,"Toolbox click-hold delay:",
2411                         (xpos+(2*edgsp)+spref),y3label,mpref,buth,
2412                         0, 0, 0, 0, 0, "");
2413                 uiDefButS(block, NUM, 0, "LMB:",
2414                         (xpos+edgsp+spref+midsp),y2,spref+edgsp,buth,
2415                         &(U.tb_leftmouse), 2, 40, 0, 0,
2416                         "Time in 1/10 seconds to hold the Left Mouse Button before opening the toolbox");
2417                 uiDefButS(block, NUM, 0, "RMB:",
2418                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y2,spref+edgsp,buth,
2419                         &(U.tb_rightmouse), 2, 40, 0, 0,
2420                         "Time in 1/10 seconds to hold the Right Mouse Button before opening the toolbox");      
2421                 uiDefButBitS(block, TOG, USER_PANELPINNED, 0, "Pin Floating Panels",
2422                         (xpos+edgsp+spref+midsp),y1,mpref,buth,
2423                         &(U.uiflag), 0, 0, 0, 0,
2424                         "Make floating panels invoked by a hotkey (eg. N Key) open at the previous location");
2425                 
2426                 
2427                 uiDefBut(block, LABEL,0,"Snap to grid:",
2428                         (xpos+(2*edgsp)+spref+midsp+mpref),y6label,mpref,buth,
2429                         0, 0, 0, 0, 0, "");
2430                 uiDefButBitS(block, TOG, USER_AUTOGRABGRID, 0, "Grab/Move",
2431                         (xpos+edgsp+mpref+spref+(2*midsp)),y5,spref,buth,
2432                         &(U.flag), 0, 0, 0, 0,
2433                         "Snap objects and sub-objects to grid units when moving");
2434                 uiDefButBitS(block, TOG, USER_AUTOROTGRID, 0, "Rotate",
2435                         (xpos+edgsp+mpref+spref+(2*midsp)),y4,spref,buth,
2436                         &(U.flag), 0, 0, 0, 0,
2437                         "Snap objects and sub-objects to grid units when rotating");
2438                 uiDefButBitS(block, TOG, USER_AUTOSIZEGRID, 0, "Scale",
2439                         (xpos+edgsp+mpref+spref+(2*midsp)),y3,spref,buth,
2440                         &(U.flag), 0, 0, 0, 0,
2441                         "Snap objects and sub-objects to grid units when scaling");
2442                 
2443                 uiDefButBitS(block, TOG, USER_LOCKAROUND, B_DRAWINFO, "Global Pivot",
2444                         (xpos+edgsp+mpref+spref+(2*midsp)),y1,spref,buth,
2445                         &(U.uiflag), 0, 0, 0, 0,
2446                         "Lock the same rotation/scaling pivot in all 3D Views");        
2447                 
2448                 uiDefBut(block, LABEL,0,"View zoom:",
2449                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y6label,mpref,buth,
2450                         0, 0, 0, 0, 0, "");
2451                 uiBlockBeginAlign(block);
2452                 uiDefButS(block, ROW, 0, "Continue",
2453                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y5,(mpref/3),buth,
2454                         &(U.viewzoom), 40, USER_ZOOM_CONT, 0, 0,
2455                         "Old style zoom, continues while moving mouse up or down");
2456                 uiDefButS(block, ROW, 0, "Dolly",
2457                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/3)),y5,(mpref/3),buth,
2458                         &(U.viewzoom), 40, USER_ZOOM_DOLLY, 0, 0,
2459                         "Zooms in and out based on vertical mouse movement.");
2460                 uiDefButS(block, ROW, 0, "Scale",
2461                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(2*mpref/3)),y5,(mpref/3),buth,
2462                         &(U.viewzoom), 40, USER_ZOOM_SCALE, 0, 0,
2463                         "Zooms in and out like scaling the view, mouse movements relative to center.");
2464                 uiBlockEndAlign(block);
2465                 
2466                 uiDefBut(block, LABEL,0,"View rotation:",
2467                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y4label,mpref,buth,
2468                         0, 0, 0, 0, 0, "");
2469                 uiBlockBeginAlign(block);
2470                 uiDefButBitS(block, TOG, USER_TRACKBALL, B_DRAWINFO, "Trackball",
2471                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y3,(mpref/2),buth,
2472                         &(U.flag), 0, 0, 0, 0,
2473                         "Allow the view to tumble freely when orbiting with the Middle Mouse Button");
2474                 uiDefButBitS(block, TOGN, USER_TRACKBALL, B_DRAWINFO, "Turntable",
2475                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y3,(mpref/2),buth,
2476                         &(U.flag), 0, 0, 0, 0,
2477                         "Keep the Global Z axis pointing upwards when orbiting the view with the Middle Mouse Button");
2478                 uiBlockEndAlign(block);
2479                 
2480                 uiDefButBitS(block, TOG, USER_AUTOPERSP, B_DRAWINFO, "Auto Perspective",
2481                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y1,spref,buth,
2482                         &(U.uiflag), 0, 0, 0, 0,
2483                         "Automatically switch between orthographic and perspective when changing from top/front/side views");
2484
2485                 uiDefBut(block, LABEL,0,"Select with:",
2486                         (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y6label,mpref,buth,
2487                         0, 0, 0, 0, 0, "");
2488                 uiBlockBeginAlign(block);
2489                 uiDefButBitS(block, TOG, USER_LMOUSESELECT, B_DRAWINFO, "Left Mouse",
2490                         (xpos+edgsp+(3*mpref)+(4*midsp)),y5,(mpref/2),buth,
2491                         &(U.flag), 0, 0, 0, 0, "Use the Left Mouse Button for selection");
2492                 uiDefButBitS(block, TOGN, USER_LMOUSESELECT, B_DRAWINFO, "Right Mouse",
2493                         (xpos+edgsp+(3*mpref)+(4*midsp)+(mpref/2)),y5,(mpref/2),buth,
2494                         &(U.flag), 0, 0, 0, 0, "Use the Right Mouse Button for selection");
2495                 uiBlockEndAlign(block);
2496                 
2497                 
2498                 if(U.flag & USER_LMOUSESELECT) {
2499                         uiDefBut(block, LABEL,0,"Cursor with: Right Mouse",
2500                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2501                                 0, 0, 0, 0, 0, "");
2502                 } else {
2503                         uiDefBut(block, LABEL,0,"Cursor with: Left Mouse",
2504                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2505                                 0, 0, 0, 0, 0, "");
2506                 }
2507                 
2508                 /* illegal combo... */
2509                 if (U.flag & USER_LMOUSESELECT) 
2510                         U.flag &= ~USER_TWOBUTTONMOUSE;
2511                 
2512                 uiDefButBitS(block, TOG, USER_TWOBUTTONMOUSE, B_DRAWINFO, "Emulate 3 Button Mouse",
2513                         (xpos+edgsp+(3*mpref)+(4*midsp)),y3,mpref,buth,
2514                         &(U.flag), 0, 0, 0, 0,
2515                         "Emulates Middle Mouse with Alt+LeftMouse (doesnt work with Left Mouse Select option)");
2516                 
2517                         
2518                 uiDefBut(block, LABEL,0,"Middle Mouse Button:",
2519                         (xpos+(2*edgsp)+(4*mpref)+(4*midsp)),y6label,mpref,buth,
2520                         0, 0, 0, 0, 0, "");
2521                 uiBlockBeginAlign(block);
2522                 uiDefButBitS(block, TOGN, USER_VIEWMOVE, B_DRAWINFO, "Rotate View",
2523                         (xpos+edgsp+(4*mpref)+(5*midsp)),y5,(mpref/2),buth,
2524                         &(U.flag), 0, 0, 0, 0, "Default action for the Middle Mouse Button");
2525                 uiDefButBitS(block, TOG, USER_VIEWMOVE, B_DRAWINFO, "Pan View",
2526                         (xpos+edgsp+(4*mpref)+(5*midsp)+(mpref/2)),y5,(mpref/2),buth,
2527                         &(U.flag), 0, 0, 0, 0, "Default action for the Middle Mouse Button");
2528                 uiBlockEndAlign(block);
2529                         
2530                 uiDefBut(block, LABEL,0,"Mouse Wheel:",
2531                         (xpos+(2*edgsp)+(4*mpref)+(4*midsp)),y4label,mpref,buth,
2532                         0, 0, 0, 0, 0, "");
2533                 uiDefButBitS(block, TOG, USER_WHEELZOOMDIR, 0, "Invert Zoom",
2534                         (xpos+edgsp+(4*mpref)+(5*midsp)),y3,spref,buth,
2535                         &(U.uiflag), 0, 0, 0, 0,
2536                         "Swap the Mouse Wheel zoom direction");
2537                 uiDefButI(block, NUM, 0, "Scroll Lines:",
2538                         (xpos+edgsp+(4*mpref)+(6*midsp)+spref-edgsp),y3,spref+edgsp,buth,
2539                         &U.wheellinescroll, 0.0, 32.0, 0, 0,
2540                         "The number of lines scrolled at a time with the mouse wheel"); 
2541
2542
2543                 uiDefBut(block, LABEL,0,"3D Transform Widget:",
2544                                  (xpos+(2*edgsp)+(5*mpref)+(5*midsp)),y6label,mpref,buth,
2545                                  0, 0, 0, 0, 0, "");
2546                 uiBlockBeginAlign(block);
2547                 uiDefButS(block, NUM, B_REDRCURW3D, "Size:",
2548                                          (xpos+edgsp+(5*mpref)+(6*midsp)),y5,(mpref/2),buth,
2549                                          &(U.tw_size), 2, 40, 0, 0, "Size of widget as percentage of window size");
2550                 uiDefButS(block, NUM, B_REDRCURW3D, "Handle:",
2551                                          (xpos+edgsp+(5*mpref)+(6*midsp)+(mpref/2)),y5,(mpref/2),buth,
2552                                          &(U.tw_handlesize), 2, 40, 0, 0, "Size of widget handles as percentage of widget radius");
2553                 uiDefButS(block, NUM, B_REDRCURW3D, "Hotspot:",
2554                                   (xpos+edgsp+(5*mpref)+(6*midsp)),y4,(mpref),buth,
2555                                   &(U.tw_hotspot), 4, 40, 0, 0, "Hotspot in pixels for clicking widget handles");
2556                 
2557                 uiBlockEndAlign(block);
2558                 
2559         } else if (U.userpref == 1) { /* edit methods */
2560
2561
2562                 uiDefBut(block, LABEL,0,"Material linked to:",
2563                         xpos,y3label,mpref,buth,
2564                         0, 0, 0, 0, 0, "");
2565                 uiBlockBeginAlign(block);
2566                 uiDefButBitS(block, TOGN, USER_MAT_ON_OB, B_DRAWINFO, "ObData",
2567                         (xpos+edgsp),y2,(mpref/2),buth,
2568                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the obData block");
2569                 uiDefButBitS(block, TOG, USER_MAT_ON_OB, B_DRAWINFO, "Object",
2570                         (xpos+edgsp+(mpref/2)),y2,(mpref/2),buth,
2571                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the object block");
2572                 uiBlockEndAlign(block);
2573
2574
2575                 uiDefBut(block, LABEL,0,"Editmode undo:",
2576                         (xpos+(2*edgsp)+mpref),y3label, mpref,buth,
2577                         0, 0, 0, 0, 0, "");
2578                 uiDefButS(block, NUMSLI, B_DRAWINFO, "Steps:",
2579                         (xpos+edgsp+mpref+midsp),y2,mpref,buth,
2580                         &(U.undosteps), 2, 64, 0, 0, "Number of undo steps available in Edit Mode (smaller values conserve memory)");
2581
2582                 uiDefButBitS(block, TOG, USER_GLOBALUNDO, B_DRAWINFO, "Global undo",
2583                         (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2584                         &(U.uiflag), 2, 64, 0, 0, "");
2585
2586
2587                 uiDefBut(block, LABEL,0,"Auto keyframe",
2588                         (xpos+(2*edgsp)+(2*mpref)+midsp),y3label,mpref,buth,
2589                         0, 0, 0, 0, 0, "");
2590                 uiDefButBitI(block, TOG, G_RECORDKEYS, REDRAWTIME, "Action and Object", 
2591                                         (xpos+edgsp+(2*mpref)+(2*midsp)),y2,mpref, buth,
2592                                          &(G.flags), 0, 0, 0, 0, "Automatic keyframe insertion in Object and Action Ipo curves");
2593
2594 //              uiDefButBitS(block, TOG, USER_KEYINSERTACT, 0, "Action",
2595 //                      (xpos+edgsp+(2*mpref)+(2*midsp)),y2,(spref+edgsp),buth,
2596 //                      &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in Action Ipo curve");
2597 //              uiDefButBitS(block, TOG, USER_KEYINSERTOBJ, 0, "Object",
2598 //                      (xpos+edgsp+(2*mpref)+(3*midsp)+spref-edgsp),y2,(spref+edgsp),buth,
2599 //                      &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in Object Ipo curve");
2600
2601
2602                 uiDefBut(block, LABEL,0,"Duplicate with object:",
2603                         (xpos+(2*edgsp)+(3*midsp)+(3*mpref)+spref),y3label,mpref,buth,
2604                         0, 0, 0, 0, 0, "");
2605
2606                 uiDefButBitS(block, TOG, USER_DUP_MESH, 0, "Mesh",
2607                         (xpos+edgsp+(4*midsp)+(3*mpref)+spref),y2,(spref+edgsp),buth,
2608                         &(U.dupflag), 0, 0, 0, 0, "Causes mesh data to be duplicated with Shift+D");
2609                 uiDefButBitS(block, TOG, USER_DUP_ARM, 0, "Armature",
2610                         (xpos+edgsp+(4*midsp)+(3*mpref)+spref),y1,(spref+edgsp),buth,
2611                         &(U.dupflag), 0, 0, 0, 0, "Causes armature data to be duplicated with Shift+D");
2612
2613                 uiDefButBitS(block, TOG, USER_DUP_SURF, 0, "Surface",
2614                         (xpos+edgsp+(5*midsp)+(3*mpref)+(2*spref)),y2,(spref+edgsp),buth,
2615                         &(U.dupflag), 0, 0, 0, 0, "Causes surface data to be duplicated with Shift+D");
2616                 uiDefButBitS(block, TOG, USER_DUP_LAMP, 0, "Lamp",
2617                         (xpos+edgsp+(5*midsp)+(3*mpref)+(2*spref)),y1,(spref+edgsp),buth,
2618                         &(U.dupflag), 0, 0, 0, 0, "Causes lamp data to be duplicated with Shift+D");
2619
2620                 uiDefButBitS(block, TOG, USER_DUP_CURVE, 0, "Curve",
2621                         (xpos+edgsp+(6*midsp)+(3*mpref)+(3*spref)),y2,(spref+edgsp),buth,
2622                         &(U.dupflag), 0, 0, 0, 0, "Causes curve data to be duplicated with Shift+D");
2623                 uiDefButBitS(block, TOG, USER_DUP_MAT, 0, "Material",
2624                         (xpos+edgsp+(6*midsp)+(3*mpref)+(3*spref)),y1,(spref+edgsp),buth,
2625                         &(U.dupflag), 0, 0, 0, 0, "Causes material data to be duplicated with Shift+D");
2626
2627                 uiDefButBitS(block, TOG, USER_DUP_FONT, 0, "Text",
2628                         (xpos+edgsp+(7*midsp)+(3*mpref)+(4*spref)),y2,(spref+edgsp),buth,
2629                         &(U.dupflag), 0, 0, 0, 0, "Causes text data to be duplicated with Shift+D");
2630                 uiDefButBitS(block, TOG, USER_DUP_TEX, 0, "Texture",
2631                         (xpos+edgsp+(7*midsp)+(3*mpref)+(4*spref)),y1,(spref+edgsp),buth,
2632                         &(U.dupflag), 0, 0, 0, 0, "Causes texture data to be duplicated with Shift+D");
2633
2634                 uiDefButBitS(block, TOG, USER_DUP_MBALL, 0, "Metaball",
2635                         (xpos+edgsp+(8*midsp)+(3*mpref)+(5*spref)),y2,(spref+edgsp),buth,
2636                         &(U.dupflag), 0, 0, 0, 0, "Causes metaball data to be duplicated with Shift+D");
2637                 uiDefButBitS(block, TOG, USER_DUP_IPO, 0, "Ipo",
2638                         (xpos+edgsp+(8*midsp)+(3*mpref)+(5*spref)),y1,(spref+edgsp),buth,
2639                         &(U.dupflag), 0, 0, 0, 0, "Causes ipo data to be duplicated with Shift+D");
2640         
2641         } else if(U.userpref == 2) { /* language & colors */
2642
2643 #ifdef INTERNATIONAL
2644                 uiDefButBitS(block, TOG, USER_DOTRANSLATE, B_DOLANGUIFONT, "International Fonts",
2645                         xpos,y2,mpref,buth,
2646                         &(U.transopts), 0, 0, 0, 0, "Activate international interface");
2647
2648                 if(U.transopts & USER_DOTRANSLATE) {
2649                         char curfont[320];
2650                         
2651                         sprintf(curfont, "Interface Font: ");
2652                         if(U.fontname[0]) strcat(curfont, U.fontname);
2653                         else strcat(curfont, "Built-in");
2654                         
2655                         uiDefBut(block, LABEL,0,curfont,
2656                                 (xpos),y3,4*mpref,buth,
2657                                 0, 0, 0, 0, 0, "");
2658
2659                         uiDefBut(block, BUT, B_LOADUIFONT, "Select Font",
2660                                 xpos,y1,mpref,buth,
2661                                 0, 0, 0, 0, 0, "Select a new font for the interface");
2662
2663                         uiDefButI(block, BUT, B_RESTOREFONT, "Restore to default",
2664                                           (xpos+edgsp+mpref+midsp),y2,mpref,buth,
2665                                           &U.fontsize, 0, 0, 0, 0, "Restores to using the default included antialised font");
2666                         
2667                         uiDefButI(block, MENU|INT, B_SETFONTSIZE, fontsize_pup(),
2668                                 (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2669                                 &U.fontsize, 0, 0, 0, 0, "Current interface font size (points)");
2670
2671 /*
2672                         uiDefButS(block, MENU|SHO, B_SETENCODING, encoding_pup(),
2673                                 (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2674                                 &U.encoding, 0, 0, 0, 0, "Current interface font encoding");
2675
2676
2677                         uiDefBut(block, LABEL,0,"Translate:",
2678                                 (xpos+edgsp+(2.1*mpref)+(2*midsp)),y3label,mpref,buth,
2679                                 0, 0, 0, 0, 0, "");
2680 */
2681
2682                         uiDefButBitS(block, TOG, USER_TR_TOOLTIPS, B_SETTRANSBUTS, "Tooltips",
2683                                 (xpos+edgsp+(2.2*mpref)+(3*midsp)),y1,spref,buth,
2684                                 &(U.transopts), 0, 0, 0, 0, "Translate tooltips");
2685
2686                         uiDefButBitS(block, TOG, USER_TR_BUTTONS, B_SETTRANSBUTS, "Buttons",
2687                                 (xpos+edgsp+(2.2*mpref)+(4*midsp)+spref),y1,spref,buth,
2688                                 &(U.transopts), 0, 0, 0, 0, "Translate button labels");
2689
2690                         uiDefButBitS(block, TOG, USER_TR_MENUS, B_SETTRANSBUTS, "Toolbox",
2691                                 (xpos+edgsp+(2.2*mpref)+(5*midsp)+(2*spref)),y1,spref,buth,
2692                                 &(U.transopts), 0, 0, 0, 0, "Translate toolbox menu");
2693
2694                         uiDefButS(block, MENU|SHO, B_SETLANGUAGE, language_pup(),
2695                                 (xpos+edgsp+(2.2*mpref)+(3*midsp)),y2,mpref+(0.5*mpref)+3,buth,
2696                                 &U.language, 0, 0, 0, 0, "Select interface language");
2697                                 
2698                         uiDefButBitS(block, TOG, USER_USETEXTUREFONT, B_USETEXTUREFONT, "Use Textured Fonts",
2699                                 (xpos+edgsp+(4*mpref)+(4*midsp)),y2,mpref,buth,
2700                                 &(U.transopts), 0, 0, 0, 0,
2701                                 "Use Textured Fonts");
2702                 }
2703
2704 /* end of INTERNATIONAL */
2705 #endif
2706
2707         } else if(U.userpref == 3) { /* auto save */
2708
2709
2710                 uiDefButS(block, NUM, 0, "Save Versions:",
2711                         (xpos+edgsp),y3,mpref,buth,
2712                         &U.versions, 0.0, 32.0, 0, 0,
2713                         "The number of old versions to maintain in the current directory, when manually saving");
2714
2715                 uiDefButBitS(block, TOG, USER_AUTOSAVE, B_RESETAUTOSAVE, "Auto Save Temp Files",
2716                         (xpos+edgsp+mpref+midsp),y3,mpref,buth,
2717                         &(U.flag), 0, 0, 0, 0,
2718                         "Enables automatic saving of temporary files");
2719
2720                 if(U.flag & USER_AUTOSAVE) {
2721
2722                         uiDefButI(block, NUM, B_RESETAUTOSAVE, "Minutes:",
2723                                 (xpos+edgsp+mpref+midsp),y2,mpref,buth,
2724                                 &(U.savetime), 1.0, 60.0, 0, 0,
2725                                 "The time (in minutes) to wait between automatic temporary saves");
2726
2727                         uiDefBut(block, BUT, B_LOADTEMP, "Open Recent",
2728                                 (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2729                                 0, 0, 0, 0, 0,"Open the most recently saved temporary file");
2730                 }
2731
2732         } else if (U.userpref == 4) { /* system & opengl */
2733                 uiDefBut(block, LABEL,0,"Solid OpenGL light:",
2734                         xpos+edgsp, y3label, mpref, buth,
2735                         0, 0, 0, 0, 0, "");
2736                 
2737                 uiDefButS(block, MENU, B_REDR, "Light1 %x0|Light2 %x1|Light3 %x2",
2738                         xpos+edgsp, y2, 2*mpref/6, buth, &cur_light, 0.0, 0.0, 0, 0, "");
2739                 uiBlockSetCol(block, TH_BUT_SETTING1);
2740                 uiDefButI(block, TOG|BIT|0, B_RECALCLIGHT, "On",
2741                         xpos+edgsp+2*mpref/6, y2, mpref/6, buth, 
2742                         &U.light[cur_light].flag, 0.0, 0.0, 0, 0, "Enable this OpenGL light in Solid draw mode");
2743                         
2744                 uiBlockSetCol(block, TH_AUTO);
2745                 uiDefButS(block, ROW, B_REDR, "Vec",
2746                         xpos+edgsp+3*mpref/6, y2, mpref/6, buth, 
2747                         &cur_light_var, 123.0, 0.0, 0, 0, "Lamp vector for OpenGL light");
2748                 uiDefButS(block, ROW, B_REDR, "Col",
2749                         xpos+edgsp+4*mpref/6, y2, mpref/6, buth, 
2750                         &cur_light_var, 123.0, 1.0, 0, 0, "Diffuse Color for OpenGL light");
2751                 uiDefButS(block, ROW, B_REDR, "Spec",
2752                         xpos+edgsp+5*mpref/6, y2, mpref/6, buth, 
2753                         &cur_light_var, 123.0, 2.0, 0, 0, "Specular color for OpenGL light");
2754
2755                 if(cur_light_var==1) {
2756                         uiDefButF(block, NUM, B_RECALCLIGHT, "R ",
2757                                &