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