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