Plumiferos fix; having more than 64 Shape Keys didn't allow the channels in
[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) group_operation_with_menu();
1387                                 else if((G.qual==LR_SHIFTKEY))
1388                                         if(G.obedit) {
1389                                                 if(G.obedit->type==OB_MESH)
1390                                                         select_mesh_group_menu();
1391                                         } else
1392                                                 select_object_grouped_menu();
1393                                 else if(G.qual==LR_ALTKEY) {
1394                                         if(okee("Clear location")) {
1395                                                 clear_object('g');
1396                                         }
1397                                 }
1398                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1399                                         v3d->twtype= V3D_MANIP_TRANSLATE;
1400                                         doredraw= 1;
1401                                 }
1402                                 else if((G.qual==0)) {
1403                                         initTransform(TFM_TRANSLATION, CTX_NONE);
1404                                         Transform();
1405                                 }
1406                                 break;
1407                         case HKEY:
1408                                 if(G.obedit) {
1409                                         if(G.obedit->type==OB_MESH) {
1410                                                 if(G.qual==LR_CTRLKEY)
1411                                                         add_hook();
1412                                                 else if(G.qual==LR_ALTKEY)
1413                                                         reveal_mesh();
1414                                                 else if((G.qual==LR_SHIFTKEY))
1415                                                         hide_mesh(1);
1416                                                 else if((G.qual==0)) 
1417                                                         hide_mesh(0);
1418                                         }
1419                                         else if(G.obedit->type== OB_SURF) {
1420                                                 if(G.qual==LR_CTRLKEY)
1421                                                         add_hook();
1422                                                 else if(G.qual==LR_ALTKEY)
1423                                                         revealNurb();
1424                                                 else if((G.qual==LR_SHIFTKEY))
1425                                                         hideNurb(1);
1426                                                 else if((G.qual==0))
1427                                                         hideNurb(0);
1428                                         }
1429                                         else if(G.obedit->type==OB_CURVE) {
1430                                                 if(G.qual==LR_CTRLKEY)
1431                                                         add_hook();
1432                                                 else {
1433                                                         if(G.qual==LR_CTRLKEY)
1434                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
1435                                                         else if((G.qual==LR_SHIFTKEY))
1436                                                                 sethandlesNurb(1);
1437                                                         else if((G.qual==0))
1438                                                                 sethandlesNurb(3);
1439                                                         
1440                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1441                                                         BIF_undo_push("Handle change");
1442                                                         allqueue(REDRAWVIEW3D, 0);
1443                                                 }
1444                                         }
1445                                         else if(G.obedit->type==OB_LATTICE) {
1446                                                 if(G.qual==LR_CTRLKEY) add_hook();
1447                                         }
1448                                         else if(G.obedit->type==OB_MBALL) {
1449                                                 if(G.qual==LR_ALTKEY)
1450                                                         reveal_mball();
1451                                                 else if((G.qual==LR_SHIFTKEY))
1452                                                         hide_mball(1);
1453                                                 else if((G.qual==0)) 
1454                                                         hide_mball(0);
1455                                         }
1456                                         else if(G.obedit->type==OB_ARMATURE) {
1457                                                 if (G.qual==0)
1458                                                         hide_selected_armature_bones();
1459                                                 else if (G.qual==LR_SHIFTKEY)
1460                                                         hide_unselected_armature_bones();
1461                                                 else if (G.qual==LR_ALTKEY)
1462                                                         show_all_armature_bones();
1463                                         }
1464                                 }
1465                                 else if(G.f & G_FACESELECT)
1466                                         hide_tface();
1467                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1468                                         if (G.qual==0)
1469                                                 hide_selected_pose_bones();
1470                                         else if (G.qual==LR_SHIFTKEY)
1471                                                 hide_unselected_pose_bones();
1472                                         else if (G.qual==LR_ALTKEY)
1473                                                 show_all_pose_bones();
1474                                 }
1475                                 break;
1476                         case IKEY:
1477                                 if(G.obedit);
1478                                 else if(G.qual==LR_CTRLKEY) {
1479                                         if(ob && ob->type==OB_ARMATURE) 
1480                                                 if(ob->flag & OB_POSEMODE) 
1481                                                         pose_add_IK();
1482                                 }
1483                                 else if(G.qual==LR_ALTKEY) {
1484                                         if(ob && ob->type==OB_ARMATURE) 
1485                                                 if(ob->flag & OB_POSEMODE) 
1486                                                         pose_clear_IK();
1487                                 }
1488                                 break;
1489                                 
1490                         case JKEY:
1491                                 if(G.qual==LR_CTRLKEY) {
1492                                         if( ob ) {
1493                                                 join_menu();
1494                                         }
1495                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1496                                                 addsegment_nurb();
1497                                 }
1498                                 else if(G.obedit) {
1499                                         if(G.obedit->type==OB_MESH) {
1500                                                 join_triangles();
1501                                         }
1502                                 }
1503
1504                                 break;
1505                         case KKEY:
1506                                 if(G.obedit) {
1507                                         if (G.obedit->type==OB_MESH) {
1508                                                 if (G.qual==LR_SHIFTKEY)
1509                                                         KnifeSubdivide(KNIFE_PROMPT);
1510                                                 else if (G.qual==0)
1511                                                         LoopMenu();
1512                                         }
1513                                         else if(G.obedit->type==OB_SURF)
1514                                                 printknots();
1515                                 }
1516                                 else {
1517                                         if((G.qual==LR_SHIFTKEY)) {
1518                                                 if(G.f & G_FACESELECT)
1519                                                         if (G.f & G_WEIGHTPAINT)
1520                                                                 clear_wpaint_selectedfaces();
1521                                                         else
1522                                                                 clear_vpaint_selectedfaces();
1523                                                 else if(G.f & G_VERTEXPAINT)
1524                                                         clear_vpaint();
1525                                                 else
1526                                                         select_select_keys();
1527                                         }
1528                                         else if (G.qual==0)
1529                                                 set_ob_ipoflags();
1530                                 }
1531                                 
1532                                 break;
1533                         case LKEY:
1534                                 if(G.obedit) {
1535                                         if(G.obedit->type==OB_MESH)
1536                                                 selectconnected_mesh(G.qual);
1537                                         if(G.obedit->type==OB_ARMATURE)
1538                                                 selectconnected_armature();
1539                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1540                                                 selectconnected_nurb();
1541                                 }
1542                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1543                                         selectconnected_posearmature();
1544                                 }
1545                                 else {
1546                                         if(G.f & G_FACESELECT) {
1547                                                 if((G.qual==0))
1548                                                         select_linked_tfaces(0);
1549                                                 else if((G.qual==LR_SHIFTKEY))
1550                                                         select_linked_tfaces(1);
1551                                                 else if(G.qual==LR_CTRLKEY)
1552                                                         select_linked_tfaces(2);
1553                                         }
1554                                         else {
1555                                                 if((G.qual==0))
1556                                                         make_local();
1557                                                 else if((G.qual==LR_SHIFTKEY))
1558                                                         selectlinks_menu();
1559                                                 else if(G.qual==LR_CTRLKEY)
1560                                                         make_links_menu();
1561                                         }
1562                                 }
1563                                 break;
1564                         case MKEY:
1565                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1566                                         mirror_uv_tface();
1567                                 else if(G.obedit){
1568                                         if(G.qual==LR_ALTKEY) {
1569                                                 if(G.obedit->type==OB_MESH) {
1570                                                         mergemenu();
1571                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1572                                                 }
1573                                         }
1574                                         else if((G.qual==0) || (G.qual==LR_CTRLKEY)) {
1575                                                 mirrormenu();
1576                                         }
1577                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1578                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
1579                                         }
1580                                 }
1581                                 else if(G.qual & LR_CTRLKEY) {
1582                                         mirrormenu();
1583                                 }
1584                                 else if(G.qual==0) {
1585                                         if(ob && (ob->flag & OB_POSEMODE))
1586                                                 pose_movetolayer();
1587                                         else
1588                                                 movetolayer();
1589                                 }
1590                                 break;
1591                         case NKEY:
1592                                 if((G.qual==0)) {
1593                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1594                                         allqueue(REDRAWVIEW3D, 0);
1595                                 }
1596                                 else if(G.obedit) {
1597                                         switch (G.obedit->type){
1598                                         case OB_ARMATURE:
1599                                                 if(G.qual==LR_CTRLKEY){
1600                                                         if (okee("Recalculate bone roll angles")) {
1601                                                                 auto_align_armature();
1602                                                                 allqueue(REDRAWVIEW3D, 0);
1603                                                         }
1604                                                 }
1605                                                 break;
1606                                         case OB_MESH: 
1607                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1608                                                         if(okee("Recalculate normals inside")) {
1609                                                                 righthandfaces(2);
1610                                                                 allqueue(REDRAWVIEW3D, 0);
1611                                                                 BIF_undo_push("Recalculate normals inside");
1612                                                         }
1613                                                 }
1614                                                 else if(G.qual==LR_CTRLKEY){
1615                                                         if(okee("Recalculate normals outside")) {
1616                                                                 righthandfaces(1);
1617                                                                 allqueue(REDRAWVIEW3D, 0);
1618                                                                 BIF_undo_push("Recalculate normals outside");
1619                                                         }
1620                                                 }
1621                                                 break;
1622                                         }
1623                                 }
1624                                 
1625                                 break;
1626                         case OKEY:
1627                                 if (G.obedit) {
1628                                         if (G.qual==LR_SHIFTKEY) {
1629                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%6;
1630                                                 allqueue(REDRAWHEADERS, 0);
1631                                         }
1632                                         else if((G.qual==LR_ALTKEY)) {
1633                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
1634                                                 else G.scene->proportional= 2;
1635                                                 allqueue(REDRAWHEADERS, 0);
1636                                         }
1637                                         else if((G.qual==0)) {
1638                                                 G.scene->proportional= !G.scene->proportional;
1639                                                 allqueue(REDRAWHEADERS, 0);
1640                                         }
1641                                 }
1642                                 else if((G.qual==LR_SHIFTKEY)) {
1643                                         if(ob && ob->type == OB_MESH) {
1644                                                 flip_subdivison(ob, -1);
1645                                         }
1646                                 }
1647                                 else if(G.qual==LR_ALTKEY) {
1648                                         if(okee("Clear origin")) {
1649                                                 clear_object('o');
1650                                         }
1651                                 }
1652                                 break;
1653
1654                         case PKEY:
1655                                 if(G.obedit) {
1656                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1657                                                 if(G.obedit->type==OB_ARMATURE)
1658                                                         make_bone_parent();
1659                                                 else
1660                                                         make_parent();
1661                                         }
1662                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
1663                                                 clear_bone_parent();
1664                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
1665                                                 separatemenu();
1666                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1667                                                 separate_nurb();
1668                                         else if (G.qual==LR_SHIFTKEY) {
1669                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
1670                                                 Transform();
1671                                         }
1672                                 }
1673                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
1674                                         make_parent();
1675                                 else if(G.qual==LR_SHIFTKEY) {
1676                                         if(G.obedit) {
1677                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
1678                                                 Transform();
1679                                         }
1680                                         else {
1681                                                 toggle_blockhandler(curarea, VIEW3D_HANDLER_PREVIEW, 0);
1682                                                 doredraw= 1;
1683                                         }
1684                                 }
1685                                 else if(G.qual==LR_ALTKEY)
1686                                         clear_parent();
1687                                 else if((G.qual==0)) {
1688                         start_game();
1689                                 }
1690                                 break;                          
1691                         case RKEY:
1692                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1693                                         rotate_uv_tface();
1694                                 else if(G.qual==LR_ALTKEY) {
1695                                         if(okee("Clear rotation")) {
1696                                                 clear_object('r');
1697                                         }
1698                                 } 
1699                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1700                                         v3d->twtype= V3D_MANIP_ROTATE;
1701                                         doredraw= 1;
1702                                 }
1703                                 else if (G.obedit) {
1704                                         if((G.qual==LR_SHIFTKEY)) {
1705                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
1706                                                         selectrow_nurb();
1707                                         }
1708                                         else if(G.qual==LR_CTRLKEY) {
1709                                                 if (G.obedit->type==OB_MESH)
1710                                                         CutEdgeloop(1);
1711                                                         BIF_undo_push("Cut Edgeloop");
1712                                         }
1713                                         else if((G.qual==0)) {
1714                                                 initTransform(TFM_ROTATION, CTX_NONE);
1715                                                 Transform();
1716                                         }
1717                                 }
1718                                 else if((G.qual==0)) {
1719                                         initTransform(TFM_ROTATION, CTX_NONE);
1720                                         Transform();
1721                                 }
1722                                 break;
1723                         case SKEY:
1724                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1725                                         v3d->twtype= V3D_MANIP_SCALE;
1726                                         doredraw= 1;
1727                                 }
1728                                 else if(G.obedit) {
1729                                         
1730                                         if(G.qual==LR_ALTKEY) {
1731                                                 if(G.obedit->type==OB_ARMATURE) {
1732                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1733                                                 }
1734                                                 else
1735                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
1736                                                 Transform();
1737                                         }
1738                                         else if(G.qual==LR_CTRLKEY) {
1739                                                 initTransform(TFM_SHEAR, CTX_NONE);
1740                                                 Transform();
1741                                         }
1742                                         else if(G.qual==LR_SHIFTKEY)
1743                                                 snapmenu();
1744                                         else if(G.qual==0) {
1745                                                 if(G.obedit->type==OB_ARMATURE) {
1746                                                         bArmature *arm= G.obedit->data;
1747                                                         if(arm->drawtype==ARM_ENVELOPE)
1748                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
1749                                                         else
1750                                                                 initTransform(TFM_RESIZE, CTX_NONE);
1751                                                 }
1752                                                 else
1753                                                         initTransform(TFM_RESIZE, CTX_NONE);
1754                                                 Transform();
1755                                         }
1756                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
1757                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
1758                                                 Transform();
1759                                         }
1760                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1761                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
1762                                         }
1763                                 }
1764                                 else if(G.qual==LR_ALTKEY) {
1765                                         if(G.f & G_WEIGHTPAINT)
1766                                                 ob= ob->parent;
1767                                         if(ob && (ob->flag & OB_POSEMODE)) {
1768                                                 bArmature *arm= ob->data;
1769                                                 if( ELEM(arm->drawtype, ARM_B_BONE, ARM_ENVELOPE)) {
1770                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1771                                                         Transform();
1772                                                         break;
1773                                                 }
1774                                         }
1775                                         
1776                                         if(okee("Clear size")) {
1777                                                 clear_object('s');
1778                                         }
1779                                 }
1780                                 else if(G.qual==LR_SHIFTKEY) {
1781                                         snapmenu();
1782                                 }
1783                                 else if((G.qual==0)) {
1784                                         initTransform(TFM_RESIZE, CTX_NONE);
1785                                         Transform();
1786                                 }
1787                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1788                                         initTransform(TFM_TOSPHERE, CTX_NONE);
1789                                         Transform();
1790                                 }
1791                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
1792                                         initTransform(TFM_SHEAR, CTX_NONE);
1793                                         Transform();
1794                                 }
1795                                 break;
1796                         case TKEY:
1797                                 if(G.obedit){
1798                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
1799                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
1800                                                 allqueue(REDRAWVIEW3D, 0);
1801                                                 countall();
1802                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1803                                         }
1804                                         if (G.obedit->type==OB_CURVE) {
1805                                                 if (G.qual==LR_ALTKEY) {
1806                                                         clear_tilt();
1807                                                 }
1808                                                 else if (G.qual==0) {
1809                                                         initTransform(TFM_TILT, CTX_NONE);
1810                                                         Transform();
1811                                                 }
1812                                         }
1813                                 }
1814                                 else if(G.qual==LR_CTRLKEY) {
1815                                         if(ob && (ob->flag & OB_POSEMODE));
1816                                         else make_track();
1817                                 }
1818                                 else if(G.qual==LR_ALTKEY) {
1819                                         if(ob && (ob->flag & OB_POSEMODE));
1820                                         else clear_track();
1821                                 }
1822                                 else if((G.qual==0)){
1823                                         texspace_edit();
1824                                 }
1825                                 
1826                                 break;
1827                         case UKEY:
1828                                 if(G.obedit) {
1829                                         if(G.obedit->type==OB_MESH) {
1830                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1831                                         }
1832                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
1833                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1834                                         }
1835                                 }
1836                                 else if((G.qual==0)) {
1837                                         if (G.f & G_FACESELECT)
1838                                                 uv_autocalc_tface();
1839                                         else if(G.f & G_WEIGHTPAINT)
1840                                                 wpaint_undo();
1841                                         else if(G.f & G_VERTEXPAINT)
1842                                                 vpaint_undo();
1843                                         else {
1844                                                 single_user();
1845                                         }
1846                                 }
1847                                         
1848                                 break;
1849                         case VKEY:
1850                                 if((G.qual==LR_SHIFTKEY)) {
1851                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
1852                                                 align_view_to_selected(v3d);
1853                                         }
1854                                         else if (G.f & G_FACESELECT) {
1855                                                 align_view_to_selected(v3d);
1856                                         }
1857                                 }
1858                                 else if(G.qual==LR_ALTKEY)
1859                                         image_aspect();
1860                                 else if (G.qual==0){
1861                                         if(G.obedit) {
1862                                                 if(G.obedit->type==OB_MESH) {
1863                                                         mesh_rip();
1864                                                 }
1865                                                 else if(G.obedit->type==OB_CURVE) {
1866                                                         sethandlesNurb(2);
1867                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1868                                                         allqueue(REDRAWVIEW3D, 0);
1869                                                         BIF_undo_push("Handle change");
1870                                                 }
1871                                         }
1872                                         else if(ob && ob->type == OB_MESH) 
1873                                                 set_vpaint();
1874                                 }
1875                                 break;
1876                         case WKEY:
1877                                 if((G.qual==LR_SHIFTKEY)) {
1878                                         initTransform(TFM_WARP, CTX_NONE);
1879                                         Transform();
1880                                 }
1881                                 else if(G.qual==LR_ALTKEY) {
1882                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
1883                                 }
1884                                 else if(G.qual==LR_CTRLKEY) {
1885                                         if(G.obedit) {
1886                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
1887                                                         switchdirectionNurb2();
1888                                                 }
1889                                         }
1890                                 }
1891                                 else if((G.qual==0))
1892                                         special_editmenu();
1893                                 
1894                                 break;
1895                         case XKEY:
1896                         case DELKEY:
1897                                 if(G.qual==0)
1898                                         delete_context_selected();
1899                                 break;
1900                         case YKEY:
1901                                 if((G.qual==0) && (G.obedit)) {
1902                                         if(G.obedit->type==OB_MESH) split_mesh();
1903                                 }
1904                                 break;
1905                         case ZKEY:
1906                                 toggle_shading();
1907                                 
1908                                 scrarea_queue_headredraw(curarea);
1909                                 scrarea_queue_winredraw(curarea);
1910                                 break;
1911                         
1912                         case HOMEKEY:
1913                                 if(G.qual==0)
1914                                         view3d_home(0);
1915                                 break;
1916                         case COMMAKEY:
1917                                 if(G.qual==LR_CTRLKEY) {
1918                                         G.vd->around= V3D_CENTROID;
1919                                 } else if(G.qual==LR_SHIFTKEY) {
1920                                         G.vd->around= V3D_CENTROID;
1921                                 } else if(G.qual==0) {
1922                                         G.vd->around= V3D_CENTRE;
1923                                 }
1924                                 handle_view3d_around();
1925                                 
1926                                 scrarea_queue_headredraw(curarea);
1927                                 scrarea_queue_winredraw(curarea);
1928                                 break;
1929                                 
1930                         case PERIODKEY:
1931                                 if(G.qual==LR_CTRLKEY) {
1932                                         G.vd->around= V3D_LOCAL;
1933                                 }       else if(G.qual==0) {
1934                                         G.vd->around= V3D_CURSOR;
1935                                 }
1936                                 handle_view3d_around();
1937                                 
1938                                 scrarea_queue_headredraw(curarea);
1939                                 scrarea_queue_winredraw(curarea);
1940                                 break;
1941                         
1942                         case PADSLASHKEY:
1943                                 if(G.qual==0) {
1944                                         if(G.vd->localview) {
1945                                                 G.vd->localview= 0;
1946                                                 endlocalview(curarea);
1947                                         }
1948                                         else {
1949                                                 G.vd->localview= 1;
1950                                                 initlocalview();
1951                                         }
1952                                         scrarea_queue_headredraw(curarea);
1953                                 }
1954                                 break;
1955                         case PADASTERKEY:       /* '*' */
1956                                 if(G.qual==0) {
1957                                         if(ob) {
1958                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
1959                                                         editmesh_align_view_to_selected(G.vd, 2);
1960                                                 } 
1961                                                 else if (G.f & G_FACESELECT) {
1962                                                         if(ob->type==OB_MESH) {
1963                                                                 Mesh *me= ob->data;
1964                                                                 faceselect_align_view_to_selected(G.vd, me, 2);
1965                                                         }
1966                                                 }
1967                                                 else
1968                                                         obmat_to_viewmat(ob);
1969                                                 
1970                                                 if(G.vd->persp==2) G.vd->persp= 1;
1971                                                 scrarea_queue_winredraw(curarea);
1972                                         }
1973                                 }
1974                                 break;
1975                         case PADPERIOD: /* '.' */
1976                                 if(G.qual==0)
1977                                         centreview();
1978                                 break;
1979                         
1980                         case PAGEUPKEY:
1981                                 if(G.qual==LR_CTRLKEY)
1982                                         movekey_obipo(1);
1983                                 else if((G.qual==0))
1984                                         nextkey_obipo(1);       /* in editipo.c */
1985                                 break;
1986
1987                         case PAGEDOWNKEY:
1988                                 if(G.qual==LR_CTRLKEY)
1989                                         movekey_obipo(-1);
1990                                 else if((G.qual==0))
1991                                         nextkey_obipo(-1);
1992                                 break;
1993                                 
1994                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1995                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1996                         case PADENTER:
1997                                 persptoetsen(event);
1998                                 doredraw= 1;
1999                                 break;
2000                         case PADMINUS:
2001                                 if ( (G.qual==LR_CTRLKEY)
2002                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2003                                         select_less();
2004                                 else {
2005                                         persptoetsen(event);
2006                                         doredraw= 1;
2007                                 }
2008                                 break;
2009
2010                         case PADPLUSKEY:
2011                                 if ( (G.qual==LR_CTRLKEY)
2012                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2013                                         select_more();
2014                                 else {
2015                                         persptoetsen(event);
2016                                         doredraw= 1;
2017                                 }
2018                                 break;
2019
2020                         case ESCKEY:
2021                                 if(G.qual==0) {
2022                                         if (G.vd->flag & V3D_DISPIMAGE) {
2023                                                 G.vd->flag &= ~V3D_DISPIMAGE;
2024                                                 doredraw= 1;
2025                                         }
2026                                 }
2027                                 break;
2028                         }
2029                 }
2030         }
2031         
2032         if(doredraw) {
2033                 scrarea_queue_winredraw(curarea);
2034                 scrarea_queue_headredraw(curarea);
2035         }
2036 }
2037
2038 static void initview3d(ScrArea *sa)
2039 {
2040         View3D *vd;
2041         
2042         vd= MEM_callocN(sizeof(View3D), "initview3d");
2043         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
2044
2045         vd->spacetype= SPACE_VIEW3D;
2046         vd->blockscale= 0.7f;
2047         vd->viewquat[0]= 1.0f;
2048         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
2049         vd->persp= 1;
2050         vd->drawtype= OB_WIRE;
2051         vd->view= 7;
2052         vd->dist= 10.0;
2053         vd->lens= 35.0f;
2054         vd->near= 0.01f;
2055         vd->far= 500.0f;
2056         vd->grid= 1.0f;
2057         vd->gridlines= 16;
2058         vd->lay= vd->layact= 1;
2059         if(G.scene) {
2060                 vd->lay= vd->layact= G.scene->lay;
2061                 vd->camera= G.scene->camera;
2062         }
2063         vd->scenelock= 1;
2064         vd->gridflag |= V3D_SHOW_X;
2065         vd->gridflag |= V3D_SHOW_Y;
2066         vd->gridflag |= V3D_SHOW_FLOOR;
2067         vd->gridflag &= ~V3D_SHOW_Z;
2068 }
2069
2070
2071 /* ******************** SPACE: IPO ********************** */
2072
2073 static void changeview2dspace(ScrArea *sa, void *spacedata)
2074 {
2075         if(G.v2d==0) return;
2076
2077         test_view2d(G.v2d, curarea->winx, curarea->winy);
2078         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
2079 }
2080
2081 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2082 {
2083         extern void do_ipobuts(unsigned short event);   // drawipo.c
2084         unsigned short event= evt->event;
2085         short val= evt->val;
2086         SpaceIpo *sipo= curarea->spacedata.first;
2087         View2D *v2d= &sipo->v2d;
2088         float dx, dy;
2089         int cfra, doredraw= 0;
2090         short mval[2];
2091         short mousebut = L_MOUSE;
2092         
2093         if(sa->win==0) return;
2094
2095         if(val) {
2096                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
2097
2098                 /* swap mouse buttons based on user preference */
2099                 if (U.flag & USER_LMOUSESELECT) {
2100                         if (event == LEFTMOUSE) {
2101                                 event = RIGHTMOUSE;
2102                                 mousebut = L_MOUSE;
2103                         } else if (event == RIGHTMOUSE) {
2104                                 event = LEFTMOUSE;
2105                                 mousebut = R_MOUSE;
2106                         }
2107                 }
2108
2109                 switch(event) {
2110                 case UI_BUT_EVENT:
2111                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
2112                         if(val>0 && val < 256) do_ipowin_buts(val-1);
2113                         else do_ipobuts(val);
2114                         break;
2115                         
2116                 case LEFTMOUSE:
2117                         if( in_ipo_buttons() ) {
2118                                 do_ipo_selectbuttons();
2119                                 doredraw= 1;
2120                         }
2121                         else if(view2dmove(LEFTMOUSE)); // only checks for sliders
2122                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
2123                         else {
2124                                 do {
2125                                         getmouseco_areawin(mval);
2126                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
2127                                         
2128                                         cfra= (int)dx;
2129                                         if(cfra< 1) cfra= 1;
2130                                         
2131                                         if( cfra!=CFRA ) {
2132                                                 CFRA= cfra;
2133                                                 update_for_newframe_nodraw(1);  /* 1 = nosound */
2134                                                 force_draw_all(0); /* To make constraint sliders redraw */
2135                                         }
2136                                         else PIL_sleep_ms(30);
2137                                 
2138                                 } while(get_mbut() & mousebut);
2139                         }
2140                         break;
2141                 case RIGHTMOUSE:
2142                         mouse_select_ipo();
2143                         allqueue (REDRAWACTION, 0);
2144                         allqueue(REDRAWNLA, 0);
2145                         break;
2146                 case MIDDLEMOUSE:
2147                         if(in_ipo_buttons()) {
2148                                 scroll_ipobuts();
2149                         }
2150                         else view2dmove(event); /* in drawipo.c */
2151                         break;
2152                 case WHEELUPMOUSE:
2153                 case WHEELDOWNMOUSE:
2154                         view2dmove(event);      /* in drawipo.c */
2155                         break;
2156                 case PADPLUSKEY:
2157                         view2d_zoom(v2d, 0.1154f, sa->winx, sa->winy);
2158                         doredraw= 1;
2159                         break;
2160                 case PADMINUS:
2161                         view2d_zoom(v2d, -0.15f, sa->winx, sa->winy);
2162                         doredraw= 1;
2163                         break;
2164                 case PAGEUPKEY:
2165                         if(G.qual==LR_CTRLKEY)
2166                                 movekey_ipo(1);
2167                         else if((G.qual==0))
2168                                 nextkey_ipo(1);
2169                         break;
2170                 case PAGEDOWNKEY:
2171                         if(G.qual==LR_CTRLKEY)
2172                                 movekey_ipo(-1);
2173                         else if((G.qual==0))
2174                                 nextkey_ipo(-1);
2175                         break;
2176                 case HOMEKEY:
2177                         if((G.qual==0))
2178                                 do_ipo_buttons(B_IPOHOME);
2179                         break;
2180                         
2181                 case AKEY:
2182                         if((G.qual==0)) {
2183                                 if(in_ipo_buttons()) {
2184                                         swap_visible_editipo();
2185                                 }
2186                                 else {
2187                                         swap_selectall_editipo();
2188                                 }
2189                                 allspace (REMAKEIPO, 0);
2190                                 allqueue (REDRAWNLA, 0);
2191                                 allqueue (REDRAWACTION, 0);
2192                         }
2193                         break;
2194                 case BKEY:
2195                         if((G.qual==0))
2196                                 borderselect_ipo();
2197                         break;
2198                 case CKEY:
2199                         if((G.qual==0))
2200                                 move_to_frame();
2201                         break;
2202                 case DKEY:
2203                         if((G.qual==LR_SHIFTKEY))
2204                                 add_duplicate_editipo();
2205                         break;
2206                 case GKEY:
2207                         if((G.qual==0))
2208                                 transform_ipo('g');
2209                         break;
2210                 case HKEY:
2211                         if(G.qual==LR_ALTKEY)
2212                                 sethandles_ipo(HD_AUTO_ANIM);
2213                         if(G.qual==LR_SHIFTKEY)
2214                                 sethandles_ipo(HD_AUTO);
2215                         else if(G.qual==0)
2216                                 sethandles_ipo(HD_ALIGN);
2217                         break;
2218                 case JKEY:
2219                         if((G.qual==0))
2220                                 join_ipo_menu();
2221                         break;
2222                 case KKEY:
2223                         if((G.qual==0)) {
2224                                 ipo_toggle_showkey();
2225                                 scrarea_queue_headredraw(curarea);
2226                                 allqueue(REDRAWVIEW3D, 0);
2227                                 doredraw= 1;
2228                         }
2229                         break;
2230                 case NKEY:
2231                         toggle_blockhandler(sa, IPO_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2232                         doredraw= 1;
2233                         break;
2234                 case RKEY:
2235                         if((G.qual==0))
2236                                 ipo_record();
2237                         break;
2238                 case SKEY:
2239                         if((G.qual==LR_SHIFTKEY)) {             
2240                                 ipo_snap_menu();
2241                         } else if((G.qual==0))
2242                                 transform_ipo('s');
2243                         break;
2244                 case TKEY:
2245                         if((G.qual==0))
2246                                 set_ipotype();
2247                         break;
2248                 case VKEY:
2249                         if((G.qual==0))
2250                                 sethandles_ipo(HD_VECT);
2251                         break;
2252                 case XKEY:
2253                 case DELKEY:
2254                         del_ipo();
2255                         break;
2256                 }
2257         }
2258
2259         if(doredraw) scrarea_queue_winredraw(sa);
2260 }
2261
2262 void initipo(ScrArea *sa)
2263 {
2264         SpaceIpo *sipo;
2265         
2266         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
2267         BLI_addhead(&sa->spacedata, sipo);
2268
2269         sipo->spacetype= SPACE_IPO;
2270         sipo->blockscale= 0.7f;
2271         
2272         /* sipo space loopt van (0,-?) tot (??,?) */
2273         sipo->v2d.tot.xmin= 0.0;
2274         sipo->v2d.tot.ymin= -10.0;
2275         sipo->v2d.tot.xmax= G.scene->r.efra;
2276         sipo->v2d.tot.ymax= 10.0;
2277
2278         sipo->v2d.cur= sipo->v2d.tot;
2279
2280         sipo->v2d.min[0]= 0.01f;
2281         sipo->v2d.min[1]= 0.01f;
2282
2283         sipo->v2d.max[0]= 15000.0f;
2284         sipo->v2d.max[1]= 10000.0f;
2285         
2286         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
2287         sipo->v2d.keeptot= 0;
2288
2289         sipo->blocktype= ID_OB;
2290 }
2291
2292 /* ******************** SPACE: INFO ********************** */
2293
2294 void space_mipmap_button_function(int event) {
2295         set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
2296
2297         allqueue(REDRAWVIEW3D, 0);
2298 }
2299
2300 #if 0
2301 static void space_sound_button_function(int event)
2302 {
2303         int a;
2304         SYS_SystemHandle syshandle;
2305
2306         if ((syshandle = SYS_GetSystem()))
2307         {
2308                 a = (U.gameflags & USER_DISABLE_SOUND);
2309                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
2310         }
2311 }
2312 #endif
2313
2314 // needed for event; choose new 'curmain' resets it...
2315 static short th_curcol= TH_BACK;
2316 static char *th_curcol_ptr= NULL;
2317 static char th_curcol_arr[4]={0, 0, 0, 255};
2318
2319 static void info_user_themebuts(uiBlock *block, short y1, short y2, short y3)
2320 {
2321         bTheme *btheme, *bt;
2322         int spacetype= 0;
2323         static short cur=1, curmain=2;
2324         short a, tot=0, isbuiltin= 0;
2325         char string[21*32], *strp, *col;
2326         
2327         y3= y2+23;      // exception!
2328         
2329         /* count total, max 16! */
2330         for(bt= U.themes.first; bt; bt= bt->next) tot++;
2331         
2332         /* if cur not is 1; move that to front of list */
2333         if(cur!=1) {
2334                 a= 1;
2335                 for(bt= U.themes.first; bt; bt= bt->next, a++) {
2336                         if(a==cur) {
2337                                 BLI_remlink(&U.themes, bt);
2338                                 BLI_addhead(&U.themes, bt);
2339                                 allqueue(REDRAWALL, 0);
2340                                 cur= 1;
2341                                 break;
2342                         }
2343                 }
2344         }
2345         
2346         /* the current theme */
2347         btheme= U.themes.first;
2348         if(strcmp(btheme->name, "Default")==0) isbuiltin= 1;
2349
2350         /* construct popup script */
2351         string[0]= 0;
2352         for(bt= U.themes.first; bt; bt= bt->next) {
2353                 strcat(string, bt->name);
2354                 if(btheme->next) strcat(string, "   |");
2355         }
2356         uiDefButS(block, MENU, B_UPDATE_THEME, string,                  45,y3,200,20, &cur, 0, 0, 0, 0, "Current theme");
2357         
2358         /* add / delete / name */
2359
2360         if(tot<16)
2361                 uiDefBut(block, BUT, B_ADD_THEME, "Add",        45,y2,200,20, NULL, 0, 0, 0, 0, "Makes new copy of this theme");
2362         if(tot>1 && isbuiltin==0)
2363                 uiDefBut(block, BUT, B_DEL_THEME, "Delete", 45,y1,200,20, NULL, 0, 0, 0, 0, "Delete theme");
2364
2365         if(isbuiltin) return;
2366         
2367         /* name */
2368         uiDefBut(block, TEX, B_NAME_THEME, "",                  255,y3,200,20, btheme->name, 1.0, 30.0, 0, 0, "Rename theme");
2369
2370         /* main choices pup */
2371         uiDefButS(block, MENU, B_CHANGE_THEME, "UI and Buttons %x1|%l|3D View %x2|%l|Ipo Curve Editor %x3|Action Editor %x4|"
2372                 "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|"
2373                 "Outliner %x11|Buttons Window %x12|%l|File Browser %x13|Image Browser %x14",
2374                                                                                                         255,y2,200,20, &curmain, 0, 0, 0, 0, "Specify theme for...");
2375         if(curmain==1) spacetype= 0;
2376         else if(curmain==2) spacetype= SPACE_VIEW3D;
2377         else if(curmain==3) spacetype= SPACE_IPO;
2378         else if(curmain==4) spacetype= SPACE_ACTION;
2379         else if(curmain==5) spacetype= SPACE_NLA;
2380         else if(curmain==6) spacetype= SPACE_IMAGE;
2381         else if(curmain==7) spacetype= SPACE_SEQ;
2382         else if(curmain==8) spacetype= SPACE_SOUND;
2383         else if(curmain==9) spacetype= SPACE_TEXT;
2384         else if(curmain==10) spacetype= SPACE_INFO;
2385         else if(curmain==11) spacetype= SPACE_OOPS;
2386         else if(curmain==12) spacetype= SPACE_BUTS;
2387         else if(curmain==13) spacetype= SPACE_FILE;
2388         else if(curmain==14) spacetype= SPACE_IMASEL;
2389         else if(curmain==15) spacetype= SPACE_TIME;
2390         else if(curmain==16) spacetype= SPACE_NODE;
2391         else return; // only needed while coding... when adding themes for more windows
2392         
2393         /* color choices pup */
2394         if(curmain==1) {
2395                 strp= BIF_ThemeColorsPup(0);
2396                 if(th_curcol==TH_BACK) th_curcol= TH_BUT_OUTLINE;  // switching main choices...
2397         }
2398         else strp= BIF_ThemeColorsPup(spacetype);
2399         
2400         uiDefButS(block, MENU, B_REDR, strp,                    255,y1,200,20, &th_curcol, 0, 0, 0, 0, "Current color");
2401         MEM_freeN(strp);
2402         
2403         th_curcol_ptr= col= BIF_ThemeGetColorPtr(btheme, spacetype, th_curcol);
2404         if(col==NULL) return;
2405         
2406         /* first handle exceptions, special single values, row selection, etc */
2407         if(th_curcol==TH_VERTEX_SIZE) {
2408                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Vertex size ", 465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2409         }
2410         else if(th_curcol==TH_FACEDOT_SIZE) {
2411                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Face dot size ",       465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2412         }
2413         else if(th_curcol==TH_BUT_DRAWTYPE) {
2414                 uiBlockBeginAlign(block);
2415                 uiDefButC(block, ROW, B_UPDATE_THEME, "Minimal",        465,y3,100,20,  col, 2.0, 0.0, 0, 0, "");
2416                 uiDefButC(block, ROW, B_UPDATE_THEME, "Shaded", 565,y3,100,20,  col, 2.0, 1.0, 0, 0, "");
2417                 uiDefButC(block, ROW, B_UPDATE_THEME, "Rounded",        465,y2,100,20,  col, 2.0, 2.0, 0, 0, "");
2418                 uiDefButC(block, ROW, B_UPDATE_THEME, "OldSkool",       565,y2,100,20,  col, 2.0, 3.0, 0, 0, "");
2419                 uiBlockEndAlign(block);
2420         }
2421         else {
2422                 uiBlockBeginAlign(block);
2423                 if ELEM7(th_curcol, TH_PANEL, TH_LAMP, TH_FACE, TH_FACE_SELECT, TH_MENU_BACK, TH_MENU_HILITE, TH_MENU_ITEM) {
2424                         uiDefButC(block, NUMSLI, B_UPDATE_THEME,"A ",   465,y3+25,200,20,  col+3, 0.0, 255.0, B_THEMECOL, 0, "");
2425                 }
2426                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"R ",   465,y3,200,20,  col, 0.0, 255.0, B_THEMECOL, 0, "");
2427                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"G ",   465,y2,200,20,  col+1, 0.0, 255.0, B_THEMECOL, 0, "");
2428                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"B ",   465,y1,200,20,  col+2, 0.0, 255.0, B_THEMECOL, 0, "");
2429                 uiBlockEndAlign(block);
2430
2431                 uiDefButC(block, COL, B_UPDATE_THEME, "",               675,y1,50,y3-y1+20, col, 0, 0, 0, 0, "");
2432                                 
2433                 /* copy paste */
2434                 uiBlockBeginAlign(block);
2435                 uiDefBut(block, BUT, B_THEME_COPY, "Copy Color",        755,y2,120,20, NULL, 0, 0, 0, 0, "Stores current color in buffer");
2436                 uiDefBut(block, BUT, B_THEME_PASTE, "Paste Color",      755,y1,120,20, NULL, 0, 0, 0, 0, "Pastes buffer color");
2437                 uiBlockEndAlign(block);
2438                 
2439                 uiDefButC(block, COL, 0, "",                            885,y1,50,y2-y1+20, th_curcol_arr, 0, 0, 0, 0, "");
2440                 
2441         }
2442 }
2443
2444
2445 void drawinfospace(ScrArea *sa, void *spacedata)
2446 {
2447         uiBlock *block;
2448         static short cur_light=0, cur_light_var=0;
2449         float fac, col[3];
2450         short xpos, ypos, ypostab,  buth, rspace, dx, y1, y2, y3, y4, y5, y6;
2451         short y2label, y3label, y4label, y5label, y6label;
2452         short spref, mpref, lpref, smfileselbut;
2453         short edgsp, midsp;
2454         char naam[32];
2455
2456         if(curarea->win==0 || curarea->winy<2) return;
2457
2458         BIF_GetThemeColor3fv(TH_BACK, col);
2459         glClearColor(col[0], col[1], col[2], 0.0);
2460         glClear(GL_COLOR_BUFFER_BIT);
2461
2462         if(curarea->winx<=1280.0) {
2463                 fac= ((float)curarea->winx)/1280.0f;
2464                 myortho2(0.375f, 1280.375f, 0.375f, curarea->winy/fac + 0.375f);
2465         }
2466         else {
2467                 myortho2(0.375f, (float)curarea->winx + 0.375f, 0.375f, (float)curarea->winy + 0.375f);
2468         }
2469         
2470         sprintf(naam, "infowin %d", curarea->win);
2471         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->win);
2472
2473
2474         /* Vars for nice grid alignment */ 
2475         dx= (1280-90)/7;        /* spacing for use in equally dividing 'tab' row */
2476
2477         xpos = 45;              /* left padding */
2478         ypos = 50;              /* bottom padding for buttons */
2479         ypostab = 10;           /* bottom padding for 'tab' row */
2480
2481         buth = 20;              /* standard button height */
2482
2483         spref = 90;     /* standard size for small preferences button */
2484         mpref = 189;    /* standard size for medium preferences button */
2485         lpref = 288;    /* standard size for large preferences button */
2486         smfileselbut = buth;    /* standard size for fileselect button (square) */
2487
2488         edgsp = 3;              /* space from edge of end 'tab' to edge of end button */
2489         midsp = 9;              /* horizontal space between buttons */
2490
2491         rspace = 3;             /* default space between rows */
2492
2493         y1 = ypos;              /* grid alignment for each row of buttons */
2494         y2 = ypos+buth+rspace;
2495         y3 = ypos+2*(buth+rspace);
2496         y4 = ypos+3*(buth+rspace);
2497         y5 = ypos+4*(buth+rspace);
2498         y6 = ypos+5*(buth+rspace);
2499
2500
2501         y2label = y2-2;         /* adjustments to offset the labels down to align better */
2502         y3label = y3-2;
2503         y4label = y4-2;
2504         y5label = y5-2;
2505         y6label = y6-2;
2506
2507
2508         /* set the colour to blue and draw the main 'tab' controls */
2509
2510         uiBlockSetCol(block, TH_BUT_SETTING1);
2511         uiBlockBeginAlign(block);
2512         
2513         uiDefButS(block, ROW,B_USERPREF,"View & Controls",
2514                 xpos,ypostab,(short)dx,buth,
2515                 &U.userpref,1.0,0.0, 0, 0,"");
2516                 
2517         uiDefButS(block, ROW,B_USERPREF,"Edit Methods",
2518                 (short)(xpos+dx),ypostab,(short)dx,buth,
2519                 &U.userpref,1.0,1.0, 0, 0,"");
2520
2521         uiDefButS(block, ROW,B_USERPREF,"Language & Font",
2522                 (short)(xpos+2*dx),ypostab,(short)dx,buth,
2523                 &U.userpref,1.0,2.0, 0, 0,"");
2524
2525         uiDefButS(block, ROW,B_USERPREF,"Themes",
2526                 (short)(xpos+3*dx),ypostab,(short)dx,buth,
2527                 &U.userpref,1.0,6.0, 0, 0,"");
2528
2529         uiDefButS(block, ROW,B_USERPREF,"Auto Save",
2530                 (short)(xpos+4*dx),ypostab,(short)dx,buth,
2531                 &U.userpref,1.0,3.0, 0, 0,"");
2532
2533         uiDefButS(block, ROW,B_USERPREF,"System & OpenGL",
2534                 (short)(xpos+5*dx),ypostab,(short)dx,buth,
2535                 &U.userpref,1.0,4.0, 0, 0,"");
2536                 
2537         uiDefButS(block, ROW,B_USERPREF,"File Paths",
2538                 (short)(xpos+6*dx),ypostab,(short)dx,buth,
2539                 &U.userpref,1.0,5.0, 0, 0,"");
2540
2541         uiBlockSetCol(block, TH_AUTO);
2542         uiBlockEndAlign(block);
2543         /* end 'tab' controls */
2544
2545         /* line 2: left x co-ord, top y co-ord, width, height */
2546
2547         if(U.userpref == 6) {
2548                 info_user_themebuts(block, y1, y2, y3);
2549         }
2550         else if (U.userpref == 0) { /* view & controls */
2551
2552                 uiDefBut(block, LABEL,0,"Display:",
2553                         xpos,y6label,spref,buth,
2554                         0, 0, 0, 0, 0, "");     
2555                 uiBlockBeginAlign(block);
2556                 uiDefButBitI(block, TOG, USER_TOOLTIPS, 0, "Tool Tips",
2557                         (xpos+edgsp),y5,spref,buth,
2558                         &(U.flag), 0, 0, 0, 0,
2559                         "Display tooltips (help tags) over buttons");
2560                 uiDefButBitI(block, TOG, USER_DRAWVIEWINFO, B_DRAWINFO, "Object Info",
2561                         (xpos+edgsp),y4,spref,buth,
2562                         &(U.uiflag), 0, 0, 0, 0,
2563                         "Display active object name and frame number in the 3D View");
2564                 uiDefButBitI(block, TOG, USER_SCENEGLOBAL, 0, "Global Scene",
2565                         (xpos+edgsp),y3,spref,buth,
2566                         &(U.flag), 0, 0, 0, 0,
2567                         "Forces the current Scene to be displayed in all Screens");
2568 #ifndef __APPLE__       
2569                 uiDefButBitS(block, TOG, 1, 0, "Large Cursors",
2570                         (xpos+edgsp),y2,spref,buth,
2571                         &(U.curssize), 0, 0, 0, 0,
2572                         "Use large mouse cursors when available");
2573 #else 
2574                 U.curssize=0; /*Small Cursor always for OS X for now */
2575 #endif
2576                 uiDefButBitI(block, TOG, USER_SHOW_VIEWPORTNAME, B_DRAWINFO, "View Name",
2577                         (xpos+edgsp),y1,spref,buth,
2578                         &(U.uiflag), 0, 0, 0, 0,
2579                         "Show the name of the view's direction in each 3D View");
2580                 uiBlockEndAlign(block);
2581
2582                 uiDefBut(block, LABEL,0,"Menus:",
2583                         (xpos+(2*edgsp)+spref),y6label,spref,buth,
2584                         0, 0, 0, 0, 0, "");
2585                 uiBlockBeginAlign(block);
2586                 uiDefButBitI(block, TOG, USER_MENUOPENAUTO, 0, "Open on Mouse Over",
2587                         (xpos+edgsp+spref+midsp),y5,mpref,buth,
2588                         &(U.uiflag), 0, 0, 0, 0,
2589                         "Open menu buttons and pulldowns automatically when the mouse is hovering");
2590                 uiDefButS(block, NUM, 0, "Top Level:",
2591                         (xpos+edgsp+spref+midsp),y4,spref+edgsp,buth,
2592                         &(U.menuthreshold1), 1, 40, 0, 0,
2593                         "Time delay in 1/10 seconds before automatically opening top level menus");
2594                 uiDefButS(block, NUM, 0, "Sublevels:",
2595                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y4,spref+edgsp,buth,
2596                         &(U.menuthreshold2), 1, 40, 0, 0,
2597                         "Time delay in 1/10 seconds before automatically opening menu sublevels");
2598                 uiBlockEndAlign(block);
2599
2600                 uiDefBut(block, LABEL,0,"Toolbox click-hold delay:",
2601                         (xpos+(2*edgsp)+spref),y3label,mpref,buth,
2602                         0, 0, 0, 0, 0, "");
2603                 uiBlockBeginAlign(block);
2604                 uiDefButS(block, NUM, 0, "LMB:",
2605                         (xpos+edgsp+spref+midsp),y2,spref+edgsp,buth,
2606                         &(U.tb_leftmouse), 2, 40, 0, 0,
2607                         "Time in 1/10 seconds to hold the Left Mouse Button before opening the toolbox");
2608                 uiDefButS(block, NUM, 0, "RMB:",
2609                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y2,spref+edgsp,buth,
2610                         &(U.tb_rightmouse), 2, 40, 0, 0,
2611                         "Time in 1/10 seconds to hold the Right Mouse Button before opening the toolbox");      
2612                 uiBlockEndAlign(block);
2613
2614                 uiDefButBitI(block, TOG, USER_PANELPINNED, 0, "Pin Floating Panels",
2615                         (xpos+edgsp+spref+midsp),y1,(mpref/2),buth,
2616                         &(U.uiflag), 0, 0, 0, 0,
2617                         "Make floating panels invoked by a hotkey (eg. N Key) open at the previous location");
2618                 uiDefButBitI(block, TOG, USER_PLAINMENUS, B_PLAINMENUS, "Plain Menus",
2619                         (xpos+edgsp+(2*spref)+(2*midsp)),y1,spref,buth,
2620                         &(U.uiflag), 0, 0, 0, 0,
2621                         "Use a column layout for toolbox and do not flip the contents of any menu");
2622                 uiBlockEndAlign(block);
2623                 
2624                 uiDefBut(block, LABEL,0,"Snap to grid:",
2625                         (xpos+(2*edgsp)+spref+midsp+mpref),y6label,mpref,buth,
2626                         0, 0, 0, 0, 0, "");
2627                 uiBlockBeginAlign(block);
2628                 uiDefButBitI(block, TOG, USER_AUTOGRABGRID, 0, "Grab/Move",
2629                         (xpos+edgsp+mpref+spref+(2*midsp)),y5,spref,buth,
2630                         &(U.flag), 0, 0, 0, 0,
2631                         "Snap objects and sub-objects to grid units when moving");
2632                 uiDefButBitI(block, TOG, USER_AUTOROTGRID, 0, "Rotate",
2633                         (xpos+edgsp+mpref+spref+(2*midsp)),y4,spref,buth,
2634                         &(U.flag), 0, 0, 0, 0,
2635                         "Snap objects and sub-objects to grid units when rotating");
2636                 uiDefButBitI(block, TOG, USER_AUTOSIZEGRID, 0, "Scale",
2637                         (xpos+edgsp+mpref+spref+(2*midsp)),y3,spref,buth,
2638                         &(U.flag), 0, 0, 0, 0,
2639                         "Snap objects and sub-objects to grid units when scaling");
2640                 uiBlockEndAlign(block);
2641                 
2642                 uiDefButBitI(block, TOG, USER_LOCKAROUND, B_DRAWINFO, "Global Pivot",
2643                         (xpos+edgsp+mpref+spref+(2*midsp)),y1,spref,buth,
2644                         &(U.uiflag), 0, 0, 0, 0,
2645                         "Lock the same rotation/scaling pivot in all 3D Views");        
2646                 
2647                 uiDefBut(block, LABEL,0,"View zoom:",
2648                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y6label,mpref,buth,
2649                         0, 0, 0, 0, 0, "");
2650                 uiBlockBeginAlign(block);
2651                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2652                 uiDefButS(block, ROW, 0, "Continue",
2653                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y5,(mpref/3),buth,
2654                         &(U.viewzoom), 40, USER_ZOOM_CONT, 0, 0,
2655                         "Old style zoom, continues while moving mouse up or down");
2656                 uiDefButS(block, ROW, 0, "Dolly",
2657                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/3)),y5,(mpref/3),buth,
2658                         &(U.viewzoom), 40, USER_ZOOM_DOLLY, 0, 0,
2659                         "Zooms in and out based on vertical mouse movement.");
2660                 uiDefButS(block, ROW, 0, "Scale",
2661                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(2*mpref/3)),y5,(mpref/3),buth,
2662                         &(U.viewzoom), 40, USER_ZOOM_SCALE, 0, 0,
2663                         "Zooms in and out like scaling the view, mouse movements relative to center.");
2664                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2665                 uiBlockEndAlign(block);
2666                 
2667                 uiDefBut(block, LABEL,0,"View rotation:",
2668                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y4label,mpref,buth,
2669                         0, 0, 0, 0, 0, "");
2670                 uiBlockBeginAlign(block);
2671                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2672                 uiDefButBitI(block, TOG, USER_TRACKBALL, B_DRAWINFO, "Trackball",
2673                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y3,(mpref/2),buth,
2674                         &(U.flag), 0, 0, 0, 0,
2675                         "Allow the view to tumble freely when orbiting with the Middle Mouse Button");
2676                 uiDefButBitI(block, TOGN, USER_TRACKBALL, B_DRAWINFO, "Turntable",
2677                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y3,(mpref/2),buth,
2678                         &(U.flag), 0, 0, 0, 0,
2679                         "Use fixed up axis for orbiting with Middle Mouse Button");
2680                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2681                 uiDefButBitI(block, TOG, USER_AUTOPERSP, B_DRAWINFO, "Auto Perspective",
2682                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y2,(mpref/2),buth,
2683                         &(U.uiflag), 0, 0, 0, 0,
2684                         "Automatically switch between orthographic and perspective when changing from top/front/side views");
2685                 uiDefButBitI(block, TOG, USER_ORBIT_SELECTION, B_DRAWINFO, "Around Active",
2686                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y2,(mpref/2),buth,
2687                         &(U.uiflag), 0, 0, 0, 0,
2688                         "Keep the active object in place when orbiting the views (Object Mode)");
2689                 uiBlockEndAlign(block);
2690                 
2691
2692                 uiBlockBeginAlign(block);
2693                 uiDefButBitI(block, TOG, USER_SHOW_ROTVIEWICON, B_DRAWINFO, "Mini Axis",
2694                          (xpos+edgsp+(2*mpref)+(2*midsp)),y1,(mpref/3),buth,
2695                          &(U.uiflag), 0, 0, 0, 0,
2696                          "Show a small rotating 3D axis in the bottom left corner of the 3D View");
2697                 uiDefButS(block, NUM, B_DRAWINFO, "Size:",
2698                         (xpos+edgsp+(2*mpref)+(2*midsp)+(mpref/3)),y1,(mpref/3),buth,
2699                         &U.rvisize, 10, 64, 0, 0,
2700                         "The axis icon's size");
2701                 uiDefButS(block, NUM, B_DRAWINFO, "Bright:",
2702                         (xpos+edgsp+(2*mpref)+(2*midsp)+2*(mpref/3)),y1,(mpref/3),buth,
2703                         &U.rvibright, 0, 10, 0, 0,
2704                         "The brightness of the icon");
2705                 uiBlockEndAlign(block);
2706                 
2707
2708                 uiDefBut(block, LABEL,0,"Select with:",
2709                         (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y6label,mpref,buth,
2710                         0, 0, 0, 0, 0, "");
2711                 uiBlockBeginAlign(block);
2712                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2713                 uiDefButBitI(block, TOG, USER_LMOUSESELECT, B_DRAWINFO, "Left Mouse",
2714                         (xpos+edgsp+(3*mpref)+(4*midsp)),y5,(mpref/2),buth,
2715                         &(U.flag), 0, 0, 0, 0, "Use the Left Mouse Button for selection");
2716                 uiDefButBitI(block, TOGN, USER_LMOUSESELECT, B_DRAWINFO, "Right Mouse",
2717                         (xpos+edgsp+(3*mpref)+(4*midsp)+(mpref/2)),y5,(mpref/2),buth,
2718                         &(U.flag), 0, 0, 0, 0, "Use the Right Mouse Button for selection");
2719                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2720                 uiBlockEndAlign(block);
2721                 
2722                 
2723                 if(U.flag & USER_LMOUSESELECT) {
2724                         uiDefBut(block, LABEL,0,"Cursor with: Right Mouse",
2725                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2726                                 0, 0, 0, 0, 0, "");
2727                 } else {
2728                         uiDefBut(block, LABEL,0,"Cursor with: Left Mouse",
2729                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2730                                 0, 0, 0, 0, 0, "");
2731                 }
2732                 
2733                 /* illegal combo... */
2734                 if (U.flag & USER_LMOUSESELECT) 
2735                         U.flag &= ~USER_TWOBUTTONMOUSE;
2736                 
2737                 uiDefButBitI(block, TOG, USER_TWOBUTTONMOUSE, B_DRAWINFO, "Emulate 3 Button Mouse",
2738                         (xpos+edgsp+(3*mpref)+(4*midsp)),y3,mpref,buth,
2739                         &(U.flag), 0, 0, 0, 0,
2740                         "Emulates Middle Mouse with Alt+LeftMouse (doesnt work with Left Mouse Select option)");
2741                 
2742                         
2743                 uiDefBut(block, LABEL,0,"Middle Mouse Button:",
2744                         (xpos+(2*edgsp)+(4*mpref)+(4*midsp)),y6label,mpref,buth,