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