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