1) Removed old annoyance in adding armature or bones.
[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(1);        // 1 == toggle
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==0))
1136                                         borderselect();
1137                                 break;
1138                         case CKEY:
1139                                 if(G.qual==LR_CTRLKEY) {
1140                                         copy_attr_menu();
1141                                 }
1142                                 else if(G.qual==LR_ALTKEY) {
1143                                         convertmenu();  /* editobject.c */
1144                                 }
1145                                 else if((G.qual==LR_SHIFTKEY)) {
1146                                         view3d_home(1);
1147                                         curs= give_cursor();
1148                                         curs[0]=curs[1]=curs[2]= 0.0;
1149                                         allqueue(REDRAWVIEW3D, 0);
1150                                 }
1151                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1152                                         makecyclicNurb();
1153                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1154                                         allqueue(REDRAWVIEW3D, 0);
1155                                 }
1156                                 else if((G.qual==0)){
1157                                         curs= give_cursor();
1158                                         G.vd->ofs[0]= -curs[0];
1159                                         G.vd->ofs[1]= -curs[1];
1160                                         G.vd->ofs[2]= -curs[2];
1161                                         scrarea_queue_winredraw(curarea);
1162                                 }
1163                         
1164                                 break;
1165                         case DKEY:
1166                                 if((G.qual==LR_SHIFTKEY)) {
1167                                         duplicate_context_selected();
1168                                 }
1169                                 else if(G.qual==LR_ALTKEY) {
1170                                         if(G.obpose)
1171                                                 error ("Duplicate not possible in posemode.");
1172                                         else if((G.obedit==0))
1173                                                 adduplicate(0);
1174                                 }
1175                                 else if(G.qual==LR_CTRLKEY) {
1176                                         imagestodisplist();
1177                                 }
1178                                 else if((G.qual==0)){
1179                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
1180                                         if(pupval>0) {
1181                                                 G.vd->drawtype= pupval;
1182                                                 doredraw= 1;
1183                                         
1184                                         }
1185                                 }
1186                                 
1187                                 break;
1188                         case EKEY:
1189                                 if (G.qual==0){
1190                                         if(G.obedit) {
1191                                                 if(G.obedit->type==OB_MESH)
1192                                                         extrude_mesh();
1193                                                 else if(G.obedit->type==OB_CURVE)
1194                                                         addvert_Nurb('e');
1195                                                 else if(G.obedit->type==OB_SURF)
1196                                                         extrude_nurb();
1197                                                 else if(G.obedit->type==OB_ARMATURE)
1198                                                         extrude_armature();
1199                                         }
1200                                 }
1201                                 else if (G.qual==LR_CTRLKEY) {
1202                                         if(G.obedit && G.obedit->type==OB_MESH)
1203                                                 Edge_Menu();
1204                                 }
1205                                 else if (G.qual==LR_SHIFTKEY) {
1206                                         if (G.obedit && G.obedit->type==OB_MESH) {
1207                                                 initTransform(TFM_CREASE, CTX_EDGE);
1208                                                 Transform();
1209                                         }
1210                                 }
1211                                 break;
1212                         case FKEY:
1213                                 if(G.obedit) {
1214                                         if(G.obedit->type==OB_MESH) {
1215                                                 if((G.qual==LR_SHIFTKEY))
1216                                                         fill_mesh();
1217                                                 else if(G.qual==LR_ALTKEY)
1218                                                         beauty_fill();
1219                                                 else if(G.qual==LR_CTRLKEY)
1220                                                         edge_flip();
1221                                                 else if (G.qual==0)
1222                                                         addedgeface_mesh();
1223                                         }
1224                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
1225                                 }
1226                                 else if(G.qual==LR_CTRLKEY)
1227                                         sort_faces();
1228                                 else if((G.qual==LR_SHIFTKEY))
1229                                         fly();
1230                                 else {
1231                                                 set_faceselect();
1232                                         }
1233                                 
1234                                 break;
1235                         case GKEY:
1236                                 /* RMGRP if(G.qual & LR_CTRLKEY) add_selected_to_group();
1237                                 else if(G.qual & LR_ALTKEY) rem_selected_from_group(); */
1238                                 if((G.qual==LR_SHIFTKEY))
1239                                         select_group_menu();
1240                                 else if(G.qual==LR_ALTKEY) {
1241                                         if(okee("Clear location")) {
1242                                                 clear_object('g');
1243                                         }
1244                                 }
1245                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1246                                         v3d->twtype= V3D_MANIP_TRANSLATE;
1247                                         doredraw= 1;
1248                                 }
1249                                 else if((G.qual==0)) {
1250                                         initTransform(TFM_TRANSLATION, CTX_NONE);
1251                                         Transform();
1252                                 }
1253                                 break;
1254                         case HKEY:
1255                                 if(G.obedit) {
1256                                         if(G.obedit->type==OB_MESH) {
1257                                                 if(G.qual==LR_CTRLKEY)
1258                                                         add_hook();
1259                                                 else if(G.qual==LR_ALTKEY)
1260                                                         reveal_mesh();
1261                                                 else if((G.qual==LR_SHIFTKEY))
1262                                                         hide_mesh(1);
1263                                                 else if((G.qual==0)) 
1264                                                         hide_mesh(0);
1265                                         }
1266                                         else if(G.obedit->type== OB_SURF) {
1267                                                 if(G.qual==LR_CTRLKEY)
1268                                                         add_hook();
1269                                                 else if(G.qual==LR_ALTKEY)
1270                                                         revealNurb();
1271                                                 else if((G.qual==LR_SHIFTKEY))
1272                                                         hideNurb(1);
1273                                                 else if((G.qual==0))
1274                                                         hideNurb(0);
1275                                         }
1276                                         else if(G.obedit->type==OB_CURVE) {
1277                                                 if(G.qual==LR_CTRLKEY)
1278                                                         add_hook();
1279                                                 else {
1280                                                         if(G.qual==LR_CTRLKEY)
1281                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
1282                                                         else if((G.qual==LR_SHIFTKEY))
1283                                                                 sethandlesNurb(1);
1284                                                         else if((G.qual==0))
1285                                                                 sethandlesNurb(3);
1286                                                         
1287                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1288                                                         BIF_undo_push("Handle change");
1289                                                         allqueue(REDRAWVIEW3D, 0);
1290                                                 }
1291                                         }
1292                                         else if(G.obedit->type==OB_LATTICE) {
1293                                                 if(G.qual==LR_CTRLKEY) add_hook();
1294                                         }
1295                                         else if(G.obedit->type==OB_MBALL) {
1296                                                 if(G.qual==LR_ALTKEY)
1297                                                         reveal_mball();
1298                                                 else if((G.qual==LR_SHIFTKEY))
1299                                                         hide_mball(1);
1300                                                 else if((G.qual==0)) 
1301                                                         hide_mball(0);
1302                                         }
1303                                 }
1304                                 else if(G.f & G_FACESELECT)
1305                                         hide_tface();
1306                                 else if(G.obpose) {
1307                                         if (G.qual==0)
1308                                                 hide_selected_pose_bones();
1309                                         else if (G.qual==LR_SHIFTKEY)
1310                                                 hide_unselected_pose_bones();
1311                                         else if (G.qual==LR_ALTKEY)
1312                                                 show_all_pose_bones();
1313                                 }
1314                                 break;
1315                         case IKEY:
1316                                 break;
1317                                 
1318                         case JKEY:
1319                                 if(G.qual==LR_CTRLKEY) {
1320                                         if( (ob= OBACT) ) {
1321                                                 if(ob->type == OB_MESH)
1322                                                         join_mesh();
1323                                                 else if(ob->type == OB_CURVE)
1324                                                         join_curve(OB_CURVE);
1325                                                 else if(ob->type == OB_SURF)
1326                                                         join_curve(OB_SURF);
1327                                                 else if(ob->type == OB_ARMATURE)
1328                                                         join_armature ();
1329                                         }
1330                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1331                                                 addsegment_nurb();
1332                                 }
1333                                 else if(G.obedit) {
1334                                         if(G.obedit->type==OB_MESH) {
1335                                                 join_triangles();
1336                                         }
1337                                 }
1338
1339                                 break;
1340                         case KKEY:
1341                                 if(G.obedit) {
1342                                         if (G.obedit->type==OB_MESH) {
1343                                                 if (G.qual==LR_SHIFTKEY)
1344                                                         KnifeSubdivide(KNIFE_PROMPT);
1345                                                 else if (G.qual==0)
1346                                                         LoopMenu();
1347                                         }
1348                                         else if(G.obedit->type==OB_SURF)
1349                                                 printknots();
1350                                 }
1351                                 else {
1352                                         if((G.qual==LR_SHIFTKEY)) {
1353                                                 if(G.f & G_FACESELECT)
1354                                                         clear_vpaint_selectedfaces();
1355                                                 else if(G.f & G_VERTEXPAINT)
1356                                                         clear_vpaint();
1357                                                 else
1358                                                         select_select_keys();
1359                                         }
1360                                         else if (G.qual==0)
1361                                                 set_ob_ipoflags();
1362                                 }
1363                                 
1364                                 break;
1365                         case LKEY:
1366                                 if(G.obedit) {
1367                                         if(G.obedit->type==OB_MESH)
1368                                                 selectconnected_mesh(G.qual);
1369                                         if(G.obedit->type==OB_ARMATURE)
1370                                                 selectconnected_armature();
1371                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1372                                                 selectconnected_nurb();
1373                                 }
1374                                 else if(G.obpose) {
1375                                         if(G.obpose->type==OB_ARMATURE)
1376                                                 selectconnected_posearmature();
1377                                 }
1378                                 else {
1379                                         if(G.f & G_FACESELECT) {
1380                                                 if((G.qual==0))
1381                                                         select_linked_tfaces(0);
1382                                                 else if((G.qual==LR_SHIFTKEY))
1383                                                         select_linked_tfaces(1);
1384                                                 else if(G.qual==LR_CTRLKEY)
1385                                                         select_linked_tfaces(2);
1386                                         }
1387                                         else {
1388                                                 if((G.qual==0))
1389                                                         make_local();
1390                                                 else if((G.qual==LR_SHIFTKEY))
1391                                                         selectlinks_menu();
1392                                                 else if(G.qual==LR_CTRLKEY)
1393                                                         make_links_menu();
1394                                         }
1395                                 }
1396                                 break;
1397                         case MKEY:
1398                                 if(G.obedit){
1399                                         if(G.qual==LR_ALTKEY) {
1400                                                 if(G.obedit->type==OB_MESH) {
1401                                                         mergemenu();
1402                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1403                                                 }
1404                                         }
1405                                         else if((G.qual==0) || (G.qual==LR_CTRLKEY)) {
1406                                                 mirrormenu();
1407                                         }
1408                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1409                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
1410                                         }
1411                                 }
1412                                 else if(G.qual & LR_CTRLKEY) {
1413                                         mirrormenu();
1414                                 }
1415                                 else if(G.qual==0) {
1416                                      movetolayer();
1417                                 }
1418                                 break;
1419                         case NKEY:
1420                                 if((G.qual==0)) {
1421                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1422                                         allqueue(REDRAWVIEW3D, 0);
1423                                 }
1424                                 else if(G.obedit) {
1425                                         switch (G.obedit->type){
1426                                         case OB_ARMATURE:
1427                                                 if(G.qual==LR_CTRLKEY){
1428                                                         if (okee("Recalculate bone roll angles")) {
1429                                                                 auto_align_armature();
1430                                                                 allqueue(REDRAWVIEW3D, 0);
1431                                                         }
1432                                                 }
1433                                                 break;
1434                                         case OB_MESH: 
1435                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1436                                                         if(okee("Recalculate normals inside")) {
1437                                                                 righthandfaces(2);
1438                                                                 allqueue(REDRAWVIEW3D, 0);
1439                                                                 BIF_undo_push("Recalculate normals inside");
1440                                                         }
1441                                                 }
1442                                                 else if(G.qual==LR_CTRLKEY){
1443                                                         if(okee("Recalculate normals outside")) {
1444                                                                 righthandfaces(1);
1445                                                                 allqueue(REDRAWVIEW3D, 0);
1446                                                                 BIF_undo_push("Recalculate normals outside");
1447                                                         }
1448                                                 }
1449                                                 break;
1450                                         }
1451                                 }
1452                                 
1453                                 break;
1454                         case OKEY:
1455                                 ob= OBACT;
1456                                 if (G.obedit) {
1457                                         if (G.qual==LR_SHIFTKEY) {
1458                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%6;
1459                                                 allqueue(REDRAWHEADERS, 0);
1460                                         }
1461                                         else if((G.qual==LR_ALTKEY)) {
1462                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
1463                                                 else G.scene->proportional= 2;
1464                                                 allqueue(REDRAWHEADERS, 0);
1465                                         }
1466                                         else if((G.qual==0)) {
1467                                                 G.scene->proportional= !G.scene->proportional;
1468                                                 allqueue(REDRAWHEADERS, 0);
1469                                         }
1470                                 }
1471                                 else if((G.qual==LR_SHIFTKEY)) {
1472                                         if(ob && ob->type == OB_MESH) {
1473                                                 flip_subdivison(ob, -1);
1474                                         }
1475                                 }
1476                                 else if(G.qual==LR_ALTKEY) {
1477                                         if(okee("Clear origin")) {
1478                                                 clear_object('o');
1479                                         }
1480                                 }
1481                                 break;
1482
1483                         case PKEY:
1484                                 if(G.obedit) {
1485                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1486                                                 if(G.obedit->type==OB_ARMATURE)
1487                                                         make_bone_parent();
1488                                                 else
1489                                                         make_parent();
1490                                         }
1491                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
1492                                                 clear_bone_parent();
1493                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
1494                                                 separatemenu();
1495                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1496                                                 separate_nurb();
1497                                         else if (G.qual==LR_SHIFTKEY) {
1498                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
1499                                                 Transform();
1500                                         }
1501                                 }
1502                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
1503                                         make_parent();
1504                                 else if(G.qual==LR_SHIFTKEY) {
1505                                         initTransform(TFM_PUSHPULL, CTX_NONE);
1506                                         Transform();
1507                                 }
1508                                 else if(G.qual==LR_ALTKEY)
1509                                         clear_parent();
1510                                 else if((G.qual==0)) {
1511                         start_game();
1512                                 }
1513                                 break;                          
1514                         case RKEY:
1515                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1516                                         rotate_uv_tface();
1517                                 else if(G.qual==LR_ALTKEY) {
1518                                         if(okee("Clear rotation")) {
1519                                                 clear_object('r');
1520                                         }
1521                                 } 
1522                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1523                                         v3d->twtype= V3D_MANIP_ROTATE;
1524                                         doredraw= 1;
1525                                 }
1526                                 else if (G.obedit) {
1527                                         if((G.qual==LR_SHIFTKEY)) {
1528                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
1529                                                         selectrow_nurb();
1530                                         }
1531                                         else if(G.qual==LR_CTRLKEY) {
1532                                                 if (G.obedit->type==OB_MESH)
1533                                                         CutEdgeloop(1);
1534                                                         BIF_undo_push("Cut Edgeloop");
1535                                         }
1536                                         else if((G.qual==0)) {
1537                                                 initTransform(TFM_ROTATION, CTX_NONE);
1538                                                 Transform();
1539                                         }
1540                                 }
1541                                 else if((G.qual==0)) {
1542                                         initTransform(TFM_ROTATION, CTX_NONE);
1543                                         Transform();
1544                                 }
1545                                 break;
1546                         case SKEY:
1547                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1548                                         v3d->twtype= V3D_MANIP_SCALE;
1549                                         doredraw= 1;
1550                                 }
1551                                 else if(G.obedit) {
1552                                         
1553                                         if(G.qual==LR_ALTKEY) {
1554                                                 if(G.obedit->type==OB_ARMATURE)
1555                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1556                                                 else
1557                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
1558                                                 Transform();
1559                                         }
1560                                         else if(G.qual==LR_CTRLKEY) {
1561                                                 initTransform(TFM_SHEAR, CTX_NONE);
1562                                                 Transform();
1563                                         }
1564                                         else if(G.qual==(LR_CTRLKEY|LR_ALTKEY)) {
1565                                                 initTransform(TFM_SHEAR, CTX_NONE);
1566                                                 Transform();
1567                                         }
1568                                         else if(G.qual==LR_SHIFTKEY)
1569                                                 snapmenu();
1570                                         else if(G.qual==0) {
1571                                                 initTransform(TFM_RESIZE, CTX_NONE);
1572                                                 Transform();
1573                                         }
1574                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
1575                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
1576                                                 Transform();
1577                                         }
1578                                         
1579                                 }
1580                                 else if(G.qual==LR_ALTKEY) {
1581                                         if(okee("Clear size")) {
1582                                                 clear_object('s');
1583                                         }
1584                                 }
1585                                 else if(G.qual==LR_SHIFTKEY) {
1586                                         snapmenu();
1587                                 }
1588                                 else if((G.qual==0)) {
1589                                         initTransform(TFM_RESIZE, CTX_NONE);
1590                                         Transform();
1591                                 }
1592                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1593                                         initTransform(TFM_TOSPHERE, CTX_NONE);
1594                                         Transform();
1595                                 }
1596                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY)) {
1597                                         initTransform(TFM_SHEAR, CTX_NONE);
1598                                         Transform();
1599                                 }
1600                                 break;
1601                         case TKEY:
1602                                 if(G.obedit){
1603                                         if((G.qual==LR_CTRLKEY) && G.obedit->type==OB_MESH) {
1604                                                 convert_to_triface(0);
1605                                                 allqueue(REDRAWVIEW3D, 0);
1606                                                 countall();
1607                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1608                                         }
1609                                         if (G.obedit->type==OB_CURVE) {
1610                                                 if (G.qual==LR_ALTKEY) {
1611                                                         clear_tilt();
1612                                                 }
1613                                                 else if (G.qual==0) {
1614                                                         initTransform(TFM_TILT, CTX_NONE);
1615                                                         Transform();
1616                                                 }
1617                                         }
1618                                 }
1619                                 else if(G.qual==LR_CTRLKEY) {
1620                                         make_track();
1621                                 }
1622                                 else if(G.qual==LR_ALTKEY) {
1623                                         clear_track();
1624                                 }
1625                                 else if((G.qual==0)){
1626                                         texspace_edit();
1627                                 }
1628                                 
1629                                 break;
1630                         case UKEY:
1631                                 if(G.obedit) {
1632                                         if(G.obedit->type==OB_MESH) {
1633                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1634                                         }
1635                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
1636                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1637                                         }
1638                                 }
1639                                 else if((G.qual==0)) {
1640                                         if (G.f & G_FACESELECT)
1641                                                 uv_autocalc_tface();
1642                                         else if(G.f & G_WEIGHTPAINT)
1643                                                 wpaint_undo();
1644                                         else if(G.f & G_VERTEXPAINT)
1645                                                 vpaint_undo();
1646                                         else {
1647                                                 single_user();
1648                                         }
1649                                 }
1650                                         
1651                                 break;
1652                         case VKEY:
1653                                 ob= OBACT;
1654                                 if((G.qual==LR_SHIFTKEY)) {
1655                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
1656                                                 align_view_to_selected(v3d);
1657                                         }
1658                                         else if (G.f & G_FACESELECT) {
1659                                                 align_view_to_selected(v3d);
1660                                         }
1661                                 }
1662                                 else if(G.qual==LR_ALTKEY)
1663                                         image_aspect();
1664                                 else if (G.qual==0){
1665                                         if(G.obedit) {
1666                                                 if(G.obedit->type==OB_CURVE) {
1667                                                         sethandlesNurb(2);
1668                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1669                                                         allqueue(REDRAWVIEW3D, 0);
1670                                                         BIF_undo_push("Handle change");
1671                                                 }
1672                                         }
1673                                         else if(ob && ob->type == OB_MESH) 
1674                                                 set_vpaint();
1675                                 }
1676                                 break;
1677                         case WKEY:
1678                                 if((G.qual==LR_SHIFTKEY)) {
1679                                         initTransform(TFM_WARP, CTX_NONE);
1680                                         Transform();
1681                                 }
1682                                 else if(G.qual==LR_ALTKEY) {
1683                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
1684                                 }
1685                                 else if(G.qual==LR_CTRLKEY) {
1686                                         if(G.obedit) {
1687                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
1688                                                         switchdirectionNurb2();
1689                                                 }
1690                                         }
1691                                 }
1692                                 else if((G.qual==0))
1693                                         special_editmenu();
1694                                 
1695                                 break;
1696                         case XKEY:
1697                         case DELKEY:
1698                                 if(G.qual==0)
1699                                         delete_context_selected();
1700                                 break;
1701                         case YKEY:
1702                                 if((G.qual==0) && (G.obedit)) {
1703                                         if(G.obedit->type==OB_MESH) split_mesh();
1704                                 }
1705                                 break;
1706                         case ZKEY:
1707                                 toggle_shading();
1708                                 
1709                                 scrarea_queue_headredraw(curarea);
1710                                 scrarea_queue_winredraw(curarea);
1711                                 break;
1712                         
1713                         case HOMEKEY:
1714                                 if(G.qual==0)
1715                                         view3d_home(0);
1716                                 break;
1717                         case COMMAKEY:
1718                                 if(G.qual==LR_CTRLKEY) {
1719                                         G.vd->around= V3D_CENTROID;
1720                                 } else if(G.qual==LR_SHIFTKEY) {
1721                                         G.vd->around= V3D_CENTROID;
1722                                 } else if(G.qual==0) {
1723                                         G.vd->around= V3D_CENTRE;
1724                                 }
1725                                 handle_view3d_around();
1726                                 
1727                                 scrarea_queue_headredraw(curarea);
1728                                 scrarea_queue_winredraw(curarea);
1729                                 break;
1730                                 
1731                         case PERIODKEY:
1732                                 if(G.qual==LR_CTRLKEY) {
1733                                         G.vd->around= V3D_LOCAL;
1734                                 }       else if(G.qual==0) {
1735                                         G.vd->around= V3D_CURSOR;
1736                                 }
1737                                 handle_view3d_around();
1738                                 
1739                                 scrarea_queue_headredraw(curarea);
1740                                 scrarea_queue_winredraw(curarea);
1741                                 break;
1742                         
1743                         case PADSLASHKEY:
1744                                 if(G.qual==0) {
1745                                         if(G.vd->localview) {
1746                                                 G.vd->localview= 0;
1747                                                 endlocalview(curarea);
1748                                         }
1749                                         else {
1750                                                 G.vd->localview= 1;
1751                                                 initlocalview();
1752                                         }
1753                                         scrarea_queue_headredraw(curarea);
1754                                 }
1755                                 break;
1756                         case PADASTERKEY:       /* '*' */
1757                                 if(G.qual==0) {
1758                                         ob= OBACT;
1759                                         if(ob) {
1760                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
1761                                                         editmesh_align_view_to_selected(G.vd, 2);
1762                                                 } 
1763                                                 else if (G.f & G_FACESELECT) {
1764                                                         if(ob->type==OB_MESH) {
1765                                                                 Mesh *me= ob->data;
1766                                                                 faceselect_align_view_to_selected(G.vd, me, 2);
1767                                                         }
1768                                                 }
1769                                                 else
1770                                                         obmat_to_viewmat(ob);
1771                                                 
1772                                                 if(G.vd->persp==2) G.vd->persp= 1;
1773                                                 scrarea_queue_winredraw(curarea);
1774                                         }
1775                                 }
1776                                 break;
1777                         case PADPERIOD: /* '.' */
1778                                 if(G.qual==0)
1779                                         centreview();
1780                                 break;
1781                         
1782                         case PAGEUPKEY:
1783                                 if(G.qual==LR_CTRLKEY)
1784                                         movekey_obipo(1);
1785                                 else if((G.qual==0))
1786                                         nextkey_obipo(1);       /* in editipo.c */
1787                                 break;
1788
1789                         case PAGEDOWNKEY:
1790                                 if(G.qual==LR_CTRLKEY)
1791                                         movekey_obipo(-1);
1792                                 else if((G.qual==0))
1793                                         nextkey_obipo(-1);
1794                                 break;
1795                                 
1796                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1797                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1798                         case PADENTER:
1799                                 persptoetsen(event);
1800                                 doredraw= 1;
1801                                 break;
1802                         case PADMINUS:
1803                                 if ( (G.qual==LR_CTRLKEY)
1804                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
1805                                         select_less();
1806                                 else {
1807                                         persptoetsen(event);
1808                                         doredraw= 1;
1809                                 }
1810                                 break;
1811
1812                         case PADPLUSKEY:
1813                                 if ( (G.qual==LR_CTRLKEY)
1814                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
1815                                         select_more();
1816                                 else {
1817                                         persptoetsen(event);
1818                                         doredraw= 1;
1819                                 }
1820                                 break;
1821
1822                         case ESCKEY:
1823                                 if(G.qual==0) {
1824                                         if (G.vd->flag & V3D_DISPIMAGE) {
1825                                                 G.vd->flag &= ~V3D_DISPIMAGE;
1826                                                 doredraw= 1;
1827                                         }
1828                                 }
1829                                 break;
1830                         }
1831                 }
1832         }
1833         
1834         if(doredraw) {
1835                 scrarea_queue_winredraw(curarea);
1836                 scrarea_queue_headredraw(curarea);
1837         }
1838 }
1839
1840 static void initview3d(ScrArea *sa)
1841 {
1842         View3D *vd;
1843         
1844         vd= MEM_callocN(sizeof(View3D), "initview3d");
1845         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
1846
1847         vd->spacetype= SPACE_VIEW3D;
1848         vd->blockscale= 0.7f;
1849         vd->viewquat[0]= 1.0f;
1850         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
1851         vd->persp= 1;
1852         vd->drawtype= OB_WIRE;
1853         vd->view= 7;
1854         vd->dist= 10.0;
1855         vd->lens= 35.0f;
1856         vd->near= 0.01f;
1857         vd->far= 500.0f;
1858         vd->grid= 1.0f;
1859         vd->gridlines= 16;
1860         vd->lay= vd->layact= 1;
1861         if(G.scene) {
1862                 vd->lay= vd->layact= G.scene->lay;
1863                 vd->camera= G.scene->camera;
1864         }
1865         vd->scenelock= 1;
1866         vd->gridflag |= V3D_SHOW_X;
1867         vd->gridflag |= V3D_SHOW_Y;
1868         vd->gridflag |= V3D_SHOW_FLOOR;
1869         vd->gridflag &= ~V3D_SHOW_Z;
1870 }
1871
1872
1873 /* ******************** SPACE: IPO ********************** */
1874
1875 static void changeview2dspace(ScrArea *sa, void *spacedata)
1876 {
1877         if(G.v2d==0) return;
1878
1879         test_view2d(G.v2d, curarea->winx, curarea->winy);
1880         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1881 }
1882
1883 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1884 {
1885         extern void do_ipobuts(unsigned short event);   // drawipo.c
1886         unsigned short event= evt->event;
1887         short val= evt->val;
1888         SpaceIpo *sipo= curarea->spacedata.first;
1889         View2D *v2d= &sipo->v2d;
1890         float dx, dy;
1891         int cfra, doredraw= 0;
1892         short mval[2];
1893         short mousebut = L_MOUSE;
1894         
1895         if(sa->win==0) return;
1896
1897         if(val) {
1898                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
1899
1900                 /* swap mouse buttons based on user preference */
1901                 if (U.flag & USER_LMOUSESELECT) {
1902                         if (event == LEFTMOUSE) {
1903                                 event = RIGHTMOUSE;
1904                                 mousebut = L_MOUSE;
1905                         } else if (event == RIGHTMOUSE) {
1906                                 event = LEFTMOUSE;
1907                                 mousebut = R_MOUSE;
1908                         }
1909                 }
1910
1911                 switch(event) {
1912                 case UI_BUT_EVENT:
1913                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
1914                         if(val>0 && val < 65) do_ipowin_buts(val-1);
1915                         else do_ipobuts(val);
1916                         break;
1917                         
1918                 case LEFTMOUSE:
1919                         if( in_ipo_buttons() ) {
1920                                 do_ipo_selectbuttons();
1921                                 doredraw= 1;
1922                         }
1923                         else if(view2dmove(LEFTMOUSE)); // only checks for sliders
1924                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
1925                         else {
1926                                 do {
1927                                         getmouseco_areawin(mval);
1928                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
1929                                         
1930                                         cfra= (int)dx;
1931                                         if(cfra< 1) cfra= 1;
1932                                         
1933                                         if( cfra!=CFRA ) {
1934                                                 CFRA= cfra;
1935                                                 update_for_newframe();
1936                                                 force_draw_all(0); /* To make constraint sliders redraw */
1937                                         }
1938                                         else PIL_sleep_ms(30);
1939                                 
1940                                 } while(get_mbut() & mousebut);
1941                         }
1942                         break;
1943                 case RIGHTMOUSE:
1944                         mouse_select_ipo();
1945                         allqueue (REDRAWACTION, 0);
1946                         allqueue(REDRAWNLA, 0);
1947                         break;
1948                 case MIDDLEMOUSE:
1949                         if(in_ipo_buttons()) {
1950                                 scroll_ipobuts();
1951                         }
1952                         else view2dmove(event); /* in drawipo.c */
1953                         break;
1954                 case WHEELUPMOUSE:
1955                 case WHEELDOWNMOUSE:
1956                         view2dmove(event);      /* in drawipo.c */
1957                         break;
1958                 case PADPLUSKEY:
1959                         view2d_zoom(v2d, 0.1154f, sa->winx, sa->winy);
1960                         doredraw= 1;
1961                         break;
1962                 case PADMINUS:
1963                         view2d_zoom(v2d, -0.15f, sa->winx, sa->winy);
1964                         doredraw= 1;
1965                         break;
1966                 case PAGEUPKEY:
1967                         if(G.qual==LR_CTRLKEY)
1968                                 movekey_ipo(1);
1969                         else if((G.qual==0))
1970                                 nextkey_ipo(1);
1971                         break;
1972                 case PAGEDOWNKEY:
1973                         if(G.qual==LR_CTRLKEY)
1974                                 movekey_ipo(-1);
1975                         else if((G.qual==0))
1976                                 nextkey_ipo(-1);
1977                         break;
1978                 case HOMEKEY:
1979                         if((G.qual==0))
1980                                 do_ipo_buttons(B_IPOHOME);
1981                         break;
1982                         
1983                 case AKEY:
1984                         if((G.qual==0)) {
1985                                 if(in_ipo_buttons()) {
1986                                         swap_visible_editipo();
1987                                 }
1988                                 else {
1989                                         swap_selectall_editipo();
1990                                 }
1991                                 allspace (REMAKEIPO, 0);
1992                                 allqueue (REDRAWNLA, 0);
1993                                 allqueue (REDRAWACTION, 0);
1994                         }
1995                         break;
1996                 case BKEY:
1997                         if((G.qual==0))
1998                                 borderselect_ipo();
1999                         break;
2000                 case CKEY:
2001                         if((G.qual==0))
2002                                 move_to_frame();
2003                         break;
2004                 case DKEY:
2005                         if((G.qual==LR_SHIFTKEY))
2006                                 add_duplicate_editipo();
2007                         break;
2008                 case GKEY:
2009                         if((G.qual==0))
2010                                 transform_ipo('g');
2011                         break;
2012                 case HKEY:
2013                         if(G.qual==LR_ALTKEY)
2014                                 sethandles_ipo(4);              // tsk tsk ton!
2015                         if(G.qual==LR_SHIFTKEY)
2016                                 sethandles_ipo(HD_AUTO);
2017                         else if(G.qual==0)
2018                                 sethandles_ipo(HD_ALIGN);
2019                         break;
2020                 case JKEY:
2021                         if((G.qual==0))
2022                                 join_ipo_menu();
2023                         break;
2024                 case KKEY:
2025                         if((G.qual==0)) {
2026                                 ipo_toggle_showkey();
2027                                 scrarea_queue_headredraw(curarea);
2028                                 allqueue(REDRAWVIEW3D, 0);
2029                                 doredraw= 1;
2030                         }
2031                         break;
2032                 case NKEY:
2033                         toggle_blockhandler(sa, IPO_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2034                         doredraw= 1;
2035                         break;
2036                 case RKEY:
2037                         if((G.qual==0))
2038                                 ipo_record();
2039                         break;
2040                 case SKEY:
2041                         if((G.qual==LR_SHIFTKEY)) {             
2042                                 ipo_snap_menu();
2043                         } else if((G.qual==0))
2044                                 transform_ipo('s');
2045                         break;
2046                 case TKEY:
2047                         if((G.qual==0))
2048                                 set_ipotype();
2049                         break;
2050                 case VKEY:
2051                         if((G.qual==0))
2052                                 sethandles_ipo(HD_VECT);
2053                         break;
2054                 case XKEY:
2055                 case DELKEY:
2056                         if((G.qual==LR_SHIFTKEY))
2057                                 delete_key();
2058                         else if((G.qual==0))
2059                                 del_ipo();
2060                         break;
2061                 }
2062         }
2063
2064         if(doredraw) scrarea_queue_winredraw(sa);
2065 }
2066
2067 void initipo(ScrArea *sa)
2068 {
2069         SpaceIpo *sipo;
2070         
2071         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
2072         BLI_addhead(&sa->spacedata, sipo);
2073
2074         sipo->spacetype= SPACE_IPO;
2075         sipo->blockscale= 0.7f;
2076         
2077         /* sipo space loopt van (0,-?) tot (??,?) */
2078         sipo->v2d.tot.xmin= 0.0;
2079         sipo->v2d.tot.ymin= -10.0;
2080         sipo->v2d.tot.xmax= G.scene->r.efra;
2081         sipo->v2d.tot.ymax= 10.0;
2082
2083         sipo->v2d.cur= sipo->v2d.tot;
2084
2085         sipo->v2d.min[0]= 0.01f;
2086         sipo->v2d.min[1]= 0.01f;
2087
2088         sipo->v2d.max[0]= 15000.0f;
2089         sipo->v2d.max[1]= 10000.0f;
2090         
2091         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
2092         sipo->v2d.keeptot= 0;
2093
2094         sipo->blocktype= ID_OB;
2095 }
2096
2097 /* ******************** SPACE: INFO ********************** */
2098
2099 void space_mipmap_button_function(int event) {
2100         set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
2101
2102         allqueue(REDRAWVIEW3D, 0);
2103 }
2104
2105 #if 0
2106 static void space_sound_button_function(int event)
2107 {
2108         int a;
2109         SYS_SystemHandle syshandle;
2110
2111         if ((syshandle = SYS_GetSystem()))
2112         {
2113                 a = (U.gameflags & USER_DISABLE_SOUND);
2114                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
2115         }
2116 }
2117 #endif
2118
2119 #define B_ADD_THEME     3301
2120 #define B_DEL_THEME     3302
2121 #define B_NAME_THEME    3303
2122 #define B_THEMECOL              3304
2123 #define B_UPDATE_THEME  3305
2124 #define B_CHANGE_THEME  3306
2125 #define B_THEME_COPY    3307
2126 #define B_THEME_PASTE   3308
2127
2128 #define B_RECALCLIGHT   3310
2129
2130 // needed for event; choose new 'curmain' resets it...
2131 static short th_curcol= TH_BACK;
2132 static char *th_curcol_ptr= NULL;
2133 static char th_curcol_arr[4]={0, 0, 0, 255};
2134
2135 static void info_user_themebuts(uiBlock *block, short y1, short y2, short y3)
2136 {
2137         bTheme *btheme, *bt;
2138         int spacetype= 0;
2139         static short cur=1, curmain=2;
2140         short a, tot=0, isbuiltin= 0;
2141         char string[21*32], *strp, *col;
2142         
2143         y3= y2+23;      // exception!
2144         
2145         /* count total, max 16! */
2146         for(bt= U.themes.first; bt; bt= bt->next) tot++;
2147         
2148         /* if cur not is 1; move that to front of list */
2149         if(cur!=1) {
2150                 a= 1;
2151                 for(bt= U.themes.first; bt; bt= bt->next, a++) {
2152                         if(a==cur) {
2153                                 BLI_remlink(&U.themes, bt);
2154                                 BLI_addhead(&U.themes, bt);
2155                                 allqueue(REDRAWALL, 0);
2156                                 cur= 1;
2157                                 break;
2158                         }
2159                 }
2160         }
2161         
2162         /* the current theme */
2163         btheme= U.themes.first;
2164         if(strcmp(btheme->name, "Default")==0) isbuiltin= 1;
2165
2166         /* construct popup script */
2167         string[0]= 0;
2168         for(bt= U.themes.first; bt; bt= bt->next) {
2169                 strcat(string, bt->name);
2170                 if(btheme->next) strcat(string, "   |");
2171         }
2172         uiDefButS(block, MENU, B_UPDATE_THEME, string,                  45,y3,200,20, &cur, 0, 0, 0, 0, "Current theme");
2173         
2174         /* add / delete / name */
2175
2176         if(tot<16)
2177                 uiDefBut(block, BUT, B_ADD_THEME, "Add",        45,y2,200,20, NULL, 0, 0, 0, 0, "Makes new copy of this theme");
2178         if(tot>1 && isbuiltin==0)
2179                 uiDefBut(block, BUT, B_DEL_THEME, "Delete", 45,y1,200,20, NULL, 0, 0, 0, 0, "Delete theme");
2180
2181         if(isbuiltin) return;
2182         
2183         /* name */
2184         uiDefBut(block, TEX, B_NAME_THEME, "",                  255,y3,200,20, btheme->name, 1.0, 30.0, 0, 0, "Rename theme");
2185
2186         /* main choices pup */
2187         uiDefButS(block, MENU, B_CHANGE_THEME, "UI and Buttons %x1|%l|3D View %x2|%l|Ipo Curve Editor %x3|Action Editor %x4|"
2188                 "NLA Editor %x5|%l|UV/Image Editor %x6|Video Sequence Editor %x7|Timeline %x15|Audio Window %x8|Text Editor %x9|%l|User Preferences %x10|"
2189                 "Outliner %x11|Buttons Window %x12|%l|File Browser %x13|Image Browser %x14",
2190                                                                                                         255,y2,200,20, &curmain, 0, 0, 0, 0, "Specify theme for...");
2191         if(curmain==1) spacetype= 0;
2192         else if(curmain==2) spacetype= SPACE_VIEW3D;
2193         else if(curmain==3) spacetype= SPACE_IPO;
2194         else if(curmain==4) spacetype= SPACE_ACTION;
2195         else if(curmain==5) spacetype= SPACE_NLA;
2196         else if(curmain==6) spacetype= SPACE_IMAGE;
2197         else if(curmain==7) spacetype= SPACE_SEQ;
2198         else if(curmain==8) spacetype= SPACE_SOUND;
2199         else if(curmain==9) spacetype= SPACE_TEXT;
2200         else if(curmain==10) spacetype= SPACE_INFO;
2201         else if(curmain==11) spacetype= SPACE_OOPS;
2202         else if(curmain==12) spacetype= SPACE_BUTS;
2203         else if(curmain==13) spacetype= SPACE_FILE;
2204         else if(curmain==14) spacetype= SPACE_IMASEL;
2205         else if(curmain==15) spacetype= SPACE_TIME;
2206         else return; // only needed while coding... when adding themes for more windows
2207         
2208         /* color choices pup */
2209         if(curmain==1) {
2210                 strp= BIF_ThemeColorsPup(0);
2211                 if(th_curcol==TH_BACK) th_curcol= TH_BUT_OUTLINE;  // switching main choices...
2212         }
2213         else strp= BIF_ThemeColorsPup(spacetype);
2214         
2215         uiDefButS(block, MENU, B_REDR, strp,                    255,y1,200,20, &th_curcol, 0, 0, 0, 0, "Current color");
2216         MEM_freeN(strp);
2217         
2218         th_curcol_ptr= col= BIF_ThemeGetColorPtr(btheme, spacetype, th_curcol);
2219         if(col==NULL) return;
2220         
2221         /* first handle exceptions, special single values, row selection, etc */
2222         if(th_curcol==TH_VERTEX_SIZE) {
2223                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Vertex size ", 465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2224         }
2225         else if(th_curcol==TH_FACEDOT_SIZE) {
2226                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Face dot size ",       465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2227         }
2228         else if(th_curcol==TH_BUT_DRAWTYPE) {
2229                 uiBlockBeginAlign(block);
2230                 uiDefButC(block, ROW, B_UPDATE_THEME, "Minimal",        465,y3,100,20,  col, 2.0, 0.0, 0, 0, "");
2231                 uiDefButC(block, ROW, B_UPDATE_THEME, "Shaded", 565,y3,100,20,  col, 2.0, 1.0, 0, 0, "");
2232                 uiDefButC(block, ROW, B_UPDATE_THEME, "Rounded",        465,y2,100,20,  col, 2.0, 2.0, 0, 0, "");
2233                 uiDefButC(block, ROW, B_UPDATE_THEME, "OldSkool",       565,y2,100,20,  col, 2.0, 3.0, 0, 0, "");
2234                 uiBlockEndAlign(block);
2235         }
2236         else {
2237                 uiBlockBeginAlign(block);
2238                 if ELEM6(th_curcol, TH_PANEL, TH_FACE, TH_FACE_SELECT, TH_MENU_BACK, TH_MENU_HILITE, TH_MENU_ITEM) {
2239                         uiDefButC(block, NUMSLI, B_UPDATE_THEME,"A ",   465,y3+25,200,20,  col+3, 0.0, 255.0, B_THEMECOL, 0, "");
2240                 }
2241                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"R ",   465,y3,200,20,  col, 0.0, 255.0, B_THEMECOL, 0, "");
2242                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"G ",   465,y2,200,20,  col+1, 0.0, 255.0, B_THEMECOL, 0, "");
2243                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"B ",   465,y1,200,20,  col+2, 0.0, 255.0, B_THEMECOL, 0, "");
2244                 uiBlockEndAlign(block);
2245
2246                 uiDefButC(block, COL, B_UPDATE_THEME, "",               675,y1,50,y3-y1+20, col, 0, 0, 0, 0, "");
2247                                 
2248                 /* copy paste */
2249                 uiBlockBeginAlign(block);
2250                 uiDefBut(block, BUT, B_THEME_COPY, "Copy Color",        755,y2,120,20, NULL, 0, 0, 0, 0, "Stores current color in buffer");
2251                 uiDefBut(block, BUT, B_THEME_PASTE, "Paste Color",      755,y1,120,20, NULL, 0, 0, 0, 0, "Pastes buffer color");
2252                 uiBlockEndAlign(block);
2253                 
2254                 uiDefButC(block, COL, 0, "",                            885,y1,50,y2-y1+20, th_curcol_arr, 0, 0, 0, 0, "");
2255                 
2256         }
2257 }
2258
2259
2260 void drawinfospace(ScrArea *sa, void *spacedata)
2261 {
2262         uiBlock *block;
2263         static short cur_light=0, cur_light_var=0;
2264         float fac, col[3];
2265         short xpos, ypos, ypostab,  buth, rspace, dx, y1, y2, y3, y4, y5, y6;
2266         short y2label, y3label, y4label, y5label, y6label;
2267         short spref, mpref, lpref, smfileselbut;
2268         short edgsp, midsp;
2269         char naam[32];
2270
2271         if(curarea->win==0) return;
2272
2273         BIF_GetThemeColor3fv(TH_BACK, col);
2274         glClearColor(col[0], col[1], col[2], 0.0);
2275         glClear(GL_COLOR_BUFFER_BIT);
2276
2277         if(curarea->winx<=1280.0) {
2278                 fac= ((float)curarea->winx)/1280.0f;
2279                 myortho2(0.375f, 1280.375f, 0.375f, curarea->winy/fac + 0.375f);
2280         }
2281         else {
2282                 myortho2(0.375f, (float)curarea->winx + 0.375f, 0.375f, (float)curarea->winy + 0.375f);
2283         }
2284         
2285         sprintf(naam, "infowin %d", curarea->win);
2286         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->win);
2287
2288
2289         /* Vars for nice grid alignment */ 
2290         dx= (1280-90)/7;        /* spacing for use in equally dividing 'tab' row */
2291
2292         xpos = 45;              /* left padding */
2293         ypos = 50;              /* bottom padding for buttons */
2294         ypostab = 10;           /* bottom padding for 'tab' row */
2295
2296         buth = 20;              /* standard button height */
2297
2298         spref = 90;     /* standard size for small preferences button */
2299         mpref = 189;    /* standard size for medium preferences button */
2300         lpref = 288;    /* standard size for large preferences button */
2301         smfileselbut = buth;    /* standard size for fileselect button (square) */
2302
2303         edgsp = 3;              /* space from edge of end 'tab' to edge of end button */
2304         midsp = 9;              /* horizontal space between buttons */
2305
2306         rspace = 3;             /* default space between rows */
2307
2308         y1 = ypos;              /* grid alignment for each row of buttons */
2309         y2 = ypos+buth+rspace;
2310         y3 = ypos+2*(buth+rspace);
2311         y4 = ypos+3*(buth+rspace);
2312         y5 = ypos+4*(buth+rspace);
2313         y6 = ypos+5*(buth+rspace);
2314
2315
2316         y2label = y2-2;         /* adjustments to offset the labels down to align better */
2317         y3label = y3-2;
2318         y4label = y4-2;
2319         y5label = y5-2;
2320         y6label = y6-2;
2321
2322
2323         /* set the colour to blue and draw the main 'tab' controls */
2324
2325         uiBlockSetCol(block, TH_BUT_SETTING1);
2326         uiBlockBeginAlign(block);
2327         
2328         uiDefButS(block, ROW,B_USERPREF,"View & Controls",
2329                 xpos,ypostab,(short)dx,buth,
2330                 &U.userpref,1.0,0.0, 0, 0,"");
2331                 
2332         uiDefButS(block, ROW,B_USERPREF,"Edit Methods",
2333                 (short)(xpos+dx),ypostab,(short)dx,buth,
2334                 &U.userpref,1.0,1.0, 0, 0,"");
2335
2336         uiDefButS(block, ROW,B_USERPREF,"Language & Font",
2337                 (short)(xpos+2*dx),ypostab,(short)dx,buth,
2338                 &U.userpref,1.0,2.0, 0, 0,"");
2339
2340         uiDefButS(block, ROW,B_USERPREF,"Themes",
2341                 (short)(xpos+3*dx),ypostab,(short)dx,buth,
2342                 &U.userpref,1.0,6.0, 0, 0,"");
2343
2344         uiDefButS(block, ROW,B_USERPREF,"Auto Save",
2345                 (short)(xpos+4*dx),ypostab,(short)dx,buth,
2346                 &U.userpref,1.0,3.0, 0, 0,"");
2347
2348         uiDefButS(block, ROW,B_USERPREF,"System & OpenGL",
2349                 (short)(xpos+5*dx),ypostab,(short)dx,buth,
2350                 &U.userpref,1.0,4.0, 0, 0,"");
2351                 
2352         uiDefButS(block, ROW,B_USERPREF,"File Paths",
2353                 (short)(xpos+6*dx),ypostab,(short)dx,buth,
2354                 &U.userpref,1.0,5.0, 0, 0,"");
2355
2356         uiBlockSetCol(block, TH_AUTO);
2357         uiBlockEndAlign(block);
2358         /* end 'tab' controls */
2359
2360         /* line 2: left x co-ord, top y co-ord, width, height */
2361
2362         if(U.userpref == 6) {
2363                 info_user_themebuts(block, y1, y2, y3);
2364         }
2365         else if (U.userpref == 0) { /* view & controls */
2366
2367                 uiDefBut(block, LABEL,0,"Display:",
2368                         xpos,y6label,spref,buth,
2369                         0, 0, 0, 0, 0, "");     
2370                 uiBlockBeginAlign(block);
2371                 uiDefButBitI(block, TOG, USER_TOOLTIPS, 0, "ToolTips",
2372                         (xpos+edgsp),y5,spref,buth,
2373                         &(U.flag), 0, 0, 0, 0,
2374                         "Display tooltips (help tags) over buttons");
2375                 uiDefButBitI(block, TOG, USER_DRAWVIEWINFO, B_DRAWINFO, "Object Info",
2376                         (xpos+edgsp),y4,spref,buth,
2377                         &(U.uiflag), 0, 0, 0, 0,
2378                         "Display active object name and frame number in the 3D View");
2379                 uiDefButBitI(block, TOG, USER_SCENEGLOBAL, 0, "Global Scene",
2380                         (xpos+edgsp),y3,spref,buth,
2381                         &(U.flag), 0, 0, 0, 0,
2382                         "Forces the current Scene to be displayed in all Screens");
2383 #ifndef __APPLE__       
2384                 uiDefButS(block, TOG|BIT|0, 0, "Large Cursors",
2385                         (xpos+edgsp),y2,spref,buth,
2386                         &(U.curssize), 0, 0, 0, 0,
2387                         "Use large mouse cursors when available");
2388 #else 
2389                 U.curssize=0; /*Small Cursor always for OS X for now */
2390 #endif
2391                 uiBlockEndAlign(block);
2392
2393                 uiDefBut(block, LABEL,0,"Menus:",
2394                         (xpos+(2*edgsp)+spref),y6label,spref,buth,
2395                         0, 0, 0, 0, 0, "");
2396                 uiBlockBeginAlign(block);
2397                 uiDefButBitI(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                 uiBlockEndAlign(block);
2410
2411                 uiDefBut(block, LABEL,0,"Toolbox click-hold delay:",
2412                         (xpos+(2*edgsp)+spref),y3label,mpref,buth,
2413                         0, 0, 0, 0, 0, "");
2414                 uiBlockBeginAlign(block);
2415                 uiDefButS(block, NUM, 0, "LMB:",
2416                         (xpos+edgsp+spref+midsp),y2,spref+edgsp,buth,
2417                         &(U.tb_leftmouse), 2, 40, 0, 0,
2418                         "Time in 1/10 seconds to hold the Left Mouse Button before opening the toolbox");
2419                 uiDefButS(block, NUM, 0, "RMB:",
2420                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y2,spref+edgsp,buth,
2421                         &(U.tb_rightmouse), 2, 40, 0, 0,
2422                         "Time in 1/10 seconds to hold the Right Mouse Button before opening the toolbox");      
2423                 uiBlockEndAlign(block);
2424
2425                 uiDefButBitI(block, TOG, USER_PANELPINNED, 0, "Pin Floating Panels",
2426                         (xpos+edgsp+spref+midsp),y1,(mpref/2),buth,
2427                         &(U.uiflag), 0, 0, 0, 0,
2428                         "Make floating panels invoked by a hotkey (eg. N Key) open at the previous location");
2429                 
2430                 uiDefButBitI(block, TOG, USER_LOCKAROUND, B_DRAWINFO, "Global Pivot",
2431                         (xpos+edgsp+spref+midsp+(mpref/2)),y1,(mpref/2),buth,
2432                         &(U.uiflag), 0, 0, 0, 0,
2433                         "Lock the same rotation/scaling pivot in all 3D Views");        
2434                 
2435                 uiDefBut(block, LABEL,0,"Snap to grid:",
2436                         (xpos+(2*edgsp)+spref+midsp+mpref),y6label,mpref,buth,
2437                         0, 0, 0, 0, 0, "");
2438                 uiBlockBeginAlign(block);
2439                 uiDefButBitI(block, TOG, USER_AUTOGRABGRID, 0, "Grab/Move",
2440                         (xpos+edgsp+mpref+spref+(2*midsp)),y5,spref,buth,
2441                         &(U.flag), 0, 0, 0, 0,
2442                         "Snap objects and sub-objects to grid units when moving");
2443                 uiDefButBitI(block, TOG, USER_AUTOROTGRID, 0, "Rotate",
2444                         (xpos+edgsp+mpref+spref+(2*midsp)),y4,spref,buth,
2445                         &(U.flag), 0, 0, 0, 0,
2446                         "Snap objects and sub-objects to grid units when rotating");
2447                 uiDefButBitI(block, TOG, USER_AUTOSIZEGRID, 0, "Scale",
2448                         (xpos+edgsp+mpref+spref+(2*midsp)),y3,spref,buth,
2449                         &(U.flag), 0, 0, 0, 0,
2450                         "Snap objects and sub-objects to grid units when scaling");
2451                 uiBlockEndAlign(block);
2452                 
2453                 uiDefBut(block, LABEL,0,"View zoom:",
2454                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y6label,mpref,buth,
2455                         0, 0, 0, 0, 0, "");
2456                 uiBlockBeginAlign(block);
2457                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2458                 uiDefButS(block, ROW, 0, "Continue",
2459                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y5,(mpref/3),buth,
2460                         &(U.viewzoom), 40, USER_ZOOM_CONT, 0, 0,
2461                         "Old style zoom, continues while moving mouse up or down");
2462                 uiDefButS(block, ROW, 0, "Dolly",
2463                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/3)),y5,(mpref/3),buth,
2464                         &(U.viewzoom), 40, USER_ZOOM_DOLLY, 0, 0,
2465                         "Zooms in and out based on vertical mouse movement.");
2466                 uiDefButS(block, ROW, 0, "Scale",
2467                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(2*mpref/3)),y5,(mpref/3),buth,
2468                         &(U.viewzoom), 40, USER_ZOOM_SCALE, 0, 0,
2469                         "Zooms in and out like scaling the view, mouse movements relative to center.");
2470                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2471                 uiBlockEndAlign(block);
2472                 
2473                 uiDefBut(block, LABEL,0,"View rotation:",
2474                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y4label,mpref,buth,
2475                         0, 0, 0, 0, 0, "");
2476                 uiBlockBeginAlign(block);
2477                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2478                 uiDefButBitI(block, TOG, USER_TRACKBALL, B_DRAWINFO, "Trackball",
2479                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y3,(mpref/2),buth,
2480                         &(U.flag), 0, 0, 0, 0,
2481                         "Allow the view to tumble freely when orbiting with the Middle Mouse Button");
2482                 uiDefButBitI(block, TOGN, USER_TRACKBALL, B_DRAWINFO, "Turntable",
2483                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y3,(mpref/2),buth,
2484                         &(U.flag), 0, 0, 0, 0,
2485                         "Allow the view to tumble freely when orbiting with the Middle Mouse Button");
2486                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2487                 uiDefButBitI(block, TOG, USER_AUTOPERSP, B_DRAWINFO, "Auto Perspective",
2488                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y2,(mpref/2),buth,
2489                         &(U.uiflag), 0, 0, 0, 0,
2490                         "Automatically switch between orthographic and perspective when changing from top/front/side views");
2491                 uiDefButBitI(block, TOG, USER_ORBIT_SELECTION, B_DRAWINFO, "Around Active",
2492                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y2,(mpref/2),buth,
2493                         &(U.uiflag), 0, 0, 0, 0,
2494                         "Keep the active object in place when orbiting the views (Object Mode)");
2495                 uiBlockEndAlign(block);
2496
2497                 uiDefBut(block, LABEL,0,"Select with:",
2498                         (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y6label,mpref,buth,
2499                         0, 0, 0, 0, 0, "");
2500                 uiBlockBeginAlign(block);
2501                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2502                 uiDefButBitI(block, TOG, USER_LMOUSESELECT, B_DRAWINFO, "Left Mouse",
2503                         (xpos+edgsp+(3*mpref)+(4*midsp)),y5,(mpref/2),buth,
2504                         &(U.flag), 0, 0, 0, 0, "Use the Left Mouse Button for selection");
2505                 uiDefButBitI(block, TOGN, USER_LMOUSESELECT, B_DRAWINFO, "Right Mouse",
2506                         (xpos+edgsp+(3*mpref)+(4*midsp)+(mpref/2)),y5,(mpref/2),buth,
2507                         &(U.flag), 0, 0, 0, 0, "Use the Right Mouse Button for selection");
2508                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2509                 uiBlockEndAlign(block);
2510                 
2511                 
2512                 if(U.flag & USER_LMOUSESELECT) {
2513                         uiDefBut(block, LABEL,0,"Cursor with: Right Mouse",
2514                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2515                                 0, 0, 0, 0, 0, "");
2516                 } else {
2517                         uiDefBut(block, LABEL,0,"Cursor with: Left Mouse",
2518                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2519                                 0, 0, 0, 0, 0, "");
2520                 }
2521                 
2522                 /* illegal combo... */
2523                 if (U.flag & USER_LMOUSESELECT) 
2524                         U.flag &= ~USER_TWOBUTTONMOUSE;
2525                 
2526                 uiDefButBitI(block, TOG, USER_TWOBUTTONMOUSE, B_DRAWINFO, "Emulate 3 Button Mouse",
2527                         (xpos+edgsp+(3*mpref)+(4*midsp)),y3,mpref,buth,
2528                         &(U.flag), 0, 0, 0, 0,
2529                         "Emulates Middle Mouse with Alt+LeftMouse (doesnt work with Left Mouse Select option)");
2530                 
2531                         
2532                 uiDefBut(block, LABEL,0,"Middle Mouse Button:",
2533                         (xpos+(2*edgsp)+(4*mpref)+(4*midsp)),y6label,mpref,buth,
2534                         0, 0, 0, 0, 0, "");
2535                 uiBlockBeginAlign(block);
2536                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2537                 uiDefButBitI(block, TOGN, USER_VIEWMOVE, B_DRAWINFO, "Rotate View",
2538                         (xpos+edgsp+(4*mpref)+(5*midsp)),y5,(mpref/2),buth,
2539                         &(U.flag), 0, 0, 0, 0, "Default action for the Middle Mouse Button");
2540                 uiDefButBitI(block, TOG, USER_VIEWMOVE, B_DRAWINFO, "Pan View",
2541                         (xpos+edgsp+(4*mpref)+(5*midsp)+(mpref/2)),y5,(mpref/2),buth,
2542                         &(U.flag), 0, 0, 0, 0, "Default action for the Middle Mouse Button");
2543                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2544                 uiBlockEndAlign(block);
2545                         
2546                 uiDefBut(block, LABEL,0,"Mouse Wheel:",
2547                         (xpos+(2*edgsp)+(4*mpref)+(4*midsp)),y4label,mpref,buth,
2548                         0, 0, 0, 0, 0, "");
2549                 uiBlockBeginAlign(block);
2550                 uiDefButBitI(block, TOG, USER_WHEELZOOMDIR, 0, "Invert Zoom",
2551                         (xpos+edgsp+(4*mpref)+(5*midsp)),y3,spref,buth,
2552                         &(U.uiflag), 0, 0, 0, 0,
2553                         "Swap the Mouse Wheel zoom direction");
2554                 uiDefButI(block, NUM, 0, "Scroll Lines:",
2555                         (xpos+edgsp+(4*mpref)+(6*midsp)+spref-edgsp),y3,spref+edgsp,buth,
2556                         &U.wheellinescroll, 0.0, 32.0, 0, 0,
2557                         "The number of lines scrolled at a time with the mouse wheel"); 
2558                 uiBlockEndAlign(block);
2559
2560
2561                 uiDefBut(block, LABEL,0,"3D Transform Widget:",
2562                                  (xpos+(2*edgsp)+(5*mpref)+(5*midsp)),y6label,mpref,buth,
2563                                  0, 0, 0, 0, 0, "");
2564                 uiBlockBeginAlign(block);
2565                 uiDefButS(block, NUM, B_REDRCURW3D, "Size:",
2566                                          (xpos+edgsp+(5*mpref)+(6*midsp)),y5,(mpref/2),buth,
2567                                          &(U.tw_size), 2, 40, 0, 0, "Size of widget as percentage of window size");
2568                 uiDefButS(block, NUM, B_REDRCURW3D, "Handle:",
2569                                          (xpos+edgsp+(5*mpref)+(6*midsp)+(mpref/2)),y5,(mpref/2),buth,
2570                                          &(U.tw_handlesize), 2, 40, 0, 0, "Size of widget handles as percentage of widget radius");
2571                 uiDefButS(block, NUM, B_REDRCURW3D, "Hotspot:",
2572                                   (xpos+edgsp+(5*mpref)+(6*midsp)),y4,(mpref),buth,
2573                                   &(U.tw_hotspot), 4, 40, 0, 0, "Hotspot in pixels for clicking widget handles");
2574                 
2575                 uiBlockEndAlign(block);
2576                 
2577         } else if (U.userpref == 1) { /* edit methods */
2578
2579
2580                 uiDefBut(block, LABEL,0,"Material linked to:",
2581                         xpos,y3label,mpref,buth,
2582                         0, 0, 0, 0, 0, "");
2583                 uiBlockBeginAlign(block);
2584                 uiDefButBitI(block, TOGN, USER_MAT_ON_OB, B_DRAWINFO, "ObData",
2585                         (xpos+edgsp),y2,(mpref/2),buth,
2586                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the obData block");
2587                 uiDefButBitI(block, TOG, USER_MAT_ON_OB, B_DRAWINFO, "Object",
2588                         (xpos+edgsp+(mpref/2)),y2,(mpref/2),buth,
2589                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the object block");
2590                 uiBlockEndAlign(block);
2591
2592
2593                 uiDefBut(block, LABEL,0,"Editmode undo:",
2594                         (xpos+(2*edgsp)+mpref),y3label, mpref,buth,
2595                         0, 0, 0, 0, 0, "");
2596                 uiBlockBeginAlign(block);
2597                 uiDefButS(block, NUMSLI, B_DRAWINFO, "Steps:",
2598                         (xpos+edgsp+mpref+midsp),y2,mpref,buth,
2599                         &(U.undosteps), 2, 64, 0, 0, "Number of undo steps available in Edit Mode (smaller values conserve memory)");
2600
2601                 uiDefButBitI(block, TOG, USER_GLOBALUNDO, B_DRAWINFO, "Global undo",
2602                         (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2603                         &(U.uiflag), 2, 64, 0, 0, "");
2604                 uiBlockEndAlign(block);
2605
2606
2607                 uiDefBut(block, LABEL,0,"Auto keyframe",
2608                         (xpos+(2*edgsp)+(2*mpref)+midsp),y3label,mpref,buth,
2609                         0, 0, 0, 0, 0, "");
2610                 uiDefButBitI(block, TOG, G_RECORDKEYS, REDRAWTIME, "Action and Object", 
2611                                         (xpos+edgsp+(2*mpref)+(2*midsp)),y2,mpref, buth,
2612                                          &(G.flags), 0, 0, 0, 0, "Automatic keyframe insertion in Object and Action Ipo curves");
2613
2614 //              uiDefButBitS(block, TOG, USER_KEYINSERTACT, 0, "Action",
2615 //                      (xpos+edgsp+(2*mpref)+(2*midsp)),y2,(spref+edgsp),buth,
2616 //                      &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in Action Ipo curve");
2617 //              uiDefButBitS(block, TOG, USER_KEYINSERTOBJ, 0, "Object",
2618 //                      (xpos+edgsp+(2*mpref)+(3*midsp)+spref-edgsp),y2,(spref+edgsp),buth,
2619 //                      &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in Object Ipo curve");
2620
2621
2622                 uiDefBut(block, LABEL,0,"Duplicate with object:",
2623                         (xpos+(2*edgsp)+(3*midsp)+(3*mpref)+spref),y3label,mpref,buth,
2624                         0, 0, 0, 0, 0, "");
2625
2626                 uiBlockBeginAlign(block);
2627                 uiDefButBitI(block, TOG, USER_DUP_MESH, 0, "Mesh",
2628                         (xpos+edgsp+(4*midsp)+(3*mpref)+spref),y2,(spref+edgsp),buth,
2629                         &(U.dupflag), 0, 0, 0, 0, "Causes mesh data to be duplicated with Shift+D");
2630                 uiDefButBitI(block, TOG, USER_DUP_SURF, 0, "Surface",
2631                         (xpos+edgsp+(5*midsp)+(3*mpref)+(2*spref)),y2,(spref+edgsp),buth,
2632                         &(U.dupflag), 0, 0, 0, 0, "Causes surface data to be duplicated with Shift+D");
2633                 uiDefButBitI(block, TOG, USER_DUP_CURVE, 0, "Curve",
2634                         (xpos+edgsp+(6*midsp)+(3*mpref)+(3*spref)),y2,(spref+edgsp),buth,
2635                         &(U.dupflag), 0, 0, 0, 0, "Causes curve data to be duplicated with Shift+D");
2636                 uiDefButBitI(block, TOG, USER_DUP_FONT, 0, "Text",
2637                         (xpos+edgsp+(7*midsp)+(3*mpref)+(4*spref)),y2,(spref+edgsp),buth,
2638                         &(U.dupflag), 0, 0, 0, 0, "Causes text data to be duplicated with Shift+D");
2639                 uiDefButBitI(block, TOG, USER_DUP_MBALL, 0, "Metaball",
2640                         (xpos+edgsp+(8*midsp)+(3*mpref)+(5*spref)),y2,(spref+edgsp),buth,
2641                         &(U.dupflag), 0, 0, 0, 0, "Causes metaball data to be duplicated with Shift+D");
2642
2643                 uiDefButBitI(block, TOG, USER_DUP_ARM, 0, "Armature",
2644                         (xpos+edgsp+(4*midsp)+(3*mpref)+spref),y1,(spref+edgsp),buth,
2645                         &(U.dupflag), 0, 0, 0, 0, "Causes armature data to be duplicated with Shift+D");
2646                 uiDefButBitI(block, TOG, USER_DUP_LAMP, 0, "Lamp",
2647                         (xpos+edgsp+(5*midsp)+(3*mpref)+(2*spref)),y1,(spref+edgsp),buth,
2648                         &(U.dupflag), 0, 0, 0, 0, "Causes lamp data to be duplicated with Shift+D");
2649                 uiDefButBitI(block, TOG, USER_DUP_MAT, 0, "Material",
2650                         (xpos+edgsp+(6*midsp)+(3*mpref)+(3*spref)),y1,(spref+edgsp),buth,
2651                         &(U.dupflag), 0, 0, 0, 0, "Causes material data to be duplicated with Shift+D");
2652                 uiDefButBitI(block, TOG, USER_DUP_TEX, 0, "Texture",
2653                         (xpos+edgsp+(7*midsp)+(3*mpref)+(4*spref)),y1,(spref+edgsp),buth,
2654                         &(U.dupflag), 0, 0, 0, 0, "Causes texture data to be duplicated with Shift+D");
2655                 uiDefButBitI(block, TOG, USER_DUP_IPO, 0, "Ipo",
2656                         (xpos+edgsp+(8*midsp)+(3*mpref)+(5*spref)),y1,(spref+edgsp),buth,
2657                         &(U.dupflag), 0, 0, 0, 0, "Causes ipo data to be duplicated with Shift+D");
2658                 uiBlockEndAlign(block);
2659         
2660         } else if(U.userpref == 2) { /* language & colors */
2661
2662 #ifdef INTERNATIONAL
2663                 uiDefButBitS(block, TOG, USER_DOTRANSLATE, B_DOLANGUIFONT, "International Fonts",
2664                         xpos,y2,mpref,buth,
2665                         &(U.transopts), 0, 0, 0, 0, "Activate international interface");
2666
2667                 if(U.transopts & USER_DOTRANSLATE) {
2668                         char curfont[320];
2669                         
2670                         sprintf(curfont, "Interface Font: ");
2671                         if(U.fontname[0]) strcat(curfont, U.fontname);
2672                         else strcat(curfont, "Built-in");
2673                         
2674                         uiDefBut(block, LABEL,0,curfont,
2675                                 (xpos),y3,4*mpref,buth,
2676                                 0, 0, 0, 0, 0, "");
2677
2678                         uiDefBut(block, BUT, B_LOADUIFONT, "Select Font",
2679                                 xpos,y1,mpref,buth,
2680                                 0, 0, 0, 0, 0, "Select a new font for the interface");
2681
2682                         uiDefButI(block, BUT, B_RESTOREFONT, "Restore to default",
2683                                           (xpos+edgsp+mpref+midsp),y2,mpref,buth,
2684                                           &U.fontsize, 0, 0, 0, 0, "Restores to using the default included antialised font");
2685                         
2686                         uiDefButI(block, MENU|INT, B_SETFONTSIZE, fontsize_pup(),
2687                                 (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2688                                 &U.fontsize, 0, 0, 0, 0, "Current interface font size (points)");
2689
2690 /*
2691                         uiDefButS(block, MENU|SHO, B_SETENCODING, encoding_pup(),
2692                                 (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2693                                 &U.encoding, 0, 0, 0, 0, "Current interface font encoding");
2694
2695
2696                         uiDefBut(block, LABEL,0,"Translate:",
2697                                 (xpos+edgsp+(2.1*mpref)+(2*midsp)),y3label,mpref,buth,
2698                                 0, 0, 0, 0, 0, "");
2699 */
2700
2701                         uiDefButBitS(block, TOG, USER_TR_TOOLTIPS, B_SETTRANSBUTS, "Tooltips",
2702                                 (xpos+edgsp+(2.2*mpref)+(3*midsp)),y1,spref,buth,
2703                                 &(U.transopts), 0, 0, 0, 0, "Translate tooltips");
2704
2705                         uiDefButBitS(block, TOG, USER_TR_BUTTONS, B_SETTRANSBUTS, "Buttons",
2706                                 (xpos+edgsp+(2.2*mpref)+(4*midsp)+spref),y1,spref,buth,
2707                                 &(U.transopts), 0, 0, 0, 0, "Translate button labels");
2708
2709                         uiDefButBitS(block, TOG, USER_TR_MENUS, B_SETTRANSBUTS, "Toolbox",
2710                                 (xpos+edgsp+(2.2*mpref)+(5*midsp)+(2*spref)),y1,spref,buth,
2711                                 &(U.transopts), 0, 0, 0, 0, "Translate toolbox menu");
2712
2713                         uiDefButI(block, MENU|SHO, B_SETLANGUAGE, language_pup(),
2714                                 (xpos+edgsp+(2.2*mpref)+(3*midsp)),y2,mpref+(0.5*mpref)+3,buth,
2715                                 &U.language, 0, 0, 0, 0, "Select interface language");
2716                                 
2717                         uiDefButBitS(block, TOG, USER_USETEXTUREFONT, B_USETEXTUREFONT, "Use Textured Fonts",
2718                                 (xpos+edgsp+(4*mpref)+(4*midsp)),y2,mpref,buth,
2719                                 &(U.transopts), 0, 0, 0, 0,
2720                                 "Use Textured Fonts");
2721                 }
2722
2723 /* end of INTERNATIONAL */
2724 #endif
2725
2726         } else if(U.userpref == 3) { /* auto save */
2727
2728
2729                 uiDefButS(block, NUM, 0, "Save Versions:",
2730                         (xpos+edgsp),y3,mpref,buth,
2731                         &U.versions, 0.0, 32.0, 0, 0,
2732                         "The number of old versions to maintain in the current directory, when manually saving");
2733
2734                 uiDefButBitI(block, TOG, USER_AUTOSAVE, B_RESETAUTOSAVE, "Auto Save Temp Files",
2735                         (xpos+edgsp+mpref+midsp),y3,mpref,buth,
2736                         &(U.flag), 0, 0, 0, 0,
2737                         "Enables automatic saving of temporary files");
2738
2739                 if(U.flag & USER_AUTOSAVE) {
2740
2741                         uiDefButI(block, NUM, B_RESETAUTOSAVE, "Minutes:",
2742                                 (xpos+edgsp+mpref+midsp),y2,mpref,buth,
2743                                 &(U.savetime), 1.0, 60.0, 0, 0,
2744                                 "The time (in minutes) to wait between automatic temporary saves");
2745
2746                         uiDefBut(block, BUT, B_LOADTEMP, "Open Recent",
2747                                 (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2748                                 0, 0, 0, 0, 0,"Open the most recently saved temporary file");
2749                 }
2750
2751         } else if (U.userpref == 4) { /* system & opengl */
2752                 uiDefBut(block, LABEL,0,"Solid OpenGL light:",
2753                         xpos+edgsp, y3label, mpref, buth,
2754                         0, 0, 0, 0, 0, "");
2755                 
2756                 uiDefButS(block, MENU, B_REDR, "Light1 %x0|Light2 %x1|Light3 %x2",
2757                         xpos+edgsp, y2, 2*mpref/6, buth, &cur_light, 0.0, 0.0, 0, 0, "");
2758 &