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