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