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