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