99ec49961c5c90511e0385f4184656e96446321d
[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
1741                                                         initTransform(TFM_SHRINKFATTEN, CTX_NONE);
1742                                                 Transform();
1743                                         }
1744                                         else if(G.qual==LR_CTRLKEY) {
1745                                                 initTransform(TFM_SHEAR, CTX_NONE);
1746                                                 Transform();
1747                                         }
1748                                         else if(G.qual==LR_SHIFTKEY)
1749                                                 snapmenu();
1750                                         else if(G.qual==0) {
1751                                                 if(G.obedit->type==OB_ARMATURE) {
1752                                                         bArmature *arm= G.obedit->data;
1753                                                         if(arm->drawtype==ARM_ENVELOPE)
1754                                                                 initTransform(TFM_BONE_ENVELOPE, CTX_NONE);
1755                                                         else
1756                                                                 initTransform(TFM_RESIZE, CTX_NONE);
1757                                                 }
1758                                                 else
1759                                                         initTransform(TFM_RESIZE, CTX_NONE);
1760                                                 Transform();
1761                                         }
1762                                         else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)){
1763                                                 initTransform(TFM_TOSPHERE, CTX_NONE);
1764                                                 Transform();
1765                                         }
1766                                         if ( G.qual == (LR_SHIFTKEY | LR_ALTKEY | LR_CTRLKEY) ) {
1767                                                 if(G.obedit->type==OB_MESH) select_sharp_edges();
1768                                         }
1769                                 }
1770                                 else if(G.qual==LR_ALTKEY) {
1771                                         if(G.f & G_WEIGHTPAINT)
1772                                                 ob= ob->parent;
1773                                         if(ob && (ob->flag & OB_POSEMODE)) {
1774                                                 bArmature *arm= ob->data;
1775                                                 if( ELEM(arm->drawtype, ARM_B_BONE, ARM_ENVELOPE)) {
1776                                                         initTransform(TFM_BONESIZE, CTX_NONE);
1777                                                         Transform();
1778                                                         break;
1779                                                 }
1780                                         }
1781                                         
1782                                         if(okee("Clear scale")) {
1783                                                 clear_object('s');
1784                                         }
1785                                 }
1786                                 else if(G.qual==LR_SHIFTKEY) {
1787                                         snapmenu();
1788                                 }
1789                                 else if((G.qual==0)) {
1790                                         initTransform(TFM_RESIZE, CTX_NONE);
1791                                         Transform();
1792                                 }
1793                                 else if(G.qual==(LR_SHIFTKEY|LR_CTRLKEY)) {
1794                                         initTransform(TFM_TOSPHERE, CTX_NONE);
1795                                         Transform();
1796                                 }
1797                                 else if(G.qual==(LR_CTRLKEY|LR_ALTKEY|LR_SHIFTKEY)) {
1798                                         initTransform(TFM_SHEAR, CTX_NONE);
1799                                         Transform();
1800                                 }
1801                                 break;
1802                         case TKEY:
1803                                 if(G.obedit){
1804                                         if((G.qual & LR_CTRLKEY) && G.obedit->type==OB_MESH) {
1805                                                 convert_to_triface(G.qual & LR_SHIFTKEY);
1806                                                 allqueue(REDRAWVIEW3D, 0);
1807                                                 countall();
1808                                                 DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1809                                         }
1810                                         if (G.obedit->type==OB_CURVE) {
1811                                                 if (G.qual==LR_ALTKEY) {
1812                                                         clear_tilt();
1813                                                 }
1814                                                 else if (G.qual==0) {
1815                                                         initTransform(TFM_TILT, CTX_NONE);
1816                                                         Transform();
1817                                                 }
1818                                         }
1819                                 }
1820                                 else if(G.qual==LR_CTRLKEY) {
1821                                         if(ob && (ob->flag & OB_POSEMODE));
1822                                         else make_track();
1823                                 }
1824                                 else if(G.qual==LR_ALTKEY) {
1825                                         if(ob && (ob->flag & OB_POSEMODE));
1826                                         else clear_track();
1827                                 }
1828                                 else if((G.qual==0)){
1829                                         texspace_edit();
1830                                 }
1831                                 
1832                                 break;
1833                         case UKEY:
1834                                 if(G.obedit) {
1835                                         if(G.obedit->type==OB_MESH) {
1836                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1837                                         }
1838                                         else if ELEM5(G.obedit->type, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
1839                                                 if(G.qual==0) BIF_undo(); else BIF_redo();
1840                                         }
1841                                 }
1842                                 else if((G.qual==0)) {
1843                                         if(G.f & G_WEIGHTPAINT)
1844                                                 wpaint_undo();
1845                                         else if(G.f & G_VERTEXPAINT)
1846                                                 vpaint_undo();
1847                                         else if (G.f & G_FACESELECT)
1848                                                 uv_autocalc_tface();
1849                                         else {
1850                                                 single_user();
1851                                         }
1852                                 }
1853                                         
1854                                 break;
1855                         case VKEY:
1856                                 if((G.qual==LR_SHIFTKEY)) {
1857                                         if ((G.obedit) && G.obedit->type==OB_MESH) {
1858                                                 align_view_to_selected(v3d);
1859                                         }
1860                                         else if (G.f & G_FACESELECT) {
1861                                                 align_view_to_selected(v3d);
1862                                         }
1863                                 }
1864                                 else if(G.qual==LR_ALTKEY)
1865                                         image_aspect();
1866                                 else if (G.qual==0){
1867                                         if(G.obedit) {
1868                                                 if(G.obedit->type==OB_MESH) {
1869                                                         mesh_rip();
1870                                                 }
1871                                                 else if(G.obedit->type==OB_CURVE) {
1872                                                         sethandlesNurb(2);
1873                                                         DAG_object_flush_update(G.scene, G.obedit, OB_RECALC_DATA);
1874                                                         allqueue(REDRAWVIEW3D, 0);
1875                                                         BIF_undo_push("Handle change");
1876                                                 }
1877                                         }
1878                                         else if(ob && ob->type == OB_MESH) 
1879                                                 set_vpaint();
1880                                 }
1881                                 break;
1882                         case WKEY:
1883                                 if((G.qual==LR_SHIFTKEY)) {
1884                                         initTransform(TFM_WARP, CTX_NONE);
1885                                         Transform();
1886                                 }
1887                                 else if(G.qual==LR_ALTKEY) {
1888                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
1889                                 }
1890                                 else if(G.qual==LR_CTRLKEY) {
1891                                         if(G.obedit) {
1892                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
1893                                                         switchdirectionNurb2();
1894                                                 }
1895                                         }
1896                                 }
1897                                 else if((G.qual==0))
1898                                         special_editmenu();
1899                                 
1900                                 break;
1901                         case XKEY:
1902                         case DELKEY:
1903                                 if(G.qual==0)
1904                                         delete_context_selected();
1905                                 break;
1906                         case YKEY:
1907                                 if((G.qual==0) && (G.obedit)) {
1908                                         if(G.obedit->type==OB_MESH) split_mesh();
1909                                 }
1910                                 break;
1911                         case ZKEY:
1912                                 toggle_shading();
1913                                 
1914                                 scrarea_queue_headredraw(curarea);
1915                                 scrarea_queue_winredraw(curarea);
1916                                 break;
1917                         
1918                         case HOMEKEY:
1919                                 if(G.qual==0)
1920                                         view3d_home(0);
1921                                 break;
1922                         case COMMAKEY:
1923                                 if(G.qual==LR_CTRLKEY) {
1924                                         G.vd->around= V3D_CENTROID;
1925                                 } else if(G.qual==LR_SHIFTKEY) {
1926                                         G.vd->around= V3D_CENTROID;
1927                                 } else if(G.qual==0) {
1928                                         G.vd->around= V3D_CENTRE;
1929                                 }
1930                                 handle_view3d_around();
1931                                 
1932                                 scrarea_queue_headredraw(curarea);
1933                                 scrarea_queue_winredraw(curarea);
1934                                 break;
1935                                 
1936                         case PERIODKEY:
1937                                 if(G.qual==LR_CTRLKEY) {
1938                                         G.vd->around= V3D_LOCAL;
1939                                 }       else if(G.qual==0) {
1940                                         G.vd->around= V3D_CURSOR;
1941                                 }
1942                                 handle_view3d_around();
1943                                 
1944                                 scrarea_queue_headredraw(curarea);
1945                                 scrarea_queue_winredraw(curarea);
1946                                 break;
1947                         
1948                         case PADSLASHKEY:
1949                                 if(G.qual==0) {
1950                                         if(G.vd->localview) {
1951                                                 G.vd->localview= 0;
1952                                                 endlocalview(curarea);
1953                                         }
1954                                         else {
1955                                                 G.vd->localview= 1;
1956                                                 initlocalview();
1957                                         }
1958                                         scrarea_queue_headredraw(curarea);
1959                                 }
1960                                 break;
1961                         case PADASTERKEY:       /* '*' */
1962                                 if(G.qual==0) {
1963                                         if(ob) {
1964                                                 if ((G.obedit) && (G.obedit->type == OB_MESH)) {
1965                                                         editmesh_align_view_to_selected(G.vd, 2);
1966                                                 } 
1967                                                 else if (G.f & G_FACESELECT) {
1968                                                         if(ob->type==OB_MESH) {
1969                                                                 Mesh *me= ob->data;
1970                                                                 faceselect_align_view_to_selected(G.vd, me, 2);
1971                                                         }
1972                                                 }
1973                                                 else
1974                                                         obmat_to_viewmat(ob);
1975                                                 
1976                                                 if(G.vd->persp==2) G.vd->persp= 1;
1977                                                 scrarea_queue_winredraw(curarea);
1978                                         }
1979                                 }
1980                                 break;
1981                         case PADPERIOD: /* '.' */
1982                                 if(G.qual==0)
1983                                         centreview();
1984                                 break;
1985                         
1986                         case PAGEUPKEY:
1987                                 if(G.qual==LR_CTRLKEY)
1988                                         movekey_obipo(1);
1989                                 else if((G.qual==0))
1990                                         nextkey_obipo(1);       /* in editipo.c */
1991                                 break;
1992
1993                         case PAGEDOWNKEY:
1994                                 if(G.qual==LR_CTRLKEY)
1995                                         movekey_obipo(-1);
1996                                 else if((G.qual==0))
1997                                         nextkey_obipo(-1);
1998                                 break;
1999                                 
2000                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
2001                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
2002                         case PADENTER:
2003                                 persptoetsen(event);
2004                                 doredraw= 1;
2005                                 break;
2006                         case PADMINUS:
2007                                 if ( (G.qual==LR_CTRLKEY)
2008                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2009                                         select_less();
2010                                 else {
2011                                         persptoetsen(event);
2012                                         doredraw= 1;
2013                                 }
2014                                 break;
2015
2016                         case PADPLUSKEY:
2017                                 if ( (G.qual==LR_CTRLKEY)
2018                                          && (G.obedit) && (G.obedit->type==OB_MESH) )
2019                                         select_more();
2020                                 else {
2021                                         persptoetsen(event);
2022                                         doredraw= 1;
2023                                 }
2024                                 break;
2025
2026                         case ESCKEY:
2027                                 if(G.qual==0) {
2028                                         if (G.vd->flag & V3D_DISPIMAGE) {
2029                                                 G.vd->flag &= ~V3D_DISPIMAGE;
2030                                                 doredraw= 1;
2031                                         }
2032                                 }
2033                                 break;
2034                         }
2035                 }
2036         }
2037         
2038         if(doredraw) {
2039                 scrarea_queue_winredraw(curarea);
2040                 scrarea_queue_headredraw(curarea);
2041         }
2042 }
2043
2044 static void initview3d(ScrArea *sa)
2045 {
2046         View3D *vd;
2047         
2048         vd= MEM_callocN(sizeof(View3D), "initview3d");
2049         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
2050
2051         vd->spacetype= SPACE_VIEW3D;
2052         vd->blockscale= 0.7f;
2053         vd->viewquat[0]= 1.0f;
2054         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0f;
2055         vd->persp= 1;
2056         vd->drawtype= OB_WIRE;
2057         vd->view= 7;
2058         vd->dist= 10.0;
2059         vd->lens= 35.0f;
2060         vd->near= 0.01f;
2061         vd->far= 500.0f;
2062         vd->grid= 1.0f;
2063         vd->gridlines= 16;
2064         vd->lay= vd->layact= 1;
2065         if(G.scene) {
2066                 vd->lay= vd->layact= G.scene->lay;
2067                 vd->camera= G.scene->camera;
2068         }
2069         vd->scenelock= 1;
2070         vd->gridflag |= V3D_SHOW_X;
2071         vd->gridflag |= V3D_SHOW_Y;
2072         vd->gridflag |= V3D_SHOW_FLOOR;
2073         vd->gridflag &= ~V3D_SHOW_Z;
2074 }
2075
2076
2077 /* ******************** SPACE: IPO ********************** */
2078
2079 static void changeview2dspace(ScrArea *sa, void *spacedata)
2080 {
2081         if(G.v2d==0) return;
2082
2083         test_view2d(G.v2d, curarea->winx, curarea->winy);
2084         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
2085 }
2086
2087 static void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2088 {
2089         extern void do_ipobuts(unsigned short event);   /* drawipo.c */
2090         unsigned short event= evt->event;
2091         short val= evt->val;
2092         SpaceIpo *sipo= curarea->spacedata.first;
2093         View2D *v2d= &sipo->v2d;
2094         float dx, dy;
2095         int cfra, doredraw= 0;
2096         short mval[2];
2097         short mousebut = L_MOUSE;
2098         
2099         if(sa->win==0) return;
2100
2101         if(val) {
2102                 if( uiDoBlocks(&sa->uiblocks, event)!=UI_NOTHING ) event= 0;
2103
2104                 /* swap mouse buttons based on user preference */
2105                 if (U.flag & USER_LMOUSESELECT) {
2106                         if (event == LEFTMOUSE) {
2107                                 event = RIGHTMOUSE;
2108                                 mousebut = L_MOUSE;
2109                         } else if (event == RIGHTMOUSE) {
2110                                 event = LEFTMOUSE;
2111                                 mousebut = R_MOUSE;
2112                         }
2113                 }
2114
2115                 switch(event) {
2116                 case UI_BUT_EVENT:
2117                         /* note: bad bad code, will be cleaned! is because event queues are all shattered */
2118                         if(val>0 && val < 256) do_ipowin_buts(val-1);
2119                         else do_ipobuts(val);
2120                         break;
2121                         
2122                 case LEFTMOUSE:
2123                         if( in_ipo_buttons() ) {
2124                                 do_ipo_selectbuttons();
2125                                 doredraw= 1;
2126                         }
2127                         else if(view2dmove(LEFTMOUSE)); /* only checks for sliders */
2128                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
2129                         else {
2130                                 do {
2131                                         getmouseco_areawin(mval);
2132                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
2133                                         
2134                                         cfra= (int)dx;
2135                                         if(cfra< 1) cfra= 1;
2136                                         
2137                                         if( cfra!=CFRA ) {
2138                                                 CFRA= cfra;
2139                                                 update_for_newframe_nodraw(0);  /* 1 = nosound */
2140                                                 force_draw_all(0); /* To make constraint sliders redraw */
2141                                         }
2142                                         else PIL_sleep_ms(30);
2143                                 
2144                                 } while(get_mbut() & mousebut);
2145                         }
2146                         break;
2147                 case RIGHTMOUSE:
2148                         mouse_select_ipo();
2149                         allqueue (REDRAWACTION, 0);
2150                         allqueue(REDRAWNLA, 0);
2151                         break;
2152                 case MIDDLEMOUSE:
2153                         if(in_ipo_buttons()) {
2154                                 scroll_ipobuts();
2155                         }
2156                         else view2dmove(event); /* in drawipo.c */
2157                         break;
2158                 case WHEELUPMOUSE:
2159                 case WHEELDOWNMOUSE:
2160                         view2dmove(event);      /* in drawipo.c */
2161                         break;
2162                 case PADPLUSKEY:
2163                         view2d_zoom(v2d, 0.1154f, sa->winx, sa->winy);
2164                         doredraw= 1;
2165                         break;
2166                 case PADMINUS:
2167                         view2d_zoom(v2d, -0.15f, sa->winx, sa->winy);
2168                         doredraw= 1;
2169                         break;
2170                 case PAGEUPKEY:
2171                         if(G.qual==LR_CTRLKEY)
2172                                 movekey_ipo(1);
2173                         else if((G.qual==0))
2174                                 nextkey_ipo(1);
2175                         break;
2176                 case PAGEDOWNKEY:
2177                         if(G.qual==LR_CTRLKEY)
2178                                 movekey_ipo(-1);
2179                         else if((G.qual==0))
2180                                 nextkey_ipo(-1);
2181                         break;
2182                 case HOMEKEY:
2183                         if((G.qual==0))
2184                                 do_ipo_buttons(B_IPOHOME);
2185                         break;
2186                         
2187                 case AKEY:
2188                         if((G.qual==0)) {
2189                                 if(in_ipo_buttons()) {
2190                                         swap_visible_editipo();
2191                                 }
2192                                 else {
2193                                         swap_selectall_editipo();
2194                                 }
2195                                 allspace (REMAKEIPO, 0);
2196                                 allqueue (REDRAWNLA, 0);
2197                                 allqueue (REDRAWACTION, 0);
2198                         }
2199                         break;
2200                 case BKEY:
2201                         if((G.qual==0))
2202                                 borderselect_ipo();
2203                         break;
2204                 case CKEY:
2205                         if((G.qual==0))
2206                                 move_to_frame();
2207                         break;
2208                 case DKEY:
2209                         if((G.qual==LR_SHIFTKEY))
2210                                 add_duplicate_editipo();
2211                         break;
2212                 case GKEY:
2213                         if((G.qual==0))
2214                                 transform_ipo('g');
2215                         break;
2216                 case HKEY:
2217                         if(G.qual==LR_ALTKEY)
2218                                 sethandles_ipo(HD_AUTO_ANIM);
2219                         if(G.qual==LR_SHIFTKEY)
2220                                 sethandles_ipo(HD_AUTO);
2221                         else if(G.qual==0)
2222                                 sethandles_ipo(HD_ALIGN);
2223                         break;
2224                 case JKEY:
2225                         if((G.qual==0))
2226                                 join_ipo_menu();
2227                         break;
2228                 case KKEY:
2229                         if((G.qual==0)) {
2230                                 ipo_toggle_showkey();
2231                                 scrarea_queue_headredraw(curarea);
2232                                 allqueue(REDRAWVIEW3D, 0);
2233                                 doredraw= 1;
2234                         }
2235                         break;
2236                 case NKEY:
2237                         toggle_blockhandler(sa, IPO_HANDLER_PROPERTIES, UI_PNL_TO_MOUSE);
2238                         doredraw= 1;
2239                         break;
2240                 case RKEY:
2241                         if((G.qual==0))
2242                                 ipo_record();
2243                         break;
2244                 case SKEY:
2245                         if((G.qual==LR_SHIFTKEY)) {             
2246                                 ipo_snap_menu();
2247                         } else if((G.qual==0))
2248                                 transform_ipo('s');
2249                         break;
2250                 case TKEY:
2251                         if((G.qual==0))
2252                                 set_ipotype();
2253                         break;
2254                 case VKEY:
2255                         if((G.qual==0))
2256                                 sethandles_ipo(HD_VECT);
2257                         break;
2258                 case XKEY:
2259                 case DELKEY:
2260                         del_ipo();
2261                         break;
2262                 }
2263         }
2264
2265         if(doredraw) scrarea_queue_winredraw(sa);
2266 }
2267
2268 void initipo(ScrArea *sa)
2269 {
2270         SpaceIpo *sipo;
2271         
2272         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
2273         BLI_addhead(&sa->spacedata, sipo);
2274
2275         sipo->spacetype= SPACE_IPO;
2276         sipo->blockscale= 0.7f;
2277         
2278         /* sipo space loopt van (0,-?) tot (??,?) */
2279         sipo->v2d.tot.xmin= 0.0;
2280         sipo->v2d.tot.ymin= -10.0;
2281         sipo->v2d.tot.xmax= G.scene->r.efra;
2282         sipo->v2d.tot.ymax= 10.0;
2283
2284         sipo->v2d.cur= sipo->v2d.tot;
2285
2286         sipo->v2d.min[0]= 0.01f;
2287         sipo->v2d.min[1]= 0.01f;
2288
2289         sipo->v2d.max[0]= 15000.0f;
2290         sipo->v2d.max[1]= 10000.0f;
2291         
2292         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
2293         sipo->v2d.keeptot= 0;
2294
2295         sipo->blocktype= ID_OB;
2296 }
2297
2298 /* ******************** SPACE: INFO ********************** */
2299
2300 void space_mipmap_button_function(int event) {
2301         set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
2302
2303         allqueue(REDRAWVIEW3D, 0);
2304 }
2305
2306 #if 0
2307 static void space_sound_button_function(int event)
2308 {
2309         int a;
2310         SYS_SystemHandle syshandle;
2311
2312         if ((syshandle = SYS_GetSystem()))
2313         {
2314                 a = (U.gameflags & USER_DISABLE_SOUND);
2315                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
2316         }
2317 }
2318 #endif
2319
2320 /* needed for event; choose new 'curmain' resets it... */
2321 static short th_curcol= TH_BACK;
2322 static char *th_curcol_ptr= NULL;
2323 static char th_curcol_arr[4]={0, 0, 0, 255};
2324
2325 static void info_user_themebuts(uiBlock *block, short y1, short y2, short y3)
2326 {
2327         bTheme *btheme, *bt;
2328         int spacetype= 0;
2329         static short cur=1, curmain=2;
2330         short a, tot=0, isbuiltin= 0;
2331         char string[21*32], *strp, *col;
2332         
2333         y3= y2+23;      /* exception! */
2334         
2335         /* count total, max 16! */
2336         for(bt= U.themes.first; bt; bt= bt->next) tot++;
2337         
2338         /* if cur not is 1; move that to front of list */
2339         if(cur!=1) {
2340                 a= 1;
2341                 for(bt= U.themes.first; bt; bt= bt->next, a++) {
2342                         if(a==cur) {
2343                                 BLI_remlink(&U.themes, bt);
2344                                 BLI_addhead(&U.themes, bt);
2345                                 allqueue(REDRAWALL, 0);
2346                                 cur= 1;
2347                                 break;
2348                         }
2349                 }
2350         }
2351         
2352         /* the current theme */
2353         btheme= U.themes.first;
2354         if(strcmp(btheme->name, "Default")==0) isbuiltin= 1;
2355
2356         /* construct popup script */
2357         string[0]= 0;
2358         for(bt= U.themes.first; bt; bt= bt->next) {
2359                 strcat(string, bt->name);
2360                 if(btheme->next) strcat(string, "   |");
2361         }
2362         uiDefButS(block, MENU, B_UPDATE_THEME, string,                  45,y3,200,20, &cur, 0, 0, 0, 0, "Current theme");
2363         
2364         /* add / delete / name */
2365
2366         if(tot<16)
2367                 uiDefBut(block, BUT, B_ADD_THEME, "Add",        45,y2,200,20, NULL, 0, 0, 0, 0, "Makes new copy of this theme");
2368         if(tot>1 && isbuiltin==0)
2369                 uiDefBut(block, BUT, B_DEL_THEME, "Delete", 45,y1,200,20, NULL, 0, 0, 0, 0, "Delete theme");
2370
2371         if(isbuiltin) return;
2372         
2373         /* name */
2374         uiDefBut(block, TEX, B_NAME_THEME, "",                  255,y3,200,20, btheme->name, 1.0, 30.0, 0, 0, "Rename theme");
2375
2376         /* main choices pup */
2377         uiDefButS(block, MENU, B_CHANGE_THEME, "UI and Buttons %x1|%l|3D View %x2|%l|Ipo Curve Editor %x3|Action Editor %x4|"
2378                 "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|"
2379                 "Outliner %x11|Buttons Window %x12|%l|File Browser %x13|Image Browser %x14",
2380                                                                                                         255,y2,200,20, &curmain, 0, 0, 0, 0, "Specify theme for...");
2381         if(curmain==1) spacetype= 0;
2382         else if(curmain==2) spacetype= SPACE_VIEW3D;
2383         else if(curmain==3) spacetype= SPACE_IPO;
2384         else if(curmain==4) spacetype= SPACE_ACTION;
2385         else if(curmain==5) spacetype= SPACE_NLA;
2386         else if(curmain==6) spacetype= SPACE_IMAGE;
2387         else if(curmain==7) spacetype= SPACE_SEQ;
2388         else if(curmain==8) spacetype= SPACE_SOUND;
2389         else if(curmain==9) spacetype= SPACE_TEXT;
2390         else if(curmain==10) spacetype= SPACE_INFO;
2391         else if(curmain==11) spacetype= SPACE_OOPS;
2392         else if(curmain==12) spacetype= SPACE_BUTS;
2393         else if(curmain==13) spacetype= SPACE_FILE;
2394         else if(curmain==14) spacetype= SPACE_IMASEL;
2395         else if(curmain==15) spacetype= SPACE_TIME;
2396         else if(curmain==16) spacetype= SPACE_NODE;
2397         else return; /* only needed while coding... when adding themes for more windows */
2398         
2399         /* color choices pup */
2400         if(curmain==1) {
2401                 strp= BIF_ThemeColorsPup(0);
2402                 if(th_curcol==TH_BACK) th_curcol= TH_BUT_OUTLINE;  /* switching main choices... */
2403         }
2404         else strp= BIF_ThemeColorsPup(spacetype);
2405         
2406         uiDefButS(block, MENU, B_REDR, strp,                    255,y1,200,20, &th_curcol, 0, 0, 0, 0, "Current color");
2407         MEM_freeN(strp);
2408         
2409         th_curcol_ptr= col= BIF_ThemeGetColorPtr(btheme, spacetype, th_curcol);
2410         if(col==NULL) return;
2411         
2412         /* first handle exceptions, special single values, row selection, etc */
2413         if(th_curcol==TH_VERTEX_SIZE) {
2414                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Vertex size ", 465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2415         }
2416         else if(th_curcol==TH_FACEDOT_SIZE) {
2417                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"Face dot size ",       465,y3,200,20,  col, 1.0, 10.0, 0, 0, "");
2418         }
2419         else if(th_curcol==TH_BUT_DRAWTYPE) {
2420                 uiBlockBeginAlign(block);
2421                 uiDefButC(block, ROW, B_UPDATE_THEME, "Minimal",        465,y3,100,20,  col, 2.0, 0.0, 0, 0, "");
2422                 uiDefButC(block, ROW, B_UPDATE_THEME, "Shaded", 565,y3,100,20,  col, 2.0, 1.0, 0, 0, "");
2423                 uiDefButC(block, ROW, B_UPDATE_THEME, "Rounded",        465,y2,100,20,  col, 2.0, 2.0, 0, 0, "");
2424                 uiDefButC(block, ROW, B_UPDATE_THEME, "OldSkool",       565,y2,100,20,  col, 2.0, 3.0, 0, 0, "");
2425                 uiBlockEndAlign(block);
2426         }
2427         else {
2428                 uiBlockBeginAlign(block);
2429                 if ELEM8(th_curcol, TH_PANEL, TH_LAMP, TH_FACE, TH_FACE_SELECT, TH_MENU_BACK, TH_MENU_HILITE, TH_MENU_ITEM, TH_NODE) {
2430                         uiDefButC(block, NUMSLI, B_UPDATE_THEME,"A ",   465,y3+25,200,20,  col+3, 0.0, 255.0, B_THEMECOL, 0, "");
2431                 }
2432                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"R ",   465,y3,200,20,  col, 0.0, 255.0, B_THEMECOL, 0, "");
2433                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"G ",   465,y2,200,20,  col+1, 0.0, 255.0, B_THEMECOL, 0, "");
2434                 uiDefButC(block, NUMSLI, B_UPDATE_THEME,"B ",   465,y1,200,20,  col+2, 0.0, 255.0, B_THEMECOL, 0, "");
2435                 uiBlockEndAlign(block);
2436
2437                 uiDefButC(block, COL, B_UPDATE_THEME, "",               675,y1,50,y3-y1+20, col, 0, 0, 0, 0, "");
2438                                 
2439                 /* copy paste */
2440                 uiBlockBeginAlign(block);
2441                 uiDefBut(block, BUT, B_THEME_COPY, "Copy Color",        755,y2,120,20, NULL, 0, 0, 0, 0, "Stores current color in buffer");
2442                 uiDefBut(block, BUT, B_THEME_PASTE, "Paste Color",      755,y1,120,20, NULL, 0, 0, 0, 0, "Pastes buffer color");
2443                 uiBlockEndAlign(block);
2444                 
2445                 uiDefButC(block, COL, 0, "",                            885,y1,50,y2-y1+20, th_curcol_arr, 0, 0, 0, 0, "");
2446                 
2447         }
2448 }
2449
2450
2451 void drawinfospace(ScrArea *sa, void *spacedata)
2452 {
2453         uiBlock *block;
2454         static short cur_light=0, cur_light_var=0;
2455         float fac, col[3];
2456         short xpos, ypos, ypostab,  buth, rspace, dx, y1, y2, y3, y4, y5, y6;
2457         short y2label, y3label, y4label, y5label, y6label;
2458         short spref, mpref, lpref, smfileselbut;
2459         short edgsp, midsp;
2460         char naam[32];
2461
2462         if(curarea->win==0 || curarea->winy<2) return;
2463
2464         BIF_GetThemeColor3fv(TH_BACK, col);
2465         glClearColor(col[0], col[1], col[2], 0.0);
2466         glClear(GL_COLOR_BUFFER_BIT);
2467
2468         if(curarea->winx<=1280.0) {
2469                 fac= ((float)curarea->winx)/1280.0f;
2470                 myortho2(0.375f, 1280.375f, 0.375f, curarea->winy/fac + 0.375f);
2471         }
2472         else {
2473                 myortho2(0.375f, (float)curarea->winx + 0.375f, 0.375f, (float)curarea->winy + 0.375f);
2474         }
2475         
2476         sprintf(naam, "infowin %d", curarea->win);
2477         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSS, UI_HELV, curarea->win);
2478
2479
2480         /* Vars for nice grid alignment */ 
2481         dx= (1280-90)/7;        /* spacing for use in equally dividing 'tab' row */
2482
2483         xpos = 45;              /* left padding */
2484         ypos = 50;              /* bottom padding for buttons */
2485         ypostab = 10;           /* bottom padding for 'tab' row */
2486
2487         buth = 20;              /* standard button height */
2488
2489         spref = 90;     /* standard size for small preferences button */
2490         mpref = 189;    /* standard size for medium preferences button */
2491         lpref = 288;    /* standard size for large preferences button */
2492         smfileselbut = buth;    /* standard size for fileselect button (square) */
2493
2494         edgsp = 3;              /* space from edge of end 'tab' to edge of end button */
2495         midsp = 9;              /* horizontal space between buttons */
2496
2497         rspace = 3;             /* default space between rows */
2498
2499         y1 = ypos;              /* grid alignment for each row of buttons */
2500         y2 = ypos+buth+rspace;
2501         y3 = ypos+2*(buth+rspace);
2502         y4 = ypos+3*(buth+rspace);
2503         y5 = ypos+4*(buth+rspace);
2504         y6 = ypos+5*(buth+rspace);
2505
2506
2507         y2label = y2-2;         /* adjustments to offset the labels down to align better */
2508         y3label = y3-2;
2509         y4label = y4-2;
2510         y5label = y5-2;
2511         y6label = y6-2;
2512
2513
2514         /* set the colour to blue and draw the main 'tab' controls */
2515
2516         uiBlockSetCol(block, TH_BUT_SETTING1);
2517         uiBlockBeginAlign(block);
2518         
2519         uiDefButS(block, ROW,B_USERPREF,"View & Controls",
2520                 xpos,ypostab,(short)dx,buth,
2521                 &U.userpref,1.0,0.0, 0, 0,"");
2522                 
2523         uiDefButS(block, ROW,B_USERPREF,"Edit Methods",
2524                 (short)(xpos+dx),ypostab,(short)dx,buth,
2525                 &U.userpref,1.0,1.0, 0, 0,"");
2526
2527         uiDefButS(block, ROW,B_USERPREF,"Language & Font",
2528                 (short)(xpos+2*dx),ypostab,(short)dx,buth,
2529                 &U.userpref,1.0,2.0, 0, 0,"");
2530
2531         uiDefButS(block, ROW,B_USERPREF,"Themes",
2532                 (short)(xpos+3*dx),ypostab,(short)dx,buth,
2533                 &U.userpref,1.0,6.0, 0, 0,"");
2534
2535         uiDefButS(block, ROW,B_USERPREF,"Auto Save",
2536                 (short)(xpos+4*dx),ypostab,(short)dx,buth,
2537                 &U.userpref,1.0,3.0, 0, 0,"");
2538
2539         uiDefButS(block, ROW,B_USERPREF,"System & OpenGL",
2540                 (short)(xpos+5*dx),ypostab,(short)dx,buth,
2541                 &U.userpref,1.0,4.0, 0, 0,"");
2542                 
2543         uiDefButS(block, ROW,B_USERPREF,"File Paths",
2544                 (short)(xpos+6*dx),ypostab,(short)dx,buth,
2545                 &U.userpref,1.0,5.0, 0, 0,"");
2546
2547         uiBlockSetCol(block, TH_AUTO);
2548         uiBlockEndAlign(block);
2549         /* end 'tab' controls */
2550
2551         /* line 2: left x co-ord, top y co-ord, width, height */
2552
2553         if(U.userpref == 6) {
2554                 info_user_themebuts(block, y1, y2, y3);
2555         }
2556         else if (U.userpref == 0) { /* view & controls */
2557
2558                 uiDefBut(block, LABEL,0,"Display:",
2559                         xpos,y6label,spref,buth,
2560                         0, 0, 0, 0, 0, "");     
2561                 uiBlockBeginAlign(block);
2562                 uiDefButBitI(block, TOG, USER_TOOLTIPS, 0, "Tool Tips",
2563                         (xpos+edgsp),y5,spref,buth,
2564                         &(U.flag), 0, 0, 0, 0,
2565                         "Display tooltips (help tags) over buttons");
2566                 uiDefButBitI(block, TOG, USER_DRAWVIEWINFO, B_DRAWINFO, "Object Info",
2567                         (xpos+edgsp),y4,spref,buth,
2568                         &(U.uiflag), 0, 0, 0, 0,
2569                         "Display active object name and frame number in the 3D View");
2570                 uiDefButBitI(block, TOG, USER_SCENEGLOBAL, 0, "Global Scene",
2571                         (xpos+edgsp),y3,spref,buth,
2572                         &(U.flag), 0, 0, 0, 0,
2573                         "Forces the current Scene to be displayed in all Screens");
2574 #ifndef __APPLE__       
2575                 uiDefButBitS(block, TOG, 1, 0, "Large Cursors",
2576                         (xpos+edgsp),y2,spref,buth,
2577                         &(U.curssize), 0, 0, 0, 0,
2578                         "Use large mouse cursors when available");
2579 #else 
2580                 U.curssize=0; /*Small Cursor always for OS X for now */
2581 #endif
2582                 uiDefButBitI(block, TOG, USER_SHOW_VIEWPORTNAME, B_DRAWINFO, "View Name",
2583                         (xpos+edgsp),y1,spref,buth,
2584                         &(U.uiflag), 0, 0, 0, 0,
2585                         "Show the name of the view's direction in each 3D View");
2586                 uiBlockEndAlign(block);
2587
2588                 uiDefBut(block, LABEL,0,"Menus:",
2589                         (xpos+(2*edgsp)+spref),y6label,spref,buth,
2590                         0, 0, 0, 0, 0, "");
2591                 uiBlockBeginAlign(block);
2592                 uiDefButBitI(block, TOG, USER_MENUOPENAUTO, 0, "Open on Mouse Over",
2593                         (xpos+edgsp+spref+midsp),y5,mpref,buth,
2594                         &(U.uiflag), 0, 0, 0, 0,
2595                         "Open menu buttons and pulldowns automatically when the mouse is hovering");
2596                 uiDefButS(block, NUM, 0, "Top Level:",
2597                         (xpos+edgsp+spref+midsp),y4,spref+edgsp,buth,
2598                         &(U.menuthreshold1), 1, 40, 0, 0,
2599                         "Time delay in 1/10 seconds before automatically opening top level menus");
2600                 uiDefButS(block, NUM, 0, "Sublevels:",
2601                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y4,spref+edgsp,buth,
2602                         &(U.menuthreshold2), 1, 40, 0, 0,
2603                         "Time delay in 1/10 seconds before automatically opening menu sublevels");
2604                 uiBlockEndAlign(block);
2605
2606                 uiDefBut(block, LABEL,0,"Toolbox click-hold delay:",
2607                         (xpos+(2*edgsp)+spref),y3label,mpref,buth,
2608                         0, 0, 0, 0, 0, "");
2609                 uiBlockBeginAlign(block);
2610                 uiDefButS(block, NUM, 0, "LMB:",
2611                         (xpos+edgsp+spref+midsp),y2,spref+edgsp,buth,
2612                         &(U.tb_leftmouse), 2, 40, 0, 0,
2613                         "Time in 1/10 seconds to hold the Left Mouse Button before opening the toolbox");
2614                 uiDefButS(block, NUM, 0, "RMB:",
2615                         (xpos+edgsp+(2*spref)+(2*midsp)-edgsp),y2,spref+edgsp,buth,
2616                         &(U.tb_rightmouse), 2, 40, 0, 0,
2617                         "Time in 1/10 seconds to hold the Right Mouse Button before opening the toolbox");      
2618                 uiBlockEndAlign(block);
2619
2620                 uiDefButBitI(block, TOG, USER_PANELPINNED, 0, "Pin Floating Panels",
2621                         (xpos+edgsp+spref+midsp),y1,(mpref/2),buth,
2622                         &(U.uiflag), 0, 0, 0, 0,
2623                         "Make floating panels invoked by a hotkey (eg. N Key) open at the previous location");
2624                 uiDefButBitI(block, TOG, USER_PLAINMENUS, B_PLAINMENUS, "Plain Menus",
2625                         (xpos+edgsp+(2*spref)+(2*midsp)),y1,spref,buth,
2626                         &(U.uiflag), 0, 0, 0, 0,
2627                         "Use a column layout for toolbox and do not flip the contents of any menu");
2628                 uiBlockEndAlign(block);
2629                 
2630                 uiDefBut(block, LABEL,0,"Snap to grid:",
2631                         (xpos+(2*edgsp)+spref+midsp+mpref),y6label,mpref,buth,
2632                         0, 0, 0, 0, 0, "");
2633                 uiBlockBeginAlign(block);
2634                 uiDefButBitI(block, TOG, USER_AUTOGRABGRID, 0, "Grab/Move",
2635                         (xpos+edgsp+mpref+spref+(2*midsp)),y5,spref,buth,
2636                         &(U.flag), 0, 0, 0, 0,
2637                         "Snap objects and sub-objects to grid units when moving");
2638                 uiDefButBitI(block, TOG, USER_AUTOROTGRID, 0, "Rotate",
2639                         (xpos+edgsp+mpref+spref+(2*midsp)),y4,spref,buth,
2640                         &(U.flag), 0, 0, 0, 0,
2641                         "Snap objects and sub-objects to grid units when rotating");
2642                 uiDefButBitI(block, TOG, USER_AUTOSIZEGRID, 0, "Scale",
2643                         (xpos+edgsp+mpref+spref+(2*midsp)),y3,spref,buth,
2644                         &(U.flag), 0, 0, 0, 0,
2645                         "Snap objects and sub-objects to grid units when scaling");
2646                 uiBlockEndAlign(block);
2647                 
2648                 uiDefButBitI(block, TOG, USER_LOCKAROUND, B_DRAWINFO, "Global Pivot",
2649                         (xpos+edgsp+mpref+spref+(2*midsp)),y1,spref,buth,
2650                         &(U.uiflag), 0, 0, 0, 0,
2651                         "Lock the same rotation/scaling pivot in all 3D Views");        
2652                 
2653                 uiDefBut(block, LABEL,0,"View zoom:",
2654                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y6label,mpref,buth,
2655                         0, 0, 0, 0, 0, "");
2656                 uiBlockBeginAlign(block);
2657                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2658                 uiDefButS(block, ROW, 0, "Continue",
2659                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y5,(mpref/3),buth,
2660                         &(U.viewzoom), 40, USER_ZOOM_CONT, 0, 0,
2661                         "Old style zoom, continues while moving mouse up or down");
2662                 uiDefButS(block, ROW, 0, "Dolly",
2663                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/3)),y5,(mpref/3),buth,
2664                         &(U.viewzoom), 40, USER_ZOOM_DOLLY, 0, 0,
2665                         "Zooms in and out based on vertical mouse movement.");
2666                 uiDefButS(block, ROW, 0, "Scale",
2667                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(2*mpref/3)),y5,(mpref/3),buth,
2668                         &(U.viewzoom), 40, USER_ZOOM_SCALE, 0, 0,
2669                         "Zooms in and out like scaling the view, mouse movements relative to center.");
2670                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2671                 uiBlockEndAlign(block);
2672                 
2673                 uiDefBut(block, LABEL,0,"View rotation:",
2674                         (xpos+(2*edgsp)+mpref+(2*spref)+(2*midsp)),y4label,mpref,buth,
2675                         0, 0, 0, 0, 0, "");
2676                 uiBlockBeginAlign(block);
2677                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2678                 uiDefButBitI(block, TOG, USER_TRACKBALL, B_DRAWINFO, "Trackball",
2679                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y3,(mpref/2),buth,
2680                         &(U.flag), 0, 0, 0, 0,
2681                         "Allow the view to tumble freely when orbiting with the Middle Mouse Button");
2682                 uiDefButBitI(block, TOGN, USER_TRACKBALL, B_DRAWINFO, "Turntable",
2683                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y3,(mpref/2),buth,
2684                         &(U.flag), 0, 0, 0, 0,
2685                         "Use fixed up axis for orbiting with Middle Mouse Button");
2686                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2687                 uiDefButBitI(block, TOG, USER_AUTOPERSP, B_DRAWINFO, "Auto Perspective",
2688                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)),y2,(mpref/2),buth,
2689                         &(U.uiflag), 0, 0, 0, 0,
2690                         "Automatically switch between orthographic and perspective when changing from top/front/side views");
2691                 uiDefButBitI(block, TOG, USER_ORBIT_SELECTION, B_DRAWINFO, "Around Active",
2692                         (xpos+edgsp+mpref+(2*spref)+(3*midsp)+(mpref/2)),y2,(mpref/2),buth,
2693                         &(U.uiflag), 0, 0, 0, 0,
2694                         "Keep the active object in place when orbiting the views (Object Mode)");
2695                 uiBlockEndAlign(block);
2696                 
2697
2698                 uiBlockBeginAlign(block);
2699                 uiDefButBitI(block, TOG, USER_SHOW_ROTVIEWICON, B_DRAWINFO, "Mini Axis",
2700                          (xpos+edgsp+(2*mpref)+(2*midsp)),y1,(mpref/3),buth,
2701                          &(U.uiflag), 0, 0, 0, 0,
2702                          "Show a small rotating 3D axis in the bottom left corner of the 3D View");
2703                 uiDefButS(block, NUM, B_DRAWINFO, "Size:",
2704                         (xpos+edgsp+(2*mpref)+(2*midsp)+(mpref/3)),y1,(mpref/3),buth,
2705                         &U.rvisize, 10, 64, 0, 0,
2706                         "The axis icon's size");
2707                 uiDefButS(block, NUM, B_DRAWINFO, "Bright:",
2708                         (xpos+edgsp+(2*mpref)+(2*midsp)+2*(mpref/3)),y1,(mpref/3),buth,
2709                         &U.rvibright, 0, 10, 0, 0,
2710                         "The brightness of the icon");
2711                 uiBlockEndAlign(block);
2712                 
2713
2714                 uiDefBut(block, LABEL,0,"Select with:",
2715                         (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y6label,mpref,buth,
2716                         0, 0, 0, 0, 0, "");
2717                 uiBlockBeginAlign(block);
2718                 uiBlockSetCol(block, TH_BUT_SETTING1);  /* mutually exclusive toggles, start color */
2719                 uiDefButBitI(block, TOG, USER_LMOUSESELECT, B_DRAWINFO, "Left Mouse",
2720                         (xpos+edgsp+(3*mpref)+(4*midsp)),y5,(mpref/2),buth,
2721                         &(U.flag), 0, 0, 0, 0, "Use the Left Mouse Button for selection");
2722                 uiDefButBitI(block, TOGN, USER_LMOUSESELECT, B_DRAWINFO, "Right Mouse",
2723                         (xpos+edgsp+(3*mpref)+(4*midsp)+(mpref/2)),y5,(mpref/2),buth,
2724                         &(U.flag), 0, 0, 0, 0, "Use the Right Mouse Button for selection");
2725                 uiBlockSetCol(block, TH_AUTO);                  /* end color */
2726                 uiBlockEndAlign(block);
2727                 
2728                 
2729                 if(U.flag & USER_LMOUSESELECT) {
2730                         uiDefBut(block, LABEL,0,"Cursor with: Right Mouse",
2731                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2732                                 0, 0, 0, 0, 0, "");
2733                 } else {
2734                         uiDefBut(block, LABEL,0,"Cursor with: Left Mouse",
2735                                 (xpos+(2*edgsp)+(3*mpref)+(3*midsp)),y4label+5,mpref,buth,
2736                                 0, 0, 0, 0, 0, "");
2737