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