48ffb96018909c8fe18fb83b8dd6456c8f06391c
[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->tface) {
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_WEIGHTPAINT)
801                         wpaint_undo();
802                 else if(G.f & G_VERTEXPAINT)
803                         vpaint_undo();
804                 else if(G.f & G_TEXTUREPAINT)
805                         imagepaint_undo();
806                 else if(G.f & G_SCULPTMODE)
807                         sculptmode_undo();
808                 else if(curarea->spacetype==SPACE_IMAGE && (G.sima->flag & SI_DRAWTOOL))
809                         imagepaint_undo();
810                 else {
811                         /* now also in faceselect mode */
812                         if(U.uiflag & USER_GLOBALUNDO) {
813                                 if(G.f & G_SCULPTMODE)
814                                         set_sculpt_object(NULL);
815
816                                 BKE_undo_step(1);
817                                 sound_initialize_sounds();
818                         }
819                 }
820         }
821 }
822
823 void BIF_redo(void)
824 {
825         if(G.obedit) {
826                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
827                         undo_editmode_step(-1);
828         }
829         else {
830                 if(G.f & G_WEIGHTPAINT)
831                         wpaint_undo();
832                 else if(G.f & G_VERTEXPAINT)
833                         vpaint_undo();
834                 else if(G.f & G_TEXTUREPAINT)
835                         imagepaint_undo();
836                 else if(G.f & G_SCULPTMODE)
837                         sculptmode_redo();
838                 else if(curarea->spacetype==SPACE_IMAGE && (G.sima->flag & SI_DRAWTOOL))
839                         imagepaint_undo();
840                 else {
841                         /* includes faceselect now */
842                         if(U.uiflag & USER_GLOBALUNDO) {
843                                 BKE_undo_step(-1);
844                                 sound_initialize_sounds();
845                         }
846                 }
847         }
848 }
849
850 void BIF_undo_menu(void)
851 {
852         if(G.obedit) {
853                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
854                         undo_editmode_menu();
855                 allqueue(REDRAWALL, 0);
856         }
857         else {
858                 if(G.f & G_WEIGHTPAINT)
859                         ;
860                 else if(G.f & G_VERTEXPAINT)
861                         ;
862                 else if(G.f & G_SCULPTMODE)
863                         sculptmode_undo_menu();
864                 else {
865                         if(U.uiflag & USER_GLOBALUNDO) {
866                                 char *menu= BKE_undo_menu_string();
867                                 if(menu) {
868                                         short event= pupmenu_col(menu, 20);
869                                         MEM_freeN(menu);
870                                         if(event>0) BKE_undo_number(event);
871                                 }
872                         }
873                 }
874         }
875 }
876
877 /* *************** */
878
879 static void winqreadview3dspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
880 {
881         View3D *v3d= sa->spacedata.first;
882         Object *ob= OBACT;      /* do not change! */
883         float *curs;
884         int doredraw= 0, pupval;
885         unsigned short event= evt->event, origevent= evt->event;
886         short val= evt->val;
887         char ascii= evt->ascii;
888         
889         if(curarea->win==0) return;     /* when it comes from sa->headqread() */
890         
891         if(val) {
892
893                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
894
895                 //if(event==MOUSEY || event==MOUSEX) return;
896                 
897                 if(event==UI_BUT_EVENT) do_butspace(val); /* temporal, view3d deserves own queue? */
898                 
899                 /* we consider manipulator a button, defaulting to leftmouse */
900                 if(event==LEFTMOUSE) if(BIF_do_manipulator(sa)) return;
901                 
902                 /* swap mouse buttons based on user preference */
903                 if (U.flag & USER_LMOUSESELECT) {
904                         if (event==LEFTMOUSE) event = RIGHTMOUSE;
905                         else if (event==RIGHTMOUSE) event = LEFTMOUSE;
906                 }
907
908                 if(!G.obedit && (G.f & G_SCULPTMODE)) {
909                         if(G.scene->sculptdata.propset) {
910                                 sculptmode_propset(event);
911                                 return;
912                         }
913                         else if(event!=LEFTMOUSE && event!=MIDDLEMOUSE && (event==MOUSEY || event==MOUSEX)) {
914                                 if(!bwin_qtest(sa->win))
915                                         allqueue(REDRAWVIEW3D, 0);
916                         }
917                 }
918
919                 /* Handle retopo painting */
920                 if(retopo_mesh_paint_check()) {
921                         if(!retopo_paint(event))
922                                 return;
923                 }
924
925                 /* run any view3d event handler script links */
926                 if (event && sa->scriptlink.totscript)
927                         if (BPY_do_spacehandlers(sa, event, SPACEHANDLER_VIEW3D_EVENT))
928                                 return; /* return if event was processed (swallowed) by handler(s) */
929
930                 /* TEXTEDITING?? */
931                 if((G.obedit) && G.obedit->type==OB_FONT) {
932                         switch(event) {
933                         
934                         case LEFTMOUSE:
935                                 mouse_cursor();
936                                 break;
937                         case MIDDLEMOUSE:
938                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
939                                 if(U.flag & USER_VIEWMOVE) {
940                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
941                                                 viewmove(0);
942                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
943                                                 viewmove(2);
944                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
945                                                 viewmove(1);
946                                 }
947                                 else {
948                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
949                                                 viewmove(1);
950                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
951                                                 viewmove(2);
952                                         else
953                                                 viewmove(0);
954                                 }
955                                 break;
956                                 
957                         case WHEELUPMOUSE:
958                                 /* Regular:   Zoom in */
959                                 /* Shift:     Scroll up */
960                                 /* Ctrl:      Scroll right */
961                                 /* Alt-Shift: Rotate up */
962                                 /* Alt-Ctrl:  Rotate right */
963
964                                 if( G.qual & LR_SHIFTKEY ) {
965                                         if( G.qual & LR_ALTKEY ) { 
966                                                 G.qual &= ~LR_SHIFTKEY;
967                                                 persptoetsen(PAD2);
968                                                 G.qual |= LR_SHIFTKEY;
969                                         } else {
970                                                 persptoetsen(PAD2);
971                                         }
972                                 } else if( G.qual & LR_CTRLKEY ) {
973                                         if( G.qual & LR_ALTKEY ) { 
974                                                 G.qual &= ~LR_CTRLKEY;
975                                                 persptoetsen(PAD4);
976                                                 G.qual |= LR_CTRLKEY;
977                                         } else {
978                                                 persptoetsen(PAD4);
979                                         }
980                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
981                                         persptoetsen(PADMINUS);
982                                 else
983                                         persptoetsen(PADPLUSKEY);
984
985                                 doredraw= 1;
986                                 break;
987
988                         case WHEELDOWNMOUSE:
989                                 /* Regular:   Zoom out */
990                                 /* Shift:     Scroll down */
991                                 /* Ctrl:      Scroll left */
992                                 /* Alt-Shift: Rotate down */
993                                 /* Alt-Ctrl:  Rotate left */
994
995                                 if( G.qual & LR_SHIFTKEY ) {
996                                         if( G.qual & LR_ALTKEY ) { 
997                                                 G.qual &= ~LR_SHIFTKEY;
998                                                 persptoetsen(PAD8);
999                                                 G.qual |= LR_SHIFTKEY;
1000                                         } else {
1001                                                 persptoetsen(PAD8);
1002                                         }
1003                                 } else if( G.qual & LR_CTRLKEY ) {
1004                                         if( G.qual & LR_ALTKEY ) { 
1005                                                 G.qual &= ~LR_CTRLKEY;
1006                                                 persptoetsen(PAD6);
1007                                                 G.qual |= LR_CTRLKEY;
1008                                         } else {
1009                                                 persptoetsen(PAD6);
1010                                         }
1011                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1012                                         persptoetsen(PADPLUSKEY);
1013                                 else
1014                                         persptoetsen(PADMINUS);
1015                                 
1016                                 doredraw= 1;
1017                                 break;
1018
1019                         case UKEY:
1020                                 if(G.qual==LR_ALTKEY) {
1021                                         remake_editText();
1022                                         doredraw= 1;
1023                                 } 
1024                                 else {
1025                                         do_textedit(event, val, ascii);
1026                                 }
1027                                 break;
1028                         case VKEY:
1029                                 if(G.qual==LR_ALTKEY) {
1030                                         paste_editText();
1031                                         doredraw= 1;
1032                                 } 
1033                                 else {
1034                                         do_textedit(event, val, ascii);
1035                                 }
1036                                 break;
1037                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1038                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1039                         case PADENTER:
1040                                 persptoetsen(event);
1041                                 doredraw= 1;
1042                                 break;
1043                                 
1044                         default:
1045                                 do_textedit(event, val, ascii);
1046                                 break;
1047                         }
1048                 }
1049                 else {
1050
1051                         if (U.flag & USER_NONUMPAD) {
1052                                 event= convert_for_nonumpad(event);
1053                         }
1054
1055                         switch(event) {
1056                         
1057                         /* Afterqueue events */
1058                         case BACKBUFDRAW:
1059                                 backdrawview3d(1);
1060                                 break;
1061                         case RENDERPREVIEW:
1062                                 BIF_view3d_previewrender(sa);
1063                                 break;
1064                                 
1065                         /* LEFTMOUSE and RIGHTMOUSE event codes can be swapped above,
1066                          * based on user preference USER_LMOUSESELECT
1067                          */
1068                         case LEFTMOUSE: 
1069                                 if ((G.obedit) || !(G.f&(G_SCULPTMODE|G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT))) {
1070                                         mouse_cursor();
1071                                 }
1072                                 else if (G.f & G_SCULPTMODE) {
1073                                         if(G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1074                                                 sculptmode_pmv(0);
1075                                         else if(!G.scene->sculptdata.propset)
1076                                                 sculpt();
1077                                 }
1078                                 else if (G.f & G_WEIGHTPAINT) {
1079                                         weight_paint();
1080                                 }
1081                                 else if (G.f & G_VERTEXPAINT) {
1082                                         vertex_paint();
1083                                 }
1084                                 else if (G.f & G_TEXTUREPAINT) {
1085                                         imagepaint_paint(origevent==LEFTMOUSE? L_MOUSE: R_MOUSE, 1);
1086                                 }
1087                                 break;
1088                         case MIDDLEMOUSE:
1089                                 /* use '&' here, because of alt+leftmouse which emulates middlemouse */
1090                                 if(U.flag & USER_VIEWMOVE) {
1091                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1092                                                 viewmove(0);
1093                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1094                                                 viewmove(2);
1095                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1096                                                 viewmove(1);
1097                                 }
1098                                 else {
1099                                         if((G.qual==LR_SHIFTKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_SHIFTKEY))))
1100                                                 viewmove(1);
1101                                         else if((G.qual==LR_CTRLKEY) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==(LR_ALTKEY|LR_CTRLKEY))))
1102                                                 viewmove(2);
1103                                         else if((G.qual==0) || ((U.flag & USER_TWOBUTTONMOUSE) && (G.qual==LR_ALTKEY)))
1104                                                 viewmove(0);
1105                                 }
1106                                 break;
1107                         case RIGHTMOUSE:
1108                                 if((G.obedit) && (G.qual & LR_CTRLKEY)==0) {
1109                                         if(G.obedit->type==OB_MESH)
1110                                                 mouse_mesh();
1111                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1112                                                 mouse_nurb();
1113                                         else if(G.obedit->type==OB_MBALL)
1114                                                 mouse_mball();
1115                                         else if(G.obedit->type==OB_LATTICE)
1116                                                 mouse_lattice();
1117                                         else if(G.obedit->type==OB_ARMATURE)
1118                                                 mouse_armature();
1119                                 }
1120                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY)))
1121                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1122                                 else if((G.obedit && G.obedit->type==OB_MESH) && (G.qual == (LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)))
1123                                         mouse_mesh();   /* loop select for 1 mousebutton dudes */
1124                                 else if(G.qual==LR_CTRLKEY)
1125                                         mouse_select(); /* also allow in editmode, for vertex parenting */
1126                                 else if(G.f & G_FACESELECT)
1127                                         face_select();
1128                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT))
1129                                         sample_vpaint();
1130                                 else if((G.f & G_SCULPTMODE) && G.qual==LR_SHIFTKEY+LR_CTRLKEY)
1131                                         sculptmode_pmv(1);
1132                                 else
1133                                         mouse_select(); /* does poses too */
1134                                 break;
1135                         case WHEELUPMOUSE:
1136                                 /* Regular:   Zoom in */
1137                                 /* Shift:     Scroll up */
1138                                 /* Ctrl:      Scroll right */
1139                                 /* Alt-Shift: Rotate up */
1140                                 /* Alt-Ctrl:  Rotate right */
1141
1142                                 if( G.qual & LR_SHIFTKEY ) {
1143                                         if( G.qual & LR_ALTKEY ) { 
1144                                                 G.qual &= ~LR_SHIFTKEY;
1145                                                 persptoetsen(PAD2);
1146                                                 G.qual |= LR_SHIFTKEY;
1147                                         } else {
1148                                                 persptoetsen(PAD2);
1149                                         }
1150                                 } else if( G.qual & LR_CTRLKEY ) {
1151                                         if( G.qual & LR_ALTKEY ) { 
1152                                                 G.qual &= ~LR_CTRLKEY;
1153                                                 persptoetsen(PAD4);
1154                                                 G.qual |= LR_CTRLKEY;
1155                                         } else {
1156                                                 persptoetsen(PAD4);
1157                                         }
1158                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1159                                         persptoetsen(PADMINUS);
1160                                 else
1161                                         persptoetsen(PADPLUSKEY);
1162
1163                                 doredraw= 1;
1164                                 break;
1165                         case WHEELDOWNMOUSE:
1166                                 /* Regular:   Zoom out */
1167                                 /* Shift:     Scroll down */
1168                                 /* Ctrl:      Scroll left */
1169                                 /* Alt-Shift: Rotate down */
1170                                 /* Alt-Ctrl:  Rotate left */
1171
1172                                 if( G.qual & LR_SHIFTKEY ) {
1173                                         if( G.qual & LR_ALTKEY ) { 
1174                                                 G.qual &= ~LR_SHIFTKEY;
1175                                                 persptoetsen(PAD8);
1176                                                 G.qual |= LR_SHIFTKEY;
1177                                         } else {
1178                                                 persptoetsen(PAD8);
1179                                         }
1180                                 } else if( G.qual & LR_CTRLKEY ) {
1181                                         if( G.qual & LR_ALTKEY ) { 
1182                                                 G.qual &= ~LR_CTRLKEY;
1183                                                 persptoetsen(PAD6);
1184                                                 G.qual |= LR_CTRLKEY;
1185                                         } else {
1186                                                 persptoetsen(PAD6);
1187                                         }
1188                                 } else if(U.uiflag & USER_WHEELZOOMDIR) 
1189                                         persptoetsen(PADPLUSKEY);
1190                                 else
1191                                         persptoetsen(PADMINUS);
1192                                 
1193                                 doredraw= 1;
1194                                 break;
1195
1196                         case ONEKEY:
1197                                 if(G.qual==LR_CTRLKEY) {
1198                                         flip_subdivison(1);
1199                                 }
1200                                 else do_layer_buttons(0); 
1201                                 break;
1202                                 
1203                         case TWOKEY:
1204                                 if(G.qual==LR_CTRLKEY) {
1205                                         flip_subdivison(2);
1206                                 }
1207                                 else do_layer_buttons(1); 
1208                                 break;
1209                                 
1210                         case THREEKEY:
1211                                 if(G.qual==LR_CTRLKEY) {
1212                                         flip_subdivison(3);
1213                                 }
1214                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1215                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1216                                                 select_faces_by_numverts(3);
1217                                 }
1218                                 else do_layer_buttons(2); 
1219                                 break;
1220                                 
1221                         case FOURKEY:
1222                                 if(G.qual==LR_CTRLKEY) {
1223                                         flip_subdivison(4);
1224                                 }
1225                                 else if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1226                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1227                                                 select_faces_by_numverts(4);
1228                                 }
1229                                 else do_layer_buttons(3); 
1230                                 break;
1231                                 
1232                         case FIVEKEY:
1233                                 if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1234                                         if ( (G.obedit) && (G.obedit->type==OB_MESH) )
1235                                                 select_faces_by_numverts(5);
1236                                 }
1237                                 else do_layer_buttons(4);
1238                                 break;
1239
1240                         case SIXKEY:
1241                                 do_layer_buttons(5); break;
1242                         case SEVENKEY:
1243                                 do_layer_buttons(6); break;
1244                         case EIGHTKEY:
1245                                 do_layer_buttons(7); break;
1246                         case NINEKEY:
1247                                 do_layer_buttons(8); break;
1248                         case ZEROKEY:
1249                                 do_layer_buttons(9); break;
1250                         case MINUSKEY:
1251                                 do_layer_buttons(10); break;
1252                         case EQUALKEY:
1253                                 do_layer_buttons(11); break;
1254                         case ACCENTGRAVEKEY:
1255                                 do_layer_buttons(-1); break;
1256                         
1257                         case SPACEKEY:
1258                                 if(G.qual == LR_CTRLKEY) {
1259                                         val= pupmenu("Manipulator%t|Enable/Disable|Translate|Rotate|Scale|Combo");
1260                                         if(val>0) {
1261                                                 if(val==1) v3d->twflag ^= V3D_USE_MANIPULATOR;
1262                                                 else {
1263                                                         if(val==2) v3d->twtype= V3D_MANIP_TRANSLATE;
1264                                                         else if(val==3) v3d->twtype= V3D_MANIP_ROTATE;
1265                                                         else if(val==4) v3d->twtype= V3D_MANIP_SCALE;
1266                                                         else if(val==5) v3d->twtype= V3D_MANIP_TRANSLATE|V3D_MANIP_ROTATE|V3D_MANIP_SCALE;
1267                                                         v3d->twflag |= V3D_USE_MANIPULATOR;
1268                                                 }
1269                                                 doredraw= 1;
1270                                         }
1271                                 }
1272                                 else if(G.qual == LR_ALTKEY) {
1273                                         BIF_selectOrientation();
1274                                         doredraw= 1;
1275                                 }
1276
1277                                 break;
1278                                 
1279                         case AKEY:
1280                                 if(G.qual & LR_CTRLKEY) apply_object(); /* also with shift! */
1281                                 else if((G.qual==LR_SHIFTKEY)) {
1282                                         toolbox_n_add();
1283                                 }
1284                                 else {
1285                                         if(G.obedit) {
1286                                                 if(G.obedit->type==OB_MESH)
1287                                                         deselectall_mesh();
1288                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1289                                                         deselectall_nurb();
1290                                                 else if(G.obedit->type==OB_MBALL)
1291                                                         deselectall_mball();
1292                                                 else if(G.obedit->type==OB_LATTICE)
1293                                                         deselectall_Latt();
1294                                                 else if(G.obedit->type==OB_ARMATURE)
1295                                                         deselectall_armature(1);        /* 1 == toggle */
1296                                         }
1297                                         else if (ob && (ob->flag & OB_POSEMODE)){
1298                                                 deselectall_posearmature(ob, 1);
1299                                         }
1300                                         else {
1301                                                 if(G.f & G_FACESELECT) deselectall_tface();
1302                                                 else {
1303                                                         /* by design, the center of the active object 
1304                                                          * (which need not necessarily by selected) will
1305                                                          * still be drawn as if it were selected.
1306                                                          */
1307                                                         deselectall();
1308                                                 }
1309                                         }
1310                                 }
1311                                 break;
1312                         case BKEY:
1313                                 if(G.qual==LR_ALTKEY)
1314                                         view3d_edit_clipping(v3d);
1315                                 else if(G.qual==LR_SHIFTKEY)
1316                                 {
1317                                         if(G.vd->persp==2)
1318                                                 set_render_border();
1319                                         else
1320                                                 view3d_border_zoom();
1321                                 }
1322                                 else if(G.qual==LR_CTRLKEY) {
1323                                         if(okee("Bake all selected")) {
1324                                                 extern void softbody_bake(Object *ob);
1325                                                 extern void fluidsimBake(Object *ob);
1326                                                 softbody_bake(NULL);
1327                                                 /* also bake first domain of selected objects... */
1328                                                 fluidsimBake(NULL);
1329                                         }
1330                                 }
1331                                 else if(G.qual==0)
1332                                         borderselect();
1333                                 break;
1334                         case CKEY:
1335                                 if(G.qual==LR_CTRLKEY) {
1336                                         if(ob && (ob->flag & OB_POSEMODE))
1337                                                 pose_copy_menu();       /* poseobject.c */
1338                                         else
1339                                                 copy_attr_menu();
1340                                 }
1341                                 else if(G.qual==LR_ALTKEY) {
1342                                         if(ob && (ob->flag & OB_POSEMODE))
1343                                                 pose_clear_constraints();       /* poseobject.c */
1344                                         else
1345                                                 convertmenu();  /* editobject.c */
1346                                 }
1347                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY)) 
1348                                         add_constraint(0);      /* editconstraint.c, generic for objects and posemode */
1349                                 else if((G.qual==LR_SHIFTKEY)) {
1350                                         view3d_home(1);
1351                                         curs= give_cursor();
1352                                         curs[0]=curs[1]=curs[2]= 0.0;
1353                                         allqueue(REDRAWVIEW3D, 0);
1354                                 }
1355                                 else if((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
1356                                         makecyclicNurb();
1357                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1358                                         allqueue(REDRAWVIEW3D, 0);
1359                                 }
1360                                 else if((G.qual==0)){
1361                                         if (G.vd->persp==2)
1362                                                 /* center the camera offset */
1363                                                 G.vd->camdx= G.vd->camdy= 0.0;
1364                                         else {
1365                                                 /*non camera center*/
1366                                                 curs= give_cursor();
1367                                                 G.vd->ofs[0]= -curs[0];
1368                                                 G.vd->ofs[1]= -curs[1];
1369                                                 G.vd->ofs[2]= -curs[2];
1370                                         }
1371                                         scrarea_queue_winredraw(curarea);
1372                                 }
1373                                 break;
1374                         case DKEY:
1375                                 if((G.qual==LR_SHIFTKEY)) {
1376                                         duplicate_context_selected();
1377                                 }
1378                                 else if(G.qual==LR_ALTKEY) {
1379                                         if(ob && (ob->flag & OB_POSEMODE))
1380                                                 error ("Duplicate not possible in posemode.");
1381                                         else if((G.obedit==NULL))
1382                                                 adduplicate(0, 0);
1383                                 }
1384                                 else if(G.qual==LR_CTRLKEY) {
1385                                         imagestodisplist();
1386                                 }
1387                                 else if((G.qual==0)){
1388                                         if(G.f & G_SCULPTMODE)
1389                                                 sculptmode_propset_init(DKEY);
1390                                         else {
1391                                                 pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4|Textured Solid %x5");
1392                                                 if(pupval>0) {
1393                                                         G.vd->drawtype= pupval;
1394                                                         doredraw= 1;
1395                                                 }
1396                                         }
1397                                 }
1398                                 
1399                                 break;
1400                         case EKEY:
1401                                 if (G.qual==0){
1402                                         if(G.obedit) {
1403                                                 if(G.obedit->type==OB_MESH)
1404                                                         extrude_mesh();
1405                                                 else if(G.obedit->type==OB_CURVE)
1406                                                         addvert_Nurb('e');
1407                                                 else if(G.obedit->type==OB_SURF)
1408                                                         extrude_nurb();
1409                                                 else if(G.obedit->type==OB_ARMATURE)
1410                                                         extrude_armature(0);
1411                                         }
1412                                 }
1413                                 else if (G.qual==LR_CTRLKEY) {
1414                                         if(G.obedit && G.obedit->type==OB_MESH)
1415                                                 Edge_Menu();
1416                                         else if (G.f & G_FACESELECT)
1417                                                 seam_mark_clear_tface(0);
1418                                 }
1419                                 else if (G.qual==LR_SHIFTKEY) {
1420                                         if (G.obedit && G.obedit->type==OB_MESH) {
1421                                                 initTransform(TFM_CREASE, CTX_EDGE);
1422                                                 Transform();
1423                                         }
1424                                         else if (G.obedit && G.obedit->type==OB_ARMATURE) {
1425                                                 extrude_armature(1);
1426                                         }
1427                                 }
1428                                 break;
1429                         case FKEY:
1430                                 if(G.obedit) {
1431                                         if(G.obedit->type==OB_MESH) {
1432                                                 if((G.qual==LR_SHIFTKEY))
1433                                                         fill_mesh();
1434                                                 else if(G.qual==LR_ALTKEY)
1435                                                         beauty_fill();
1436                                                 else if(G.qual==LR_CTRLKEY)
1437                                                         edge_flip();
1438                                                 else if (G.qual==0)
1439                                                         addedgeface_mesh();
1440                                                 else if ( G.qual == 
1441                                                          (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1442                                                         select_linked_flat_faces();
1443                                                 }
1444
1445                                         }
1446                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
1447                                 }
1448                                 else if(G.qual==LR_CTRLKEY)
1449                                         sort_faces();
1450                                 else if((G.qual==LR_SHIFTKEY)) {
1451                                         if(ob && (ob->flag & OB_POSEMODE))
1452                                            pose_activate_flipped_bone();
1453                                         else if(G.f & G_WEIGHTPAINT)
1454                                                 pose_activate_flipped_bone();
1455                                         else
1456                                                 fly();
1457                                 }
1458                                 else {
1459                                         set_faceselect();
1460                                 }
1461                                 
1462                                 break;
1463                         case GKEY:
1464                                 if(G.qual == LR_CTRLKEY) 
1465                                         group_operation_with_menu();
1466                                 else if((G.qual==LR_SHIFTKEY))
1467                                         if(G.obedit) {
1468                                                 if(G.obedit->type==OB_MESH)
1469                                                         select_mesh_group_menu();
1470                                         } else
1471                                                 select_object_grouped_menu();
1472                                 else if(G.qual==LR_ALTKEY) {
1473                                         if(okee("Clear location")) {
1474                                                 clear_object('g');
1475                                         }
1476                                 }
1477                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1478                                         v3d->twtype= V3D_MANIP_TRANSLATE;
1479                                         doredraw= 1;
1480                                 }
1481                                 else if((G.qual==0)) {
1482                                         initTransform(TFM_TRANSLATION, CTX_NONE);
1483                                         Transform();
1484                                 }
1485                                 break;
1486                         case HKEY:
1487                                 if(G.obedit) {
1488                                         if(G.obedit->type==OB_MESH) {
1489                                                 if(G.qual==LR_CTRLKEY)
1490                                                         add_hook();
1491                                                 else if(G.qual==LR_ALTKEY)
1492                                                         reveal_mesh();
1493                                                 else if((G.qual==LR_SHIFTKEY))
1494                                                         hide_mesh(1);
1495                                                 else if((G.qual==0)) 
1496                                                         hide_mesh(0);
1497                                         }
1498                                         else if(G.obedit->type== OB_SURF) {
1499                                                 if(G.qual==LR_CTRLKEY)
1500                                                         add_hook();
1501                                                 else if(G.qual==LR_ALTKEY)
1502                                                         revealNurb();
1503                                                 else if((G.qual==LR_SHIFTKEY))
1504                                                         hideNurb(1);
1505                                                 else if((G.qual==0))
1506                                                         hideNurb(0);
1507                                         }
1508                                         else if(G.obedit->type==OB_CURVE) {
1509                                                 if(G.qual==LR_CTRLKEY)
1510                                                         add_hook();
1511                                                 else {
1512                                                         if(G.qual==LR_CTRLKEY)
1513                                                                 autocalchandlesNurb_all(1);     /* flag=1, selected */
1514                                                         else if((G.qual==LR_SHIFTKEY))
1515                                                                 sethandlesNurb(1);
1516                                                         else if((G.qual==0))
1517                                                                 sethandlesNurb(3);
1518                                                         
1519                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1520                                                         BIF_undo_push("Handle change");
1521                                                         allqueue(REDRAWVIEW3D, 0);
1522                                                 }
1523                                         }
1524                                         else if(G.obedit->type==OB_LATTICE) {
1525                                                 if(G.qual==LR_CTRLKEY) add_hook();
1526                                         }
1527                                         else if(G.obedit->type==OB_MBALL) {
1528                                                 if(G.qual==LR_ALTKEY)
1529                                                         reveal_mball();
1530                                                 else if((G.qual==LR_SHIFTKEY))
1531                                                         hide_mball(1);
1532                                                 else if((G.qual==0)) 
1533                                                         hide_mball(0);
1534                                         }
1535                                         else if(G.obedit->type==OB_ARMATURE) {
1536                                                 if (G.qual==0)
1537                                                         hide_selected_armature_bones();
1538                                                 else if (G.qual==LR_SHIFTKEY)
1539                                                         hide_unselected_armature_bones();
1540                                                 else if (G.qual==LR_ALTKEY)
1541                                                         show_all_armature_bones();
1542                                         }
1543                                 }
1544                                 else if(G.f & G_FACESELECT)
1545                                         hide_tface();
1546                                 else if(G.f & G_SCULPTMODE) {
1547                                         if(G.qual==LR_ALTKEY) {
1548                                                 waitcursor(1);
1549                                                 sculptmode_pmv_off(get_mesh(ob));
1550                                                 BIF_undo_push("Partial mesh hide");
1551                                                 allqueue(REDRAWVIEW3D,0);
1552                                                 waitcursor(0);
1553                                         }
1554                                 }
1555                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1556                                         if (G.qual==0)
1557                                                 hide_selected_pose_bones();
1558                                         else if (G.qual==LR_SHIFTKEY)
1559                                                 hide_unselected_pose_bones();
1560                                         else if (G.qual==LR_ALTKEY)
1561                                                 show_all_pose_bones();
1562                                 }
1563                                 break;
1564                         case IKEY:
1565                                 if(G.obedit);
1566                                 else if(G.qual==LR_CTRLKEY) {
1567                                         if(ob && ob->type==OB_ARMATURE) 
1568                                                 if(ob->flag & OB_POSEMODE) 
1569                                                         pose_add_IK();
1570                                 }
1571                                 else if(G.qual==LR_ALTKEY) {
1572                                         if(ob && ob->type==OB_ARMATURE) 
1573                                                 if(ob->flag & OB_POSEMODE) 
1574                                                         pose_clear_IK();
1575                                 }
1576                                 break;
1577                                 
1578                         case JKEY:
1579                                 if(G.qual==LR_CTRLKEY) {
1580                                         if( ob ) {
1581                                                 join_menu();
1582                                         }
1583                                         else if ((G.obedit) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1584                                                 addsegment_nurb();
1585                                 }
1586                                 else if(G.obedit) {
1587                                         if(G.obedit->type==OB_MESH) {
1588                                                 join_triangles();
1589                                         }
1590                                 }
1591
1592                                 break;
1593                         case KKEY:
1594                                 if(G.obedit) {
1595                                         if (G.obedit->type==OB_MESH) {
1596                                                 if (G.qual==LR_SHIFTKEY)
1597                                                         KnifeSubdivide(KNIFE_PROMPT);
1598                                                 else if (G.qual==0)
1599                                                         LoopMenu();
1600                                         }
1601                                         else if(G.obedit->type==OB_SURF)
1602                                                 printknots();
1603                                 }
1604                                 else {
1605                                         if((G.qual==LR_SHIFTKEY)) {
1606                                                 if(G.f & G_FACESELECT)
1607                                                         if (G.f & G_WEIGHTPAINT)
1608                                                                 clear_wpaint_selectedfaces();
1609                                                         else
1610                                                                 clear_vpaint_selectedfaces();
1611                                                 else if(G.f & G_VERTEXPAINT)
1612                                                         clear_vpaint();
1613                                                 else
1614                                                         select_select_keys();
1615                                         }
1616                                         else if (G.qual==0)
1617                                                 set_ob_ipoflags();
1618                                 }
1619                                 
1620                                 break;
1621                         case LKEY:
1622                                 if(G.obedit) {
1623                                         if(G.obedit->type==OB_MESH)
1624                                                 selectconnected_mesh(G.qual);
1625                                         if(G.obedit->type==OB_ARMATURE)
1626                                                 selectconnected_armature();
1627                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
1628                                                 selectconnected_nurb();
1629                                 }
1630                                 else if(ob && (ob->flag & OB_POSEMODE)) {
1631                                         selectconnected_posearmature();
1632                                 }
1633                                 else {
1634                                         if(G.f & G_FACESELECT) {
1635                                                 if((G.qual==0))
1636                                                         select_linked_tfaces(0);
1637                                                 else if((G.qual==LR_SHIFTKEY))
1638                                                         select_linked_tfaces(1);
1639                                                 else if(G.qual==LR_CTRLKEY)
1640                                                         select_linked_tfaces(2);
1641                                         }
1642                                         else {
1643                                                 if((G.qual==0))
1644                                                         make_local_menu();
1645                                                 else if((G.qual==LR_SHIFTKEY))
1646                                                         selectlinks_menu();
1647                                                 else if(G.qual==LR_CTRLKEY)
1648                                                         make_links_menu();
1649                                         }
1650                                 }
1651                                 break;
1652                         case MKEY:
1653                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1654                                         mirror_uv_tface();
1655                                 else if(G.obedit){
1656                                         if(G.qual==LR_ALTKEY) {
1657                                                 if(G.obedit->type==OB_MESH) {
1658                                                         mergemenu();
1659                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1660                                                 }
1661                                         }
1662                                         else if((G.qual==0) || (G.qual==LR_CTRLKEY)) {
1663                                                 mirrormenu();
1664                                         }
1665                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1666                                                 if(G.obedit->type==OB_MESH) select_non_manifold();
1667                                         }
1668                                 }
1669                                 else if(G.qual & LR_CTRLKEY) {
1670                                         mirrormenu();
1671                                 }
1672                                 else if(G.qual==0) {
1673                                         if(ob && (ob->flag & OB_POSEMODE))
1674                                                 pose_movetolayer();
1675                                         else
1676                                                 movetolayer();
1677                                 }
1678                                 break;
1679                         case NKEY:
1680                                 if((G.qual==0)) {
1681                                         toggle_blockhandler(curarea, VIEW3D_HANDLER_OBJECT, UI_PNL_TO_MOUSE);
1682                                         allqueue(REDRAWVIEW3D, 0);
1683                                 }
1684                                 else if(G.obedit) {
1685                                         switch (G.obedit->type){
1686                                         case OB_ARMATURE:
1687                                                 if(G.qual==LR_CTRLKEY){
1688                                                         if (okee("Recalculate bone roll angles")) {
1689                                                                 auto_align_armature();
1690                                                                 allqueue(REDRAWVIEW3D, 0);
1691                                                         }
1692                                                 }
1693                                                 break;
1694                                         case OB_MESH: 
1695                                                 if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1696                                                         if(okee("Recalculate normals inside")) {
1697                                                                 righthandfaces(2);
1698                                                                 allqueue(REDRAWVIEW3D, 0);
1699                                                                 BIF_undo_push("Recalculate normals inside");
1700                                                         }
1701                                                 }
1702                                                 else if(G.qual==LR_CTRLKEY){
1703                                                         if(okee("Recalculate normals outside")) {
1704                                                                 righthandfaces(1);
1705                                                                 allqueue(REDRAWVIEW3D, 0);
1706                                                                 BIF_undo_push("Recalculate normals outside");
1707                                                         }
1708                                                 }
1709                                                 break;
1710                                         }
1711                                 }
1712                                 
1713                                 break;
1714                         case OKEY:
1715                                 if (G.obedit) {
1716                                         if (G.qual==LR_SHIFTKEY) {
1717                                                 G.scene->prop_mode = (G.scene->prop_mode+1)%7;
1718                                                 allqueue(REDRAWHEADERS, 0);
1719                                         }
1720                                         else if((G.qual==LR_ALTKEY)) {
1721                                                 if(G.scene->proportional==2) G.scene->proportional= 1;
1722                                                 else G.scene->proportional= 2;
1723                                                 allqueue(REDRAWHEADERS, 0);
1724                                         }
1725                                         else if((G.qual==0)) {
1726                                                 G.scene->proportional= !G.scene->proportional;
1727                                                 allqueue(REDRAWHEADERS, 0);
1728                                         }
1729                                 }
1730                                 else if((G.qual==LR_SHIFTKEY || G.qual==(LR_ALTKEY|LR_SHIFTKEY))) {
1731                                         flip_subdivison(-1);
1732                                 }
1733                                 else if(G.qual==LR_ALTKEY) {
1734                                         if(okee("Clear origin")) {
1735                                                 clear_object('o');
1736                                         }
1737                                 }
1738                                 break;
1739
1740                         case PKEY:
1741                                 if(G.obedit) {
1742                                         if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1743                                                 if(G.obedit->type==OB_ARMATURE)
1744                                                         make_bone_parent();
1745                                                 else
1746                                                         make_parent();
1747                                         }
1748                                         
1749                                         else if(G.qual==LR_ALTKEY && G.obedit->type==OB_ARMATURE)
1750                                                 clear_bone_parent();
1751                                         else if((G.qual==0) && (G.obedit->type==OB_ARMATURE)) 
1752                                                 select_bone_parent();
1753                                         else if((G.qual==0) && G.obedit->type==OB_MESH)
1754                                                 separatemenu();
1755                                         else if ((G.qual==0) && ELEM(G.obedit->type, OB_CURVE, OB_SURF))
1756                                                 separate_nurb();
1757                                         else if (G.qual==LR_SHIFTKEY) {
1758                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
1759                                                 Transform();
1760                                         }
1761                                 }
1762                                 else if(G.qual==LR_CTRLKEY || G.qual==(LR_SHIFTKEY|LR_CTRLKEY))
1763                                         make_parent();
1764                                 else if(G.qual==LR_SHIFTKEY) {
1765                                         if(G.obedit) {
1766                                                 initTransform(TFM_PUSHPULL, CTX_NONE);
1767                                                 Transform();
1768                                         }
1769                                         else {
1770                                                 toggle_blockhandler(curarea, VIEW3D_HANDLER_PREVIEW, 0);
1771                                                 doredraw= 1;
1772                                         }
1773                                 }
1774                                 else if(G.qual==LR_ALTKEY)
1775                                         clear_parent();
1776                                 else if(G.qual==(LR_ALTKEY|LR_CTRLKEY))
1777                                         make_proxy();
1778                                 else if((G.qual==0) && (OBACT) && (OBACT->type==OB_ARMATURE) && (OBACT->flag & OB_POSEMODE))
1779                                         select_bone_parent();
1780                                 else if((G.qual==0)) {
1781                         start_game();
1782                                 }
1783                                 break;                          
1784                         case RKEY:
1785                                 if((G.obedit==0) && (G.f & G_FACESELECT) && (G.qual==0))
1786                                         rotate_uv_tface();
1787                                 else if(G.qual==LR_ALTKEY) {
1788                                         if(okee("Clear rotation")) {
1789                                                 clear_object('r');
1790                                         }
1791                                 } 
1792                                 else if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1793                                         v3d->twtype= V3D_MANIP_ROTATE;
1794                                         doredraw= 1;
1795                                 }
1796                                 else if (G.obedit) {
1797                                         if((G.qual==LR_SHIFTKEY)) {
1798                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF)                                     
1799                                                         selectrow_nurb();
1800                                         }
1801                                         else if(G.qual==LR_CTRLKEY) {
1802                                                 if (G.obedit->type==OB_MESH)
1803                                                         CutEdgeloop(1);
1804                                                         BIF_undo_push("Cut Edgeloop");
1805                                         }
1806                                         else if((G.qual==0)) {
1807                                                 initTransform(TFM_ROTATION, CTX_NONE);
1808                                                 Transform();
1809                                         }
1810                                 }
1811                                 else if((G.qual==0)) {
1812                                         initTransform(TFM_ROTATION, CTX_NONE);
1813                                         Transform();
1814                                 }
1815                                 break;
1816                         case SKEY:
1817                                 if(G.qual== (LR_CTRLKEY|LR_ALTKEY)) {
1818                                         v3d->twtype= V3D_MANIP_SCALE;
1819                                         doredraw= 1;
1820                                 }
1821                                 else if(G.obedit) {
1822                                         
1823                                         if(G.qual==LR_ALTKEY) {
1824                                                 if(G.obedit->type==OB_ARMATURE) {
1825                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1826                                                 }
1827                                                 else if (G.obedit->type==OB_CURVE) {
1828                                                         initTransform(TFM_CURVE_SHRINKFATTEN, CTX_NONE);
1829                                                 } else {
1830                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
1831                                                 }
1832                                                 Transform();
1833                                         }
1834                                         else if(G.qual==LR_CTRLKEY) {
1835                                                 initTransform(TFM_SHEAR, CTX_NONE);
1836                                                 Transform();
1837                                         }
1838                                         else if(G.qual==LR_SHIFTKEY)
1839                                                 snapmenu();
1840                                         else if(G.qual==0) {
1841                                                 if(G.obedit->type==OB_ARMATURE) {
1842                                                         bArmature *arm= G.obedit->data;
1843                                                         if(arm->drawtype==ARM_ENVELOPE)
1844                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
1845                                                         else
1846                                                                 initTransform(TFM_RESIZE, CTX_NONE);
1847                                                 }
1848                                                 else
1849                                                         initTransform(TFM_RESIZE, CTX_NONE);
1850                                                 Transform();
1851                                         }
1852                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
1853                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
1854                                                 Transform();
1855                                         }
1856                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1857                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
1858                                         }
1859                                 }
1860                                 else if(G.qual==LR_ALTKEY) {
1861                                         if(G.f & G_WEIGHTPAINT)
1862                                                 ob= ob->parent;
1863                                         if(ob && (ob->flag & OB_POSEMODE)) {
1864                                                 bArmature *arm= ob->data;
1865                                                 if( ELEM(arm->drawtype, ARM_B_BONE, ARM_ENVELOPE)) {
1866                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1867                                                         Transform();
1868                                                         break;
1869                                                 }
1870                                         }
1871                                         
1872                                         if(okee("Clear scale")) {
1873                                                 clear_object('s');
1874                                         }
1875                                 }
1876                                 else if(G.qual==LR_SHIFTKEY) {
1877                                         snapmenu();
1878                                 }
1879                                 else if((G.qual==0)) {
1880                                         initTransform(TFM_RESIZE, CTX_NONE);
1881                                         Transform();
1882                                 }
1883                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1884                                         initTransform(TFM_TOSPHERE, CTX_NONE);
1885                                         Transform();
1886                                 }
1887                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
1888                                         initTransform(TFM_SHEAR, CTX_NONE);
1889                                         Transform();
1890                                 }
1891                                 break;
1892                         case TKEY:
1893                                 if(G.obedit){
1894                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
1895                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
1896                                                 allqueue(REDRAWVIEW3D, 0);
1897                                                 countall();
1898                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1899                                         }
1900                                         if (G.obedit->type==OB_CURVE) {
1901                                                 if (G.qual==LR_ALTKEY) {
1902                                                         clear_tilt();
1903                                                 }
1904                                                 else if (G.qual==0) {
1905                                                         initTransform(TFM_TILT, CTX_NONE);
1906                                                         Transform();
1907                                                 }
1908                                         }
1909                                 }
1910                                 else if(G.qual==LR_CTRLKEY) {
1911                                         if(ob && (ob->flag & OB_POSEMODE));
1912                                         else make_track();
1913                                 }
1914                                 else if(G.qual==LR_ALTKEY) {
1915                                         if(ob && (ob->flag & OB_POSEMODE));
1916                                         else clear_track();
1917                                 }
1918                                 else if((G.qual==0)){
1919                                         texspace_edit();
1920                                 }
1921                                 
1922                                 break;
1923                         case UKEY:
1924                                 if(G.obedit) {
1925                                         if(G.obedit->type==OB_MESH) {
1926                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1927                                         }
1928                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
1929                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1930                                         }
1931                                 }
1932                                 else if((G.qual==0)) {
1933                                         if(G.f & G_WEIGHTPAINT)
1934                                                 wpaint_undo();
1935                                         else if(G.f & G_VERTEXPAINT)
1936                                                 vpaint_undo();
1937                                         else if(G.f & G_TEXTUREPAINT)
1938                                                 imagepaint_undo();
1939                                         else if (G.f & G_FACESELECT)
1940                                                 uv_autocalc_tface();
1941                                         else {
1942                                                 single_user();
1943                                         }
1944                                 }
1945                                         
1946                                 break;
1947                         case VKEY:
1948                                 if((G.qual==LR_SHIFTKEY)) {
1949                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
1950                                                 align_view_to_selected(v3d);
1951                                         }
1952                                         else if (G.f & G_FACESELECT) {
1953                                                 align_view_to_selected(v3d);
1954                                         }
1955                                 }
1956                                 else if(G.qual==LR_ALTKEY)
1957                                         image_aspect();
1958                                 else if (G.qual==0){
1959                                         if(G.obedit) {
1960                                                 if(G.obedit->type==OB_MESH) {
1961                                                         mesh_rip();
1962                                                 }
1963                                                 else if(G.obedit->type==OB_CURVE) {
1964                                                         sethandlesNurb(2);
1965                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1966                                                         allqueue(REDRAWVIEW3D, 0);
1967                                                         BIF_undo_push("Handle change");
1968                                                 }
1969                                         }
1970                                         else if(ob && ob->type == OB_MESH) 
1971                                                 set_vpaint();
1972                                 }
1973                                 break;
1974                         case WKEY:
1975                                 if((G.qual==LR_SHIFTKEY)) {
1976                                         initTransform(TFM_WARP, CTX_NONE);
1977                                         Transform();
1978                                 }
1979                                 else if(G.qual==LR_ALTKEY) {
1980                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
1981                                 }
1982                                 else if(G.qual==LR_CTRLKEY) {
1983                                         if(G.obedit) {
1984                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
1985                                                         switchdirectionNurb2();
1986                                                 }
1987                                         }
1988                                 }
1989                                 else if((G.qual==0))
1990                                         special_editmenu();
1991                                 
1992                                 break;
1993                         case XKEY:
1994                         case DELKEY:
1995                                 if(G.qual==0 || G.qual==LR_SHIFTKEY)
1996                                         delete_context_selected();
1997                                 break;
1998                         case YKEY:
1999                                 if((G.qual==0) && (G.obedit)) {
2000                                         if(G.obedit->type==OB_MESH) split_mesh();
2001                                 }
2002                                 break;
2003                         case ZKEY:
2004                                 toggle_shading();
2005                                 
2006                                 scrarea_queue_headredraw(curarea);
2007                                 scrarea_queue_winredraw(curarea);
2008                                 break;
2009                         
2010                         case HOMEKEY:
2011                                 if(G.qual==0)
2012                                         view3d_home(0);
2013                                 break;
2014                         case COMMAKEY:
2015                                 if(G.qual==LR_CTRLKEY) {
2016                                         G.vd->around= V3D_CENTROID;
2017                                 } else if(G.qual==LR_SHIFTKEY) {
2018                                         G.vd->around= V3D_CENTROID;
2019                                 } else if(G.qual==0) {
2020                                         G.vd->around= V3D_CENTRE;
2021                                 }
2022                                 handle_view3d_around();
2023                                 
2024                                 scrarea_queue_headredraw(curarea);
2025                                 scrarea_queue_winredraw(curarea);
2026                                 break;
2027                                 
2028                         case PERIODKEY:
2029                                 if(G.qual==LR_CTRLKEY) {
2030                                         G.vd->around= V3D_LOCAL;
2031                                 }       else if(G.qual==0) {
2032                                         G.vd->around= V3D_CURSOR;
2033                                 }
2034                                 handle_view3d_around();
2035                                 
2036                                 scrarea_queue_headredraw(curarea);
2037                                 scrarea_queue_winredraw(curarea);
2038                                 break;
2039                         
2040                         case PADSLASHKEY:
2041                                 if(G.qual==0) {
2042                                         if(G.vd->localview) {
2043                                                 G.vd->localview= 0;
2044                                                 endlocalview(curarea);
2045                                         }
2046                                         else {
2047                                                 G.vd->localview= 1;
2048                                                 initlocalview();
2049                                         }
2050                                         scrarea_queue_headredraw(curarea);
2051                                 }
2052                                 break;
2053                         case PADASTERKEY:       /* '*' */
2054                                 if(G.qual==0) {
2055                                         if(ob) {
2056                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
2057                                                         editmesh_align_view_to_selected(G.vd, 2);
2058                                                 } 
2059                                                 else if (G.f & G_FACESELECT) {
2060                                                         if(ob->type==OB_MESH) {
2061                                                                 Mesh *me= ob->data;
2062                                                                 faceselect_align_view_to_selected(G.vd, me, 2);
2063                                                         }
2064                                                 }
2065                                                 else
2066                                                         obmat_to_viewmat(ob);
2067                                                 
2068                                                 if(G.vd->persp==2) G.vd->persp= 1;
2069                                                 scrarea_queue_winredraw(curarea);
2070                                         }
2071                                 }
2072                                 break;
2073                         case PADPERIOD: /* '.' */
2074                                 if(G.qual==0)
2075                                         centreview();
2076                                 break;
2077                         
2078                         case PAGEUPKEY:
2079                                 if(G.f & G_SCULPTMODE) {
2080                                         if(ob && ob->type == OB_MESH && ((Mesh*)ob->data)->mr) {
2081                                                 ((Mesh*)ob->data)->mr->newlvl= ((Mesh*)ob->data)->mr->current+1;
2082                                                 multires_set_level(ob,ob->data);
2083                                         }
2084                                 } else {
2085                                         if(G.qual==LR_CTRLKEY)
2086                                                 movekey_obipo(1);
2087                                         else if((G.qual==0))
2088                                                 nextkey_obipo(1);       /* in editipo.c */
2089                                 }
2090                                 break;
2091
2092                         case PAGEDOWNKEY:
2093                                 if(G.f & G_SCULPTMODE) {
2094                                         if(ob && ob->type == OB_MESH && ((Mesh*)ob->data)->mr) {
2095                                                 ((Mesh*)ob->data)->mr->newlvl= ((Mesh*)ob->data)->mr->current-1;
2096                                                 multires_set_level(ob,ob->data);
2097                                         }
2098                                 } else {
2099                                         if(G.qual==LR_CTRLKEY)
2100                                                 movekey_obipo(-1);
2101                                         else if((G.qual==0))
2102                                                 nextkey_obipo(-1);
2103                                 }
2104                                 break;
2105
2106                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
2107                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
2108                         case PADENTER:
2109                                 persptoetsen(event);
2110                                 doredraw= 1;
2111                                 break;
2112                         case PADMINUS:
2113                                 if ( (G.qual==LR_CTRLKEY)
2114                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2115                                         select_less();
2116                                 else if ( (G.qual==LR_CTRLKEY)
2117                                          && (G.obedit) && (G.obedit->type==OB_CURVE) )
2118                                         select_less_nurb();
2119                                 /*
2120                                 else if ( (G.qual==LR_CTRLKEY)
2121                                          && (G.obedit) && (G.obedit->type==OB_SURF) )
2122                                         select_less_nurb(); 
2123                                 */
2124                                 else {
2125                                         persptoetsen(event);
2126                                         doredraw= 1;
2127                                 }
2128                                 break;
2129
2130                         case PADPLUSKEY:
2131                                 if ( (G.qual==LR_CTRLKEY)
2132                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2133                                         select_more();
2134                                 else if ( (G.qual==LR_CTRLKEY)
2135                                          && (G.obedit) && (G.obedit->type==OB_CURVE) )
2136                                         select_more_nurb();
2137                                 /*
2138                                 else if ( (G.qual==LR_CTRLKEY)
2139                                          && (G.obedit) && (G.obedit->type==OB_SURF) )
2140                                         select_more_nurb();
2141                                 */
2142                                 else {
2143                                         persptoetsen(event);
2144                                         doredraw= 1;
2145                                 }
2146                                 break;
2147
2148                         case ESCKEY:
2149                                 if(G.qual==0) {
2150                                         if (G.vd->flag & V3D_DISPIMAGE) {
2151                                                 G.vd->flag &= ~V3D_DISPIMAGE;
2152                                                 doredraw= 1;
2153                                         }
2154                                 }
2155                                 break;
2156                         }
2157                 }
2158         }
2159         
2160         if(doredraw) {
2161                 scrarea_queue_winredraw(curarea);
2162                 scrarea_queue_headredraw(curarea);
2163         }
2164 }
2165
2166 static void initview3d(ScrArea *sa)
2167 {
2168         View3D *vd;
2169         
2170         vd= MEM_callocN(sizeof(View3D), "initview3d");
2171         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
2172
2173         vd->spacetype= SPACE_VIEW3D;
2174         vd->blockscale= 0.7f;
2175         vd->viewquat[0]= 1.0f;
2176         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
2177         vd->persp= 1;
2178         vd->drawtype= OB_WIRE;
2179         vd->view= 7;
2180         vd->dist= 10.0;
2181         vd->lens= 35.0f;
2182         vd->near= 0.01f;
2183         vd->far= 500.0f;
2184         vd->grid= 1.0f;
2185         vd->gridlines= 16;
2186         vd->lay= vd->layact= 1;
2187         if(G.scene) {
2188                 vd->lay= vd->layact= G.scene->lay;
2189                 vd->camera= G.scene->camera;
2190         }
2191         vd->scenelock= 1;
2192         vd->gridflag |= V3D_SHOW_X;
2193         vd->gridflag |= V3D_SHOW_Y;
2194         vd->gridflag |= V3D_SHOW_FLOOR;
2195         vd->gridflag &= ~V3D_SHOW_Z;
2196
2197         vd->depths= NULL;
2198 }
2199
2200
2201 /* ******************** SPACE: IPO ********************** */
2202
2203 static void changeview2dspace(ScrArea *sa, void *spacedata)
2204 {
2205         if(G.v2d==0) return;
2206
2207         test_view2d(G.v2d, curarea->winx, curarea->winy);
2208         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
2209 }
2210
2211 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2212 {
2213         extern void do_ipobuts(unsigned short event);   /* drawipo.c */
2214         unsigned short event= evt->event;
2215         short val= evt->val;
2216         SpaceIpo *sipo= curarea->spacedata.first;
2217         View2D *v2d= &sipo->v2d;
2218         float dx, dy;
2219         int cfra, doredraw= 0;
2220         short mval[2];
2221         short mousebut = L_MOUSE;
2222         
2223         if(sa->win==0) return;
2224
2225         if(val) {
2226                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
2227
2228                 /* swap mouse buttons based on user preference */
2229                 if (U.flag & USER_LMOUSESELECT) {
2230                         if (event == LEFTMOUSE) {
2231                                 event = RIGHTMOUSE;
2232                                 mousebut = L_MOUSE;
2233                         } else if (event == RIGHTMOUSE) {
2234                                 event = LEFTMOUSE;
2235                                 mousebut = R_MOUSE;
2236                         }
2237                 }
2238
2239                 switch(event) {
2240                 case UI_BUT_EVENT:
2241                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
2242                         if(val>0 && val < 256) do_ipowin_buts(val-1);
2243                         else do_ipobuts(val);
2244                         break;
2245                         
2246                 case LEFTMOUSE:
2247                         if( in_ipo_buttons() ) {
2248                                 do_ipo_selectbuttons();
2249                                 doredraw= 1;
2250                         }
2251                         else if(view2dmove(LEFTMOUSE)); /* only checks for sliders */
2252                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
2253                         else {
2254                                 do {
2255                                         getmouseco_areawin(mval);
2256                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
2257                                         
2258                                         cfra= (int)dx;
2259                                         if(cfra< 1) cfra= 1;
2260                                         
2261                                         if( cfra!=CFRA ) {
2262                                                 CFRA= cfra;
2263                                                 update_for_newframe_nodraw(0);  /* 1 = nosound */
2264                                                 force_draw_all(0); /* To make constraint sliders redraw */
2265                                         }
2266                                         else PIL_sleep_ms(30);
2267                                 
2268                                 } while(get_mbut() & mousebut);
2269                         }
2270                         break;
2271                 case RIGHTMOUSE:
2272                         mouse_select_ipo();
2273                         allqueue (REDRAWACTION, 0);
2274                         allqueue(REDRAWNLA, 0);
2275                         break;
2276                 case MIDDLEMOUSE:
2277                         if(in_ipo_buttons()) {
2278                                 scroll_ipobuts();
2279                         }
2280                         else view2dmove(event); /* in drawipo.c */
2281                         break;
2282                 case WHEELUPMOUSE:
2283                 case WHEELDOWNMOUSE:
2284                         view2dmove(event);      /* in drawipo.c */
2285                         break;
2286                 case PADPLUSKEY:
2287                         view2d_zoom(v2d, 0.1154f, sa->winx, sa->winy);
2288                         doredraw= 1;
2289                         break;
2290                 case PADMINUS:
2291                         view2d_zoom(v2d, -0.15f, sa->winx, sa->winy);
2292                         doredraw= 1;
2293                         break;
2294                 case PAGEUPKEY:
2295                         if(G.qual==LR_CTRLKEY)
2296                                 movekey_ipo(1);
2297                         else if((G.qual==0))
2298                                 nextkey_ipo(1);
2299                         break;
2300                 case PAGEDOWNKEY:
2301                         if(G.qual==LR_CTRLKEY)
2302                                 movekey_ipo(-1);
2303                         else if((G.qual==0))
2304                                 nextkey_ipo(-1);
2305                         break;
2306                 case HOMEKEY:
2307                         if((G.qual==0))
2308                                 do_ipo_buttons(B_IPOHOME);
2309                         break;
2310                         
2311                 case AKEY:
2312                         if((G.qual==0)) {
2313                                 if(in_ipo_buttons()) {
2314                                         swap_visible_editipo();
2315                                 }
2316                                 else {
2317                                         swap_selectall_editipo();
2318                                 }
2319                                 allspace (REMAKEIPO, 0);
2320                                 allqueue (REDRAWNLA, 0);
2321                                 allqueue (REDRAWACTION, 0);
2322                         }
2323                         break;
2324                 case BKEY:
2325                         if((G.qual==0))
2326                                 borderselect_ipo();
2327                         break;
2328                 case CKEY:
2329                         if((G.qual==0))
2330                                 move_to_frame();
2331                         break;
2332                 case DKEY:
2333                         if((G.qual==LR_SHIFTKEY))
2334                                 add_duplicate_editipo();
2335                         break;
2336                 case GKEY:
2337                         if((G.qual==0))
2338                                 transform_ipo('g');
2339                         break;
2340                 case HKEY:
2341                         if(G.qual==LR_ALTKEY)
2342                                 sethandles_ipo(HD_AUTO_ANIM);
2343                         if(G.qual==LR_SHIFTKEY)
2344                                 sethandles_ipo(HD_AUTO);
2345                         else if(G.qual==0)
2346                                 sethandles_ipo(HD_ALIGN);
2347                         break;
2348                 case JKEY:
2349                         if((G.qual==LR_CTRLKEY))
2350                                 join_ipo_menu();
2351                         break;
2352                 case KKEY:
2353                         if((G.qual==0)) {
2354                                 ipo_toggle_showkey();
2355                                 scrarea_queue_headredraw(curarea);
2356                                 allqueue(REDRAWVIEW3D, 0);
2357                                 doredraw= 1;
2358                         }
2359                         break;
2360                 case MKEY:
2361                         if((G.qual==0))
2362                                 ipo_mirror_menu();
2363                         break;
2364                 case NKEY:
2365                         toggle_blockhandler(sa, IPO_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2366                         doredraw= 1;
2367                         break;
2368                 case OKEY: 
2369                         if(G.qual==LR_SHIFTKEY) 
2370                                 smooth_ipo();
2371                         else
2372                                 clean_ipo(sipo->ipo, 1);
2373                         break;
2374                 case RKEY:
2375                         if((G.qual==0))
2376                                 ipo_record();
2377                         break;
2378                 case SKEY:
2379                         if((G.qual==LR_SHIFTKEY)) {             
2380                                 ipo_snap_menu();
2381                         } else if((G.qual==0))
2382                                 transform_ipo('s');
2383                         break;
2384                 case TKEY:
2385                         if((G.qual==0))
2386                                 set_ipotype();
2387                         break;
2388                 case VKEY:
2389                         if((G.qual==0))
2390                                 sethandles_ipo(HD_VECT);
2391                         break;
2392                 case XKEY:
2393                 case DELKEY:
2394                         del_ipo();
2395                         break;
2396                 }
2397         }
2398
2399         if(doredraw) scrarea_queue_winredraw(sa);
2400 }
2401
2402 void initipo(ScrArea *sa)
2403 {
2404         SpaceIpo *sipo;
2405         
2406         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
2407         BLI_addhead(&sa->spacedata, sipo);
2408
2409         sipo->spacetype= SPACE_IPO;
2410         sipo->blockscale= 0.7f;
2411         
2412         /* sipo space loopt van (0,-?) tot (??,?) */
2413         sipo->v2d.tot.xmin= 0.0;
2414         sipo->v2d.tot.ymin= -10.0;
2415         sipo->v2d.tot.xmax= G.scene->r.efra;
2416         sipo->v2d.tot.ymax= 10.0;
2417
2418         sipo->v2d.cur= sipo->v2d.tot;
2419
2420         sipo->v2d.min[0]= 0.01f;
2421         sipo->v2d.min[1]= 0.01f;
2422
2423         sipo->v2d.max[0]= 15000.0f;
2424         sipo->v2d.max[1]= 10000.0f;
2425         
2426         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
2427         sipo->v2d.keeptot= 0;
2428
2429         sipo->blocktype= ID_OB;
2430 }
2431
2432 /* ******************** SPACE: INFO ********************** */
2433
2434 void space_mipmap_button_function(int event) {
2435         set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
2436
2437         allqueue(REDRAWVIEW3D, 0);
2438 }
2439
2440 #if 0
2441 static void space_sound_button_function(int event)
2442 {
2443         int a;
2444         SYS_SystemHandle syshandle;
2445
2446         if ((syshandle = SYS_GetSystem()))
2447         {
2448                 a = (U.gameflags & USER_DISABLE_SOUND);
2449                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
2450         }
2451 }
2452 #endif
2453
2454 /* needed for event; choose new 'curmain' resets it... */
2455 static short th_curcol= TH_BACK;
2456 static char *th_curcol_ptr= NULL;
2457 static char th_curcol_arr[4]={0, 0, 0, 255};
2458
2459 static void info_user_themebuts(uiBlock *block, short y1, short y2, short y3)
2460 {
2461         bTheme *btheme, *bt;
2462         int spacetype= 0;
2463         static short cur=1, curmain=2;
2464         short a, tot=0, isbuiltin= 0;
2465         char string[21*32], *strp, *col;
2466         
2467         y3= y2+23;      /* exception! */
2468         
2469         /* count total, max 16! */
2470         for(bt= U.themes.first; bt; bt= bt->next) tot++;
2471         
2472         /* if cur not is 1; move that to front of list */
2473         if(cur!=1) {
2474                 a= 1;
2475                 for(bt= U.themes.first; bt; bt= bt->next, a++) {
2476                         if(a==cur) {
2477                                 BLI_remlink(&U.themes, bt);
2478                                 BLI_addhead(&U.themes, bt);
2479                                 allqueue(REDRAWALL, 0);
2480                                 cur= 1;
2481                                 break;
2482                         }
2483                 }
2484         }
2485         
2486         /* the current theme */
2487         btheme= U.themes.first;
2488         if(strcmp(btheme->name, "Default")==0) isbuiltin= 1;
2489
2490         /* construct popup script */
2491         string[0]= 0;
2492         for(bt= U.themes.first; bt; bt= bt->next) {
2493                 strcat(string, bt->name);
2494                 if(btheme->next) strcat(string, "   |");
2495         }
2496         uiDefButS(block, MENU, B_UPDATE_THEME, string,                  45,y3,200,20, &cur, 0, 0, 0, 0, "Current theme");
2497         
2498         /* add / delete / name */
2499
2500         if(tot<16)
2501                 uiDefBut(block, BUT, B_ADD_THEME, "Add",        45,y2,200,20, NULL, 0, 0, 0, 0, "Makes new copy of this theme");
2502         if(tot>1 && isbuiltin==0)
2503                 uiDefBut(block, BUT, B_DEL_THEME, "Delete", 45,y1,200,20, NULL, 0, 0, 0, 0, "Delete theme");
2504
2505         if(isbuiltin) return;
2506         
2507         /* name */
2508         uiDefBut(block, TEX, B_NAME_THEME, "",                  255,y3,200,20, btheme->name, 1.0, 30.0, 0, 0, "Rename theme");
2509
2510         /* main choices pup */
2511         uiDefButS(block, MENU, B_CHANGE_THEME, "UI and Buttons %x1|%l|3D View %x2|%l|Ipo Curve Editor %x3|Action Editor %x4|"
2512                 "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|"
2513                 "Outliner %x11|Buttons Window %x12|%l|File Browser %x13|Image Browser %x14",
2514                                                                                                         255,y2,200,20, &curmain, 0, 0, 0, 0, "Specify theme for...");
2515         if(curmain==1) spacetype= 0;
2516         else if(curmain==2) spacetype= SPACE_VIEW3D;
2517         else if(curmain==3) spacetype= SPACE_IPO;
2518         else if(curmain==4) spacetype= SPACE_ACTION;
2519         else if(curmain==5) spacetype= SPACE_NLA;
2520         else if(curmain==6) spacetype= SPACE_IMAGE;
2521         else if(curmain==7) spacetype= SPACE_SEQ;
2522         else if(curmain==8) spacetype= SPACE_SOUND;
2523         else if(curmain==9) spacetype= SPACE_TEXT;
2524         else if(curmain==10) spacetype= SPACE_INFO;
2525         else if(curmain==11) spacetype= SPACE_OOPS;
2526         else if(curmain==12) spacetype= SPACE_BUTS;
2527         else if(curmain==13) spacetype= SPACE_FILE;
2528         else if(curmain==14) spacetype= SPACE_IMASEL;
2529         else if(curmain==15) spacetype= SPACE_TIME;
2530         else if(curmain==16) spacetype= SPACE_NODE;
2531         else return; /* only needed while coding... when adding themes for more windows */
2532         
2533         /* color choices pup */
2534         if(curmain==1) {
2535                 strp= BIF_ThemeColorsPup(0);
2536                 if(th_curcol==TH_BACK) th_curcol= TH_BUT_OUTLINE;  /* switching main choices... */
2537         }
2538         else strp= BIF_ThemeColorsPup(spacetype);
2539         
2540         uiDefButS(block, MENU, B_REDR, strp,                    255,y1,200,20, &th_curcol, 0, 0, 0, 0, "Current color");
2541         MEM_freeN(strp);
2542         
2543         th_curcol_ptr= col= BIF_ThemeGetColorPtr(btheme, spacetype, th_curcol);
2544         if(col==NULL) return;
2545         
2546         /* first handle exceptions, special single values, row selection, etc */
2547         if(th_curcol==TH_VERTEX_SIZE) {
2548                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Vertex size ", 465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2549         }
2550         else if(th_curcol==TH_FACEDOT_SIZE) {
2551                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Face dot size ",       465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2552         }
2553         else if(th_curcol==TH_BUT_DRAWTYPE) {
2554                 uiBlockBeginAlign(block);
2555                 uiDefButC(block, ROW, B_UPDATE_THEME, "Minimal", 465,y3,100,20,  col, 2.0, (float) TH_MINIMAL, 0, 0, "");
2556                 uiDefButC(block, ROW, B_UPDATE_THEME, "Shaded", 565,y3,100,20,  col, 2.0, (float) TH_SHADED, 0, 0, "");
2557                 uiDefButC(block, ROW, B_UPDATE_THEME, "Rounded", 465,y2,100,20,  col, 2.0, (float) TH_ROUNDED, 0, 0, "");
2558                 uiDefButC(block, ROW, B_UPDATE_THEME, "OldSkool", 565,y2,100,20,  col, 2.0, (float) TH_OLDSKOOL, 0, 0, "");
2559                 uiBlockEndAlign(block);
2560         }
2561         else {
2562                 uiBlockBeginAlign(block);
2563                 if ELEM8(th_curcol, TH_PANEL, TH_LAMP, TH_FACE, TH_FACE_SELECT, TH_MENU_BACK, TH_MENU_HILITE, TH_MENU_ITEM, TH_NODE) {
2564                         uiDefButC(block, NUMSLI, B_UPDATE_THEME,"A ",   465,y3+25,200,20,  col+3, 0.0, 255.0, B_THEMECOL, 0, "");
2565                 }
2566                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"R ",   465,y3,200,20,  col, 0.0, 255.0, B_THEMECOL, 0, "");
2567                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"G ",   465,y2,200,20,  col+1, 0.0, 255.0, B_THEMECOL, 0, "");
2568                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"B ",   465,y1,200,20,  col+2, 0.0, 255.0, B_THEMECOL, 0, "");
2569                 uiBlockEndAlign(block);
2570
2571                 uiDefButC(block, COL, B_UPDATE_THEME, "",               675,y1,50,y3-y1+20, col, 0, 0, 0, 0, "");
2572                                 
2573                 /* copy paste */
2574                 uiBlockBeginAlign(block);
2575                 uiDefBut(block, BUT, B_THEME_COPY, "Copy Color",        755,y2,120,20, NULL, 0, 0, 0, 0, "Stores current color in buffer");
2576                 uiDefBut(block, BUT, B_THEME_PASTE, "Paste Color",      755,y1,120,20, NULL, 0, 0, 0, 0, "Pastes buffer color");
2577                 uiBlockEndAlign(block);
2578                 
2579                 uiDefButC(block, COL, 0, "",                            885,y1,50,y2-y1+20, th_curcol_arr, 0, 0, 0, 0, "");
2580                 
2581         }
2582 }
2583
2584
2585 void drawinfospace(ScrArea *sa, void *spacedata)
2586 {
2587         uiBlock *block;
2588         static short cur_light=0, cur_light_var=0;
2589         float fac, col[3];
2590         short xpos, ypos, ypostab,  buth, rspace, dx, y1, y2, y3, y4, y5, y6;
2591         short y2label, y3label, y4label, y5label, y6label;
2592         short spref, mpref, lpref, smfileselbut;
2593         short edgsp, midsp;
2594         char naam[32];
2595
2596         if(curarea->win==0 || curarea->winy<2) return;
2597
2598         BIF_GetThemeColor3fv(TH_BACK, col);
2599         glClearColor(col[0], col[1], col[2], 0.0);
2600         glClear(GL_COLOR_BUFFER_BIT);
2601
2602         if(curarea->winx<=1280.0) {
2603                 fac= ((float)curarea->winx)/1280.0f;
2604                 myortho2(0.375f, 1280.375f, 0.375f, curarea->winy/fac + 0.375f);
2605         }
2606         else {
2607                 myortho2(0.375f, (float)curarea->winx + 0.375f, 0.375f, (float)curarea->winy + 0.375f);
2608         }
2609         
2610         sprintf(naam, "infowin %d", curarea->win);
2611         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->win);
2612
2613
2614         /* Vars for nice grid alignment */ 
2615         dx= (1280-90)/7;        /* spacing for use in equally dividing 'tab' row */
2616
2617         xpos = 45;              /* left padding */
2618         ypos = 50;              /* bottom padding for buttons */
2619         ypostab = 10;           /* bottom padding for 'tab' row */
2620
2621         buth = 20;              /* standard button height */
2622
2623         spref = 90;     /* standard size for small preferences button */
2624         mpref = 189;    /* standard size for medium preferences button */
2625         lpref = 288;    /* standard size for large preferences button */
2626         smfileselbut = buth;    /* standard size for fileselect button (square) */
2627
2628         edgsp = 3;              /* space from edge of end 'tab' to edge of end button */
2629         midsp = 9;              /* horizontal space between buttons */
2630
2631         rspace = 3;             /* default space between rows */
2632
2633         y1 = ypos;              /* grid alignment for each row of buttons */
2634         y2 = ypos+buth+rspace;
2635         y3 = ypos+2*(buth+rspace);
2636         y4 = ypos+3*(buth+rspace);
2637         y5 = ypos+4*(buth+rspace);
2638         y6 = ypos+5*(buth+rspace);
2639
2640
2641         y2label = y2-2;         /* adjustments to offset the labels down to align better */
2642         y3label = y3-2;
2643         y4label = y4-2;
2644         y5label = y5-2;
2645         y6label = y6-2;
2646
2647
2648         /* set the colour to blue and draw the main 'tab' controls */
2649
2650         uiBlockSetCol(block, TH_BUT_SETTING1);
2651         uiBlockBeginAlign(block);
2652         
2653         uiDefButS(block, ROW,B_USERPREF,"View & Controls",
2654                 xpos,ypostab,(short)dx,buth,
2655                 &U.userpref,1.0,0.0, 0, 0,"");
2656                 
2657         uiDefButS(block, ROW,B_USERPREF,"Edit Methods",
2658                 (short)(xpos+dx),ypostab,(short)dx,buth,
2659                 &U.userpref,1.0,1.0, 0, 0,"");
2660
2661         uiDefButS(block, ROW,B_USERPREF,"Language & Font",
2662                 (short)(xpos+2*dx),ypostab,(short)dx,buth,
2663                 &U.userpref,1.0,2.0, 0, 0,"");
2664
2665         uiDefButS(block, ROW,B_USERPREF,"Themes",
2666                 (short)(xpos+3*dx),ypostab,(short)dx,buth,
2667                 &U.userpref,1.0,6.0, 0, 0,"");
2668
2669         uiDefButS(block, ROW,B_USERPREF,"Auto Save",
2670                 (short)(xpos+4*dx),ypostab,(short)dx,buth,
2671                 &U.userpref,1.0,3.0, 0, 0,"");
2672
2673         uiDefButS(block, ROW,B_USERPREF,"System & OpenGL",
2674                 (short)(xpos+5*dx),ypostab,(short)dx,buth,
2675                 &U.userpref,1.0,4.0, 0, 0,"");
2676                 
2677         uiDefButS(block, ROW,B_USERPREF,"File Paths",
2678                 (short)(xpos+6*dx),ypostab,(short)dx,buth,
2679                 &U.userpref,1.0,5.0, 0, 0,"");
2680
2681         uiBlockSetCol(block, TH_AUTO);
2682         uiBlockEndAlign(block);
2683         /* end 'tab' controls */
2684
2685         /* line 2: left x co-ord, top y co-ord, width, height */
2686
2687         if(U.userpref == 6) {
2688                 info_user_themebuts(block, y1, y2, y3);
2689         }
2690         else if (U.userpref == 0) { /* view & controls */
2691
2692                 uiDefBut(block, LABEL,0,"Display:",
2693                         xpos,y6label,spref,buth,
2694                         0, 0, 0, 0, 0, "");     
2695                 uiBlockBeginAlign(block);
2696                 uiDefButBitI(block, TOG, USER_TOOLTIPS, 0, "Tool Tips",
2697                         (xpos+edgsp),y5,spref,buth,
2698                         &(U.flag), 0, 0, 0, 0,
2699                         "Display tooltips (help tags) over buttons");
2700                 uiDefButBitI(block, TOG, USER_DRAWVIEWINFO, B_DRAWINFO, "Object Info",
2701                         (xpos+edgsp),y4,spref,buth,
2702                         &(U.uiflag), 0, 0, 0, 0,
2703                         "Display active object name and frame number in the 3D View");
2704                 uiDefButBitI(block, TOG, USER_SCENEGLOBAL, 0, "Global Scene",
2705                         (xpos+edgsp),y3,spref,buth,
2706                         &(U.flag), 0, 0, 0, 0,
2707                         "Forces the current Scene to be displayed in all Screens");
2708 #ifndef __APPLE__       
2709                 uiDefButBitS(block, TOG, 1, 0, "Large Cursors",
2710                         (xpos+edgsp),y2,spref,buth,
2711                         &(U.curssize), 0, 0, 0, 0,
2712                         "Use large mouse cursors when available");
2713 #else 
2714                 U.curssize=0; /*Small Cursor always for OS X for now */
2715 #endif
2716                 uiDefButBitI(block, TOG, USER_SHOW_VIEWPORTNAME, B_DRAWINFO, "View Name",
2717                         (xpos+edgsp),y1,spref,buth,
2718                         &(U.uiflag), 0, 0, 0, 0,
2719                         "Show the name of the view's direction in each 3D View");
2720                 uiBlockEndAlign(block);
2721
2722                 uiDefBut(block, LABEL,0,"Menus:",
2723                         (xpos+(2*edgsp)+spref),y6label,spref,buth,
2724                         0, 0, 0, 0, 0, "");
2725                 uiBlockBeginAlign(block);
2726                 uiDefButBitI(block, TOG, USER_MENUOPENAUTO, 0, "Open on Mouse Over",
2727                         (xpos+edgsp+spref+midsp),y5,mpref,buth,
2728                         &(U.uiflag), 0, 0, 0, 0,
2729                         "Open menu buttons and pulldowns automatically when the mouse is hovering");
2730                 uiDefButS(block, NUM, 0, "Top Level:",
2731                         (xpos+edgsp+spref+midsp),y4,spref+edgsp,buth,
2732                         &(U.menuthreshold1), 1, 40, 0, 0,
2733                         "Time delay in 1/10 seconds before automatically opening top level menus");
2734                 uiDefButS(block, NUM, 0, "Sublevels:",
2735                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y4,spref+edgsp,buth,
2736                         &(U.menuthreshold2), 1, 40, 0,