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