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