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