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