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