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