Orange; stuff for the animation department!
[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) {
685                                 BKE_undo_step(1);
686                                 sound_initialize_sounds();
687                         }
688                 }
689         }
690 }
691
692 void BIF_redo(void)
693 {
694         if(G.obedit) {
695                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
696                         undo_editmode_step(-1);
697         }
698         else {
699                 if(G.f & G_WEIGHTPAINT)
700                         wpaint_undo();
701                 else if(G.f & G_VERTEXPAINT)
702                         vpaint_undo();
703                 else {
704                         /* includes faceselect now */
705                         if(U.uiflag & USER_GLOBALUNDO) {
706                                 BKE_undo_step(-1);
707                                 sound_initialize_sounds();
708                         }
709                 }
710         }
711 }
712
713 void BIF_undo_menu(void)
714 {
715         if(G.obedit) {
716                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
717                         undo_editmode_menu();
718                 allqueue(REDRAWALL, 0);
719         }
720         else {
721                 if(G.f & G_WEIGHTPAINT)
722                         ;
723                 else if(G.f & G_VERTEXPAINT)
724                         ;
725                 else {
726                         if(U.uiflag & USER_GLOBALUNDO) {
727                                 char *menu= BKE_undo_menu_string();
728                                 if(menu) {
729                                         short event= pupmenu_col(menu, 20);
730                                         MEM_freeN(menu);
731                                         if(event>0) BKE_undo_number(event);
732                                 }
733                         }
734                 }
735         }
736 }
737
738 /* *************** */
739
740 static void winqreadview3dspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
741 {
742         View3D *v3d= sa->spacedata.first;
743         Object *ob= OBACT;      // do not change!
744         float *curs;
745         int doredraw= 0, pupval;
746         unsigned short event= evt->event;
747         short val= evt->val;
748         char ascii= evt->ascii;
749         
750         if(curarea->win==0) return;     /* when it comes from sa->headqread() */
751         
752         if(val) {
753
754                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
755                 if(event==MOUSEY || event==MOUSEX) return;
756                 
757                 if(event==UI_BUT_EVENT) do_butspace(val); // temporal, view3d deserves own queue?
758                 
759                 /* we consider manipulator a button, defaulting to leftmouse */
760                 if(event==LEFTMOUSE) if(BIF_do_manipulator(sa)) return;
761                 
762                 /* swap mouse buttons based on user preference */
763                 if (U.flag & USER_LMOUSESELECT) {
764                         if (event==LEFTMOUSE) event = RIGHTMOUSE;
765                         else if (event==RIGHTMOUSE) event = LEFTMOUSE;
766                 }
767
768                 /* run any view3d event handler script links */
769                 if (event && sa->scriptlink.totscript)
770                         if (BPY_do_spacehandlers(sa, event, SPACEHANDLER_VIEW3D_EVENT))
771                                 return; /* return if event was processed (swallowed) by handler(s) */
772
773                 /* TEXTEDITING?? */
774                 if((G.obedit) && G.obedit->type==OB_FONT) {
775                         switch(event) {
776                         
777                         case LEFTMOUSE:
778                                 mouse_cursor();
779                                 break;
780                         case MIDDLEMOUSE:
781                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
782                                 if(U.flag & USER_VIEWMOVE) {
783                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
784                                                 viewmove(0);
785                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
786                                                 viewmove(2);
787                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
788                                                 viewmove(1);
789                                 }
790                                 else {
791                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
792                                                 viewmove(1);
793                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
794                                                 viewmove(2);
795                                         else
796                                                 viewmove(0);
797                                 }
798                                 break;
799                                 
800                         case WHEELUPMOUSE:
801                                 /* Regular:   Zoom in */
802                                 /* Shift:     Scroll up */
803                                 /* Ctrl:      Scroll right */
804                                 /* Alt-Shift: Rotate up */
805                                 /* Alt-Ctrl:  Rotate right */
806
807                                 if( G.qual & LR_SHIFTKEY ) {
808                                         if( G.qual & LR_ALTKEY ) { 
809                                                 G.qual &= ~LR_SHIFTKEY;
810                                                 persptoetsen(PAD2);
811                                                 G.qual |= LR_SHIFTKEY;
812                                         } else {
813                                                 persptoetsen(PAD2);
814                                         }
815                                 } else if( G.qual & LR_CTRLKEY ) {
816                                         if( G.qual & LR_ALTKEY ) { 
817                                                 G.qual &= ~LR_CTRLKEY;
818                                                 persptoetsen(PAD4);
819                                                 G.qual |= LR_CTRLKEY;
820                                         } else {
821                                                 persptoetsen(PAD4);
822                                         }
823                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
824                                         persptoetsen(PADMINUS);
825                                 else
826                                         persptoetsen(PADPLUSKEY);
827
828                                 doredraw= 1;
829                                 break;
830
831                         case WHEELDOWNMOUSE:
832                                 /* Regular:   Zoom out */
833                                 /* Shift:     Scroll down */
834                                 /* Ctrl:      Scroll left */
835                                 /* Alt-Shift: Rotate down */
836                                 /* Alt-Ctrl:  Rotate left */
837
838                                 if( G.qual & LR_SHIFTKEY ) {
839                                         if( G.qual & LR_ALTKEY ) { 
840                                                 G.qual &= ~LR_SHIFTKEY;
841                                                 persptoetsen(PAD8);
842                                                 G.qual |= LR_SHIFTKEY;
843                                         } else {
844                                                 persptoetsen(PAD8);
845                                         }
846                                 } else if( G.qual & LR_CTRLKEY ) {
847                                         if( G.qual & LR_ALTKEY ) { 
848                                                 G.qual &= ~LR_CTRLKEY;
849                                                 persptoetsen(PAD6);
850                                                 G.qual |= LR_CTRLKEY;
851                                         } else {
852                                                 persptoetsen(PAD6);
853                                         }
854                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
855                                         persptoetsen(PADPLUSKEY);
856                                 else
857                                         persptoetsen(PADMINUS);
858                                 
859                                 doredraw= 1;
860                                 break;
861
862                         case UKEY:
863                                 if(G.qual==LR_ALTKEY) {
864                                         remake_editText();
865                                         doredraw= 1;
866                                 } 
867                                 else {
868                                         do_textedit(event, val, ascii);
869                                 }
870                                 break;
871                         case VKEY:
872                                 if(G.qual==LR_ALTKEY) {
873                                         paste_editText();
874                                         doredraw= 1;
875                                 } 
876                                 else {
877                                         do_textedit(event, val, ascii);
878                                 }
879                                 break;
880                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
881                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
882                         case PADENTER:
883                                 persptoetsen(event);
884                                 doredraw= 1;
885                                 break;
886                                 
887                         default:
888                                 do_textedit(event, val, ascii);
889                                 break;
890                         }
891                 }
892                 else {
893
894                         if (U.flag & USER_NONUMPAD) {
895                                 event= convert_for_nonumpad(event);
896                         }
897
898                         switch(event) {
899                         
900                         case BACKBUFDRAW:
901                                 backdrawview3d(1);
902                                 break;
903                                                 
904                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
905                          * based on user preference USER_LMOUSESELECT
906                          */
907                         case LEFTMOUSE: 
908                                 if ((G.obedit) || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT))) {
909                                         mouse_cursor();
910                                 } else if (G.f & G_VERTEXPAINT) {
911                                         vertex_paint();
912                                 }
913                                 else if (G.f & G_WEIGHTPAINT){
914                                         weight_paint();
915                                 }
916                                 else if (G.f & G_TEXTUREPAINT) {
917                                         face_draw();
918                                 }
919                                 break;
920                         case MIDDLEMOUSE:
921                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
922                                 if(U.flag & USER_VIEWMOVE) {
923                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
924                                                 viewmove(0);
925                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
926                                                 viewmove(2);
927                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
928                                                 viewmove(1);
929                                 }
930                                 else {
931                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
932                                                 viewmove(1);
933                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
934                                                 viewmove(2);
935                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
936                                                 viewmove(0);
937                                 }
938                                 break;
939                         case RIGHTMOUSE:
940                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
941                                         if(G.obedit->type==OB_MESH)
942                                                 mouse_mesh();
943                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
944                                                 mouse_nurb();
945                                         else if(G.obedit->type==OB_MBALL)
946                                                 mouse_mball();
947                                         else if(G.obedit->type==OB_LATTICE)
948                                                 mouse_lattice();
949                                         else if(G.obedit->type==OB_ARMATURE)
950                                                 mouse_armature();
951                                 }
952                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
953                                         mouse_mesh();   // loop select for 1 mousebutton dudes
954                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
955                                         mouse_mesh();   // loop select for 1 mousebutton dudes
956                                 else if(G.qual==LR_CTRLKEY)
957                                         mouse_select(); // also allow in editmode, for vertex parenting
958                                 else if(G.f & G_FACESELECT)
959                                         face_select();
960                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
961                                         sample_vpaint();
962                                 else
963                                         mouse_select(); // does poses too
964                                 break;
965                         case WHEELUPMOUSE:
966                                 /* Regular:   Zoom in */
967                                 /* Shift:     Scroll up */
968                                 /* Ctrl:      Scroll right */
969                                 /* Alt-Shift: Rotate up */
970                                 /* Alt-Ctrl:  Rotate right */
971
972                                 if( G.qual & LR_SHIFTKEY ) {
973                                         if( G.qual & LR_ALTKEY ) { 
974                                                 G.qual &= ~LR_SHIFTKEY;
975                                                 persptoetsen(PAD2);
976                                                 G.qual |= LR_SHIFTKEY;
977                                         } else {
978                                                 persptoetsen(PAD2);
979                                         }
980                                 } else if( G.qual & LR_CTRLKEY ) {
981                                         if( G.qual & LR_ALTKEY ) { 
982                                                 G.qual &= ~LR_CTRLKEY;
983                                                 persptoetsen(PAD4);
984                                                 G.qual |= LR_CTRLKEY;
985                                         } else {
986                                                 persptoetsen(PAD4);
987                                         }
988                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
989                                         persptoetsen(PADMINUS);
990                                 else
991                                         persptoetsen(PADPLUSKEY);
992
993                                 doredraw= 1;
994                                 break;
995                         case WHEELDOWNMOUSE:
996                                 /* Regular:   Zoom out */
997                                 /* Shift:     Scroll down */
998                                 /* Ctrl:      Scroll left */
999                                 /* Alt-Shift: Rotate down */
1000                                 /* Alt-Ctrl:  Rotate left */
1001
1002                                 if( G.qual & LR_SHIFTKEY ) {
1003                                         if( G.qual & LR_ALTKEY ) { 
1004                                                 G.qual &= ~LR_SHIFTKEY;
1005                                                 persptoetsen(PAD8);
1006                                                 G.qual |= LR_SHIFTKEY;
1007                                         } else {
1008                                                 persptoetsen(PAD8);
1009                                         }
1010                                 } else if( G.qual & LR_CTRLKEY ) {
1011                                         if( G.qual & LR_ALTKEY ) { 
1012                                                 G.qual &= ~LR_CTRLKEY;
1013                                                 persptoetsen(PAD6);
1014                                                 G.qual |= LR_CTRLKEY;
1015                                         } else {
1016                                                 persptoetsen(PAD6);
1017                                         }
1018                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1019                                         persptoetsen(PADPLUSKEY);
1020                                 else
1021                                         persptoetsen(PADMINUS);
1022                                 
1023                                 doredraw= 1;
1024                                 break;
1025                         
1026                         case ONEKEY:
1027                                 if(G.qual==LR_CTRLKEY) {
1028                                         if(ob && ob->type == OB_MESH) {
1029                                                 flip_subdivison(ob, 1);
1030                                         }
1031                                 }
1032                                 else do_layer_buttons(0); 
1033                                 break;
1034                                 
1035                         case TWOKEY:
1036                                 if(G.qual==LR_CTRLKEY) {
1037                                         if(ob && ob->type == OB_MESH) {
1038                                                 flip_subdivison(ob, 2);
1039                                         }
1040                                 }
1041                                 else do_layer_buttons(1); 
1042                                 break;
1043                                 
1044                         case THREEKEY:
1045                                 if(G.qual==LR_CTRLKEY) {
1046                                         if(ob && ob->type == OB_MESH) {
1047                                                 flip_subdivison(ob, 3);
1048                                         }
1049                                 }
1050                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1051                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1052                                                 select_faces_by_numverts(3);
1053                                 }
1054                                 else do_layer_buttons(2); 
1055                                 break;
1056                                 
1057                         case FOURKEY:
1058                                 if(G.qual==LR_CTRLKEY) {
1059                                         if(ob && ob->type == OB_MESH) {
1060                                                 flip_subdivison(ob, 4);
1061                                         }
1062                                 }
1063                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1064                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1065                                                 select_faces_by_numverts(4);
1066                                 }
1067                                 else do_layer_buttons(3); 
1068                                 break;
1069                                 
1070                         case FIVEKEY:
1071                                 if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1072                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1073                                                 select_faces_by_numverts(5);
1074                                 }
1075                                 else do_layer_buttons(4);
1076                                 break;
1077
1078                         case SIXKEY:
1079                                 do_layer_buttons(5); break;
1080                         case SEVENKEY:
1081                                 do_layer_buttons(6); break;
1082                         case EIGHTKEY:
1083                                 do_layer_buttons(7); break;
1084                         case NINEKEY:
1085                                 do_layer_buttons(8); break;
1086                         case ZEROKEY:
1087                                 do_layer_buttons(9); break;
1088                         case MINUSKEY:
1089                                 do_layer_buttons(10); break;
1090                         case EQUALKEY:
1091                                 do_layer_buttons(11); break;
1092                         case ACCENTGRAVEKEY:
1093                                 do_layer_buttons(-1); break;
1094                         
1095                         case SPACEKEY:
1096                                 if(G.qual == LR_CTRLKEY) {
1097                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1098                                         if(val>0) {
1099                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1100                                                 else {
1101                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1102                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1103                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1104                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1105                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1106                                                 }
1107                                                 doredraw= 1;
1108                                         }
1109                                 }
1110                                 else if(G.qual == LR_ALTKEY) {
1111                                         BIF_selectOrientation();
1112                                         doredraw= 1;
1113                                 }
1114
1115                                 break;
1116                                 
1117                         case AKEY:
1118                                 if(G.qual & LR_CTRLKEY) apply_object(); // also with shift!
1119                                 else if((G.qual==LR_SHIFTKEY)) {
1120                                         toolbox_n_add();
1121                                 }
1122                                 else {
1123                                         if(G.obedit) {
1124                                                 if(G.obedit->type==OB_MESH)
1125                                                         deselectall_mesh();
1126                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1127                                                         deselectall_nurb();
1128                                                 else if(G.obedit->type==OB_MBALL)
1129                                                         deselectall_mball();
1130                                                 else if(G.obedit->type==OB_LATTICE)
1131                                                         deselectall_Latt();
1132                                                 else if(G.obedit->type==OB_ARMATURE)
1133                                                         deselectall_armature(1);        // 1 == toggle
1134                                         }
1135                                         else if (ob && (ob->flag & OB_POSEMODE)){
1136                                                 deselectall_posearmature(ob, 1);
1137                                         }
1138                                         else {
1139                                                 if(G.f & G_FACESELECT) deselectall_tface();
1140                                                 else {
1141                                                         /* by design, the center of the active object 
1142                                                          * (which need not necessarily by selected) will
1143                                                          * still be drawn as if it were selected.
1144                                                          */
1145                                                         deselectall();
1146                                                 }
1147                                         }
1148                                 }
1149                                 break;
1150                         case BKEY:
1151                                 if(G.qual==LR_ALTKEY)
1152                                         view3d_edit_clipping(v3d);
1153                                 else if(G.qual==LR_SHIFTKEY)
1154                                         set_render_border();
1155                                 else if(G.qual==0)
1156                                         borderselect();
1157                                 break;
1158                         case CKEY:
1159                                 if(G.qual==LR_CTRLKEY) {
1160                                         if(ob && (ob->flag & OB_POSEMODE))
1161                                                 pose_copy_menu();       /* poseobject.c */
1162                                         else
1163                                                 copy_attr_menu();
1164                                 }
1165                                 else if(G.qual==LR_ALTKEY) {
1166                                         if(ob && (ob->flag & OB_POSEMODE))
1167                                                 pose_clear_constraints();       /* poseobject.c */
1168                                         else
1169                                                 convertmenu();  /* editobject.c */
1170                                 }
1171                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY)) 
1172                                         add_constraint(0);      /* editconstraint.c, generic for objects and posemode */
1173                                 else if((G.qual==LR_SHIFTKEY)) {
1174                                         view3d_home(1);
1175                                         curs= give_cursor();
1176                                         curs[0]=curs[1]=curs[2]= 0.0;
1177                                         allqueue(REDRAWVIEW3D, 0);
1178                                 }
1179                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1180                                         makecyclicNurb();
1181                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1182                                         allqueue(REDRAWVIEW3D, 0);
1183                                 }
1184                                 else if((G.qual==0)){
1185                                         curs= give_cursor();
1186                                         G.vd->ofs[0]= -curs[0];
1187                                         G.vd->ofs[1]= -curs[1];
1188                                         G.vd->ofs[2]= -curs[2];
1189                                         scrarea_queue_winredraw(curarea);
1190                                 }
1191                         
1192                                 break;
1193                         case DKEY:
1194                                 if((G.qual==LR_SHIFTKEY)) {
1195                                         duplicate_context_selected();
1196                                 }
1197                                 else if(G.qual==LR_ALTKEY) {
1198                                         if(ob && (ob->flag & OB_POSEMODE))
1199                                                 error ("Duplicate not possible in posemode.");
1200                                         else if((G.obedit==NULL))
1201                                                 adduplicate(0);
1202                                 }
1203                                 else if(G.qual==LR_CTRLKEY) {
1204                                         imagestodisplist();
1205                                 }
1206                                 else if((G.qual==0)){
1207                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
1208                                         if(pupval>0) {
1209                                                 G.vd->drawtype= pupval;
1210                                                 doredraw= 1;
1211                                         
1212                                         }
1213                                 }
1214                                 
1215                                 break;
1216                         case EKEY:
1217                                 if (G.qual==0){
1218                                         if(G.obedit) {
1219                                                 if(G.obedit->type==OB_MESH)
1220                                                         extrude_mesh();
1221                                                 else if(G.obedit->type==OB_CURVE)
1222                                                         addvert_Nurb('e');
1223                                                 else if(G.obedit->type==OB_SURF)
1224                                                         extrude_nurb();
1225                                                 else if(G.obedit->type==OB_ARMATURE)
1226                                                         extrude_armature(0);
1227                                         }
1228                                 }
1229                                 else if (G.qual==LR_CTRLKEY) {
1230                                         if(G.obedit && G.obedit->type==OB_MESH)
1231                                                 Edge_Menu();
1232                                 }
1233                                 else if (G.qual==LR_SHIFTKEY) {
1234                                         if (G.obedit && G.obedit->type==OB_MESH) {
1235                                                 initTransform(TFM_CREASE, CTX_EDGE);
1236                                                 Transform();
1237                                         }
1238                                         else if (G.obedit && G.obedit->type==OB_ARMATURE) {
1239                                                 extrude_armature(1);
1240                                         }
1241                                 }
1242                                 break;
1243                         case FKEY:
1244                                 if(G.obedit) {
1245                                         if(G.obedit->type==OB_MESH) {
1246                                                 if((G.qual==LR_SHIFTKEY))
1247                                                         fill_mesh();
1248                                                 else if(G.qual==LR_ALTKEY)
1249                                                         beauty_fill();
1250                                                 else if(G.qual==LR_CTRLKEY)
1251                                                         edge_flip();
1252                                                 else if (G.qual==0)
1253                                                         addedgeface_mesh();
1254                                                 else if ( G.qual == 
1255                                                          (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1256                                                         select_linked_flat_faces();
1257                                                 }
1258
1259                                         }
1260                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
1261                                 }
1262                                 else if(G.qual==LR_CTRLKEY)
1263                                         sort_faces();
1264                                 else if((G.qual==LR_SHIFTKEY)) {
1265                                         if(ob && (ob->flag & OB_POSEMODE))
1266                                            pose_activate_flipped_bone();
1267                                         else if(G.f & G_WEIGHTPAINT)
1268                                                 pose_activate_flipped_bone();
1269                                         else
1270                                                 fly();
1271                                 }
1272                                 else {
1273                                         set_faceselect();
1274                                 }
1275                                 
1276                                 break;
1277                         case GKEY:
1278                                 if(G.qual & LR_CTRLKEY) group_operation_with_menu();
1279                                 else if((G.qual==LR_SHIFTKEY))
1280                                         select_grouped_menu();
1281                                 else if(G.qual==LR_ALTKEY) {
1282                                         if(okee("Clear location")) {
1283                                                 clear_object('g');
1284                                         }
1285                                 }
1286                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1287                                         v3d->twtype= V3D_MANIP_TRANSLATE;
1288                                         doredraw= 1;
1289                                 }
1290                                 else if((G.qual==0)) {
1291                                         initTransform(TFM_TRANSLATION, CTX_NONE);
1292                                         Transform();
1293                                 }
1294                                 break;
1295                         case HKEY:
1296                                 if(G.obedit) {
1297                                         if(G.obedit->type==OB_MESH) {
1298                                                 if(G.qual==LR_CTRLKEY)
1299                                                         add_hook();
1300                                                 else if(G.qual==LR_ALTKEY)
1301                                                         reveal_mesh();
1302                                                 else if((G.qual==LR_SHIFTKEY))
1303                                                         hide_mesh(1);
1304                                                 else if((G.qual==0)) 
1305                                                         hide_mesh(0);
1306                                         }
1307                                         else if(G.obedit->type== OB_SURF) {
1308                                                 if(G.qual==LR_CTRLKEY)
1309                                                         add_hook();
1310                                                 else if(G.qual==LR_ALTKEY)
1311                                                         revealNurb();
1312                                                 else if((G.qual==LR_SHIFTKEY))
1313                                                         hideNurb(1);
1314                                                 else if((G.qual==0))
1315                                                         hideNurb(0);
1316                                         }
1317                                         else if(G.obedit->type==OB_CURVE) {
1318                                                 if(G.qual==LR_CTRLKEY)
1319                                                         add_hook();
1320                                                 else {
1321                                                         if(G.qual==LR_CTRLKEY)
1322                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
1323                                                         else if((G.qual==LR_SHIFTKEY))
1324                                                                 sethandlesNurb(1);
1325                                                         else if((G.qual==0))
1326                                                                 sethandlesNurb(3);
1327                                                         
1328                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1329                                                         BIF_undo_push("Handle change");
1330                                                         allqueue(REDRAWVIEW3D, 0);
1331                                                 }
1332                                         }
1333                                         else if(G.obedit->type==OB_LATTICE) {
1334                                                 if(G.qual==LR_CTRLKEY) add_hook();
1335                                         }
1336                                         else if(G.obedit->type==OB_MBALL) {
1337                                                 if(G.qual==LR_ALTKEY)
1338                                                         reveal_mball();
1339                                                 else if((G.qual==LR_SHIFTKEY))
1340                                                         hide_mball(1);
1341                                                 else if((G.qual==0)) 
1342                                                         hide_mball(0);
1343                                         }
1344                                         else if(G.obedit->type==OB_ARMATURE) {
1345                                                 if (G.qual==0)
1346                                                         hide_selected_armature_bones();
1347                                                 else if (G.qual==LR_SHIFTKEY)
1348                                                         hide_unselected_armature_bones();
1349                                                 else if (G.qual==LR_ALTKEY)
1350                                                         show_all_armature_bones();
1351                                         }
1352                                 }
1353                                 else if(G.f & G_FACESELECT)
1354                                         hide_tface();
1355                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1356                                         if (G.qual==0)
1357                                                 hide_selected_pose_bones();
1358                                         else if (G.qual==LR_SHIFTKEY)
1359                                                 hide_unselected_pose_bones();
1360                                         else if (G.qual==LR_ALTKEY)
1361                                                 show_all_pose_bones();
1362                                 }
1363                                 break;
1364                         case IKEY:
1365                                 if(G.obedit);
1366                                 else if(G.qual==LR_CTRLKEY) {
1367                                         if(ob && ob->type==OB_ARMATURE) 
1368                                                 if(ob->flag & OB_POSEMODE) 
1369                                                         pose_add_IK();
1370                                 }
1371                                 else if(G.qual==LR_ALTKEY) {
1372                                         if(ob && ob->type==OB_ARMATURE) 
1373                                                 if(ob->flag & OB_POSEMODE) 
1374                                                         pose_clear_IK();
1375                                 }
1376                                 break;
1377                                 
1378                         case JKEY:
1379                                 if(G.qual==LR_CTRLKEY) {
1380                                         if( ob ) {
1381                                                 if(ob->type == OB_MESH)
1382                                                         join_mesh();
1383                                                 else if(ob->type == OB_CURVE)
1384                                                         join_curve(OB_CURVE);
1385                                                 else if(ob->type == OB_SURF)
1386                                                         join_curve(OB_SURF);
1387                                                 else if(ob->type == OB_ARMATURE)
1388                                                         join_armature ();
1389                                         }
1390                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1391                                                 addsegment_nurb();
1392                                 }
1393                                 else if(G.obedit) {
1394                                         if(G.obedit->type==OB_MESH) {
1395                                                 join_triangles();
1396                                         }
1397                                 }
1398
1399                                 break;
1400                         case KKEY:
1401                                 if(G.obedit) {
1402                                         if (G.obedit->type==OB_MESH) {
1403                                                 if (G.qual==LR_SHIFTKEY)
1404                                                         KnifeSubdivide(KNIFE_PROMPT);
1405                                                 else if (G.qual==0)
1406                                                         LoopMenu();
1407                                         }
1408                                         else if(G.obedit->type==OB_SURF)
1409                                                 printknots();
1410                                 }
1411                                 else {
1412                                         if((G.qual==LR_SHIFTKEY)) {
1413                                                 if(G.f & G_FACESELECT)
1414                                                         clear_vpaint_selectedfaces();
1415                                                 else if(G.f & G_VERTEXPAINT)
1416                                                         clear_vpaint();
1417                                                 else
1418                                                         select_select_keys();
1419                                         }
1420                                         else if (G.qual==0)
1421                                                 set_ob_ipoflags();
1422                                 }
1423                                 
1424                                 break;
1425                         case LKEY:
1426                                 if(G.obedit) {
1427                                         if(G.obedit->type==OB_MESH)
1428                                                 selectconnected_mesh(G.qual);
1429                                         if(G.obedit->type==OB_ARMATURE)
1430                                                 selectconnected_armature();
1431                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1432                                                 selectconnected_nurb();
1433                                 }
1434                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1435                                         selectconnected_posearmature();
1436                                 }
1437                                 else {
1438                                         if(G.f & G_FACESELECT) {
1439                                                 if((G.qual==0))
1440                                                         select_linked_tfaces(0);
1441                                                 else if((G.qual==LR_SHIFTKEY))
1442                                                         select_linked_tfaces(1);
1443                                                 else if(G.qual==LR_CTRLKEY)
1444                                                         select_linked_tfaces(2);
1445                                         }
1446                                         else {
1447                                                 if((G.qual==0))
1448                                                         make_local();
1449                                                 else if((G.qual==LR_SHIFTKEY))
1450                                                         selectlinks_menu();
1451                                                 else if(G.qual==LR_CTRLKEY)
1452                                                         make_links_menu();
1453                                         }
1454                                 }
1455                                 break;
1456                         case MKEY:
1457                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1458                                         mirror_uv_tface();
1459                                 else if(G.obedit){
1460                                         if(G.qual==LR_ALTKEY) {
1461                                                 if(G.obedit->type==OB_MESH) {
1462                                                         mergemenu();
1463                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1464                                                 }
1465                                         }
1466                                         else if((G.qual==0) || (G.qual==LR_CTRLKEY)) {
1467                                                 mirrormenu();
1468                                         }
1469                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1470                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
1471                                         }
1472                                 }
1473                                 else if(G.qual & LR_CTRLKEY) {
1474                                         mirrormenu();
1475                                 }
1476                                 else if(G.qual==0) {
1477                                         if(ob && (ob->flag & OB_POSEMODE))
1478                                                 pose_movetolayer();
1479                                         else
1480                                                 movetolayer();
1481                                 }
1482                                 break;
1483                         case NKEY:
1484                                 if((G.qual==0)) {
1485                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1486                                         allqueue(REDRAWVIEW3D, 0);
1487                                 }
1488                                 else if(G.obedit) {
1489                                         switch (G.obedit->type){
1490                                         case OB_ARMATURE:
1491                                                 if(G.qual==LR_CTRLKEY){
1492                                                         if (okee("Recalculate bone roll angles")) {
1493                                                                 auto_align_armature();
1494                                                                 allqueue(REDRAWVIEW3D, 0);
1495                                                         }
1496                                                 }
1497                                                 break;
1498                                         case OB_MESH: 
1499                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1500                                                         if(okee("Recalculate normals inside")) {
1501                                                                 righthandfaces(2);
1502                                                                 allqueue(REDRAWVIEW3D, 0);
1503                                                                 BIF_undo_push("Recalculate normals inside");
1504                                                         }
1505                                                 }
1506                                                 else if(G.qual==LR_CTRLKEY){
1507                                                         if(okee("Recalculate normals outside")) {
1508                                                                 righthandfaces(1);
1509                                                                 allqueue(REDRAWVIEW3D, 0);
1510                                                                 BIF_undo_push("Recalculate normals outside");
1511                                                         }
1512                                                 }
1513                                                 break;
1514                                         }
1515                                 }
1516                                 
1517                                 break;
1518                         case OKEY:
1519                                 if (G.obedit) {
1520                                         if (G.qual==LR_SHIFTKEY) {
1521                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%6;
1522                                                 allqueue(REDRAWHEADERS, 0);
1523                                         }
1524                                         else if((G.qual==LR_ALTKEY)) {
1525                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
1526                                                 else G.scene->proportional= 2;
1527                                                 allqueue(REDRAWHEADERS, 0);
1528                                         }
1529                                         else if((G.qual==0)) {
1530                                                 G.scene->proportional= !G.scene->proportional;
1531                                                 allqueue(REDRAWHEADERS, 0);
1532                                         }
1533                                 }
1534                                 else if((G.qual==LR_SHIFTKEY)) {
1535                                         if(ob && ob->type == OB_MESH) {
1536                                                 flip_subdivison(ob, -1);
1537                                         }
1538                                 }
1539                                 else if(G.qual==LR_ALTKEY) {
1540                                         if(okee("Clear origin")) {
1541                                                 clear_object('o');
1542                                         }
1543                                 }
1544                                 break;
1545
1546                         case PKEY:
1547                                 if(G.obedit) {
1548                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1549                                                 if(G.obedit->type==OB_ARMATURE)
1550                                                         make_bone_parent();
1551                                                 else
1552                                                         make_parent();
1553                                         }
1554                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
1555                                                 clear_bone_parent();
1556                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
1557                                                 separatemenu();
1558                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1559                                                 separate_nurb();
1560                                         else if (G.qual==LR_SHIFTKEY) {
1561                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
1562                                                 Transform();
1563                                         }
1564                                 }
1565                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
1566                                         make_parent();
1567                                 else if(G.qual==LR_SHIFTKEY) {
1568                                         initTransform(TFM_PUSHPULL, CTX_NONE);
1569                                         Transform();
1570                                 }
1571                                 else if(G.qual==LR_ALTKEY)
1572                                         clear_parent();
1573                                 else if((G.qual==0)) {
1574                         start_game();
1575                                 }
1576                                 break;                          
1577                         case RKEY:
1578                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1579                                         rotate_uv_tface();
1580                                 else if(G.qual==LR_ALTKEY) {
1581                                         if(okee("Clear rotation")) {
1582                                                 clear_object('r');
1583                                         }
1584                                 } 
1585                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1586                                         v3d->twtype= V3D_MANIP_ROTATE;
1587                                         doredraw= 1;
1588                                 }
1589                                 else if (G.obedit) {
1590                                         if((G.qual==LR_SHIFTKEY)) {
1591                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
1592                                                         selectrow_nurb();
1593                                         }
1594                                         else if(G.qual==LR_CTRLKEY) {
1595                                                 if (G.obedit->type==OB_MESH)
1596                                                         CutEdgeloop(1);
1597                                                         BIF_undo_push("Cut Edgeloop");
1598                                         }
1599                                         else if((G.qual==0)) {
1600                                                 initTransform(TFM_ROTATION, CTX_NONE);
1601                                                 Transform();
1602                                         }
1603                                 }
1604                                 else if((G.qual==0)) {
1605                                         initTransform(TFM_ROTATION, CTX_NONE);
1606                                         Transform();
1607                                 }
1608                                 break;
1609                         case SKEY:
1610                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1611                                         v3d->twtype= V3D_MANIP_SCALE;
1612                                         doredraw= 1;
1613                                 }
1614                                 else if(G.obedit) {
1615                                         
1616                                         if(G.qual==LR_ALTKEY) {
1617                                                 if(G.obedit->type==OB_ARMATURE) {
1618                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1619                                                 }
1620                                                 else
1621                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
1622                                                 Transform();
1623                                         }
1624                                         else if(G.qual==LR_CTRLKEY) {
1625                                                 initTransform(TFM_SHEAR, CTX_NONE);
1626                                                 Transform();
1627                                         }
1628                                         else if(G.qual==LR_SHIFTKEY)
1629                                                 snapmenu();
1630                                         else if(G.qual==0) {
1631                                                 if(G.obedit->type==OB_ARMATURE) {
1632                                                         bArmature *arm= G.obedit->data;
1633                                                         if(arm->drawtype==ARM_ENVELOPE)
1634                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
1635                                                         else
1636                                                                 initTransform(TFM_RESIZE, CTX_NONE);
1637                                                 }
1638                                                 else
1639                                                         initTransform(TFM_RESIZE, CTX_NONE);
1640                                                 Transform();
1641                                         }
1642                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
1643                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
1644                                                 Transform();
1645                                         }
1646                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1647                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
1648                                         }
1649                                 }
1650                                 else if(G.qual==LR_ALTKEY) {
1651                                         if(G.f & G_WEIGHTPAINT)
1652                                                 ob= ob->parent;
1653                                         if(ob && (ob->flag & OB_POSEMODE)) {
1654                                                 bArmature *arm= ob->data;
1655                                                 if( ELEM(arm->drawtype, ARM_B_BONE, ARM_ENVELOPE)) {
1656                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1657                                                         Transform();
1658                                                         break;
1659                                                 }
1660                                         }
1661                                         
1662                                         if(okee("Clear size")) {
1663                                                 clear_object('s');
1664                                         }
1665                                 }
1666                                 else if(G.qual==LR_SHIFTKEY) {
1667                                         snapmenu();
1668                                 }
1669                                 else if((G.qual==0)) {
1670                                         initTransform(TFM_RESIZE, CTX_NONE);
1671                                         Transform();
1672                                 }
1673                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1674                                         initTransform(TFM_TOSPHERE, CTX_NONE);
1675                                         Transform();
1676                                 }
1677                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
1678                                         initTransform(TFM_SHEAR, CTX_NONE);
1679                                         Transform();
1680                                 }
1681                                 break;
1682                         case TKEY:
1683                                 if(G.obedit){
1684                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
1685                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
1686                                                 allqueue(REDRAWVIEW3D, 0);
1687                                                 countall();
1688                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1689                                         }
1690                                         if (G.obedit->type==OB_CURVE) {
1691                                                 if (G.qual==LR_ALTKEY) {
1692                                                         clear_tilt();
1693                                                 }
1694                                                 else if (G.qual==0) {
1695                                                         initTransform(TFM_TILT, CTX_NONE);
1696                                                         Transform();
1697                                                 }
1698                                         }
1699                                 }
1700                                 else if(G.qual==LR_CTRLKEY) {
1701                                         if(ob && (ob->flag & OB_POSEMODE));
1702                                         else make_track();
1703                                 }
1704                                 else if(G.qual==LR_ALTKEY) {
1705                                         if(ob && (ob->flag & OB_POSEMODE));
1706                                         else clear_track();
1707                                 }
1708                                 else if((G.qual==0)){
1709                                         texspace_edit();
1710                                 }
1711                                 
1712                                 break;
1713                         case UKEY:
1714                                 if(G.obedit) {
1715                                         if(G.obedit->type==OB_MESH) {
1716                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1717                                         }
1718                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
1719                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1720                                         }
1721                                 }
1722                                 else if((G.qual==0)) {
1723                                         if (G.f & G_FACESELECT)
1724                                                 uv_autocalc_tface();
1725                                         else if(G.f & G_WEIGHTPAINT)
1726                                                 wpaint_undo();
1727                                         else if(G.f & G_VERTEXPAINT)
1728                                                 vpaint_undo();
1729                                         else {
1730                                                 single_user();
1731                                         }
1732                                 }
1733                                         
1734                                 break;
1735                         case VKEY:
1736                                 if((G.qual==LR_SHIFTKEY)) {
1737                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
1738                                                 align_view_to_selected(v3d);
1739                                         }
1740                                         else if (G.f & G_FACESELECT) {
1741                                                 align_view_to_selected(v3d);
1742                                         }
1743                                 }
1744                                 else if(G.qual==LR_ALTKEY)
1745                                         image_aspect();
1746                                 else if (G.qual==0){
1747                                         if(G.obedit) {
1748                                                 if(G.obedit->type==OB_MESH) {
1749                                                         mesh_rip();
1750                                                 }
1751                                                 else if(G.obedit->type==OB_CURVE) {
1752                                                         sethandlesNurb(2);
1753                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1754                                                         allqueue(REDRAWVIEW3D, 0);
1755                                                         BIF_undo_push("Handle change");
1756                                                 }
1757                                         }
1758                                         else if(ob && ob->type == OB_MESH) 
1759                                                 set_vpaint();
1760                                 }
1761                                 break;
1762                         case WKEY:
1763                                 if((G.qual==LR_SHIFTKEY)) {
1764                                         initTransform(TFM_WARP, CTX_NONE);
1765                                         Transform();
1766                                 }
1767                                 else if(G.qual==LR_ALTKEY) {
1768                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
1769                                 }
1770                                 else if(G.qual==LR_CTRLKEY) {
1771                                         if(G.obedit) {
1772                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
1773                                                         switchdirectionNurb2();
1774                                                 }
1775                                         }
1776                                 }
1777                                 else if((G.qual==0))
1778                                         special_editmenu();
1779                                 
1780                                 break;
1781                         case XKEY:
1782                         case DELKEY:
1783                                 if(G.qual==0)
1784                                         delete_context_selected();
1785                                 break;
1786                         case YKEY:
1787                                 if((G.qual==0) && (G.obedit)) {
1788                                         if(G.obedit->type==OB_MESH) split_mesh();
1789                                 }
1790                                 break;
1791                         case ZKEY:
1792                                 toggle_shading();
1793                                 
1794                                 scrarea_queue_headredraw(curarea);
1795                                 scrarea_queue_winredraw(curarea);
1796                                 break;
1797                         
1798                         case HOMEKEY:
1799                                 if(G.qual==0)
1800                                         view3d_home(0);
1801                                 break;
1802                         case COMMAKEY:
1803                                 if(G.qual==LR_CTRLKEY) {
1804                                         G.vd->around= V3D_CENTROID;
1805                                 } else if(G.qual==LR_SHIFTKEY) {
1806                                         G.vd->around= V3D_CENTROID;
1807                                 } else if(G.qual==0) {
1808                                         G.vd->around= V3D_CENTRE;
1809                                 }
1810                                 handle_view3d_around();
1811                                 
1812                                 scrarea_queue_headredraw(curarea);
1813                                 scrarea_queue_winredraw(curarea);
1814                                 break;
1815                                 
1816                         case PERIODKEY:
1817                                 if(G.qual==LR_CTRLKEY) {
1818                                         G.vd->around= V3D_LOCAL;
1819                                 }       else if(G.qual==0) {
1820                                         G.vd->around= V3D_CURSOR;
1821                                 }
1822                                 handle_view3d_around();
1823                                 
1824                                 scrarea_queue_headredraw(curarea);
1825                                 scrarea_queue_winredraw(curarea);
1826                                 break;
1827                         
1828                         case PADSLASHKEY:
1829                                 if(G.qual==0) {
1830                                         if(G.vd->localview) {
1831                                                 G.vd->localview= 0;
1832                                                 endlocalview(curarea);
1833                                         }
1834                                         else {
1835                                                 G.vd->localview= 1;
1836                                                 initlocalview();
1837                                         }
1838                                         scrarea_queue_headredraw(curarea);
1839                                 }
1840                                 break;
1841                         case PADASTERKEY:       /* '*' */
1842                                 if(G.qual==0) {
1843                                         if(ob) {
1844                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
1845                                                         editmesh_align_view_to_selected(G.vd, 2);
1846                                                 } 
1847                                                 else if (G.f & G_FACESELECT) {
1848                                                         if(ob->type==OB_MESH) {
1849                                                                 Mesh *me= ob->data;
1850                                                                 faceselect_align_view_to_selected(G.vd, me, 2);
1851                                                         }
1852                                                 }
1853                                                 else
1854                                                         obmat_to_viewmat(ob);
1855                                                 
1856                                                 if(G.vd->persp==2) G.vd->persp= 1;
1857                                                 scrarea_queue_winredraw(curarea);
1858                                         }
1859                                 }
1860                                 break;
1861                         case PADPERIOD: /* '.' */
1862                                 if(G.qual==0)
1863                                         centreview();
1864                                 break;
1865                         
1866                         case PAGEUPKEY:
1867                                 if(G.qual==LR_CTRLKEY)
1868                                         movekey_obipo(1);
1869                                 else if((G.qual==0))
1870                                         nextkey_obipo(1);       /* in editipo.c */
1871                                 break;
1872
1873                         case PAGEDOWNKEY:
1874                                 if(G.qual==LR_CTRLKEY)
1875                                         movekey_obipo(-1);
1876                                 else if((G.qual==0))
1877                                         nextkey_obipo(-1);
1878                                 break;
1879                                 
1880                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1881                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1882                         case PADENTER:
1883                                 persptoetsen(event);
1884                                 doredraw= 1;
1885                                 break;
1886                         case PADMINUS:
1887                                 if ( (G.qual==LR_CTRLKEY)
1888                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
1889                                         select_less();
1890                                 else {
1891                                         persptoetsen(event);
1892                                         doredraw= 1;
1893                                 }
1894                                 break;
1895
1896                         case PADPLUSKEY:
1897                                 if ( (G.qual==LR_CTRLKEY)
1898                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
1899                                         select_more();
1900                                 else {
1901                                         persptoetsen(event);
1902                                         doredraw= 1;
1903                                 }
1904                                 break;
1905
1906                         case ESCKEY:
1907                                 if(G.qual==0) {
1908                                         if (G.vd->flag & V3D_DISPIMAGE) {
1909                                                 G.vd->flag &= ~V3D_DISPIMAGE;
1910                                                 doredraw= 1;
1911                                         }
1912                                 }
1913                                 break;
1914                         }
1915                 }
1916         }
1917         
1918         if(doredraw) {
1919                 scrarea_queue_winredraw(curarea);
1920                 scrarea_queue_headredraw(curarea);
1921         }
1922 }
1923
1924 static void initview3d(ScrArea *sa)
1925 {
1926         View3D *vd;
1927         
1928         vd= MEM_callocN(sizeof(View3D), "initview3d");
1929         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
1930
1931         vd->spacetype= SPACE_VIEW3D;
1932         vd->blockscale= 0.7f;
1933         vd->viewquat[0]= 1.0f;
1934         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
1935         vd->persp= 1;
1936         vd->drawtype= OB_WIRE;
1937         vd->view= 7;
1938         vd->dist= 10.0;
1939         vd->lens= 35.0f;
1940         vd->near= 0.01f;
1941         vd->far= 500.0f;
1942         vd->grid= 1.0f;
1943         vd->gridlines= 16;
1944         vd->lay= vd->layact= 1;
1945         if(G.scene) {
1946                 vd->lay= vd->layact= G.scene->lay;
1947                 vd->camera= G.scene->camera;
1948         }
1949         vd->scenelock= 1;
1950         vd->gridflag |= V3D_SHOW_X;
1951         vd->gridflag |= V3D_SHOW_Y;
1952         vd->gridflag |= V3D_SHOW_FLOOR;
1953         vd->gridflag &= ~V3D_SHOW_Z;
1954 }
1955
1956
1957 /* ******************** SPACE: IPO ********************** */
1958
1959 static void changeview2dspace(ScrArea *sa, void *spacedata)
1960 {
1961         if(G.v2d==0) return;
1962
1963         test_view2d(G.v2d, curarea->winx, curarea->winy);
1964         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1965 }
1966
1967 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1968 {
1969         extern void do_ipobuts(unsigned short event);   // drawipo.c
1970         unsigned short event= evt->event;
1971         short val= evt->val;
1972         SpaceIpo *sipo= curarea->spacedata.first;
1973         View2D *v2d= &sipo->v2d;
1974         float dx, dy;
1975         int cfra, doredraw= 0;
1976         short mval[2];
1977         short mousebut = L_MOUSE;
1978         
1979         if(sa->win==0) return;
1980
1981         if(val) {
1982                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
1983
1984                 /* swap mouse buttons based on user preference */
1985                 if (U.flag & USER_LMOUSESELECT) {
1986                         if (event == LEFTMOUSE) {
1987                                 event = RIGHTMOUSE;
1988                                 mousebut = L_MOUSE;
1989                         } else if (event == RIGHTMOUSE) {
1990                                 event = LEFTMOUSE;
1991                                 mousebut = R_MOUSE;
1992                         }
1993                 }
1994
1995                 switch(event) {
1996                 case UI_BUT_EVENT:
1997                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
1998                         if(val>0 && val < 65) do_ipowin_buts(val-1);
1999                         else do_ipobuts(val);
2000                         break;
2001                         
2002                 case LEFTMOUSE:
2003                         if( in_ipo_buttons() ) {
2004                                 do_ipo_selectbuttons();
2005                                 doredraw= 1;
2006                         }
2007                         else if(view2dmove(LEFTMOUSE)); // only checks for sliders
2008                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
2009                         else {
2010                                 do {
2011                                         getmouseco_areawin(mval);
2012                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
2013                                         
2014                                         cfra= (int)dx;
2015                                         if(cfra< 1) cfra= 1;
2016                                         
2017                                         if( cfra!=CFRA ) {
2018                                                 CFRA= cfra;
2019                                                 update_for_newframe_nodraw(1);  /* 1 = nosound */
2020                                                 force_draw_all(0); /* To make constraint sliders redraw */
2021                                         }
2022                                         else PIL_sleep_ms(30);
2023                                 
2024                                 } while(get_mbut() & mousebut);
2025                         }
2026                         break;
2027                 case RIGHTMOUSE:
2028                         mouse_select_ipo();
2029                         allqueue (REDRAWACTION, 0);
2030                         allqueue(REDRAWNLA, 0);
2031                         break;
2032                 case MIDDLEMOUSE:
2033                         if(in_ipo_buttons()) {
2034                                 scroll_ipobuts();
2035                         }
2036                         else view2dmove(event); /* in drawipo.c */
2037                         break;
2038                 case WHEELUPMOUSE:
2039                 case WHEELDOWNMOUSE:
2040                         view2dmove(event);      /* in drawipo.c */
2041                         break;
2042                 case PADPLUSKEY:
2043                         view2d_zoom(v2d, 0.1154f, sa->winx, sa->winy);
2044                         doredraw= 1;
2045                         break;
2046                 case PADMINUS:
2047                         view2d_zoom(v2d, -0.15f, sa->winx, sa->winy);
2048                         doredraw= 1;
2049                         break;
2050                 case PAGEUPKEY:
2051                         if(G.qual==LR_CTRLKEY)
2052                                 movekey_ipo(1);
2053                         else if((G.qual==0))
2054                                 nextkey_ipo(1);
2055                         break;
2056                 case PAGEDOWNKEY:
2057                         if(G.qual==LR_CTRLKEY)
2058                                 movekey_ipo(-1);
2059                         else if((G.qual==0))
2060                                 nextkey_ipo(-1);
2061                         break;
2062                 case HOMEKEY:
2063                         if((G.qual==0))
2064                                 do_ipo_buttons(B_IPOHOME);
2065                         break;
2066                         
2067                 case AKEY:
2068                         if((G.qual==0)) {
2069                                 if(in_ipo_buttons()) {
2070                                         swap_visible_editipo();
2071                                 }
2072                                 else {
2073                                         swap_selectall_editipo();
2074                                 }
2075                                 allspace (REMAKEIPO, 0);
2076                                 allqueue (REDRAWNLA, 0);
2077                                 allqueue (REDRAWACTION, 0);
2078                         }
2079                         break;
2080                 case BKEY:
2081                         if((G.qual==0))
2082                                 borderselect_ipo();
2083                         break;
2084                 case CKEY:
2085                         if((G.qual==0))
2086                                 move_to_frame();
2087                         break;
2088                 case DKEY:
2089                         if((G.qual==LR_SHIFTKEY))
2090                                 add_duplicate_editipo();
2091                         break;
2092                 case GKEY:
2093                         if((G.qual==0))
2094                                 transform_ipo('g');
2095                         break;
2096                 case HKEY:
2097                         if(G.qual==LR_ALTKEY)
2098                                 sethandles_ipo(HD_AUTO_ANIM);
2099                         if(G.qual==LR_SHIFTKEY)
2100                                 sethandles_ipo(HD_AUTO);
2101                         else if(G.qual==0)
2102                                 sethandles_ipo(HD_ALIGN);
2103                         break;
2104                 case JKEY:
2105                         if((G.qual==0))
2106                                 join_ipo_menu();
2107                         break;
2108                 case KKEY:
2109                         if((G.qual==0)) {
2110                                 ipo_toggle_showkey();
2111                                 scrarea_queue_headredraw(curarea);
2112                                 allqueue(REDRAWVIEW3D, 0);
2113                                 doredraw= 1;
2114                         }
2115                         break;
2116                 case NKEY:
2117                         toggle_blockhandler(sa, IPO_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2118                         doredraw= 1;
2119                         break;
2120                 case RKEY:
2121                         if((G.qual==0))
2122                                 ipo_record();
2123                         break;
2124                 case SKEY:
2125                         if((G.qual==LR_SHIFTKEY)) {             
2126                                 ipo_snap_menu();
2127                         } else if((G.qual==0))
2128                                 transform_ipo('s');
2129                         break;
2130                 case TKEY:
2131                         if((G.qual==0))
2132                                 set_ipotype();
2133                         break;
2134                 case VKEY:
2135                         if((G.qual==0))
2136                                 sethandles_ipo(HD_VECT);
2137                         break;
2138                 case XKEY:
2139                 case DELKEY:
2140                         del_ipo();
2141                         break;
2142                 }
2143         }
2144
2145         if(doredraw) scrarea_queue_winredraw(sa);
2146 }
2147
2148 void initipo(ScrArea *sa)
2149 {
2150         SpaceIpo *sipo;
2151         
2152         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
2153         BLI_addhead(&sa->spacedata, sipo);
2154
2155         sipo->spacetype= SPACE_IPO;
2156         sipo->blockscale= 0.7f;
2157         
2158         /* sipo space loopt van (0,-?) tot (??,?) */
2159         sipo->v2d.tot.xmin= 0.0;
2160         sipo->v2d.tot.ymin= -10.0;
2161         sipo->v2d.tot.xmax= G.scene->r.efra;
2162         sipo->v2d.tot.ymax= 10.0;
2163
2164         sipo->v2d.cur= sipo->v2d.tot;
2165
2166         sipo->v2d.min[0]= 0.01f;
2167         sipo->v2d.min[1]= 0.01f;
2168
2169         sipo->v2d.max[0]= 15000.0f;
2170         sipo->v2d.max[1]= 10000.0f;
2171         
2172         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
2173         sipo->v2d.keeptot= 0;
2174
2175         sipo->blocktype= ID_OB;
2176 }
2177
2178 /* ******************** SPACE: INFO ********************** */
2179
2180 void space_mipmap_button_function(int event) {
2181         set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
2182
2183         allqueue(REDRAWVIEW3D, 0);
2184 }
2185
2186 #if 0
2187 static void space_sound_button_function(int event)
2188 {
2189         int a;
2190         SYS_SystemHandle syshandle;
2191
2192         if ((syshandle = SYS_GetSystem()))
2193         {
2194                 a = (U.gameflags & USER_DISABLE_SOUND);
2195                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
2196         }
2197 }
2198 #endif
2199
2200 // needed for event; choose new 'curmain' resets it...
2201 static short th_curcol= TH_BACK;
2202 static char *th_curcol_ptr= NULL;
2203 static char th_curcol_arr[4]={0, 0, 0, 255};
2204
2205 static void info_user_themebuts(uiBlock *block, short y1, short y2, short y3)
2206 {
2207         bTheme *btheme, *bt;
2208         int spacetype= 0;
2209         static short cur=1, curmain=2;
2210         short a, tot=0, isbuiltin= 0;
2211         char string[21*32], *strp, *col;
2212         
2213         y3= y2+23;      // exception!
2214         
2215         /* count total, max 16! */
2216         for(bt= U.themes.first; bt; bt= bt->next) tot++;
2217         
2218         /* if cur not is 1; move that to front of list */
2219         if(cur!=1) {
2220                 a= 1;
2221                 for(bt= U.themes.first; bt; bt= bt->next, a++) {
2222                         if(a==cur) {
2223                                 BLI_remlink(&U.themes, bt);
2224                                 BLI_addhead(&U.themes, bt);
2225                                 allqueue(REDRAWALL, 0);
2226                                 cur= 1;
2227                                 break;
2228                         }
2229                 }
2230         }
2231         
2232         /* the current theme */
2233         btheme= U.themes.first;
2234         if(strcmp(btheme->name, "Default")==0) isbuiltin= 1;
2235
2236         /* construct popup script */
2237         string[0]= 0;
2238         for(bt= U.themes.first; bt; bt= bt->next) {
2239                 strcat(string, bt->name);
2240                 if(btheme->next) strcat(string, "   |");
2241         }
2242         uiDefButS(block, MENU, B_UPDATE_THEME, string,                  45,y3,200,20, &cur, 0, 0, 0, 0, "Current theme");
2243         
2244         /* add / delete / name */
2245
2246         if(tot<16)
2247                 uiDefBut(block, BUT, B_ADD_THEME, "Add",        45,y2,200,20, NULL, 0, 0, 0, 0, "Makes new copy of this theme");
2248         if(tot>1 && isbuiltin==0)
2249                 uiDefBut(block, BUT, B_DEL_THEME, "Delete", 45,y1,200,20, NULL, 0, 0, 0, 0, "Delete theme");
2250
2251         if(isbuiltin) return;
2252         
2253         /* name */
2254         uiDefBut(block, TEX, B_NAME_THEME, "",                  255,y3,200,20, btheme->name, 1.0, 30.0, 0, 0, "Rename theme");
2255
2256         /* main choices pup */
2257         uiDefButS(block, MENU, B_CHANGE_THEME, "UI and Buttons %x1|%l|3D View %x2|%l|Ipo Curve Editor %x3|Action Editor %x4|"
2258                 "NLA Editor %x5|%l|UV/Image Editor %x6|Video Sequence Editor %x7|Node Editor %x16|Timeline %x15|Audio Window %x8|Text Editor %x9|%l|User Preferences %x10|"
2259                 "Outliner %x11|Buttons Window %x12|%l|File Browser %x13|Image Browser %x14",
2260                                                                                                         255,y2,200,20, &curmain, 0, 0, 0, 0, "Specify theme for...");
2261         if(curmain==1) spacetype= 0;
2262         else if(curmain==2) spacetype= SPACE_VIEW3D;
2263         else if(curmain==3) spacetype= SPACE_IPO;
2264         else if(curmain==4) spacetype= SPACE_ACTION;
2265         else if(curmain==5) spacetype= SPACE_NLA;
2266         else if(curmain==6) spacetype= SPACE_IMAGE;
2267         else if(curmain==7) spacetype= SPACE_SEQ;
2268         else if(curmain==8) spacetype= SPACE_SOUND;
2269         else if(curmain==9) spacetype= SPACE_TEXT;
2270         else if(curmain==10) spacetype= SPACE_INFO;
2271         else if(curmain==11) spacetype= SPACE_OOPS;
2272         else if(curmain==12) spacetype= SPACE_BUTS;
2273         else if(curmain==13) spacetype= SPACE_FILE;
2274         else if(curmain==14) spacetype= SPACE_IMASEL;
2275         else if(curmain==15) spacetype= SPACE_TIME;
2276         else if(curmain==16) spacetype= SPACE_NODE;
2277         else return; // only needed while coding... when adding themes for more windows
2278         
2279         /* color choices pup */
2280         if(curmain==1) {
2281                 strp= BIF_ThemeColorsPup(0);
2282                 if(th_curcol==TH_BACK) th_curcol= TH_BUT_OUTLINE;  // switching main choices...
2283         }
2284         else strp= BIF_ThemeColorsPup(spacetype);
2285         
2286         uiDefButS(block, MENU, B_REDR, strp,                    255,y1,200,20, &th_curcol, 0, 0, 0, 0, "Current color");
2287         MEM_freeN(strp);
2288         
2289         th_curcol_ptr= col= BIF_ThemeGetColorPtr(btheme, spacetype, th_curcol);
2290         if(col==NULL) return;
2291         
2292         /* first handle exceptions, special single values, row selection, etc */
2293         if(th_curcol==TH_VERTEX_SIZE) {
2294                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Vertex size ", 465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2295         }
2296         else if(th_curcol==TH_FACEDOT_SIZE) {
2297                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Face dot size ",       465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2298         }
2299         else if(th_curcol==TH_BUT_DRAWTYPE) {
2300                 uiBlockBeginAlign(block);
2301                 uiDefButC(block, ROW, B_UPDATE_THEME, "Minimal",        465,y3,100,20,  col, 2.0, 0.0, 0, 0, "");
2302                 uiDefButC(block, ROW, B_UPDATE_THEME, "Shaded", 565,y3,100,20,  col, 2.0, 1.0, 0, 0, "");
2303                 uiDefButC(block, ROW, B_UPDATE_THEME, "Rounded",        465,y2,100,20,  col, 2.0, 2.0, 0, 0, "");
2304                 uiDefButC(block, ROW, B_UPDATE_THEME, "OldSkool",       565,y2,100,20,  col, 2.0, 3.0, 0, 0, "");
2305                 uiBlockEndAlign(block);
2306         }
2307         else {
2308                 uiBlockBeginAlign(block);
2309                 if ELEM7(th_curcol, TH_PANEL, TH_LAMP, TH_FACE, TH_FACE_SELECT, TH_MENU_BACK, TH_MENU_HILITE, TH_MENU_ITEM) {
2310                         uiDefButC(block, NUMSLI, B_UPDATE_THEME,"A ",   465,y3+25,200,20,  col+3, 0.0, 255.0, B_THEMECOL, 0, "");
2311                 }
2312                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"R ",   465,y3,200,20,  col, 0.0, 255.0, B_THEMECOL, 0, "");
2313                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"G ",   465,y2,200,20,  col+1, 0.0, 255.0, B_THEMECOL, 0, "");
2314                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"B ",   465,y1,200,20,  col+2, 0.0, 255.0, B_THEMECOL, 0, "");
2315                 uiBlockEndAlign(block);
2316
2317                 uiDefButC(block, COL, B_UPDATE_THEME, "",               675,y1,50,y3-y1+20, col, 0, 0, 0, 0, "");
2318                                 
2319                 /* copy paste */
2320                 uiBlockBeginAlign(block);
2321                 uiDefBut(block, BUT, B_THEME_COPY, "Copy Color",        755,y2,120,20, NULL, 0, 0, 0, 0, "Stores current color in buffer");
2322                 uiDefBut(block, BUT, B_THEME_PASTE, "Paste Color",      755,y1,120,20, NULL, 0, 0, 0, 0, "Pastes buffer color");
2323                 uiBlockEndAlign(block);
2324                 
2325                 uiDefButC(block, COL, 0, "",                            885,y1,50,y2-y1+20, th_curcol_arr, 0, 0, 0, 0, "");
2326                 
2327         }
2328 }
2329
2330
2331 void drawinfospace(ScrArea *sa, void *spacedata)
2332 {
2333         uiBlock *block;
2334         static short cur_light=0, cur_light_var=0;
2335         float fac, col[3];
2336         short xpos, ypos, ypostab,  buth, rspace, dx, y1, y2, y3, y4, y5, y6;
2337         short y2label, y3label, y4label, y5label, y6label;
2338         short spref, mpref, lpref, smfileselbut;
2339         short edgsp, midsp;
2340         char naam[32];
2341
2342         if(curarea->win==0 || curarea->winy<2) return;
2343
2344         BIF_GetThemeColor3fv(TH_BACK, col);
2345         glClearColor(col[0], col[1], col[2], 0.0);
2346         glClear(GL_COLOR_BUFFER_BIT);
2347
2348         if(curarea->winx<=1280.0) {
2349                 fac= ((float)curarea->winx)/1280.0f;
2350                 myortho2(0.375f, 1280.375f, 0.375f, curarea->winy/fac + 0.375f);
2351         }
2352         else {
2353                 myortho2(0.375f, (float)curarea->winx + 0.375f, 0.375f, (float)curarea->winy + 0.375f);
2354         }
2355         
2356         sprintf(naam, "infowin %d", curarea->win);
2357         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->win);
2358
2359
2360         /* Vars for nice grid alignment */ 
2361         dx= (1280-90)/7;        /* spacing for use in equally dividing 'tab' row */
2362
2363         xpos = 45;              /* left padding */
2364         ypos = 50;              /* bottom padding for buttons */
2365         ypostab = 10;           /* bottom padding for 'tab' row */
2366
2367         buth = 20;              /* standard button height */
2368
2369         spref = 90;     /* standard size for small preferences button */
2370         mpref = 189;    /* standard size for medium preferences button */
2371         lpref = 288;    /* standard size for large preferences button */
2372         smfileselbut = buth;    /* standard size for fileselect button (square) */
2373
2374         edgsp = 3;              /* space from edge of end 'tab' to edge of end button */
2375         midsp = 9;              /* horizontal space between buttons */
2376
2377         rspace = 3;             /* default space between rows */
2378
2379         y1 = ypos;              /* grid alignment for each row of buttons */
2380         y2 = ypos+buth+rspace;
2381         y3 = ypos+2*(buth+rspace);
2382         y4 = ypos+3*(buth+rspace);
2383         y5 = ypos+4*(buth+rspace);
2384         y6 = ypos+5*(buth+rspace);
2385
2386
2387         y2label = y2-2;         /* adjustments to offset the labels down to align better */
2388         y3label = y3-2;
2389         y4label = y4-2;
2390         y5label = y5-2;
2391         y6label = y6-2;
2392
2393
2394         /* set the colour to blue and draw the main 'tab' controls */
2395
2396         uiBlockSetCol(block, TH_BUT_SETTING1);
2397         uiBlockBeginAlign(block);
2398         
2399         uiDefButS(block, ROW,B_USERPREF,"View & Controls",
2400                 xpos,ypostab,(short)dx,buth,
2401                 &U.userpref,1.0,0.0, 0, 0,"");
2402                 
2403         uiDefButS(block, ROW,B_USERPREF,"Edit Methods",
2404                 (short)(xpos+dx),ypostab,(short)dx,buth,
2405                 &U.userpref,1.0,1.0, 0, 0,"");
2406
2407         uiDefButS(block, ROW,B_USERPREF,"Language & Font",
2408                 (short)(xpos+2*dx),ypostab,(short)dx,buth,
2409                 &U.userpref,1.0,2.0, 0, 0,"");
2410
2411         uiDefButS(block, ROW,B_USERPREF,"Themes",
2412                 (short)(xpos+3*dx),ypostab,(short)dx,buth,
2413                 &U.userpref,1.0,6.0, 0, 0,"");
2414
2415         uiDefButS(block, ROW,B_USERPREF,"Auto Save",
2416                 (short)(xpos+4*dx),ypostab,(short)dx,buth,
2417                 &U.userpref,1.0,3.0, 0, 0,"");
2418
2419         uiDefButS(block, ROW,B_USERPREF,"System & OpenGL",
2420                 (short)(xpos+5*dx),ypostab,(short)dx,buth,
2421                 &U.userpref,1.0,4.0, 0, 0,"");
2422                 
2423         uiDefButS(block, ROW,B_USERPREF,"File Paths",
2424                 (short)(xpos+6*dx),ypostab,(short)dx,buth,
2425                 &U.userpref,1.0,5.0, 0, 0,"");
2426
2427         uiBlockSetCol(block, TH_AUTO);
2428         uiBlockEndAlign(block);
2429         /* end 'tab' controls */
2430
2431         /* line 2: left x co-ord, top y co-ord, width, height */
2432
2433         if(U.userpref == 6) {
2434                 info_user_themebuts(block, y1, y2, y3);
2435         }
2436         else if (U.userpref == 0) { /* view & controls */
2437
2438                 uiDefBut(block, LABEL,0,"Display:",
2439                         xpos,y6label,spref,buth,
2440                         0, 0, 0, 0, 0, "");     
2441                 uiBlockBeginAlign(block);
2442                 uiDefButBitI(block, TOG, USER_TOOLTIPS, 0, "ToolTips",
2443                         (xpos+edgsp),y5,spref,buth,
2444                         &(U.flag), 0, 0, 0, 0,
2445                         "Display tooltips (help tags) over buttons");
2446                 uiDefButBitI(block, TOG, USER_DRAWVIEWINFO, B_DRAWINFO, "Object Info",
2447                         (xpos+edgsp),y4,spref,buth,
2448                         &(U.uiflag), 0, 0, 0, 0,
2449                         "Display active object name and frame number in the 3D View");
2450                 uiDefButBitI(block, TOG, USER_SCENEGLOBAL, 0, "Global Scene",
2451                         (xpos+edgsp),y3,spref,buth,
2452                         &(U.flag), 0, 0, 0, 0,
2453                         "Forces the current Scene to be displayed in all Screens");
2454 #ifndef __APPLE__       
2455                 uiDefButBitS(block, TOG, 1, 0, "Large Cursors",
2456                         (xpos+edgsp),y2,spref,buth,
2457                         &(U.curssize), 0, 0, 0, 0,
2458                         "Use large mouse cursors when available");
2459 #else 
2460                 U.curssize=0; /*Small Cursor always for OS X for now */
2461 #endif
2462                 uiDefButBitI(block, TOG, USER_PLAINMENUS, B_PLAINMENUS, "Plain menus",
2463                         (xpos+edgsp),y1,spref,buth,
2464                         &(U.uiflag), 0, 0, 0, 0,
2465                         "Use column layout for toolbox and do not flip contents in any menu");
2466                 uiBlockEndAlign(block);
2467
2468                 uiDefBut(block, LABEL,0,"Menus:",
2469                         (xpos+(2*edgsp)+spref),y6label,spref,buth,
2470                         0, 0, 0, 0, 0, "");
2471                 uiBlockBeginAlign(block);
2472                 uiDefButBitI(block, TOG, USER_MENUOPENAUTO, 0, "Open on Mouse Over",
2473                         (xpos+edgsp+spref+midsp),y5,mpref,buth,
2474                         &(U.uiflag), 0, 0, 0, 0,
2475                         "Open menu buttons and pulldowns automatically when the mouse is hovering");
2476                 uiDefButS(block, NUM, 0, "Top Level:",
2477                         (xpos+edgsp+spref+midsp),y4,spref+edgsp,buth,
2478                         &(U.menuthreshold1), 1, 40, 0, 0,
2479                         "Time delay in 1/10 seconds before automatically opening top level menus");
2480                 uiDefButS(block, NUM, 0, "Sublevels:",
2481                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y4,spref+edgsp,buth,
2482                         &(U.menuthreshold2), 1, 40, 0, 0,
2483                         "Time delay in 1/10 seconds before automatically opening menu sublevels");
2484                 uiBlockEndAlign(block);
2485
2486                 uiDefBut(block, LABEL,0,"Toolbox click-hold delay:",
2487                         (xpos+(2*edgsp)+spref),y3label,mpref,buth,
2488                         0, 0, 0, 0, 0, "");
2489                 uiBlockBeginAlign(block);
2490                 uiDefButS(block, NUM, 0, "LMB:",
2491                         (xpos+edgsp+spref+midsp),y2,spref+edgsp,buth,
2492                         &(U.tb_leftmouse), 2, 40, 0, 0,
2493                         "Time in 1/10 seconds to hold the Left Mouse Button before opening the toolbox");
2494                 uiDefButS(block, NUM, 0, "RMB:",
2495                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y2,spref+edgsp,buth,
2496                         &(U.tb_rightmouse), 2, 40, 0, 0,
2497                         "Time in 1/10 seconds to hold the Right Mouse Button before opening the toolbox");      
2498                 uiBlockEndAlign(block);
2499
2500                 uiDefButBitI(block, TOG, USER_PANELPINNED, 0, "Pin Floating Panels",
2501                         (xpos+edgsp+spref+midsp),y1,(mpref/2),buth,
2502                         &(U.uiflag), 0, 0, 0, 0,
2503                         "Make floating panels invoked by a hotkey (eg. N Key) open at the previous location");
2504                 
2505                 uiDefButBitI(block, TOG, USER_LOCKAROUND, B_DRAWINFO, "Global Pivot",
2506                         (xpos+edgsp+spref+midsp+(mpref/2)),y1,(mpref/2),buth,
2507                         &(U.uiflag), 0, 0, 0, 0,
2508                         "Lock the same rotation/scaling pivot in all 3D Views");        
2509                 
2510                 uiDefBut(block, LABEL,0,"Snap to grid:",
2511                         (xpos+(2*edgsp)+spref+midsp+mpref),y6label,mpref,buth,
2512                         0, 0, 0, 0, 0, "");
2513                 uiBlockBeginAlign(block);
2514                 uiDefButBitI(block, TOG, USER_AUTOGRABGRID, 0, "Grab/Move",
2515                         (xpos+edgsp+mpref+spref+(2*midsp)),y5,spref,buth,
2516                         &(U.flag), 0, 0, 0, 0,
2517                         "Snap objects and sub-objects to grid units when moving");
2518                 uiDefButBitI(block, TOG, USER_AUTOROTGRID, 0, "Rotate",
2519                         (xpos+edgsp+mpref+spref+(2*midsp)),y4,spref,buth,
2520                         &(U.flag), 0, 0, 0, 0,
2521                         "Snap objects and sub-objects to grid units when rotating");
2522                 uiDefButBitI(block, TOG, USER_AUTOSIZEGRID, 0, "Scale",
2523                         (xpos+edgsp+mpref+spref+(2*midsp)),y3,spref,buth,
2524                         &(U.flag), 0, 0, 0, 0,
2525                         "Snap objects and sub-objects to grid units when scaling");
2526                 uiBlockEndAlign(block);
2527                 
2528                 uiDefBut(block, LABEL,0,"View zoom:",
2529                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y6label,mpref,buth,
2530                         0, 0, 0, 0, 0, "");
2531                 uiBlockBeginAlign(block);
2532                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2533                 uiDefButS(block, ROW, 0, "Continue",
2534                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y5,(mpref/3),buth,
2535                         &(U.viewzoom), 40, USER_ZOOM_CONT, 0, 0,
2536                         "Old style zoom, continues while moving mouse up or down");
2537                 uiDefButS(block, ROW, 0, "Dolly",
2538                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/3)),y5,(mpref/3),buth,
2539                         &(U.viewzoom), 40, USER_ZOOM_DOLLY, 0, 0,
2540                         "Zooms in and out based on vertical mouse movement.");
2541                 uiDefButS(block, ROW, 0, "Scale",
2542                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(2*mpref/3)),y5,(mpref/3),buth,
2543                         &(U.viewzoom), 40, USER_ZOOM_SCALE, 0, 0,
2544                         "Zooms in and out like scaling the view, mouse movements relative to center.");
2545                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2546                 uiBlockEndAlign(block);
2547                 
2548                 uiDefBut(block, LABEL,0,"View rotation:",
2549                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y4label,mpref,buth,
2550                         0, 0, 0, 0, 0, "");
2551                 uiBlockBeginAlign(block);
2552                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2553                 uiDefButBitI(block, TOG, USER_TRACKBALL, B_DRAWINFO, "Trackball",
2554                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y3,(mpref/2),buth,
2555                         &(U.flag), 0, 0, 0, 0,
2556                         "Allow the view to tumble freely when orbiting with the Middle Mouse Button");
2557                 uiDefButBitI(block, TOGN, USER_TRACKBALL, B_DRAWINFO, "Turntable",
2558                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y3,(mpref/2),buth,
2559                         &(U.flag), 0, 0, 0, 0,
2560                         "Use fixed up axis for orbiting with Middle Mouse Button");
2561                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2562                 uiDefButBitI(block, TOG, USER_AUTOPERSP, B_DRAWINFO, "Auto Perspective",
2563                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y2,(mpref/2),buth,
2564                         &(U.uiflag), 0, 0, 0, 0,
2565                         "Automatically switch between orthographic and perspective when changing from top/front/side views");
2566                 uiDefButBitI(block, TOG, USER_ORBIT_SELECTION, B_DRAWINFO, "Around Active",
2567                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y2,(mpref/2),buth,
2568                         &(U.uiflag), 0, 0, 0, 0,
2569                         "Keep the active object in place when orbiting the views (Object Mode)");
2570                 uiBlockEndAlign(block);
2571
2572                 uiDefBut(block, LABEL,0,"Select with:",
2573                         (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y6label,mpref,buth,
2574                         0, 0, 0, 0, 0, "");
2575                 uiBlockBeginAlign(block);
2576                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2577                 uiDefButBitI(block, TOG, USER_LMOUSESELECT, B_DRAWINFO, "Left Mouse",
2578                         (xpos+edgsp+(3*mpref)+(4*midsp)),y5,(mpref/2),buth,
2579                         &(U.flag), 0, 0, 0, 0, "Use the Left Mouse Button for selection");
2580                 uiDefButBitI(block, TOGN, USER_LMOUSESELECT, B_DRAWINFO, "Right Mouse",
2581                         (xpos+edgsp+(3*mpref)+(4*midsp)+(mpref/2)),y5,(mpref/2),buth,
2582                         &(U.flag), 0, 0, 0, 0, "Use the Right Mouse Button for selection");
2583                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2584                 uiBlockEndAlign(block);
2585                 
2586                 
2587                 if(U.flag & USER_LMOUSESELECT) {
2588                         uiDefBut(block, LABEL,0,"Cursor with: Right Mouse",
2589                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2590                                 0, 0, 0, 0, 0, "");
2591                 } else {
2592                         uiDefBut(block, LABEL,0,"Cursor with: Left Mouse",
2593                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2594                                 0, 0, 0, 0, 0, "");
2595                 }
2596                 
2597                 /* illegal combo... */
2598                 if (U.flag & USER_LMOUSESELECT) 
2599                         U.flag &= ~USER_TWOBUTTONMOUSE;
2600                 
2601                 uiDefButBitI(block, TOG, USER_TWOBUTTONMOUSE, B_DRAWINFO, "Emulate 3 Button Mouse",
2602                         (xpos+edgsp+(3*mpref)+(4*midsp)),y3,mpref,buth,
2603                         &(U.flag), 0, 0, 0, 0,
2604                         "Emulates Middle Mouse with Alt+LeftMouse (doesnt work with Left Mouse Select option)");
2605                 
2606                         
2607                 uiDefBut(block, LABEL,0,"Middle Mouse Button:",
2608                         (xpos+(2*edgsp)+(4*mpref)+(4*midsp)),y6label,mpref,buth,
2609                         0, 0, 0, 0, 0, "");
2610                 uiBlockBeginAlign(block);
2611                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2612                 uiDefButBitI(block, TOGN, USER_VIEWMOVE, B_DRAWINFO, "Rotate View",
2613                         (xpos+edgsp+(4*mpref)+(5*midsp)),y5,(mpref/2),buth,
2614                         &(U.flag), 0, 0, 0, 0, "Default action for the Middle Mouse Button");
2615                 uiDefButBitI(block, TOG, USER_VIEWMOVE, B_DRAWINFO, "Pan View",
2616                         (xpos+edgsp+(4*mpref)+(5*midsp)+(mpref/2)),y5,(mpref/2),buth,
2617                         &(U.flag), 0, 0, 0, 0, "Default action for the Middle Mouse Button");
2618                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2619                 uiBlockEndAlign(block);
2620                         
2621                 uiDefBut(block, LABEL,0,"Mouse Wheel:",
2622                         (xpos+(2*edgsp)+(4*mpref)+(4*midsp)),y4label,mpref,buth,
2623                         0, 0, 0, 0, 0, "");
2624                 uiBlockBeginAlign(block);
2625                 uiDefButBitI(block, TOG, USER_WHEELZOOMDIR, 0, "Invert Zoom",
2626                         (xpos+edgsp+(4*mpref)+(5*midsp)),y3,spref,buth,
2627                         &(U.uiflag), 0, 0, 0, 0,
2628                         "Swap the Mouse Wheel zoom direction");
2629                 uiDefButI(block, NUM, 0, "Scroll Lines:",
2630                         (xpos+edgsp+(4*mpref)+(6*midsp)+spref-edgsp),y3,spref+edgsp,buth,
2631                         &U.wheellinescroll, 0.0, 32.0, 0, 0,
2632                         "The number of lines scrolled at a time with the mouse wheel"); 
2633                 uiBlockEndAlign(block);
2634
2635
2636                 uiDefBut(block, LABEL,0,"3D Transform Widget:",
2637                                  (xpos+(2*edgsp)+(5*mpref)+(5*midsp)),y6label,mpref,buth,
2638                                  0, 0, 0, 0, 0, "");
2639                 uiBlockBeginAlign(block);
2640                 uiDefButS(block, NUM, B_REDRCURW3D, "Size:",
2641                                          (xpos+edgsp+(5*mpref)+(6*midsp)),y5,(mpref/2),buth,
2642                                          &(U.tw_size), 2, 40, 0, 0, "Diameter of widget, in 10 pixel units");
2643                 uiDefButS(block, NUM, B_REDRCURW3D, "Handle:",
2644                                          (xpos+edgsp+(5*mpref)+(6*midsp)+(mpref/2)),y5,(mpref/2),buth,
2645                                          &(U.tw_handlesize), 2, 40, 0, 0, "Size of widget handles as percentage of widget radius");
2646                 uiDefButS(block, NUM, B_REDRCURW3D, "Hotspot:",
2647                                   (xpos+edgsp+(5*mpref)+(6*midsp)),y4,(mpref),buth,
2648                                   &(U.tw_hotspot), 4, 40, 0, 0, "Hotspot in pixels for clicking widget handles");
2649                 uiBlockEndAlign(block);
2650                 
2651                 
2652                 uiDefBut(block, LABEL,0,"Object center diameter",
2653                                  (xpos+(2*edgsp)+(5*mpref)+(5*midsp)),y3label,mpref,buth,
2654                                  0, 0, 0, 0, 0, "");
2655                 uiBlockBeginAlign(block);
2656                 uiDefButS(block, NUM, B_REDRCURW3D, "Size",
2657                                   (xpos+(2*edgsp)+(5*mpref)+(5*midsp)),y2,mpref,buth,
2658                                   &(U.obcenter_dia), 4, 10, 0, 0,
2659                                   "Diameter in Pixels for Object/Lamp center drawing");
2660                 
2661                 
2662         } else if (U.userpref == 1) { /* edit methods */
2663
2664
2665                 uiDefBut(block, LABEL,0,"Material linked to:",
2666                         xpos,y3label,mpref,buth,
2667                         0, 0, 0, 0, 0, "");
2668                 uiBlockBeginAlign(block);
2669                 uiDefButBitI(block, TOGN, USER_MAT_ON_OB, B_DRAWINFO, "ObData",
2670                         (xpos+edgsp),y2,(mpref/2),buth,
2671                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the obData block");
2672                 uiDefButBitI(block, TOG, USER_MAT_ON_OB, B_DRAWINFO, "Object",
2673                         (xpos+edgsp+(mpref/2)),y2,(mpref/2),buth,
2674                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the object block");
2675                 uiBlockEndAlign(block);
2676
2677
2678                 uiDefBut(block, LABEL,0,"Undo:",
2679                         (xpos+(2*edgsp)+mpref),y3label, mpref,buth,
2680                         0, 0, 0, 0, 0, "");
2681                 uiBlockBeginAlign(block);
2682                 uiDefButS(block, NUMSLI, B_DRAWINFO, "Steps:",
2683                         (xpos+edgsp+mpref+midsp),y2,mpref,buth,
2684                         &(U.undosteps), 0, 64, 0, 0, "Number of undo steps available (smaller values conserve memory)");
2685
2686                 uiDefButBitI(block, TOG, USER_GLOBALUNDO, B_DRAWINFO, "Global undo",
2687                         (xpos+edgsp+mpref+midsp),y1,mpref,buth,
2688                         &(U.uiflag), 2, 64, 0, 0, "Global undo works by keeping a full copy of the file itself in memory, so takes extra memory");
2689                 uiBlockEndAlign(block);
2690
2691
2692                 uiDefBut(block, LABEL,0,"Auto keyframe",
2693                         (xpos+(2*edgsp)+(2*mpref)+midsp),y3label,mpref,buth,
2694                         0, 0, 0, 0, 0, "");
2695
2696                 uiDefButBitI(block, TOG, G_RECORDKEYS, REDRAWTIME, "Action and Object", 
2697                                         (xpos+edgsp+(2*mpref)+(2*midsp)),y2,mpref, buth,
2698                                          &(G.flags), 0, 0, 0, 0, "Automatic keyframe insertion in Object and Action Ipo curves");
2699
2700                 uiDefButBitI(block, TOG, USER_KEYINSERTAVAI, REDRAWTIME, "Available", 
2701                         (xpos+edgsp+(2*mpref)+(2*midsp)),y1,mpref, buth,
2702                         &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in available curves");
2703
2704 //              uiDefButBitS(block, TOG, USER_KEYINSERTACT, 0, "Action",
2705 //                      (xpos+edgsp+(2*mpref)+(2*midsp)),y2,(spref+edgsp),buth,
2706 //                      &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in Action Ipo curve");
2707 //              uiDefButBitS(block, TOG, USER_KEYINSERTOBJ, 0, "Object",
2708 //                      (xpos+edgsp+(2*mpref)+(3*midsp)+spref-edgsp),y2,(spref+edgsp),buth,
2709 //                      &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in Object Ipo curve");
2710
2711
2712                 uiDefBut(block, LABEL,0,"Duplicate with object:",
2713                         (xpos+(2*edgsp)+(3*midsp)+(3*mpref)+spref),y3label,mpref,buth,
2714                         0, 0, 0, 0, 0, "");
2715
2716                 uiBlockBeginAlign(block);
2717                 uiDefButBitI(block, TOG, USER_DUP_MESH, 0, "Mesh",
2718                         (xpos+edgsp+(4*midsp)+(3*mpref)+spref),y2,(spref+edgsp),buth,
2719                         &(U.dupflag), 0, 0, 0, 0, "Causes mesh data to be duplicated with Shift+D");
2720                 uiDefButBitI(block, TOG, USER_DUP_SURF, 0, "Surface",
2721                         (xpos+edgsp+(5*midsp)+(3*mpref)+(2*spref)),y2,(spref+edgsp),buth,
2722                         &(U.dupflag), 0, 0, 0, 0, "Causes surface data to be duplicated with Shift+D");
2723                 uiDefButBitI(block, TOG, USER_DUP_CURVE, 0, "Curve",
2724                         (xpos+edgsp+(6*midsp)+(3*mpref)+(3*spref)),y2,(spref+edgsp),buth,
2725                         &(U.dupflag), 0, 0, 0, 0, "Causes curve data to be duplicated with Shift+D");
2726                 uiDefButBitI(block, TOG, USER_DUP_FONT, 0, "Text",
2727                         (xpos+edgsp+(7*midsp)+(3*mpref)+(4*spref)),y2,(spref+edgsp),buth,
2728                         &(U.dupflag), 0, 0, 0, 0, "Causes text data to be duplicated with Shift+D");
2729                 uiDefButBitI(block, TOG, USER_DUP_MBALL, 0, "Metaball",
2730                         (xpos+edgsp+(8*midsp)+(3*mpref)+(5*spref)),y2,(spref+edgsp),buth,
2731                         &(U.dupflag), 0, 0, 0, 0, "Causes metaball data to be duplicated with Shift+D");
2732
2733                 uiDefButBitI(block, TOG, USER_DUP_ARM, 0, "Armature",