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