- moved the 'Enable All Codecs' button over in the info window (for Win32)
[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 #ifdef WIN32
44 #include "BLI_winstuff.h"
45 #endif
46
47 #include "MEM_guardedalloc.h"
48
49 #ifdef INTERNATIONAL
50 #include "BIF_language.h"
51 #endif
52
53 #include "IMB_imbuf_types.h"
54 #include "IMB_imbuf.h"
55
56 #include "BLI_blenlib.h"
57 #include "BLI_arithb.h"
58 #include "BLI_editVert.h"
59 #include "BLI_linklist.h"
60
61 #include "DNA_action_types.h"
62 #include "DNA_curve_types.h"
63 #include "DNA_image_types.h"
64 #include "DNA_ipo_types.h"
65 #include "DNA_mesh_types.h"
66 #include "DNA_object_types.h"
67 #include "DNA_scene_types.h"
68 #include "DNA_screen_types.h"
69 #include "DNA_sequence_types.h"
70 #include "DNA_sound_types.h"
71 #include "DNA_space_types.h"
72 #include "DNA_userdef_types.h"
73 #include "DNA_view2d_types.h"
74 #include "DNA_view3d_types.h"
75
76 #include "BKE_blender.h"
77 #include "BKE_curve.h"
78 #include "BKE_displist.h"
79 #include "BKE_global.h"
80 #include "BKE_ipo.h"
81 #include "BKE_main.h"
82 #include "BKE_scene.h"
83 #include "BKE_utildefines.h"
84
85 #include "BIF_buttons.h"
86 #include "BIF_drawimage.h"
87 #include "BIF_drawseq.h"
88 #include "BIF_drawtext.h"
89 #include "BIF_editarmature.h"
90 #include "BIF_editfont.h"
91 #include "BIF_editika.h"
92 #include "BIF_editkey.h"
93 #include "BIF_editlattice.h"
94 #include "BIF_editmesh.h"
95 #include "BIF_editoops.h"
96 #include "BIF_editseq.h"
97 #include "BIF_editsima.h"
98 #include "BIF_editsound.h"
99 #include "BIF_editview.h"
100 #include "BIF_gl.h"
101 #include "BIF_imasel.h"
102 #include "BIF_interface.h"
103 #include "BIF_mywindow.h"
104 #include "BIF_oops.h"
105 #include "BIF_resources.h"
106 #include "BIF_screen.h"
107 #include "BIF_space.h"
108 #include "BIF_spacetypes.h"
109 #include "BIF_toets.h"
110 #include "BIF_toolbox.h"
111 #include "BIF_usiblender.h"
112 #include "BIF_previewrender.h"
113
114 #include "BSE_edit.h"
115 #include "BSE_view.h"
116 #include "BSE_editipo.h"
117 #include "BSE_drawipo.h"
118 #include "BSE_drawview.h"
119 #include "BSE_drawnla.h"
120 #include "BSE_filesel.h"
121 #include "BSE_headerbuttons.h"
122 #include "BSE_editnla_types.h"
123
124 #include "BDR_vpaint.h"
125 #include "BDR_editmball.h"
126 #include "BDR_editobject.h"
127 #include "BDR_editcurve.h"
128 #include "BDR_editface.h"
129 #include "BDR_drawmesh.h"
130
131 #include "BLO_readfile.h" /* for BLO_blendhandle_close */
132
133 #include "interface.h"
134 #include "mydevice.h"
135 #include "blendef.h"
136 #include "datatoc.h"
137
138 #include "BPY_extern.h" // Blender Python library
139
140 #include "TPT_DependKludge.h"
141 #ifdef NAN_TPT
142 #include "BSE_trans_types.h"
143 #include "IMG_Api.h"
144 #endif /* NAN_TPT */
145
146 #include "SYS_System.h" /* for the user def menu ... should move elsewhere. */
147
148 extern void StartKetsjiShell(ScrArea *area, char* startscenename, struct Main* maggie, int always_use_expand_framing);
149
150 /**
151  * When the mipmap setting changes, we want to redraw the view right
152  * away to reflect this setting.
153  */
154 void space_mipmap_button_function(int event);
155
156
157 unsigned short convert_for_nonumpad(unsigned short event);
158 void free_soundspace(SpaceSound *ssound);
159
160 /* ************* SPACE: VIEW3D  ************* */
161
162 /*  extern void drawview3dspace(ScrArea *sa, void *spacedata); BSE_drawview.h */
163
164
165 void copy_view3d_lock(short val)
166 {
167         bScreen *sc;
168         int bit;
169         
170         /* from G.scene copy to the other views */
171         sc= G.main->screen.first;
172         
173         while(sc) {
174                 if(sc->scene==G.scene) {
175                         ScrArea *sa= sc->areabase.first;
176                         while(sa) {
177                                 SpaceLink *sl= sa->spacedata.first;
178                                 while(sl) {
179                                         if(sl->spacetype==SPACE_OOPS && val==REDRAW) {
180                                                 if(sa->win) scrarea_queue_winredraw(sa);
181                                         }
182                                         else if(sl->spacetype==SPACE_VIEW3D) {
183                                                 View3D *vd= (View3D*) sl;
184                                                 if(vd->scenelock && vd->localview==0) {
185                                                         vd->lay= G.scene->lay;
186                                                         vd->camera= G.scene->camera;
187                                                         
188                                                         if(vd->camera==0 && vd->persp>1) vd->persp= 1;
189                                                         
190                                                         if( (vd->lay & vd->layact) == 0) {
191                                                                 bit= 0;
192                                                                 while(bit<32) {
193                                                                         if(vd->lay & (1<<bit)) {
194                                                                                 vd->layact= 1<<bit;
195                                                                                 break;
196                                                                         }
197                                                                         bit++;
198                                                                 }
199                                                         }
200                                                         
201                                                         if(val==REDRAW && vd==sa->spacedata.first) {
202                                                                 if(sa->win) scrarea_queue_redraw(sa);
203                                                         }
204                                                 }
205                                         }
206                                         sl= sl->next;
207                                 }
208                                 sa= sa->next;
209                         }
210                 }
211                 sc= sc->id.next;
212         }
213 }
214
215 void handle_view3d_lock()
216 {
217         if (G.vd != NULL) {
218                 if(G.vd->localview==0 && G.vd->scenelock && curarea->spacetype==SPACE_VIEW3D) {
219
220                         /* copy to scene */
221                         G.scene->lay= G.vd->lay;
222                         G.scene->camera= G.vd->camera;
223         
224                         copy_view3d_lock(REDRAW);
225                 }
226         }
227 }
228
229 void space_set_commmandline_options(void) {
230         SYS_SystemHandle syshandle;
231         int a;
232                 
233         if ( (syshandle = SYS_GetSystem()) ) {
234                 /* User defined settings */
235                 a= (U.gameflags & USERDEF_VERTEX_ARRAYS);
236                 SYS_WriteCommandLineInt(syshandle, "vertexarrays", a);
237
238                 a= (U.gameflags & USERDEF_DISABLE_SOUND);
239                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
240
241                 a= (U.gameflags & USERDEF_DISABLE_MIPMAP);
242                 set_mipmap(!a);
243                 SYS_WriteCommandLineInt(syshandle, "nomipmap", a);
244
245                 /* File specific settings: */
246                 /* Only test the first one. These two are switched
247                  * simultaneously. */
248                 a= (G.fileflags & G_FILE_SHOW_FRAMERATE);
249                 SYS_WriteCommandLineInt(syshandle, "show_framerate", a);
250                 SYS_WriteCommandLineInt(syshandle, "show_profile", a);
251
252                 /* When in wireframe mode, always draw debug props. */
253                 if (G.vd) {
254                         a = ( (G.fileflags & G_FILE_SHOW_DEBUG_PROPS) 
255                                   || (G.vd->drawtype == OB_WIRE)          
256                                   || (G.vd->drawtype == OB_SOLID)         );
257                         SYS_WriteCommandLineInt(syshandle, "show_properties", a);
258                 }
259
260                 a= (G.fileflags & G_FILE_ENABLE_ALL_FRAMES);
261                 SYS_WriteCommandLineInt(syshandle, "fixedtime", a);
262         }
263 }
264
265         /**
266          * These two routines imported from the gameengine, 
267          * I suspect a lot of the resetting stuff is cruft
268          * and can be removed, but it should be checked.
269          */
270 static void SaveState(void)
271 {
272         glPushAttrib(GL_ALL_ATTRIB_BITS);
273
274         init_realtime_GL();
275         init_gl_stuff();
276
277         if(G.scene->camera==0 || G.scene->camera->type!=OB_CAMERA)
278                 error("no (correct) camera");
279
280         waitcursor(1);
281 }
282
283 static void RestoreState(void)
284 {
285         curarea->win_swap = 0;
286         curarea->head_swap=0;
287         allqueue(REDRAWVIEW3D, 0);
288         allqueue(REDRAWBUTSALL, 0);
289         reset_slowparents();
290         waitcursor(0);
291         G.qual= 0;
292         glPopAttrib();
293 }
294
295 static LinkNode *save_and_reset_all_scene_cfra(void)
296 {
297         LinkNode *storelist= NULL;
298         Scene *sc;
299         
300         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
301                 BLI_linklist_prepend(&storelist, (void*) (long) sc->r.cfra);
302                 sc->r.cfra= 1;
303
304                 set_scene_bg(sc);
305         }
306         
307         BLI_linklist_reverse(&storelist);
308         
309         return storelist;
310 }
311
312 static void restore_all_scene_cfra(LinkNode *storelist) {
313         LinkNode *sc_store= storelist;
314         Scene *sc;
315         
316         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
317                 int stored_cfra= (int) sc_store->link;
318                 
319                 sc->r.cfra= stored_cfra;
320                 set_scene_bg(sc);
321                 
322                 sc_store= sc_store->next;
323         }
324         
325         BLI_linklist_free(storelist, NULL);
326 }
327
328 void start_game(void)
329 {
330 #if GAMEBLENDER == 1
331         Scene *sc, *startscene = G.scene;
332         LinkNode *scene_cfra_store;
333
334                 /* XXX, silly code -  the game engine can
335                  * access any scene through logic, so we try 
336                  * to make sure each scene has a valid camera, 
337                  * just in case the game engine tries to use it.
338                  * 
339                  * Better would be to make a better routine
340                  * in the game engine for finding the camera.
341                  *  - zr
342                  */
343         for (sc= G.main->scene.first; sc; sc= sc->id.next) {
344                 if (!sc->camera) {
345                         Base *base;
346         
347                         for (base= sc->base.first; base; base= base->next)
348                                 if (base->object->type==OB_CAMERA)
349                                         break;
350                         
351                         sc->camera= base?base->object:NULL;
352                 }
353         }
354
355         /* these two lines make sure front and backbuffer are equal. for swapbuffers */
356         markdirty_all();
357         screen_swapbuffers();
358
359         /* can start from header */
360         mywinset(curarea->win);
361     
362         scene_cfra_store= save_and_reset_all_scene_cfra();
363         
364         BPY_end_python();
365
366         sound_stop_all_sounds();
367
368         /* Before jumping into Ketsji, we configure some settings. */
369         space_set_commmandline_options();
370
371         SaveState();
372         StartKetsjiShell(curarea, startscene->id.name+2, G.main, 1);
373         RestoreState();
374
375         BPY_start_python();
376
377         restore_all_scene_cfra(scene_cfra_store);
378         set_scene_bg(startscene);
379         
380         if (G.flags & G_FLAGS_AUTOPLAY)
381                 exit_usiblender();
382
383                 /* groups could have changed ipo */
384         allqueue(REDRAWNLA, 0);
385         allqueue(REDRAWACTION, 0);
386         allspace(REMAKEIPO, 0);
387         allqueue(REDRAWIPO, 0);
388 #else
389         notice("Game engine is disabled in this release!");
390 #endif
391 }
392
393 static void changeview3dspace(ScrArea *sa, void *spacedata)
394 {
395         setwinmatrixview3d(0);  /* 0= no pick rect */
396 }
397
398         /* Callable from editmode and faceselect mode from the
399          * moment, would be nice (and is easy) to generalize
400          * to any mode.
401          */
402 static void align_view_to_selected(View3D *v3d)
403 {
404         int nr= pupmenu("Align view%t|To selection (top)%x2|To selection (front)%x1|To selection (side)%x0");
405
406         if (nr!=-1) {
407                 int axis= nr;
408
409                 if (G.obedit && (G.obedit->type == OB_MESH)) {
410                         editmesh_align_view_to_selected(v3d, axis);
411                         addqueue(v3d->area->win, REDRAW, 1);
412                 } else if (G.f & G_FACESELECT) {
413                         Object *obact= OBACT;
414                         if (obact && obact->type==OB_MESH) {
415                                 Mesh *me= obact->data;
416
417                                 if (me->tface) {
418                                         faceselect_align_view_to_selected(v3d, me, axis);
419                                         addqueue(v3d->area->win, REDRAW, 1);
420                                 }
421                         }
422                 }
423         }
424 }
425
426 void select_children(Object *ob)
427 {
428         Base *base;
429
430         for (base= FIRSTBASE; base; base= base->next)
431                 if (ob == base->object->parent) {
432                         base->flag |= SELECT;
433                         select_children(base->object);
434                 }
435 }
436
437 void winqreadview3dspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
438 {
439         unsigned short event= evt->event;
440         short val= evt->val;
441         char ascii= evt->ascii;
442         View3D *v3d= curarea->spacedata.first;
443         Object *ob;
444         float *curs;
445         int doredraw= 0, pupval;
446         
447         if(curarea->win==0) return;     /* when it comes from sa->headqread() */
448         if(event==MOUSEY) return;
449         
450         if(val) {
451
452                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
453
454                 /* TEXTEDITING?? */
455                 if(G.obedit && G.obedit->type==OB_FONT) {
456                         switch(event) {
457                         
458                         case LEFTMOUSE:
459                                 mouse_cursor();
460                                 break;
461                         case MIDDLEMOUSE:
462                                 if(U.flag & VIEWMOVE) {
463                                         if(G.qual & LR_SHIFTKEY) viewmove(0);
464                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
465                                         else viewmove(1);
466                                 }
467                                 else {
468                                         if(G.qual & LR_SHIFTKEY) viewmove(1);
469                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
470                                         else viewmove(0);
471                                 }
472                         case WHEELUPMOUSE:
473                                 /* Regular:   Zoom in */
474                                 /* Shift:     Scroll up */
475                                 /* Ctrl:      Scroll right */
476                                 /* Alt-Shift: Rotate up */
477                                 /* Alt-Ctrl:  Rotate right */
478
479                                 if( G.qual & LR_SHIFTKEY ) {
480                                         if( G.qual & LR_ALTKEY ) { 
481                                                 G.qual &= ~LR_SHIFTKEY;
482                                                 persptoetsen(PAD2);
483                                                 G.qual |= LR_SHIFTKEY;
484                                         } else {
485                                                 persptoetsen(PAD2);
486                                         }
487                                 } else if( G.qual & LR_CTRLKEY ) {
488                                         if( G.qual & LR_ALTKEY ) { 
489                                                 G.qual &= ~LR_CTRLKEY;
490                                                 persptoetsen(PAD4);
491                                                 G.qual |= LR_CTRLKEY;
492                                         } else {
493                                                 persptoetsen(PAD4);
494                                         }
495                                 } else if(U.uiflag & WHEELZOOMDIR) 
496                                         persptoetsen(PADMINUS);
497                                 else
498                                         persptoetsen(PADPLUSKEY);
499
500                                 doredraw= 1;
501                                 break;
502
503                         case WHEELDOWNMOUSE:
504                                 /* Regular:   Zoom out */
505                                 /* Shift:     Scroll down */
506                                 /* Ctrl:      Scroll left */
507                                 /* Alt-Shift: Rotate down */
508                                 /* Alt-Ctrl:  Rotate left */
509
510                                 if( G.qual & LR_SHIFTKEY ) {
511                                         if( G.qual & LR_ALTKEY ) { 
512                                                 G.qual &= ~LR_SHIFTKEY;
513                                                 persptoetsen(PAD8);
514                                                 G.qual |= LR_SHIFTKEY;
515                                         } else {
516                                                 persptoetsen(PAD8);
517                                         }
518                                 } else if( G.qual & LR_CTRLKEY ) {
519                                         if( G.qual & LR_ALTKEY ) { 
520                                                 G.qual &= ~LR_CTRLKEY;
521                                                 persptoetsen(PAD6);
522                                                 G.qual |= LR_CTRLKEY;
523                                         } else {
524                                                 persptoetsen(PAD6);
525                                         }
526                                 } else if(U.uiflag & WHEELZOOMDIR) 
527                                         persptoetsen(PADPLUSKEY);
528                                 else
529                                         persptoetsen(PADMINUS);
530                                 
531                                 doredraw= 1;
532                                 break;
533
534                         case UKEY:
535                                 if(G.qual & LR_ALTKEY) {
536                                         remake_editText();
537                                         doredraw= 1;
538                                 } else {
539                                         do_textedit(event, val, ascii);
540                                 }
541                                 break;
542                         case VKEY:
543                                 if(G.qual & LR_ALTKEY) {
544                                         paste_editText();
545                                         doredraw= 1;
546                                 } else {
547                                         do_textedit(event, val, ascii);
548                                 }
549                                 break;
550                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
551                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
552                         case PADENTER:
553                                 persptoetsen(event);
554                                 doredraw= 1;
555                                 break;
556                                 
557                         default:
558                                 do_textedit(event, val, ascii);
559                                 break;
560                         }
561                 }
562                 else {
563                         switch(event) {
564                         
565                         case BACKBUFDRAW:
566                                 backdrawview3d(1);
567                                 break;
568                                 
569                         case LEFTMOUSE:
570                                 if (G.obedit || !(G.f&(G_VERTEXPAINT|G_WEIGHTPAINT|G_TEXTUREPAINT))) {
571                                         mouse_cursor();
572                                 }
573                                 else if (G.f & G_VERTEXPAINT) {
574                                         vertex_paint();
575                                 }
576                                 else if (G.f & G_WEIGHTPAINT){
577                                         weight_paint();
578                                 }
579                                 else if (G.f & G_TEXTUREPAINT) {
580                                         face_draw();
581                                 }
582                                 break;
583                         case MIDDLEMOUSE:
584                                 if(U.flag & VIEWMOVE) {
585                                         if(G.qual & LR_SHIFTKEY) viewmove(0);
586                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
587                                         else viewmove(1);
588                                 }
589                                 else {
590                                         if(G.qual & LR_SHIFTKEY) viewmove(1);
591                                         else if(G.qual & LR_CTRLKEY) viewmove(2);
592                                         else viewmove(0);
593                                 }
594                                 break;
595                         case RIGHTMOUSE:
596                                 if(G.obedit && (G.qual & LR_CTRLKEY)==0) {
597                                         if(G.obedit->type==OB_MESH) mouse_mesh();
598                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) mouse_nurb();
599                                         else if(G.obedit->type==OB_MBALL) mouse_mball();
600                                         else if(G.obedit->type==OB_LATTICE) mouse_lattice();
601                                         else if(G.obedit->type==OB_ARMATURE) mouse_armature();
602                                 }
603                                 else if(G.obpose) { 
604                                         if (G.obpose->type==OB_ARMATURE) mousepose_armature();
605                                 }
606                                 else if( G.qual & LR_CTRLKEY ) mouse_select();
607                                 else if(G.f & G_FACESELECT) face_select();
608                                 else if( G.f & (G_VERTEXPAINT|G_TEXTUREPAINT)) sample_vpaint();
609                                 else
610                                         mouse_select();
611                                 break;
612
613                         case WHEELUPMOUSE:
614                                 /* Regular:   Zoom in */
615                                 /* Shift:     Scroll up */
616                                 /* Ctrl:      Scroll right */
617                                 /* Alt-Shift: Rotate up */
618                                 /* Alt-Ctrl:  Rotate right */
619
620                                 if( G.qual & LR_SHIFTKEY ) {
621                                         if( G.qual & LR_ALTKEY ) { 
622                                                 G.qual &= ~LR_SHIFTKEY;
623                                                 persptoetsen(PAD2);
624                                                 G.qual |= LR_SHIFTKEY;
625                                         } else {
626                                                 persptoetsen(PAD2);
627                                         }
628                                 } else if( G.qual & LR_CTRLKEY ) {
629                                         if( G.qual & LR_ALTKEY ) { 
630                                                 G.qual &= ~LR_CTRLKEY;
631                                                 persptoetsen(PAD4);
632                                                 G.qual |= LR_CTRLKEY;
633                                         } else {
634                                                 persptoetsen(PAD4);
635                                         }
636                                 } else if(U.uiflag & WHEELZOOMDIR) 
637                                         persptoetsen(PADMINUS);
638                                 else
639                                         persptoetsen(PADPLUSKEY);
640
641                                 doredraw= 1;
642                                 break;
643                         case WHEELDOWNMOUSE:
644                                 /* Regular:   Zoom out */
645                                 /* Shift:     Scroll down */
646                                 /* Ctrl:      Scroll left */
647                                 /* Alt-Shift: Rotate down */
648                                 /* Alt-Ctrl:  Rotate left */
649
650                                 if( G.qual & LR_SHIFTKEY ) {
651                                         if( G.qual & LR_ALTKEY ) { 
652                                                 G.qual &= ~LR_SHIFTKEY;
653                                                 persptoetsen(PAD8);
654                                                 G.qual |= LR_SHIFTKEY;
655                                         } else {
656                                                 persptoetsen(PAD8);
657                                         }
658                                 } else if( G.qual & LR_CTRLKEY ) {
659                                         if( G.qual & LR_ALTKEY ) { 
660                                                 G.qual &= ~LR_CTRLKEY;
661                                                 persptoetsen(PAD6);
662                                                 G.qual |= LR_CTRLKEY;
663                                         } else {
664                                                 persptoetsen(PAD6);
665                                         }
666                                 } else if(U.uiflag & WHEELZOOMDIR) 
667                                         persptoetsen(PADPLUSKEY);
668                                 else
669                                         persptoetsen(PADMINUS);
670                                 
671                                 doredraw= 1;
672                                 break;
673                         
674                         case ONEKEY:
675                                 ob= OBACT;
676                                 if(G.qual & LR_CTRLKEY) {
677                                         if(G.obedit) {
678                                                         flip_subdivison(G.obedit, 1);
679                                         }
680                                         else if(ob->type == OB_MESH) {
681                                                 flip_subdivison(ob, 1);
682                                         }
683                                 }
684                                 else
685                                         do_layer_buttons(0); break;
686                         case TWOKEY:
687                                 ob= OBACT;
688                                 if(G.qual & LR_CTRLKEY) {
689                                         if(G.obedit) {
690                                                         flip_subdivison(G.obedit, 2);
691                                         }
692                                         else if(ob->type == OB_MESH) {
693                                                 flip_subdivison(ob, 2);
694                                         }
695                                 }
696                                 else
697                                         do_layer_buttons(1); 
698                                 break;
699                         case THREEKEY:
700                                 ob= OBACT;
701                                 if(G.qual & LR_CTRLKEY) {
702                                         if(G.obedit) {
703                                                         flip_subdivison(G.obedit, 3);
704                                         }
705                                         else if(ob->type == OB_MESH) {
706                                                 flip_subdivison(ob, 3);
707                                         }
708                                 }
709                                 else
710                                 do_layer_buttons(2); break;
711                         case FOURKEY:
712                                 ob= OBACT;
713                                 if(G.qual & LR_CTRLKEY) {
714                                         if(G.obedit) {
715                                                         flip_subdivison(G.obedit, 4);
716                                         }
717                                         else if(ob->type == OB_MESH) {
718                                                 flip_subdivison(ob, 4);
719                                         }
720                                 }
721                                 else
722                                 do_layer_buttons(3); break;
723                         case FIVEKEY:
724                                 do_layer_buttons(4); break;
725                         case SIXKEY:
726                                 do_layer_buttons(5); break;
727                         case SEVENKEY:
728                                 do_layer_buttons(6); break;
729                         case EIGHTKEY:
730                                 do_layer_buttons(7); break;
731                         case NINEKEY:
732                                 do_layer_buttons(8); break;
733                         case ZEROKEY:
734                                 do_layer_buttons(9); break;
735                         case MINUSKEY:
736                                 do_layer_buttons(10); break;
737                         case EQUALKEY:
738                                 do_layer_buttons(11); break;
739                         case ACCENTGRAVEKEY:
740                                 do_layer_buttons(-1); break;
741                                 
742                         case AKEY:
743                                 if(G.qual & LR_CTRLKEY) apply_object();
744                                 else if(G.qual & LR_SHIFTKEY) {
745                                         tbox_setmain(0);
746                                         toolbox();
747                                 }
748                                 else {
749                                         if(G.obedit) {
750                                                 if(G.obedit->type==OB_MESH) deselectall_mesh();
751                                                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) deselectall_nurb();
752                                                 else if(G.obedit->type==OB_MBALL) deselectall_mball();
753                                                 else if(G.obedit->type==OB_LATTICE) deselectall_Latt();
754                                                 else if(G.obedit->type==OB_ARMATURE) deselectall_armature();
755                                         }
756                                         else if (G.obpose){
757                                                 switch (G.obpose->type){
758                                                 case OB_ARMATURE:
759                                                         deselectall_posearmature(1);
760                                                         break;
761                                                 }
762                                         }
763                                         else {
764                                                 if(G.f & G_FACESELECT) deselectall_tface();
765                                                 else {
766                                                         /* by design, the center of the active object 
767                                                          * (which need not necessarily by selected) will
768                                                          * still be drawn as if it were selected.
769                                                          */
770                                                         deselectall();
771                                                 }
772                                         }
773                                 }
774                                 break;
775                         case BKEY:
776                                 if(G.qual & LR_SHIFTKEY) set_render_border();
777                                 else borderselect();
778                                 break;
779                         case CKEY:
780                                 if(G.qual & LR_CTRLKEY) {
781                                         copymenu();
782                                 }
783                                 else if(G.qual & LR_ALTKEY) {
784                                         convertmenu();  /* editobject.c */
785                                 }
786                                 else if(G.qual & LR_SHIFTKEY) {
787                                         view3d_home(1);
788                                         curs= give_cursor();
789                                         curs[0]=curs[1]=curs[2]= 0.0;
790                                         scrarea_queue_winredraw(curarea);
791                                 }
792                                 else if(G.obedit!=0 && ELEM(G.obedit->type, OB_CURVE, OB_SURF) ) {
793                                         makecyclicNurb();
794                                         makeDispList(G.obedit);
795                                         allqueue(REDRAWVIEW3D, 0);
796                                 }
797                                 else {
798                                         curs= give_cursor();
799                                         G.vd->ofs[0]= -curs[0];
800                                         G.vd->ofs[1]= -curs[1];
801                                         G.vd->ofs[2]= -curs[2];
802                                         scrarea_queue_winredraw(curarea);
803                                 }
804                         
805                                 break;
806                         case DKEY:
807                                 if(G.qual & LR_SHIFTKEY) {
808                                         duplicate_context_selected();
809                                 }
810                                 else if(G.qual & LR_ALTKEY) {
811                                         if(G.obpose) error ("Duplicate not possible in posemode.");
812                                         else
813                                         if(G.obedit==0) adduplicate(0);
814                                 }
815                                 else if(G.qual & LR_CTRLKEY) {
816                                         imagestodisplist();
817                                 }
818                                 else {
819                                         pupval= pupmenu("Draw mode%t|BoundBox %x1|Wire %x2|OpenGL Solid %x3|Shaded Solid %x4");
820                                         if(pupval>0) {
821                                                 G.vd->drawtype= pupval;
822                                                 doredraw= 1;
823                                         
824                                         }
825                                 }
826                                 
827                                 break;
828                         case EKEY:
829                                 if(G.obedit) {
830                                         if(G.obedit->type==OB_MESH) extrude_mesh();
831                                         else if(G.obedit->type==OB_CURVE) addvert_Nurb('e');
832                                         else if(G.obedit->type==OB_SURF) extrude_nurb();
833                                         else if(G.obedit->type==OB_ARMATURE) extrude_armature();
834                                 }
835                                 else {
836                                         ob= OBACT;
837                                         if(ob && ob->type==OB_IKA) if(okee("extrude IKA")) extrude_ika(ob, 1);
838                                 }
839                                 break;
840                         case FKEY:
841                                 if(G.obedit) {
842                                         if(G.obedit->type==OB_MESH) {
843                                                 if(G.qual & LR_SHIFTKEY) fill_mesh();
844                                                 else if(G.qual & LR_ALTKEY) beauty_fill();
845                                                 else if(G.qual & LR_CTRLKEY) edge_flip();
846                                                 else addedgevlak_mesh();
847                                         }
848                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) addsegment_nurb();
849                                 }
850                                 else if(G.qual & LR_CTRLKEY) sort_faces();
851                                 else if(G.qual & LR_SHIFTKEY) fly();
852                                 else {
853                                                 set_faceselect();
854                                         }
855                                 
856                                 break;
857                         case GKEY:
858                                 /* RMGRP if(G.qual & LR_CTRLKEY) add_selected_to_group();
859                                 else if(G.qual & LR_ALTKEY) rem_selected_from_group();
860                                 else if(G.qual & LR_SHIFTKEY) group_menu();
861                                 else */ 
862                                 if(G.qual & LR_ALTKEY) clear_object('g');
863                                 else
864                                         transform('g');
865                                 break;
866                         case HKEY:
867                                 if(G.obedit) {
868                                         if(G.obedit->type==OB_MESH) {
869                                                 if(G.qual & LR_ALTKEY) reveal_mesh();
870                                                 else hide_mesh(G.qual & LR_SHIFTKEY);
871                                         }
872                                         else if(G.obedit->type== OB_SURF) {
873                                                 if(G.qual & LR_ALTKEY) revealNurb();
874                                                 else hideNurb(G.qual & LR_SHIFTKEY);
875                                         }
876                                         else if(G.obedit->type==OB_CURVE) {
877                                         
878                                                 if(G.qual & LR_CTRLKEY) autocalchandlesNurb_all(1);     /* flag=1, selected */
879                                                 else if(G.qual & LR_SHIFTKEY) sethandlesNurb(1);
880                                                 else sethandlesNurb(3);
881                                                 
882                                                 makeDispList(G.obedit);
883                                                 
884                                                 allqueue(REDRAWVIEW3D, 0);
885                                         }
886                                 }
887                                 else if(G.f & G_FACESELECT) hide_tface();
888                                 
889                                 break;
890                         case IKEY:
891                                 break;
892                                 
893                         case JKEY:
894                                 if(G.qual & LR_CTRLKEY) {
895                                         if( (ob= OBACT) ) {
896                                                 if(ob->type == OB_MESH) join_mesh();
897                                                 else if(ob->type == OB_CURVE) join_curve(OB_CURVE);
898                                                 else if(ob->type == OB_SURF) join_curve(OB_SURF);
899                                                 else if(ob->type == OB_ARMATURE) join_armature ();
900                                         }
901                                         else if (G.obedit && ELEM(G.obedit->type, OB_CURVE, OB_SURF)) addsegment_nurb();
902                                 } else if(G.obedit) {
903                                         if(G.obedit->type==OB_MESH) {
904                                                 join_triangles();
905                                         }
906                                 }
907
908                                 break;
909                         case KKEY:
910                                 if(G.obedit) {
911                                         if(G.obedit->type==OB_SURF) printknots();
912                                 }
913                                 else {
914                                         if(G.qual & LR_SHIFTKEY) {
915                                                 if(G.f & G_FACESELECT) clear_vpaint_selectedfaces();
916                                                 else if(G.f & G_VERTEXPAINT) clear_vpaint();
917                                                 else select_select_keys();
918                                         }
919                                         else if(G.qual & LR_CTRLKEY) make_skeleton();
920 /*                                      else if(G.qual & LR_ALTKEY) delete_skeleton(); */
921                                         else set_ob_ipoflags();
922                                 }
923                                 
924                                 break;
925                         case LKEY:
926                                 if(G.obedit) {
927                                         if(G.obedit->type==OB_MESH) selectconnected_mesh();
928                                         if(G.obedit->type==OB_ARMATURE) selectconnected_armature();
929                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) selectconnected_nurb();
930                                 }
931                                 else if(G.obpose) {
932                                         if(G.obpose->type==OB_ARMATURE) selectconnected_posearmature();
933                                 }
934                                 else {
935                                 
936                                         if(G.qual & LR_SHIFTKEY) selectlinks();
937                                         else if(G.qual & LR_CTRLKEY) linkmenu();
938                                         else if(G.f & G_FACESELECT) select_linked_tfaces();
939                                         else make_local();
940                                 }
941                                 break;
942                         case MKEY:
943                                 if((G.obedit) && (G.qual & LR_ALTKEY)) {
944                                         if(G.obedit->type==OB_MESH) mergemenu();
945                                 }
946                                 else {
947                                         movetolayer();
948                                 }
949                                 break;
950                         case NKEY:
951                                 if(G.obedit) {
952                                         switch (G.obedit->type){
953                                         case OB_ARMATURE:
954                                                 if (okee("Recalc bone roll angles")) auto_align_armature();
955                                                 break;
956                                         case OB_MESH: 
957                                                 if(G.qual & LR_SHIFTKEY) {
958                                                         if(okee("Recalc normals inside")) righthandfaces(2);
959                                                 }
960                                                 else {
961                                                         if(okee("Recalc normals outside")) righthandfaces(1);
962                                                 }
963                                                 break;
964                                         }
965                                         allqueue(REDRAWVIEW3D, 0);
966                                 }
967                                 break;
968                         case OKEY:
969                                 ob= OBACT;
970                                 if(G.qual & LR_SHIFTKEY) {
971                                         if(G.obedit) {
972                                                         flip_subdivison(G.obedit, 0);
973                                         }
974                                         else if(ob->type == OB_MESH) {
975                                                 flip_subdivison(ob, 0);
976                                         }
977                                 }
978                                 else if(G.qual & LR_ALTKEY) clear_object('o');
979                                 else if(G.obedit) {
980                                         extern int prop_mode;
981
982                                         if (G.qual & LR_SHIFTKEY) prop_mode= !prop_mode;
983                                         else G.f ^= G_PROPORTIONAL;
984
985                                         allqueue(REDRAWHEADERS, 0);
986                                 }
987                                 break;
988                         case PKEY:
989                                 
990                                 if(G.obedit) {
991                                         if(G.qual) {
992                                                 if(G.qual & LR_CTRLKEY) make_parent();
993                                         }
994                                         else if(G.obedit->type==OB_MESH) separate_mesh();
995                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) separate_nurb();
996                                 }
997                                 else if (G.qual == LR_SHIFTKEY) {
998                                         select_children(OBACT);
999                                         allqueue(REDRAWVIEW3D, 0);                                      
1000                                 }                               
1001                                 else if(G.qual & LR_CTRLKEY) make_parent();
1002                                 else if(G.qual & LR_ALTKEY) clear_parent();
1003                                 else {
1004                         start_game();
1005                                 }
1006                                 break;
1007                         case RKEY:
1008                                 if(G.obedit==0 && (G.f & G_FACESELECT)) rotate_uv_tface();
1009                                 else if(G.qual & LR_ALTKEY) clear_object('r');
1010                                 else if(G.qual & LR_SHIFTKEY) selectrow_nurb();
1011                                 else transform('r');
1012                                 break;
1013                         case SKEY:
1014                                 if(G.qual & LR_ALTKEY) {
1015                                         if(G.obedit) transform('N');    /* scale by vertex normal */
1016                                         else clear_object('s');
1017                                 }
1018                                 else if(G.qual & LR_SHIFTKEY) snapmenu();
1019                                 else if(G.qual & LR_CTRLKEY) {
1020                                         if(G.obedit) transform('S');
1021                                 }
1022                                 else transform('s');
1023                                 break;
1024                         case TKEY:
1025                                 if(G.qual & LR_CTRLKEY) {
1026                                         if(G.obedit) {
1027                                                 if(G.obedit->type==OB_MESH) {
1028                                                         convert_to_triface(0);
1029                                                         allqueue(REDRAWVIEW3D, 0);
1030                                                         countall();
1031                                                         makeDispList(G.obedit);
1032                                                 }
1033                                         }
1034                                         else make_track();
1035                                 }
1036                                 else if(G.qual & LR_ALTKEY) {
1037                                         if(G.obedit && G.obedit->type==OB_CURVE) clear_tilt();
1038                                         else clear_track();
1039                                 }
1040                                 else {
1041                                         if(G.obedit) transform('t');
1042                                         else texspace_edit();
1043                                 }
1044                                 
1045                                 break;
1046                         case UKEY:
1047                                 if(G.obedit) {
1048                                         if(G.obedit->type==OB_MESH) remake_editMesh();
1049                                         else if(G.obedit->type==OB_ARMATURE) remake_editArmature();
1050                                         else if ELEM(G.obedit->type, OB_CURVE, OB_SURF) remake_editNurb();
1051                                         else if(G.obedit->type==OB_LATTICE) remake_editLatt();
1052                                 }
1053                                 else if(G.f & G_FACESELECT) uv_autocalc_tface();
1054                                 else if(G.f & G_WEIGHTPAINT) wpaint_undo();
1055                                 else if(G.f & G_VERTEXPAINT) vpaint_undo();
1056                                 else single_user();
1057                                 
1058                                 break;
1059                         case VKEY:
1060                                 if(G.qual==LR_SHIFTKEY) {
1061                                         if (G.obedit && G.obedit->type==OB_MESH) {
1062                                                 align_view_to_selected(v3d);
1063                                         } else if (G.f & G_FACESELECT) {
1064                                                 align_view_to_selected(v3d);
1065                                         }
1066                                 } else {
1067                                         if(G.obedit) {
1068                                                 if(G.obedit->type==OB_CURVE) {
1069                                                         sethandlesNurb(2);
1070                                                         makeDispList(G.obedit);
1071                                                         allqueue(REDRAWVIEW3D, 0);
1072                                                 }
1073                                         }
1074                                         else if(G.qual & LR_ALTKEY) image_aspect();
1075                                         else set_vpaint();
1076                                 }
1077                                 break;
1078                         case WKEY:
1079                                 if(G.qual & LR_SHIFTKEY) {
1080                                         transform('w');
1081                                 }
1082                                 else if(G.qual & LR_ALTKEY) {
1083                                         /* if(G.obedit && G.obedit->type==OB_MESH) write_videoscape(); */
1084                                 }
1085                                 else if(G.qual & LR_CTRLKEY) {
1086                                         if(G.obedit) {
1087                                                 if ELEM(G.obedit->type,  OB_CURVE, OB_SURF) {
1088                                                         switchdirectionNurb2();
1089                                                 }
1090                                         }
1091                                 }
1092                                 else special_editmenu();
1093                                 
1094                                 break;
1095                         case XKEY:
1096                         case DELKEY:
1097                                 delete_context_selected();
1098                                 break;
1099                         case YKEY:
1100                                 if(G.obedit) {
1101                                         if(G.obedit->type==OB_MESH) split_mesh();
1102                                 }
1103                                 break;
1104                         case ZKEY:
1105                                 toggle_shading();
1106                                 
1107                                 scrarea_queue_headredraw(curarea);
1108                                 scrarea_queue_winredraw(curarea);
1109                                 break;
1110                                 
1111                         
1112                         case HOMEKEY:
1113                                 view3d_home(0);
1114                                 break;
1115                         case COMMAKEY:
1116                                 G.vd->around= V3D_CENTRE;
1117                                 scrarea_queue_headredraw(curarea);
1118                                 break;
1119                                 
1120                         case PERIODKEY:
1121                                 G.vd->around= V3D_CURSOR;
1122                                 scrarea_queue_headredraw(curarea);
1123                                 break;
1124                         
1125                         case PADSLASHKEY:
1126                                 if(G.vd->localview) {
1127                                         G.vd->localview= 0;
1128                                         endlocalview(curarea);
1129                                 }
1130                                 else {
1131                                         G.vd->localview= 1;
1132                                         initlocalview();
1133                                 }
1134                                 scrarea_queue_headredraw(curarea);
1135                                 break;
1136                         case PADASTERKEY:       /* '*' */
1137                                 ob= OBACT;
1138                                 if(ob) {
1139                                         obmat_to_viewmat(ob);
1140                                         if(G.vd->persp==2) G.vd->persp= 1;
1141                                         scrarea_queue_winredraw(curarea);
1142                                 }
1143                                 break;
1144                         case PADPERIOD: /* '.' */
1145                                 centreview();
1146                                 break;
1147                         
1148                         case PAGEUPKEY:
1149                                 if(G.qual & LR_CTRLKEY) movekey_obipo(1);
1150                                 else nextkey_obipo(1);  /* in editipo.c */
1151                                 break;
1152
1153                         case PAGEDOWNKEY:
1154                                 if(G.qual & LR_CTRLKEY) movekey_obipo(-1);
1155                                 else nextkey_obipo(-1);
1156                                 break;
1157                                 
1158                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1159                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1160                         case PADMINUS: case PADPLUSKEY: case PADENTER:
1161                                 persptoetsen(event);
1162                                 doredraw= 1;
1163                                 break;
1164                         
1165                         case ESCKEY:
1166                                 if (G.vd->flag & V3D_DISPIMAGE) {
1167                                         G.vd->flag &= ~V3D_DISPIMAGE;
1168                                         doredraw= 1;
1169                                 }
1170                                 break;
1171                         }
1172                 }
1173         }
1174         
1175         if(doredraw) {
1176                 scrarea_queue_winredraw(curarea);
1177                 scrarea_queue_headredraw(curarea);
1178         }
1179 }
1180
1181 void initview3d(ScrArea *sa)
1182 {
1183         View3D *vd;
1184         
1185         vd= MEM_callocN(sizeof(View3D), "initview3d");
1186         BLI_addhead(&sa->spacedata, vd);        /* addhead! not addtail */
1187
1188         vd->spacetype= SPACE_VIEW3D;
1189         vd->viewquat[0]= 1.0;
1190         vd->viewquat[1]= vd->viewquat[2]= vd->viewquat[3]= 0.0;
1191         vd->persp= 1;
1192         vd->drawtype= OB_WIRE;
1193         vd->view= 7;
1194         vd->dist= 10.0;
1195         vd->lens= 35.0;
1196         vd->near= 0.01;
1197         vd->far= 500.0;
1198         vd->grid= 1.0;
1199         vd->gridlines= 16;
1200         vd->lay= vd->layact= 1;
1201         if(G.scene) {
1202                 vd->lay= vd->layact= G.scene->lay;
1203                 vd->camera= G.scene->camera;
1204         }
1205         vd->scenelock= 1;
1206 }
1207
1208
1209 /* ******************** SPACE: IPO ********************** */
1210
1211 static void changeview2dspace(ScrArea *sa, void *spacedata)
1212 {
1213         if(G.v2d==0) return;
1214
1215         test_view2d(G.v2d, curarea->winx, curarea->winy);
1216         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
1217 }
1218
1219 void winqreadipospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1220 {
1221         unsigned short event= evt->event;
1222         short val= evt->val;
1223         SpaceIpo *sipo= curarea->spacedata.first;
1224         View2D *v2d= &sipo->v2d;
1225         float dx, dy;
1226         int cfra, doredraw= 0;
1227         short mval[2];
1228         
1229         if(curarea->win==0) return;
1230
1231         if(val) {
1232                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1233
1234                 switch(event) {
1235                 case UI_BUT_EVENT:
1236                         if(val>0) do_ipowin_buts(val-1);
1237                         break;
1238                 case LEFTMOUSE:
1239                         if( in_ipo_buttons() ) {
1240                                 do_ipo_selectbuttons();
1241                                 doredraw= 1;
1242                         }                       
1243                         else if(G.qual & LR_CTRLKEY) add_vert_ipo();
1244                         else {
1245                                 do {
1246                                         getmouseco_areawin(mval);
1247                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
1248                                         
1249                                         cfra= (int)dx;
1250                                         if(cfra< 1) cfra= 1;
1251                                         
1252                                         if( cfra!=CFRA ) {
1253                                                 CFRA= cfra;
1254                                                 update_for_newframe();
1255                                                 force_draw_plus(SPACE_VIEW3D);
1256                                                 force_draw_plus(SPACE_ACTION);
1257                                                 force_draw_plus(SPACE_BUTS);    /* To make constraint sliders redraw */
1258                                         }
1259                                 
1260                                 } while(get_mbut()&L_MOUSE);
1261                         }
1262                         
1263                         break;
1264                 case MIDDLEMOUSE:
1265                         if(in_ipo_buttons()) {
1266                                 scroll_ipobuts();
1267                         }
1268                         else view2dmove(event); /* in drawipo.c */
1269                         break;
1270
1271                 case WHEELUPMOUSE:
1272                 case WHEELDOWNMOUSE:
1273                         view2dmove(event);      /* in drawipo.c */
1274                         break;
1275
1276                 case RIGHTMOUSE:
1277                         mouse_select_ipo();
1278                         allqueue (REDRAWACTION, 0);
1279                         allqueue(REDRAWNLA, 0);
1280                         break;
1281                 case PADPLUSKEY:
1282                         view2d_zoom(v2d, 0.1154, curarea->winx, curarea->winy);
1283                         doredraw= 1;
1284                         break;
1285                 case PADMINUS:
1286                         view2d_zoom(v2d, -0.15, curarea->winx, curarea->winy);
1287                         doredraw= 1;
1288                         break;
1289                 case PAGEUPKEY:
1290                         if(G.qual & LR_CTRLKEY) movekey_ipo(1);
1291                         else nextkey_ipo(1);
1292                         break;
1293                 case PAGEDOWNKEY:
1294                         if(G.qual & LR_CTRLKEY) movekey_ipo(-1);
1295                         else nextkey_ipo(-1);
1296                         break;
1297                 case HOMEKEY:
1298                         do_ipo_buttons(B_IPOHOME);
1299                         break;
1300                         
1301                 case AKEY:
1302                         if(in_ipo_buttons()) {
1303                                 swap_visible_editipo();
1304                         }
1305                         else swap_selectall_editipo();
1306                         allspace (REMAKEIPO, 0);
1307                         allqueue (REDRAWNLA, 0);
1308                         allqueue (REDRAWACTION, 0);
1309                         break;
1310                 case BKEY:
1311                         borderselect_ipo();
1312                         break;
1313                 case CKEY:
1314                         move_to_frame();
1315                         break;
1316                 case DKEY:
1317                         if(G.qual & LR_SHIFTKEY) add_duplicate_editipo();
1318                         break;
1319                 case GKEY:
1320                         transform_ipo('g');
1321                         break;
1322                 case HKEY:
1323                         if(G.qual & LR_SHIFTKEY) sethandles_ipo(HD_AUTO);
1324                         else sethandles_ipo(HD_ALIGN);
1325                         break;
1326                 case JKEY:
1327                         join_ipo();
1328                         break;
1329                 case KKEY:
1330                         ipo_toggle_showkey();
1331                         scrarea_queue_headredraw(curarea);
1332                         allqueue(REDRAWVIEW3D, 0);
1333                         doredraw= 1;
1334                         break;
1335                 case RKEY:
1336                         ipo_record();
1337                         break;
1338                 case SKEY:
1339                         if(G.qual & LR_SHIFTKEY) ipo_snapmenu();
1340                         else transform_ipo('s');
1341                         break;
1342                 case TKEY:
1343                         set_ipotype();
1344                         break;
1345                 case VKEY:
1346                         sethandles_ipo(HD_VECT);
1347                         break;
1348                 case XKEY:
1349                 case DELKEY:
1350                         if(G.qual & LR_SHIFTKEY) delete_key();
1351                         else del_ipo();
1352                         break;
1353                 }
1354         }
1355
1356         if(doredraw) scrarea_queue_winredraw(curarea);
1357 }
1358
1359 void initipo(ScrArea *sa)
1360 {
1361         SpaceIpo *sipo;
1362         
1363         sipo= MEM_callocN(sizeof(SpaceIpo), "initipo");
1364         BLI_addhead(&sa->spacedata, sipo);
1365
1366         sipo->spacetype= SPACE_IPO;
1367         /* sipo space loopt van (0,-?) tot (??,?) */
1368         sipo->v2d.tot.xmin= 0.0;
1369         sipo->v2d.tot.ymin= -10.0;
1370         sipo->v2d.tot.xmax= G.scene->r.efra;
1371         sipo->v2d.tot.ymax= 10.0;
1372
1373         sipo->v2d.cur= sipo->v2d.tot;
1374
1375         sipo->v2d.min[0]= 0.01;
1376         sipo->v2d.min[1]= 0.01;
1377
1378         sipo->v2d.max[0]= 15000.0;
1379         sipo->v2d.max[1]= 10000.0;
1380         
1381         sipo->v2d.scroll= L_SCROLL+B_SCROLL;
1382         sipo->v2d.keeptot= 0;
1383
1384         sipo->blocktype= ID_OB;
1385 }
1386
1387 /* ******************** SPACE: INFO ********************** */
1388
1389 void space_mipmap_button_function(int event) {
1390         set_mipmap(!(U.gameflags & USERDEF_DISABLE_MIPMAP));
1391
1392         allqueue(REDRAWVIEW3D, 0);
1393 }
1394
1395 void space_sound_button_function(int event)
1396 {
1397         int a;
1398         SYS_SystemHandle syshandle;
1399
1400         if ((syshandle = SYS_GetSystem()))
1401         {
1402                 a = (U.gameflags & USERDEF_DISABLE_SOUND);
1403                 SYS_WriteCommandLineInt(syshandle, "noaudio", a);
1404         }
1405 }
1406
1407
1408 void drawinfospace(ScrArea *sa, void *spacedata)
1409 {
1410         uiBlock *block;
1411         float fac;
1412         short xpos, ypos, ypostab,  buth, rspace, dx, y1, y2, y3, y4, y2label, y3label, y4label;
1413         short smallprefbut, medprefbut, largeprefbut, smfileselbut;
1414         short edgespace, midspace;
1415         char naam[32];
1416
1417         if(curarea->win==0) return;
1418
1419         glClearColor(0.5, 0.5, 0.5, 0.0); 
1420         glClear(GL_COLOR_BUFFER_BIT);
1421
1422         fac= ((float)curarea->winx)/1280.0;
1423         myortho2(0.0, 1280.0, 0.0, curarea->winy/fac);
1424         
1425         sprintf(naam, "infowin %d", curarea->win);
1426         block= uiNewBlock(&curarea->uiblocks, naam, UI_EMBOSSX, UI_HELV, curarea->win);
1427
1428
1429         dx= (1280-90)/6;        /* spacing for use in equally dividing 'tab' row */
1430
1431         xpos = 45;              /* left padding */
1432         ypos = 50;              /* bottom padding for buttons */
1433         ypostab = 10;           /* bottom padding for 'tab' row */
1434
1435
1436         buth = 20;              /* standard button height */
1437
1438         smallprefbut = 94;      /* standard size for small preferences button */
1439         medprefbut = 193;       /* standard size for medium preferences button */
1440         largeprefbut = 292;     /* standard size for large preferences button */
1441         smfileselbut = buth;    /* standard size for fileselect button (square) */
1442         
1443         edgespace = 3;          /* space from edge of end 'tab' to edge of end button */
1444         midspace = 5;           /* horizontal space between buttons */
1445
1446         rspace = 3;             /* default space between rows */
1447
1448         y1 = ypos;              /* bottom padding of 1st (bottom) button row */
1449         y2 = ypos+buth+rspace;  /* bottom padding of 2nd button row */
1450         y3 = ypos+2*(buth+rspace)+(3*rspace);   /* bottom padding of 3rd button row */
1451         y4 = ypos+3*(buth+rspace)+(3*rspace);   /* bottom padding of 4th button row */
1452
1453         y2label = y2-2;         /* adjustments to offset the labels down to align better */
1454         y3label = y3-(3*rspace)-2;      /* again for 3rd row */
1455         y4label = y4-2;         /* again for 4th row */
1456
1457
1458         /* set the colour to blue and draw the main 'tab' controls */
1459
1460         uiBlockSetCol(block, BUTBLUE);
1461
1462         uiDefButI(block, ROW,B_USERPREF,"View & Controls",
1463                 xpos,ypostab,(short)dx,buth,
1464                 &U.userpref,1.0,0.0, 0, 0,"");
1465                 
1466         uiDefButI(block, ROW,B_USERPREF,"Edit Methods",
1467                 (short)(xpos+dx),ypostab,(short)dx,buth,
1468                 &U.userpref,1.0,1.0, 0, 0,"");
1469
1470         uiDefButI(block, ROW,B_USERPREF,"Language & Font",
1471                 (short)(xpos+2*dx),ypostab,(short)dx,buth,
1472                 &U.userpref,1.0,2.0, 0, 0,"");
1473
1474         uiDefButI(block, ROW,B_USERPREF,"Auto Save",
1475                 (short)(xpos+3*dx),ypostab,(short)dx,buth,
1476                 &U.userpref,1.0,3.0, 0, 0,"");
1477
1478         uiDefButI(block, ROW,B_USERPREF,"System & OpenGL",
1479                 (short)(xpos+5*dx),ypostab,(short)dx,buth,
1480                 &U.userpref,1.0,4.0, 0, 0,"");
1481
1482         uiDefButI(block, ROW,B_USERPREF,"File Paths",
1483                 (short)(xpos+4*dx),ypostab,(short)dx,buth,
1484                 &U.userpref,1.0,5.0, 0, 0,"");
1485
1486         uiBlockSetEmboss(block, UI_EMBOSSX);
1487         uiBlockSetCol(block, BUTGREY);
1488
1489         /* end 'tab' controls */
1490
1491         /* line 2: left x co-ord, top y co-ord, width, height */
1492
1493         if (U.userpref == 0) { /* view & controls */
1494
1495                 uiDefBut(block, LABEL,0,"Display:",
1496                         xpos,y3label,medprefbut,buth,
1497                         0, 0, 0, 0, 0, "");
1498
1499                 uiDefButS(block, TOG|BIT|11, 0, "ToolTips",
1500                         (xpos+edgespace),y2,smallprefbut,buth,
1501                         &(U.flag), 0, 0, 0, 0,
1502                         "Displays tooltips (help tags) over buttons");
1503
1504                 uiDefButS(block, TOG|BIT|4, B_DRAWINFO, "Object Info",
1505                         (xpos+edgespace+midspace+smallprefbut),y2,smallprefbut,buth,
1506                         &(U.uiflag), 0, 0, 0, 0,
1507                         "Displays current object name and frame number in the 3D viewport");
1508
1509                 uiDefButS(block, TOG|BIT|4, 0, "Global Scene",
1510                         (xpos+edgespace),y1,medprefbut,buth,
1511                         &(U.flag), 0, 0, 0, 0,
1512                         "Forces the current Scene to be displayed in all Screens");
1513
1514
1515                 uiDefBut(block, LABEL,0,"Snap to grid:",
1516                         (xpos+edgespace+medprefbut),y3label,medprefbut,buth,
1517                         0, 0, 0, 0, 0, "");
1518
1519                 uiDefButS(block, TOG|BIT|1, 0, "Grab",
1520                         (xpos+edgespace+medprefbut+midspace),y2,smallprefbut,buth,
1521                         &(U.flag), 0, 0, 0, 0,
1522                         "Move objects to grid units");
1523
1524                 uiDefButS(block, TOG|BIT|2, 0, "Rotate",
1525                         (xpos+edgespace+medprefbut+(2*midspace)+smallprefbut),y2,smallprefbut,buth,
1526                         &(U.flag), 0, 0, 0, 0,
1527                         "Rotate objects to grid units");
1528
1529                 uiDefButS(block, TOG|BIT|3, 0, "Size",
1530                         (xpos+edgespace+medprefbut+(3*midspace)+(2*smallprefbut)),y2,smallprefbut,buth,
1531                         &(U.flag), 0, 0, 0, 0,
1532                         "Scale objects to grid units");
1533
1534
1535                         
1536
1537                 uiBlockSetCol(block, BUTGREEN);
1538
1539                 uiDefButS(block, TOGN|BIT|10, B_DRAWINFO, "Rotate View",
1540                         (xpos+edgespace+(4*midspace)+(4*medprefbut)),y2,(smallprefbut+2),buth,
1541                         &(U.flag), 0, 0, 0, 0, "Default action for the middle mouse button");
1542
1543                 uiDefButS(block, TOG|BIT|10, B_DRAWINFO, "Pan View",
1544                         (xpos+edgespace+(4*midspace)+(4*medprefbut)+smallprefbut+2),y2,(smallprefbut+2),buth,
1545                         &(U.flag), 0, 0, 0, 0, "Default action for the middle mouse button");
1546
1547                 uiBlockSetCol(block, BUTGREY);
1548                 
1549                 uiDefBut(block, LABEL,0,"Middle mouse button:",
1550                         (xpos+edgespace+(3*midspace)+(4*medprefbut)),y3label,medprefbut,buth,
1551                         0, 0, 0, 0, 0, "");
1552                 uiDefButS(block, TOG|BIT|12, 0, "Emulate 3 Buttons",
1553                         (xpos+edgespace+(4*midspace)+(4*medprefbut)),y1,medprefbut,buth,
1554                         &(U.flag), 0, 0, 0, 0,
1555                         "Emulates a middle mouse button with ALT+LeftMouse");
1556
1557
1558
1559                 uiDefBut(block, LABEL,0,"View rotation method:",
1560                         (xpos+edgespace+(3*midspace)+(3*medprefbut)),y3label,medprefbut,buth,
1561                         0, 0, 0, 0, 0, "");
1562
1563                 uiBlockSetCol(block, BUTGREEN);
1564
1565                 uiDefButS(block, TOG|BIT|5, B_DRAWINFO, "Trackball",
1566                         (xpos+edgespace+(3*midspace)+(3*medprefbut)),y2,(smallprefbut+2),buth,
1567                         &(U.flag), 0, 0, 0, 0,
1568                         "Use trackball style rotation with middle mouse button");
1569
1570                 uiDefButS(block, TOGN|BIT|5, B_DRAWINFO, "Turntable",
1571                         (xpos+edgespace+(3*midspace)+(3*medprefbut)+smallprefbut+2),y2,(smallprefbut+2),buth,
1572                         &(U.flag), 0, 0, 0, 0,
1573                         "Use turntable style rotation with middle mouse button");
1574
1575                 uiBlockSetCol(block, BUTGREY);
1576
1577                 uiDefBut(block, LABEL,0,"Mousewheel:",
1578                         (xpos+edgespace+(4*midspace)+(5*medprefbut)),y3label,medprefbut,buth,
1579                         0, 0, 0, 0, 0, "");
1580                 uiDefButS(block, TOG|BIT|2, 0, "Invert Wheel Zoom",
1581                         (xpos+edgespace+(5*midspace)+(5*medprefbut)),y1,medprefbut,buth,
1582                         &(U.uiflag), 0, 0, 0, 0,
1583                         "Swaps mouse wheel zoom direction");
1584
1585
1586                 uiDefButI(block, NUM, 0, "Scroll Lines:",
1587                         (xpos+edgespace+(5*midspace)+(5*medprefbut)),y2,medprefbut,buth,
1588                         &U.wheellinescroll, 0.0, 32.0, 0, 0,
1589                         "The number of lines scrolled at a time with the mouse wheel");
1590
1591
1592         } else if (U.userpref == 1) { /* edit methods */
1593
1594
1595                 uiDefBut(block, LABEL,0,"Material linked to:",
1596                         xpos,y3label,medprefbut,buth,
1597                         0, 0, 0, 0, 0, "");
1598
1599                 uiBlockSetCol(block, BUTGREEN);
1600
1601                 uiDefButS(block, TOGN|BIT|8, B_DRAWINFO, "ObData",
1602                         (xpos+edgespace),y2,(smallprefbut+2),buth,
1603                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the obData block");
1604
1605                 uiDefButS(block, TOG|BIT|8, B_DRAWINFO, "Object",
1606                         (xpos+edgespace+smallprefbut+2),y2,(smallprefbut+2),buth,
1607                         &(U.flag), 0, 0, 0, 0, "Link new objects' material to the object block");
1608
1609                 uiBlockSetCol(block, BUTGREY);
1610
1611
1612
1613                 uiDefBut(block, LABEL,0,"Auto keyframe on:",
1614                         (xpos+edgespace+(2*medprefbut)+midspace),y3label,medprefbut,buth,
1615                         0, 0, 0, 0, 0, "");
1616
1617                 uiDefButS(block, TOG|BIT|0, 0, "Action",
1618                         (xpos+edgespace+(2*medprefbut)+(2*midspace)),y2,smallprefbut,buth,
1619                         &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in action ipo curve");
1620
1621                 uiDefButS(block, TOG|BIT|1, 0, "Object",
1622                         (xpos+edgespace+(2*medprefbut)+(3*midspace)+smallprefbut),y2,smallprefbut,buth,
1623                         &(U.uiflag), 0, 0, 0, 0, "Automatic keyframe insertion in object ipo curve");
1624
1625
1626
1627                 uiDefBut(block, LABEL,0,"Duplicate with object:",
1628                         (xpos+edgespace+(3*midspace)+(3*medprefbut)+smallprefbut),y3label,medprefbut,buth,
1629                         0, 0, 0, 0, 0, "");
1630
1631                 uiDefButS(block, TOG|BIT|0, 0, "Mesh",
1632                         (xpos+edgespace+(4*midspace)+(3*medprefbut)+smallprefbut),y2,smallprefbut,buth,
1633                         &(U.dupflag), 0, 0, 0, 0, "Causes mesh data to be duplicated with Shift+D");
1634                 uiDefButS(block, TOG|BIT|9, 0, "Armature",
1635                         (xpos+edgespace+(4*midspace)+(3*medprefbut)+smallprefbut),y1,smallprefbut,buth,
1636                         &(U.dupflag), 0, 0, 0, 0, "Causes armature data to be duplicated with Shift+D");
1637
1638                 uiDefButS(block, TOG|BIT|2, 0, "Surface",
1639                         (xpos+edgespace+(5*midspace)+(3*medprefbut)+(2*smallprefbut)),y2,smallprefbut,buth,
1640                         &(U.dupflag), 0, 0, 0, 0, "Causes surface data to be duplicated with Shift+D");
1641                 uiDefButS(block, TOG|BIT|5, 0, "Lamp",
1642                         (xpos+edgespace+(5*midspace)+(3*medprefbut)+(2*smallprefbut)),y1,smallprefbut,buth,
1643                         &(U.dupflag), 0, 0, 0, 0, "Causes lamp data to be duplicated with Shift+D");
1644
1645                 uiDefButS(block, TOG|BIT|1, 0, "Curve",
1646                         (xpos+edgespace+(6*midspace)+(3*medprefbut)+(3*smallprefbut)),y2,smallprefbut,buth,
1647                         &(U.dupflag), 0, 0, 0, 0, "Causes curve data to be duplicated with Shift+D");
1648                 uiDefButS(block, TOG|BIT|7, 0, "Material",
1649                         (xpos+edgespace+(6*midspace)+(3*medprefbut)+(3*smallprefbut)),y1,smallprefbut,buth,
1650                         &(U.dupflag), 0, 0, 0, 0, "Causes material data to be duplicated with Shift+D");
1651
1652                 uiDefButS(block, TOG|BIT|3, 0, "Text",
1653                         (xpos+edgespace+(7*midspace)+(3*medprefbut)+(4*smallprefbut)),y2,smallprefbut,buth,
1654                         &(U.dupflag), 0, 0, 0, 0, "Causes text data to be duplicated with Shift+D");
1655                 uiDefButS(block, TOG|BIT|8, 0, "Texture",
1656                         (xpos+edgespace+(7*midspace)+(3*medprefbut)+(4*smallprefbut)),y1,smallprefbut,buth,
1657                         &(U.dupflag), 0, 0, 0, 0, "Causes texture data to be duplicated with Shift+D");
1658
1659                 uiDefButS(block, TOG|BIT|4, 0, "Metaball",
1660                         (xpos+edgespace+(8*midspace)+(3*medprefbut)+(5*smallprefbut)),y2,smallprefbut,buth,
1661                         &(U.dupflag), 0, 0, 0, 0, "Causes metaball data to be duplicated with Shift+D");
1662                 uiDefButS(block, TOG|BIT|6, 0, "Ipo",
1663                         (xpos+edgespace+(8*midspace)+(3*medprefbut)+(5*smallprefbut)),y1,smallprefbut,buth,
1664                         &(U.dupflag), 0, 0, 0, 0, "Causes ipo data to be duplicated with Shift+D");
1665
1666
1667         } else if(U.userpref == 2) { /* language & colors */
1668
1669 #ifdef INTERNATIONAL
1670                 char curfont[64];
1671
1672                 sprintf(curfont, "Interface Font: ");
1673                 strcat(curfont,U.fontname);
1674
1675                 uiDefButS(block, TOG|BIT|5, B_DOLANGUIFONT, "International Fonts",
1676                         xpos,y2,medprefbut,buth,
1677                         &(U.transopts), 0, 0, 0, 0, "Activate international interface");
1678
1679                 if(U.transopts & TR_ALL) {
1680                         uiDefBut(block, LABEL,0,curfont,
1681                                 (xpos+edgespace+medprefbut+midspace),y2,medprefbut,buth,
1682                                 0, 0, 0, 0, 0, "");
1683
1684                         uiBlockSetCol(block, BUTSALMON);
1685         //(xpos+edgespace)
1686                         uiDefBut(block, BUT, B_LOADUIFONT, "Select Font",
1687                                 xpos,y1,medprefbut,buth,
1688                                 0, 0, 0, 0, 0, "Select a new font for the interface");
1689
1690                         uiBlockSetCol(block, BUTGREY);
1691
1692
1693                         uiDefButI(block, MENU|INT, B_SETFONTSIZE, fontsize_pup(),
1694                                 (xpos+edgespace+medprefbut+midspace),y1,medprefbut,buth,
1695                                 &U.fontsize, 0, 0, 0, 0, "Current interface font size (points)");
1696
1697 /*
1698                         uiDefButS(block, MENU|SHO, B_SETENCODING, encoding_pup(),
1699                                 (xpos+edgespace+medprefbut+midspace),y1,medprefbut,buth,
1700                                 &U.encoding, 0, 0, 0, 0, "Current interface font encoding");
1701
1702
1703                         uiDefBut(block, LABEL,0,"Translate:",
1704                                 (xpos+edgespace+(2.1*medprefbut)+(2*midspace)),y3label,medprefbut,buth,
1705                                 0, 0, 0, 0, 0, "");
1706 */
1707
1708                         uiDefButS(block, TOG|BIT|0, B_SETTRANSBUTS, "Tooltips",
1709                                 (xpos+edgespace+(2.2*medprefbut)+(3*midspace)),y1,smallprefbut,buth,
1710                                 &(U.transopts), 0, 0, 0, 0, "Translate tooltips");
1711
1712                         uiDefButS(block, TOG|BIT|1, B_SETTRANSBUTS, "Buttons",
1713                                 (xpos+edgespace+(2.2*medprefbut)+(4*midspace)+smallprefbut),y1,smallprefbut,buth,
1714                                 &(U.transopts), 0, 0, 0, 0, "Translate button labels");
1715
1716                         uiDefButS(block, TOG|BIT|2, B_SETTRANSBUTS, "Toolbox",
1717                                 (xpos+edgespace+(2.2*medprefbut)+(5*midspace)+(2*smallprefbut)),y1,smallprefbut,buth,
1718                                 &(U.transopts), 0, 0, 0, 0, "Translate toolbox menu");
1719
1720                         uiDefButS(block, MENU|SHO, B_SETLANGUAGE, language_pup(),
1721                                 (xpos+edgespace+(2.2*medprefbut)+(3*midspace)),y2,medprefbut+(0.5*medprefbut)+3,buth,
1722                                 &U.language, 0, 0, 0, 0, "Select interface language");
1723                                 
1724                         /* uiDefButS(block, TOG|BIT|3, B_SETTRANSBUTS, "FTF All windows",
1725                                 (xpos+edgespace+(4*medprefbut)+(4*midspace)),y1,medprefbut,buth,
1726                                 &(U.transopts), 0, 0, 0, 0,
1727                                 "Use FTF drawing for fileselect and textwindow "
1728                                 "(under construction)");
1729                         */
1730                 }
1731
1732 /* end of INTERNATIONAL */
1733 #endif
1734
1735         } else if(U.userpref == 3) { /* auto save */
1736
1737
1738                 uiDefButS(block, TOG|BIT|0, B_RESETAUTOSAVE, "Auto Save Temp Files",
1739                         (xpos+edgespace),y2,medprefbut,buth,
1740                         &(U.flag), 0, 0, 0, 0,
1741                         "Enables automatic saving of temporary files");
1742
1743                 if(U.flag & AUTOSAVE) {
1744                         uiBlockSetCol(block, BUTSALMON);
1745
1746                         uiDefBut(block, BUT, B_LOADTEMP, "Open Recent",
1747                                 (xpos+edgespace),y1,medprefbut,buth,
1748                                 0, 0, 0, 0, 0,"Opens the most recently saved temporary file");
1749
1750                         uiBlockSetCol(block, BUTGREY);
1751
1752
1753                         uiDefButI(block, NUM, B_RESETAUTOSAVE, "Minutes:",
1754                                 (xpos+edgespace+medprefbut+midspace),y2,medprefbut,buth,
1755                                 &(U.savetime), 1.0, 60.0, 0, 0,
1756                                 "The time (in minutes) to wait between automatic temporary saves");
1757
1758                         uiDefButS(block, NUM, 0, "Versions:",
1759                                 (xpos+edgespace+medprefbut+midspace),y1,medprefbut,buth,
1760                                 &U.versions, 0.0, 32.0, 0, 0,
1761                                 "The number of old versions to maintain when saving");
1762                 }
1763
1764         } else if (U.userpref == 4) { /* system & opengl */
1765
1766
1767 /*
1768                 uiDefButS(block, TOG|BIT|5, 0, "Log Events to Console",
1769                         (xpos+edgespace),y2,largeprefbut,buth,
1770                         &(U.uiflag), 0, 0, 0, 0, "Display a list of input events in the console");
1771
1772                 uiDefButS(block, MENU|SHO, B_CONSOLEOUT, consolemethod_pup(),
1773                         (xpos+edgespace), y1, largeprefbut,buth,
1774                         &U.console_out, 0, 0, 0, 0, "Select console output method");
1775
1776                 uiDefButS(block, NUM, B_CONSOLENUMLINES, "Lines:",
1777                         (xpos+edgespace+largeprefbut+midspace),y1,smallprefbut,buth,
1778                         &U.console_buffer, 1.0, 4000.0, 0, 0, "Maximum number of internal console lines");
1779 */
1780
1781 #ifdef _WIN32
1782                 uiDefBut(block, LABEL,0,"Win Codecs:",
1783                         (xpos+edgespace+(1*midspace)+(1*medprefbut)),y3label,medprefbut,buth,
1784                         0, 0, 0, 0, 0, "");
1785
1786                 uiDefButS(block, TOG|BIT|8, 0, "Enable all codecs",
1787                         (xpos+edgespace+(1*medprefbut)+(1*midspace)),y2,medprefbut,buth,
1788                         &(U.uiflag), 0, 0, 0, 0, "Allows all codecs for rendering (not guaranteed)");
1789 #endif
1790
1791                 uiDefBut(block, LABEL,0,"Keyboard:",
1792                         (xpos+edgespace+(3*midspace)+(3*medprefbut)),y3label,medprefbut,buth,
1793                         0, 0, 0, 0, 0, "");
1794
1795                 uiDefButS(block, TOG|BIT|9, B_U_CAPSLOCK, "Disable Caps Lock",
1796                         (xpos+edgespace+(3*midspace)+(3*medprefbut)),y1,medprefbut,buth,
1797                         &(U.flag), 0, 0, 0, 0,
1798                         "Disables the Caps Lock key when entering text");
1799
1800                 uiDefButS(block, TOG|BIT|13, 0, "Emulate Numpad",
1801                         (xpos+edgespace+(3*midspace)+(3*medprefbut)),y2,medprefbut,buth,
1802                         &(U.flag), 0, 0, 0, 0,
1803                         "Causes the 1 to 0 keys to act as the numpad (useful for laptops)");
1804
1805
1806                 uiDefBut(block, LABEL,0,"System:",
1807                         (xpos+edgespace+(4*midspace)+(4*medprefbut)),y3label,medprefbut,buth,
1808                         0, 0, 0, 0, 0, "");
1809
1810                 uiDefButI(block, TOG|BIT|USERDEF_DISABLE_SOUND_BIT, B_SOUNDTOGGLE, "Disable Sound",
1811                         (xpos+edgespace+(4*medprefbut)+(4*midspace)),y2,medprefbut,buth,
1812                         &(U.gameflags), 0, 0, 0, 0, "Disables sounds from being played");
1813
1814                 uiDefButS(block, TOG|BIT|3, 0, "Filter File Extensions",
1815                         (xpos+edgespace+(4*medprefbut)+(4*midspace)),y1,medprefbut,buth,
1816                         &(U.uiflag), 0, 0, 0, 0, "Display only files with extensions in the image select window");
1817
1818
1819                 uiDefBut(block, LABEL,0,"OpenGL:",
1820                         (xpos+edgespace+(5*midspace)+(5*medprefbut)),y3label,medprefbut,buth,
1821                         0, 0, 0, 0, 0, "");
1822
1823                 uiDefButI(block, TOGN|BIT|USERDEF_DISABLE_MIPMAP_BIT, B_MIPMAPCHANGED, "Mipmaps",
1824                         (xpos+edgespace+(5*medprefbut)+(5*midspace)),y2,medprefbut,buth,
1825                         &(U.gameflags), 0, 0, 0, 0, "Toggles between mipmap textures on (beautiful) and off (fast)");
1826
1827                 uiDefButI(block, TOG|BIT|USERDEF_VERTEX_ARRAYS_BIT, 0, "Vertex Arrays",
1828                         (xpos+edgespace+(5*medprefbut)+(5*midspace)),y1,medprefbut,buth,
1829                         &(U.gameflags), 0, 0, 0, 0, "Toggles between vertex arrays on (less reliable) and off (more reliable)");
1830
1831                 uiDefBut(block, LABEL,0,"Audio:",
1832                         (xpos+edgespace+(2*midspace)+(2*medprefbut)),y3label,medprefbut,buth,
1833                         0, 0, 0, 0, 0, "");
1834
1835                 uiDefButI(block, ROW, 0, "Mixing buffer 256", (xpos+edgespace+(2*midspace)+(2*medprefbut)),y2,medprefbut,buth, &U.mixbufsize, 2.0, 256.0, 0, 0, "Set audio buffer size to 256 samples");
1836                 uiDefButI(block, ROW, 0, "512", (xpos+edgespace+(2*midspace)+(2*medprefbut)),y1,61,buth, &U.mixbufsize, 2.0, 512.0, 0, 0, "Set audio buffer size to 512 samples");      
1837                 uiDefButI(block, ROW, 0, "1024", (xpos+edgespace+(2*midspace)+(2*medprefbut))+61+midspace,y1,61,buth, &U.mixbufsize, 2.0, 1024.0, 0, 0, "Set audio buffer size to 1024 samples");               
1838                 uiDefButI(block, ROW, 0, "2048", (xpos+edgespace+(2*midspace)+(2*medprefbut))+2*(61+midspace),y1,61,buth, &U.mixbufsize, 2.0, 2048.0, 0, 0, "Set audio buffer size to 2048 samples");                   
1839
1840         } else if(U.userpref == 5) { /* file paths */
1841
1842
1843                 uiDefBut(block, TEX, 0, "Fonts: ",
1844                         (xpos+edgespace),y2,(largeprefbut-smfileselbut),buth,
1845                         U.fontdir, 1.0, 63.0, 0, 0,
1846                         "The default directory to search for loading fonts");
1847                 uiDefIconBut(block, BUT, B_FONTDIRFILESEL, ICON_FILESEL,
1848                         (xpos+edgespace+largeprefbut-smfileselbut),y2,smfileselbut,buth,
1849                         0, 0, 0, 0, 0, "Select the default font directory");
1850
1851                 uiDefBut(block, TEX, 0, "Textures: ",
1852                         (xpos+edgespace+largeprefbut+midspace),y2,(largeprefbut-smfileselbut),buth,
1853                         U.textudir, 1.0, 63.0, 0, 0, "The default directory to search for textures");
1854                 uiDefIconBut(block, BUT, B_TEXTUDIRFILESEL, ICON_FILESEL,
1855                         (xpos+edgespace+(2*largeprefbut)+midspace-smfileselbut),y2,smfileselbut,buth,
1856                         0, 0, 0, 0, 0, "Select the default texture location");
1857
1858
1859                 uiDefBut(block, TEX, 0, "Tex Plugins: ",
1860                         (xpos+edgespace+(2*largeprefbut)+(2*midspace)),y2,(largeprefbut-smfileselbut),buth,
1861                         U.plugtexdir, 1.0, 63.0, 0, 0, "The default directory to search for texture plugins");
1862                 uiDefIconBut(block, BUT, B_PLUGTEXDIRFILESEL, ICON_FILESEL,
1863                         (xpos+edgespace+(3*largeprefbut)+(2*midspace)-smfileselbut),y2,smfileselbut,buth,
1864                         0, 0, 0, 0, 0, "Select the default texture plugin location");
1865
1866                 uiDefBut(block, TEX, 0, "Seq Plugins: ",
1867                         (xpos+edgespace+(3*largeprefbut)+(3*midspace)),y2,(largeprefbut-smfileselbut),buth,
1868                         U.plugseqdir, 1.0, 63.0, 0, 0, "The default directory to search for sequence plugins");
1869                 uiDefIconBut(block, BUT, B_PLUGSEQDIRFILESEL, ICON_FILESEL,
1870                         (xpos+edgespace+(4*largeprefbut)+(3*midspace)-smfileselbut),y2,smfileselbut,buth,
1871                         0, 0, 0, 0, 0, "Select the default sequence plugin location");
1872
1873
1874                 uiDefBut(block, TEX, 0, "Render: ",
1875                         (xpos+edgespace),y1,(largeprefbut-smfileselbut),buth,
1876                         U.renderdir, 1.0, 63.0, 0, 0, "The default directory for rendering output");
1877                 uiDefIconBut(block, BUT, B_RENDERDIRFILESEL, ICON_FILESEL,
1878                         (xpos+edgespace+largeprefbut-smfileselbut),y1,smfileselbut,buth,
1879                         0, 0, 0, 0, 0, "Select the default render output location");
1880
1881                 uiDefBut(block, TEX, 0, "Python: ",
1882                         (xpos+edgespace+largeprefbut+midspace),y1,(largeprefbut-smfileselbut),buth,
1883                         U.pythondir, 1.0, 63.0, 0, 0, "The default directory to search for Python scripts");
1884                 uiDefIconBut(block, BUT, B_PYTHONDIRFILESEL, ICON_FILESEL,
1885                         (xpos+edgespace+(2*largeprefbut)+midspace-smfileselbut),y1,smfileselbut,buth,
1886                         0, 0, 0, 0, 0, "Select the default Python script location");
1887
1888
1889                 uiDefBut(block, TEX, 0, "Sounds: ",
1890                         (xpos+edgespace+(2*largeprefbut)+(2*midspace)),y1,(largeprefbut-smfileselbut),buth,
1891                         U.sounddir, 1.0, 63.0, 0, 0, "The default directory to search for sounds");
1892                 uiDefIconBut(block, BUT, B_SOUNDDIRFILESEL, ICON_FILESEL,
1893                         (xpos+edgespace+(3*largeprefbut)+(2*midspace)-smfileselbut),y1,smfileselbut,buth,
1894                         0, 0, 0, 0, 0, "Select the default sound location");
1895
1896                 uiDefBut(block, TEX, 0, "Temp: ",
1897                          (xpos+edgespace+(3*largeprefbut)+(3*midspace)),y1,(largeprefbut-smfileselbut),buth,
1898                          U.tempdir, 1.0, 63.0, 0, 0, "The directory for storing temporary save files");
1899                 uiDefIconBut(block, BUT, B_TEMPDIRFILESEL, ICON_FILESEL,
1900                         (xpos+edgespace+(4*largeprefbut)+(3*midspace)-smfileselbut),y1,smfileselbut,buth,
1901                         0, 0, 0, 0, 0, "Select the default temporary save file location");
1902
1903         }
1904
1905         uiDrawBlock(block);
1906 }
1907
1908
1909 void winqreadinfospace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1910 {
1911         unsigned short event= evt->event;
1912         short val= evt->val;
1913
1914         if(val) {
1915                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1916
1917                 switch(event) {
1918                 case UI_BUT_EVENT:
1919                         do_global_buttons(val);
1920                         
1921                         break;  
1922                 }
1923         }
1924 }
1925
1926 void init_infospace(ScrArea *sa)
1927 {
1928         SpaceInfo *sinfo;
1929         
1930         sinfo= MEM_callocN(sizeof(SpaceInfo), "initinfo");
1931         BLI_addhead(&sa->spacedata, sinfo);
1932
1933         sinfo->spacetype=SPACE_INFO;
1934 }
1935
1936 /* ******************** SPACE: BUTS ********************** */
1937
1938 extern void drawbutspace(ScrArea *sa, void *spacedata); /* buttons.c */
1939
1940 static void changebutspace(ScrArea *sa, void *spacedata)
1941 {
1942         if(G.v2d==0) return;
1943         
1944         test_view2d(G.v2d, curarea->winx, curarea->winy);
1945         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin-0.6, G.v2d->cur.ymax+0.6);
1946 }
1947
1948 void winqreadbutspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
1949 {
1950         unsigned short event= evt->event;
1951         short val= evt->val;
1952         SpaceButs *sbuts= curarea->spacedata.first;
1953         ScrArea *sa2, *sa3d;
1954         int doredraw= 0;
1955
1956         if(val) {
1957                 
1958                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
1959
1960                 switch(event) {
1961                 case UI_BUT_EVENT:
1962                         do_blenderbuttons(val);
1963                         break;
1964                         
1965                 case MIDDLEMOUSE:
1966                 case WHEELUPMOUSE:
1967                 case WHEELDOWNMOUSE:
1968                         view2dmove(event);      /* in drawipo.c */
1969                         break;
1970
1971                 case PADPLUSKEY:
1972                         view2d_zoom(&sbuts->v2d, 0.06, curarea->winx, curarea->winy);
1973                         scrarea_queue_winredraw(curarea);
1974                         break;
1975                 case PADMINUS:
1976                         view2d_zoom(&sbuts->v2d, -0.075, curarea->winx, curarea->winy);
1977                         scrarea_queue_winredraw(curarea);
1978                         break;
1979                 case RENDERPREVIEW:
1980                         BIF_previewrender(sbuts);
1981                         break;
1982                 
1983                 case HOMEKEY:
1984                         do_buts_buttons(B_BUTSHOME);
1985                         break;
1986
1987
1988                 /* if only 1 view, also de persp, excluding arrowkeys */
1989                 case PAD0: case PAD1: case PAD3:
1990                 case PAD5: case PAD7: case PAD9:
1991                 case PADENTER: case ZKEY: case PKEY:
1992                         sa3d= 0;
1993                         sa2= G.curscreen->areabase.first;
1994                         while(sa2) {
1995                                 if(sa2->spacetype==SPACE_VIEW3D) {
1996                                         if(sa3d) return;
1997                                         sa3d= sa2;
1998                                 }
1999                                 sa2= sa2->next;
2000                         }
2001                         if(sa3d) {
2002                                 sa= curarea;
2003                                 areawinset(sa3d->win);
2004                                 
2005                                 if(event==PKEY) start_game();
2006                                 else if(event==ZKEY) toggle_shading();
2007                                 else persptoetsen(event);
2008                                 
2009                                 scrarea_queue_winredraw(sa3d);
2010                                 scrarea_queue_headredraw(sa3d);
2011                                 areawinset(sa->win);
2012                         }
2013                 }
2014         }
2015
2016         if(doredraw) scrarea_queue_winredraw(curarea);
2017 }
2018
2019 void set_rects_butspace(SpaceButs *buts)
2020 {
2021         /* buts space goes from (0,0) to (1280, 228) */
2022
2023         buts->v2d.tot.xmin= 0.0;
2024         buts->v2d.tot.ymin= 0.0;
2025         buts->v2d.tot.xmax= 1279.0;
2026         buts->v2d.tot.ymax= 228.0;
2027         
2028         buts->v2d.min[0]= 256.0;
2029         buts->v2d.min[1]= 42.0;
2030
2031         buts->v2d.max[0]= 1600.0;
2032         buts->v2d.max[1]= 450.0;
2033         
2034         buts->v2d.minzoom= 0.5;
2035         buts->v2d.maxzoom= 1.41;
2036         
2037         buts->v2d.scroll= 0;
2038         buts->v2d.keepaspect= 1;
2039         buts->v2d.keepzoom= 1;
2040         buts->v2d.keeptot= 1;
2041         
2042 }
2043
2044 void test_butspace(void)
2045 {
2046         ScrArea *area= curarea;
2047         int blocksmin= uiBlocksGetYMin(&area->uiblocks)-10.0;
2048         
2049         G.buts->v2d.tot.ymin= MIN2(0.0, blocksmin-10.0);
2050 }
2051
2052 void init_butspace(ScrArea *sa)
2053 {
2054         SpaceButs *buts;
2055         
2056         buts= MEM_callocN(sizeof(SpaceButs), "initbuts");
2057         BLI_addhead(&sa->spacedata, buts);
2058
2059         buts->spacetype= SPACE_BUTS;
2060         buts->scaflag= BUTS_SENS_LINK|BUTS_SENS_ACT|BUTS_CONT_ACT|BUTS_ACT_ACT|BUTS_ACT_LINK;
2061
2062         /* set_rects only does defaults, so after reading a file the cur has not changed */
2063         set_rects_butspace(buts);
2064         buts->v2d.cur= buts->v2d.tot;
2065 }
2066
2067 void extern_set_butspace(int fkey)
2068 {
2069         ScrArea *sa;
2070         SpaceButs *sbuts;
2071         
2072         /* when a f-key pressed: closest button window is initialized */
2073         if(curarea->spacetype==SPACE_BUTS) sa= curarea;
2074         else {
2075                 /* find area */
2076                 sa= G.curscreen->areabase.first;
2077                 while(sa) {
2078                         if(sa->spacetype==SPACE_BUTS) break;
2079                         sa= sa->next;
2080                 }
2081         }
2082         
2083         if(sa==0) return;
2084         
2085         if(sa!=curarea) areawinset(sa->win);
2086         
2087         sbuts= sa->spacedata.first;
2088         
2089         if(fkey==F4KEY) sbuts->mainb= BUTS_LAMP;
2090         else if(fkey==F5KEY) sbuts->mainb= BUTS_MAT;
2091         else if(fkey==F6KEY) sbuts->mainb= BUTS_TEX;
2092         else if(fkey==F7KEY) sbuts->mainb= BUTS_ANIM;
2093         else if(fkey==F8KEY) sbuts->mainb= BUTS_GAME;
2094         else if(fkey==F9KEY) sbuts->mainb= BUTS_EDIT;
2095         else if(fkey==F10KEY) sbuts->mainb= BUTS_RENDER;
2096
2097         scrarea_queue_headredraw(sa);
2098         scrarea_queue_winredraw(sa);
2099         BIF_preview_changed(sbuts);
2100 }
2101
2102 /* ******************** SPACE: SEQUENCE ********************** */
2103
2104 /*  extern void drawseqspace(ScrArea *sa, void *spacedata); BIF_drawseq.h */
2105
2106 void winqreadseqspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2107 {
2108         unsigned short event= evt->event;
2109         short val= evt->val;
2110         SpaceSeq *sseq= curarea->spacedata.first;
2111         View2D *v2d= &sseq->v2d;
2112         extern Sequence *last_seq;
2113         float dx, dy;
2114         int doredraw= 0, cfra, first;
2115         short mval[2];
2116         
2117         if(curarea->win==0) return;
2118
2119         if(val) {
2120                 
2121                 if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
2122
2123                 switch(event) {
2124                 case LEFTMOUSE:
2125                         if(sseq->mainb || view2dmove(event)==0) {
2126                                 
2127                                 first= 1;               
2128                                 set_special_seq_update(1);
2129
2130                                 do {
2131                                         getmouseco_areawin(mval);
2132                                         areamouseco_to_ipoco(v2d, mval, &dx, &dy);
2133                                         
2134                                         cfra= (int)dx;
2135                                         if(cfra< 1) cfra= 1;
2136                                         /* else if(cfra> EFRA) cfra= EFRA; */
2137                                         
2138                                         if( cfra!=CFRA || first ) {
2139                                                 first= 0;
2140                                 
2141                                                 CFRA= cfra;
2142                                                 force_draw();
2143                                                 update_for_newframe();  /* for audio scrubbing */                                               
2144                                         }
2145                                 
2146                                 } while(get_mbut()&L_MOUSE);
2147                                 
2148                                 set_special_seq_update(0);
2149                                 
2150                                 update_for_newframe();
2151                         }
2152                         break;
2153                 case MIDDLEMOUSE:
2154                 case WHEELUPMOUSE:
2155                 case WHEELDOWNMOUSE:
2156                         if(sseq->mainb) break;
2157                         view2dmove(event);      /* in drawipo.c */
2158                         break;
2159                 case RIGHTMOUSE:
2160                         if(sseq->mainb) break;
2161                         mouse_select_seq();
2162                         break;
2163                 case PADPLUSKEY:
2164                         if(sseq->mainb) {
2165                                 sseq->zoom++;
2166                                 if(sseq->zoom>8) sseq->zoom= 8;
2167                         }
2168                         else {
2169                                 if(G.qual) {
2170                                         if(G.qual & LR_SHIFTKEY) insert_gap(25, CFRA);
2171                                         else if(G.qual & LR_ALTKEY) insert_gap(250, CFRA);
2172                                         allqueue(REDRAWSEQ, 0);
2173                                 }
2174                                 else {
2175                                         dx= 0.1154*(v2d->cur.xmax-v2d->cur.xmin);
2176                                         v2d->cur.xmin+= dx;
2177                                         v2d->cur.xmax-= dx;
2178                                         test_view2d(G.v2d, curarea->winx, curarea->winy);
2179                                 }
2180                         }
2181                         doredraw= 1;
2182                         break;
2183                 case PADMINUS:
2184                         if(sseq->mainb) {
2185                                 sseq->zoom--;
2186                                 if(sseq->zoom<1) sseq->zoom= 1;
2187                         }
2188                         else {
2189                                 if(G.qual) {
2190                                         if(G.qual & LR_SHIFTKEY) no_gaps();
2191                                 }
2192                                 else {
2193                                         dx= 0.15*(v2d->cur.xmax-v2d->cur.xmin);
2194                                         v2d->cur.xmin-= dx;
2195                                         v2d->cur.xmax+= dx;
2196                                         test_view2d(G.v2d, curarea->winx, curarea->winy);
2197                                 }
2198                         }
2199                         doredraw= 1;
2200                         break;
2201                 case HOMEKEY:
2202                         do_seq_buttons(B_SEQHOME);
2203                         break;
2204                 case PADPERIOD: 
2205                         if(last_seq) {
2206                                 CFRA= last_seq->startdisp;
2207                                 v2d->cur.xmin= last_seq->startdisp- (last_seq->len/20);
2208                                 v2d->cur.xmax= last_seq->enddisp+ (last_seq->len/20);
2209                                 update_for_newframe();
2210                         }
2211                         break;
2212                         
2213                 case AKEY:
2214                         if(sseq->mainb) break;
2215                         if(G.qual & LR_SHIFTKEY) {
2216                                 add_sequence(0);
2217                         }
2218                         else swap_select_seq();
2219                         break;
2220                 case BKEY:
2221                         if(sseq->mainb) break;
2222                         borderselect_seq();
2223                         break;
2224                 case CKEY:
2225                         if(last_seq && (last_seq->flag & (SEQ_LEFTSEL+SEQ_RIGHTSEL))) {
2226                                 if(last_seq->flag & SEQ_LEFTSEL) CFRA= last_seq->startdisp;
2227                                 else CFRA= last_seq->enddisp-1;
2228                                 
2229                                 dx= CFRA-(v2d->cur.xmax+v2d->cur.xmin)/2;
2230                                 v2d->cur.xmax+= dx;
2231                                 v2d->cur.xmin+= dx;
2232                                 update_for_newframe();
2233                         }
2234                         else change_sequence();
2235                         break;
2236                 case DKEY:
2237                         if(sseq->mainb) break;
2238                         if(G.qual & LR_SHIFTKEY) add_duplicate_seq();
2239                         break;
2240                 case EKEY:
2241                         break;
2242                 case FKEY:
2243                         set_filter_seq();
2244                         break;
2245                 case GKEY:
2246                         if(sseq->mainb) break;
2247                         transform_seq('g');
2248                         break;
2249                 case MKEY:
2250                         if(G.qual & LR_ALTKEY) un_meta();
2251                         else {
2252                                 if ((last_seq) && (last_seq->type == SEQ_SOUND)) 
2253                                 {
2254                                         last_seq->flag ^= SEQ_MUTE;
2255                                         doredraw = 1;
2256                                 }
2257                                 else make_meta();
2258                         }
2259                         break;
2260                 case SKEY:
2261                         if(G.qual & LR_SHIFTKEY) seq_snapmenu();
2262                         break;
2263                 case TKEY:
2264                         touch_seq_files();
2265                         break;
2266                 case XKEY:
2267                 case DELKEY:
2268                         if(sseq->mainb) break;
2269                         del_seq();
2270                         break;
2271                 }
2272         }
2273
2274         if(doredraw) scrarea_queue_winredraw(curarea);
2275 }
2276
2277
2278 void init_seqspace(ScrArea *sa)
2279 {
2280         SpaceSeq *sseq;
2281         
2282         sseq= MEM_callocN(sizeof(SpaceSeq), "initseqspace");
2283         BLI_addhead(&sa->spacedata, sseq);
2284
2285         sseq->spacetype= SPACE_SEQ;
2286         sseq->zoom= 1;
2287         
2288         /* seq space goes from (0,8) to (250, 0) */
2289
2290         sseq->v2d.tot.xmin= 0.0;
2291         sseq->v2d.tot.ymin= 0.0;
2292         sseq->v2d.tot.xmax= 250.0;
2293         sseq->v2d.tot.ymax= 8.0;
2294         
2295         sseq->v2d.cur= sseq->v2d.tot;
2296
2297         sseq->v2d.min[0]= 10.0;
2298         sseq->v2d.min[1]= 4.0;
2299
2300         sseq->v2d.max[0]= 32000.0;
2301         sseq->v2d.max[1]= MAXSEQ;
2302         
2303         sseq->v2d.minzoom= 0.1;
2304         sseq->v2d.maxzoom= 10.0;
2305         
2306         sseq->v2d.scroll= L_SCROLL+B_SCROLL;
2307         sseq->v2d.keepaspect= 0;
2308         sseq->v2d.keepzoom= 0;
2309         sseq->v2d.keeptot= 0;
2310 }
2311
2312 /* ******************** SPACE: ACTION ********************** */
2313 extern void drawactionspace(ScrArea *sa, void *spacedata);
2314 extern void winqreadactionspace(struct ScrArea *sa, void *spacedata, struct BWinEvent *evt);
2315
2316 static void changeactionspace(ScrArea *sa, void *spacedata)
2317 {
2318         if(G.v2d==0) return;
2319
2320         /* action space uses weird matrices... local calculated in a function */
2321         
2322         // test_view2d(G.v2d, curarea->winx, curarea->winy);
2323         // myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
2324 }
2325
2326
2327 void init_actionspace(ScrArea *sa)
2328 {
2329         SpaceAction *saction;
2330         
2331         saction= MEM_callocN(sizeof(SpaceAction), "initactionspace");
2332         BLI_addhead(&sa->spacedata, saction);
2333
2334         saction->spacetype= SPACE_ACTION;
2335
2336         saction->v2d.tot.xmin= 1.0;
2337         saction->v2d.tot.ymin=  0.0;
2338         saction->v2d.tot.xmax= 1000.0;
2339         saction->v2d.tot.ymax= 1000.0;
2340         
2341         saction->v2d.cur.xmin= -5.0;
2342         saction->v2d.cur.ymin= 0.0;
2343         saction->v2d.cur.xmax= 65.0;
2344         saction->v2d.cur.ymax= 1000.0;
2345
2346
2347         saction->v2d.min[0]= 0.0;
2348         saction->v2d.min[1]= 0.0;
2349
2350         saction->v2d.max[0]= 1000.0;
2351         saction->v2d.max[1]= 1000.0;
2352         
2353         saction->v2d.minzoom= 0.1;
2354         saction->v2d.maxzoom= 10;
2355
2356         saction->v2d.scroll= R_SCROLL+B_SCROLL;
2357         saction->v2d.keepaspect= 0;
2358         saction->v2d.keepzoom= V2D_LOCKZOOM_Y;
2359         saction->v2d.keeptot= 0;
2360         
2361 }
2362
2363 void free_actionspace(SpaceAction *saction)
2364 {
2365         /* don't free saction itself */
2366         
2367         /* __PINFAKE */
2368 /*      if (saction->flag & SACTION_PIN)
2369                 if (saction->action)
2370                         saction->action->id.us --;
2371
2372 */      /* end PINFAKE */
2373 }
2374
2375
2376 /* ******************** SPACE: FILE ********************** */
2377
2378 void init_filespace(ScrArea *sa)
2379 {
2380         SpaceFile *sfile;
2381         
2382         sfile= MEM_callocN(sizeof(SpaceFile), "initfilespace");
2383         BLI_addhead(&sa->spacedata, sfile);
2384
2385         sfile->dir[0]= '/';
2386         sfile->type= FILE_UNIX;
2387
2388         sfile->spacetype= SPACE_FILE;
2389 }
2390
2391 void init_textspace(ScrArea *sa)
2392 {
2393         SpaceText *st;
2394         
2395         st= MEM_callocN(sizeof(SpaceText), "inittextspace");
2396         BLI_addhead(&sa->spacedata, st);
2397
2398         st->spacetype= SPACE_TEXT;      
2399         
2400         st->text= NULL;
2401         st->flags= 0;
2402         
2403         st->font_id= 5;
2404         st->lheight= 12;
2405         st->showlinenrs= 0;
2406         
2407         st->top= 0;
2408 }
2409
2410 void init_imaselspace(ScrArea *sa)
2411 {
2412         SpaceImaSel *simasel;
2413         
2414         simasel= MEM_callocN(sizeof(SpaceImaSel), "initimaselspace");
2415         BLI_addhead(&sa->spacedata, simasel);
2416
2417         simasel->spacetype= SPACE_IMASEL;
2418         
2419         simasel->mode = 7;
2420         strcpy (simasel->dir,  U.textudir);     /* TON */
2421         strcpy (simasel->file, "");
2422         strcpy(simasel->fole, simasel->file);
2423         strcpy(simasel->dor,  simasel->dir);
2424
2425         simasel->first_sel_ima  =  0;
2426         simasel->hilite_ima         =  0;
2427         simasel->firstdir               =  0;
2428         simasel->firstfile              =  0;
2429         simasel->cmap           =  0;
2430         simasel->returnfunc     =  0;
2431         
2432         simasel->title[0]       =  0;
2433         
2434         clear_ima_dir(simasel);
2435         
2436         // simasel->cmap= IMB_loadiffmem((int*)datatoc_cmap_tga, IB_rect|IB_cmap);
2437         simasel->cmap= IMB_ibImageFromMemory((int *)datatoc_cmap_tga, datatoc_cmap_tga_size, IB_rect|IB_cmap);
2438         if (!simasel->cmap) {
2439                 error("in console");
2440                 printf("Image select cmap file not found \n");
2441         }
2442 }
2443
2444 /* ******************** SPACE: SOUND ********************** */
2445
2446 extern void drawsoundspace(ScrArea *sa, void *spacedata);
2447 extern void winqreadsoundspace(struct ScrArea *sa, void *spacedata, struct BWinEvent *evt);
2448
2449 void init_soundspace(ScrArea *sa)
2450 {
2451         SpaceSound *ssound;
2452         
2453         ssound= MEM_callocN(sizeof(SpaceSound), "initsoundspace");
2454         BLI_addhead(&sa->spacedata, ssound);
2455
2456         ssound->spacetype= SPACE_SOUND;
2457         
2458         /* sound space goes from (0,8) to (250, 0) */
2459
2460         ssound->v2d.tot.xmin= -4.0;
2461         ssound->v2d.tot.ymin= -4.0;
2462         ssound->v2d.tot.xmax= 250.0;
2463         ssound->v2d.tot.ymax= 255.0;
2464         
2465         ssound->v2d.cur.xmin= -4.0;
2466         ssound->v2d.cur.ymin= -4.0;
2467         ssound->v2d.cur.xmax= 50.0;
2468         ssound->v2d.cur.ymax= 255.0;
2469
2470         ssound->v2d.min[0]= 1.0;
2471         ssound->v2d.min[1]= 259.0;
2472
2473         ssound->v2d.max[0]= 32000.0;
2474         ssound->v2d.max[1]= 259;
2475         
2476         ssound->v2d.minzoom= 0.1;
2477         ssound->v2d.maxzoom= 10.0;
2478         
2479         ssound->v2d.scroll= B_SCROLL;
2480         ssound->v2d.keepaspect= 0;
2481         ssound->v2d.keepzoom= 0;
2482         ssound->v2d.keeptot= 0;
2483         
2484 }
2485
2486 void free_soundspace(SpaceSound *ssound)
2487 {
2488         /* don't free ssound itself */
2489         
2490         
2491 }
2492
2493 /* ******************** SPACE: IMAGE ********************** */
2494
2495 /*  extern void drawimagespace(ScrArea *sa, void *spacedata); BIF_drawimage.h */
2496
2497 void winqreadimagespace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2498 {
2499         unsigned short event= evt->event;
2500         short val= evt->val;
2501         SpaceImage *sima= curarea->spacedata.first;
2502         View2D *v2d= &sima->v2d;
2503 #ifdef NAN_TPT
2504         IMG_BrushPtr brush;
2505         IMG_CanvasPtr canvas;
2506         int rowBytes;
2507         short xy_prev[2], xy_curr[2];
2508         float uv_prev[2], uv_curr[2];
2509         extern VPaint Gvp;
2510 #endif /* NAN_TPT */    
2511         if(val==0) return;
2512
2513         if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
2514         
2515         if (sima->flag & SI_DRAWTOOL) {
2516 #ifdef NAN_TPT
2517                 /* Draw tool is active */
2518                 switch(event) {
2519                         case LEFTMOUSE:
2520                                 /* Paranoia checks */
2521                                 if (!sima) break;
2522                                 if (!sima->image) break;
2523                                 if (!sima->image->ibuf) break;
2524                                 if (sima->image->packedfile) {
2525                                         error("Painting in packed images not supported");
2526                                         break;
2527                                 }
2528                         
2529                                 brush = IMG_BrushCreate(Gvp.size, Gvp.size, Gvp.r, Gvp.g, Gvp.b, Gvp.a);
2530                                 /* skipx is not set most of the times. Make a guess. */
2531                                 rowBytes = sima->image->ibuf->skipx ? sima->image->ibuf->skipx : sima->image->ibuf->x * 4;
2532                                 canvas = IMG_CanvasCreateFromPtr(sima->image->ibuf->rect, sima->image->ibuf->x, sima->image->ibuf->y, rowBytes);
2533
2534                                 getmouseco_areawin(xy_prev);
2535                                 while (get_mbut() & L_MOUSE) {
2536                                         getmouseco_areawin(xy_curr);
2537                                         /* Check if mouse position changed */
2538                                         if ((xy_prev[0] != xy_curr[0]) || (xy_prev[1] != xy_curr[1])) {
2539                                                 /* Convert mouse coordinates to u,v and draw */
2540                                                 areamouseco_to_ipoco(v2d, xy_prev, &uv_prev[0], &uv_prev[1]);
2541                                                 areamouseco_to_ipoco(v2d, xy_curr, &uv_curr[0], &uv_curr[1]);
2542                                                 IMG_CanvasDrawLineUV(canvas, brush, uv_prev[0], uv_prev[1], uv_curr[0], uv_curr[1]);
2543                                                 if (G.sima->lock) {
2544                                                         /* Make OpenGL aware of a changed texture */
2545                                                         free_realtime_image(sima->image);
2546                                                         /* Redraw this view and the 3D view */
2547                                                         force_draw_plus(SPACE_VIEW3D);
2548                                                 }
2549                                                 else {
2550                                                         /* Redraw only this view */
2551                                                         force_draw();
2552                                                 }
2553                                                 xy_prev[0] = xy_curr[0];
2554                                                 xy_prev[1] = xy_curr[1];
2555                                         }
2556                                 }
2557                                 /* Set the dirty bit in the image so that it is clear that it has been modified. */
2558                                 sima->image->ibuf->userflags |= IB_BITMAPDIRTY;
2559                                 if (!G.sima->lock) {
2560                                         /* Make OpenGL aware of a changed texture */
2561                                         free_realtime_image(sima->image);
2562                                         /* Redraw this view and the 3D view */
2563                                         force_draw_plus(SPACE_VIEW3D);
2564                                 }
2565                                 IMG_BrushDispose(brush);
2566                                 IMG_CanvasDispose(canvas);
2567                                 allqueue(REDRAWHEADERS, 0);
2568                                 break;
2569                 }
2570 #endif /* NAN_TPT */
2571         }
2572         else {
2573                 /* Draw tool is inactive */
2574                 switch(event) {
2575                         case LEFTMOUSE:
2576                                 if(G.qual & LR_SHIFTKEY) mouseco_to_curtile();
2577                                 else gesture();
2578                                 break;
2579                         case MIDDLEMOUSE:
2580                                 image_viewmove();
2581                                 break;
2582                         case RIGHTMOUSE:
2583                                 mouse_select_sima();
2584                                 break;
2585                         case AKEY:
2586                                 select_swap_tface_uv();
2587                                 break;
2588                         case BKEY:
2589                                 borderselect_sima();
2590                                 break;
2591                         case GKEY:
2592                                 transform_tface_uv('g');
2593                                 break;
2594                         case NKEY:
2595                                 if(G.qual & LR_CTRLKEY) replace_names_but();
2596                                 break;
2597                         case RKEY:
2598                                 transform_tface_uv('r');
2599                                 break;
2600                         case SKEY:
2601                                 transform_tface_uv('s');
2602                                 break;
2603                 }
2604         }
2605
2606         /* Events handled always (whether the draw tool is active or not) */
2607         switch (event) {
2608                 case MIDDLEMOUSE:
2609                         image_viewmove();
2610                         break;
2611                 case WHEELUPMOUSE:
2612                 case WHEELDOWNMOUSE:
2613                 case PADPLUSKEY:
2614                 case PADMINUS:
2615                         image_viewzoom(event);
2616                         scrarea_queue_winredraw(curarea);
2617                         break;
2618                 case HOMEKEY:
2619                         image_home();
2620                         break;
2621         }
2622 }
2623
2624
2625 void init_imagespace(ScrArea *sa)
2626 {
2627         SpaceImage *sima;
2628         
2629         sima= MEM_callocN(sizeof(SpaceImage), "initimaspace");
2630         BLI_addhead(&sa->spacedata, sima);
2631
2632         sima->spacetype= SPACE_IMAGE;
2633         sima->zoom= 1;
2634 }
2635
2636
2637 /* ******************** SPACE: IMASEL ********************** */
2638
2639 extern void drawimaselspace(ScrArea *sa, void *spacedata);
2640 extern void winqreadimaselspace(struct ScrArea *sa, void *spacedata, struct BWinEvent *evt);
2641
2642
2643 /* everything to imasel.c */
2644
2645
2646 /* ******************** SPACE: OOPS ********************** */
2647
2648 extern void drawoopsspace(ScrArea *sa, void *spacedata);
2649
2650 void winqreadoopsspace(ScrArea *sa, void *spacedata, BWinEvent *evt)
2651 {
2652         unsigned short event= evt->event;
2653         short val= evt->val;
2654         SpaceOops *soops= curarea->spacedata.first;
2655         View2D *v2d= &soops->v2d;
2656         float dx, dy;
2657
2658         if(val==0) return;
2659
2660         if( uiDoBlocks(&curarea->uiblocks, event)!=UI_NOTHING ) event= 0;
2661
2662         switch(event) {
2663         case LEFTMOUSE:
2664                 gesture();
2665                 break;
2666         case MIDDLEMOUSE:
2667         case WHEELUPMOUSE:
2668         case WHEELDOWNMOUSE:
2669                 view2dmove(event);      /* in drawipo.c */
2670                 break;
2671         case RIGHTMOUSE:
2672                 mouse_select_oops();
2673                 break;
2674         case PADPLUSKEY:
2675         
2676                 dx= 0.1154*(v2d->cur.xmax-v2d->cur.xmin);
2677                 dy= 0.1154*(v2d->cur.ymax-v2d->cur.ymin);
2678                 v2d->cur.xmin+= dx;
2679                 v2d->cur.xmax-= dx;
2680                 v2d->cur.ymin+= dy;
2681                 v2d->cur.ymax-= dy;
2682                 test_view2d(G.v2d, curarea->winx, curarea->winy);
2683                 scrarea_queue_winredraw(curarea);
2684                 break;
2685         
2686         case PADMINUS:
2687
2688                 dx= 0.15*(v2d->cur.xmax-v2d->cur.xmin);
2689                 dy= 0.15*(v2d->cur.ymax-v2d->cur.ymin);
2690                 v2d->cur.xmin-= dx;
2691                 v2d->cur.xmax+= dx;
2692                 v2d->cur.ymin-= dy;
2693                 v2d->cur.ymax+= dy;
2694                 test_view2d(G.v2d, curarea->winx, curarea->winy);
2695                 scrarea_queue_winredraw(curarea);
2696                 break;
2697                 
2698         case HOMEKEY:   
2699                 do_oops_buttons(B_OOPSHOME);
2700                 break;
2701                 
2702         case AKEY:
2703                 swap_select_all_oops();
2704                 scrarea_queue_winredraw(curarea);
2705                 break;
2706         case BKEY:
2707                 borderselect_oops();
2708                 break;
2709         case GKEY:
2710                 transform_oops('g');
2711                 break;
2712         case LKEY:
2713                 if(G.qual & LR_SHIFTKEY) select_backlinked_oops();
2714                 else select_linked_oops();
2715                 break;
2716         case SKEY:
2717                 
2718                 if(G.qual & LR_ALTKEY) shrink_oops();
2719                 else if(G.qual & LR_SHIFTKEY) shuffle_oops();
2720                 else transform_oops('s');
2721                 break;
2722
2723         case ONEKEY:
2724                 do_layer_buttons(0); break;
2725         case TWOKEY:
2726                 do_layer_buttons(1); break;
2727         case THREEKEY:
2728                 do_layer_buttons(2); break;
2729         case FOURKEY:
2730                 do_layer_buttons(3); break;
2731         case FIVEKEY:
2732                 do_layer_buttons(4); break;
2733         case SIXKEY:
2734                 do_layer_buttons(5); break;
2735         case SEVENKEY:
2736                 do_layer_buttons(6); break;
2737         case EIGHTKEY:
2738                 do_layer_buttons(7); break;
2739         case NINEKEY:
2740                 do_layer_buttons(8); break;
2741         case ZEROKEY:
2742                 do_layer_buttons(9); break;
2743         case MINUSKEY:
2744                 do_layer_buttons(10); break;
2745         case EQUALKEY:
2746                 do_layer_buttons(11); break;
2747         case ACCENTGRAVEKEY:
2748                 do_layer_buttons(-1); break;
2749         
2750         }
2751 }
2752
2753 void init_v2d_oops(View2D *v2d)
2754 {
2755         v2d->tot.xmin= -28.0;
2756         v2d->tot.xmax= 28.0;
2757         v2d->tot.ymin= -28.0;
2758         v2d->tot.ymax= 28.0;
2759         
2760         v2d->cur= v2d->tot;
2761
2762         v2d->min[0]= 10.0;
2763         v2d->min[1]= 4.0;
2764
2765         v2d->max[0]= 320.0;
2766         v2d->max[1]= 320.0;
2767         
2768         v2d->minzoom= 0.01;
2769         v2d->maxzoom= 2.0;
2770         
2771         /* v2d->scroll= L_SCROLL+B_SCROLL; */
2772         v2d->scroll= 0;
2773         v2d->keepaspect= 1;
2774         v2d->keepzoom= 0;
2775         v2d->keeptot= 0;
2776         
2777 }
2778
2779 void init_oopsspace(ScrArea *sa)
2780 {
2781         SpaceOops *soops;
2782         
2783         soops= MEM_callocN(sizeof(SpaceOops), "initoopsspace");
2784         BLI_addhead(&sa->spacedata, soops);
2785
2786         soops->visiflag= OOPS_OB+OOPS_MA+OOPS_ME+OOPS_TE+OOPS_CU+OOPS_IP;
2787         
2788         soops->spacetype= SPACE_OOPS;
2789         init_v2d_oops(&soops->v2d);
2790 }
2791
2792 /* ******************** SPACE: PAINT ********************** */
2793
2794
2795 /* ******************** SPACE: Text ********************** */
2796
2797 extern void drawtextspace(ScrArea *sa, void *spacedata);
2798 extern void winqreadtextspace(struct ScrArea *sa, void *spacedata, struct BWinEvent *evt);
2799
2800 /* ******************** SPACE: ALGEMEEN ********************** */
2801
2802 void newspace(ScrArea *sa, int type)
2803 {
2804         if(type>=0) {
2805                 if(sa->spacetype != type) {
2806                         SpaceLink *sl;
2807                         
2808                         sa->spacetype= type;
2809                         sa->headbutofs= 0;
2810                         
2811                         uiFreeBlocks(&sa->uiblocks);
2812                         wich_cursor(sa);
2813                         
2814                         if (sa->headwin) addqueue(sa->headwin, CHANGED, 1);
2815                         scrarea_queue_headredraw(sa);
2816
2817                         addqueue(sa->win, CHANGED, 1);
2818                         scrarea_queue_winredraw(sa);
2819
2820                         areawinset(sa->win);
2821
2822                         bwin_clear_viewmat(sa->win);
2823                         
2824                         for (sl= sa->spacedata.first; sl; sl= sl->next)
2825                                 if(sl->spacetype==type)
2826                                         break;
2827
2828                         if (sl) {                       
2829                                 BLI_remlink(&sa->spacedata, sl);
2830                                 BLI_addhead(&sa->spacedata, sl);
2831                         } else {
2832                                 if(type==SPACE_VIEW3D)
2833                                         initview3d(sa);
2834                                 else if(type==SPACE_IPO)
2835                                         initipo(sa);
2836                                 else if(type==SPACE_INFO)
2837                                         init_infospace(sa);
2838                                 else if(type==SPACE_BUTS)
2839                                         init_butspace(sa);
2840                                 else if(type==SPACE_FILE)
2841                                         init_filespace(sa);
2842                                 else if(type==SPACE_SEQ)
2843                                         init_seqspace(sa);
2844                                 else if(type==SPACE_IMAGE)
2845                                         init_imagespace(sa);
2846                                 else if(type==SPACE_IMASEL)
2847                                         init_imaselspace(sa);
2848                                 else if(type==SPACE_OOPS)
2849                                         init_oopsspace(sa);
2850                                 else if(type==SPACE_ACTION)
2851                                         init_actionspace(sa);
2852                                 else if(type==SPACE_TEXT)
2853                                         init_textspace(sa);
2854                                 else if(type==SPACE_SOUND)
2855                                         init_soundspace(sa);
2856                                 else if(type==SPACE_NLA)
2857                                         init_nlaspace(sa);
2858
2859                                 sl= sa->spacedata.first;
2860                                 sl->area= sa;
2861                         }
2862                 }
2863         }
2864
2865                 
2866         /* exception: filespace */
2867         if(curarea->spacetype==SPACE_FILE) {
2868                 SpaceFile *sfile= curarea->spacedata.first;
2869                 
2870                 if(sfile->type==FILE_MAIN) {
2871                         freefilelist(sfile);
2872                 } else {
2873                         sfile->type= FILE_UNIX;
2874                 }
2875                 
2876                 sfile->returnfunc= 0;
2877                 sfile->title[0]= 0;
2878                 if(sfile->filelist) test_flags_file(sfile);
2879         }
2880         /* exception: imasel space */
2881         else if(curarea->spacetype==SPACE_IMASEL) {
2882                 SpaceImaSel *simasel= curarea->spacedata.first;
2883                 simasel->returnfunc= 0;
2884                 simasel->title[0]= 0;
2885         }
2886 }
2887
2888 void freespacelist(ListBase *lb)
2889 {
2890         SpaceLink *sl;
2891
2892         for (sl= lb->first; sl; sl= sl->next) {
2893                 if(sl->spacetype==SPACE_FILE) {
2894                         SpaceFile *sfile= (SpaceFile*) sl;
2895                         if(sfile->libfiledata)  
2896                                 BLO_blendhandle_close(sfile->libfiledata);
2897                 }
2898                 else if(sl->spacetype==SPACE_BUTS) {
2899                         SpaceButs *buts= (SpaceButs*) sl;
2900                         if(buts->rect) MEM_freeN(buts->rect);
2901                         if(G.buts==buts) G.buts= 0;
2902                 }
2903                 else if(sl->spacetype==SPACE_IPO) {
2904                         SpaceIpo *si= (SpaceIpo*) sl;
2905                         if(si->editipo) MEM_freeN(si->editipo);
2906                         free_ipokey(&si->ipokey);
2907                         if(G.sipo==si) G.sipo= 0;
2908                 }
2909                 else if(sl->spacetype==SPACE_VIEW3D) {
2910                         View3D *vd= (View3D*) sl;
2911                         if(vd->bgpic) {
2912                                 if(vd->bgpic->rect) MEM_freeN(vd->bgpic->rect);
2913                                 if(vd->bgpic->ima) vd->bgpic->ima->id.us--;
2914                                 MEM_freeN(vd->bgpic);
2915                         }